2012-11-28 Oleg Raikhman <oleg@adapteva.com>
[official-gcc.git] / gcc / cp / semantics.c
blobeaf706968e4b2ff005207c4c92b90aa2356ba593
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 vector 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 vector. `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 vector 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, va_gc> * GTY(()) deferred_access_checks;
125 /* The current mode of access checks. */
126 enum deferring_kind deferring_access_checks_kind;
128 } deferred_access;
130 /* Data for deferred access checking. */
131 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
132 static GTY(()) unsigned deferred_access_no_check;
134 /* Save the current deferred access states and start deferred
135 access checking iff DEFER_P is true. */
137 void
138 push_deferring_access_checks (deferring_kind deferring)
140 /* For context like template instantiation, access checking
141 disabling applies to all nested context. */
142 if (deferred_access_no_check || deferring == dk_no_check)
143 deferred_access_no_check++;
144 else
146 deferred_access e = {NULL, deferring};
147 vec_safe_push (deferred_access_stack, e);
151 /* Resume deferring access checks again after we stopped doing
152 this previously. */
154 void
155 resume_deferring_access_checks (void)
157 if (!deferred_access_no_check)
158 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
161 /* Stop deferring access checks. */
163 void
164 stop_deferring_access_checks (void)
166 if (!deferred_access_no_check)
167 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
170 /* Discard the current deferred access checks and restore the
171 previous states. */
173 void
174 pop_deferring_access_checks (void)
176 if (deferred_access_no_check)
177 deferred_access_no_check--;
178 else
179 deferred_access_stack->pop ();
182 /* Returns a TREE_LIST representing the deferred checks.
183 The TREE_PURPOSE of each node is the type through which the
184 access occurred; the TREE_VALUE is the declaration named.
187 vec<deferred_access_check, va_gc> *
188 get_deferred_access_checks (void)
190 if (deferred_access_no_check)
191 return NULL;
192 else
193 return (deferred_access_stack->last().deferred_access_checks);
196 /* Take current deferred checks and combine with the
197 previous states if we also defer checks previously.
198 Otherwise perform checks now. */
200 void
201 pop_to_parent_deferring_access_checks (void)
203 if (deferred_access_no_check)
204 deferred_access_no_check--;
205 else
207 vec<deferred_access_check, va_gc> *checks;
208 deferred_access *ptr;
210 checks = (deferred_access_stack->last ().deferred_access_checks);
212 deferred_access_stack->pop ();
213 ptr = &deferred_access_stack->last ();
214 if (ptr->deferring_access_checks_kind == dk_no_deferred)
216 /* Check access. */
217 perform_access_checks (checks, tf_warning_or_error);
219 else
221 /* Merge with parent. */
222 int i, j;
223 deferred_access_check *chk, *probe;
225 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
227 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
229 if (probe->binfo == chk->binfo &&
230 probe->decl == chk->decl &&
231 probe->diag_decl == chk->diag_decl)
232 goto found;
234 /* Insert into parent's checks. */
235 vec_safe_push (ptr->deferred_access_checks, *chk);
236 found:;
242 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
243 is the BINFO indicating the qualifying scope used to access the
244 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
245 or we aren't in SFINAE context or all the checks succeed return TRUE,
246 otherwise FALSE. */
248 bool
249 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
250 tsubst_flags_t complain)
252 int i;
253 deferred_access_check *chk;
254 location_t loc = input_location;
255 bool ok = true;
257 if (!checks)
258 return true;
260 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
262 input_location = chk->loc;
263 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
266 input_location = loc;
267 return (complain & tf_error) ? true : ok;
270 /* Perform the deferred access checks.
272 After performing the checks, we still have to keep the list
273 `deferred_access_stack->deferred_access_checks' since we may want
274 to check access for them again later in a different context.
275 For example:
277 class A {
278 typedef int X;
279 static X a;
281 A::X A::a, x; // No error for `A::a', error for `x'
283 We have to perform deferred access of `A::X', first with `A::a',
284 next with `x'. Return value like perform_access_checks above. */
286 bool
287 perform_deferred_access_checks (tsubst_flags_t complain)
289 return perform_access_checks (get_deferred_access_checks (), complain);
292 /* Defer checking the accessibility of DECL, when looked up in
293 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
294 Return value like perform_access_checks above. */
296 bool
297 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
298 tsubst_flags_t complain)
300 int i;
301 deferred_access *ptr;
302 deferred_access_check *chk;
305 /* Exit if we are in a context that no access checking is performed.
307 if (deferred_access_no_check)
308 return true;
310 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
312 ptr = &deferred_access_stack->last ();
314 /* If we are not supposed to defer access checks, just check now. */
315 if (ptr->deferring_access_checks_kind == dk_no_deferred)
317 bool ok = enforce_access (binfo, decl, diag_decl, complain);
318 return (complain & tf_error) ? true : ok;
321 /* See if we are already going to perform this check. */
322 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
324 if (chk->decl == decl && chk->binfo == binfo &&
325 chk->diag_decl == diag_decl)
327 return true;
330 /* If not, record the check. */
331 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
332 vec_safe_push (ptr->deferred_access_checks, new_access);
334 return true;
337 /* Returns nonzero if the current statement is a full expression,
338 i.e. temporaries created during that statement should be destroyed
339 at the end of the statement. */
342 stmts_are_full_exprs_p (void)
344 return current_stmt_tree ()->stmts_are_full_exprs_p;
347 /* T is a statement. Add it to the statement-tree. This is the C++
348 version. The C/ObjC frontends have a slightly different version of
349 this function. */
351 tree
352 add_stmt (tree t)
354 enum tree_code code = TREE_CODE (t);
356 if (EXPR_P (t) && code != LABEL_EXPR)
358 if (!EXPR_HAS_LOCATION (t))
359 SET_EXPR_LOCATION (t, input_location);
361 /* When we expand a statement-tree, we must know whether or not the
362 statements are full-expressions. We record that fact here. */
363 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
366 /* Add T to the statement-tree. Non-side-effect statements need to be
367 recorded during statement expressions. */
368 gcc_checking_assert (!stmt_list_stack->is_empty ());
369 append_to_statement_list_force (t, &cur_stmt_list);
371 return t;
374 /* Returns the stmt_tree to which statements are currently being added. */
376 stmt_tree
377 current_stmt_tree (void)
379 return (cfun
380 ? &cfun->language->base.x_stmt_tree
381 : &scope_chain->x_stmt_tree);
384 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
386 static tree
387 maybe_cleanup_point_expr (tree expr)
389 if (!processing_template_decl && stmts_are_full_exprs_p ())
390 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
391 return expr;
394 /* Like maybe_cleanup_point_expr except have the type of the new expression be
395 void so we don't need to create a temporary variable to hold the inner
396 expression. The reason why we do this is because the original type might be
397 an aggregate and we cannot create a temporary variable for that type. */
399 tree
400 maybe_cleanup_point_expr_void (tree expr)
402 if (!processing_template_decl && stmts_are_full_exprs_p ())
403 expr = fold_build_cleanup_point_expr (void_type_node, expr);
404 return expr;
409 /* Create a declaration statement for the declaration given by the DECL. */
411 void
412 add_decl_expr (tree decl)
414 tree r = build_stmt (input_location, DECL_EXPR, decl);
415 if (DECL_INITIAL (decl)
416 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
417 r = maybe_cleanup_point_expr_void (r);
418 add_stmt (r);
421 /* Finish a scope. */
423 tree
424 do_poplevel (tree stmt_list)
426 tree block = NULL;
428 if (stmts_are_full_exprs_p ())
429 block = poplevel (kept_level_p (), 1, 0);
431 stmt_list = pop_stmt_list (stmt_list);
433 if (!processing_template_decl)
435 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
436 /* ??? See c_end_compound_stmt re statement expressions. */
439 return stmt_list;
442 /* Begin a new scope. */
444 static tree
445 do_pushlevel (scope_kind sk)
447 tree ret = push_stmt_list ();
448 if (stmts_are_full_exprs_p ())
449 begin_scope (sk, NULL);
450 return ret;
453 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
454 when the current scope is exited. EH_ONLY is true when this is not
455 meant to apply to normal control flow transfer. */
457 void
458 push_cleanup (tree decl, tree cleanup, bool eh_only)
460 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
461 CLEANUP_EH_ONLY (stmt) = eh_only;
462 add_stmt (stmt);
463 CLEANUP_BODY (stmt) = push_stmt_list ();
466 /* Begin a conditional that might contain a declaration. When generating
467 normal code, we want the declaration to appear before the statement
468 containing the conditional. When generating template code, we want the
469 conditional to be rendered as the raw DECL_EXPR. */
471 static void
472 begin_cond (tree *cond_p)
474 if (processing_template_decl)
475 *cond_p = push_stmt_list ();
478 /* Finish such a conditional. */
480 static void
481 finish_cond (tree *cond_p, tree expr)
483 if (processing_template_decl)
485 tree cond = pop_stmt_list (*cond_p);
487 if (expr == NULL_TREE)
488 /* Empty condition in 'for'. */
489 gcc_assert (empty_expr_stmt_p (cond));
490 else if (check_for_bare_parameter_packs (expr))
491 expr = error_mark_node;
492 else if (!empty_expr_stmt_p (cond))
493 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
495 *cond_p = expr;
498 /* If *COND_P specifies a conditional with a declaration, transform the
499 loop such that
500 while (A x = 42) { }
501 for (; A x = 42;) { }
502 becomes
503 while (true) { A x = 42; if (!x) break; }
504 for (;;) { A x = 42; if (!x) break; }
505 The statement list for BODY will be empty if the conditional did
506 not declare anything. */
508 static void
509 simplify_loop_decl_cond (tree *cond_p, tree body)
511 tree cond, if_stmt;
513 if (!TREE_SIDE_EFFECTS (body))
514 return;
516 cond = *cond_p;
517 *cond_p = boolean_true_node;
519 if_stmt = begin_if_stmt ();
520 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
521 finish_if_stmt_cond (cond, if_stmt);
522 finish_break_stmt ();
523 finish_then_clause (if_stmt);
524 finish_if_stmt (if_stmt);
527 /* Finish a goto-statement. */
529 tree
530 finish_goto_stmt (tree destination)
532 if (TREE_CODE (destination) == IDENTIFIER_NODE)
533 destination = lookup_label (destination);
535 /* We warn about unused labels with -Wunused. That means we have to
536 mark the used labels as used. */
537 if (TREE_CODE (destination) == LABEL_DECL)
538 TREE_USED (destination) = 1;
539 else
541 destination = mark_rvalue_use (destination);
542 if (!processing_template_decl)
544 destination = cp_convert (ptr_type_node, destination,
545 tf_warning_or_error);
546 if (error_operand_p (destination))
547 return NULL_TREE;
548 destination
549 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
550 destination);
554 check_goto (destination);
556 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
559 /* COND is the condition-expression for an if, while, etc.,
560 statement. Convert it to a boolean value, if appropriate.
561 In addition, verify sequence points if -Wsequence-point is enabled. */
563 static tree
564 maybe_convert_cond (tree cond)
566 /* Empty conditions remain empty. */
567 if (!cond)
568 return NULL_TREE;
570 /* Wait until we instantiate templates before doing conversion. */
571 if (processing_template_decl)
572 return cond;
574 if (warn_sequence_point)
575 verify_sequence_points (cond);
577 /* Do the conversion. */
578 cond = convert_from_reference (cond);
580 if (TREE_CODE (cond) == MODIFY_EXPR
581 && !TREE_NO_WARNING (cond)
582 && warn_parentheses)
584 warning (OPT_Wparentheses,
585 "suggest parentheses around assignment used as truth value");
586 TREE_NO_WARNING (cond) = 1;
589 return condition_conversion (cond);
592 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
594 tree
595 finish_expr_stmt (tree expr)
597 tree r = NULL_TREE;
599 if (expr != NULL_TREE)
601 if (!processing_template_decl)
603 if (warn_sequence_point)
604 verify_sequence_points (expr);
605 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
607 else if (!type_dependent_expression_p (expr))
608 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
609 tf_warning_or_error);
611 if (check_for_bare_parameter_packs (expr))
612 expr = error_mark_node;
614 /* Simplification of inner statement expressions, compound exprs,
615 etc can result in us already having an EXPR_STMT. */
616 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
618 if (TREE_CODE (expr) != EXPR_STMT)
619 expr = build_stmt (input_location, EXPR_STMT, expr);
620 expr = maybe_cleanup_point_expr_void (expr);
623 r = add_stmt (expr);
626 finish_stmt ();
628 return r;
632 /* Begin an if-statement. Returns a newly created IF_STMT if
633 appropriate. */
635 tree
636 begin_if_stmt (void)
638 tree r, scope;
639 scope = do_pushlevel (sk_cond);
640 r = build_stmt (input_location, IF_STMT, NULL_TREE,
641 NULL_TREE, NULL_TREE, scope);
642 begin_cond (&IF_COND (r));
643 return r;
646 /* Process the COND of an if-statement, which may be given by
647 IF_STMT. */
649 void
650 finish_if_stmt_cond (tree cond, tree if_stmt)
652 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
653 add_stmt (if_stmt);
654 THEN_CLAUSE (if_stmt) = push_stmt_list ();
657 /* Finish the then-clause of an if-statement, which may be given by
658 IF_STMT. */
660 tree
661 finish_then_clause (tree if_stmt)
663 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
664 return if_stmt;
667 /* Begin the else-clause of an if-statement. */
669 void
670 begin_else_clause (tree if_stmt)
672 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
675 /* Finish the else-clause of an if-statement, which may be given by
676 IF_STMT. */
678 void
679 finish_else_clause (tree if_stmt)
681 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
684 /* Finish an if-statement. */
686 void
687 finish_if_stmt (tree if_stmt)
689 tree scope = IF_SCOPE (if_stmt);
690 IF_SCOPE (if_stmt) = NULL;
691 add_stmt (do_poplevel (scope));
692 finish_stmt ();
695 /* Begin a while-statement. Returns a newly created WHILE_STMT if
696 appropriate. */
698 tree
699 begin_while_stmt (void)
701 tree r;
702 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
703 add_stmt (r);
704 WHILE_BODY (r) = do_pushlevel (sk_block);
705 begin_cond (&WHILE_COND (r));
706 return r;
709 /* Process the COND of a while-statement, which may be given by
710 WHILE_STMT. */
712 void
713 finish_while_stmt_cond (tree cond, tree while_stmt)
715 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
716 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
719 /* Finish a while-statement, which may be given by WHILE_STMT. */
721 void
722 finish_while_stmt (tree while_stmt)
724 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
725 finish_stmt ();
728 /* Begin a do-statement. Returns a newly created DO_STMT if
729 appropriate. */
731 tree
732 begin_do_stmt (void)
734 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
735 add_stmt (r);
736 DO_BODY (r) = push_stmt_list ();
737 return r;
740 /* Finish the body of a do-statement, which may be given by DO_STMT. */
742 void
743 finish_do_body (tree do_stmt)
745 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
747 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
748 body = STATEMENT_LIST_TAIL (body)->stmt;
750 if (IS_EMPTY_STMT (body))
751 warning (OPT_Wempty_body,
752 "suggest explicit braces around empty body in %<do%> statement");
755 /* Finish a do-statement, which may be given by DO_STMT, and whose
756 COND is as indicated. */
758 void
759 finish_do_stmt (tree cond, tree do_stmt)
761 cond = maybe_convert_cond (cond);
762 DO_COND (do_stmt) = cond;
763 finish_stmt ();
766 /* Finish a return-statement. The EXPRESSION returned, if any, is as
767 indicated. */
769 tree
770 finish_return_stmt (tree expr)
772 tree r;
773 bool no_warning;
775 expr = check_return_expr (expr, &no_warning);
777 if (flag_openmp && !check_omp_return ())
778 return error_mark_node;
779 if (!processing_template_decl)
781 if (warn_sequence_point)
782 verify_sequence_points (expr);
784 if (DECL_DESTRUCTOR_P (current_function_decl)
785 || (DECL_CONSTRUCTOR_P (current_function_decl)
786 && targetm.cxx.cdtor_returns_this ()))
788 /* Similarly, all destructors must run destructors for
789 base-classes before returning. So, all returns in a
790 destructor get sent to the DTOR_LABEL; finish_function emits
791 code to return a value there. */
792 return finish_goto_stmt (cdtor_label);
796 r = build_stmt (input_location, RETURN_EXPR, expr);
797 TREE_NO_WARNING (r) |= no_warning;
798 r = maybe_cleanup_point_expr_void (r);
799 r = add_stmt (r);
800 finish_stmt ();
802 return r;
805 /* Begin the scope of a for-statement or a range-for-statement.
806 Both the returned trees are to be used in a call to
807 begin_for_stmt or begin_range_for_stmt. */
809 tree
810 begin_for_scope (tree *init)
812 tree scope = NULL_TREE;
813 if (flag_new_for_scope > 0)
814 scope = do_pushlevel (sk_for);
816 if (processing_template_decl)
817 *init = push_stmt_list ();
818 else
819 *init = NULL_TREE;
821 return scope;
824 /* Begin a for-statement. Returns a new FOR_STMT.
825 SCOPE and INIT should be the return of begin_for_scope,
826 or both NULL_TREE */
828 tree
829 begin_for_stmt (tree scope, tree init)
831 tree r;
833 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
834 NULL_TREE, NULL_TREE, NULL_TREE);
836 if (scope == NULL_TREE)
838 gcc_assert (!init || !(flag_new_for_scope > 0));
839 if (!init)
840 scope = begin_for_scope (&init);
842 FOR_INIT_STMT (r) = init;
843 FOR_SCOPE (r) = scope;
845 return r;
848 /* Finish the for-init-statement of a for-statement, which may be
849 given by FOR_STMT. */
851 void
852 finish_for_init_stmt (tree for_stmt)
854 if (processing_template_decl)
855 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
856 add_stmt (for_stmt);
857 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
858 begin_cond (&FOR_COND (for_stmt));
861 /* Finish the COND of a for-statement, which may be given by
862 FOR_STMT. */
864 void
865 finish_for_cond (tree cond, tree for_stmt)
867 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
868 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
871 /* Finish the increment-EXPRESSION in a for-statement, which may be
872 given by FOR_STMT. */
874 void
875 finish_for_expr (tree expr, tree for_stmt)
877 if (!expr)
878 return;
879 /* If EXPR is an overloaded function, issue an error; there is no
880 context available to use to perform overload resolution. */
881 if (type_unknown_p (expr))
883 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
884 expr = error_mark_node;
886 if (!processing_template_decl)
888 if (warn_sequence_point)
889 verify_sequence_points (expr);
890 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
891 tf_warning_or_error);
893 else if (!type_dependent_expression_p (expr))
894 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
895 tf_warning_or_error);
896 expr = maybe_cleanup_point_expr_void (expr);
897 if (check_for_bare_parameter_packs (expr))
898 expr = error_mark_node;
899 FOR_EXPR (for_stmt) = expr;
902 /* Finish the body of a for-statement, which may be given by
903 FOR_STMT. The increment-EXPR for the loop must be
904 provided.
905 It can also finish RANGE_FOR_STMT. */
907 void
908 finish_for_stmt (tree for_stmt)
910 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
911 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
912 else
913 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
915 /* Pop the scope for the body of the loop. */
916 if (flag_new_for_scope > 0)
918 tree scope;
919 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
920 ? &RANGE_FOR_SCOPE (for_stmt)
921 : &FOR_SCOPE (for_stmt));
922 scope = *scope_ptr;
923 *scope_ptr = NULL;
924 add_stmt (do_poplevel (scope));
927 finish_stmt ();
930 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
931 SCOPE and INIT should be the return of begin_for_scope,
932 or both NULL_TREE .
933 To finish it call finish_for_stmt(). */
935 tree
936 begin_range_for_stmt (tree scope, tree init)
938 tree r;
940 r = build_stmt (input_location, RANGE_FOR_STMT,
941 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
943 if (scope == NULL_TREE)
945 gcc_assert (!init || !(flag_new_for_scope > 0));
946 if (!init)
947 scope = begin_for_scope (&init);
950 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
951 pop it now. */
952 if (init)
953 pop_stmt_list (init);
954 RANGE_FOR_SCOPE (r) = scope;
956 return r;
959 /* Finish the head of a range-based for statement, which may
960 be given by RANGE_FOR_STMT. DECL must be the declaration
961 and EXPR must be the loop expression. */
963 void
964 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
966 RANGE_FOR_DECL (range_for_stmt) = decl;
967 RANGE_FOR_EXPR (range_for_stmt) = expr;
968 add_stmt (range_for_stmt);
969 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
972 /* Finish a break-statement. */
974 tree
975 finish_break_stmt (void)
977 /* In switch statements break is sometimes stylistically used after
978 a return statement. This can lead to spurious warnings about
979 control reaching the end of a non-void function when it is
980 inlined. Note that we are calling block_may_fallthru with
981 language specific tree nodes; this works because
982 block_may_fallthru returns true when given something it does not
983 understand. */
984 if (!block_may_fallthru (cur_stmt_list))
985 return void_zero_node;
986 return add_stmt (build_stmt (input_location, BREAK_STMT));
989 /* Finish a continue-statement. */
991 tree
992 finish_continue_stmt (void)
994 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
997 /* Begin a switch-statement. Returns a new SWITCH_STMT if
998 appropriate. */
1000 tree
1001 begin_switch_stmt (void)
1003 tree r, scope;
1005 scope = do_pushlevel (sk_cond);
1006 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1008 begin_cond (&SWITCH_STMT_COND (r));
1010 return r;
1013 /* Finish the cond of a switch-statement. */
1015 void
1016 finish_switch_cond (tree cond, tree switch_stmt)
1018 tree orig_type = NULL;
1019 if (!processing_template_decl)
1021 /* Convert the condition to an integer or enumeration type. */
1022 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1023 if (cond == NULL_TREE)
1025 error ("switch quantity not an integer");
1026 cond = error_mark_node;
1028 orig_type = TREE_TYPE (cond);
1029 if (cond != error_mark_node)
1031 /* [stmt.switch]
1033 Integral promotions are performed. */
1034 cond = perform_integral_promotions (cond);
1035 cond = maybe_cleanup_point_expr (cond);
1038 if (check_for_bare_parameter_packs (cond))
1039 cond = error_mark_node;
1040 else if (!processing_template_decl && warn_sequence_point)
1041 verify_sequence_points (cond);
1043 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1044 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1045 add_stmt (switch_stmt);
1046 push_switch (switch_stmt);
1047 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1050 /* Finish the body of a switch-statement, which may be given by
1051 SWITCH_STMT. The COND to switch on is indicated. */
1053 void
1054 finish_switch_stmt (tree switch_stmt)
1056 tree scope;
1058 SWITCH_STMT_BODY (switch_stmt) =
1059 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1060 pop_switch ();
1061 finish_stmt ();
1063 scope = SWITCH_STMT_SCOPE (switch_stmt);
1064 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1065 add_stmt (do_poplevel (scope));
1068 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1069 appropriate. */
1071 tree
1072 begin_try_block (void)
1074 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1075 add_stmt (r);
1076 TRY_STMTS (r) = push_stmt_list ();
1077 return r;
1080 /* Likewise, for a function-try-block. The block returned in
1081 *COMPOUND_STMT is an artificial outer scope, containing the
1082 function-try-block. */
1084 tree
1085 begin_function_try_block (tree *compound_stmt)
1087 tree r;
1088 /* This outer scope does not exist in the C++ standard, but we need
1089 a place to put __FUNCTION__ and similar variables. */
1090 *compound_stmt = begin_compound_stmt (0);
1091 r = begin_try_block ();
1092 FN_TRY_BLOCK_P (r) = 1;
1093 return r;
1096 /* Finish a try-block, which may be given by TRY_BLOCK. */
1098 void
1099 finish_try_block (tree try_block)
1101 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1102 TRY_HANDLERS (try_block) = push_stmt_list ();
1105 /* Finish the body of a cleanup try-block, which may be given by
1106 TRY_BLOCK. */
1108 void
1109 finish_cleanup_try_block (tree try_block)
1111 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1114 /* Finish an implicitly generated try-block, with a cleanup is given
1115 by CLEANUP. */
1117 void
1118 finish_cleanup (tree cleanup, tree try_block)
1120 TRY_HANDLERS (try_block) = cleanup;
1121 CLEANUP_P (try_block) = 1;
1124 /* Likewise, for a function-try-block. */
1126 void
1127 finish_function_try_block (tree try_block)
1129 finish_try_block (try_block);
1130 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1131 the try block, but moving it inside. */
1132 in_function_try_handler = 1;
1135 /* Finish a handler-sequence for a try-block, which may be given by
1136 TRY_BLOCK. */
1138 void
1139 finish_handler_sequence (tree try_block)
1141 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1142 check_handlers (TRY_HANDLERS (try_block));
1145 /* Finish the handler-seq for a function-try-block, given by
1146 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1147 begin_function_try_block. */
1149 void
1150 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1152 in_function_try_handler = 0;
1153 finish_handler_sequence (try_block);
1154 finish_compound_stmt (compound_stmt);
1157 /* Begin a handler. Returns a HANDLER if appropriate. */
1159 tree
1160 begin_handler (void)
1162 tree r;
1164 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1165 add_stmt (r);
1167 /* Create a binding level for the eh_info and the exception object
1168 cleanup. */
1169 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1171 return r;
1174 /* Finish the handler-parameters for a handler, which may be given by
1175 HANDLER. DECL is the declaration for the catch parameter, or NULL
1176 if this is a `catch (...)' clause. */
1178 void
1179 finish_handler_parms (tree decl, tree handler)
1181 tree type = NULL_TREE;
1182 if (processing_template_decl)
1184 if (decl)
1186 decl = pushdecl (decl);
1187 decl = push_template_decl (decl);
1188 HANDLER_PARMS (handler) = decl;
1189 type = TREE_TYPE (decl);
1192 else
1193 type = expand_start_catch_block (decl);
1194 HANDLER_TYPE (handler) = type;
1195 if (!processing_template_decl && type)
1196 mark_used (eh_type_info (type));
1199 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1200 the return value from the matching call to finish_handler_parms. */
1202 void
1203 finish_handler (tree handler)
1205 if (!processing_template_decl)
1206 expand_end_catch_block ();
1207 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1210 /* Begin a compound statement. FLAGS contains some bits that control the
1211 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1212 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1213 block of a function. If BCS_TRY_BLOCK is set, this is the block
1214 created on behalf of a TRY statement. Returns a token to be passed to
1215 finish_compound_stmt. */
1217 tree
1218 begin_compound_stmt (unsigned int flags)
1220 tree r;
1222 if (flags & BCS_NO_SCOPE)
1224 r = push_stmt_list ();
1225 STATEMENT_LIST_NO_SCOPE (r) = 1;
1227 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1228 But, if it's a statement-expression with a scopeless block, there's
1229 nothing to keep, and we don't want to accidentally keep a block
1230 *inside* the scopeless block. */
1231 keep_next_level (false);
1233 else
1234 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1236 /* When processing a template, we need to remember where the braces were,
1237 so that we can set up identical scopes when instantiating the template
1238 later. BIND_EXPR is a handy candidate for this.
1239 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1240 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1241 processing templates. */
1242 if (processing_template_decl)
1244 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1245 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1246 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1247 TREE_SIDE_EFFECTS (r) = 1;
1250 return r;
1253 /* Finish a compound-statement, which is given by STMT. */
1255 void
1256 finish_compound_stmt (tree stmt)
1258 if (TREE_CODE (stmt) == BIND_EXPR)
1260 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1261 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1262 discard the BIND_EXPR so it can be merged with the containing
1263 STATEMENT_LIST. */
1264 if (TREE_CODE (body) == STATEMENT_LIST
1265 && STATEMENT_LIST_HEAD (body) == NULL
1266 && !BIND_EXPR_BODY_BLOCK (stmt)
1267 && !BIND_EXPR_TRY_BLOCK (stmt))
1268 stmt = body;
1269 else
1270 BIND_EXPR_BODY (stmt) = body;
1272 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1273 stmt = pop_stmt_list (stmt);
1274 else
1276 /* Destroy any ObjC "super" receivers that may have been
1277 created. */
1278 objc_clear_super_receiver ();
1280 stmt = do_poplevel (stmt);
1283 /* ??? See c_end_compound_stmt wrt statement expressions. */
1284 add_stmt (stmt);
1285 finish_stmt ();
1288 /* Finish an asm-statement, whose components are a STRING, some
1289 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1290 LABELS. Also note whether the asm-statement should be
1291 considered volatile. */
1293 tree
1294 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1295 tree input_operands, tree clobbers, tree labels)
1297 tree r;
1298 tree t;
1299 int ninputs = list_length (input_operands);
1300 int noutputs = list_length (output_operands);
1302 if (!processing_template_decl)
1304 const char *constraint;
1305 const char **oconstraints;
1306 bool allows_mem, allows_reg, is_inout;
1307 tree operand;
1308 int i;
1310 oconstraints = XALLOCAVEC (const char *, noutputs);
1312 string = resolve_asm_operand_names (string, output_operands,
1313 input_operands, labels);
1315 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1317 operand = TREE_VALUE (t);
1319 /* ??? Really, this should not be here. Users should be using a
1320 proper lvalue, dammit. But there's a long history of using
1321 casts in the output operands. In cases like longlong.h, this
1322 becomes a primitive form of typechecking -- if the cast can be
1323 removed, then the output operand had a type of the proper width;
1324 otherwise we'll get an error. Gross, but ... */
1325 STRIP_NOPS (operand);
1327 operand = mark_lvalue_use (operand);
1329 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1330 operand = error_mark_node;
1332 if (operand != error_mark_node
1333 && (TREE_READONLY (operand)
1334 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1335 /* Functions are not modifiable, even though they are
1336 lvalues. */
1337 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1338 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1339 /* If it's an aggregate and any field is const, then it is
1340 effectively const. */
1341 || (CLASS_TYPE_P (TREE_TYPE (operand))
1342 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1343 cxx_readonly_error (operand, lv_asm);
1345 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1346 oconstraints[i] = constraint;
1348 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1349 &allows_mem, &allows_reg, &is_inout))
1351 /* If the operand is going to end up in memory,
1352 mark it addressable. */
1353 if (!allows_reg && !cxx_mark_addressable (operand))
1354 operand = error_mark_node;
1356 else
1357 operand = error_mark_node;
1359 TREE_VALUE (t) = operand;
1362 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1364 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1365 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1367 /* If the type of the operand hasn't been determined (e.g.,
1368 because it involves an overloaded function), then issue
1369 an error message. There's no context available to
1370 resolve the overloading. */
1371 if (TREE_TYPE (operand) == unknown_type_node)
1373 error ("type of asm operand %qE could not be determined",
1374 TREE_VALUE (t));
1375 operand = error_mark_node;
1378 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1379 oconstraints, &allows_mem, &allows_reg))
1381 /* If the operand is going to end up in memory,
1382 mark it addressable. */
1383 if (!allows_reg && allows_mem)
1385 /* Strip the nops as we allow this case. FIXME, this really
1386 should be rejected or made deprecated. */
1387 STRIP_NOPS (operand);
1388 if (!cxx_mark_addressable (operand))
1389 operand = error_mark_node;
1392 else
1393 operand = error_mark_node;
1395 TREE_VALUE (t) = operand;
1399 r = build_stmt (input_location, ASM_EXPR, string,
1400 output_operands, input_operands,
1401 clobbers, labels);
1402 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1403 r = maybe_cleanup_point_expr_void (r);
1404 return add_stmt (r);
1407 /* Finish a label with the indicated NAME. Returns the new label. */
1409 tree
1410 finish_label_stmt (tree name)
1412 tree decl = define_label (input_location, name);
1414 if (decl == error_mark_node)
1415 return error_mark_node;
1417 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1419 return decl;
1422 /* Finish a series of declarations for local labels. G++ allows users
1423 to declare "local" labels, i.e., labels with scope. This extension
1424 is useful when writing code involving statement-expressions. */
1426 void
1427 finish_label_decl (tree name)
1429 if (!at_function_scope_p ())
1431 error ("__label__ declarations are only allowed in function scopes");
1432 return;
1435 add_decl_expr (declare_local_label (name));
1438 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1440 void
1441 finish_decl_cleanup (tree decl, tree cleanup)
1443 push_cleanup (decl, cleanup, false);
1446 /* If the current scope exits with an exception, run CLEANUP. */
1448 void
1449 finish_eh_cleanup (tree cleanup)
1451 push_cleanup (NULL, cleanup, true);
1454 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1455 order they were written by the user. Each node is as for
1456 emit_mem_initializers. */
1458 void
1459 finish_mem_initializers (tree mem_inits)
1461 /* Reorder the MEM_INITS so that they are in the order they appeared
1462 in the source program. */
1463 mem_inits = nreverse (mem_inits);
1465 if (processing_template_decl)
1467 tree mem;
1469 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1471 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1472 check for bare parameter packs in the TREE_VALUE, because
1473 any parameter packs in the TREE_VALUE have already been
1474 bound as part of the TREE_PURPOSE. See
1475 make_pack_expansion for more information. */
1476 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1477 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1478 TREE_VALUE (mem) = error_mark_node;
1481 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1482 CTOR_INITIALIZER, mem_inits));
1484 else
1485 emit_mem_initializers (mem_inits);
1488 /* Finish a parenthesized expression EXPR. */
1490 tree
1491 finish_parenthesized_expr (tree expr)
1493 if (EXPR_P (expr))
1494 /* This inhibits warnings in c_common_truthvalue_conversion. */
1495 TREE_NO_WARNING (expr) = 1;
1497 if (TREE_CODE (expr) == OFFSET_REF
1498 || TREE_CODE (expr) == SCOPE_REF)
1499 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1500 enclosed in parentheses. */
1501 PTRMEM_OK_P (expr) = 0;
1503 if (TREE_CODE (expr) == STRING_CST)
1504 PAREN_STRING_LITERAL_P (expr) = 1;
1506 return expr;
1509 /* Finish a reference to a non-static data member (DECL) that is not
1510 preceded by `.' or `->'. */
1512 tree
1513 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1515 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1517 if (!object)
1519 tree scope = qualifying_scope;
1520 if (scope == NULL_TREE)
1521 scope = context_for_name_lookup (decl);
1522 object = maybe_dummy_object (scope, NULL);
1525 if (object == error_mark_node)
1526 return error_mark_node;
1528 /* DR 613: Can use non-static data members without an associated
1529 object in sizeof/decltype/alignof. */
1530 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1531 && (!processing_template_decl || !current_class_ref))
1533 if (current_function_decl
1534 && DECL_STATIC_FUNCTION_P (current_function_decl))
1535 error ("invalid use of member %q+D in static member function", decl);
1536 else
1537 error ("invalid use of non-static data member %q+D", decl);
1538 error ("from this location");
1540 return error_mark_node;
1543 if (current_class_ptr)
1544 TREE_USED (current_class_ptr) = 1;
1545 if (processing_template_decl && !qualifying_scope)
1547 tree type = TREE_TYPE (decl);
1549 if (TREE_CODE (type) == REFERENCE_TYPE)
1550 /* Quals on the object don't matter. */;
1551 else
1553 /* Set the cv qualifiers. */
1554 int quals = (current_class_ref
1555 ? cp_type_quals (TREE_TYPE (current_class_ref))
1556 : TYPE_UNQUALIFIED);
1558 if (DECL_MUTABLE_P (decl))
1559 quals &= ~TYPE_QUAL_CONST;
1561 quals |= cp_type_quals (TREE_TYPE (decl));
1562 type = cp_build_qualified_type (type, quals);
1565 return (convert_from_reference
1566 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1568 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1569 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1570 for now. */
1571 else if (processing_template_decl)
1572 return build_qualified_name (TREE_TYPE (decl),
1573 qualifying_scope,
1574 decl,
1575 /*template_p=*/false);
1576 else
1578 tree access_type = TREE_TYPE (object);
1580 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1581 decl, tf_warning_or_error);
1583 /* If the data member was named `C::M', convert `*this' to `C'
1584 first. */
1585 if (qualifying_scope)
1587 tree binfo = NULL_TREE;
1588 object = build_scoped_ref (object, qualifying_scope,
1589 &binfo);
1592 return build_class_member_access_expr (object, decl,
1593 /*access_path=*/NULL_TREE,
1594 /*preserve_reference=*/false,
1595 tf_warning_or_error);
1599 /* If we are currently parsing a template and we encountered a typedef
1600 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1601 adds the typedef to a list tied to the current template.
1602 At template instantiation time, that list is walked and access check
1603 performed for each typedef.
1604 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1606 void
1607 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1608 tree context,
1609 location_t location)
1611 tree template_info = NULL;
1612 tree cs = current_scope ();
1614 if (!is_typedef_decl (typedef_decl)
1615 || !context
1616 || !CLASS_TYPE_P (context)
1617 || !cs)
1618 return;
1620 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1621 template_info = get_template_info (cs);
1623 if (template_info
1624 && TI_TEMPLATE (template_info)
1625 && !currently_open_class (context))
1626 append_type_to_template_for_access_check (cs, typedef_decl,
1627 context, location);
1630 /* DECL was the declaration to which a qualified-id resolved. Issue
1631 an error message if it is not accessible. If OBJECT_TYPE is
1632 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1633 type of `*x', or `x', respectively. If the DECL was named as
1634 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1636 void
1637 check_accessibility_of_qualified_id (tree decl,
1638 tree object_type,
1639 tree nested_name_specifier)
1641 tree scope;
1642 tree qualifying_type = NULL_TREE;
1644 /* If we are parsing a template declaration and if decl is a typedef,
1645 add it to a list tied to the template.
1646 At template instantiation time, that list will be walked and
1647 access check performed. */
1648 add_typedef_to_current_template_for_access_check (decl,
1649 nested_name_specifier
1650 ? nested_name_specifier
1651 : DECL_CONTEXT (decl),
1652 input_location);
1654 /* If we're not checking, return immediately. */
1655 if (deferred_access_no_check)
1656 return;
1658 /* Determine the SCOPE of DECL. */
1659 scope = context_for_name_lookup (decl);
1660 /* If the SCOPE is not a type, then DECL is not a member. */
1661 if (!TYPE_P (scope))
1662 return;
1663 /* Compute the scope through which DECL is being accessed. */
1664 if (object_type
1665 /* OBJECT_TYPE might not be a class type; consider:
1667 class A { typedef int I; };
1668 I *p;
1669 p->A::I::~I();
1671 In this case, we will have "A::I" as the DECL, but "I" as the
1672 OBJECT_TYPE. */
1673 && CLASS_TYPE_P (object_type)
1674 && DERIVED_FROM_P (scope, object_type))
1675 /* If we are processing a `->' or `.' expression, use the type of the
1676 left-hand side. */
1677 qualifying_type = object_type;
1678 else if (nested_name_specifier)
1680 /* If the reference is to a non-static member of the
1681 current class, treat it as if it were referenced through
1682 `this'. */
1683 if (DECL_NONSTATIC_MEMBER_P (decl)
1684 && current_class_ptr
1685 && DERIVED_FROM_P (scope, current_class_type))
1686 qualifying_type = current_class_type;
1687 /* Otherwise, use the type indicated by the
1688 nested-name-specifier. */
1689 else
1690 qualifying_type = nested_name_specifier;
1692 else
1693 /* Otherwise, the name must be from the current class or one of
1694 its bases. */
1695 qualifying_type = currently_open_derived_class (scope);
1697 if (qualifying_type
1698 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1699 or similar in a default argument value. */
1700 && CLASS_TYPE_P (qualifying_type)
1701 && !dependent_type_p (qualifying_type))
1702 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1703 decl, tf_warning_or_error);
1706 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1707 class named to the left of the "::" operator. DONE is true if this
1708 expression is a complete postfix-expression; it is false if this
1709 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1710 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1711 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1712 is true iff this qualified name appears as a template argument. */
1714 tree
1715 finish_qualified_id_expr (tree qualifying_class,
1716 tree expr,
1717 bool done,
1718 bool address_p,
1719 bool template_p,
1720 bool template_arg_p)
1722 gcc_assert (TYPE_P (qualifying_class));
1724 if (error_operand_p (expr))
1725 return error_mark_node;
1727 if (DECL_P (expr) || BASELINK_P (expr))
1728 mark_used (expr);
1730 if (template_p)
1731 check_template_keyword (expr);
1733 /* If EXPR occurs as the operand of '&', use special handling that
1734 permits a pointer-to-member. */
1735 if (address_p && done)
1737 if (TREE_CODE (expr) == SCOPE_REF)
1738 expr = TREE_OPERAND (expr, 1);
1739 expr = build_offset_ref (qualifying_class, expr,
1740 /*address_p=*/true);
1741 return expr;
1744 /* Within the scope of a class, turn references to non-static
1745 members into expression of the form "this->...". */
1746 if (template_arg_p)
1747 /* But, within a template argument, we do not want make the
1748 transformation, as there is no "this" pointer. */
1750 else if (TREE_CODE (expr) == FIELD_DECL)
1752 push_deferring_access_checks (dk_no_check);
1753 expr = finish_non_static_data_member (expr, NULL_TREE,
1754 qualifying_class);
1755 pop_deferring_access_checks ();
1757 else if (BASELINK_P (expr) && !processing_template_decl)
1759 tree ob;
1761 /* See if any of the functions are non-static members. */
1762 /* If so, the expression may be relative to 'this'. */
1763 if (!shared_member_p (expr)
1764 && (ob = maybe_dummy_object (qualifying_class, NULL),
1765 !is_dummy_object (ob)))
1766 expr = (build_class_member_access_expr
1767 (ob,
1768 expr,
1769 BASELINK_ACCESS_BINFO (expr),
1770 /*preserve_reference=*/false,
1771 tf_warning_or_error));
1772 else if (done)
1773 /* The expression is a qualified name whose address is not
1774 being taken. */
1775 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1777 else if (BASELINK_P (expr))
1779 else
1781 expr = convert_from_reference (expr);
1783 /* In a template, return a SCOPE_REF for most qualified-ids
1784 so that we can check access at instantiation time. But if
1785 we're looking at a member of the current instantiation, we
1786 know we have access and building up the SCOPE_REF confuses
1787 non-type template argument handling. */
1788 if (processing_template_decl
1789 && !currently_open_class (qualifying_class))
1790 expr = build_qualified_name (TREE_TYPE (expr),
1791 qualifying_class, expr,
1792 template_p);
1795 return expr;
1798 /* Begin a statement-expression. The value returned must be passed to
1799 finish_stmt_expr. */
1801 tree
1802 begin_stmt_expr (void)
1804 return push_stmt_list ();
1807 /* Process the final expression of a statement expression. EXPR can be
1808 NULL, if the final expression is empty. Return a STATEMENT_LIST
1809 containing all the statements in the statement-expression, or
1810 ERROR_MARK_NODE if there was an error. */
1812 tree
1813 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1815 if (error_operand_p (expr))
1817 /* The type of the statement-expression is the type of the last
1818 expression. */
1819 TREE_TYPE (stmt_expr) = error_mark_node;
1820 return error_mark_node;
1823 /* If the last statement does not have "void" type, then the value
1824 of the last statement is the value of the entire expression. */
1825 if (expr)
1827 tree type = TREE_TYPE (expr);
1829 if (processing_template_decl)
1831 expr = build_stmt (input_location, EXPR_STMT, expr);
1832 expr = add_stmt (expr);
1833 /* Mark the last statement so that we can recognize it as such at
1834 template-instantiation time. */
1835 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1837 else if (VOID_TYPE_P (type))
1839 /* Just treat this like an ordinary statement. */
1840 expr = finish_expr_stmt (expr);
1842 else
1844 /* It actually has a value we need to deal with. First, force it
1845 to be an rvalue so that we won't need to build up a copy
1846 constructor call later when we try to assign it to something. */
1847 expr = force_rvalue (expr, tf_warning_or_error);
1848 if (error_operand_p (expr))
1849 return error_mark_node;
1851 /* Update for array-to-pointer decay. */
1852 type = TREE_TYPE (expr);
1854 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1855 normal statement, but don't convert to void or actually add
1856 the EXPR_STMT. */
1857 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1858 expr = maybe_cleanup_point_expr (expr);
1859 add_stmt (expr);
1862 /* The type of the statement-expression is the type of the last
1863 expression. */
1864 TREE_TYPE (stmt_expr) = type;
1867 return stmt_expr;
1870 /* Finish a statement-expression. EXPR should be the value returned
1871 by the previous begin_stmt_expr. Returns an expression
1872 representing the statement-expression. */
1874 tree
1875 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1877 tree type;
1878 tree result;
1880 if (error_operand_p (stmt_expr))
1882 pop_stmt_list (stmt_expr);
1883 return error_mark_node;
1886 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1888 type = TREE_TYPE (stmt_expr);
1889 result = pop_stmt_list (stmt_expr);
1890 TREE_TYPE (result) = type;
1892 if (processing_template_decl)
1894 result = build_min (STMT_EXPR, type, result);
1895 TREE_SIDE_EFFECTS (result) = 1;
1896 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1898 else if (CLASS_TYPE_P (type))
1900 /* Wrap the statement-expression in a TARGET_EXPR so that the
1901 temporary object created by the final expression is destroyed at
1902 the end of the full-expression containing the
1903 statement-expression. */
1904 result = force_target_expr (type, result, tf_warning_or_error);
1907 return result;
1910 /* Returns the expression which provides the value of STMT_EXPR. */
1912 tree
1913 stmt_expr_value_expr (tree stmt_expr)
1915 tree t = STMT_EXPR_STMT (stmt_expr);
1917 if (TREE_CODE (t) == BIND_EXPR)
1918 t = BIND_EXPR_BODY (t);
1920 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1921 t = STATEMENT_LIST_TAIL (t)->stmt;
1923 if (TREE_CODE (t) == EXPR_STMT)
1924 t = EXPR_STMT_EXPR (t);
1926 return t;
1929 /* Return TRUE iff EXPR_STMT is an empty list of
1930 expression statements. */
1932 bool
1933 empty_expr_stmt_p (tree expr_stmt)
1935 tree body = NULL_TREE;
1937 if (expr_stmt == void_zero_node)
1938 return true;
1940 if (expr_stmt)
1942 if (TREE_CODE (expr_stmt) == EXPR_STMT)
1943 body = EXPR_STMT_EXPR (expr_stmt);
1944 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1945 body = expr_stmt;
1948 if (body)
1950 if (TREE_CODE (body) == STATEMENT_LIST)
1951 return tsi_end_p (tsi_start (body));
1952 else
1953 return empty_expr_stmt_p (body);
1955 return false;
1958 /* Perform Koenig lookup. FN is the postfix-expression representing
1959 the function (or functions) to call; ARGS are the arguments to the
1960 call; if INCLUDE_STD then the `std' namespace is automatically
1961 considered an associated namespace (used in range-based for loops).
1962 Returns the functions to be considered by overload resolution. */
1964 tree
1965 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args, bool include_std,
1966 tsubst_flags_t complain)
1968 tree identifier = NULL_TREE;
1969 tree functions = NULL_TREE;
1970 tree tmpl_args = NULL_TREE;
1971 bool template_id = false;
1973 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1975 /* Use a separate flag to handle null args. */
1976 template_id = true;
1977 tmpl_args = TREE_OPERAND (fn, 1);
1978 fn = TREE_OPERAND (fn, 0);
1981 /* Find the name of the overloaded function. */
1982 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1983 identifier = fn;
1984 else if (is_overloaded_fn (fn))
1986 functions = fn;
1987 identifier = DECL_NAME (get_first_fn (functions));
1989 else if (DECL_P (fn))
1991 functions = fn;
1992 identifier = DECL_NAME (fn);
1995 /* A call to a namespace-scope function using an unqualified name.
1997 Do Koenig lookup -- unless any of the arguments are
1998 type-dependent. */
1999 if (!any_type_dependent_arguments_p (args)
2000 && !any_dependent_template_arguments_p (tmpl_args))
2002 fn = lookup_arg_dependent (identifier, functions, args, include_std);
2003 if (!fn)
2005 /* The unqualified name could not be resolved. */
2006 if (complain)
2007 fn = unqualified_fn_lookup_error (identifier);
2008 else
2009 fn = identifier;
2013 if (fn && template_id)
2014 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2016 return fn;
2019 /* Generate an expression for `FN (ARGS)'. This may change the
2020 contents of ARGS.
2022 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2023 as a virtual call, even if FN is virtual. (This flag is set when
2024 encountering an expression where the function name is explicitly
2025 qualified. For example a call to `X::f' never generates a virtual
2026 call.)
2028 Returns code for the call. */
2030 tree
2031 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2032 bool koenig_p, tsubst_flags_t complain)
2034 tree result;
2035 tree orig_fn;
2036 vec<tree, va_gc> *orig_args = NULL;
2038 if (fn == error_mark_node)
2039 return error_mark_node;
2041 gcc_assert (!TYPE_P (fn));
2043 orig_fn = fn;
2045 if (processing_template_decl)
2047 /* If the call expression is dependent, build a CALL_EXPR node
2048 with no type; type_dependent_expression_p recognizes
2049 expressions with no type as being dependent. */
2050 if (type_dependent_expression_p (fn)
2051 || any_type_dependent_arguments_p (*args)
2052 /* For a non-static member function that doesn't have an
2053 explicit object argument, we need to specifically
2054 test the type dependency of the "this" pointer because it
2055 is not included in *ARGS even though it is considered to
2056 be part of the list of arguments. Note that this is
2057 related to CWG issues 515 and 1005. */
2058 || (TREE_CODE (fn) != COMPONENT_REF
2059 && non_static_member_function_p (fn)
2060 && current_class_ref
2061 && type_dependent_expression_p (current_class_ref)))
2063 result = build_nt_call_vec (fn, *args);
2064 SET_EXPR_LOCATION (result, EXPR_LOC_OR_HERE (fn));
2065 KOENIG_LOOKUP_P (result) = koenig_p;
2066 if (cfun)
2070 tree fndecl = OVL_CURRENT (fn);
2071 if (TREE_CODE (fndecl) != FUNCTION_DECL
2072 || !TREE_THIS_VOLATILE (fndecl))
2073 break;
2074 fn = OVL_NEXT (fn);
2076 while (fn);
2077 if (!fn)
2078 current_function_returns_abnormally = 1;
2080 return result;
2082 orig_args = make_tree_vector_copy (*args);
2083 if (!BASELINK_P (fn)
2084 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2085 && TREE_TYPE (fn) != unknown_type_node)
2086 fn = build_non_dependent_expr (fn);
2087 make_args_non_dependent (*args);
2090 if (TREE_CODE (fn) == COMPONENT_REF)
2092 tree member = TREE_OPERAND (fn, 1);
2093 if (BASELINK_P (member))
2095 tree object = TREE_OPERAND (fn, 0);
2096 return build_new_method_call (object, member,
2097 args, NULL_TREE,
2098 (disallow_virtual
2099 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2100 : LOOKUP_NORMAL),
2101 /*fn_p=*/NULL,
2102 complain);
2106 if (is_overloaded_fn (fn))
2107 fn = baselink_for_fns (fn);
2109 result = NULL_TREE;
2110 if (BASELINK_P (fn))
2112 tree object;
2114 /* A call to a member function. From [over.call.func]:
2116 If the keyword this is in scope and refers to the class of
2117 that member function, or a derived class thereof, then the
2118 function call is transformed into a qualified function call
2119 using (*this) as the postfix-expression to the left of the
2120 . operator.... [Otherwise] a contrived object of type T
2121 becomes the implied object argument.
2123 In this situation:
2125 struct A { void f(); };
2126 struct B : public A {};
2127 struct C : public A { void g() { B::f(); }};
2129 "the class of that member function" refers to `A'. But 11.2
2130 [class.access.base] says that we need to convert 'this' to B* as
2131 part of the access, so we pass 'B' to maybe_dummy_object. */
2133 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2134 NULL);
2136 if (processing_template_decl)
2138 if (type_dependent_expression_p (object))
2140 tree ret = build_nt_call_vec (orig_fn, orig_args);
2141 release_tree_vector (orig_args);
2142 return ret;
2144 object = build_non_dependent_expr (object);
2147 result = build_new_method_call (object, fn, args, NULL_TREE,
2148 (disallow_virtual
2149 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2150 : LOOKUP_NORMAL),
2151 /*fn_p=*/NULL,
2152 complain);
2154 else if (is_overloaded_fn (fn))
2156 /* If the function is an overloaded builtin, resolve it. */
2157 if (TREE_CODE (fn) == FUNCTION_DECL
2158 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2159 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2160 result = resolve_overloaded_builtin (input_location, fn, *args);
2162 if (!result)
2164 if (warn_sizeof_pointer_memaccess
2165 && !vec_safe_is_empty (*args)
2166 && !processing_template_decl)
2168 location_t sizeof_arg_loc[3];
2169 tree sizeof_arg[3];
2170 unsigned int i;
2171 for (i = 0; i < 3; i++)
2173 tree t;
2175 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2176 sizeof_arg[i] = NULL_TREE;
2177 if (i >= (*args)->length ())
2178 continue;
2179 t = (**args)[i];
2180 if (TREE_CODE (t) != SIZEOF_EXPR)
2181 continue;
2182 if (SIZEOF_EXPR_TYPE_P (t))
2183 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2184 else
2185 sizeof_arg[i] = TREE_OPERAND (t, 0);
2186 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2188 sizeof_pointer_memaccess_warning
2189 (sizeof_arg_loc, fn, *args,
2190 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2193 /* A call to a namespace-scope function. */
2194 result = build_new_function_call (fn, args, koenig_p, complain);
2197 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2199 if (!vec_safe_is_empty (*args))
2200 error ("arguments to destructor are not allowed");
2201 /* Mark the pseudo-destructor call as having side-effects so
2202 that we do not issue warnings about its use. */
2203 result = build1 (NOP_EXPR,
2204 void_type_node,
2205 TREE_OPERAND (fn, 0));
2206 TREE_SIDE_EFFECTS (result) = 1;
2208 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2209 /* If the "function" is really an object of class type, it might
2210 have an overloaded `operator ()'. */
2211 result = build_op_call (fn, args, complain);
2213 if (!result)
2214 /* A call where the function is unknown. */
2215 result = cp_build_function_call_vec (fn, args, complain);
2217 if (processing_template_decl && result != error_mark_node)
2219 if (TREE_CODE (result) == INDIRECT_REF)
2220 result = TREE_OPERAND (result, 0);
2221 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2222 SET_EXPR_LOCATION (result, input_location);
2223 KOENIG_LOOKUP_P (result) = koenig_p;
2224 release_tree_vector (orig_args);
2225 result = convert_from_reference (result);
2228 if (koenig_p)
2230 /* Free garbage OVERLOADs from arg-dependent lookup. */
2231 tree next = NULL_TREE;
2232 for (fn = orig_fn;
2233 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2234 fn = next)
2236 if (processing_template_decl)
2237 /* In a template, we'll re-use them at instantiation time. */
2238 OVL_ARG_DEPENDENT (fn) = false;
2239 else
2241 next = OVL_CHAIN (fn);
2242 ggc_free (fn);
2247 return result;
2250 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2251 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2252 POSTDECREMENT_EXPR.) */
2254 tree
2255 finish_increment_expr (tree expr, enum tree_code code)
2257 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2260 /* Finish a use of `this'. Returns an expression for `this'. */
2262 tree
2263 finish_this_expr (void)
2265 tree result;
2267 if (current_class_ptr)
2269 tree type = TREE_TYPE (current_class_ref);
2271 /* In a lambda expression, 'this' refers to the captured 'this'. */
2272 if (LAMBDA_TYPE_P (type))
2273 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2274 else
2275 result = current_class_ptr;
2278 else if (current_function_decl
2279 && DECL_STATIC_FUNCTION_P (current_function_decl))
2281 error ("%<this%> is unavailable for static member functions");
2282 result = error_mark_node;
2284 else
2286 if (current_function_decl)
2287 error ("invalid use of %<this%> in non-member function");
2288 else
2289 error ("invalid use of %<this%> at top level");
2290 result = error_mark_node;
2293 return result;
2296 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2297 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2298 the TYPE for the type given. If SCOPE is non-NULL, the expression
2299 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2301 tree
2302 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2304 if (object == error_mark_node || destructor == error_mark_node)
2305 return error_mark_node;
2307 gcc_assert (TYPE_P (destructor));
2309 if (!processing_template_decl)
2311 if (scope == error_mark_node)
2313 error ("invalid qualifying scope in pseudo-destructor name");
2314 return error_mark_node;
2316 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2318 error ("qualified type %qT does not match destructor name ~%qT",
2319 scope, destructor);
2320 return error_mark_node;
2324 /* [expr.pseudo] says both:
2326 The type designated by the pseudo-destructor-name shall be
2327 the same as the object type.
2329 and:
2331 The cv-unqualified versions of the object type and of the
2332 type designated by the pseudo-destructor-name shall be the
2333 same type.
2335 We implement the more generous second sentence, since that is
2336 what most other compilers do. */
2337 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2338 destructor))
2340 error ("%qE is not of type %qT", object, destructor);
2341 return error_mark_node;
2345 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2348 /* Finish an expression of the form CODE EXPR. */
2350 tree
2351 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr)
2353 tree result = build_x_unary_op (loc, code, expr, tf_warning_or_error);
2354 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2355 overflow_warning (input_location, result);
2357 return result;
2360 /* Finish a compound-literal expression. TYPE is the type to which
2361 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2363 tree
2364 finish_compound_literal (tree type, tree compound_literal,
2365 tsubst_flags_t complain)
2367 if (type == error_mark_node)
2368 return error_mark_node;
2370 if (TREE_CODE (type) == REFERENCE_TYPE)
2372 compound_literal
2373 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2374 complain);
2375 return cp_build_c_cast (type, compound_literal, complain);
2378 if (!TYPE_OBJ_P (type))
2380 if (complain & tf_error)
2381 error ("compound literal of non-object type %qT", type);
2382 return error_mark_node;
2385 if (processing_template_decl)
2387 TREE_TYPE (compound_literal) = type;
2388 /* Mark the expression as a compound literal. */
2389 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2390 return compound_literal;
2393 type = complete_type (type);
2395 if (TYPE_NON_AGGREGATE_CLASS (type))
2397 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2398 everywhere that deals with function arguments would be a pain, so
2399 just wrap it in a TREE_LIST. The parser set a flag so we know
2400 that it came from T{} rather than T({}). */
2401 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2402 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2403 return build_functional_cast (type, compound_literal, complain);
2406 if (TREE_CODE (type) == ARRAY_TYPE
2407 && check_array_initializer (NULL_TREE, type, compound_literal))
2408 return error_mark_node;
2409 compound_literal = reshape_init (type, compound_literal, complain);
2410 if (SCALAR_TYPE_P (type)
2411 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2412 && (complain & tf_warning_or_error))
2413 check_narrowing (type, compound_literal);
2414 if (TREE_CODE (type) == ARRAY_TYPE
2415 && TYPE_DOMAIN (type) == NULL_TREE)
2417 cp_complete_array_type_or_error (&type, compound_literal,
2418 false, complain);
2419 if (type == error_mark_node)
2420 return error_mark_node;
2422 compound_literal = digest_init (type, compound_literal, complain);
2423 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2424 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2425 /* Put static/constant array temporaries in static variables, but always
2426 represent class temporaries with TARGET_EXPR so we elide copies. */
2427 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2428 && TREE_CODE (type) == ARRAY_TYPE
2429 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2430 && initializer_constant_valid_p (compound_literal, type))
2432 tree decl = create_temporary_var (type);
2433 DECL_INITIAL (decl) = compound_literal;
2434 TREE_STATIC (decl) = 1;
2435 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2437 /* 5.19 says that a constant expression can include an
2438 lvalue-rvalue conversion applied to "a glvalue of literal type
2439 that refers to a non-volatile temporary object initialized
2440 with a constant expression". Rather than try to communicate
2441 that this VAR_DECL is a temporary, just mark it constexpr. */
2442 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2443 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2444 TREE_CONSTANT (decl) = true;
2446 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2447 decl = pushdecl_top_level (decl);
2448 DECL_NAME (decl) = make_anon_name ();
2449 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2450 return decl;
2452 else
2453 return get_target_expr_sfinae (compound_literal, complain);
2456 /* Return the declaration for the function-name variable indicated by
2457 ID. */
2459 tree
2460 finish_fname (tree id)
2462 tree decl;
2464 decl = fname_decl (input_location, C_RID_CODE (id), id);
2465 if (processing_template_decl && current_function_decl)
2466 decl = DECL_NAME (decl);
2467 return decl;
2470 /* Finish a translation unit. */
2472 void
2473 finish_translation_unit (void)
2475 /* In case there were missing closebraces,
2476 get us back to the global binding level. */
2477 pop_everything ();
2478 while (current_namespace != global_namespace)
2479 pop_namespace ();
2481 /* Do file scope __FUNCTION__ et al. */
2482 finish_fname_decls ();
2485 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2486 Returns the parameter. */
2488 tree
2489 finish_template_type_parm (tree aggr, tree identifier)
2491 if (aggr != class_type_node)
2493 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2494 aggr = class_type_node;
2497 return build_tree_list (aggr, identifier);
2500 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2501 Returns the parameter. */
2503 tree
2504 finish_template_template_parm (tree aggr, tree identifier)
2506 tree decl = build_decl (input_location,
2507 TYPE_DECL, identifier, NULL_TREE);
2508 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2509 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2510 DECL_TEMPLATE_RESULT (tmpl) = decl;
2511 DECL_ARTIFICIAL (decl) = 1;
2512 end_template_decl ();
2514 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2516 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2517 /*is_primary=*/true, /*is_partial=*/false,
2518 /*is_friend=*/0);
2520 return finish_template_type_parm (aggr, tmpl);
2523 /* ARGUMENT is the default-argument value for a template template
2524 parameter. If ARGUMENT is invalid, issue error messages and return
2525 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2527 tree
2528 check_template_template_default_arg (tree argument)
2530 if (TREE_CODE (argument) != TEMPLATE_DECL
2531 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2532 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2534 if (TREE_CODE (argument) == TYPE_DECL)
2535 error ("invalid use of type %qT as a default value for a template "
2536 "template-parameter", TREE_TYPE (argument));
2537 else
2538 error ("invalid default argument for a template template parameter");
2539 return error_mark_node;
2542 return argument;
2545 /* Begin a class definition, as indicated by T. */
2547 tree
2548 begin_class_definition (tree t)
2550 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2551 return error_mark_node;
2553 if (processing_template_parmlist)
2555 error ("definition of %q#T inside template parameter list", t);
2556 return error_mark_node;
2559 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2560 are passed the same as decimal scalar types. */
2561 if (TREE_CODE (t) == RECORD_TYPE
2562 && !processing_template_decl)
2564 tree ns = TYPE_CONTEXT (t);
2565 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2566 && DECL_CONTEXT (ns) == std_node
2567 && DECL_NAME (ns)
2568 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2570 const char *n = TYPE_NAME_STRING (t);
2571 if ((strcmp (n, "decimal32") == 0)
2572 || (strcmp (n, "decimal64") == 0)
2573 || (strcmp (n, "decimal128") == 0))
2574 TYPE_TRANSPARENT_AGGR (t) = 1;
2578 /* A non-implicit typename comes from code like:
2580 template <typename T> struct A {
2581 template <typename U> struct A<T>::B ...
2583 This is erroneous. */
2584 else if (TREE_CODE (t) == TYPENAME_TYPE)
2586 error ("invalid definition of qualified type %qT", t);
2587 t = error_mark_node;
2590 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2592 t = make_class_type (RECORD_TYPE);
2593 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2596 if (TYPE_BEING_DEFINED (t))
2598 t = make_class_type (TREE_CODE (t));
2599 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2601 maybe_process_partial_specialization (t);
2602 pushclass (t);
2603 TYPE_BEING_DEFINED (t) = 1;
2605 if (flag_pack_struct)
2607 tree v;
2608 TYPE_PACKED (t) = 1;
2609 /* Even though the type is being defined for the first time
2610 here, there might have been a forward declaration, so there
2611 might be cv-qualified variants of T. */
2612 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2613 TYPE_PACKED (v) = 1;
2615 /* Reset the interface data, at the earliest possible
2616 moment, as it might have been set via a class foo;
2617 before. */
2618 if (! TYPE_ANONYMOUS_P (t))
2620 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2621 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2622 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2623 (t, finfo->interface_unknown);
2625 reset_specialization();
2627 /* Make a declaration for this class in its own scope. */
2628 build_self_reference ();
2630 return t;
2633 /* Finish the member declaration given by DECL. */
2635 void
2636 finish_member_declaration (tree decl)
2638 if (decl == error_mark_node || decl == NULL_TREE)
2639 return;
2641 if (decl == void_type_node)
2642 /* The COMPONENT was a friend, not a member, and so there's
2643 nothing for us to do. */
2644 return;
2646 /* We should see only one DECL at a time. */
2647 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2649 /* Set up access control for DECL. */
2650 TREE_PRIVATE (decl)
2651 = (current_access_specifier == access_private_node);
2652 TREE_PROTECTED (decl)
2653 = (current_access_specifier == access_protected_node);
2654 if (TREE_CODE (decl) == TEMPLATE_DECL)
2656 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2657 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2660 /* Mark the DECL as a member of the current class, unless it's
2661 a member of an enumeration. */
2662 if (TREE_CODE (decl) != CONST_DECL)
2663 DECL_CONTEXT (decl) = current_class_type;
2665 /* Check for bare parameter packs in the member variable declaration. */
2666 if (TREE_CODE (decl) == FIELD_DECL)
2668 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2669 TREE_TYPE (decl) = error_mark_node;
2670 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2671 DECL_ATTRIBUTES (decl) = NULL_TREE;
2674 /* [dcl.link]
2676 A C language linkage is ignored for the names of class members
2677 and the member function type of class member functions. */
2678 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2679 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2681 /* Put functions on the TYPE_METHODS list and everything else on the
2682 TYPE_FIELDS list. Note that these are built up in reverse order.
2683 We reverse them (to obtain declaration order) in finish_struct. */
2684 if (TREE_CODE (decl) == FUNCTION_DECL
2685 || DECL_FUNCTION_TEMPLATE_P (decl))
2687 /* We also need to add this function to the
2688 CLASSTYPE_METHOD_VEC. */
2689 if (add_method (current_class_type, decl, NULL_TREE))
2691 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2692 TYPE_METHODS (current_class_type) = decl;
2694 maybe_add_class_template_decl_list (current_class_type, decl,
2695 /*friend_p=*/0);
2698 /* Enter the DECL into the scope of the class. */
2699 else if (pushdecl_class_level (decl))
2701 if (TREE_CODE (decl) == USING_DECL)
2703 /* For now, ignore class-scope USING_DECLS, so that
2704 debugging backends do not see them. */
2705 DECL_IGNORED_P (decl) = 1;
2708 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2709 go at the beginning. The reason is that lookup_field_1
2710 searches the list in order, and we want a field name to
2711 override a type name so that the "struct stat hack" will
2712 work. In particular:
2714 struct S { enum E { }; int E } s;
2715 s.E = 3;
2717 is valid. In addition, the FIELD_DECLs must be maintained in
2718 declaration order so that class layout works as expected.
2719 However, we don't need that order until class layout, so we
2720 save a little time by putting FIELD_DECLs on in reverse order
2721 here, and then reversing them in finish_struct_1. (We could
2722 also keep a pointer to the correct insertion points in the
2723 list.) */
2725 if (TREE_CODE (decl) == TYPE_DECL)
2726 TYPE_FIELDS (current_class_type)
2727 = chainon (TYPE_FIELDS (current_class_type), decl);
2728 else
2730 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2731 TYPE_FIELDS (current_class_type) = decl;
2734 maybe_add_class_template_decl_list (current_class_type, decl,
2735 /*friend_p=*/0);
2738 if (pch_file)
2739 note_decl_for_pch (decl);
2742 /* DECL has been declared while we are building a PCH file. Perform
2743 actions that we might normally undertake lazily, but which can be
2744 performed now so that they do not have to be performed in
2745 translation units which include the PCH file. */
2747 void
2748 note_decl_for_pch (tree decl)
2750 gcc_assert (pch_file);
2752 /* There's a good chance that we'll have to mangle names at some
2753 point, even if only for emission in debugging information. */
2754 if ((TREE_CODE (decl) == VAR_DECL
2755 || TREE_CODE (decl) == FUNCTION_DECL)
2756 && !processing_template_decl)
2757 mangle_decl (decl);
2760 /* Finish processing a complete template declaration. The PARMS are
2761 the template parameters. */
2763 void
2764 finish_template_decl (tree parms)
2766 if (parms)
2767 end_template_decl ();
2768 else
2769 end_specialization ();
2772 /* Finish processing a template-id (which names a type) of the form
2773 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2774 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2775 the scope of template-id indicated. */
2777 tree
2778 finish_template_type (tree name, tree args, int entering_scope)
2780 tree type;
2782 type = lookup_template_class (name, args,
2783 NULL_TREE, NULL_TREE, entering_scope,
2784 tf_warning_or_error | tf_user);
2785 if (type == error_mark_node)
2786 return type;
2787 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2788 return TYPE_STUB_DECL (type);
2789 else
2790 return TYPE_NAME (type);
2793 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2794 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2795 BASE_CLASS, or NULL_TREE if an error occurred. The
2796 ACCESS_SPECIFIER is one of
2797 access_{default,public,protected_private}_node. For a virtual base
2798 we set TREE_TYPE. */
2800 tree
2801 finish_base_specifier (tree base, tree access, bool virtual_p)
2803 tree result;
2805 if (base == error_mark_node)
2807 error ("invalid base-class specification");
2808 result = NULL_TREE;
2810 else if (! MAYBE_CLASS_TYPE_P (base))
2812 error ("%qT is not a class type", base);
2813 result = NULL_TREE;
2815 else
2817 if (cp_type_quals (base) != 0)
2819 /* DR 484: Can a base-specifier name a cv-qualified
2820 class type? */
2821 base = TYPE_MAIN_VARIANT (base);
2823 result = build_tree_list (access, base);
2824 if (virtual_p)
2825 TREE_TYPE (result) = integer_type_node;
2828 return result;
2831 /* If FNS is a member function, a set of member functions, or a
2832 template-id referring to one or more member functions, return a
2833 BASELINK for FNS, incorporating the current access context.
2834 Otherwise, return FNS unchanged. */
2836 tree
2837 baselink_for_fns (tree fns)
2839 tree scope;
2840 tree cl;
2842 if (BASELINK_P (fns)
2843 || error_operand_p (fns))
2844 return fns;
2846 scope = ovl_scope (fns);
2847 if (!CLASS_TYPE_P (scope))
2848 return fns;
2850 cl = currently_open_derived_class (scope);
2851 if (!cl)
2852 cl = scope;
2853 cl = TYPE_BINFO (cl);
2854 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2857 /* Returns true iff DECL is an automatic variable from a function outside
2858 the current one. */
2860 static bool
2861 outer_automatic_var_p (tree decl)
2863 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2864 && DECL_FUNCTION_SCOPE_P (decl)
2865 && !TREE_STATIC (decl)
2866 && DECL_CONTEXT (decl) != current_function_decl);
2869 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2870 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2871 if non-NULL, is the type or namespace used to explicitly qualify
2872 ID_EXPRESSION. DECL is the entity to which that name has been
2873 resolved.
2875 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2876 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2877 be set to true if this expression isn't permitted in a
2878 constant-expression, but it is otherwise not set by this function.
2879 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2880 constant-expression, but a non-constant expression is also
2881 permissible.
2883 DONE is true if this expression is a complete postfix-expression;
2884 it is false if this expression is followed by '->', '[', '(', etc.
2885 ADDRESS_P is true iff this expression is the operand of '&'.
2886 TEMPLATE_P is true iff the qualified-id was of the form
2887 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2888 appears as a template argument.
2890 If an error occurs, and it is the kind of error that might cause
2891 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2892 is the caller's responsibility to issue the message. *ERROR_MSG
2893 will be a string with static storage duration, so the caller need
2894 not "free" it.
2896 Return an expression for the entity, after issuing appropriate
2897 diagnostics. This function is also responsible for transforming a
2898 reference to a non-static member into a COMPONENT_REF that makes
2899 the use of "this" explicit.
2901 Upon return, *IDK will be filled in appropriately. */
2902 tree
2903 finish_id_expression (tree id_expression,
2904 tree decl,
2905 tree scope,
2906 cp_id_kind *idk,
2907 bool integral_constant_expression_p,
2908 bool allow_non_integral_constant_expression_p,
2909 bool *non_integral_constant_expression_p,
2910 bool template_p,
2911 bool done,
2912 bool address_p,
2913 bool template_arg_p,
2914 const char **error_msg,
2915 location_t location)
2917 decl = strip_using_decl (decl);
2919 /* Initialize the output parameters. */
2920 *idk = CP_ID_KIND_NONE;
2921 *error_msg = NULL;
2923 if (id_expression == error_mark_node)
2924 return error_mark_node;
2925 /* If we have a template-id, then no further lookup is
2926 required. If the template-id was for a template-class, we
2927 will sometimes have a TYPE_DECL at this point. */
2928 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2929 || TREE_CODE (decl) == TYPE_DECL)
2931 /* Look up the name. */
2932 else
2934 if (decl == error_mark_node)
2936 /* Name lookup failed. */
2937 if (scope
2938 && (!TYPE_P (scope)
2939 || (!dependent_type_p (scope)
2940 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2941 && IDENTIFIER_TYPENAME_P (id_expression)
2942 && dependent_type_p (TREE_TYPE (id_expression))))))
2944 /* If the qualifying type is non-dependent (and the name
2945 does not name a conversion operator to a dependent
2946 type), issue an error. */
2947 qualified_name_lookup_error (scope, id_expression, decl, location);
2948 return error_mark_node;
2950 else if (!scope)
2952 /* It may be resolved via Koenig lookup. */
2953 *idk = CP_ID_KIND_UNQUALIFIED;
2954 return id_expression;
2956 else
2957 decl = id_expression;
2959 /* If DECL is a variable that would be out of scope under
2960 ANSI/ISO rules, but in scope in the ARM, name lookup
2961 will succeed. Issue a diagnostic here. */
2962 else
2963 decl = check_for_out_of_scope_variable (decl);
2965 /* Remember that the name was used in the definition of
2966 the current class so that we can check later to see if
2967 the meaning would have been different after the class
2968 was entirely defined. */
2969 if (!scope && decl != error_mark_node
2970 && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2971 maybe_note_name_used_in_class (id_expression, decl);
2973 /* Disallow uses of local variables from containing functions, except
2974 within lambda-expressions. */
2975 if (outer_automatic_var_p (decl)
2976 /* It's not a use (3.2) if we're in an unevaluated context. */
2977 && !cp_unevaluated_operand)
2979 tree context = DECL_CONTEXT (decl);
2980 tree containing_function = current_function_decl;
2981 tree lambda_stack = NULL_TREE;
2982 tree lambda_expr = NULL_TREE;
2983 tree initializer = convert_from_reference (decl);
2985 /* Mark it as used now even if the use is ill-formed. */
2986 mark_used (decl);
2988 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2989 support for an approach in which a reference to a local
2990 [constant] automatic variable in a nested class or lambda body
2991 would enter the expression as an rvalue, which would reduce
2992 the complexity of the problem"
2994 FIXME update for final resolution of core issue 696. */
2995 if (decl_constant_var_p (decl))
2996 return integral_constant_value (decl);
2998 /* If we are in a lambda function, we can move out until we hit
2999 1. the context,
3000 2. a non-lambda function, or
3001 3. a non-default capturing lambda function. */
3002 while (context != containing_function
3003 && LAMBDA_FUNCTION_P (containing_function))
3005 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3006 (DECL_CONTEXT (containing_function));
3008 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3009 == CPLD_NONE)
3010 break;
3012 lambda_stack = tree_cons (NULL_TREE,
3013 lambda_expr,
3014 lambda_stack);
3016 containing_function
3017 = decl_function_context (containing_function);
3020 if (context == containing_function)
3022 decl = add_default_capture (lambda_stack,
3023 /*id=*/DECL_NAME (decl),
3024 initializer);
3026 else if (lambda_expr)
3028 error ("%qD is not captured", decl);
3029 return error_mark_node;
3031 else
3033 error (TREE_CODE (decl) == VAR_DECL
3034 ? G_("use of local variable with automatic storage from containing function")
3035 : G_("use of parameter from containing function"));
3036 error (" %q+#D declared here", decl);
3037 return error_mark_node;
3041 /* Also disallow uses of function parameters outside the function
3042 body, except inside an unevaluated context (i.e. decltype). */
3043 if (TREE_CODE (decl) == PARM_DECL
3044 && DECL_CONTEXT (decl) == NULL_TREE
3045 && !cp_unevaluated_operand)
3047 error ("use of parameter %qD outside function body", decl);
3048 return error_mark_node;
3052 /* If we didn't find anything, or what we found was a type,
3053 then this wasn't really an id-expression. */
3054 if (TREE_CODE (decl) == TEMPLATE_DECL
3055 && !DECL_FUNCTION_TEMPLATE_P (decl))
3057 *error_msg = "missing template arguments";
3058 return error_mark_node;
3060 else if (TREE_CODE (decl) == TYPE_DECL
3061 || TREE_CODE (decl) == NAMESPACE_DECL)
3063 *error_msg = "expected primary-expression";
3064 return error_mark_node;
3067 /* If the name resolved to a template parameter, there is no
3068 need to look it up again later. */
3069 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3070 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3072 tree r;
3074 *idk = CP_ID_KIND_NONE;
3075 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3076 decl = TEMPLATE_PARM_DECL (decl);
3077 r = convert_from_reference (DECL_INITIAL (decl));
3079 if (integral_constant_expression_p
3080 && !dependent_type_p (TREE_TYPE (decl))
3081 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3083 if (!allow_non_integral_constant_expression_p)
3084 error ("template parameter %qD of type %qT is not allowed in "
3085 "an integral constant expression because it is not of "
3086 "integral or enumeration type", decl, TREE_TYPE (decl));
3087 *non_integral_constant_expression_p = true;
3089 return r;
3091 else
3093 bool dependent_p;
3095 /* If the declaration was explicitly qualified indicate
3096 that. The semantics of `A::f(3)' are different than
3097 `f(3)' if `f' is virtual. */
3098 *idk = (scope
3099 ? CP_ID_KIND_QUALIFIED
3100 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3101 ? CP_ID_KIND_TEMPLATE_ID
3102 : CP_ID_KIND_UNQUALIFIED));
3105 /* [temp.dep.expr]
3107 An id-expression is type-dependent if it contains an
3108 identifier that was declared with a dependent type.
3110 The standard is not very specific about an id-expression that
3111 names a set of overloaded functions. What if some of them
3112 have dependent types and some of them do not? Presumably,
3113 such a name should be treated as a dependent name. */
3114 /* Assume the name is not dependent. */
3115 dependent_p = false;
3116 if (!processing_template_decl)
3117 /* No names are dependent outside a template. */
3119 else if (TREE_CODE (decl) == CONST_DECL)
3120 /* We don't want to treat enumerators as dependent. */
3122 /* A template-id where the name of the template was not resolved
3123 is definitely dependent. */
3124 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3125 && (TREE_CODE (TREE_OPERAND (decl, 0))
3126 == IDENTIFIER_NODE))
3127 dependent_p = true;
3128 /* For anything except an overloaded function, just check its
3129 type. */
3130 else if (!is_overloaded_fn (decl))
3131 dependent_p
3132 = dependent_type_p (TREE_TYPE (decl));
3133 /* For a set of overloaded functions, check each of the
3134 functions. */
3135 else
3137 tree fns = decl;
3139 if (BASELINK_P (fns))
3140 fns = BASELINK_FUNCTIONS (fns);
3142 /* For a template-id, check to see if the template
3143 arguments are dependent. */
3144 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3146 tree args = TREE_OPERAND (fns, 1);
3147 dependent_p = any_dependent_template_arguments_p (args);
3148 /* The functions are those referred to by the
3149 template-id. */
3150 fns = TREE_OPERAND (fns, 0);
3153 /* If there are no dependent template arguments, go through
3154 the overloaded functions. */
3155 while (fns && !dependent_p)
3157 tree fn = OVL_CURRENT (fns);
3159 /* Member functions of dependent classes are
3160 dependent. */
3161 if (TREE_CODE (fn) == FUNCTION_DECL
3162 && type_dependent_expression_p (fn))
3163 dependent_p = true;
3164 else if (TREE_CODE (fn) == TEMPLATE_DECL
3165 && dependent_template_p (fn))
3166 dependent_p = true;
3168 fns = OVL_NEXT (fns);
3172 /* If the name was dependent on a template parameter, we will
3173 resolve the name at instantiation time. */
3174 if (dependent_p)
3176 /* Create a SCOPE_REF for qualified names, if the scope is
3177 dependent. */
3178 if (scope)
3180 if (TYPE_P (scope))
3182 if (address_p && done)
3183 decl = finish_qualified_id_expr (scope, decl,
3184 done, address_p,
3185 template_p,
3186 template_arg_p);
3187 else
3189 tree type = NULL_TREE;
3190 if (DECL_P (decl) && !dependent_scope_p (scope))
3191 type = TREE_TYPE (decl);
3192 decl = build_qualified_name (type,
3193 scope,
3194 id_expression,
3195 template_p);
3198 if (TREE_TYPE (decl))
3199 decl = convert_from_reference (decl);
3200 return decl;
3202 /* A TEMPLATE_ID already contains all the information we
3203 need. */
3204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3205 return id_expression;
3206 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3207 /* If we found a variable, then name lookup during the
3208 instantiation will always resolve to the same VAR_DECL
3209 (or an instantiation thereof). */
3210 if (TREE_CODE (decl) == VAR_DECL
3211 || TREE_CODE (decl) == PARM_DECL)
3213 mark_used (decl);
3214 return convert_from_reference (decl);
3216 /* The same is true for FIELD_DECL, but we also need to
3217 make sure that the syntax is correct. */
3218 else if (TREE_CODE (decl) == FIELD_DECL)
3220 /* Since SCOPE is NULL here, this is an unqualified name.
3221 Access checking has been performed during name lookup
3222 already. Turn off checking to avoid duplicate errors. */
3223 push_deferring_access_checks (dk_no_check);
3224 decl = finish_non_static_data_member
3225 (decl, NULL_TREE,
3226 /*qualifying_scope=*/NULL_TREE);
3227 pop_deferring_access_checks ();
3228 return decl;
3230 return id_expression;
3233 if (TREE_CODE (decl) == NAMESPACE_DECL)
3235 error ("use of namespace %qD as expression", decl);
3236 return error_mark_node;
3238 else if (DECL_CLASS_TEMPLATE_P (decl))
3240 error ("use of class template %qT as expression", decl);
3241 return error_mark_node;
3243 else if (TREE_CODE (decl) == TREE_LIST)
3245 /* Ambiguous reference to base members. */
3246 error ("request for member %qD is ambiguous in "
3247 "multiple inheritance lattice", id_expression);
3248 print_candidates (decl);
3249 return error_mark_node;
3252 /* Mark variable-like entities as used. Functions are similarly
3253 marked either below or after overload resolution. */
3254 if ((TREE_CODE (decl) == VAR_DECL
3255 || TREE_CODE (decl) == PARM_DECL
3256 || TREE_CODE (decl) == CONST_DECL
3257 || TREE_CODE (decl) == RESULT_DECL)
3258 && !mark_used (decl))
3259 return error_mark_node;
3261 /* Only certain kinds of names are allowed in constant
3262 expression. Template parameters have already
3263 been handled above. */
3264 if (! error_operand_p (decl)
3265 && integral_constant_expression_p
3266 && ! decl_constant_var_p (decl)
3267 && TREE_CODE (decl) != CONST_DECL
3268 && ! builtin_valid_in_constant_expr_p (decl))
3270 if (!allow_non_integral_constant_expression_p)
3272 error ("%qD cannot appear in a constant-expression", decl);
3273 return error_mark_node;
3275 *non_integral_constant_expression_p = true;
3278 tree wrap;
3279 if (TREE_CODE (decl) == VAR_DECL
3280 && !cp_unevaluated_operand
3281 && DECL_THREAD_LOCAL_P (decl)
3282 && (wrap = get_tls_wrapper_fn (decl)))
3284 /* Replace an evaluated use of the thread_local variable with
3285 a call to its wrapper. */
3286 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3288 else if (scope)
3290 decl = (adjust_result_of_qualified_name_lookup
3291 (decl, scope, current_nonlambda_class_type()));
3293 if (TREE_CODE (decl) == FUNCTION_DECL)
3294 mark_used (decl);
3296 if (TYPE_P (scope))
3297 decl = finish_qualified_id_expr (scope,
3298 decl,
3299 done,
3300 address_p,
3301 template_p,
3302 template_arg_p);
3303 else
3304 decl = convert_from_reference (decl);
3306 else if (TREE_CODE (decl) == FIELD_DECL)
3308 /* Since SCOPE is NULL here, this is an unqualified name.
3309 Access checking has been performed during name lookup
3310 already. Turn off checking to avoid duplicate errors. */
3311 push_deferring_access_checks (dk_no_check);
3312 decl = finish_non_static_data_member (decl, NULL_TREE,
3313 /*qualifying_scope=*/NULL_TREE);
3314 pop_deferring_access_checks ();
3316 else if (is_overloaded_fn (decl))
3318 tree first_fn;
3320 first_fn = get_first_fn (decl);
3321 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3322 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3324 if (!really_overloaded_fn (decl)
3325 && !mark_used (first_fn))
3326 return error_mark_node;
3328 if (!template_arg_p
3329 && TREE_CODE (first_fn) == FUNCTION_DECL
3330 && DECL_FUNCTION_MEMBER_P (first_fn)
3331 && !shared_member_p (decl))
3333 /* A set of member functions. */
3334 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3335 return finish_class_member_access_expr (decl, id_expression,
3336 /*template_p=*/false,
3337 tf_warning_or_error);
3340 decl = baselink_for_fns (decl);
3342 else
3344 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3345 && DECL_CLASS_SCOPE_P (decl))
3347 tree context = context_for_name_lookup (decl);
3348 if (context != current_class_type)
3350 tree path = currently_open_derived_class (context);
3351 perform_or_defer_access_check (TYPE_BINFO (path),
3352 decl, decl,
3353 tf_warning_or_error);
3357 decl = convert_from_reference (decl);
3361 if (TREE_DEPRECATED (decl))
3362 warn_deprecated_use (decl, NULL_TREE);
3364 return decl;
3367 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3368 use as a type-specifier. */
3370 tree
3371 finish_typeof (tree expr)
3373 tree type;
3375 if (type_dependent_expression_p (expr))
3377 type = cxx_make_type (TYPEOF_TYPE);
3378 TYPEOF_TYPE_EXPR (type) = expr;
3379 SET_TYPE_STRUCTURAL_EQUALITY (type);
3381 return type;
3384 expr = mark_type_use (expr);
3386 type = unlowered_expr_type (expr);
3388 if (!type || type == unknown_type_node)
3390 error ("type of %qE is unknown", expr);
3391 return error_mark_node;
3394 return type;
3397 /* Implement the __underlying_type keyword: Return the underlying
3398 type of TYPE, suitable for use as a type-specifier. */
3400 tree
3401 finish_underlying_type (tree type)
3403 tree underlying_type;
3405 if (processing_template_decl)
3407 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3408 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3409 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3411 return underlying_type;
3414 complete_type (type);
3416 if (TREE_CODE (type) != ENUMERAL_TYPE)
3418 error ("%qT is not an enumeration type", type);
3419 return error_mark_node;
3422 underlying_type = ENUM_UNDERLYING_TYPE (type);
3424 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3425 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3426 See finish_enum_value_list for details. */
3427 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3428 underlying_type
3429 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3430 TYPE_UNSIGNED (underlying_type));
3432 return underlying_type;
3435 /* Implement the __direct_bases keyword: Return the direct base classes
3436 of type */
3438 tree
3439 calculate_direct_bases (tree type)
3441 vec<tree, va_gc> *vector = make_tree_vector();
3442 tree bases_vec = NULL_TREE;
3443 vec<tree, va_gc> *base_binfos;
3444 tree binfo;
3445 unsigned i;
3447 complete_type (type);
3449 if (!NON_UNION_CLASS_TYPE_P (type))
3450 return make_tree_vec (0);
3452 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3454 /* Virtual bases are initialized first */
3455 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3457 if (BINFO_VIRTUAL_P (binfo))
3459 vec_safe_push (vector, binfo);
3463 /* Now non-virtuals */
3464 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3466 if (!BINFO_VIRTUAL_P (binfo))
3468 vec_safe_push (vector, binfo);
3473 bases_vec = make_tree_vec (vector->length ());
3475 for (i = 0; i < vector->length (); ++i)
3477 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3479 return bases_vec;
3482 /* Implement the __bases keyword: Return the base classes
3483 of type */
3485 /* Find morally non-virtual base classes by walking binfo hierarchy */
3486 /* Virtual base classes are handled separately in finish_bases */
3488 static tree
3489 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3491 /* Don't walk bases of virtual bases */
3492 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3495 static tree
3496 dfs_calculate_bases_post (tree binfo, void *data_)
3498 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3499 if (!BINFO_VIRTUAL_P (binfo))
3501 vec_safe_push (*data, BINFO_TYPE (binfo));
3503 return NULL_TREE;
3506 /* Calculates the morally non-virtual base classes of a class */
3507 static vec<tree, va_gc> *
3508 calculate_bases_helper (tree type)
3510 vec<tree, va_gc> *vector = make_tree_vector();
3512 /* Now add non-virtual base classes in order of construction */
3513 dfs_walk_all (TYPE_BINFO (type),
3514 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3515 return vector;
3518 tree
3519 calculate_bases (tree type)
3521 vec<tree, va_gc> *vector = make_tree_vector();
3522 tree bases_vec = NULL_TREE;
3523 unsigned i;
3524 vec<tree, va_gc> *vbases;
3525 vec<tree, va_gc> *nonvbases;
3526 tree binfo;
3528 complete_type (type);
3530 if (!NON_UNION_CLASS_TYPE_P (type))
3531 return make_tree_vec (0);
3533 /* First go through virtual base classes */
3534 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3535 vec_safe_iterate (vbases, i, &binfo); i++)
3537 vec<tree, va_gc> *vbase_bases;
3538 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3539 vec_safe_splice (vector, vbase_bases);
3540 release_tree_vector (vbase_bases);
3543 /* Now for the non-virtual bases */
3544 nonvbases = calculate_bases_helper (type);
3545 vec_safe_splice (vector, nonvbases);
3546 release_tree_vector (nonvbases);
3548 /* Last element is entire class, so don't copy */
3549 bases_vec = make_tree_vec (vector->length () - 1);
3551 for (i = 0; i < vector->length () - 1; ++i)
3553 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3555 release_tree_vector (vector);
3556 return bases_vec;
3559 tree
3560 finish_bases (tree type, bool direct)
3562 tree bases = NULL_TREE;
3564 if (!processing_template_decl)
3566 /* Parameter packs can only be used in templates */
3567 error ("Parameter pack __bases only valid in template declaration");
3568 return error_mark_node;
3571 bases = cxx_make_type (BASES);
3572 BASES_TYPE (bases) = type;
3573 BASES_DIRECT (bases) = direct;
3574 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3576 return bases;
3579 /* Perform C++-specific checks for __builtin_offsetof before calling
3580 fold_offsetof. */
3582 tree
3583 finish_offsetof (tree expr)
3585 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3587 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3588 TREE_OPERAND (expr, 2));
3589 return error_mark_node;
3591 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3592 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3593 || TREE_TYPE (expr) == unknown_type_node)
3595 if (TREE_CODE (expr) == COMPONENT_REF
3596 || TREE_CODE (expr) == COMPOUND_EXPR)
3597 expr = TREE_OPERAND (expr, 1);
3598 error ("cannot apply %<offsetof%> to member function %qD", expr);
3599 return error_mark_node;
3601 if (REFERENCE_REF_P (expr))
3602 expr = TREE_OPERAND (expr, 0);
3603 if (TREE_CODE (expr) == COMPONENT_REF)
3605 tree object = TREE_OPERAND (expr, 0);
3606 if (!complete_type_or_else (TREE_TYPE (object), object))
3607 return error_mark_node;
3609 return fold_offsetof (expr);
3612 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3613 function is broken out from the above for the benefit of the tree-ssa
3614 project. */
3616 void
3617 simplify_aggr_init_expr (tree *tp)
3619 tree aggr_init_expr = *tp;
3621 /* Form an appropriate CALL_EXPR. */
3622 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3623 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3624 tree type = TREE_TYPE (slot);
3626 tree call_expr;
3627 enum style_t { ctor, arg, pcc } style;
3629 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3630 style = ctor;
3631 #ifdef PCC_STATIC_STRUCT_RETURN
3632 else if (1)
3633 style = pcc;
3634 #endif
3635 else
3637 gcc_assert (TREE_ADDRESSABLE (type));
3638 style = arg;
3641 call_expr = build_call_array_loc (input_location,
3642 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3644 aggr_init_expr_nargs (aggr_init_expr),
3645 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3646 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3648 if (style == ctor)
3650 /* Replace the first argument to the ctor with the address of the
3651 slot. */
3652 cxx_mark_addressable (slot);
3653 CALL_EXPR_ARG (call_expr, 0) =
3654 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3656 else if (style == arg)
3658 /* Just mark it addressable here, and leave the rest to
3659 expand_call{,_inline}. */
3660 cxx_mark_addressable (slot);
3661 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3662 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3664 else if (style == pcc)
3666 /* If we're using the non-reentrant PCC calling convention, then we
3667 need to copy the returned value out of the static buffer into the
3668 SLOT. */
3669 push_deferring_access_checks (dk_no_check);
3670 call_expr = build_aggr_init (slot, call_expr,
3671 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3672 tf_warning_or_error);
3673 pop_deferring_access_checks ();
3674 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3677 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3679 tree init = build_zero_init (type, NULL_TREE,
3680 /*static_storage_p=*/false);
3681 init = build2 (INIT_EXPR, void_type_node, slot, init);
3682 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3683 init, call_expr);
3686 *tp = call_expr;
3689 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3691 void
3692 emit_associated_thunks (tree fn)
3694 /* When we use vcall offsets, we emit thunks with the virtual
3695 functions to which they thunk. The whole point of vcall offsets
3696 is so that you can know statically the entire set of thunks that
3697 will ever be needed for a given virtual function, thereby
3698 enabling you to output all the thunks with the function itself. */
3699 if (DECL_VIRTUAL_P (fn)
3700 /* Do not emit thunks for extern template instantiations. */
3701 && ! DECL_REALLY_EXTERN (fn))
3703 tree thunk;
3705 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3707 if (!THUNK_ALIAS (thunk))
3709 use_thunk (thunk, /*emit_p=*/1);
3710 if (DECL_RESULT_THUNK_P (thunk))
3712 tree probe;
3714 for (probe = DECL_THUNKS (thunk);
3715 probe; probe = DECL_CHAIN (probe))
3716 use_thunk (probe, /*emit_p=*/1);
3719 else
3720 gcc_assert (!DECL_THUNKS (thunk));
3725 /* Returns true iff FUN is an instantiation of a constexpr function
3726 template. */
3728 static inline bool
3729 is_instantiation_of_constexpr (tree fun)
3731 return (DECL_TEMPLOID_INSTANTIATION (fun)
3732 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3733 (DECL_TI_TEMPLATE (fun))));
3736 /* Generate RTL for FN. */
3738 bool
3739 expand_or_defer_fn_1 (tree fn)
3741 /* When the parser calls us after finishing the body of a template
3742 function, we don't really want to expand the body. */
3743 if (processing_template_decl)
3745 /* Normally, collection only occurs in rest_of_compilation. So,
3746 if we don't collect here, we never collect junk generated
3747 during the processing of templates until we hit a
3748 non-template function. It's not safe to do this inside a
3749 nested class, though, as the parser may have local state that
3750 is not a GC root. */
3751 if (!function_depth)
3752 ggc_collect ();
3753 return false;
3756 gcc_assert (DECL_SAVED_TREE (fn));
3758 /* If this is a constructor or destructor body, we have to clone
3759 it. */
3760 if (maybe_clone_body (fn))
3762 /* We don't want to process FN again, so pretend we've written
3763 it out, even though we haven't. */
3764 TREE_ASM_WRITTEN (fn) = 1;
3765 /* If this is an instantiation of a constexpr function, keep
3766 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3767 if (!is_instantiation_of_constexpr (fn))
3768 DECL_SAVED_TREE (fn) = NULL_TREE;
3769 return false;
3772 /* We make a decision about linkage for these functions at the end
3773 of the compilation. Until that point, we do not want the back
3774 end to output them -- but we do want it to see the bodies of
3775 these functions so that it can inline them as appropriate. */
3776 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3778 if (DECL_INTERFACE_KNOWN (fn))
3779 /* We've already made a decision as to how this function will
3780 be handled. */;
3781 else if (!at_eof)
3783 DECL_EXTERNAL (fn) = 1;
3784 DECL_NOT_REALLY_EXTERN (fn) = 1;
3785 note_vague_linkage_fn (fn);
3786 /* A non-template inline function with external linkage will
3787 always be COMDAT. As we must eventually determine the
3788 linkage of all functions, and as that causes writes to
3789 the data mapped in from the PCH file, it's advantageous
3790 to mark the functions at this point. */
3791 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3793 /* This function must have external linkage, as
3794 otherwise DECL_INTERFACE_KNOWN would have been
3795 set. */
3796 gcc_assert (TREE_PUBLIC (fn));
3797 comdat_linkage (fn);
3798 DECL_INTERFACE_KNOWN (fn) = 1;
3801 else
3802 import_export_decl (fn);
3804 /* If the user wants us to keep all inline functions, then mark
3805 this function as needed so that finish_file will make sure to
3806 output it later. Similarly, all dllexport'd functions must
3807 be emitted; there may be callers in other DLLs. */
3808 if ((flag_keep_inline_functions
3809 && DECL_DECLARED_INLINE_P (fn)
3810 && !DECL_REALLY_EXTERN (fn))
3811 || (flag_keep_inline_dllexport
3812 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
3814 mark_needed (fn);
3815 DECL_EXTERNAL (fn) = 0;
3819 /* There's no reason to do any of the work here if we're only doing
3820 semantic analysis; this code just generates RTL. */
3821 if (flag_syntax_only)
3822 return false;
3824 return true;
3827 void
3828 expand_or_defer_fn (tree fn)
3830 if (expand_or_defer_fn_1 (fn))
3832 function_depth++;
3834 /* Expand or defer, at the whim of the compilation unit manager. */
3835 cgraph_finalize_function (fn, function_depth > 1);
3836 emit_associated_thunks (fn);
3838 function_depth--;
3842 struct nrv_data
3844 tree var;
3845 tree result;
3846 hash_table <pointer_hash <tree_node> > visited;
3849 /* Helper function for walk_tree, used by finalize_nrv below. */
3851 static tree
3852 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3854 struct nrv_data *dp = (struct nrv_data *)data;
3855 tree_node **slot;
3857 /* No need to walk into types. There wouldn't be any need to walk into
3858 non-statements, except that we have to consider STMT_EXPRs. */
3859 if (TYPE_P (*tp))
3860 *walk_subtrees = 0;
3861 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3862 but differs from using NULL_TREE in that it indicates that we care
3863 about the value of the RESULT_DECL. */
3864 else if (TREE_CODE (*tp) == RETURN_EXPR)
3865 TREE_OPERAND (*tp, 0) = dp->result;
3866 /* Change all cleanups for the NRV to only run when an exception is
3867 thrown. */
3868 else if (TREE_CODE (*tp) == CLEANUP_STMT
3869 && CLEANUP_DECL (*tp) == dp->var)
3870 CLEANUP_EH_ONLY (*tp) = 1;
3871 /* Replace the DECL_EXPR for the NRV with an initialization of the
3872 RESULT_DECL, if needed. */
3873 else if (TREE_CODE (*tp) == DECL_EXPR
3874 && DECL_EXPR_DECL (*tp) == dp->var)
3876 tree init;
3877 if (DECL_INITIAL (dp->var)
3878 && DECL_INITIAL (dp->var) != error_mark_node)
3879 init = build2 (INIT_EXPR, void_type_node, dp->result,
3880 DECL_INITIAL (dp->var));
3881 else
3882 init = build_empty_stmt (EXPR_LOCATION (*tp));
3883 DECL_INITIAL (dp->var) = NULL_TREE;
3884 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
3885 *tp = init;
3887 /* And replace all uses of the NRV with the RESULT_DECL. */
3888 else if (*tp == dp->var)
3889 *tp = dp->result;
3891 /* Avoid walking into the same tree more than once. Unfortunately, we
3892 can't just use walk_tree_without duplicates because it would only call
3893 us for the first occurrence of dp->var in the function body. */
3894 slot = dp->visited.find_slot (*tp, INSERT);
3895 if (*slot)
3896 *walk_subtrees = 0;
3897 else
3898 *slot = *tp;
3900 /* Keep iterating. */
3901 return NULL_TREE;
3904 /* Called from finish_function to implement the named return value
3905 optimization by overriding all the RETURN_EXPRs and pertinent
3906 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3907 RESULT_DECL for the function. */
3909 void
3910 finalize_nrv (tree *tp, tree var, tree result)
3912 struct nrv_data data;
3914 /* Copy name from VAR to RESULT. */
3915 DECL_NAME (result) = DECL_NAME (var);
3916 /* Don't forget that we take its address. */
3917 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3918 /* Finally set DECL_VALUE_EXPR to avoid assigning
3919 a stack slot at -O0 for the original var and debug info
3920 uses RESULT location for VAR. */
3921 SET_DECL_VALUE_EXPR (var, result);
3922 DECL_HAS_VALUE_EXPR_P (var) = 1;
3924 data.var = var;
3925 data.result = result;
3926 data.visited.create (37);
3927 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
3928 data.visited.dispose ();
3931 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
3933 bool
3934 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
3935 bool need_copy_ctor, bool need_copy_assignment)
3937 int save_errorcount = errorcount;
3938 tree info, t;
3940 /* Always allocate 3 elements for simplicity. These are the
3941 function decls for the ctor, dtor, and assignment op.
3942 This layout is known to the three lang hooks,
3943 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
3944 and cxx_omp_clause_assign_op. */
3945 info = make_tree_vec (3);
3946 CP_OMP_CLAUSE_INFO (c) = info;
3948 if (need_default_ctor || need_copy_ctor)
3950 if (need_default_ctor)
3951 t = get_default_ctor (type);
3952 else
3953 t = get_copy_ctor (type, tf_warning_or_error);
3955 if (t && !trivial_fn_p (t))
3956 TREE_VEC_ELT (info, 0) = t;
3959 if ((need_default_ctor || need_copy_ctor)
3960 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3961 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
3963 if (need_copy_assignment)
3965 t = get_copy_assign (type);
3967 if (t && !trivial_fn_p (t))
3968 TREE_VEC_ELT (info, 2) = t;
3971 return errorcount != save_errorcount;
3974 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
3975 Remove any elements from the list that are invalid. */
3977 tree
3978 finish_omp_clauses (tree clauses)
3980 bitmap_head generic_head, firstprivate_head, lastprivate_head;
3981 tree c, t, *pc = &clauses;
3982 const char *name;
3984 bitmap_obstack_initialize (NULL);
3985 bitmap_initialize (&generic_head, &bitmap_default_obstack);
3986 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
3987 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
3989 for (pc = &clauses, c = clauses; c ; c = *pc)
3991 bool remove = false;
3993 switch (OMP_CLAUSE_CODE (c))
3995 case OMP_CLAUSE_SHARED:
3996 name = "shared";
3997 goto check_dup_generic;
3998 case OMP_CLAUSE_PRIVATE:
3999 name = "private";
4000 goto check_dup_generic;
4001 case OMP_CLAUSE_REDUCTION:
4002 name = "reduction";
4003 goto check_dup_generic;
4004 case OMP_CLAUSE_COPYPRIVATE:
4005 name = "copyprivate";
4006 goto check_dup_generic;
4007 case OMP_CLAUSE_COPYIN:
4008 name = "copyin";
4009 goto check_dup_generic;
4010 check_dup_generic:
4011 t = OMP_CLAUSE_DECL (c);
4012 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4014 if (processing_template_decl)
4015 break;
4016 if (DECL_P (t))
4017 error ("%qD is not a variable in clause %qs", t, name);
4018 else
4019 error ("%qE is not a variable in clause %qs", t, name);
4020 remove = true;
4022 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4023 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
4024 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
4026 error ("%qD appears more than once in data clauses", t);
4027 remove = true;
4029 else
4030 bitmap_set_bit (&generic_head, DECL_UID (t));
4031 break;
4033 case OMP_CLAUSE_FIRSTPRIVATE:
4034 t = OMP_CLAUSE_DECL (c);
4035 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4037 if (processing_template_decl)
4038 break;
4039 if (DECL_P (t))
4040 error ("%qD is not a variable in clause %<firstprivate%>", t);
4041 else
4042 error ("%qE is not a variable in clause %<firstprivate%>", t);
4043 remove = true;
4045 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4046 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4048 error ("%qD appears more than once in data clauses", t);
4049 remove = true;
4051 else
4052 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
4053 break;
4055 case OMP_CLAUSE_LASTPRIVATE:
4056 t = OMP_CLAUSE_DECL (c);
4057 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4059 if (processing_template_decl)
4060 break;
4061 if (DECL_P (t))
4062 error ("%qD is not a variable in clause %<lastprivate%>", t);
4063 else
4064 error ("%qE is not a variable in clause %<lastprivate%>", t);
4065 remove = true;
4067 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4068 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
4070 error ("%qD appears more than once in data clauses", t);
4071 remove = true;
4073 else
4074 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
4075 break;
4077 case OMP_CLAUSE_IF:
4078 t = OMP_CLAUSE_IF_EXPR (c);
4079 t = maybe_convert_cond (t);
4080 if (t == error_mark_node)
4081 remove = true;
4082 else if (!processing_template_decl)
4083 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4084 OMP_CLAUSE_IF_EXPR (c) = t;
4085 break;
4087 case OMP_CLAUSE_FINAL:
4088 t = OMP_CLAUSE_FINAL_EXPR (c);
4089 t = maybe_convert_cond (t);
4090 if (t == error_mark_node)
4091 remove = true;
4092 else if (!processing_template_decl)
4093 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4094 OMP_CLAUSE_FINAL_EXPR (c) = t;
4095 break;
4097 case OMP_CLAUSE_NUM_THREADS:
4098 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
4099 if (t == error_mark_node)
4100 remove = true;
4101 else if (!type_dependent_expression_p (t)
4102 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4104 error ("num_threads expression must be integral");
4105 remove = true;
4107 else
4109 t = mark_rvalue_use (t);
4110 if (!processing_template_decl)
4111 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4112 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
4114 break;
4116 case OMP_CLAUSE_SCHEDULE:
4117 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
4118 if (t == NULL)
4120 else if (t == error_mark_node)
4121 remove = true;
4122 else if (!type_dependent_expression_p (t)
4123 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4125 error ("schedule chunk size expression must be integral");
4126 remove = true;
4128 else
4130 t = mark_rvalue_use (t);
4131 if (!processing_template_decl)
4132 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4133 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
4135 break;
4137 case OMP_CLAUSE_NOWAIT:
4138 case OMP_CLAUSE_ORDERED:
4139 case OMP_CLAUSE_DEFAULT:
4140 case OMP_CLAUSE_UNTIED:
4141 case OMP_CLAUSE_COLLAPSE:
4142 case OMP_CLAUSE_MERGEABLE:
4143 break;
4145 default:
4146 gcc_unreachable ();
4149 if (remove)
4150 *pc = OMP_CLAUSE_CHAIN (c);
4151 else
4152 pc = &OMP_CLAUSE_CHAIN (c);
4155 for (pc = &clauses, c = clauses; c ; c = *pc)
4157 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
4158 bool remove = false;
4159 bool need_complete_non_reference = false;
4160 bool need_default_ctor = false;
4161 bool need_copy_ctor = false;
4162 bool need_copy_assignment = false;
4163 bool need_implicitly_determined = false;
4164 tree type, inner_type;
4166 switch (c_kind)
4168 case OMP_CLAUSE_SHARED:
4169 name = "shared";
4170 need_implicitly_determined = true;
4171 break;
4172 case OMP_CLAUSE_PRIVATE:
4173 name = "private";
4174 need_complete_non_reference = true;
4175 need_default_ctor = true;
4176 need_implicitly_determined = true;
4177 break;
4178 case OMP_CLAUSE_FIRSTPRIVATE:
4179 name = "firstprivate";
4180 need_complete_non_reference = true;
4181 need_copy_ctor = true;
4182 need_implicitly_determined = true;
4183 break;
4184 case OMP_CLAUSE_LASTPRIVATE:
4185 name = "lastprivate";
4186 need_complete_non_reference = true;
4187 need_copy_assignment = true;
4188 need_implicitly_determined = true;
4189 break;
4190 case OMP_CLAUSE_REDUCTION:
4191 name = "reduction";
4192 need_implicitly_determined = true;
4193 break;
4194 case OMP_CLAUSE_COPYPRIVATE:
4195 name = "copyprivate";
4196 need_copy_assignment = true;
4197 break;
4198 case OMP_CLAUSE_COPYIN:
4199 name = "copyin";
4200 need_copy_assignment = true;
4201 break;
4202 default:
4203 pc = &OMP_CLAUSE_CHAIN (c);
4204 continue;
4207 t = OMP_CLAUSE_DECL (c);
4208 if (processing_template_decl
4209 && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4211 pc = &OMP_CLAUSE_CHAIN (c);
4212 continue;
4215 switch (c_kind)
4217 case OMP_CLAUSE_LASTPRIVATE:
4218 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4219 need_default_ctor = true;
4220 break;
4222 case OMP_CLAUSE_REDUCTION:
4223 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
4224 || POINTER_TYPE_P (TREE_TYPE (t)))
4226 error ("%qE has invalid type for %<reduction%>", t);
4227 remove = true;
4229 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
4231 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
4232 switch (r_code)
4234 case PLUS_EXPR:
4235 case MULT_EXPR:
4236 case MINUS_EXPR:
4237 case MIN_EXPR:
4238 case MAX_EXPR:
4239 break;
4240 default:
4241 error ("%qE has invalid type for %<reduction(%s)%>",
4242 t, operator_name_info[r_code].name);
4243 remove = true;
4246 break;
4248 case OMP_CLAUSE_COPYIN:
4249 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
4251 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
4252 remove = true;
4254 break;
4256 default:
4257 break;
4260 if (need_complete_non_reference || need_copy_assignment)
4262 t = require_complete_type (t);
4263 if (t == error_mark_node)
4264 remove = true;
4265 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4266 && need_complete_non_reference)
4268 error ("%qE has reference type for %qs", t, name);
4269 remove = true;
4272 if (need_implicitly_determined)
4274 const char *share_name = NULL;
4276 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4277 share_name = "threadprivate";
4278 else switch (cxx_omp_predetermined_sharing (t))
4280 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
4281 break;
4282 case OMP_CLAUSE_DEFAULT_SHARED:
4283 /* const vars may be specified in firstprivate clause. */
4284 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
4285 && cxx_omp_const_qual_no_mutable (t))
4286 break;
4287 share_name = "shared";
4288 break;
4289 case OMP_CLAUSE_DEFAULT_PRIVATE:
4290 share_name = "private";
4291 break;
4292 default:
4293 gcc_unreachable ();
4295 if (share_name)
4297 error ("%qE is predetermined %qs for %qs",
4298 t, share_name, name);
4299 remove = true;
4303 /* We're interested in the base element, not arrays. */
4304 inner_type = type = TREE_TYPE (t);
4305 while (TREE_CODE (inner_type) == ARRAY_TYPE)
4306 inner_type = TREE_TYPE (inner_type);
4308 /* Check for special function availability by building a call to one.
4309 Save the results, because later we won't be in the right context
4310 for making these queries. */
4311 if (CLASS_TYPE_P (inner_type)
4312 && COMPLETE_TYPE_P (inner_type)
4313 && (need_default_ctor || need_copy_ctor || need_copy_assignment)
4314 && !type_dependent_expression_p (t)
4315 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
4316 need_copy_ctor, need_copy_assignment))
4317 remove = true;
4319 if (remove)
4320 *pc = OMP_CLAUSE_CHAIN (c);
4321 else
4322 pc = &OMP_CLAUSE_CHAIN (c);
4325 bitmap_obstack_release (NULL);
4326 return clauses;
4329 /* For all variables in the tree_list VARS, mark them as thread local. */
4331 void
4332 finish_omp_threadprivate (tree vars)
4334 tree t;
4336 /* Mark every variable in VARS to be assigned thread local storage. */
4337 for (t = vars; t; t = TREE_CHAIN (t))
4339 tree v = TREE_PURPOSE (t);
4341 if (error_operand_p (v))
4343 else if (TREE_CODE (v) != VAR_DECL)
4344 error ("%<threadprivate%> %qD is not file, namespace "
4345 "or block scope variable", v);
4346 /* If V had already been marked threadprivate, it doesn't matter
4347 whether it had been used prior to this point. */
4348 else if (TREE_USED (v)
4349 && (DECL_LANG_SPECIFIC (v) == NULL
4350 || !CP_DECL_THREADPRIVATE_P (v)))
4351 error ("%qE declared %<threadprivate%> after first use", v);
4352 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
4353 error ("automatic variable %qE cannot be %<threadprivate%>", v);
4354 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
4355 error ("%<threadprivate%> %qE has incomplete type", v);
4356 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
4357 && CP_DECL_CONTEXT (v) != current_class_type)
4358 error ("%<threadprivate%> %qE directive not "
4359 "in %qT definition", v, CP_DECL_CONTEXT (v));
4360 else
4362 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
4363 if (DECL_LANG_SPECIFIC (v) == NULL)
4365 retrofit_lang_decl (v);
4367 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
4368 after the allocation of the lang_decl structure. */
4369 if (DECL_DISCRIMINATOR_P (v))
4370 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
4373 if (! DECL_THREAD_LOCAL_P (v))
4375 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
4376 /* If rtl has been already set for this var, call
4377 make_decl_rtl once again, so that encode_section_info
4378 has a chance to look at the new decl flags. */
4379 if (DECL_RTL_SET_P (v))
4380 make_decl_rtl (v);
4382 CP_DECL_THREADPRIVATE_P (v) = 1;
4387 /* Build an OpenMP structured block. */
4389 tree
4390 begin_omp_structured_block (void)
4392 return do_pushlevel (sk_omp);
4395 tree
4396 finish_omp_structured_block (tree block)
4398 return do_poplevel (block);
4401 /* Similarly, except force the retention of the BLOCK. */
4403 tree
4404 begin_omp_parallel (void)
4406 keep_next_level (true);
4407 return begin_omp_structured_block ();
4410 tree
4411 finish_omp_parallel (tree clauses, tree body)
4413 tree stmt;
4415 body = finish_omp_structured_block (body);
4417 stmt = make_node (OMP_PARALLEL);
4418 TREE_TYPE (stmt) = void_type_node;
4419 OMP_PARALLEL_CLAUSES (stmt) = clauses;
4420 OMP_PARALLEL_BODY (stmt) = body;
4422 return add_stmt (stmt);
4425 tree
4426 begin_omp_task (void)
4428 keep_next_level (true);
4429 return begin_omp_structured_block ();
4432 tree
4433 finish_omp_task (tree clauses, tree body)
4435 tree stmt;
4437 body = finish_omp_structured_block (body);
4439 stmt = make_node (OMP_TASK);
4440 TREE_TYPE (stmt) = void_type_node;
4441 OMP_TASK_CLAUSES (stmt) = clauses;
4442 OMP_TASK_BODY (stmt) = body;
4444 return add_stmt (stmt);
4447 /* Helper function for finish_omp_for. Convert Ith random access iterator
4448 into integral iterator. Return FALSE if successful. */
4450 static bool
4451 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
4452 tree condv, tree incrv, tree *body,
4453 tree *pre_body, tree clauses)
4455 tree diff, iter_init, iter_incr = NULL, last;
4456 tree incr_var = NULL, orig_pre_body, orig_body, c;
4457 tree decl = TREE_VEC_ELT (declv, i);
4458 tree init = TREE_VEC_ELT (initv, i);
4459 tree cond = TREE_VEC_ELT (condv, i);
4460 tree incr = TREE_VEC_ELT (incrv, i);
4461 tree iter = decl;
4462 location_t elocus = locus;
4464 if (init && EXPR_HAS_LOCATION (init))
4465 elocus = EXPR_LOCATION (init);
4467 switch (TREE_CODE (cond))
4469 case GT_EXPR:
4470 case GE_EXPR:
4471 case LT_EXPR:
4472 case LE_EXPR:
4473 if (TREE_OPERAND (cond, 1) == iter)
4474 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
4475 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
4476 if (TREE_OPERAND (cond, 0) != iter)
4477 cond = error_mark_node;
4478 else
4480 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
4481 TREE_CODE (cond),
4482 iter, ERROR_MARK,
4483 TREE_OPERAND (cond, 1), ERROR_MARK,
4484 NULL, tf_warning_or_error);
4485 if (error_operand_p (tem))
4486 return true;
4488 break;
4489 default:
4490 cond = error_mark_node;
4491 break;
4493 if (cond == error_mark_node)
4495 error_at (elocus, "invalid controlling predicate");
4496 return true;
4498 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
4499 ERROR_MARK, iter, ERROR_MARK, NULL,
4500 tf_warning_or_error);
4501 if (error_operand_p (diff))
4502 return true;
4503 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
4505 error_at (elocus, "difference between %qE and %qD does not have integer type",
4506 TREE_OPERAND (cond, 1), iter);
4507 return true;
4510 switch (TREE_CODE (incr))
4512 case PREINCREMENT_EXPR:
4513 case PREDECREMENT_EXPR:
4514 case POSTINCREMENT_EXPR:
4515 case POSTDECREMENT_EXPR:
4516 if (TREE_OPERAND (incr, 0) != iter)
4518 incr = error_mark_node;
4519 break;
4521 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
4522 TREE_CODE (incr), iter,
4523 tf_warning_or_error);
4524 if (error_operand_p (iter_incr))
4525 return true;
4526 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
4527 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
4528 incr = integer_one_node;
4529 else
4530 incr = integer_minus_one_node;
4531 break;
4532 case MODIFY_EXPR:
4533 if (TREE_OPERAND (incr, 0) != iter)
4534 incr = error_mark_node;
4535 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
4536 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
4538 tree rhs = TREE_OPERAND (incr, 1);
4539 if (TREE_OPERAND (rhs, 0) == iter)
4541 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
4542 != INTEGER_TYPE)
4543 incr = error_mark_node;
4544 else
4546 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4547 iter, TREE_CODE (rhs),
4548 TREE_OPERAND (rhs, 1),
4549 tf_warning_or_error);
4550 if (error_operand_p (iter_incr))
4551 return true;
4552 incr = TREE_OPERAND (rhs, 1);
4553 incr = cp_convert (TREE_TYPE (diff), incr,
4554 tf_warning_or_error);
4555 if (TREE_CODE (rhs) == MINUS_EXPR)
4557 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
4558 incr = fold_if_not_in_template (incr);
4560 if (TREE_CODE (incr) != INTEGER_CST
4561 && (TREE_CODE (incr) != NOP_EXPR
4562 || (TREE_CODE (TREE_OPERAND (incr, 0))
4563 != INTEGER_CST)))
4564 iter_incr = NULL;
4567 else if (TREE_OPERAND (rhs, 1) == iter)
4569 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
4570 || TREE_CODE (rhs) != PLUS_EXPR)
4571 incr = error_mark_node;
4572 else
4574 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
4575 PLUS_EXPR,
4576 TREE_OPERAND (rhs, 0),
4577 ERROR_MARK, iter,
4578 ERROR_MARK, NULL,
4579 tf_warning_or_error);
4580 if (error_operand_p (iter_incr))
4581 return true;
4582 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4583 iter, NOP_EXPR,
4584 iter_incr,
4585 tf_warning_or_error);
4586 if (error_operand_p (iter_incr))
4587 return true;
4588 incr = TREE_OPERAND (rhs, 0);
4589 iter_incr = NULL;
4592 else
4593 incr = error_mark_node;
4595 else
4596 incr = error_mark_node;
4597 break;
4598 default:
4599 incr = error_mark_node;
4600 break;
4603 if (incr == error_mark_node)
4605 error_at (elocus, "invalid increment expression");
4606 return true;
4609 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
4610 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
4611 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
4612 && OMP_CLAUSE_DECL (c) == iter)
4613 break;
4615 decl = create_temporary_var (TREE_TYPE (diff));
4616 pushdecl (decl);
4617 add_decl_expr (decl);
4618 last = create_temporary_var (TREE_TYPE (diff));
4619 pushdecl (last);
4620 add_decl_expr (last);
4621 if (c && iter_incr == NULL)
4623 incr_var = create_temporary_var (TREE_TYPE (diff));
4624 pushdecl (incr_var);
4625 add_decl_expr (incr_var);
4627 gcc_assert (stmts_are_full_exprs_p ());
4629 orig_pre_body = *pre_body;
4630 *pre_body = push_stmt_list ();
4631 if (orig_pre_body)
4632 add_stmt (orig_pre_body);
4633 if (init != NULL)
4634 finish_expr_stmt (build_x_modify_expr (elocus,
4635 iter, NOP_EXPR, init,
4636 tf_warning_or_error));
4637 init = build_int_cst (TREE_TYPE (diff), 0);
4638 if (c && iter_incr == NULL)
4640 finish_expr_stmt (build_x_modify_expr (elocus,
4641 incr_var, NOP_EXPR,
4642 incr, tf_warning_or_error));
4643 incr = incr_var;
4644 iter_incr = build_x_modify_expr (elocus,
4645 iter, PLUS_EXPR, incr,
4646 tf_warning_or_error);
4648 finish_expr_stmt (build_x_modify_expr (elocus,
4649 last, NOP_EXPR, init,
4650 tf_warning_or_error));
4651 *pre_body = pop_stmt_list (*pre_body);
4653 cond = cp_build_binary_op (elocus,
4654 TREE_CODE (cond), decl, diff,
4655 tf_warning_or_error);
4656 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
4657 elocus, incr, NULL_TREE);
4659 orig_body = *body;
4660 *body = push_stmt_list ();
4661 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
4662 iter_init = build_x_modify_expr (elocus,
4663 iter, PLUS_EXPR, iter_init,
4664 tf_warning_or_error);
4665 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
4666 finish_expr_stmt (iter_init);
4667 finish_expr_stmt (build_x_modify_expr (elocus,
4668 last, NOP_EXPR, decl,
4669 tf_warning_or_error));
4670 add_stmt (orig_body);
4671 *body = pop_stmt_list (*body);
4673 if (c)
4675 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
4676 finish_expr_stmt (iter_incr);
4677 OMP_CLAUSE_LASTPRIVATE_STMT (c)
4678 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
4681 TREE_VEC_ELT (declv, i) = decl;
4682 TREE_VEC_ELT (initv, i) = init;
4683 TREE_VEC_ELT (condv, i) = cond;
4684 TREE_VEC_ELT (incrv, i) = incr;
4686 return false;
4689 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
4690 are directly for their associated operands in the statement. DECL
4691 and INIT are a combo; if DECL is NULL then INIT ought to be a
4692 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
4693 optional statements that need to go before the loop into its
4694 sk_omp scope. */
4696 tree
4697 finish_omp_for (location_t locus, tree declv, tree initv, tree condv,
4698 tree incrv, tree body, tree pre_body, tree clauses)
4700 tree omp_for = NULL, orig_incr = NULL;
4701 tree decl, init, cond, incr;
4702 location_t elocus;
4703 int i;
4705 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
4706 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
4707 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
4708 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4710 decl = TREE_VEC_ELT (declv, i);
4711 init = TREE_VEC_ELT (initv, i);
4712 cond = TREE_VEC_ELT (condv, i);
4713 incr = TREE_VEC_ELT (incrv, i);
4714 elocus = locus;
4716 if (decl == NULL)
4718 if (init != NULL)
4719 switch (TREE_CODE (init))
4721 case MODIFY_EXPR:
4722 decl = TREE_OPERAND (init, 0);
4723 init = TREE_OPERAND (init, 1);
4724 break;
4725 case MODOP_EXPR:
4726 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
4728 decl = TREE_OPERAND (init, 0);
4729 init = TREE_OPERAND (init, 2);
4731 break;
4732 default:
4733 break;
4736 if (decl == NULL)
4738 error_at (locus,
4739 "expected iteration declaration or initialization");
4740 return NULL;
4744 if (init && EXPR_HAS_LOCATION (init))
4745 elocus = EXPR_LOCATION (init);
4747 if (cond == NULL)
4749 error_at (elocus, "missing controlling predicate");
4750 return NULL;
4753 if (incr == NULL)
4755 error_at (elocus, "missing increment expression");
4756 return NULL;
4759 TREE_VEC_ELT (declv, i) = decl;
4760 TREE_VEC_ELT (initv, i) = init;
4763 if (dependent_omp_for_p (declv, initv, condv, incrv))
4765 tree stmt;
4767 stmt = make_node (OMP_FOR);
4769 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4771 /* This is really just a place-holder. We'll be decomposing this
4772 again and going through the cp_build_modify_expr path below when
4773 we instantiate the thing. */
4774 TREE_VEC_ELT (initv, i)
4775 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
4776 TREE_VEC_ELT (initv, i));
4779 TREE_TYPE (stmt) = void_type_node;
4780 OMP_FOR_INIT (stmt) = initv;
4781 OMP_FOR_COND (stmt) = condv;
4782 OMP_FOR_INCR (stmt) = incrv;
4783 OMP_FOR_BODY (stmt) = body;
4784 OMP_FOR_PRE_BODY (stmt) = pre_body;
4785 OMP_FOR_CLAUSES (stmt) = clauses;
4787 SET_EXPR_LOCATION (stmt, locus);
4788 return add_stmt (stmt);
4791 if (processing_template_decl)
4792 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
4794 for (i = 0; i < TREE_VEC_LENGTH (declv); )
4796 decl = TREE_VEC_ELT (declv, i);
4797 init = TREE_VEC_ELT (initv, i);
4798 cond = TREE_VEC_ELT (condv, i);
4799 incr = TREE_VEC_ELT (incrv, i);
4800 if (orig_incr)
4801 TREE_VEC_ELT (orig_incr, i) = incr;
4802 elocus = locus;
4804 if (init && EXPR_HAS_LOCATION (init))
4805 elocus = EXPR_LOCATION (init);
4807 if (!DECL_P (decl))
4809 error_at (elocus, "expected iteration declaration or initialization");
4810 return NULL;
4813 if (incr && TREE_CODE (incr) == MODOP_EXPR)
4815 if (orig_incr)
4816 TREE_VEC_ELT (orig_incr, i) = incr;
4817 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
4818 TREE_CODE (TREE_OPERAND (incr, 1)),
4819 TREE_OPERAND (incr, 2),
4820 tf_warning_or_error);
4823 if (CLASS_TYPE_P (TREE_TYPE (decl)))
4825 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
4826 incrv, &body, &pre_body, clauses))
4827 return NULL;
4828 continue;
4831 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
4832 && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
4834 error_at (elocus, "invalid type for iteration variable %qE", decl);
4835 return NULL;
4838 if (!processing_template_decl)
4840 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
4841 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
4843 else
4844 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
4845 if (cond
4846 && TREE_SIDE_EFFECTS (cond)
4847 && COMPARISON_CLASS_P (cond)
4848 && !processing_template_decl)
4850 tree t = TREE_OPERAND (cond, 0);
4851 if (TREE_SIDE_EFFECTS (t)
4852 && t != decl
4853 && (TREE_CODE (t) != NOP_EXPR
4854 || TREE_OPERAND (t, 0) != decl))
4855 TREE_OPERAND (cond, 0)
4856 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4858 t = TREE_OPERAND (cond, 1);
4859 if (TREE_SIDE_EFFECTS (t)
4860 && t != decl
4861 && (TREE_CODE (t) != NOP_EXPR
4862 || TREE_OPERAND (t, 0) != decl))
4863 TREE_OPERAND (cond, 1)
4864 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4866 if (decl == error_mark_node || init == error_mark_node)
4867 return NULL;
4869 TREE_VEC_ELT (declv, i) = decl;
4870 TREE_VEC_ELT (initv, i) = init;
4871 TREE_VEC_ELT (condv, i) = cond;
4872 TREE_VEC_ELT (incrv, i) = incr;
4873 i++;
4876 if (IS_EMPTY_STMT (pre_body))
4877 pre_body = NULL;
4879 omp_for = c_finish_omp_for (locus, declv, initv, condv, incrv,
4880 body, pre_body);
4882 if (omp_for == NULL)
4883 return NULL;
4885 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
4887 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
4888 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
4890 if (TREE_CODE (incr) != MODIFY_EXPR)
4891 continue;
4893 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
4894 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
4895 && !processing_template_decl)
4897 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
4898 if (TREE_SIDE_EFFECTS (t)
4899 && t != decl
4900 && (TREE_CODE (t) != NOP_EXPR
4901 || TREE_OPERAND (t, 0) != decl))
4902 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
4903 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4905 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
4906 if (TREE_SIDE_EFFECTS (t)
4907 && t != decl
4908 && (TREE_CODE (t) != NOP_EXPR
4909 || TREE_OPERAND (t, 0) != decl))
4910 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
4911 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4914 if (orig_incr)
4915 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
4917 if (omp_for != NULL)
4918 OMP_FOR_CLAUSES (omp_for) = clauses;
4919 return omp_for;
4922 void
4923 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
4924 tree rhs, tree v, tree lhs1, tree rhs1)
4926 tree orig_lhs;
4927 tree orig_rhs;
4928 tree orig_v;
4929 tree orig_lhs1;
4930 tree orig_rhs1;
4931 bool dependent_p;
4932 tree stmt;
4934 orig_lhs = lhs;
4935 orig_rhs = rhs;
4936 orig_v = v;
4937 orig_lhs1 = lhs1;
4938 orig_rhs1 = rhs1;
4939 dependent_p = false;
4940 stmt = NULL_TREE;
4942 /* Even in a template, we can detect invalid uses of the atomic
4943 pragma if neither LHS nor RHS is type-dependent. */
4944 if (processing_template_decl)
4946 dependent_p = (type_dependent_expression_p (lhs)
4947 || (rhs && type_dependent_expression_p (rhs))
4948 || (v && type_dependent_expression_p (v))
4949 || (lhs1 && type_dependent_expression_p (lhs1))
4950 || (rhs1 && type_dependent_expression_p (rhs1)));
4951 if (!dependent_p)
4953 lhs = build_non_dependent_expr (lhs);
4954 if (rhs)
4955 rhs = build_non_dependent_expr (rhs);
4956 if (v)
4957 v = build_non_dependent_expr (v);
4958 if (lhs1)
4959 lhs1 = build_non_dependent_expr (lhs1);
4960 if (rhs1)
4961 rhs1 = build_non_dependent_expr (rhs1);
4964 if (!dependent_p)
4966 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
4967 v, lhs1, rhs1);
4968 if (stmt == error_mark_node)
4969 return;
4971 if (processing_template_decl)
4973 if (code == OMP_ATOMIC_READ)
4975 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
4976 OMP_ATOMIC_READ, orig_lhs);
4977 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
4979 else
4981 if (opcode == NOP_EXPR)
4982 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
4983 else
4984 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
4985 if (orig_rhs1)
4986 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
4987 COMPOUND_EXPR, orig_rhs1, stmt);
4988 if (code != OMP_ATOMIC)
4990 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
4991 code, orig_lhs1, stmt);
4992 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
4995 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
4997 add_stmt (stmt);
5000 void
5001 finish_omp_barrier (void)
5003 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
5004 vec<tree, va_gc> *vec = make_tree_vector ();
5005 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5006 release_tree_vector (vec);
5007 finish_expr_stmt (stmt);
5010 void
5011 finish_omp_flush (void)
5013 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
5014 vec<tree, va_gc> *vec = make_tree_vector ();
5015 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5016 release_tree_vector (vec);
5017 finish_expr_stmt (stmt);
5020 void
5021 finish_omp_taskwait (void)
5023 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
5024 vec<tree, va_gc> *vec = make_tree_vector ();
5025 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5026 release_tree_vector (vec);
5027 finish_expr_stmt (stmt);
5030 void
5031 finish_omp_taskyield (void)
5033 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
5034 vec<tree, va_gc> *vec = make_tree_vector ();
5035 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5036 release_tree_vector (vec);
5037 finish_expr_stmt (stmt);
5040 /* Begin a __transaction_atomic or __transaction_relaxed statement.
5041 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
5042 should create an extra compound stmt. */
5044 tree
5045 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
5047 tree r;
5049 if (pcompound)
5050 *pcompound = begin_compound_stmt (0);
5052 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
5054 /* Only add the statement to the function if support enabled. */
5055 if (flag_tm)
5056 add_stmt (r);
5057 else
5058 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
5059 ? G_("%<__transaction_relaxed%> without "
5060 "transactional memory support enabled")
5061 : G_("%<__transaction_atomic%> without "
5062 "transactional memory support enabled")));
5064 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
5065 return r;
5068 /* End a __transaction_atomic or __transaction_relaxed statement.
5069 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
5070 and we should end the compound. If NOEX is non-NULL, we wrap the body in
5071 a MUST_NOT_THROW_EXPR with NOEX as condition. */
5073 void
5074 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
5076 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
5077 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
5078 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
5079 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
5081 /* noexcept specifications are not allowed for function transactions. */
5082 gcc_assert (!(noex && compound_stmt));
5083 if (noex)
5085 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
5086 noex);
5087 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
5088 TREE_SIDE_EFFECTS (body) = 1;
5089 TRANSACTION_EXPR_BODY (stmt) = body;
5092 if (compound_stmt)
5093 finish_compound_stmt (compound_stmt);
5094 finish_stmt ();
5097 /* Build a __transaction_atomic or __transaction_relaxed expression. If
5098 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
5099 condition. */
5101 tree
5102 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
5104 tree ret;
5105 if (noex)
5107 expr = build_must_not_throw_expr (expr, noex);
5108 SET_EXPR_LOCATION (expr, loc);
5109 TREE_SIDE_EFFECTS (expr) = 1;
5111 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
5112 if (flags & TM_STMT_ATTR_RELAXED)
5113 TRANSACTION_EXPR_RELAXED (ret) = 1;
5114 SET_EXPR_LOCATION (ret, loc);
5115 return ret;
5118 void
5119 init_cp_semantics (void)
5123 /* Build a STATIC_ASSERT for a static assertion with the condition
5124 CONDITION and the message text MESSAGE. LOCATION is the location
5125 of the static assertion in the source code. When MEMBER_P, this
5126 static assertion is a member of a class. */
5127 void
5128 finish_static_assert (tree condition, tree message, location_t location,
5129 bool member_p)
5131 if (message == NULL_TREE
5132 || message == error_mark_node
5133 || condition == NULL_TREE
5134 || condition == error_mark_node)
5135 return;
5137 if (check_for_bare_parameter_packs (condition))
5138 condition = error_mark_node;
5140 if (type_dependent_expression_p (condition)
5141 || value_dependent_expression_p (condition))
5143 /* We're in a template; build a STATIC_ASSERT and put it in
5144 the right place. */
5145 tree assertion;
5147 assertion = make_node (STATIC_ASSERT);
5148 STATIC_ASSERT_CONDITION (assertion) = condition;
5149 STATIC_ASSERT_MESSAGE (assertion) = message;
5150 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
5152 if (member_p)
5153 maybe_add_class_template_decl_list (current_class_type,
5154 assertion,
5155 /*friend_p=*/0);
5156 else
5157 add_stmt (assertion);
5159 return;
5162 /* Fold the expression and convert it to a boolean value. */
5163 condition = fold_non_dependent_expr (condition);
5164 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
5165 condition = maybe_constant_value (condition);
5167 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
5168 /* Do nothing; the condition is satisfied. */
5170 else
5172 location_t saved_loc = input_location;
5174 input_location = location;
5175 if (TREE_CODE (condition) == INTEGER_CST
5176 && integer_zerop (condition))
5177 /* Report the error. */
5178 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
5179 else if (condition && condition != error_mark_node)
5181 error ("non-constant condition for static assertion");
5182 cxx_constant_value (condition);
5184 input_location = saved_loc;
5188 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
5189 suitable for use as a type-specifier.
5191 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
5192 id-expression or a class member access, FALSE when it was parsed as
5193 a full expression. */
5195 tree
5196 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
5197 tsubst_flags_t complain)
5199 tree type = NULL_TREE;
5201 if (!expr || error_operand_p (expr))
5202 return error_mark_node;
5204 if (TYPE_P (expr)
5205 || TREE_CODE (expr) == TYPE_DECL
5206 || (TREE_CODE (expr) == BIT_NOT_EXPR
5207 && TYPE_P (TREE_OPERAND (expr, 0))))
5209 if (complain & tf_error)
5210 error ("argument to decltype must be an expression");
5211 return error_mark_node;
5214 /* Depending on the resolution of DR 1172, we may later need to distinguish
5215 instantiation-dependent but not type-dependent expressions so that, say,
5216 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
5217 if (instantiation_dependent_expression_p (expr))
5219 type = cxx_make_type (DECLTYPE_TYPE);
5220 DECLTYPE_TYPE_EXPR (type) = expr;
5221 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
5222 = id_expression_or_member_access_p;
5223 SET_TYPE_STRUCTURAL_EQUALITY (type);
5225 return type;
5228 /* The type denoted by decltype(e) is defined as follows: */
5230 expr = resolve_nondeduced_context (expr);
5232 if (type_unknown_p (expr))
5234 if (complain & tf_error)
5235 error ("decltype cannot resolve address of overloaded function");
5236 return error_mark_node;
5239 if (invalid_nonstatic_memfn_p (expr, complain))
5240 return error_mark_node;
5242 /* To get the size of a static data member declared as an array of
5243 unknown bound, we need to instantiate it. */
5244 if (TREE_CODE (expr) == VAR_DECL
5245 && VAR_HAD_UNKNOWN_BOUND (expr)
5246 && DECL_TEMPLATE_INSTANTIATION (expr))
5247 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
5249 if (id_expression_or_member_access_p)
5251 /* If e is an id-expression or a class member access (5.2.5
5252 [expr.ref]), decltype(e) is defined as the type of the entity
5253 named by e. If there is no such entity, or e names a set of
5254 overloaded functions, the program is ill-formed. */
5255 if (TREE_CODE (expr) == IDENTIFIER_NODE)
5256 expr = lookup_name (expr);
5258 if (TREE_CODE (expr) == INDIRECT_REF)
5259 /* This can happen when the expression is, e.g., "a.b". Just
5260 look at the underlying operand. */
5261 expr = TREE_OPERAND (expr, 0);
5263 if (TREE_CODE (expr) == OFFSET_REF
5264 || TREE_CODE (expr) == MEMBER_REF)
5265 /* We're only interested in the field itself. If it is a
5266 BASELINK, we will need to see through it in the next
5267 step. */
5268 expr = TREE_OPERAND (expr, 1);
5270 if (BASELINK_P (expr))
5271 /* See through BASELINK nodes to the underlying function. */
5272 expr = BASELINK_FUNCTIONS (expr);
5274 switch (TREE_CODE (expr))
5276 case FIELD_DECL:
5277 if (DECL_BIT_FIELD_TYPE (expr))
5279 type = DECL_BIT_FIELD_TYPE (expr);
5280 break;
5282 /* Fall through for fields that aren't bitfields. */
5284 case FUNCTION_DECL:
5285 case VAR_DECL:
5286 case CONST_DECL:
5287 case PARM_DECL:
5288 case RESULT_DECL:
5289 case TEMPLATE_PARM_INDEX:
5290 expr = mark_type_use (expr);
5291 type = TREE_TYPE (expr);
5292 break;
5294 case ERROR_MARK:
5295 type = error_mark_node;
5296 break;
5298 case COMPONENT_REF:
5299 mark_type_use (expr);
5300 type = is_bitfield_expr_with_lowered_type (expr);
5301 if (!type)
5302 type = TREE_TYPE (TREE_OPERAND (expr, 1));
5303 break;
5305 case BIT_FIELD_REF:
5306 gcc_unreachable ();
5308 case INTEGER_CST:
5309 case PTRMEM_CST:
5310 /* We can get here when the id-expression refers to an
5311 enumerator or non-type template parameter. */
5312 type = TREE_TYPE (expr);
5313 break;
5315 default:
5316 gcc_unreachable ();
5317 return error_mark_node;
5320 else
5322 /* Within a lambda-expression:
5324 Every occurrence of decltype((x)) where x is a possibly
5325 parenthesized id-expression that names an entity of
5326 automatic storage duration is treated as if x were
5327 transformed into an access to a corresponding data member
5328 of the closure type that would have been declared if x
5329 were a use of the denoted entity. */
5330 if (outer_automatic_var_p (expr)
5331 && current_function_decl
5332 && LAMBDA_FUNCTION_P (current_function_decl))
5333 type = capture_decltype (expr);
5334 else if (error_operand_p (expr))
5335 type = error_mark_node;
5336 else if (expr == current_class_ptr)
5337 /* If the expression is just "this", we want the
5338 cv-unqualified pointer for the "this" type. */
5339 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
5340 else
5342 /* Otherwise, where T is the type of e, if e is an lvalue,
5343 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
5344 cp_lvalue_kind clk = lvalue_kind (expr);
5345 type = unlowered_expr_type (expr);
5346 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5348 /* For vector types, pick a non-opaque variant. */
5349 if (TREE_CODE (type) == VECTOR_TYPE)
5350 type = strip_typedefs (type);
5352 if (clk != clk_none && !(clk & clk_class))
5353 type = cp_build_reference_type (type, (clk & clk_rvalueref));
5357 return type;
5360 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
5361 __has_nothrow_copy, depending on assign_p. */
5363 static bool
5364 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
5366 tree fns;
5368 if (assign_p)
5370 int ix;
5371 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
5372 if (ix < 0)
5373 return false;
5374 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
5376 else if (TYPE_HAS_COPY_CTOR (type))
5378 /* If construction of the copy constructor was postponed, create
5379 it now. */
5380 if (CLASSTYPE_LAZY_COPY_CTOR (type))
5381 lazily_declare_fn (sfk_copy_constructor, type);
5382 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
5383 lazily_declare_fn (sfk_move_constructor, type);
5384 fns = CLASSTYPE_CONSTRUCTORS (type);
5386 else
5387 return false;
5389 for (; fns; fns = OVL_NEXT (fns))
5391 tree fn = OVL_CURRENT (fns);
5393 if (assign_p)
5395 if (copy_fn_p (fn) == 0)
5396 continue;
5398 else if (copy_fn_p (fn) <= 0)
5399 continue;
5401 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
5402 return false;
5405 return true;
5408 /* Actually evaluates the trait. */
5410 static bool
5411 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
5413 enum tree_code type_code1;
5414 tree t;
5416 type_code1 = TREE_CODE (type1);
5418 switch (kind)
5420 case CPTK_HAS_NOTHROW_ASSIGN:
5421 type1 = strip_array_types (type1);
5422 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5423 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
5424 || (CLASS_TYPE_P (type1)
5425 && classtype_has_nothrow_assign_or_copy_p (type1,
5426 true))));
5428 case CPTK_HAS_TRIVIAL_ASSIGN:
5429 /* ??? The standard seems to be missing the "or array of such a class
5430 type" wording for this trait. */
5431 type1 = strip_array_types (type1);
5432 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5433 && (trivial_type_p (type1)
5434 || (CLASS_TYPE_P (type1)
5435 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
5437 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5438 type1 = strip_array_types (type1);
5439 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
5440 || (CLASS_TYPE_P (type1)
5441 && (t = locate_ctor (type1))
5442 && TYPE_NOTHROW_P (TREE_TYPE (t))));
5444 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5445 type1 = strip_array_types (type1);
5446 return (trivial_type_p (type1)
5447 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
5449 case CPTK_HAS_NOTHROW_COPY:
5450 type1 = strip_array_types (type1);
5451 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
5452 || (CLASS_TYPE_P (type1)
5453 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
5455 case CPTK_HAS_TRIVIAL_COPY:
5456 /* ??? The standard seems to be missing the "or array of such a class
5457 type" wording for this trait. */
5458 type1 = strip_array_types (type1);
5459 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5460 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
5462 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5463 type1 = strip_array_types (type1);
5464 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5465 || (CLASS_TYPE_P (type1)
5466 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
5468 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5469 return type_has_virtual_destructor (type1);
5471 case CPTK_IS_ABSTRACT:
5472 return (ABSTRACT_CLASS_TYPE_P (type1));
5474 case CPTK_IS_BASE_OF:
5475 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5476 && DERIVED_FROM_P (type1, type2));
5478 case CPTK_IS_CLASS:
5479 return (NON_UNION_CLASS_TYPE_P (type1));
5481 case CPTK_IS_CONVERTIBLE_TO:
5482 /* TODO */
5483 return false;
5485 case CPTK_IS_EMPTY:
5486 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
5488 case CPTK_IS_ENUM:
5489 return (type_code1 == ENUMERAL_TYPE);
5491 case CPTK_IS_FINAL:
5492 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
5494 case CPTK_IS_LITERAL_TYPE:
5495 return (literal_type_p (type1));
5497 case CPTK_IS_POD:
5498 return (pod_type_p (type1));
5500 case CPTK_IS_POLYMORPHIC:
5501 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
5503 case CPTK_IS_STD_LAYOUT:
5504 return (std_layout_type_p (type1));
5506 case CPTK_IS_TRIVIAL:
5507 return (trivial_type_p (type1));
5509 case CPTK_IS_UNION:
5510 return (type_code1 == UNION_TYPE);
5512 default:
5513 gcc_unreachable ();
5514 return false;
5518 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
5519 void, or a complete type, returns it, otherwise NULL_TREE. */
5521 static tree
5522 check_trait_type (tree type)
5524 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
5525 && COMPLETE_TYPE_P (TREE_TYPE (type)))
5526 return type;
5528 if (VOID_TYPE_P (type))
5529 return type;
5531 return complete_type_or_else (strip_array_types (type), NULL_TREE);
5534 /* Process a trait expression. */
5536 tree
5537 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
5539 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
5540 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
5541 || kind == CPTK_HAS_NOTHROW_COPY
5542 || kind == CPTK_HAS_TRIVIAL_ASSIGN
5543 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
5544 || kind == CPTK_HAS_TRIVIAL_COPY
5545 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
5546 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
5547 || kind == CPTK_IS_ABSTRACT
5548 || kind == CPTK_IS_BASE_OF
5549 || kind == CPTK_IS_CLASS
5550 || kind == CPTK_IS_CONVERTIBLE_TO
5551 || kind == CPTK_IS_EMPTY
5552 || kind == CPTK_IS_ENUM
5553 || kind == CPTK_IS_FINAL
5554 || kind == CPTK_IS_LITERAL_TYPE
5555 || kind == CPTK_IS_POD
5556 || kind == CPTK_IS_POLYMORPHIC
5557 || kind == CPTK_IS_STD_LAYOUT
5558 || kind == CPTK_IS_TRIVIAL
5559 || kind == CPTK_IS_UNION);
5561 if (kind == CPTK_IS_CONVERTIBLE_TO)
5563 sorry ("__is_convertible_to");
5564 return error_mark_node;
5567 if (type1 == error_mark_node
5568 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
5569 && type2 == error_mark_node))
5570 return error_mark_node;
5572 if (processing_template_decl)
5574 tree trait_expr = make_node (TRAIT_EXPR);
5575 TREE_TYPE (trait_expr) = boolean_type_node;
5576 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
5577 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
5578 TRAIT_EXPR_KIND (trait_expr) = kind;
5579 return trait_expr;
5582 switch (kind)
5584 case CPTK_HAS_NOTHROW_ASSIGN:
5585 case CPTK_HAS_TRIVIAL_ASSIGN:
5586 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5587 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5588 case CPTK_HAS_NOTHROW_COPY:
5589 case CPTK_HAS_TRIVIAL_COPY:
5590 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5591 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5592 case CPTK_IS_ABSTRACT:
5593 case CPTK_IS_EMPTY:
5594 case CPTK_IS_FINAL:
5595 case CPTK_IS_LITERAL_TYPE:
5596 case CPTK_IS_POD:
5597 case CPTK_IS_POLYMORPHIC:
5598 case CPTK_IS_STD_LAYOUT:
5599 case CPTK_IS_TRIVIAL:
5600 if (!check_trait_type (type1))
5601 return error_mark_node;
5602 break;
5604 case CPTK_IS_BASE_OF:
5605 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5606 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
5607 && !complete_type_or_else (type2, NULL_TREE))
5608 /* We already issued an error. */
5609 return error_mark_node;
5610 break;
5612 case CPTK_IS_CLASS:
5613 case CPTK_IS_ENUM:
5614 case CPTK_IS_UNION:
5615 break;
5617 case CPTK_IS_CONVERTIBLE_TO:
5618 default:
5619 gcc_unreachable ();
5622 return (trait_expr_value (kind, type1, type2)
5623 ? boolean_true_node : boolean_false_node);
5626 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
5627 which is ignored for C++. */
5629 void
5630 set_float_const_decimal64 (void)
5634 void
5635 clear_float_const_decimal64 (void)
5639 bool
5640 float_const_decimal64_p (void)
5642 return 0;
5646 /* Return true if T is a literal type. */
5648 bool
5649 literal_type_p (tree t)
5651 if (SCALAR_TYPE_P (t)
5652 || TREE_CODE (t) == VECTOR_TYPE
5653 || TREE_CODE (t) == REFERENCE_TYPE)
5654 return true;
5655 if (CLASS_TYPE_P (t))
5657 t = complete_type (t);
5658 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
5659 return CLASSTYPE_LITERAL_P (t);
5661 if (TREE_CODE (t) == ARRAY_TYPE)
5662 return literal_type_p (strip_array_types (t));
5663 return false;
5666 /* If DECL is a variable declared `constexpr', require its type
5667 be literal. Return the DECL if OK, otherwise NULL. */
5669 tree
5670 ensure_literal_type_for_constexpr_object (tree decl)
5672 tree type = TREE_TYPE (decl);
5673 if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONSTEXPR_P (decl)
5674 && !processing_template_decl)
5676 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
5677 /* Don't complain here, we'll complain about incompleteness
5678 when we try to initialize the variable. */;
5679 else if (!literal_type_p (type))
5681 error ("the type %qT of constexpr variable %qD is not literal",
5682 type, decl);
5683 explain_non_literal_class (type);
5684 return NULL;
5687 return decl;
5690 /* Representation of entries in the constexpr function definition table. */
5692 typedef struct GTY(()) constexpr_fundef {
5693 tree decl;
5694 tree body;
5695 } constexpr_fundef;
5697 /* This table holds all constexpr function definitions seen in
5698 the current translation unit. */
5700 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
5702 /* Utility function used for managing the constexpr function table.
5703 Return true if the entries pointed to by P and Q are for the
5704 same constexpr function. */
5706 static inline int
5707 constexpr_fundef_equal (const void *p, const void *q)
5709 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
5710 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
5711 return lhs->decl == rhs->decl;
5714 /* Utility function used for managing the constexpr function table.
5715 Return a hash value for the entry pointed to by Q. */
5717 static inline hashval_t
5718 constexpr_fundef_hash (const void *p)
5720 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
5721 return DECL_UID (fundef->decl);
5724 /* Return a previously saved definition of function FUN. */
5726 static constexpr_fundef *
5727 retrieve_constexpr_fundef (tree fun)
5729 constexpr_fundef fundef = { NULL, NULL };
5730 if (constexpr_fundef_table == NULL)
5731 return NULL;
5733 fundef.decl = fun;
5734 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
5737 /* Check whether the parameter and return types of FUN are valid for a
5738 constexpr function, and complain if COMPLAIN. */
5740 static bool
5741 is_valid_constexpr_fn (tree fun, bool complain)
5743 tree parm = FUNCTION_FIRST_USER_PARM (fun);
5744 bool ret = true;
5745 for (; parm != NULL; parm = TREE_CHAIN (parm))
5746 if (!literal_type_p (TREE_TYPE (parm)))
5748 ret = false;
5749 if (complain)
5751 error ("invalid type for parameter %d of constexpr "
5752 "function %q+#D", DECL_PARM_INDEX (parm), fun);
5753 explain_non_literal_class (TREE_TYPE (parm));
5757 if (!DECL_CONSTRUCTOR_P (fun))
5759 tree rettype = TREE_TYPE (TREE_TYPE (fun));
5760 if (!literal_type_p (rettype))
5762 ret = false;
5763 if (complain)
5765 error ("invalid return type %qT of constexpr function %q+D",
5766 rettype, fun);
5767 explain_non_literal_class (rettype);
5771 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5772 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
5774 ret = false;
5775 if (complain)
5777 error ("enclosing class of constexpr non-static member "
5778 "function %q+#D is not a literal type", fun);
5779 explain_non_literal_class (DECL_CONTEXT (fun));
5783 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
5785 ret = false;
5786 if (complain)
5787 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
5790 return ret;
5793 /* Subroutine of build_constexpr_constructor_member_initializers.
5794 The expression tree T represents a data member initialization
5795 in a (constexpr) constructor definition. Build a pairing of
5796 the data member with its initializer, and prepend that pair
5797 to the existing initialization pair INITS. */
5799 static bool
5800 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
5802 tree member, init;
5803 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
5804 t = TREE_OPERAND (t, 0);
5805 if (TREE_CODE (t) == EXPR_STMT)
5806 t = TREE_OPERAND (t, 0);
5807 if (t == error_mark_node)
5808 return false;
5809 if (TREE_CODE (t) == STATEMENT_LIST)
5811 tree_stmt_iterator i;
5812 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5814 if (! build_data_member_initialization (tsi_stmt (i), vec))
5815 return false;
5817 return true;
5819 if (TREE_CODE (t) == CLEANUP_STMT)
5821 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
5822 but we can in a constexpr constructor for a non-literal class. Just
5823 ignore it; either all the initialization will be constant, in which
5824 case the cleanup can't run, or it can't be constexpr.
5825 Still recurse into CLEANUP_BODY. */
5826 return build_data_member_initialization (CLEANUP_BODY (t), vec);
5828 if (TREE_CODE (t) == CONVERT_EXPR)
5829 t = TREE_OPERAND (t, 0);
5830 if (TREE_CODE (t) == INIT_EXPR
5831 || TREE_CODE (t) == MODIFY_EXPR)
5833 member = TREE_OPERAND (t, 0);
5834 init = unshare_expr (TREE_OPERAND (t, 1));
5836 else
5838 gcc_assert (TREE_CODE (t) == CALL_EXPR);
5839 member = CALL_EXPR_ARG (t, 0);
5840 /* We don't use build_cplus_new here because it complains about
5841 abstract bases. Leaving the call unwrapped means that it has the
5842 wrong type, but cxx_eval_constant_expression doesn't care. */
5843 init = unshare_expr (t);
5845 if (TREE_CODE (member) == INDIRECT_REF)
5846 member = TREE_OPERAND (member, 0);
5847 if (TREE_CODE (member) == NOP_EXPR)
5849 tree op = member;
5850 STRIP_NOPS (op);
5851 if (TREE_CODE (op) == ADDR_EXPR)
5853 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5854 (TREE_TYPE (TREE_TYPE (op)),
5855 TREE_TYPE (TREE_TYPE (member))));
5856 /* Initializing a cv-qualified member; we need to look through
5857 the const_cast. */
5858 member = op;
5860 else if (op == current_class_ptr
5861 && (same_type_ignoring_top_level_qualifiers_p
5862 (TREE_TYPE (TREE_TYPE (member)),
5863 current_class_type)))
5864 /* Delegating constructor. */
5865 member = op;
5866 else
5868 /* This is an initializer for an empty base; keep it for now so
5869 we can check it in cxx_eval_bare_aggregate. */
5870 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
5873 if (TREE_CODE (member) == ADDR_EXPR)
5874 member = TREE_OPERAND (member, 0);
5875 if (TREE_CODE (member) == COMPONENT_REF
5876 /* If we're initializing a member of a subaggregate, it's a vtable
5877 pointer. Leave it as COMPONENT_REF so we remember the path to get
5878 to the vfield. */
5879 && TREE_CODE (TREE_OPERAND (member, 0)) != COMPONENT_REF)
5880 member = TREE_OPERAND (member, 1);
5881 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
5882 return true;
5885 /* Make sure that there are no statements after LAST in the constructor
5886 body represented by LIST. */
5888 bool
5889 check_constexpr_ctor_body (tree last, tree list)
5891 bool ok = true;
5892 if (TREE_CODE (list) == STATEMENT_LIST)
5894 tree_stmt_iterator i = tsi_last (list);
5895 for (; !tsi_end_p (i); tsi_prev (&i))
5897 tree t = tsi_stmt (i);
5898 if (t == last)
5899 break;
5900 if (TREE_CODE (t) == BIND_EXPR)
5902 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
5903 return false;
5904 else
5905 continue;
5907 /* We currently allow typedefs and static_assert.
5908 FIXME allow them in the standard, too. */
5909 if (TREE_CODE (t) != STATIC_ASSERT)
5911 ok = false;
5912 break;
5916 else if (list != last
5917 && TREE_CODE (list) != STATIC_ASSERT)
5918 ok = false;
5919 if (!ok)
5921 error ("constexpr constructor does not have empty body");
5922 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
5924 return ok;
5927 /* V is a vector of constructor elements built up for the base and member
5928 initializers of a constructor for TYPE. They need to be in increasing
5929 offset order, which they might not be yet if TYPE has a primary base
5930 which is not first in the base-clause. */
5932 static vec<constructor_elt, va_gc> *
5933 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
5935 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
5936 constructor_elt elt;
5937 int i;
5939 if (pri == NULL_TREE
5940 || pri == BINFO_BASE_BINFO (TYPE_BINFO (type), 0))
5941 return v;
5943 /* Find the element for the primary base and move it to the beginning of
5944 the vec. */
5945 vec<constructor_elt, va_gc> &vref = *v;
5946 pri = BINFO_TYPE (pri);
5947 for (i = 1; ; ++i)
5948 if (TREE_TYPE (vref[i].index) == pri)
5949 break;
5951 elt = vref[i];
5952 for (; i > 0; --i)
5953 vref[i] = vref[i-1];
5954 vref[0] = elt;
5955 return v;
5958 /* Build compile-time evalable representations of member-initializer list
5959 for a constexpr constructor. */
5961 static tree
5962 build_constexpr_constructor_member_initializers (tree type, tree body)
5964 vec<constructor_elt, va_gc> *vec = NULL;
5965 bool ok = true;
5966 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
5967 || TREE_CODE (body) == EH_SPEC_BLOCK)
5968 body = TREE_OPERAND (body, 0);
5969 if (TREE_CODE (body) == STATEMENT_LIST)
5970 body = STATEMENT_LIST_HEAD (body)->stmt;
5971 body = BIND_EXPR_BODY (body);
5972 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
5974 body = TREE_OPERAND (body, 0);
5975 if (TREE_CODE (body) == EXPR_STMT)
5976 body = TREE_OPERAND (body, 0);
5977 if (TREE_CODE (body) == INIT_EXPR
5978 && (same_type_ignoring_top_level_qualifiers_p
5979 (TREE_TYPE (TREE_OPERAND (body, 0)),
5980 current_class_type)))
5982 /* Trivial copy. */
5983 return TREE_OPERAND (body, 1);
5985 ok = build_data_member_initialization (body, &vec);
5987 else if (TREE_CODE (body) == STATEMENT_LIST)
5989 tree_stmt_iterator i;
5990 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
5992 ok = build_data_member_initialization (tsi_stmt (i), &vec);
5993 if (!ok)
5994 break;
5997 else if (TREE_CODE (body) == TRY_BLOCK)
5999 error ("body of %<constexpr%> constructor cannot be "
6000 "a function-try-block");
6001 return error_mark_node;
6003 else if (EXPR_P (body))
6004 ok = build_data_member_initialization (body, &vec);
6005 else
6006 gcc_assert (errorcount > 0);
6007 if (ok)
6009 if (vec_safe_length (vec) > 0)
6011 /* In a delegating constructor, return the target. */
6012 constructor_elt *ce = &(*vec)[0];
6013 if (ce->index == current_class_ptr)
6015 body = ce->value;
6016 vec_free (vec);
6017 return body;
6020 vec = sort_constexpr_mem_initializers (type, vec);
6021 return build_constructor (type, vec);
6023 else
6024 return error_mark_node;
6027 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
6028 declared to be constexpr, or a sub-statement thereof. Returns the
6029 return value if suitable, error_mark_node for a statement not allowed in
6030 a constexpr function, or NULL_TREE if no return value was found. */
6032 static tree
6033 constexpr_fn_retval (tree body)
6035 switch (TREE_CODE (body))
6037 case STATEMENT_LIST:
6039 tree_stmt_iterator i;
6040 tree expr = NULL_TREE;
6041 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
6043 tree s = constexpr_fn_retval (tsi_stmt (i));
6044 if (s == error_mark_node)
6045 return error_mark_node;
6046 else if (s == NULL_TREE)
6047 /* Keep iterating. */;
6048 else if (expr)
6049 /* Multiple return statements. */
6050 return error_mark_node;
6051 else
6052 expr = s;
6054 return expr;
6057 case RETURN_EXPR:
6058 return unshare_expr (TREE_OPERAND (body, 0));
6060 case DECL_EXPR:
6061 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
6062 return NULL_TREE;
6063 return error_mark_node;
6065 case CLEANUP_POINT_EXPR:
6066 return constexpr_fn_retval (TREE_OPERAND (body, 0));
6068 case USING_STMT:
6069 return NULL_TREE;
6071 default:
6072 return error_mark_node;
6076 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
6077 FUN; do the necessary transformations to turn it into a single expression
6078 that we can store in the hash table. */
6080 static tree
6081 massage_constexpr_body (tree fun, tree body)
6083 if (DECL_CONSTRUCTOR_P (fun))
6084 body = build_constexpr_constructor_member_initializers
6085 (DECL_CONTEXT (fun), body);
6086 else
6088 if (TREE_CODE (body) == EH_SPEC_BLOCK)
6089 body = EH_SPEC_STMTS (body);
6090 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
6091 body = TREE_OPERAND (body, 0);
6092 if (TREE_CODE (body) == BIND_EXPR)
6093 body = BIND_EXPR_BODY (body);
6094 body = constexpr_fn_retval (body);
6096 return body;
6099 /* FUN is a constexpr constructor with massaged body BODY. Return true
6100 if some bases/fields are uninitialized, and complain if COMPLAIN. */
6102 static bool
6103 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
6105 bool bad;
6106 tree field;
6107 unsigned i, nelts;
6108 tree ctype;
6110 if (TREE_CODE (body) != CONSTRUCTOR)
6111 return false;
6113 nelts = CONSTRUCTOR_NELTS (body);
6114 ctype = DECL_CONTEXT (fun);
6115 field = TYPE_FIELDS (ctype);
6117 if (TREE_CODE (ctype) == UNION_TYPE)
6119 if (nelts == 0 && next_initializable_field (field))
6121 if (complain)
6122 error ("%<constexpr%> constructor for union %qT must "
6123 "initialize exactly one non-static data member", ctype);
6124 return true;
6126 return false;
6129 bad = false;
6130 for (i = 0; i <= nelts; ++i)
6132 tree index;
6133 if (i == nelts)
6134 index = NULL_TREE;
6135 else
6137 index = CONSTRUCTOR_ELT (body, i)->index;
6138 /* Skip base and vtable inits. */
6139 if (TREE_CODE (index) != FIELD_DECL
6140 || DECL_ARTIFICIAL (index))
6141 continue;
6143 for (; field != index; field = DECL_CHAIN (field))
6145 tree ftype;
6146 if (TREE_CODE (field) != FIELD_DECL
6147 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
6148 || DECL_ARTIFICIAL (field))
6149 continue;
6150 ftype = strip_array_types (TREE_TYPE (field));
6151 if (type_has_constexpr_default_constructor (ftype))
6153 /* It's OK to skip a member with a trivial constexpr ctor.
6154 A constexpr ctor that isn't trivial should have been
6155 added in by now. */
6156 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
6157 || errorcount != 0);
6158 continue;
6160 if (!complain)
6161 return true;
6162 error ("uninitialized member %qD in %<constexpr%> constructor",
6163 field);
6164 bad = true;
6166 if (field == NULL_TREE)
6167 break;
6168 field = DECL_CHAIN (field);
6171 return bad;
6174 /* We are processing the definition of the constexpr function FUN.
6175 Check that its BODY fulfills the propriate requirements and
6176 enter it in the constexpr function definition table.
6177 For constructor BODY is actually the TREE_LIST of the
6178 member-initializer list. */
6180 tree
6181 register_constexpr_fundef (tree fun, tree body)
6183 constexpr_fundef entry;
6184 constexpr_fundef **slot;
6186 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
6187 return NULL;
6189 body = massage_constexpr_body (fun, body);
6190 if (body == NULL_TREE || body == error_mark_node)
6192 if (!DECL_CONSTRUCTOR_P (fun))
6193 error ("body of constexpr function %qD not a return-statement", fun);
6194 return NULL;
6197 if (!potential_rvalue_constant_expression (body))
6199 if (!DECL_GENERATED_P (fun))
6200 require_potential_rvalue_constant_expression (body);
6201 return NULL;
6204 if (DECL_CONSTRUCTOR_P (fun)
6205 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
6206 return NULL;
6208 /* Create the constexpr function table if necessary. */
6209 if (constexpr_fundef_table == NULL)
6210 constexpr_fundef_table = htab_create_ggc (101,
6211 constexpr_fundef_hash,
6212 constexpr_fundef_equal,
6213 ggc_free);
6214 entry.decl = fun;
6215 entry.body = body;
6216 slot = (constexpr_fundef **)
6217 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
6219 gcc_assert (*slot == NULL);
6220 *slot = ggc_alloc_constexpr_fundef ();
6221 **slot = entry;
6223 return fun;
6226 /* FUN is a non-constexpr function called in a context that requires a
6227 constant expression. If it comes from a constexpr template, explain why
6228 the instantiation isn't constexpr. */
6230 void
6231 explain_invalid_constexpr_fn (tree fun)
6233 static struct pointer_set_t *diagnosed;
6234 tree body;
6235 location_t save_loc;
6236 /* Only diagnose defaulted functions or instantiations. */
6237 if (!DECL_DEFAULTED_FN (fun)
6238 && !is_instantiation_of_constexpr (fun))
6239 return;
6240 if (diagnosed == NULL)
6241 diagnosed = pointer_set_create ();
6242 if (pointer_set_insert (diagnosed, fun) != 0)
6243 /* Already explained. */
6244 return;
6246 save_loc = input_location;
6247 input_location = DECL_SOURCE_LOCATION (fun);
6248 inform (0, "%q+D is not usable as a constexpr function because:", fun);
6249 /* First check the declaration. */
6250 if (is_valid_constexpr_fn (fun, true))
6252 /* Then if it's OK, the body. */
6253 if (DECL_DEFAULTED_FN (fun))
6254 explain_implicit_non_constexpr (fun);
6255 else
6257 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
6258 require_potential_rvalue_constant_expression (body);
6259 if (DECL_CONSTRUCTOR_P (fun))
6260 cx_check_missing_mem_inits (fun, body, true);
6263 input_location = save_loc;
6266 /* Objects of this type represent calls to constexpr functions
6267 along with the bindings of parameters to their arguments, for
6268 the purpose of compile time evaluation. */
6270 typedef struct GTY(()) constexpr_call {
6271 /* Description of the constexpr function definition. */
6272 constexpr_fundef *fundef;
6273 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
6274 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
6275 Note: This arrangement is made to accomodate the use of
6276 iterative_hash_template_arg (see pt.c). If you change this
6277 representation, also change the hash calculation in
6278 cxx_eval_call_expression. */
6279 tree bindings;
6280 /* Result of the call.
6281 NULL means the call is being evaluated.
6282 error_mark_node means that the evaluation was erroneous;
6283 otherwise, the actuall value of the call. */
6284 tree result;
6285 /* The hash of this call; we remember it here to avoid having to
6286 recalculate it when expanding the hash table. */
6287 hashval_t hash;
6288 } constexpr_call;
6290 /* A table of all constexpr calls that have been evaluated by the
6291 compiler in this translation unit. */
6293 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
6295 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
6296 bool, bool, bool *, bool *);
6298 /* Compute a hash value for a constexpr call representation. */
6300 static hashval_t
6301 constexpr_call_hash (const void *p)
6303 const constexpr_call *info = (const constexpr_call *) p;
6304 return info->hash;
6307 /* Return 1 if the objects pointed to by P and Q represent calls
6308 to the same constexpr function with the same arguments.
6309 Otherwise, return 0. */
6311 static int
6312 constexpr_call_equal (const void *p, const void *q)
6314 const constexpr_call *lhs = (const constexpr_call *) p;
6315 const constexpr_call *rhs = (const constexpr_call *) q;
6316 tree lhs_bindings;
6317 tree rhs_bindings;
6318 if (lhs == rhs)
6319 return 1;
6320 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
6321 return 0;
6322 lhs_bindings = lhs->bindings;
6323 rhs_bindings = rhs->bindings;
6324 while (lhs_bindings != NULL && rhs_bindings != NULL)
6326 tree lhs_arg = TREE_VALUE (lhs_bindings);
6327 tree rhs_arg = TREE_VALUE (rhs_bindings);
6328 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
6329 if (!cp_tree_equal (lhs_arg, rhs_arg))
6330 return 0;
6331 lhs_bindings = TREE_CHAIN (lhs_bindings);
6332 rhs_bindings = TREE_CHAIN (rhs_bindings);
6334 return lhs_bindings == rhs_bindings;
6337 /* Initialize the constexpr call table, if needed. */
6339 static void
6340 maybe_initialize_constexpr_call_table (void)
6342 if (constexpr_call_table == NULL)
6343 constexpr_call_table = htab_create_ggc (101,
6344 constexpr_call_hash,
6345 constexpr_call_equal,
6346 ggc_free);
6349 /* Return true if T designates the implied `this' parameter. */
6351 static inline bool
6352 is_this_parameter (tree t)
6354 return t == current_class_ptr;
6357 /* We have an expression tree T that represents a call, either CALL_EXPR
6358 or AGGR_INIT_EXPR. If the call is lexically to a named function,
6359 retrun the _DECL for that function. */
6361 static tree
6362 get_function_named_in_call (tree t)
6364 tree fun = NULL;
6365 switch (TREE_CODE (t))
6367 case CALL_EXPR:
6368 fun = CALL_EXPR_FN (t);
6369 break;
6371 case AGGR_INIT_EXPR:
6372 fun = AGGR_INIT_EXPR_FN (t);
6373 break;
6375 default:
6376 gcc_unreachable();
6377 break;
6379 if (TREE_CODE (fun) == ADDR_EXPR
6380 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
6381 fun = TREE_OPERAND (fun, 0);
6382 return fun;
6385 /* We have an expression tree T that represents a call, either CALL_EXPR
6386 or AGGR_INIT_EXPR. Return the Nth argument. */
6388 static inline tree
6389 get_nth_callarg (tree t, int n)
6391 switch (TREE_CODE (t))
6393 case CALL_EXPR:
6394 return CALL_EXPR_ARG (t, n);
6396 case AGGR_INIT_EXPR:
6397 return AGGR_INIT_EXPR_ARG (t, n);
6399 default:
6400 gcc_unreachable ();
6401 return NULL;
6405 /* Look up the binding of the function parameter T in a constexpr
6406 function call context CALL. */
6408 static tree
6409 lookup_parameter_binding (const constexpr_call *call, tree t)
6411 tree b = purpose_member (t, call->bindings);
6412 return TREE_VALUE (b);
6415 /* Attempt to evaluate T which represents a call to a builtin function.
6416 We assume here that all builtin functions evaluate to scalar types
6417 represented by _CST nodes. */
6419 static tree
6420 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
6421 bool allow_non_constant, bool addr,
6422 bool *non_constant_p, bool *overflow_p)
6424 const int nargs = call_expr_nargs (t);
6425 tree *args = (tree *) alloca (nargs * sizeof (tree));
6426 tree new_call;
6427 int i;
6428 for (i = 0; i < nargs; ++i)
6430 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
6431 allow_non_constant, addr,
6432 non_constant_p, overflow_p);
6433 if (allow_non_constant && *non_constant_p)
6434 return t;
6436 if (*non_constant_p)
6437 return t;
6438 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
6439 CALL_EXPR_FN (t), nargs, args);
6440 return fold (new_call);
6443 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
6444 the type of the value to match. */
6446 static tree
6447 adjust_temp_type (tree type, tree temp)
6449 if (TREE_TYPE (temp) == type)
6450 return temp;
6451 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
6452 if (TREE_CODE (temp) == CONSTRUCTOR)
6453 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
6454 gcc_assert (SCALAR_TYPE_P (type));
6455 return cp_fold_convert (type, temp);
6458 /* Subroutine of cxx_eval_call_expression.
6459 We are processing a call expression (either CALL_EXPR or
6460 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
6461 all arguments and bind their values to correspondings
6462 parameters, making up the NEW_CALL context. */
6464 static void
6465 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
6466 constexpr_call *new_call,
6467 bool allow_non_constant,
6468 bool *non_constant_p, bool *overflow_p)
6470 const int nargs = call_expr_nargs (t);
6471 tree fun = new_call->fundef->decl;
6472 tree parms = DECL_ARGUMENTS (fun);
6473 int i;
6474 for (i = 0; i < nargs; ++i)
6476 tree x, arg;
6477 tree type = parms ? TREE_TYPE (parms) : void_type_node;
6478 /* For member function, the first argument is a pointer to the implied
6479 object. And for an object contruction, don't bind `this' before
6480 it is fully constructed. */
6481 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
6482 goto next;
6483 x = get_nth_callarg (t, i);
6484 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
6485 TREE_CODE (type) == REFERENCE_TYPE,
6486 non_constant_p, overflow_p);
6487 /* Don't VERIFY_CONSTANT here. */
6488 if (*non_constant_p && allow_non_constant)
6489 return;
6490 /* Just discard ellipsis args after checking their constantitude. */
6491 if (!parms)
6492 continue;
6493 if (*non_constant_p)
6494 /* Don't try to adjust the type of non-constant args. */
6495 goto next;
6497 /* Make sure the binding has the same type as the parm. */
6498 if (TREE_CODE (type) != REFERENCE_TYPE)
6499 arg = adjust_temp_type (type, arg);
6500 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
6501 next:
6502 parms = TREE_CHAIN (parms);
6506 /* Variables and functions to manage constexpr call expansion context.
6507 These do not need to be marked for PCH or GC. */
6509 /* FIXME remember and print actual constant arguments. */
6510 static vec<tree> call_stack = vNULL;
6511 static int call_stack_tick;
6512 static int last_cx_error_tick;
6514 static bool
6515 push_cx_call_context (tree call)
6517 ++call_stack_tick;
6518 if (!EXPR_HAS_LOCATION (call))
6519 SET_EXPR_LOCATION (call, input_location);
6520 call_stack.safe_push (call);
6521 if (call_stack.length () > (unsigned) max_constexpr_depth)
6522 return false;
6523 return true;
6526 static void
6527 pop_cx_call_context (void)
6529 ++call_stack_tick;
6530 call_stack.pop ();
6533 vec<tree>
6534 cx_error_context (void)
6536 vec<tree> r = vNULL;
6537 if (call_stack_tick != last_cx_error_tick
6538 && !call_stack.is_empty ())
6539 r = call_stack;
6540 last_cx_error_tick = call_stack_tick;
6541 return r;
6544 /* Subroutine of cxx_eval_constant_expression.
6545 Evaluate the call expression tree T in the context of OLD_CALL expression
6546 evaluation. */
6548 static tree
6549 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
6550 bool allow_non_constant, bool addr,
6551 bool *non_constant_p, bool *overflow_p)
6553 location_t loc = EXPR_LOC_OR_HERE (t);
6554 tree fun = get_function_named_in_call (t);
6555 tree result;
6556 constexpr_call new_call = { NULL, NULL, NULL, 0 };
6557 constexpr_call **slot;
6558 constexpr_call *entry;
6559 bool depth_ok;
6561 if (TREE_CODE (fun) != FUNCTION_DECL)
6563 /* Might be a constexpr function pointer. */
6564 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
6565 /*addr*/false, non_constant_p, overflow_p);
6566 if (TREE_CODE (fun) == ADDR_EXPR)
6567 fun = TREE_OPERAND (fun, 0);
6569 if (TREE_CODE (fun) != FUNCTION_DECL)
6571 if (!allow_non_constant && !*non_constant_p)
6572 error_at (loc, "expression %qE does not designate a constexpr "
6573 "function", fun);
6574 *non_constant_p = true;
6575 return t;
6577 if (DECL_CLONED_FUNCTION_P (fun))
6578 fun = DECL_CLONED_FUNCTION (fun);
6579 if (is_builtin_fn (fun))
6580 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
6581 addr, non_constant_p, overflow_p);
6582 if (!DECL_DECLARED_CONSTEXPR_P (fun))
6584 if (!allow_non_constant)
6586 error_at (loc, "call to non-constexpr function %qD", fun);
6587 explain_invalid_constexpr_fn (fun);
6589 *non_constant_p = true;
6590 return t;
6593 /* Shortcut trivial copy constructor/op=. */
6594 if (call_expr_nargs (t) == 2 && trivial_fn_p (fun))
6596 tree arg = convert_from_reference (get_nth_callarg (t, 1));
6597 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
6598 addr, non_constant_p, overflow_p);
6601 /* If in direct recursive call, optimize definition search. */
6602 if (old_call != NULL && old_call->fundef->decl == fun)
6603 new_call.fundef = old_call->fundef;
6604 else
6606 new_call.fundef = retrieve_constexpr_fundef (fun);
6607 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
6609 if (!allow_non_constant)
6611 if (DECL_INITIAL (fun))
6613 /* The definition of fun was somehow unsuitable. */
6614 error_at (loc, "%qD called in a constant expression", fun);
6615 explain_invalid_constexpr_fn (fun);
6617 else
6618 error_at (loc, "%qD used before its definition", fun);
6620 *non_constant_p = true;
6621 return t;
6624 cxx_bind_parameters_in_call (old_call, t, &new_call,
6625 allow_non_constant, non_constant_p, overflow_p);
6626 if (*non_constant_p)
6627 return t;
6629 depth_ok = push_cx_call_context (t);
6631 new_call.hash
6632 = iterative_hash_template_arg (new_call.bindings,
6633 constexpr_fundef_hash (new_call.fundef));
6635 /* If we have seen this call before, we are done. */
6636 maybe_initialize_constexpr_call_table ();
6637 slot = (constexpr_call **)
6638 htab_find_slot (constexpr_call_table, &new_call, INSERT);
6639 entry = *slot;
6640 if (entry == NULL)
6642 /* We need to keep a pointer to the entry, not just the slot, as the
6643 slot can move in the call to cxx_eval_builtin_function_call. */
6644 *slot = entry = ggc_alloc_constexpr_call ();
6645 *entry = new_call;
6647 /* Calls which are in progress have their result set to NULL
6648 so that we can detect circular dependencies. */
6649 else if (entry->result == NULL)
6651 if (!allow_non_constant)
6652 error ("call has circular dependency");
6653 *non_constant_p = true;
6654 entry->result = result = error_mark_node;
6657 if (!depth_ok)
6659 if (!allow_non_constant)
6660 error ("constexpr evaluation depth exceeds maximum of %d (use "
6661 "-fconstexpr-depth= to increase the maximum)",
6662 max_constexpr_depth);
6663 *non_constant_p = true;
6664 entry->result = result = error_mark_node;
6666 else
6668 result = entry->result;
6669 if (!result || result == error_mark_node)
6670 result = (cxx_eval_constant_expression
6671 (&new_call, new_call.fundef->body,
6672 allow_non_constant, addr,
6673 non_constant_p, overflow_p));
6674 if (result == error_mark_node)
6675 *non_constant_p = true;
6676 if (*non_constant_p)
6677 entry->result = result = error_mark_node;
6678 else
6680 /* If this was a call to initialize an object, set the type of
6681 the CONSTRUCTOR to the type of that object. */
6682 if (DECL_CONSTRUCTOR_P (fun))
6684 tree ob_arg = get_nth_callarg (t, 0);
6685 STRIP_NOPS (ob_arg);
6686 gcc_assert (TREE_CODE (TREE_TYPE (ob_arg)) == POINTER_TYPE
6687 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
6688 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
6689 result);
6691 entry->result = result;
6695 pop_cx_call_context ();
6696 return unshare_expr (result);
6699 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
6701 bool
6702 reduced_constant_expression_p (tree t)
6704 /* FIXME are we calling this too much? */
6705 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
6708 /* Some expressions may have constant operands but are not constant
6709 themselves, such as 1/0. Call this function (or rather, the macro
6710 following it) to check for that condition.
6712 We only call this in places that require an arithmetic constant, not in
6713 places where we might have a non-constant expression that can be a
6714 component of a constant expression, such as the address of a constexpr
6715 variable that might be dereferenced later. */
6717 static bool
6718 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
6719 bool *overflow_p)
6721 if (!*non_constant_p && !reduced_constant_expression_p (t))
6723 if (!allow_non_constant)
6724 error ("%q+E is not a constant expression", t);
6725 *non_constant_p = true;
6727 if (TREE_OVERFLOW_P (t))
6729 if (!allow_non_constant)
6731 permerror (input_location, "overflow in constant expression");
6732 /* If we're being permissive (and are in an enforcing
6733 context), ignore the overflow. */
6734 if (flag_permissive)
6735 return *non_constant_p;
6737 *overflow_p = true;
6739 return *non_constant_p;
6741 #define VERIFY_CONSTANT(X) \
6742 do { \
6743 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
6744 return t; \
6745 } while (0)
6747 /* Subroutine of cxx_eval_constant_expression.
6748 Attempt to reduce the unary expression tree T to a compile time value.
6749 If successful, return the value. Otherwise issue a diagnostic
6750 and return error_mark_node. */
6752 static tree
6753 cxx_eval_unary_expression (const constexpr_call *call, tree t,
6754 bool allow_non_constant, bool addr,
6755 bool *non_constant_p, bool *overflow_p)
6757 tree r;
6758 tree orig_arg = TREE_OPERAND (t, 0);
6759 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
6760 addr, non_constant_p, overflow_p);
6761 VERIFY_CONSTANT (arg);
6762 if (arg == orig_arg)
6763 return t;
6764 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
6765 VERIFY_CONSTANT (r);
6766 return r;
6769 /* Subroutine of cxx_eval_constant_expression.
6770 Like cxx_eval_unary_expression, except for binary expressions. */
6772 static tree
6773 cxx_eval_binary_expression (const constexpr_call *call, tree t,
6774 bool allow_non_constant, bool addr,
6775 bool *non_constant_p, bool *overflow_p)
6777 tree r;
6778 tree orig_lhs = TREE_OPERAND (t, 0);
6779 tree orig_rhs = TREE_OPERAND (t, 1);
6780 tree lhs, rhs;
6781 lhs = cxx_eval_constant_expression (call, orig_lhs,
6782 allow_non_constant, addr,
6783 non_constant_p, overflow_p);
6784 VERIFY_CONSTANT (lhs);
6785 rhs = cxx_eval_constant_expression (call, orig_rhs,
6786 allow_non_constant, addr,
6787 non_constant_p, overflow_p);
6788 VERIFY_CONSTANT (rhs);
6789 if (lhs == orig_lhs && rhs == orig_rhs)
6790 return t;
6791 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
6792 VERIFY_CONSTANT (r);
6793 return r;
6796 /* Subroutine of cxx_eval_constant_expression.
6797 Attempt to evaluate condition expressions. Dead branches are not
6798 looked into. */
6800 static tree
6801 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
6802 bool allow_non_constant, bool addr,
6803 bool *non_constant_p, bool *overflow_p)
6805 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
6806 allow_non_constant, addr,
6807 non_constant_p, overflow_p);
6808 VERIFY_CONSTANT (val);
6809 /* Don't VERIFY_CONSTANT the other operands. */
6810 if (integer_zerop (val))
6811 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
6812 allow_non_constant, addr,
6813 non_constant_p, overflow_p);
6814 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
6815 allow_non_constant, addr,
6816 non_constant_p, overflow_p);
6819 /* Subroutine of cxx_eval_constant_expression.
6820 Attempt to reduce a reference to an array slot. */
6822 static tree
6823 cxx_eval_array_reference (const constexpr_call *call, tree t,
6824 bool allow_non_constant, bool addr,
6825 bool *non_constant_p, bool *overflow_p)
6827 tree oldary = TREE_OPERAND (t, 0);
6828 tree ary = cxx_eval_constant_expression (call, oldary,
6829 allow_non_constant, addr,
6830 non_constant_p, overflow_p);
6831 tree index, oldidx;
6832 HOST_WIDE_INT i;
6833 tree elem_type;
6834 unsigned len, elem_nchars = 1;
6835 if (*non_constant_p)
6836 return t;
6837 oldidx = TREE_OPERAND (t, 1);
6838 index = cxx_eval_constant_expression (call, oldidx,
6839 allow_non_constant, false,
6840 non_constant_p, overflow_p);
6841 VERIFY_CONSTANT (index);
6842 if (addr && ary == oldary && index == oldidx)
6843 return t;
6844 else if (addr)
6845 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6846 elem_type = TREE_TYPE (TREE_TYPE (ary));
6847 if (TREE_CODE (ary) == CONSTRUCTOR)
6848 len = CONSTRUCTOR_NELTS (ary);
6849 else if (TREE_CODE (ary) == STRING_CST)
6851 elem_nchars = (TYPE_PRECISION (elem_type)
6852 / TYPE_PRECISION (char_type_node));
6853 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6855 else
6857 /* We can't do anything with other tree codes, so use
6858 VERIFY_CONSTANT to complain and fail. */
6859 VERIFY_CONSTANT (ary);
6860 gcc_unreachable ();
6862 if (compare_tree_int (index, len) >= 0)
6864 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
6866 /* If it's within the array bounds but doesn't have an explicit
6867 initializer, it's value-initialized. */
6868 tree val = build_value_init (elem_type, tf_warning_or_error);
6869 return cxx_eval_constant_expression (call, val,
6870 allow_non_constant, addr,
6871 non_constant_p, overflow_p);
6874 if (!allow_non_constant)
6875 error ("array subscript out of bound");
6876 *non_constant_p = true;
6877 return t;
6879 i = tree_low_cst (index, 0);
6880 if (TREE_CODE (ary) == CONSTRUCTOR)
6881 return (*CONSTRUCTOR_ELTS (ary))[i].value;
6882 else if (elem_nchars == 1)
6883 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
6884 TREE_STRING_POINTER (ary)[i]);
6885 else
6887 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
6888 return native_interpret_expr (type, (const unsigned char *)
6889 TREE_STRING_POINTER (ary)
6890 + i * elem_nchars, elem_nchars);
6892 /* Don't VERIFY_CONSTANT here. */
6895 /* Subroutine of cxx_eval_constant_expression.
6896 Attempt to reduce a field access of a value of class type. */
6898 static tree
6899 cxx_eval_component_reference (const constexpr_call *call, tree t,
6900 bool allow_non_constant, bool addr,
6901 bool *non_constant_p, bool *overflow_p)
6903 unsigned HOST_WIDE_INT i;
6904 tree field;
6905 tree value;
6906 tree part = TREE_OPERAND (t, 1);
6907 tree orig_whole = TREE_OPERAND (t, 0);
6908 tree whole = cxx_eval_constant_expression (call, orig_whole,
6909 allow_non_constant, addr,
6910 non_constant_p, overflow_p);
6911 if (whole == orig_whole)
6912 return t;
6913 if (addr)
6914 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6915 whole, part, NULL_TREE);
6916 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6917 CONSTRUCTOR. */
6918 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
6920 if (!allow_non_constant)
6921 error ("%qE is not a constant expression", orig_whole);
6922 *non_constant_p = true;
6924 if (DECL_MUTABLE_P (part))
6926 if (!allow_non_constant)
6927 error ("mutable %qD is not usable in a constant expression", part);
6928 *non_constant_p = true;
6930 if (*non_constant_p)
6931 return t;
6932 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6934 if (field == part)
6935 return value;
6937 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
6938 && CONSTRUCTOR_NELTS (whole) > 0)
6940 /* DR 1188 says we don't have to deal with this. */
6941 if (!allow_non_constant)
6942 error ("accessing %qD member instead of initialized %qD member in "
6943 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
6944 *non_constant_p = true;
6945 return t;
6948 /* If there's no explicit init for this field, it's value-initialized. */
6949 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6950 return cxx_eval_constant_expression (call, value,
6951 allow_non_constant, addr,
6952 non_constant_p, overflow_p);
6955 /* Subroutine of cxx_eval_constant_expression.
6956 Attempt to reduce a field access of a value of class type that is
6957 expressed as a BIT_FIELD_REF. */
6959 static tree
6960 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
6961 bool allow_non_constant, bool addr,
6962 bool *non_constant_p, bool *overflow_p)
6964 tree orig_whole = TREE_OPERAND (t, 0);
6965 tree retval, fldval, utype, mask;
6966 bool fld_seen = false;
6967 HOST_WIDE_INT istart, isize;
6968 tree whole = cxx_eval_constant_expression (call, orig_whole,
6969 allow_non_constant, addr,
6970 non_constant_p, overflow_p);
6971 tree start, field, value;
6972 unsigned HOST_WIDE_INT i;
6974 if (whole == orig_whole)
6975 return t;
6976 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6977 CONSTRUCTOR. */
6978 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
6980 if (!allow_non_constant)
6981 error ("%qE is not a constant expression", orig_whole);
6982 *non_constant_p = true;
6984 if (*non_constant_p)
6985 return t;
6987 start = TREE_OPERAND (t, 2);
6988 istart = tree_low_cst (start, 0);
6989 isize = tree_low_cst (TREE_OPERAND (t, 1), 0);
6990 utype = TREE_TYPE (t);
6991 if (!TYPE_UNSIGNED (utype))
6992 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6993 retval = build_int_cst (utype, 0);
6994 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6996 tree bitpos = bit_position (field);
6997 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6998 return value;
6999 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
7000 && TREE_CODE (value) == INTEGER_CST
7001 && host_integerp (bitpos, 0)
7002 && host_integerp (DECL_SIZE (field), 0))
7004 HOST_WIDE_INT bit = tree_low_cst (bitpos, 0);
7005 HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0);
7006 HOST_WIDE_INT shift;
7007 if (bit >= istart && bit + sz <= istart + isize)
7009 fldval = fold_convert (utype, value);
7010 mask = build_int_cst_type (utype, -1);
7011 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
7012 size_int (TYPE_PRECISION (utype) - sz));
7013 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
7014 size_int (TYPE_PRECISION (utype) - sz));
7015 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
7016 shift = bit - istart;
7017 if (BYTES_BIG_ENDIAN)
7018 shift = TYPE_PRECISION (utype) - shift - sz;
7019 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
7020 size_int (shift));
7021 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
7022 fld_seen = true;
7026 if (fld_seen)
7027 return fold_convert (TREE_TYPE (t), retval);
7028 gcc_unreachable ();
7029 return error_mark_node;
7032 /* Subroutine of cxx_eval_constant_expression.
7033 Evaluate a short-circuited logical expression T in the context
7034 of a given constexpr CALL. BAILOUT_VALUE is the value for
7035 early return. CONTINUE_VALUE is used here purely for
7036 sanity check purposes. */
7038 static tree
7039 cxx_eval_logical_expression (const constexpr_call *call, tree t,
7040 tree bailout_value, tree continue_value,
7041 bool allow_non_constant, bool addr,
7042 bool *non_constant_p, bool *overflow_p)
7044 tree r;
7045 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7046 allow_non_constant, addr,
7047 non_constant_p, overflow_p);
7048 VERIFY_CONSTANT (lhs);
7049 if (tree_int_cst_equal (lhs, bailout_value))
7050 return lhs;
7051 gcc_assert (tree_int_cst_equal (lhs, continue_value));
7052 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7053 allow_non_constant, addr, non_constant_p, overflow_p);
7054 VERIFY_CONSTANT (r);
7055 return r;
7058 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
7059 CONSTRUCTOR elements to initialize (part of) an object containing that
7060 field. Return a pointer to the constructor_elt corresponding to the
7061 initialization of the field. */
7063 static constructor_elt *
7064 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
7066 tree aggr = TREE_OPERAND (ref, 0);
7067 tree field = TREE_OPERAND (ref, 1);
7068 HOST_WIDE_INT i;
7069 constructor_elt *ce;
7071 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
7073 if (TREE_CODE (aggr) == COMPONENT_REF)
7075 constructor_elt *base_ce
7076 = base_field_constructor_elt (v, aggr);
7077 v = CONSTRUCTOR_ELTS (base_ce->value);
7080 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7081 if (ce->index == field)
7082 return ce;
7084 gcc_unreachable ();
7085 return NULL;
7088 /* Subroutine of cxx_eval_constant_expression.
7089 The expression tree T denotes a C-style array or a C-style
7090 aggregate. Reduce it to a constant expression. */
7092 static tree
7093 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
7094 bool allow_non_constant, bool addr,
7095 bool *non_constant_p, bool *overflow_p)
7097 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
7098 vec<constructor_elt, va_gc> *n;
7099 vec_alloc (n, vec_safe_length (v));
7100 constructor_elt *ce;
7101 HOST_WIDE_INT i;
7102 bool changed = false;
7103 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
7104 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7106 tree elt = cxx_eval_constant_expression (call, ce->value,
7107 allow_non_constant, addr,
7108 non_constant_p, overflow_p);
7109 /* Don't VERIFY_CONSTANT here. */
7110 if (allow_non_constant && *non_constant_p)
7111 goto fail;
7112 if (elt != ce->value)
7113 changed = true;
7114 if (TREE_CODE (ce->index) == COMPONENT_REF)
7116 /* This is an initialization of a vfield inside a base
7117 subaggregate that we already initialized; push this
7118 initialization into the previous initialization. */
7119 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
7120 inner->value = elt;
7122 else if (TREE_CODE (ce->index) == NOP_EXPR)
7124 /* This is an initializer for an empty base; now that we've
7125 checked that it's constant, we can ignore it. */
7126 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
7128 else
7129 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
7131 if (*non_constant_p || !changed)
7133 fail:
7134 vec_free (n);
7135 return t;
7137 t = build_constructor (TREE_TYPE (t), n);
7138 TREE_CONSTANT (t) = true;
7139 return t;
7142 /* Subroutine of cxx_eval_constant_expression.
7143 The expression tree T is a VEC_INIT_EXPR which denotes the desired
7144 initialization of a non-static data member of array type. Reduce it to a
7145 CONSTRUCTOR.
7147 Note that apart from value-initialization (when VALUE_INIT is true),
7148 this is only intended to support value-initialization and the
7149 initializations done by defaulted constructors for classes with
7150 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7151 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7152 for the copy/move constructor. */
7154 static tree
7155 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
7156 bool value_init, bool allow_non_constant, bool addr,
7157 bool *non_constant_p, bool *overflow_p)
7159 tree elttype = TREE_TYPE (atype);
7160 int max = tree_low_cst (array_type_nelts (atype), 0);
7161 vec<constructor_elt, va_gc> *n;
7162 vec_alloc (n, max + 1);
7163 bool pre_init = false;
7164 int i;
7166 /* For the default constructor, build up a call to the default
7167 constructor of the element type. We only need to handle class types
7168 here, as for a constructor to be constexpr, all members must be
7169 initialized, which for a defaulted default constructor means they must
7170 be of a class type with a constexpr default constructor. */
7171 if (TREE_CODE (elttype) == ARRAY_TYPE)
7172 /* We only do this at the lowest level. */;
7173 else if (value_init)
7175 init = build_value_init (elttype, tf_warning_or_error);
7176 init = cxx_eval_constant_expression
7177 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
7178 pre_init = true;
7180 else if (!init)
7182 vec<tree, va_gc> *argvec = make_tree_vector ();
7183 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7184 &argvec, elttype, LOOKUP_NORMAL,
7185 tf_warning_or_error);
7186 release_tree_vector (argvec);
7187 init = cxx_eval_constant_expression (call, init, allow_non_constant,
7188 addr, non_constant_p, overflow_p);
7189 pre_init = true;
7192 if (*non_constant_p && !allow_non_constant)
7193 goto fail;
7195 for (i = 0; i <= max; ++i)
7197 tree idx = build_int_cst (size_type_node, i);
7198 tree eltinit;
7199 if (TREE_CODE (elttype) == ARRAY_TYPE)
7201 /* A multidimensional array; recurse. */
7202 if (value_init || init == NULL_TREE)
7203 eltinit = NULL_TREE;
7204 else
7205 eltinit = cp_build_array_ref (input_location, init, idx,
7206 tf_warning_or_error);
7207 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
7208 allow_non_constant, addr,
7209 non_constant_p, overflow_p);
7211 else if (pre_init)
7213 /* Initializing an element using value or default initialization
7214 we just pre-built above. */
7215 if (i == 0)
7216 eltinit = init;
7217 else
7218 eltinit = unshare_expr (init);
7220 else
7222 /* Copying an element. */
7223 vec<tree, va_gc> *argvec;
7224 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7225 (atype, TREE_TYPE (init)));
7226 eltinit = cp_build_array_ref (input_location, init, idx,
7227 tf_warning_or_error);
7228 if (!real_lvalue_p (init))
7229 eltinit = move (eltinit);
7230 argvec = make_tree_vector ();
7231 argvec->quick_push (eltinit);
7232 eltinit = (build_special_member_call
7233 (NULL_TREE, complete_ctor_identifier, &argvec,
7234 elttype, LOOKUP_NORMAL, tf_warning_or_error));
7235 release_tree_vector (argvec);
7236 eltinit = cxx_eval_constant_expression
7237 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
7239 if (*non_constant_p && !allow_non_constant)
7240 goto fail;
7241 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
7244 if (!*non_constant_p)
7246 init = build_constructor (atype, n);
7247 TREE_CONSTANT (init) = true;
7248 return init;
7251 fail:
7252 vec_free (n);
7253 return init;
7256 static tree
7257 cxx_eval_vec_init (const constexpr_call *call, tree t,
7258 bool allow_non_constant, bool addr,
7259 bool *non_constant_p, bool *overflow_p)
7261 tree atype = TREE_TYPE (t);
7262 tree init = VEC_INIT_EXPR_INIT (t);
7263 tree r = cxx_eval_vec_init_1 (call, atype, init,
7264 VEC_INIT_EXPR_VALUE_INIT (t),
7265 allow_non_constant, addr, non_constant_p, overflow_p);
7266 if (*non_constant_p)
7267 return t;
7268 else
7269 return r;
7272 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7273 match. We want to be less strict for simple *& folding; if we have a
7274 non-const temporary that we access through a const pointer, that should
7275 work. We handle this here rather than change fold_indirect_ref_1
7276 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7277 don't really make sense outside of constant expression evaluation. Also
7278 we want to allow folding to COMPONENT_REF, which could cause trouble
7279 with TBAA in fold_indirect_ref_1.
7281 Try to keep this function synced with fold_indirect_ref_1. */
7283 static tree
7284 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
7286 tree sub, subtype;
7288 sub = op0;
7289 STRIP_NOPS (sub);
7290 subtype = TREE_TYPE (sub);
7291 if (!POINTER_TYPE_P (subtype))
7292 return NULL_TREE;
7294 if (TREE_CODE (sub) == ADDR_EXPR)
7296 tree op = TREE_OPERAND (sub, 0);
7297 tree optype = TREE_TYPE (op);
7299 /* *&CONST_DECL -> to the value of the const decl. */
7300 if (TREE_CODE (op) == CONST_DECL)
7301 return DECL_INITIAL (op);
7302 /* *&p => p; make sure to handle *&"str"[cst] here. */
7303 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
7305 tree fop = fold_read_from_constant_string (op);
7306 if (fop)
7307 return fop;
7308 else
7309 return op;
7311 /* *(foo *)&fooarray => fooarray[0] */
7312 else if (TREE_CODE (optype) == ARRAY_TYPE
7313 && (same_type_ignoring_top_level_qualifiers_p
7314 (type, TREE_TYPE (optype))))
7316 tree type_domain = TYPE_DOMAIN (optype);
7317 tree min_val = size_zero_node;
7318 if (type_domain && TYPE_MIN_VALUE (type_domain))
7319 min_val = TYPE_MIN_VALUE (type_domain);
7320 return build4_loc (loc, ARRAY_REF, type, op, min_val,
7321 NULL_TREE, NULL_TREE);
7323 /* *(foo *)&complexfoo => __real__ complexfoo */
7324 else if (TREE_CODE (optype) == COMPLEX_TYPE
7325 && (same_type_ignoring_top_level_qualifiers_p
7326 (type, TREE_TYPE (optype))))
7327 return fold_build1_loc (loc, REALPART_EXPR, type, op);
7328 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7329 else if (TREE_CODE (optype) == VECTOR_TYPE
7330 && (same_type_ignoring_top_level_qualifiers_p
7331 (type, TREE_TYPE (optype))))
7333 tree part_width = TYPE_SIZE (type);
7334 tree index = bitsize_int (0);
7335 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
7337 /* Also handle conversion to an empty base class, which
7338 is represented with a NOP_EXPR. */
7339 else if (is_empty_class (type)
7340 && CLASS_TYPE_P (optype)
7341 && DERIVED_FROM_P (type, optype))
7343 *empty_base = true;
7344 return op;
7346 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
7347 else if (RECORD_OR_UNION_TYPE_P (optype))
7349 tree field = TYPE_FIELDS (optype);
7350 for (; field; field = DECL_CHAIN (field))
7351 if (TREE_CODE (field) == FIELD_DECL
7352 && integer_zerop (byte_position (field))
7353 && (same_type_ignoring_top_level_qualifiers_p
7354 (TREE_TYPE (field), type)))
7356 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
7357 break;
7361 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7362 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
7364 tree op00 = TREE_OPERAND (sub, 0);
7365 tree op01 = TREE_OPERAND (sub, 1);
7367 STRIP_NOPS (op00);
7368 if (TREE_CODE (op00) == ADDR_EXPR)
7370 tree op00type;
7371 op00 = TREE_OPERAND (op00, 0);
7372 op00type = TREE_TYPE (op00);
7374 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
7375 if (TREE_CODE (op00type) == VECTOR_TYPE
7376 && (same_type_ignoring_top_level_qualifiers_p
7377 (type, TREE_TYPE (op00type))))
7379 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
7380 tree part_width = TYPE_SIZE (type);
7381 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
7382 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
7383 tree index = bitsize_int (indexi);
7385 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
7386 return fold_build3_loc (loc,
7387 BIT_FIELD_REF, type, op00,
7388 part_width, index);
7391 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7392 else if (TREE_CODE (op00type) == COMPLEX_TYPE
7393 && (same_type_ignoring_top_level_qualifiers_p
7394 (type, TREE_TYPE (op00type))))
7396 tree size = TYPE_SIZE_UNIT (type);
7397 if (tree_int_cst_equal (size, op01))
7398 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
7400 /* ((foo *)&fooarray)[1] => fooarray[1] */
7401 else if (TREE_CODE (op00type) == ARRAY_TYPE
7402 && (same_type_ignoring_top_level_qualifiers_p
7403 (type, TREE_TYPE (op00type))))
7405 tree type_domain = TYPE_DOMAIN (op00type);
7406 tree min_val = size_zero_node;
7407 if (type_domain && TYPE_MIN_VALUE (type_domain))
7408 min_val = TYPE_MIN_VALUE (type_domain);
7409 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
7410 TYPE_SIZE_UNIT (type));
7411 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
7412 return build4_loc (loc, ARRAY_REF, type, op00, op01,
7413 NULL_TREE, NULL_TREE);
7415 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
7416 else if (RECORD_OR_UNION_TYPE_P (op00type))
7418 tree field = TYPE_FIELDS (op00type);
7419 for (; field; field = DECL_CHAIN (field))
7420 if (TREE_CODE (field) == FIELD_DECL
7421 && tree_int_cst_equal (byte_position (field), op01)
7422 && (same_type_ignoring_top_level_qualifiers_p
7423 (TREE_TYPE (field), type)))
7425 return fold_build3 (COMPONENT_REF, type, op00,
7426 field, NULL_TREE);
7427 break;
7432 /* *(foo *)fooarrptreturn> (*fooarrptr)[0] */
7433 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7434 && (same_type_ignoring_top_level_qualifiers_p
7435 (type, TREE_TYPE (TREE_TYPE (subtype)))))
7437 tree type_domain;
7438 tree min_val = size_zero_node;
7439 sub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
7440 if (!sub)
7441 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7442 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7443 if (type_domain && TYPE_MIN_VALUE (type_domain))
7444 min_val = TYPE_MIN_VALUE (type_domain);
7445 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7446 NULL_TREE);
7449 return NULL_TREE;
7452 static tree
7453 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
7454 bool allow_non_constant, bool addr,
7455 bool *non_constant_p, bool *overflow_p)
7457 tree orig_op0 = TREE_OPERAND (t, 0);
7458 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
7459 /*addr*/false, non_constant_p, overflow_p);
7460 bool empty_base = false;
7461 tree r;
7463 /* Don't VERIFY_CONSTANT here. */
7464 if (*non_constant_p)
7465 return t;
7467 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
7468 &empty_base);
7470 if (r)
7471 r = cxx_eval_constant_expression (call, r, allow_non_constant,
7472 addr, non_constant_p, overflow_p);
7473 else
7475 tree sub = op0;
7476 STRIP_NOPS (sub);
7477 if (TREE_CODE (sub) == POINTER_PLUS_EXPR)
7479 sub = TREE_OPERAND (sub, 0);
7480 STRIP_NOPS (sub);
7482 if (TREE_CODE (sub) == ADDR_EXPR)
7484 /* We couldn't fold to a constant value. Make sure it's not
7485 something we should have been able to fold. */
7486 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
7487 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7488 /* DR 1188 says we don't have to deal with this. */
7489 if (!allow_non_constant)
7490 error ("accessing value of %qE through a %qT glvalue in a "
7491 "constant expression", build_fold_indirect_ref (sub),
7492 TREE_TYPE (t));
7493 *non_constant_p = true;
7494 return t;
7498 /* If we're pulling out the value of an empty base, make sure
7499 that the whole object is constant and then return an empty
7500 CONSTRUCTOR. */
7501 if (empty_base)
7503 VERIFY_CONSTANT (r);
7504 r = build_constructor (TREE_TYPE (t), NULL);
7505 TREE_CONSTANT (r) = true;
7508 if (r == NULL_TREE)
7510 if (!addr)
7511 VERIFY_CONSTANT (t);
7512 return t;
7514 return r;
7517 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7518 Shared between potential_constant_expression and
7519 cxx_eval_constant_expression. */
7521 static void
7522 non_const_var_error (tree r)
7524 tree type = TREE_TYPE (r);
7525 error ("the value of %qD is not usable in a constant "
7526 "expression", r);
7527 /* Avoid error cascade. */
7528 if (DECL_INITIAL (r) == error_mark_node)
7529 return;
7530 if (DECL_DECLARED_CONSTEXPR_P (r))
7531 inform (DECL_SOURCE_LOCATION (r),
7532 "%qD used in its own initializer", r);
7533 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7535 if (!CP_TYPE_CONST_P (type))
7536 inform (DECL_SOURCE_LOCATION (r),
7537 "%q#D is not const", r);
7538 else if (CP_TYPE_VOLATILE_P (type))
7539 inform (DECL_SOURCE_LOCATION (r),
7540 "%q#D is volatile", r);
7541 else if (!DECL_INITIAL (r)
7542 || !TREE_CONSTANT (DECL_INITIAL (r)))
7543 inform (DECL_SOURCE_LOCATION (r),
7544 "%qD was not initialized with a constant "
7545 "expression", r);
7546 else
7547 gcc_unreachable ();
7549 else
7551 if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r))
7552 inform (DECL_SOURCE_LOCATION (r),
7553 "%qD was not declared %<constexpr%>", r);
7554 else
7555 inform (DECL_SOURCE_LOCATION (r),
7556 "%qD does not have integral or enumeration type",
7561 /* Evaluate VEC_PERM_EXPR (v1, v2, mask). */
7562 static tree
7563 cxx_eval_vec_perm_expr (const constexpr_call *call, tree t,
7564 bool allow_non_constant, bool addr,
7565 bool *non_constant_p, bool *overflow_p)
7567 int i;
7568 tree args[3];
7569 tree val;
7570 tree elttype = TREE_TYPE (t);
7572 for (i = 0; i < 3; i++)
7574 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
7575 allow_non_constant, addr,
7576 non_constant_p, overflow_p);
7577 if (*non_constant_p)
7578 goto fail;
7581 gcc_assert (TREE_CODE (TREE_TYPE (args[0])) == VECTOR_TYPE);
7582 gcc_assert (TREE_CODE (TREE_TYPE (args[1])) == VECTOR_TYPE);
7583 gcc_assert (TREE_CODE (TREE_TYPE (args[2])) == VECTOR_TYPE);
7585 val = fold_ternary_loc (EXPR_LOCATION (t), VEC_PERM_EXPR, elttype,
7586 args[0], args[1], args[2]);
7587 if (val != NULL_TREE)
7588 return val;
7590 fail:
7591 return t;
7594 /* Attempt to reduce the expression T to a constant value.
7595 On failure, issue diagnostic and return error_mark_node. */
7596 /* FIXME unify with c_fully_fold */
7598 static tree
7599 cxx_eval_constant_expression (const constexpr_call *call, tree t,
7600 bool allow_non_constant, bool addr,
7601 bool *non_constant_p, bool *overflow_p)
7603 tree r = t;
7605 if (t == error_mark_node)
7607 *non_constant_p = true;
7608 return t;
7610 if (CONSTANT_CLASS_P (t))
7612 if (TREE_CODE (t) == PTRMEM_CST)
7613 t = cplus_expand_constant (t);
7614 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
7615 *overflow_p = true;
7616 return t;
7618 if (TREE_CODE (t) != NOP_EXPR
7619 && reduced_constant_expression_p (t))
7620 return fold (t);
7622 switch (TREE_CODE (t))
7624 case VAR_DECL:
7625 if (addr)
7626 return t;
7627 /* else fall through. */
7628 case CONST_DECL:
7629 r = integral_constant_value (t);
7630 if (TREE_CODE (r) == TARGET_EXPR
7631 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7632 r = TARGET_EXPR_INITIAL (r);
7633 if (DECL_P (r))
7635 if (!allow_non_constant)
7636 non_const_var_error (r);
7637 *non_constant_p = true;
7639 break;
7641 case FUNCTION_DECL:
7642 case TEMPLATE_DECL:
7643 case LABEL_DECL:
7644 return t;
7646 case PARM_DECL:
7647 if (call && DECL_CONTEXT (t) == call->fundef->decl)
7649 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
7651 if (!allow_non_constant)
7652 sorry ("use of the value of the object being constructed "
7653 "in a constant expression");
7654 *non_constant_p = true;
7656 else
7657 r = lookup_parameter_binding (call, t);
7659 else if (addr)
7660 /* Defer in case this is only used for its type. */;
7661 else
7663 if (!allow_non_constant)
7664 error ("%qE is not a constant expression", t);
7665 *non_constant_p = true;
7667 break;
7669 case CALL_EXPR:
7670 case AGGR_INIT_EXPR:
7671 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
7672 non_constant_p, overflow_p);
7673 break;
7675 case TARGET_EXPR:
7676 if (!literal_type_p (TREE_TYPE (t)))
7678 if (!allow_non_constant)
7680 error ("temporary of non-literal type %qT in a "
7681 "constant expression", TREE_TYPE (t));
7682 explain_non_literal_class (TREE_TYPE (t));
7684 *non_constant_p = true;
7685 break;
7687 /* else fall through. */
7688 case INIT_EXPR:
7689 /* Pass false for 'addr' because these codes indicate
7690 initialization of a temporary. */
7691 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7692 allow_non_constant, false,
7693 non_constant_p, overflow_p);
7694 if (!*non_constant_p)
7695 /* Adjust the type of the result to the type of the temporary. */
7696 r = adjust_temp_type (TREE_TYPE (t), r);
7697 break;
7699 case SCOPE_REF:
7700 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7701 allow_non_constant, addr,
7702 non_constant_p, overflow_p);
7703 break;
7705 case RETURN_EXPR:
7706 case NON_LVALUE_EXPR:
7707 case TRY_CATCH_EXPR:
7708 case CLEANUP_POINT_EXPR:
7709 case MUST_NOT_THROW_EXPR:
7710 case SAVE_EXPR:
7711 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7712 allow_non_constant, addr,
7713 non_constant_p, overflow_p);
7714 break;
7716 /* These differ from cxx_eval_unary_expression in that this doesn't
7717 check for a constant operand or result; an address can be
7718 constant without its operand being, and vice versa. */
7719 case INDIRECT_REF:
7720 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
7721 non_constant_p, overflow_p);
7722 break;
7724 case ADDR_EXPR:
7726 tree oldop = TREE_OPERAND (t, 0);
7727 tree op = cxx_eval_constant_expression (call, oldop,
7728 allow_non_constant,
7729 /*addr*/true,
7730 non_constant_p, overflow_p);
7731 /* Don't VERIFY_CONSTANT here. */
7732 if (*non_constant_p)
7733 return t;
7734 /* This function does more aggressive folding than fold itself. */
7735 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7736 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7737 return t;
7738 break;
7741 case REALPART_EXPR:
7742 case IMAGPART_EXPR:
7743 case CONJ_EXPR:
7744 case FIX_TRUNC_EXPR:
7745 case FLOAT_EXPR:
7746 case NEGATE_EXPR:
7747 case ABS_EXPR:
7748 case BIT_NOT_EXPR:
7749 case TRUTH_NOT_EXPR:
7750 case FIXED_CONVERT_EXPR:
7751 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
7752 non_constant_p, overflow_p);
7753 break;
7755 case SIZEOF_EXPR:
7756 if (SIZEOF_EXPR_TYPE_P (t))
7757 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
7758 SIZEOF_EXPR, false);
7759 else if (TYPE_P (TREE_OPERAND (t, 0)))
7760 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
7761 false);
7762 else
7763 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
7764 false);
7765 if (r == error_mark_node)
7766 r = size_one_node;
7767 VERIFY_CONSTANT (r);
7768 break;
7770 case COMPOUND_EXPR:
7772 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7773 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7774 introduced by build_call_a. */
7775 tree op0 = TREE_OPERAND (t, 0);
7776 tree op1 = TREE_OPERAND (t, 1);
7777 STRIP_NOPS (op1);
7778 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7779 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7780 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
7781 addr, non_constant_p, overflow_p);
7782 else
7784 /* Check that the LHS is constant and then discard it. */
7785 cxx_eval_constant_expression (call, op0, allow_non_constant,
7786 false, non_constant_p, overflow_p);
7787 op1 = TREE_OPERAND (t, 1);
7788 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
7789 addr, non_constant_p, overflow_p);
7792 break;
7794 case POINTER_PLUS_EXPR:
7795 case PLUS_EXPR:
7796 case MINUS_EXPR:
7797 case MULT_EXPR:
7798 case TRUNC_DIV_EXPR:
7799 case CEIL_DIV_EXPR:
7800 case FLOOR_DIV_EXPR:
7801 case ROUND_DIV_EXPR:
7802 case TRUNC_MOD_EXPR:
7803 case CEIL_MOD_EXPR:
7804 case ROUND_MOD_EXPR:
7805 case RDIV_EXPR:
7806 case EXACT_DIV_EXPR:
7807 case MIN_EXPR:
7808 case MAX_EXPR:
7809 case LSHIFT_EXPR:
7810 case RSHIFT_EXPR:
7811 case LROTATE_EXPR:
7812 case RROTATE_EXPR:
7813 case BIT_IOR_EXPR:
7814 case BIT_XOR_EXPR:
7815 case BIT_AND_EXPR:
7816 case TRUTH_XOR_EXPR:
7817 case LT_EXPR:
7818 case LE_EXPR:
7819 case GT_EXPR:
7820 case GE_EXPR:
7821 case EQ_EXPR:
7822 case NE_EXPR:
7823 case UNORDERED_EXPR:
7824 case ORDERED_EXPR:
7825 case UNLT_EXPR:
7826 case UNLE_EXPR:
7827 case UNGT_EXPR:
7828 case UNGE_EXPR:
7829 case UNEQ_EXPR:
7830 case RANGE_EXPR:
7831 case COMPLEX_EXPR:
7832 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
7833 non_constant_p, overflow_p);
7834 break;
7836 /* fold can introduce non-IF versions of these; still treat them as
7837 short-circuiting. */
7838 case TRUTH_AND_EXPR:
7839 case TRUTH_ANDIF_EXPR:
7840 r = cxx_eval_logical_expression (call, t, boolean_false_node,
7841 boolean_true_node,
7842 allow_non_constant, addr,
7843 non_constant_p, overflow_p);
7844 break;
7846 case TRUTH_OR_EXPR:
7847 case TRUTH_ORIF_EXPR:
7848 r = cxx_eval_logical_expression (call, t, boolean_true_node,
7849 boolean_false_node,
7850 allow_non_constant, addr,
7851 non_constant_p, overflow_p);
7852 break;
7854 case ARRAY_REF:
7855 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
7856 non_constant_p, overflow_p);
7857 break;
7859 case COMPONENT_REF:
7860 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
7861 non_constant_p, overflow_p);
7862 break;
7864 case BIT_FIELD_REF:
7865 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
7866 non_constant_p, overflow_p);
7867 break;
7869 case COND_EXPR:
7870 case VEC_COND_EXPR:
7871 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
7872 non_constant_p, overflow_p);
7873 break;
7875 case CONSTRUCTOR:
7876 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
7877 non_constant_p, overflow_p);
7878 break;
7880 case VEC_INIT_EXPR:
7881 /* We can get this in a defaulted constructor for a class with a
7882 non-static data member of array type. Either the initializer will
7883 be NULL, meaning default-initialization, or it will be an lvalue
7884 or xvalue of the same type, meaning direct-initialization from the
7885 corresponding member. */
7886 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
7887 non_constant_p, overflow_p);
7888 break;
7890 case VEC_PERM_EXPR:
7891 r = cxx_eval_vec_perm_expr (call, t, allow_non_constant, addr,
7892 non_constant_p, overflow_p);
7893 break;
7895 case CONVERT_EXPR:
7896 case VIEW_CONVERT_EXPR:
7897 case NOP_EXPR:
7899 tree oldop = TREE_OPERAND (t, 0);
7900 tree op = cxx_eval_constant_expression (call, oldop,
7901 allow_non_constant, addr,
7902 non_constant_p, overflow_p);
7903 if (*non_constant_p)
7904 return t;
7905 if (op == oldop)
7906 /* We didn't fold at the top so we could check for ptr-int
7907 conversion. */
7908 return fold (t);
7909 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
7910 /* Conversion of an out-of-range value has implementation-defined
7911 behavior; the language considers it different from arithmetic
7912 overflow, which is undefined. */
7913 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7914 TREE_OVERFLOW (r) = false;
7916 break;
7918 case EMPTY_CLASS_EXPR:
7919 /* This is good enough for a function argument that might not get
7920 used, and they can't do anything with it, so just return it. */
7921 return t;
7923 case LAMBDA_EXPR:
7924 case PREINCREMENT_EXPR:
7925 case POSTINCREMENT_EXPR:
7926 case PREDECREMENT_EXPR:
7927 case POSTDECREMENT_EXPR:
7928 case NEW_EXPR:
7929 case VEC_NEW_EXPR:
7930 case DELETE_EXPR:
7931 case VEC_DELETE_EXPR:
7932 case THROW_EXPR:
7933 case MODIFY_EXPR:
7934 case MODOP_EXPR:
7935 /* GCC internal stuff. */
7936 case VA_ARG_EXPR:
7937 case OBJ_TYPE_REF:
7938 case WITH_CLEANUP_EXPR:
7939 case STATEMENT_LIST:
7940 case BIND_EXPR:
7941 case NON_DEPENDENT_EXPR:
7942 case BASELINK:
7943 case EXPR_STMT:
7944 case OFFSET_REF:
7945 if (!allow_non_constant)
7946 error_at (EXPR_LOC_OR_HERE (t),
7947 "expression %qE is not a constant-expression", t);
7948 *non_constant_p = true;
7949 break;
7951 default:
7952 internal_error ("unexpected expression %qE of kind %s", t,
7953 tree_code_name[TREE_CODE (t)]);
7954 *non_constant_p = true;
7955 break;
7958 if (r == error_mark_node)
7959 *non_constant_p = true;
7961 if (*non_constant_p)
7962 return t;
7963 else
7964 return r;
7967 static tree
7968 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
7970 bool non_constant_p = false;
7971 bool overflow_p = false;
7972 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
7973 false, &non_constant_p, &overflow_p);
7975 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
7977 if (TREE_CODE (t) != CONSTRUCTOR
7978 && cp_has_mutable_p (TREE_TYPE (t)))
7980 /* We allow a mutable type if the original expression was a
7981 CONSTRUCTOR so that we can do aggregate initialization of
7982 constexpr variables. */
7983 if (!allow_non_constant)
7984 error ("%qT cannot be the type of a complete constant expression "
7985 "because it has mutable sub-objects", TREE_TYPE (t));
7986 non_constant_p = true;
7989 /* Technically we should check this for all subexpressions, but that
7990 runs into problems with our internal representation of pointer
7991 subtraction and the 5.19 rules are still in flux. */
7992 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
7993 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
7994 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
7996 if (!allow_non_constant)
7997 error ("conversion from pointer type %qT "
7998 "to arithmetic type %qT in a constant-expression",
7999 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
8000 non_constant_p = true;
8003 if (!non_constant_p && overflow_p)
8004 non_constant_p = true;
8006 if (non_constant_p && !allow_non_constant)
8007 return error_mark_node;
8008 else if (non_constant_p && TREE_CONSTANT (r))
8010 /* This isn't actually constant, so unset TREE_CONSTANT. */
8011 if (EXPR_P (r))
8012 r = copy_node (r);
8013 else if (TREE_CODE (r) == CONSTRUCTOR)
8014 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
8015 else
8016 r = build_nop (TREE_TYPE (r), r);
8017 TREE_CONSTANT (r) = false;
8019 else if (non_constant_p || r == t)
8020 return t;
8022 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8024 if (TREE_CODE (t) == TARGET_EXPR
8025 && TARGET_EXPR_INITIAL (t) == r)
8026 return t;
8027 else
8029 r = get_target_expr (r);
8030 TREE_CONSTANT (r) = true;
8031 return r;
8034 else
8035 return r;
8038 /* Returns true if T is a valid subexpression of a constant expression,
8039 even if it isn't itself a constant expression. */
8041 bool
8042 is_sub_constant_expr (tree t)
8044 bool non_constant_p = false;
8045 bool overflow_p = false;
8046 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
8047 &overflow_p);
8048 return !non_constant_p && !overflow_p;
8051 /* If T represents a constant expression returns its reduced value.
8052 Otherwise return error_mark_node. If T is dependent, then
8053 return NULL. */
8055 tree
8056 cxx_constant_value (tree t)
8058 return cxx_eval_outermost_constant_expr (t, false);
8061 /* If T is a constant expression, returns its reduced value.
8062 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8063 Otherwise, returns a version of T without TREE_CONSTANT. */
8065 tree
8066 maybe_constant_value (tree t)
8068 tree r;
8070 if (type_dependent_expression_p (t)
8071 || type_unknown_p (t)
8072 || BRACE_ENCLOSED_INITIALIZER_P (t)
8073 || !potential_constant_expression (t)
8074 || value_dependent_expression_p (t))
8076 if (TREE_OVERFLOW_P (t))
8078 t = build_nop (TREE_TYPE (t), t);
8079 TREE_CONSTANT (t) = false;
8081 return t;
8084 r = cxx_eval_outermost_constant_expr (t, true);
8085 #ifdef ENABLE_CHECKING
8086 /* cp_tree_equal looks through NOPs, so allow them. */
8087 gcc_assert (r == t
8088 || CONVERT_EXPR_P (t)
8089 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8090 || !cp_tree_equal (r, t));
8091 #endif
8092 return r;
8095 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8096 than wrapped in a TARGET_EXPR. */
8098 tree
8099 maybe_constant_init (tree t)
8101 t = maybe_constant_value (t);
8102 if (TREE_CODE (t) == TARGET_EXPR)
8104 tree init = TARGET_EXPR_INITIAL (t);
8105 if (TREE_CODE (init) == CONSTRUCTOR)
8106 t = init;
8108 return t;
8111 #if 0
8112 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8113 /* Return true if the object referred to by REF has automatic or thread
8114 local storage. */
8116 enum { ck_ok, ck_bad, ck_unknown };
8117 static int
8118 check_automatic_or_tls (tree ref)
8120 enum machine_mode mode;
8121 HOST_WIDE_INT bitsize, bitpos;
8122 tree offset;
8123 int volatilep = 0, unsignedp = 0;
8124 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8125 &mode, &unsignedp, &volatilep, false);
8126 duration_kind dk;
8128 /* If there isn't a decl in the middle, we don't know the linkage here,
8129 and this isn't a constant expression anyway. */
8130 if (!DECL_P (decl))
8131 return ck_unknown;
8132 dk = decl_storage_duration (decl);
8133 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8135 #endif
8137 /* Return true if T denotes a potentially constant expression. Issue
8138 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8139 an lvalue-rvalue conversion is implied.
8141 C++0x [expr.const] used to say
8143 6 An expression is a potential constant expression if it is
8144 a constant expression where all occurences of function
8145 parameters are replaced by arbitrary constant expressions
8146 of the appropriate type.
8148 2 A conditional expression is a constant expression unless it
8149 involves one of the following as a potentially evaluated
8150 subexpression (3.2), but subexpressions of logical AND (5.14),
8151 logical OR (5.15), and conditional (5.16) operations that are
8152 not evaluated are not considered. */
8154 static bool
8155 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
8157 enum { any = false, rval = true };
8158 int i;
8159 tree tmp;
8161 if (t == error_mark_node)
8162 return false;
8163 if (t == NULL_TREE)
8164 return true;
8165 if (TREE_THIS_VOLATILE (t))
8167 if (flags & tf_error)
8168 error ("expression %qE has side-effects", t);
8169 return false;
8171 if (CONSTANT_CLASS_P (t))
8172 return true;
8174 switch (TREE_CODE (t))
8176 case FUNCTION_DECL:
8177 case BASELINK:
8178 case TEMPLATE_DECL:
8179 case OVERLOAD:
8180 case TEMPLATE_ID_EXPR:
8181 case LABEL_DECL:
8182 case CONST_DECL:
8183 case SIZEOF_EXPR:
8184 case ALIGNOF_EXPR:
8185 case OFFSETOF_EXPR:
8186 case NOEXCEPT_EXPR:
8187 case TEMPLATE_PARM_INDEX:
8188 case TRAIT_EXPR:
8189 case IDENTIFIER_NODE:
8190 case USERDEF_LITERAL:
8191 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8192 case FIELD_DECL:
8193 case PARM_DECL:
8194 case USING_DECL:
8195 return true;
8197 case AGGR_INIT_EXPR:
8198 case CALL_EXPR:
8199 /* -- an invocation of a function other than a constexpr function
8200 or a constexpr constructor. */
8202 tree fun = get_function_named_in_call (t);
8203 const int nargs = call_expr_nargs (t);
8204 i = 0;
8206 if (is_overloaded_fn (fun))
8208 if (TREE_CODE (fun) == FUNCTION_DECL)
8210 if (builtin_valid_in_constant_expr_p (fun))
8211 return true;
8212 if (!DECL_DECLARED_CONSTEXPR_P (fun)
8213 /* Allow any built-in function; if the expansion
8214 isn't constant, we'll deal with that then. */
8215 && !is_builtin_fn (fun))
8217 if (flags & tf_error)
8219 error_at (EXPR_LOC_OR_HERE (t),
8220 "call to non-constexpr function %qD", fun);
8221 explain_invalid_constexpr_fn (fun);
8223 return false;
8225 /* A call to a non-static member function takes the address
8226 of the object as the first argument. But in a constant
8227 expression the address will be folded away, so look
8228 through it now. */
8229 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
8230 && !DECL_CONSTRUCTOR_P (fun))
8232 tree x = get_nth_callarg (t, 0);
8233 if (is_this_parameter (x))
8235 if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8237 if (flags & tf_error)
8238 sorry ("calling a member function of the "
8239 "object being constructed in a constant "
8240 "expression");
8241 return false;
8243 /* Otherwise OK. */;
8245 else if (!potential_constant_expression_1 (x, rval, flags))
8246 return false;
8247 i = 1;
8250 else
8251 fun = get_first_fn (fun);
8252 /* Skip initial arguments to base constructors. */
8253 if (DECL_BASE_CONSTRUCTOR_P (fun))
8254 i = num_artificial_parms_for (fun);
8255 fun = DECL_ORIGIN (fun);
8257 else
8259 if (potential_constant_expression_1 (fun, rval, flags))
8260 /* Might end up being a constant function pointer. */;
8261 else
8262 return false;
8264 for (; i < nargs; ++i)
8266 tree x = get_nth_callarg (t, i);
8267 if (!potential_constant_expression_1 (x, rval, flags))
8268 return false;
8270 return true;
8273 case NON_LVALUE_EXPR:
8274 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8275 -- an lvalue of integral type that refers to a non-volatile
8276 const variable or static data member initialized with
8277 constant expressions, or
8279 -- an lvalue of literal type that refers to non-volatile
8280 object defined with constexpr, or that refers to a
8281 sub-object of such an object; */
8282 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
8284 case VAR_DECL:
8285 if (want_rval && !decl_constant_var_p (t)
8286 && !dependent_type_p (TREE_TYPE (t)))
8288 if (flags & tf_error)
8289 non_const_var_error (t);
8290 return false;
8292 return true;
8294 case NOP_EXPR:
8295 case CONVERT_EXPR:
8296 case VIEW_CONVERT_EXPR:
8297 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8298 may change to something more specific to type-punning (DR 1312). */
8300 tree from = TREE_OPERAND (t, 0);
8301 return (potential_constant_expression_1
8302 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
8305 case ADDR_EXPR:
8306 /* -- a unary operator & that is applied to an lvalue that
8307 designates an object with thread or automatic storage
8308 duration; */
8309 t = TREE_OPERAND (t, 0);
8310 #if 0
8311 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8312 any checking here, as we might dereference the pointer later. If
8313 we remove this code, also remove check_automatic_or_tls. */
8314 i = check_automatic_or_tls (t);
8315 if (i == ck_ok)
8316 return true;
8317 if (i == ck_bad)
8319 if (flags & tf_error)
8320 error ("address-of an object %qE with thread local or "
8321 "automatic storage is not a constant expression", t);
8322 return false;
8324 #endif
8325 return potential_constant_expression_1 (t, any, flags);
8327 case COMPONENT_REF:
8328 case BIT_FIELD_REF:
8329 case ARROW_EXPR:
8330 case OFFSET_REF:
8331 /* -- a class member access unless its postfix-expression is
8332 of literal type or of pointer to literal type. */
8333 /* This test would be redundant, as it follows from the
8334 postfix-expression being a potential constant expression. */
8335 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8336 want_rval, flags);
8338 case EXPR_PACK_EXPANSION:
8339 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
8340 want_rval, flags);
8342 case INDIRECT_REF:
8344 tree x = TREE_OPERAND (t, 0);
8345 STRIP_NOPS (x);
8346 if (is_this_parameter (x))
8348 if (want_rval && DECL_CONTEXT (x)
8349 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8351 if (flags & tf_error)
8352 sorry ("use of the value of the object being constructed "
8353 "in a constant expression");
8354 return false;
8356 return true;
8358 return potential_constant_expression_1 (x, rval, flags);
8361 case LAMBDA_EXPR:
8362 case DYNAMIC_CAST_EXPR:
8363 case PSEUDO_DTOR_EXPR:
8364 case PREINCREMENT_EXPR:
8365 case POSTINCREMENT_EXPR:
8366 case PREDECREMENT_EXPR:
8367 case POSTDECREMENT_EXPR:
8368 case NEW_EXPR:
8369 case VEC_NEW_EXPR:
8370 case DELETE_EXPR:
8371 case VEC_DELETE_EXPR:
8372 case THROW_EXPR:
8373 case MODIFY_EXPR:
8374 case MODOP_EXPR:
8375 /* GCC internal stuff. */
8376 case VA_ARG_EXPR:
8377 case OBJ_TYPE_REF:
8378 case WITH_CLEANUP_EXPR:
8379 case CLEANUP_POINT_EXPR:
8380 case MUST_NOT_THROW_EXPR:
8381 case TRY_CATCH_EXPR:
8382 case STATEMENT_LIST:
8383 /* Don't bother trying to define a subset of statement-expressions to
8384 be constant-expressions, at least for now. */
8385 case STMT_EXPR:
8386 case EXPR_STMT:
8387 case BIND_EXPR:
8388 case TRANSACTION_EXPR:
8389 case IF_STMT:
8390 case DO_STMT:
8391 case FOR_STMT:
8392 case WHILE_STMT:
8393 if (flags & tf_error)
8394 error ("expression %qE is not a constant-expression", t);
8395 return false;
8397 case TYPEID_EXPR:
8398 /* -- a typeid expression whose operand is of polymorphic
8399 class type; */
8401 tree e = TREE_OPERAND (t, 0);
8402 if (!TYPE_P (e) && !type_dependent_expression_p (e)
8403 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
8405 if (flags & tf_error)
8406 error ("typeid-expression is not a constant expression "
8407 "because %qE is of polymorphic type", e);
8408 return false;
8410 return true;
8413 case MINUS_EXPR:
8414 /* -- a subtraction where both operands are pointers. */
8415 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8416 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
8418 if (flags & tf_error)
8419 error ("difference of two pointer expressions is not "
8420 "a constant expression");
8421 return false;
8423 want_rval = true;
8424 goto binary;
8426 case LT_EXPR:
8427 case LE_EXPR:
8428 case GT_EXPR:
8429 case GE_EXPR:
8430 case EQ_EXPR:
8431 case NE_EXPR:
8432 /* -- a relational or equality operator where at least
8433 one of the operands is a pointer. */
8434 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8435 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
8437 if (flags & tf_error)
8438 error ("pointer comparison expression is not a "
8439 "constant expression");
8440 return false;
8442 want_rval = true;
8443 goto binary;
8445 case BIT_NOT_EXPR:
8446 /* A destructor. */
8447 if (TYPE_P (TREE_OPERAND (t, 0)))
8448 return true;
8449 /* else fall through. */
8451 case REALPART_EXPR:
8452 case IMAGPART_EXPR:
8453 case CONJ_EXPR:
8454 case SAVE_EXPR:
8455 case FIX_TRUNC_EXPR:
8456 case FLOAT_EXPR:
8457 case NEGATE_EXPR:
8458 case ABS_EXPR:
8459 case TRUTH_NOT_EXPR:
8460 case FIXED_CONVERT_EXPR:
8461 case UNARY_PLUS_EXPR:
8462 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
8463 flags);
8465 case CAST_EXPR:
8466 case CONST_CAST_EXPR:
8467 case STATIC_CAST_EXPR:
8468 case REINTERPRET_CAST_EXPR:
8469 case IMPLICIT_CONV_EXPR:
8470 return (potential_constant_expression_1
8471 (TREE_OPERAND (t, 0),
8472 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
8474 case PAREN_EXPR:
8475 case NON_DEPENDENT_EXPR:
8476 /* For convenience. */
8477 case RETURN_EXPR:
8478 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8479 want_rval, flags);
8481 case SCOPE_REF:
8482 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8483 want_rval, flags);
8485 case TARGET_EXPR:
8486 if (!literal_type_p (TREE_TYPE (t)))
8488 if (flags & tf_error)
8490 error ("temporary of non-literal type %qT in a "
8491 "constant expression", TREE_TYPE (t));
8492 explain_non_literal_class (TREE_TYPE (t));
8494 return false;
8496 case INIT_EXPR:
8497 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8498 rval, flags);
8500 case CONSTRUCTOR:
8502 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8503 constructor_elt *ce;
8504 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8505 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
8506 return false;
8507 return true;
8510 case TREE_LIST:
8512 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8513 || DECL_P (TREE_PURPOSE (t)));
8514 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
8515 flags))
8516 return false;
8517 if (TREE_CHAIN (t) == NULL_TREE)
8518 return true;
8519 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
8520 flags);
8523 case TRUNC_DIV_EXPR:
8524 case CEIL_DIV_EXPR:
8525 case FLOOR_DIV_EXPR:
8526 case ROUND_DIV_EXPR:
8527 case TRUNC_MOD_EXPR:
8528 case CEIL_MOD_EXPR:
8529 case ROUND_MOD_EXPR:
8531 tree denom = TREE_OPERAND (t, 1);
8532 /* We can't call maybe_constant_value on an expression
8533 that hasn't been through fold_non_dependent_expr yet. */
8534 if (!processing_template_decl)
8535 denom = maybe_constant_value (denom);
8536 if (integer_zerop (denom))
8538 if (flags & tf_error)
8539 error ("division by zero is not a constant-expression");
8540 return false;
8542 else
8544 want_rval = true;
8545 goto binary;
8549 case COMPOUND_EXPR:
8551 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8552 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8553 introduced by build_call_a. */
8554 tree op0 = TREE_OPERAND (t, 0);
8555 tree op1 = TREE_OPERAND (t, 1);
8556 STRIP_NOPS (op1);
8557 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8558 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8559 return potential_constant_expression_1 (op0, want_rval, flags);
8560 else
8561 goto binary;
8564 /* If the first operand is the non-short-circuit constant, look at
8565 the second operand; otherwise we only care about the first one for
8566 potentiality. */
8567 case TRUTH_AND_EXPR:
8568 case TRUTH_ANDIF_EXPR:
8569 tmp = boolean_true_node;
8570 goto truth;
8571 case TRUTH_OR_EXPR:
8572 case TRUTH_ORIF_EXPR:
8573 tmp = boolean_false_node;
8574 truth:
8576 tree op = TREE_OPERAND (t, 0);
8577 if (!potential_constant_expression_1 (op, rval, flags))
8578 return false;
8579 if (!processing_template_decl)
8580 op = maybe_constant_value (op);
8581 if (tree_int_cst_equal (op, tmp))
8582 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
8583 else
8584 return true;
8587 case PLUS_EXPR:
8588 case MULT_EXPR:
8589 case POINTER_PLUS_EXPR:
8590 case RDIV_EXPR:
8591 case EXACT_DIV_EXPR:
8592 case MIN_EXPR:
8593 case MAX_EXPR:
8594 case LSHIFT_EXPR:
8595 case RSHIFT_EXPR:
8596 case LROTATE_EXPR:
8597 case RROTATE_EXPR:
8598 case BIT_IOR_EXPR:
8599 case BIT_XOR_EXPR:
8600 case BIT_AND_EXPR:
8601 case TRUTH_XOR_EXPR:
8602 case UNORDERED_EXPR:
8603 case ORDERED_EXPR:
8604 case UNLT_EXPR:
8605 case UNLE_EXPR:
8606 case UNGT_EXPR:
8607 case UNGE_EXPR:
8608 case UNEQ_EXPR:
8609 case LTGT_EXPR:
8610 case RANGE_EXPR:
8611 case COMPLEX_EXPR:
8612 want_rval = true;
8613 /* Fall through. */
8614 case ARRAY_REF:
8615 case ARRAY_RANGE_REF:
8616 case MEMBER_REF:
8617 case DOTSTAR_EXPR:
8618 binary:
8619 for (i = 0; i < 2; ++i)
8620 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8621 want_rval, flags))
8622 return false;
8623 return true;
8625 case FMA_EXPR:
8626 case VEC_PERM_EXPR:
8627 for (i = 0; i < 3; ++i)
8628 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8629 true, flags))
8630 return false;
8631 return true;
8633 case COND_EXPR:
8634 case VEC_COND_EXPR:
8635 /* If the condition is a known constant, we know which of the legs we
8636 care about; otherwise we only require that the condition and
8637 either of the legs be potentially constant. */
8638 tmp = TREE_OPERAND (t, 0);
8639 if (!potential_constant_expression_1 (tmp, rval, flags))
8640 return false;
8641 if (!processing_template_decl)
8642 tmp = maybe_constant_value (tmp);
8643 if (integer_zerop (tmp))
8644 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
8645 want_rval, flags);
8646 else if (TREE_CODE (tmp) == INTEGER_CST)
8647 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8648 want_rval, flags);
8649 for (i = 1; i < 3; ++i)
8650 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
8651 want_rval, tf_none))
8652 return true;
8653 if (flags & tf_error)
8654 error ("expression %qE is not a constant-expression", t);
8655 return false;
8657 case VEC_INIT_EXPR:
8658 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
8659 return true;
8660 if (flags & tf_error)
8662 error ("non-constant array initialization");
8663 diagnose_non_constexpr_vec_init (t);
8665 return false;
8667 default:
8668 if (objc_is_property_ref (t))
8669 return false;
8671 sorry ("unexpected AST of kind %s", tree_code_name[TREE_CODE (t)]);
8672 gcc_unreachable();
8673 return false;
8677 /* The main entry point to the above. */
8679 bool
8680 potential_constant_expression (tree t)
8682 return potential_constant_expression_1 (t, false, tf_none);
8685 /* As above, but require a constant rvalue. */
8687 bool
8688 potential_rvalue_constant_expression (tree t)
8690 return potential_constant_expression_1 (t, true, tf_none);
8693 /* Like above, but complain about non-constant expressions. */
8695 bool
8696 require_potential_constant_expression (tree t)
8698 return potential_constant_expression_1 (t, false, tf_warning_or_error);
8701 /* Cross product of the above. */
8703 bool
8704 require_potential_rvalue_constant_expression (tree t)
8706 return potential_constant_expression_1 (t, true, tf_warning_or_error);
8709 /* Constructor for a lambda expression. */
8711 tree
8712 build_lambda_expr (void)
8714 tree lambda = make_node (LAMBDA_EXPR);
8715 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
8716 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
8717 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
8718 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
8719 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
8720 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
8721 return lambda;
8724 /* Create the closure object for a LAMBDA_EXPR. */
8726 tree
8727 build_lambda_object (tree lambda_expr)
8729 /* Build aggregate constructor call.
8730 - cp_parser_braced_list
8731 - cp_parser_functional_cast */
8732 vec<constructor_elt, va_gc> *elts = NULL;
8733 tree node, expr, type;
8734 location_t saved_loc;
8736 if (processing_template_decl)
8737 return lambda_expr;
8739 /* Make sure any error messages refer to the lambda-introducer. */
8740 saved_loc = input_location;
8741 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
8743 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8744 node;
8745 node = TREE_CHAIN (node))
8747 tree field = TREE_PURPOSE (node);
8748 tree val = TREE_VALUE (node);
8750 if (field == error_mark_node)
8752 expr = error_mark_node;
8753 goto out;
8756 if (DECL_P (val))
8757 mark_used (val);
8759 /* Mere mortals can't copy arrays with aggregate initialization, so
8760 do some magic to make it work here. */
8761 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8762 val = build_array_copy (val);
8763 else if (DECL_NORMAL_CAPTURE_P (field)
8764 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
8766 /* "the entities that are captured by copy are used to
8767 direct-initialize each corresponding non-static data
8768 member of the resulting closure object."
8770 There's normally no way to express direct-initialization
8771 from an element of a CONSTRUCTOR, so we build up a special
8772 TARGET_EXPR to bypass the usual copy-initialization. */
8773 val = force_rvalue (val, tf_warning_or_error);
8774 if (TREE_CODE (val) == TARGET_EXPR)
8775 TARGET_EXPR_DIRECT_INIT_P (val) = true;
8778 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
8781 expr = build_constructor (init_list_type_node, elts);
8782 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
8784 /* N2927: "[The closure] class type is not an aggregate."
8785 But we briefly treat it as an aggregate to make this simpler. */
8786 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
8787 CLASSTYPE_NON_AGGREGATE (type) = 0;
8788 expr = finish_compound_literal (type, expr, tf_warning_or_error);
8789 CLASSTYPE_NON_AGGREGATE (type) = 1;
8791 out:
8792 input_location = saved_loc;
8793 return expr;
8796 /* Return an initialized RECORD_TYPE for LAMBDA.
8797 LAMBDA must have its explicit captures already. */
8799 tree
8800 begin_lambda_type (tree lambda)
8802 tree type;
8805 /* Unique name. This is just like an unnamed class, but we cannot use
8806 make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
8807 tree name;
8808 name = make_lambda_name ();
8810 /* Create the new RECORD_TYPE for this lambda. */
8811 type = xref_tag (/*tag_code=*/record_type,
8812 name,
8813 /*scope=*/ts_within_enclosing_non_class,
8814 /*template_header_p=*/false);
8817 /* Designate it as a struct so that we can use aggregate initialization. */
8818 CLASSTYPE_DECLARED_CLASS (type) = false;
8820 /* Cross-reference the expression and the type. */
8821 LAMBDA_EXPR_CLOSURE (lambda) = type;
8822 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
8824 /* Clear base types. */
8825 xref_basetypes (type, /*bases=*/NULL_TREE);
8827 /* Start the class. */
8828 type = begin_class_definition (type);
8829 if (type == error_mark_node)
8830 return error_mark_node;
8832 return type;
8835 /* Returns the type to use for the return type of the operator() of a
8836 closure class. */
8838 tree
8839 lambda_return_type (tree expr)
8841 if (expr == NULL_TREE)
8842 return void_type_node;
8843 if (type_unknown_p (expr)
8844 || BRACE_ENCLOSED_INITIALIZER_P (expr))
8846 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
8847 return void_type_node;
8849 gcc_checking_assert (!type_dependent_expression_p (expr));
8850 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
8853 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
8854 closure type. */
8856 tree
8857 lambda_function (tree lambda)
8859 tree type;
8860 if (TREE_CODE (lambda) == LAMBDA_EXPR)
8861 type = LAMBDA_EXPR_CLOSURE (lambda);
8862 else
8863 type = lambda;
8864 gcc_assert (LAMBDA_TYPE_P (type));
8865 /* Don't let debug_tree cause instantiation. */
8866 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
8867 && !COMPLETE_OR_OPEN_TYPE_P (type))
8868 return NULL_TREE;
8869 lambda = lookup_member (type, ansi_opname (CALL_EXPR),
8870 /*protect=*/0, /*want_type=*/false,
8871 tf_warning_or_error);
8872 if (lambda)
8873 lambda = BASELINK_FUNCTIONS (lambda);
8874 return lambda;
8877 /* Returns the type to use for the FIELD_DECL corresponding to the
8878 capture of EXPR.
8879 The caller should add REFERENCE_TYPE for capture by reference. */
8881 tree
8882 lambda_capture_field_type (tree expr)
8884 tree type;
8885 if (type_dependent_expression_p (expr))
8887 type = cxx_make_type (DECLTYPE_TYPE);
8888 DECLTYPE_TYPE_EXPR (type) = expr;
8889 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
8890 SET_TYPE_STRUCTURAL_EQUALITY (type);
8892 else
8893 type = non_reference (unlowered_expr_type (expr));
8894 return type;
8897 /* Insert the deduced return type for an auto function. */
8899 void
8900 apply_deduced_return_type (tree fco, tree return_type)
8902 tree result;
8904 if (return_type == error_mark_node)
8905 return;
8907 if (LAMBDA_FUNCTION_P (fco))
8909 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
8910 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
8913 if (DECL_CONV_FN_P (fco))
8914 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
8916 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
8918 result = DECL_RESULT (fco);
8919 if (result == NULL_TREE)
8920 return;
8921 if (TREE_TYPE (result) == return_type)
8922 return;
8924 /* We already have a DECL_RESULT from start_preparsed_function.
8925 Now we need to redo the work it and allocate_struct_function
8926 did to reflect the new type. */
8927 gcc_assert (current_function_decl == fco);
8928 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
8929 TYPE_MAIN_VARIANT (return_type));
8930 DECL_ARTIFICIAL (result) = 1;
8931 DECL_IGNORED_P (result) = 1;
8932 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
8933 result);
8935 DECL_RESULT (fco) = result;
8937 if (!processing_template_decl)
8939 bool aggr = aggregate_value_p (result, fco);
8940 #ifdef PCC_STATIC_STRUCT_RETURN
8941 cfun->returns_pcc_struct = aggr;
8942 #endif
8943 cfun->returns_struct = aggr;
8948 /* DECL is a local variable or parameter from the surrounding scope of a
8949 lambda-expression. Returns the decltype for a use of the capture field
8950 for DECL even if it hasn't been captured yet. */
8952 static tree
8953 capture_decltype (tree decl)
8955 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
8956 /* FIXME do lookup instead of list walk? */
8957 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
8958 tree type;
8960 if (cap)
8961 type = TREE_TYPE (TREE_PURPOSE (cap));
8962 else
8963 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
8965 case CPLD_NONE:
8966 error ("%qD is not captured", decl);
8967 return error_mark_node;
8969 case CPLD_COPY:
8970 type = TREE_TYPE (decl);
8971 if (TREE_CODE (type) == REFERENCE_TYPE
8972 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
8973 type = TREE_TYPE (type);
8974 break;
8976 case CPLD_REFERENCE:
8977 type = TREE_TYPE (decl);
8978 if (TREE_CODE (type) != REFERENCE_TYPE)
8979 type = build_reference_type (TREE_TYPE (decl));
8980 break;
8982 default:
8983 gcc_unreachable ();
8986 if (TREE_CODE (type) != REFERENCE_TYPE)
8988 if (!LAMBDA_EXPR_MUTABLE_P (lam))
8989 type = cp_build_qualified_type (type, (cp_type_quals (type)
8990 |TYPE_QUAL_CONST));
8991 type = build_reference_type (type);
8993 return type;
8996 /* Returns true iff DECL is a lambda capture proxy variable created by
8997 build_capture_proxy. */
8999 bool
9000 is_capture_proxy (tree decl)
9002 return (TREE_CODE (decl) == VAR_DECL
9003 && DECL_HAS_VALUE_EXPR_P (decl)
9004 && !DECL_ANON_UNION_VAR_P (decl)
9005 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
9008 /* Returns true iff DECL is a capture proxy for a normal capture
9009 (i.e. without explicit initializer). */
9011 bool
9012 is_normal_capture_proxy (tree decl)
9014 if (!is_capture_proxy (decl))
9015 /* It's not a capture proxy. */
9016 return false;
9018 /* It is a capture proxy, is it a normal capture? */
9019 tree val = DECL_VALUE_EXPR (decl);
9020 if (val == error_mark_node)
9021 return true;
9023 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
9024 val = TREE_OPERAND (val, 1);
9025 return DECL_NORMAL_CAPTURE_P (val);
9028 /* VAR is a capture proxy created by build_capture_proxy; add it to the
9029 current function, which is the operator() for the appropriate lambda. */
9031 void
9032 insert_capture_proxy (tree var)
9034 cp_binding_level *b;
9035 int skip;
9036 tree stmt_list;
9038 /* Put the capture proxy in the extra body block so that it won't clash
9039 with a later local variable. */
9040 b = current_binding_level;
9041 for (skip = 0; ; ++skip)
9043 cp_binding_level *n = b->level_chain;
9044 if (n->kind == sk_function_parms)
9045 break;
9046 b = n;
9048 pushdecl_with_scope (var, b, false);
9050 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
9051 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
9052 stmt_list = (*stmt_list_stack)[stmt_list_stack->length () - 1 - skip];
9053 gcc_assert (stmt_list);
9054 append_to_statement_list_force (var, &stmt_list);
9057 /* We've just finished processing a lambda; if the containing scope is also
9058 a lambda, insert any capture proxies that were created while processing
9059 the nested lambda. */
9061 void
9062 insert_pending_capture_proxies (void)
9064 tree lam;
9065 vec<tree, va_gc> *proxies;
9066 unsigned i;
9068 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
9069 return;
9071 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9072 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
9073 for (i = 0; i < vec_safe_length (proxies); ++i)
9075 tree var = (*proxies)[i];
9076 insert_capture_proxy (var);
9078 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
9079 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
9082 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
9083 return the type we want the proxy to have: the type of the field itself,
9084 with added const-qualification if the lambda isn't mutable and the
9085 capture is by value. */
9087 tree
9088 lambda_proxy_type (tree ref)
9090 tree type;
9091 if (REFERENCE_REF_P (ref))
9092 ref = TREE_OPERAND (ref, 0);
9093 type = TREE_TYPE (ref);
9094 if (!dependent_type_p (type))
9095 return type;
9096 type = cxx_make_type (DECLTYPE_TYPE);
9097 DECLTYPE_TYPE_EXPR (type) = ref;
9098 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
9099 SET_TYPE_STRUCTURAL_EQUALITY (type);
9100 return type;
9103 /* MEMBER is a capture field in a lambda closure class. Now that we're
9104 inside the operator(), build a placeholder var for future lookups and
9105 debugging. */
9107 tree
9108 build_capture_proxy (tree member)
9110 tree var, object, fn, closure, name, lam, type;
9112 closure = DECL_CONTEXT (member);
9113 fn = lambda_function (closure);
9114 lam = CLASSTYPE_LAMBDA_EXPR (closure);
9116 /* The proxy variable forwards to the capture field. */
9117 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
9118 object = finish_non_static_data_member (member, object, NULL_TREE);
9119 if (REFERENCE_REF_P (object))
9120 object = TREE_OPERAND (object, 0);
9122 /* Remove the __ inserted by add_capture. */
9123 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
9125 type = lambda_proxy_type (object);
9126 var = build_decl (input_location, VAR_DECL, name, type);
9127 SET_DECL_VALUE_EXPR (var, object);
9128 DECL_HAS_VALUE_EXPR_P (var) = 1;
9129 DECL_ARTIFICIAL (var) = 1;
9130 TREE_USED (var) = 1;
9131 DECL_CONTEXT (var) = fn;
9133 if (name == this_identifier)
9135 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
9136 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
9139 if (fn == current_function_decl)
9140 insert_capture_proxy (var);
9141 else
9142 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
9144 return var;
9147 /* From an ID and INITIALIZER, create a capture (by reference if
9148 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
9149 and return it. */
9151 tree
9152 add_capture (tree lambda, tree id, tree initializer, bool by_reference_p,
9153 bool explicit_init_p)
9155 char *buf;
9156 tree type, member, name;
9158 type = lambda_capture_field_type (initializer);
9159 if (by_reference_p)
9161 type = build_reference_type (type);
9162 if (!real_lvalue_p (initializer))
9163 error ("cannot capture %qE by reference", initializer);
9165 else
9166 /* Capture by copy requires a complete type. */
9167 type = complete_type (type);
9169 /* Add __ to the beginning of the field name so that user code
9170 won't find the field with name lookup. We can't just leave the name
9171 unset because template instantiation uses the name to find
9172 instantiated fields. */
9173 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
9174 buf[1] = buf[0] = '_';
9175 memcpy (buf + 2, IDENTIFIER_POINTER (id),
9176 IDENTIFIER_LENGTH (id) + 1);
9177 name = get_identifier (buf);
9179 /* If TREE_TYPE isn't set, we're still in the introducer, so check
9180 for duplicates. */
9181 if (!LAMBDA_EXPR_CLOSURE (lambda))
9183 if (IDENTIFIER_MARKED (name))
9185 pedwarn (input_location, 0,
9186 "already captured %qD in lambda expression", id);
9187 return NULL_TREE;
9189 IDENTIFIER_MARKED (name) = true;
9192 /* Make member variable. */
9193 member = build_lang_decl (FIELD_DECL, name, type);
9195 if (!explicit_init_p)
9196 /* Normal captures are invisible to name lookup but uses are replaced
9197 with references to the capture field; we implement this by only
9198 really making them invisible in unevaluated context; see
9199 qualify_lookup. For now, let's make explicitly initialized captures
9200 always visible. */
9201 DECL_NORMAL_CAPTURE_P (member) = true;
9203 if (id == this_identifier)
9204 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
9206 /* Add it to the appropriate closure class if we've started it. */
9207 if (current_class_type
9208 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
9209 finish_member_declaration (member);
9211 LAMBDA_EXPR_CAPTURE_LIST (lambda)
9212 = tree_cons (member, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
9214 if (LAMBDA_EXPR_CLOSURE (lambda))
9215 return build_capture_proxy (member);
9216 /* For explicit captures we haven't started the function yet, so we wait
9217 and build the proxy from cp_parser_lambda_body. */
9218 return NULL_TREE;
9221 /* Register all the capture members on the list CAPTURES, which is the
9222 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
9224 void
9225 register_capture_members (tree captures)
9227 if (captures == NULL_TREE)
9228 return;
9230 register_capture_members (TREE_CHAIN (captures));
9231 /* We set this in add_capture to avoid duplicates. */
9232 IDENTIFIER_MARKED (DECL_NAME (TREE_PURPOSE (captures))) = false;
9233 finish_member_declaration (TREE_PURPOSE (captures));
9236 /* Similar to add_capture, except this works on a stack of nested lambdas.
9237 BY_REFERENCE_P in this case is derived from the default capture mode.
9238 Returns the capture for the lambda at the bottom of the stack. */
9240 tree
9241 add_default_capture (tree lambda_stack, tree id, tree initializer)
9243 bool this_capture_p = (id == this_identifier);
9245 tree var = NULL_TREE;
9247 tree saved_class_type = current_class_type;
9249 tree node;
9251 for (node = lambda_stack;
9252 node;
9253 node = TREE_CHAIN (node))
9255 tree lambda = TREE_VALUE (node);
9257 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
9258 var = add_capture (lambda,
9260 initializer,
9261 /*by_reference_p=*/
9262 (!this_capture_p
9263 && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
9264 == CPLD_REFERENCE)),
9265 /*explicit_init_p=*/false);
9266 initializer = convert_from_reference (var);
9269 current_class_type = saved_class_type;
9271 return var;
9274 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the form of an
9275 INDIRECT_REF, possibly adding it through default capturing. */
9277 tree
9278 lambda_expr_this_capture (tree lambda)
9280 tree result;
9282 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9284 /* Try to default capture 'this' if we can. */
9285 if (!this_capture
9286 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)
9288 tree containing_function = TYPE_CONTEXT (LAMBDA_EXPR_CLOSURE (lambda));
9289 tree lambda_stack = tree_cons (NULL_TREE, lambda, NULL_TREE);
9290 tree init = NULL_TREE;
9292 /* If we are in a lambda function, we can move out until we hit:
9293 1. a non-lambda function,
9294 2. a lambda function capturing 'this', or
9295 3. a non-default capturing lambda function. */
9296 while (LAMBDA_FUNCTION_P (containing_function))
9298 tree lambda
9299 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
9301 if (LAMBDA_EXPR_THIS_CAPTURE (lambda))
9303 /* An outer lambda has already captured 'this'. */
9304 init = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9305 break;
9308 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_NONE)
9309 /* An outer lambda won't let us capture 'this'. */
9310 break;
9312 lambda_stack = tree_cons (NULL_TREE,
9313 lambda,
9314 lambda_stack);
9316 containing_function = decl_function_context (containing_function);
9319 if (!init && DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function)
9320 && !LAMBDA_FUNCTION_P (containing_function))
9321 /* First parameter is 'this'. */
9322 init = DECL_ARGUMENTS (containing_function);
9324 if (init)
9325 this_capture = add_default_capture (lambda_stack,
9326 /*id=*/this_identifier,
9327 init);
9330 if (!this_capture)
9332 error ("%<this%> was not captured for this lambda function");
9333 result = error_mark_node;
9335 else
9337 /* To make sure that current_class_ref is for the lambda. */
9338 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
9339 == LAMBDA_EXPR_CLOSURE (lambda));
9341 result = this_capture;
9343 /* If 'this' is captured, each use of 'this' is transformed into an
9344 access to the corresponding unnamed data member of the closure
9345 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
9346 ensures that the transformed expression is an rvalue. ] */
9347 result = rvalue (result);
9350 return result;
9353 /* Returns the method basetype of the innermost non-lambda function, or
9354 NULL_TREE if none. */
9356 tree
9357 nonlambda_method_basetype (void)
9359 tree fn, type;
9360 if (!current_class_ref)
9361 return NULL_TREE;
9363 type = current_class_type;
9364 if (!LAMBDA_TYPE_P (type))
9365 return type;
9367 /* Find the nearest enclosing non-lambda function. */
9368 fn = TYPE_NAME (type);
9370 fn = decl_function_context (fn);
9371 while (fn && LAMBDA_FUNCTION_P (fn));
9373 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9374 return NULL_TREE;
9376 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
9379 /* If the closure TYPE has a static op(), also add a conversion to function
9380 pointer. */
9382 void
9383 maybe_add_lambda_conv_op (tree type)
9385 bool nested = (current_function_decl != NULL_TREE);
9386 tree callop = lambda_function (type);
9387 tree rettype, name, fntype, fn, body, compound_stmt;
9388 tree thistype, stattype, statfn, convfn, call, arg;
9389 vec<tree, va_gc> *argvec;
9391 if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
9392 return;
9394 if (processing_template_decl)
9395 return;
9397 stattype = build_function_type (TREE_TYPE (TREE_TYPE (callop)),
9398 FUNCTION_ARG_CHAIN (callop));
9400 /* First build up the conversion op. */
9402 rettype = build_pointer_type (stattype);
9403 name = mangle_conv_op_name_for_type (rettype);
9404 thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
9405 fntype = build_method_type_directly (thistype, rettype, void_list_node);
9406 fn = convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
9407 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9409 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9410 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9411 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9413 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
9414 grokclassfn (type, fn, NO_SPECIAL);
9415 set_linkage_according_to_type (type, fn);
9416 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9417 DECL_IN_AGGR_P (fn) = 1;
9418 DECL_ARTIFICIAL (fn) = 1;
9419 DECL_NOT_REALLY_EXTERN (fn) = 1;
9420 DECL_DECLARED_INLINE_P (fn) = 1;
9421 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
9423 add_method (type, fn, NULL_TREE);
9425 /* Generic thunk code fails for varargs; we'll complain in mark_used if
9426 the conversion op is used. */
9427 if (varargs_function_p (callop))
9429 DECL_DELETED_FN (fn) = 1;
9430 return;
9433 /* Now build up the thunk to be returned. */
9435 name = get_identifier ("_FUN");
9436 fn = statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
9437 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9438 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9439 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9440 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9441 grokclassfn (type, fn, NO_SPECIAL);
9442 set_linkage_according_to_type (type, fn);
9443 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9444 DECL_IN_AGGR_P (fn) = 1;
9445 DECL_ARTIFICIAL (fn) = 1;
9446 DECL_NOT_REALLY_EXTERN (fn) = 1;
9447 DECL_DECLARED_INLINE_P (fn) = 1;
9448 DECL_STATIC_FUNCTION_P (fn) = 1;
9449 DECL_ARGUMENTS (fn) = copy_list (DECL_CHAIN (DECL_ARGUMENTS (callop)));
9450 for (arg = DECL_ARGUMENTS (fn); arg; arg = DECL_CHAIN (arg))
9451 DECL_CONTEXT (arg) = fn;
9453 add_method (type, fn, NULL_TREE);
9455 if (nested)
9456 push_function_context ();
9457 else
9458 /* Still increment function_depth so that we don't GC in the
9459 middle of an expression. */
9460 ++function_depth;
9462 /* Generate the body of the thunk. */
9464 start_preparsed_function (statfn, NULL_TREE,
9465 SF_PRE_PARSED | SF_INCLASS_INLINE);
9466 if (DECL_ONE_ONLY (statfn))
9468 /* Put the thunk in the same comdat group as the call op. */
9469 symtab_add_to_same_comdat_group
9470 ((symtab_node) cgraph_get_create_node (statfn),
9471 (symtab_node) cgraph_get_create_node (callop));
9473 body = begin_function_body ();
9474 compound_stmt = begin_compound_stmt (0);
9476 arg = build1 (NOP_EXPR, TREE_TYPE (DECL_ARGUMENTS (callop)),
9477 null_pointer_node);
9478 argvec = make_tree_vector ();
9479 argvec->quick_push (arg);
9480 for (arg = DECL_ARGUMENTS (statfn); arg; arg = DECL_CHAIN (arg))
9482 mark_exp_read (arg);
9483 vec_safe_push (argvec, arg);
9485 call = build_call_a (callop, argvec->length (), argvec->address ());
9486 CALL_FROM_THUNK_P (call) = 1;
9487 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
9488 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
9489 call = convert_from_reference (call);
9490 finish_return_stmt (call);
9492 finish_compound_stmt (compound_stmt);
9493 finish_function_body (body);
9495 expand_or_defer_fn (finish_function (2));
9497 /* Generate the body of the conversion op. */
9499 start_preparsed_function (convfn, NULL_TREE,
9500 SF_PRE_PARSED | SF_INCLASS_INLINE);
9501 body = begin_function_body ();
9502 compound_stmt = begin_compound_stmt (0);
9504 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
9506 finish_compound_stmt (compound_stmt);
9507 finish_function_body (body);
9509 expand_or_defer_fn (finish_function (2));
9511 if (nested)
9512 pop_function_context ();
9513 else
9514 --function_depth;
9517 /* Returns true iff VAL is a lambda-related declaration which should
9518 be ignored by unqualified lookup. */
9520 bool
9521 is_lambda_ignored_entity (tree val)
9523 /* In unevaluated context, look past normal capture proxies. */
9524 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
9525 return true;
9527 /* Always ignore lambda fields, their names are only for debugging. */
9528 if (TREE_CODE (val) == FIELD_DECL
9529 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
9530 return true;
9532 /* None of the lookups that use qualify_lookup want the op() from the
9533 lambda; they want the one from the enclosing class. */
9534 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
9535 return true;
9537 return false;
9540 #include "gt-cp-semantics.h"