Merge from trunk
[official-gcc.git] / gcc / cp / semantics.c
blobf43f18d0d1b6d5f3c2ee43b6e36b37dba2fca5c3
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "pointer-set.h"
48 #include "hash-table.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "omp-low.h"
53 static bool verify_constant (tree, bool, bool *, bool *);
54 #define VERIFY_CONSTANT(X) \
55 do { \
56 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
57 return t; \
58 } while (0)
60 /* There routines provide a modular interface to perform many parsing
61 operations. They may therefore be used during actual parsing, or
62 during template instantiation, which may be regarded as a
63 degenerate form of parsing. */
65 static tree maybe_convert_cond (tree);
66 static tree finalize_nrv_r (tree *, int *, void *);
67 static tree capture_decltype (tree);
70 /* Deferred Access Checking Overview
71 ---------------------------------
73 Most C++ expressions and declarations require access checking
74 to be performed during parsing. However, in several cases,
75 this has to be treated differently.
77 For member declarations, access checking has to be deferred
78 until more information about the declaration is known. For
79 example:
81 class A {
82 typedef int X;
83 public:
84 X f();
87 A::X A::f();
88 A::X g();
90 When we are parsing the function return type `A::X', we don't
91 really know if this is allowed until we parse the function name.
93 Furthermore, some contexts require that access checking is
94 never performed at all. These include class heads, and template
95 instantiations.
97 Typical use of access checking functions is described here:
99 1. When we enter a context that requires certain access checking
100 mode, the function `push_deferring_access_checks' is called with
101 DEFERRING argument specifying the desired mode. Access checking
102 may be performed immediately (dk_no_deferred), deferred
103 (dk_deferred), or not performed (dk_no_check).
105 2. When a declaration such as a type, or a variable, is encountered,
106 the function `perform_or_defer_access_check' is called. It
107 maintains a vector of all deferred checks.
109 3. The global `current_class_type' or `current_function_decl' is then
110 setup by the parser. `enforce_access' relies on these information
111 to check access.
113 4. Upon exiting the context mentioned in step 1,
114 `perform_deferred_access_checks' is called to check all declaration
115 stored in the vector. `pop_deferring_access_checks' is then
116 called to restore the previous access checking mode.
118 In case of parsing error, we simply call `pop_deferring_access_checks'
119 without `perform_deferred_access_checks'. */
121 typedef struct GTY(()) deferred_access {
122 /* A vector representing name-lookups for which we have deferred
123 checking access controls. We cannot check the accessibility of
124 names used in a decl-specifier-seq until we know what is being
125 declared because code like:
127 class A {
128 class B {};
129 B* f();
132 A::B* A::f() { return 0; }
134 is valid, even though `A::B' is not generally accessible. */
135 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
137 /* The current mode of access checks. */
138 enum deferring_kind deferring_access_checks_kind;
140 } deferred_access;
142 /* Data for deferred access checking. */
143 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
144 static GTY(()) unsigned deferred_access_no_check;
146 /* Save the current deferred access states and start deferred
147 access checking iff DEFER_P is true. */
149 void
150 push_deferring_access_checks (deferring_kind deferring)
152 /* For context like template instantiation, access checking
153 disabling applies to all nested context. */
154 if (deferred_access_no_check || deferring == dk_no_check)
155 deferred_access_no_check++;
156 else
158 deferred_access e = {NULL, deferring};
159 vec_safe_push (deferred_access_stack, e);
163 /* Save the current deferred access states and start deferred access
164 checking, continuing the set of deferred checks in CHECKS. */
166 void
167 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
169 push_deferring_access_checks (dk_deferred);
170 if (!deferred_access_no_check)
171 deferred_access_stack->last().deferred_access_checks = checks;
174 /* Resume deferring access checks again after we stopped doing
175 this previously. */
177 void
178 resume_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
184 /* Stop deferring access checks. */
186 void
187 stop_deferring_access_checks (void)
189 if (!deferred_access_no_check)
190 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
193 /* Discard the current deferred access checks and restore the
194 previous states. */
196 void
197 pop_deferring_access_checks (void)
199 if (deferred_access_no_check)
200 deferred_access_no_check--;
201 else
202 deferred_access_stack->pop ();
205 /* Returns a TREE_LIST representing the deferred checks.
206 The TREE_PURPOSE of each node is the type through which the
207 access occurred; the TREE_VALUE is the declaration named.
210 vec<deferred_access_check, va_gc> *
211 get_deferred_access_checks (void)
213 if (deferred_access_no_check)
214 return NULL;
215 else
216 return (deferred_access_stack->last().deferred_access_checks);
219 /* Take current deferred checks and combine with the
220 previous states if we also defer checks previously.
221 Otherwise perform checks now. */
223 void
224 pop_to_parent_deferring_access_checks (void)
226 if (deferred_access_no_check)
227 deferred_access_no_check--;
228 else
230 vec<deferred_access_check, va_gc> *checks;
231 deferred_access *ptr;
233 checks = (deferred_access_stack->last ().deferred_access_checks);
235 deferred_access_stack->pop ();
236 ptr = &deferred_access_stack->last ();
237 if (ptr->deferring_access_checks_kind == dk_no_deferred)
239 /* Check access. */
240 perform_access_checks (checks, tf_warning_or_error);
242 else
244 /* Merge with parent. */
245 int i, j;
246 deferred_access_check *chk, *probe;
248 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
250 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
252 if (probe->binfo == chk->binfo &&
253 probe->decl == chk->decl &&
254 probe->diag_decl == chk->diag_decl)
255 goto found;
257 /* Insert into parent's checks. */
258 vec_safe_push (ptr->deferred_access_checks, *chk);
259 found:;
265 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
266 is the BINFO indicating the qualifying scope used to access the
267 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
268 or we aren't in SFINAE context or all the checks succeed return TRUE,
269 otherwise FALSE. */
271 bool
272 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
273 tsubst_flags_t complain)
275 int i;
276 deferred_access_check *chk;
277 location_t loc = input_location;
278 bool ok = true;
280 if (!checks)
281 return true;
283 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
285 input_location = chk->loc;
286 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
289 input_location = loc;
290 return (complain & tf_error) ? true : ok;
293 /* Perform the deferred access checks.
295 After performing the checks, we still have to keep the list
296 `deferred_access_stack->deferred_access_checks' since we may want
297 to check access for them again later in a different context.
298 For example:
300 class A {
301 typedef int X;
302 static X a;
304 A::X A::a, x; // No error for `A::a', error for `x'
306 We have to perform deferred access of `A::X', first with `A::a',
307 next with `x'. Return value like perform_access_checks above. */
309 bool
310 perform_deferred_access_checks (tsubst_flags_t complain)
312 return perform_access_checks (get_deferred_access_checks (), complain);
315 /* Defer checking the accessibility of DECL, when looked up in
316 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
317 Return value like perform_access_checks above. */
319 bool
320 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
321 tsubst_flags_t complain)
323 int i;
324 deferred_access *ptr;
325 deferred_access_check *chk;
328 /* Exit if we are in a context that no access checking is performed.
330 if (deferred_access_no_check)
331 return true;
333 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
335 ptr = &deferred_access_stack->last ();
337 /* If we are not supposed to defer access checks, just check now. */
338 if (ptr->deferring_access_checks_kind == dk_no_deferred)
340 bool ok = enforce_access (binfo, decl, diag_decl, complain);
341 return (complain & tf_error) ? true : ok;
344 /* See if we are already going to perform this check. */
345 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
347 if (chk->decl == decl && chk->binfo == binfo &&
348 chk->diag_decl == diag_decl)
350 return true;
353 /* If not, record the check. */
354 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
355 vec_safe_push (ptr->deferred_access_checks, new_access);
357 return true;
360 /* Returns nonzero if the current statement is a full expression,
361 i.e. temporaries created during that statement should be destroyed
362 at the end of the statement. */
365 stmts_are_full_exprs_p (void)
367 return current_stmt_tree ()->stmts_are_full_exprs_p;
370 /* T is a statement. Add it to the statement-tree. This is the C++
371 version. The C/ObjC frontends have a slightly different version of
372 this function. */
374 tree
375 add_stmt (tree t)
377 enum tree_code code = TREE_CODE (t);
379 if (EXPR_P (t) && code != LABEL_EXPR)
381 if (!EXPR_HAS_LOCATION (t))
382 SET_EXPR_LOCATION (t, input_location);
384 /* When we expand a statement-tree, we must know whether or not the
385 statements are full-expressions. We record that fact here. */
386 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
394 return t;
397 /* Returns the stmt_tree to which statements are currently being added. */
399 stmt_tree
400 current_stmt_tree (void)
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
409 static tree
410 maybe_cleanup_point_expr (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
432 /* Create a declaration statement for the declaration given by the DECL. */
434 void
435 add_decl_expr (tree decl)
437 tree r = build_stmt (input_location, DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
444 /* Finish a scope. */
446 tree
447 do_poplevel (tree stmt_list)
449 tree block = NULL;
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
454 stmt_list = pop_stmt_list (stmt_list);
456 if (!processing_template_decl)
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
462 return stmt_list;
465 /* Begin a new scope. */
467 static tree
468 do_pushlevel (scope_kind sk)
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
496 static void
497 begin_maybe_infinite_loop (tree cond)
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
506 cond = fold_non_dependent_expr_sfinae (cond, tf_none);
507 cond = maybe_constant_value (cond);
508 maybe_infinite = integer_nonzerop (cond);
510 vec_safe_push (cp_function_chain->infinite_loops,
511 maybe_infinite ? error_mark_node : NULL_TREE);
515 /* A break is a possible exit for the current loop. */
517 void
518 break_maybe_infinite_loop (void)
520 if (!cfun)
521 return;
522 cp_function_chain->infinite_loops->last() = NULL_TREE;
525 /* If we reach the end of the loop without seeing a possible exit, we have
526 an infinite loop. */
528 static void
529 end_maybe_infinite_loop (tree cond)
531 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
532 && !processing_template_decl))
533 return;
534 tree current = cp_function_chain->infinite_loops->pop();
535 if (current != NULL_TREE)
537 cond = fold_non_dependent_expr (cond);
538 cond = maybe_constant_value (cond);
539 if (integer_nonzerop (cond))
540 current_function_infinite_loop = 1;
545 /* Begin a conditional that might contain a declaration. When generating
546 normal code, we want the declaration to appear before the statement
547 containing the conditional. When generating template code, we want the
548 conditional to be rendered as the raw DECL_EXPR. */
550 static void
551 begin_cond (tree *cond_p)
553 if (processing_template_decl)
554 *cond_p = push_stmt_list ();
557 /* Finish such a conditional. */
559 static void
560 finish_cond (tree *cond_p, tree expr)
562 if (processing_template_decl)
564 tree cond = pop_stmt_list (*cond_p);
566 if (expr == NULL_TREE)
567 /* Empty condition in 'for'. */
568 gcc_assert (empty_expr_stmt_p (cond));
569 else if (check_for_bare_parameter_packs (expr))
570 expr = error_mark_node;
571 else if (!empty_expr_stmt_p (cond))
572 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
574 *cond_p = expr;
577 /* If *COND_P specifies a conditional with a declaration, transform the
578 loop such that
579 while (A x = 42) { }
580 for (; A x = 42;) { }
581 becomes
582 while (true) { A x = 42; if (!x) break; }
583 for (;;) { A x = 42; if (!x) break; }
584 The statement list for BODY will be empty if the conditional did
585 not declare anything. */
587 static void
588 simplify_loop_decl_cond (tree *cond_p, tree body)
590 tree cond, if_stmt;
592 if (!TREE_SIDE_EFFECTS (body))
593 return;
595 cond = *cond_p;
596 *cond_p = boolean_true_node;
598 if_stmt = begin_if_stmt ();
599 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
600 finish_if_stmt_cond (cond, if_stmt);
601 finish_break_stmt ();
602 finish_then_clause (if_stmt);
603 finish_if_stmt (if_stmt);
606 /* Finish a goto-statement. */
608 tree
609 finish_goto_stmt (tree destination)
611 if (identifier_p (destination))
612 destination = lookup_label (destination);
614 /* We warn about unused labels with -Wunused. That means we have to
615 mark the used labels as used. */
616 if (TREE_CODE (destination) == LABEL_DECL)
617 TREE_USED (destination) = 1;
618 else
620 destination = mark_rvalue_use (destination);
621 if (!processing_template_decl)
623 destination = cp_convert (ptr_type_node, destination,
624 tf_warning_or_error);
625 if (error_operand_p (destination))
626 return NULL_TREE;
627 destination
628 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
629 destination);
633 check_goto (destination);
635 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
638 /* COND is the condition-expression for an if, while, etc.,
639 statement. Convert it to a boolean value, if appropriate.
640 In addition, verify sequence points if -Wsequence-point is enabled. */
642 static tree
643 maybe_convert_cond (tree cond)
645 /* Empty conditions remain empty. */
646 if (!cond)
647 return NULL_TREE;
649 /* Wait until we instantiate templates before doing conversion. */
650 if (processing_template_decl)
651 return cond;
653 if (warn_sequence_point)
654 verify_sequence_points (cond);
656 /* Do the conversion. */
657 cond = convert_from_reference (cond);
659 if (TREE_CODE (cond) == MODIFY_EXPR
660 && !TREE_NO_WARNING (cond)
661 && warn_parentheses)
663 warning (OPT_Wparentheses,
664 "suggest parentheses around assignment used as truth value");
665 TREE_NO_WARNING (cond) = 1;
668 return condition_conversion (cond);
671 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
673 tree
674 finish_expr_stmt (tree expr)
676 tree r = NULL_TREE;
678 if (expr != NULL_TREE)
680 if (!processing_template_decl)
682 if (warn_sequence_point)
683 verify_sequence_points (expr);
684 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
686 else if (!type_dependent_expression_p (expr))
687 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
688 tf_warning_or_error);
690 if (check_for_bare_parameter_packs (expr))
691 expr = error_mark_node;
693 /* Simplification of inner statement expressions, compound exprs,
694 etc can result in us already having an EXPR_STMT. */
695 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
697 if (TREE_CODE (expr) != EXPR_STMT)
698 expr = build_stmt (input_location, EXPR_STMT, expr);
699 expr = maybe_cleanup_point_expr_void (expr);
702 r = add_stmt (expr);
705 return r;
709 /* Begin an if-statement. Returns a newly created IF_STMT if
710 appropriate. */
712 tree
713 begin_if_stmt (void)
715 tree r, scope;
716 scope = do_pushlevel (sk_cond);
717 r = build_stmt (input_location, IF_STMT, NULL_TREE,
718 NULL_TREE, NULL_TREE, scope);
719 begin_cond (&IF_COND (r));
720 return r;
723 /* Process the COND of an if-statement, which may be given by
724 IF_STMT. */
726 void
727 finish_if_stmt_cond (tree cond, tree if_stmt)
729 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
730 add_stmt (if_stmt);
731 THEN_CLAUSE (if_stmt) = push_stmt_list ();
734 /* Finish the then-clause of an if-statement, which may be given by
735 IF_STMT. */
737 tree
738 finish_then_clause (tree if_stmt)
740 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
741 return if_stmt;
744 /* Begin the else-clause of an if-statement. */
746 void
747 begin_else_clause (tree if_stmt)
749 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
752 /* Finish the else-clause of an if-statement, which may be given by
753 IF_STMT. */
755 void
756 finish_else_clause (tree if_stmt)
758 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
761 /* Finish an if-statement. */
763 void
764 finish_if_stmt (tree if_stmt)
766 tree scope = IF_SCOPE (if_stmt);
767 IF_SCOPE (if_stmt) = NULL;
768 add_stmt (do_poplevel (scope));
771 /* Begin a while-statement. Returns a newly created WHILE_STMT if
772 appropriate. */
774 tree
775 begin_while_stmt (void)
777 tree r;
778 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
779 add_stmt (r);
780 WHILE_BODY (r) = do_pushlevel (sk_block);
781 begin_cond (&WHILE_COND (r));
782 return r;
785 /* Process the COND of a while-statement, which may be given by
786 WHILE_STMT. */
788 void
789 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
791 cond = maybe_convert_cond (cond);
792 finish_cond (&WHILE_COND (while_stmt), cond);
793 begin_maybe_infinite_loop (cond);
794 if (ivdep && cond != error_mark_node)
795 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
796 TREE_TYPE (WHILE_COND (while_stmt)),
797 WHILE_COND (while_stmt),
798 build_int_cst (integer_type_node,
799 annot_expr_ivdep_kind));
800 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
803 /* Finish a while-statement, which may be given by WHILE_STMT. */
805 void
806 finish_while_stmt (tree while_stmt)
808 end_maybe_infinite_loop (boolean_true_node);
809 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
812 /* Begin a do-statement. Returns a newly created DO_STMT if
813 appropriate. */
815 tree
816 begin_do_stmt (void)
818 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
819 begin_maybe_infinite_loop (boolean_true_node);
820 add_stmt (r);
821 DO_BODY (r) = push_stmt_list ();
822 return r;
825 /* Finish the body of a do-statement, which may be given by DO_STMT. */
827 void
828 finish_do_body (tree do_stmt)
830 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
832 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
833 body = STATEMENT_LIST_TAIL (body)->stmt;
835 if (IS_EMPTY_STMT (body))
836 warning (OPT_Wempty_body,
837 "suggest explicit braces around empty body in %<do%> statement");
840 /* Finish a do-statement, which may be given by DO_STMT, and whose
841 COND is as indicated. */
843 void
844 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
846 cond = maybe_convert_cond (cond);
847 end_maybe_infinite_loop (cond);
848 if (ivdep && cond != error_mark_node)
849 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
850 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
851 DO_COND (do_stmt) = cond;
854 /* Finish a return-statement. The EXPRESSION returned, if any, is as
855 indicated. */
857 tree
858 finish_return_stmt (tree expr)
860 tree r;
861 bool no_warning;
863 expr = check_return_expr (expr, &no_warning);
865 if (error_operand_p (expr)
866 || (flag_openmp && !check_omp_return ()))
867 return error_mark_node;
868 if (!processing_template_decl)
870 if (warn_sequence_point)
871 verify_sequence_points (expr);
873 if (DECL_DESTRUCTOR_P (current_function_decl)
874 || (DECL_CONSTRUCTOR_P (current_function_decl)
875 && targetm.cxx.cdtor_returns_this ()))
877 /* Similarly, all destructors must run destructors for
878 base-classes before returning. So, all returns in a
879 destructor get sent to the DTOR_LABEL; finish_function emits
880 code to return a value there. */
881 return finish_goto_stmt (cdtor_label);
885 r = build_stmt (input_location, RETURN_EXPR, expr);
886 TREE_NO_WARNING (r) |= no_warning;
887 r = maybe_cleanup_point_expr_void (r);
888 r = add_stmt (r);
890 return r;
893 /* Begin the scope of a for-statement or a range-for-statement.
894 Both the returned trees are to be used in a call to
895 begin_for_stmt or begin_range_for_stmt. */
897 tree
898 begin_for_scope (tree *init)
900 tree scope = NULL_TREE;
901 if (flag_new_for_scope > 0)
902 scope = do_pushlevel (sk_for);
904 if (processing_template_decl)
905 *init = push_stmt_list ();
906 else
907 *init = NULL_TREE;
909 return scope;
912 /* Begin a for-statement. Returns a new FOR_STMT.
913 SCOPE and INIT should be the return of begin_for_scope,
914 or both NULL_TREE */
916 tree
917 begin_for_stmt (tree scope, tree init)
919 tree r;
921 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
922 NULL_TREE, NULL_TREE, NULL_TREE);
924 if (scope == NULL_TREE)
926 gcc_assert (!init || !(flag_new_for_scope > 0));
927 if (!init)
928 scope = begin_for_scope (&init);
930 FOR_INIT_STMT (r) = init;
931 FOR_SCOPE (r) = scope;
933 return r;
936 /* Finish the for-init-statement of a for-statement, which may be
937 given by FOR_STMT. */
939 void
940 finish_for_init_stmt (tree for_stmt)
942 if (processing_template_decl)
943 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
944 add_stmt (for_stmt);
945 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
946 begin_cond (&FOR_COND (for_stmt));
949 /* Finish the COND of a for-statement, which may be given by
950 FOR_STMT. */
952 void
953 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
955 cond = maybe_convert_cond (cond);
956 finish_cond (&FOR_COND (for_stmt), cond);
957 begin_maybe_infinite_loop (cond);
958 if (ivdep && cond != error_mark_node)
959 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
960 TREE_TYPE (FOR_COND (for_stmt)),
961 FOR_COND (for_stmt),
962 build_int_cst (integer_type_node,
963 annot_expr_ivdep_kind));
964 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
967 /* Finish the increment-EXPRESSION in a for-statement, which may be
968 given by FOR_STMT. */
970 void
971 finish_for_expr (tree expr, tree for_stmt)
973 if (!expr)
974 return;
975 /* If EXPR is an overloaded function, issue an error; there is no
976 context available to use to perform overload resolution. */
977 if (type_unknown_p (expr))
979 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
980 expr = error_mark_node;
982 if (!processing_template_decl)
984 if (warn_sequence_point)
985 verify_sequence_points (expr);
986 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
987 tf_warning_or_error);
989 else if (!type_dependent_expression_p (expr))
990 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
991 tf_warning_or_error);
992 expr = maybe_cleanup_point_expr_void (expr);
993 if (check_for_bare_parameter_packs (expr))
994 expr = error_mark_node;
995 FOR_EXPR (for_stmt) = expr;
998 /* Finish the body of a for-statement, which may be given by
999 FOR_STMT. The increment-EXPR for the loop must be
1000 provided.
1001 It can also finish RANGE_FOR_STMT. */
1003 void
1004 finish_for_stmt (tree for_stmt)
1006 end_maybe_infinite_loop (boolean_true_node);
1008 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1009 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1010 else
1011 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1013 /* Pop the scope for the body of the loop. */
1014 if (flag_new_for_scope > 0)
1016 tree scope;
1017 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1018 ? &RANGE_FOR_SCOPE (for_stmt)
1019 : &FOR_SCOPE (for_stmt));
1020 scope = *scope_ptr;
1021 *scope_ptr = NULL;
1022 add_stmt (do_poplevel (scope));
1026 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1027 SCOPE and INIT should be the return of begin_for_scope,
1028 or both NULL_TREE .
1029 To finish it call finish_for_stmt(). */
1031 tree
1032 begin_range_for_stmt (tree scope, tree init)
1034 tree r;
1036 begin_maybe_infinite_loop (boolean_false_node);
1038 r = build_stmt (input_location, RANGE_FOR_STMT,
1039 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1041 if (scope == NULL_TREE)
1043 gcc_assert (!init || !(flag_new_for_scope > 0));
1044 if (!init)
1045 scope = begin_for_scope (&init);
1048 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1049 pop it now. */
1050 if (init)
1051 pop_stmt_list (init);
1052 RANGE_FOR_SCOPE (r) = scope;
1054 return r;
1057 /* Finish the head of a range-based for statement, which may
1058 be given by RANGE_FOR_STMT. DECL must be the declaration
1059 and EXPR must be the loop expression. */
1061 void
1062 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1064 RANGE_FOR_DECL (range_for_stmt) = decl;
1065 RANGE_FOR_EXPR (range_for_stmt) = expr;
1066 add_stmt (range_for_stmt);
1067 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1070 /* Finish a break-statement. */
1072 tree
1073 finish_break_stmt (void)
1075 /* In switch statements break is sometimes stylistically used after
1076 a return statement. This can lead to spurious warnings about
1077 control reaching the end of a non-void function when it is
1078 inlined. Note that we are calling block_may_fallthru with
1079 language specific tree nodes; this works because
1080 block_may_fallthru returns true when given something it does not
1081 understand. */
1082 if (!block_may_fallthru (cur_stmt_list))
1083 return void_zero_node;
1084 return add_stmt (build_stmt (input_location, BREAK_STMT));
1087 /* Finish a continue-statement. */
1089 tree
1090 finish_continue_stmt (void)
1092 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1095 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1096 appropriate. */
1098 tree
1099 begin_switch_stmt (void)
1101 tree r, scope;
1103 scope = do_pushlevel (sk_cond);
1104 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1106 begin_cond (&SWITCH_STMT_COND (r));
1108 return r;
1111 /* Finish the cond of a switch-statement. */
1113 void
1114 finish_switch_cond (tree cond, tree switch_stmt)
1116 tree orig_type = NULL;
1117 if (!processing_template_decl)
1119 /* Convert the condition to an integer or enumeration type. */
1120 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1121 if (cond == NULL_TREE)
1123 error ("switch quantity not an integer");
1124 cond = error_mark_node;
1126 orig_type = TREE_TYPE (cond);
1127 if (cond != error_mark_node)
1129 /* [stmt.switch]
1131 Integral promotions are performed. */
1132 cond = perform_integral_promotions (cond);
1133 cond = maybe_cleanup_point_expr (cond);
1136 if (check_for_bare_parameter_packs (cond))
1137 cond = error_mark_node;
1138 else if (!processing_template_decl && warn_sequence_point)
1139 verify_sequence_points (cond);
1141 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1142 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1143 add_stmt (switch_stmt);
1144 push_switch (switch_stmt);
1145 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1148 /* Finish the body of a switch-statement, which may be given by
1149 SWITCH_STMT. The COND to switch on is indicated. */
1151 void
1152 finish_switch_stmt (tree switch_stmt)
1154 tree scope;
1156 SWITCH_STMT_BODY (switch_stmt) =
1157 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1158 pop_switch ();
1160 scope = SWITCH_STMT_SCOPE (switch_stmt);
1161 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1162 add_stmt (do_poplevel (scope));
1165 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1166 appropriate. */
1168 tree
1169 begin_try_block (void)
1171 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1172 add_stmt (r);
1173 TRY_STMTS (r) = push_stmt_list ();
1174 return r;
1177 /* Likewise, for a function-try-block. The block returned in
1178 *COMPOUND_STMT is an artificial outer scope, containing the
1179 function-try-block. */
1181 tree
1182 begin_function_try_block (tree *compound_stmt)
1184 tree r;
1185 /* This outer scope does not exist in the C++ standard, but we need
1186 a place to put __FUNCTION__ and similar variables. */
1187 *compound_stmt = begin_compound_stmt (0);
1188 r = begin_try_block ();
1189 FN_TRY_BLOCK_P (r) = 1;
1190 return r;
1193 /* Finish a try-block, which may be given by TRY_BLOCK. */
1195 void
1196 finish_try_block (tree try_block)
1198 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1199 TRY_HANDLERS (try_block) = push_stmt_list ();
1202 /* Finish the body of a cleanup try-block, which may be given by
1203 TRY_BLOCK. */
1205 void
1206 finish_cleanup_try_block (tree try_block)
1208 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1211 /* Finish an implicitly generated try-block, with a cleanup is given
1212 by CLEANUP. */
1214 void
1215 finish_cleanup (tree cleanup, tree try_block)
1217 TRY_HANDLERS (try_block) = cleanup;
1218 CLEANUP_P (try_block) = 1;
1221 /* Likewise, for a function-try-block. */
1223 void
1224 finish_function_try_block (tree try_block)
1226 finish_try_block (try_block);
1227 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1228 the try block, but moving it inside. */
1229 in_function_try_handler = 1;
1232 /* Finish a handler-sequence for a try-block, which may be given by
1233 TRY_BLOCK. */
1235 void
1236 finish_handler_sequence (tree try_block)
1238 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1239 check_handlers (TRY_HANDLERS (try_block));
1242 /* Finish the handler-seq for a function-try-block, given by
1243 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1244 begin_function_try_block. */
1246 void
1247 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1249 in_function_try_handler = 0;
1250 finish_handler_sequence (try_block);
1251 finish_compound_stmt (compound_stmt);
1254 /* Begin a handler. Returns a HANDLER if appropriate. */
1256 tree
1257 begin_handler (void)
1259 tree r;
1261 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1262 add_stmt (r);
1264 /* Create a binding level for the eh_info and the exception object
1265 cleanup. */
1266 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1268 return r;
1271 /* Finish the handler-parameters for a handler, which may be given by
1272 HANDLER. DECL is the declaration for the catch parameter, or NULL
1273 if this is a `catch (...)' clause. */
1275 void
1276 finish_handler_parms (tree decl, tree handler)
1278 tree type = NULL_TREE;
1279 if (processing_template_decl)
1281 if (decl)
1283 decl = pushdecl (decl);
1284 decl = push_template_decl (decl);
1285 HANDLER_PARMS (handler) = decl;
1286 type = TREE_TYPE (decl);
1289 else
1290 type = expand_start_catch_block (decl);
1291 HANDLER_TYPE (handler) = type;
1294 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1295 the return value from the matching call to finish_handler_parms. */
1297 void
1298 finish_handler (tree handler)
1300 if (!processing_template_decl)
1301 expand_end_catch_block ();
1302 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1305 /* Begin a compound statement. FLAGS contains some bits that control the
1306 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1307 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1308 block of a function. If BCS_TRY_BLOCK is set, this is the block
1309 created on behalf of a TRY statement. Returns a token to be passed to
1310 finish_compound_stmt. */
1312 tree
1313 begin_compound_stmt (unsigned int flags)
1315 tree r;
1317 if (flags & BCS_NO_SCOPE)
1319 r = push_stmt_list ();
1320 STATEMENT_LIST_NO_SCOPE (r) = 1;
1322 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1323 But, if it's a statement-expression with a scopeless block, there's
1324 nothing to keep, and we don't want to accidentally keep a block
1325 *inside* the scopeless block. */
1326 keep_next_level (false);
1328 else
1329 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1331 /* When processing a template, we need to remember where the braces were,
1332 so that we can set up identical scopes when instantiating the template
1333 later. BIND_EXPR is a handy candidate for this.
1334 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1335 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1336 processing templates. */
1337 if (processing_template_decl)
1339 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1340 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1341 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1342 TREE_SIDE_EFFECTS (r) = 1;
1345 return r;
1348 /* Finish a compound-statement, which is given by STMT. */
1350 void
1351 finish_compound_stmt (tree stmt)
1353 if (TREE_CODE (stmt) == BIND_EXPR)
1355 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1356 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1357 discard the BIND_EXPR so it can be merged with the containing
1358 STATEMENT_LIST. */
1359 if (TREE_CODE (body) == STATEMENT_LIST
1360 && STATEMENT_LIST_HEAD (body) == NULL
1361 && !BIND_EXPR_BODY_BLOCK (stmt)
1362 && !BIND_EXPR_TRY_BLOCK (stmt))
1363 stmt = body;
1364 else
1365 BIND_EXPR_BODY (stmt) = body;
1367 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1368 stmt = pop_stmt_list (stmt);
1369 else
1371 /* Destroy any ObjC "super" receivers that may have been
1372 created. */
1373 objc_clear_super_receiver ();
1375 stmt = do_poplevel (stmt);
1378 /* ??? See c_end_compound_stmt wrt statement expressions. */
1379 add_stmt (stmt);
1382 /* Finish an asm-statement, whose components are a STRING, some
1383 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1384 LABELS. Also note whether the asm-statement should be
1385 considered volatile. */
1387 tree
1388 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1389 tree input_operands, tree clobbers, tree labels)
1391 tree r;
1392 tree t;
1393 int ninputs = list_length (input_operands);
1394 int noutputs = list_length (output_operands);
1396 if (!processing_template_decl)
1398 const char *constraint;
1399 const char **oconstraints;
1400 bool allows_mem, allows_reg, is_inout;
1401 tree operand;
1402 int i;
1404 oconstraints = XALLOCAVEC (const char *, noutputs);
1406 string = resolve_asm_operand_names (string, output_operands,
1407 input_operands, labels);
1409 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1411 operand = TREE_VALUE (t);
1413 /* ??? Really, this should not be here. Users should be using a
1414 proper lvalue, dammit. But there's a long history of using
1415 casts in the output operands. In cases like longlong.h, this
1416 becomes a primitive form of typechecking -- if the cast can be
1417 removed, then the output operand had a type of the proper width;
1418 otherwise we'll get an error. Gross, but ... */
1419 STRIP_NOPS (operand);
1421 operand = mark_lvalue_use (operand);
1423 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1424 operand = error_mark_node;
1426 if (operand != error_mark_node
1427 && (TREE_READONLY (operand)
1428 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1429 /* Functions are not modifiable, even though they are
1430 lvalues. */
1431 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1432 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1433 /* If it's an aggregate and any field is const, then it is
1434 effectively const. */
1435 || (CLASS_TYPE_P (TREE_TYPE (operand))
1436 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1437 cxx_readonly_error (operand, lv_asm);
1439 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1440 oconstraints[i] = constraint;
1442 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1443 &allows_mem, &allows_reg, &is_inout))
1445 /* If the operand is going to end up in memory,
1446 mark it addressable. */
1447 if (!allows_reg && !cxx_mark_addressable (operand))
1448 operand = error_mark_node;
1450 else
1451 operand = error_mark_node;
1453 TREE_VALUE (t) = operand;
1456 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1458 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1459 bool constraint_parsed
1460 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1461 oconstraints, &allows_mem, &allows_reg);
1462 /* If the operand is going to end up in memory, don't call
1463 decay_conversion. */
1464 if (constraint_parsed && !allows_reg && allows_mem)
1465 operand = mark_lvalue_use (TREE_VALUE (t));
1466 else
1467 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1469 /* If the type of the operand hasn't been determined (e.g.,
1470 because it involves an overloaded function), then issue
1471 an error message. There's no context available to
1472 resolve the overloading. */
1473 if (TREE_TYPE (operand) == unknown_type_node)
1475 error ("type of asm operand %qE could not be determined",
1476 TREE_VALUE (t));
1477 operand = error_mark_node;
1480 if (constraint_parsed)
1482 /* If the operand is going to end up in memory,
1483 mark it addressable. */
1484 if (!allows_reg && allows_mem)
1486 /* Strip the nops as we allow this case. FIXME, this really
1487 should be rejected or made deprecated. */
1488 STRIP_NOPS (operand);
1489 if (!cxx_mark_addressable (operand))
1490 operand = error_mark_node;
1492 else if (!allows_reg && !allows_mem)
1494 /* If constraint allows neither register nor memory,
1495 try harder to get a constant. */
1496 tree constop = maybe_constant_value (operand);
1497 if (TREE_CONSTANT (constop))
1498 operand = constop;
1501 else
1502 operand = error_mark_node;
1504 TREE_VALUE (t) = operand;
1508 r = build_stmt (input_location, ASM_EXPR, string,
1509 output_operands, input_operands,
1510 clobbers, labels);
1511 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1512 r = maybe_cleanup_point_expr_void (r);
1513 return add_stmt (r);
1516 /* Finish a label with the indicated NAME. Returns the new label. */
1518 tree
1519 finish_label_stmt (tree name)
1521 tree decl = define_label (input_location, name);
1523 if (decl == error_mark_node)
1524 return error_mark_node;
1526 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1528 return decl;
1531 /* Finish a series of declarations for local labels. G++ allows users
1532 to declare "local" labels, i.e., labels with scope. This extension
1533 is useful when writing code involving statement-expressions. */
1535 void
1536 finish_label_decl (tree name)
1538 if (!at_function_scope_p ())
1540 error ("__label__ declarations are only allowed in function scopes");
1541 return;
1544 add_decl_expr (declare_local_label (name));
1547 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1549 void
1550 finish_decl_cleanup (tree decl, tree cleanup)
1552 push_cleanup (decl, cleanup, false);
1555 /* If the current scope exits with an exception, run CLEANUP. */
1557 void
1558 finish_eh_cleanup (tree cleanup)
1560 push_cleanup (NULL, cleanup, true);
1563 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1564 order they were written by the user. Each node is as for
1565 emit_mem_initializers. */
1567 void
1568 finish_mem_initializers (tree mem_inits)
1570 /* Reorder the MEM_INITS so that they are in the order they appeared
1571 in the source program. */
1572 mem_inits = nreverse (mem_inits);
1574 if (processing_template_decl)
1576 tree mem;
1578 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1580 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1581 check for bare parameter packs in the TREE_VALUE, because
1582 any parameter packs in the TREE_VALUE have already been
1583 bound as part of the TREE_PURPOSE. See
1584 make_pack_expansion for more information. */
1585 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1586 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1587 TREE_VALUE (mem) = error_mark_node;
1590 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1591 CTOR_INITIALIZER, mem_inits));
1593 else
1594 emit_mem_initializers (mem_inits);
1597 /* Obfuscate EXPR if it looks like an id-expression or member access so
1598 that the call to finish_decltype in do_auto_deduction will give the
1599 right result. */
1601 tree
1602 force_paren_expr (tree expr)
1604 /* This is only needed for decltype(auto) in C++14. */
1605 if (cxx_dialect < cxx1y)
1606 return expr;
1608 /* If we're in unevaluated context, we can't be deducing a
1609 return/initializer type, so we don't need to mess with this. */
1610 if (cp_unevaluated_operand)
1611 return expr;
1613 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1614 && TREE_CODE (expr) != SCOPE_REF)
1615 return expr;
1617 if (TREE_CODE (expr) == COMPONENT_REF)
1618 REF_PARENTHESIZED_P (expr) = true;
1619 else if (type_dependent_expression_p (expr))
1620 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1621 else
1623 cp_lvalue_kind kind = lvalue_kind (expr);
1624 if ((kind & ~clk_class) != clk_none)
1626 tree type = unlowered_expr_type (expr);
1627 bool rval = !!(kind & clk_rvalueref);
1628 type = cp_build_reference_type (type, rval);
1629 expr = build_static_cast (type, expr, tf_error);
1633 return expr;
1636 /* Finish a parenthesized expression EXPR. */
1638 tree
1639 finish_parenthesized_expr (tree expr)
1641 if (EXPR_P (expr))
1642 /* This inhibits warnings in c_common_truthvalue_conversion. */
1643 TREE_NO_WARNING (expr) = 1;
1645 if (TREE_CODE (expr) == OFFSET_REF
1646 || TREE_CODE (expr) == SCOPE_REF)
1647 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1648 enclosed in parentheses. */
1649 PTRMEM_OK_P (expr) = 0;
1651 if (TREE_CODE (expr) == STRING_CST)
1652 PAREN_STRING_LITERAL_P (expr) = 1;
1654 expr = force_paren_expr (expr);
1656 return expr;
1659 /* Finish a reference to a non-static data member (DECL) that is not
1660 preceded by `.' or `->'. */
1662 tree
1663 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1665 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1667 if (!object)
1669 tree scope = qualifying_scope;
1670 if (scope == NULL_TREE)
1671 scope = context_for_name_lookup (decl);
1672 object = maybe_dummy_object (scope, NULL);
1675 object = maybe_resolve_dummy (object);
1676 if (object == error_mark_node)
1677 return error_mark_node;
1679 /* DR 613: Can use non-static data members without an associated
1680 object in sizeof/decltype/alignof. */
1681 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1682 && (!processing_template_decl || !current_class_ref))
1684 if (current_function_decl
1685 && DECL_STATIC_FUNCTION_P (current_function_decl))
1686 error ("invalid use of member %q+D in static member function", decl);
1687 else
1688 error ("invalid use of non-static data member %q+D", decl);
1689 error ("from this location");
1691 return error_mark_node;
1694 if (current_class_ptr)
1695 TREE_USED (current_class_ptr) = 1;
1696 if (processing_template_decl && !qualifying_scope)
1698 tree type = TREE_TYPE (decl);
1700 if (TREE_CODE (type) == REFERENCE_TYPE)
1701 /* Quals on the object don't matter. */;
1702 else if (PACK_EXPANSION_P (type))
1703 /* Don't bother trying to represent this. */
1704 type = NULL_TREE;
1705 else
1707 /* Set the cv qualifiers. */
1708 int quals = cp_type_quals (TREE_TYPE (object));
1710 if (DECL_MUTABLE_P (decl))
1711 quals &= ~TYPE_QUAL_CONST;
1713 quals |= cp_type_quals (TREE_TYPE (decl));
1714 type = cp_build_qualified_type (type, quals);
1717 return (convert_from_reference
1718 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1720 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1721 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1722 for now. */
1723 else if (processing_template_decl)
1724 return build_qualified_name (TREE_TYPE (decl),
1725 qualifying_scope,
1726 decl,
1727 /*template_p=*/false);
1728 else
1730 tree access_type = TREE_TYPE (object);
1732 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1733 decl, tf_warning_or_error);
1735 /* If the data member was named `C::M', convert `*this' to `C'
1736 first. */
1737 if (qualifying_scope)
1739 tree binfo = NULL_TREE;
1740 object = build_scoped_ref (object, qualifying_scope,
1741 &binfo);
1744 return build_class_member_access_expr (object, decl,
1745 /*access_path=*/NULL_TREE,
1746 /*preserve_reference=*/false,
1747 tf_warning_or_error);
1751 /* If we are currently parsing a template and we encountered a typedef
1752 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1753 adds the typedef to a list tied to the current template.
1754 At template instantiation time, that list is walked and access check
1755 performed for each typedef.
1756 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1758 void
1759 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1760 tree context,
1761 location_t location)
1763 tree template_info = NULL;
1764 tree cs = current_scope ();
1766 if (!is_typedef_decl (typedef_decl)
1767 || !context
1768 || !CLASS_TYPE_P (context)
1769 || !cs)
1770 return;
1772 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1773 template_info = get_template_info (cs);
1775 if (template_info
1776 && TI_TEMPLATE (template_info)
1777 && !currently_open_class (context))
1778 append_type_to_template_for_access_check (cs, typedef_decl,
1779 context, location);
1782 /* DECL was the declaration to which a qualified-id resolved. Issue
1783 an error message if it is not accessible. If OBJECT_TYPE is
1784 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1785 type of `*x', or `x', respectively. If the DECL was named as
1786 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1788 void
1789 check_accessibility_of_qualified_id (tree decl,
1790 tree object_type,
1791 tree nested_name_specifier)
1793 tree scope;
1794 tree qualifying_type = NULL_TREE;
1796 /* If we are parsing a template declaration and if decl is a typedef,
1797 add it to a list tied to the template.
1798 At template instantiation time, that list will be walked and
1799 access check performed. */
1800 add_typedef_to_current_template_for_access_check (decl,
1801 nested_name_specifier
1802 ? nested_name_specifier
1803 : DECL_CONTEXT (decl),
1804 input_location);
1806 /* If we're not checking, return immediately. */
1807 if (deferred_access_no_check)
1808 return;
1810 /* Determine the SCOPE of DECL. */
1811 scope = context_for_name_lookup (decl);
1812 /* If the SCOPE is not a type, then DECL is not a member. */
1813 if (!TYPE_P (scope))
1814 return;
1815 /* Compute the scope through which DECL is being accessed. */
1816 if (object_type
1817 /* OBJECT_TYPE might not be a class type; consider:
1819 class A { typedef int I; };
1820 I *p;
1821 p->A::I::~I();
1823 In this case, we will have "A::I" as the DECL, but "I" as the
1824 OBJECT_TYPE. */
1825 && CLASS_TYPE_P (object_type)
1826 && DERIVED_FROM_P (scope, object_type))
1827 /* If we are processing a `->' or `.' expression, use the type of the
1828 left-hand side. */
1829 qualifying_type = object_type;
1830 else if (nested_name_specifier)
1832 /* If the reference is to a non-static member of the
1833 current class, treat it as if it were referenced through
1834 `this'. */
1835 if (DECL_NONSTATIC_MEMBER_P (decl)
1836 && current_class_ptr
1837 && DERIVED_FROM_P (scope, current_class_type))
1838 qualifying_type = current_class_type;
1839 /* Otherwise, use the type indicated by the
1840 nested-name-specifier. */
1841 else
1842 qualifying_type = nested_name_specifier;
1844 else
1845 /* Otherwise, the name must be from the current class or one of
1846 its bases. */
1847 qualifying_type = currently_open_derived_class (scope);
1849 if (qualifying_type
1850 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1851 or similar in a default argument value. */
1852 && CLASS_TYPE_P (qualifying_type)
1853 && !dependent_type_p (qualifying_type))
1854 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1855 decl, tf_warning_or_error);
1858 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1859 class named to the left of the "::" operator. DONE is true if this
1860 expression is a complete postfix-expression; it is false if this
1861 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1862 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1863 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1864 is true iff this qualified name appears as a template argument. */
1866 tree
1867 finish_qualified_id_expr (tree qualifying_class,
1868 tree expr,
1869 bool done,
1870 bool address_p,
1871 bool template_p,
1872 bool template_arg_p,
1873 tsubst_flags_t complain)
1875 gcc_assert (TYPE_P (qualifying_class));
1877 if (error_operand_p (expr))
1878 return error_mark_node;
1880 if ((DECL_P (expr) || BASELINK_P (expr))
1881 && !mark_used (expr, complain))
1882 return error_mark_node;
1884 if (template_p)
1885 check_template_keyword (expr);
1887 /* If EXPR occurs as the operand of '&', use special handling that
1888 permits a pointer-to-member. */
1889 if (address_p && done)
1891 if (TREE_CODE (expr) == SCOPE_REF)
1892 expr = TREE_OPERAND (expr, 1);
1893 expr = build_offset_ref (qualifying_class, expr,
1894 /*address_p=*/true, complain);
1895 return expr;
1898 /* No need to check access within an enum. */
1899 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1900 return expr;
1902 /* Within the scope of a class, turn references to non-static
1903 members into expression of the form "this->...". */
1904 if (template_arg_p)
1905 /* But, within a template argument, we do not want make the
1906 transformation, as there is no "this" pointer. */
1908 else if (TREE_CODE (expr) == FIELD_DECL)
1910 push_deferring_access_checks (dk_no_check);
1911 expr = finish_non_static_data_member (expr, NULL_TREE,
1912 qualifying_class);
1913 pop_deferring_access_checks ();
1915 else if (BASELINK_P (expr) && !processing_template_decl)
1917 /* See if any of the functions are non-static members. */
1918 /* If so, the expression may be relative to 'this'. */
1919 if (!shared_member_p (expr)
1920 && current_class_ptr
1921 && DERIVED_FROM_P (qualifying_class,
1922 current_nonlambda_class_type ()))
1923 expr = (build_class_member_access_expr
1924 (maybe_dummy_object (qualifying_class, NULL),
1925 expr,
1926 BASELINK_ACCESS_BINFO (expr),
1927 /*preserve_reference=*/false,
1928 complain));
1929 else if (done)
1930 /* The expression is a qualified name whose address is not
1931 being taken. */
1932 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1933 complain);
1935 else if (BASELINK_P (expr))
1937 else
1939 /* In a template, return a SCOPE_REF for most qualified-ids
1940 so that we can check access at instantiation time. But if
1941 we're looking at a member of the current instantiation, we
1942 know we have access and building up the SCOPE_REF confuses
1943 non-type template argument handling. */
1944 if (processing_template_decl
1945 && !currently_open_class (qualifying_class))
1946 expr = build_qualified_name (TREE_TYPE (expr),
1947 qualifying_class, expr,
1948 template_p);
1950 expr = convert_from_reference (expr);
1953 return expr;
1956 /* Begin a statement-expression. The value returned must be passed to
1957 finish_stmt_expr. */
1959 tree
1960 begin_stmt_expr (void)
1962 return push_stmt_list ();
1965 /* Process the final expression of a statement expression. EXPR can be
1966 NULL, if the final expression is empty. Return a STATEMENT_LIST
1967 containing all the statements in the statement-expression, or
1968 ERROR_MARK_NODE if there was an error. */
1970 tree
1971 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1973 if (error_operand_p (expr))
1975 /* The type of the statement-expression is the type of the last
1976 expression. */
1977 TREE_TYPE (stmt_expr) = error_mark_node;
1978 return error_mark_node;
1981 /* If the last statement does not have "void" type, then the value
1982 of the last statement is the value of the entire expression. */
1983 if (expr)
1985 tree type = TREE_TYPE (expr);
1987 if (processing_template_decl)
1989 expr = build_stmt (input_location, EXPR_STMT, expr);
1990 expr = add_stmt (expr);
1991 /* Mark the last statement so that we can recognize it as such at
1992 template-instantiation time. */
1993 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1995 else if (VOID_TYPE_P (type))
1997 /* Just treat this like an ordinary statement. */
1998 expr = finish_expr_stmt (expr);
2000 else
2002 /* It actually has a value we need to deal with. First, force it
2003 to be an rvalue so that we won't need to build up a copy
2004 constructor call later when we try to assign it to something. */
2005 expr = force_rvalue (expr, tf_warning_or_error);
2006 if (error_operand_p (expr))
2007 return error_mark_node;
2009 /* Update for array-to-pointer decay. */
2010 type = TREE_TYPE (expr);
2012 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2013 normal statement, but don't convert to void or actually add
2014 the EXPR_STMT. */
2015 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2016 expr = maybe_cleanup_point_expr (expr);
2017 add_stmt (expr);
2020 /* The type of the statement-expression is the type of the last
2021 expression. */
2022 TREE_TYPE (stmt_expr) = type;
2025 return stmt_expr;
2028 /* Finish a statement-expression. EXPR should be the value returned
2029 by the previous begin_stmt_expr. Returns an expression
2030 representing the statement-expression. */
2032 tree
2033 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2035 tree type;
2036 tree result;
2038 if (error_operand_p (stmt_expr))
2040 pop_stmt_list (stmt_expr);
2041 return error_mark_node;
2044 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2046 type = TREE_TYPE (stmt_expr);
2047 result = pop_stmt_list (stmt_expr);
2048 TREE_TYPE (result) = type;
2050 if (processing_template_decl)
2052 result = build_min (STMT_EXPR, type, result);
2053 TREE_SIDE_EFFECTS (result) = 1;
2054 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2056 else if (CLASS_TYPE_P (type))
2058 /* Wrap the statement-expression in a TARGET_EXPR so that the
2059 temporary object created by the final expression is destroyed at
2060 the end of the full-expression containing the
2061 statement-expression. */
2062 result = force_target_expr (type, result, tf_warning_or_error);
2065 return result;
2068 /* Returns the expression which provides the value of STMT_EXPR. */
2070 tree
2071 stmt_expr_value_expr (tree stmt_expr)
2073 tree t = STMT_EXPR_STMT (stmt_expr);
2075 if (TREE_CODE (t) == BIND_EXPR)
2076 t = BIND_EXPR_BODY (t);
2078 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2079 t = STATEMENT_LIST_TAIL (t)->stmt;
2081 if (TREE_CODE (t) == EXPR_STMT)
2082 t = EXPR_STMT_EXPR (t);
2084 return t;
2087 /* Return TRUE iff EXPR_STMT is an empty list of
2088 expression statements. */
2090 bool
2091 empty_expr_stmt_p (tree expr_stmt)
2093 tree body = NULL_TREE;
2095 if (expr_stmt == void_zero_node)
2096 return true;
2098 if (expr_stmt)
2100 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2101 body = EXPR_STMT_EXPR (expr_stmt);
2102 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2103 body = expr_stmt;
2106 if (body)
2108 if (TREE_CODE (body) == STATEMENT_LIST)
2109 return tsi_end_p (tsi_start (body));
2110 else
2111 return empty_expr_stmt_p (body);
2113 return false;
2116 /* Perform Koenig lookup. FN is the postfix-expression representing
2117 the function (or functions) to call; ARGS are the arguments to the
2118 call. Returns the functions to be considered by overload resolution. */
2120 tree
2121 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2122 tsubst_flags_t complain)
2124 tree identifier = NULL_TREE;
2125 tree functions = NULL_TREE;
2126 tree tmpl_args = NULL_TREE;
2127 bool template_id = false;
2129 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2131 /* Use a separate flag to handle null args. */
2132 template_id = true;
2133 tmpl_args = TREE_OPERAND (fn, 1);
2134 fn = TREE_OPERAND (fn, 0);
2137 /* Find the name of the overloaded function. */
2138 if (identifier_p (fn))
2139 identifier = fn;
2140 else if (is_overloaded_fn (fn))
2142 functions = fn;
2143 identifier = DECL_NAME (get_first_fn (functions));
2145 else if (DECL_P (fn))
2147 functions = fn;
2148 identifier = DECL_NAME (fn);
2151 /* A call to a namespace-scope function using an unqualified name.
2153 Do Koenig lookup -- unless any of the arguments are
2154 type-dependent. */
2155 if (!any_type_dependent_arguments_p (args)
2156 && !any_dependent_template_arguments_p (tmpl_args))
2158 fn = lookup_arg_dependent (identifier, functions, args);
2159 if (!fn)
2161 /* The unqualified name could not be resolved. */
2162 if (complain)
2163 fn = unqualified_fn_lookup_error (identifier);
2164 else
2165 fn = identifier;
2169 if (fn && template_id)
2170 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2172 return fn;
2175 /* Generate an expression for `FN (ARGS)'. This may change the
2176 contents of ARGS.
2178 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2179 as a virtual call, even if FN is virtual. (This flag is set when
2180 encountering an expression where the function name is explicitly
2181 qualified. For example a call to `X::f' never generates a virtual
2182 call.)
2184 Returns code for the call. */
2186 tree
2187 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2188 bool koenig_p, tsubst_flags_t complain)
2190 tree result;
2191 tree orig_fn;
2192 vec<tree, va_gc> *orig_args = NULL;
2194 if (fn == error_mark_node)
2195 return error_mark_node;
2197 gcc_assert (!TYPE_P (fn));
2199 orig_fn = fn;
2201 if (processing_template_decl)
2203 /* If the call expression is dependent, build a CALL_EXPR node
2204 with no type; type_dependent_expression_p recognizes
2205 expressions with no type as being dependent. */
2206 if (type_dependent_expression_p (fn)
2207 || any_type_dependent_arguments_p (*args)
2208 /* For a non-static member function that doesn't have an
2209 explicit object argument, we need to specifically
2210 test the type dependency of the "this" pointer because it
2211 is not included in *ARGS even though it is considered to
2212 be part of the list of arguments. Note that this is
2213 related to CWG issues 515 and 1005. */
2214 || (TREE_CODE (fn) != COMPONENT_REF
2215 && non_static_member_function_p (fn)
2216 && current_class_ref
2217 && type_dependent_expression_p (current_class_ref)))
2219 result = build_nt_call_vec (fn, *args);
2220 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2221 KOENIG_LOOKUP_P (result) = koenig_p;
2222 if (cfun)
2226 tree fndecl = OVL_CURRENT (fn);
2227 if (TREE_CODE (fndecl) != FUNCTION_DECL
2228 || !TREE_THIS_VOLATILE (fndecl))
2229 break;
2230 fn = OVL_NEXT (fn);
2232 while (fn);
2233 if (!fn)
2234 current_function_returns_abnormally = 1;
2236 return result;
2238 orig_args = make_tree_vector_copy (*args);
2239 if (!BASELINK_P (fn)
2240 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2241 && TREE_TYPE (fn) != unknown_type_node)
2242 fn = build_non_dependent_expr (fn);
2243 make_args_non_dependent (*args);
2246 if (TREE_CODE (fn) == COMPONENT_REF)
2248 tree member = TREE_OPERAND (fn, 1);
2249 if (BASELINK_P (member))
2251 tree object = TREE_OPERAND (fn, 0);
2252 return build_new_method_call (object, member,
2253 args, NULL_TREE,
2254 (disallow_virtual
2255 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2256 : LOOKUP_NORMAL),
2257 /*fn_p=*/NULL,
2258 complain);
2262 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2263 if (TREE_CODE (fn) == ADDR_EXPR
2264 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2265 fn = TREE_OPERAND (fn, 0);
2267 if (is_overloaded_fn (fn))
2268 fn = baselink_for_fns (fn);
2270 result = NULL_TREE;
2271 if (BASELINK_P (fn))
2273 tree object;
2275 /* A call to a member function. From [over.call.func]:
2277 If the keyword this is in scope and refers to the class of
2278 that member function, or a derived class thereof, then the
2279 function call is transformed into a qualified function call
2280 using (*this) as the postfix-expression to the left of the
2281 . operator.... [Otherwise] a contrived object of type T
2282 becomes the implied object argument.
2284 In this situation:
2286 struct A { void f(); };
2287 struct B : public A {};
2288 struct C : public A { void g() { B::f(); }};
2290 "the class of that member function" refers to `A'. But 11.2
2291 [class.access.base] says that we need to convert 'this' to B* as
2292 part of the access, so we pass 'B' to maybe_dummy_object. */
2294 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2295 NULL);
2297 if (processing_template_decl)
2299 if (type_dependent_expression_p (object))
2301 tree ret = build_nt_call_vec (orig_fn, orig_args);
2302 release_tree_vector (orig_args);
2303 return ret;
2305 object = build_non_dependent_expr (object);
2308 result = build_new_method_call (object, fn, args, NULL_TREE,
2309 (disallow_virtual
2310 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2311 : LOOKUP_NORMAL),
2312 /*fn_p=*/NULL,
2313 complain);
2315 else if (is_overloaded_fn (fn))
2317 /* If the function is an overloaded builtin, resolve it. */
2318 if (TREE_CODE (fn) == FUNCTION_DECL
2319 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2320 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2321 result = resolve_overloaded_builtin (input_location, fn, *args);
2323 if (!result)
2325 if (warn_sizeof_pointer_memaccess
2326 && !vec_safe_is_empty (*args)
2327 && !processing_template_decl)
2329 location_t sizeof_arg_loc[3];
2330 tree sizeof_arg[3];
2331 unsigned int i;
2332 for (i = 0; i < 3; i++)
2334 tree t;
2336 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2337 sizeof_arg[i] = NULL_TREE;
2338 if (i >= (*args)->length ())
2339 continue;
2340 t = (**args)[i];
2341 if (TREE_CODE (t) != SIZEOF_EXPR)
2342 continue;
2343 if (SIZEOF_EXPR_TYPE_P (t))
2344 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2345 else
2346 sizeof_arg[i] = TREE_OPERAND (t, 0);
2347 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2349 sizeof_pointer_memaccess_warning
2350 (sizeof_arg_loc, fn, *args,
2351 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2354 /* A call to a namespace-scope function. */
2355 result = build_new_function_call (fn, args, koenig_p, complain);
2358 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2360 if (!vec_safe_is_empty (*args))
2361 error ("arguments to destructor are not allowed");
2362 /* Mark the pseudo-destructor call as having side-effects so
2363 that we do not issue warnings about its use. */
2364 result = build1 (NOP_EXPR,
2365 void_type_node,
2366 TREE_OPERAND (fn, 0));
2367 TREE_SIDE_EFFECTS (result) = 1;
2369 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2370 /* If the "function" is really an object of class type, it might
2371 have an overloaded `operator ()'. */
2372 result = build_op_call (fn, args, complain);
2374 if (!result)
2375 /* A call where the function is unknown. */
2376 result = cp_build_function_call_vec (fn, args, complain);
2378 if (processing_template_decl && result != error_mark_node)
2380 if (INDIRECT_REF_P (result))
2381 result = TREE_OPERAND (result, 0);
2382 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2383 SET_EXPR_LOCATION (result, input_location);
2384 KOENIG_LOOKUP_P (result) = koenig_p;
2385 release_tree_vector (orig_args);
2386 result = convert_from_reference (result);
2389 if (koenig_p)
2391 /* Free garbage OVERLOADs from arg-dependent lookup. */
2392 tree next = NULL_TREE;
2393 for (fn = orig_fn;
2394 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2395 fn = next)
2397 if (processing_template_decl)
2398 /* In a template, we'll re-use them at instantiation time. */
2399 OVL_ARG_DEPENDENT (fn) = false;
2400 else
2402 next = OVL_CHAIN (fn);
2403 ggc_free (fn);
2408 return result;
2411 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2412 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2413 POSTDECREMENT_EXPR.) */
2415 tree
2416 finish_increment_expr (tree expr, enum tree_code code)
2418 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2421 /* Finish a use of `this'. Returns an expression for `this'. */
2423 tree
2424 finish_this_expr (void)
2426 tree result;
2428 if (current_class_ptr)
2430 tree type = TREE_TYPE (current_class_ref);
2432 /* In a lambda expression, 'this' refers to the captured 'this'. */
2433 if (LAMBDA_TYPE_P (type))
2434 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2435 else
2436 result = current_class_ptr;
2438 else if (current_function_decl
2439 && DECL_STATIC_FUNCTION_P (current_function_decl))
2441 error ("%<this%> is unavailable for static member functions");
2442 result = error_mark_node;
2444 else
2446 if (current_function_decl)
2447 error ("invalid use of %<this%> in non-member function");
2448 else
2449 error ("invalid use of %<this%> at top level");
2450 result = error_mark_node;
2453 /* The keyword 'this' is a prvalue expression. */
2454 result = rvalue (result);
2456 return result;
2459 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2460 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2461 the TYPE for the type given. If SCOPE is non-NULL, the expression
2462 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2464 tree
2465 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2466 location_t loc)
2468 if (object == error_mark_node || destructor == error_mark_node)
2469 return error_mark_node;
2471 gcc_assert (TYPE_P (destructor));
2473 if (!processing_template_decl)
2475 if (scope == error_mark_node)
2477 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2478 return error_mark_node;
2480 if (is_auto (destructor))
2481 destructor = TREE_TYPE (object);
2482 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2484 error_at (loc,
2485 "qualified type %qT does not match destructor name ~%qT",
2486 scope, destructor);
2487 return error_mark_node;
2491 /* [expr.pseudo] says both:
2493 The type designated by the pseudo-destructor-name shall be
2494 the same as the object type.
2496 and:
2498 The cv-unqualified versions of the object type and of the
2499 type designated by the pseudo-destructor-name shall be the
2500 same type.
2502 We implement the more generous second sentence, since that is
2503 what most other compilers do. */
2504 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2505 destructor))
2507 error_at (loc, "%qE is not of type %qT", object, destructor);
2508 return error_mark_node;
2512 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2513 scope, destructor);
2516 /* Finish an expression of the form CODE EXPR. */
2518 tree
2519 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2520 tsubst_flags_t complain)
2522 tree result = build_x_unary_op (loc, code, expr, complain);
2523 if ((complain & tf_warning)
2524 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2525 overflow_warning (input_location, result);
2527 return result;
2530 /* Finish a compound-literal expression. TYPE is the type to which
2531 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2533 tree
2534 finish_compound_literal (tree type, tree compound_literal,
2535 tsubst_flags_t complain)
2537 if (type == error_mark_node)
2538 return error_mark_node;
2540 if (TREE_CODE (type) == REFERENCE_TYPE)
2542 compound_literal
2543 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2544 complain);
2545 return cp_build_c_cast (type, compound_literal, complain);
2548 if (!TYPE_OBJ_P (type))
2550 if (complain & tf_error)
2551 error ("compound literal of non-object type %qT", type);
2552 return error_mark_node;
2555 if (processing_template_decl)
2557 TREE_TYPE (compound_literal) = type;
2558 /* Mark the expression as a compound literal. */
2559 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2560 return compound_literal;
2563 type = complete_type (type);
2565 if (TYPE_NON_AGGREGATE_CLASS (type))
2567 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2568 everywhere that deals with function arguments would be a pain, so
2569 just wrap it in a TREE_LIST. The parser set a flag so we know
2570 that it came from T{} rather than T({}). */
2571 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2572 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2573 return build_functional_cast (type, compound_literal, complain);
2576 if (TREE_CODE (type) == ARRAY_TYPE
2577 && check_array_initializer (NULL_TREE, type, compound_literal))
2578 return error_mark_node;
2579 compound_literal = reshape_init (type, compound_literal, complain);
2580 if (SCALAR_TYPE_P (type)
2581 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2582 && (complain & tf_warning_or_error))
2583 check_narrowing (type, compound_literal);
2584 if (TREE_CODE (type) == ARRAY_TYPE
2585 && TYPE_DOMAIN (type) == NULL_TREE)
2587 cp_complete_array_type_or_error (&type, compound_literal,
2588 false, complain);
2589 if (type == error_mark_node)
2590 return error_mark_node;
2592 compound_literal = digest_init (type, compound_literal, complain);
2593 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2594 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2595 /* Put static/constant array temporaries in static variables, but always
2596 represent class temporaries with TARGET_EXPR so we elide copies. */
2597 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2598 && TREE_CODE (type) == ARRAY_TYPE
2599 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2600 && !cp_unevaluated_operand
2601 && initializer_constant_valid_p (compound_literal, type))
2603 tree decl = create_temporary_var (type);
2604 DECL_INITIAL (decl) = compound_literal;
2605 TREE_STATIC (decl) = 1;
2606 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2608 /* 5.19 says that a constant expression can include an
2609 lvalue-rvalue conversion applied to "a glvalue of literal type
2610 that refers to a non-volatile temporary object initialized
2611 with a constant expression". Rather than try to communicate
2612 that this VAR_DECL is a temporary, just mark it constexpr. */
2613 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2614 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2615 TREE_CONSTANT (decl) = true;
2617 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2618 decl = pushdecl_top_level (decl);
2619 DECL_NAME (decl) = make_anon_name ();
2620 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2621 /* Make sure the destructor is callable. */
2622 tree clean = cxx_maybe_build_cleanup (decl, complain);
2623 if (clean == error_mark_node)
2624 return error_mark_node;
2625 return decl;
2627 else
2628 return get_target_expr_sfinae (compound_literal, complain);
2631 /* Return the declaration for the function-name variable indicated by
2632 ID. */
2634 tree
2635 finish_fname (tree id)
2637 tree decl;
2639 decl = fname_decl (input_location, C_RID_CODE (id), id);
2640 if (processing_template_decl && current_function_decl
2641 && decl != error_mark_node)
2642 decl = DECL_NAME (decl);
2643 return decl;
2646 /* Finish a translation unit. */
2648 void
2649 finish_translation_unit (void)
2651 /* In case there were missing closebraces,
2652 get us back to the global binding level. */
2653 pop_everything ();
2654 while (current_namespace != global_namespace)
2655 pop_namespace ();
2657 /* Do file scope __FUNCTION__ et al. */
2658 finish_fname_decls ();
2661 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2662 Returns the parameter. */
2664 tree
2665 finish_template_type_parm (tree aggr, tree identifier)
2667 if (aggr != class_type_node)
2669 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2670 aggr = class_type_node;
2673 return build_tree_list (aggr, identifier);
2676 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2677 Returns the parameter. */
2679 tree
2680 finish_template_template_parm (tree aggr, tree identifier)
2682 tree decl = build_lang_decl_loc (input_location,
2683 TYPE_DECL, identifier, NULL_TREE);
2685 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2686 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2687 DECL_CONSTRAINTS (tmpl) = current_template_reqs;
2688 DECL_TEMPLATE_RESULT (tmpl) = decl;
2689 DECL_ARTIFICIAL (decl) = 1;
2691 end_template_decl ();
2693 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2695 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2696 /*is_primary=*/true, /*is_partial=*/false,
2697 /*is_friend=*/0);
2699 return finish_template_type_parm (aggr, tmpl);
2702 /* ARGUMENT is the default-argument value for a template template
2703 parameter. If ARGUMENT is invalid, issue error messages and return
2704 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2706 tree
2707 check_template_template_default_arg (tree argument)
2709 if (TREE_CODE (argument) != TEMPLATE_DECL
2710 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2711 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2713 if (TREE_CODE (argument) == TYPE_DECL)
2714 error ("invalid use of type %qT as a default value for a template "
2715 "template-parameter", TREE_TYPE (argument));
2716 else
2717 error ("invalid default argument for a template template parameter");
2718 return error_mark_node;
2721 return argument;
2724 /* Begin a class definition, as indicated by T. */
2726 tree
2727 begin_class_definition (tree t)
2729 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2730 return error_mark_node;
2732 if (processing_template_parmlist)
2734 error ("definition of %q#T inside template parameter list", t);
2735 return error_mark_node;
2738 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2739 are passed the same as decimal scalar types. */
2740 if (TREE_CODE (t) == RECORD_TYPE
2741 && !processing_template_decl)
2743 tree ns = TYPE_CONTEXT (t);
2744 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2745 && DECL_CONTEXT (ns) == std_node
2746 && DECL_NAME (ns)
2747 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2749 const char *n = TYPE_NAME_STRING (t);
2750 if ((strcmp (n, "decimal32") == 0)
2751 || (strcmp (n, "decimal64") == 0)
2752 || (strcmp (n, "decimal128") == 0))
2753 TYPE_TRANSPARENT_AGGR (t) = 1;
2757 /* A non-implicit typename comes from code like:
2759 template <typename T> struct A {
2760 template <typename U> struct A<T>::B ...
2762 This is erroneous. */
2763 else if (TREE_CODE (t) == TYPENAME_TYPE)
2765 error ("invalid definition of qualified type %qT", t);
2766 t = error_mark_node;
2769 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2771 t = make_class_type (RECORD_TYPE);
2772 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2775 if (TYPE_BEING_DEFINED (t))
2777 t = make_class_type (TREE_CODE (t));
2778 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2780 maybe_process_partial_specialization (t);
2781 pushclass (t);
2782 TYPE_BEING_DEFINED (t) = 1;
2784 if (flag_pack_struct)
2786 tree v;
2787 TYPE_PACKED (t) = 1;
2788 /* Even though the type is being defined for the first time
2789 here, there might have been a forward declaration, so there
2790 might be cv-qualified variants of T. */
2791 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2792 TYPE_PACKED (v) = 1;
2794 /* Reset the interface data, at the earliest possible
2795 moment, as it might have been set via a class foo;
2796 before. */
2797 if (! TYPE_ANONYMOUS_P (t))
2799 struct c_fileinfo *finfo = \
2800 get_fileinfo (LOCATION_FILE (input_location));
2801 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2802 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2803 (t, finfo->interface_unknown);
2805 reset_specialization();
2807 /* Make a declaration for this class in its own scope. */
2808 build_self_reference ();
2810 return t;
2813 /* Finish the member declaration given by DECL. */
2815 void
2816 finish_member_declaration (tree decl)
2818 if (decl == error_mark_node || decl == NULL_TREE)
2819 return;
2821 if (decl == void_type_node)
2822 /* The COMPONENT was a friend, not a member, and so there's
2823 nothing for us to do. */
2824 return;
2826 /* We should see only one DECL at a time. */
2827 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2829 /* Set up access control for DECL. */
2830 TREE_PRIVATE (decl)
2831 = (current_access_specifier == access_private_node);
2832 TREE_PROTECTED (decl)
2833 = (current_access_specifier == access_protected_node);
2834 if (TREE_CODE (decl) == TEMPLATE_DECL)
2836 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2837 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2840 /* Mark the DECL as a member of the current class, unless it's
2841 a member of an enumeration. */
2842 if (TREE_CODE (decl) != CONST_DECL)
2843 DECL_CONTEXT (decl) = current_class_type;
2845 /* Check for bare parameter packs in the member variable declaration. */
2846 if (TREE_CODE (decl) == FIELD_DECL)
2848 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2849 TREE_TYPE (decl) = error_mark_node;
2850 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2851 DECL_ATTRIBUTES (decl) = NULL_TREE;
2854 /* [dcl.link]
2856 A C language linkage is ignored for the names of class members
2857 and the member function type of class member functions. */
2858 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2859 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2861 /* Put functions on the TYPE_METHODS list and everything else on the
2862 TYPE_FIELDS list. Note that these are built up in reverse order.
2863 We reverse them (to obtain declaration order) in finish_struct. */
2864 if (DECL_DECLARES_FUNCTION_P (decl))
2866 /* We also need to add this function to the
2867 CLASSTYPE_METHOD_VEC. */
2868 if (add_method (current_class_type, decl, NULL_TREE))
2870 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2871 TYPE_METHODS (current_class_type) = decl;
2873 maybe_add_class_template_decl_list (current_class_type, decl,
2874 /*friend_p=*/0);
2877 /* Enter the DECL into the scope of the class, if the class
2878 isn't a closure (whose fields are supposed to be unnamed). */
2879 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2880 || pushdecl_class_level (decl))
2882 if (TREE_CODE (decl) == USING_DECL)
2884 /* For now, ignore class-scope USING_DECLS, so that
2885 debugging backends do not see them. */
2886 DECL_IGNORED_P (decl) = 1;
2889 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2890 go at the beginning. The reason is that lookup_field_1
2891 searches the list in order, and we want a field name to
2892 override a type name so that the "struct stat hack" will
2893 work. In particular:
2895 struct S { enum E { }; int E } s;
2896 s.E = 3;
2898 is valid. In addition, the FIELD_DECLs must be maintained in
2899 declaration order so that class layout works as expected.
2900 However, we don't need that order until class layout, so we
2901 save a little time by putting FIELD_DECLs on in reverse order
2902 here, and then reversing them in finish_struct_1. (We could
2903 also keep a pointer to the correct insertion points in the
2904 list.) */
2906 if (TREE_CODE (decl) == TYPE_DECL)
2907 TYPE_FIELDS (current_class_type)
2908 = chainon (TYPE_FIELDS (current_class_type), decl);
2909 else
2911 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2912 TYPE_FIELDS (current_class_type) = decl;
2915 maybe_add_class_template_decl_list (current_class_type, decl,
2916 /*friend_p=*/0);
2919 if (pch_file)
2920 note_decl_for_pch (decl);
2923 /* DECL has been declared while we are building a PCH file. Perform
2924 actions that we might normally undertake lazily, but which can be
2925 performed now so that they do not have to be performed in
2926 translation units which include the PCH file. */
2928 void
2929 note_decl_for_pch (tree decl)
2931 gcc_assert (pch_file);
2933 /* There's a good chance that we'll have to mangle names at some
2934 point, even if only for emission in debugging information. */
2935 if (VAR_OR_FUNCTION_DECL_P (decl)
2936 && !processing_template_decl)
2937 mangle_decl (decl);
2940 /* Finish processing a complete template declaration. The PARMS are
2941 the template parameters. */
2943 void
2944 finish_template_decl (tree parms)
2946 if (parms)
2947 end_template_decl ();
2948 else
2949 end_specialization ();
2952 // Returns the template type of the class scope being entered. If we're
2953 // entering a constrained class scope. TYPE is the class template
2954 // scope being entered and we may need to match the intended type with
2955 // a constrained specialization. For example:
2957 // template<Object T>
2958 // struct S { void f(); }; #1
2960 // template<Object T>
2961 // void S<T>::f() { } #2
2963 // We check, in #2, that S<T> refers precisely to the type declared by
2964 // #1 (i.e., that the constraints match). Note that the following should
2965 // be an error since there is no specialization of S<T> that is
2966 // unconstrained, but this is not diagnosed here.
2968 // template<typename T>
2969 // void S<T>::f() { }
2971 // We cannot diagnose this problem here since this function also matches
2972 // qualified template names that are not part of a definition. For example:
2974 // template<Integral T, Floating_point U>
2975 // typename pair<T, U>::first_type void f(T, U);
2977 // Here, it is unlikely that there is a partial specialization of
2978 // pair constrained for for Integral and Floating_point arguments.
2980 // The general rule is: if a constrained specialization with matching
2981 // constraints is found return that type. Alos note that if TYPE is not a
2982 // class-type (e.g. a typename type), then no fixup is needed.
2983 static tree
2984 fixup_template_type (tree type)
2986 // Don't try to fix non-class types.
2987 if (!CLASS_TYPE_P (type))
2988 return type;
2990 // Find the template parameter list at the a depth appropriate to
2991 // the scope we're trying to enter.
2992 tree parms = current_template_parms;
2993 int depth = template_class_depth (type);
2994 for (int n = processing_template_decl; n > depth && parms; --n)
2995 parms = TREE_CHAIN (parms);
2996 if (!parms)
2997 return type;
2998 tree cur_constr = TEMPLATE_PARMS_CONSTRAINTS (parms);
3000 // Search for a specialization whose type and constraints match.
3001 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3002 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3003 while (specs)
3005 tree spec_constr = DECL_CONSTRAINTS (TREE_VALUE (specs));
3007 // If the type and constraints match a specialization, then we
3008 // are entering that type. Note that the type comparison is
3009 // structural since constrained partial specialiations may
3010 // have different canonical types for the same type patterns.
3011 if (comptypes (type, TREE_TYPE (specs), COMPARE_STRUCTURAL)
3012 && equivalent_constraints (cur_constr, spec_constr))
3013 return TREE_TYPE (specs);
3014 specs = TREE_CHAIN (specs);
3017 // If no specialization matches, then must return the type
3018 // previously found.
3019 return type;
3023 /* Finish processing a template-id (which names a type) of the form
3024 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3025 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3026 the scope of template-id indicated. */
3028 tree
3029 finish_template_type (tree name, tree args, int entering_scope)
3031 tree type;
3033 type = lookup_template_class (name, args,
3034 NULL_TREE, NULL_TREE, entering_scope,
3035 tf_warning_or_error | tf_user);
3037 // If entering a scope, correct the lookup to account for constraints.
3038 if (entering_scope)
3039 type = fixup_template_type (type);
3041 if (type == error_mark_node)
3042 return type;
3043 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3044 return TYPE_STUB_DECL (type);
3045 else
3046 return TYPE_NAME (type);
3049 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3050 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3051 BASE_CLASS, or NULL_TREE if an error occurred. The
3052 ACCESS_SPECIFIER is one of
3053 access_{default,public,protected_private}_node. For a virtual base
3054 we set TREE_TYPE. */
3056 tree
3057 finish_base_specifier (tree base, tree access, bool virtual_p)
3059 tree result;
3061 if (base == error_mark_node)
3063 error ("invalid base-class specification");
3064 result = NULL_TREE;
3066 else if (! MAYBE_CLASS_TYPE_P (base))
3068 error ("%qT is not a class type", base);
3069 result = NULL_TREE;
3071 else
3073 if (cp_type_quals (base) != 0)
3075 /* DR 484: Can a base-specifier name a cv-qualified
3076 class type? */
3077 base = TYPE_MAIN_VARIANT (base);
3079 result = build_tree_list (access, base);
3080 if (virtual_p)
3081 TREE_TYPE (result) = integer_type_node;
3084 return result;
3087 /* If FNS is a member function, a set of member functions, or a
3088 template-id referring to one or more member functions, return a
3089 BASELINK for FNS, incorporating the current access context.
3090 Otherwise, return FNS unchanged. */
3092 tree
3093 baselink_for_fns (tree fns)
3095 tree scope;
3096 tree cl;
3098 if (BASELINK_P (fns)
3099 || error_operand_p (fns))
3100 return fns;
3102 scope = ovl_scope (fns);
3103 if (!CLASS_TYPE_P (scope))
3104 return fns;
3106 cl = currently_open_derived_class (scope);
3107 if (!cl)
3108 cl = scope;
3109 cl = TYPE_BINFO (cl);
3110 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3113 /* Returns true iff DECL is a variable from a function outside
3114 the current one. */
3116 static bool
3117 outer_var_p (tree decl)
3119 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3120 && DECL_FUNCTION_SCOPE_P (decl)
3121 && (DECL_CONTEXT (decl) != current_function_decl
3122 || parsing_nsdmi ()));
3125 /* As above, but also checks that DECL is automatic. */
3127 static bool
3128 outer_automatic_var_p (tree decl)
3130 return (outer_var_p (decl)
3131 && !TREE_STATIC (decl));
3134 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3135 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3136 if non-NULL, is the type or namespace used to explicitly qualify
3137 ID_EXPRESSION. DECL is the entity to which that name has been
3138 resolved.
3140 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3141 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3142 be set to true if this expression isn't permitted in a
3143 constant-expression, but it is otherwise not set by this function.
3144 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3145 constant-expression, but a non-constant expression is also
3146 permissible.
3148 DONE is true if this expression is a complete postfix-expression;
3149 it is false if this expression is followed by '->', '[', '(', etc.
3150 ADDRESS_P is true iff this expression is the operand of '&'.
3151 TEMPLATE_P is true iff the qualified-id was of the form
3152 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3153 appears as a template argument.
3155 If an error occurs, and it is the kind of error that might cause
3156 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3157 is the caller's responsibility to issue the message. *ERROR_MSG
3158 will be a string with static storage duration, so the caller need
3159 not "free" it.
3161 Return an expression for the entity, after issuing appropriate
3162 diagnostics. This function is also responsible for transforming a
3163 reference to a non-static member into a COMPONENT_REF that makes
3164 the use of "this" explicit.
3166 Upon return, *IDK will be filled in appropriately. */
3167 tree
3168 finish_id_expression (tree id_expression,
3169 tree decl,
3170 tree scope,
3171 cp_id_kind *idk,
3172 bool integral_constant_expression_p,
3173 bool allow_non_integral_constant_expression_p,
3174 bool *non_integral_constant_expression_p,
3175 bool template_p,
3176 bool done,
3177 bool address_p,
3178 bool template_arg_p,
3179 const char **error_msg,
3180 location_t location)
3182 decl = strip_using_decl (decl);
3184 /* Initialize the output parameters. */
3185 *idk = CP_ID_KIND_NONE;
3186 *error_msg = NULL;
3188 if (id_expression == error_mark_node)
3189 return error_mark_node;
3190 /* If we have a template-id, then no further lookup is
3191 required. If the template-id was for a template-class, we
3192 will sometimes have a TYPE_DECL at this point. */
3193 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3194 || TREE_CODE (decl) == TYPE_DECL)
3196 /* Look up the name. */
3197 else
3199 if (decl == error_mark_node)
3201 /* Name lookup failed. */
3202 if (scope
3203 && (!TYPE_P (scope)
3204 || (!dependent_type_p (scope)
3205 && !(identifier_p (id_expression)
3206 && IDENTIFIER_TYPENAME_P (id_expression)
3207 && dependent_type_p (TREE_TYPE (id_expression))))))
3209 /* If the qualifying type is non-dependent (and the name
3210 does not name a conversion operator to a dependent
3211 type), issue an error. */
3212 qualified_name_lookup_error (scope, id_expression, decl, location);
3213 return error_mark_node;
3215 else if (!scope)
3217 /* It may be resolved via Koenig lookup. */
3218 *idk = CP_ID_KIND_UNQUALIFIED;
3219 return id_expression;
3221 else
3222 decl = id_expression;
3224 /* If DECL is a variable that would be out of scope under
3225 ANSI/ISO rules, but in scope in the ARM, name lookup
3226 will succeed. Issue a diagnostic here. */
3227 else
3228 decl = check_for_out_of_scope_variable (decl);
3230 /* Remember that the name was used in the definition of
3231 the current class so that we can check later to see if
3232 the meaning would have been different after the class
3233 was entirely defined. */
3234 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3235 maybe_note_name_used_in_class (id_expression, decl);
3237 /* Disallow uses of local variables from containing functions, except
3238 within lambda-expressions. */
3239 if (!outer_var_p (decl))
3240 /* OK */;
3241 else if (TREE_STATIC (decl)
3242 /* It's not a use (3.2) if we're in an unevaluated context. */
3243 || cp_unevaluated_operand)
3245 if (processing_template_decl)
3246 /* For a use of an outer static/unevaluated var, return the id
3247 so that we'll look it up again in the instantiation. */
3248 return id_expression;
3250 else
3252 tree context = DECL_CONTEXT (decl);
3253 tree containing_function = current_function_decl;
3254 tree lambda_stack = NULL_TREE;
3255 tree lambda_expr = NULL_TREE;
3256 tree initializer = convert_from_reference (decl);
3258 /* Mark it as used now even if the use is ill-formed. */
3259 mark_used (decl);
3261 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3262 support for an approach in which a reference to a local
3263 [constant] automatic variable in a nested class or lambda body
3264 would enter the expression as an rvalue, which would reduce
3265 the complexity of the problem"
3267 FIXME update for final resolution of core issue 696. */
3268 if (decl_constant_var_p (decl))
3270 if (processing_template_decl)
3271 /* In a template, the constant value may not be in a usable
3272 form, so look it up again at instantiation time. */
3273 return id_expression;
3274 else
3275 return integral_constant_value (decl);
3278 if (parsing_nsdmi ())
3279 containing_function = NULL_TREE;
3280 /* If we are in a lambda function, we can move out until we hit
3281 1. the context,
3282 2. a non-lambda function, or
3283 3. a non-default capturing lambda function. */
3284 else while (context != containing_function
3285 && LAMBDA_FUNCTION_P (containing_function))
3287 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3288 (DECL_CONTEXT (containing_function));
3290 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3291 == CPLD_NONE)
3292 break;
3294 lambda_stack = tree_cons (NULL_TREE,
3295 lambda_expr,
3296 lambda_stack);
3298 containing_function
3299 = decl_function_context (containing_function);
3302 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3303 && DECL_ANON_UNION_VAR_P (decl))
3305 error ("cannot capture member %qD of anonymous union", decl);
3306 return error_mark_node;
3308 if (context == containing_function)
3310 decl = add_default_capture (lambda_stack,
3311 /*id=*/DECL_NAME (decl),
3312 initializer);
3314 else if (lambda_expr)
3316 error ("%qD is not captured", decl);
3317 return error_mark_node;
3319 else
3321 error (VAR_P (decl)
3322 ? G_("use of local variable with automatic storage from containing function")
3323 : G_("use of parameter from containing function"));
3324 inform (input_location, "%q+#D declared here", decl);
3325 return error_mark_node;
3329 /* Also disallow uses of function parameters outside the function
3330 body, except inside an unevaluated context (i.e. decltype). */
3331 if (TREE_CODE (decl) == PARM_DECL
3332 && DECL_CONTEXT (decl) == NULL_TREE
3333 && !cp_unevaluated_operand)
3335 *error_msg = "use of parameter outside function body";
3336 return error_mark_node;
3340 /* If we didn't find anything, or what we found was a type,
3341 then this wasn't really an id-expression. */
3342 if (TREE_CODE (decl) == TEMPLATE_DECL
3343 && !DECL_FUNCTION_TEMPLATE_P (decl))
3345 *error_msg = "missing template arguments";
3346 return error_mark_node;
3348 else if (TREE_CODE (decl) == TYPE_DECL
3349 || TREE_CODE (decl) == NAMESPACE_DECL)
3351 *error_msg = "expected primary-expression";
3352 return error_mark_node;
3355 /* If the name resolved to a template parameter, there is no
3356 need to look it up again later. */
3357 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3358 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3360 tree r;
3362 *idk = CP_ID_KIND_NONE;
3363 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3364 decl = TEMPLATE_PARM_DECL (decl);
3365 r = convert_from_reference (DECL_INITIAL (decl));
3367 if (integral_constant_expression_p
3368 && !dependent_type_p (TREE_TYPE (decl))
3369 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3371 if (!allow_non_integral_constant_expression_p)
3372 error ("template parameter %qD of type %qT is not allowed in "
3373 "an integral constant expression because it is not of "
3374 "integral or enumeration type", decl, TREE_TYPE (decl));
3375 *non_integral_constant_expression_p = true;
3377 return r;
3379 else
3381 bool dependent_p;
3383 /* If the declaration was explicitly qualified indicate
3384 that. The semantics of `A::f(3)' are different than
3385 `f(3)' if `f' is virtual. */
3386 *idk = (scope
3387 ? CP_ID_KIND_QUALIFIED
3388 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3389 ? CP_ID_KIND_TEMPLATE_ID
3390 : CP_ID_KIND_UNQUALIFIED));
3393 /* [temp.dep.expr]
3395 An id-expression is type-dependent if it contains an
3396 identifier that was declared with a dependent type.
3398 The standard is not very specific about an id-expression that
3399 names a set of overloaded functions. What if some of them
3400 have dependent types and some of them do not? Presumably,
3401 such a name should be treated as a dependent name. */
3402 /* Assume the name is not dependent. */
3403 dependent_p = false;
3404 if (!processing_template_decl)
3405 /* No names are dependent outside a template. */
3407 else if (TREE_CODE (decl) == CONST_DECL)
3408 /* We don't want to treat enumerators as dependent. */
3410 /* A template-id where the name of the template was not resolved
3411 is definitely dependent. */
3412 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3413 && (identifier_p (TREE_OPERAND (decl, 0))))
3414 dependent_p = true;
3415 /* For anything except an overloaded function, just check its
3416 type. */
3417 else if (!is_overloaded_fn (decl))
3418 dependent_p
3419 = dependent_type_p (TREE_TYPE (decl));
3420 /* For a set of overloaded functions, check each of the
3421 functions. */
3422 else
3424 tree fns = decl;
3426 if (BASELINK_P (fns))
3427 fns = BASELINK_FUNCTIONS (fns);
3429 /* For a template-id, check to see if the template
3430 arguments are dependent. */
3431 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3433 tree args = TREE_OPERAND (fns, 1);
3434 dependent_p = any_dependent_template_arguments_p (args);
3435 /* The functions are those referred to by the
3436 template-id. */
3437 fns = TREE_OPERAND (fns, 0);
3440 /* If there are no dependent template arguments, go through
3441 the overloaded functions. */
3442 while (fns && !dependent_p)
3444 tree fn = OVL_CURRENT (fns);
3446 /* Member functions of dependent classes are
3447 dependent. */
3448 if (TREE_CODE (fn) == FUNCTION_DECL
3449 && type_dependent_expression_p (fn))
3450 dependent_p = true;
3451 else if (TREE_CODE (fn) == TEMPLATE_DECL
3452 && dependent_template_p (fn))
3453 dependent_p = true;
3455 fns = OVL_NEXT (fns);
3459 /* If the name was dependent on a template parameter, we will
3460 resolve the name at instantiation time. */
3461 if (dependent_p)
3463 /* Create a SCOPE_REF for qualified names, if the scope is
3464 dependent. */
3465 if (scope)
3467 if (TYPE_P (scope))
3469 if (address_p && done)
3470 decl = finish_qualified_id_expr (scope, decl,
3471 done, address_p,
3472 template_p,
3473 template_arg_p,
3474 tf_warning_or_error);
3475 else
3477 tree type = NULL_TREE;
3478 if (DECL_P (decl) && !dependent_scope_p (scope))
3479 type = TREE_TYPE (decl);
3480 decl = build_qualified_name (type,
3481 scope,
3482 id_expression,
3483 template_p);
3486 if (TREE_TYPE (decl))
3487 decl = convert_from_reference (decl);
3488 return decl;
3490 /* A TEMPLATE_ID already contains all the information we
3491 need. */
3492 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3493 return id_expression;
3494 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3495 /* If we found a variable, then name lookup during the
3496 instantiation will always resolve to the same VAR_DECL
3497 (or an instantiation thereof). */
3498 if (VAR_P (decl)
3499 || TREE_CODE (decl) == PARM_DECL)
3501 mark_used (decl);
3502 return convert_from_reference (decl);
3504 /* The same is true for FIELD_DECL, but we also need to
3505 make sure that the syntax is correct. */
3506 else if (TREE_CODE (decl) == FIELD_DECL)
3508 /* Since SCOPE is NULL here, this is an unqualified name.
3509 Access checking has been performed during name lookup
3510 already. Turn off checking to avoid duplicate errors. */
3511 push_deferring_access_checks (dk_no_check);
3512 decl = finish_non_static_data_member
3513 (decl, NULL_TREE,
3514 /*qualifying_scope=*/NULL_TREE);
3515 pop_deferring_access_checks ();
3516 return decl;
3518 return id_expression;
3521 if (TREE_CODE (decl) == NAMESPACE_DECL)
3523 error ("use of namespace %qD as expression", decl);
3524 return error_mark_node;
3526 else if (DECL_CLASS_TEMPLATE_P (decl))
3528 error ("use of class template %qT as expression", decl);
3529 return error_mark_node;
3531 else if (TREE_CODE (decl) == TREE_LIST)
3533 /* Ambiguous reference to base members. */
3534 error ("request for member %qD is ambiguous in "
3535 "multiple inheritance lattice", id_expression);
3536 print_candidates (decl);
3537 return error_mark_node;
3540 /* Mark variable-like entities as used. Functions are similarly
3541 marked either below or after overload resolution. */
3542 if ((VAR_P (decl)
3543 || TREE_CODE (decl) == PARM_DECL
3544 || TREE_CODE (decl) == CONST_DECL
3545 || TREE_CODE (decl) == RESULT_DECL)
3546 && !mark_used (decl))
3547 return error_mark_node;
3549 /* Only certain kinds of names are allowed in constant
3550 expression. Template parameters have already
3551 been handled above. */
3552 if (! error_operand_p (decl)
3553 && integral_constant_expression_p
3554 && ! decl_constant_var_p (decl)
3555 && TREE_CODE (decl) != CONST_DECL
3556 && ! builtin_valid_in_constant_expr_p (decl))
3558 if (!allow_non_integral_constant_expression_p)
3560 error ("%qD cannot appear in a constant-expression", decl);
3561 return error_mark_node;
3563 *non_integral_constant_expression_p = true;
3566 tree wrap;
3567 if (VAR_P (decl)
3568 && !cp_unevaluated_operand
3569 && DECL_THREAD_LOCAL_P (decl)
3570 && (wrap = get_tls_wrapper_fn (decl)))
3572 /* Replace an evaluated use of the thread_local variable with
3573 a call to its wrapper. */
3574 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3576 else if (scope)
3578 decl = (adjust_result_of_qualified_name_lookup
3579 (decl, scope, current_nonlambda_class_type()));
3581 if (TREE_CODE (decl) == FUNCTION_DECL)
3582 mark_used (decl);
3584 if (TYPE_P (scope))
3585 decl = finish_qualified_id_expr (scope,
3586 decl,
3587 done,
3588 address_p,
3589 template_p,
3590 template_arg_p,
3591 tf_warning_or_error);
3592 else
3593 decl = convert_from_reference (decl);
3595 else if (TREE_CODE (decl) == FIELD_DECL)
3597 /* Since SCOPE is NULL here, this is an unqualified name.
3598 Access checking has been performed during name lookup
3599 already. Turn off checking to avoid duplicate errors. */
3600 push_deferring_access_checks (dk_no_check);
3601 decl = finish_non_static_data_member (decl, NULL_TREE,
3602 /*qualifying_scope=*/NULL_TREE);
3603 pop_deferring_access_checks ();
3605 else if (is_overloaded_fn (decl))
3607 tree first_fn;
3609 first_fn = get_first_fn (decl);
3610 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3611 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3613 if (!really_overloaded_fn (decl)
3614 && !mark_used (first_fn))
3615 return error_mark_node;
3617 if (!template_arg_p
3618 && TREE_CODE (first_fn) == FUNCTION_DECL
3619 && DECL_FUNCTION_MEMBER_P (first_fn)
3620 && !shared_member_p (decl))
3622 /* A set of member functions. */
3623 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3624 return finish_class_member_access_expr (decl, id_expression,
3625 /*template_p=*/false,
3626 tf_warning_or_error);
3629 decl = baselink_for_fns (decl);
3631 else
3633 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3634 && DECL_CLASS_SCOPE_P (decl))
3636 tree context = context_for_name_lookup (decl);
3637 if (context != current_class_type)
3639 tree path = currently_open_derived_class (context);
3640 perform_or_defer_access_check (TYPE_BINFO (path),
3641 decl, decl,
3642 tf_warning_or_error);
3646 decl = convert_from_reference (decl);
3650 /* Handle references (c++/56130). */
3651 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3652 if (TREE_DEPRECATED (t))
3653 warn_deprecated_use (t, NULL_TREE);
3655 return decl;
3658 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3659 use as a type-specifier. */
3661 tree
3662 finish_typeof (tree expr)
3664 tree type;
3666 if (type_dependent_expression_p (expr))
3668 type = cxx_make_type (TYPEOF_TYPE);
3669 TYPEOF_TYPE_EXPR (type) = expr;
3670 SET_TYPE_STRUCTURAL_EQUALITY (type);
3672 return type;
3675 expr = mark_type_use (expr);
3677 type = unlowered_expr_type (expr);
3679 if (!type || type == unknown_type_node)
3681 error ("type of %qE is unknown", expr);
3682 return error_mark_node;
3685 return type;
3688 /* Implement the __underlying_type keyword: Return the underlying
3689 type of TYPE, suitable for use as a type-specifier. */
3691 tree
3692 finish_underlying_type (tree type)
3694 tree underlying_type;
3696 if (processing_template_decl)
3698 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3699 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3700 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3702 return underlying_type;
3705 complete_type (type);
3707 if (TREE_CODE (type) != ENUMERAL_TYPE)
3709 error ("%qT is not an enumeration type", type);
3710 return error_mark_node;
3713 underlying_type = ENUM_UNDERLYING_TYPE (type);
3715 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3716 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3717 See finish_enum_value_list for details. */
3718 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3719 underlying_type
3720 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3721 TYPE_UNSIGNED (underlying_type));
3723 return underlying_type;
3726 /* Implement the __direct_bases keyword: Return the direct base classes
3727 of type */
3729 tree
3730 calculate_direct_bases (tree type)
3732 vec<tree, va_gc> *vector = make_tree_vector();
3733 tree bases_vec = NULL_TREE;
3734 vec<tree, va_gc> *base_binfos;
3735 tree binfo;
3736 unsigned i;
3738 complete_type (type);
3740 if (!NON_UNION_CLASS_TYPE_P (type))
3741 return make_tree_vec (0);
3743 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3745 /* Virtual bases are initialized first */
3746 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3748 if (BINFO_VIRTUAL_P (binfo))
3750 vec_safe_push (vector, binfo);
3754 /* Now non-virtuals */
3755 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3757 if (!BINFO_VIRTUAL_P (binfo))
3759 vec_safe_push (vector, binfo);
3764 bases_vec = make_tree_vec (vector->length ());
3766 for (i = 0; i < vector->length (); ++i)
3768 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3770 return bases_vec;
3773 /* Implement the __bases keyword: Return the base classes
3774 of type */
3776 /* Find morally non-virtual base classes by walking binfo hierarchy */
3777 /* Virtual base classes are handled separately in finish_bases */
3779 static tree
3780 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3782 /* Don't walk bases of virtual bases */
3783 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3786 static tree
3787 dfs_calculate_bases_post (tree binfo, void *data_)
3789 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3790 if (!BINFO_VIRTUAL_P (binfo))
3792 vec_safe_push (*data, BINFO_TYPE (binfo));
3794 return NULL_TREE;
3797 /* Calculates the morally non-virtual base classes of a class */
3798 static vec<tree, va_gc> *
3799 calculate_bases_helper (tree type)
3801 vec<tree, va_gc> *vector = make_tree_vector();
3803 /* Now add non-virtual base classes in order of construction */
3804 dfs_walk_all (TYPE_BINFO (type),
3805 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3806 return vector;
3809 tree
3810 calculate_bases (tree type)
3812 vec<tree, va_gc> *vector = make_tree_vector();
3813 tree bases_vec = NULL_TREE;
3814 unsigned i;
3815 vec<tree, va_gc> *vbases;
3816 vec<tree, va_gc> *nonvbases;
3817 tree binfo;
3819 complete_type (type);
3821 if (!NON_UNION_CLASS_TYPE_P (type))
3822 return make_tree_vec (0);
3824 /* First go through virtual base classes */
3825 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3826 vec_safe_iterate (vbases, i, &binfo); i++)
3828 vec<tree, va_gc> *vbase_bases;
3829 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3830 vec_safe_splice (vector, vbase_bases);
3831 release_tree_vector (vbase_bases);
3834 /* Now for the non-virtual bases */
3835 nonvbases = calculate_bases_helper (type);
3836 vec_safe_splice (vector, nonvbases);
3837 release_tree_vector (nonvbases);
3839 /* Last element is entire class, so don't copy */
3840 bases_vec = make_tree_vec (vector->length () - 1);
3842 for (i = 0; i < vector->length () - 1; ++i)
3844 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3846 release_tree_vector (vector);
3847 return bases_vec;
3850 tree
3851 finish_bases (tree type, bool direct)
3853 tree bases = NULL_TREE;
3855 if (!processing_template_decl)
3857 /* Parameter packs can only be used in templates */
3858 error ("Parameter pack __bases only valid in template declaration");
3859 return error_mark_node;
3862 bases = cxx_make_type (BASES);
3863 BASES_TYPE (bases) = type;
3864 BASES_DIRECT (bases) = direct;
3865 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3867 return bases;
3870 /* Perform C++-specific checks for __builtin_offsetof before calling
3871 fold_offsetof. */
3873 tree
3874 finish_offsetof (tree expr)
3876 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3878 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3879 TREE_OPERAND (expr, 2));
3880 return error_mark_node;
3882 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3883 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3884 || TREE_TYPE (expr) == unknown_type_node)
3886 if (INDIRECT_REF_P (expr))
3887 error ("second operand of %<offsetof%> is neither a single "
3888 "identifier nor a sequence of member accesses and "
3889 "array references");
3890 else
3892 if (TREE_CODE (expr) == COMPONENT_REF
3893 || TREE_CODE (expr) == COMPOUND_EXPR)
3894 expr = TREE_OPERAND (expr, 1);
3895 error ("cannot apply %<offsetof%> to member function %qD", expr);
3897 return error_mark_node;
3899 if (REFERENCE_REF_P (expr))
3900 expr = TREE_OPERAND (expr, 0);
3901 if (TREE_CODE (expr) == COMPONENT_REF)
3903 tree object = TREE_OPERAND (expr, 0);
3904 if (!complete_type_or_else (TREE_TYPE (object), object))
3905 return error_mark_node;
3907 return fold_offsetof (expr);
3910 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3911 function is broken out from the above for the benefit of the tree-ssa
3912 project. */
3914 void
3915 simplify_aggr_init_expr (tree *tp)
3917 tree aggr_init_expr = *tp;
3919 /* Form an appropriate CALL_EXPR. */
3920 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3921 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3922 tree type = TREE_TYPE (slot);
3924 tree call_expr;
3925 enum style_t { ctor, arg, pcc } style;
3927 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3928 style = ctor;
3929 #ifdef PCC_STATIC_STRUCT_RETURN
3930 else if (1)
3931 style = pcc;
3932 #endif
3933 else
3935 gcc_assert (TREE_ADDRESSABLE (type));
3936 style = arg;
3939 call_expr = build_call_array_loc (input_location,
3940 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3942 aggr_init_expr_nargs (aggr_init_expr),
3943 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3944 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3946 if (style == ctor)
3948 /* Replace the first argument to the ctor with the address of the
3949 slot. */
3950 cxx_mark_addressable (slot);
3951 CALL_EXPR_ARG (call_expr, 0) =
3952 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3954 else if (style == arg)
3956 /* Just mark it addressable here, and leave the rest to
3957 expand_call{,_inline}. */
3958 cxx_mark_addressable (slot);
3959 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3960 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3962 else if (style == pcc)
3964 /* If we're using the non-reentrant PCC calling convention, then we
3965 need to copy the returned value out of the static buffer into the
3966 SLOT. */
3967 push_deferring_access_checks (dk_no_check);
3968 call_expr = build_aggr_init (slot, call_expr,
3969 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3970 tf_warning_or_error);
3971 pop_deferring_access_checks ();
3972 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3975 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3977 tree init = build_zero_init (type, NULL_TREE,
3978 /*static_storage_p=*/false);
3979 init = build2 (INIT_EXPR, void_type_node, slot, init);
3980 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3981 init, call_expr);
3984 *tp = call_expr;
3987 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3989 void
3990 emit_associated_thunks (tree fn)
3992 /* When we use vcall offsets, we emit thunks with the virtual
3993 functions to which they thunk. The whole point of vcall offsets
3994 is so that you can know statically the entire set of thunks that
3995 will ever be needed for a given virtual function, thereby
3996 enabling you to output all the thunks with the function itself. */
3997 if (DECL_VIRTUAL_P (fn)
3998 /* Do not emit thunks for extern template instantiations. */
3999 && ! DECL_REALLY_EXTERN (fn))
4001 tree thunk;
4003 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4005 if (!THUNK_ALIAS (thunk))
4007 use_thunk (thunk, /*emit_p=*/1);
4008 if (DECL_RESULT_THUNK_P (thunk))
4010 tree probe;
4012 for (probe = DECL_THUNKS (thunk);
4013 probe; probe = DECL_CHAIN (probe))
4014 use_thunk (probe, /*emit_p=*/1);
4017 else
4018 gcc_assert (!DECL_THUNKS (thunk));
4023 /* Returns true iff FUN is an instantiation of a constexpr function
4024 template. */
4026 static inline bool
4027 is_instantiation_of_constexpr (tree fun)
4029 return (DECL_TEMPLOID_INSTANTIATION (fun)
4030 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)));
4033 /* Generate RTL for FN. */
4035 bool
4036 expand_or_defer_fn_1 (tree fn)
4038 /* When the parser calls us after finishing the body of a template
4039 function, we don't really want to expand the body. */
4040 if (processing_template_decl)
4042 /* Normally, collection only occurs in rest_of_compilation. So,
4043 if we don't collect here, we never collect junk generated
4044 during the processing of templates until we hit a
4045 non-template function. It's not safe to do this inside a
4046 nested class, though, as the parser may have local state that
4047 is not a GC root. */
4048 if (!function_depth)
4049 ggc_collect ();
4050 return false;
4053 gcc_assert (DECL_SAVED_TREE (fn));
4055 /* We make a decision about linkage for these functions at the end
4056 of the compilation. Until that point, we do not want the back
4057 end to output them -- but we do want it to see the bodies of
4058 these functions so that it can inline them as appropriate. */
4059 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4061 if (DECL_INTERFACE_KNOWN (fn))
4062 /* We've already made a decision as to how this function will
4063 be handled. */;
4064 else if (!at_eof)
4065 tentative_decl_linkage (fn);
4066 else
4067 import_export_decl (fn);
4069 /* If the user wants us to keep all inline functions, then mark
4070 this function as needed so that finish_file will make sure to
4071 output it later. Similarly, all dllexport'd functions must
4072 be emitted; there may be callers in other DLLs. */
4073 if ((flag_keep_inline_functions
4074 && DECL_DECLARED_INLINE_P (fn)
4075 && !DECL_REALLY_EXTERN (fn))
4076 || (flag_keep_inline_dllexport
4077 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4079 mark_needed (fn);
4080 DECL_EXTERNAL (fn) = 0;
4084 /* If this is a constructor or destructor body, we have to clone
4085 it. */
4086 if (maybe_clone_body (fn))
4088 /* We don't want to process FN again, so pretend we've written
4089 it out, even though we haven't. */
4090 TREE_ASM_WRITTEN (fn) = 1;
4091 /* If this is an instantiation of a constexpr function, keep
4092 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4093 if (!is_instantiation_of_constexpr (fn))
4094 DECL_SAVED_TREE (fn) = NULL_TREE;
4095 return false;
4098 /* There's no reason to do any of the work here if we're only doing
4099 semantic analysis; this code just generates RTL. */
4100 if (flag_syntax_only)
4101 return false;
4103 return true;
4106 void
4107 expand_or_defer_fn (tree fn)
4109 if (expand_or_defer_fn_1 (fn))
4111 function_depth++;
4113 /* Expand or defer, at the whim of the compilation unit manager. */
4114 cgraph_finalize_function (fn, function_depth > 1);
4115 emit_associated_thunks (fn);
4117 function_depth--;
4121 struct nrv_data
4123 tree var;
4124 tree result;
4125 hash_table <pointer_hash <tree_node> > visited;
4128 /* Helper function for walk_tree, used by finalize_nrv below. */
4130 static tree
4131 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4133 struct nrv_data *dp = (struct nrv_data *)data;
4134 tree_node **slot;
4136 /* No need to walk into types. There wouldn't be any need to walk into
4137 non-statements, except that we have to consider STMT_EXPRs. */
4138 if (TYPE_P (*tp))
4139 *walk_subtrees = 0;
4140 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4141 but differs from using NULL_TREE in that it indicates that we care
4142 about the value of the RESULT_DECL. */
4143 else if (TREE_CODE (*tp) == RETURN_EXPR)
4144 TREE_OPERAND (*tp, 0) = dp->result;
4145 /* Change all cleanups for the NRV to only run when an exception is
4146 thrown. */
4147 else if (TREE_CODE (*tp) == CLEANUP_STMT
4148 && CLEANUP_DECL (*tp) == dp->var)
4149 CLEANUP_EH_ONLY (*tp) = 1;
4150 /* Replace the DECL_EXPR for the NRV with an initialization of the
4151 RESULT_DECL, if needed. */
4152 else if (TREE_CODE (*tp) == DECL_EXPR
4153 && DECL_EXPR_DECL (*tp) == dp->var)
4155 tree init;
4156 if (DECL_INITIAL (dp->var)
4157 && DECL_INITIAL (dp->var) != error_mark_node)
4158 init = build2 (INIT_EXPR, void_type_node, dp->result,
4159 DECL_INITIAL (dp->var));
4160 else
4161 init = build_empty_stmt (EXPR_LOCATION (*tp));
4162 DECL_INITIAL (dp->var) = NULL_TREE;
4163 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4164 *tp = init;
4166 /* And replace all uses of the NRV with the RESULT_DECL. */
4167 else if (*tp == dp->var)
4168 *tp = dp->result;
4170 /* Avoid walking into the same tree more than once. Unfortunately, we
4171 can't just use walk_tree_without duplicates because it would only call
4172 us for the first occurrence of dp->var in the function body. */
4173 slot = dp->visited.find_slot (*tp, INSERT);
4174 if (*slot)
4175 *walk_subtrees = 0;
4176 else
4177 *slot = *tp;
4179 /* Keep iterating. */
4180 return NULL_TREE;
4183 /* Called from finish_function to implement the named return value
4184 optimization by overriding all the RETURN_EXPRs and pertinent
4185 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4186 RESULT_DECL for the function. */
4188 void
4189 finalize_nrv (tree *tp, tree var, tree result)
4191 struct nrv_data data;
4193 /* Copy name from VAR to RESULT. */
4194 DECL_NAME (result) = DECL_NAME (var);
4195 /* Don't forget that we take its address. */
4196 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4197 /* Finally set DECL_VALUE_EXPR to avoid assigning
4198 a stack slot at -O0 for the original var and debug info
4199 uses RESULT location for VAR. */
4200 SET_DECL_VALUE_EXPR (var, result);
4201 DECL_HAS_VALUE_EXPR_P (var) = 1;
4203 data.var = var;
4204 data.result = result;
4205 data.visited.create (37);
4206 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4207 data.visited.dispose ();
4210 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4212 bool
4213 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4214 bool need_copy_ctor, bool need_copy_assignment,
4215 bool need_dtor)
4217 int save_errorcount = errorcount;
4218 tree info, t;
4220 /* Always allocate 3 elements for simplicity. These are the
4221 function decls for the ctor, dtor, and assignment op.
4222 This layout is known to the three lang hooks,
4223 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4224 and cxx_omp_clause_assign_op. */
4225 info = make_tree_vec (3);
4226 CP_OMP_CLAUSE_INFO (c) = info;
4228 if (need_default_ctor || need_copy_ctor)
4230 if (need_default_ctor)
4231 t = get_default_ctor (type);
4232 else
4233 t = get_copy_ctor (type, tf_warning_or_error);
4235 if (t && !trivial_fn_p (t))
4236 TREE_VEC_ELT (info, 0) = t;
4239 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4240 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4242 if (need_copy_assignment)
4244 t = get_copy_assign (type);
4246 if (t && !trivial_fn_p (t))
4247 TREE_VEC_ELT (info, 2) = t;
4250 return errorcount != save_errorcount;
4253 /* Helper function for handle_omp_array_sections. Called recursively
4254 to handle multiple array-section-subscripts. C is the clause,
4255 T current expression (initially OMP_CLAUSE_DECL), which is either
4256 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4257 expression if specified, TREE_VALUE length expression if specified,
4258 TREE_CHAIN is what it has been specified after, or some decl.
4259 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4260 set to true if any of the array-section-subscript could have length
4261 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4262 first array-section-subscript which is known not to have length
4263 of one. Given say:
4264 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4265 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4266 all are or may have length of 1, array-section-subscript [:2] is the
4267 first one knonwn not to have length 1. For array-section-subscript
4268 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4269 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4270 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4271 case though, as some lengths could be zero. */
4273 static tree
4274 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4275 bool &maybe_zero_len, unsigned int &first_non_one)
4277 tree ret, low_bound, length, type;
4278 if (TREE_CODE (t) != TREE_LIST)
4280 if (error_operand_p (t))
4281 return error_mark_node;
4282 if (type_dependent_expression_p (t))
4283 return NULL_TREE;
4284 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4286 if (processing_template_decl)
4287 return NULL_TREE;
4288 if (DECL_P (t))
4289 error_at (OMP_CLAUSE_LOCATION (c),
4290 "%qD is not a variable in %qs clause", t,
4291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4292 else
4293 error_at (OMP_CLAUSE_LOCATION (c),
4294 "%qE is not a variable in %qs clause", t,
4295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4296 return error_mark_node;
4298 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4299 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4301 error_at (OMP_CLAUSE_LOCATION (c),
4302 "%qD is threadprivate variable in %qs clause", t,
4303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4304 return error_mark_node;
4306 t = convert_from_reference (t);
4307 return t;
4310 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4311 maybe_zero_len, first_non_one);
4312 if (ret == error_mark_node || ret == NULL_TREE)
4313 return ret;
4315 type = TREE_TYPE (ret);
4316 low_bound = TREE_PURPOSE (t);
4317 length = TREE_VALUE (t);
4318 if ((low_bound && type_dependent_expression_p (low_bound))
4319 || (length && type_dependent_expression_p (length)))
4320 return NULL_TREE;
4322 if (low_bound == error_mark_node || length == error_mark_node)
4323 return error_mark_node;
4325 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4327 error_at (OMP_CLAUSE_LOCATION (c),
4328 "low bound %qE of array section does not have integral type",
4329 low_bound);
4330 return error_mark_node;
4332 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4334 error_at (OMP_CLAUSE_LOCATION (c),
4335 "length %qE of array section does not have integral type",
4336 length);
4337 return error_mark_node;
4339 if (low_bound
4340 && TREE_CODE (low_bound) == INTEGER_CST
4341 && TYPE_PRECISION (TREE_TYPE (low_bound))
4342 > TYPE_PRECISION (sizetype))
4343 low_bound = fold_convert (sizetype, low_bound);
4344 if (length
4345 && TREE_CODE (length) == INTEGER_CST
4346 && TYPE_PRECISION (TREE_TYPE (length))
4347 > TYPE_PRECISION (sizetype))
4348 length = fold_convert (sizetype, length);
4349 if (low_bound == NULL_TREE)
4350 low_bound = integer_zero_node;
4352 if (length != NULL_TREE)
4354 if (!integer_nonzerop (length))
4355 maybe_zero_len = true;
4356 if (first_non_one == types.length ()
4357 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4358 first_non_one++;
4360 if (TREE_CODE (type) == ARRAY_TYPE)
4362 if (length == NULL_TREE
4363 && (TYPE_DOMAIN (type) == NULL_TREE
4364 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4366 error_at (OMP_CLAUSE_LOCATION (c),
4367 "for unknown bound array type length expression must "
4368 "be specified");
4369 return error_mark_node;
4371 if (TREE_CODE (low_bound) == INTEGER_CST
4372 && tree_int_cst_sgn (low_bound) == -1)
4374 error_at (OMP_CLAUSE_LOCATION (c),
4375 "negative low bound in array section in %qs clause",
4376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4377 return error_mark_node;
4379 if (length != NULL_TREE
4380 && TREE_CODE (length) == INTEGER_CST
4381 && tree_int_cst_sgn (length) == -1)
4383 error_at (OMP_CLAUSE_LOCATION (c),
4384 "negative length in array section in %qs clause",
4385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4386 return error_mark_node;
4388 if (TYPE_DOMAIN (type)
4389 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4390 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4391 == INTEGER_CST)
4393 tree size = size_binop (PLUS_EXPR,
4394 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4395 size_one_node);
4396 if (TREE_CODE (low_bound) == INTEGER_CST)
4398 if (tree_int_cst_lt (size, low_bound))
4400 error_at (OMP_CLAUSE_LOCATION (c),
4401 "low bound %qE above array section size "
4402 "in %qs clause", low_bound,
4403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4404 return error_mark_node;
4406 if (tree_int_cst_equal (size, low_bound))
4407 maybe_zero_len = true;
4408 else if (length == NULL_TREE
4409 && first_non_one == types.length ()
4410 && tree_int_cst_equal
4411 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4412 low_bound))
4413 first_non_one++;
4415 else if (length == NULL_TREE)
4417 maybe_zero_len = true;
4418 if (first_non_one == types.length ())
4419 first_non_one++;
4421 if (length && TREE_CODE (length) == INTEGER_CST)
4423 if (tree_int_cst_lt (size, length))
4425 error_at (OMP_CLAUSE_LOCATION (c),
4426 "length %qE above array section size "
4427 "in %qs clause", length,
4428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4429 return error_mark_node;
4431 if (TREE_CODE (low_bound) == INTEGER_CST)
4433 tree lbpluslen
4434 = size_binop (PLUS_EXPR,
4435 fold_convert (sizetype, low_bound),
4436 fold_convert (sizetype, length));
4437 if (TREE_CODE (lbpluslen) == INTEGER_CST
4438 && tree_int_cst_lt (size, lbpluslen))
4440 error_at (OMP_CLAUSE_LOCATION (c),
4441 "high bound %qE above array section size "
4442 "in %qs clause", lbpluslen,
4443 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4444 return error_mark_node;
4449 else if (length == NULL_TREE)
4451 maybe_zero_len = true;
4452 if (first_non_one == types.length ())
4453 first_non_one++;
4456 /* For [lb:] we will need to evaluate lb more than once. */
4457 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4459 tree lb = cp_save_expr (low_bound);
4460 if (lb != low_bound)
4462 TREE_PURPOSE (t) = lb;
4463 low_bound = lb;
4467 else if (TREE_CODE (type) == POINTER_TYPE)
4469 if (length == NULL_TREE)
4471 error_at (OMP_CLAUSE_LOCATION (c),
4472 "for pointer type length expression must be specified");
4473 return error_mark_node;
4475 /* If there is a pointer type anywhere but in the very first
4476 array-section-subscript, the array section can't be contiguous. */
4477 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4478 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4480 error_at (OMP_CLAUSE_LOCATION (c),
4481 "array section is not contiguous in %qs clause",
4482 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4483 return error_mark_node;
4486 else
4488 error_at (OMP_CLAUSE_LOCATION (c),
4489 "%qE does not have pointer or array type", ret);
4490 return error_mark_node;
4492 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4493 types.safe_push (TREE_TYPE (ret));
4494 /* We will need to evaluate lb more than once. */
4495 tree lb = cp_save_expr (low_bound);
4496 if (lb != low_bound)
4498 TREE_PURPOSE (t) = lb;
4499 low_bound = lb;
4501 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4502 return ret;
4505 /* Handle array sections for clause C. */
4507 static bool
4508 handle_omp_array_sections (tree c)
4510 bool maybe_zero_len = false;
4511 unsigned int first_non_one = 0;
4512 auto_vec<tree> types;
4513 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4514 maybe_zero_len, first_non_one);
4515 if (first == error_mark_node)
4516 return true;
4517 if (first == NULL_TREE)
4518 return false;
4519 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4521 tree t = OMP_CLAUSE_DECL (c);
4522 tree tem = NULL_TREE;
4523 if (processing_template_decl)
4524 return false;
4525 /* Need to evaluate side effects in the length expressions
4526 if any. */
4527 while (TREE_CODE (t) == TREE_LIST)
4529 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4531 if (tem == NULL_TREE)
4532 tem = TREE_VALUE (t);
4533 else
4534 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4535 TREE_VALUE (t), tem);
4537 t = TREE_CHAIN (t);
4539 if (tem)
4540 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4541 OMP_CLAUSE_DECL (c) = first;
4543 else
4545 unsigned int num = types.length (), i;
4546 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4547 tree condition = NULL_TREE;
4549 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4550 maybe_zero_len = true;
4551 if (processing_template_decl && maybe_zero_len)
4552 return false;
4554 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4555 t = TREE_CHAIN (t))
4557 tree low_bound = TREE_PURPOSE (t);
4558 tree length = TREE_VALUE (t);
4560 i--;
4561 if (low_bound
4562 && TREE_CODE (low_bound) == INTEGER_CST
4563 && TYPE_PRECISION (TREE_TYPE (low_bound))
4564 > TYPE_PRECISION (sizetype))
4565 low_bound = fold_convert (sizetype, low_bound);
4566 if (length
4567 && TREE_CODE (length) == INTEGER_CST
4568 && TYPE_PRECISION (TREE_TYPE (length))
4569 > TYPE_PRECISION (sizetype))
4570 length = fold_convert (sizetype, length);
4571 if (low_bound == NULL_TREE)
4572 low_bound = integer_zero_node;
4573 if (!maybe_zero_len && i > first_non_one)
4575 if (integer_nonzerop (low_bound))
4576 goto do_warn_noncontiguous;
4577 if (length != NULL_TREE
4578 && TREE_CODE (length) == INTEGER_CST
4579 && TYPE_DOMAIN (types[i])
4580 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4581 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4582 == INTEGER_CST)
4584 tree size;
4585 size = size_binop (PLUS_EXPR,
4586 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4587 size_one_node);
4588 if (!tree_int_cst_equal (length, size))
4590 do_warn_noncontiguous:
4591 error_at (OMP_CLAUSE_LOCATION (c),
4592 "array section is not contiguous in %qs "
4593 "clause",
4594 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4595 return true;
4598 if (!processing_template_decl
4599 && length != NULL_TREE
4600 && TREE_SIDE_EFFECTS (length))
4602 if (side_effects == NULL_TREE)
4603 side_effects = length;
4604 else
4605 side_effects = build2 (COMPOUND_EXPR,
4606 TREE_TYPE (side_effects),
4607 length, side_effects);
4610 else if (processing_template_decl)
4611 continue;
4612 else
4614 tree l;
4616 if (i > first_non_one && length && integer_nonzerop (length))
4617 continue;
4618 if (length)
4619 l = fold_convert (sizetype, length);
4620 else
4622 l = size_binop (PLUS_EXPR,
4623 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4624 size_one_node);
4625 l = size_binop (MINUS_EXPR, l,
4626 fold_convert (sizetype, low_bound));
4628 if (i > first_non_one)
4630 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4631 size_zero_node);
4632 if (condition == NULL_TREE)
4633 condition = l;
4634 else
4635 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4636 l, condition);
4638 else if (size == NULL_TREE)
4640 size = size_in_bytes (TREE_TYPE (types[i]));
4641 size = size_binop (MULT_EXPR, size, l);
4642 if (condition)
4643 size = fold_build3 (COND_EXPR, sizetype, condition,
4644 size, size_zero_node);
4646 else
4647 size = size_binop (MULT_EXPR, size, l);
4650 if (!processing_template_decl)
4652 if (side_effects)
4653 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4654 OMP_CLAUSE_DECL (c) = first;
4655 OMP_CLAUSE_SIZE (c) = size;
4656 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4657 return false;
4658 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4659 OMP_CLAUSE_MAP);
4660 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4661 if (!cxx_mark_addressable (t))
4662 return false;
4663 OMP_CLAUSE_DECL (c2) = t;
4664 t = build_fold_addr_expr (first);
4665 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4666 ptrdiff_type_node, t);
4667 tree ptr = OMP_CLAUSE_DECL (c2);
4668 ptr = convert_from_reference (ptr);
4669 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4670 ptr = build_fold_addr_expr (ptr);
4671 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4672 ptrdiff_type_node, t,
4673 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4674 ptrdiff_type_node, ptr));
4675 OMP_CLAUSE_SIZE (c2) = t;
4676 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4677 OMP_CLAUSE_CHAIN (c) = c2;
4678 ptr = OMP_CLAUSE_DECL (c2);
4679 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4680 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4682 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4683 OMP_CLAUSE_MAP);
4684 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4685 OMP_CLAUSE_DECL (c3) = ptr;
4686 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4687 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4688 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4689 OMP_CLAUSE_CHAIN (c2) = c3;
4693 return false;
4696 /* Return identifier to look up for omp declare reduction. */
4698 tree
4699 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4701 const char *p = NULL;
4702 const char *m = NULL;
4703 switch (reduction_code)
4705 case PLUS_EXPR:
4706 case MULT_EXPR:
4707 case MINUS_EXPR:
4708 case BIT_AND_EXPR:
4709 case BIT_XOR_EXPR:
4710 case BIT_IOR_EXPR:
4711 case TRUTH_ANDIF_EXPR:
4712 case TRUTH_ORIF_EXPR:
4713 reduction_id = ansi_opname (reduction_code);
4714 break;
4715 case MIN_EXPR:
4716 p = "min";
4717 break;
4718 case MAX_EXPR:
4719 p = "max";
4720 break;
4721 default:
4722 break;
4725 if (p == NULL)
4727 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4728 return error_mark_node;
4729 p = IDENTIFIER_POINTER (reduction_id);
4732 if (type != NULL_TREE)
4733 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4735 const char prefix[] = "omp declare reduction ";
4736 size_t lenp = sizeof (prefix);
4737 if (strncmp (p, prefix, lenp - 1) == 0)
4738 lenp = 1;
4739 size_t len = strlen (p);
4740 size_t lenm = m ? strlen (m) + 1 : 0;
4741 char *name = XALLOCAVEC (char, lenp + len + lenm);
4742 if (lenp > 1)
4743 memcpy (name, prefix, lenp - 1);
4744 memcpy (name + lenp - 1, p, len + 1);
4745 if (m)
4747 name[lenp + len - 1] = '~';
4748 memcpy (name + lenp + len, m, lenm);
4750 return get_identifier (name);
4753 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4754 FUNCTION_DECL or NULL_TREE if not found. */
4756 static tree
4757 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4758 vec<tree> *ambiguousp)
4760 tree orig_id = id;
4761 tree baselink = NULL_TREE;
4762 if (identifier_p (id))
4764 cp_id_kind idk;
4765 bool nonint_cst_expression_p;
4766 const char *error_msg;
4767 id = omp_reduction_id (ERROR_MARK, id, type);
4768 tree decl = lookup_name (id);
4769 if (decl == NULL_TREE)
4770 decl = error_mark_node;
4771 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4772 &nonint_cst_expression_p, false, true, false,
4773 false, &error_msg, loc);
4774 if (idk == CP_ID_KIND_UNQUALIFIED
4775 && identifier_p (id))
4777 vec<tree, va_gc> *args = NULL;
4778 vec_safe_push (args, build_reference_type (type));
4779 id = perform_koenig_lookup (id, args, tf_none);
4782 else if (TREE_CODE (id) == SCOPE_REF)
4783 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4784 omp_reduction_id (ERROR_MARK,
4785 TREE_OPERAND (id, 1),
4786 type),
4787 false, false);
4788 tree fns = id;
4789 if (id && is_overloaded_fn (id))
4790 id = get_fns (id);
4791 for (; id; id = OVL_NEXT (id))
4793 tree fndecl = OVL_CURRENT (id);
4794 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4796 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4797 if (same_type_p (TREE_TYPE (argtype), type))
4798 break;
4801 if (id && BASELINK_P (fns))
4803 if (baselinkp)
4804 *baselinkp = fns;
4805 else
4806 baselink = fns;
4808 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4810 vec<tree> ambiguous = vNULL;
4811 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4812 unsigned int ix;
4813 if (ambiguousp == NULL)
4814 ambiguousp = &ambiguous;
4815 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4817 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4818 baselinkp ? baselinkp : &baselink,
4819 ambiguousp);
4820 if (id == NULL_TREE)
4821 continue;
4822 if (!ambiguousp->is_empty ())
4823 ambiguousp->safe_push (id);
4824 else if (ret != NULL_TREE)
4826 ambiguousp->safe_push (ret);
4827 ambiguousp->safe_push (id);
4828 ret = NULL_TREE;
4830 else
4831 ret = id;
4833 if (ambiguousp != &ambiguous)
4834 return ret;
4835 if (!ambiguous.is_empty ())
4837 const char *str = _("candidates are:");
4838 unsigned int idx;
4839 tree udr;
4840 error_at (loc, "user defined reduction lookup is ambiguous");
4841 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4843 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4844 if (idx == 0)
4845 str = get_spaces (str);
4847 ambiguous.release ();
4848 ret = error_mark_node;
4849 baselink = NULL_TREE;
4851 id = ret;
4853 if (id && baselink)
4854 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4855 id, id, tf_warning_or_error);
4856 return id;
4859 /* Helper function for cp_parser_omp_declare_reduction_exprs
4860 and tsubst_omp_udr.
4861 Remove CLEANUP_STMT for data (omp_priv variable).
4862 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4863 DECL_EXPR. */
4865 tree
4866 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4868 if (TYPE_P (*tp))
4869 *walk_subtrees = 0;
4870 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4871 *tp = CLEANUP_BODY (*tp);
4872 else if (TREE_CODE (*tp) == DECL_EXPR)
4874 tree decl = DECL_EXPR_DECL (*tp);
4875 if (!processing_template_decl
4876 && decl == (tree) data
4877 && DECL_INITIAL (decl)
4878 && DECL_INITIAL (decl) != error_mark_node)
4880 tree list = NULL_TREE;
4881 append_to_statement_list_force (*tp, &list);
4882 tree init_expr = build2 (INIT_EXPR, void_type_node,
4883 decl, DECL_INITIAL (decl));
4884 DECL_INITIAL (decl) = NULL_TREE;
4885 append_to_statement_list_force (init_expr, &list);
4886 *tp = list;
4889 return NULL_TREE;
4892 /* Data passed from cp_check_omp_declare_reduction to
4893 cp_check_omp_declare_reduction_r. */
4895 struct cp_check_omp_declare_reduction_data
4897 location_t loc;
4898 tree stmts[7];
4899 bool combiner_p;
4902 /* Helper function for cp_check_omp_declare_reduction, called via
4903 cp_walk_tree. */
4905 static tree
4906 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4908 struct cp_check_omp_declare_reduction_data *udr_data
4909 = (struct cp_check_omp_declare_reduction_data *) data;
4910 if (SSA_VAR_P (*tp)
4911 && !DECL_ARTIFICIAL (*tp)
4912 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4913 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4915 location_t loc = udr_data->loc;
4916 if (udr_data->combiner_p)
4917 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4918 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4919 *tp);
4920 else
4921 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4922 "to variable %qD which is not %<omp_priv%> nor "
4923 "%<omp_orig%>",
4924 *tp);
4925 return *tp;
4927 return NULL_TREE;
4930 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4932 void
4933 cp_check_omp_declare_reduction (tree udr)
4935 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4936 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4937 type = TREE_TYPE (type);
4938 int i;
4939 location_t loc = DECL_SOURCE_LOCATION (udr);
4941 if (type == error_mark_node)
4942 return;
4943 if (ARITHMETIC_TYPE_P (type))
4945 static enum tree_code predef_codes[]
4946 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4947 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4948 for (i = 0; i < 8; i++)
4950 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4951 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4952 const char *n2 = IDENTIFIER_POINTER (id);
4953 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4954 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4955 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4956 break;
4959 if (i == 8
4960 && TREE_CODE (type) != COMPLEX_EXPR)
4962 const char prefix_minmax[] = "omp declare reduction m";
4963 size_t prefix_size = sizeof (prefix_minmax) - 1;
4964 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4965 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4966 prefix_minmax, prefix_size) == 0
4967 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4968 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4969 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4970 i = 0;
4972 if (i < 8)
4974 error_at (loc, "predeclared arithmetic type %qT in "
4975 "%<#pragma omp declare reduction%>", type);
4976 return;
4979 else if (TREE_CODE (type) == FUNCTION_TYPE
4980 || TREE_CODE (type) == METHOD_TYPE
4981 || TREE_CODE (type) == ARRAY_TYPE)
4983 error_at (loc, "function or array type %qT in "
4984 "%<#pragma omp declare reduction%>", type);
4985 return;
4987 else if (TREE_CODE (type) == REFERENCE_TYPE)
4989 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4990 type);
4991 return;
4993 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4995 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4996 "%<#pragma omp declare reduction%>", type);
4997 return;
5000 tree body = DECL_SAVED_TREE (udr);
5001 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5002 return;
5004 tree_stmt_iterator tsi;
5005 struct cp_check_omp_declare_reduction_data data;
5006 memset (data.stmts, 0, sizeof data.stmts);
5007 for (i = 0, tsi = tsi_start (body);
5008 i < 7 && !tsi_end_p (tsi);
5009 i++, tsi_next (&tsi))
5010 data.stmts[i] = tsi_stmt (tsi);
5011 data.loc = loc;
5012 gcc_assert (tsi_end_p (tsi));
5013 if (i >= 3)
5015 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5016 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5017 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5018 return;
5019 data.combiner_p = true;
5020 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5021 &data, NULL))
5022 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5024 if (i >= 6)
5026 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5027 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5028 data.combiner_p = false;
5029 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5030 &data, NULL)
5031 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5032 cp_check_omp_declare_reduction_r, &data, NULL))
5033 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5034 if (i == 7)
5035 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5039 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5040 an inline call. But, remap
5041 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5042 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5044 static tree
5045 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5046 tree decl, tree placeholder)
5048 copy_body_data id;
5049 struct pointer_map_t *decl_map = pointer_map_create ();
5051 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
5052 *pointer_map_insert (decl_map, omp_decl2) = decl;
5053 memset (&id, 0, sizeof (id));
5054 id.src_fn = DECL_CONTEXT (omp_decl1);
5055 id.dst_fn = current_function_decl;
5056 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5057 id.decl_map = decl_map;
5059 id.copy_decl = copy_decl_no_change;
5060 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5061 id.transform_new_cfg = true;
5062 id.transform_return_to_modify = false;
5063 id.transform_lang_insert_block = NULL;
5064 id.eh_lp_nr = 0;
5065 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5066 pointer_map_destroy (decl_map);
5067 return stmt;
5070 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5071 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5073 static tree
5074 find_omp_placeholder_r (tree *tp, int *, void *data)
5076 if (*tp == (tree) data)
5077 return *tp;
5078 return NULL_TREE;
5081 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5082 Return true if there is some error and the clause should be removed. */
5084 static bool
5085 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5087 tree t = OMP_CLAUSE_DECL (c);
5088 bool predefined = false;
5089 tree type = TREE_TYPE (t);
5090 if (TREE_CODE (type) == REFERENCE_TYPE)
5091 type = TREE_TYPE (type);
5092 if (type == error_mark_node)
5093 return true;
5094 else if (ARITHMETIC_TYPE_P (type))
5095 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5097 case PLUS_EXPR:
5098 case MULT_EXPR:
5099 case MINUS_EXPR:
5100 predefined = true;
5101 break;
5102 case MIN_EXPR:
5103 case MAX_EXPR:
5104 if (TREE_CODE (type) == COMPLEX_TYPE)
5105 break;
5106 predefined = true;
5107 break;
5108 case BIT_AND_EXPR:
5109 case BIT_IOR_EXPR:
5110 case BIT_XOR_EXPR:
5111 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5112 break;
5113 predefined = true;
5114 break;
5115 case TRUTH_ANDIF_EXPR:
5116 case TRUTH_ORIF_EXPR:
5117 if (FLOAT_TYPE_P (type))
5118 break;
5119 predefined = true;
5120 break;
5121 default:
5122 break;
5124 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5126 error ("%qE has invalid type for %<reduction%>", t);
5127 return true;
5129 else if (!processing_template_decl)
5131 t = require_complete_type (t);
5132 if (t == error_mark_node)
5133 return true;
5134 OMP_CLAUSE_DECL (c) = t;
5137 if (predefined)
5139 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5140 return false;
5142 else if (processing_template_decl)
5143 return false;
5145 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5147 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5148 if (TREE_CODE (type) == REFERENCE_TYPE)
5149 type = TREE_TYPE (type);
5150 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5151 if (id == NULL_TREE)
5152 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5153 NULL_TREE, NULL_TREE);
5154 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5155 if (id)
5157 if (id == error_mark_node)
5158 return true;
5159 id = OVL_CURRENT (id);
5160 mark_used (id);
5161 tree body = DECL_SAVED_TREE (id);
5162 if (TREE_CODE (body) == STATEMENT_LIST)
5164 tree_stmt_iterator tsi;
5165 tree placeholder = NULL_TREE;
5166 int i;
5167 tree stmts[7];
5168 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5169 atype = TREE_TYPE (atype);
5170 bool need_static_cast = !same_type_p (type, atype);
5171 memset (stmts, 0, sizeof stmts);
5172 for (i = 0, tsi = tsi_start (body);
5173 i < 7 && !tsi_end_p (tsi);
5174 i++, tsi_next (&tsi))
5175 stmts[i] = tsi_stmt (tsi);
5176 gcc_assert (tsi_end_p (tsi));
5178 if (i >= 3)
5180 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5181 && TREE_CODE (stmts[1]) == DECL_EXPR);
5182 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5183 DECL_ARTIFICIAL (placeholder) = 1;
5184 DECL_IGNORED_P (placeholder) = 1;
5185 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5186 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5187 cxx_mark_addressable (placeholder);
5188 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5189 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5190 != REFERENCE_TYPE)
5191 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5192 tree omp_out = placeholder;
5193 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5194 if (need_static_cast)
5196 tree rtype = build_reference_type (atype);
5197 omp_out = build_static_cast (rtype, omp_out,
5198 tf_warning_or_error);
5199 omp_in = build_static_cast (rtype, omp_in,
5200 tf_warning_or_error);
5201 if (omp_out == error_mark_node || omp_in == error_mark_node)
5202 return true;
5203 omp_out = convert_from_reference (omp_out);
5204 omp_in = convert_from_reference (omp_in);
5206 OMP_CLAUSE_REDUCTION_MERGE (c)
5207 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5208 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5210 if (i >= 6)
5212 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5213 && TREE_CODE (stmts[4]) == DECL_EXPR);
5214 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5215 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5216 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5217 cxx_mark_addressable (placeholder);
5218 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5219 tree omp_orig = placeholder;
5220 if (need_static_cast)
5222 if (i == 7)
5224 error_at (OMP_CLAUSE_LOCATION (c),
5225 "user defined reduction with constructor "
5226 "initializer for base class %qT", atype);
5227 return true;
5229 tree rtype = build_reference_type (atype);
5230 omp_priv = build_static_cast (rtype, omp_priv,
5231 tf_warning_or_error);
5232 omp_orig = build_static_cast (rtype, omp_orig,
5233 tf_warning_or_error);
5234 if (omp_priv == error_mark_node
5235 || omp_orig == error_mark_node)
5236 return true;
5237 omp_priv = convert_from_reference (omp_priv);
5238 omp_orig = convert_from_reference (omp_orig);
5240 if (i == 6)
5241 *need_default_ctor = true;
5242 OMP_CLAUSE_REDUCTION_INIT (c)
5243 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5244 DECL_EXPR_DECL (stmts[3]),
5245 omp_priv, omp_orig);
5246 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5247 find_omp_placeholder_r, placeholder, NULL))
5248 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5250 else if (i >= 3)
5252 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5253 *need_default_ctor = true;
5254 else
5256 tree init;
5257 tree v = convert_from_reference (t);
5258 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5259 init = build_constructor (TREE_TYPE (v), NULL);
5260 else
5261 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5262 OMP_CLAUSE_REDUCTION_INIT (c)
5263 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5268 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5269 *need_dtor = true;
5270 else
5272 error ("user defined reduction not found for %qD", t);
5273 return true;
5275 return false;
5278 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5279 Remove any elements from the list that are invalid. */
5281 tree
5282 finish_omp_clauses (tree clauses)
5284 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5285 bitmap_head aligned_head;
5286 tree c, t, *pc = &clauses;
5287 bool branch_seen = false;
5288 bool copyprivate_seen = false;
5290 bitmap_obstack_initialize (NULL);
5291 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5292 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5293 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5294 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5296 for (pc = &clauses, c = clauses; c ; c = *pc)
5298 bool remove = false;
5300 switch (OMP_CLAUSE_CODE (c))
5302 case OMP_CLAUSE_SHARED:
5303 goto check_dup_generic;
5304 case OMP_CLAUSE_PRIVATE:
5305 goto check_dup_generic;
5306 case OMP_CLAUSE_REDUCTION:
5307 goto check_dup_generic;
5308 case OMP_CLAUSE_COPYPRIVATE:
5309 copyprivate_seen = true;
5310 goto check_dup_generic;
5311 case OMP_CLAUSE_COPYIN:
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_LINEAR:
5314 t = OMP_CLAUSE_DECL (c);
5315 if (!type_dependent_expression_p (t)
5316 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5317 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5319 error ("linear clause applied to non-integral non-pointer "
5320 "variable with %qT type", TREE_TYPE (t));
5321 remove = true;
5322 break;
5324 t = OMP_CLAUSE_LINEAR_STEP (c);
5325 if (t == NULL_TREE)
5326 t = integer_one_node;
5327 if (t == error_mark_node)
5329 remove = true;
5330 break;
5332 else if (!type_dependent_expression_p (t)
5333 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5335 error ("linear step expression must be integral");
5336 remove = true;
5337 break;
5339 else
5341 t = mark_rvalue_use (t);
5342 if (!processing_template_decl)
5344 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5345 t = maybe_constant_value (t);
5346 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5347 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5348 == POINTER_TYPE)
5350 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5351 OMP_CLAUSE_DECL (c), t);
5352 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5353 MINUS_EXPR, sizetype, t,
5354 OMP_CLAUSE_DECL (c));
5355 if (t == error_mark_node)
5357 remove = true;
5358 break;
5362 OMP_CLAUSE_LINEAR_STEP (c) = t;
5364 goto check_dup_generic;
5365 check_dup_generic:
5366 t = OMP_CLAUSE_DECL (c);
5367 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5369 if (processing_template_decl)
5370 break;
5371 if (DECL_P (t))
5372 error ("%qD is not a variable in clause %qs", t,
5373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5374 else
5375 error ("%qE is not a variable in clause %qs", t,
5376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5377 remove = true;
5379 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5380 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5381 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5383 error ("%qD appears more than once in data clauses", t);
5384 remove = true;
5386 else
5387 bitmap_set_bit (&generic_head, DECL_UID (t));
5388 break;
5390 case OMP_CLAUSE_FIRSTPRIVATE:
5391 t = OMP_CLAUSE_DECL (c);
5392 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5394 if (processing_template_decl)
5395 break;
5396 if (DECL_P (t))
5397 error ("%qD is not a variable in clause %<firstprivate%>", t);
5398 else
5399 error ("%qE is not a variable in clause %<firstprivate%>", t);
5400 remove = true;
5402 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5403 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5405 error ("%qD appears more than once in data clauses", t);
5406 remove = true;
5408 else
5409 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5410 break;
5412 case OMP_CLAUSE_LASTPRIVATE:
5413 t = OMP_CLAUSE_DECL (c);
5414 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5416 if (processing_template_decl)
5417 break;
5418 if (DECL_P (t))
5419 error ("%qD is not a variable in clause %<lastprivate%>", t);
5420 else
5421 error ("%qE is not a variable in clause %<lastprivate%>", t);
5422 remove = true;
5424 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5425 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5427 error ("%qD appears more than once in data clauses", t);
5428 remove = true;
5430 else
5431 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5432 break;
5434 case OMP_CLAUSE_IF:
5435 t = OMP_CLAUSE_IF_EXPR (c);
5436 t = maybe_convert_cond (t);
5437 if (t == error_mark_node)
5438 remove = true;
5439 else if (!processing_template_decl)
5440 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5441 OMP_CLAUSE_IF_EXPR (c) = t;
5442 break;
5444 case OMP_CLAUSE_FINAL:
5445 t = OMP_CLAUSE_FINAL_EXPR (c);
5446 t = maybe_convert_cond (t);
5447 if (t == error_mark_node)
5448 remove = true;
5449 else if (!processing_template_decl)
5450 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5451 OMP_CLAUSE_FINAL_EXPR (c) = t;
5452 break;
5454 case OMP_CLAUSE_NUM_THREADS:
5455 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5456 if (t == error_mark_node)
5457 remove = true;
5458 else if (!type_dependent_expression_p (t)
5459 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5461 error ("num_threads expression must be integral");
5462 remove = true;
5464 else
5466 t = mark_rvalue_use (t);
5467 if (!processing_template_decl)
5468 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5469 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5471 break;
5473 case OMP_CLAUSE_SCHEDULE:
5474 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5475 if (t == NULL)
5477 else if (t == error_mark_node)
5478 remove = true;
5479 else if (!type_dependent_expression_p (t)
5480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5482 error ("schedule chunk size expression must be integral");
5483 remove = true;
5485 else
5487 t = mark_rvalue_use (t);
5488 if (!processing_template_decl)
5489 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5490 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5492 break;
5494 case OMP_CLAUSE_SIMDLEN:
5495 case OMP_CLAUSE_SAFELEN:
5496 t = OMP_CLAUSE_OPERAND (c, 0);
5497 if (t == error_mark_node)
5498 remove = true;
5499 else if (!type_dependent_expression_p (t)
5500 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5502 error ("%qs length expression must be integral",
5503 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5504 remove = true;
5506 else
5508 t = mark_rvalue_use (t);
5509 t = maybe_constant_value (t);
5510 if (!processing_template_decl)
5512 if (TREE_CODE (t) != INTEGER_CST
5513 || tree_int_cst_sgn (t) != 1)
5515 error ("%qs length expression must be positive constant"
5516 " integer expression",
5517 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5518 remove = true;
5521 OMP_CLAUSE_OPERAND (c, 0) = t;
5523 break;
5525 case OMP_CLAUSE_NUM_TEAMS:
5526 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5527 if (t == error_mark_node)
5528 remove = true;
5529 else if (!type_dependent_expression_p (t)
5530 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5532 error ("%<num_teams%> expression must be integral");
5533 remove = true;
5535 else
5537 t = mark_rvalue_use (t);
5538 if (!processing_template_decl)
5539 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5540 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5542 break;
5544 case OMP_CLAUSE_THREAD_LIMIT:
5545 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5546 if (t == error_mark_node)
5547 remove = true;
5548 else if (!type_dependent_expression_p (t)
5549 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5551 error ("%<thread_limit%> expression must be integral");
5552 remove = true;
5554 else
5556 t = mark_rvalue_use (t);
5557 if (!processing_template_decl)
5558 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5559 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5561 break;
5563 case OMP_CLAUSE_DEVICE:
5564 t = OMP_CLAUSE_DEVICE_ID (c);
5565 if (t == error_mark_node)
5566 remove = true;
5567 else if (!type_dependent_expression_p (t)
5568 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5570 error ("%<device%> id must be integral");
5571 remove = true;
5573 else
5575 t = mark_rvalue_use (t);
5576 if (!processing_template_decl)
5577 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5578 OMP_CLAUSE_DEVICE_ID (c) = t;
5580 break;
5582 case OMP_CLAUSE_DIST_SCHEDULE:
5583 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5584 if (t == NULL)
5586 else if (t == error_mark_node)
5587 remove = true;
5588 else if (!type_dependent_expression_p (t)
5589 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5591 error ("%<dist_schedule%> chunk size expression must be "
5592 "integral");
5593 remove = true;
5595 else
5597 t = mark_rvalue_use (t);
5598 if (!processing_template_decl)
5599 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5600 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5602 break;
5604 case OMP_CLAUSE_ALIGNED:
5605 t = OMP_CLAUSE_DECL (c);
5606 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5608 if (processing_template_decl)
5609 break;
5610 if (DECL_P (t))
5611 error ("%qD is not a variable in %<aligned%> clause", t);
5612 else
5613 error ("%qE is not a variable in %<aligned%> clause", t);
5614 remove = true;
5616 else if (!type_dependent_expression_p (t)
5617 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5618 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5619 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5620 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5621 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5622 != ARRAY_TYPE))))
5624 error_at (OMP_CLAUSE_LOCATION (c),
5625 "%qE in %<aligned%> clause is neither a pointer nor "
5626 "an array nor a reference to pointer or array", t);
5627 remove = true;
5629 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5631 error ("%qD appears more than once in %<aligned%> clauses", t);
5632 remove = true;
5634 else
5635 bitmap_set_bit (&aligned_head, DECL_UID (t));
5636 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5637 if (t == error_mark_node)
5638 remove = true;
5639 else if (t == NULL_TREE)
5640 break;
5641 else if (!type_dependent_expression_p (t)
5642 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5644 error ("%<aligned%> clause alignment expression must "
5645 "be integral");
5646 remove = true;
5648 else
5650 t = mark_rvalue_use (t);
5651 t = maybe_constant_value (t);
5652 if (!processing_template_decl)
5654 if (TREE_CODE (t) != INTEGER_CST
5655 || tree_int_cst_sgn (t) != 1)
5657 error ("%<aligned%> clause alignment expression must be "
5658 "positive constant integer expression");
5659 remove = true;
5662 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5664 break;
5666 case OMP_CLAUSE_DEPEND:
5667 t = OMP_CLAUSE_DECL (c);
5668 if (TREE_CODE (t) == TREE_LIST)
5670 if (handle_omp_array_sections (c))
5671 remove = true;
5672 break;
5674 if (t == error_mark_node)
5675 remove = true;
5676 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5678 if (processing_template_decl)
5679 break;
5680 if (DECL_P (t))
5681 error ("%qD is not a variable in %<depend%> clause", t);
5682 else
5683 error ("%qE is not a variable in %<depend%> clause", t);
5684 remove = true;
5686 else if (!processing_template_decl
5687 && !cxx_mark_addressable (t))
5688 remove = true;
5689 break;
5691 case OMP_CLAUSE_MAP:
5692 case OMP_CLAUSE_TO:
5693 case OMP_CLAUSE_FROM:
5694 t = OMP_CLAUSE_DECL (c);
5695 if (TREE_CODE (t) == TREE_LIST)
5697 if (handle_omp_array_sections (c))
5698 remove = true;
5699 else
5701 t = OMP_CLAUSE_DECL (c);
5702 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5704 error_at (OMP_CLAUSE_LOCATION (c),
5705 "array section does not have mappable type "
5706 "in %qs clause",
5707 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5708 remove = true;
5711 break;
5713 if (t == error_mark_node)
5714 remove = true;
5715 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5717 if (processing_template_decl)
5718 break;
5719 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5720 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5721 break;
5722 if (DECL_P (t))
5723 error ("%qD is not a variable in %qs clause", t,
5724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5725 else
5726 error ("%qE is not a variable in %qs clause", t,
5727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5728 remove = true;
5730 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5732 error ("%qD is threadprivate variable in %qs clause", t,
5733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5734 remove = true;
5736 else if (!processing_template_decl
5737 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5738 && !cxx_mark_addressable (t))
5739 remove = true;
5740 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5741 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5742 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5743 == REFERENCE_TYPE)
5744 ? TREE_TYPE (TREE_TYPE (t))
5745 : TREE_TYPE (t)))
5747 error_at (OMP_CLAUSE_LOCATION (c),
5748 "%qD does not have a mappable type in %qs clause", t,
5749 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5750 remove = true;
5752 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5754 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5755 error ("%qD appears more than once in motion clauses", t);
5756 else
5757 error ("%qD appears more than once in map clauses", t);
5758 remove = true;
5760 else
5761 bitmap_set_bit (&generic_head, DECL_UID (t));
5762 break;
5764 case OMP_CLAUSE_UNIFORM:
5765 t = OMP_CLAUSE_DECL (c);
5766 if (TREE_CODE (t) != PARM_DECL)
5768 if (processing_template_decl)
5769 break;
5770 if (DECL_P (t))
5771 error ("%qD is not an argument in %<uniform%> clause", t);
5772 else
5773 error ("%qE is not an argument in %<uniform%> clause", t);
5774 remove = true;
5775 break;
5777 goto check_dup_generic;
5779 case OMP_CLAUSE_NOWAIT:
5780 case OMP_CLAUSE_ORDERED:
5781 case OMP_CLAUSE_DEFAULT:
5782 case OMP_CLAUSE_UNTIED:
5783 case OMP_CLAUSE_COLLAPSE:
5784 case OMP_CLAUSE_MERGEABLE:
5785 case OMP_CLAUSE_PARALLEL:
5786 case OMP_CLAUSE_FOR:
5787 case OMP_CLAUSE_SECTIONS:
5788 case OMP_CLAUSE_TASKGROUP:
5789 case OMP_CLAUSE_PROC_BIND:
5790 break;
5792 case OMP_CLAUSE_INBRANCH:
5793 case OMP_CLAUSE_NOTINBRANCH:
5794 if (branch_seen)
5796 error ("%<inbranch%> clause is incompatible with "
5797 "%<notinbranch%>");
5798 remove = true;
5800 branch_seen = true;
5801 break;
5803 default:
5804 gcc_unreachable ();
5807 if (remove)
5808 *pc = OMP_CLAUSE_CHAIN (c);
5809 else
5810 pc = &OMP_CLAUSE_CHAIN (c);
5813 for (pc = &clauses, c = clauses; c ; c = *pc)
5815 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5816 bool remove = false;
5817 bool need_complete_non_reference = false;
5818 bool need_default_ctor = false;
5819 bool need_copy_ctor = false;
5820 bool need_copy_assignment = false;
5821 bool need_implicitly_determined = false;
5822 bool need_dtor = false;
5823 tree type, inner_type;
5825 switch (c_kind)
5827 case OMP_CLAUSE_SHARED:
5828 need_implicitly_determined = true;
5829 break;
5830 case OMP_CLAUSE_PRIVATE:
5831 need_complete_non_reference = true;
5832 need_default_ctor = true;
5833 need_dtor = true;
5834 need_implicitly_determined = true;
5835 break;
5836 case OMP_CLAUSE_FIRSTPRIVATE:
5837 need_complete_non_reference = true;
5838 need_copy_ctor = true;
5839 need_dtor = true;
5840 need_implicitly_determined = true;
5841 break;
5842 case OMP_CLAUSE_LASTPRIVATE:
5843 need_complete_non_reference = true;
5844 need_copy_assignment = true;
5845 need_implicitly_determined = true;
5846 break;
5847 case OMP_CLAUSE_REDUCTION:
5848 need_implicitly_determined = true;
5849 break;
5850 case OMP_CLAUSE_COPYPRIVATE:
5851 need_copy_assignment = true;
5852 break;
5853 case OMP_CLAUSE_COPYIN:
5854 need_copy_assignment = true;
5855 break;
5856 case OMP_CLAUSE_NOWAIT:
5857 if (copyprivate_seen)
5859 error_at (OMP_CLAUSE_LOCATION (c),
5860 "%<nowait%> clause must not be used together "
5861 "with %<copyprivate%>");
5862 *pc = OMP_CLAUSE_CHAIN (c);
5863 continue;
5865 /* FALLTHRU */
5866 default:
5867 pc = &OMP_CLAUSE_CHAIN (c);
5868 continue;
5871 t = OMP_CLAUSE_DECL (c);
5872 if (processing_template_decl
5873 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5875 pc = &OMP_CLAUSE_CHAIN (c);
5876 continue;
5879 switch (c_kind)
5881 case OMP_CLAUSE_LASTPRIVATE:
5882 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5884 need_default_ctor = true;
5885 need_dtor = true;
5887 break;
5889 case OMP_CLAUSE_REDUCTION:
5890 if (finish_omp_reduction_clause (c, &need_default_ctor,
5891 &need_dtor))
5892 remove = true;
5893 else
5894 t = OMP_CLAUSE_DECL (c);
5895 break;
5897 case OMP_CLAUSE_COPYIN:
5898 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5900 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5901 remove = true;
5903 break;
5905 default:
5906 break;
5909 if (need_complete_non_reference || need_copy_assignment)
5911 t = require_complete_type (t);
5912 if (t == error_mark_node)
5913 remove = true;
5914 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5915 && need_complete_non_reference)
5917 error ("%qE has reference type for %qs", t,
5918 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5919 remove = true;
5922 if (need_implicitly_determined)
5924 const char *share_name = NULL;
5926 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5927 share_name = "threadprivate";
5928 else switch (cxx_omp_predetermined_sharing (t))
5930 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5931 break;
5932 case OMP_CLAUSE_DEFAULT_SHARED:
5933 /* const vars may be specified in firstprivate clause. */
5934 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5935 && cxx_omp_const_qual_no_mutable (t))
5936 break;
5937 share_name = "shared";
5938 break;
5939 case OMP_CLAUSE_DEFAULT_PRIVATE:
5940 share_name = "private";
5941 break;
5942 default:
5943 gcc_unreachable ();
5945 if (share_name)
5947 error ("%qE is predetermined %qs for %qs",
5948 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5949 remove = true;
5953 /* We're interested in the base element, not arrays. */
5954 inner_type = type = TREE_TYPE (t);
5955 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5956 inner_type = TREE_TYPE (inner_type);
5958 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5959 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5960 inner_type = TREE_TYPE (inner_type);
5962 /* Check for special function availability by building a call to one.
5963 Save the results, because later we won't be in the right context
5964 for making these queries. */
5965 if (CLASS_TYPE_P (inner_type)
5966 && COMPLETE_TYPE_P (inner_type)
5967 && (need_default_ctor || need_copy_ctor
5968 || need_copy_assignment || need_dtor)
5969 && !type_dependent_expression_p (t)
5970 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5971 need_copy_ctor, need_copy_assignment,
5972 need_dtor))
5973 remove = true;
5975 if (remove)
5976 *pc = OMP_CLAUSE_CHAIN (c);
5977 else
5978 pc = &OMP_CLAUSE_CHAIN (c);
5981 bitmap_obstack_release (NULL);
5982 return clauses;
5985 /* For all variables in the tree_list VARS, mark them as thread local. */
5987 void
5988 finish_omp_threadprivate (tree vars)
5990 tree t;
5992 /* Mark every variable in VARS to be assigned thread local storage. */
5993 for (t = vars; t; t = TREE_CHAIN (t))
5995 tree v = TREE_PURPOSE (t);
5997 if (error_operand_p (v))
5999 else if (!VAR_P (v))
6000 error ("%<threadprivate%> %qD is not file, namespace "
6001 "or block scope variable", v);
6002 /* If V had already been marked threadprivate, it doesn't matter
6003 whether it had been used prior to this point. */
6004 else if (TREE_USED (v)
6005 && (DECL_LANG_SPECIFIC (v) == NULL
6006 || !CP_DECL_THREADPRIVATE_P (v)))
6007 error ("%qE declared %<threadprivate%> after first use", v);
6008 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6009 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6010 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6011 error ("%<threadprivate%> %qE has incomplete type", v);
6012 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6013 && CP_DECL_CONTEXT (v) != current_class_type)
6014 error ("%<threadprivate%> %qE directive not "
6015 "in %qT definition", v, CP_DECL_CONTEXT (v));
6016 else
6018 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6019 if (DECL_LANG_SPECIFIC (v) == NULL)
6021 retrofit_lang_decl (v);
6023 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6024 after the allocation of the lang_decl structure. */
6025 if (DECL_DISCRIMINATOR_P (v))
6026 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6029 if (! DECL_THREAD_LOCAL_P (v))
6031 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
6032 /* If rtl has been already set for this var, call
6033 make_decl_rtl once again, so that encode_section_info
6034 has a chance to look at the new decl flags. */
6035 if (DECL_RTL_SET_P (v))
6036 make_decl_rtl (v);
6038 CP_DECL_THREADPRIVATE_P (v) = 1;
6043 /* Build an OpenMP structured block. */
6045 tree
6046 begin_omp_structured_block (void)
6048 return do_pushlevel (sk_omp);
6051 tree
6052 finish_omp_structured_block (tree block)
6054 return do_poplevel (block);
6057 /* Similarly, except force the retention of the BLOCK. */
6059 tree
6060 begin_omp_parallel (void)
6062 keep_next_level (true);
6063 return begin_omp_structured_block ();
6066 tree
6067 finish_omp_parallel (tree clauses, tree body)
6069 tree stmt;
6071 body = finish_omp_structured_block (body);
6073 stmt = make_node (OMP_PARALLEL);
6074 TREE_TYPE (stmt) = void_type_node;
6075 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6076 OMP_PARALLEL_BODY (stmt) = body;
6078 return add_stmt (stmt);
6081 tree
6082 begin_omp_task (void)
6084 keep_next_level (true);
6085 return begin_omp_structured_block ();
6088 tree
6089 finish_omp_task (tree clauses, tree body)
6091 tree stmt;
6093 body = finish_omp_structured_block (body);
6095 stmt = make_node (OMP_TASK);
6096 TREE_TYPE (stmt) = void_type_node;
6097 OMP_TASK_CLAUSES (stmt) = clauses;
6098 OMP_TASK_BODY (stmt) = body;
6100 return add_stmt (stmt);
6103 /* Helper function for finish_omp_for. Convert Ith random access iterator
6104 into integral iterator. Return FALSE if successful. */
6106 static bool
6107 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6108 tree condv, tree incrv, tree *body,
6109 tree *pre_body, tree clauses)
6111 tree diff, iter_init, iter_incr = NULL, last;
6112 tree incr_var = NULL, orig_pre_body, orig_body, c;
6113 tree decl = TREE_VEC_ELT (declv, i);
6114 tree init = TREE_VEC_ELT (initv, i);
6115 tree cond = TREE_VEC_ELT (condv, i);
6116 tree incr = TREE_VEC_ELT (incrv, i);
6117 tree iter = decl;
6118 location_t elocus = locus;
6120 if (init && EXPR_HAS_LOCATION (init))
6121 elocus = EXPR_LOCATION (init);
6123 switch (TREE_CODE (cond))
6125 case GT_EXPR:
6126 case GE_EXPR:
6127 case LT_EXPR:
6128 case LE_EXPR:
6129 if (TREE_OPERAND (cond, 1) == iter)
6130 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6131 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6132 if (TREE_OPERAND (cond, 0) != iter)
6133 cond = error_mark_node;
6134 else
6136 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6137 TREE_CODE (cond),
6138 iter, ERROR_MARK,
6139 TREE_OPERAND (cond, 1), ERROR_MARK,
6140 NULL, tf_warning_or_error);
6141 if (error_operand_p (tem))
6142 return true;
6144 break;
6145 default:
6146 cond = error_mark_node;
6147 break;
6149 if (cond == error_mark_node)
6151 error_at (elocus, "invalid controlling predicate");
6152 return true;
6154 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6155 ERROR_MARK, iter, ERROR_MARK, NULL,
6156 tf_warning_or_error);
6157 if (error_operand_p (diff))
6158 return true;
6159 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6161 error_at (elocus, "difference between %qE and %qD does not have integer type",
6162 TREE_OPERAND (cond, 1), iter);
6163 return true;
6166 switch (TREE_CODE (incr))
6168 case PREINCREMENT_EXPR:
6169 case PREDECREMENT_EXPR:
6170 case POSTINCREMENT_EXPR:
6171 case POSTDECREMENT_EXPR:
6172 if (TREE_OPERAND (incr, 0) != iter)
6174 incr = error_mark_node;
6175 break;
6177 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6178 TREE_CODE (incr), iter,
6179 tf_warning_or_error);
6180 if (error_operand_p (iter_incr))
6181 return true;
6182 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6183 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6184 incr = integer_one_node;
6185 else
6186 incr = integer_minus_one_node;
6187 break;
6188 case MODIFY_EXPR:
6189 if (TREE_OPERAND (incr, 0) != iter)
6190 incr = error_mark_node;
6191 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6192 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6194 tree rhs = TREE_OPERAND (incr, 1);
6195 if (TREE_OPERAND (rhs, 0) == iter)
6197 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6198 != INTEGER_TYPE)
6199 incr = error_mark_node;
6200 else
6202 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6203 iter, TREE_CODE (rhs),
6204 TREE_OPERAND (rhs, 1),
6205 tf_warning_or_error);
6206 if (error_operand_p (iter_incr))
6207 return true;
6208 incr = TREE_OPERAND (rhs, 1);
6209 incr = cp_convert (TREE_TYPE (diff), incr,
6210 tf_warning_or_error);
6211 if (TREE_CODE (rhs) == MINUS_EXPR)
6213 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6214 incr = fold_if_not_in_template (incr);
6216 if (TREE_CODE (incr) != INTEGER_CST
6217 && (TREE_CODE (incr) != NOP_EXPR
6218 || (TREE_CODE (TREE_OPERAND (incr, 0))
6219 != INTEGER_CST)))
6220 iter_incr = NULL;
6223 else if (TREE_OPERAND (rhs, 1) == iter)
6225 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6226 || TREE_CODE (rhs) != PLUS_EXPR)
6227 incr = error_mark_node;
6228 else
6230 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6231 PLUS_EXPR,
6232 TREE_OPERAND (rhs, 0),
6233 ERROR_MARK, iter,
6234 ERROR_MARK, NULL,
6235 tf_warning_or_error);
6236 if (error_operand_p (iter_incr))
6237 return true;
6238 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6239 iter, NOP_EXPR,
6240 iter_incr,
6241 tf_warning_or_error);
6242 if (error_operand_p (iter_incr))
6243 return true;
6244 incr = TREE_OPERAND (rhs, 0);
6245 iter_incr = NULL;
6248 else
6249 incr = error_mark_node;
6251 else
6252 incr = error_mark_node;
6253 break;
6254 default:
6255 incr = error_mark_node;
6256 break;
6259 if (incr == error_mark_node)
6261 error_at (elocus, "invalid increment expression");
6262 return true;
6265 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6266 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6268 && OMP_CLAUSE_DECL (c) == iter)
6269 break;
6271 decl = create_temporary_var (TREE_TYPE (diff));
6272 pushdecl (decl);
6273 add_decl_expr (decl);
6274 last = create_temporary_var (TREE_TYPE (diff));
6275 pushdecl (last);
6276 add_decl_expr (last);
6277 if (c && iter_incr == NULL)
6279 incr_var = create_temporary_var (TREE_TYPE (diff));
6280 pushdecl (incr_var);
6281 add_decl_expr (incr_var);
6283 gcc_assert (stmts_are_full_exprs_p ());
6285 orig_pre_body = *pre_body;
6286 *pre_body = push_stmt_list ();
6287 if (orig_pre_body)
6288 add_stmt (orig_pre_body);
6289 if (init != NULL)
6290 finish_expr_stmt (build_x_modify_expr (elocus,
6291 iter, NOP_EXPR, init,
6292 tf_warning_or_error));
6293 init = build_int_cst (TREE_TYPE (diff), 0);
6294 if (c && iter_incr == NULL)
6296 finish_expr_stmt (build_x_modify_expr (elocus,
6297 incr_var, NOP_EXPR,
6298 incr, tf_warning_or_error));
6299 incr = incr_var;
6300 iter_incr = build_x_modify_expr (elocus,
6301 iter, PLUS_EXPR, incr,
6302 tf_warning_or_error);
6304 finish_expr_stmt (build_x_modify_expr (elocus,
6305 last, NOP_EXPR, init,
6306 tf_warning_or_error));
6307 *pre_body = pop_stmt_list (*pre_body);
6309 cond = cp_build_binary_op (elocus,
6310 TREE_CODE (cond), decl, diff,
6311 tf_warning_or_error);
6312 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6313 elocus, incr, NULL_TREE);
6315 orig_body = *body;
6316 *body = push_stmt_list ();
6317 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6318 iter_init = build_x_modify_expr (elocus,
6319 iter, PLUS_EXPR, iter_init,
6320 tf_warning_or_error);
6321 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6322 finish_expr_stmt (iter_init);
6323 finish_expr_stmt (build_x_modify_expr (elocus,
6324 last, NOP_EXPR, decl,
6325 tf_warning_or_error));
6326 add_stmt (orig_body);
6327 *body = pop_stmt_list (*body);
6329 if (c)
6331 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6332 finish_expr_stmt (iter_incr);
6333 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6334 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6337 TREE_VEC_ELT (declv, i) = decl;
6338 TREE_VEC_ELT (initv, i) = init;
6339 TREE_VEC_ELT (condv, i) = cond;
6340 TREE_VEC_ELT (incrv, i) = incr;
6342 return false;
6345 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6346 are directly for their associated operands in the statement. DECL
6347 and INIT are a combo; if DECL is NULL then INIT ought to be a
6348 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6349 optional statements that need to go before the loop into its
6350 sk_omp scope. */
6352 tree
6353 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6354 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6356 tree omp_for = NULL, orig_incr = NULL;
6357 tree decl, init, cond, incr;
6358 location_t elocus;
6359 int i;
6361 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6362 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6363 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6364 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6366 decl = TREE_VEC_ELT (declv, i);
6367 init = TREE_VEC_ELT (initv, i);
6368 cond = TREE_VEC_ELT (condv, i);
6369 incr = TREE_VEC_ELT (incrv, i);
6370 elocus = locus;
6372 if (decl == NULL)
6374 if (init != NULL)
6375 switch (TREE_CODE (init))
6377 case MODIFY_EXPR:
6378 decl = TREE_OPERAND (init, 0);
6379 init = TREE_OPERAND (init, 1);
6380 break;
6381 case MODOP_EXPR:
6382 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6384 decl = TREE_OPERAND (init, 0);
6385 init = TREE_OPERAND (init, 2);
6387 break;
6388 default:
6389 break;
6392 if (decl == NULL)
6394 error_at (locus,
6395 "expected iteration declaration or initialization");
6396 return NULL;
6400 if (init && EXPR_HAS_LOCATION (init))
6401 elocus = EXPR_LOCATION (init);
6403 if (cond == NULL)
6405 error_at (elocus, "missing controlling predicate");
6406 return NULL;
6409 if (incr == NULL)
6411 error_at (elocus, "missing increment expression");
6412 return NULL;
6415 TREE_VEC_ELT (declv, i) = decl;
6416 TREE_VEC_ELT (initv, i) = init;
6419 if (dependent_omp_for_p (declv, initv, condv, incrv))
6421 tree stmt;
6423 stmt = make_node (code);
6425 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6427 /* This is really just a place-holder. We'll be decomposing this
6428 again and going through the cp_build_modify_expr path below when
6429 we instantiate the thing. */
6430 TREE_VEC_ELT (initv, i)
6431 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6432 TREE_VEC_ELT (initv, i));
6435 TREE_TYPE (stmt) = void_type_node;
6436 OMP_FOR_INIT (stmt) = initv;
6437 OMP_FOR_COND (stmt) = condv;
6438 OMP_FOR_INCR (stmt) = incrv;
6439 OMP_FOR_BODY (stmt) = body;
6440 OMP_FOR_PRE_BODY (stmt) = pre_body;
6441 OMP_FOR_CLAUSES (stmt) = clauses;
6443 SET_EXPR_LOCATION (stmt, locus);
6444 return add_stmt (stmt);
6447 if (processing_template_decl)
6448 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6450 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6452 decl = TREE_VEC_ELT (declv, i);
6453 init = TREE_VEC_ELT (initv, i);
6454 cond = TREE_VEC_ELT (condv, i);
6455 incr = TREE_VEC_ELT (incrv, i);
6456 if (orig_incr)
6457 TREE_VEC_ELT (orig_incr, i) = incr;
6458 elocus = locus;
6460 if (init && EXPR_HAS_LOCATION (init))
6461 elocus = EXPR_LOCATION (init);
6463 if (!DECL_P (decl))
6465 error_at (elocus, "expected iteration declaration or initialization");
6466 return NULL;
6469 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6471 if (orig_incr)
6472 TREE_VEC_ELT (orig_incr, i) = incr;
6473 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6474 TREE_CODE (TREE_OPERAND (incr, 1)),
6475 TREE_OPERAND (incr, 2),
6476 tf_warning_or_error);
6479 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6481 if (code == OMP_SIMD)
6483 error_at (elocus, "%<#pragma omp simd%> used with class "
6484 "iteration variable %qE", decl);
6485 return NULL;
6487 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6488 incrv, &body, &pre_body, clauses))
6489 return NULL;
6490 continue;
6493 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6494 && !TYPE_PTR_P (TREE_TYPE (decl)))
6496 error_at (elocus, "invalid type for iteration variable %qE", decl);
6497 return NULL;
6500 if (!processing_template_decl)
6502 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6503 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6505 else
6506 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6507 if (cond
6508 && TREE_SIDE_EFFECTS (cond)
6509 && COMPARISON_CLASS_P (cond)
6510 && !processing_template_decl)
6512 tree t = TREE_OPERAND (cond, 0);
6513 if (TREE_SIDE_EFFECTS (t)
6514 && t != decl
6515 && (TREE_CODE (t) != NOP_EXPR
6516 || TREE_OPERAND (t, 0) != decl))
6517 TREE_OPERAND (cond, 0)
6518 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6520 t = TREE_OPERAND (cond, 1);
6521 if (TREE_SIDE_EFFECTS (t)
6522 && t != decl
6523 && (TREE_CODE (t) != NOP_EXPR
6524 || TREE_OPERAND (t, 0) != decl))
6525 TREE_OPERAND (cond, 1)
6526 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6528 if (decl == error_mark_node || init == error_mark_node)
6529 return NULL;
6531 TREE_VEC_ELT (declv, i) = decl;
6532 TREE_VEC_ELT (initv, i) = init;
6533 TREE_VEC_ELT (condv, i) = cond;
6534 TREE_VEC_ELT (incrv, i) = incr;
6535 i++;
6538 if (IS_EMPTY_STMT (pre_body))
6539 pre_body = NULL;
6541 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6542 body, pre_body);
6544 if (omp_for == NULL)
6545 return NULL;
6547 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6549 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6550 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6552 if (TREE_CODE (incr) != MODIFY_EXPR)
6553 continue;
6555 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6556 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6557 && !processing_template_decl)
6559 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6560 if (TREE_SIDE_EFFECTS (t)
6561 && t != decl
6562 && (TREE_CODE (t) != NOP_EXPR
6563 || TREE_OPERAND (t, 0) != decl))
6564 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6565 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6567 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6568 if (TREE_SIDE_EFFECTS (t)
6569 && t != decl
6570 && (TREE_CODE (t) != NOP_EXPR
6571 || TREE_OPERAND (t, 0) != decl))
6572 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6573 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6576 if (orig_incr)
6577 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6579 if (omp_for != NULL)
6580 OMP_FOR_CLAUSES (omp_for) = clauses;
6581 return omp_for;
6584 void
6585 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6586 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6588 tree orig_lhs;
6589 tree orig_rhs;
6590 tree orig_v;
6591 tree orig_lhs1;
6592 tree orig_rhs1;
6593 bool dependent_p;
6594 tree stmt;
6596 orig_lhs = lhs;
6597 orig_rhs = rhs;
6598 orig_v = v;
6599 orig_lhs1 = lhs1;
6600 orig_rhs1 = rhs1;
6601 dependent_p = false;
6602 stmt = NULL_TREE;
6604 /* Even in a template, we can detect invalid uses of the atomic
6605 pragma if neither LHS nor RHS is type-dependent. */
6606 if (processing_template_decl)
6608 dependent_p = (type_dependent_expression_p (lhs)
6609 || (rhs && type_dependent_expression_p (rhs))
6610 || (v && type_dependent_expression_p (v))
6611 || (lhs1 && type_dependent_expression_p (lhs1))
6612 || (rhs1 && type_dependent_expression_p (rhs1)));
6613 if (!dependent_p)
6615 lhs = build_non_dependent_expr (lhs);
6616 if (rhs)
6617 rhs = build_non_dependent_expr (rhs);
6618 if (v)
6619 v = build_non_dependent_expr (v);
6620 if (lhs1)
6621 lhs1 = build_non_dependent_expr (lhs1);
6622 if (rhs1)
6623 rhs1 = build_non_dependent_expr (rhs1);
6626 if (!dependent_p)
6628 bool swapped = false;
6629 if (rhs1 && cp_tree_equal (lhs, rhs))
6631 tree tem = rhs;
6632 rhs = rhs1;
6633 rhs1 = tem;
6634 swapped = !commutative_tree_code (opcode);
6636 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6638 if (code == OMP_ATOMIC)
6639 error ("%<#pragma omp atomic update%> uses two different "
6640 "expressions for memory");
6641 else
6642 error ("%<#pragma omp atomic capture%> uses two different "
6643 "expressions for memory");
6644 return;
6646 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6648 if (code == OMP_ATOMIC)
6649 error ("%<#pragma omp atomic update%> uses two different "
6650 "expressions for memory");
6651 else
6652 error ("%<#pragma omp atomic capture%> uses two different "
6653 "expressions for memory");
6654 return;
6656 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6657 v, lhs1, rhs1, swapped, seq_cst);
6658 if (stmt == error_mark_node)
6659 return;
6661 if (processing_template_decl)
6663 if (code == OMP_ATOMIC_READ)
6665 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6666 OMP_ATOMIC_READ, orig_lhs);
6667 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6668 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6670 else
6672 if (opcode == NOP_EXPR)
6673 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6674 else
6675 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6676 if (orig_rhs1)
6677 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6678 COMPOUND_EXPR, orig_rhs1, stmt);
6679 if (code != OMP_ATOMIC)
6681 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6682 code, orig_lhs1, stmt);
6683 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6684 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6687 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6688 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6690 finish_expr_stmt (stmt);
6693 void
6694 finish_omp_barrier (void)
6696 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6697 vec<tree, va_gc> *vec = make_tree_vector ();
6698 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6699 release_tree_vector (vec);
6700 finish_expr_stmt (stmt);
6703 void
6704 finish_omp_flush (void)
6706 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6707 vec<tree, va_gc> *vec = make_tree_vector ();
6708 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6709 release_tree_vector (vec);
6710 finish_expr_stmt (stmt);
6713 void
6714 finish_omp_taskwait (void)
6716 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6717 vec<tree, va_gc> *vec = make_tree_vector ();
6718 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6719 release_tree_vector (vec);
6720 finish_expr_stmt (stmt);
6723 void
6724 finish_omp_taskyield (void)
6726 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6727 vec<tree, va_gc> *vec = make_tree_vector ();
6728 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6729 release_tree_vector (vec);
6730 finish_expr_stmt (stmt);
6733 void
6734 finish_omp_cancel (tree clauses)
6736 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6737 int mask = 0;
6738 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6739 mask = 1;
6740 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6741 mask = 2;
6742 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6743 mask = 4;
6744 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6745 mask = 8;
6746 else
6748 error ("%<#pragma omp cancel must specify one of "
6749 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6750 return;
6752 vec<tree, va_gc> *vec = make_tree_vector ();
6753 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6754 if (ifc != NULL_TREE)
6756 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6757 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6758 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6759 build_zero_cst (type));
6761 else
6762 ifc = boolean_true_node;
6763 vec->quick_push (build_int_cst (integer_type_node, mask));
6764 vec->quick_push (ifc);
6765 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6766 release_tree_vector (vec);
6767 finish_expr_stmt (stmt);
6770 void
6771 finish_omp_cancellation_point (tree clauses)
6773 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6774 int mask = 0;
6775 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6776 mask = 1;
6777 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6778 mask = 2;
6779 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6780 mask = 4;
6781 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6782 mask = 8;
6783 else
6785 error ("%<#pragma omp cancellation point must specify one of "
6786 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6787 return;
6789 vec<tree, va_gc> *vec
6790 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6791 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6792 release_tree_vector (vec);
6793 finish_expr_stmt (stmt);
6796 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6797 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6798 should create an extra compound stmt. */
6800 tree
6801 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6803 tree r;
6805 if (pcompound)
6806 *pcompound = begin_compound_stmt (0);
6808 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6810 /* Only add the statement to the function if support enabled. */
6811 if (flag_tm)
6812 add_stmt (r);
6813 else
6814 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6815 ? G_("%<__transaction_relaxed%> without "
6816 "transactional memory support enabled")
6817 : G_("%<__transaction_atomic%> without "
6818 "transactional memory support enabled")));
6820 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6821 TREE_SIDE_EFFECTS (r) = 1;
6822 return r;
6825 /* End a __transaction_atomic or __transaction_relaxed statement.
6826 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6827 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6828 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6830 void
6831 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6833 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6834 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6835 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6836 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6838 /* noexcept specifications are not allowed for function transactions. */
6839 gcc_assert (!(noex && compound_stmt));
6840 if (noex)
6842 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6843 noex);
6844 /* This may not be true when the STATEMENT_LIST is empty. */
6845 if (EXPR_P (body))
6846 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6847 TREE_SIDE_EFFECTS (body) = 1;
6848 TRANSACTION_EXPR_BODY (stmt) = body;
6851 if (compound_stmt)
6852 finish_compound_stmt (compound_stmt);
6855 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6856 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6857 condition. */
6859 tree
6860 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6862 tree ret;
6863 if (noex)
6865 expr = build_must_not_throw_expr (expr, noex);
6866 if (EXPR_P (expr))
6867 SET_EXPR_LOCATION (expr, loc);
6868 TREE_SIDE_EFFECTS (expr) = 1;
6870 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6871 if (flags & TM_STMT_ATTR_RELAXED)
6872 TRANSACTION_EXPR_RELAXED (ret) = 1;
6873 TREE_SIDE_EFFECTS (ret) = 1;
6874 SET_EXPR_LOCATION (ret, loc);
6875 return ret;
6878 void
6879 init_cp_semantics (void)
6883 /* Build a STATIC_ASSERT for a static assertion with the condition
6884 CONDITION and the message text MESSAGE. LOCATION is the location
6885 of the static assertion in the source code. When MEMBER_P, this
6886 static assertion is a member of a class. */
6887 void
6888 finish_static_assert (tree condition, tree message, location_t location,
6889 bool member_p)
6891 if (message == NULL_TREE
6892 || message == error_mark_node
6893 || condition == NULL_TREE
6894 || condition == error_mark_node)
6895 return;
6897 if (check_for_bare_parameter_packs (condition))
6898 condition = error_mark_node;
6900 if (type_dependent_expression_p (condition)
6901 || value_dependent_expression_p (condition))
6903 /* We're in a template; build a STATIC_ASSERT and put it in
6904 the right place. */
6905 tree assertion;
6907 assertion = make_node (STATIC_ASSERT);
6908 STATIC_ASSERT_CONDITION (assertion) = condition;
6909 STATIC_ASSERT_MESSAGE (assertion) = message;
6910 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6912 if (member_p)
6913 maybe_add_class_template_decl_list (current_class_type,
6914 assertion,
6915 /*friend_p=*/0);
6916 else
6917 add_stmt (assertion);
6919 return;
6922 /* Fold the expression and convert it to a boolean value. */
6923 condition = fold_non_dependent_expr (condition);
6924 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6925 condition = maybe_constant_value (condition);
6927 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6928 /* Do nothing; the condition is satisfied. */
6930 else
6932 location_t saved_loc = input_location;
6934 input_location = location;
6935 if (TREE_CODE (condition) == INTEGER_CST
6936 && integer_zerop (condition))
6937 /* Report the error. */
6938 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6939 else if (condition && condition != error_mark_node)
6941 error ("non-constant condition for static assertion");
6942 cxx_constant_value (condition);
6944 input_location = saved_loc;
6948 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6949 suitable for use as a type-specifier.
6951 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6952 id-expression or a class member access, FALSE when it was parsed as
6953 a full expression. */
6955 tree
6956 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6957 tsubst_flags_t complain)
6959 tree type = NULL_TREE;
6961 if (!expr || error_operand_p (expr))
6962 return error_mark_node;
6964 if (TYPE_P (expr)
6965 || TREE_CODE (expr) == TYPE_DECL
6966 || (TREE_CODE (expr) == BIT_NOT_EXPR
6967 && TYPE_P (TREE_OPERAND (expr, 0))))
6969 if (complain & tf_error)
6970 error ("argument to decltype must be an expression");
6971 return error_mark_node;
6974 /* Depending on the resolution of DR 1172, we may later need to distinguish
6975 instantiation-dependent but not type-dependent expressions so that, say,
6976 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6977 if (instantiation_dependent_expression_p (expr))
6979 type = cxx_make_type (DECLTYPE_TYPE);
6980 DECLTYPE_TYPE_EXPR (type) = expr;
6981 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6982 = id_expression_or_member_access_p;
6983 SET_TYPE_STRUCTURAL_EQUALITY (type);
6985 return type;
6988 /* The type denoted by decltype(e) is defined as follows: */
6990 expr = resolve_nondeduced_context (expr);
6992 if (invalid_nonstatic_memfn_p (expr, complain))
6993 return error_mark_node;
6995 if (type_unknown_p (expr))
6997 if (complain & tf_error)
6998 error ("decltype cannot resolve address of overloaded function");
6999 return error_mark_node;
7002 /* To get the size of a static data member declared as an array of
7003 unknown bound, we need to instantiate it. */
7004 if (VAR_P (expr)
7005 && VAR_HAD_UNKNOWN_BOUND (expr)
7006 && DECL_TEMPLATE_INSTANTIATION (expr))
7007 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7009 if (id_expression_or_member_access_p)
7011 /* If e is an id-expression or a class member access (5.2.5
7012 [expr.ref]), decltype(e) is defined as the type of the entity
7013 named by e. If there is no such entity, or e names a set of
7014 overloaded functions, the program is ill-formed. */
7015 if (identifier_p (expr))
7016 expr = lookup_name (expr);
7018 if (INDIRECT_REF_P (expr))
7019 /* This can happen when the expression is, e.g., "a.b". Just
7020 look at the underlying operand. */
7021 expr = TREE_OPERAND (expr, 0);
7023 if (TREE_CODE (expr) == OFFSET_REF
7024 || TREE_CODE (expr) == MEMBER_REF
7025 || TREE_CODE (expr) == SCOPE_REF)
7026 /* We're only interested in the field itself. If it is a
7027 BASELINK, we will need to see through it in the next
7028 step. */
7029 expr = TREE_OPERAND (expr, 1);
7031 if (BASELINK_P (expr))
7032 /* See through BASELINK nodes to the underlying function. */
7033 expr = BASELINK_FUNCTIONS (expr);
7035 switch (TREE_CODE (expr))
7037 case FIELD_DECL:
7038 if (DECL_BIT_FIELD_TYPE (expr))
7040 type = DECL_BIT_FIELD_TYPE (expr);
7041 break;
7043 /* Fall through for fields that aren't bitfields. */
7045 case FUNCTION_DECL:
7046 case VAR_DECL:
7047 case CONST_DECL:
7048 case PARM_DECL:
7049 case RESULT_DECL:
7050 case TEMPLATE_PARM_INDEX:
7051 expr = mark_type_use (expr);
7052 type = TREE_TYPE (expr);
7053 break;
7055 case ERROR_MARK:
7056 type = error_mark_node;
7057 break;
7059 case COMPONENT_REF:
7060 case COMPOUND_EXPR:
7061 mark_type_use (expr);
7062 type = is_bitfield_expr_with_lowered_type (expr);
7063 if (!type)
7064 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7065 break;
7067 case BIT_FIELD_REF:
7068 gcc_unreachable ();
7070 case INTEGER_CST:
7071 case PTRMEM_CST:
7072 /* We can get here when the id-expression refers to an
7073 enumerator or non-type template parameter. */
7074 type = TREE_TYPE (expr);
7075 break;
7077 default:
7078 /* Handle instantiated template non-type arguments. */
7079 type = TREE_TYPE (expr);
7080 break;
7083 else
7085 /* Within a lambda-expression:
7087 Every occurrence of decltype((x)) where x is a possibly
7088 parenthesized id-expression that names an entity of
7089 automatic storage duration is treated as if x were
7090 transformed into an access to a corresponding data member
7091 of the closure type that would have been declared if x
7092 were a use of the denoted entity. */
7093 if (outer_automatic_var_p (expr)
7094 && current_function_decl
7095 && LAMBDA_FUNCTION_P (current_function_decl))
7096 type = capture_decltype (expr);
7097 else if (error_operand_p (expr))
7098 type = error_mark_node;
7099 else if (expr == current_class_ptr)
7100 /* If the expression is just "this", we want the
7101 cv-unqualified pointer for the "this" type. */
7102 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7103 else
7105 /* Otherwise, where T is the type of e, if e is an lvalue,
7106 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7107 cp_lvalue_kind clk = lvalue_kind (expr);
7108 type = unlowered_expr_type (expr);
7109 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7111 /* For vector types, pick a non-opaque variant. */
7112 if (TREE_CODE (type) == VECTOR_TYPE)
7113 type = strip_typedefs (type);
7115 if (clk != clk_none && !(clk & clk_class))
7116 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7120 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
7121 && (flag_iso || warn_vla > 0))
7123 if (complain & tf_warning_or_error)
7124 pedwarn (input_location, OPT_Wvla,
7125 "taking decltype of array of runtime bound");
7126 else
7127 return error_mark_node;
7130 return type;
7133 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7134 __has_nothrow_copy, depending on assign_p. */
7136 static bool
7137 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7139 tree fns;
7141 if (assign_p)
7143 int ix;
7144 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7145 if (ix < 0)
7146 return false;
7147 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7149 else if (TYPE_HAS_COPY_CTOR (type))
7151 /* If construction of the copy constructor was postponed, create
7152 it now. */
7153 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7154 lazily_declare_fn (sfk_copy_constructor, type);
7155 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7156 lazily_declare_fn (sfk_move_constructor, type);
7157 fns = CLASSTYPE_CONSTRUCTORS (type);
7159 else
7160 return false;
7162 for (; fns; fns = OVL_NEXT (fns))
7164 tree fn = OVL_CURRENT (fns);
7166 if (assign_p)
7168 if (copy_fn_p (fn) == 0)
7169 continue;
7171 else if (copy_fn_p (fn) <= 0)
7172 continue;
7174 maybe_instantiate_noexcept (fn);
7175 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7176 return false;
7179 return true;
7182 // Returns true if K denotes a unary type trait.
7183 inline bool
7184 is_unary_trait (cp_trait_kind k)
7186 if (k == CPTK_IS_CONVERTIBLE_TO || k == CPTK_IS_BASE_OF)
7187 return false;
7188 return true;
7191 // Returns true if K denotes a binary type trait.
7192 bool
7193 is_binary_trait (cp_trait_kind k)
7195 return !is_unary_trait (k);
7198 // Returns a type for T that can be used as an xvalue. For function
7199 // types, this returns an rvalue reference to T. For all other types,
7200 // this simply returns T.
7201 tree
7202 xvalue_result_type (tree t)
7204 if (TREE_CODE (t) == FUNCTION_TYPE)
7205 return cp_build_reference_type(t, true);
7206 else
7207 return t;
7210 /* Actually evaluates the trait. */
7212 static bool
7213 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7215 enum tree_code type_code1;
7216 tree t;
7218 type_code1 = TREE_CODE (type1);
7220 switch (kind)
7222 case CPTK_HAS_NOTHROW_ASSIGN:
7223 type1 = strip_array_types (type1);
7224 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7225 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7226 || (CLASS_TYPE_P (type1)
7227 && classtype_has_nothrow_assign_or_copy_p (type1,
7228 true))));
7230 case CPTK_HAS_TRIVIAL_ASSIGN:
7231 /* ??? The standard seems to be missing the "or array of such a class
7232 type" wording for this trait. */
7233 type1 = strip_array_types (type1);
7234 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7235 && (trivial_type_p (type1)
7236 || (CLASS_TYPE_P (type1)
7237 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7239 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7240 type1 = strip_array_types (type1);
7241 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7242 || (CLASS_TYPE_P (type1)
7243 && (t = locate_ctor (type1))
7244 && (maybe_instantiate_noexcept (t),
7245 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7247 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7248 type1 = strip_array_types (type1);
7249 return (trivial_type_p (type1)
7250 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7252 case CPTK_HAS_NOTHROW_COPY:
7253 type1 = strip_array_types (type1);
7254 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7255 || (CLASS_TYPE_P (type1)
7256 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7258 case CPTK_HAS_TRIVIAL_COPY:
7259 /* ??? The standard seems to be missing the "or array of such a class
7260 type" wording for this trait. */
7261 type1 = strip_array_types (type1);
7262 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7263 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7265 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7266 type1 = strip_array_types (type1);
7267 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7268 || (CLASS_TYPE_P (type1)
7269 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7271 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7272 return type_has_virtual_destructor (type1);
7274 case CPTK_IS_ABSTRACT:
7275 return (ABSTRACT_CLASS_TYPE_P (type1));
7277 case CPTK_IS_BASE_OF:
7278 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7279 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7280 || DERIVED_FROM_P (type1, type2)));
7282 case CPTK_IS_CLASS:
7283 return (NON_UNION_CLASS_TYPE_P (type1));
7285 case CPTK_IS_CONVERTIBLE_TO:
7286 return can_convert (type2, xvalue_result_type (type1), tf_none);
7288 case CPTK_IS_EMPTY:
7289 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7291 case CPTK_IS_ENUM:
7292 return (type_code1 == ENUMERAL_TYPE);
7294 case CPTK_IS_FINAL:
7295 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7297 case CPTK_IS_LITERAL_TYPE:
7298 return (literal_type_p (type1));
7300 case CPTK_IS_POD:
7301 return (pod_type_p (type1));
7303 case CPTK_IS_POLYMORPHIC:
7304 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7306 case CPTK_IS_SAME_AS:
7307 return same_type_p (type1, type2);
7309 case CPTK_IS_STD_LAYOUT:
7310 return (std_layout_type_p (type1));
7312 case CPTK_IS_TRIVIAL:
7313 return (trivial_type_p (type1));
7315 case CPTK_IS_UNION:
7316 return (type_code1 == UNION_TYPE);
7318 default:
7319 gcc_unreachable ();
7320 return false;
7324 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7325 void, or a complete type, returns it, otherwise NULL_TREE. */
7327 static tree
7328 check_trait_type (tree type)
7330 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7331 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7332 return type;
7334 if (VOID_TYPE_P (type))
7335 return type;
7337 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7340 /* Process a trait expression. */
7342 tree
7343 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7345 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7346 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7347 || kind == CPTK_HAS_NOTHROW_COPY
7348 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7349 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7350 || kind == CPTK_HAS_TRIVIAL_COPY
7351 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7352 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7353 || kind == CPTK_IS_ABSTRACT
7354 || kind == CPTK_IS_BASE_OF
7355 || kind == CPTK_IS_CLASS
7356 || kind == CPTK_IS_CONVERTIBLE_TO
7357 || kind == CPTK_IS_EMPTY
7358 || kind == CPTK_IS_ENUM
7359 || kind == CPTK_IS_FINAL
7360 || kind == CPTK_IS_LITERAL_TYPE
7361 || kind == CPTK_IS_POD
7362 || kind == CPTK_IS_POLYMORPHIC
7363 || kind == CPTK_IS_SAME_AS
7364 || kind == CPTK_IS_STD_LAYOUT
7365 || kind == CPTK_IS_TRIVIAL
7366 || kind == CPTK_IS_UNION);
7368 if (type1 == error_mark_node
7369 || (is_binary_trait (kind) && type2 == error_mark_node))
7370 return error_mark_node;
7372 if (processing_template_decl)
7374 tree trait_expr = make_node (TRAIT_EXPR);
7375 TREE_TYPE (trait_expr) = boolean_type_node;
7376 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7377 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7378 TRAIT_EXPR_KIND (trait_expr) = kind;
7379 return trait_expr;
7382 switch (kind)
7384 case CPTK_HAS_NOTHROW_ASSIGN:
7385 case CPTK_HAS_TRIVIAL_ASSIGN:
7386 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7387 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7388 case CPTK_HAS_NOTHROW_COPY:
7389 case CPTK_HAS_TRIVIAL_COPY:
7390 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7391 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7392 case CPTK_IS_ABSTRACT:
7393 case CPTK_IS_EMPTY:
7394 case CPTK_IS_FINAL:
7395 case CPTK_IS_LITERAL_TYPE:
7396 case CPTK_IS_POD:
7397 case CPTK_IS_POLYMORPHIC:
7398 case CPTK_IS_STD_LAYOUT:
7399 case CPTK_IS_TRIVIAL:
7400 if (!check_trait_type (type1))
7401 return error_mark_node;
7402 break;
7404 case CPTK_IS_BASE_OF:
7405 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7406 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7407 && !complete_type_or_else (type2, NULL_TREE))
7408 /* We already issued an error. */
7409 return error_mark_node;
7410 break;
7412 case CPTK_IS_CLASS:
7413 case CPTK_IS_ENUM:
7414 case CPTK_IS_UNION:
7415 case CPTK_IS_SAME_AS:
7416 break;
7418 case CPTK_IS_CONVERTIBLE_TO:
7419 if (!check_trait_type (type1))
7420 return error_mark_node;
7421 if (!check_trait_type (type2))
7422 return error_mark_node;
7423 break;
7425 default:
7426 gcc_unreachable ();
7429 return (trait_expr_value (kind, type1, type2)
7430 ? boolean_true_node : boolean_false_node);
7433 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7434 which is ignored for C++. */
7436 void
7437 set_float_const_decimal64 (void)
7441 void
7442 clear_float_const_decimal64 (void)
7446 bool
7447 float_const_decimal64_p (void)
7449 return 0;
7453 /* Return true if T is a literal type. */
7455 bool
7456 literal_type_p (tree t)
7458 if (SCALAR_TYPE_P (t)
7459 || TREE_CODE (t) == VECTOR_TYPE
7460 || TREE_CODE (t) == REFERENCE_TYPE)
7461 return true;
7462 if (CLASS_TYPE_P (t))
7464 t = complete_type (t);
7465 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7466 return CLASSTYPE_LITERAL_P (t);
7468 if (TREE_CODE (t) == ARRAY_TYPE)
7469 return literal_type_p (strip_array_types (t));
7470 return false;
7473 /* If DECL is a variable declared `constexpr', require its type
7474 be literal. Return the DECL if OK, otherwise NULL. */
7476 tree
7477 ensure_literal_type_for_constexpr_object (tree decl)
7479 tree type = TREE_TYPE (decl);
7480 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7481 && !processing_template_decl)
7483 tree stype = strip_array_types (type);
7484 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7485 /* Don't complain here, we'll complain about incompleteness
7486 when we try to initialize the variable. */;
7487 else if (!literal_type_p (type))
7489 error ("the type %qT of constexpr variable %qD is not literal",
7490 type, decl);
7491 explain_non_literal_class (type);
7492 return NULL;
7495 return decl;
7498 /* Representation of entries in the constexpr function definition table. */
7500 typedef struct GTY(()) constexpr_fundef {
7501 tree decl;
7502 tree body;
7503 } constexpr_fundef;
7505 /* This table holds all constexpr function definitions seen in
7506 the current translation unit. */
7508 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7510 /* Utility function used for managing the constexpr function table.
7511 Return true if the entries pointed to by P and Q are for the
7512 same constexpr function. */
7514 static inline int
7515 constexpr_fundef_equal (const void *p, const void *q)
7517 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7518 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7519 return lhs->decl == rhs->decl;
7522 /* Utility function used for managing the constexpr function table.
7523 Return a hash value for the entry pointed to by Q. */
7525 static inline hashval_t
7526 constexpr_fundef_hash (const void *p)
7528 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7529 return DECL_UID (fundef->decl);
7532 /* Return a previously saved definition of function FUN. */
7534 static constexpr_fundef *
7535 retrieve_constexpr_fundef (tree fun)
7537 constexpr_fundef fundef = { NULL, NULL };
7538 if (constexpr_fundef_table == NULL)
7539 return NULL;
7541 fundef.decl = fun;
7542 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7545 /* Check whether the parameter and return types of FUN are valid for a
7546 constexpr function, and complain if COMPLAIN. */
7548 static bool
7549 is_valid_constexpr_fn (tree fun, bool complain)
7551 tree parm = FUNCTION_FIRST_USER_PARM (fun);
7552 bool ret = true;
7553 for (; parm != NULL; parm = TREE_CHAIN (parm))
7554 if (!literal_type_p (TREE_TYPE (parm)))
7556 ret = false;
7557 if (complain)
7559 error ("invalid type for parameter %d of constexpr "
7560 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7561 explain_non_literal_class (TREE_TYPE (parm));
7565 if (!DECL_CONSTRUCTOR_P (fun))
7567 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7568 if (!literal_type_p (rettype))
7570 ret = false;
7571 if (complain)
7573 error ("invalid return type %qT of constexpr function %q+D",
7574 rettype, fun);
7575 explain_non_literal_class (rettype);
7579 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7580 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7582 ret = false;
7583 if (complain)
7585 error ("enclosing class of constexpr non-static member "
7586 "function %q+#D is not a literal type", fun);
7587 explain_non_literal_class (DECL_CONTEXT (fun));
7591 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7593 ret = false;
7594 if (complain)
7595 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7598 return ret;
7601 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7602 for a member of an anonymous aggregate, INIT is the initializer for that
7603 member, and VEC_OUTER is the vector of constructor elements for the class
7604 whose constructor we are processing. Add the initializer to the vector
7605 and return true to indicate success. */
7607 static bool
7608 build_anon_member_initialization (tree member, tree init,
7609 vec<constructor_elt, va_gc> **vec_outer)
7611 /* MEMBER presents the relevant fields from the inside out, but we need
7612 to build up the initializer from the outside in so that we can reuse
7613 previously built CONSTRUCTORs if this is, say, the second field in an
7614 anonymous struct. So we use a vec as a stack. */
7615 auto_vec<tree, 2> fields;
7618 fields.safe_push (TREE_OPERAND (member, 1));
7619 member = TREE_OPERAND (member, 0);
7621 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7622 && TREE_CODE (member) == COMPONENT_REF);
7624 /* VEC has the constructor elements vector for the context of FIELD.
7625 If FIELD is an anonymous aggregate, we will push inside it. */
7626 vec<constructor_elt, va_gc> **vec = vec_outer;
7627 tree field;
7628 while (field = fields.pop(),
7629 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7631 tree ctor;
7632 /* If there is already an outer constructor entry for the anonymous
7633 aggregate FIELD, use it; otherwise, insert one. */
7634 if (vec_safe_is_empty (*vec)
7635 || (*vec)->last().index != field)
7637 ctor = build_constructor (TREE_TYPE (field), NULL);
7638 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7640 else
7641 ctor = (*vec)->last().value;
7642 vec = &CONSTRUCTOR_ELTS (ctor);
7645 /* Now we're at the innermost field, the one that isn't an anonymous
7646 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7647 gcc_assert (fields.is_empty());
7648 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7650 return true;
7653 /* Subroutine of build_constexpr_constructor_member_initializers.
7654 The expression tree T represents a data member initialization
7655 in a (constexpr) constructor definition. Build a pairing of
7656 the data member with its initializer, and prepend that pair
7657 to the existing initialization pair INITS. */
7659 static bool
7660 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7662 tree member, init;
7663 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7664 t = TREE_OPERAND (t, 0);
7665 if (TREE_CODE (t) == EXPR_STMT)
7666 t = TREE_OPERAND (t, 0);
7667 if (t == error_mark_node)
7668 return false;
7669 if (TREE_CODE (t) == STATEMENT_LIST)
7671 tree_stmt_iterator i;
7672 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7674 if (! build_data_member_initialization (tsi_stmt (i), vec))
7675 return false;
7677 return true;
7679 if (TREE_CODE (t) == CLEANUP_STMT)
7681 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7682 but we can in a constexpr constructor for a non-literal class. Just
7683 ignore it; either all the initialization will be constant, in which
7684 case the cleanup can't run, or it can't be constexpr.
7685 Still recurse into CLEANUP_BODY. */
7686 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7688 if (TREE_CODE (t) == CONVERT_EXPR)
7689 t = TREE_OPERAND (t, 0);
7690 if (TREE_CODE (t) == INIT_EXPR
7691 || TREE_CODE (t) == MODIFY_EXPR)
7693 member = TREE_OPERAND (t, 0);
7694 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7696 else if (TREE_CODE (t) == CALL_EXPR)
7698 member = CALL_EXPR_ARG (t, 0);
7699 /* We don't use build_cplus_new here because it complains about
7700 abstract bases. Leaving the call unwrapped means that it has the
7701 wrong type, but cxx_eval_constant_expression doesn't care. */
7702 init = break_out_target_exprs (t);
7704 else if (TREE_CODE (t) == DECL_EXPR)
7705 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7706 return true;
7707 else
7708 gcc_unreachable ();
7709 if (INDIRECT_REF_P (member))
7710 member = TREE_OPERAND (member, 0);
7711 if (TREE_CODE (member) == NOP_EXPR)
7713 tree op = member;
7714 STRIP_NOPS (op);
7715 if (TREE_CODE (op) == ADDR_EXPR)
7717 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7718 (TREE_TYPE (TREE_TYPE (op)),
7719 TREE_TYPE (TREE_TYPE (member))));
7720 /* Initializing a cv-qualified member; we need to look through
7721 the const_cast. */
7722 member = op;
7724 else if (op == current_class_ptr
7725 && (same_type_ignoring_top_level_qualifiers_p
7726 (TREE_TYPE (TREE_TYPE (member)),
7727 current_class_type)))
7728 /* Delegating constructor. */
7729 member = op;
7730 else
7732 /* This is an initializer for an empty base; keep it for now so
7733 we can check it in cxx_eval_bare_aggregate. */
7734 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7737 if (TREE_CODE (member) == ADDR_EXPR)
7738 member = TREE_OPERAND (member, 0);
7739 if (TREE_CODE (member) == COMPONENT_REF)
7741 tree aggr = TREE_OPERAND (member, 0);
7742 if (TREE_CODE (aggr) != COMPONENT_REF)
7743 /* Normal member initialization. */
7744 member = TREE_OPERAND (member, 1);
7745 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7746 /* Initializing a member of an anonymous union. */
7747 return build_anon_member_initialization (member, init, vec);
7748 else
7749 /* We're initializing a vtable pointer in a base. Leave it as
7750 COMPONENT_REF so we remember the path to get to the vfield. */
7751 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7754 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7755 return true;
7758 /* Make sure that there are no statements after LAST in the constructor
7759 body represented by LIST. */
7761 bool
7762 check_constexpr_ctor_body (tree last, tree list)
7764 bool ok = true;
7765 if (TREE_CODE (list) == STATEMENT_LIST)
7767 tree_stmt_iterator i = tsi_last (list);
7768 for (; !tsi_end_p (i); tsi_prev (&i))
7770 tree t = tsi_stmt (i);
7771 if (t == last)
7772 break;
7773 if (TREE_CODE (t) == BIND_EXPR)
7775 if (BIND_EXPR_VARS (t))
7777 ok = false;
7778 break;
7780 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7781 return false;
7782 else
7783 continue;
7785 /* We currently allow typedefs and static_assert.
7786 FIXME allow them in the standard, too. */
7787 if (TREE_CODE (t) != STATIC_ASSERT)
7789 ok = false;
7790 break;
7794 else if (list != last
7795 && TREE_CODE (list) != STATIC_ASSERT)
7796 ok = false;
7797 if (!ok)
7799 error ("constexpr constructor does not have empty body");
7800 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7802 return ok;
7805 /* V is a vector of constructor elements built up for the base and member
7806 initializers of a constructor for TYPE. They need to be in increasing
7807 offset order, which they might not be yet if TYPE has a primary base
7808 which is not first in the base-clause or a vptr and at least one base
7809 all of which are non-primary. */
7811 static vec<constructor_elt, va_gc> *
7812 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7814 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7815 tree field_type;
7816 constructor_elt elt;
7817 int i;
7819 if (pri)
7820 field_type = BINFO_TYPE (pri);
7821 else if (TYPE_CONTAINS_VPTR_P (type))
7822 field_type = vtbl_ptr_type_node;
7823 else
7824 return v;
7826 /* Find the element for the primary base or vptr and move it to the
7827 beginning of the vec. */
7828 vec<constructor_elt, va_gc> &vref = *v;
7829 for (i = 0; ; ++i)
7830 if (TREE_TYPE (vref[i].index) == field_type)
7831 break;
7833 if (i > 0)
7835 elt = vref[i];
7836 for (; i > 0; --i)
7837 vref[i] = vref[i-1];
7838 vref[0] = elt;
7841 return v;
7844 /* Build compile-time evalable representations of member-initializer list
7845 for a constexpr constructor. */
7847 static tree
7848 build_constexpr_constructor_member_initializers (tree type, tree body)
7850 vec<constructor_elt, va_gc> *vec = NULL;
7851 bool ok = true;
7852 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7853 || TREE_CODE (body) == EH_SPEC_BLOCK)
7854 body = TREE_OPERAND (body, 0);
7855 if (TREE_CODE (body) == STATEMENT_LIST)
7856 body = STATEMENT_LIST_HEAD (body)->stmt;
7857 body = BIND_EXPR_BODY (body);
7858 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7860 body = TREE_OPERAND (body, 0);
7861 if (TREE_CODE (body) == EXPR_STMT)
7862 body = TREE_OPERAND (body, 0);
7863 if (TREE_CODE (body) == INIT_EXPR
7864 && (same_type_ignoring_top_level_qualifiers_p
7865 (TREE_TYPE (TREE_OPERAND (body, 0)),
7866 current_class_type)))
7868 /* Trivial copy. */
7869 return TREE_OPERAND (body, 1);
7871 ok = build_data_member_initialization (body, &vec);
7873 else if (TREE_CODE (body) == STATEMENT_LIST)
7875 tree_stmt_iterator i;
7876 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7878 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7879 if (!ok)
7880 break;
7883 else if (TREE_CODE (body) == TRY_BLOCK)
7885 error ("body of %<constexpr%> constructor cannot be "
7886 "a function-try-block");
7887 return error_mark_node;
7889 else if (EXPR_P (body))
7890 ok = build_data_member_initialization (body, &vec);
7891 else
7892 gcc_assert (errorcount > 0);
7893 if (ok)
7895 if (vec_safe_length (vec) > 0)
7897 /* In a delegating constructor, return the target. */
7898 constructor_elt *ce = &(*vec)[0];
7899 if (ce->index == current_class_ptr)
7901 body = ce->value;
7902 vec_free (vec);
7903 return body;
7906 vec = sort_constexpr_mem_initializers (type, vec);
7907 return build_constructor (type, vec);
7909 else
7910 return error_mark_node;
7913 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7914 declared to be constexpr, or a sub-statement thereof. Returns the
7915 return value if suitable, error_mark_node for a statement not allowed in
7916 a constexpr function, or NULL_TREE if no return value was found. */
7918 static tree
7919 constexpr_fn_retval (tree body)
7921 switch (TREE_CODE (body))
7923 case STATEMENT_LIST:
7925 tree_stmt_iterator i;
7926 tree expr = NULL_TREE;
7927 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7929 tree s = constexpr_fn_retval (tsi_stmt (i));
7930 if (s == error_mark_node)
7931 return error_mark_node;
7932 else if (s == NULL_TREE)
7933 /* Keep iterating. */;
7934 else if (expr)
7935 /* Multiple return statements. */
7936 return error_mark_node;
7937 else
7938 expr = s;
7940 return expr;
7943 case RETURN_EXPR:
7944 return break_out_target_exprs (TREE_OPERAND (body, 0));
7946 case DECL_EXPR:
7947 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7948 return NULL_TREE;
7949 return error_mark_node;
7951 case CLEANUP_POINT_EXPR:
7952 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7954 case USING_STMT:
7955 return NULL_TREE;
7957 default:
7958 return error_mark_node;
7962 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7963 FUN; do the necessary transformations to turn it into a single expression
7964 that we can store in the hash table. */
7966 static tree
7967 massage_constexpr_body (tree fun, tree body)
7969 if (DECL_CONSTRUCTOR_P (fun))
7970 body = build_constexpr_constructor_member_initializers
7971 (DECL_CONTEXT (fun), body);
7972 else
7974 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7975 body = EH_SPEC_STMTS (body);
7976 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7977 body = TREE_OPERAND (body, 0);
7978 if (TREE_CODE (body) == BIND_EXPR)
7979 body = BIND_EXPR_BODY (body);
7980 body = constexpr_fn_retval (body);
7982 return body;
7985 /* FUN is a constexpr constructor with massaged body BODY. Return true
7986 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7988 static bool
7989 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7991 bool bad;
7992 tree field;
7993 unsigned i, nelts;
7994 tree ctype;
7996 if (TREE_CODE (body) != CONSTRUCTOR)
7997 return false;
7999 nelts = CONSTRUCTOR_NELTS (body);
8000 ctype = DECL_CONTEXT (fun);
8001 field = TYPE_FIELDS (ctype);
8003 if (TREE_CODE (ctype) == UNION_TYPE)
8005 if (nelts == 0 && next_initializable_field (field))
8007 if (complain)
8008 error ("%<constexpr%> constructor for union %qT must "
8009 "initialize exactly one non-static data member", ctype);
8010 return true;
8012 return false;
8015 bad = false;
8016 for (i = 0; i <= nelts; ++i)
8018 tree index;
8019 if (i == nelts)
8020 index = NULL_TREE;
8021 else
8023 index = CONSTRUCTOR_ELT (body, i)->index;
8024 /* Skip base and vtable inits. */
8025 if (TREE_CODE (index) != FIELD_DECL
8026 || DECL_ARTIFICIAL (index))
8027 continue;
8029 for (; field != index; field = DECL_CHAIN (field))
8031 tree ftype;
8032 if (TREE_CODE (field) != FIELD_DECL
8033 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
8034 || DECL_ARTIFICIAL (field))
8035 continue;
8036 ftype = strip_array_types (TREE_TYPE (field));
8037 if (type_has_constexpr_default_constructor (ftype))
8039 /* It's OK to skip a member with a trivial constexpr ctor.
8040 A constexpr ctor that isn't trivial should have been
8041 added in by now. */
8042 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
8043 || errorcount != 0);
8044 continue;
8046 if (!complain)
8047 return true;
8048 error ("uninitialized member %qD in %<constexpr%> constructor",
8049 field);
8050 bad = true;
8052 if (field == NULL_TREE)
8053 break;
8054 field = DECL_CHAIN (field);
8057 return bad;
8060 /* We are processing the definition of the constexpr function FUN.
8061 Check that its BODY fulfills the propriate requirements and
8062 enter it in the constexpr function definition table.
8063 For constructor BODY is actually the TREE_LIST of the
8064 member-initializer list. */
8066 tree
8067 register_constexpr_fundef (tree fun, tree body)
8069 constexpr_fundef entry;
8070 constexpr_fundef **slot;
8072 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
8073 return NULL;
8075 body = massage_constexpr_body (fun, body);
8076 if (body == NULL_TREE || body == error_mark_node)
8078 if (!DECL_CONSTRUCTOR_P (fun))
8079 error ("body of constexpr function %qD not a return-statement", fun);
8080 return NULL;
8083 if (!potential_rvalue_constant_expression (body))
8085 if (!DECL_GENERATED_P (fun))
8086 require_potential_rvalue_constant_expression (body);
8087 return NULL;
8090 if (DECL_CONSTRUCTOR_P (fun)
8091 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8092 return NULL;
8094 /* Create the constexpr function table if necessary. */
8095 if (constexpr_fundef_table == NULL)
8096 constexpr_fundef_table = htab_create_ggc (101,
8097 constexpr_fundef_hash,
8098 constexpr_fundef_equal,
8099 ggc_free);
8100 entry.decl = fun;
8101 entry.body = body;
8102 slot = (constexpr_fundef **)
8103 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8105 gcc_assert (*slot == NULL);
8106 *slot = ggc_alloc_constexpr_fundef ();
8107 **slot = entry;
8109 return fun;
8112 /* FUN is a non-constexpr function called in a context that requires a
8113 constant expression. If it comes from a constexpr template, explain why
8114 the instantiation isn't constexpr. */
8116 void
8117 explain_invalid_constexpr_fn (tree fun)
8119 static struct pointer_set_t *diagnosed;
8120 tree body;
8121 location_t save_loc;
8122 /* Only diagnose defaulted functions or instantiations. */
8123 if (!DECL_DEFAULTED_FN (fun)
8124 && !is_instantiation_of_constexpr (fun))
8125 return;
8126 if (diagnosed == NULL)
8127 diagnosed = pointer_set_create ();
8128 if (pointer_set_insert (diagnosed, fun) != 0)
8129 /* Already explained. */
8130 return;
8132 save_loc = input_location;
8133 input_location = DECL_SOURCE_LOCATION (fun);
8134 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8135 /* First check the declaration. */
8136 if (is_valid_constexpr_fn (fun, true))
8138 /* Then if it's OK, the body. */
8139 if (DECL_DEFAULTED_FN (fun))
8140 explain_implicit_non_constexpr (fun);
8141 else
8143 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8144 require_potential_rvalue_constant_expression (body);
8145 if (DECL_CONSTRUCTOR_P (fun))
8146 cx_check_missing_mem_inits (fun, body, true);
8149 input_location = save_loc;
8152 /* Objects of this type represent calls to constexpr functions
8153 along with the bindings of parameters to their arguments, for
8154 the purpose of compile time evaluation. */
8156 typedef struct GTY(()) constexpr_call {
8157 /* Description of the constexpr function definition. */
8158 constexpr_fundef *fundef;
8159 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8160 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8161 Note: This arrangement is made to accommodate the use of
8162 iterative_hash_template_arg (see pt.c). If you change this
8163 representation, also change the hash calculation in
8164 cxx_eval_call_expression. */
8165 tree bindings;
8166 /* Result of the call.
8167 NULL means the call is being evaluated.
8168 error_mark_node means that the evaluation was erroneous;
8169 otherwise, the actuall value of the call. */
8170 tree result;
8171 /* The hash of this call; we remember it here to avoid having to
8172 recalculate it when expanding the hash table. */
8173 hashval_t hash;
8174 } constexpr_call;
8176 /* A table of all constexpr calls that have been evaluated by the
8177 compiler in this translation unit. */
8179 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8181 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8182 bool, bool, bool *, bool *);
8184 /* Compute a hash value for a constexpr call representation. */
8186 static hashval_t
8187 constexpr_call_hash (const void *p)
8189 const constexpr_call *info = (const constexpr_call *) p;
8190 return info->hash;
8193 /* Return 1 if the objects pointed to by P and Q represent calls
8194 to the same constexpr function with the same arguments.
8195 Otherwise, return 0. */
8197 static int
8198 constexpr_call_equal (const void *p, const void *q)
8200 const constexpr_call *lhs = (const constexpr_call *) p;
8201 const constexpr_call *rhs = (const constexpr_call *) q;
8202 tree lhs_bindings;
8203 tree rhs_bindings;
8204 if (lhs == rhs)
8205 return 1;
8206 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8207 return 0;
8208 lhs_bindings = lhs->bindings;
8209 rhs_bindings = rhs->bindings;
8210 while (lhs_bindings != NULL && rhs_bindings != NULL)
8212 tree lhs_arg = TREE_VALUE (lhs_bindings);
8213 tree rhs_arg = TREE_VALUE (rhs_bindings);
8214 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8215 if (!cp_tree_equal (lhs_arg, rhs_arg))
8216 return 0;
8217 lhs_bindings = TREE_CHAIN (lhs_bindings);
8218 rhs_bindings = TREE_CHAIN (rhs_bindings);
8220 return lhs_bindings == rhs_bindings;
8223 /* Initialize the constexpr call table, if needed. */
8225 static void
8226 maybe_initialize_constexpr_call_table (void)
8228 if (constexpr_call_table == NULL)
8229 constexpr_call_table = htab_create_ggc (101,
8230 constexpr_call_hash,
8231 constexpr_call_equal,
8232 ggc_free);
8235 /* Return true if T designates the implied `this' parameter. */
8237 static inline bool
8238 is_this_parameter (tree t)
8240 return t == current_class_ptr;
8243 /* We have an expression tree T that represents a call, either CALL_EXPR
8244 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8245 retrun the _DECL for that function. */
8247 static tree
8248 get_function_named_in_call (tree t)
8250 tree fun = NULL;
8251 switch (TREE_CODE (t))
8253 case CALL_EXPR:
8254 fun = CALL_EXPR_FN (t);
8255 break;
8257 case AGGR_INIT_EXPR:
8258 fun = AGGR_INIT_EXPR_FN (t);
8259 break;
8261 default:
8262 gcc_unreachable();
8263 break;
8265 if (TREE_CODE (fun) == ADDR_EXPR
8266 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8267 fun = TREE_OPERAND (fun, 0);
8268 return fun;
8271 /* We have an expression tree T that represents a call, either CALL_EXPR
8272 or AGGR_INIT_EXPR. Return the Nth argument. */
8274 static inline tree
8275 get_nth_callarg (tree t, int n)
8277 switch (TREE_CODE (t))
8279 case CALL_EXPR:
8280 return CALL_EXPR_ARG (t, n);
8282 case AGGR_INIT_EXPR:
8283 return AGGR_INIT_EXPR_ARG (t, n);
8285 default:
8286 gcc_unreachable ();
8287 return NULL;
8291 /* Look up the binding of the function parameter T in a constexpr
8292 function call context CALL. */
8294 static tree
8295 lookup_parameter_binding (const constexpr_call *call, tree t)
8297 tree b = purpose_member (t, call->bindings);
8298 return TREE_VALUE (b);
8301 /* Attempt to evaluate T which represents a call to a builtin function.
8302 We assume here that all builtin functions evaluate to scalar types
8303 represented by _CST nodes. */
8305 static tree
8306 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8307 bool allow_non_constant, bool addr,
8308 bool *non_constant_p, bool *overflow_p)
8310 const int nargs = call_expr_nargs (t);
8311 tree *args = (tree *) alloca (nargs * sizeof (tree));
8312 tree new_call;
8313 int i;
8314 for (i = 0; i < nargs; ++i)
8316 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8317 allow_non_constant, addr,
8318 non_constant_p, overflow_p);
8319 if (allow_non_constant && *non_constant_p)
8320 return t;
8322 if (*non_constant_p)
8323 return t;
8324 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8325 CALL_EXPR_FN (t), nargs, args);
8326 new_call = fold (new_call);
8327 VERIFY_CONSTANT (new_call);
8328 return new_call;
8331 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8332 the type of the value to match. */
8334 static tree
8335 adjust_temp_type (tree type, tree temp)
8337 if (TREE_TYPE (temp) == type)
8338 return temp;
8339 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8340 if (TREE_CODE (temp) == CONSTRUCTOR)
8341 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8342 gcc_assert (scalarish_type_p (type));
8343 return cp_fold_convert (type, temp);
8346 /* Subroutine of cxx_eval_call_expression.
8347 We are processing a call expression (either CALL_EXPR or
8348 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8349 all arguments and bind their values to correspondings
8350 parameters, making up the NEW_CALL context. */
8352 static void
8353 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8354 constexpr_call *new_call,
8355 bool allow_non_constant,
8356 bool *non_constant_p, bool *overflow_p)
8358 const int nargs = call_expr_nargs (t);
8359 tree fun = new_call->fundef->decl;
8360 tree parms = DECL_ARGUMENTS (fun);
8361 int i;
8362 for (i = 0; i < nargs; ++i)
8364 tree x, arg;
8365 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8366 /* For member function, the first argument is a pointer to the implied
8367 object. And for an object construction, don't bind `this' before
8368 it is fully constructed. */
8369 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8370 goto next;
8371 x = get_nth_callarg (t, i);
8372 if (parms && DECL_BY_REFERENCE (parms))
8374 /* cp_genericize made this a reference for argument passing, but
8375 we don't want to treat it like one for constexpr evaluation. */
8376 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8377 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8378 type = TREE_TYPE (type);
8379 x = convert_from_reference (x);
8381 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8382 TREE_CODE (type) == REFERENCE_TYPE,
8383 non_constant_p, overflow_p);
8384 /* Don't VERIFY_CONSTANT here. */
8385 if (*non_constant_p && allow_non_constant)
8386 return;
8387 /* Just discard ellipsis args after checking their constantitude. */
8388 if (!parms)
8389 continue;
8390 if (*non_constant_p)
8391 /* Don't try to adjust the type of non-constant args. */
8392 goto next;
8394 /* Make sure the binding has the same type as the parm. */
8395 if (TREE_CODE (type) != REFERENCE_TYPE)
8396 arg = adjust_temp_type (type, arg);
8397 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8398 next:
8399 parms = TREE_CHAIN (parms);
8403 /* Variables and functions to manage constexpr call expansion context.
8404 These do not need to be marked for PCH or GC. */
8406 /* FIXME remember and print actual constant arguments. */
8407 static vec<tree> call_stack = vNULL;
8408 static int call_stack_tick;
8409 static int last_cx_error_tick;
8411 static bool
8412 push_cx_call_context (tree call)
8414 ++call_stack_tick;
8415 if (!EXPR_HAS_LOCATION (call))
8416 SET_EXPR_LOCATION (call, input_location);
8417 call_stack.safe_push (call);
8418 if (call_stack.length () > (unsigned) max_constexpr_depth)
8419 return false;
8420 return true;
8423 static void
8424 pop_cx_call_context (void)
8426 ++call_stack_tick;
8427 call_stack.pop ();
8430 vec<tree>
8431 cx_error_context (void)
8433 vec<tree> r = vNULL;
8434 if (call_stack_tick != last_cx_error_tick
8435 && !call_stack.is_empty ())
8436 r = call_stack;
8437 last_cx_error_tick = call_stack_tick;
8438 return r;
8441 /* Subroutine of cxx_eval_constant_expression.
8442 Evaluate the call expression tree T in the context of OLD_CALL expression
8443 evaluation. */
8445 static tree
8446 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8447 bool allow_non_constant, bool addr,
8448 bool *non_constant_p, bool *overflow_p)
8450 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8451 tree fun = get_function_named_in_call (t);
8452 tree result;
8453 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8454 constexpr_call **slot;
8455 constexpr_call *entry;
8456 bool depth_ok;
8458 if (TREE_CODE (fun) != FUNCTION_DECL)
8460 /* Might be a constexpr function pointer. */
8461 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8462 /*addr*/false, non_constant_p, overflow_p);
8463 if (TREE_CODE (fun) == ADDR_EXPR)
8464 fun = TREE_OPERAND (fun, 0);
8466 if (TREE_CODE (fun) != FUNCTION_DECL)
8468 if (!allow_non_constant && !*non_constant_p)
8469 error_at (loc, "expression %qE does not designate a constexpr "
8470 "function", fun);
8471 *non_constant_p = true;
8472 return t;
8474 if (DECL_CLONED_FUNCTION_P (fun))
8475 fun = DECL_CLONED_FUNCTION (fun);
8476 if (is_builtin_fn (fun))
8477 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8478 addr, non_constant_p, overflow_p);
8479 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8481 if (!allow_non_constant)
8483 error_at (loc, "call to non-constexpr function %qD", fun);
8484 explain_invalid_constexpr_fn (fun);
8486 *non_constant_p = true;
8487 return t;
8490 /* Shortcut trivial constructor/op=. */
8491 if (trivial_fn_p (fun))
8493 if (call_expr_nargs (t) == 2)
8495 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8496 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8497 addr, non_constant_p, overflow_p);
8499 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8500 && AGGR_INIT_ZERO_FIRST (t))
8501 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8504 /* If in direct recursive call, optimize definition search. */
8505 if (old_call != NULL && old_call->fundef->decl == fun)
8506 new_call.fundef = old_call->fundef;
8507 else
8509 new_call.fundef = retrieve_constexpr_fundef (fun);
8510 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8512 if (!allow_non_constant)
8514 if (DECL_INITIAL (fun))
8516 /* The definition of fun was somehow unsuitable. */
8517 error_at (loc, "%qD called in a constant expression", fun);
8518 explain_invalid_constexpr_fn (fun);
8520 else
8521 error_at (loc, "%qD used before its definition", fun);
8523 *non_constant_p = true;
8524 return t;
8527 cxx_bind_parameters_in_call (old_call, t, &new_call,
8528 allow_non_constant, non_constant_p, overflow_p);
8529 if (*non_constant_p)
8530 return t;
8532 depth_ok = push_cx_call_context (t);
8534 new_call.hash
8535 = iterative_hash_template_arg (new_call.bindings,
8536 constexpr_fundef_hash (new_call.fundef));
8538 /* If we have seen this call before, we are done. */
8539 maybe_initialize_constexpr_call_table ();
8540 slot = (constexpr_call **)
8541 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8542 entry = *slot;
8543 if (entry == NULL)
8545 /* We need to keep a pointer to the entry, not just the slot, as the
8546 slot can move in the call to cxx_eval_builtin_function_call. */
8547 *slot = entry = ggc_alloc_constexpr_call ();
8548 *entry = new_call;
8550 /* Calls which are in progress have their result set to NULL
8551 so that we can detect circular dependencies. */
8552 else if (entry->result == NULL)
8554 if (!allow_non_constant)
8555 error ("call has circular dependency");
8556 *non_constant_p = true;
8557 entry->result = result = error_mark_node;
8560 if (!depth_ok)
8562 if (!allow_non_constant)
8563 error ("constexpr evaluation depth exceeds maximum of %d (use "
8564 "-fconstexpr-depth= to increase the maximum)",
8565 max_constexpr_depth);
8566 *non_constant_p = true;
8567 entry->result = result = error_mark_node;
8569 else
8571 result = entry->result;
8572 if (!result || result == error_mark_node)
8573 result = (cxx_eval_constant_expression
8574 (&new_call, new_call.fundef->body,
8575 allow_non_constant, addr,
8576 non_constant_p, overflow_p));
8577 if (result == error_mark_node)
8578 *non_constant_p = true;
8579 if (*non_constant_p)
8580 entry->result = result = error_mark_node;
8581 else
8583 /* If this was a call to initialize an object, set the type of
8584 the CONSTRUCTOR to the type of that object. */
8585 if (DECL_CONSTRUCTOR_P (fun))
8587 tree ob_arg = get_nth_callarg (t, 0);
8588 STRIP_NOPS (ob_arg);
8589 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8590 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8591 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8592 result);
8594 entry->result = result;
8598 pop_cx_call_context ();
8599 return unshare_expr (result);
8602 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8604 bool
8605 reduced_constant_expression_p (tree t)
8607 if (TREE_CODE (t) == PTRMEM_CST)
8608 /* Even if we can't lower this yet, it's constant. */
8609 return true;
8610 /* FIXME are we calling this too much? */
8611 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8614 /* Some expressions may have constant operands but are not constant
8615 themselves, such as 1/0. Call this function (or rather, the macro
8616 following it) to check for that condition.
8618 We only call this in places that require an arithmetic constant, not in
8619 places where we might have a non-constant expression that can be a
8620 component of a constant expression, such as the address of a constexpr
8621 variable that might be dereferenced later. */
8623 static bool
8624 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8625 bool *overflow_p)
8627 if (!*non_constant_p && !reduced_constant_expression_p (t))
8629 if (!allow_non_constant)
8630 error ("%q+E is not a constant expression", t);
8631 *non_constant_p = true;
8633 if (TREE_OVERFLOW_P (t))
8635 if (!allow_non_constant)
8637 permerror (input_location, "overflow in constant expression");
8638 /* If we're being permissive (and are in an enforcing
8639 context), ignore the overflow. */
8640 if (flag_permissive)
8641 return *non_constant_p;
8643 *overflow_p = true;
8645 return *non_constant_p;
8648 /* Subroutine of cxx_eval_constant_expression.
8649 Attempt to reduce the unary expression tree T to a compile time value.
8650 If successful, return the value. Otherwise issue a diagnostic
8651 and return error_mark_node. */
8653 static tree
8654 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8655 bool allow_non_constant, bool addr,
8656 bool *non_constant_p, bool *overflow_p)
8658 tree r;
8659 tree orig_arg = TREE_OPERAND (t, 0);
8660 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8661 addr, non_constant_p, overflow_p);
8662 VERIFY_CONSTANT (arg);
8663 if (arg == orig_arg)
8664 return t;
8665 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8666 VERIFY_CONSTANT (r);
8667 return r;
8670 /* Subroutine of cxx_eval_constant_expression.
8671 Like cxx_eval_unary_expression, except for binary expressions. */
8673 static tree
8674 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8675 bool allow_non_constant, bool addr,
8676 bool *non_constant_p, bool *overflow_p)
8678 tree r;
8679 tree orig_lhs = TREE_OPERAND (t, 0);
8680 tree orig_rhs = TREE_OPERAND (t, 1);
8681 tree lhs, rhs;
8682 lhs = cxx_eval_constant_expression (call, orig_lhs,
8683 allow_non_constant, addr,
8684 non_constant_p, overflow_p);
8685 VERIFY_CONSTANT (lhs);
8686 rhs = cxx_eval_constant_expression (call, orig_rhs,
8687 allow_non_constant, addr,
8688 non_constant_p, overflow_p);
8689 VERIFY_CONSTANT (rhs);
8690 if (lhs == orig_lhs && rhs == orig_rhs)
8691 return t;
8692 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8693 VERIFY_CONSTANT (r);
8694 return r;
8697 /* Subroutine of cxx_eval_constant_expression.
8698 Attempt to evaluate condition expressions. Dead branches are not
8699 looked into. */
8701 static tree
8702 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8703 bool allow_non_constant, bool addr,
8704 bool *non_constant_p, bool *overflow_p)
8706 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8707 allow_non_constant, addr,
8708 non_constant_p, overflow_p);
8709 VERIFY_CONSTANT (val);
8710 /* Don't VERIFY_CONSTANT the other operands. */
8711 if (integer_zerop (val))
8712 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8713 allow_non_constant, addr,
8714 non_constant_p, overflow_p);
8715 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8716 allow_non_constant, addr,
8717 non_constant_p, overflow_p);
8720 /* Subroutine of cxx_eval_constant_expression.
8721 Attempt to reduce a reference to an array slot. */
8723 static tree
8724 cxx_eval_array_reference (const constexpr_call *call, tree t,
8725 bool allow_non_constant, bool addr,
8726 bool *non_constant_p, bool *overflow_p)
8728 tree oldary = TREE_OPERAND (t, 0);
8729 tree ary = cxx_eval_constant_expression (call, oldary,
8730 allow_non_constant, addr,
8731 non_constant_p, overflow_p);
8732 tree index, oldidx;
8733 HOST_WIDE_INT i;
8734 tree elem_type;
8735 unsigned len, elem_nchars = 1;
8736 if (*non_constant_p)
8737 return t;
8738 oldidx = TREE_OPERAND (t, 1);
8739 index = cxx_eval_constant_expression (call, oldidx,
8740 allow_non_constant, false,
8741 non_constant_p, overflow_p);
8742 VERIFY_CONSTANT (index);
8743 if (addr && ary == oldary && index == oldidx)
8744 return t;
8745 else if (addr)
8746 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8747 elem_type = TREE_TYPE (TREE_TYPE (ary));
8748 if (TREE_CODE (ary) == CONSTRUCTOR)
8749 len = CONSTRUCTOR_NELTS (ary);
8750 else if (TREE_CODE (ary) == STRING_CST)
8752 elem_nchars = (TYPE_PRECISION (elem_type)
8753 / TYPE_PRECISION (char_type_node));
8754 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8756 else
8758 /* We can't do anything with other tree codes, so use
8759 VERIFY_CONSTANT to complain and fail. */
8760 VERIFY_CONSTANT (ary);
8761 gcc_unreachable ();
8763 if (compare_tree_int (index, len) >= 0)
8765 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8767 /* If it's within the array bounds but doesn't have an explicit
8768 initializer, it's value-initialized. */
8769 tree val = build_value_init (elem_type, tf_warning_or_error);
8770 return cxx_eval_constant_expression (call, val,
8771 allow_non_constant, addr,
8772 non_constant_p, overflow_p);
8775 if (!allow_non_constant)
8776 error ("array subscript out of bound");
8777 *non_constant_p = true;
8778 return t;
8780 else if (tree_int_cst_lt (index, integer_zero_node))
8782 if (!allow_non_constant)
8783 error ("negative array subscript");
8784 *non_constant_p = true;
8785 return t;
8787 i = tree_to_shwi (index);
8788 if (TREE_CODE (ary) == CONSTRUCTOR)
8789 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8790 else if (elem_nchars == 1)
8791 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8792 TREE_STRING_POINTER (ary)[i]);
8793 else
8795 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8796 return native_interpret_expr (type, (const unsigned char *)
8797 TREE_STRING_POINTER (ary)
8798 + i * elem_nchars, elem_nchars);
8800 /* Don't VERIFY_CONSTANT here. */
8803 /* Subroutine of cxx_eval_constant_expression.
8804 Attempt to reduce a field access of a value of class type. */
8806 static tree
8807 cxx_eval_component_reference (const constexpr_call *call, tree t,
8808 bool allow_non_constant, bool addr,
8809 bool *non_constant_p, bool *overflow_p)
8811 unsigned HOST_WIDE_INT i;
8812 tree field;
8813 tree value;
8814 tree part = TREE_OPERAND (t, 1);
8815 tree orig_whole = TREE_OPERAND (t, 0);
8816 tree whole = cxx_eval_constant_expression (call, orig_whole,
8817 allow_non_constant, addr,
8818 non_constant_p, overflow_p);
8819 if (whole == orig_whole)
8820 return t;
8821 if (addr)
8822 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8823 whole, part, NULL_TREE);
8824 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8825 CONSTRUCTOR. */
8826 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8828 if (!allow_non_constant)
8829 error ("%qE is not a constant expression", orig_whole);
8830 *non_constant_p = true;
8832 if (DECL_MUTABLE_P (part))
8834 if (!allow_non_constant)
8835 error ("mutable %qD is not usable in a constant expression", part);
8836 *non_constant_p = true;
8838 if (*non_constant_p)
8839 return t;
8840 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8842 if (field == part)
8843 return value;
8845 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8846 && CONSTRUCTOR_NELTS (whole) > 0)
8848 /* DR 1188 says we don't have to deal with this. */
8849 if (!allow_non_constant)
8850 error ("accessing %qD member instead of initialized %qD member in "
8851 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8852 *non_constant_p = true;
8853 return t;
8856 /* If there's no explicit init for this field, it's value-initialized. */
8857 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8858 return cxx_eval_constant_expression (call, value,
8859 allow_non_constant, addr,
8860 non_constant_p, overflow_p);
8863 /* Subroutine of cxx_eval_constant_expression.
8864 Attempt to reduce a field access of a value of class type that is
8865 expressed as a BIT_FIELD_REF. */
8867 static tree
8868 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8869 bool allow_non_constant, bool addr,
8870 bool *non_constant_p, bool *overflow_p)
8872 tree orig_whole = TREE_OPERAND (t, 0);
8873 tree retval, fldval, utype, mask;
8874 bool fld_seen = false;
8875 HOST_WIDE_INT istart, isize;
8876 tree whole = cxx_eval_constant_expression (call, orig_whole,
8877 allow_non_constant, addr,
8878 non_constant_p, overflow_p);
8879 tree start, field, value;
8880 unsigned HOST_WIDE_INT i;
8882 if (whole == orig_whole)
8883 return t;
8884 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8885 CONSTRUCTOR. */
8886 if (!*non_constant_p
8887 && TREE_CODE (whole) != VECTOR_CST
8888 && TREE_CODE (whole) != CONSTRUCTOR)
8890 if (!allow_non_constant)
8891 error ("%qE is not a constant expression", orig_whole);
8892 *non_constant_p = true;
8894 if (*non_constant_p)
8895 return t;
8897 if (TREE_CODE (whole) == VECTOR_CST)
8898 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8899 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8901 start = TREE_OPERAND (t, 2);
8902 istart = tree_to_shwi (start);
8903 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8904 utype = TREE_TYPE (t);
8905 if (!TYPE_UNSIGNED (utype))
8906 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8907 retval = build_int_cst (utype, 0);
8908 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8910 tree bitpos = bit_position (field);
8911 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8912 return value;
8913 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8914 && TREE_CODE (value) == INTEGER_CST
8915 && tree_fits_shwi_p (bitpos)
8916 && tree_fits_shwi_p (DECL_SIZE (field)))
8918 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8919 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8920 HOST_WIDE_INT shift;
8921 if (bit >= istart && bit + sz <= istart + isize)
8923 fldval = fold_convert (utype, value);
8924 mask = build_int_cst_type (utype, -1);
8925 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8926 size_int (TYPE_PRECISION (utype) - sz));
8927 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8928 size_int (TYPE_PRECISION (utype) - sz));
8929 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8930 shift = bit - istart;
8931 if (BYTES_BIG_ENDIAN)
8932 shift = TYPE_PRECISION (utype) - shift - sz;
8933 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8934 size_int (shift));
8935 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8936 fld_seen = true;
8940 if (fld_seen)
8941 return fold_convert (TREE_TYPE (t), retval);
8942 gcc_unreachable ();
8943 return error_mark_node;
8946 /* Subroutine of cxx_eval_constant_expression.
8947 Evaluate a short-circuited logical expression T in the context
8948 of a given constexpr CALL. BAILOUT_VALUE is the value for
8949 early return. CONTINUE_VALUE is used here purely for
8950 sanity check purposes. */
8952 static tree
8953 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8954 tree bailout_value, tree continue_value,
8955 bool allow_non_constant, bool addr,
8956 bool *non_constant_p, bool *overflow_p)
8958 tree r;
8959 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8960 allow_non_constant, addr,
8961 non_constant_p, overflow_p);
8962 VERIFY_CONSTANT (lhs);
8963 if (tree_int_cst_equal (lhs, bailout_value))
8964 return lhs;
8965 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8966 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8967 allow_non_constant, addr, non_constant_p, overflow_p);
8968 VERIFY_CONSTANT (r);
8969 return r;
8972 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8973 CONSTRUCTOR elements to initialize (part of) an object containing that
8974 field. Return a pointer to the constructor_elt corresponding to the
8975 initialization of the field. */
8977 static constructor_elt *
8978 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8980 tree aggr = TREE_OPERAND (ref, 0);
8981 tree field = TREE_OPERAND (ref, 1);
8982 HOST_WIDE_INT i;
8983 constructor_elt *ce;
8985 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8987 if (TREE_CODE (aggr) == COMPONENT_REF)
8989 constructor_elt *base_ce
8990 = base_field_constructor_elt (v, aggr);
8991 v = CONSTRUCTOR_ELTS (base_ce->value);
8994 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8995 if (ce->index == field)
8996 return ce;
8998 gcc_unreachable ();
8999 return NULL;
9002 /* Subroutine of cxx_eval_constant_expression.
9003 The expression tree T denotes a C-style array or a C-style
9004 aggregate. Reduce it to a constant expression. */
9006 static tree
9007 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
9008 bool allow_non_constant, bool addr,
9009 bool *non_constant_p, bool *overflow_p)
9011 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9012 vec<constructor_elt, va_gc> *n;
9013 vec_alloc (n, vec_safe_length (v));
9014 constructor_elt *ce;
9015 HOST_WIDE_INT i;
9016 bool changed = false;
9017 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
9018 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9020 tree elt = cxx_eval_constant_expression (call, ce->value,
9021 allow_non_constant, addr,
9022 non_constant_p, overflow_p);
9023 /* Don't VERIFY_CONSTANT here. */
9024 if (allow_non_constant && *non_constant_p)
9025 goto fail;
9026 if (elt != ce->value)
9027 changed = true;
9028 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
9030 /* This is an initialization of a vfield inside a base
9031 subaggregate that we already initialized; push this
9032 initialization into the previous initialization. */
9033 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
9034 inner->value = elt;
9036 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
9038 /* This is an initializer for an empty base; now that we've
9039 checked that it's constant, we can ignore it. */
9040 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
9042 else
9043 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
9045 if (*non_constant_p || !changed)
9047 fail:
9048 vec_free (n);
9049 return t;
9051 t = build_constructor (TREE_TYPE (t), n);
9052 TREE_CONSTANT (t) = true;
9053 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
9054 t = fold (t);
9055 return t;
9058 /* Subroutine of cxx_eval_constant_expression.
9059 The expression tree T is a VEC_INIT_EXPR which denotes the desired
9060 initialization of a non-static data member of array type. Reduce it to a
9061 CONSTRUCTOR.
9063 Note that apart from value-initialization (when VALUE_INIT is true),
9064 this is only intended to support value-initialization and the
9065 initializations done by defaulted constructors for classes with
9066 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
9067 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
9068 for the copy/move constructor. */
9070 static tree
9071 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
9072 bool value_init, bool allow_non_constant, bool addr,
9073 bool *non_constant_p, bool *overflow_p)
9075 tree elttype = TREE_TYPE (atype);
9076 int max = tree_to_shwi (array_type_nelts (atype));
9077 vec<constructor_elt, va_gc> *n;
9078 vec_alloc (n, max + 1);
9079 bool pre_init = false;
9080 int i;
9082 /* For the default constructor, build up a call to the default
9083 constructor of the element type. We only need to handle class types
9084 here, as for a constructor to be constexpr, all members must be
9085 initialized, which for a defaulted default constructor means they must
9086 be of a class type with a constexpr default constructor. */
9087 if (TREE_CODE (elttype) == ARRAY_TYPE)
9088 /* We only do this at the lowest level. */;
9089 else if (value_init)
9091 init = build_value_init (elttype, tf_warning_or_error);
9092 init = cxx_eval_constant_expression
9093 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9094 pre_init = true;
9096 else if (!init)
9098 vec<tree, va_gc> *argvec = make_tree_vector ();
9099 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9100 &argvec, elttype, LOOKUP_NORMAL,
9101 tf_warning_or_error);
9102 release_tree_vector (argvec);
9103 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9104 addr, non_constant_p, overflow_p);
9105 pre_init = true;
9108 if (*non_constant_p && !allow_non_constant)
9109 goto fail;
9111 for (i = 0; i <= max; ++i)
9113 tree idx = build_int_cst (size_type_node, i);
9114 tree eltinit;
9115 if (TREE_CODE (elttype) == ARRAY_TYPE)
9117 /* A multidimensional array; recurse. */
9118 if (value_init || init == NULL_TREE)
9119 eltinit = NULL_TREE;
9120 else
9121 eltinit = cp_build_array_ref (input_location, init, idx,
9122 tf_warning_or_error);
9123 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9124 allow_non_constant, addr,
9125 non_constant_p, overflow_p);
9127 else if (pre_init)
9129 /* Initializing an element using value or default initialization
9130 we just pre-built above. */
9131 if (i == 0)
9132 eltinit = init;
9133 else
9134 eltinit = unshare_expr (init);
9136 else
9138 /* Copying an element. */
9139 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9140 (atype, TREE_TYPE (init)));
9141 eltinit = cp_build_array_ref (input_location, init, idx,
9142 tf_warning_or_error);
9143 if (!real_lvalue_p (init))
9144 eltinit = move (eltinit);
9145 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9146 eltinit = cxx_eval_constant_expression
9147 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9149 if (*non_constant_p && !allow_non_constant)
9150 goto fail;
9151 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9154 if (!*non_constant_p)
9156 init = build_constructor (atype, n);
9157 TREE_CONSTANT (init) = true;
9158 return init;
9161 fail:
9162 vec_free (n);
9163 return init;
9166 static tree
9167 cxx_eval_vec_init (const constexpr_call *call, tree t,
9168 bool allow_non_constant, bool addr,
9169 bool *non_constant_p, bool *overflow_p)
9171 tree atype = TREE_TYPE (t);
9172 tree init = VEC_INIT_EXPR_INIT (t);
9173 tree r = cxx_eval_vec_init_1 (call, atype, init,
9174 VEC_INIT_EXPR_VALUE_INIT (t),
9175 allow_non_constant, addr, non_constant_p, overflow_p);
9176 if (*non_constant_p)
9177 return t;
9178 else
9179 return r;
9182 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9183 match. We want to be less strict for simple *& folding; if we have a
9184 non-const temporary that we access through a const pointer, that should
9185 work. We handle this here rather than change fold_indirect_ref_1
9186 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9187 don't really make sense outside of constant expression evaluation. Also
9188 we want to allow folding to COMPONENT_REF, which could cause trouble
9189 with TBAA in fold_indirect_ref_1.
9191 Try to keep this function synced with fold_indirect_ref_1. */
9193 static tree
9194 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9196 tree sub, subtype;
9198 sub = op0;
9199 STRIP_NOPS (sub);
9200 subtype = TREE_TYPE (sub);
9201 if (!POINTER_TYPE_P (subtype))
9202 return NULL_TREE;
9204 if (TREE_CODE (sub) == ADDR_EXPR)
9206 tree op = TREE_OPERAND (sub, 0);
9207 tree optype = TREE_TYPE (op);
9209 /* *&CONST_DECL -> to the value of the const decl. */
9210 if (TREE_CODE (op) == CONST_DECL)
9211 return DECL_INITIAL (op);
9212 /* *&p => p; make sure to handle *&"str"[cst] here. */
9213 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9215 tree fop = fold_read_from_constant_string (op);
9216 if (fop)
9217 return fop;
9218 else
9219 return op;
9221 /* *(foo *)&fooarray => fooarray[0] */
9222 else if (TREE_CODE (optype) == ARRAY_TYPE
9223 && (same_type_ignoring_top_level_qualifiers_p
9224 (type, TREE_TYPE (optype))))
9226 tree type_domain = TYPE_DOMAIN (optype);
9227 tree min_val = size_zero_node;
9228 if (type_domain && TYPE_MIN_VALUE (type_domain))
9229 min_val = TYPE_MIN_VALUE (type_domain);
9230 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9231 NULL_TREE, NULL_TREE);
9233 /* *(foo *)&complexfoo => __real__ complexfoo */
9234 else if (TREE_CODE (optype) == COMPLEX_TYPE
9235 && (same_type_ignoring_top_level_qualifiers_p
9236 (type, TREE_TYPE (optype))))
9237 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9238 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9239 else if (TREE_CODE (optype) == VECTOR_TYPE
9240 && (same_type_ignoring_top_level_qualifiers_p
9241 (type, TREE_TYPE (optype))))
9243 tree part_width = TYPE_SIZE (type);
9244 tree index = bitsize_int (0);
9245 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9247 /* Also handle conversion to an empty base class, which
9248 is represented with a NOP_EXPR. */
9249 else if (is_empty_class (type)
9250 && CLASS_TYPE_P (optype)
9251 && DERIVED_FROM_P (type, optype))
9253 *empty_base = true;
9254 return op;
9256 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9257 else if (RECORD_OR_UNION_TYPE_P (optype))
9259 tree field = TYPE_FIELDS (optype);
9260 for (; field; field = DECL_CHAIN (field))
9261 if (TREE_CODE (field) == FIELD_DECL
9262 && integer_zerop (byte_position (field))
9263 && (same_type_ignoring_top_level_qualifiers_p
9264 (TREE_TYPE (field), type)))
9266 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9267 break;
9271 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9272 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9274 tree op00 = TREE_OPERAND (sub, 0);
9275 tree op01 = TREE_OPERAND (sub, 1);
9277 STRIP_NOPS (op00);
9278 if (TREE_CODE (op00) == ADDR_EXPR)
9280 tree op00type;
9281 op00 = TREE_OPERAND (op00, 0);
9282 op00type = TREE_TYPE (op00);
9284 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9285 if (TREE_CODE (op00type) == VECTOR_TYPE
9286 && (same_type_ignoring_top_level_qualifiers_p
9287 (type, TREE_TYPE (op00type))))
9289 HOST_WIDE_INT offset = tree_to_shwi (op01);
9290 tree part_width = TYPE_SIZE (type);
9291 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9292 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9293 tree index = bitsize_int (indexi);
9295 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9296 return fold_build3_loc (loc,
9297 BIT_FIELD_REF, type, op00,
9298 part_width, index);
9301 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9302 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9303 && (same_type_ignoring_top_level_qualifiers_p
9304 (type, TREE_TYPE (op00type))))
9306 tree size = TYPE_SIZE_UNIT (type);
9307 if (tree_int_cst_equal (size, op01))
9308 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9310 /* ((foo *)&fooarray)[1] => fooarray[1] */
9311 else if (TREE_CODE (op00type) == ARRAY_TYPE
9312 && (same_type_ignoring_top_level_qualifiers_p
9313 (type, TREE_TYPE (op00type))))
9315 tree type_domain = TYPE_DOMAIN (op00type);
9316 tree min_val = size_zero_node;
9317 if (type_domain && TYPE_MIN_VALUE (type_domain))
9318 min_val = TYPE_MIN_VALUE (type_domain);
9319 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9320 TYPE_SIZE_UNIT (type));
9321 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9322 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9323 NULL_TREE, NULL_TREE);
9325 /* Also handle conversion to an empty base class, which
9326 is represented with a NOP_EXPR. */
9327 else if (is_empty_class (type)
9328 && CLASS_TYPE_P (op00type)
9329 && DERIVED_FROM_P (type, op00type))
9331 *empty_base = true;
9332 return op00;
9334 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9335 else if (RECORD_OR_UNION_TYPE_P (op00type))
9337 tree field = TYPE_FIELDS (op00type);
9338 for (; field; field = DECL_CHAIN (field))
9339 if (TREE_CODE (field) == FIELD_DECL
9340 && tree_int_cst_equal (byte_position (field), op01)
9341 && (same_type_ignoring_top_level_qualifiers_p
9342 (TREE_TYPE (field), type)))
9344 return fold_build3 (COMPONENT_REF, type, op00,
9345 field, NULL_TREE);
9346 break;
9351 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9352 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9353 && (same_type_ignoring_top_level_qualifiers_p
9354 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9356 tree type_domain;
9357 tree min_val = size_zero_node;
9358 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9359 if (newsub)
9360 sub = newsub;
9361 else
9362 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9363 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9364 if (type_domain && TYPE_MIN_VALUE (type_domain))
9365 min_val = TYPE_MIN_VALUE (type_domain);
9366 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9367 NULL_TREE);
9370 return NULL_TREE;
9373 static tree
9374 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9375 bool allow_non_constant, bool addr,
9376 bool *non_constant_p, bool *overflow_p)
9378 tree orig_op0 = TREE_OPERAND (t, 0);
9379 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9380 /*addr*/false, non_constant_p, overflow_p);
9381 bool empty_base = false;
9382 tree r;
9384 /* Don't VERIFY_CONSTANT here. */
9385 if (*non_constant_p)
9386 return t;
9388 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9389 &empty_base);
9391 if (r)
9392 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9393 addr, non_constant_p, overflow_p);
9394 else
9396 tree sub = op0;
9397 STRIP_NOPS (sub);
9398 if (TREE_CODE (sub) == ADDR_EXPR)
9400 /* We couldn't fold to a constant value. Make sure it's not
9401 something we should have been able to fold. */
9402 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9403 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9404 /* DR 1188 says we don't have to deal with this. */
9405 if (!allow_non_constant)
9406 error ("accessing value of %qE through a %qT glvalue in a "
9407 "constant expression", build_fold_indirect_ref (sub),
9408 TREE_TYPE (t));
9409 *non_constant_p = true;
9410 return t;
9414 /* If we're pulling out the value of an empty base, make sure
9415 that the whole object is constant and then return an empty
9416 CONSTRUCTOR. */
9417 if (empty_base)
9419 VERIFY_CONSTANT (r);
9420 r = build_constructor (TREE_TYPE (t), NULL);
9421 TREE_CONSTANT (r) = true;
9424 if (r == NULL_TREE)
9426 if (addr && op0 != orig_op0)
9427 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9428 if (!addr)
9429 VERIFY_CONSTANT (t);
9430 return t;
9432 return r;
9435 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9436 Shared between potential_constant_expression and
9437 cxx_eval_constant_expression. */
9439 static void
9440 non_const_var_error (tree r)
9442 tree type = TREE_TYPE (r);
9443 error ("the value of %qD is not usable in a constant "
9444 "expression", r);
9445 /* Avoid error cascade. */
9446 if (DECL_INITIAL (r) == error_mark_node)
9447 return;
9448 if (DECL_DECLARED_CONSTEXPR_P (r))
9449 inform (DECL_SOURCE_LOCATION (r),
9450 "%qD used in its own initializer", r);
9451 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9453 if (!CP_TYPE_CONST_P (type))
9454 inform (DECL_SOURCE_LOCATION (r),
9455 "%q#D is not const", r);
9456 else if (CP_TYPE_VOLATILE_P (type))
9457 inform (DECL_SOURCE_LOCATION (r),
9458 "%q#D is volatile", r);
9459 else if (!DECL_INITIAL (r)
9460 || !TREE_CONSTANT (DECL_INITIAL (r)))
9461 inform (DECL_SOURCE_LOCATION (r),
9462 "%qD was not initialized with a constant "
9463 "expression", r);
9464 else
9465 gcc_unreachable ();
9467 else
9469 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9470 inform (DECL_SOURCE_LOCATION (r),
9471 "%qD was not declared %<constexpr%>", r);
9472 else
9473 inform (DECL_SOURCE_LOCATION (r),
9474 "%qD does not have integral or enumeration type",
9479 /* Subroutine of cxx_eval_constant_expression.
9480 Like cxx_eval_unary_expression, except for trinary expressions. */
9482 static tree
9483 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9484 bool allow_non_constant, bool addr,
9485 bool *non_constant_p, bool *overflow_p)
9487 int i;
9488 tree args[3];
9489 tree val;
9491 for (i = 0; i < 3; i++)
9493 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9494 allow_non_constant, addr,
9495 non_constant_p, overflow_p);
9496 VERIFY_CONSTANT (args[i]);
9499 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9500 args[0], args[1], args[2]);
9501 if (val == NULL_TREE)
9502 return t;
9503 VERIFY_CONSTANT (val);
9504 return val;
9507 /* Attempt to reduce the expression T to a constant value.
9508 On failure, issue diagnostic and return error_mark_node. */
9509 /* FIXME unify with c_fully_fold */
9511 static tree
9512 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9513 bool allow_non_constant, bool addr,
9514 bool *non_constant_p, bool *overflow_p)
9516 tree r = t;
9518 if (t == error_mark_node)
9520 *non_constant_p = true;
9521 return t;
9523 if (CONSTANT_CLASS_P (t))
9525 if (TREE_CODE (t) == PTRMEM_CST)
9526 t = cplus_expand_constant (t);
9527 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9528 *overflow_p = true;
9529 return t;
9531 if (TREE_CODE (t) != NOP_EXPR
9532 && reduced_constant_expression_p (t))
9533 return fold (t);
9535 switch (TREE_CODE (t))
9537 case VAR_DECL:
9538 if (addr)
9539 return t;
9540 /* else fall through. */
9541 case CONST_DECL:
9542 r = integral_constant_value (t);
9543 if (TREE_CODE (r) == TARGET_EXPR
9544 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9545 r = TARGET_EXPR_INITIAL (r);
9546 if (DECL_P (r))
9548 if (!allow_non_constant)
9549 non_const_var_error (r);
9550 *non_constant_p = true;
9552 break;
9554 case FUNCTION_DECL:
9555 case TEMPLATE_DECL:
9556 case LABEL_DECL:
9557 return t;
9559 case PARM_DECL:
9560 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9562 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9564 if (!allow_non_constant)
9565 sorry ("use of the value of the object being constructed "
9566 "in a constant expression");
9567 *non_constant_p = true;
9569 else
9570 r = lookup_parameter_binding (call, t);
9572 else if (addr)
9573 /* Defer in case this is only used for its type. */;
9574 else
9576 if (!allow_non_constant)
9577 error ("%qE is not a constant expression", t);
9578 *non_constant_p = true;
9580 break;
9582 case CALL_EXPR:
9583 case AGGR_INIT_EXPR:
9584 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9585 non_constant_p, overflow_p);
9586 break;
9588 case TARGET_EXPR:
9589 if (!literal_type_p (TREE_TYPE (t)))
9591 if (!allow_non_constant)
9593 error ("temporary of non-literal type %qT in a "
9594 "constant expression", TREE_TYPE (t));
9595 explain_non_literal_class (TREE_TYPE (t));
9597 *non_constant_p = true;
9598 break;
9600 /* else fall through. */
9601 case INIT_EXPR:
9602 /* Pass false for 'addr' because these codes indicate
9603 initialization of a temporary. */
9604 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9605 allow_non_constant, false,
9606 non_constant_p, overflow_p);
9607 if (!*non_constant_p)
9608 /* Adjust the type of the result to the type of the temporary. */
9609 r = adjust_temp_type (TREE_TYPE (t), r);
9610 break;
9612 case SCOPE_REF:
9613 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9614 allow_non_constant, addr,
9615 non_constant_p, overflow_p);
9616 break;
9618 case RETURN_EXPR:
9619 case NON_LVALUE_EXPR:
9620 case TRY_CATCH_EXPR:
9621 case CLEANUP_POINT_EXPR:
9622 case MUST_NOT_THROW_EXPR:
9623 case SAVE_EXPR:
9624 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9625 allow_non_constant, addr,
9626 non_constant_p, overflow_p);
9627 break;
9629 /* These differ from cxx_eval_unary_expression in that this doesn't
9630 check for a constant operand or result; an address can be
9631 constant without its operand being, and vice versa. */
9632 case INDIRECT_REF:
9633 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9634 non_constant_p, overflow_p);
9635 break;
9637 case ADDR_EXPR:
9639 tree oldop = TREE_OPERAND (t, 0);
9640 tree op = cxx_eval_constant_expression (call, oldop,
9641 allow_non_constant,
9642 /*addr*/true,
9643 non_constant_p, overflow_p);
9644 /* Don't VERIFY_CONSTANT here. */
9645 if (*non_constant_p)
9646 return t;
9647 /* This function does more aggressive folding than fold itself. */
9648 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9649 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9650 return t;
9651 break;
9654 case REALPART_EXPR:
9655 case IMAGPART_EXPR:
9656 case CONJ_EXPR:
9657 case FIX_TRUNC_EXPR:
9658 case FLOAT_EXPR:
9659 case NEGATE_EXPR:
9660 case ABS_EXPR:
9661 case BIT_NOT_EXPR:
9662 case TRUTH_NOT_EXPR:
9663 case FIXED_CONVERT_EXPR:
9664 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9665 non_constant_p, overflow_p);
9666 break;
9668 case SIZEOF_EXPR:
9669 if (SIZEOF_EXPR_TYPE_P (t))
9670 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9671 SIZEOF_EXPR, false);
9672 else if (TYPE_P (TREE_OPERAND (t, 0)))
9673 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9674 false);
9675 else
9676 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9677 false);
9678 if (r == error_mark_node)
9679 r = size_one_node;
9680 VERIFY_CONSTANT (r);
9681 break;
9683 case COMPOUND_EXPR:
9685 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9686 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9687 introduced by build_call_a. */
9688 tree op0 = TREE_OPERAND (t, 0);
9689 tree op1 = TREE_OPERAND (t, 1);
9690 STRIP_NOPS (op1);
9691 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9692 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9693 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9694 addr, non_constant_p, overflow_p);
9695 else
9697 /* Check that the LHS is constant and then discard it. */
9698 cxx_eval_constant_expression (call, op0, allow_non_constant,
9699 false, non_constant_p, overflow_p);
9700 op1 = TREE_OPERAND (t, 1);
9701 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9702 addr, non_constant_p, overflow_p);
9705 break;
9707 case POINTER_PLUS_EXPR:
9708 case PLUS_EXPR:
9709 case MINUS_EXPR:
9710 case MULT_EXPR:
9711 case TRUNC_DIV_EXPR:
9712 case CEIL_DIV_EXPR:
9713 case FLOOR_DIV_EXPR:
9714 case ROUND_DIV_EXPR:
9715 case TRUNC_MOD_EXPR:
9716 case CEIL_MOD_EXPR:
9717 case ROUND_MOD_EXPR:
9718 case RDIV_EXPR:
9719 case EXACT_DIV_EXPR:
9720 case MIN_EXPR:
9721 case MAX_EXPR:
9722 case LSHIFT_EXPR:
9723 case RSHIFT_EXPR:
9724 case LROTATE_EXPR:
9725 case RROTATE_EXPR:
9726 case BIT_IOR_EXPR:
9727 case BIT_XOR_EXPR:
9728 case BIT_AND_EXPR:
9729 case TRUTH_XOR_EXPR:
9730 case LT_EXPR:
9731 case LE_EXPR:
9732 case GT_EXPR:
9733 case GE_EXPR:
9734 case EQ_EXPR:
9735 case NE_EXPR:
9736 case UNORDERED_EXPR:
9737 case ORDERED_EXPR:
9738 case UNLT_EXPR:
9739 case UNLE_EXPR:
9740 case UNGT_EXPR:
9741 case UNGE_EXPR:
9742 case UNEQ_EXPR:
9743 case LTGT_EXPR:
9744 case RANGE_EXPR:
9745 case COMPLEX_EXPR:
9746 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9747 non_constant_p, overflow_p);
9748 break;
9750 /* fold can introduce non-IF versions of these; still treat them as
9751 short-circuiting. */
9752 case TRUTH_AND_EXPR:
9753 case TRUTH_ANDIF_EXPR:
9754 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9755 boolean_true_node,
9756 allow_non_constant, addr,
9757 non_constant_p, overflow_p);
9758 break;
9760 case TRUTH_OR_EXPR:
9761 case TRUTH_ORIF_EXPR:
9762 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9763 boolean_false_node,
9764 allow_non_constant, addr,
9765 non_constant_p, overflow_p);
9766 break;
9768 case ARRAY_REF:
9769 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9770 non_constant_p, overflow_p);
9771 break;
9773 case COMPONENT_REF:
9774 if (is_overloaded_fn (t))
9776 /* We can only get here in checking mode via
9777 build_non_dependent_expr, because any expression that
9778 calls or takes the address of the function will have
9779 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9780 gcc_checking_assert (allow_non_constant || errorcount);
9781 *non_constant_p = true;
9782 return t;
9784 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9785 non_constant_p, overflow_p);
9786 break;
9788 case BIT_FIELD_REF:
9789 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9790 non_constant_p, overflow_p);
9791 break;
9793 case COND_EXPR:
9794 case VEC_COND_EXPR:
9795 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9796 non_constant_p, overflow_p);
9797 break;
9799 case CONSTRUCTOR:
9800 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9801 non_constant_p, overflow_p);
9802 break;
9804 case VEC_INIT_EXPR:
9805 /* We can get this in a defaulted constructor for a class with a
9806 non-static data member of array type. Either the initializer will
9807 be NULL, meaning default-initialization, or it will be an lvalue
9808 or xvalue of the same type, meaning direct-initialization from the
9809 corresponding member. */
9810 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9811 non_constant_p, overflow_p);
9812 break;
9814 case FMA_EXPR:
9815 case VEC_PERM_EXPR:
9816 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9817 non_constant_p, overflow_p);
9818 break;
9820 case CONVERT_EXPR:
9821 case VIEW_CONVERT_EXPR:
9822 case NOP_EXPR:
9824 tree oldop = TREE_OPERAND (t, 0);
9825 tree op = cxx_eval_constant_expression (call, oldop,
9826 allow_non_constant, addr,
9827 non_constant_p, overflow_p);
9828 if (*non_constant_p)
9829 return t;
9830 if (POINTER_TYPE_P (TREE_TYPE (t))
9831 && TREE_CODE (op) == INTEGER_CST
9832 && !integer_zerop (op))
9834 if (!allow_non_constant)
9835 error_at (EXPR_LOC_OR_LOC (t, input_location),
9836 "reinterpret_cast from integer to pointer");
9837 *non_constant_p = true;
9838 return t;
9840 if (op == oldop)
9841 /* We didn't fold at the top so we could check for ptr-int
9842 conversion. */
9843 return fold (t);
9844 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9845 /* Conversion of an out-of-range value has implementation-defined
9846 behavior; the language considers it different from arithmetic
9847 overflow, which is undefined. */
9848 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9849 TREE_OVERFLOW (r) = false;
9851 break;
9853 case EMPTY_CLASS_EXPR:
9854 /* This is good enough for a function argument that might not get
9855 used, and they can't do anything with it, so just return it. */
9856 return t;
9858 case LAMBDA_EXPR:
9859 case PREINCREMENT_EXPR:
9860 case POSTINCREMENT_EXPR:
9861 case PREDECREMENT_EXPR:
9862 case POSTDECREMENT_EXPR:
9863 case NEW_EXPR:
9864 case VEC_NEW_EXPR:
9865 case DELETE_EXPR:
9866 case VEC_DELETE_EXPR:
9867 case THROW_EXPR:
9868 case MODIFY_EXPR:
9869 case MODOP_EXPR:
9870 /* GCC internal stuff. */
9871 case VA_ARG_EXPR:
9872 case OBJ_TYPE_REF:
9873 case WITH_CLEANUP_EXPR:
9874 case STATEMENT_LIST:
9875 case BIND_EXPR:
9876 case NON_DEPENDENT_EXPR:
9877 case BASELINK:
9878 case EXPR_STMT:
9879 case OFFSET_REF:
9880 if (!allow_non_constant)
9881 error_at (EXPR_LOC_OR_LOC (t, input_location),
9882 "expression %qE is not a constant-expression", t);
9883 *non_constant_p = true;
9884 break;
9886 default:
9887 internal_error ("unexpected expression %qE of kind %s", t,
9888 get_tree_code_name (TREE_CODE (t)));
9889 *non_constant_p = true;
9890 break;
9893 if (r == error_mark_node)
9894 *non_constant_p = true;
9896 if (*non_constant_p)
9897 return t;
9898 else
9899 return r;
9902 static tree
9903 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9905 bool non_constant_p = false;
9906 bool overflow_p = false;
9907 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9908 false, &non_constant_p, &overflow_p);
9910 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9912 if (TREE_CODE (t) != CONSTRUCTOR
9913 && cp_has_mutable_p (TREE_TYPE (t)))
9915 /* We allow a mutable type if the original expression was a
9916 CONSTRUCTOR so that we can do aggregate initialization of
9917 constexpr variables. */
9918 if (!allow_non_constant)
9919 error ("%qT cannot be the type of a complete constant expression "
9920 "because it has mutable sub-objects", TREE_TYPE (t));
9921 non_constant_p = true;
9924 /* Technically we should check this for all subexpressions, but that
9925 runs into problems with our internal representation of pointer
9926 subtraction and the 5.19 rules are still in flux. */
9927 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9928 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9929 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9931 if (!allow_non_constant)
9932 error ("conversion from pointer type %qT "
9933 "to arithmetic type %qT in a constant-expression",
9934 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9935 non_constant_p = true;
9938 if (!non_constant_p && overflow_p)
9939 non_constant_p = true;
9941 if (non_constant_p && !allow_non_constant)
9942 return error_mark_node;
9943 else if (non_constant_p && TREE_CONSTANT (r))
9945 /* This isn't actually constant, so unset TREE_CONSTANT. */
9946 if (EXPR_P (r))
9947 r = copy_node (r);
9948 else if (TREE_CODE (r) == CONSTRUCTOR)
9949 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9950 else
9951 r = build_nop (TREE_TYPE (r), r);
9952 TREE_CONSTANT (r) = false;
9954 else if (non_constant_p || r == t)
9955 return t;
9957 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9959 if (TREE_CODE (t) == TARGET_EXPR
9960 && TARGET_EXPR_INITIAL (t) == r)
9961 return t;
9962 else
9964 r = get_target_expr (r);
9965 TREE_CONSTANT (r) = true;
9966 return r;
9969 else
9970 return r;
9973 /* Returns true if T is a valid subexpression of a constant expression,
9974 even if it isn't itself a constant expression. */
9976 bool
9977 is_sub_constant_expr (tree t)
9979 bool non_constant_p = false;
9980 bool overflow_p = false;
9981 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9982 &overflow_p);
9983 return !non_constant_p && !overflow_p;
9986 /* If T represents a constant expression returns its reduced value.
9987 Otherwise return error_mark_node. If T is dependent, then
9988 return NULL. */
9990 tree
9991 cxx_constant_value (tree t)
9993 return cxx_eval_outermost_constant_expr (t, false);
9996 /* If T is a constant expression, returns its reduced value.
9997 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9998 Otherwise, returns a version of T without TREE_CONSTANT. */
10000 tree
10001 maybe_constant_value (tree t)
10003 tree r;
10005 if (instantiation_dependent_expression_p (t)
10006 || type_unknown_p (t)
10007 || BRACE_ENCLOSED_INITIALIZER_P (t)
10008 || !potential_constant_expression (t))
10010 if (TREE_OVERFLOW_P (t))
10012 t = build_nop (TREE_TYPE (t), t);
10013 TREE_CONSTANT (t) = false;
10015 return t;
10018 r = cxx_eval_outermost_constant_expr (t, true);
10019 #ifdef ENABLE_CHECKING
10020 /* cp_tree_equal looks through NOPs, so allow them. */
10021 gcc_assert (r == t
10022 || CONVERT_EXPR_P (t)
10023 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10024 || !cp_tree_equal (r, t));
10025 #endif
10026 return r;
10029 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
10030 than wrapped in a TARGET_EXPR. */
10032 tree
10033 maybe_constant_init (tree t)
10035 t = maybe_constant_value (t);
10036 if (TREE_CODE (t) == TARGET_EXPR)
10038 tree init = TARGET_EXPR_INITIAL (t);
10039 if (TREE_CODE (init) == CONSTRUCTOR)
10040 t = init;
10042 return t;
10045 #if 0
10046 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
10047 /* Return true if the object referred to by REF has automatic or thread
10048 local storage. */
10050 enum { ck_ok, ck_bad, ck_unknown };
10051 static int
10052 check_automatic_or_tls (tree ref)
10054 enum machine_mode mode;
10055 HOST_WIDE_INT bitsize, bitpos;
10056 tree offset;
10057 int volatilep = 0, unsignedp = 0;
10058 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
10059 &mode, &unsignedp, &volatilep, false);
10060 duration_kind dk;
10062 /* If there isn't a decl in the middle, we don't know the linkage here,
10063 and this isn't a constant expression anyway. */
10064 if (!DECL_P (decl))
10065 return ck_unknown;
10066 dk = decl_storage_duration (decl);
10067 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
10069 #endif
10071 /* Return true if T denotes a potentially constant expression. Issue
10072 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
10073 an lvalue-rvalue conversion is implied.
10075 C++0x [expr.const] used to say
10077 6 An expression is a potential constant expression if it is
10078 a constant expression where all occurrences of function
10079 parameters are replaced by arbitrary constant expressions
10080 of the appropriate type.
10082 2 A conditional expression is a constant expression unless it
10083 involves one of the following as a potentially evaluated
10084 subexpression (3.2), but subexpressions of logical AND (5.14),
10085 logical OR (5.15), and conditional (5.16) operations that are
10086 not evaluated are not considered. */
10088 static bool
10089 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10091 enum { any = false, rval = true };
10092 int i;
10093 tree tmp;
10095 if (t == error_mark_node)
10096 return false;
10097 if (t == NULL_TREE)
10098 return true;
10099 if (TREE_THIS_VOLATILE (t))
10101 if (flags & tf_error)
10102 error ("expression %qE has side-effects", t);
10103 return false;
10105 if (CONSTANT_CLASS_P (t))
10106 return true;
10108 switch (TREE_CODE (t))
10110 case FUNCTION_DECL:
10111 case BASELINK:
10112 case TEMPLATE_DECL:
10113 case OVERLOAD:
10114 case TEMPLATE_ID_EXPR:
10115 case LABEL_DECL:
10116 case LABEL_EXPR:
10117 case CONST_DECL:
10118 case SIZEOF_EXPR:
10119 case ALIGNOF_EXPR:
10120 case OFFSETOF_EXPR:
10121 case NOEXCEPT_EXPR:
10122 case TEMPLATE_PARM_INDEX:
10123 case TRAIT_EXPR:
10124 case IDENTIFIER_NODE:
10125 case USERDEF_LITERAL:
10126 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10127 case FIELD_DECL:
10128 case PARM_DECL:
10129 case USING_DECL:
10130 case REQUIRES_EXPR:
10131 case EXPR_REQ:
10132 case TYPE_REQ:
10133 case NESTED_REQ:
10134 case VALIDEXPR_EXPR:
10135 case VALIDTYPE_EXPR:
10136 case CONSTEXPR_EXPR:
10137 return true;
10139 case AGGR_INIT_EXPR:
10140 case CALL_EXPR:
10141 /* -- an invocation of a function other than a constexpr function
10142 or a constexpr constructor. */
10144 tree fun = get_function_named_in_call (t);
10145 const int nargs = call_expr_nargs (t);
10146 i = 0;
10148 if (is_overloaded_fn (fun))
10150 if (TREE_CODE (fun) == FUNCTION_DECL)
10152 if (builtin_valid_in_constant_expr_p (fun))
10153 return true;
10154 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10155 /* Allow any built-in function; if the expansion
10156 isn't constant, we'll deal with that then. */
10157 && !is_builtin_fn (fun))
10159 if (flags & tf_error)
10161 error_at (EXPR_LOC_OR_LOC (t, input_location),
10162 "call to non-constexpr function %qD", fun);
10163 explain_invalid_constexpr_fn (fun);
10165 return false;
10167 /* A call to a non-static member function takes the address
10168 of the object as the first argument. But in a constant
10169 expression the address will be folded away, so look
10170 through it now. */
10171 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10172 && !DECL_CONSTRUCTOR_P (fun))
10174 tree x = get_nth_callarg (t, 0);
10175 if (is_this_parameter (x))
10177 if (DECL_CONTEXT (x) == NULL_TREE
10178 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10180 if (flags & tf_error)
10181 sorry ("calling a member function of the "
10182 "object being constructed in a constant "
10183 "expression");
10184 return false;
10186 /* Otherwise OK. */;
10188 else if (!potential_constant_expression_1 (x, rval, flags))
10189 return false;
10190 i = 1;
10193 else
10195 if (!potential_constant_expression_1 (fun, true, flags))
10196 return false;
10197 fun = get_first_fn (fun);
10199 /* Skip initial arguments to base constructors. */
10200 if (DECL_BASE_CONSTRUCTOR_P (fun))
10201 i = num_artificial_parms_for (fun);
10202 fun = DECL_ORIGIN (fun);
10204 else
10206 if (potential_constant_expression_1 (fun, rval, flags))
10207 /* Might end up being a constant function pointer. */;
10208 else
10209 return false;
10211 for (; i < nargs; ++i)
10213 tree x = get_nth_callarg (t, i);
10214 if (!potential_constant_expression_1 (x, rval, flags))
10215 return false;
10217 return true;
10220 case NON_LVALUE_EXPR:
10221 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10222 -- an lvalue of integral type that refers to a non-volatile
10223 const variable or static data member initialized with
10224 constant expressions, or
10226 -- an lvalue of literal type that refers to non-volatile
10227 object defined with constexpr, or that refers to a
10228 sub-object of such an object; */
10229 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10231 case VAR_DECL:
10232 if (want_rval && !decl_constant_var_p (t)
10233 && !dependent_type_p (TREE_TYPE (t)))
10235 if (flags & tf_error)
10236 non_const_var_error (t);
10237 return false;
10239 return true;
10241 case NOP_EXPR:
10242 case CONVERT_EXPR:
10243 case VIEW_CONVERT_EXPR:
10244 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10245 may change to something more specific to type-punning (DR 1312). */
10247 tree from = TREE_OPERAND (t, 0);
10248 if (POINTER_TYPE_P (TREE_TYPE (t))
10249 && TREE_CODE (from) == INTEGER_CST
10250 && !integer_zerop (from))
10252 if (flags & tf_error)
10253 error_at (EXPR_LOC_OR_LOC (t, input_location),
10254 "reinterpret_cast from integer to pointer");
10255 return false;
10257 return (potential_constant_expression_1
10258 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10261 case ADDR_EXPR:
10262 /* -- a unary operator & that is applied to an lvalue that
10263 designates an object with thread or automatic storage
10264 duration; */
10265 t = TREE_OPERAND (t, 0);
10266 #if 0
10267 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10268 any checking here, as we might dereference the pointer later. If
10269 we remove this code, also remove check_automatic_or_tls. */
10270 i = check_automatic_or_tls (t);
10271 if (i == ck_ok)
10272 return true;
10273 if (i == ck_bad)
10275 if (flags & tf_error)
10276 error ("address-of an object %qE with thread local or "
10277 "automatic storage is not a constant expression", t);
10278 return false;
10280 #endif
10281 return potential_constant_expression_1 (t, any, flags);
10283 case COMPONENT_REF:
10284 case BIT_FIELD_REF:
10285 case ARROW_EXPR:
10286 case OFFSET_REF:
10287 /* -- a class member access unless its postfix-expression is
10288 of literal type or of pointer to literal type. */
10289 /* This test would be redundant, as it follows from the
10290 postfix-expression being a potential constant expression. */
10291 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10292 want_rval, flags);
10294 case EXPR_PACK_EXPANSION:
10295 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10296 want_rval, flags);
10298 case INDIRECT_REF:
10300 tree x = TREE_OPERAND (t, 0);
10301 STRIP_NOPS (x);
10302 if (is_this_parameter (x))
10304 if (DECL_CONTEXT (x)
10305 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10307 if (flags & tf_error)
10308 error ("use of %<this%> in a constant expression");
10309 return false;
10311 if (want_rval && DECL_CONTEXT (x)
10312 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10314 if (flags & tf_error)
10315 sorry ("use of the value of the object being constructed "
10316 "in a constant expression");
10317 return false;
10319 return true;
10321 return potential_constant_expression_1 (x, rval, flags);
10324 case LAMBDA_EXPR:
10325 case DYNAMIC_CAST_EXPR:
10326 case PSEUDO_DTOR_EXPR:
10327 case PREINCREMENT_EXPR:
10328 case POSTINCREMENT_EXPR:
10329 case PREDECREMENT_EXPR:
10330 case POSTDECREMENT_EXPR:
10331 case NEW_EXPR:
10332 case VEC_NEW_EXPR:
10333 case DELETE_EXPR:
10334 case VEC_DELETE_EXPR:
10335 case THROW_EXPR:
10336 case MODIFY_EXPR:
10337 case MODOP_EXPR:
10338 case OMP_ATOMIC:
10339 case OMP_ATOMIC_READ:
10340 case OMP_ATOMIC_CAPTURE_OLD:
10341 case OMP_ATOMIC_CAPTURE_NEW:
10342 /* GCC internal stuff. */
10343 case VA_ARG_EXPR:
10344 case OBJ_TYPE_REF:
10345 case WITH_CLEANUP_EXPR:
10346 case CLEANUP_POINT_EXPR:
10347 case MUST_NOT_THROW_EXPR:
10348 case TRY_CATCH_EXPR:
10349 case STATEMENT_LIST:
10350 /* Don't bother trying to define a subset of statement-expressions to
10351 be constant-expressions, at least for now. */
10352 case STMT_EXPR:
10353 case EXPR_STMT:
10354 case BIND_EXPR:
10355 case TRANSACTION_EXPR:
10356 case IF_STMT:
10357 case DO_STMT:
10358 case FOR_STMT:
10359 case WHILE_STMT:
10360 if (flags & tf_error)
10361 error ("expression %qE is not a constant-expression", t);
10362 return false;
10364 case TYPEID_EXPR:
10365 /* -- a typeid expression whose operand is of polymorphic
10366 class type; */
10368 tree e = TREE_OPERAND (t, 0);
10369 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10370 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10372 if (flags & tf_error)
10373 error ("typeid-expression is not a constant expression "
10374 "because %qE is of polymorphic type", e);
10375 return false;
10377 return true;
10380 case MINUS_EXPR:
10381 /* -- a subtraction where both operands are pointers. */
10382 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10383 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10385 if (flags & tf_error)
10386 error ("difference of two pointer expressions is not "
10387 "a constant expression");
10388 return false;
10390 want_rval = true;
10391 goto binary;
10393 case LT_EXPR:
10394 case LE_EXPR:
10395 case GT_EXPR:
10396 case GE_EXPR:
10397 case EQ_EXPR:
10398 case NE_EXPR:
10399 /* -- a relational or equality operator where at least
10400 one of the operands is a pointer. */
10401 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10402 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10404 if (flags & tf_error)
10405 error ("pointer comparison expression is not a "
10406 "constant expression");
10407 return false;
10409 want_rval = true;
10410 goto binary;
10412 case BIT_NOT_EXPR:
10413 /* A destructor. */
10414 if (TYPE_P (TREE_OPERAND (t, 0)))
10415 return true;
10416 /* else fall through. */
10418 case REALPART_EXPR:
10419 case IMAGPART_EXPR:
10420 case CONJ_EXPR:
10421 case SAVE_EXPR:
10422 case FIX_TRUNC_EXPR:
10423 case FLOAT_EXPR:
10424 case NEGATE_EXPR:
10425 case ABS_EXPR:
10426 case TRUTH_NOT_EXPR:
10427 case FIXED_CONVERT_EXPR:
10428 case UNARY_PLUS_EXPR:
10429 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10430 flags);
10432 case CAST_EXPR:
10433 case CONST_CAST_EXPR:
10434 case STATIC_CAST_EXPR:
10435 case REINTERPRET_CAST_EXPR:
10436 case IMPLICIT_CONV_EXPR:
10437 if (cxx_dialect < cxx11
10438 && !dependent_type_p (TREE_TYPE (t))
10439 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10440 /* In C++98, a conversion to non-integral type can't be part of a
10441 constant expression. */
10443 if (flags & tf_error)
10444 error ("cast to non-integral type %qT in a constant expression",
10445 TREE_TYPE (t));
10446 return false;
10449 return (potential_constant_expression_1
10450 (TREE_OPERAND (t, 0),
10451 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10453 case PAREN_EXPR:
10454 case NON_DEPENDENT_EXPR:
10455 /* For convenience. */
10456 case RETURN_EXPR:
10457 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10458 want_rval, flags);
10460 case SCOPE_REF:
10461 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10462 want_rval, flags);
10464 case TARGET_EXPR:
10465 if (!literal_type_p (TREE_TYPE (t)))
10467 if (flags & tf_error)
10469 error ("temporary of non-literal type %qT in a "
10470 "constant expression", TREE_TYPE (t));
10471 explain_non_literal_class (TREE_TYPE (t));
10473 return false;
10475 case INIT_EXPR:
10476 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10477 rval, flags);
10479 case CONSTRUCTOR:
10481 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10482 constructor_elt *ce;
10483 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10484 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10485 return false;
10486 return true;
10489 case TREE_LIST:
10491 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10492 || DECL_P (TREE_PURPOSE (t)));
10493 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10494 flags))
10495 return false;
10496 if (TREE_CHAIN (t) == NULL_TREE)
10497 return true;
10498 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10499 flags);
10502 case TRUNC_DIV_EXPR:
10503 case CEIL_DIV_EXPR:
10504 case FLOOR_DIV_EXPR:
10505 case ROUND_DIV_EXPR:
10506 case TRUNC_MOD_EXPR:
10507 case CEIL_MOD_EXPR:
10508 case ROUND_MOD_EXPR:
10510 tree denom = TREE_OPERAND (t, 1);
10511 if (!potential_constant_expression_1 (denom, rval, flags))
10512 return false;
10513 /* We can't call cxx_eval_outermost_constant_expr on an expression
10514 that hasn't been through fold_non_dependent_expr yet. */
10515 if (!processing_template_decl)
10516 denom = cxx_eval_outermost_constant_expr (denom, true);
10517 if (integer_zerop (denom))
10519 if (flags & tf_error)
10520 error ("division by zero is not a constant-expression");
10521 return false;
10523 else
10525 want_rval = true;
10526 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10527 want_rval, flags);
10531 case COMPOUND_EXPR:
10533 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10534 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10535 introduced by build_call_a. */
10536 tree op0 = TREE_OPERAND (t, 0);
10537 tree op1 = TREE_OPERAND (t, 1);
10538 STRIP_NOPS (op1);
10539 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10540 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10541 return potential_constant_expression_1 (op0, want_rval, flags);
10542 else
10543 goto binary;
10546 /* If the first operand is the non-short-circuit constant, look at
10547 the second operand; otherwise we only care about the first one for
10548 potentiality. */
10549 case TRUTH_AND_EXPR:
10550 case TRUTH_ANDIF_EXPR:
10551 tmp = boolean_true_node;
10552 goto truth;
10553 case TRUTH_OR_EXPR:
10554 case TRUTH_ORIF_EXPR:
10555 tmp = boolean_false_node;
10556 truth:
10558 tree op = TREE_OPERAND (t, 0);
10559 if (!potential_constant_expression_1 (op, rval, flags))
10560 return false;
10561 if (!processing_template_decl)
10562 op = cxx_eval_outermost_constant_expr (op, true);
10563 if (tree_int_cst_equal (op, tmp))
10564 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10565 else
10566 return true;
10569 case PLUS_EXPR:
10570 case MULT_EXPR:
10571 case POINTER_PLUS_EXPR:
10572 case RDIV_EXPR:
10573 case EXACT_DIV_EXPR:
10574 case MIN_EXPR:
10575 case MAX_EXPR:
10576 case LSHIFT_EXPR:
10577 case RSHIFT_EXPR:
10578 case LROTATE_EXPR:
10579 case RROTATE_EXPR:
10580 case BIT_IOR_EXPR:
10581 case BIT_XOR_EXPR:
10582 case BIT_AND_EXPR:
10583 case TRUTH_XOR_EXPR:
10584 case UNORDERED_EXPR:
10585 case ORDERED_EXPR:
10586 case UNLT_EXPR:
10587 case UNLE_EXPR:
10588 case UNGT_EXPR:
10589 case UNGE_EXPR:
10590 case UNEQ_EXPR:
10591 case LTGT_EXPR:
10592 case RANGE_EXPR:
10593 case COMPLEX_EXPR:
10594 want_rval = true;
10595 /* Fall through. */
10596 case ARRAY_REF:
10597 case ARRAY_RANGE_REF:
10598 case MEMBER_REF:
10599 case DOTSTAR_EXPR:
10600 binary:
10601 for (i = 0; i < 2; ++i)
10602 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10603 want_rval, flags))
10604 return false;
10605 return true;
10607 case CILK_SYNC_STMT:
10608 case CILK_SPAWN_STMT:
10609 case ARRAY_NOTATION_REF:
10610 return false;
10612 case FMA_EXPR:
10613 case VEC_PERM_EXPR:
10614 for (i = 0; i < 3; ++i)
10615 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10616 true, flags))
10617 return false;
10618 return true;
10620 case COND_EXPR:
10621 case VEC_COND_EXPR:
10622 /* If the condition is a known constant, we know which of the legs we
10623 care about; otherwise we only require that the condition and
10624 either of the legs be potentially constant. */
10625 tmp = TREE_OPERAND (t, 0);
10626 if (!potential_constant_expression_1 (tmp, rval, flags))
10627 return false;
10628 if (!processing_template_decl)
10629 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10630 if (integer_zerop (tmp))
10631 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10632 want_rval, flags);
10633 else if (TREE_CODE (tmp) == INTEGER_CST)
10634 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10635 want_rval, flags);
10636 for (i = 1; i < 3; ++i)
10637 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10638 want_rval, tf_none))
10639 return true;
10640 if (flags & tf_error)
10641 error ("expression %qE is not a constant-expression", t);
10642 return false;
10644 case VEC_INIT_EXPR:
10645 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10646 return true;
10647 if (flags & tf_error)
10649 error ("non-constant array initialization");
10650 diagnose_non_constexpr_vec_init (t);
10652 return false;
10654 default:
10655 if (objc_is_property_ref (t))
10656 return false;
10658 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10659 gcc_unreachable();
10660 return false;
10664 /* The main entry point to the above. */
10666 bool
10667 potential_constant_expression (tree t)
10669 return potential_constant_expression_1 (t, false, tf_none);
10672 /* As above, but require a constant rvalue. */
10674 bool
10675 potential_rvalue_constant_expression (tree t)
10677 return potential_constant_expression_1 (t, true, tf_none);
10680 /* Like above, but complain about non-constant expressions. */
10682 bool
10683 require_potential_constant_expression (tree t)
10685 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10688 /* Cross product of the above. */
10690 bool
10691 require_potential_rvalue_constant_expression (tree t)
10693 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10696 /* Insert the deduced return type for an auto function. */
10698 void
10699 apply_deduced_return_type (tree fco, tree return_type)
10701 tree result;
10703 if (return_type == error_mark_node)
10704 return;
10706 if (LAMBDA_FUNCTION_P (fco))
10708 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10709 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10712 if (DECL_CONV_FN_P (fco))
10713 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10715 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10717 result = DECL_RESULT (fco);
10718 if (result == NULL_TREE)
10719 return;
10720 if (TREE_TYPE (result) == return_type)
10721 return;
10723 /* We already have a DECL_RESULT from start_preparsed_function.
10724 Now we need to redo the work it and allocate_struct_function
10725 did to reflect the new type. */
10726 gcc_assert (current_function_decl == fco);
10727 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10728 TYPE_MAIN_VARIANT (return_type));
10729 DECL_ARTIFICIAL (result) = 1;
10730 DECL_IGNORED_P (result) = 1;
10731 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10732 result);
10734 DECL_RESULT (fco) = result;
10736 if (!processing_template_decl)
10738 bool aggr = aggregate_value_p (result, fco);
10739 #ifdef PCC_STATIC_STRUCT_RETURN
10740 cfun->returns_pcc_struct = aggr;
10741 #endif
10742 cfun->returns_struct = aggr;
10747 /* DECL is a local variable or parameter from the surrounding scope of a
10748 lambda-expression. Returns the decltype for a use of the capture field
10749 for DECL even if it hasn't been captured yet. */
10751 static tree
10752 capture_decltype (tree decl)
10754 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10755 /* FIXME do lookup instead of list walk? */
10756 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10757 tree type;
10759 if (cap)
10760 type = TREE_TYPE (TREE_PURPOSE (cap));
10761 else
10762 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10764 case CPLD_NONE:
10765 error ("%qD is not captured", decl);
10766 return error_mark_node;
10768 case CPLD_COPY:
10769 type = TREE_TYPE (decl);
10770 if (TREE_CODE (type) == REFERENCE_TYPE
10771 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10772 type = TREE_TYPE (type);
10773 break;
10775 case CPLD_REFERENCE:
10776 type = TREE_TYPE (decl);
10777 if (TREE_CODE (type) != REFERENCE_TYPE)
10778 type = build_reference_type (TREE_TYPE (decl));
10779 break;
10781 default:
10782 gcc_unreachable ();
10785 if (TREE_CODE (type) != REFERENCE_TYPE)
10787 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10788 type = cp_build_qualified_type (type, (cp_type_quals (type)
10789 |TYPE_QUAL_CONST));
10790 type = build_reference_type (type);
10792 return type;
10795 #include "gt-cp-semantics.h"