2014-01-30 Alangi Derick <alangiderick@gmail.com>
[official-gcc.git] / gcc / cp / semantics.c
blob9fb4fc0c692dbe68e6c9e49f7203fc77ace06e06
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 (cond);
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 (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1609 && TREE_CODE (expr) != SCOPE_REF)
1610 return expr;
1612 if (processing_template_decl)
1613 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1614 else
1616 cp_lvalue_kind kind = lvalue_kind (expr);
1617 if ((kind & ~clk_class) != clk_none)
1619 tree type = unlowered_expr_type (expr);
1620 bool rval = !!(kind & clk_rvalueref);
1621 type = cp_build_reference_type (type, rval);
1622 expr = build_static_cast (type, expr, tf_warning_or_error);
1626 return expr;
1629 /* Finish a parenthesized expression EXPR. */
1631 tree
1632 finish_parenthesized_expr (tree expr)
1634 if (EXPR_P (expr))
1635 /* This inhibits warnings in c_common_truthvalue_conversion. */
1636 TREE_NO_WARNING (expr) = 1;
1638 if (TREE_CODE (expr) == OFFSET_REF
1639 || TREE_CODE (expr) == SCOPE_REF)
1640 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1641 enclosed in parentheses. */
1642 PTRMEM_OK_P (expr) = 0;
1644 if (TREE_CODE (expr) == STRING_CST)
1645 PAREN_STRING_LITERAL_P (expr) = 1;
1647 expr = force_paren_expr (expr);
1649 return expr;
1652 /* Finish a reference to a non-static data member (DECL) that is not
1653 preceded by `.' or `->'. */
1655 tree
1656 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1658 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1660 if (!object)
1662 tree scope = qualifying_scope;
1663 if (scope == NULL_TREE)
1664 scope = context_for_name_lookup (decl);
1665 object = maybe_dummy_object (scope, NULL);
1668 object = maybe_resolve_dummy (object);
1669 if (object == error_mark_node)
1670 return error_mark_node;
1672 /* DR 613: Can use non-static data members without an associated
1673 object in sizeof/decltype/alignof. */
1674 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1675 && (!processing_template_decl || !current_class_ref))
1677 if (current_function_decl
1678 && DECL_STATIC_FUNCTION_P (current_function_decl))
1679 error ("invalid use of member %q+D in static member function", decl);
1680 else
1681 error ("invalid use of non-static data member %q+D", decl);
1682 error ("from this location");
1684 return error_mark_node;
1687 if (current_class_ptr)
1688 TREE_USED (current_class_ptr) = 1;
1689 if (processing_template_decl && !qualifying_scope)
1691 tree type = TREE_TYPE (decl);
1693 if (TREE_CODE (type) == REFERENCE_TYPE)
1694 /* Quals on the object don't matter. */;
1695 else if (PACK_EXPANSION_P (type))
1696 /* Don't bother trying to represent this. */
1697 type = NULL_TREE;
1698 else
1700 /* Set the cv qualifiers. */
1701 int quals = cp_type_quals (TREE_TYPE (object));
1703 if (DECL_MUTABLE_P (decl))
1704 quals &= ~TYPE_QUAL_CONST;
1706 quals |= cp_type_quals (TREE_TYPE (decl));
1707 type = cp_build_qualified_type (type, quals);
1710 return (convert_from_reference
1711 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1713 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1714 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1715 for now. */
1716 else if (processing_template_decl)
1717 return build_qualified_name (TREE_TYPE (decl),
1718 qualifying_scope,
1719 decl,
1720 /*template_p=*/false);
1721 else
1723 tree access_type = TREE_TYPE (object);
1725 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1726 decl, tf_warning_or_error);
1728 /* If the data member was named `C::M', convert `*this' to `C'
1729 first. */
1730 if (qualifying_scope)
1732 tree binfo = NULL_TREE;
1733 object = build_scoped_ref (object, qualifying_scope,
1734 &binfo);
1737 return build_class_member_access_expr (object, decl,
1738 /*access_path=*/NULL_TREE,
1739 /*preserve_reference=*/false,
1740 tf_warning_or_error);
1744 /* If we are currently parsing a template and we encountered a typedef
1745 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1746 adds the typedef to a list tied to the current template.
1747 At template instantiation time, that list is walked and access check
1748 performed for each typedef.
1749 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1751 void
1752 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1753 tree context,
1754 location_t location)
1756 tree template_info = NULL;
1757 tree cs = current_scope ();
1759 if (!is_typedef_decl (typedef_decl)
1760 || !context
1761 || !CLASS_TYPE_P (context)
1762 || !cs)
1763 return;
1765 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1766 template_info = get_template_info (cs);
1768 if (template_info
1769 && TI_TEMPLATE (template_info)
1770 && !currently_open_class (context))
1771 append_type_to_template_for_access_check (cs, typedef_decl,
1772 context, location);
1775 /* DECL was the declaration to which a qualified-id resolved. Issue
1776 an error message if it is not accessible. If OBJECT_TYPE is
1777 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1778 type of `*x', or `x', respectively. If the DECL was named as
1779 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1781 void
1782 check_accessibility_of_qualified_id (tree decl,
1783 tree object_type,
1784 tree nested_name_specifier)
1786 tree scope;
1787 tree qualifying_type = NULL_TREE;
1789 /* If we are parsing a template declaration and if decl is a typedef,
1790 add it to a list tied to the template.
1791 At template instantiation time, that list will be walked and
1792 access check performed. */
1793 add_typedef_to_current_template_for_access_check (decl,
1794 nested_name_specifier
1795 ? nested_name_specifier
1796 : DECL_CONTEXT (decl),
1797 input_location);
1799 /* If we're not checking, return immediately. */
1800 if (deferred_access_no_check)
1801 return;
1803 /* Determine the SCOPE of DECL. */
1804 scope = context_for_name_lookup (decl);
1805 /* If the SCOPE is not a type, then DECL is not a member. */
1806 if (!TYPE_P (scope))
1807 return;
1808 /* Compute the scope through which DECL is being accessed. */
1809 if (object_type
1810 /* OBJECT_TYPE might not be a class type; consider:
1812 class A { typedef int I; };
1813 I *p;
1814 p->A::I::~I();
1816 In this case, we will have "A::I" as the DECL, but "I" as the
1817 OBJECT_TYPE. */
1818 && CLASS_TYPE_P (object_type)
1819 && DERIVED_FROM_P (scope, object_type))
1820 /* If we are processing a `->' or `.' expression, use the type of the
1821 left-hand side. */
1822 qualifying_type = object_type;
1823 else if (nested_name_specifier)
1825 /* If the reference is to a non-static member of the
1826 current class, treat it as if it were referenced through
1827 `this'. */
1828 if (DECL_NONSTATIC_MEMBER_P (decl)
1829 && current_class_ptr
1830 && DERIVED_FROM_P (scope, current_class_type))
1831 qualifying_type = current_class_type;
1832 /* Otherwise, use the type indicated by the
1833 nested-name-specifier. */
1834 else
1835 qualifying_type = nested_name_specifier;
1837 else
1838 /* Otherwise, the name must be from the current class or one of
1839 its bases. */
1840 qualifying_type = currently_open_derived_class (scope);
1842 if (qualifying_type
1843 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1844 or similar in a default argument value. */
1845 && CLASS_TYPE_P (qualifying_type)
1846 && !dependent_type_p (qualifying_type))
1847 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1848 decl, tf_warning_or_error);
1851 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1852 class named to the left of the "::" operator. DONE is true if this
1853 expression is a complete postfix-expression; it is false if this
1854 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1855 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1856 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1857 is true iff this qualified name appears as a template argument. */
1859 tree
1860 finish_qualified_id_expr (tree qualifying_class,
1861 tree expr,
1862 bool done,
1863 bool address_p,
1864 bool template_p,
1865 bool template_arg_p,
1866 tsubst_flags_t complain)
1868 gcc_assert (TYPE_P (qualifying_class));
1870 if (error_operand_p (expr))
1871 return error_mark_node;
1873 if ((DECL_P (expr) || BASELINK_P (expr))
1874 && !mark_used (expr, complain))
1875 return error_mark_node;
1877 if (template_p)
1878 check_template_keyword (expr);
1880 /* If EXPR occurs as the operand of '&', use special handling that
1881 permits a pointer-to-member. */
1882 if (address_p && done)
1884 if (TREE_CODE (expr) == SCOPE_REF)
1885 expr = TREE_OPERAND (expr, 1);
1886 expr = build_offset_ref (qualifying_class, expr,
1887 /*address_p=*/true, complain);
1888 return expr;
1891 /* No need to check access within an enum. */
1892 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1893 return expr;
1895 /* Within the scope of a class, turn references to non-static
1896 members into expression of the form "this->...". */
1897 if (template_arg_p)
1898 /* But, within a template argument, we do not want make the
1899 transformation, as there is no "this" pointer. */
1901 else if (TREE_CODE (expr) == FIELD_DECL)
1903 push_deferring_access_checks (dk_no_check);
1904 expr = finish_non_static_data_member (expr, NULL_TREE,
1905 qualifying_class);
1906 pop_deferring_access_checks ();
1908 else if (BASELINK_P (expr) && !processing_template_decl)
1910 /* See if any of the functions are non-static members. */
1911 /* If so, the expression may be relative to 'this'. */
1912 if (!shared_member_p (expr)
1913 && current_class_ptr
1914 && DERIVED_FROM_P (qualifying_class,
1915 current_nonlambda_class_type ()))
1916 expr = (build_class_member_access_expr
1917 (maybe_dummy_object (qualifying_class, NULL),
1918 expr,
1919 BASELINK_ACCESS_BINFO (expr),
1920 /*preserve_reference=*/false,
1921 complain));
1922 else if (done)
1923 /* The expression is a qualified name whose address is not
1924 being taken. */
1925 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1926 complain);
1928 else if (BASELINK_P (expr))
1930 else
1932 /* In a template, return a SCOPE_REF for most qualified-ids
1933 so that we can check access at instantiation time. But if
1934 we're looking at a member of the current instantiation, we
1935 know we have access and building up the SCOPE_REF confuses
1936 non-type template argument handling. */
1937 if (processing_template_decl
1938 && !currently_open_class (qualifying_class))
1939 expr = build_qualified_name (TREE_TYPE (expr),
1940 qualifying_class, expr,
1941 template_p);
1943 expr = convert_from_reference (expr);
1946 return expr;
1949 /* Begin a statement-expression. The value returned must be passed to
1950 finish_stmt_expr. */
1952 tree
1953 begin_stmt_expr (void)
1955 return push_stmt_list ();
1958 /* Process the final expression of a statement expression. EXPR can be
1959 NULL, if the final expression is empty. Return a STATEMENT_LIST
1960 containing all the statements in the statement-expression, or
1961 ERROR_MARK_NODE if there was an error. */
1963 tree
1964 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1966 if (error_operand_p (expr))
1968 /* The type of the statement-expression is the type of the last
1969 expression. */
1970 TREE_TYPE (stmt_expr) = error_mark_node;
1971 return error_mark_node;
1974 /* If the last statement does not have "void" type, then the value
1975 of the last statement is the value of the entire expression. */
1976 if (expr)
1978 tree type = TREE_TYPE (expr);
1980 if (processing_template_decl)
1982 expr = build_stmt (input_location, EXPR_STMT, expr);
1983 expr = add_stmt (expr);
1984 /* Mark the last statement so that we can recognize it as such at
1985 template-instantiation time. */
1986 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1988 else if (VOID_TYPE_P (type))
1990 /* Just treat this like an ordinary statement. */
1991 expr = finish_expr_stmt (expr);
1993 else
1995 /* It actually has a value we need to deal with. First, force it
1996 to be an rvalue so that we won't need to build up a copy
1997 constructor call later when we try to assign it to something. */
1998 expr = force_rvalue (expr, tf_warning_or_error);
1999 if (error_operand_p (expr))
2000 return error_mark_node;
2002 /* Update for array-to-pointer decay. */
2003 type = TREE_TYPE (expr);
2005 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2006 normal statement, but don't convert to void or actually add
2007 the EXPR_STMT. */
2008 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2009 expr = maybe_cleanup_point_expr (expr);
2010 add_stmt (expr);
2013 /* The type of the statement-expression is the type of the last
2014 expression. */
2015 TREE_TYPE (stmt_expr) = type;
2018 return stmt_expr;
2021 /* Finish a statement-expression. EXPR should be the value returned
2022 by the previous begin_stmt_expr. Returns an expression
2023 representing the statement-expression. */
2025 tree
2026 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2028 tree type;
2029 tree result;
2031 if (error_operand_p (stmt_expr))
2033 pop_stmt_list (stmt_expr);
2034 return error_mark_node;
2037 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2039 type = TREE_TYPE (stmt_expr);
2040 result = pop_stmt_list (stmt_expr);
2041 TREE_TYPE (result) = type;
2043 if (processing_template_decl)
2045 result = build_min (STMT_EXPR, type, result);
2046 TREE_SIDE_EFFECTS (result) = 1;
2047 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2049 else if (CLASS_TYPE_P (type))
2051 /* Wrap the statement-expression in a TARGET_EXPR so that the
2052 temporary object created by the final expression is destroyed at
2053 the end of the full-expression containing the
2054 statement-expression. */
2055 result = force_target_expr (type, result, tf_warning_or_error);
2058 return result;
2061 /* Returns the expression which provides the value of STMT_EXPR. */
2063 tree
2064 stmt_expr_value_expr (tree stmt_expr)
2066 tree t = STMT_EXPR_STMT (stmt_expr);
2068 if (TREE_CODE (t) == BIND_EXPR)
2069 t = BIND_EXPR_BODY (t);
2071 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2072 t = STATEMENT_LIST_TAIL (t)->stmt;
2074 if (TREE_CODE (t) == EXPR_STMT)
2075 t = EXPR_STMT_EXPR (t);
2077 return t;
2080 /* Return TRUE iff EXPR_STMT is an empty list of
2081 expression statements. */
2083 bool
2084 empty_expr_stmt_p (tree expr_stmt)
2086 tree body = NULL_TREE;
2088 if (expr_stmt == void_zero_node)
2089 return true;
2091 if (expr_stmt)
2093 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2094 body = EXPR_STMT_EXPR (expr_stmt);
2095 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2096 body = expr_stmt;
2099 if (body)
2101 if (TREE_CODE (body) == STATEMENT_LIST)
2102 return tsi_end_p (tsi_start (body));
2103 else
2104 return empty_expr_stmt_p (body);
2106 return false;
2109 /* Perform Koenig lookup. FN is the postfix-expression representing
2110 the function (or functions) to call; ARGS are the arguments to the
2111 call. Returns the functions to be considered by overload resolution. */
2113 tree
2114 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2115 tsubst_flags_t complain)
2117 tree identifier = NULL_TREE;
2118 tree functions = NULL_TREE;
2119 tree tmpl_args = NULL_TREE;
2120 bool template_id = false;
2122 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2124 /* Use a separate flag to handle null args. */
2125 template_id = true;
2126 tmpl_args = TREE_OPERAND (fn, 1);
2127 fn = TREE_OPERAND (fn, 0);
2130 /* Find the name of the overloaded function. */
2131 if (identifier_p (fn))
2132 identifier = fn;
2133 else if (is_overloaded_fn (fn))
2135 functions = fn;
2136 identifier = DECL_NAME (get_first_fn (functions));
2138 else if (DECL_P (fn))
2140 functions = fn;
2141 identifier = DECL_NAME (fn);
2144 /* A call to a namespace-scope function using an unqualified name.
2146 Do Koenig lookup -- unless any of the arguments are
2147 type-dependent. */
2148 if (!any_type_dependent_arguments_p (args)
2149 && !any_dependent_template_arguments_p (tmpl_args))
2151 fn = lookup_arg_dependent (identifier, functions, args);
2152 if (!fn)
2154 /* The unqualified name could not be resolved. */
2155 if (complain)
2156 fn = unqualified_fn_lookup_error (identifier);
2157 else
2158 fn = identifier;
2162 if (fn && template_id)
2163 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2165 return fn;
2168 /* Generate an expression for `FN (ARGS)'. This may change the
2169 contents of ARGS.
2171 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2172 as a virtual call, even if FN is virtual. (This flag is set when
2173 encountering an expression where the function name is explicitly
2174 qualified. For example a call to `X::f' never generates a virtual
2175 call.)
2177 Returns code for the call. */
2179 tree
2180 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2181 bool koenig_p, tsubst_flags_t complain)
2183 tree result;
2184 tree orig_fn;
2185 vec<tree, va_gc> *orig_args = NULL;
2187 if (fn == error_mark_node)
2188 return error_mark_node;
2190 gcc_assert (!TYPE_P (fn));
2192 orig_fn = fn;
2194 if (processing_template_decl)
2196 /* If the call expression is dependent, build a CALL_EXPR node
2197 with no type; type_dependent_expression_p recognizes
2198 expressions with no type as being dependent. */
2199 if (type_dependent_expression_p (fn)
2200 || any_type_dependent_arguments_p (*args)
2201 /* For a non-static member function that doesn't have an
2202 explicit object argument, we need to specifically
2203 test the type dependency of the "this" pointer because it
2204 is not included in *ARGS even though it is considered to
2205 be part of the list of arguments. Note that this is
2206 related to CWG issues 515 and 1005. */
2207 || (TREE_CODE (fn) != COMPONENT_REF
2208 && non_static_member_function_p (fn)
2209 && current_class_ref
2210 && type_dependent_expression_p (current_class_ref)))
2212 result = build_nt_call_vec (fn, *args);
2213 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2214 KOENIG_LOOKUP_P (result) = koenig_p;
2215 if (cfun)
2219 tree fndecl = OVL_CURRENT (fn);
2220 if (TREE_CODE (fndecl) != FUNCTION_DECL
2221 || !TREE_THIS_VOLATILE (fndecl))
2222 break;
2223 fn = OVL_NEXT (fn);
2225 while (fn);
2226 if (!fn)
2227 current_function_returns_abnormally = 1;
2229 return result;
2231 orig_args = make_tree_vector_copy (*args);
2232 if (!BASELINK_P (fn)
2233 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2234 && TREE_TYPE (fn) != unknown_type_node)
2235 fn = build_non_dependent_expr (fn);
2236 make_args_non_dependent (*args);
2239 if (TREE_CODE (fn) == COMPONENT_REF)
2241 tree member = TREE_OPERAND (fn, 1);
2242 if (BASELINK_P (member))
2244 tree object = TREE_OPERAND (fn, 0);
2245 return build_new_method_call (object, member,
2246 args, NULL_TREE,
2247 (disallow_virtual
2248 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2249 : LOOKUP_NORMAL),
2250 /*fn_p=*/NULL,
2251 complain);
2255 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2256 if (TREE_CODE (fn) == ADDR_EXPR
2257 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2258 fn = TREE_OPERAND (fn, 0);
2260 if (is_overloaded_fn (fn))
2261 fn = baselink_for_fns (fn);
2263 result = NULL_TREE;
2264 if (BASELINK_P (fn))
2266 tree object;
2268 /* A call to a member function. From [over.call.func]:
2270 If the keyword this is in scope and refers to the class of
2271 that member function, or a derived class thereof, then the
2272 function call is transformed into a qualified function call
2273 using (*this) as the postfix-expression to the left of the
2274 . operator.... [Otherwise] a contrived object of type T
2275 becomes the implied object argument.
2277 In this situation:
2279 struct A { void f(); };
2280 struct B : public A {};
2281 struct C : public A { void g() { B::f(); }};
2283 "the class of that member function" refers to `A'. But 11.2
2284 [class.access.base] says that we need to convert 'this' to B* as
2285 part of the access, so we pass 'B' to maybe_dummy_object. */
2287 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2288 NULL);
2290 if (processing_template_decl)
2292 if (type_dependent_expression_p (object))
2294 tree ret = build_nt_call_vec (orig_fn, orig_args);
2295 release_tree_vector (orig_args);
2296 return ret;
2298 object = build_non_dependent_expr (object);
2301 result = build_new_method_call (object, fn, args, NULL_TREE,
2302 (disallow_virtual
2303 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2304 : LOOKUP_NORMAL),
2305 /*fn_p=*/NULL,
2306 complain);
2308 else if (is_overloaded_fn (fn))
2310 /* If the function is an overloaded builtin, resolve it. */
2311 if (TREE_CODE (fn) == FUNCTION_DECL
2312 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2313 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2314 result = resolve_overloaded_builtin (input_location, fn, *args);
2316 if (!result)
2318 if (warn_sizeof_pointer_memaccess
2319 && !vec_safe_is_empty (*args)
2320 && !processing_template_decl)
2322 location_t sizeof_arg_loc[3];
2323 tree sizeof_arg[3];
2324 unsigned int i;
2325 for (i = 0; i < 3; i++)
2327 tree t;
2329 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2330 sizeof_arg[i] = NULL_TREE;
2331 if (i >= (*args)->length ())
2332 continue;
2333 t = (**args)[i];
2334 if (TREE_CODE (t) != SIZEOF_EXPR)
2335 continue;
2336 if (SIZEOF_EXPR_TYPE_P (t))
2337 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2338 else
2339 sizeof_arg[i] = TREE_OPERAND (t, 0);
2340 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2342 sizeof_pointer_memaccess_warning
2343 (sizeof_arg_loc, fn, *args,
2344 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2347 /* A call to a namespace-scope function. */
2348 result = build_new_function_call (fn, args, koenig_p, complain);
2351 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2353 if (!vec_safe_is_empty (*args))
2354 error ("arguments to destructor are not allowed");
2355 /* Mark the pseudo-destructor call as having side-effects so
2356 that we do not issue warnings about its use. */
2357 result = build1 (NOP_EXPR,
2358 void_type_node,
2359 TREE_OPERAND (fn, 0));
2360 TREE_SIDE_EFFECTS (result) = 1;
2362 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2363 /* If the "function" is really an object of class type, it might
2364 have an overloaded `operator ()'. */
2365 result = build_op_call (fn, args, complain);
2367 if (!result)
2368 /* A call where the function is unknown. */
2369 result = cp_build_function_call_vec (fn, args, complain);
2371 if (processing_template_decl && result != error_mark_node)
2373 if (INDIRECT_REF_P (result))
2374 result = TREE_OPERAND (result, 0);
2375 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2376 SET_EXPR_LOCATION (result, input_location);
2377 KOENIG_LOOKUP_P (result) = koenig_p;
2378 release_tree_vector (orig_args);
2379 result = convert_from_reference (result);
2382 if (koenig_p)
2384 /* Free garbage OVERLOADs from arg-dependent lookup. */
2385 tree next = NULL_TREE;
2386 for (fn = orig_fn;
2387 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2388 fn = next)
2390 if (processing_template_decl)
2391 /* In a template, we'll re-use them at instantiation time. */
2392 OVL_ARG_DEPENDENT (fn) = false;
2393 else
2395 next = OVL_CHAIN (fn);
2396 ggc_free (fn);
2401 return result;
2404 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2405 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2406 POSTDECREMENT_EXPR.) */
2408 tree
2409 finish_increment_expr (tree expr, enum tree_code code)
2411 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2414 /* Finish a use of `this'. Returns an expression for `this'. */
2416 tree
2417 finish_this_expr (void)
2419 tree result;
2421 if (current_class_ptr)
2423 tree type = TREE_TYPE (current_class_ref);
2425 /* In a lambda expression, 'this' refers to the captured 'this'. */
2426 if (LAMBDA_TYPE_P (type))
2427 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2428 else
2429 result = current_class_ptr;
2431 else if (current_function_decl
2432 && DECL_STATIC_FUNCTION_P (current_function_decl))
2434 error ("%<this%> is unavailable for static member functions");
2435 result = error_mark_node;
2437 else
2439 if (current_function_decl)
2440 error ("invalid use of %<this%> in non-member function");
2441 else
2442 error ("invalid use of %<this%> at top level");
2443 result = error_mark_node;
2446 /* The keyword 'this' is a prvalue expression. */
2447 result = rvalue (result);
2449 return result;
2452 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2453 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2454 the TYPE for the type given. If SCOPE is non-NULL, the expression
2455 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2457 tree
2458 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2459 location_t loc)
2461 if (object == error_mark_node || destructor == error_mark_node)
2462 return error_mark_node;
2464 gcc_assert (TYPE_P (destructor));
2466 if (!processing_template_decl)
2468 if (scope == error_mark_node)
2470 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2471 return error_mark_node;
2473 if (is_auto (destructor))
2474 destructor = TREE_TYPE (object);
2475 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2477 error_at (loc,
2478 "qualified type %qT does not match destructor name ~%qT",
2479 scope, destructor);
2480 return error_mark_node;
2484 /* [expr.pseudo] says both:
2486 The type designated by the pseudo-destructor-name shall be
2487 the same as the object type.
2489 and:
2491 The cv-unqualified versions of the object type and of the
2492 type designated by the pseudo-destructor-name shall be the
2493 same type.
2495 We implement the more generous second sentence, since that is
2496 what most other compilers do. */
2497 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2498 destructor))
2500 error_at (loc, "%qE is not of type %qT", object, destructor);
2501 return error_mark_node;
2505 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2506 scope, destructor);
2509 /* Finish an expression of the form CODE EXPR. */
2511 tree
2512 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2513 tsubst_flags_t complain)
2515 tree result = build_x_unary_op (loc, code, expr, complain);
2516 if ((complain & tf_warning)
2517 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2518 overflow_warning (input_location, result);
2520 return result;
2523 /* Finish a compound-literal expression. TYPE is the type to which
2524 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2526 tree
2527 finish_compound_literal (tree type, tree compound_literal,
2528 tsubst_flags_t complain)
2530 if (type == error_mark_node)
2531 return error_mark_node;
2533 if (TREE_CODE (type) == REFERENCE_TYPE)
2535 compound_literal
2536 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2537 complain);
2538 return cp_build_c_cast (type, compound_literal, complain);
2541 if (!TYPE_OBJ_P (type))
2543 if (complain & tf_error)
2544 error ("compound literal of non-object type %qT", type);
2545 return error_mark_node;
2548 if (processing_template_decl)
2550 TREE_TYPE (compound_literal) = type;
2551 /* Mark the expression as a compound literal. */
2552 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2553 return compound_literal;
2556 type = complete_type (type);
2558 if (TYPE_NON_AGGREGATE_CLASS (type))
2560 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2561 everywhere that deals with function arguments would be a pain, so
2562 just wrap it in a TREE_LIST. The parser set a flag so we know
2563 that it came from T{} rather than T({}). */
2564 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2565 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2566 return build_functional_cast (type, compound_literal, complain);
2569 if (TREE_CODE (type) == ARRAY_TYPE
2570 && check_array_initializer (NULL_TREE, type, compound_literal))
2571 return error_mark_node;
2572 compound_literal = reshape_init (type, compound_literal, complain);
2573 if (SCALAR_TYPE_P (type)
2574 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2575 && (complain & tf_warning_or_error))
2576 check_narrowing (type, compound_literal);
2577 if (TREE_CODE (type) == ARRAY_TYPE
2578 && TYPE_DOMAIN (type) == NULL_TREE)
2580 cp_complete_array_type_or_error (&type, compound_literal,
2581 false, complain);
2582 if (type == error_mark_node)
2583 return error_mark_node;
2585 compound_literal = digest_init (type, compound_literal, complain);
2586 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2587 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2588 /* Put static/constant array temporaries in static variables, but always
2589 represent class temporaries with TARGET_EXPR so we elide copies. */
2590 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2591 && TREE_CODE (type) == ARRAY_TYPE
2592 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2593 && !cp_unevaluated_operand
2594 && initializer_constant_valid_p (compound_literal, type))
2596 tree decl = create_temporary_var (type);
2597 DECL_INITIAL (decl) = compound_literal;
2598 TREE_STATIC (decl) = 1;
2599 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2601 /* 5.19 says that a constant expression can include an
2602 lvalue-rvalue conversion applied to "a glvalue of literal type
2603 that refers to a non-volatile temporary object initialized
2604 with a constant expression". Rather than try to communicate
2605 that this VAR_DECL is a temporary, just mark it constexpr. */
2606 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2607 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2608 TREE_CONSTANT (decl) = true;
2610 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2611 decl = pushdecl_top_level (decl);
2612 DECL_NAME (decl) = make_anon_name ();
2613 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2614 /* Make sure the destructor is callable. */
2615 tree clean = cxx_maybe_build_cleanup (decl, complain);
2616 if (clean == error_mark_node)
2617 return error_mark_node;
2618 return decl;
2620 else
2621 return get_target_expr_sfinae (compound_literal, complain);
2624 /* Return the declaration for the function-name variable indicated by
2625 ID. */
2627 tree
2628 finish_fname (tree id)
2630 tree decl;
2632 decl = fname_decl (input_location, C_RID_CODE (id), id);
2633 if (processing_template_decl && current_function_decl)
2634 decl = DECL_NAME (decl);
2635 return decl;
2638 /* Finish a translation unit. */
2640 void
2641 finish_translation_unit (void)
2643 /* In case there were missing closebraces,
2644 get us back to the global binding level. */
2645 pop_everything ();
2646 while (current_namespace != global_namespace)
2647 pop_namespace ();
2649 /* Do file scope __FUNCTION__ et al. */
2650 finish_fname_decls ();
2653 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2654 Returns the parameter. */
2656 tree
2657 finish_template_type_parm (tree aggr, tree identifier)
2659 if (aggr != class_type_node)
2661 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2662 aggr = class_type_node;
2665 return build_tree_list (aggr, identifier);
2668 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2669 Returns the parameter. */
2671 tree
2672 finish_template_template_parm (tree aggr, tree identifier)
2674 tree decl = build_decl (input_location,
2675 TYPE_DECL, identifier, NULL_TREE);
2676 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2677 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2678 DECL_TEMPLATE_RESULT (tmpl) = decl;
2679 DECL_ARTIFICIAL (decl) = 1;
2680 end_template_decl ();
2682 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2684 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2685 /*is_primary=*/true, /*is_partial=*/false,
2686 /*is_friend=*/0);
2688 return finish_template_type_parm (aggr, tmpl);
2691 /* ARGUMENT is the default-argument value for a template template
2692 parameter. If ARGUMENT is invalid, issue error messages and return
2693 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2695 tree
2696 check_template_template_default_arg (tree argument)
2698 if (TREE_CODE (argument) != TEMPLATE_DECL
2699 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2700 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2702 if (TREE_CODE (argument) == TYPE_DECL)
2703 error ("invalid use of type %qT as a default value for a template "
2704 "template-parameter", TREE_TYPE (argument));
2705 else
2706 error ("invalid default argument for a template template parameter");
2707 return error_mark_node;
2710 return argument;
2713 /* Begin a class definition, as indicated by T. */
2715 tree
2716 begin_class_definition (tree t)
2718 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2719 return error_mark_node;
2721 if (processing_template_parmlist)
2723 error ("definition of %q#T inside template parameter list", t);
2724 return error_mark_node;
2727 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2728 are passed the same as decimal scalar types. */
2729 if (TREE_CODE (t) == RECORD_TYPE
2730 && !processing_template_decl)
2732 tree ns = TYPE_CONTEXT (t);
2733 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2734 && DECL_CONTEXT (ns) == std_node
2735 && DECL_NAME (ns)
2736 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2738 const char *n = TYPE_NAME_STRING (t);
2739 if ((strcmp (n, "decimal32") == 0)
2740 || (strcmp (n, "decimal64") == 0)
2741 || (strcmp (n, "decimal128") == 0))
2742 TYPE_TRANSPARENT_AGGR (t) = 1;
2746 /* A non-implicit typename comes from code like:
2748 template <typename T> struct A {
2749 template <typename U> struct A<T>::B ...
2751 This is erroneous. */
2752 else if (TREE_CODE (t) == TYPENAME_TYPE)
2754 error ("invalid definition of qualified type %qT", t);
2755 t = error_mark_node;
2758 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2760 t = make_class_type (RECORD_TYPE);
2761 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2764 if (TYPE_BEING_DEFINED (t))
2766 t = make_class_type (TREE_CODE (t));
2767 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2769 maybe_process_partial_specialization (t);
2770 pushclass (t);
2771 TYPE_BEING_DEFINED (t) = 1;
2773 if (flag_pack_struct)
2775 tree v;
2776 TYPE_PACKED (t) = 1;
2777 /* Even though the type is being defined for the first time
2778 here, there might have been a forward declaration, so there
2779 might be cv-qualified variants of T. */
2780 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2781 TYPE_PACKED (v) = 1;
2783 /* Reset the interface data, at the earliest possible
2784 moment, as it might have been set via a class foo;
2785 before. */
2786 if (! TYPE_ANONYMOUS_P (t))
2788 struct c_fileinfo *finfo = \
2789 get_fileinfo (LOCATION_FILE (input_location));
2790 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2791 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2792 (t, finfo->interface_unknown);
2794 reset_specialization();
2796 /* Make a declaration for this class in its own scope. */
2797 build_self_reference ();
2799 return t;
2802 /* Finish the member declaration given by DECL. */
2804 void
2805 finish_member_declaration (tree decl)
2807 if (decl == error_mark_node || decl == NULL_TREE)
2808 return;
2810 if (decl == void_type_node)
2811 /* The COMPONENT was a friend, not a member, and so there's
2812 nothing for us to do. */
2813 return;
2815 /* We should see only one DECL at a time. */
2816 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2818 /* Set up access control for DECL. */
2819 TREE_PRIVATE (decl)
2820 = (current_access_specifier == access_private_node);
2821 TREE_PROTECTED (decl)
2822 = (current_access_specifier == access_protected_node);
2823 if (TREE_CODE (decl) == TEMPLATE_DECL)
2825 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2826 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2829 /* Mark the DECL as a member of the current class, unless it's
2830 a member of an enumeration. */
2831 if (TREE_CODE (decl) != CONST_DECL)
2832 DECL_CONTEXT (decl) = current_class_type;
2834 /* Check for bare parameter packs in the member variable declaration. */
2835 if (TREE_CODE (decl) == FIELD_DECL)
2837 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2838 TREE_TYPE (decl) = error_mark_node;
2839 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2840 DECL_ATTRIBUTES (decl) = NULL_TREE;
2843 /* [dcl.link]
2845 A C language linkage is ignored for the names of class members
2846 and the member function type of class member functions. */
2847 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2848 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2850 /* Put functions on the TYPE_METHODS list and everything else on the
2851 TYPE_FIELDS list. Note that these are built up in reverse order.
2852 We reverse them (to obtain declaration order) in finish_struct. */
2853 if (DECL_DECLARES_FUNCTION_P (decl))
2855 /* We also need to add this function to the
2856 CLASSTYPE_METHOD_VEC. */
2857 if (add_method (current_class_type, decl, NULL_TREE))
2859 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2860 TYPE_METHODS (current_class_type) = decl;
2862 maybe_add_class_template_decl_list (current_class_type, decl,
2863 /*friend_p=*/0);
2866 /* Enter the DECL into the scope of the class, if the class
2867 isn't a closure (whose fields are supposed to be unnamed). */
2868 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2869 || pushdecl_class_level (decl))
2871 if (TREE_CODE (decl) == USING_DECL)
2873 /* For now, ignore class-scope USING_DECLS, so that
2874 debugging backends do not see them. */
2875 DECL_IGNORED_P (decl) = 1;
2878 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2879 go at the beginning. The reason is that lookup_field_1
2880 searches the list in order, and we want a field name to
2881 override a type name so that the "struct stat hack" will
2882 work. In particular:
2884 struct S { enum E { }; int E } s;
2885 s.E = 3;
2887 is valid. In addition, the FIELD_DECLs must be maintained in
2888 declaration order so that class layout works as expected.
2889 However, we don't need that order until class layout, so we
2890 save a little time by putting FIELD_DECLs on in reverse order
2891 here, and then reversing them in finish_struct_1. (We could
2892 also keep a pointer to the correct insertion points in the
2893 list.) */
2895 if (TREE_CODE (decl) == TYPE_DECL)
2896 TYPE_FIELDS (current_class_type)
2897 = chainon (TYPE_FIELDS (current_class_type), decl);
2898 else
2900 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2901 TYPE_FIELDS (current_class_type) = decl;
2904 maybe_add_class_template_decl_list (current_class_type, decl,
2905 /*friend_p=*/0);
2908 if (pch_file)
2909 note_decl_for_pch (decl);
2912 /* DECL has been declared while we are building a PCH file. Perform
2913 actions that we might normally undertake lazily, but which can be
2914 performed now so that they do not have to be performed in
2915 translation units which include the PCH file. */
2917 void
2918 note_decl_for_pch (tree decl)
2920 gcc_assert (pch_file);
2922 /* There's a good chance that we'll have to mangle names at some
2923 point, even if only for emission in debugging information. */
2924 if (VAR_OR_FUNCTION_DECL_P (decl)
2925 && !processing_template_decl)
2926 mangle_decl (decl);
2929 /* Finish processing a complete template declaration. The PARMS are
2930 the template parameters. */
2932 void
2933 finish_template_decl (tree parms)
2935 if (parms)
2936 end_template_decl ();
2937 else
2938 end_specialization ();
2941 /* Finish processing a template-id (which names a type) of the form
2942 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2943 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2944 the scope of template-id indicated. */
2946 tree
2947 finish_template_type (tree name, tree args, int entering_scope)
2949 tree type;
2951 type = lookup_template_class (name, args,
2952 NULL_TREE, NULL_TREE, entering_scope,
2953 tf_warning_or_error | tf_user);
2954 if (type == error_mark_node)
2955 return type;
2956 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2957 return TYPE_STUB_DECL (type);
2958 else
2959 return TYPE_NAME (type);
2962 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2963 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2964 BASE_CLASS, or NULL_TREE if an error occurred. The
2965 ACCESS_SPECIFIER is one of
2966 access_{default,public,protected_private}_node. For a virtual base
2967 we set TREE_TYPE. */
2969 tree
2970 finish_base_specifier (tree base, tree access, bool virtual_p)
2972 tree result;
2974 if (base == error_mark_node)
2976 error ("invalid base-class specification");
2977 result = NULL_TREE;
2979 else if (! MAYBE_CLASS_TYPE_P (base))
2981 error ("%qT is not a class type", base);
2982 result = NULL_TREE;
2984 else
2986 if (cp_type_quals (base) != 0)
2988 /* DR 484: Can a base-specifier name a cv-qualified
2989 class type? */
2990 base = TYPE_MAIN_VARIANT (base);
2992 result = build_tree_list (access, base);
2993 if (virtual_p)
2994 TREE_TYPE (result) = integer_type_node;
2997 return result;
3000 /* If FNS is a member function, a set of member functions, or a
3001 template-id referring to one or more member functions, return a
3002 BASELINK for FNS, incorporating the current access context.
3003 Otherwise, return FNS unchanged. */
3005 tree
3006 baselink_for_fns (tree fns)
3008 tree scope;
3009 tree cl;
3011 if (BASELINK_P (fns)
3012 || error_operand_p (fns))
3013 return fns;
3015 scope = ovl_scope (fns);
3016 if (!CLASS_TYPE_P (scope))
3017 return fns;
3019 cl = currently_open_derived_class (scope);
3020 if (!cl)
3021 cl = scope;
3022 cl = TYPE_BINFO (cl);
3023 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3026 /* Returns true iff DECL is a variable from a function outside
3027 the current one. */
3029 static bool
3030 outer_var_p (tree decl)
3032 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3033 && DECL_FUNCTION_SCOPE_P (decl)
3034 && (DECL_CONTEXT (decl) != current_function_decl
3035 || parsing_nsdmi ()));
3038 /* As above, but also checks that DECL is automatic. */
3040 static bool
3041 outer_automatic_var_p (tree decl)
3043 return (outer_var_p (decl)
3044 && !TREE_STATIC (decl));
3047 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3048 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3049 if non-NULL, is the type or namespace used to explicitly qualify
3050 ID_EXPRESSION. DECL is the entity to which that name has been
3051 resolved.
3053 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3054 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3055 be set to true if this expression isn't permitted in a
3056 constant-expression, but it is otherwise not set by this function.
3057 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3058 constant-expression, but a non-constant expression is also
3059 permissible.
3061 DONE is true if this expression is a complete postfix-expression;
3062 it is false if this expression is followed by '->', '[', '(', etc.
3063 ADDRESS_P is true iff this expression is the operand of '&'.
3064 TEMPLATE_P is true iff the qualified-id was of the form
3065 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3066 appears as a template argument.
3068 If an error occurs, and it is the kind of error that might cause
3069 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3070 is the caller's responsibility to issue the message. *ERROR_MSG
3071 will be a string with static storage duration, so the caller need
3072 not "free" it.
3074 Return an expression for the entity, after issuing appropriate
3075 diagnostics. This function is also responsible for transforming a
3076 reference to a non-static member into a COMPONENT_REF that makes
3077 the use of "this" explicit.
3079 Upon return, *IDK will be filled in appropriately. */
3080 tree
3081 finish_id_expression (tree id_expression,
3082 tree decl,
3083 tree scope,
3084 cp_id_kind *idk,
3085 bool integral_constant_expression_p,
3086 bool allow_non_integral_constant_expression_p,
3087 bool *non_integral_constant_expression_p,
3088 bool template_p,
3089 bool done,
3090 bool address_p,
3091 bool template_arg_p,
3092 const char **error_msg,
3093 location_t location)
3095 decl = strip_using_decl (decl);
3097 /* Initialize the output parameters. */
3098 *idk = CP_ID_KIND_NONE;
3099 *error_msg = NULL;
3101 if (id_expression == error_mark_node)
3102 return error_mark_node;
3103 /* If we have a template-id, then no further lookup is
3104 required. If the template-id was for a template-class, we
3105 will sometimes have a TYPE_DECL at this point. */
3106 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3107 || TREE_CODE (decl) == TYPE_DECL)
3109 /* Look up the name. */
3110 else
3112 if (decl == error_mark_node)
3114 /* Name lookup failed. */
3115 if (scope
3116 && (!TYPE_P (scope)
3117 || (!dependent_type_p (scope)
3118 && !(identifier_p (id_expression)
3119 && IDENTIFIER_TYPENAME_P (id_expression)
3120 && dependent_type_p (TREE_TYPE (id_expression))))))
3122 /* If the qualifying type is non-dependent (and the name
3123 does not name a conversion operator to a dependent
3124 type), issue an error. */
3125 qualified_name_lookup_error (scope, id_expression, decl, location);
3126 return error_mark_node;
3128 else if (!scope)
3130 /* It may be resolved via Koenig lookup. */
3131 *idk = CP_ID_KIND_UNQUALIFIED;
3132 return id_expression;
3134 else
3135 decl = id_expression;
3137 /* If DECL is a variable that would be out of scope under
3138 ANSI/ISO rules, but in scope in the ARM, name lookup
3139 will succeed. Issue a diagnostic here. */
3140 else
3141 decl = check_for_out_of_scope_variable (decl);
3143 /* Remember that the name was used in the definition of
3144 the current class so that we can check later to see if
3145 the meaning would have been different after the class
3146 was entirely defined. */
3147 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3148 maybe_note_name_used_in_class (id_expression, decl);
3150 /* Disallow uses of local variables from containing functions, except
3151 within lambda-expressions. */
3152 if (!outer_var_p (decl))
3153 /* OK */;
3154 else if (TREE_STATIC (decl)
3155 /* It's not a use (3.2) if we're in an unevaluated context. */
3156 || cp_unevaluated_operand)
3158 if (processing_template_decl)
3159 /* For a use of an outer static/unevaluated var, return the id
3160 so that we'll look it up again in the instantiation. */
3161 return id_expression;
3163 else
3165 tree context = DECL_CONTEXT (decl);
3166 tree containing_function = current_function_decl;
3167 tree lambda_stack = NULL_TREE;
3168 tree lambda_expr = NULL_TREE;
3169 tree initializer = convert_from_reference (decl);
3171 /* Mark it as used now even if the use is ill-formed. */
3172 mark_used (decl);
3174 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3175 support for an approach in which a reference to a local
3176 [constant] automatic variable in a nested class or lambda body
3177 would enter the expression as an rvalue, which would reduce
3178 the complexity of the problem"
3180 FIXME update for final resolution of core issue 696. */
3181 if (decl_constant_var_p (decl))
3183 if (processing_template_decl)
3184 /* In a template, the constant value may not be in a usable
3185 form, so look it up again at instantiation time. */
3186 return id_expression;
3187 else
3188 return integral_constant_value (decl);
3191 if (parsing_nsdmi ())
3192 containing_function = NULL_TREE;
3193 /* If we are in a lambda function, we can move out until we hit
3194 1. the context,
3195 2. a non-lambda function, or
3196 3. a non-default capturing lambda function. */
3197 else while (context != containing_function
3198 && LAMBDA_FUNCTION_P (containing_function))
3200 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3201 (DECL_CONTEXT (containing_function));
3203 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3204 == CPLD_NONE)
3205 break;
3207 lambda_stack = tree_cons (NULL_TREE,
3208 lambda_expr,
3209 lambda_stack);
3211 containing_function
3212 = decl_function_context (containing_function);
3215 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3216 && DECL_ANON_UNION_VAR_P (decl))
3218 error ("cannot capture member %qD of anonymous union", decl);
3219 return error_mark_node;
3221 if (context == containing_function)
3223 decl = add_default_capture (lambda_stack,
3224 /*id=*/DECL_NAME (decl),
3225 initializer);
3227 else if (lambda_expr)
3229 error ("%qD is not captured", decl);
3230 return error_mark_node;
3232 else
3234 error (VAR_P (decl)
3235 ? G_("use of local variable with automatic storage from containing function")
3236 : G_("use of parameter from containing function"));
3237 inform (input_location, "%q+#D declared here", decl);
3238 return error_mark_node;
3242 /* Also disallow uses of function parameters outside the function
3243 body, except inside an unevaluated context (i.e. decltype). */
3244 if (TREE_CODE (decl) == PARM_DECL
3245 && DECL_CONTEXT (decl) == NULL_TREE
3246 && !cp_unevaluated_operand)
3248 error ("use of parameter %qD outside function body", decl);
3249 return error_mark_node;
3253 /* If we didn't find anything, or what we found was a type,
3254 then this wasn't really an id-expression. */
3255 if (TREE_CODE (decl) == TEMPLATE_DECL
3256 && !DECL_FUNCTION_TEMPLATE_P (decl))
3258 *error_msg = "missing template arguments";
3259 return error_mark_node;
3261 else if (TREE_CODE (decl) == TYPE_DECL
3262 || TREE_CODE (decl) == NAMESPACE_DECL)
3264 *error_msg = "expected primary-expression";
3265 return error_mark_node;
3268 /* If the name resolved to a template parameter, there is no
3269 need to look it up again later. */
3270 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3271 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3273 tree r;
3275 *idk = CP_ID_KIND_NONE;
3276 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3277 decl = TEMPLATE_PARM_DECL (decl);
3278 r = convert_from_reference (DECL_INITIAL (decl));
3280 if (integral_constant_expression_p
3281 && !dependent_type_p (TREE_TYPE (decl))
3282 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3284 if (!allow_non_integral_constant_expression_p)
3285 error ("template parameter %qD of type %qT is not allowed in "
3286 "an integral constant expression because it is not of "
3287 "integral or enumeration type", decl, TREE_TYPE (decl));
3288 *non_integral_constant_expression_p = true;
3290 return r;
3292 else
3294 bool dependent_p;
3296 /* If the declaration was explicitly qualified indicate
3297 that. The semantics of `A::f(3)' are different than
3298 `f(3)' if `f' is virtual. */
3299 *idk = (scope
3300 ? CP_ID_KIND_QUALIFIED
3301 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3302 ? CP_ID_KIND_TEMPLATE_ID
3303 : CP_ID_KIND_UNQUALIFIED));
3306 /* [temp.dep.expr]
3308 An id-expression is type-dependent if it contains an
3309 identifier that was declared with a dependent type.
3311 The standard is not very specific about an id-expression that
3312 names a set of overloaded functions. What if some of them
3313 have dependent types and some of them do not? Presumably,
3314 such a name should be treated as a dependent name. */
3315 /* Assume the name is not dependent. */
3316 dependent_p = false;
3317 if (!processing_template_decl)
3318 /* No names are dependent outside a template. */
3320 else if (TREE_CODE (decl) == CONST_DECL)
3321 /* We don't want to treat enumerators as dependent. */
3323 /* A template-id where the name of the template was not resolved
3324 is definitely dependent. */
3325 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3326 && (identifier_p (TREE_OPERAND (decl, 0))))
3327 dependent_p = true;
3328 /* For anything except an overloaded function, just check its
3329 type. */
3330 else if (!is_overloaded_fn (decl))
3331 dependent_p
3332 = dependent_type_p (TREE_TYPE (decl));
3333 /* For a set of overloaded functions, check each of the
3334 functions. */
3335 else
3337 tree fns = decl;
3339 if (BASELINK_P (fns))
3340 fns = BASELINK_FUNCTIONS (fns);
3342 /* For a template-id, check to see if the template
3343 arguments are dependent. */
3344 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3346 tree args = TREE_OPERAND (fns, 1);
3347 dependent_p = any_dependent_template_arguments_p (args);
3348 /* The functions are those referred to by the
3349 template-id. */
3350 fns = TREE_OPERAND (fns, 0);
3353 /* If there are no dependent template arguments, go through
3354 the overloaded functions. */
3355 while (fns && !dependent_p)
3357 tree fn = OVL_CURRENT (fns);
3359 /* Member functions of dependent classes are
3360 dependent. */
3361 if (TREE_CODE (fn) == FUNCTION_DECL
3362 && type_dependent_expression_p (fn))
3363 dependent_p = true;
3364 else if (TREE_CODE (fn) == TEMPLATE_DECL
3365 && dependent_template_p (fn))
3366 dependent_p = true;
3368 fns = OVL_NEXT (fns);
3372 /* If the name was dependent on a template parameter, we will
3373 resolve the name at instantiation time. */
3374 if (dependent_p)
3376 /* Create a SCOPE_REF for qualified names, if the scope is
3377 dependent. */
3378 if (scope)
3380 if (TYPE_P (scope))
3382 if (address_p && done)
3383 decl = finish_qualified_id_expr (scope, decl,
3384 done, address_p,
3385 template_p,
3386 template_arg_p,
3387 tf_warning_or_error);
3388 else
3390 tree type = NULL_TREE;
3391 if (DECL_P (decl) && !dependent_scope_p (scope))
3392 type = TREE_TYPE (decl);
3393 decl = build_qualified_name (type,
3394 scope,
3395 id_expression,
3396 template_p);
3399 if (TREE_TYPE (decl))
3400 decl = convert_from_reference (decl);
3401 return decl;
3403 /* A TEMPLATE_ID already contains all the information we
3404 need. */
3405 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3406 return id_expression;
3407 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3408 /* If we found a variable, then name lookup during the
3409 instantiation will always resolve to the same VAR_DECL
3410 (or an instantiation thereof). */
3411 if (VAR_P (decl)
3412 || TREE_CODE (decl) == PARM_DECL)
3414 mark_used (decl);
3415 return convert_from_reference (decl);
3417 /* The same is true for FIELD_DECL, but we also need to
3418 make sure that the syntax is correct. */
3419 else if (TREE_CODE (decl) == FIELD_DECL)
3421 /* Since SCOPE is NULL here, this is an unqualified name.
3422 Access checking has been performed during name lookup
3423 already. Turn off checking to avoid duplicate errors. */
3424 push_deferring_access_checks (dk_no_check);
3425 decl = finish_non_static_data_member
3426 (decl, NULL_TREE,
3427 /*qualifying_scope=*/NULL_TREE);
3428 pop_deferring_access_checks ();
3429 return decl;
3431 return id_expression;
3434 if (TREE_CODE (decl) == NAMESPACE_DECL)
3436 error ("use of namespace %qD as expression", decl);
3437 return error_mark_node;
3439 else if (DECL_CLASS_TEMPLATE_P (decl))
3441 error ("use of class template %qT as expression", decl);
3442 return error_mark_node;
3444 else if (TREE_CODE (decl) == TREE_LIST)
3446 /* Ambiguous reference to base members. */
3447 error ("request for member %qD is ambiguous in "
3448 "multiple inheritance lattice", id_expression);
3449 print_candidates (decl);
3450 return error_mark_node;
3453 /* Mark variable-like entities as used. Functions are similarly
3454 marked either below or after overload resolution. */
3455 if ((VAR_P (decl)
3456 || TREE_CODE (decl) == PARM_DECL
3457 || TREE_CODE (decl) == CONST_DECL
3458 || TREE_CODE (decl) == RESULT_DECL)
3459 && !mark_used (decl))
3460 return error_mark_node;
3462 /* Only certain kinds of names are allowed in constant
3463 expression. Template parameters have already
3464 been handled above. */
3465 if (! error_operand_p (decl)
3466 && integral_constant_expression_p
3467 && ! decl_constant_var_p (decl)
3468 && TREE_CODE (decl) != CONST_DECL
3469 && ! builtin_valid_in_constant_expr_p (decl))
3471 if (!allow_non_integral_constant_expression_p)
3473 error ("%qD cannot appear in a constant-expression", decl);
3474 return error_mark_node;
3476 *non_integral_constant_expression_p = true;
3479 tree wrap;
3480 if (VAR_P (decl)
3481 && !cp_unevaluated_operand
3482 && DECL_THREAD_LOCAL_P (decl)
3483 && (wrap = get_tls_wrapper_fn (decl)))
3485 /* Replace an evaluated use of the thread_local variable with
3486 a call to its wrapper. */
3487 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3489 else if (scope)
3491 decl = (adjust_result_of_qualified_name_lookup
3492 (decl, scope, current_nonlambda_class_type()));
3494 if (TREE_CODE (decl) == FUNCTION_DECL)
3495 mark_used (decl);
3497 if (TYPE_P (scope))
3498 decl = finish_qualified_id_expr (scope,
3499 decl,
3500 done,
3501 address_p,
3502 template_p,
3503 template_arg_p,
3504 tf_warning_or_error);
3505 else
3506 decl = convert_from_reference (decl);
3508 else if (TREE_CODE (decl) == FIELD_DECL)
3510 /* Since SCOPE is NULL here, this is an unqualified name.
3511 Access checking has been performed during name lookup
3512 already. Turn off checking to avoid duplicate errors. */
3513 push_deferring_access_checks (dk_no_check);
3514 decl = finish_non_static_data_member (decl, NULL_TREE,
3515 /*qualifying_scope=*/NULL_TREE);
3516 pop_deferring_access_checks ();
3518 else if (is_overloaded_fn (decl))
3520 tree first_fn;
3522 first_fn = get_first_fn (decl);
3523 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3524 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3526 if (!really_overloaded_fn (decl)
3527 && !mark_used (first_fn))
3528 return error_mark_node;
3530 if (!template_arg_p
3531 && TREE_CODE (first_fn) == FUNCTION_DECL
3532 && DECL_FUNCTION_MEMBER_P (first_fn)
3533 && !shared_member_p (decl))
3535 /* A set of member functions. */
3536 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3537 return finish_class_member_access_expr (decl, id_expression,
3538 /*template_p=*/false,
3539 tf_warning_or_error);
3542 decl = baselink_for_fns (decl);
3544 else
3546 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3547 && DECL_CLASS_SCOPE_P (decl))
3549 tree context = context_for_name_lookup (decl);
3550 if (context != current_class_type)
3552 tree path = currently_open_derived_class (context);
3553 perform_or_defer_access_check (TYPE_BINFO (path),
3554 decl, decl,
3555 tf_warning_or_error);
3559 decl = convert_from_reference (decl);
3563 /* Handle references (c++/56130). */
3564 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3565 if (TREE_DEPRECATED (t))
3566 warn_deprecated_use (t, NULL_TREE);
3568 return decl;
3571 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3572 use as a type-specifier. */
3574 tree
3575 finish_typeof (tree expr)
3577 tree type;
3579 if (type_dependent_expression_p (expr))
3581 type = cxx_make_type (TYPEOF_TYPE);
3582 TYPEOF_TYPE_EXPR (type) = expr;
3583 SET_TYPE_STRUCTURAL_EQUALITY (type);
3585 return type;
3588 expr = mark_type_use (expr);
3590 type = unlowered_expr_type (expr);
3592 if (!type || type == unknown_type_node)
3594 error ("type of %qE is unknown", expr);
3595 return error_mark_node;
3598 return type;
3601 /* Implement the __underlying_type keyword: Return the underlying
3602 type of TYPE, suitable for use as a type-specifier. */
3604 tree
3605 finish_underlying_type (tree type)
3607 tree underlying_type;
3609 if (processing_template_decl)
3611 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3612 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3613 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3615 return underlying_type;
3618 complete_type (type);
3620 if (TREE_CODE (type) != ENUMERAL_TYPE)
3622 error ("%qT is not an enumeration type", type);
3623 return error_mark_node;
3626 underlying_type = ENUM_UNDERLYING_TYPE (type);
3628 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3629 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3630 See finish_enum_value_list for details. */
3631 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3632 underlying_type
3633 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3634 TYPE_UNSIGNED (underlying_type));
3636 return underlying_type;
3639 /* Implement the __direct_bases keyword: Return the direct base classes
3640 of type */
3642 tree
3643 calculate_direct_bases (tree type)
3645 vec<tree, va_gc> *vector = make_tree_vector();
3646 tree bases_vec = NULL_TREE;
3647 vec<tree, va_gc> *base_binfos;
3648 tree binfo;
3649 unsigned i;
3651 complete_type (type);
3653 if (!NON_UNION_CLASS_TYPE_P (type))
3654 return make_tree_vec (0);
3656 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3658 /* Virtual bases are initialized first */
3659 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3661 if (BINFO_VIRTUAL_P (binfo))
3663 vec_safe_push (vector, binfo);
3667 /* Now non-virtuals */
3668 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3670 if (!BINFO_VIRTUAL_P (binfo))
3672 vec_safe_push (vector, binfo);
3677 bases_vec = make_tree_vec (vector->length ());
3679 for (i = 0; i < vector->length (); ++i)
3681 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3683 return bases_vec;
3686 /* Implement the __bases keyword: Return the base classes
3687 of type */
3689 /* Find morally non-virtual base classes by walking binfo hierarchy */
3690 /* Virtual base classes are handled separately in finish_bases */
3692 static tree
3693 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3695 /* Don't walk bases of virtual bases */
3696 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3699 static tree
3700 dfs_calculate_bases_post (tree binfo, void *data_)
3702 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3703 if (!BINFO_VIRTUAL_P (binfo))
3705 vec_safe_push (*data, BINFO_TYPE (binfo));
3707 return NULL_TREE;
3710 /* Calculates the morally non-virtual base classes of a class */
3711 static vec<tree, va_gc> *
3712 calculate_bases_helper (tree type)
3714 vec<tree, va_gc> *vector = make_tree_vector();
3716 /* Now add non-virtual base classes in order of construction */
3717 dfs_walk_all (TYPE_BINFO (type),
3718 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3719 return vector;
3722 tree
3723 calculate_bases (tree type)
3725 vec<tree, va_gc> *vector = make_tree_vector();
3726 tree bases_vec = NULL_TREE;
3727 unsigned i;
3728 vec<tree, va_gc> *vbases;
3729 vec<tree, va_gc> *nonvbases;
3730 tree binfo;
3732 complete_type (type);
3734 if (!NON_UNION_CLASS_TYPE_P (type))
3735 return make_tree_vec (0);
3737 /* First go through virtual base classes */
3738 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3739 vec_safe_iterate (vbases, i, &binfo); i++)
3741 vec<tree, va_gc> *vbase_bases;
3742 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3743 vec_safe_splice (vector, vbase_bases);
3744 release_tree_vector (vbase_bases);
3747 /* Now for the non-virtual bases */
3748 nonvbases = calculate_bases_helper (type);
3749 vec_safe_splice (vector, nonvbases);
3750 release_tree_vector (nonvbases);
3752 /* Last element is entire class, so don't copy */
3753 bases_vec = make_tree_vec (vector->length () - 1);
3755 for (i = 0; i < vector->length () - 1; ++i)
3757 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3759 release_tree_vector (vector);
3760 return bases_vec;
3763 tree
3764 finish_bases (tree type, bool direct)
3766 tree bases = NULL_TREE;
3768 if (!processing_template_decl)
3770 /* Parameter packs can only be used in templates */
3771 error ("Parameter pack __bases only valid in template declaration");
3772 return error_mark_node;
3775 bases = cxx_make_type (BASES);
3776 BASES_TYPE (bases) = type;
3777 BASES_DIRECT (bases) = direct;
3778 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3780 return bases;
3783 /* Perform C++-specific checks for __builtin_offsetof before calling
3784 fold_offsetof. */
3786 tree
3787 finish_offsetof (tree expr)
3789 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3791 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3792 TREE_OPERAND (expr, 2));
3793 return error_mark_node;
3795 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3796 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3797 || TREE_TYPE (expr) == unknown_type_node)
3799 if (INDIRECT_REF_P (expr))
3800 error ("second operand of %<offsetof%> is neither a single "
3801 "identifier nor a sequence of member accesses and "
3802 "array references");
3803 else
3805 if (TREE_CODE (expr) == COMPONENT_REF
3806 || TREE_CODE (expr) == COMPOUND_EXPR)
3807 expr = TREE_OPERAND (expr, 1);
3808 error ("cannot apply %<offsetof%> to member function %qD", expr);
3810 return error_mark_node;
3812 if (REFERENCE_REF_P (expr))
3813 expr = TREE_OPERAND (expr, 0);
3814 if (TREE_CODE (expr) == COMPONENT_REF)
3816 tree object = TREE_OPERAND (expr, 0);
3817 if (!complete_type_or_else (TREE_TYPE (object), object))
3818 return error_mark_node;
3820 return fold_offsetof (expr);
3823 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3824 function is broken out from the above for the benefit of the tree-ssa
3825 project. */
3827 void
3828 simplify_aggr_init_expr (tree *tp)
3830 tree aggr_init_expr = *tp;
3832 /* Form an appropriate CALL_EXPR. */
3833 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3834 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3835 tree type = TREE_TYPE (slot);
3837 tree call_expr;
3838 enum style_t { ctor, arg, pcc } style;
3840 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3841 style = ctor;
3842 #ifdef PCC_STATIC_STRUCT_RETURN
3843 else if (1)
3844 style = pcc;
3845 #endif
3846 else
3848 gcc_assert (TREE_ADDRESSABLE (type));
3849 style = arg;
3852 call_expr = build_call_array_loc (input_location,
3853 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3855 aggr_init_expr_nargs (aggr_init_expr),
3856 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3857 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3859 if (style == ctor)
3861 /* Replace the first argument to the ctor with the address of the
3862 slot. */
3863 cxx_mark_addressable (slot);
3864 CALL_EXPR_ARG (call_expr, 0) =
3865 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3867 else if (style == arg)
3869 /* Just mark it addressable here, and leave the rest to
3870 expand_call{,_inline}. */
3871 cxx_mark_addressable (slot);
3872 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3873 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3875 else if (style == pcc)
3877 /* If we're using the non-reentrant PCC calling convention, then we
3878 need to copy the returned value out of the static buffer into the
3879 SLOT. */
3880 push_deferring_access_checks (dk_no_check);
3881 call_expr = build_aggr_init (slot, call_expr,
3882 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3883 tf_warning_or_error);
3884 pop_deferring_access_checks ();
3885 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3888 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3890 tree init = build_zero_init (type, NULL_TREE,
3891 /*static_storage_p=*/false);
3892 init = build2 (INIT_EXPR, void_type_node, slot, init);
3893 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3894 init, call_expr);
3897 *tp = call_expr;
3900 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3902 void
3903 emit_associated_thunks (tree fn)
3905 /* When we use vcall offsets, we emit thunks with the virtual
3906 functions to which they thunk. The whole point of vcall offsets
3907 is so that you can know statically the entire set of thunks that
3908 will ever be needed for a given virtual function, thereby
3909 enabling you to output all the thunks with the function itself. */
3910 if (DECL_VIRTUAL_P (fn)
3911 /* Do not emit thunks for extern template instantiations. */
3912 && ! DECL_REALLY_EXTERN (fn))
3914 tree thunk;
3916 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3918 if (!THUNK_ALIAS (thunk))
3920 use_thunk (thunk, /*emit_p=*/1);
3921 if (DECL_RESULT_THUNK_P (thunk))
3923 tree probe;
3925 for (probe = DECL_THUNKS (thunk);
3926 probe; probe = DECL_CHAIN (probe))
3927 use_thunk (probe, /*emit_p=*/1);
3930 else
3931 gcc_assert (!DECL_THUNKS (thunk));
3936 /* Returns true iff FUN is an instantiation of a constexpr function
3937 template. */
3939 static inline bool
3940 is_instantiation_of_constexpr (tree fun)
3942 return (DECL_TEMPLOID_INSTANTIATION (fun)
3943 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3944 (DECL_TI_TEMPLATE (fun))));
3947 /* Generate RTL for FN. */
3949 bool
3950 expand_or_defer_fn_1 (tree fn)
3952 /* When the parser calls us after finishing the body of a template
3953 function, we don't really want to expand the body. */
3954 if (processing_template_decl)
3956 /* Normally, collection only occurs in rest_of_compilation. So,
3957 if we don't collect here, we never collect junk generated
3958 during the processing of templates until we hit a
3959 non-template function. It's not safe to do this inside a
3960 nested class, though, as the parser may have local state that
3961 is not a GC root. */
3962 if (!function_depth)
3963 ggc_collect ();
3964 return false;
3967 gcc_assert (DECL_SAVED_TREE (fn));
3969 /* We make a decision about linkage for these functions at the end
3970 of the compilation. Until that point, we do not want the back
3971 end to output them -- but we do want it to see the bodies of
3972 these functions so that it can inline them as appropriate. */
3973 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3975 if (DECL_INTERFACE_KNOWN (fn))
3976 /* We've already made a decision as to how this function will
3977 be handled. */;
3978 else if (!at_eof)
3980 DECL_EXTERNAL (fn) = 1;
3981 DECL_NOT_REALLY_EXTERN (fn) = 1;
3982 note_vague_linkage_fn (fn);
3983 /* A non-template inline function with external linkage will
3984 always be COMDAT. As we must eventually determine the
3985 linkage of all functions, and as that causes writes to
3986 the data mapped in from the PCH file, it's advantageous
3987 to mark the functions at this point. */
3988 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3990 /* This function must have external linkage, as
3991 otherwise DECL_INTERFACE_KNOWN would have been
3992 set. */
3993 gcc_assert (TREE_PUBLIC (fn));
3994 comdat_linkage (fn);
3995 DECL_INTERFACE_KNOWN (fn) = 1;
3998 else
3999 import_export_decl (fn);
4001 /* If the user wants us to keep all inline functions, then mark
4002 this function as needed so that finish_file will make sure to
4003 output it later. Similarly, all dllexport'd functions must
4004 be emitted; there may be callers in other DLLs. */
4005 if ((flag_keep_inline_functions
4006 && DECL_DECLARED_INLINE_P (fn)
4007 && !DECL_REALLY_EXTERN (fn))
4008 || (flag_keep_inline_dllexport
4009 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4011 mark_needed (fn);
4012 DECL_EXTERNAL (fn) = 0;
4016 /* If this is a constructor or destructor body, we have to clone
4017 it. */
4018 if (maybe_clone_body (fn))
4020 /* We don't want to process FN again, so pretend we've written
4021 it out, even though we haven't. */
4022 TREE_ASM_WRITTEN (fn) = 1;
4023 /* If this is an instantiation of a constexpr function, keep
4024 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4025 if (!is_instantiation_of_constexpr (fn))
4026 DECL_SAVED_TREE (fn) = NULL_TREE;
4027 return false;
4030 /* There's no reason to do any of the work here if we're only doing
4031 semantic analysis; this code just generates RTL. */
4032 if (flag_syntax_only)
4033 return false;
4035 return true;
4038 void
4039 expand_or_defer_fn (tree fn)
4041 if (expand_or_defer_fn_1 (fn))
4043 function_depth++;
4045 /* Expand or defer, at the whim of the compilation unit manager. */
4046 cgraph_finalize_function (fn, function_depth > 1);
4047 emit_associated_thunks (fn);
4049 function_depth--;
4053 struct nrv_data
4055 tree var;
4056 tree result;
4057 hash_table <pointer_hash <tree_node> > visited;
4060 /* Helper function for walk_tree, used by finalize_nrv below. */
4062 static tree
4063 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4065 struct nrv_data *dp = (struct nrv_data *)data;
4066 tree_node **slot;
4068 /* No need to walk into types. There wouldn't be any need to walk into
4069 non-statements, except that we have to consider STMT_EXPRs. */
4070 if (TYPE_P (*tp))
4071 *walk_subtrees = 0;
4072 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4073 but differs from using NULL_TREE in that it indicates that we care
4074 about the value of the RESULT_DECL. */
4075 else if (TREE_CODE (*tp) == RETURN_EXPR)
4076 TREE_OPERAND (*tp, 0) = dp->result;
4077 /* Change all cleanups for the NRV to only run when an exception is
4078 thrown. */
4079 else if (TREE_CODE (*tp) == CLEANUP_STMT
4080 && CLEANUP_DECL (*tp) == dp->var)
4081 CLEANUP_EH_ONLY (*tp) = 1;
4082 /* Replace the DECL_EXPR for the NRV with an initialization of the
4083 RESULT_DECL, if needed. */
4084 else if (TREE_CODE (*tp) == DECL_EXPR
4085 && DECL_EXPR_DECL (*tp) == dp->var)
4087 tree init;
4088 if (DECL_INITIAL (dp->var)
4089 && DECL_INITIAL (dp->var) != error_mark_node)
4090 init = build2 (INIT_EXPR, void_type_node, dp->result,
4091 DECL_INITIAL (dp->var));
4092 else
4093 init = build_empty_stmt (EXPR_LOCATION (*tp));
4094 DECL_INITIAL (dp->var) = NULL_TREE;
4095 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4096 *tp = init;
4098 /* And replace all uses of the NRV with the RESULT_DECL. */
4099 else if (*tp == dp->var)
4100 *tp = dp->result;
4102 /* Avoid walking into the same tree more than once. Unfortunately, we
4103 can't just use walk_tree_without duplicates because it would only call
4104 us for the first occurrence of dp->var in the function body. */
4105 slot = dp->visited.find_slot (*tp, INSERT);
4106 if (*slot)
4107 *walk_subtrees = 0;
4108 else
4109 *slot = *tp;
4111 /* Keep iterating. */
4112 return NULL_TREE;
4115 /* Called from finish_function to implement the named return value
4116 optimization by overriding all the RETURN_EXPRs and pertinent
4117 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4118 RESULT_DECL for the function. */
4120 void
4121 finalize_nrv (tree *tp, tree var, tree result)
4123 struct nrv_data data;
4125 /* Copy name from VAR to RESULT. */
4126 DECL_NAME (result) = DECL_NAME (var);
4127 /* Don't forget that we take its address. */
4128 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4129 /* Finally set DECL_VALUE_EXPR to avoid assigning
4130 a stack slot at -O0 for the original var and debug info
4131 uses RESULT location for VAR. */
4132 SET_DECL_VALUE_EXPR (var, result);
4133 DECL_HAS_VALUE_EXPR_P (var) = 1;
4135 data.var = var;
4136 data.result = result;
4137 data.visited.create (37);
4138 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4139 data.visited.dispose ();
4142 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4144 bool
4145 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4146 bool need_copy_ctor, bool need_copy_assignment,
4147 bool need_dtor)
4149 int save_errorcount = errorcount;
4150 tree info, t;
4152 /* Always allocate 3 elements for simplicity. These are the
4153 function decls for the ctor, dtor, and assignment op.
4154 This layout is known to the three lang hooks,
4155 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4156 and cxx_omp_clause_assign_op. */
4157 info = make_tree_vec (3);
4158 CP_OMP_CLAUSE_INFO (c) = info;
4160 if (need_default_ctor || need_copy_ctor)
4162 if (need_default_ctor)
4163 t = get_default_ctor (type);
4164 else
4165 t = get_copy_ctor (type, tf_warning_or_error);
4167 if (t && !trivial_fn_p (t))
4168 TREE_VEC_ELT (info, 0) = t;
4171 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4172 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4174 if (need_copy_assignment)
4176 t = get_copy_assign (type);
4178 if (t && !trivial_fn_p (t))
4179 TREE_VEC_ELT (info, 2) = t;
4182 return errorcount != save_errorcount;
4185 /* Helper function for handle_omp_array_sections. Called recursively
4186 to handle multiple array-section-subscripts. C is the clause,
4187 T current expression (initially OMP_CLAUSE_DECL), which is either
4188 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4189 expression if specified, TREE_VALUE length expression if specified,
4190 TREE_CHAIN is what it has been specified after, or some decl.
4191 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4192 set to true if any of the array-section-subscript could have length
4193 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4194 first array-section-subscript which is known not to have length
4195 of one. Given say:
4196 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4197 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4198 all are or may have length of 1, array-section-subscript [:2] is the
4199 first one knonwn not to have length 1. For array-section-subscript
4200 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4201 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4202 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4203 case though, as some lengths could be zero. */
4205 static tree
4206 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4207 bool &maybe_zero_len, unsigned int &first_non_one)
4209 tree ret, low_bound, length, type;
4210 if (TREE_CODE (t) != TREE_LIST)
4212 if (error_operand_p (t))
4213 return error_mark_node;
4214 if (type_dependent_expression_p (t))
4215 return NULL_TREE;
4216 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4218 if (processing_template_decl)
4219 return NULL_TREE;
4220 if (DECL_P (t))
4221 error_at (OMP_CLAUSE_LOCATION (c),
4222 "%qD is not a variable in %qs clause", t,
4223 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4224 else
4225 error_at (OMP_CLAUSE_LOCATION (c),
4226 "%qE is not a variable in %qs clause", t,
4227 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4228 return error_mark_node;
4230 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4231 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4233 error_at (OMP_CLAUSE_LOCATION (c),
4234 "%qD is threadprivate variable in %qs clause", t,
4235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4236 return error_mark_node;
4238 t = convert_from_reference (t);
4239 return t;
4242 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4243 maybe_zero_len, first_non_one);
4244 if (ret == error_mark_node || ret == NULL_TREE)
4245 return ret;
4247 type = TREE_TYPE (ret);
4248 low_bound = TREE_PURPOSE (t);
4249 length = TREE_VALUE (t);
4250 if ((low_bound && type_dependent_expression_p (low_bound))
4251 || (length && type_dependent_expression_p (length)))
4252 return NULL_TREE;
4254 if (low_bound == error_mark_node || length == error_mark_node)
4255 return error_mark_node;
4257 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4259 error_at (OMP_CLAUSE_LOCATION (c),
4260 "low bound %qE of array section does not have integral type",
4261 low_bound);
4262 return error_mark_node;
4264 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4266 error_at (OMP_CLAUSE_LOCATION (c),
4267 "length %qE of array section does not have integral type",
4268 length);
4269 return error_mark_node;
4271 if (low_bound
4272 && TREE_CODE (low_bound) == INTEGER_CST
4273 && TYPE_PRECISION (TREE_TYPE (low_bound))
4274 > TYPE_PRECISION (sizetype))
4275 low_bound = fold_convert (sizetype, low_bound);
4276 if (length
4277 && TREE_CODE (length) == INTEGER_CST
4278 && TYPE_PRECISION (TREE_TYPE (length))
4279 > TYPE_PRECISION (sizetype))
4280 length = fold_convert (sizetype, length);
4281 if (low_bound == NULL_TREE)
4282 low_bound = integer_zero_node;
4284 if (length != NULL_TREE)
4286 if (!integer_nonzerop (length))
4287 maybe_zero_len = true;
4288 if (first_non_one == types.length ()
4289 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4290 first_non_one++;
4292 if (TREE_CODE (type) == ARRAY_TYPE)
4294 if (length == NULL_TREE
4295 && (TYPE_DOMAIN (type) == NULL_TREE
4296 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4298 error_at (OMP_CLAUSE_LOCATION (c),
4299 "for unknown bound array type length expression must "
4300 "be specified");
4301 return error_mark_node;
4303 if (TREE_CODE (low_bound) == INTEGER_CST
4304 && tree_int_cst_sgn (low_bound) == -1)
4306 error_at (OMP_CLAUSE_LOCATION (c),
4307 "negative low bound in array section in %qs clause",
4308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4309 return error_mark_node;
4311 if (length != NULL_TREE
4312 && TREE_CODE (length) == INTEGER_CST
4313 && tree_int_cst_sgn (length) == -1)
4315 error_at (OMP_CLAUSE_LOCATION (c),
4316 "negative length in array section in %qs clause",
4317 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4318 return error_mark_node;
4320 if (TYPE_DOMAIN (type)
4321 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4322 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4323 == INTEGER_CST)
4325 tree size = size_binop (PLUS_EXPR,
4326 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4327 size_one_node);
4328 if (TREE_CODE (low_bound) == INTEGER_CST)
4330 if (tree_int_cst_lt (size, low_bound))
4332 error_at (OMP_CLAUSE_LOCATION (c),
4333 "low bound %qE above array section size "
4334 "in %qs clause", low_bound,
4335 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4336 return error_mark_node;
4338 if (tree_int_cst_equal (size, low_bound))
4339 maybe_zero_len = true;
4340 else if (length == NULL_TREE
4341 && first_non_one == types.length ()
4342 && tree_int_cst_equal
4343 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4344 low_bound))
4345 first_non_one++;
4347 else if (length == NULL_TREE)
4349 maybe_zero_len = true;
4350 if (first_non_one == types.length ())
4351 first_non_one++;
4353 if (length && TREE_CODE (length) == INTEGER_CST)
4355 if (tree_int_cst_lt (size, length))
4357 error_at (OMP_CLAUSE_LOCATION (c),
4358 "length %qE above array section size "
4359 "in %qs clause", length,
4360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4361 return error_mark_node;
4363 if (TREE_CODE (low_bound) == INTEGER_CST)
4365 tree lbpluslen
4366 = size_binop (PLUS_EXPR,
4367 fold_convert (sizetype, low_bound),
4368 fold_convert (sizetype, length));
4369 if (TREE_CODE (lbpluslen) == INTEGER_CST
4370 && tree_int_cst_lt (size, lbpluslen))
4372 error_at (OMP_CLAUSE_LOCATION (c),
4373 "high bound %qE above array section size "
4374 "in %qs clause", lbpluslen,
4375 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4376 return error_mark_node;
4381 else if (length == NULL_TREE)
4383 maybe_zero_len = true;
4384 if (first_non_one == types.length ())
4385 first_non_one++;
4388 /* For [lb:] we will need to evaluate lb more than once. */
4389 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4391 tree lb = cp_save_expr (low_bound);
4392 if (lb != low_bound)
4394 TREE_PURPOSE (t) = lb;
4395 low_bound = lb;
4399 else if (TREE_CODE (type) == POINTER_TYPE)
4401 if (length == NULL_TREE)
4403 error_at (OMP_CLAUSE_LOCATION (c),
4404 "for pointer type length expression must be specified");
4405 return error_mark_node;
4407 /* If there is a pointer type anywhere but in the very first
4408 array-section-subscript, the array section can't be contiguous. */
4409 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4410 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4412 error_at (OMP_CLAUSE_LOCATION (c),
4413 "array section is not contiguous in %qs clause",
4414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4415 return error_mark_node;
4418 else
4420 error_at (OMP_CLAUSE_LOCATION (c),
4421 "%qE does not have pointer or array type", ret);
4422 return error_mark_node;
4424 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4425 types.safe_push (TREE_TYPE (ret));
4426 /* We will need to evaluate lb more than once. */
4427 tree lb = cp_save_expr (low_bound);
4428 if (lb != low_bound)
4430 TREE_PURPOSE (t) = lb;
4431 low_bound = lb;
4433 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4434 return ret;
4437 /* Handle array sections for clause C. */
4439 static bool
4440 handle_omp_array_sections (tree c)
4442 bool maybe_zero_len = false;
4443 unsigned int first_non_one = 0;
4444 auto_vec<tree> types;
4445 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4446 maybe_zero_len, first_non_one);
4447 if (first == error_mark_node)
4448 return true;
4449 if (first == NULL_TREE)
4450 return false;
4451 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4453 tree t = OMP_CLAUSE_DECL (c);
4454 tree tem = NULL_TREE;
4455 if (processing_template_decl)
4456 return false;
4457 /* Need to evaluate side effects in the length expressions
4458 if any. */
4459 while (TREE_CODE (t) == TREE_LIST)
4461 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4463 if (tem == NULL_TREE)
4464 tem = TREE_VALUE (t);
4465 else
4466 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4467 TREE_VALUE (t), tem);
4469 t = TREE_CHAIN (t);
4471 if (tem)
4472 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4473 OMP_CLAUSE_DECL (c) = first;
4475 else
4477 unsigned int num = types.length (), i;
4478 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4479 tree condition = NULL_TREE;
4481 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4482 maybe_zero_len = true;
4483 if (processing_template_decl && maybe_zero_len)
4484 return false;
4486 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4487 t = TREE_CHAIN (t))
4489 tree low_bound = TREE_PURPOSE (t);
4490 tree length = TREE_VALUE (t);
4492 i--;
4493 if (low_bound
4494 && TREE_CODE (low_bound) == INTEGER_CST
4495 && TYPE_PRECISION (TREE_TYPE (low_bound))
4496 > TYPE_PRECISION (sizetype))
4497 low_bound = fold_convert (sizetype, low_bound);
4498 if (length
4499 && TREE_CODE (length) == INTEGER_CST
4500 && TYPE_PRECISION (TREE_TYPE (length))
4501 > TYPE_PRECISION (sizetype))
4502 length = fold_convert (sizetype, length);
4503 if (low_bound == NULL_TREE)
4504 low_bound = integer_zero_node;
4505 if (!maybe_zero_len && i > first_non_one)
4507 if (integer_nonzerop (low_bound))
4508 goto do_warn_noncontiguous;
4509 if (length != NULL_TREE
4510 && TREE_CODE (length) == INTEGER_CST
4511 && TYPE_DOMAIN (types[i])
4512 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4513 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4514 == INTEGER_CST)
4516 tree size;
4517 size = size_binop (PLUS_EXPR,
4518 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4519 size_one_node);
4520 if (!tree_int_cst_equal (length, size))
4522 do_warn_noncontiguous:
4523 error_at (OMP_CLAUSE_LOCATION (c),
4524 "array section is not contiguous in %qs "
4525 "clause",
4526 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4527 return true;
4530 if (!processing_template_decl
4531 && length != NULL_TREE
4532 && TREE_SIDE_EFFECTS (length))
4534 if (side_effects == NULL_TREE)
4535 side_effects = length;
4536 else
4537 side_effects = build2 (COMPOUND_EXPR,
4538 TREE_TYPE (side_effects),
4539 length, side_effects);
4542 else if (processing_template_decl)
4543 continue;
4544 else
4546 tree l;
4548 if (i > first_non_one && length && integer_nonzerop (length))
4549 continue;
4550 if (length)
4551 l = fold_convert (sizetype, length);
4552 else
4554 l = size_binop (PLUS_EXPR,
4555 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4556 size_one_node);
4557 l = size_binop (MINUS_EXPR, l,
4558 fold_convert (sizetype, low_bound));
4560 if (i > first_non_one)
4562 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4563 size_zero_node);
4564 if (condition == NULL_TREE)
4565 condition = l;
4566 else
4567 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4568 l, condition);
4570 else if (size == NULL_TREE)
4572 size = size_in_bytes (TREE_TYPE (types[i]));
4573 size = size_binop (MULT_EXPR, size, l);
4574 if (condition)
4575 size = fold_build3 (COND_EXPR, sizetype, condition,
4576 size, size_zero_node);
4578 else
4579 size = size_binop (MULT_EXPR, size, l);
4582 if (!processing_template_decl)
4584 if (side_effects)
4585 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4586 OMP_CLAUSE_DECL (c) = first;
4587 OMP_CLAUSE_SIZE (c) = size;
4588 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4589 return false;
4590 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4591 OMP_CLAUSE_MAP);
4592 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4593 if (!cxx_mark_addressable (t))
4594 return false;
4595 OMP_CLAUSE_DECL (c2) = t;
4596 t = build_fold_addr_expr (first);
4597 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4598 ptrdiff_type_node, t);
4599 tree ptr = OMP_CLAUSE_DECL (c2);
4600 ptr = convert_from_reference (ptr);
4601 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4602 ptr = build_fold_addr_expr (ptr);
4603 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4604 ptrdiff_type_node, t,
4605 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4606 ptrdiff_type_node, ptr));
4607 OMP_CLAUSE_SIZE (c2) = t;
4608 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4609 OMP_CLAUSE_CHAIN (c) = c2;
4610 ptr = OMP_CLAUSE_DECL (c2);
4611 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4612 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4614 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4615 OMP_CLAUSE_MAP);
4616 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4617 OMP_CLAUSE_DECL (c3) = ptr;
4618 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4619 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4620 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4621 OMP_CLAUSE_CHAIN (c2) = c3;
4625 return false;
4628 /* Return identifier to look up for omp declare reduction. */
4630 tree
4631 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4633 const char *p = NULL;
4634 const char *m = NULL;
4635 switch (reduction_code)
4637 case PLUS_EXPR:
4638 case MULT_EXPR:
4639 case MINUS_EXPR:
4640 case BIT_AND_EXPR:
4641 case BIT_XOR_EXPR:
4642 case BIT_IOR_EXPR:
4643 case TRUTH_ANDIF_EXPR:
4644 case TRUTH_ORIF_EXPR:
4645 reduction_id = ansi_opname (reduction_code);
4646 break;
4647 case MIN_EXPR:
4648 p = "min";
4649 break;
4650 case MAX_EXPR:
4651 p = "max";
4652 break;
4653 default:
4654 break;
4657 if (p == NULL)
4659 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4660 return error_mark_node;
4661 p = IDENTIFIER_POINTER (reduction_id);
4664 if (type != NULL_TREE)
4665 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4667 const char prefix[] = "omp declare reduction ";
4668 size_t lenp = sizeof (prefix);
4669 if (strncmp (p, prefix, lenp - 1) == 0)
4670 lenp = 1;
4671 size_t len = strlen (p);
4672 size_t lenm = m ? strlen (m) + 1 : 0;
4673 char *name = XALLOCAVEC (char, lenp + len + lenm);
4674 if (lenp > 1)
4675 memcpy (name, prefix, lenp - 1);
4676 memcpy (name + lenp - 1, p, len + 1);
4677 if (m)
4679 name[lenp + len - 1] = '~';
4680 memcpy (name + lenp + len, m, lenm);
4682 return get_identifier (name);
4685 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4686 FUNCTION_DECL or NULL_TREE if not found. */
4688 static tree
4689 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4690 vec<tree> *ambiguousp)
4692 tree orig_id = id;
4693 tree baselink = NULL_TREE;
4694 if (identifier_p (id))
4696 cp_id_kind idk;
4697 bool nonint_cst_expression_p;
4698 const char *error_msg;
4699 id = omp_reduction_id (ERROR_MARK, id, type);
4700 tree decl = lookup_name (id);
4701 if (decl == NULL_TREE)
4702 decl = error_mark_node;
4703 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4704 &nonint_cst_expression_p, false, true, false,
4705 false, &error_msg, loc);
4706 if (idk == CP_ID_KIND_UNQUALIFIED
4707 && identifier_p (id))
4709 vec<tree, va_gc> *args = NULL;
4710 vec_safe_push (args, build_reference_type (type));
4711 id = perform_koenig_lookup (id, args, tf_none);
4714 else if (TREE_CODE (id) == SCOPE_REF)
4715 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4716 omp_reduction_id (ERROR_MARK,
4717 TREE_OPERAND (id, 1),
4718 type),
4719 false, false);
4720 tree fns = id;
4721 if (id && is_overloaded_fn (id))
4722 id = get_fns (id);
4723 for (; id; id = OVL_NEXT (id))
4725 tree fndecl = OVL_CURRENT (id);
4726 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4728 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4729 if (same_type_p (TREE_TYPE (argtype), type))
4730 break;
4733 if (id && BASELINK_P (fns))
4735 if (baselinkp)
4736 *baselinkp = fns;
4737 else
4738 baselink = fns;
4740 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4742 vec<tree> ambiguous = vNULL;
4743 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4744 unsigned int ix;
4745 if (ambiguousp == NULL)
4746 ambiguousp = &ambiguous;
4747 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4749 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4750 baselinkp ? baselinkp : &baselink,
4751 ambiguousp);
4752 if (id == NULL_TREE)
4753 continue;
4754 if (!ambiguousp->is_empty ())
4755 ambiguousp->safe_push (id);
4756 else if (ret != NULL_TREE)
4758 ambiguousp->safe_push (ret);
4759 ambiguousp->safe_push (id);
4760 ret = NULL_TREE;
4762 else
4763 ret = id;
4765 if (ambiguousp != &ambiguous)
4766 return ret;
4767 if (!ambiguous.is_empty ())
4769 const char *str = _("candidates are:");
4770 unsigned int idx;
4771 tree udr;
4772 error_at (loc, "user defined reduction lookup is ambiguous");
4773 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4775 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4776 if (idx == 0)
4777 str = get_spaces (str);
4779 ambiguous.release ();
4780 ret = error_mark_node;
4781 baselink = NULL_TREE;
4783 id = ret;
4785 if (id && baselink)
4786 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4787 id, id, tf_warning_or_error);
4788 return id;
4791 /* Helper function for cp_parser_omp_declare_reduction_exprs
4792 and tsubst_omp_udr.
4793 Remove CLEANUP_STMT for data (omp_priv variable).
4794 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4795 DECL_EXPR. */
4797 tree
4798 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4800 if (TYPE_P (*tp))
4801 *walk_subtrees = 0;
4802 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4803 *tp = CLEANUP_BODY (*tp);
4804 else if (TREE_CODE (*tp) == DECL_EXPR)
4806 tree decl = DECL_EXPR_DECL (*tp);
4807 if (!processing_template_decl
4808 && decl == (tree) data
4809 && DECL_INITIAL (decl)
4810 && DECL_INITIAL (decl) != error_mark_node)
4812 tree list = NULL_TREE;
4813 append_to_statement_list_force (*tp, &list);
4814 tree init_expr = build2 (INIT_EXPR, void_type_node,
4815 decl, DECL_INITIAL (decl));
4816 DECL_INITIAL (decl) = NULL_TREE;
4817 append_to_statement_list_force (init_expr, &list);
4818 *tp = list;
4821 return NULL_TREE;
4824 /* Data passed from cp_check_omp_declare_reduction to
4825 cp_check_omp_declare_reduction_r. */
4827 struct cp_check_omp_declare_reduction_data
4829 location_t loc;
4830 tree stmts[7];
4831 bool combiner_p;
4834 /* Helper function for cp_check_omp_declare_reduction, called via
4835 cp_walk_tree. */
4837 static tree
4838 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4840 struct cp_check_omp_declare_reduction_data *udr_data
4841 = (struct cp_check_omp_declare_reduction_data *) data;
4842 if (SSA_VAR_P (*tp)
4843 && !DECL_ARTIFICIAL (*tp)
4844 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4845 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4847 location_t loc = udr_data->loc;
4848 if (udr_data->combiner_p)
4849 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4850 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4851 *tp);
4852 else
4853 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4854 "to variable %qD which is not %<omp_priv%> nor "
4855 "%<omp_orig%>",
4856 *tp);
4857 return *tp;
4859 return NULL_TREE;
4862 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4864 void
4865 cp_check_omp_declare_reduction (tree udr)
4867 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4868 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4869 type = TREE_TYPE (type);
4870 int i;
4871 location_t loc = DECL_SOURCE_LOCATION (udr);
4873 if (type == error_mark_node)
4874 return;
4875 if (ARITHMETIC_TYPE_P (type))
4877 static enum tree_code predef_codes[]
4878 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4879 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4880 for (i = 0; i < 8; i++)
4882 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4883 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4884 const char *n2 = IDENTIFIER_POINTER (id);
4885 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4886 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4887 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4888 break;
4891 if (i == 8
4892 && TREE_CODE (type) != COMPLEX_EXPR)
4894 const char prefix_minmax[] = "omp declare reduction m";
4895 size_t prefix_size = sizeof (prefix_minmax) - 1;
4896 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4897 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4898 prefix_minmax, prefix_size) == 0
4899 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4900 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4901 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4902 i = 0;
4904 if (i < 8)
4906 error_at (loc, "predeclared arithmetic type %qT in "
4907 "%<#pragma omp declare reduction%>", type);
4908 return;
4911 else if (TREE_CODE (type) == FUNCTION_TYPE
4912 || TREE_CODE (type) == METHOD_TYPE
4913 || TREE_CODE (type) == ARRAY_TYPE)
4915 error_at (loc, "function or array type %qT in "
4916 "%<#pragma omp declare reduction%>", type);
4917 return;
4919 else if (TREE_CODE (type) == REFERENCE_TYPE)
4921 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4922 type);
4923 return;
4925 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4927 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4928 "%<#pragma omp declare reduction%>", type);
4929 return;
4932 tree body = DECL_SAVED_TREE (udr);
4933 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4934 return;
4936 tree_stmt_iterator tsi;
4937 struct cp_check_omp_declare_reduction_data data;
4938 memset (data.stmts, 0, sizeof data.stmts);
4939 for (i = 0, tsi = tsi_start (body);
4940 i < 7 && !tsi_end_p (tsi);
4941 i++, tsi_next (&tsi))
4942 data.stmts[i] = tsi_stmt (tsi);
4943 data.loc = loc;
4944 gcc_assert (tsi_end_p (tsi));
4945 if (i >= 3)
4947 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4948 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4949 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4950 return;
4951 data.combiner_p = true;
4952 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4953 &data, NULL))
4954 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4956 if (i >= 6)
4958 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4959 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4960 data.combiner_p = false;
4961 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4962 &data, NULL)
4963 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4964 cp_check_omp_declare_reduction_r, &data, NULL))
4965 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4966 if (i == 7)
4967 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4971 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4972 an inline call. But, remap
4973 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4974 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4976 static tree
4977 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4978 tree decl, tree placeholder)
4980 copy_body_data id;
4981 struct pointer_map_t *decl_map = pointer_map_create ();
4983 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
4984 *pointer_map_insert (decl_map, omp_decl2) = decl;
4985 memset (&id, 0, sizeof (id));
4986 id.src_fn = DECL_CONTEXT (omp_decl1);
4987 id.dst_fn = current_function_decl;
4988 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4989 id.decl_map = decl_map;
4991 id.copy_decl = copy_decl_no_change;
4992 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4993 id.transform_new_cfg = true;
4994 id.transform_return_to_modify = false;
4995 id.transform_lang_insert_block = NULL;
4996 id.eh_lp_nr = 0;
4997 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
4998 pointer_map_destroy (decl_map);
4999 return stmt;
5002 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5003 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5005 static tree
5006 find_omp_placeholder_r (tree *tp, int *, void *data)
5008 if (*tp == (tree) data)
5009 return *tp;
5010 return NULL_TREE;
5013 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5014 Return true if there is some error and the clause should be removed. */
5016 static bool
5017 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5019 tree t = OMP_CLAUSE_DECL (c);
5020 bool predefined = false;
5021 tree type = TREE_TYPE (t);
5022 if (TREE_CODE (type) == REFERENCE_TYPE)
5023 type = TREE_TYPE (type);
5024 if (type == error_mark_node)
5025 return true;
5026 else if (ARITHMETIC_TYPE_P (type))
5027 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5029 case PLUS_EXPR:
5030 case MULT_EXPR:
5031 case MINUS_EXPR:
5032 predefined = true;
5033 break;
5034 case MIN_EXPR:
5035 case MAX_EXPR:
5036 if (TREE_CODE (type) == COMPLEX_TYPE)
5037 break;
5038 predefined = true;
5039 break;
5040 case BIT_AND_EXPR:
5041 case BIT_IOR_EXPR:
5042 case BIT_XOR_EXPR:
5043 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5044 break;
5045 predefined = true;
5046 break;
5047 case TRUTH_ANDIF_EXPR:
5048 case TRUTH_ORIF_EXPR:
5049 if (FLOAT_TYPE_P (type))
5050 break;
5051 predefined = true;
5052 break;
5053 default:
5054 break;
5056 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5058 error ("%qE has invalid type for %<reduction%>", t);
5059 return true;
5061 else if (!processing_template_decl)
5063 t = require_complete_type (t);
5064 if (t == error_mark_node)
5065 return true;
5066 OMP_CLAUSE_DECL (c) = t;
5069 if (predefined)
5071 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5072 return false;
5074 else if (processing_template_decl)
5075 return false;
5077 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5079 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5080 if (TREE_CODE (type) == REFERENCE_TYPE)
5081 type = TREE_TYPE (type);
5082 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5083 if (id == NULL_TREE)
5084 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5085 NULL_TREE, NULL_TREE);
5086 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5087 if (id)
5089 if (id == error_mark_node)
5090 return true;
5091 id = OVL_CURRENT (id);
5092 mark_used (id);
5093 tree body = DECL_SAVED_TREE (id);
5094 if (TREE_CODE (body) == STATEMENT_LIST)
5096 tree_stmt_iterator tsi;
5097 tree placeholder = NULL_TREE;
5098 int i;
5099 tree stmts[7];
5100 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5101 atype = TREE_TYPE (atype);
5102 bool need_static_cast = !same_type_p (type, atype);
5103 memset (stmts, 0, sizeof stmts);
5104 for (i = 0, tsi = tsi_start (body);
5105 i < 7 && !tsi_end_p (tsi);
5106 i++, tsi_next (&tsi))
5107 stmts[i] = tsi_stmt (tsi);
5108 gcc_assert (tsi_end_p (tsi));
5110 if (i >= 3)
5112 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5113 && TREE_CODE (stmts[1]) == DECL_EXPR);
5114 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5115 DECL_ARTIFICIAL (placeholder) = 1;
5116 DECL_IGNORED_P (placeholder) = 1;
5117 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5118 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5119 cxx_mark_addressable (placeholder);
5120 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5121 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5122 != REFERENCE_TYPE)
5123 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5124 tree omp_out = placeholder;
5125 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5126 if (need_static_cast)
5128 tree rtype = build_reference_type (atype);
5129 omp_out = build_static_cast (rtype, omp_out,
5130 tf_warning_or_error);
5131 omp_in = build_static_cast (rtype, omp_in,
5132 tf_warning_or_error);
5133 if (omp_out == error_mark_node || omp_in == error_mark_node)
5134 return true;
5135 omp_out = convert_from_reference (omp_out);
5136 omp_in = convert_from_reference (omp_in);
5138 OMP_CLAUSE_REDUCTION_MERGE (c)
5139 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5140 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5142 if (i >= 6)
5144 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5145 && TREE_CODE (stmts[4]) == DECL_EXPR);
5146 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5147 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5148 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5149 cxx_mark_addressable (placeholder);
5150 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5151 tree omp_orig = placeholder;
5152 if (need_static_cast)
5154 if (i == 7)
5156 error_at (OMP_CLAUSE_LOCATION (c),
5157 "user defined reduction with constructor "
5158 "initializer for base class %qT", atype);
5159 return true;
5161 tree rtype = build_reference_type (atype);
5162 omp_priv = build_static_cast (rtype, omp_priv,
5163 tf_warning_or_error);
5164 omp_orig = build_static_cast (rtype, omp_orig,
5165 tf_warning_or_error);
5166 if (omp_priv == error_mark_node
5167 || omp_orig == error_mark_node)
5168 return true;
5169 omp_priv = convert_from_reference (omp_priv);
5170 omp_orig = convert_from_reference (omp_orig);
5172 if (i == 6)
5173 *need_default_ctor = true;
5174 OMP_CLAUSE_REDUCTION_INIT (c)
5175 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5176 DECL_EXPR_DECL (stmts[3]),
5177 omp_priv, omp_orig);
5178 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5179 find_omp_placeholder_r, placeholder, NULL))
5180 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5182 else if (i >= 3)
5184 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5185 *need_default_ctor = true;
5186 else
5188 tree init;
5189 tree v = convert_from_reference (t);
5190 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5191 init = build_constructor (TREE_TYPE (v), NULL);
5192 else
5193 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5194 OMP_CLAUSE_REDUCTION_INIT (c)
5195 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5200 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5201 *need_dtor = true;
5202 else
5204 error ("user defined reduction not found for %qD", t);
5205 return true;
5207 return false;
5210 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5211 Remove any elements from the list that are invalid. */
5213 tree
5214 finish_omp_clauses (tree clauses)
5216 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5217 bitmap_head aligned_head;
5218 tree c, t, *pc = &clauses;
5219 bool branch_seen = false;
5220 bool copyprivate_seen = false;
5222 bitmap_obstack_initialize (NULL);
5223 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5224 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5225 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5226 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5228 for (pc = &clauses, c = clauses; c ; c = *pc)
5230 bool remove = false;
5232 switch (OMP_CLAUSE_CODE (c))
5234 case OMP_CLAUSE_SHARED:
5235 goto check_dup_generic;
5236 case OMP_CLAUSE_PRIVATE:
5237 goto check_dup_generic;
5238 case OMP_CLAUSE_REDUCTION:
5239 goto check_dup_generic;
5240 case OMP_CLAUSE_COPYPRIVATE:
5241 copyprivate_seen = true;
5242 goto check_dup_generic;
5243 case OMP_CLAUSE_COPYIN:
5244 goto check_dup_generic;
5245 case OMP_CLAUSE_LINEAR:
5246 t = OMP_CLAUSE_DECL (c);
5247 if (!type_dependent_expression_p (t)
5248 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5249 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5251 error ("linear clause applied to non-integral non-pointer "
5252 "variable with %qT type", TREE_TYPE (t));
5253 remove = true;
5254 break;
5256 t = OMP_CLAUSE_LINEAR_STEP (c);
5257 if (t == NULL_TREE)
5258 t = integer_one_node;
5259 if (t == error_mark_node)
5261 remove = true;
5262 break;
5264 else if (!type_dependent_expression_p (t)
5265 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5267 error ("linear step expression must be integral");
5268 remove = true;
5269 break;
5271 else
5273 t = mark_rvalue_use (t);
5274 if (!processing_template_decl)
5276 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5277 t = maybe_constant_value (t);
5278 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5279 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5280 == POINTER_TYPE)
5282 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5283 OMP_CLAUSE_DECL (c), t);
5284 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5285 MINUS_EXPR, sizetype, t,
5286 OMP_CLAUSE_DECL (c));
5287 if (t == error_mark_node)
5289 remove = true;
5290 break;
5294 OMP_CLAUSE_LINEAR_STEP (c) = t;
5296 goto check_dup_generic;
5297 check_dup_generic:
5298 t = OMP_CLAUSE_DECL (c);
5299 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5301 if (processing_template_decl)
5302 break;
5303 if (DECL_P (t))
5304 error ("%qD is not a variable in clause %qs", t,
5305 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5306 else
5307 error ("%qE is not a variable in clause %qs", t,
5308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5309 remove = true;
5311 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5312 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5313 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5315 error ("%qD appears more than once in data clauses", t);
5316 remove = true;
5318 else
5319 bitmap_set_bit (&generic_head, DECL_UID (t));
5320 break;
5322 case OMP_CLAUSE_FIRSTPRIVATE:
5323 t = OMP_CLAUSE_DECL (c);
5324 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5326 if (processing_template_decl)
5327 break;
5328 if (DECL_P (t))
5329 error ("%qD is not a variable in clause %<firstprivate%>", t);
5330 else
5331 error ("%qE is not a variable in clause %<firstprivate%>", t);
5332 remove = true;
5334 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5335 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5337 error ("%qD appears more than once in data clauses", t);
5338 remove = true;
5340 else
5341 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5342 break;
5344 case OMP_CLAUSE_LASTPRIVATE:
5345 t = OMP_CLAUSE_DECL (c);
5346 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5348 if (processing_template_decl)
5349 break;
5350 if (DECL_P (t))
5351 error ("%qD is not a variable in clause %<lastprivate%>", t);
5352 else
5353 error ("%qE is not a variable in clause %<lastprivate%>", t);
5354 remove = true;
5356 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5357 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5359 error ("%qD appears more than once in data clauses", t);
5360 remove = true;
5362 else
5363 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5364 break;
5366 case OMP_CLAUSE_IF:
5367 t = OMP_CLAUSE_IF_EXPR (c);
5368 t = maybe_convert_cond (t);
5369 if (t == error_mark_node)
5370 remove = true;
5371 else if (!processing_template_decl)
5372 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5373 OMP_CLAUSE_IF_EXPR (c) = t;
5374 break;
5376 case OMP_CLAUSE_FINAL:
5377 t = OMP_CLAUSE_FINAL_EXPR (c);
5378 t = maybe_convert_cond (t);
5379 if (t == error_mark_node)
5380 remove = true;
5381 else if (!processing_template_decl)
5382 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5383 OMP_CLAUSE_FINAL_EXPR (c) = t;
5384 break;
5386 case OMP_CLAUSE_NUM_THREADS:
5387 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5388 if (t == error_mark_node)
5389 remove = true;
5390 else if (!type_dependent_expression_p (t)
5391 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5393 error ("num_threads expression must be integral");
5394 remove = true;
5396 else
5398 t = mark_rvalue_use (t);
5399 if (!processing_template_decl)
5400 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5401 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5403 break;
5405 case OMP_CLAUSE_SCHEDULE:
5406 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5407 if (t == NULL)
5409 else if (t == error_mark_node)
5410 remove = true;
5411 else if (!type_dependent_expression_p (t)
5412 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5414 error ("schedule chunk size expression must be integral");
5415 remove = true;
5417 else
5419 t = mark_rvalue_use (t);
5420 if (!processing_template_decl)
5421 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5422 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5424 break;
5426 case OMP_CLAUSE_SIMDLEN:
5427 case OMP_CLAUSE_SAFELEN:
5428 t = OMP_CLAUSE_OPERAND (c, 0);
5429 if (t == error_mark_node)
5430 remove = true;
5431 else if (!type_dependent_expression_p (t)
5432 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5434 error ("%qs length expression must be integral",
5435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5436 remove = true;
5438 else
5440 t = mark_rvalue_use (t);
5441 t = maybe_constant_value (t);
5442 if (!processing_template_decl)
5444 if (TREE_CODE (t) != INTEGER_CST
5445 || tree_int_cst_sgn (t) != 1)
5447 error ("%qs length expression must be positive constant"
5448 " integer expression",
5449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5450 remove = true;
5453 OMP_CLAUSE_OPERAND (c, 0) = t;
5455 break;
5457 case OMP_CLAUSE_NUM_TEAMS:
5458 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5459 if (t == error_mark_node)
5460 remove = true;
5461 else if (!type_dependent_expression_p (t)
5462 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5464 error ("%<num_teams%> expression must be integral");
5465 remove = true;
5467 else
5469 t = mark_rvalue_use (t);
5470 if (!processing_template_decl)
5471 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5472 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5474 break;
5476 case OMP_CLAUSE_THREAD_LIMIT:
5477 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5478 if (t == error_mark_node)
5479 remove = true;
5480 else if (!type_dependent_expression_p (t)
5481 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5483 error ("%<thread_limit%> expression must be integral");
5484 remove = true;
5486 else
5488 t = mark_rvalue_use (t);
5489 if (!processing_template_decl)
5490 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5491 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5493 break;
5495 case OMP_CLAUSE_DEVICE:
5496 t = OMP_CLAUSE_DEVICE_ID (c);
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 ("%<device%> id must be integral");
5503 remove = true;
5505 else
5507 t = mark_rvalue_use (t);
5508 if (!processing_template_decl)
5509 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5510 OMP_CLAUSE_DEVICE_ID (c) = t;
5512 break;
5514 case OMP_CLAUSE_DIST_SCHEDULE:
5515 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5516 if (t == NULL)
5518 else if (t == error_mark_node)
5519 remove = true;
5520 else if (!type_dependent_expression_p (t)
5521 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5523 error ("%<dist_schedule%> chunk size expression must be "
5524 "integral");
5525 remove = true;
5527 else
5529 t = mark_rvalue_use (t);
5530 if (!processing_template_decl)
5531 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5532 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5534 break;
5536 case OMP_CLAUSE_ALIGNED:
5537 t = OMP_CLAUSE_DECL (c);
5538 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5540 if (processing_template_decl)
5541 break;
5542 if (DECL_P (t))
5543 error ("%qD is not a variable in %<aligned%> clause", t);
5544 else
5545 error ("%qE is not a variable in %<aligned%> clause", t);
5546 remove = true;
5548 else if (!type_dependent_expression_p (t)
5549 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5550 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5551 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5552 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5553 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5554 != ARRAY_TYPE))))
5556 error_at (OMP_CLAUSE_LOCATION (c),
5557 "%qE in %<aligned%> clause is neither a pointer nor "
5558 "an array nor a reference to pointer or array", t);
5559 remove = true;
5561 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5563 error ("%qD appears more than once in %<aligned%> clauses", t);
5564 remove = true;
5566 else
5567 bitmap_set_bit (&aligned_head, DECL_UID (t));
5568 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5569 if (t == error_mark_node)
5570 remove = true;
5571 else if (t == NULL_TREE)
5572 break;
5573 else if (!type_dependent_expression_p (t)
5574 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5576 error ("%<aligned%> clause alignment expression must "
5577 "be integral");
5578 remove = true;
5580 else
5582 t = mark_rvalue_use (t);
5583 t = maybe_constant_value (t);
5584 if (!processing_template_decl)
5586 if (TREE_CODE (t) != INTEGER_CST
5587 || tree_int_cst_sgn (t) != 1)
5589 error ("%<aligned%> clause alignment expression must be "
5590 "positive constant integer expression");
5591 remove = true;
5594 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5596 break;
5598 case OMP_CLAUSE_DEPEND:
5599 t = OMP_CLAUSE_DECL (c);
5600 if (TREE_CODE (t) == TREE_LIST)
5602 if (handle_omp_array_sections (c))
5603 remove = true;
5604 break;
5606 if (t == error_mark_node)
5607 remove = true;
5608 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5610 if (processing_template_decl)
5611 break;
5612 if (DECL_P (t))
5613 error ("%qD is not a variable in %<depend%> clause", t);
5614 else
5615 error ("%qE is not a variable in %<depend%> clause", t);
5616 remove = true;
5618 else if (!processing_template_decl
5619 && !cxx_mark_addressable (t))
5620 remove = true;
5621 break;
5623 case OMP_CLAUSE_MAP:
5624 case OMP_CLAUSE_TO:
5625 case OMP_CLAUSE_FROM:
5626 t = OMP_CLAUSE_DECL (c);
5627 if (TREE_CODE (t) == TREE_LIST)
5629 if (handle_omp_array_sections (c))
5630 remove = true;
5631 else
5633 t = OMP_CLAUSE_DECL (c);
5634 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5636 error_at (OMP_CLAUSE_LOCATION (c),
5637 "array section does not have mappable type "
5638 "in %qs clause",
5639 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5640 remove = true;
5643 break;
5645 if (t == error_mark_node)
5646 remove = true;
5647 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5649 if (processing_template_decl)
5650 break;
5651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5652 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5653 break;
5654 if (DECL_P (t))
5655 error ("%qD is not a variable in %qs clause", t,
5656 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5657 else
5658 error ("%qE is not a variable in %qs clause", t,
5659 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5660 remove = true;
5662 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5664 error ("%qD is threadprivate variable in %qs clause", t,
5665 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5666 remove = true;
5668 else if (!processing_template_decl
5669 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5670 && !cxx_mark_addressable (t))
5671 remove = true;
5672 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5673 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5674 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5675 == REFERENCE_TYPE)
5676 ? TREE_TYPE (TREE_TYPE (t))
5677 : TREE_TYPE (t)))
5679 error_at (OMP_CLAUSE_LOCATION (c),
5680 "%qD does not have a mappable type in %qs clause", t,
5681 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5682 remove = true;
5684 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5686 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5687 error ("%qD appears more than once in motion clauses", t);
5688 else
5689 error ("%qD appears more than once in map clauses", t);
5690 remove = true;
5692 else
5693 bitmap_set_bit (&generic_head, DECL_UID (t));
5694 break;
5696 case OMP_CLAUSE_UNIFORM:
5697 t = OMP_CLAUSE_DECL (c);
5698 if (TREE_CODE (t) != PARM_DECL)
5700 if (processing_template_decl)
5701 break;
5702 if (DECL_P (t))
5703 error ("%qD is not an argument in %<uniform%> clause", t);
5704 else
5705 error ("%qE is not an argument in %<uniform%> clause", t);
5706 remove = true;
5707 break;
5709 goto check_dup_generic;
5711 case OMP_CLAUSE_NOWAIT:
5712 case OMP_CLAUSE_ORDERED:
5713 case OMP_CLAUSE_DEFAULT:
5714 case OMP_CLAUSE_UNTIED:
5715 case OMP_CLAUSE_COLLAPSE:
5716 case OMP_CLAUSE_MERGEABLE:
5717 case OMP_CLAUSE_PARALLEL:
5718 case OMP_CLAUSE_FOR:
5719 case OMP_CLAUSE_SECTIONS:
5720 case OMP_CLAUSE_TASKGROUP:
5721 case OMP_CLAUSE_PROC_BIND:
5722 break;
5724 case OMP_CLAUSE_INBRANCH:
5725 case OMP_CLAUSE_NOTINBRANCH:
5726 if (branch_seen)
5728 error ("%<inbranch%> clause is incompatible with "
5729 "%<notinbranch%>");
5730 remove = true;
5732 branch_seen = true;
5733 break;
5735 default:
5736 gcc_unreachable ();
5739 if (remove)
5740 *pc = OMP_CLAUSE_CHAIN (c);
5741 else
5742 pc = &OMP_CLAUSE_CHAIN (c);
5745 for (pc = &clauses, c = clauses; c ; c = *pc)
5747 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5748 bool remove = false;
5749 bool need_complete_non_reference = false;
5750 bool need_default_ctor = false;
5751 bool need_copy_ctor = false;
5752 bool need_copy_assignment = false;
5753 bool need_implicitly_determined = false;
5754 bool need_dtor = false;
5755 tree type, inner_type;
5757 switch (c_kind)
5759 case OMP_CLAUSE_SHARED:
5760 need_implicitly_determined = true;
5761 break;
5762 case OMP_CLAUSE_PRIVATE:
5763 need_complete_non_reference = true;
5764 need_default_ctor = true;
5765 need_dtor = true;
5766 need_implicitly_determined = true;
5767 break;
5768 case OMP_CLAUSE_FIRSTPRIVATE:
5769 need_complete_non_reference = true;
5770 need_copy_ctor = true;
5771 need_dtor = true;
5772 need_implicitly_determined = true;
5773 break;
5774 case OMP_CLAUSE_LASTPRIVATE:
5775 need_complete_non_reference = true;
5776 need_copy_assignment = true;
5777 need_implicitly_determined = true;
5778 break;
5779 case OMP_CLAUSE_REDUCTION:
5780 need_implicitly_determined = true;
5781 break;
5782 case OMP_CLAUSE_COPYPRIVATE:
5783 need_copy_assignment = true;
5784 break;
5785 case OMP_CLAUSE_COPYIN:
5786 need_copy_assignment = true;
5787 break;
5788 case OMP_CLAUSE_NOWAIT:
5789 if (copyprivate_seen)
5791 error_at (OMP_CLAUSE_LOCATION (c),
5792 "%<nowait%> clause must not be used together "
5793 "with %<copyprivate%>");
5794 *pc = OMP_CLAUSE_CHAIN (c);
5795 continue;
5797 /* FALLTHRU */
5798 default:
5799 pc = &OMP_CLAUSE_CHAIN (c);
5800 continue;
5803 t = OMP_CLAUSE_DECL (c);
5804 if (processing_template_decl
5805 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5807 pc = &OMP_CLAUSE_CHAIN (c);
5808 continue;
5811 switch (c_kind)
5813 case OMP_CLAUSE_LASTPRIVATE:
5814 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5816 need_default_ctor = true;
5817 need_dtor = true;
5819 break;
5821 case OMP_CLAUSE_REDUCTION:
5822 if (finish_omp_reduction_clause (c, &need_default_ctor,
5823 &need_dtor))
5824 remove = true;
5825 else
5826 t = OMP_CLAUSE_DECL (c);
5827 break;
5829 case OMP_CLAUSE_COPYIN:
5830 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5832 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5833 remove = true;
5835 break;
5837 default:
5838 break;
5841 if (need_complete_non_reference || need_copy_assignment)
5843 t = require_complete_type (t);
5844 if (t == error_mark_node)
5845 remove = true;
5846 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5847 && need_complete_non_reference)
5849 error ("%qE has reference type for %qs", t,
5850 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5851 remove = true;
5854 if (need_implicitly_determined)
5856 const char *share_name = NULL;
5858 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5859 share_name = "threadprivate";
5860 else switch (cxx_omp_predetermined_sharing (t))
5862 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5863 break;
5864 case OMP_CLAUSE_DEFAULT_SHARED:
5865 /* const vars may be specified in firstprivate clause. */
5866 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5867 && cxx_omp_const_qual_no_mutable (t))
5868 break;
5869 share_name = "shared";
5870 break;
5871 case OMP_CLAUSE_DEFAULT_PRIVATE:
5872 share_name = "private";
5873 break;
5874 default:
5875 gcc_unreachable ();
5877 if (share_name)
5879 error ("%qE is predetermined %qs for %qs",
5880 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5881 remove = true;
5885 /* We're interested in the base element, not arrays. */
5886 inner_type = type = TREE_TYPE (t);
5887 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5888 inner_type = TREE_TYPE (inner_type);
5890 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5891 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5892 inner_type = TREE_TYPE (inner_type);
5894 /* Check for special function availability by building a call to one.
5895 Save the results, because later we won't be in the right context
5896 for making these queries. */
5897 if (CLASS_TYPE_P (inner_type)
5898 && COMPLETE_TYPE_P (inner_type)
5899 && (need_default_ctor || need_copy_ctor
5900 || need_copy_assignment || need_dtor)
5901 && !type_dependent_expression_p (t)
5902 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5903 need_copy_ctor, need_copy_assignment,
5904 need_dtor))
5905 remove = true;
5907 if (remove)
5908 *pc = OMP_CLAUSE_CHAIN (c);
5909 else
5910 pc = &OMP_CLAUSE_CHAIN (c);
5913 bitmap_obstack_release (NULL);
5914 return clauses;
5917 /* For all variables in the tree_list VARS, mark them as thread local. */
5919 void
5920 finish_omp_threadprivate (tree vars)
5922 tree t;
5924 /* Mark every variable in VARS to be assigned thread local storage. */
5925 for (t = vars; t; t = TREE_CHAIN (t))
5927 tree v = TREE_PURPOSE (t);
5929 if (error_operand_p (v))
5931 else if (!VAR_P (v))
5932 error ("%<threadprivate%> %qD is not file, namespace "
5933 "or block scope variable", v);
5934 /* If V had already been marked threadprivate, it doesn't matter
5935 whether it had been used prior to this point. */
5936 else if (TREE_USED (v)
5937 && (DECL_LANG_SPECIFIC (v) == NULL
5938 || !CP_DECL_THREADPRIVATE_P (v)))
5939 error ("%qE declared %<threadprivate%> after first use", v);
5940 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5941 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5942 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5943 error ("%<threadprivate%> %qE has incomplete type", v);
5944 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5945 && CP_DECL_CONTEXT (v) != current_class_type)
5946 error ("%<threadprivate%> %qE directive not "
5947 "in %qT definition", v, CP_DECL_CONTEXT (v));
5948 else
5950 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5951 if (DECL_LANG_SPECIFIC (v) == NULL)
5953 retrofit_lang_decl (v);
5955 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5956 after the allocation of the lang_decl structure. */
5957 if (DECL_DISCRIMINATOR_P (v))
5958 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5961 if (! DECL_THREAD_LOCAL_P (v))
5963 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
5964 /* If rtl has been already set for this var, call
5965 make_decl_rtl once again, so that encode_section_info
5966 has a chance to look at the new decl flags. */
5967 if (DECL_RTL_SET_P (v))
5968 make_decl_rtl (v);
5970 CP_DECL_THREADPRIVATE_P (v) = 1;
5975 /* Build an OpenMP structured block. */
5977 tree
5978 begin_omp_structured_block (void)
5980 return do_pushlevel (sk_omp);
5983 tree
5984 finish_omp_structured_block (tree block)
5986 return do_poplevel (block);
5989 /* Similarly, except force the retention of the BLOCK. */
5991 tree
5992 begin_omp_parallel (void)
5994 keep_next_level (true);
5995 return begin_omp_structured_block ();
5998 tree
5999 finish_omp_parallel (tree clauses, tree body)
6001 tree stmt;
6003 body = finish_omp_structured_block (body);
6005 stmt = make_node (OMP_PARALLEL);
6006 TREE_TYPE (stmt) = void_type_node;
6007 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6008 OMP_PARALLEL_BODY (stmt) = body;
6010 return add_stmt (stmt);
6013 tree
6014 begin_omp_task (void)
6016 keep_next_level (true);
6017 return begin_omp_structured_block ();
6020 tree
6021 finish_omp_task (tree clauses, tree body)
6023 tree stmt;
6025 body = finish_omp_structured_block (body);
6027 stmt = make_node (OMP_TASK);
6028 TREE_TYPE (stmt) = void_type_node;
6029 OMP_TASK_CLAUSES (stmt) = clauses;
6030 OMP_TASK_BODY (stmt) = body;
6032 return add_stmt (stmt);
6035 /* Helper function for finish_omp_for. Convert Ith random access iterator
6036 into integral iterator. Return FALSE if successful. */
6038 static bool
6039 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6040 tree condv, tree incrv, tree *body,
6041 tree *pre_body, tree clauses)
6043 tree diff, iter_init, iter_incr = NULL, last;
6044 tree incr_var = NULL, orig_pre_body, orig_body, c;
6045 tree decl = TREE_VEC_ELT (declv, i);
6046 tree init = TREE_VEC_ELT (initv, i);
6047 tree cond = TREE_VEC_ELT (condv, i);
6048 tree incr = TREE_VEC_ELT (incrv, i);
6049 tree iter = decl;
6050 location_t elocus = locus;
6052 if (init && EXPR_HAS_LOCATION (init))
6053 elocus = EXPR_LOCATION (init);
6055 switch (TREE_CODE (cond))
6057 case GT_EXPR:
6058 case GE_EXPR:
6059 case LT_EXPR:
6060 case LE_EXPR:
6061 if (TREE_OPERAND (cond, 1) == iter)
6062 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6063 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6064 if (TREE_OPERAND (cond, 0) != iter)
6065 cond = error_mark_node;
6066 else
6068 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6069 TREE_CODE (cond),
6070 iter, ERROR_MARK,
6071 TREE_OPERAND (cond, 1), ERROR_MARK,
6072 NULL, tf_warning_or_error);
6073 if (error_operand_p (tem))
6074 return true;
6076 break;
6077 default:
6078 cond = error_mark_node;
6079 break;
6081 if (cond == error_mark_node)
6083 error_at (elocus, "invalid controlling predicate");
6084 return true;
6086 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6087 ERROR_MARK, iter, ERROR_MARK, NULL,
6088 tf_warning_or_error);
6089 if (error_operand_p (diff))
6090 return true;
6091 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6093 error_at (elocus, "difference between %qE and %qD does not have integer type",
6094 TREE_OPERAND (cond, 1), iter);
6095 return true;
6098 switch (TREE_CODE (incr))
6100 case PREINCREMENT_EXPR:
6101 case PREDECREMENT_EXPR:
6102 case POSTINCREMENT_EXPR:
6103 case POSTDECREMENT_EXPR:
6104 if (TREE_OPERAND (incr, 0) != iter)
6106 incr = error_mark_node;
6107 break;
6109 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6110 TREE_CODE (incr), iter,
6111 tf_warning_or_error);
6112 if (error_operand_p (iter_incr))
6113 return true;
6114 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6115 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6116 incr = integer_one_node;
6117 else
6118 incr = integer_minus_one_node;
6119 break;
6120 case MODIFY_EXPR:
6121 if (TREE_OPERAND (incr, 0) != iter)
6122 incr = error_mark_node;
6123 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6124 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6126 tree rhs = TREE_OPERAND (incr, 1);
6127 if (TREE_OPERAND (rhs, 0) == iter)
6129 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6130 != INTEGER_TYPE)
6131 incr = error_mark_node;
6132 else
6134 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6135 iter, TREE_CODE (rhs),
6136 TREE_OPERAND (rhs, 1),
6137 tf_warning_or_error);
6138 if (error_operand_p (iter_incr))
6139 return true;
6140 incr = TREE_OPERAND (rhs, 1);
6141 incr = cp_convert (TREE_TYPE (diff), incr,
6142 tf_warning_or_error);
6143 if (TREE_CODE (rhs) == MINUS_EXPR)
6145 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6146 incr = fold_if_not_in_template (incr);
6148 if (TREE_CODE (incr) != INTEGER_CST
6149 && (TREE_CODE (incr) != NOP_EXPR
6150 || (TREE_CODE (TREE_OPERAND (incr, 0))
6151 != INTEGER_CST)))
6152 iter_incr = NULL;
6155 else if (TREE_OPERAND (rhs, 1) == iter)
6157 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6158 || TREE_CODE (rhs) != PLUS_EXPR)
6159 incr = error_mark_node;
6160 else
6162 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6163 PLUS_EXPR,
6164 TREE_OPERAND (rhs, 0),
6165 ERROR_MARK, iter,
6166 ERROR_MARK, NULL,
6167 tf_warning_or_error);
6168 if (error_operand_p (iter_incr))
6169 return true;
6170 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6171 iter, NOP_EXPR,
6172 iter_incr,
6173 tf_warning_or_error);
6174 if (error_operand_p (iter_incr))
6175 return true;
6176 incr = TREE_OPERAND (rhs, 0);
6177 iter_incr = NULL;
6180 else
6181 incr = error_mark_node;
6183 else
6184 incr = error_mark_node;
6185 break;
6186 default:
6187 incr = error_mark_node;
6188 break;
6191 if (incr == error_mark_node)
6193 error_at (elocus, "invalid increment expression");
6194 return true;
6197 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6198 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6199 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6200 && OMP_CLAUSE_DECL (c) == iter)
6201 break;
6203 decl = create_temporary_var (TREE_TYPE (diff));
6204 pushdecl (decl);
6205 add_decl_expr (decl);
6206 last = create_temporary_var (TREE_TYPE (diff));
6207 pushdecl (last);
6208 add_decl_expr (last);
6209 if (c && iter_incr == NULL)
6211 incr_var = create_temporary_var (TREE_TYPE (diff));
6212 pushdecl (incr_var);
6213 add_decl_expr (incr_var);
6215 gcc_assert (stmts_are_full_exprs_p ());
6217 orig_pre_body = *pre_body;
6218 *pre_body = push_stmt_list ();
6219 if (orig_pre_body)
6220 add_stmt (orig_pre_body);
6221 if (init != NULL)
6222 finish_expr_stmt (build_x_modify_expr (elocus,
6223 iter, NOP_EXPR, init,
6224 tf_warning_or_error));
6225 init = build_int_cst (TREE_TYPE (diff), 0);
6226 if (c && iter_incr == NULL)
6228 finish_expr_stmt (build_x_modify_expr (elocus,
6229 incr_var, NOP_EXPR,
6230 incr, tf_warning_or_error));
6231 incr = incr_var;
6232 iter_incr = build_x_modify_expr (elocus,
6233 iter, PLUS_EXPR, incr,
6234 tf_warning_or_error);
6236 finish_expr_stmt (build_x_modify_expr (elocus,
6237 last, NOP_EXPR, init,
6238 tf_warning_or_error));
6239 *pre_body = pop_stmt_list (*pre_body);
6241 cond = cp_build_binary_op (elocus,
6242 TREE_CODE (cond), decl, diff,
6243 tf_warning_or_error);
6244 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6245 elocus, incr, NULL_TREE);
6247 orig_body = *body;
6248 *body = push_stmt_list ();
6249 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6250 iter_init = build_x_modify_expr (elocus,
6251 iter, PLUS_EXPR, iter_init,
6252 tf_warning_or_error);
6253 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6254 finish_expr_stmt (iter_init);
6255 finish_expr_stmt (build_x_modify_expr (elocus,
6256 last, NOP_EXPR, decl,
6257 tf_warning_or_error));
6258 add_stmt (orig_body);
6259 *body = pop_stmt_list (*body);
6261 if (c)
6263 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6264 finish_expr_stmt (iter_incr);
6265 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6266 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6269 TREE_VEC_ELT (declv, i) = decl;
6270 TREE_VEC_ELT (initv, i) = init;
6271 TREE_VEC_ELT (condv, i) = cond;
6272 TREE_VEC_ELT (incrv, i) = incr;
6274 return false;
6277 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6278 are directly for their associated operands in the statement. DECL
6279 and INIT are a combo; if DECL is NULL then INIT ought to be a
6280 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6281 optional statements that need to go before the loop into its
6282 sk_omp scope. */
6284 tree
6285 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6286 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6288 tree omp_for = NULL, orig_incr = NULL;
6289 tree decl, init, cond, incr;
6290 location_t elocus;
6291 int i;
6293 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6294 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6295 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6296 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6298 decl = TREE_VEC_ELT (declv, i);
6299 init = TREE_VEC_ELT (initv, i);
6300 cond = TREE_VEC_ELT (condv, i);
6301 incr = TREE_VEC_ELT (incrv, i);
6302 elocus = locus;
6304 if (decl == NULL)
6306 if (init != NULL)
6307 switch (TREE_CODE (init))
6309 case MODIFY_EXPR:
6310 decl = TREE_OPERAND (init, 0);
6311 init = TREE_OPERAND (init, 1);
6312 break;
6313 case MODOP_EXPR:
6314 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6316 decl = TREE_OPERAND (init, 0);
6317 init = TREE_OPERAND (init, 2);
6319 break;
6320 default:
6321 break;
6324 if (decl == NULL)
6326 error_at (locus,
6327 "expected iteration declaration or initialization");
6328 return NULL;
6332 if (init && EXPR_HAS_LOCATION (init))
6333 elocus = EXPR_LOCATION (init);
6335 if (cond == NULL)
6337 error_at (elocus, "missing controlling predicate");
6338 return NULL;
6341 if (incr == NULL)
6343 error_at (elocus, "missing increment expression");
6344 return NULL;
6347 TREE_VEC_ELT (declv, i) = decl;
6348 TREE_VEC_ELT (initv, i) = init;
6351 if (dependent_omp_for_p (declv, initv, condv, incrv))
6353 tree stmt;
6355 stmt = make_node (code);
6357 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6359 /* This is really just a place-holder. We'll be decomposing this
6360 again and going through the cp_build_modify_expr path below when
6361 we instantiate the thing. */
6362 TREE_VEC_ELT (initv, i)
6363 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6364 TREE_VEC_ELT (initv, i));
6367 TREE_TYPE (stmt) = void_type_node;
6368 OMP_FOR_INIT (stmt) = initv;
6369 OMP_FOR_COND (stmt) = condv;
6370 OMP_FOR_INCR (stmt) = incrv;
6371 OMP_FOR_BODY (stmt) = body;
6372 OMP_FOR_PRE_BODY (stmt) = pre_body;
6373 OMP_FOR_CLAUSES (stmt) = clauses;
6375 SET_EXPR_LOCATION (stmt, locus);
6376 return add_stmt (stmt);
6379 if (processing_template_decl)
6380 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6382 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6384 decl = TREE_VEC_ELT (declv, i);
6385 init = TREE_VEC_ELT (initv, i);
6386 cond = TREE_VEC_ELT (condv, i);
6387 incr = TREE_VEC_ELT (incrv, i);
6388 if (orig_incr)
6389 TREE_VEC_ELT (orig_incr, i) = incr;
6390 elocus = locus;
6392 if (init && EXPR_HAS_LOCATION (init))
6393 elocus = EXPR_LOCATION (init);
6395 if (!DECL_P (decl))
6397 error_at (elocus, "expected iteration declaration or initialization");
6398 return NULL;
6401 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6403 if (orig_incr)
6404 TREE_VEC_ELT (orig_incr, i) = incr;
6405 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6406 TREE_CODE (TREE_OPERAND (incr, 1)),
6407 TREE_OPERAND (incr, 2),
6408 tf_warning_or_error);
6411 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6413 if (code == OMP_SIMD)
6415 error_at (elocus, "%<#pragma omp simd%> used with class "
6416 "iteration variable %qE", decl);
6417 return NULL;
6419 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6420 incrv, &body, &pre_body, clauses))
6421 return NULL;
6422 continue;
6425 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6426 && !TYPE_PTR_P (TREE_TYPE (decl)))
6428 error_at (elocus, "invalid type for iteration variable %qE", decl);
6429 return NULL;
6432 if (!processing_template_decl)
6434 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6435 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6437 else
6438 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6439 if (cond
6440 && TREE_SIDE_EFFECTS (cond)
6441 && COMPARISON_CLASS_P (cond)
6442 && !processing_template_decl)
6444 tree t = TREE_OPERAND (cond, 0);
6445 if (TREE_SIDE_EFFECTS (t)
6446 && t != decl
6447 && (TREE_CODE (t) != NOP_EXPR
6448 || TREE_OPERAND (t, 0) != decl))
6449 TREE_OPERAND (cond, 0)
6450 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6452 t = TREE_OPERAND (cond, 1);
6453 if (TREE_SIDE_EFFECTS (t)
6454 && t != decl
6455 && (TREE_CODE (t) != NOP_EXPR
6456 || TREE_OPERAND (t, 0) != decl))
6457 TREE_OPERAND (cond, 1)
6458 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6460 if (decl == error_mark_node || init == error_mark_node)
6461 return NULL;
6463 TREE_VEC_ELT (declv, i) = decl;
6464 TREE_VEC_ELT (initv, i) = init;
6465 TREE_VEC_ELT (condv, i) = cond;
6466 TREE_VEC_ELT (incrv, i) = incr;
6467 i++;
6470 if (IS_EMPTY_STMT (pre_body))
6471 pre_body = NULL;
6473 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6474 body, pre_body);
6476 if (omp_for == NULL)
6477 return NULL;
6479 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6481 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6482 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6484 if (TREE_CODE (incr) != MODIFY_EXPR)
6485 continue;
6487 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6488 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6489 && !processing_template_decl)
6491 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6492 if (TREE_SIDE_EFFECTS (t)
6493 && t != decl
6494 && (TREE_CODE (t) != NOP_EXPR
6495 || TREE_OPERAND (t, 0) != decl))
6496 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6497 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6499 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6500 if (TREE_SIDE_EFFECTS (t)
6501 && t != decl
6502 && (TREE_CODE (t) != NOP_EXPR
6503 || TREE_OPERAND (t, 0) != decl))
6504 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6505 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6508 if (orig_incr)
6509 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6511 if (omp_for != NULL)
6512 OMP_FOR_CLAUSES (omp_for) = clauses;
6513 return omp_for;
6516 void
6517 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6518 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6520 tree orig_lhs;
6521 tree orig_rhs;
6522 tree orig_v;
6523 tree orig_lhs1;
6524 tree orig_rhs1;
6525 bool dependent_p;
6526 tree stmt;
6528 orig_lhs = lhs;
6529 orig_rhs = rhs;
6530 orig_v = v;
6531 orig_lhs1 = lhs1;
6532 orig_rhs1 = rhs1;
6533 dependent_p = false;
6534 stmt = NULL_TREE;
6536 /* Even in a template, we can detect invalid uses of the atomic
6537 pragma if neither LHS nor RHS is type-dependent. */
6538 if (processing_template_decl)
6540 dependent_p = (type_dependent_expression_p (lhs)
6541 || (rhs && type_dependent_expression_p (rhs))
6542 || (v && type_dependent_expression_p (v))
6543 || (lhs1 && type_dependent_expression_p (lhs1))
6544 || (rhs1 && type_dependent_expression_p (rhs1)));
6545 if (!dependent_p)
6547 lhs = build_non_dependent_expr (lhs);
6548 if (rhs)
6549 rhs = build_non_dependent_expr (rhs);
6550 if (v)
6551 v = build_non_dependent_expr (v);
6552 if (lhs1)
6553 lhs1 = build_non_dependent_expr (lhs1);
6554 if (rhs1)
6555 rhs1 = build_non_dependent_expr (rhs1);
6558 if (!dependent_p)
6560 bool swapped = false;
6561 if (rhs1 && cp_tree_equal (lhs, rhs))
6563 tree tem = rhs;
6564 rhs = rhs1;
6565 rhs1 = tem;
6566 swapped = !commutative_tree_code (opcode);
6568 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6570 if (code == OMP_ATOMIC)
6571 error ("%<#pragma omp atomic update%> uses two different "
6572 "expressions for memory");
6573 else
6574 error ("%<#pragma omp atomic capture%> uses two different "
6575 "expressions for memory");
6576 return;
6578 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6580 if (code == OMP_ATOMIC)
6581 error ("%<#pragma omp atomic update%> uses two different "
6582 "expressions for memory");
6583 else
6584 error ("%<#pragma omp atomic capture%> uses two different "
6585 "expressions for memory");
6586 return;
6588 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6589 v, lhs1, rhs1, swapped, seq_cst);
6590 if (stmt == error_mark_node)
6591 return;
6593 if (processing_template_decl)
6595 if (code == OMP_ATOMIC_READ)
6597 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6598 OMP_ATOMIC_READ, orig_lhs);
6599 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6600 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6602 else
6604 if (opcode == NOP_EXPR)
6605 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6606 else
6607 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6608 if (orig_rhs1)
6609 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6610 COMPOUND_EXPR, orig_rhs1, stmt);
6611 if (code != OMP_ATOMIC)
6613 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6614 code, orig_lhs1, stmt);
6615 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6616 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6619 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6620 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6622 finish_expr_stmt (stmt);
6625 void
6626 finish_omp_barrier (void)
6628 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6629 vec<tree, va_gc> *vec = make_tree_vector ();
6630 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6631 release_tree_vector (vec);
6632 finish_expr_stmt (stmt);
6635 void
6636 finish_omp_flush (void)
6638 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6639 vec<tree, va_gc> *vec = make_tree_vector ();
6640 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6641 release_tree_vector (vec);
6642 finish_expr_stmt (stmt);
6645 void
6646 finish_omp_taskwait (void)
6648 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6649 vec<tree, va_gc> *vec = make_tree_vector ();
6650 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6651 release_tree_vector (vec);
6652 finish_expr_stmt (stmt);
6655 void
6656 finish_omp_taskyield (void)
6658 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6659 vec<tree, va_gc> *vec = make_tree_vector ();
6660 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6661 release_tree_vector (vec);
6662 finish_expr_stmt (stmt);
6665 void
6666 finish_omp_cancel (tree clauses)
6668 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6669 int mask = 0;
6670 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6671 mask = 1;
6672 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6673 mask = 2;
6674 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6675 mask = 4;
6676 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6677 mask = 8;
6678 else
6680 error ("%<#pragma omp cancel must specify one of "
6681 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6682 return;
6684 vec<tree, va_gc> *vec = make_tree_vector ();
6685 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6686 if (ifc != NULL_TREE)
6688 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6689 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6690 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6691 build_zero_cst (type));
6693 else
6694 ifc = boolean_true_node;
6695 vec->quick_push (build_int_cst (integer_type_node, mask));
6696 vec->quick_push (ifc);
6697 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6698 release_tree_vector (vec);
6699 finish_expr_stmt (stmt);
6702 void
6703 finish_omp_cancellation_point (tree clauses)
6705 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6706 int mask = 0;
6707 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6708 mask = 1;
6709 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6710 mask = 2;
6711 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6712 mask = 4;
6713 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6714 mask = 8;
6715 else
6717 error ("%<#pragma omp cancellation point must specify one of "
6718 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6719 return;
6721 vec<tree, va_gc> *vec
6722 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6723 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6724 release_tree_vector (vec);
6725 finish_expr_stmt (stmt);
6728 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6729 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6730 should create an extra compound stmt. */
6732 tree
6733 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6735 tree r;
6737 if (pcompound)
6738 *pcompound = begin_compound_stmt (0);
6740 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6742 /* Only add the statement to the function if support enabled. */
6743 if (flag_tm)
6744 add_stmt (r);
6745 else
6746 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6747 ? G_("%<__transaction_relaxed%> without "
6748 "transactional memory support enabled")
6749 : G_("%<__transaction_atomic%> without "
6750 "transactional memory support enabled")));
6752 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6753 TREE_SIDE_EFFECTS (r) = 1;
6754 return r;
6757 /* End a __transaction_atomic or __transaction_relaxed statement.
6758 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6759 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6760 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6762 void
6763 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6765 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6766 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6767 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6768 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6770 /* noexcept specifications are not allowed for function transactions. */
6771 gcc_assert (!(noex && compound_stmt));
6772 if (noex)
6774 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6775 noex);
6776 /* This may not be true when the STATEMENT_LIST is empty. */
6777 if (EXPR_P (body))
6778 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6779 TREE_SIDE_EFFECTS (body) = 1;
6780 TRANSACTION_EXPR_BODY (stmt) = body;
6783 if (compound_stmt)
6784 finish_compound_stmt (compound_stmt);
6787 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6788 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6789 condition. */
6791 tree
6792 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6794 tree ret;
6795 if (noex)
6797 expr = build_must_not_throw_expr (expr, noex);
6798 if (EXPR_P (expr))
6799 SET_EXPR_LOCATION (expr, loc);
6800 TREE_SIDE_EFFECTS (expr) = 1;
6802 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6803 if (flags & TM_STMT_ATTR_RELAXED)
6804 TRANSACTION_EXPR_RELAXED (ret) = 1;
6805 TREE_SIDE_EFFECTS (ret) = 1;
6806 SET_EXPR_LOCATION (ret, loc);
6807 return ret;
6810 void
6811 init_cp_semantics (void)
6815 /* Build a STATIC_ASSERT for a static assertion with the condition
6816 CONDITION and the message text MESSAGE. LOCATION is the location
6817 of the static assertion in the source code. When MEMBER_P, this
6818 static assertion is a member of a class. */
6819 void
6820 finish_static_assert (tree condition, tree message, location_t location,
6821 bool member_p)
6823 if (message == NULL_TREE
6824 || message == error_mark_node
6825 || condition == NULL_TREE
6826 || condition == error_mark_node)
6827 return;
6829 if (check_for_bare_parameter_packs (condition))
6830 condition = error_mark_node;
6832 if (type_dependent_expression_p (condition)
6833 || value_dependent_expression_p (condition))
6835 /* We're in a template; build a STATIC_ASSERT and put it in
6836 the right place. */
6837 tree assertion;
6839 assertion = make_node (STATIC_ASSERT);
6840 STATIC_ASSERT_CONDITION (assertion) = condition;
6841 STATIC_ASSERT_MESSAGE (assertion) = message;
6842 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6844 if (member_p)
6845 maybe_add_class_template_decl_list (current_class_type,
6846 assertion,
6847 /*friend_p=*/0);
6848 else
6849 add_stmt (assertion);
6851 return;
6854 /* Fold the expression and convert it to a boolean value. */
6855 condition = fold_non_dependent_expr (condition);
6856 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6857 condition = maybe_constant_value (condition);
6859 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6860 /* Do nothing; the condition is satisfied. */
6862 else
6864 location_t saved_loc = input_location;
6866 input_location = location;
6867 if (TREE_CODE (condition) == INTEGER_CST
6868 && integer_zerop (condition))
6869 /* Report the error. */
6870 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6871 else if (condition && condition != error_mark_node)
6873 error ("non-constant condition for static assertion");
6874 cxx_constant_value (condition);
6876 input_location = saved_loc;
6880 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6881 suitable for use as a type-specifier.
6883 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6884 id-expression or a class member access, FALSE when it was parsed as
6885 a full expression. */
6887 tree
6888 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6889 tsubst_flags_t complain)
6891 tree type = NULL_TREE;
6893 if (!expr || error_operand_p (expr))
6894 return error_mark_node;
6896 if (TYPE_P (expr)
6897 || TREE_CODE (expr) == TYPE_DECL
6898 || (TREE_CODE (expr) == BIT_NOT_EXPR
6899 && TYPE_P (TREE_OPERAND (expr, 0))))
6901 if (complain & tf_error)
6902 error ("argument to decltype must be an expression");
6903 return error_mark_node;
6906 /* Depending on the resolution of DR 1172, we may later need to distinguish
6907 instantiation-dependent but not type-dependent expressions so that, say,
6908 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6909 if (instantiation_dependent_expression_p (expr))
6911 type = cxx_make_type (DECLTYPE_TYPE);
6912 DECLTYPE_TYPE_EXPR (type) = expr;
6913 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6914 = id_expression_or_member_access_p;
6915 SET_TYPE_STRUCTURAL_EQUALITY (type);
6917 return type;
6920 /* The type denoted by decltype(e) is defined as follows: */
6922 expr = resolve_nondeduced_context (expr);
6924 if (invalid_nonstatic_memfn_p (expr, complain))
6925 return error_mark_node;
6927 if (type_unknown_p (expr))
6929 if (complain & tf_error)
6930 error ("decltype cannot resolve address of overloaded function");
6931 return error_mark_node;
6934 /* To get the size of a static data member declared as an array of
6935 unknown bound, we need to instantiate it. */
6936 if (VAR_P (expr)
6937 && VAR_HAD_UNKNOWN_BOUND (expr)
6938 && DECL_TEMPLATE_INSTANTIATION (expr))
6939 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6941 if (id_expression_or_member_access_p)
6943 /* If e is an id-expression or a class member access (5.2.5
6944 [expr.ref]), decltype(e) is defined as the type of the entity
6945 named by e. If there is no such entity, or e names a set of
6946 overloaded functions, the program is ill-formed. */
6947 if (identifier_p (expr))
6948 expr = lookup_name (expr);
6950 if (INDIRECT_REF_P (expr))
6951 /* This can happen when the expression is, e.g., "a.b". Just
6952 look at the underlying operand. */
6953 expr = TREE_OPERAND (expr, 0);
6955 if (TREE_CODE (expr) == OFFSET_REF
6956 || TREE_CODE (expr) == MEMBER_REF
6957 || TREE_CODE (expr) == SCOPE_REF)
6958 /* We're only interested in the field itself. If it is a
6959 BASELINK, we will need to see through it in the next
6960 step. */
6961 expr = TREE_OPERAND (expr, 1);
6963 if (BASELINK_P (expr))
6964 /* See through BASELINK nodes to the underlying function. */
6965 expr = BASELINK_FUNCTIONS (expr);
6967 switch (TREE_CODE (expr))
6969 case FIELD_DECL:
6970 if (DECL_BIT_FIELD_TYPE (expr))
6972 type = DECL_BIT_FIELD_TYPE (expr);
6973 break;
6975 /* Fall through for fields that aren't bitfields. */
6977 case FUNCTION_DECL:
6978 case VAR_DECL:
6979 case CONST_DECL:
6980 case PARM_DECL:
6981 case RESULT_DECL:
6982 case TEMPLATE_PARM_INDEX:
6983 expr = mark_type_use (expr);
6984 type = TREE_TYPE (expr);
6985 break;
6987 case ERROR_MARK:
6988 type = error_mark_node;
6989 break;
6991 case COMPONENT_REF:
6992 case COMPOUND_EXPR:
6993 mark_type_use (expr);
6994 type = is_bitfield_expr_with_lowered_type (expr);
6995 if (!type)
6996 type = TREE_TYPE (TREE_OPERAND (expr, 1));
6997 break;
6999 case BIT_FIELD_REF:
7000 gcc_unreachable ();
7002 case INTEGER_CST:
7003 case PTRMEM_CST:
7004 /* We can get here when the id-expression refers to an
7005 enumerator or non-type template parameter. */
7006 type = TREE_TYPE (expr);
7007 break;
7009 default:
7010 /* Handle instantiated template non-type arguments. */
7011 type = TREE_TYPE (expr);
7012 break;
7015 else
7017 /* Within a lambda-expression:
7019 Every occurrence of decltype((x)) where x is a possibly
7020 parenthesized id-expression that names an entity of
7021 automatic storage duration is treated as if x were
7022 transformed into an access to a corresponding data member
7023 of the closure type that would have been declared if x
7024 were a use of the denoted entity. */
7025 if (outer_automatic_var_p (expr)
7026 && current_function_decl
7027 && LAMBDA_FUNCTION_P (current_function_decl))
7028 type = capture_decltype (expr);
7029 else if (error_operand_p (expr))
7030 type = error_mark_node;
7031 else if (expr == current_class_ptr)
7032 /* If the expression is just "this", we want the
7033 cv-unqualified pointer for the "this" type. */
7034 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7035 else
7037 /* Otherwise, where T is the type of e, if e is an lvalue,
7038 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7039 cp_lvalue_kind clk = lvalue_kind (expr);
7040 type = unlowered_expr_type (expr);
7041 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7043 /* For vector types, pick a non-opaque variant. */
7044 if (TREE_CODE (type) == VECTOR_TYPE)
7045 type = strip_typedefs (type);
7047 if (clk != clk_none && !(clk & clk_class))
7048 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7052 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
7054 if (complain & tf_warning_or_error)
7055 pedwarn (input_location, OPT_Wvla,
7056 "taking decltype of array of runtime bound");
7057 else
7058 return error_mark_node;
7061 return type;
7064 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7065 __has_nothrow_copy, depending on assign_p. */
7067 static bool
7068 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7070 tree fns;
7072 if (assign_p)
7074 int ix;
7075 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7076 if (ix < 0)
7077 return false;
7078 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7080 else if (TYPE_HAS_COPY_CTOR (type))
7082 /* If construction of the copy constructor was postponed, create
7083 it now. */
7084 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7085 lazily_declare_fn (sfk_copy_constructor, type);
7086 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7087 lazily_declare_fn (sfk_move_constructor, type);
7088 fns = CLASSTYPE_CONSTRUCTORS (type);
7090 else
7091 return false;
7093 for (; fns; fns = OVL_NEXT (fns))
7095 tree fn = OVL_CURRENT (fns);
7097 if (assign_p)
7099 if (copy_fn_p (fn) == 0)
7100 continue;
7102 else if (copy_fn_p (fn) <= 0)
7103 continue;
7105 maybe_instantiate_noexcept (fn);
7106 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7107 return false;
7110 return true;
7113 /* Actually evaluates the trait. */
7115 static bool
7116 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7118 enum tree_code type_code1;
7119 tree t;
7121 type_code1 = TREE_CODE (type1);
7123 switch (kind)
7125 case CPTK_HAS_NOTHROW_ASSIGN:
7126 type1 = strip_array_types (type1);
7127 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7128 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7129 || (CLASS_TYPE_P (type1)
7130 && classtype_has_nothrow_assign_or_copy_p (type1,
7131 true))));
7133 case CPTK_HAS_TRIVIAL_ASSIGN:
7134 /* ??? The standard seems to be missing the "or array of such a class
7135 type" wording for this trait. */
7136 type1 = strip_array_types (type1);
7137 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7138 && (trivial_type_p (type1)
7139 || (CLASS_TYPE_P (type1)
7140 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7142 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7143 type1 = strip_array_types (type1);
7144 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7145 || (CLASS_TYPE_P (type1)
7146 && (t = locate_ctor (type1))
7147 && (maybe_instantiate_noexcept (t),
7148 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7150 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7151 type1 = strip_array_types (type1);
7152 return (trivial_type_p (type1)
7153 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7155 case CPTK_HAS_NOTHROW_COPY:
7156 type1 = strip_array_types (type1);
7157 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7158 || (CLASS_TYPE_P (type1)
7159 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7161 case CPTK_HAS_TRIVIAL_COPY:
7162 /* ??? The standard seems to be missing the "or array of such a class
7163 type" wording for this trait. */
7164 type1 = strip_array_types (type1);
7165 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7166 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7168 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7169 type1 = strip_array_types (type1);
7170 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7171 || (CLASS_TYPE_P (type1)
7172 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7174 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7175 return type_has_virtual_destructor (type1);
7177 case CPTK_IS_ABSTRACT:
7178 return (ABSTRACT_CLASS_TYPE_P (type1));
7180 case CPTK_IS_BASE_OF:
7181 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7182 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7183 || DERIVED_FROM_P (type1, type2)));
7185 case CPTK_IS_CLASS:
7186 return (NON_UNION_CLASS_TYPE_P (type1));
7188 case CPTK_IS_CONVERTIBLE_TO:
7189 /* TODO */
7190 return false;
7192 case CPTK_IS_EMPTY:
7193 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7195 case CPTK_IS_ENUM:
7196 return (type_code1 == ENUMERAL_TYPE);
7198 case CPTK_IS_FINAL:
7199 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7201 case CPTK_IS_LITERAL_TYPE:
7202 return (literal_type_p (type1));
7204 case CPTK_IS_POD:
7205 return (pod_type_p (type1));
7207 case CPTK_IS_POLYMORPHIC:
7208 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7210 case CPTK_IS_STD_LAYOUT:
7211 return (std_layout_type_p (type1));
7213 case CPTK_IS_TRIVIAL:
7214 return (trivial_type_p (type1));
7216 case CPTK_IS_UNION:
7217 return (type_code1 == UNION_TYPE);
7219 default:
7220 gcc_unreachable ();
7221 return false;
7225 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7226 void, or a complete type, returns it, otherwise NULL_TREE. */
7228 static tree
7229 check_trait_type (tree type)
7231 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7232 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7233 return type;
7235 if (VOID_TYPE_P (type))
7236 return type;
7238 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7241 /* Process a trait expression. */
7243 tree
7244 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7246 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7247 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7248 || kind == CPTK_HAS_NOTHROW_COPY
7249 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7250 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7251 || kind == CPTK_HAS_TRIVIAL_COPY
7252 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7253 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7254 || kind == CPTK_IS_ABSTRACT
7255 || kind == CPTK_IS_BASE_OF
7256 || kind == CPTK_IS_CLASS
7257 || kind == CPTK_IS_CONVERTIBLE_TO
7258 || kind == CPTK_IS_EMPTY
7259 || kind == CPTK_IS_ENUM
7260 || kind == CPTK_IS_FINAL
7261 || kind == CPTK_IS_LITERAL_TYPE
7262 || kind == CPTK_IS_POD
7263 || kind == CPTK_IS_POLYMORPHIC
7264 || kind == CPTK_IS_STD_LAYOUT
7265 || kind == CPTK_IS_TRIVIAL
7266 || kind == CPTK_IS_UNION);
7268 if (kind == CPTK_IS_CONVERTIBLE_TO)
7270 sorry ("__is_convertible_to");
7271 return error_mark_node;
7274 if (type1 == error_mark_node
7275 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7276 && type2 == error_mark_node))
7277 return error_mark_node;
7279 if (processing_template_decl)
7281 tree trait_expr = make_node (TRAIT_EXPR);
7282 TREE_TYPE (trait_expr) = boolean_type_node;
7283 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7284 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7285 TRAIT_EXPR_KIND (trait_expr) = kind;
7286 return trait_expr;
7289 switch (kind)
7291 case CPTK_HAS_NOTHROW_ASSIGN:
7292 case CPTK_HAS_TRIVIAL_ASSIGN:
7293 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7294 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7295 case CPTK_HAS_NOTHROW_COPY:
7296 case CPTK_HAS_TRIVIAL_COPY:
7297 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7298 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7299 case CPTK_IS_ABSTRACT:
7300 case CPTK_IS_EMPTY:
7301 case CPTK_IS_FINAL:
7302 case CPTK_IS_LITERAL_TYPE:
7303 case CPTK_IS_POD:
7304 case CPTK_IS_POLYMORPHIC:
7305 case CPTK_IS_STD_LAYOUT:
7306 case CPTK_IS_TRIVIAL:
7307 if (!check_trait_type (type1))
7308 return error_mark_node;
7309 break;
7311 case CPTK_IS_BASE_OF:
7312 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7313 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7314 && !complete_type_or_else (type2, NULL_TREE))
7315 /* We already issued an error. */
7316 return error_mark_node;
7317 break;
7319 case CPTK_IS_CLASS:
7320 case CPTK_IS_ENUM:
7321 case CPTK_IS_UNION:
7322 break;
7324 case CPTK_IS_CONVERTIBLE_TO:
7325 default:
7326 gcc_unreachable ();
7329 return (trait_expr_value (kind, type1, type2)
7330 ? boolean_true_node : boolean_false_node);
7333 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7334 which is ignored for C++. */
7336 void
7337 set_float_const_decimal64 (void)
7341 void
7342 clear_float_const_decimal64 (void)
7346 bool
7347 float_const_decimal64_p (void)
7349 return 0;
7353 /* Return true if T is a literal type. */
7355 bool
7356 literal_type_p (tree t)
7358 if (SCALAR_TYPE_P (t)
7359 || TREE_CODE (t) == VECTOR_TYPE
7360 || TREE_CODE (t) == REFERENCE_TYPE)
7361 return true;
7362 if (CLASS_TYPE_P (t))
7364 t = complete_type (t);
7365 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7366 return CLASSTYPE_LITERAL_P (t);
7368 if (TREE_CODE (t) == ARRAY_TYPE)
7369 return literal_type_p (strip_array_types (t));
7370 return false;
7373 /* If DECL is a variable declared `constexpr', require its type
7374 be literal. Return the DECL if OK, otherwise NULL. */
7376 tree
7377 ensure_literal_type_for_constexpr_object (tree decl)
7379 tree type = TREE_TYPE (decl);
7380 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7381 && !processing_template_decl)
7383 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
7384 /* Don't complain here, we'll complain about incompleteness
7385 when we try to initialize the variable. */;
7386 else if (!literal_type_p (type))
7388 error ("the type %qT of constexpr variable %qD is not literal",
7389 type, decl);
7390 explain_non_literal_class (type);
7391 return NULL;
7394 return decl;
7397 /* Representation of entries in the constexpr function definition table. */
7399 typedef struct GTY(()) constexpr_fundef {
7400 tree decl;
7401 tree body;
7402 } constexpr_fundef;
7404 /* This table holds all constexpr function definitions seen in
7405 the current translation unit. */
7407 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7409 /* Utility function used for managing the constexpr function table.
7410 Return true if the entries pointed to by P and Q are for the
7411 same constexpr function. */
7413 static inline int
7414 constexpr_fundef_equal (const void *p, const void *q)
7416 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7417 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7418 return lhs->decl == rhs->decl;
7421 /* Utility function used for managing the constexpr function table.
7422 Return a hash value for the entry pointed to by Q. */
7424 static inline hashval_t
7425 constexpr_fundef_hash (const void *p)
7427 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7428 return DECL_UID (fundef->decl);
7431 /* Return a previously saved definition of function FUN. */
7433 static constexpr_fundef *
7434 retrieve_constexpr_fundef (tree fun)
7436 constexpr_fundef fundef = { NULL, NULL };
7437 if (constexpr_fundef_table == NULL)
7438 return NULL;
7440 fundef.decl = fun;
7441 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7444 /* Check whether the parameter and return types of FUN are valid for a
7445 constexpr function, and complain if COMPLAIN. */
7447 static bool
7448 is_valid_constexpr_fn (tree fun, bool complain)
7450 tree parm = FUNCTION_FIRST_USER_PARM (fun);
7451 bool ret = true;
7452 for (; parm != NULL; parm = TREE_CHAIN (parm))
7453 if (!literal_type_p (TREE_TYPE (parm)))
7455 ret = false;
7456 if (complain)
7458 error ("invalid type for parameter %d of constexpr "
7459 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7460 explain_non_literal_class (TREE_TYPE (parm));
7464 if (!DECL_CONSTRUCTOR_P (fun))
7466 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7467 if (!literal_type_p (rettype))
7469 ret = false;
7470 if (complain)
7472 error ("invalid return type %qT of constexpr function %q+D",
7473 rettype, fun);
7474 explain_non_literal_class (rettype);
7478 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7479 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7481 ret = false;
7482 if (complain)
7484 error ("enclosing class of constexpr non-static member "
7485 "function %q+#D is not a literal type", fun);
7486 explain_non_literal_class (DECL_CONTEXT (fun));
7490 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7492 ret = false;
7493 if (complain)
7494 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7497 return ret;
7500 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7501 for a member of an anonymous aggregate, INIT is the initializer for that
7502 member, and VEC_OUTER is the vector of constructor elements for the class
7503 whose constructor we are processing. Add the initializer to the vector
7504 and return true to indicate success. */
7506 static bool
7507 build_anon_member_initialization (tree member, tree init,
7508 vec<constructor_elt, va_gc> **vec_outer)
7510 /* MEMBER presents the relevant fields from the inside out, but we need
7511 to build up the initializer from the outside in so that we can reuse
7512 previously built CONSTRUCTORs if this is, say, the second field in an
7513 anonymous struct. So we use a vec as a stack. */
7514 auto_vec<tree, 2> fields;
7517 fields.safe_push (TREE_OPERAND (member, 1));
7518 member = TREE_OPERAND (member, 0);
7520 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7521 && TREE_CODE (member) == COMPONENT_REF);
7523 /* VEC has the constructor elements vector for the context of FIELD.
7524 If FIELD is an anonymous aggregate, we will push inside it. */
7525 vec<constructor_elt, va_gc> **vec = vec_outer;
7526 tree field;
7527 while (field = fields.pop(),
7528 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7530 tree ctor;
7531 /* If there is already an outer constructor entry for the anonymous
7532 aggregate FIELD, use it; otherwise, insert one. */
7533 if (vec_safe_is_empty (*vec)
7534 || (*vec)->last().index != field)
7536 ctor = build_constructor (TREE_TYPE (field), NULL);
7537 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7539 else
7540 ctor = (*vec)->last().value;
7541 vec = &CONSTRUCTOR_ELTS (ctor);
7544 /* Now we're at the innermost field, the one that isn't an anonymous
7545 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7546 gcc_assert (fields.is_empty());
7547 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7549 return true;
7552 /* Subroutine of build_constexpr_constructor_member_initializers.
7553 The expression tree T represents a data member initialization
7554 in a (constexpr) constructor definition. Build a pairing of
7555 the data member with its initializer, and prepend that pair
7556 to the existing initialization pair INITS. */
7558 static bool
7559 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7561 tree member, init;
7562 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7563 t = TREE_OPERAND (t, 0);
7564 if (TREE_CODE (t) == EXPR_STMT)
7565 t = TREE_OPERAND (t, 0);
7566 if (t == error_mark_node)
7567 return false;
7568 if (TREE_CODE (t) == STATEMENT_LIST)
7570 tree_stmt_iterator i;
7571 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7573 if (! build_data_member_initialization (tsi_stmt (i), vec))
7574 return false;
7576 return true;
7578 if (TREE_CODE (t) == CLEANUP_STMT)
7580 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7581 but we can in a constexpr constructor for a non-literal class. Just
7582 ignore it; either all the initialization will be constant, in which
7583 case the cleanup can't run, or it can't be constexpr.
7584 Still recurse into CLEANUP_BODY. */
7585 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7587 if (TREE_CODE (t) == CONVERT_EXPR)
7588 t = TREE_OPERAND (t, 0);
7589 if (TREE_CODE (t) == INIT_EXPR
7590 || TREE_CODE (t) == MODIFY_EXPR)
7592 member = TREE_OPERAND (t, 0);
7593 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7595 else if (TREE_CODE (t) == CALL_EXPR)
7597 member = CALL_EXPR_ARG (t, 0);
7598 /* We don't use build_cplus_new here because it complains about
7599 abstract bases. Leaving the call unwrapped means that it has the
7600 wrong type, but cxx_eval_constant_expression doesn't care. */
7601 init = break_out_target_exprs (t);
7603 else if (TREE_CODE (t) == DECL_EXPR)
7604 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7605 return true;
7606 else
7607 gcc_unreachable ();
7608 if (INDIRECT_REF_P (member))
7609 member = TREE_OPERAND (member, 0);
7610 if (TREE_CODE (member) == NOP_EXPR)
7612 tree op = member;
7613 STRIP_NOPS (op);
7614 if (TREE_CODE (op) == ADDR_EXPR)
7616 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7617 (TREE_TYPE (TREE_TYPE (op)),
7618 TREE_TYPE (TREE_TYPE (member))));
7619 /* Initializing a cv-qualified member; we need to look through
7620 the const_cast. */
7621 member = op;
7623 else if (op == current_class_ptr
7624 && (same_type_ignoring_top_level_qualifiers_p
7625 (TREE_TYPE (TREE_TYPE (member)),
7626 current_class_type)))
7627 /* Delegating constructor. */
7628 member = op;
7629 else
7631 /* This is an initializer for an empty base; keep it for now so
7632 we can check it in cxx_eval_bare_aggregate. */
7633 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7636 if (TREE_CODE (member) == ADDR_EXPR)
7637 member = TREE_OPERAND (member, 0);
7638 if (TREE_CODE (member) == COMPONENT_REF)
7640 tree aggr = TREE_OPERAND (member, 0);
7641 if (TREE_CODE (aggr) != COMPONENT_REF)
7642 /* Normal member initialization. */
7643 member = TREE_OPERAND (member, 1);
7644 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7645 /* Initializing a member of an anonymous union. */
7646 return build_anon_member_initialization (member, init, vec);
7647 else
7648 /* We're initializing a vtable pointer in a base. Leave it as
7649 COMPONENT_REF so we remember the path to get to the vfield. */
7650 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7653 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7654 return true;
7657 /* Make sure that there are no statements after LAST in the constructor
7658 body represented by LIST. */
7660 bool
7661 check_constexpr_ctor_body (tree last, tree list)
7663 bool ok = true;
7664 if (TREE_CODE (list) == STATEMENT_LIST)
7666 tree_stmt_iterator i = tsi_last (list);
7667 for (; !tsi_end_p (i); tsi_prev (&i))
7669 tree t = tsi_stmt (i);
7670 if (t == last)
7671 break;
7672 if (TREE_CODE (t) == BIND_EXPR)
7674 if (BIND_EXPR_VARS (t))
7676 ok = false;
7677 break;
7679 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7680 return false;
7681 else
7682 continue;
7684 /* We currently allow typedefs and static_assert.
7685 FIXME allow them in the standard, too. */
7686 if (TREE_CODE (t) != STATIC_ASSERT)
7688 ok = false;
7689 break;
7693 else if (list != last
7694 && TREE_CODE (list) != STATIC_ASSERT)
7695 ok = false;
7696 if (!ok)
7698 error ("constexpr constructor does not have empty body");
7699 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7701 return ok;
7704 /* V is a vector of constructor elements built up for the base and member
7705 initializers of a constructor for TYPE. They need to be in increasing
7706 offset order, which they might not be yet if TYPE has a primary base
7707 which is not first in the base-clause or a vptr and at least one base
7708 all of which are non-primary. */
7710 static vec<constructor_elt, va_gc> *
7711 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7713 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7714 tree field_type;
7715 constructor_elt elt;
7716 int i;
7718 if (pri)
7719 field_type = BINFO_TYPE (pri);
7720 else if (TYPE_CONTAINS_VPTR_P (type))
7721 field_type = vtbl_ptr_type_node;
7722 else
7723 return v;
7725 /* Find the element for the primary base or vptr and move it to the
7726 beginning of the vec. */
7727 vec<constructor_elt, va_gc> &vref = *v;
7728 for (i = 0; ; ++i)
7729 if (TREE_TYPE (vref[i].index) == field_type)
7730 break;
7732 if (i > 0)
7734 elt = vref[i];
7735 for (; i > 0; --i)
7736 vref[i] = vref[i-1];
7737 vref[0] = elt;
7740 return v;
7743 /* Build compile-time evalable representations of member-initializer list
7744 for a constexpr constructor. */
7746 static tree
7747 build_constexpr_constructor_member_initializers (tree type, tree body)
7749 vec<constructor_elt, va_gc> *vec = NULL;
7750 bool ok = true;
7751 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7752 || TREE_CODE (body) == EH_SPEC_BLOCK)
7753 body = TREE_OPERAND (body, 0);
7754 if (TREE_CODE (body) == STATEMENT_LIST)
7755 body = STATEMENT_LIST_HEAD (body)->stmt;
7756 body = BIND_EXPR_BODY (body);
7757 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7759 body = TREE_OPERAND (body, 0);
7760 if (TREE_CODE (body) == EXPR_STMT)
7761 body = TREE_OPERAND (body, 0);
7762 if (TREE_CODE (body) == INIT_EXPR
7763 && (same_type_ignoring_top_level_qualifiers_p
7764 (TREE_TYPE (TREE_OPERAND (body, 0)),
7765 current_class_type)))
7767 /* Trivial copy. */
7768 return TREE_OPERAND (body, 1);
7770 ok = build_data_member_initialization (body, &vec);
7772 else if (TREE_CODE (body) == STATEMENT_LIST)
7774 tree_stmt_iterator i;
7775 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7777 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7778 if (!ok)
7779 break;
7782 else if (TREE_CODE (body) == TRY_BLOCK)
7784 error ("body of %<constexpr%> constructor cannot be "
7785 "a function-try-block");
7786 return error_mark_node;
7788 else if (EXPR_P (body))
7789 ok = build_data_member_initialization (body, &vec);
7790 else
7791 gcc_assert (errorcount > 0);
7792 if (ok)
7794 if (vec_safe_length (vec) > 0)
7796 /* In a delegating constructor, return the target. */
7797 constructor_elt *ce = &(*vec)[0];
7798 if (ce->index == current_class_ptr)
7800 body = ce->value;
7801 vec_free (vec);
7802 return body;
7805 vec = sort_constexpr_mem_initializers (type, vec);
7806 return build_constructor (type, vec);
7808 else
7809 return error_mark_node;
7812 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7813 declared to be constexpr, or a sub-statement thereof. Returns the
7814 return value if suitable, error_mark_node for a statement not allowed in
7815 a constexpr function, or NULL_TREE if no return value was found. */
7817 static tree
7818 constexpr_fn_retval (tree body)
7820 switch (TREE_CODE (body))
7822 case STATEMENT_LIST:
7824 tree_stmt_iterator i;
7825 tree expr = NULL_TREE;
7826 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7828 tree s = constexpr_fn_retval (tsi_stmt (i));
7829 if (s == error_mark_node)
7830 return error_mark_node;
7831 else if (s == NULL_TREE)
7832 /* Keep iterating. */;
7833 else if (expr)
7834 /* Multiple return statements. */
7835 return error_mark_node;
7836 else
7837 expr = s;
7839 return expr;
7842 case RETURN_EXPR:
7843 return break_out_target_exprs (TREE_OPERAND (body, 0));
7845 case DECL_EXPR:
7846 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7847 return NULL_TREE;
7848 return error_mark_node;
7850 case CLEANUP_POINT_EXPR:
7851 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7853 case USING_STMT:
7854 return NULL_TREE;
7856 default:
7857 return error_mark_node;
7861 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7862 FUN; do the necessary transformations to turn it into a single expression
7863 that we can store in the hash table. */
7865 static tree
7866 massage_constexpr_body (tree fun, tree body)
7868 if (DECL_CONSTRUCTOR_P (fun))
7869 body = build_constexpr_constructor_member_initializers
7870 (DECL_CONTEXT (fun), body);
7871 else
7873 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7874 body = EH_SPEC_STMTS (body);
7875 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7876 body = TREE_OPERAND (body, 0);
7877 if (TREE_CODE (body) == BIND_EXPR)
7878 body = BIND_EXPR_BODY (body);
7879 body = constexpr_fn_retval (body);
7881 return body;
7884 /* FUN is a constexpr constructor with massaged body BODY. Return true
7885 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7887 static bool
7888 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7890 bool bad;
7891 tree field;
7892 unsigned i, nelts;
7893 tree ctype;
7895 if (TREE_CODE (body) != CONSTRUCTOR)
7896 return false;
7898 nelts = CONSTRUCTOR_NELTS (body);
7899 ctype = DECL_CONTEXT (fun);
7900 field = TYPE_FIELDS (ctype);
7902 if (TREE_CODE (ctype) == UNION_TYPE)
7904 if (nelts == 0 && next_initializable_field (field))
7906 if (complain)
7907 error ("%<constexpr%> constructor for union %qT must "
7908 "initialize exactly one non-static data member", ctype);
7909 return true;
7911 return false;
7914 bad = false;
7915 for (i = 0; i <= nelts; ++i)
7917 tree index;
7918 if (i == nelts)
7919 index = NULL_TREE;
7920 else
7922 index = CONSTRUCTOR_ELT (body, i)->index;
7923 /* Skip base and vtable inits. */
7924 if (TREE_CODE (index) != FIELD_DECL
7925 || DECL_ARTIFICIAL (index))
7926 continue;
7928 for (; field != index; field = DECL_CHAIN (field))
7930 tree ftype;
7931 if (TREE_CODE (field) != FIELD_DECL
7932 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7933 || DECL_ARTIFICIAL (field))
7934 continue;
7935 ftype = strip_array_types (TREE_TYPE (field));
7936 if (type_has_constexpr_default_constructor (ftype))
7938 /* It's OK to skip a member with a trivial constexpr ctor.
7939 A constexpr ctor that isn't trivial should have been
7940 added in by now. */
7941 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7942 || errorcount != 0);
7943 continue;
7945 if (!complain)
7946 return true;
7947 error ("uninitialized member %qD in %<constexpr%> constructor",
7948 field);
7949 bad = true;
7951 if (field == NULL_TREE)
7952 break;
7953 field = DECL_CHAIN (field);
7956 return bad;
7959 /* We are processing the definition of the constexpr function FUN.
7960 Check that its BODY fulfills the propriate requirements and
7961 enter it in the constexpr function definition table.
7962 For constructor BODY is actually the TREE_LIST of the
7963 member-initializer list. */
7965 tree
7966 register_constexpr_fundef (tree fun, tree body)
7968 constexpr_fundef entry;
7969 constexpr_fundef **slot;
7971 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
7972 return NULL;
7974 body = massage_constexpr_body (fun, body);
7975 if (body == NULL_TREE || body == error_mark_node)
7977 if (!DECL_CONSTRUCTOR_P (fun))
7978 error ("body of constexpr function %qD not a return-statement", fun);
7979 return NULL;
7982 if (!potential_rvalue_constant_expression (body))
7984 if (!DECL_GENERATED_P (fun))
7985 require_potential_rvalue_constant_expression (body);
7986 return NULL;
7989 if (DECL_CONSTRUCTOR_P (fun)
7990 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
7991 return NULL;
7993 /* Create the constexpr function table if necessary. */
7994 if (constexpr_fundef_table == NULL)
7995 constexpr_fundef_table = htab_create_ggc (101,
7996 constexpr_fundef_hash,
7997 constexpr_fundef_equal,
7998 ggc_free);
7999 entry.decl = fun;
8000 entry.body = body;
8001 slot = (constexpr_fundef **)
8002 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8004 gcc_assert (*slot == NULL);
8005 *slot = ggc_alloc_constexpr_fundef ();
8006 **slot = entry;
8008 return fun;
8011 /* FUN is a non-constexpr function called in a context that requires a
8012 constant expression. If it comes from a constexpr template, explain why
8013 the instantiation isn't constexpr. */
8015 void
8016 explain_invalid_constexpr_fn (tree fun)
8018 static struct pointer_set_t *diagnosed;
8019 tree body;
8020 location_t save_loc;
8021 /* Only diagnose defaulted functions or instantiations. */
8022 if (!DECL_DEFAULTED_FN (fun)
8023 && !is_instantiation_of_constexpr (fun))
8024 return;
8025 if (diagnosed == NULL)
8026 diagnosed = pointer_set_create ();
8027 if (pointer_set_insert (diagnosed, fun) != 0)
8028 /* Already explained. */
8029 return;
8031 save_loc = input_location;
8032 input_location = DECL_SOURCE_LOCATION (fun);
8033 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8034 /* First check the declaration. */
8035 if (is_valid_constexpr_fn (fun, true))
8037 /* Then if it's OK, the body. */
8038 if (DECL_DEFAULTED_FN (fun))
8039 explain_implicit_non_constexpr (fun);
8040 else
8042 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8043 require_potential_rvalue_constant_expression (body);
8044 if (DECL_CONSTRUCTOR_P (fun))
8045 cx_check_missing_mem_inits (fun, body, true);
8048 input_location = save_loc;
8051 /* Objects of this type represent calls to constexpr functions
8052 along with the bindings of parameters to their arguments, for
8053 the purpose of compile time evaluation. */
8055 typedef struct GTY(()) constexpr_call {
8056 /* Description of the constexpr function definition. */
8057 constexpr_fundef *fundef;
8058 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8059 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8060 Note: This arrangement is made to accommodate the use of
8061 iterative_hash_template_arg (see pt.c). If you change this
8062 representation, also change the hash calculation in
8063 cxx_eval_call_expression. */
8064 tree bindings;
8065 /* Result of the call.
8066 NULL means the call is being evaluated.
8067 error_mark_node means that the evaluation was erroneous;
8068 otherwise, the actuall value of the call. */
8069 tree result;
8070 /* The hash of this call; we remember it here to avoid having to
8071 recalculate it when expanding the hash table. */
8072 hashval_t hash;
8073 } constexpr_call;
8075 /* A table of all constexpr calls that have been evaluated by the
8076 compiler in this translation unit. */
8078 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8080 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8081 bool, bool, bool *, bool *);
8083 /* Compute a hash value for a constexpr call representation. */
8085 static hashval_t
8086 constexpr_call_hash (const void *p)
8088 const constexpr_call *info = (const constexpr_call *) p;
8089 return info->hash;
8092 /* Return 1 if the objects pointed to by P and Q represent calls
8093 to the same constexpr function with the same arguments.
8094 Otherwise, return 0. */
8096 static int
8097 constexpr_call_equal (const void *p, const void *q)
8099 const constexpr_call *lhs = (const constexpr_call *) p;
8100 const constexpr_call *rhs = (const constexpr_call *) q;
8101 tree lhs_bindings;
8102 tree rhs_bindings;
8103 if (lhs == rhs)
8104 return 1;
8105 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8106 return 0;
8107 lhs_bindings = lhs->bindings;
8108 rhs_bindings = rhs->bindings;
8109 while (lhs_bindings != NULL && rhs_bindings != NULL)
8111 tree lhs_arg = TREE_VALUE (lhs_bindings);
8112 tree rhs_arg = TREE_VALUE (rhs_bindings);
8113 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8114 if (!cp_tree_equal (lhs_arg, rhs_arg))
8115 return 0;
8116 lhs_bindings = TREE_CHAIN (lhs_bindings);
8117 rhs_bindings = TREE_CHAIN (rhs_bindings);
8119 return lhs_bindings == rhs_bindings;
8122 /* Initialize the constexpr call table, if needed. */
8124 static void
8125 maybe_initialize_constexpr_call_table (void)
8127 if (constexpr_call_table == NULL)
8128 constexpr_call_table = htab_create_ggc (101,
8129 constexpr_call_hash,
8130 constexpr_call_equal,
8131 ggc_free);
8134 /* Return true if T designates the implied `this' parameter. */
8136 static inline bool
8137 is_this_parameter (tree t)
8139 return t == current_class_ptr;
8142 /* We have an expression tree T that represents a call, either CALL_EXPR
8143 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8144 retrun the _DECL for that function. */
8146 static tree
8147 get_function_named_in_call (tree t)
8149 tree fun = NULL;
8150 switch (TREE_CODE (t))
8152 case CALL_EXPR:
8153 fun = CALL_EXPR_FN (t);
8154 break;
8156 case AGGR_INIT_EXPR:
8157 fun = AGGR_INIT_EXPR_FN (t);
8158 break;
8160 default:
8161 gcc_unreachable();
8162 break;
8164 if (TREE_CODE (fun) == ADDR_EXPR
8165 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8166 fun = TREE_OPERAND (fun, 0);
8167 return fun;
8170 /* We have an expression tree T that represents a call, either CALL_EXPR
8171 or AGGR_INIT_EXPR. Return the Nth argument. */
8173 static inline tree
8174 get_nth_callarg (tree t, int n)
8176 switch (TREE_CODE (t))
8178 case CALL_EXPR:
8179 return CALL_EXPR_ARG (t, n);
8181 case AGGR_INIT_EXPR:
8182 return AGGR_INIT_EXPR_ARG (t, n);
8184 default:
8185 gcc_unreachable ();
8186 return NULL;
8190 /* Look up the binding of the function parameter T in a constexpr
8191 function call context CALL. */
8193 static tree
8194 lookup_parameter_binding (const constexpr_call *call, tree t)
8196 tree b = purpose_member (t, call->bindings);
8197 return TREE_VALUE (b);
8200 /* Attempt to evaluate T which represents a call to a builtin function.
8201 We assume here that all builtin functions evaluate to scalar types
8202 represented by _CST nodes. */
8204 static tree
8205 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8206 bool allow_non_constant, bool addr,
8207 bool *non_constant_p, bool *overflow_p)
8209 const int nargs = call_expr_nargs (t);
8210 tree *args = (tree *) alloca (nargs * sizeof (tree));
8211 tree new_call;
8212 int i;
8213 for (i = 0; i < nargs; ++i)
8215 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8216 allow_non_constant, addr,
8217 non_constant_p, overflow_p);
8218 if (allow_non_constant && *non_constant_p)
8219 return t;
8221 if (*non_constant_p)
8222 return t;
8223 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8224 CALL_EXPR_FN (t), nargs, args);
8225 new_call = fold (new_call);
8226 VERIFY_CONSTANT (new_call);
8227 return new_call;
8230 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8231 the type of the value to match. */
8233 static tree
8234 adjust_temp_type (tree type, tree temp)
8236 if (TREE_TYPE (temp) == type)
8237 return temp;
8238 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8239 if (TREE_CODE (temp) == CONSTRUCTOR)
8240 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8241 gcc_assert (scalarish_type_p (type));
8242 return cp_fold_convert (type, temp);
8245 /* Subroutine of cxx_eval_call_expression.
8246 We are processing a call expression (either CALL_EXPR or
8247 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8248 all arguments and bind their values to correspondings
8249 parameters, making up the NEW_CALL context. */
8251 static void
8252 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8253 constexpr_call *new_call,
8254 bool allow_non_constant,
8255 bool *non_constant_p, bool *overflow_p)
8257 const int nargs = call_expr_nargs (t);
8258 tree fun = new_call->fundef->decl;
8259 tree parms = DECL_ARGUMENTS (fun);
8260 int i;
8261 for (i = 0; i < nargs; ++i)
8263 tree x, arg;
8264 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8265 /* For member function, the first argument is a pointer to the implied
8266 object. And for an object construction, don't bind `this' before
8267 it is fully constructed. */
8268 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8269 goto next;
8270 x = get_nth_callarg (t, i);
8271 if (parms && DECL_BY_REFERENCE (parms))
8273 /* cp_genericize made this a reference for argument passing, but
8274 we don't want to treat it like one for constexpr evaluation. */
8275 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8276 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8277 type = TREE_TYPE (type);
8278 x = convert_from_reference (x);
8280 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8281 TREE_CODE (type) == REFERENCE_TYPE,
8282 non_constant_p, overflow_p);
8283 /* Don't VERIFY_CONSTANT here. */
8284 if (*non_constant_p && allow_non_constant)
8285 return;
8286 /* Just discard ellipsis args after checking their constantitude. */
8287 if (!parms)
8288 continue;
8289 if (*non_constant_p)
8290 /* Don't try to adjust the type of non-constant args. */
8291 goto next;
8293 /* Make sure the binding has the same type as the parm. */
8294 if (TREE_CODE (type) != REFERENCE_TYPE)
8295 arg = adjust_temp_type (type, arg);
8296 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8297 next:
8298 parms = TREE_CHAIN (parms);
8302 /* Variables and functions to manage constexpr call expansion context.
8303 These do not need to be marked for PCH or GC. */
8305 /* FIXME remember and print actual constant arguments. */
8306 static vec<tree> call_stack = vNULL;
8307 static int call_stack_tick;
8308 static int last_cx_error_tick;
8310 static bool
8311 push_cx_call_context (tree call)
8313 ++call_stack_tick;
8314 if (!EXPR_HAS_LOCATION (call))
8315 SET_EXPR_LOCATION (call, input_location);
8316 call_stack.safe_push (call);
8317 if (call_stack.length () > (unsigned) max_constexpr_depth)
8318 return false;
8319 return true;
8322 static void
8323 pop_cx_call_context (void)
8325 ++call_stack_tick;
8326 call_stack.pop ();
8329 vec<tree>
8330 cx_error_context (void)
8332 vec<tree> r = vNULL;
8333 if (call_stack_tick != last_cx_error_tick
8334 && !call_stack.is_empty ())
8335 r = call_stack;
8336 last_cx_error_tick = call_stack_tick;
8337 return r;
8340 /* Subroutine of cxx_eval_constant_expression.
8341 Evaluate the call expression tree T in the context of OLD_CALL expression
8342 evaluation. */
8344 static tree
8345 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8346 bool allow_non_constant, bool addr,
8347 bool *non_constant_p, bool *overflow_p)
8349 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8350 tree fun = get_function_named_in_call (t);
8351 tree result;
8352 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8353 constexpr_call **slot;
8354 constexpr_call *entry;
8355 bool depth_ok;
8357 if (TREE_CODE (fun) != FUNCTION_DECL)
8359 /* Might be a constexpr function pointer. */
8360 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8361 /*addr*/false, non_constant_p, overflow_p);
8362 if (TREE_CODE (fun) == ADDR_EXPR)
8363 fun = TREE_OPERAND (fun, 0);
8365 if (TREE_CODE (fun) != FUNCTION_DECL)
8367 if (!allow_non_constant && !*non_constant_p)
8368 error_at (loc, "expression %qE does not designate a constexpr "
8369 "function", fun);
8370 *non_constant_p = true;
8371 return t;
8373 if (DECL_CLONED_FUNCTION_P (fun))
8374 fun = DECL_CLONED_FUNCTION (fun);
8375 if (is_builtin_fn (fun))
8376 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8377 addr, non_constant_p, overflow_p);
8378 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8380 if (!allow_non_constant)
8382 error_at (loc, "call to non-constexpr function %qD", fun);
8383 explain_invalid_constexpr_fn (fun);
8385 *non_constant_p = true;
8386 return t;
8389 /* Shortcut trivial constructor/op=. */
8390 if (trivial_fn_p (fun))
8392 if (call_expr_nargs (t) == 2)
8394 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8395 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8396 addr, non_constant_p, overflow_p);
8398 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8399 && AGGR_INIT_ZERO_FIRST (t))
8400 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8403 /* If in direct recursive call, optimize definition search. */
8404 if (old_call != NULL && old_call->fundef->decl == fun)
8405 new_call.fundef = old_call->fundef;
8406 else
8408 new_call.fundef = retrieve_constexpr_fundef (fun);
8409 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8411 if (!allow_non_constant)
8413 if (DECL_INITIAL (fun))
8415 /* The definition of fun was somehow unsuitable. */
8416 error_at (loc, "%qD called in a constant expression", fun);
8417 explain_invalid_constexpr_fn (fun);
8419 else
8420 error_at (loc, "%qD used before its definition", fun);
8422 *non_constant_p = true;
8423 return t;
8426 cxx_bind_parameters_in_call (old_call, t, &new_call,
8427 allow_non_constant, non_constant_p, overflow_p);
8428 if (*non_constant_p)
8429 return t;
8431 depth_ok = push_cx_call_context (t);
8433 new_call.hash
8434 = iterative_hash_template_arg (new_call.bindings,
8435 constexpr_fundef_hash (new_call.fundef));
8437 /* If we have seen this call before, we are done. */
8438 maybe_initialize_constexpr_call_table ();
8439 slot = (constexpr_call **)
8440 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8441 entry = *slot;
8442 if (entry == NULL)
8444 /* We need to keep a pointer to the entry, not just the slot, as the
8445 slot can move in the call to cxx_eval_builtin_function_call. */
8446 *slot = entry = ggc_alloc_constexpr_call ();
8447 *entry = new_call;
8449 /* Calls which are in progress have their result set to NULL
8450 so that we can detect circular dependencies. */
8451 else if (entry->result == NULL)
8453 if (!allow_non_constant)
8454 error ("call has circular dependency");
8455 *non_constant_p = true;
8456 entry->result = result = error_mark_node;
8459 if (!depth_ok)
8461 if (!allow_non_constant)
8462 error ("constexpr evaluation depth exceeds maximum of %d (use "
8463 "-fconstexpr-depth= to increase the maximum)",
8464 max_constexpr_depth);
8465 *non_constant_p = true;
8466 entry->result = result = error_mark_node;
8468 else
8470 result = entry->result;
8471 if (!result || result == error_mark_node)
8472 result = (cxx_eval_constant_expression
8473 (&new_call, new_call.fundef->body,
8474 allow_non_constant, addr,
8475 non_constant_p, overflow_p));
8476 if (result == error_mark_node)
8477 *non_constant_p = true;
8478 if (*non_constant_p)
8479 entry->result = result = error_mark_node;
8480 else
8482 /* If this was a call to initialize an object, set the type of
8483 the CONSTRUCTOR to the type of that object. */
8484 if (DECL_CONSTRUCTOR_P (fun))
8486 tree ob_arg = get_nth_callarg (t, 0);
8487 STRIP_NOPS (ob_arg);
8488 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8489 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8490 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8491 result);
8493 entry->result = result;
8497 pop_cx_call_context ();
8498 return unshare_expr (result);
8501 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8503 bool
8504 reduced_constant_expression_p (tree t)
8506 if (TREE_CODE (t) == PTRMEM_CST)
8507 /* Even if we can't lower this yet, it's constant. */
8508 return true;
8509 /* FIXME are we calling this too much? */
8510 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8513 /* Some expressions may have constant operands but are not constant
8514 themselves, such as 1/0. Call this function (or rather, the macro
8515 following it) to check for that condition.
8517 We only call this in places that require an arithmetic constant, not in
8518 places where we might have a non-constant expression that can be a
8519 component of a constant expression, such as the address of a constexpr
8520 variable that might be dereferenced later. */
8522 static bool
8523 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8524 bool *overflow_p)
8526 if (!*non_constant_p && !reduced_constant_expression_p (t))
8528 if (!allow_non_constant)
8529 error ("%q+E is not a constant expression", t);
8530 *non_constant_p = true;
8532 if (TREE_OVERFLOW_P (t))
8534 if (!allow_non_constant)
8536 permerror (input_location, "overflow in constant expression");
8537 /* If we're being permissive (and are in an enforcing
8538 context), ignore the overflow. */
8539 if (flag_permissive)
8540 return *non_constant_p;
8542 *overflow_p = true;
8544 return *non_constant_p;
8547 /* Subroutine of cxx_eval_constant_expression.
8548 Attempt to reduce the unary expression tree T to a compile time value.
8549 If successful, return the value. Otherwise issue a diagnostic
8550 and return error_mark_node. */
8552 static tree
8553 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8554 bool allow_non_constant, bool addr,
8555 bool *non_constant_p, bool *overflow_p)
8557 tree r;
8558 tree orig_arg = TREE_OPERAND (t, 0);
8559 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8560 addr, non_constant_p, overflow_p);
8561 VERIFY_CONSTANT (arg);
8562 if (arg == orig_arg)
8563 return t;
8564 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8565 VERIFY_CONSTANT (r);
8566 return r;
8569 /* Subroutine of cxx_eval_constant_expression.
8570 Like cxx_eval_unary_expression, except for binary expressions. */
8572 static tree
8573 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8574 bool allow_non_constant, bool addr,
8575 bool *non_constant_p, bool *overflow_p)
8577 tree r;
8578 tree orig_lhs = TREE_OPERAND (t, 0);
8579 tree orig_rhs = TREE_OPERAND (t, 1);
8580 tree lhs, rhs;
8581 lhs = cxx_eval_constant_expression (call, orig_lhs,
8582 allow_non_constant, addr,
8583 non_constant_p, overflow_p);
8584 VERIFY_CONSTANT (lhs);
8585 rhs = cxx_eval_constant_expression (call, orig_rhs,
8586 allow_non_constant, addr,
8587 non_constant_p, overflow_p);
8588 VERIFY_CONSTANT (rhs);
8589 if (lhs == orig_lhs && rhs == orig_rhs)
8590 return t;
8591 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8592 VERIFY_CONSTANT (r);
8593 return r;
8596 /* Subroutine of cxx_eval_constant_expression.
8597 Attempt to evaluate condition expressions. Dead branches are not
8598 looked into. */
8600 static tree
8601 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8602 bool allow_non_constant, bool addr,
8603 bool *non_constant_p, bool *overflow_p)
8605 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8606 allow_non_constant, addr,
8607 non_constant_p, overflow_p);
8608 VERIFY_CONSTANT (val);
8609 /* Don't VERIFY_CONSTANT the other operands. */
8610 if (integer_zerop (val))
8611 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8612 allow_non_constant, addr,
8613 non_constant_p, overflow_p);
8614 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8615 allow_non_constant, addr,
8616 non_constant_p, overflow_p);
8619 /* Subroutine of cxx_eval_constant_expression.
8620 Attempt to reduce a reference to an array slot. */
8622 static tree
8623 cxx_eval_array_reference (const constexpr_call *call, tree t,
8624 bool allow_non_constant, bool addr,
8625 bool *non_constant_p, bool *overflow_p)
8627 tree oldary = TREE_OPERAND (t, 0);
8628 tree ary = cxx_eval_constant_expression (call, oldary,
8629 allow_non_constant, addr,
8630 non_constant_p, overflow_p);
8631 tree index, oldidx;
8632 HOST_WIDE_INT i;
8633 tree elem_type;
8634 unsigned len, elem_nchars = 1;
8635 if (*non_constant_p)
8636 return t;
8637 oldidx = TREE_OPERAND (t, 1);
8638 index = cxx_eval_constant_expression (call, oldidx,
8639 allow_non_constant, false,
8640 non_constant_p, overflow_p);
8641 VERIFY_CONSTANT (index);
8642 if (addr && ary == oldary && index == oldidx)
8643 return t;
8644 else if (addr)
8645 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8646 elem_type = TREE_TYPE (TREE_TYPE (ary));
8647 if (TREE_CODE (ary) == CONSTRUCTOR)
8648 len = CONSTRUCTOR_NELTS (ary);
8649 else if (TREE_CODE (ary) == STRING_CST)
8651 elem_nchars = (TYPE_PRECISION (elem_type)
8652 / TYPE_PRECISION (char_type_node));
8653 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8655 else
8657 /* We can't do anything with other tree codes, so use
8658 VERIFY_CONSTANT to complain and fail. */
8659 VERIFY_CONSTANT (ary);
8660 gcc_unreachable ();
8662 if (compare_tree_int (index, len) >= 0)
8664 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8666 /* If it's within the array bounds but doesn't have an explicit
8667 initializer, it's value-initialized. */
8668 tree val = build_value_init (elem_type, tf_warning_or_error);
8669 return cxx_eval_constant_expression (call, val,
8670 allow_non_constant, addr,
8671 non_constant_p, overflow_p);
8674 if (!allow_non_constant)
8675 error ("array subscript out of bound");
8676 *non_constant_p = true;
8677 return t;
8679 else if (tree_int_cst_lt (index, integer_zero_node))
8681 if (!allow_non_constant)
8682 error ("negative array subscript");
8683 *non_constant_p = true;
8684 return t;
8686 i = tree_to_shwi (index);
8687 if (TREE_CODE (ary) == CONSTRUCTOR)
8688 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8689 else if (elem_nchars == 1)
8690 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8691 TREE_STRING_POINTER (ary)[i]);
8692 else
8694 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8695 return native_interpret_expr (type, (const unsigned char *)
8696 TREE_STRING_POINTER (ary)
8697 + i * elem_nchars, elem_nchars);
8699 /* Don't VERIFY_CONSTANT here. */
8702 /* Subroutine of cxx_eval_constant_expression.
8703 Attempt to reduce a field access of a value of class type. */
8705 static tree
8706 cxx_eval_component_reference (const constexpr_call *call, tree t,
8707 bool allow_non_constant, bool addr,
8708 bool *non_constant_p, bool *overflow_p)
8710 unsigned HOST_WIDE_INT i;
8711 tree field;
8712 tree value;
8713 tree part = TREE_OPERAND (t, 1);
8714 tree orig_whole = TREE_OPERAND (t, 0);
8715 tree whole = cxx_eval_constant_expression (call, orig_whole,
8716 allow_non_constant, addr,
8717 non_constant_p, overflow_p);
8718 if (whole == orig_whole)
8719 return t;
8720 if (addr)
8721 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8722 whole, part, NULL_TREE);
8723 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8724 CONSTRUCTOR. */
8725 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8727 if (!allow_non_constant)
8728 error ("%qE is not a constant expression", orig_whole);
8729 *non_constant_p = true;
8731 if (DECL_MUTABLE_P (part))
8733 if (!allow_non_constant)
8734 error ("mutable %qD is not usable in a constant expression", part);
8735 *non_constant_p = true;
8737 if (*non_constant_p)
8738 return t;
8739 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8741 if (field == part)
8742 return value;
8744 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8745 && CONSTRUCTOR_NELTS (whole) > 0)
8747 /* DR 1188 says we don't have to deal with this. */
8748 if (!allow_non_constant)
8749 error ("accessing %qD member instead of initialized %qD member in "
8750 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8751 *non_constant_p = true;
8752 return t;
8755 /* If there's no explicit init for this field, it's value-initialized. */
8756 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8757 return cxx_eval_constant_expression (call, value,
8758 allow_non_constant, addr,
8759 non_constant_p, overflow_p);
8762 /* Subroutine of cxx_eval_constant_expression.
8763 Attempt to reduce a field access of a value of class type that is
8764 expressed as a BIT_FIELD_REF. */
8766 static tree
8767 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8768 bool allow_non_constant, bool addr,
8769 bool *non_constant_p, bool *overflow_p)
8771 tree orig_whole = TREE_OPERAND (t, 0);
8772 tree retval, fldval, utype, mask;
8773 bool fld_seen = false;
8774 HOST_WIDE_INT istart, isize;
8775 tree whole = cxx_eval_constant_expression (call, orig_whole,
8776 allow_non_constant, addr,
8777 non_constant_p, overflow_p);
8778 tree start, field, value;
8779 unsigned HOST_WIDE_INT i;
8781 if (whole == orig_whole)
8782 return t;
8783 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8784 CONSTRUCTOR. */
8785 if (!*non_constant_p
8786 && TREE_CODE (whole) != VECTOR_CST
8787 && TREE_CODE (whole) != CONSTRUCTOR)
8789 if (!allow_non_constant)
8790 error ("%qE is not a constant expression", orig_whole);
8791 *non_constant_p = true;
8793 if (*non_constant_p)
8794 return t;
8796 if (TREE_CODE (whole) == VECTOR_CST)
8797 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8798 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8800 start = TREE_OPERAND (t, 2);
8801 istart = tree_to_shwi (start);
8802 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8803 utype = TREE_TYPE (t);
8804 if (!TYPE_UNSIGNED (utype))
8805 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8806 retval = build_int_cst (utype, 0);
8807 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8809 tree bitpos = bit_position (field);
8810 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8811 return value;
8812 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8813 && TREE_CODE (value) == INTEGER_CST
8814 && tree_fits_shwi_p (bitpos)
8815 && tree_fits_shwi_p (DECL_SIZE (field)))
8817 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8818 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8819 HOST_WIDE_INT shift;
8820 if (bit >= istart && bit + sz <= istart + isize)
8822 fldval = fold_convert (utype, value);
8823 mask = build_int_cst_type (utype, -1);
8824 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8825 size_int (TYPE_PRECISION (utype) - sz));
8826 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8827 size_int (TYPE_PRECISION (utype) - sz));
8828 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8829 shift = bit - istart;
8830 if (BYTES_BIG_ENDIAN)
8831 shift = TYPE_PRECISION (utype) - shift - sz;
8832 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8833 size_int (shift));
8834 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8835 fld_seen = true;
8839 if (fld_seen)
8840 return fold_convert (TREE_TYPE (t), retval);
8841 gcc_unreachable ();
8842 return error_mark_node;
8845 /* Subroutine of cxx_eval_constant_expression.
8846 Evaluate a short-circuited logical expression T in the context
8847 of a given constexpr CALL. BAILOUT_VALUE is the value for
8848 early return. CONTINUE_VALUE is used here purely for
8849 sanity check purposes. */
8851 static tree
8852 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8853 tree bailout_value, tree continue_value,
8854 bool allow_non_constant, bool addr,
8855 bool *non_constant_p, bool *overflow_p)
8857 tree r;
8858 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8859 allow_non_constant, addr,
8860 non_constant_p, overflow_p);
8861 VERIFY_CONSTANT (lhs);
8862 if (tree_int_cst_equal (lhs, bailout_value))
8863 return lhs;
8864 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8865 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8866 allow_non_constant, addr, non_constant_p, overflow_p);
8867 VERIFY_CONSTANT (r);
8868 return r;
8871 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8872 CONSTRUCTOR elements to initialize (part of) an object containing that
8873 field. Return a pointer to the constructor_elt corresponding to the
8874 initialization of the field. */
8876 static constructor_elt *
8877 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8879 tree aggr = TREE_OPERAND (ref, 0);
8880 tree field = TREE_OPERAND (ref, 1);
8881 HOST_WIDE_INT i;
8882 constructor_elt *ce;
8884 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8886 if (TREE_CODE (aggr) == COMPONENT_REF)
8888 constructor_elt *base_ce
8889 = base_field_constructor_elt (v, aggr);
8890 v = CONSTRUCTOR_ELTS (base_ce->value);
8893 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8894 if (ce->index == field)
8895 return ce;
8897 gcc_unreachable ();
8898 return NULL;
8901 /* Subroutine of cxx_eval_constant_expression.
8902 The expression tree T denotes a C-style array or a C-style
8903 aggregate. Reduce it to a constant expression. */
8905 static tree
8906 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8907 bool allow_non_constant, bool addr,
8908 bool *non_constant_p, bool *overflow_p)
8910 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8911 vec<constructor_elt, va_gc> *n;
8912 vec_alloc (n, vec_safe_length (v));
8913 constructor_elt *ce;
8914 HOST_WIDE_INT i;
8915 bool changed = false;
8916 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8917 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8919 tree elt = cxx_eval_constant_expression (call, ce->value,
8920 allow_non_constant, addr,
8921 non_constant_p, overflow_p);
8922 /* Don't VERIFY_CONSTANT here. */
8923 if (allow_non_constant && *non_constant_p)
8924 goto fail;
8925 if (elt != ce->value)
8926 changed = true;
8927 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8929 /* This is an initialization of a vfield inside a base
8930 subaggregate that we already initialized; push this
8931 initialization into the previous initialization. */
8932 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8933 inner->value = elt;
8935 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
8937 /* This is an initializer for an empty base; now that we've
8938 checked that it's constant, we can ignore it. */
8939 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8941 else
8942 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
8944 if (*non_constant_p || !changed)
8946 fail:
8947 vec_free (n);
8948 return t;
8950 t = build_constructor (TREE_TYPE (t), n);
8951 TREE_CONSTANT (t) = true;
8952 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
8953 t = fold (t);
8954 return t;
8957 /* Subroutine of cxx_eval_constant_expression.
8958 The expression tree T is a VEC_INIT_EXPR which denotes the desired
8959 initialization of a non-static data member of array type. Reduce it to a
8960 CONSTRUCTOR.
8962 Note that apart from value-initialization (when VALUE_INIT is true),
8963 this is only intended to support value-initialization and the
8964 initializations done by defaulted constructors for classes with
8965 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
8966 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
8967 for the copy/move constructor. */
8969 static tree
8970 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
8971 bool value_init, bool allow_non_constant, bool addr,
8972 bool *non_constant_p, bool *overflow_p)
8974 tree elttype = TREE_TYPE (atype);
8975 int max = tree_to_shwi (array_type_nelts (atype));
8976 vec<constructor_elt, va_gc> *n;
8977 vec_alloc (n, max + 1);
8978 bool pre_init = false;
8979 int i;
8981 /* For the default constructor, build up a call to the default
8982 constructor of the element type. We only need to handle class types
8983 here, as for a constructor to be constexpr, all members must be
8984 initialized, which for a defaulted default constructor means they must
8985 be of a class type with a constexpr default constructor. */
8986 if (TREE_CODE (elttype) == ARRAY_TYPE)
8987 /* We only do this at the lowest level. */;
8988 else if (value_init)
8990 init = build_value_init (elttype, tf_warning_or_error);
8991 init = cxx_eval_constant_expression
8992 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
8993 pre_init = true;
8995 else if (!init)
8997 vec<tree, va_gc> *argvec = make_tree_vector ();
8998 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8999 &argvec, elttype, LOOKUP_NORMAL,
9000 tf_warning_or_error);
9001 release_tree_vector (argvec);
9002 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9003 addr, non_constant_p, overflow_p);
9004 pre_init = true;
9007 if (*non_constant_p && !allow_non_constant)
9008 goto fail;
9010 for (i = 0; i <= max; ++i)
9012 tree idx = build_int_cst (size_type_node, i);
9013 tree eltinit;
9014 if (TREE_CODE (elttype) == ARRAY_TYPE)
9016 /* A multidimensional array; recurse. */
9017 if (value_init || init == NULL_TREE)
9018 eltinit = NULL_TREE;
9019 else
9020 eltinit = cp_build_array_ref (input_location, init, idx,
9021 tf_warning_or_error);
9022 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9023 allow_non_constant, addr,
9024 non_constant_p, overflow_p);
9026 else if (pre_init)
9028 /* Initializing an element using value or default initialization
9029 we just pre-built above. */
9030 if (i == 0)
9031 eltinit = init;
9032 else
9033 eltinit = unshare_expr (init);
9035 else
9037 /* Copying an element. */
9038 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9039 (atype, TREE_TYPE (init)));
9040 eltinit = cp_build_array_ref (input_location, init, idx,
9041 tf_warning_or_error);
9042 if (!real_lvalue_p (init))
9043 eltinit = move (eltinit);
9044 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9045 eltinit = cxx_eval_constant_expression
9046 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9048 if (*non_constant_p && !allow_non_constant)
9049 goto fail;
9050 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9053 if (!*non_constant_p)
9055 init = build_constructor (atype, n);
9056 TREE_CONSTANT (init) = true;
9057 return init;
9060 fail:
9061 vec_free (n);
9062 return init;
9065 static tree
9066 cxx_eval_vec_init (const constexpr_call *call, tree t,
9067 bool allow_non_constant, bool addr,
9068 bool *non_constant_p, bool *overflow_p)
9070 tree atype = TREE_TYPE (t);
9071 tree init = VEC_INIT_EXPR_INIT (t);
9072 tree r = cxx_eval_vec_init_1 (call, atype, init,
9073 VEC_INIT_EXPR_VALUE_INIT (t),
9074 allow_non_constant, addr, non_constant_p, overflow_p);
9075 if (*non_constant_p)
9076 return t;
9077 else
9078 return r;
9081 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9082 match. We want to be less strict for simple *& folding; if we have a
9083 non-const temporary that we access through a const pointer, that should
9084 work. We handle this here rather than change fold_indirect_ref_1
9085 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9086 don't really make sense outside of constant expression evaluation. Also
9087 we want to allow folding to COMPONENT_REF, which could cause trouble
9088 with TBAA in fold_indirect_ref_1.
9090 Try to keep this function synced with fold_indirect_ref_1. */
9092 static tree
9093 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9095 tree sub, subtype;
9097 sub = op0;
9098 STRIP_NOPS (sub);
9099 subtype = TREE_TYPE (sub);
9100 if (!POINTER_TYPE_P (subtype))
9101 return NULL_TREE;
9103 if (TREE_CODE (sub) == ADDR_EXPR)
9105 tree op = TREE_OPERAND (sub, 0);
9106 tree optype = TREE_TYPE (op);
9108 /* *&CONST_DECL -> to the value of the const decl. */
9109 if (TREE_CODE (op) == CONST_DECL)
9110 return DECL_INITIAL (op);
9111 /* *&p => p; make sure to handle *&"str"[cst] here. */
9112 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9114 tree fop = fold_read_from_constant_string (op);
9115 if (fop)
9116 return fop;
9117 else
9118 return op;
9120 /* *(foo *)&fooarray => fooarray[0] */
9121 else if (TREE_CODE (optype) == ARRAY_TYPE
9122 && (same_type_ignoring_top_level_qualifiers_p
9123 (type, TREE_TYPE (optype))))
9125 tree type_domain = TYPE_DOMAIN (optype);
9126 tree min_val = size_zero_node;
9127 if (type_domain && TYPE_MIN_VALUE (type_domain))
9128 min_val = TYPE_MIN_VALUE (type_domain);
9129 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9130 NULL_TREE, NULL_TREE);
9132 /* *(foo *)&complexfoo => __real__ complexfoo */
9133 else if (TREE_CODE (optype) == COMPLEX_TYPE
9134 && (same_type_ignoring_top_level_qualifiers_p
9135 (type, TREE_TYPE (optype))))
9136 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9137 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9138 else if (TREE_CODE (optype) == VECTOR_TYPE
9139 && (same_type_ignoring_top_level_qualifiers_p
9140 (type, TREE_TYPE (optype))))
9142 tree part_width = TYPE_SIZE (type);
9143 tree index = bitsize_int (0);
9144 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9146 /* Also handle conversion to an empty base class, which
9147 is represented with a NOP_EXPR. */
9148 else if (is_empty_class (type)
9149 && CLASS_TYPE_P (optype)
9150 && DERIVED_FROM_P (type, optype))
9152 *empty_base = true;
9153 return op;
9155 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9156 else if (RECORD_OR_UNION_TYPE_P (optype))
9158 tree field = TYPE_FIELDS (optype);
9159 for (; field; field = DECL_CHAIN (field))
9160 if (TREE_CODE (field) == FIELD_DECL
9161 && integer_zerop (byte_position (field))
9162 && (same_type_ignoring_top_level_qualifiers_p
9163 (TREE_TYPE (field), type)))
9165 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9166 break;
9170 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9171 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9173 tree op00 = TREE_OPERAND (sub, 0);
9174 tree op01 = TREE_OPERAND (sub, 1);
9176 STRIP_NOPS (op00);
9177 if (TREE_CODE (op00) == ADDR_EXPR)
9179 tree op00type;
9180 op00 = TREE_OPERAND (op00, 0);
9181 op00type = TREE_TYPE (op00);
9183 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9184 if (TREE_CODE (op00type) == VECTOR_TYPE
9185 && (same_type_ignoring_top_level_qualifiers_p
9186 (type, TREE_TYPE (op00type))))
9188 HOST_WIDE_INT offset = tree_to_shwi (op01);
9189 tree part_width = TYPE_SIZE (type);
9190 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9191 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9192 tree index = bitsize_int (indexi);
9194 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9195 return fold_build3_loc (loc,
9196 BIT_FIELD_REF, type, op00,
9197 part_width, index);
9200 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9201 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9202 && (same_type_ignoring_top_level_qualifiers_p
9203 (type, TREE_TYPE (op00type))))
9205 tree size = TYPE_SIZE_UNIT (type);
9206 if (tree_int_cst_equal (size, op01))
9207 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9209 /* ((foo *)&fooarray)[1] => fooarray[1] */
9210 else if (TREE_CODE (op00type) == ARRAY_TYPE
9211 && (same_type_ignoring_top_level_qualifiers_p
9212 (type, TREE_TYPE (op00type))))
9214 tree type_domain = TYPE_DOMAIN (op00type);
9215 tree min_val = size_zero_node;
9216 if (type_domain && TYPE_MIN_VALUE (type_domain))
9217 min_val = TYPE_MIN_VALUE (type_domain);
9218 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9219 TYPE_SIZE_UNIT (type));
9220 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9221 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9222 NULL_TREE, NULL_TREE);
9224 /* Also handle conversion to an empty base class, which
9225 is represented with a NOP_EXPR. */
9226 else if (is_empty_class (type)
9227 && CLASS_TYPE_P (op00type)
9228 && DERIVED_FROM_P (type, op00type))
9230 *empty_base = true;
9231 return op00;
9233 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9234 else if (RECORD_OR_UNION_TYPE_P (op00type))
9236 tree field = TYPE_FIELDS (op00type);
9237 for (; field; field = DECL_CHAIN (field))
9238 if (TREE_CODE (field) == FIELD_DECL
9239 && tree_int_cst_equal (byte_position (field), op01)
9240 && (same_type_ignoring_top_level_qualifiers_p
9241 (TREE_TYPE (field), type)))
9243 return fold_build3 (COMPONENT_REF, type, op00,
9244 field, NULL_TREE);
9245 break;
9250 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9251 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9252 && (same_type_ignoring_top_level_qualifiers_p
9253 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9255 tree type_domain;
9256 tree min_val = size_zero_node;
9257 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9258 if (newsub)
9259 sub = newsub;
9260 else
9261 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9262 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9263 if (type_domain && TYPE_MIN_VALUE (type_domain))
9264 min_val = TYPE_MIN_VALUE (type_domain);
9265 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9266 NULL_TREE);
9269 return NULL_TREE;
9272 static tree
9273 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9274 bool allow_non_constant, bool addr,
9275 bool *non_constant_p, bool *overflow_p)
9277 tree orig_op0 = TREE_OPERAND (t, 0);
9278 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9279 /*addr*/false, non_constant_p, overflow_p);
9280 bool empty_base = false;
9281 tree r;
9283 /* Don't VERIFY_CONSTANT here. */
9284 if (*non_constant_p)
9285 return t;
9287 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9288 &empty_base);
9290 if (r)
9291 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9292 addr, non_constant_p, overflow_p);
9293 else
9295 tree sub = op0;
9296 STRIP_NOPS (sub);
9297 if (TREE_CODE (sub) == ADDR_EXPR)
9299 /* We couldn't fold to a constant value. Make sure it's not
9300 something we should have been able to fold. */
9301 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9302 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9303 /* DR 1188 says we don't have to deal with this. */
9304 if (!allow_non_constant)
9305 error ("accessing value of %qE through a %qT glvalue in a "
9306 "constant expression", build_fold_indirect_ref (sub),
9307 TREE_TYPE (t));
9308 *non_constant_p = true;
9309 return t;
9313 /* If we're pulling out the value of an empty base, make sure
9314 that the whole object is constant and then return an empty
9315 CONSTRUCTOR. */
9316 if (empty_base)
9318 VERIFY_CONSTANT (r);
9319 r = build_constructor (TREE_TYPE (t), NULL);
9320 TREE_CONSTANT (r) = true;
9323 if (r == NULL_TREE)
9325 if (addr && op0 != orig_op0)
9326 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9327 if (!addr)
9328 VERIFY_CONSTANT (t);
9329 return t;
9331 return r;
9334 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9335 Shared between potential_constant_expression and
9336 cxx_eval_constant_expression. */
9338 static void
9339 non_const_var_error (tree r)
9341 tree type = TREE_TYPE (r);
9342 error ("the value of %qD is not usable in a constant "
9343 "expression", r);
9344 /* Avoid error cascade. */
9345 if (DECL_INITIAL (r) == error_mark_node)
9346 return;
9347 if (DECL_DECLARED_CONSTEXPR_P (r))
9348 inform (DECL_SOURCE_LOCATION (r),
9349 "%qD used in its own initializer", r);
9350 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9352 if (!CP_TYPE_CONST_P (type))
9353 inform (DECL_SOURCE_LOCATION (r),
9354 "%q#D is not const", r);
9355 else if (CP_TYPE_VOLATILE_P (type))
9356 inform (DECL_SOURCE_LOCATION (r),
9357 "%q#D is volatile", r);
9358 else if (!DECL_INITIAL (r)
9359 || !TREE_CONSTANT (DECL_INITIAL (r)))
9360 inform (DECL_SOURCE_LOCATION (r),
9361 "%qD was not initialized with a constant "
9362 "expression", r);
9363 else
9364 gcc_unreachable ();
9366 else
9368 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9369 inform (DECL_SOURCE_LOCATION (r),
9370 "%qD was not declared %<constexpr%>", r);
9371 else
9372 inform (DECL_SOURCE_LOCATION (r),
9373 "%qD does not have integral or enumeration type",
9378 /* Subroutine of cxx_eval_constant_expression.
9379 Like cxx_eval_unary_expression, except for trinary expressions. */
9381 static tree
9382 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9383 bool allow_non_constant, bool addr,
9384 bool *non_constant_p, bool *overflow_p)
9386 int i;
9387 tree args[3];
9388 tree val;
9390 for (i = 0; i < 3; i++)
9392 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9393 allow_non_constant, addr,
9394 non_constant_p, overflow_p);
9395 VERIFY_CONSTANT (args[i]);
9398 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9399 args[0], args[1], args[2]);
9400 if (val == NULL_TREE)
9401 return t;
9402 VERIFY_CONSTANT (val);
9403 return val;
9406 /* Attempt to reduce the expression T to a constant value.
9407 On failure, issue diagnostic and return error_mark_node. */
9408 /* FIXME unify with c_fully_fold */
9410 static tree
9411 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9412 bool allow_non_constant, bool addr,
9413 bool *non_constant_p, bool *overflow_p)
9415 tree r = t;
9417 if (t == error_mark_node)
9419 *non_constant_p = true;
9420 return t;
9422 if (CONSTANT_CLASS_P (t))
9424 if (TREE_CODE (t) == PTRMEM_CST)
9425 t = cplus_expand_constant (t);
9426 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9427 *overflow_p = true;
9428 return t;
9430 if (TREE_CODE (t) != NOP_EXPR
9431 && reduced_constant_expression_p (t))
9432 return fold (t);
9434 switch (TREE_CODE (t))
9436 case VAR_DECL:
9437 if (addr)
9438 return t;
9439 /* else fall through. */
9440 case CONST_DECL:
9441 r = integral_constant_value (t);
9442 if (TREE_CODE (r) == TARGET_EXPR
9443 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9444 r = TARGET_EXPR_INITIAL (r);
9445 if (DECL_P (r))
9447 if (!allow_non_constant)
9448 non_const_var_error (r);
9449 *non_constant_p = true;
9451 break;
9453 case FUNCTION_DECL:
9454 case TEMPLATE_DECL:
9455 case LABEL_DECL:
9456 return t;
9458 case PARM_DECL:
9459 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9461 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9463 if (!allow_non_constant)
9464 sorry ("use of the value of the object being constructed "
9465 "in a constant expression");
9466 *non_constant_p = true;
9468 else
9469 r = lookup_parameter_binding (call, t);
9471 else if (addr)
9472 /* Defer in case this is only used for its type. */;
9473 else
9475 if (!allow_non_constant)
9476 error ("%qE is not a constant expression", t);
9477 *non_constant_p = true;
9479 break;
9481 case CALL_EXPR:
9482 case AGGR_INIT_EXPR:
9483 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9484 non_constant_p, overflow_p);
9485 break;
9487 case TARGET_EXPR:
9488 if (!literal_type_p (TREE_TYPE (t)))
9490 if (!allow_non_constant)
9492 error ("temporary of non-literal type %qT in a "
9493 "constant expression", TREE_TYPE (t));
9494 explain_non_literal_class (TREE_TYPE (t));
9496 *non_constant_p = true;
9497 break;
9499 /* else fall through. */
9500 case INIT_EXPR:
9501 /* Pass false for 'addr' because these codes indicate
9502 initialization of a temporary. */
9503 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9504 allow_non_constant, false,
9505 non_constant_p, overflow_p);
9506 if (!*non_constant_p)
9507 /* Adjust the type of the result to the type of the temporary. */
9508 r = adjust_temp_type (TREE_TYPE (t), r);
9509 break;
9511 case SCOPE_REF:
9512 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9513 allow_non_constant, addr,
9514 non_constant_p, overflow_p);
9515 break;
9517 case RETURN_EXPR:
9518 case NON_LVALUE_EXPR:
9519 case TRY_CATCH_EXPR:
9520 case CLEANUP_POINT_EXPR:
9521 case MUST_NOT_THROW_EXPR:
9522 case SAVE_EXPR:
9523 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9524 allow_non_constant, addr,
9525 non_constant_p, overflow_p);
9526 break;
9528 /* These differ from cxx_eval_unary_expression in that this doesn't
9529 check for a constant operand or result; an address can be
9530 constant without its operand being, and vice versa. */
9531 case INDIRECT_REF:
9532 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9533 non_constant_p, overflow_p);
9534 break;
9536 case ADDR_EXPR:
9538 tree oldop = TREE_OPERAND (t, 0);
9539 tree op = cxx_eval_constant_expression (call, oldop,
9540 allow_non_constant,
9541 /*addr*/true,
9542 non_constant_p, overflow_p);
9543 /* Don't VERIFY_CONSTANT here. */
9544 if (*non_constant_p)
9545 return t;
9546 /* This function does more aggressive folding than fold itself. */
9547 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9548 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9549 return t;
9550 break;
9553 case REALPART_EXPR:
9554 case IMAGPART_EXPR:
9555 case CONJ_EXPR:
9556 case FIX_TRUNC_EXPR:
9557 case FLOAT_EXPR:
9558 case NEGATE_EXPR:
9559 case ABS_EXPR:
9560 case BIT_NOT_EXPR:
9561 case TRUTH_NOT_EXPR:
9562 case FIXED_CONVERT_EXPR:
9563 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9564 non_constant_p, overflow_p);
9565 break;
9567 case SIZEOF_EXPR:
9568 if (SIZEOF_EXPR_TYPE_P (t))
9569 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9570 SIZEOF_EXPR, false);
9571 else if (TYPE_P (TREE_OPERAND (t, 0)))
9572 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9573 false);
9574 else
9575 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9576 false);
9577 if (r == error_mark_node)
9578 r = size_one_node;
9579 VERIFY_CONSTANT (r);
9580 break;
9582 case COMPOUND_EXPR:
9584 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9585 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9586 introduced by build_call_a. */
9587 tree op0 = TREE_OPERAND (t, 0);
9588 tree op1 = TREE_OPERAND (t, 1);
9589 STRIP_NOPS (op1);
9590 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9591 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9592 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9593 addr, non_constant_p, overflow_p);
9594 else
9596 /* Check that the LHS is constant and then discard it. */
9597 cxx_eval_constant_expression (call, op0, allow_non_constant,
9598 false, non_constant_p, overflow_p);
9599 op1 = TREE_OPERAND (t, 1);
9600 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9601 addr, non_constant_p, overflow_p);
9604 break;
9606 case POINTER_PLUS_EXPR:
9607 case PLUS_EXPR:
9608 case MINUS_EXPR:
9609 case MULT_EXPR:
9610 case TRUNC_DIV_EXPR:
9611 case CEIL_DIV_EXPR:
9612 case FLOOR_DIV_EXPR:
9613 case ROUND_DIV_EXPR:
9614 case TRUNC_MOD_EXPR:
9615 case CEIL_MOD_EXPR:
9616 case ROUND_MOD_EXPR:
9617 case RDIV_EXPR:
9618 case EXACT_DIV_EXPR:
9619 case MIN_EXPR:
9620 case MAX_EXPR:
9621 case LSHIFT_EXPR:
9622 case RSHIFT_EXPR:
9623 case LROTATE_EXPR:
9624 case RROTATE_EXPR:
9625 case BIT_IOR_EXPR:
9626 case BIT_XOR_EXPR:
9627 case BIT_AND_EXPR:
9628 case TRUTH_XOR_EXPR:
9629 case LT_EXPR:
9630 case LE_EXPR:
9631 case GT_EXPR:
9632 case GE_EXPR:
9633 case EQ_EXPR:
9634 case NE_EXPR:
9635 case UNORDERED_EXPR:
9636 case ORDERED_EXPR:
9637 case UNLT_EXPR:
9638 case UNLE_EXPR:
9639 case UNGT_EXPR:
9640 case UNGE_EXPR:
9641 case UNEQ_EXPR:
9642 case LTGT_EXPR:
9643 case RANGE_EXPR:
9644 case COMPLEX_EXPR:
9645 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9646 non_constant_p, overflow_p);
9647 break;
9649 /* fold can introduce non-IF versions of these; still treat them as
9650 short-circuiting. */
9651 case TRUTH_AND_EXPR:
9652 case TRUTH_ANDIF_EXPR:
9653 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9654 boolean_true_node,
9655 allow_non_constant, addr,
9656 non_constant_p, overflow_p);
9657 break;
9659 case TRUTH_OR_EXPR:
9660 case TRUTH_ORIF_EXPR:
9661 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9662 boolean_false_node,
9663 allow_non_constant, addr,
9664 non_constant_p, overflow_p);
9665 break;
9667 case ARRAY_REF:
9668 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9669 non_constant_p, overflow_p);
9670 break;
9672 case COMPONENT_REF:
9673 if (is_overloaded_fn (t))
9675 /* We can only get here in checking mode via
9676 build_non_dependent_expr, because any expression that
9677 calls or takes the address of the function will have
9678 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9679 gcc_checking_assert (allow_non_constant);
9680 *non_constant_p = true;
9681 return t;
9683 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9684 non_constant_p, overflow_p);
9685 break;
9687 case BIT_FIELD_REF:
9688 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9689 non_constant_p, overflow_p);
9690 break;
9692 case COND_EXPR:
9693 case VEC_COND_EXPR:
9694 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9695 non_constant_p, overflow_p);
9696 break;
9698 case CONSTRUCTOR:
9699 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9700 non_constant_p, overflow_p);
9701 break;
9703 case VEC_INIT_EXPR:
9704 /* We can get this in a defaulted constructor for a class with a
9705 non-static data member of array type. Either the initializer will
9706 be NULL, meaning default-initialization, or it will be an lvalue
9707 or xvalue of the same type, meaning direct-initialization from the
9708 corresponding member. */
9709 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9710 non_constant_p, overflow_p);
9711 break;
9713 case FMA_EXPR:
9714 case VEC_PERM_EXPR:
9715 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9716 non_constant_p, overflow_p);
9717 break;
9719 case CONVERT_EXPR:
9720 case VIEW_CONVERT_EXPR:
9721 case NOP_EXPR:
9723 tree oldop = TREE_OPERAND (t, 0);
9724 tree op = cxx_eval_constant_expression (call, oldop,
9725 allow_non_constant, addr,
9726 non_constant_p, overflow_p);
9727 if (*non_constant_p)
9728 return t;
9729 if (POINTER_TYPE_P (TREE_TYPE (t))
9730 && TREE_CODE (op) == INTEGER_CST
9731 && !integer_zerop (op))
9733 if (!allow_non_constant)
9734 error_at (EXPR_LOC_OR_LOC (t, input_location),
9735 "reinterpret_cast from integer to pointer");
9736 *non_constant_p = true;
9737 return t;
9739 if (op == oldop)
9740 /* We didn't fold at the top so we could check for ptr-int
9741 conversion. */
9742 return fold (t);
9743 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9744 /* Conversion of an out-of-range value has implementation-defined
9745 behavior; the language considers it different from arithmetic
9746 overflow, which is undefined. */
9747 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9748 TREE_OVERFLOW (r) = false;
9750 break;
9752 case EMPTY_CLASS_EXPR:
9753 /* This is good enough for a function argument that might not get
9754 used, and they can't do anything with it, so just return it. */
9755 return t;
9757 case LAMBDA_EXPR:
9758 case PREINCREMENT_EXPR:
9759 case POSTINCREMENT_EXPR:
9760 case PREDECREMENT_EXPR:
9761 case POSTDECREMENT_EXPR:
9762 case NEW_EXPR:
9763 case VEC_NEW_EXPR:
9764 case DELETE_EXPR:
9765 case VEC_DELETE_EXPR:
9766 case THROW_EXPR:
9767 case MODIFY_EXPR:
9768 case MODOP_EXPR:
9769 /* GCC internal stuff. */
9770 case VA_ARG_EXPR:
9771 case OBJ_TYPE_REF:
9772 case WITH_CLEANUP_EXPR:
9773 case STATEMENT_LIST:
9774 case BIND_EXPR:
9775 case NON_DEPENDENT_EXPR:
9776 case BASELINK:
9777 case EXPR_STMT:
9778 case OFFSET_REF:
9779 if (!allow_non_constant)
9780 error_at (EXPR_LOC_OR_LOC (t, input_location),
9781 "expression %qE is not a constant-expression", t);
9782 *non_constant_p = true;
9783 break;
9785 default:
9786 internal_error ("unexpected expression %qE of kind %s", t,
9787 get_tree_code_name (TREE_CODE (t)));
9788 *non_constant_p = true;
9789 break;
9792 if (r == error_mark_node)
9793 *non_constant_p = true;
9795 if (*non_constant_p)
9796 return t;
9797 else
9798 return r;
9801 static tree
9802 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9804 bool non_constant_p = false;
9805 bool overflow_p = false;
9806 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9807 false, &non_constant_p, &overflow_p);
9809 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9811 if (TREE_CODE (t) != CONSTRUCTOR
9812 && cp_has_mutable_p (TREE_TYPE (t)))
9814 /* We allow a mutable type if the original expression was a
9815 CONSTRUCTOR so that we can do aggregate initialization of
9816 constexpr variables. */
9817 if (!allow_non_constant)
9818 error ("%qT cannot be the type of a complete constant expression "
9819 "because it has mutable sub-objects", TREE_TYPE (t));
9820 non_constant_p = true;
9823 /* Technically we should check this for all subexpressions, but that
9824 runs into problems with our internal representation of pointer
9825 subtraction and the 5.19 rules are still in flux. */
9826 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9827 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9828 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9830 if (!allow_non_constant)
9831 error ("conversion from pointer type %qT "
9832 "to arithmetic type %qT in a constant-expression",
9833 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9834 non_constant_p = true;
9837 if (!non_constant_p && overflow_p)
9838 non_constant_p = true;
9840 if (non_constant_p && !allow_non_constant)
9841 return error_mark_node;
9842 else if (non_constant_p && TREE_CONSTANT (r))
9844 /* This isn't actually constant, so unset TREE_CONSTANT. */
9845 if (EXPR_P (r))
9846 r = copy_node (r);
9847 else if (TREE_CODE (r) == CONSTRUCTOR)
9848 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9849 else
9850 r = build_nop (TREE_TYPE (r), r);
9851 TREE_CONSTANT (r) = false;
9853 else if (non_constant_p || r == t)
9854 return t;
9856 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9858 if (TREE_CODE (t) == TARGET_EXPR
9859 && TARGET_EXPR_INITIAL (t) == r)
9860 return t;
9861 else
9863 r = get_target_expr (r);
9864 TREE_CONSTANT (r) = true;
9865 return r;
9868 else
9869 return r;
9872 /* Returns true if T is a valid subexpression of a constant expression,
9873 even if it isn't itself a constant expression. */
9875 bool
9876 is_sub_constant_expr (tree t)
9878 bool non_constant_p = false;
9879 bool overflow_p = false;
9880 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9881 &overflow_p);
9882 return !non_constant_p && !overflow_p;
9885 /* If T represents a constant expression returns its reduced value.
9886 Otherwise return error_mark_node. If T is dependent, then
9887 return NULL. */
9889 tree
9890 cxx_constant_value (tree t)
9892 return cxx_eval_outermost_constant_expr (t, false);
9895 /* If T is a constant expression, returns its reduced value.
9896 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9897 Otherwise, returns a version of T without TREE_CONSTANT. */
9899 tree
9900 maybe_constant_value (tree t)
9902 tree r;
9904 if (instantiation_dependent_expression_p (t)
9905 || type_unknown_p (t)
9906 || BRACE_ENCLOSED_INITIALIZER_P (t)
9907 || !potential_constant_expression (t))
9909 if (TREE_OVERFLOW_P (t))
9911 t = build_nop (TREE_TYPE (t), t);
9912 TREE_CONSTANT (t) = false;
9914 return t;
9917 r = cxx_eval_outermost_constant_expr (t, true);
9918 #ifdef ENABLE_CHECKING
9919 /* cp_tree_equal looks through NOPs, so allow them. */
9920 gcc_assert (r == t
9921 || CONVERT_EXPR_P (t)
9922 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9923 || !cp_tree_equal (r, t));
9924 #endif
9925 return r;
9928 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9929 than wrapped in a TARGET_EXPR. */
9931 tree
9932 maybe_constant_init (tree t)
9934 t = maybe_constant_value (t);
9935 if (TREE_CODE (t) == TARGET_EXPR)
9937 tree init = TARGET_EXPR_INITIAL (t);
9938 if (TREE_CODE (init) == CONSTRUCTOR)
9939 t = init;
9941 return t;
9944 #if 0
9945 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9946 /* Return true if the object referred to by REF has automatic or thread
9947 local storage. */
9949 enum { ck_ok, ck_bad, ck_unknown };
9950 static int
9951 check_automatic_or_tls (tree ref)
9953 enum machine_mode mode;
9954 HOST_WIDE_INT bitsize, bitpos;
9955 tree offset;
9956 int volatilep = 0, unsignedp = 0;
9957 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9958 &mode, &unsignedp, &volatilep, false);
9959 duration_kind dk;
9961 /* If there isn't a decl in the middle, we don't know the linkage here,
9962 and this isn't a constant expression anyway. */
9963 if (!DECL_P (decl))
9964 return ck_unknown;
9965 dk = decl_storage_duration (decl);
9966 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9968 #endif
9970 /* Return true if T denotes a potentially constant expression. Issue
9971 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9972 an lvalue-rvalue conversion is implied.
9974 C++0x [expr.const] used to say
9976 6 An expression is a potential constant expression if it is
9977 a constant expression where all occurrences of function
9978 parameters are replaced by arbitrary constant expressions
9979 of the appropriate type.
9981 2 A conditional expression is a constant expression unless it
9982 involves one of the following as a potentially evaluated
9983 subexpression (3.2), but subexpressions of logical AND (5.14),
9984 logical OR (5.15), and conditional (5.16) operations that are
9985 not evaluated are not considered. */
9987 static bool
9988 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
9990 enum { any = false, rval = true };
9991 int i;
9992 tree tmp;
9994 if (t == error_mark_node)
9995 return false;
9996 if (t == NULL_TREE)
9997 return true;
9998 if (TREE_THIS_VOLATILE (t))
10000 if (flags & tf_error)
10001 error ("expression %qE has side-effects", t);
10002 return false;
10004 if (CONSTANT_CLASS_P (t))
10005 return true;
10007 switch (TREE_CODE (t))
10009 case FUNCTION_DECL:
10010 case BASELINK:
10011 case TEMPLATE_DECL:
10012 case OVERLOAD:
10013 case TEMPLATE_ID_EXPR:
10014 case LABEL_DECL:
10015 case LABEL_EXPR:
10016 case CONST_DECL:
10017 case SIZEOF_EXPR:
10018 case ALIGNOF_EXPR:
10019 case OFFSETOF_EXPR:
10020 case NOEXCEPT_EXPR:
10021 case TEMPLATE_PARM_INDEX:
10022 case TRAIT_EXPR:
10023 case IDENTIFIER_NODE:
10024 case USERDEF_LITERAL:
10025 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10026 case FIELD_DECL:
10027 case PARM_DECL:
10028 case USING_DECL:
10029 return true;
10031 case AGGR_INIT_EXPR:
10032 case CALL_EXPR:
10033 /* -- an invocation of a function other than a constexpr function
10034 or a constexpr constructor. */
10036 tree fun = get_function_named_in_call (t);
10037 const int nargs = call_expr_nargs (t);
10038 i = 0;
10040 if (is_overloaded_fn (fun))
10042 if (TREE_CODE (fun) == FUNCTION_DECL)
10044 if (builtin_valid_in_constant_expr_p (fun))
10045 return true;
10046 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10047 /* Allow any built-in function; if the expansion
10048 isn't constant, we'll deal with that then. */
10049 && !is_builtin_fn (fun))
10051 if (flags & tf_error)
10053 error_at (EXPR_LOC_OR_LOC (t, input_location),
10054 "call to non-constexpr function %qD", fun);
10055 explain_invalid_constexpr_fn (fun);
10057 return false;
10059 /* A call to a non-static member function takes the address
10060 of the object as the first argument. But in a constant
10061 expression the address will be folded away, so look
10062 through it now. */
10063 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10064 && !DECL_CONSTRUCTOR_P (fun))
10066 tree x = get_nth_callarg (t, 0);
10067 if (is_this_parameter (x))
10069 if (DECL_CONTEXT (x) == NULL_TREE
10070 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10072 if (flags & tf_error)
10073 sorry ("calling a member function of the "
10074 "object being constructed in a constant "
10075 "expression");
10076 return false;
10078 /* Otherwise OK. */;
10080 else if (!potential_constant_expression_1 (x, rval, flags))
10081 return false;
10082 i = 1;
10085 else
10087 if (!potential_constant_expression_1 (fun, true, flags))
10088 return false;
10089 fun = get_first_fn (fun);
10091 /* Skip initial arguments to base constructors. */
10092 if (DECL_BASE_CONSTRUCTOR_P (fun))
10093 i = num_artificial_parms_for (fun);
10094 fun = DECL_ORIGIN (fun);
10096 else
10098 if (potential_constant_expression_1 (fun, rval, flags))
10099 /* Might end up being a constant function pointer. */;
10100 else
10101 return false;
10103 for (; i < nargs; ++i)
10105 tree x = get_nth_callarg (t, i);
10106 if (!potential_constant_expression_1 (x, rval, flags))
10107 return false;
10109 return true;
10112 case NON_LVALUE_EXPR:
10113 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10114 -- an lvalue of integral type that refers to a non-volatile
10115 const variable or static data member initialized with
10116 constant expressions, or
10118 -- an lvalue of literal type that refers to non-volatile
10119 object defined with constexpr, or that refers to a
10120 sub-object of such an object; */
10121 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10123 case VAR_DECL:
10124 if (want_rval && !decl_constant_var_p (t)
10125 && !dependent_type_p (TREE_TYPE (t)))
10127 if (flags & tf_error)
10128 non_const_var_error (t);
10129 return false;
10131 return true;
10133 case NOP_EXPR:
10134 case CONVERT_EXPR:
10135 case VIEW_CONVERT_EXPR:
10136 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10137 may change to something more specific to type-punning (DR 1312). */
10139 tree from = TREE_OPERAND (t, 0);
10140 if (POINTER_TYPE_P (TREE_TYPE (t))
10141 && TREE_CODE (from) == INTEGER_CST
10142 && !integer_zerop (from))
10144 if (flags & tf_error)
10145 error_at (EXPR_LOC_OR_LOC (t, input_location),
10146 "reinterpret_cast from integer to pointer");
10147 return false;
10149 return (potential_constant_expression_1
10150 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10153 case ADDR_EXPR:
10154 /* -- a unary operator & that is applied to an lvalue that
10155 designates an object with thread or automatic storage
10156 duration; */
10157 t = TREE_OPERAND (t, 0);
10158 #if 0
10159 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10160 any checking here, as we might dereference the pointer later. If
10161 we remove this code, also remove check_automatic_or_tls. */
10162 i = check_automatic_or_tls (t);
10163 if (i == ck_ok)
10164 return true;
10165 if (i == ck_bad)
10167 if (flags & tf_error)
10168 error ("address-of an object %qE with thread local or "
10169 "automatic storage is not a constant expression", t);
10170 return false;
10172 #endif
10173 return potential_constant_expression_1 (t, any, flags);
10175 case COMPONENT_REF:
10176 case BIT_FIELD_REF:
10177 case ARROW_EXPR:
10178 case OFFSET_REF:
10179 /* -- a class member access unless its postfix-expression is
10180 of literal type or of pointer to literal type. */
10181 /* This test would be redundant, as it follows from the
10182 postfix-expression being a potential constant expression. */
10183 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10184 want_rval, flags);
10186 case EXPR_PACK_EXPANSION:
10187 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10188 want_rval, flags);
10190 case INDIRECT_REF:
10192 tree x = TREE_OPERAND (t, 0);
10193 STRIP_NOPS (x);
10194 if (is_this_parameter (x))
10196 if (DECL_CONTEXT (x)
10197 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10199 if (flags & tf_error)
10200 error ("use of %<this%> in a constant expression");
10201 return false;
10203 if (want_rval && DECL_CONTEXT (x)
10204 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10206 if (flags & tf_error)
10207 sorry ("use of the value of the object being constructed "
10208 "in a constant expression");
10209 return false;
10211 return true;
10213 return potential_constant_expression_1 (x, rval, flags);
10216 case LAMBDA_EXPR:
10217 case DYNAMIC_CAST_EXPR:
10218 case PSEUDO_DTOR_EXPR:
10219 case PREINCREMENT_EXPR:
10220 case POSTINCREMENT_EXPR:
10221 case PREDECREMENT_EXPR:
10222 case POSTDECREMENT_EXPR:
10223 case NEW_EXPR:
10224 case VEC_NEW_EXPR:
10225 case DELETE_EXPR:
10226 case VEC_DELETE_EXPR:
10227 case THROW_EXPR:
10228 case MODIFY_EXPR:
10229 case MODOP_EXPR:
10230 case OMP_ATOMIC:
10231 case OMP_ATOMIC_READ:
10232 case OMP_ATOMIC_CAPTURE_OLD:
10233 case OMP_ATOMIC_CAPTURE_NEW:
10234 /* GCC internal stuff. */
10235 case VA_ARG_EXPR:
10236 case OBJ_TYPE_REF:
10237 case WITH_CLEANUP_EXPR:
10238 case CLEANUP_POINT_EXPR:
10239 case MUST_NOT_THROW_EXPR:
10240 case TRY_CATCH_EXPR:
10241 case STATEMENT_LIST:
10242 /* Don't bother trying to define a subset of statement-expressions to
10243 be constant-expressions, at least for now. */
10244 case STMT_EXPR:
10245 case EXPR_STMT:
10246 case BIND_EXPR:
10247 case TRANSACTION_EXPR:
10248 case IF_STMT:
10249 case DO_STMT:
10250 case FOR_STMT:
10251 case WHILE_STMT:
10252 if (flags & tf_error)
10253 error ("expression %qE is not a constant-expression", t);
10254 return false;
10256 case TYPEID_EXPR:
10257 /* -- a typeid expression whose operand is of polymorphic
10258 class type; */
10260 tree e = TREE_OPERAND (t, 0);
10261 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10262 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10264 if (flags & tf_error)
10265 error ("typeid-expression is not a constant expression "
10266 "because %qE is of polymorphic type", e);
10267 return false;
10269 return true;
10272 case MINUS_EXPR:
10273 /* -- a subtraction where both operands are pointers. */
10274 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10275 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10277 if (flags & tf_error)
10278 error ("difference of two pointer expressions is not "
10279 "a constant expression");
10280 return false;
10282 want_rval = true;
10283 goto binary;
10285 case LT_EXPR:
10286 case LE_EXPR:
10287 case GT_EXPR:
10288 case GE_EXPR:
10289 case EQ_EXPR:
10290 case NE_EXPR:
10291 /* -- a relational or equality operator where at least
10292 one of the operands is a pointer. */
10293 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10294 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10296 if (flags & tf_error)
10297 error ("pointer comparison expression is not a "
10298 "constant expression");
10299 return false;
10301 want_rval = true;
10302 goto binary;
10304 case BIT_NOT_EXPR:
10305 /* A destructor. */
10306 if (TYPE_P (TREE_OPERAND (t, 0)))
10307 return true;
10308 /* else fall through. */
10310 case REALPART_EXPR:
10311 case IMAGPART_EXPR:
10312 case CONJ_EXPR:
10313 case SAVE_EXPR:
10314 case FIX_TRUNC_EXPR:
10315 case FLOAT_EXPR:
10316 case NEGATE_EXPR:
10317 case ABS_EXPR:
10318 case TRUTH_NOT_EXPR:
10319 case FIXED_CONVERT_EXPR:
10320 case UNARY_PLUS_EXPR:
10321 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10322 flags);
10324 case CAST_EXPR:
10325 case CONST_CAST_EXPR:
10326 case STATIC_CAST_EXPR:
10327 case REINTERPRET_CAST_EXPR:
10328 case IMPLICIT_CONV_EXPR:
10329 if (cxx_dialect < cxx11
10330 && !dependent_type_p (TREE_TYPE (t))
10331 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10332 /* In C++98, a conversion to non-integral type can't be part of a
10333 constant expression. */
10335 if (flags & tf_error)
10336 error ("cast to non-integral type %qT in a constant expression",
10337 TREE_TYPE (t));
10338 return false;
10341 return (potential_constant_expression_1
10342 (TREE_OPERAND (t, 0),
10343 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10345 case PAREN_EXPR:
10346 case NON_DEPENDENT_EXPR:
10347 /* For convenience. */
10348 case RETURN_EXPR:
10349 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10350 want_rval, flags);
10352 case SCOPE_REF:
10353 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10354 want_rval, flags);
10356 case TARGET_EXPR:
10357 if (!literal_type_p (TREE_TYPE (t)))
10359 if (flags & tf_error)
10361 error ("temporary of non-literal type %qT in a "
10362 "constant expression", TREE_TYPE (t));
10363 explain_non_literal_class (TREE_TYPE (t));
10365 return false;
10367 case INIT_EXPR:
10368 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10369 rval, flags);
10371 case CONSTRUCTOR:
10373 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10374 constructor_elt *ce;
10375 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10376 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10377 return false;
10378 return true;
10381 case TREE_LIST:
10383 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10384 || DECL_P (TREE_PURPOSE (t)));
10385 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10386 flags))
10387 return false;
10388 if (TREE_CHAIN (t) == NULL_TREE)
10389 return true;
10390 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10391 flags);
10394 case TRUNC_DIV_EXPR:
10395 case CEIL_DIV_EXPR:
10396 case FLOOR_DIV_EXPR:
10397 case ROUND_DIV_EXPR:
10398 case TRUNC_MOD_EXPR:
10399 case CEIL_MOD_EXPR:
10400 case ROUND_MOD_EXPR:
10402 tree denom = TREE_OPERAND (t, 1);
10403 if (!potential_constant_expression_1 (denom, rval, flags))
10404 return false;
10405 /* We can't call cxx_eval_outermost_constant_expr on an expression
10406 that hasn't been through fold_non_dependent_expr yet. */
10407 if (!processing_template_decl)
10408 denom = cxx_eval_outermost_constant_expr (denom, true);
10409 if (integer_zerop (denom))
10411 if (flags & tf_error)
10412 error ("division by zero is not a constant-expression");
10413 return false;
10415 else
10417 want_rval = true;
10418 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10419 want_rval, flags);
10423 case COMPOUND_EXPR:
10425 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10426 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10427 introduced by build_call_a. */
10428 tree op0 = TREE_OPERAND (t, 0);
10429 tree op1 = TREE_OPERAND (t, 1);
10430 STRIP_NOPS (op1);
10431 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10432 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10433 return potential_constant_expression_1 (op0, want_rval, flags);
10434 else
10435 goto binary;
10438 /* If the first operand is the non-short-circuit constant, look at
10439 the second operand; otherwise we only care about the first one for
10440 potentiality. */
10441 case TRUTH_AND_EXPR:
10442 case TRUTH_ANDIF_EXPR:
10443 tmp = boolean_true_node;
10444 goto truth;
10445 case TRUTH_OR_EXPR:
10446 case TRUTH_ORIF_EXPR:
10447 tmp = boolean_false_node;
10448 truth:
10450 tree op = TREE_OPERAND (t, 0);
10451 if (!potential_constant_expression_1 (op, rval, flags))
10452 return false;
10453 if (!processing_template_decl)
10454 op = cxx_eval_outermost_constant_expr (op, true);
10455 if (tree_int_cst_equal (op, tmp))
10456 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10457 else
10458 return true;
10461 case PLUS_EXPR:
10462 case MULT_EXPR:
10463 case POINTER_PLUS_EXPR:
10464 case RDIV_EXPR:
10465 case EXACT_DIV_EXPR:
10466 case MIN_EXPR:
10467 case MAX_EXPR:
10468 case LSHIFT_EXPR:
10469 case RSHIFT_EXPR:
10470 case LROTATE_EXPR:
10471 case RROTATE_EXPR:
10472 case BIT_IOR_EXPR:
10473 case BIT_XOR_EXPR:
10474 case BIT_AND_EXPR:
10475 case TRUTH_XOR_EXPR:
10476 case UNORDERED_EXPR:
10477 case ORDERED_EXPR:
10478 case UNLT_EXPR:
10479 case UNLE_EXPR:
10480 case UNGT_EXPR:
10481 case UNGE_EXPR:
10482 case UNEQ_EXPR:
10483 case LTGT_EXPR:
10484 case RANGE_EXPR:
10485 case COMPLEX_EXPR:
10486 want_rval = true;
10487 /* Fall through. */
10488 case ARRAY_REF:
10489 case ARRAY_RANGE_REF:
10490 case MEMBER_REF:
10491 case DOTSTAR_EXPR:
10492 binary:
10493 for (i = 0; i < 2; ++i)
10494 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10495 want_rval, flags))
10496 return false;
10497 return true;
10499 case CILK_SYNC_STMT:
10500 case CILK_SPAWN_STMT:
10501 case ARRAY_NOTATION_REF:
10502 return false;
10504 case FMA_EXPR:
10505 case VEC_PERM_EXPR:
10506 for (i = 0; i < 3; ++i)
10507 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10508 true, flags))
10509 return false;
10510 return true;
10512 case COND_EXPR:
10513 case VEC_COND_EXPR:
10514 /* If the condition is a known constant, we know which of the legs we
10515 care about; otherwise we only require that the condition and
10516 either of the legs be potentially constant. */
10517 tmp = TREE_OPERAND (t, 0);
10518 if (!potential_constant_expression_1 (tmp, rval, flags))
10519 return false;
10520 if (!processing_template_decl)
10521 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10522 if (integer_zerop (tmp))
10523 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10524 want_rval, flags);
10525 else if (TREE_CODE (tmp) == INTEGER_CST)
10526 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10527 want_rval, flags);
10528 for (i = 1; i < 3; ++i)
10529 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10530 want_rval, tf_none))
10531 return true;
10532 if (flags & tf_error)
10533 error ("expression %qE is not a constant-expression", t);
10534 return false;
10536 case VEC_INIT_EXPR:
10537 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10538 return true;
10539 if (flags & tf_error)
10541 error ("non-constant array initialization");
10542 diagnose_non_constexpr_vec_init (t);
10544 return false;
10546 default:
10547 if (objc_is_property_ref (t))
10548 return false;
10550 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10551 gcc_unreachable();
10552 return false;
10556 /* The main entry point to the above. */
10558 bool
10559 potential_constant_expression (tree t)
10561 return potential_constant_expression_1 (t, false, tf_none);
10564 /* As above, but require a constant rvalue. */
10566 bool
10567 potential_rvalue_constant_expression (tree t)
10569 return potential_constant_expression_1 (t, true, tf_none);
10572 /* Like above, but complain about non-constant expressions. */
10574 bool
10575 require_potential_constant_expression (tree t)
10577 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10580 /* Cross product of the above. */
10582 bool
10583 require_potential_rvalue_constant_expression (tree t)
10585 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10588 /* Insert the deduced return type for an auto function. */
10590 void
10591 apply_deduced_return_type (tree fco, tree return_type)
10593 tree result;
10595 if (return_type == error_mark_node)
10596 return;
10598 if (LAMBDA_FUNCTION_P (fco))
10600 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10601 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10604 if (DECL_CONV_FN_P (fco))
10605 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10607 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10609 result = DECL_RESULT (fco);
10610 if (result == NULL_TREE)
10611 return;
10612 if (TREE_TYPE (result) == return_type)
10613 return;
10615 /* We already have a DECL_RESULT from start_preparsed_function.
10616 Now we need to redo the work it and allocate_struct_function
10617 did to reflect the new type. */
10618 gcc_assert (current_function_decl == fco);
10619 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10620 TYPE_MAIN_VARIANT (return_type));
10621 DECL_ARTIFICIAL (result) = 1;
10622 DECL_IGNORED_P (result) = 1;
10623 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10624 result);
10626 DECL_RESULT (fco) = result;
10628 if (!processing_template_decl)
10630 bool aggr = aggregate_value_p (result, fco);
10631 #ifdef PCC_STATIC_STRUCT_RETURN
10632 cfun->returns_pcc_struct = aggr;
10633 #endif
10634 cfun->returns_struct = aggr;
10639 /* DECL is a local variable or parameter from the surrounding scope of a
10640 lambda-expression. Returns the decltype for a use of the capture field
10641 for DECL even if it hasn't been captured yet. */
10643 static tree
10644 capture_decltype (tree decl)
10646 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10647 /* FIXME do lookup instead of list walk? */
10648 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10649 tree type;
10651 if (cap)
10652 type = TREE_TYPE (TREE_PURPOSE (cap));
10653 else
10654 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10656 case CPLD_NONE:
10657 error ("%qD is not captured", decl);
10658 return error_mark_node;
10660 case CPLD_COPY:
10661 type = TREE_TYPE (decl);
10662 if (TREE_CODE (type) == REFERENCE_TYPE
10663 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10664 type = TREE_TYPE (type);
10665 break;
10667 case CPLD_REFERENCE:
10668 type = TREE_TYPE (decl);
10669 if (TREE_CODE (type) != REFERENCE_TYPE)
10670 type = build_reference_type (TREE_TYPE (decl));
10671 break;
10673 default:
10674 gcc_unreachable ();
10677 if (TREE_CODE (type) != REFERENCE_TYPE)
10679 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10680 type = cp_build_qualified_type (type, (cp_type_quals (type)
10681 |TYPE_QUAL_CONST));
10682 type = build_reference_type (type);
10684 return type;
10687 #include "gt-cp-semantics.h"