remove pointer-set.[ch]
[official-gcc.git] / gcc / cp / semantics.c
blob14bf82d6f0a072413f332059df13b03d06839c23
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 "hash-table.h"
48 #include "gimplify.h"
49 #include "bitmap.h"
50 #include "omp-low.h"
51 #include "builtins.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 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
390 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
392 /* Add T to the statement-tree. Non-side-effect statements need to be
393 recorded during statement expressions. */
394 gcc_checking_assert (!stmt_list_stack->is_empty ());
395 append_to_statement_list_force (t, &cur_stmt_list);
397 return t;
400 /* Returns the stmt_tree to which statements are currently being added. */
402 stmt_tree
403 current_stmt_tree (void)
405 return (cfun
406 ? &cfun->language->base.x_stmt_tree
407 : &scope_chain->x_stmt_tree);
410 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
412 static tree
413 maybe_cleanup_point_expr (tree expr)
415 if (!processing_template_decl && stmts_are_full_exprs_p ())
416 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
417 return expr;
420 /* Like maybe_cleanup_point_expr except have the type of the new expression be
421 void so we don't need to create a temporary variable to hold the inner
422 expression. The reason why we do this is because the original type might be
423 an aggregate and we cannot create a temporary variable for that type. */
425 tree
426 maybe_cleanup_point_expr_void (tree expr)
428 if (!processing_template_decl && stmts_are_full_exprs_p ())
429 expr = fold_build_cleanup_point_expr (void_type_node, expr);
430 return expr;
435 /* Create a declaration statement for the declaration given by the DECL. */
437 void
438 add_decl_expr (tree decl)
440 tree r = build_stmt (input_location, DECL_EXPR, decl);
441 if (DECL_INITIAL (decl)
442 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
443 r = maybe_cleanup_point_expr_void (r);
444 add_stmt (r);
447 /* Finish a scope. */
449 tree
450 do_poplevel (tree stmt_list)
452 tree block = NULL;
454 if (stmts_are_full_exprs_p ())
455 block = poplevel (kept_level_p (), 1, 0);
457 stmt_list = pop_stmt_list (stmt_list);
459 if (!processing_template_decl)
461 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
462 /* ??? See c_end_compound_stmt re statement expressions. */
465 return stmt_list;
468 /* Begin a new scope. */
470 static tree
471 do_pushlevel (scope_kind sk)
473 tree ret = push_stmt_list ();
474 if (stmts_are_full_exprs_p ())
475 begin_scope (sk, NULL);
476 return ret;
479 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
480 when the current scope is exited. EH_ONLY is true when this is not
481 meant to apply to normal control flow transfer. */
483 void
484 push_cleanup (tree decl, tree cleanup, bool eh_only)
486 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
487 CLEANUP_EH_ONLY (stmt) = eh_only;
488 add_stmt (stmt);
489 CLEANUP_BODY (stmt) = push_stmt_list ();
492 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
493 the current loops, represented by 'NULL_TREE' if we've seen a possible
494 exit, and 'error_mark_node' if not. This is currently used only to
495 suppress the warning about a function with no return statements, and
496 therefore we don't bother noting returns as possible exits. We also
497 don't bother with gotos. */
499 static void
500 begin_maybe_infinite_loop (tree cond)
502 /* Only track this while parsing a function, not during instantiation. */
503 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
504 && !processing_template_decl))
505 return;
506 bool maybe_infinite = true;
507 if (cond)
509 cond = fold_non_dependent_expr_sfinae (cond, tf_none);
510 cond = maybe_constant_value (cond);
511 maybe_infinite = integer_nonzerop (cond);
513 vec_safe_push (cp_function_chain->infinite_loops,
514 maybe_infinite ? error_mark_node : NULL_TREE);
518 /* A break is a possible exit for the current loop. */
520 void
521 break_maybe_infinite_loop (void)
523 if (!cfun)
524 return;
525 cp_function_chain->infinite_loops->last() = NULL_TREE;
528 /* If we reach the end of the loop without seeing a possible exit, we have
529 an infinite loop. */
531 static void
532 end_maybe_infinite_loop (tree cond)
534 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
535 && !processing_template_decl))
536 return;
537 tree current = cp_function_chain->infinite_loops->pop();
538 if (current != NULL_TREE)
540 cond = fold_non_dependent_expr (cond);
541 cond = maybe_constant_value (cond);
542 if (integer_nonzerop (cond))
543 current_function_infinite_loop = 1;
548 /* Begin a conditional that might contain a declaration. When generating
549 normal code, we want the declaration to appear before the statement
550 containing the conditional. When generating template code, we want the
551 conditional to be rendered as the raw DECL_EXPR. */
553 static void
554 begin_cond (tree *cond_p)
556 if (processing_template_decl)
557 *cond_p = push_stmt_list ();
560 /* Finish such a conditional. */
562 static void
563 finish_cond (tree *cond_p, tree expr)
565 if (processing_template_decl)
567 tree cond = pop_stmt_list (*cond_p);
569 if (expr == NULL_TREE)
570 /* Empty condition in 'for'. */
571 gcc_assert (empty_expr_stmt_p (cond));
572 else if (check_for_bare_parameter_packs (expr))
573 expr = error_mark_node;
574 else if (!empty_expr_stmt_p (cond))
575 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
577 *cond_p = expr;
580 /* If *COND_P specifies a conditional with a declaration, transform the
581 loop such that
582 while (A x = 42) { }
583 for (; A x = 42;) { }
584 becomes
585 while (true) { A x = 42; if (!x) break; }
586 for (;;) { A x = 42; if (!x) break; }
587 The statement list for BODY will be empty if the conditional did
588 not declare anything. */
590 static void
591 simplify_loop_decl_cond (tree *cond_p, tree body)
593 tree cond, if_stmt;
595 if (!TREE_SIDE_EFFECTS (body))
596 return;
598 cond = *cond_p;
599 *cond_p = boolean_true_node;
601 if_stmt = begin_if_stmt ();
602 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
603 finish_if_stmt_cond (cond, if_stmt);
604 finish_break_stmt ();
605 finish_then_clause (if_stmt);
606 finish_if_stmt (if_stmt);
609 /* Finish a goto-statement. */
611 tree
612 finish_goto_stmt (tree destination)
614 if (identifier_p (destination))
615 destination = lookup_label (destination);
617 /* We warn about unused labels with -Wunused. That means we have to
618 mark the used labels as used. */
619 if (TREE_CODE (destination) == LABEL_DECL)
620 TREE_USED (destination) = 1;
621 else
623 destination = mark_rvalue_use (destination);
624 if (!processing_template_decl)
626 destination = cp_convert (ptr_type_node, destination,
627 tf_warning_or_error);
628 if (error_operand_p (destination))
629 return NULL_TREE;
630 destination
631 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
632 destination);
636 check_goto (destination);
638 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
641 /* COND is the condition-expression for an if, while, etc.,
642 statement. Convert it to a boolean value, if appropriate.
643 In addition, verify sequence points if -Wsequence-point is enabled. */
645 static tree
646 maybe_convert_cond (tree cond)
648 /* Empty conditions remain empty. */
649 if (!cond)
650 return NULL_TREE;
652 /* Wait until we instantiate templates before doing conversion. */
653 if (processing_template_decl)
654 return cond;
656 if (warn_sequence_point)
657 verify_sequence_points (cond);
659 /* Do the conversion. */
660 cond = convert_from_reference (cond);
662 if (TREE_CODE (cond) == MODIFY_EXPR
663 && !TREE_NO_WARNING (cond)
664 && warn_parentheses)
666 warning (OPT_Wparentheses,
667 "suggest parentheses around assignment used as truth value");
668 TREE_NO_WARNING (cond) = 1;
671 return condition_conversion (cond);
674 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
676 tree
677 finish_expr_stmt (tree expr)
679 tree r = NULL_TREE;
681 if (expr != NULL_TREE)
683 if (!processing_template_decl)
685 if (warn_sequence_point)
686 verify_sequence_points (expr);
687 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
689 else if (!type_dependent_expression_p (expr))
690 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
691 tf_warning_or_error);
693 if (check_for_bare_parameter_packs (expr))
694 expr = error_mark_node;
696 /* Simplification of inner statement expressions, compound exprs,
697 etc can result in us already having an EXPR_STMT. */
698 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
700 if (TREE_CODE (expr) != EXPR_STMT)
701 expr = build_stmt (input_location, EXPR_STMT, expr);
702 expr = maybe_cleanup_point_expr_void (expr);
705 r = add_stmt (expr);
708 return r;
712 /* Begin an if-statement. Returns a newly created IF_STMT if
713 appropriate. */
715 tree
716 begin_if_stmt (void)
718 tree r, scope;
719 scope = do_pushlevel (sk_cond);
720 r = build_stmt (input_location, IF_STMT, NULL_TREE,
721 NULL_TREE, NULL_TREE, scope);
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 void
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
733 add_stmt (if_stmt);
734 THEN_CLAUSE (if_stmt) = push_stmt_list ();
737 /* Finish the then-clause of an if-statement, which may be given by
738 IF_STMT. */
740 tree
741 finish_then_clause (tree if_stmt)
743 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
744 return if_stmt;
747 /* Begin the else-clause of an if-statement. */
749 void
750 begin_else_clause (tree if_stmt)
752 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
755 /* Finish the else-clause of an if-statement, which may be given by
756 IF_STMT. */
758 void
759 finish_else_clause (tree if_stmt)
761 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
764 /* Finish an if-statement. */
766 void
767 finish_if_stmt (tree if_stmt)
769 tree scope = IF_SCOPE (if_stmt);
770 IF_SCOPE (if_stmt) = NULL;
771 add_stmt (do_poplevel (scope));
774 /* Begin a while-statement. Returns a newly created WHILE_STMT if
775 appropriate. */
777 tree
778 begin_while_stmt (void)
780 tree r;
781 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
782 add_stmt (r);
783 WHILE_BODY (r) = do_pushlevel (sk_block);
784 begin_cond (&WHILE_COND (r));
785 return r;
788 /* Process the COND of a while-statement, which may be given by
789 WHILE_STMT. */
791 void
792 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
794 cond = maybe_convert_cond (cond);
795 finish_cond (&WHILE_COND (while_stmt), cond);
796 begin_maybe_infinite_loop (cond);
797 if (ivdep && cond != error_mark_node)
798 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
799 TREE_TYPE (WHILE_COND (while_stmt)),
800 WHILE_COND (while_stmt),
801 build_int_cst (integer_type_node,
802 annot_expr_ivdep_kind));
803 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
806 /* Finish a while-statement, which may be given by WHILE_STMT. */
808 void
809 finish_while_stmt (tree while_stmt)
811 end_maybe_infinite_loop (boolean_true_node);
812 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
815 /* Begin a do-statement. Returns a newly created DO_STMT if
816 appropriate. */
818 tree
819 begin_do_stmt (void)
821 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
822 begin_maybe_infinite_loop (boolean_true_node);
823 add_stmt (r);
824 DO_BODY (r) = push_stmt_list ();
825 return r;
828 /* Finish the body of a do-statement, which may be given by DO_STMT. */
830 void
831 finish_do_body (tree do_stmt)
833 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
835 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
836 body = STATEMENT_LIST_TAIL (body)->stmt;
838 if (IS_EMPTY_STMT (body))
839 warning (OPT_Wempty_body,
840 "suggest explicit braces around empty body in %<do%> statement");
843 /* Finish a do-statement, which may be given by DO_STMT, and whose
844 COND is as indicated. */
846 void
847 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
849 cond = maybe_convert_cond (cond);
850 end_maybe_infinite_loop (cond);
851 if (ivdep && cond != error_mark_node)
852 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
853 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
854 DO_COND (do_stmt) = cond;
857 /* Finish a return-statement. The EXPRESSION returned, if any, is as
858 indicated. */
860 tree
861 finish_return_stmt (tree expr)
863 tree r;
864 bool no_warning;
866 expr = check_return_expr (expr, &no_warning);
868 if (error_operand_p (expr)
869 || (flag_openmp && !check_omp_return ()))
870 return error_mark_node;
871 if (!processing_template_decl)
873 if (warn_sequence_point)
874 verify_sequence_points (expr);
876 if (DECL_DESTRUCTOR_P (current_function_decl)
877 || (DECL_CONSTRUCTOR_P (current_function_decl)
878 && targetm.cxx.cdtor_returns_this ()))
880 /* Similarly, all destructors must run destructors for
881 base-classes before returning. So, all returns in a
882 destructor get sent to the DTOR_LABEL; finish_function emits
883 code to return a value there. */
884 return finish_goto_stmt (cdtor_label);
888 r = build_stmt (input_location, RETURN_EXPR, expr);
889 TREE_NO_WARNING (r) |= no_warning;
890 r = maybe_cleanup_point_expr_void (r);
891 r = add_stmt (r);
893 return r;
896 /* Begin the scope of a for-statement or a range-for-statement.
897 Both the returned trees are to be used in a call to
898 begin_for_stmt or begin_range_for_stmt. */
900 tree
901 begin_for_scope (tree *init)
903 tree scope = NULL_TREE;
904 if (flag_new_for_scope > 0)
905 scope = do_pushlevel (sk_for);
907 if (processing_template_decl)
908 *init = push_stmt_list ();
909 else
910 *init = NULL_TREE;
912 return scope;
915 /* Begin a for-statement. Returns a new FOR_STMT.
916 SCOPE and INIT should be the return of begin_for_scope,
917 or both NULL_TREE */
919 tree
920 begin_for_stmt (tree scope, tree init)
922 tree r;
924 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
925 NULL_TREE, NULL_TREE, NULL_TREE);
927 if (scope == NULL_TREE)
929 gcc_assert (!init || !(flag_new_for_scope > 0));
930 if (!init)
931 scope = begin_for_scope (&init);
933 FOR_INIT_STMT (r) = init;
934 FOR_SCOPE (r) = scope;
936 return r;
939 /* Finish the for-init-statement of a for-statement, which may be
940 given by FOR_STMT. */
942 void
943 finish_for_init_stmt (tree for_stmt)
945 if (processing_template_decl)
946 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
947 add_stmt (for_stmt);
948 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
949 begin_cond (&FOR_COND (for_stmt));
952 /* Finish the COND of a for-statement, which may be given by
953 FOR_STMT. */
955 void
956 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
958 cond = maybe_convert_cond (cond);
959 finish_cond (&FOR_COND (for_stmt), cond);
960 begin_maybe_infinite_loop (cond);
961 if (ivdep && cond != error_mark_node)
962 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
963 TREE_TYPE (FOR_COND (for_stmt)),
964 FOR_COND (for_stmt),
965 build_int_cst (integer_type_node,
966 annot_expr_ivdep_kind));
967 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
970 /* Finish the increment-EXPRESSION in a for-statement, which may be
971 given by FOR_STMT. */
973 void
974 finish_for_expr (tree expr, tree for_stmt)
976 if (!expr)
977 return;
978 /* If EXPR is an overloaded function, issue an error; there is no
979 context available to use to perform overload resolution. */
980 if (type_unknown_p (expr))
982 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
983 expr = error_mark_node;
985 if (!processing_template_decl)
987 if (warn_sequence_point)
988 verify_sequence_points (expr);
989 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
990 tf_warning_or_error);
992 else if (!type_dependent_expression_p (expr))
993 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
994 tf_warning_or_error);
995 expr = maybe_cleanup_point_expr_void (expr);
996 if (check_for_bare_parameter_packs (expr))
997 expr = error_mark_node;
998 FOR_EXPR (for_stmt) = expr;
1001 /* Finish the body of a for-statement, which may be given by
1002 FOR_STMT. The increment-EXPR for the loop must be
1003 provided.
1004 It can also finish RANGE_FOR_STMT. */
1006 void
1007 finish_for_stmt (tree for_stmt)
1009 end_maybe_infinite_loop (boolean_true_node);
1011 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1012 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1013 else
1014 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1016 /* Pop the scope for the body of the loop. */
1017 if (flag_new_for_scope > 0)
1019 tree scope;
1020 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1021 ? &RANGE_FOR_SCOPE (for_stmt)
1022 : &FOR_SCOPE (for_stmt));
1023 scope = *scope_ptr;
1024 *scope_ptr = NULL;
1025 add_stmt (do_poplevel (scope));
1029 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1030 SCOPE and INIT should be the return of begin_for_scope,
1031 or both NULL_TREE .
1032 To finish it call finish_for_stmt(). */
1034 tree
1035 begin_range_for_stmt (tree scope, tree init)
1037 tree r;
1039 begin_maybe_infinite_loop (boolean_false_node);
1041 r = build_stmt (input_location, RANGE_FOR_STMT,
1042 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1044 if (scope == NULL_TREE)
1046 gcc_assert (!init || !(flag_new_for_scope > 0));
1047 if (!init)
1048 scope = begin_for_scope (&init);
1051 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1052 pop it now. */
1053 if (init)
1054 pop_stmt_list (init);
1055 RANGE_FOR_SCOPE (r) = scope;
1057 return r;
1060 /* Finish the head of a range-based for statement, which may
1061 be given by RANGE_FOR_STMT. DECL must be the declaration
1062 and EXPR must be the loop expression. */
1064 void
1065 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1067 RANGE_FOR_DECL (range_for_stmt) = decl;
1068 RANGE_FOR_EXPR (range_for_stmt) = expr;
1069 add_stmt (range_for_stmt);
1070 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1073 /* Finish a break-statement. */
1075 tree
1076 finish_break_stmt (void)
1078 /* In switch statements break is sometimes stylistically used after
1079 a return statement. This can lead to spurious warnings about
1080 control reaching the end of a non-void function when it is
1081 inlined. Note that we are calling block_may_fallthru with
1082 language specific tree nodes; this works because
1083 block_may_fallthru returns true when given something it does not
1084 understand. */
1085 if (!block_may_fallthru (cur_stmt_list))
1086 return void_node;
1087 return add_stmt (build_stmt (input_location, BREAK_STMT));
1090 /* Finish a continue-statement. */
1092 tree
1093 finish_continue_stmt (void)
1095 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1098 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1099 appropriate. */
1101 tree
1102 begin_switch_stmt (void)
1104 tree r, scope;
1106 scope = do_pushlevel (sk_cond);
1107 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1109 begin_cond (&SWITCH_STMT_COND (r));
1111 return r;
1114 /* Finish the cond of a switch-statement. */
1116 void
1117 finish_switch_cond (tree cond, tree switch_stmt)
1119 tree orig_type = NULL;
1120 if (!processing_template_decl)
1122 /* Convert the condition to an integer or enumeration type. */
1123 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1124 if (cond == NULL_TREE)
1126 error ("switch quantity not an integer");
1127 cond = error_mark_node;
1129 orig_type = TREE_TYPE (cond);
1130 if (cond != error_mark_node)
1132 /* Warn if the condition has boolean value. */
1133 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1134 warning_at (input_location, OPT_Wswitch_bool,
1135 "switch condition has type bool");
1137 /* [stmt.switch]
1139 Integral promotions are performed. */
1140 cond = perform_integral_promotions (cond);
1141 cond = maybe_cleanup_point_expr (cond);
1144 if (check_for_bare_parameter_packs (cond))
1145 cond = error_mark_node;
1146 else if (!processing_template_decl && warn_sequence_point)
1147 verify_sequence_points (cond);
1149 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1150 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1151 add_stmt (switch_stmt);
1152 push_switch (switch_stmt);
1153 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1156 /* Finish the body of a switch-statement, which may be given by
1157 SWITCH_STMT. The COND to switch on is indicated. */
1159 void
1160 finish_switch_stmt (tree switch_stmt)
1162 tree scope;
1164 SWITCH_STMT_BODY (switch_stmt) =
1165 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1166 pop_switch ();
1168 scope = SWITCH_STMT_SCOPE (switch_stmt);
1169 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1170 add_stmt (do_poplevel (scope));
1173 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1174 appropriate. */
1176 tree
1177 begin_try_block (void)
1179 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1180 add_stmt (r);
1181 TRY_STMTS (r) = push_stmt_list ();
1182 return r;
1185 /* Likewise, for a function-try-block. The block returned in
1186 *COMPOUND_STMT is an artificial outer scope, containing the
1187 function-try-block. */
1189 tree
1190 begin_function_try_block (tree *compound_stmt)
1192 tree r;
1193 /* This outer scope does not exist in the C++ standard, but we need
1194 a place to put __FUNCTION__ and similar variables. */
1195 *compound_stmt = begin_compound_stmt (0);
1196 r = begin_try_block ();
1197 FN_TRY_BLOCK_P (r) = 1;
1198 return r;
1201 /* Finish a try-block, which may be given by TRY_BLOCK. */
1203 void
1204 finish_try_block (tree try_block)
1206 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1207 TRY_HANDLERS (try_block) = push_stmt_list ();
1210 /* Finish the body of a cleanup try-block, which may be given by
1211 TRY_BLOCK. */
1213 void
1214 finish_cleanup_try_block (tree try_block)
1216 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1219 /* Finish an implicitly generated try-block, with a cleanup is given
1220 by CLEANUP. */
1222 void
1223 finish_cleanup (tree cleanup, tree try_block)
1225 TRY_HANDLERS (try_block) = cleanup;
1226 CLEANUP_P (try_block) = 1;
1229 /* Likewise, for a function-try-block. */
1231 void
1232 finish_function_try_block (tree try_block)
1234 finish_try_block (try_block);
1235 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1236 the try block, but moving it inside. */
1237 in_function_try_handler = 1;
1240 /* Finish a handler-sequence for a try-block, which may be given by
1241 TRY_BLOCK. */
1243 void
1244 finish_handler_sequence (tree try_block)
1246 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1247 check_handlers (TRY_HANDLERS (try_block));
1250 /* Finish the handler-seq for a function-try-block, given by
1251 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1252 begin_function_try_block. */
1254 void
1255 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1257 in_function_try_handler = 0;
1258 finish_handler_sequence (try_block);
1259 finish_compound_stmt (compound_stmt);
1262 /* Begin a handler. Returns a HANDLER if appropriate. */
1264 tree
1265 begin_handler (void)
1267 tree r;
1269 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1270 add_stmt (r);
1272 /* Create a binding level for the eh_info and the exception object
1273 cleanup. */
1274 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1276 return r;
1279 /* Finish the handler-parameters for a handler, which may be given by
1280 HANDLER. DECL is the declaration for the catch parameter, or NULL
1281 if this is a `catch (...)' clause. */
1283 void
1284 finish_handler_parms (tree decl, tree handler)
1286 tree type = NULL_TREE;
1287 if (processing_template_decl)
1289 if (decl)
1291 decl = pushdecl (decl);
1292 decl = push_template_decl (decl);
1293 HANDLER_PARMS (handler) = decl;
1294 type = TREE_TYPE (decl);
1297 else
1298 type = expand_start_catch_block (decl);
1299 HANDLER_TYPE (handler) = type;
1302 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1303 the return value from the matching call to finish_handler_parms. */
1305 void
1306 finish_handler (tree handler)
1308 if (!processing_template_decl)
1309 expand_end_catch_block ();
1310 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1313 /* Begin a compound statement. FLAGS contains some bits that control the
1314 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1315 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1316 block of a function. If BCS_TRY_BLOCK is set, this is the block
1317 created on behalf of a TRY statement. Returns a token to be passed to
1318 finish_compound_stmt. */
1320 tree
1321 begin_compound_stmt (unsigned int flags)
1323 tree r;
1325 if (flags & BCS_NO_SCOPE)
1327 r = push_stmt_list ();
1328 STATEMENT_LIST_NO_SCOPE (r) = 1;
1330 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1331 But, if it's a statement-expression with a scopeless block, there's
1332 nothing to keep, and we don't want to accidentally keep a block
1333 *inside* the scopeless block. */
1334 keep_next_level (false);
1336 else
1337 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1339 /* When processing a template, we need to remember where the braces were,
1340 so that we can set up identical scopes when instantiating the template
1341 later. BIND_EXPR is a handy candidate for this.
1342 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1343 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1344 processing templates. */
1345 if (processing_template_decl)
1347 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1348 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1349 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1350 TREE_SIDE_EFFECTS (r) = 1;
1353 return r;
1356 /* Finish a compound-statement, which is given by STMT. */
1358 void
1359 finish_compound_stmt (tree stmt)
1361 if (TREE_CODE (stmt) == BIND_EXPR)
1363 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1364 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1365 discard the BIND_EXPR so it can be merged with the containing
1366 STATEMENT_LIST. */
1367 if (TREE_CODE (body) == STATEMENT_LIST
1368 && STATEMENT_LIST_HEAD (body) == NULL
1369 && !BIND_EXPR_BODY_BLOCK (stmt)
1370 && !BIND_EXPR_TRY_BLOCK (stmt))
1371 stmt = body;
1372 else
1373 BIND_EXPR_BODY (stmt) = body;
1375 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1376 stmt = pop_stmt_list (stmt);
1377 else
1379 /* Destroy any ObjC "super" receivers that may have been
1380 created. */
1381 objc_clear_super_receiver ();
1383 stmt = do_poplevel (stmt);
1386 /* ??? See c_end_compound_stmt wrt statement expressions. */
1387 add_stmt (stmt);
1390 /* Finish an asm-statement, whose components are a STRING, some
1391 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1392 LABELS. Also note whether the asm-statement should be
1393 considered volatile. */
1395 tree
1396 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1397 tree input_operands, tree clobbers, tree labels)
1399 tree r;
1400 tree t;
1401 int ninputs = list_length (input_operands);
1402 int noutputs = list_length (output_operands);
1404 if (!processing_template_decl)
1406 const char *constraint;
1407 const char **oconstraints;
1408 bool allows_mem, allows_reg, is_inout;
1409 tree operand;
1410 int i;
1412 oconstraints = XALLOCAVEC (const char *, noutputs);
1414 string = resolve_asm_operand_names (string, output_operands,
1415 input_operands, labels);
1417 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1419 operand = TREE_VALUE (t);
1421 /* ??? Really, this should not be here. Users should be using a
1422 proper lvalue, dammit. But there's a long history of using
1423 casts in the output operands. In cases like longlong.h, this
1424 becomes a primitive form of typechecking -- if the cast can be
1425 removed, then the output operand had a type of the proper width;
1426 otherwise we'll get an error. Gross, but ... */
1427 STRIP_NOPS (operand);
1429 operand = mark_lvalue_use (operand);
1431 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1432 operand = error_mark_node;
1434 if (operand != error_mark_node
1435 && (TREE_READONLY (operand)
1436 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1437 /* Functions are not modifiable, even though they are
1438 lvalues. */
1439 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1440 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1441 /* If it's an aggregate and any field is const, then it is
1442 effectively const. */
1443 || (CLASS_TYPE_P (TREE_TYPE (operand))
1444 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1445 cxx_readonly_error (operand, lv_asm);
1447 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1448 oconstraints[i] = constraint;
1450 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1451 &allows_mem, &allows_reg, &is_inout))
1453 /* If the operand is going to end up in memory,
1454 mark it addressable. */
1455 if (!allows_reg && !cxx_mark_addressable (operand))
1456 operand = error_mark_node;
1458 else
1459 operand = error_mark_node;
1461 TREE_VALUE (t) = operand;
1464 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1466 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1467 bool constraint_parsed
1468 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1469 oconstraints, &allows_mem, &allows_reg);
1470 /* If the operand is going to end up in memory, don't call
1471 decay_conversion. */
1472 if (constraint_parsed && !allows_reg && allows_mem)
1473 operand = mark_lvalue_use (TREE_VALUE (t));
1474 else
1475 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1477 /* If the type of the operand hasn't been determined (e.g.,
1478 because it involves an overloaded function), then issue
1479 an error message. There's no context available to
1480 resolve the overloading. */
1481 if (TREE_TYPE (operand) == unknown_type_node)
1483 error ("type of asm operand %qE could not be determined",
1484 TREE_VALUE (t));
1485 operand = error_mark_node;
1488 if (constraint_parsed)
1490 /* If the operand is going to end up in memory,
1491 mark it addressable. */
1492 if (!allows_reg && allows_mem)
1494 /* Strip the nops as we allow this case. FIXME, this really
1495 should be rejected or made deprecated. */
1496 STRIP_NOPS (operand);
1497 if (!cxx_mark_addressable (operand))
1498 operand = error_mark_node;
1500 else if (!allows_reg && !allows_mem)
1502 /* If constraint allows neither register nor memory,
1503 try harder to get a constant. */
1504 tree constop = maybe_constant_value (operand);
1505 if (TREE_CONSTANT (constop))
1506 operand = constop;
1509 else
1510 operand = error_mark_node;
1512 TREE_VALUE (t) = operand;
1516 r = build_stmt (input_location, ASM_EXPR, string,
1517 output_operands, input_operands,
1518 clobbers, labels);
1519 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1520 r = maybe_cleanup_point_expr_void (r);
1521 return add_stmt (r);
1524 /* Finish a label with the indicated NAME. Returns the new label. */
1526 tree
1527 finish_label_stmt (tree name)
1529 tree decl = define_label (input_location, name);
1531 if (decl == error_mark_node)
1532 return error_mark_node;
1534 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1536 return decl;
1539 /* Finish a series of declarations for local labels. G++ allows users
1540 to declare "local" labels, i.e., labels with scope. This extension
1541 is useful when writing code involving statement-expressions. */
1543 void
1544 finish_label_decl (tree name)
1546 if (!at_function_scope_p ())
1548 error ("__label__ declarations are only allowed in function scopes");
1549 return;
1552 add_decl_expr (declare_local_label (name));
1555 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1557 void
1558 finish_decl_cleanup (tree decl, tree cleanup)
1560 push_cleanup (decl, cleanup, false);
1563 /* If the current scope exits with an exception, run CLEANUP. */
1565 void
1566 finish_eh_cleanup (tree cleanup)
1568 push_cleanup (NULL, cleanup, true);
1571 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1572 order they were written by the user. Each node is as for
1573 emit_mem_initializers. */
1575 void
1576 finish_mem_initializers (tree mem_inits)
1578 /* Reorder the MEM_INITS so that they are in the order they appeared
1579 in the source program. */
1580 mem_inits = nreverse (mem_inits);
1582 if (processing_template_decl)
1584 tree mem;
1586 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1588 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1589 check for bare parameter packs in the TREE_VALUE, because
1590 any parameter packs in the TREE_VALUE have already been
1591 bound as part of the TREE_PURPOSE. See
1592 make_pack_expansion for more information. */
1593 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1594 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1595 TREE_VALUE (mem) = error_mark_node;
1598 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1599 CTOR_INITIALIZER, mem_inits));
1601 else
1602 emit_mem_initializers (mem_inits);
1605 /* Obfuscate EXPR if it looks like an id-expression or member access so
1606 that the call to finish_decltype in do_auto_deduction will give the
1607 right result. */
1609 tree
1610 force_paren_expr (tree expr)
1612 /* This is only needed for decltype(auto) in C++14. */
1613 if (cxx_dialect < cxx1y)
1614 return expr;
1616 /* If we're in unevaluated context, we can't be deducing a
1617 return/initializer type, so we don't need to mess with this. */
1618 if (cp_unevaluated_operand)
1619 return expr;
1621 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1622 && TREE_CODE (expr) != SCOPE_REF)
1623 return expr;
1625 if (TREE_CODE (expr) == COMPONENT_REF)
1626 REF_PARENTHESIZED_P (expr) = true;
1627 else if (type_dependent_expression_p (expr))
1628 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1629 else
1631 cp_lvalue_kind kind = lvalue_kind (expr);
1632 if ((kind & ~clk_class) != clk_none)
1634 tree type = unlowered_expr_type (expr);
1635 bool rval = !!(kind & clk_rvalueref);
1636 type = cp_build_reference_type (type, rval);
1637 expr = build_static_cast (type, expr, tf_error);
1641 return expr;
1644 /* Finish a parenthesized expression EXPR. */
1646 tree
1647 finish_parenthesized_expr (tree expr)
1649 if (EXPR_P (expr))
1650 /* This inhibits warnings in c_common_truthvalue_conversion. */
1651 TREE_NO_WARNING (expr) = 1;
1653 if (TREE_CODE (expr) == OFFSET_REF
1654 || TREE_CODE (expr) == SCOPE_REF)
1655 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1656 enclosed in parentheses. */
1657 PTRMEM_OK_P (expr) = 0;
1659 if (TREE_CODE (expr) == STRING_CST)
1660 PAREN_STRING_LITERAL_P (expr) = 1;
1662 expr = force_paren_expr (expr);
1664 return expr;
1667 /* Finish a reference to a non-static data member (DECL) that is not
1668 preceded by `.' or `->'. */
1670 tree
1671 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1673 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1675 if (!object)
1677 tree scope = qualifying_scope;
1678 if (scope == NULL_TREE)
1679 scope = context_for_name_lookup (decl);
1680 object = maybe_dummy_object (scope, NULL);
1683 object = maybe_resolve_dummy (object, true);
1684 if (object == error_mark_node)
1685 return error_mark_node;
1687 /* DR 613: Can use non-static data members without an associated
1688 object in sizeof/decltype/alignof. */
1689 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1690 && (!processing_template_decl || !current_class_ref))
1692 if (current_function_decl
1693 && DECL_STATIC_FUNCTION_P (current_function_decl))
1694 error ("invalid use of member %q+D in static member function", decl);
1695 else
1696 error ("invalid use of non-static data member %q+D", decl);
1697 error ("from this location");
1699 return error_mark_node;
1702 if (current_class_ptr)
1703 TREE_USED (current_class_ptr) = 1;
1704 if (processing_template_decl && !qualifying_scope)
1706 tree type = TREE_TYPE (decl);
1708 if (TREE_CODE (type) == REFERENCE_TYPE)
1709 /* Quals on the object don't matter. */;
1710 else if (PACK_EXPANSION_P (type))
1711 /* Don't bother trying to represent this. */
1712 type = NULL_TREE;
1713 else
1715 /* Set the cv qualifiers. */
1716 int quals = cp_type_quals (TREE_TYPE (object));
1718 if (DECL_MUTABLE_P (decl))
1719 quals &= ~TYPE_QUAL_CONST;
1721 quals |= cp_type_quals (TREE_TYPE (decl));
1722 type = cp_build_qualified_type (type, quals);
1725 return (convert_from_reference
1726 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1728 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1729 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1730 for now. */
1731 else if (processing_template_decl)
1732 return build_qualified_name (TREE_TYPE (decl),
1733 qualifying_scope,
1734 decl,
1735 /*template_p=*/false);
1736 else
1738 tree access_type = TREE_TYPE (object);
1740 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1741 decl, tf_warning_or_error);
1743 /* If the data member was named `C::M', convert `*this' to `C'
1744 first. */
1745 if (qualifying_scope)
1747 tree binfo = NULL_TREE;
1748 object = build_scoped_ref (object, qualifying_scope,
1749 &binfo);
1752 return build_class_member_access_expr (object, decl,
1753 /*access_path=*/NULL_TREE,
1754 /*preserve_reference=*/false,
1755 tf_warning_or_error);
1759 /* If we are currently parsing a template and we encountered a typedef
1760 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1761 adds the typedef to a list tied to the current template.
1762 At template instantiation time, that list is walked and access check
1763 performed for each typedef.
1764 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1766 void
1767 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1768 tree context,
1769 location_t location)
1771 tree template_info = NULL;
1772 tree cs = current_scope ();
1774 if (!is_typedef_decl (typedef_decl)
1775 || !context
1776 || !CLASS_TYPE_P (context)
1777 || !cs)
1778 return;
1780 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1781 template_info = get_template_info (cs);
1783 if (template_info
1784 && TI_TEMPLATE (template_info)
1785 && !currently_open_class (context))
1786 append_type_to_template_for_access_check (cs, typedef_decl,
1787 context, location);
1790 /* DECL was the declaration to which a qualified-id resolved. Issue
1791 an error message if it is not accessible. If OBJECT_TYPE is
1792 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1793 type of `*x', or `x', respectively. If the DECL was named as
1794 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1796 void
1797 check_accessibility_of_qualified_id (tree decl,
1798 tree object_type,
1799 tree nested_name_specifier)
1801 tree scope;
1802 tree qualifying_type = NULL_TREE;
1804 /* If we are parsing a template declaration and if decl is a typedef,
1805 add it to a list tied to the template.
1806 At template instantiation time, that list will be walked and
1807 access check performed. */
1808 add_typedef_to_current_template_for_access_check (decl,
1809 nested_name_specifier
1810 ? nested_name_specifier
1811 : DECL_CONTEXT (decl),
1812 input_location);
1814 /* If we're not checking, return immediately. */
1815 if (deferred_access_no_check)
1816 return;
1818 /* Determine the SCOPE of DECL. */
1819 scope = context_for_name_lookup (decl);
1820 /* If the SCOPE is not a type, then DECL is not a member. */
1821 if (!TYPE_P (scope))
1822 return;
1823 /* Compute the scope through which DECL is being accessed. */
1824 if (object_type
1825 /* OBJECT_TYPE might not be a class type; consider:
1827 class A { typedef int I; };
1828 I *p;
1829 p->A::I::~I();
1831 In this case, we will have "A::I" as the DECL, but "I" as the
1832 OBJECT_TYPE. */
1833 && CLASS_TYPE_P (object_type)
1834 && DERIVED_FROM_P (scope, object_type))
1835 /* If we are processing a `->' or `.' expression, use the type of the
1836 left-hand side. */
1837 qualifying_type = object_type;
1838 else if (nested_name_specifier)
1840 /* If the reference is to a non-static member of the
1841 current class, treat it as if it were referenced through
1842 `this'. */
1843 tree ct;
1844 if (DECL_NONSTATIC_MEMBER_P (decl)
1845 && current_class_ptr
1846 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1847 qualifying_type = ct;
1848 /* Otherwise, use the type indicated by the
1849 nested-name-specifier. */
1850 else
1851 qualifying_type = nested_name_specifier;
1853 else
1854 /* Otherwise, the name must be from the current class or one of
1855 its bases. */
1856 qualifying_type = currently_open_derived_class (scope);
1858 if (qualifying_type
1859 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1860 or similar in a default argument value. */
1861 && CLASS_TYPE_P (qualifying_type)
1862 && !dependent_type_p (qualifying_type))
1863 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1864 decl, tf_warning_or_error);
1867 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1868 class named to the left of the "::" operator. DONE is true if this
1869 expression is a complete postfix-expression; it is false if this
1870 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1871 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1872 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1873 is true iff this qualified name appears as a template argument. */
1875 tree
1876 finish_qualified_id_expr (tree qualifying_class,
1877 tree expr,
1878 bool done,
1879 bool address_p,
1880 bool template_p,
1881 bool template_arg_p,
1882 tsubst_flags_t complain)
1884 gcc_assert (TYPE_P (qualifying_class));
1886 if (error_operand_p (expr))
1887 return error_mark_node;
1889 if ((DECL_P (expr) || BASELINK_P (expr))
1890 && !mark_used (expr, complain))
1891 return error_mark_node;
1893 if (template_p)
1894 check_template_keyword (expr);
1896 /* If EXPR occurs as the operand of '&', use special handling that
1897 permits a pointer-to-member. */
1898 if (address_p && done)
1900 if (TREE_CODE (expr) == SCOPE_REF)
1901 expr = TREE_OPERAND (expr, 1);
1902 expr = build_offset_ref (qualifying_class, expr,
1903 /*address_p=*/true, complain);
1904 return expr;
1907 /* No need to check access within an enum. */
1908 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1909 return expr;
1911 /* Within the scope of a class, turn references to non-static
1912 members into expression of the form "this->...". */
1913 if (template_arg_p)
1914 /* But, within a template argument, we do not want make the
1915 transformation, as there is no "this" pointer. */
1917 else if (TREE_CODE (expr) == FIELD_DECL)
1919 push_deferring_access_checks (dk_no_check);
1920 expr = finish_non_static_data_member (expr, NULL_TREE,
1921 qualifying_class);
1922 pop_deferring_access_checks ();
1924 else if (BASELINK_P (expr) && !processing_template_decl)
1926 /* See if any of the functions are non-static members. */
1927 /* If so, the expression may be relative to 'this'. */
1928 if (!shared_member_p (expr)
1929 && current_class_ptr
1930 && DERIVED_FROM_P (qualifying_class,
1931 current_nonlambda_class_type ()))
1932 expr = (build_class_member_access_expr
1933 (maybe_dummy_object (qualifying_class, NULL),
1934 expr,
1935 BASELINK_ACCESS_BINFO (expr),
1936 /*preserve_reference=*/false,
1937 complain));
1938 else if (done)
1939 /* The expression is a qualified name whose address is not
1940 being taken. */
1941 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1942 complain);
1944 else if (BASELINK_P (expr))
1946 else
1948 /* In a template, return a SCOPE_REF for most qualified-ids
1949 so that we can check access at instantiation time. But if
1950 we're looking at a member of the current instantiation, we
1951 know we have access and building up the SCOPE_REF confuses
1952 non-type template argument handling. */
1953 if (processing_template_decl
1954 && !currently_open_class (qualifying_class))
1955 expr = build_qualified_name (TREE_TYPE (expr),
1956 qualifying_class, expr,
1957 template_p);
1959 expr = convert_from_reference (expr);
1962 return expr;
1965 /* Begin a statement-expression. The value returned must be passed to
1966 finish_stmt_expr. */
1968 tree
1969 begin_stmt_expr (void)
1971 return push_stmt_list ();
1974 /* Process the final expression of a statement expression. EXPR can be
1975 NULL, if the final expression is empty. Return a STATEMENT_LIST
1976 containing all the statements in the statement-expression, or
1977 ERROR_MARK_NODE if there was an error. */
1979 tree
1980 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1982 if (error_operand_p (expr))
1984 /* The type of the statement-expression is the type of the last
1985 expression. */
1986 TREE_TYPE (stmt_expr) = error_mark_node;
1987 return error_mark_node;
1990 /* If the last statement does not have "void" type, then the value
1991 of the last statement is the value of the entire expression. */
1992 if (expr)
1994 tree type = TREE_TYPE (expr);
1996 if (processing_template_decl)
1998 expr = build_stmt (input_location, EXPR_STMT, expr);
1999 expr = add_stmt (expr);
2000 /* Mark the last statement so that we can recognize it as such at
2001 template-instantiation time. */
2002 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2004 else if (VOID_TYPE_P (type))
2006 /* Just treat this like an ordinary statement. */
2007 expr = finish_expr_stmt (expr);
2009 else
2011 /* It actually has a value we need to deal with. First, force it
2012 to be an rvalue so that we won't need to build up a copy
2013 constructor call later when we try to assign it to something. */
2014 expr = force_rvalue (expr, tf_warning_or_error);
2015 if (error_operand_p (expr))
2016 return error_mark_node;
2018 /* Update for array-to-pointer decay. */
2019 type = TREE_TYPE (expr);
2021 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2022 normal statement, but don't convert to void or actually add
2023 the EXPR_STMT. */
2024 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2025 expr = maybe_cleanup_point_expr (expr);
2026 add_stmt (expr);
2029 /* The type of the statement-expression is the type of the last
2030 expression. */
2031 TREE_TYPE (stmt_expr) = type;
2034 return stmt_expr;
2037 /* Finish a statement-expression. EXPR should be the value returned
2038 by the previous begin_stmt_expr. Returns an expression
2039 representing the statement-expression. */
2041 tree
2042 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2044 tree type;
2045 tree result;
2047 if (error_operand_p (stmt_expr))
2049 pop_stmt_list (stmt_expr);
2050 return error_mark_node;
2053 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2055 type = TREE_TYPE (stmt_expr);
2056 result = pop_stmt_list (stmt_expr);
2057 TREE_TYPE (result) = type;
2059 if (processing_template_decl)
2061 result = build_min (STMT_EXPR, type, result);
2062 TREE_SIDE_EFFECTS (result) = 1;
2063 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2065 else if (CLASS_TYPE_P (type))
2067 /* Wrap the statement-expression in a TARGET_EXPR so that the
2068 temporary object created by the final expression is destroyed at
2069 the end of the full-expression containing the
2070 statement-expression. */
2071 result = force_target_expr (type, result, tf_warning_or_error);
2074 return result;
2077 /* Returns the expression which provides the value of STMT_EXPR. */
2079 tree
2080 stmt_expr_value_expr (tree stmt_expr)
2082 tree t = STMT_EXPR_STMT (stmt_expr);
2084 if (TREE_CODE (t) == BIND_EXPR)
2085 t = BIND_EXPR_BODY (t);
2087 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2088 t = STATEMENT_LIST_TAIL (t)->stmt;
2090 if (TREE_CODE (t) == EXPR_STMT)
2091 t = EXPR_STMT_EXPR (t);
2093 return t;
2096 /* Return TRUE iff EXPR_STMT is an empty list of
2097 expression statements. */
2099 bool
2100 empty_expr_stmt_p (tree expr_stmt)
2102 tree body = NULL_TREE;
2104 if (expr_stmt == void_node)
2105 return true;
2107 if (expr_stmt)
2109 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2110 body = EXPR_STMT_EXPR (expr_stmt);
2111 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2112 body = expr_stmt;
2115 if (body)
2117 if (TREE_CODE (body) == STATEMENT_LIST)
2118 return tsi_end_p (tsi_start (body));
2119 else
2120 return empty_expr_stmt_p (body);
2122 return false;
2125 /* Perform Koenig lookup. FN is the postfix-expression representing
2126 the function (or functions) to call; ARGS are the arguments to the
2127 call. Returns the functions to be considered by overload resolution. */
2129 tree
2130 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2131 tsubst_flags_t complain)
2133 tree identifier = NULL_TREE;
2134 tree functions = NULL_TREE;
2135 tree tmpl_args = NULL_TREE;
2136 bool template_id = false;
2138 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2140 /* Use a separate flag to handle null args. */
2141 template_id = true;
2142 tmpl_args = TREE_OPERAND (fn, 1);
2143 fn = TREE_OPERAND (fn, 0);
2146 /* Find the name of the overloaded function. */
2147 if (identifier_p (fn))
2148 identifier = fn;
2149 else if (is_overloaded_fn (fn))
2151 functions = fn;
2152 identifier = DECL_NAME (get_first_fn (functions));
2154 else if (DECL_P (fn))
2156 functions = fn;
2157 identifier = DECL_NAME (fn);
2160 /* A call to a namespace-scope function using an unqualified name.
2162 Do Koenig lookup -- unless any of the arguments are
2163 type-dependent. */
2164 if (!any_type_dependent_arguments_p (args)
2165 && !any_dependent_template_arguments_p (tmpl_args))
2167 fn = lookup_arg_dependent (identifier, functions, args);
2168 if (!fn)
2170 /* The unqualified name could not be resolved. */
2171 if (complain)
2172 fn = unqualified_fn_lookup_error (identifier);
2173 else
2174 fn = identifier;
2178 if (fn && template_id)
2179 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2181 return fn;
2184 /* Generate an expression for `FN (ARGS)'. This may change the
2185 contents of ARGS.
2187 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2188 as a virtual call, even if FN is virtual. (This flag is set when
2189 encountering an expression where the function name is explicitly
2190 qualified. For example a call to `X::f' never generates a virtual
2191 call.)
2193 Returns code for the call. */
2195 tree
2196 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2197 bool koenig_p, tsubst_flags_t complain)
2199 tree result;
2200 tree orig_fn;
2201 vec<tree, va_gc> *orig_args = NULL;
2203 if (fn == error_mark_node)
2204 return error_mark_node;
2206 gcc_assert (!TYPE_P (fn));
2208 orig_fn = fn;
2210 if (processing_template_decl)
2212 /* If the call expression is dependent, build a CALL_EXPR node
2213 with no type; type_dependent_expression_p recognizes
2214 expressions with no type as being dependent. */
2215 if (type_dependent_expression_p (fn)
2216 || any_type_dependent_arguments_p (*args)
2217 /* For a non-static member function that doesn't have an
2218 explicit object argument, we need to specifically
2219 test the type dependency of the "this" pointer because it
2220 is not included in *ARGS even though it is considered to
2221 be part of the list of arguments. Note that this is
2222 related to CWG issues 515 and 1005. */
2223 || (TREE_CODE (fn) != COMPONENT_REF
2224 && non_static_member_function_p (fn)
2225 && current_class_ref
2226 && type_dependent_expression_p (current_class_ref)))
2228 result = build_nt_call_vec (fn, *args);
2229 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2230 KOENIG_LOOKUP_P (result) = koenig_p;
2231 if (cfun)
2235 tree fndecl = OVL_CURRENT (fn);
2236 if (TREE_CODE (fndecl) != FUNCTION_DECL
2237 || !TREE_THIS_VOLATILE (fndecl))
2238 break;
2239 fn = OVL_NEXT (fn);
2241 while (fn);
2242 if (!fn)
2243 current_function_returns_abnormally = 1;
2245 return result;
2247 orig_args = make_tree_vector_copy (*args);
2248 if (!BASELINK_P (fn)
2249 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2250 && TREE_TYPE (fn) != unknown_type_node)
2251 fn = build_non_dependent_expr (fn);
2252 make_args_non_dependent (*args);
2255 if (TREE_CODE (fn) == COMPONENT_REF)
2257 tree member = TREE_OPERAND (fn, 1);
2258 if (BASELINK_P (member))
2260 tree object = TREE_OPERAND (fn, 0);
2261 return build_new_method_call (object, member,
2262 args, NULL_TREE,
2263 (disallow_virtual
2264 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2265 : LOOKUP_NORMAL),
2266 /*fn_p=*/NULL,
2267 complain);
2271 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2272 if (TREE_CODE (fn) == ADDR_EXPR
2273 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2274 fn = TREE_OPERAND (fn, 0);
2276 if (is_overloaded_fn (fn))
2277 fn = baselink_for_fns (fn);
2279 result = NULL_TREE;
2280 if (BASELINK_P (fn))
2282 tree object;
2284 /* A call to a member function. From [over.call.func]:
2286 If the keyword this is in scope and refers to the class of
2287 that member function, or a derived class thereof, then the
2288 function call is transformed into a qualified function call
2289 using (*this) as the postfix-expression to the left of the
2290 . operator.... [Otherwise] a contrived object of type T
2291 becomes the implied object argument.
2293 In this situation:
2295 struct A { void f(); };
2296 struct B : public A {};
2297 struct C : public A { void g() { B::f(); }};
2299 "the class of that member function" refers to `A'. But 11.2
2300 [class.access.base] says that we need to convert 'this' to B* as
2301 part of the access, so we pass 'B' to maybe_dummy_object. */
2303 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2304 NULL);
2306 if (processing_template_decl)
2308 if (type_dependent_expression_p (object))
2310 tree ret = build_nt_call_vec (orig_fn, orig_args);
2311 release_tree_vector (orig_args);
2312 return ret;
2314 object = build_non_dependent_expr (object);
2317 result = build_new_method_call (object, fn, args, NULL_TREE,
2318 (disallow_virtual
2319 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2320 : LOOKUP_NORMAL),
2321 /*fn_p=*/NULL,
2322 complain);
2324 else if (is_overloaded_fn (fn))
2326 /* If the function is an overloaded builtin, resolve it. */
2327 if (TREE_CODE (fn) == FUNCTION_DECL
2328 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2329 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2330 result = resolve_overloaded_builtin (input_location, fn, *args);
2332 if (!result)
2334 if (warn_sizeof_pointer_memaccess
2335 && !vec_safe_is_empty (*args)
2336 && !processing_template_decl)
2338 location_t sizeof_arg_loc[3];
2339 tree sizeof_arg[3];
2340 unsigned int i;
2341 for (i = 0; i < 3; i++)
2343 tree t;
2345 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2346 sizeof_arg[i] = NULL_TREE;
2347 if (i >= (*args)->length ())
2348 continue;
2349 t = (**args)[i];
2350 if (TREE_CODE (t) != SIZEOF_EXPR)
2351 continue;
2352 if (SIZEOF_EXPR_TYPE_P (t))
2353 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2354 else
2355 sizeof_arg[i] = TREE_OPERAND (t, 0);
2356 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2358 sizeof_pointer_memaccess_warning
2359 (sizeof_arg_loc, fn, *args,
2360 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2363 /* A call to a namespace-scope function. */
2364 result = build_new_function_call (fn, args, koenig_p, complain);
2367 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2369 if (!vec_safe_is_empty (*args))
2370 error ("arguments to destructor are not allowed");
2371 /* Mark the pseudo-destructor call as having side-effects so
2372 that we do not issue warnings about its use. */
2373 result = build1 (NOP_EXPR,
2374 void_type_node,
2375 TREE_OPERAND (fn, 0));
2376 TREE_SIDE_EFFECTS (result) = 1;
2378 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2379 /* If the "function" is really an object of class type, it might
2380 have an overloaded `operator ()'. */
2381 result = build_op_call (fn, args, complain);
2383 if (!result)
2384 /* A call where the function is unknown. */
2385 result = cp_build_function_call_vec (fn, args, complain);
2387 if (processing_template_decl && result != error_mark_node)
2389 if (INDIRECT_REF_P (result))
2390 result = TREE_OPERAND (result, 0);
2391 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2392 SET_EXPR_LOCATION (result, input_location);
2393 KOENIG_LOOKUP_P (result) = koenig_p;
2394 release_tree_vector (orig_args);
2395 result = convert_from_reference (result);
2398 if (koenig_p)
2400 /* Free garbage OVERLOADs from arg-dependent lookup. */
2401 tree next = NULL_TREE;
2402 for (fn = orig_fn;
2403 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2404 fn = next)
2406 if (processing_template_decl)
2407 /* In a template, we'll re-use them at instantiation time. */
2408 OVL_ARG_DEPENDENT (fn) = false;
2409 else
2411 next = OVL_CHAIN (fn);
2412 ggc_free (fn);
2417 return result;
2420 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
2422 tree
2423 finish_template_variable (tree var)
2425 return instantiate_template (TREE_OPERAND (var, 0), TREE_OPERAND (var, 1),
2426 tf_error);
2429 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2430 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2431 POSTDECREMENT_EXPR.) */
2433 tree
2434 finish_increment_expr (tree expr, enum tree_code code)
2436 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2439 /* Finish a use of `this'. Returns an expression for `this'. */
2441 tree
2442 finish_this_expr (void)
2444 tree result;
2446 if (current_class_ptr)
2448 tree type = TREE_TYPE (current_class_ref);
2450 /* In a lambda expression, 'this' refers to the captured 'this'. */
2451 if (LAMBDA_TYPE_P (type))
2452 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2453 else
2454 result = current_class_ptr;
2456 else if (current_function_decl
2457 && DECL_STATIC_FUNCTION_P (current_function_decl))
2459 error ("%<this%> is unavailable for static member functions");
2460 result = error_mark_node;
2462 else
2464 if (current_function_decl)
2465 error ("invalid use of %<this%> in non-member function");
2466 else
2467 error ("invalid use of %<this%> at top level");
2468 result = error_mark_node;
2471 /* The keyword 'this' is a prvalue expression. */
2472 result = rvalue (result);
2474 return result;
2477 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2478 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2479 the TYPE for the type given. If SCOPE is non-NULL, the expression
2480 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2482 tree
2483 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2484 location_t loc)
2486 if (object == error_mark_node || destructor == error_mark_node)
2487 return error_mark_node;
2489 gcc_assert (TYPE_P (destructor));
2491 if (!processing_template_decl)
2493 if (scope == error_mark_node)
2495 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2496 return error_mark_node;
2498 if (is_auto (destructor))
2499 destructor = TREE_TYPE (object);
2500 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2502 error_at (loc,
2503 "qualified type %qT does not match destructor name ~%qT",
2504 scope, destructor);
2505 return error_mark_node;
2509 /* [expr.pseudo] says both:
2511 The type designated by the pseudo-destructor-name shall be
2512 the same as the object type.
2514 and:
2516 The cv-unqualified versions of the object type and of the
2517 type designated by the pseudo-destructor-name shall be the
2518 same type.
2520 We implement the more generous second sentence, since that is
2521 what most other compilers do. */
2522 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2523 destructor))
2525 error_at (loc, "%qE is not of type %qT", object, destructor);
2526 return error_mark_node;
2530 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2531 scope, destructor);
2534 /* Finish an expression of the form CODE EXPR. */
2536 tree
2537 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2538 tsubst_flags_t complain)
2540 tree result = build_x_unary_op (loc, code, expr, complain);
2541 if ((complain & tf_warning)
2542 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2543 overflow_warning (input_location, result);
2545 return result;
2548 /* Finish a compound-literal expression. TYPE is the type to which
2549 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2551 tree
2552 finish_compound_literal (tree type, tree compound_literal,
2553 tsubst_flags_t complain)
2555 if (type == error_mark_node)
2556 return error_mark_node;
2558 if (TREE_CODE (type) == REFERENCE_TYPE)
2560 compound_literal
2561 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2562 complain);
2563 return cp_build_c_cast (type, compound_literal, complain);
2566 if (!TYPE_OBJ_P (type))
2568 if (complain & tf_error)
2569 error ("compound literal of non-object type %qT", type);
2570 return error_mark_node;
2573 if (processing_template_decl)
2575 TREE_TYPE (compound_literal) = type;
2576 /* Mark the expression as a compound literal. */
2577 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2578 return compound_literal;
2581 type = complete_type (type);
2583 if (TYPE_NON_AGGREGATE_CLASS (type))
2585 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2586 everywhere that deals with function arguments would be a pain, so
2587 just wrap it in a TREE_LIST. The parser set a flag so we know
2588 that it came from T{} rather than T({}). */
2589 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2590 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2591 return build_functional_cast (type, compound_literal, complain);
2594 if (TREE_CODE (type) == ARRAY_TYPE
2595 && check_array_initializer (NULL_TREE, type, compound_literal))
2596 return error_mark_node;
2597 compound_literal = reshape_init (type, compound_literal, complain);
2598 if (SCALAR_TYPE_P (type)
2599 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2600 && (complain & tf_warning_or_error))
2601 check_narrowing (type, compound_literal);
2602 if (TREE_CODE (type) == ARRAY_TYPE
2603 && TYPE_DOMAIN (type) == NULL_TREE)
2605 cp_complete_array_type_or_error (&type, compound_literal,
2606 false, complain);
2607 if (type == error_mark_node)
2608 return error_mark_node;
2610 compound_literal = digest_init (type, compound_literal, complain);
2611 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2612 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2613 /* Put static/constant array temporaries in static variables, but always
2614 represent class temporaries with TARGET_EXPR so we elide copies. */
2615 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2616 && TREE_CODE (type) == ARRAY_TYPE
2617 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2618 && initializer_constant_valid_p (compound_literal, type))
2620 tree decl = create_temporary_var (type);
2621 DECL_INITIAL (decl) = compound_literal;
2622 TREE_STATIC (decl) = 1;
2623 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2625 /* 5.19 says that a constant expression can include an
2626 lvalue-rvalue conversion applied to "a glvalue of literal type
2627 that refers to a non-volatile temporary object initialized
2628 with a constant expression". Rather than try to communicate
2629 that this VAR_DECL is a temporary, just mark it constexpr. */
2630 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2631 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2632 TREE_CONSTANT (decl) = true;
2634 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2635 decl = pushdecl_top_level (decl);
2636 DECL_NAME (decl) = make_anon_name ();
2637 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2638 /* Make sure the destructor is callable. */
2639 tree clean = cxx_maybe_build_cleanup (decl, complain);
2640 if (clean == error_mark_node)
2641 return error_mark_node;
2642 return decl;
2644 else
2645 return get_target_expr_sfinae (compound_literal, complain);
2648 /* Return the declaration for the function-name variable indicated by
2649 ID. */
2651 tree
2652 finish_fname (tree id)
2654 tree decl;
2656 decl = fname_decl (input_location, C_RID_CODE (id), id);
2657 if (processing_template_decl && current_function_decl
2658 && decl != error_mark_node)
2659 decl = DECL_NAME (decl);
2660 return decl;
2663 /* Finish a translation unit. */
2665 void
2666 finish_translation_unit (void)
2668 /* In case there were missing closebraces,
2669 get us back to the global binding level. */
2670 pop_everything ();
2671 while (current_namespace != global_namespace)
2672 pop_namespace ();
2674 /* Do file scope __FUNCTION__ et al. */
2675 finish_fname_decls ();
2678 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2679 Returns the parameter. */
2681 tree
2682 finish_template_type_parm (tree aggr, tree identifier)
2684 if (aggr != class_type_node)
2686 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2687 aggr = class_type_node;
2690 return build_tree_list (aggr, identifier);
2693 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2694 Returns the parameter. */
2696 tree
2697 finish_template_template_parm (tree aggr, tree identifier)
2699 tree decl = build_decl (input_location,
2700 TYPE_DECL, identifier, NULL_TREE);
2701 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2702 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2703 DECL_TEMPLATE_RESULT (tmpl) = decl;
2704 DECL_ARTIFICIAL (decl) = 1;
2705 end_template_decl ();
2707 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2709 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2710 /*is_primary=*/true, /*is_partial=*/false,
2711 /*is_friend=*/0);
2713 return finish_template_type_parm (aggr, tmpl);
2716 /* ARGUMENT is the default-argument value for a template template
2717 parameter. If ARGUMENT is invalid, issue error messages and return
2718 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2720 tree
2721 check_template_template_default_arg (tree argument)
2723 if (TREE_CODE (argument) != TEMPLATE_DECL
2724 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2725 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2727 if (TREE_CODE (argument) == TYPE_DECL)
2728 error ("invalid use of type %qT as a default value for a template "
2729 "template-parameter", TREE_TYPE (argument));
2730 else
2731 error ("invalid default argument for a template template parameter");
2732 return error_mark_node;
2735 return argument;
2738 /* Begin a class definition, as indicated by T. */
2740 tree
2741 begin_class_definition (tree t)
2743 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2744 return error_mark_node;
2746 if (processing_template_parmlist)
2748 error ("definition of %q#T inside template parameter list", t);
2749 return error_mark_node;
2752 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2753 are passed the same as decimal scalar types. */
2754 if (TREE_CODE (t) == RECORD_TYPE
2755 && !processing_template_decl)
2757 tree ns = TYPE_CONTEXT (t);
2758 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2759 && DECL_CONTEXT (ns) == std_node
2760 && DECL_NAME (ns)
2761 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2763 const char *n = TYPE_NAME_STRING (t);
2764 if ((strcmp (n, "decimal32") == 0)
2765 || (strcmp (n, "decimal64") == 0)
2766 || (strcmp (n, "decimal128") == 0))
2767 TYPE_TRANSPARENT_AGGR (t) = 1;
2771 /* A non-implicit typename comes from code like:
2773 template <typename T> struct A {
2774 template <typename U> struct A<T>::B ...
2776 This is erroneous. */
2777 else if (TREE_CODE (t) == TYPENAME_TYPE)
2779 error ("invalid definition of qualified type %qT", t);
2780 t = error_mark_node;
2783 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2785 t = make_class_type (RECORD_TYPE);
2786 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2789 if (TYPE_BEING_DEFINED (t))
2791 t = make_class_type (TREE_CODE (t));
2792 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2794 maybe_process_partial_specialization (t);
2795 pushclass (t);
2796 TYPE_BEING_DEFINED (t) = 1;
2797 class_binding_level->defining_class_p = 1;
2799 if (flag_pack_struct)
2801 tree v;
2802 TYPE_PACKED (t) = 1;
2803 /* Even though the type is being defined for the first time
2804 here, there might have been a forward declaration, so there
2805 might be cv-qualified variants of T. */
2806 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2807 TYPE_PACKED (v) = 1;
2809 /* Reset the interface data, at the earliest possible
2810 moment, as it might have been set via a class foo;
2811 before. */
2812 if (! TYPE_ANONYMOUS_P (t))
2814 struct c_fileinfo *finfo = \
2815 get_fileinfo (LOCATION_FILE (input_location));
2816 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2817 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2818 (t, finfo->interface_unknown);
2820 reset_specialization();
2822 /* Make a declaration for this class in its own scope. */
2823 build_self_reference ();
2825 return t;
2828 /* Finish the member declaration given by DECL. */
2830 void
2831 finish_member_declaration (tree decl)
2833 if (decl == error_mark_node || decl == NULL_TREE)
2834 return;
2836 if (decl == void_type_node)
2837 /* The COMPONENT was a friend, not a member, and so there's
2838 nothing for us to do. */
2839 return;
2841 /* We should see only one DECL at a time. */
2842 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2844 /* Set up access control for DECL. */
2845 TREE_PRIVATE (decl)
2846 = (current_access_specifier == access_private_node);
2847 TREE_PROTECTED (decl)
2848 = (current_access_specifier == access_protected_node);
2849 if (TREE_CODE (decl) == TEMPLATE_DECL)
2851 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2852 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2855 /* Mark the DECL as a member of the current class, unless it's
2856 a member of an enumeration. */
2857 if (TREE_CODE (decl) != CONST_DECL)
2858 DECL_CONTEXT (decl) = current_class_type;
2860 /* Check for bare parameter packs in the member variable declaration. */
2861 if (TREE_CODE (decl) == FIELD_DECL)
2863 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2864 TREE_TYPE (decl) = error_mark_node;
2865 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2866 DECL_ATTRIBUTES (decl) = NULL_TREE;
2869 /* [dcl.link]
2871 A C language linkage is ignored for the names of class members
2872 and the member function type of class member functions. */
2873 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2874 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2876 /* Put functions on the TYPE_METHODS list and everything else on the
2877 TYPE_FIELDS list. Note that these are built up in reverse order.
2878 We reverse them (to obtain declaration order) in finish_struct. */
2879 if (DECL_DECLARES_FUNCTION_P (decl))
2881 /* We also need to add this function to the
2882 CLASSTYPE_METHOD_VEC. */
2883 if (add_method (current_class_type, decl, NULL_TREE))
2885 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2886 TYPE_METHODS (current_class_type) = decl;
2888 maybe_add_class_template_decl_list (current_class_type, decl,
2889 /*friend_p=*/0);
2892 /* Enter the DECL into the scope of the class, if the class
2893 isn't a closure (whose fields are supposed to be unnamed). */
2894 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2895 || pushdecl_class_level (decl))
2897 if (TREE_CODE (decl) == USING_DECL)
2899 /* For now, ignore class-scope USING_DECLS, so that
2900 debugging backends do not see them. */
2901 DECL_IGNORED_P (decl) = 1;
2904 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2905 go at the beginning. The reason is that lookup_field_1
2906 searches the list in order, and we want a field name to
2907 override a type name so that the "struct stat hack" will
2908 work. In particular:
2910 struct S { enum E { }; int E } s;
2911 s.E = 3;
2913 is valid. In addition, the FIELD_DECLs must be maintained in
2914 declaration order so that class layout works as expected.
2915 However, we don't need that order until class layout, so we
2916 save a little time by putting FIELD_DECLs on in reverse order
2917 here, and then reversing them in finish_struct_1. (We could
2918 also keep a pointer to the correct insertion points in the
2919 list.) */
2921 if (TREE_CODE (decl) == TYPE_DECL)
2922 TYPE_FIELDS (current_class_type)
2923 = chainon (TYPE_FIELDS (current_class_type), decl);
2924 else
2926 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2927 TYPE_FIELDS (current_class_type) = decl;
2930 maybe_add_class_template_decl_list (current_class_type, decl,
2931 /*friend_p=*/0);
2934 if (pch_file)
2935 note_decl_for_pch (decl);
2938 /* DECL has been declared while we are building a PCH file. Perform
2939 actions that we might normally undertake lazily, but which can be
2940 performed now so that they do not have to be performed in
2941 translation units which include the PCH file. */
2943 void
2944 note_decl_for_pch (tree decl)
2946 gcc_assert (pch_file);
2948 /* There's a good chance that we'll have to mangle names at some
2949 point, even if only for emission in debugging information. */
2950 if (VAR_OR_FUNCTION_DECL_P (decl)
2951 && !processing_template_decl)
2952 mangle_decl (decl);
2955 /* Finish processing a complete template declaration. The PARMS are
2956 the template parameters. */
2958 void
2959 finish_template_decl (tree parms)
2961 if (parms)
2962 end_template_decl ();
2963 else
2964 end_specialization ();
2967 /* Finish processing a template-id (which names a type) of the form
2968 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2969 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2970 the scope of template-id indicated. */
2972 tree
2973 finish_template_type (tree name, tree args, int entering_scope)
2975 tree type;
2977 type = lookup_template_class (name, args,
2978 NULL_TREE, NULL_TREE, entering_scope,
2979 tf_warning_or_error | tf_user);
2980 if (type == error_mark_node)
2981 return type;
2982 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2983 return TYPE_STUB_DECL (type);
2984 else
2985 return TYPE_NAME (type);
2988 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2989 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2990 BASE_CLASS, or NULL_TREE if an error occurred. The
2991 ACCESS_SPECIFIER is one of
2992 access_{default,public,protected_private}_node. For a virtual base
2993 we set TREE_TYPE. */
2995 tree
2996 finish_base_specifier (tree base, tree access, bool virtual_p)
2998 tree result;
3000 if (base == error_mark_node)
3002 error ("invalid base-class specification");
3003 result = NULL_TREE;
3005 else if (! MAYBE_CLASS_TYPE_P (base))
3007 error ("%qT is not a class type", base);
3008 result = NULL_TREE;
3010 else
3012 if (cp_type_quals (base) != 0)
3014 /* DR 484: Can a base-specifier name a cv-qualified
3015 class type? */
3016 base = TYPE_MAIN_VARIANT (base);
3018 result = build_tree_list (access, base);
3019 if (virtual_p)
3020 TREE_TYPE (result) = integer_type_node;
3023 return result;
3026 /* If FNS is a member function, a set of member functions, or a
3027 template-id referring to one or more member functions, return a
3028 BASELINK for FNS, incorporating the current access context.
3029 Otherwise, return FNS unchanged. */
3031 tree
3032 baselink_for_fns (tree fns)
3034 tree scope;
3035 tree cl;
3037 if (BASELINK_P (fns)
3038 || error_operand_p (fns))
3039 return fns;
3041 scope = ovl_scope (fns);
3042 if (!CLASS_TYPE_P (scope))
3043 return fns;
3045 cl = currently_open_derived_class (scope);
3046 if (!cl)
3047 cl = scope;
3048 cl = TYPE_BINFO (cl);
3049 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3052 /* Returns true iff DECL is a variable from a function outside
3053 the current one. */
3055 static bool
3056 outer_var_p (tree decl)
3058 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3059 && DECL_FUNCTION_SCOPE_P (decl)
3060 && (DECL_CONTEXT (decl) != current_function_decl
3061 || parsing_nsdmi ()));
3064 /* As above, but also checks that DECL is automatic. */
3066 static bool
3067 outer_automatic_var_p (tree decl)
3069 return (outer_var_p (decl)
3070 && !TREE_STATIC (decl));
3073 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3074 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3075 if non-NULL, is the type or namespace used to explicitly qualify
3076 ID_EXPRESSION. DECL is the entity to which that name has been
3077 resolved.
3079 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3080 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3081 be set to true if this expression isn't permitted in a
3082 constant-expression, but it is otherwise not set by this function.
3083 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3084 constant-expression, but a non-constant expression is also
3085 permissible.
3087 DONE is true if this expression is a complete postfix-expression;
3088 it is false if this expression is followed by '->', '[', '(', etc.
3089 ADDRESS_P is true iff this expression is the operand of '&'.
3090 TEMPLATE_P is true iff the qualified-id was of the form
3091 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3092 appears as a template argument.
3094 If an error occurs, and it is the kind of error that might cause
3095 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3096 is the caller's responsibility to issue the message. *ERROR_MSG
3097 will be a string with static storage duration, so the caller need
3098 not "free" it.
3100 Return an expression for the entity, after issuing appropriate
3101 diagnostics. This function is also responsible for transforming a
3102 reference to a non-static member into a COMPONENT_REF that makes
3103 the use of "this" explicit.
3105 Upon return, *IDK will be filled in appropriately. */
3106 tree
3107 finish_id_expression (tree id_expression,
3108 tree decl,
3109 tree scope,
3110 cp_id_kind *idk,
3111 bool integral_constant_expression_p,
3112 bool allow_non_integral_constant_expression_p,
3113 bool *non_integral_constant_expression_p,
3114 bool template_p,
3115 bool done,
3116 bool address_p,
3117 bool template_arg_p,
3118 const char **error_msg,
3119 location_t location)
3121 decl = strip_using_decl (decl);
3123 /* Initialize the output parameters. */
3124 *idk = CP_ID_KIND_NONE;
3125 *error_msg = NULL;
3127 if (id_expression == error_mark_node)
3128 return error_mark_node;
3129 /* If we have a template-id, then no further lookup is
3130 required. If the template-id was for a template-class, we
3131 will sometimes have a TYPE_DECL at this point. */
3132 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3133 || TREE_CODE (decl) == TYPE_DECL)
3135 /* Look up the name. */
3136 else
3138 if (decl == error_mark_node)
3140 /* Name lookup failed. */
3141 if (scope
3142 && (!TYPE_P (scope)
3143 || (!dependent_type_p (scope)
3144 && !(identifier_p (id_expression)
3145 && IDENTIFIER_TYPENAME_P (id_expression)
3146 && dependent_type_p (TREE_TYPE (id_expression))))))
3148 /* If the qualifying type is non-dependent (and the name
3149 does not name a conversion operator to a dependent
3150 type), issue an error. */
3151 qualified_name_lookup_error (scope, id_expression, decl, location);
3152 return error_mark_node;
3154 else if (!scope)
3156 /* It may be resolved via Koenig lookup. */
3157 *idk = CP_ID_KIND_UNQUALIFIED;
3158 return id_expression;
3160 else
3161 decl = id_expression;
3163 /* If DECL is a variable that would be out of scope under
3164 ANSI/ISO rules, but in scope in the ARM, name lookup
3165 will succeed. Issue a diagnostic here. */
3166 else
3167 decl = check_for_out_of_scope_variable (decl);
3169 /* Remember that the name was used in the definition of
3170 the current class so that we can check later to see if
3171 the meaning would have been different after the class
3172 was entirely defined. */
3173 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3174 maybe_note_name_used_in_class (id_expression, decl);
3176 /* Disallow uses of local variables from containing functions, except
3177 within lambda-expressions. */
3178 if (!outer_var_p (decl))
3179 /* OK */;
3180 else if (TREE_STATIC (decl)
3181 /* It's not a use (3.2) if we're in an unevaluated context. */
3182 || cp_unevaluated_operand)
3183 /* OK */;
3184 else
3186 tree context = DECL_CONTEXT (decl);
3187 tree containing_function = current_function_decl;
3188 tree lambda_stack = NULL_TREE;
3189 tree lambda_expr = NULL_TREE;
3190 tree initializer = convert_from_reference (decl);
3192 /* Mark it as used now even if the use is ill-formed. */
3193 mark_used (decl);
3195 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3196 support for an approach in which a reference to a local
3197 [constant] automatic variable in a nested class or lambda body
3198 would enter the expression as an rvalue, which would reduce
3199 the complexity of the problem"
3201 FIXME update for final resolution of core issue 696. */
3202 if (decl_maybe_constant_var_p (decl))
3204 if (processing_template_decl)
3205 /* In a template, the constant value may not be in a usable
3206 form, so wait until instantiation time. */
3207 return decl;
3208 else if (decl_constant_var_p (decl))
3209 return integral_constant_value (decl);
3212 if (parsing_nsdmi ())
3213 containing_function = NULL_TREE;
3214 /* If we are in a lambda function, we can move out until we hit
3215 1. the context,
3216 2. a non-lambda function, or
3217 3. a non-default capturing lambda function. */
3218 else while (context != containing_function
3219 && LAMBDA_FUNCTION_P (containing_function))
3221 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3222 (DECL_CONTEXT (containing_function));
3224 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3225 == CPLD_NONE)
3226 break;
3228 lambda_stack = tree_cons (NULL_TREE,
3229 lambda_expr,
3230 lambda_stack);
3232 containing_function
3233 = decl_function_context (containing_function);
3236 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3237 && DECL_ANON_UNION_VAR_P (decl))
3239 error ("cannot capture member %qD of anonymous union", decl);
3240 return error_mark_node;
3242 if (context == containing_function)
3244 decl = add_default_capture (lambda_stack,
3245 /*id=*/DECL_NAME (decl),
3246 initializer);
3248 else if (lambda_expr)
3250 error ("%qD is not captured", decl);
3251 return error_mark_node;
3253 else
3255 error (VAR_P (decl)
3256 ? G_("use of local variable with automatic storage from containing function")
3257 : G_("use of parameter from containing function"));
3258 inform (input_location, "%q+#D declared here", decl);
3259 return error_mark_node;
3263 /* Also disallow uses of function parameters outside the function
3264 body, except inside an unevaluated context (i.e. decltype). */
3265 if (TREE_CODE (decl) == PARM_DECL
3266 && DECL_CONTEXT (decl) == NULL_TREE
3267 && !cp_unevaluated_operand)
3269 *error_msg = "use of parameter outside function body";
3270 return error_mark_node;
3274 /* If we didn't find anything, or what we found was a type,
3275 then this wasn't really an id-expression. */
3276 if (TREE_CODE (decl) == TEMPLATE_DECL
3277 && !DECL_FUNCTION_TEMPLATE_P (decl))
3279 *error_msg = "missing template arguments";
3280 return error_mark_node;
3282 else if (TREE_CODE (decl) == TYPE_DECL
3283 || TREE_CODE (decl) == NAMESPACE_DECL)
3285 *error_msg = "expected primary-expression";
3286 return error_mark_node;
3289 /* If the name resolved to a template parameter, there is no
3290 need to look it up again later. */
3291 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3292 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3294 tree r;
3296 *idk = CP_ID_KIND_NONE;
3297 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3298 decl = TEMPLATE_PARM_DECL (decl);
3299 r = convert_from_reference (DECL_INITIAL (decl));
3301 if (integral_constant_expression_p
3302 && !dependent_type_p (TREE_TYPE (decl))
3303 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3305 if (!allow_non_integral_constant_expression_p)
3306 error ("template parameter %qD of type %qT is not allowed in "
3307 "an integral constant expression because it is not of "
3308 "integral or enumeration type", decl, TREE_TYPE (decl));
3309 *non_integral_constant_expression_p = true;
3311 return r;
3313 else
3315 bool dependent_p;
3317 /* If the declaration was explicitly qualified indicate
3318 that. The semantics of `A::f(3)' are different than
3319 `f(3)' if `f' is virtual. */
3320 *idk = (scope
3321 ? CP_ID_KIND_QUALIFIED
3322 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3323 ? CP_ID_KIND_TEMPLATE_ID
3324 : CP_ID_KIND_UNQUALIFIED));
3327 /* [temp.dep.expr]
3329 An id-expression is type-dependent if it contains an
3330 identifier that was declared with a dependent type.
3332 The standard is not very specific about an id-expression that
3333 names a set of overloaded functions. What if some of them
3334 have dependent types and some of them do not? Presumably,
3335 such a name should be treated as a dependent name. */
3336 /* Assume the name is not dependent. */
3337 dependent_p = false;
3338 if (!processing_template_decl)
3339 /* No names are dependent outside a template. */
3341 else if (TREE_CODE (decl) == CONST_DECL)
3342 /* We don't want to treat enumerators as dependent. */
3344 /* A template-id where the name of the template was not resolved
3345 is definitely dependent. */
3346 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3347 && (identifier_p (TREE_OPERAND (decl, 0))))
3348 dependent_p = true;
3349 /* For anything except an overloaded function, just check its
3350 type. */
3351 else if (!is_overloaded_fn (decl))
3352 dependent_p
3353 = dependent_type_p (TREE_TYPE (decl));
3354 /* For a set of overloaded functions, check each of the
3355 functions. */
3356 else
3358 tree fns = decl;
3360 if (BASELINK_P (fns))
3361 fns = BASELINK_FUNCTIONS (fns);
3363 /* For a template-id, check to see if the template
3364 arguments are dependent. */
3365 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3367 tree args = TREE_OPERAND (fns, 1);
3368 dependent_p = any_dependent_template_arguments_p (args);
3369 /* The functions are those referred to by the
3370 template-id. */
3371 fns = TREE_OPERAND (fns, 0);
3374 /* If there are no dependent template arguments, go through
3375 the overloaded functions. */
3376 while (fns && !dependent_p)
3378 tree fn = OVL_CURRENT (fns);
3380 /* Member functions of dependent classes are
3381 dependent. */
3382 if (TREE_CODE (fn) == FUNCTION_DECL
3383 && type_dependent_expression_p (fn))
3384 dependent_p = true;
3385 else if (TREE_CODE (fn) == TEMPLATE_DECL
3386 && dependent_template_p (fn))
3387 dependent_p = true;
3389 fns = OVL_NEXT (fns);
3393 /* If the name was dependent on a template parameter, we will
3394 resolve the name at instantiation time. */
3395 if (dependent_p)
3397 /* Create a SCOPE_REF for qualified names, if the scope is
3398 dependent. */
3399 if (scope)
3401 if (TYPE_P (scope))
3403 if (address_p && done)
3404 decl = finish_qualified_id_expr (scope, decl,
3405 done, address_p,
3406 template_p,
3407 template_arg_p,
3408 tf_warning_or_error);
3409 else
3411 tree type = NULL_TREE;
3412 if (DECL_P (decl) && !dependent_scope_p (scope))
3413 type = TREE_TYPE (decl);
3414 decl = build_qualified_name (type,
3415 scope,
3416 id_expression,
3417 template_p);
3420 if (TREE_TYPE (decl))
3421 decl = convert_from_reference (decl);
3422 return decl;
3424 /* A TEMPLATE_ID already contains all the information we
3425 need. */
3426 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3427 return id_expression;
3428 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3429 /* If we found a variable, then name lookup during the
3430 instantiation will always resolve to the same VAR_DECL
3431 (or an instantiation thereof). */
3432 if (VAR_P (decl)
3433 || TREE_CODE (decl) == PARM_DECL)
3435 mark_used (decl);
3436 return convert_from_reference (decl);
3438 /* The same is true for FIELD_DECL, but we also need to
3439 make sure that the syntax is correct. */
3440 else if (TREE_CODE (decl) == FIELD_DECL)
3442 /* Since SCOPE is NULL here, this is an unqualified name.
3443 Access checking has been performed during name lookup
3444 already. Turn off checking to avoid duplicate errors. */
3445 push_deferring_access_checks (dk_no_check);
3446 decl = finish_non_static_data_member
3447 (decl, NULL_TREE,
3448 /*qualifying_scope=*/NULL_TREE);
3449 pop_deferring_access_checks ();
3450 return decl;
3452 return id_expression;
3455 if (TREE_CODE (decl) == NAMESPACE_DECL)
3457 error ("use of namespace %qD as expression", decl);
3458 return error_mark_node;
3460 else if (DECL_CLASS_TEMPLATE_P (decl))
3462 error ("use of class template %qT as expression", decl);
3463 return error_mark_node;
3465 else if (TREE_CODE (decl) == TREE_LIST)
3467 /* Ambiguous reference to base members. */
3468 error ("request for member %qD is ambiguous in "
3469 "multiple inheritance lattice", id_expression);
3470 print_candidates (decl);
3471 return error_mark_node;
3474 /* Mark variable-like entities as used. Functions are similarly
3475 marked either below or after overload resolution. */
3476 if ((VAR_P (decl)
3477 || TREE_CODE (decl) == PARM_DECL
3478 || TREE_CODE (decl) == CONST_DECL
3479 || TREE_CODE (decl) == RESULT_DECL)
3480 && !mark_used (decl))
3481 return error_mark_node;
3483 /* Only certain kinds of names are allowed in constant
3484 expression. Template parameters have already
3485 been handled above. */
3486 if (! error_operand_p (decl)
3487 && integral_constant_expression_p
3488 && ! decl_constant_var_p (decl)
3489 && TREE_CODE (decl) != CONST_DECL
3490 && ! builtin_valid_in_constant_expr_p (decl))
3492 if (!allow_non_integral_constant_expression_p)
3494 error ("%qD cannot appear in a constant-expression", decl);
3495 return error_mark_node;
3497 *non_integral_constant_expression_p = true;
3500 tree wrap;
3501 if (VAR_P (decl)
3502 && !cp_unevaluated_operand
3503 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3504 && DECL_THREAD_LOCAL_P (decl)
3505 && (wrap = get_tls_wrapper_fn (decl)))
3507 /* Replace an evaluated use of the thread_local variable with
3508 a call to its wrapper. */
3509 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3511 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3512 && variable_template_p (TREE_OPERAND (decl, 0)))
3514 decl = finish_template_variable (decl);
3516 else if (scope)
3518 decl = (adjust_result_of_qualified_name_lookup
3519 (decl, scope, current_nonlambda_class_type()));
3521 if (TREE_CODE (decl) == FUNCTION_DECL)
3522 mark_used (decl);
3524 if (TYPE_P (scope))
3525 decl = finish_qualified_id_expr (scope,
3526 decl,
3527 done,
3528 address_p,
3529 template_p,
3530 template_arg_p,
3531 tf_warning_or_error);
3532 else
3533 decl = convert_from_reference (decl);
3535 else if (TREE_CODE (decl) == FIELD_DECL)
3537 /* Since SCOPE is NULL here, this is an unqualified name.
3538 Access checking has been performed during name lookup
3539 already. Turn off checking to avoid duplicate errors. */
3540 push_deferring_access_checks (dk_no_check);
3541 decl = finish_non_static_data_member (decl, NULL_TREE,
3542 /*qualifying_scope=*/NULL_TREE);
3543 pop_deferring_access_checks ();
3545 else if (is_overloaded_fn (decl))
3547 tree first_fn;
3549 first_fn = get_first_fn (decl);
3550 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3551 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3553 if (!really_overloaded_fn (decl)
3554 && !mark_used (first_fn))
3555 return error_mark_node;
3557 if (!template_arg_p
3558 && TREE_CODE (first_fn) == FUNCTION_DECL
3559 && DECL_FUNCTION_MEMBER_P (first_fn)
3560 && !shared_member_p (decl))
3562 /* A set of member functions. */
3563 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3564 return finish_class_member_access_expr (decl, id_expression,
3565 /*template_p=*/false,
3566 tf_warning_or_error);
3569 decl = baselink_for_fns (decl);
3571 else
3573 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3574 && DECL_CLASS_SCOPE_P (decl))
3576 tree context = context_for_name_lookup (decl);
3577 if (context != current_class_type)
3579 tree path = currently_open_derived_class (context);
3580 perform_or_defer_access_check (TYPE_BINFO (path),
3581 decl, decl,
3582 tf_warning_or_error);
3586 decl = convert_from_reference (decl);
3590 /* Handle references (c++/56130). */
3591 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3592 if (TREE_DEPRECATED (t))
3593 warn_deprecated_use (t, NULL_TREE);
3595 return decl;
3598 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3599 use as a type-specifier. */
3601 tree
3602 finish_typeof (tree expr)
3604 tree type;
3606 if (type_dependent_expression_p (expr))
3608 type = cxx_make_type (TYPEOF_TYPE);
3609 TYPEOF_TYPE_EXPR (type) = expr;
3610 SET_TYPE_STRUCTURAL_EQUALITY (type);
3612 return type;
3615 expr = mark_type_use (expr);
3617 type = unlowered_expr_type (expr);
3619 if (!type || type == unknown_type_node)
3621 error ("type of %qE is unknown", expr);
3622 return error_mark_node;
3625 return type;
3628 /* Implement the __underlying_type keyword: Return the underlying
3629 type of TYPE, suitable for use as a type-specifier. */
3631 tree
3632 finish_underlying_type (tree type)
3634 tree underlying_type;
3636 if (processing_template_decl)
3638 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3639 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3640 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3642 return underlying_type;
3645 complete_type (type);
3647 if (TREE_CODE (type) != ENUMERAL_TYPE)
3649 error ("%qT is not an enumeration type", type);
3650 return error_mark_node;
3653 underlying_type = ENUM_UNDERLYING_TYPE (type);
3655 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3656 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3657 See finish_enum_value_list for details. */
3658 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3659 underlying_type
3660 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3661 TYPE_UNSIGNED (underlying_type));
3663 return underlying_type;
3666 /* Implement the __direct_bases keyword: Return the direct base classes
3667 of type */
3669 tree
3670 calculate_direct_bases (tree type)
3672 vec<tree, va_gc> *vector = make_tree_vector();
3673 tree bases_vec = NULL_TREE;
3674 vec<tree, va_gc> *base_binfos;
3675 tree binfo;
3676 unsigned i;
3678 complete_type (type);
3680 if (!NON_UNION_CLASS_TYPE_P (type))
3681 return make_tree_vec (0);
3683 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3685 /* Virtual bases are initialized first */
3686 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3688 if (BINFO_VIRTUAL_P (binfo))
3690 vec_safe_push (vector, binfo);
3694 /* Now non-virtuals */
3695 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3697 if (!BINFO_VIRTUAL_P (binfo))
3699 vec_safe_push (vector, binfo);
3704 bases_vec = make_tree_vec (vector->length ());
3706 for (i = 0; i < vector->length (); ++i)
3708 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3710 return bases_vec;
3713 /* Implement the __bases keyword: Return the base classes
3714 of type */
3716 /* Find morally non-virtual base classes by walking binfo hierarchy */
3717 /* Virtual base classes are handled separately in finish_bases */
3719 static tree
3720 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3722 /* Don't walk bases of virtual bases */
3723 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3726 static tree
3727 dfs_calculate_bases_post (tree binfo, void *data_)
3729 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3730 if (!BINFO_VIRTUAL_P (binfo))
3732 vec_safe_push (*data, BINFO_TYPE (binfo));
3734 return NULL_TREE;
3737 /* Calculates the morally non-virtual base classes of a class */
3738 static vec<tree, va_gc> *
3739 calculate_bases_helper (tree type)
3741 vec<tree, va_gc> *vector = make_tree_vector();
3743 /* Now add non-virtual base classes in order of construction */
3744 dfs_walk_all (TYPE_BINFO (type),
3745 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3746 return vector;
3749 tree
3750 calculate_bases (tree type)
3752 vec<tree, va_gc> *vector = make_tree_vector();
3753 tree bases_vec = NULL_TREE;
3754 unsigned i;
3755 vec<tree, va_gc> *vbases;
3756 vec<tree, va_gc> *nonvbases;
3757 tree binfo;
3759 complete_type (type);
3761 if (!NON_UNION_CLASS_TYPE_P (type))
3762 return make_tree_vec (0);
3764 /* First go through virtual base classes */
3765 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3766 vec_safe_iterate (vbases, i, &binfo); i++)
3768 vec<tree, va_gc> *vbase_bases;
3769 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3770 vec_safe_splice (vector, vbase_bases);
3771 release_tree_vector (vbase_bases);
3774 /* Now for the non-virtual bases */
3775 nonvbases = calculate_bases_helper (type);
3776 vec_safe_splice (vector, nonvbases);
3777 release_tree_vector (nonvbases);
3779 /* Last element is entire class, so don't copy */
3780 bases_vec = make_tree_vec (vector->length () - 1);
3782 for (i = 0; i < vector->length () - 1; ++i)
3784 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3786 release_tree_vector (vector);
3787 return bases_vec;
3790 tree
3791 finish_bases (tree type, bool direct)
3793 tree bases = NULL_TREE;
3795 if (!processing_template_decl)
3797 /* Parameter packs can only be used in templates */
3798 error ("Parameter pack __bases only valid in template declaration");
3799 return error_mark_node;
3802 bases = cxx_make_type (BASES);
3803 BASES_TYPE (bases) = type;
3804 BASES_DIRECT (bases) = direct;
3805 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3807 return bases;
3810 /* Perform C++-specific checks for __builtin_offsetof before calling
3811 fold_offsetof. */
3813 tree
3814 finish_offsetof (tree expr)
3816 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3818 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3819 TREE_OPERAND (expr, 2));
3820 return error_mark_node;
3822 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3823 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3824 || TREE_TYPE (expr) == unknown_type_node)
3826 if (INDIRECT_REF_P (expr))
3827 error ("second operand of %<offsetof%> is neither a single "
3828 "identifier nor a sequence of member accesses and "
3829 "array references");
3830 else
3832 if (TREE_CODE (expr) == COMPONENT_REF
3833 || TREE_CODE (expr) == COMPOUND_EXPR)
3834 expr = TREE_OPERAND (expr, 1);
3835 error ("cannot apply %<offsetof%> to member function %qD", expr);
3837 return error_mark_node;
3839 if (REFERENCE_REF_P (expr))
3840 expr = TREE_OPERAND (expr, 0);
3841 if (TREE_CODE (expr) == COMPONENT_REF)
3843 tree object = TREE_OPERAND (expr, 0);
3844 if (!complete_type_or_else (TREE_TYPE (object), object))
3845 return error_mark_node;
3847 return fold_offsetof (expr);
3850 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3851 function is broken out from the above for the benefit of the tree-ssa
3852 project. */
3854 void
3855 simplify_aggr_init_expr (tree *tp)
3857 tree aggr_init_expr = *tp;
3859 /* Form an appropriate CALL_EXPR. */
3860 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3861 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3862 tree type = TREE_TYPE (slot);
3864 tree call_expr;
3865 enum style_t { ctor, arg, pcc } style;
3867 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3868 style = ctor;
3869 #ifdef PCC_STATIC_STRUCT_RETURN
3870 else if (1)
3871 style = pcc;
3872 #endif
3873 else
3875 gcc_assert (TREE_ADDRESSABLE (type));
3876 style = arg;
3879 call_expr = build_call_array_loc (input_location,
3880 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3882 aggr_init_expr_nargs (aggr_init_expr),
3883 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3884 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3885 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3887 if (style == ctor)
3889 /* Replace the first argument to the ctor with the address of the
3890 slot. */
3891 cxx_mark_addressable (slot);
3892 CALL_EXPR_ARG (call_expr, 0) =
3893 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3895 else if (style == arg)
3897 /* Just mark it addressable here, and leave the rest to
3898 expand_call{,_inline}. */
3899 cxx_mark_addressable (slot);
3900 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3901 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3903 else if (style == pcc)
3905 /* If we're using the non-reentrant PCC calling convention, then we
3906 need to copy the returned value out of the static buffer into the
3907 SLOT. */
3908 push_deferring_access_checks (dk_no_check);
3909 call_expr = build_aggr_init (slot, call_expr,
3910 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3911 tf_warning_or_error);
3912 pop_deferring_access_checks ();
3913 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3916 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3918 tree init = build_zero_init (type, NULL_TREE,
3919 /*static_storage_p=*/false);
3920 init = build2 (INIT_EXPR, void_type_node, slot, init);
3921 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3922 init, call_expr);
3925 *tp = call_expr;
3928 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3930 void
3931 emit_associated_thunks (tree fn)
3933 /* When we use vcall offsets, we emit thunks with the virtual
3934 functions to which they thunk. The whole point of vcall offsets
3935 is so that you can know statically the entire set of thunks that
3936 will ever be needed for a given virtual function, thereby
3937 enabling you to output all the thunks with the function itself. */
3938 if (DECL_VIRTUAL_P (fn)
3939 /* Do not emit thunks for extern template instantiations. */
3940 && ! DECL_REALLY_EXTERN (fn))
3942 tree thunk;
3944 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3946 if (!THUNK_ALIAS (thunk))
3948 use_thunk (thunk, /*emit_p=*/1);
3949 if (DECL_RESULT_THUNK_P (thunk))
3951 tree probe;
3953 for (probe = DECL_THUNKS (thunk);
3954 probe; probe = DECL_CHAIN (probe))
3955 use_thunk (probe, /*emit_p=*/1);
3958 else
3959 gcc_assert (!DECL_THUNKS (thunk));
3964 /* Returns true iff FUN is an instantiation of a constexpr function
3965 template or a defaulted constexpr function. */
3967 static inline bool
3968 is_instantiation_of_constexpr (tree fun)
3970 return ((DECL_TEMPLOID_INSTANTIATION (fun)
3971 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
3972 || (DECL_DEFAULTED_FN (fun)
3973 && DECL_DECLARED_CONSTEXPR_P (fun)));
3977 /* Generate RTL for FN. */
3979 bool
3980 expand_or_defer_fn_1 (tree fn)
3982 /* When the parser calls us after finishing the body of a template
3983 function, we don't really want to expand the body. */
3984 if (processing_template_decl)
3986 /* Normally, collection only occurs in rest_of_compilation. So,
3987 if we don't collect here, we never collect junk generated
3988 during the processing of templates until we hit a
3989 non-template function. It's not safe to do this inside a
3990 nested class, though, as the parser may have local state that
3991 is not a GC root. */
3992 if (!function_depth)
3993 ggc_collect ();
3994 return false;
3997 gcc_assert (DECL_SAVED_TREE (fn));
3999 /* We make a decision about linkage for these functions at the end
4000 of the compilation. Until that point, we do not want the back
4001 end to output them -- but we do want it to see the bodies of
4002 these functions so that it can inline them as appropriate. */
4003 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4005 if (DECL_INTERFACE_KNOWN (fn))
4006 /* We've already made a decision as to how this function will
4007 be handled. */;
4008 else if (!at_eof)
4009 tentative_decl_linkage (fn);
4010 else
4011 import_export_decl (fn);
4013 /* If the user wants us to keep all inline functions, then mark
4014 this function as needed so that finish_file will make sure to
4015 output it later. Similarly, all dllexport'd functions must
4016 be emitted; there may be callers in other DLLs. */
4017 if ((flag_keep_inline_functions
4018 && DECL_DECLARED_INLINE_P (fn)
4019 && !DECL_REALLY_EXTERN (fn))
4020 || (flag_keep_inline_dllexport
4021 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4023 mark_needed (fn);
4024 DECL_EXTERNAL (fn) = 0;
4028 /* If this is a constructor or destructor body, we have to clone
4029 it. */
4030 if (maybe_clone_body (fn))
4032 /* We don't want to process FN again, so pretend we've written
4033 it out, even though we haven't. */
4034 TREE_ASM_WRITTEN (fn) = 1;
4035 /* If this is an instantiation of a constexpr function, keep
4036 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4037 if (!is_instantiation_of_constexpr (fn))
4038 DECL_SAVED_TREE (fn) = NULL_TREE;
4039 return false;
4042 /* There's no reason to do any of the work here if we're only doing
4043 semantic analysis; this code just generates RTL. */
4044 if (flag_syntax_only)
4045 return false;
4047 return true;
4050 void
4051 expand_or_defer_fn (tree fn)
4053 if (expand_or_defer_fn_1 (fn))
4055 function_depth++;
4057 /* Expand or defer, at the whim of the compilation unit manager. */
4058 cgraph_finalize_function (fn, function_depth > 1);
4059 emit_associated_thunks (fn);
4061 function_depth--;
4065 struct nrv_data
4067 nrv_data () : visited (37) {}
4069 tree var;
4070 tree result;
4071 hash_table<pointer_hash <tree_node> > visited;
4074 /* Helper function for walk_tree, used by finalize_nrv below. */
4076 static tree
4077 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4079 struct nrv_data *dp = (struct nrv_data *)data;
4080 tree_node **slot;
4082 /* No need to walk into types. There wouldn't be any need to walk into
4083 non-statements, except that we have to consider STMT_EXPRs. */
4084 if (TYPE_P (*tp))
4085 *walk_subtrees = 0;
4086 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4087 but differs from using NULL_TREE in that it indicates that we care
4088 about the value of the RESULT_DECL. */
4089 else if (TREE_CODE (*tp) == RETURN_EXPR)
4090 TREE_OPERAND (*tp, 0) = dp->result;
4091 /* Change all cleanups for the NRV to only run when an exception is
4092 thrown. */
4093 else if (TREE_CODE (*tp) == CLEANUP_STMT
4094 && CLEANUP_DECL (*tp) == dp->var)
4095 CLEANUP_EH_ONLY (*tp) = 1;
4096 /* Replace the DECL_EXPR for the NRV with an initialization of the
4097 RESULT_DECL, if needed. */
4098 else if (TREE_CODE (*tp) == DECL_EXPR
4099 && DECL_EXPR_DECL (*tp) == dp->var)
4101 tree init;
4102 if (DECL_INITIAL (dp->var)
4103 && DECL_INITIAL (dp->var) != error_mark_node)
4104 init = build2 (INIT_EXPR, void_type_node, dp->result,
4105 DECL_INITIAL (dp->var));
4106 else
4107 init = build_empty_stmt (EXPR_LOCATION (*tp));
4108 DECL_INITIAL (dp->var) = NULL_TREE;
4109 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4110 *tp = init;
4112 /* And replace all uses of the NRV with the RESULT_DECL. */
4113 else if (*tp == dp->var)
4114 *tp = dp->result;
4116 /* Avoid walking into the same tree more than once. Unfortunately, we
4117 can't just use walk_tree_without duplicates because it would only call
4118 us for the first occurrence of dp->var in the function body. */
4119 slot = dp->visited.find_slot (*tp, INSERT);
4120 if (*slot)
4121 *walk_subtrees = 0;
4122 else
4123 *slot = *tp;
4125 /* Keep iterating. */
4126 return NULL_TREE;
4129 /* Called from finish_function to implement the named return value
4130 optimization by overriding all the RETURN_EXPRs and pertinent
4131 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4132 RESULT_DECL for the function. */
4134 void
4135 finalize_nrv (tree *tp, tree var, tree result)
4137 struct nrv_data data;
4139 /* Copy name from VAR to RESULT. */
4140 DECL_NAME (result) = DECL_NAME (var);
4141 /* Don't forget that we take its address. */
4142 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4143 /* Finally set DECL_VALUE_EXPR to avoid assigning
4144 a stack slot at -O0 for the original var and debug info
4145 uses RESULT location for VAR. */
4146 SET_DECL_VALUE_EXPR (var, result);
4147 DECL_HAS_VALUE_EXPR_P (var) = 1;
4149 data.var = var;
4150 data.result = result;
4151 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4154 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4156 bool
4157 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4158 bool need_copy_ctor, bool need_copy_assignment,
4159 bool need_dtor)
4161 int save_errorcount = errorcount;
4162 tree info, t;
4164 /* Always allocate 3 elements for simplicity. These are the
4165 function decls for the ctor, dtor, and assignment op.
4166 This layout is known to the three lang hooks,
4167 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4168 and cxx_omp_clause_assign_op. */
4169 info = make_tree_vec (3);
4170 CP_OMP_CLAUSE_INFO (c) = info;
4172 if (need_default_ctor || need_copy_ctor)
4174 if (need_default_ctor)
4175 t = get_default_ctor (type);
4176 else
4177 t = get_copy_ctor (type, tf_warning_or_error);
4179 if (t && !trivial_fn_p (t))
4180 TREE_VEC_ELT (info, 0) = t;
4183 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4184 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4186 if (need_copy_assignment)
4188 t = get_copy_assign (type);
4190 if (t && !trivial_fn_p (t))
4191 TREE_VEC_ELT (info, 2) = t;
4194 return errorcount != save_errorcount;
4197 /* Helper function for handle_omp_array_sections. Called recursively
4198 to handle multiple array-section-subscripts. C is the clause,
4199 T current expression (initially OMP_CLAUSE_DECL), which is either
4200 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4201 expression if specified, TREE_VALUE length expression if specified,
4202 TREE_CHAIN is what it has been specified after, or some decl.
4203 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4204 set to true if any of the array-section-subscript could have length
4205 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4206 first array-section-subscript which is known not to have length
4207 of one. Given say:
4208 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4209 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4210 all are or may have length of 1, array-section-subscript [:2] is the
4211 first one knonwn not to have length 1. For array-section-subscript
4212 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4213 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4214 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4215 case though, as some lengths could be zero. */
4217 static tree
4218 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4219 bool &maybe_zero_len, unsigned int &first_non_one)
4221 tree ret, low_bound, length, type;
4222 if (TREE_CODE (t) != TREE_LIST)
4224 if (error_operand_p (t))
4225 return error_mark_node;
4226 if (type_dependent_expression_p (t))
4227 return NULL_TREE;
4228 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4230 if (processing_template_decl)
4231 return NULL_TREE;
4232 if (DECL_P (t))
4233 error_at (OMP_CLAUSE_LOCATION (c),
4234 "%qD is not a variable in %qs clause", t,
4235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4236 else
4237 error_at (OMP_CLAUSE_LOCATION (c),
4238 "%qE is not a variable in %qs clause", t,
4239 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4240 return error_mark_node;
4242 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4243 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4245 error_at (OMP_CLAUSE_LOCATION (c),
4246 "%qD is threadprivate variable in %qs clause", t,
4247 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4248 return error_mark_node;
4250 t = convert_from_reference (t);
4251 return t;
4254 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4255 maybe_zero_len, first_non_one);
4256 if (ret == error_mark_node || ret == NULL_TREE)
4257 return ret;
4259 type = TREE_TYPE (ret);
4260 low_bound = TREE_PURPOSE (t);
4261 length = TREE_VALUE (t);
4262 if ((low_bound && type_dependent_expression_p (low_bound))
4263 || (length && type_dependent_expression_p (length)))
4264 return NULL_TREE;
4266 if (low_bound == error_mark_node || length == error_mark_node)
4267 return error_mark_node;
4269 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4271 error_at (OMP_CLAUSE_LOCATION (c),
4272 "low bound %qE of array section does not have integral type",
4273 low_bound);
4274 return error_mark_node;
4276 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4278 error_at (OMP_CLAUSE_LOCATION (c),
4279 "length %qE of array section does not have integral type",
4280 length);
4281 return error_mark_node;
4283 if (low_bound
4284 && TREE_CODE (low_bound) == INTEGER_CST
4285 && TYPE_PRECISION (TREE_TYPE (low_bound))
4286 > TYPE_PRECISION (sizetype))
4287 low_bound = fold_convert (sizetype, low_bound);
4288 if (length
4289 && TREE_CODE (length) == INTEGER_CST
4290 && TYPE_PRECISION (TREE_TYPE (length))
4291 > TYPE_PRECISION (sizetype))
4292 length = fold_convert (sizetype, length);
4293 if (low_bound == NULL_TREE)
4294 low_bound = integer_zero_node;
4296 if (length != NULL_TREE)
4298 if (!integer_nonzerop (length))
4299 maybe_zero_len = true;
4300 if (first_non_one == types.length ()
4301 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4302 first_non_one++;
4304 if (TREE_CODE (type) == ARRAY_TYPE)
4306 if (length == NULL_TREE
4307 && (TYPE_DOMAIN (type) == NULL_TREE
4308 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4310 error_at (OMP_CLAUSE_LOCATION (c),
4311 "for unknown bound array type length expression must "
4312 "be specified");
4313 return error_mark_node;
4315 if (TREE_CODE (low_bound) == INTEGER_CST
4316 && tree_int_cst_sgn (low_bound) == -1)
4318 error_at (OMP_CLAUSE_LOCATION (c),
4319 "negative low bound in array section in %qs clause",
4320 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4321 return error_mark_node;
4323 if (length != NULL_TREE
4324 && TREE_CODE (length) == INTEGER_CST
4325 && tree_int_cst_sgn (length) == -1)
4327 error_at (OMP_CLAUSE_LOCATION (c),
4328 "negative length in array section in %qs clause",
4329 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4330 return error_mark_node;
4332 if (TYPE_DOMAIN (type)
4333 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4334 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4335 == INTEGER_CST)
4337 tree size = size_binop (PLUS_EXPR,
4338 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4339 size_one_node);
4340 if (TREE_CODE (low_bound) == INTEGER_CST)
4342 if (tree_int_cst_lt (size, low_bound))
4344 error_at (OMP_CLAUSE_LOCATION (c),
4345 "low bound %qE above array section size "
4346 "in %qs clause", low_bound,
4347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4348 return error_mark_node;
4350 if (tree_int_cst_equal (size, low_bound))
4351 maybe_zero_len = true;
4352 else if (length == NULL_TREE
4353 && first_non_one == types.length ()
4354 && tree_int_cst_equal
4355 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4356 low_bound))
4357 first_non_one++;
4359 else if (length == NULL_TREE)
4361 maybe_zero_len = true;
4362 if (first_non_one == types.length ())
4363 first_non_one++;
4365 if (length && TREE_CODE (length) == INTEGER_CST)
4367 if (tree_int_cst_lt (size, length))
4369 error_at (OMP_CLAUSE_LOCATION (c),
4370 "length %qE above array section size "
4371 "in %qs clause", length,
4372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4373 return error_mark_node;
4375 if (TREE_CODE (low_bound) == INTEGER_CST)
4377 tree lbpluslen
4378 = size_binop (PLUS_EXPR,
4379 fold_convert (sizetype, low_bound),
4380 fold_convert (sizetype, length));
4381 if (TREE_CODE (lbpluslen) == INTEGER_CST
4382 && tree_int_cst_lt (size, lbpluslen))
4384 error_at (OMP_CLAUSE_LOCATION (c),
4385 "high bound %qE above array section size "
4386 "in %qs clause", lbpluslen,
4387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4388 return error_mark_node;
4393 else if (length == NULL_TREE)
4395 maybe_zero_len = true;
4396 if (first_non_one == types.length ())
4397 first_non_one++;
4400 /* For [lb:] we will need to evaluate lb more than once. */
4401 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4403 tree lb = cp_save_expr (low_bound);
4404 if (lb != low_bound)
4406 TREE_PURPOSE (t) = lb;
4407 low_bound = lb;
4411 else if (TREE_CODE (type) == POINTER_TYPE)
4413 if (length == NULL_TREE)
4415 error_at (OMP_CLAUSE_LOCATION (c),
4416 "for pointer type length expression must be specified");
4417 return error_mark_node;
4419 /* If there is a pointer type anywhere but in the very first
4420 array-section-subscript, the array section can't be contiguous. */
4421 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4422 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4424 error_at (OMP_CLAUSE_LOCATION (c),
4425 "array section is not contiguous in %qs clause",
4426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4427 return error_mark_node;
4430 else
4432 error_at (OMP_CLAUSE_LOCATION (c),
4433 "%qE does not have pointer or array type", ret);
4434 return error_mark_node;
4436 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4437 types.safe_push (TREE_TYPE (ret));
4438 /* We will need to evaluate lb more than once. */
4439 tree lb = cp_save_expr (low_bound);
4440 if (lb != low_bound)
4442 TREE_PURPOSE (t) = lb;
4443 low_bound = lb;
4445 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4446 return ret;
4449 /* Handle array sections for clause C. */
4451 static bool
4452 handle_omp_array_sections (tree c)
4454 bool maybe_zero_len = false;
4455 unsigned int first_non_one = 0;
4456 auto_vec<tree> types;
4457 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4458 maybe_zero_len, first_non_one);
4459 if (first == error_mark_node)
4460 return true;
4461 if (first == NULL_TREE)
4462 return false;
4463 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4465 tree t = OMP_CLAUSE_DECL (c);
4466 tree tem = NULL_TREE;
4467 if (processing_template_decl)
4468 return false;
4469 /* Need to evaluate side effects in the length expressions
4470 if any. */
4471 while (TREE_CODE (t) == TREE_LIST)
4473 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4475 if (tem == NULL_TREE)
4476 tem = TREE_VALUE (t);
4477 else
4478 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4479 TREE_VALUE (t), tem);
4481 t = TREE_CHAIN (t);
4483 if (tem)
4484 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4485 OMP_CLAUSE_DECL (c) = first;
4487 else
4489 unsigned int num = types.length (), i;
4490 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4491 tree condition = NULL_TREE;
4493 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4494 maybe_zero_len = true;
4495 if (processing_template_decl && maybe_zero_len)
4496 return false;
4498 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4499 t = TREE_CHAIN (t))
4501 tree low_bound = TREE_PURPOSE (t);
4502 tree length = TREE_VALUE (t);
4504 i--;
4505 if (low_bound
4506 && TREE_CODE (low_bound) == INTEGER_CST
4507 && TYPE_PRECISION (TREE_TYPE (low_bound))
4508 > TYPE_PRECISION (sizetype))
4509 low_bound = fold_convert (sizetype, low_bound);
4510 if (length
4511 && TREE_CODE (length) == INTEGER_CST
4512 && TYPE_PRECISION (TREE_TYPE (length))
4513 > TYPE_PRECISION (sizetype))
4514 length = fold_convert (sizetype, length);
4515 if (low_bound == NULL_TREE)
4516 low_bound = integer_zero_node;
4517 if (!maybe_zero_len && i > first_non_one)
4519 if (integer_nonzerop (low_bound))
4520 goto do_warn_noncontiguous;
4521 if (length != NULL_TREE
4522 && TREE_CODE (length) == INTEGER_CST
4523 && TYPE_DOMAIN (types[i])
4524 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4525 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4526 == INTEGER_CST)
4528 tree size;
4529 size = size_binop (PLUS_EXPR,
4530 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4531 size_one_node);
4532 if (!tree_int_cst_equal (length, size))
4534 do_warn_noncontiguous:
4535 error_at (OMP_CLAUSE_LOCATION (c),
4536 "array section is not contiguous in %qs "
4537 "clause",
4538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4539 return true;
4542 if (!processing_template_decl
4543 && length != NULL_TREE
4544 && TREE_SIDE_EFFECTS (length))
4546 if (side_effects == NULL_TREE)
4547 side_effects = length;
4548 else
4549 side_effects = build2 (COMPOUND_EXPR,
4550 TREE_TYPE (side_effects),
4551 length, side_effects);
4554 else if (processing_template_decl)
4555 continue;
4556 else
4558 tree l;
4560 if (i > first_non_one && length && integer_nonzerop (length))
4561 continue;
4562 if (length)
4563 l = fold_convert (sizetype, length);
4564 else
4566 l = size_binop (PLUS_EXPR,
4567 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4568 size_one_node);
4569 l = size_binop (MINUS_EXPR, l,
4570 fold_convert (sizetype, low_bound));
4572 if (i > first_non_one)
4574 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4575 size_zero_node);
4576 if (condition == NULL_TREE)
4577 condition = l;
4578 else
4579 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4580 l, condition);
4582 else if (size == NULL_TREE)
4584 size = size_in_bytes (TREE_TYPE (types[i]));
4585 size = size_binop (MULT_EXPR, size, l);
4586 if (condition)
4587 size = fold_build3 (COND_EXPR, sizetype, condition,
4588 size, size_zero_node);
4590 else
4591 size = size_binop (MULT_EXPR, size, l);
4594 if (!processing_template_decl)
4596 if (side_effects)
4597 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4598 OMP_CLAUSE_DECL (c) = first;
4599 OMP_CLAUSE_SIZE (c) = size;
4600 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4601 return false;
4602 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4603 OMP_CLAUSE_MAP);
4604 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4605 if (!cxx_mark_addressable (t))
4606 return false;
4607 OMP_CLAUSE_DECL (c2) = t;
4608 t = build_fold_addr_expr (first);
4609 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4610 ptrdiff_type_node, t);
4611 tree ptr = OMP_CLAUSE_DECL (c2);
4612 ptr = convert_from_reference (ptr);
4613 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4614 ptr = build_fold_addr_expr (ptr);
4615 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4616 ptrdiff_type_node, t,
4617 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4618 ptrdiff_type_node, ptr));
4619 OMP_CLAUSE_SIZE (c2) = t;
4620 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4621 OMP_CLAUSE_CHAIN (c) = c2;
4622 ptr = OMP_CLAUSE_DECL (c2);
4623 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4624 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4626 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4627 OMP_CLAUSE_MAP);
4628 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4629 OMP_CLAUSE_DECL (c3) = ptr;
4630 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4631 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4632 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4633 OMP_CLAUSE_CHAIN (c2) = c3;
4637 return false;
4640 /* Return identifier to look up for omp declare reduction. */
4642 tree
4643 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4645 const char *p = NULL;
4646 const char *m = NULL;
4647 switch (reduction_code)
4649 case PLUS_EXPR:
4650 case MULT_EXPR:
4651 case MINUS_EXPR:
4652 case BIT_AND_EXPR:
4653 case BIT_XOR_EXPR:
4654 case BIT_IOR_EXPR:
4655 case TRUTH_ANDIF_EXPR:
4656 case TRUTH_ORIF_EXPR:
4657 reduction_id = ansi_opname (reduction_code);
4658 break;
4659 case MIN_EXPR:
4660 p = "min";
4661 break;
4662 case MAX_EXPR:
4663 p = "max";
4664 break;
4665 default:
4666 break;
4669 if (p == NULL)
4671 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4672 return error_mark_node;
4673 p = IDENTIFIER_POINTER (reduction_id);
4676 if (type != NULL_TREE)
4677 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4679 const char prefix[] = "omp declare reduction ";
4680 size_t lenp = sizeof (prefix);
4681 if (strncmp (p, prefix, lenp - 1) == 0)
4682 lenp = 1;
4683 size_t len = strlen (p);
4684 size_t lenm = m ? strlen (m) + 1 : 0;
4685 char *name = XALLOCAVEC (char, lenp + len + lenm);
4686 if (lenp > 1)
4687 memcpy (name, prefix, lenp - 1);
4688 memcpy (name + lenp - 1, p, len + 1);
4689 if (m)
4691 name[lenp + len - 1] = '~';
4692 memcpy (name + lenp + len, m, lenm);
4694 return get_identifier (name);
4697 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4698 FUNCTION_DECL or NULL_TREE if not found. */
4700 static tree
4701 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4702 vec<tree> *ambiguousp)
4704 tree orig_id = id;
4705 tree baselink = NULL_TREE;
4706 if (identifier_p (id))
4708 cp_id_kind idk;
4709 bool nonint_cst_expression_p;
4710 const char *error_msg;
4711 id = omp_reduction_id (ERROR_MARK, id, type);
4712 tree decl = lookup_name (id);
4713 if (decl == NULL_TREE)
4714 decl = error_mark_node;
4715 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4716 &nonint_cst_expression_p, false, true, false,
4717 false, &error_msg, loc);
4718 if (idk == CP_ID_KIND_UNQUALIFIED
4719 && identifier_p (id))
4721 vec<tree, va_gc> *args = NULL;
4722 vec_safe_push (args, build_reference_type (type));
4723 id = perform_koenig_lookup (id, args, tf_none);
4726 else if (TREE_CODE (id) == SCOPE_REF)
4727 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4728 omp_reduction_id (ERROR_MARK,
4729 TREE_OPERAND (id, 1),
4730 type),
4731 false, false);
4732 tree fns = id;
4733 if (id && is_overloaded_fn (id))
4734 id = get_fns (id);
4735 for (; id; id = OVL_NEXT (id))
4737 tree fndecl = OVL_CURRENT (id);
4738 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4740 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4741 if (same_type_p (TREE_TYPE (argtype), type))
4742 break;
4745 if (id && BASELINK_P (fns))
4747 if (baselinkp)
4748 *baselinkp = fns;
4749 else
4750 baselink = fns;
4752 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4754 vec<tree> ambiguous = vNULL;
4755 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4756 unsigned int ix;
4757 if (ambiguousp == NULL)
4758 ambiguousp = &ambiguous;
4759 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4761 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4762 baselinkp ? baselinkp : &baselink,
4763 ambiguousp);
4764 if (id == NULL_TREE)
4765 continue;
4766 if (!ambiguousp->is_empty ())
4767 ambiguousp->safe_push (id);
4768 else if (ret != NULL_TREE)
4770 ambiguousp->safe_push (ret);
4771 ambiguousp->safe_push (id);
4772 ret = NULL_TREE;
4774 else
4775 ret = id;
4777 if (ambiguousp != &ambiguous)
4778 return ret;
4779 if (!ambiguous.is_empty ())
4781 const char *str = _("candidates are:");
4782 unsigned int idx;
4783 tree udr;
4784 error_at (loc, "user defined reduction lookup is ambiguous");
4785 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4787 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4788 if (idx == 0)
4789 str = get_spaces (str);
4791 ambiguous.release ();
4792 ret = error_mark_node;
4793 baselink = NULL_TREE;
4795 id = ret;
4797 if (id && baselink)
4798 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4799 id, id, tf_warning_or_error);
4800 return id;
4803 /* Helper function for cp_parser_omp_declare_reduction_exprs
4804 and tsubst_omp_udr.
4805 Remove CLEANUP_STMT for data (omp_priv variable).
4806 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4807 DECL_EXPR. */
4809 tree
4810 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4812 if (TYPE_P (*tp))
4813 *walk_subtrees = 0;
4814 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4815 *tp = CLEANUP_BODY (*tp);
4816 else if (TREE_CODE (*tp) == DECL_EXPR)
4818 tree decl = DECL_EXPR_DECL (*tp);
4819 if (!processing_template_decl
4820 && decl == (tree) data
4821 && DECL_INITIAL (decl)
4822 && DECL_INITIAL (decl) != error_mark_node)
4824 tree list = NULL_TREE;
4825 append_to_statement_list_force (*tp, &list);
4826 tree init_expr = build2 (INIT_EXPR, void_type_node,
4827 decl, DECL_INITIAL (decl));
4828 DECL_INITIAL (decl) = NULL_TREE;
4829 append_to_statement_list_force (init_expr, &list);
4830 *tp = list;
4833 return NULL_TREE;
4836 /* Data passed from cp_check_omp_declare_reduction to
4837 cp_check_omp_declare_reduction_r. */
4839 struct cp_check_omp_declare_reduction_data
4841 location_t loc;
4842 tree stmts[7];
4843 bool combiner_p;
4846 /* Helper function for cp_check_omp_declare_reduction, called via
4847 cp_walk_tree. */
4849 static tree
4850 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4852 struct cp_check_omp_declare_reduction_data *udr_data
4853 = (struct cp_check_omp_declare_reduction_data *) data;
4854 if (SSA_VAR_P (*tp)
4855 && !DECL_ARTIFICIAL (*tp)
4856 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4857 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4859 location_t loc = udr_data->loc;
4860 if (udr_data->combiner_p)
4861 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4862 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4863 *tp);
4864 else
4865 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4866 "to variable %qD which is not %<omp_priv%> nor "
4867 "%<omp_orig%>",
4868 *tp);
4869 return *tp;
4871 return NULL_TREE;
4874 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4876 void
4877 cp_check_omp_declare_reduction (tree udr)
4879 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4880 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4881 type = TREE_TYPE (type);
4882 int i;
4883 location_t loc = DECL_SOURCE_LOCATION (udr);
4885 if (type == error_mark_node)
4886 return;
4887 if (ARITHMETIC_TYPE_P (type))
4889 static enum tree_code predef_codes[]
4890 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4891 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4892 for (i = 0; i < 8; i++)
4894 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4895 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4896 const char *n2 = IDENTIFIER_POINTER (id);
4897 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4898 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4899 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4900 break;
4903 if (i == 8
4904 && TREE_CODE (type) != COMPLEX_EXPR)
4906 const char prefix_minmax[] = "omp declare reduction m";
4907 size_t prefix_size = sizeof (prefix_minmax) - 1;
4908 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4909 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4910 prefix_minmax, prefix_size) == 0
4911 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4912 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4913 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4914 i = 0;
4916 if (i < 8)
4918 error_at (loc, "predeclared arithmetic type %qT in "
4919 "%<#pragma omp declare reduction%>", type);
4920 return;
4923 else if (TREE_CODE (type) == FUNCTION_TYPE
4924 || TREE_CODE (type) == METHOD_TYPE
4925 || TREE_CODE (type) == ARRAY_TYPE)
4927 error_at (loc, "function or array type %qT in "
4928 "%<#pragma omp declare reduction%>", type);
4929 return;
4931 else if (TREE_CODE (type) == REFERENCE_TYPE)
4933 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4934 type);
4935 return;
4937 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4939 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4940 "%<#pragma omp declare reduction%>", type);
4941 return;
4944 tree body = DECL_SAVED_TREE (udr);
4945 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4946 return;
4948 tree_stmt_iterator tsi;
4949 struct cp_check_omp_declare_reduction_data data;
4950 memset (data.stmts, 0, sizeof data.stmts);
4951 for (i = 0, tsi = tsi_start (body);
4952 i < 7 && !tsi_end_p (tsi);
4953 i++, tsi_next (&tsi))
4954 data.stmts[i] = tsi_stmt (tsi);
4955 data.loc = loc;
4956 gcc_assert (tsi_end_p (tsi));
4957 if (i >= 3)
4959 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4960 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4961 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4962 return;
4963 data.combiner_p = true;
4964 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4965 &data, NULL))
4966 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4968 if (i >= 6)
4970 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4971 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4972 data.combiner_p = false;
4973 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4974 &data, NULL)
4975 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4976 cp_check_omp_declare_reduction_r, &data, NULL))
4977 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4978 if (i == 7)
4979 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4983 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4984 an inline call. But, remap
4985 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4986 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4988 static tree
4989 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4990 tree decl, tree placeholder)
4992 copy_body_data id;
4993 hash_map<tree, tree> decl_map;
4995 decl_map.put (omp_decl1, placeholder);
4996 decl_map.put (omp_decl2, decl);
4997 memset (&id, 0, sizeof (id));
4998 id.src_fn = DECL_CONTEXT (omp_decl1);
4999 id.dst_fn = current_function_decl;
5000 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5001 id.decl_map = &decl_map;
5003 id.copy_decl = copy_decl_no_change;
5004 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5005 id.transform_new_cfg = true;
5006 id.transform_return_to_modify = false;
5007 id.transform_lang_insert_block = NULL;
5008 id.eh_lp_nr = 0;
5009 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5010 return stmt;
5013 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5014 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5016 static tree
5017 find_omp_placeholder_r (tree *tp, int *, void *data)
5019 if (*tp == (tree) data)
5020 return *tp;
5021 return NULL_TREE;
5024 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5025 Return true if there is some error and the clause should be removed. */
5027 static bool
5028 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5030 tree t = OMP_CLAUSE_DECL (c);
5031 bool predefined = false;
5032 tree type = TREE_TYPE (t);
5033 if (TREE_CODE (type) == REFERENCE_TYPE)
5034 type = TREE_TYPE (type);
5035 if (type == error_mark_node)
5036 return true;
5037 else if (ARITHMETIC_TYPE_P (type))
5038 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5040 case PLUS_EXPR:
5041 case MULT_EXPR:
5042 case MINUS_EXPR:
5043 predefined = true;
5044 break;
5045 case MIN_EXPR:
5046 case MAX_EXPR:
5047 if (TREE_CODE (type) == COMPLEX_TYPE)
5048 break;
5049 predefined = true;
5050 break;
5051 case BIT_AND_EXPR:
5052 case BIT_IOR_EXPR:
5053 case BIT_XOR_EXPR:
5054 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5055 break;
5056 predefined = true;
5057 break;
5058 case TRUTH_ANDIF_EXPR:
5059 case TRUTH_ORIF_EXPR:
5060 if (FLOAT_TYPE_P (type))
5061 break;
5062 predefined = true;
5063 break;
5064 default:
5065 break;
5067 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5069 error ("%qE has invalid type for %<reduction%>", t);
5070 return true;
5072 else if (!processing_template_decl)
5074 t = require_complete_type (t);
5075 if (t == error_mark_node)
5076 return true;
5077 OMP_CLAUSE_DECL (c) = t;
5080 if (predefined)
5082 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5083 return false;
5085 else if (processing_template_decl)
5086 return false;
5088 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5090 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5091 if (TREE_CODE (type) == REFERENCE_TYPE)
5092 type = TREE_TYPE (type);
5093 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5094 if (id == NULL_TREE)
5095 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5096 NULL_TREE, NULL_TREE);
5097 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5098 if (id)
5100 if (id == error_mark_node)
5101 return true;
5102 id = OVL_CURRENT (id);
5103 mark_used (id);
5104 tree body = DECL_SAVED_TREE (id);
5105 if (TREE_CODE (body) == STATEMENT_LIST)
5107 tree_stmt_iterator tsi;
5108 tree placeholder = NULL_TREE;
5109 int i;
5110 tree stmts[7];
5111 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5112 atype = TREE_TYPE (atype);
5113 bool need_static_cast = !same_type_p (type, atype);
5114 memset (stmts, 0, sizeof stmts);
5115 for (i = 0, tsi = tsi_start (body);
5116 i < 7 && !tsi_end_p (tsi);
5117 i++, tsi_next (&tsi))
5118 stmts[i] = tsi_stmt (tsi);
5119 gcc_assert (tsi_end_p (tsi));
5121 if (i >= 3)
5123 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5124 && TREE_CODE (stmts[1]) == DECL_EXPR);
5125 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5126 DECL_ARTIFICIAL (placeholder) = 1;
5127 DECL_IGNORED_P (placeholder) = 1;
5128 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5129 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5130 cxx_mark_addressable (placeholder);
5131 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5132 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5133 != REFERENCE_TYPE)
5134 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5135 tree omp_out = placeholder;
5136 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5137 if (need_static_cast)
5139 tree rtype = build_reference_type (atype);
5140 omp_out = build_static_cast (rtype, omp_out,
5141 tf_warning_or_error);
5142 omp_in = build_static_cast (rtype, omp_in,
5143 tf_warning_or_error);
5144 if (omp_out == error_mark_node || omp_in == error_mark_node)
5145 return true;
5146 omp_out = convert_from_reference (omp_out);
5147 omp_in = convert_from_reference (omp_in);
5149 OMP_CLAUSE_REDUCTION_MERGE (c)
5150 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5151 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5153 if (i >= 6)
5155 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5156 && TREE_CODE (stmts[4]) == DECL_EXPR);
5157 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5158 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5159 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5160 cxx_mark_addressable (placeholder);
5161 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5162 tree omp_orig = placeholder;
5163 if (need_static_cast)
5165 if (i == 7)
5167 error_at (OMP_CLAUSE_LOCATION (c),
5168 "user defined reduction with constructor "
5169 "initializer for base class %qT", atype);
5170 return true;
5172 tree rtype = build_reference_type (atype);
5173 omp_priv = build_static_cast (rtype, omp_priv,
5174 tf_warning_or_error);
5175 omp_orig = build_static_cast (rtype, omp_orig,
5176 tf_warning_or_error);
5177 if (omp_priv == error_mark_node
5178 || omp_orig == error_mark_node)
5179 return true;
5180 omp_priv = convert_from_reference (omp_priv);
5181 omp_orig = convert_from_reference (omp_orig);
5183 if (i == 6)
5184 *need_default_ctor = true;
5185 OMP_CLAUSE_REDUCTION_INIT (c)
5186 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5187 DECL_EXPR_DECL (stmts[3]),
5188 omp_priv, omp_orig);
5189 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5190 find_omp_placeholder_r, placeholder, NULL))
5191 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5193 else if (i >= 3)
5195 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5196 *need_default_ctor = true;
5197 else
5199 tree init;
5200 tree v = convert_from_reference (t);
5201 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5202 init = build_constructor (TREE_TYPE (v), NULL);
5203 else
5204 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5205 OMP_CLAUSE_REDUCTION_INIT (c)
5206 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5211 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5212 *need_dtor = true;
5213 else
5215 error ("user defined reduction not found for %qD", t);
5216 return true;
5218 return false;
5221 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5222 Remove any elements from the list that are invalid. */
5224 tree
5225 finish_omp_clauses (tree clauses)
5227 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5228 bitmap_head aligned_head;
5229 tree c, t, *pc;
5230 bool branch_seen = false;
5231 bool copyprivate_seen = false;
5233 bitmap_obstack_initialize (NULL);
5234 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5235 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5236 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5237 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5239 for (pc = &clauses, c = clauses; c ; c = *pc)
5241 bool remove = false;
5243 switch (OMP_CLAUSE_CODE (c))
5245 case OMP_CLAUSE_SHARED:
5246 goto check_dup_generic;
5247 case OMP_CLAUSE_PRIVATE:
5248 goto check_dup_generic;
5249 case OMP_CLAUSE_REDUCTION:
5250 goto check_dup_generic;
5251 case OMP_CLAUSE_COPYPRIVATE:
5252 copyprivate_seen = true;
5253 goto check_dup_generic;
5254 case OMP_CLAUSE_COPYIN:
5255 goto check_dup_generic;
5256 case OMP_CLAUSE_LINEAR:
5257 t = OMP_CLAUSE_DECL (c);
5258 if (!type_dependent_expression_p (t)
5259 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5260 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5262 error ("linear clause applied to non-integral non-pointer "
5263 "variable with %qT type", TREE_TYPE (t));
5264 remove = true;
5265 break;
5267 t = OMP_CLAUSE_LINEAR_STEP (c);
5268 if (t == NULL_TREE)
5269 t = integer_one_node;
5270 if (t == error_mark_node)
5272 remove = true;
5273 break;
5275 else if (!type_dependent_expression_p (t)
5276 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5278 error ("linear step expression must be integral");
5279 remove = true;
5280 break;
5282 else
5284 t = mark_rvalue_use (t);
5285 if (!processing_template_decl)
5287 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5288 t = maybe_constant_value (t);
5289 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5290 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5291 == POINTER_TYPE)
5293 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5294 OMP_CLAUSE_DECL (c), t);
5295 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5296 MINUS_EXPR, sizetype, t,
5297 OMP_CLAUSE_DECL (c));
5298 if (t == error_mark_node)
5300 remove = true;
5301 break;
5304 else
5305 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5307 OMP_CLAUSE_LINEAR_STEP (c) = t;
5309 goto check_dup_generic;
5310 check_dup_generic:
5311 t = OMP_CLAUSE_DECL (c);
5312 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5314 if (processing_template_decl)
5315 break;
5316 if (DECL_P (t))
5317 error ("%qD is not a variable in clause %qs", t,
5318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5319 else
5320 error ("%qE is not a variable in clause %qs", t,
5321 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5322 remove = true;
5324 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5325 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5326 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5328 error ("%qD appears more than once in data clauses", t);
5329 remove = true;
5331 else
5332 bitmap_set_bit (&generic_head, DECL_UID (t));
5333 break;
5335 case OMP_CLAUSE_FIRSTPRIVATE:
5336 t = OMP_CLAUSE_DECL (c);
5337 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5339 if (processing_template_decl)
5340 break;
5341 if (DECL_P (t))
5342 error ("%qD is not a variable in clause %<firstprivate%>", t);
5343 else
5344 error ("%qE is not a variable in clause %<firstprivate%>", t);
5345 remove = true;
5347 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5348 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5350 error ("%qD appears more than once in data clauses", t);
5351 remove = true;
5353 else
5354 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5355 break;
5357 case OMP_CLAUSE_LASTPRIVATE:
5358 t = OMP_CLAUSE_DECL (c);
5359 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5361 if (processing_template_decl)
5362 break;
5363 if (DECL_P (t))
5364 error ("%qD is not a variable in clause %<lastprivate%>", t);
5365 else
5366 error ("%qE is not a variable in clause %<lastprivate%>", t);
5367 remove = true;
5369 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5370 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5372 error ("%qD appears more than once in data clauses", t);
5373 remove = true;
5375 else
5376 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5377 break;
5379 case OMP_CLAUSE_IF:
5380 t = OMP_CLAUSE_IF_EXPR (c);
5381 t = maybe_convert_cond (t);
5382 if (t == error_mark_node)
5383 remove = true;
5384 else if (!processing_template_decl)
5385 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5386 OMP_CLAUSE_IF_EXPR (c) = t;
5387 break;
5389 case OMP_CLAUSE_FINAL:
5390 t = OMP_CLAUSE_FINAL_EXPR (c);
5391 t = maybe_convert_cond (t);
5392 if (t == error_mark_node)
5393 remove = true;
5394 else if (!processing_template_decl)
5395 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5396 OMP_CLAUSE_FINAL_EXPR (c) = t;
5397 break;
5399 case OMP_CLAUSE_NUM_THREADS:
5400 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5401 if (t == error_mark_node)
5402 remove = true;
5403 else if (!type_dependent_expression_p (t)
5404 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5406 error ("num_threads expression must be integral");
5407 remove = true;
5409 else
5411 t = mark_rvalue_use (t);
5412 if (!processing_template_decl)
5413 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5414 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5416 break;
5418 case OMP_CLAUSE_SCHEDULE:
5419 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5420 if (t == NULL)
5422 else if (t == error_mark_node)
5423 remove = true;
5424 else if (!type_dependent_expression_p (t)
5425 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5427 error ("schedule chunk size expression must be integral");
5428 remove = true;
5430 else
5432 t = mark_rvalue_use (t);
5433 if (!processing_template_decl)
5434 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5435 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5437 break;
5439 case OMP_CLAUSE_SIMDLEN:
5440 case OMP_CLAUSE_SAFELEN:
5441 t = OMP_CLAUSE_OPERAND (c, 0);
5442 if (t == error_mark_node)
5443 remove = true;
5444 else if (!type_dependent_expression_p (t)
5445 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5447 error ("%qs length expression must be integral",
5448 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5449 remove = true;
5451 else
5453 t = mark_rvalue_use (t);
5454 t = maybe_constant_value (t);
5455 if (!processing_template_decl)
5457 if (TREE_CODE (t) != INTEGER_CST
5458 || tree_int_cst_sgn (t) != 1)
5460 error ("%qs length expression must be positive constant"
5461 " integer expression",
5462 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5463 remove = true;
5466 OMP_CLAUSE_OPERAND (c, 0) = t;
5468 break;
5470 case OMP_CLAUSE_NUM_TEAMS:
5471 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5472 if (t == error_mark_node)
5473 remove = true;
5474 else if (!type_dependent_expression_p (t)
5475 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5477 error ("%<num_teams%> expression must be integral");
5478 remove = true;
5480 else
5482 t = mark_rvalue_use (t);
5483 if (!processing_template_decl)
5484 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5485 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5487 break;
5489 case OMP_CLAUSE_THREAD_LIMIT:
5490 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5491 if (t == error_mark_node)
5492 remove = true;
5493 else if (!type_dependent_expression_p (t)
5494 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5496 error ("%<thread_limit%> expression must be integral");
5497 remove = true;
5499 else
5501 t = mark_rvalue_use (t);
5502 if (!processing_template_decl)
5503 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5504 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5506 break;
5508 case OMP_CLAUSE_DEVICE:
5509 t = OMP_CLAUSE_DEVICE_ID (c);
5510 if (t == error_mark_node)
5511 remove = true;
5512 else if (!type_dependent_expression_p (t)
5513 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5515 error ("%<device%> id must be integral");
5516 remove = true;
5518 else
5520 t = mark_rvalue_use (t);
5521 if (!processing_template_decl)
5522 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5523 OMP_CLAUSE_DEVICE_ID (c) = t;
5525 break;
5527 case OMP_CLAUSE_DIST_SCHEDULE:
5528 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5529 if (t == NULL)
5531 else if (t == error_mark_node)
5532 remove = true;
5533 else if (!type_dependent_expression_p (t)
5534 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5536 error ("%<dist_schedule%> chunk size expression must be "
5537 "integral");
5538 remove = true;
5540 else
5542 t = mark_rvalue_use (t);
5543 if (!processing_template_decl)
5544 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5545 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5547 break;
5549 case OMP_CLAUSE_ALIGNED:
5550 t = OMP_CLAUSE_DECL (c);
5551 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5553 if (processing_template_decl)
5554 break;
5555 if (DECL_P (t))
5556 error ("%qD is not a variable in %<aligned%> clause", t);
5557 else
5558 error ("%qE is not a variable in %<aligned%> clause", t);
5559 remove = true;
5561 else if (!type_dependent_expression_p (t)
5562 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5563 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5564 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5565 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5566 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5567 != ARRAY_TYPE))))
5569 error_at (OMP_CLAUSE_LOCATION (c),
5570 "%qE in %<aligned%> clause is neither a pointer nor "
5571 "an array nor a reference to pointer or array", t);
5572 remove = true;
5574 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5576 error ("%qD appears more than once in %<aligned%> clauses", t);
5577 remove = true;
5579 else
5580 bitmap_set_bit (&aligned_head, DECL_UID (t));
5581 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5582 if (t == error_mark_node)
5583 remove = true;
5584 else if (t == NULL_TREE)
5585 break;
5586 else if (!type_dependent_expression_p (t)
5587 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5589 error ("%<aligned%> clause alignment expression must "
5590 "be integral");
5591 remove = true;
5593 else
5595 t = mark_rvalue_use (t);
5596 t = maybe_constant_value (t);
5597 if (!processing_template_decl)
5599 if (TREE_CODE (t) != INTEGER_CST
5600 || tree_int_cst_sgn (t) != 1)
5602 error ("%<aligned%> clause alignment expression must be "
5603 "positive constant integer expression");
5604 remove = true;
5607 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5609 break;
5611 case OMP_CLAUSE_DEPEND:
5612 t = OMP_CLAUSE_DECL (c);
5613 if (TREE_CODE (t) == TREE_LIST)
5615 if (handle_omp_array_sections (c))
5616 remove = true;
5617 break;
5619 if (t == error_mark_node)
5620 remove = true;
5621 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5623 if (processing_template_decl)
5624 break;
5625 if (DECL_P (t))
5626 error ("%qD is not a variable in %<depend%> clause", t);
5627 else
5628 error ("%qE is not a variable in %<depend%> clause", t);
5629 remove = true;
5631 else if (!processing_template_decl
5632 && !cxx_mark_addressable (t))
5633 remove = true;
5634 break;
5636 case OMP_CLAUSE_MAP:
5637 case OMP_CLAUSE_TO:
5638 case OMP_CLAUSE_FROM:
5639 t = OMP_CLAUSE_DECL (c);
5640 if (TREE_CODE (t) == TREE_LIST)
5642 if (handle_omp_array_sections (c))
5643 remove = true;
5644 else
5646 t = OMP_CLAUSE_DECL (c);
5647 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5649 error_at (OMP_CLAUSE_LOCATION (c),
5650 "array section does not have mappable type "
5651 "in %qs clause",
5652 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5653 remove = true;
5656 break;
5658 if (t == error_mark_node)
5659 remove = true;
5660 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5662 if (processing_template_decl)
5663 break;
5664 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5665 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5666 break;
5667 if (DECL_P (t))
5668 error ("%qD is not a variable in %qs clause", t,
5669 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5670 else
5671 error ("%qE is not a variable in %qs clause", t,
5672 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5673 remove = true;
5675 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5677 error ("%qD is threadprivate variable in %qs clause", t,
5678 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5679 remove = true;
5681 else if (!processing_template_decl
5682 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5683 && !cxx_mark_addressable (t))
5684 remove = true;
5685 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5686 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5687 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5688 == REFERENCE_TYPE)
5689 ? TREE_TYPE (TREE_TYPE (t))
5690 : TREE_TYPE (t)))
5692 error_at (OMP_CLAUSE_LOCATION (c),
5693 "%qD does not have a mappable type in %qs clause", t,
5694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5695 remove = true;
5697 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5699 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5700 error ("%qD appears more than once in motion clauses", t);
5701 else
5702 error ("%qD appears more than once in map clauses", t);
5703 remove = true;
5705 else
5706 bitmap_set_bit (&generic_head, DECL_UID (t));
5707 break;
5709 case OMP_CLAUSE_UNIFORM:
5710 t = OMP_CLAUSE_DECL (c);
5711 if (TREE_CODE (t) != PARM_DECL)
5713 if (processing_template_decl)
5714 break;
5715 if (DECL_P (t))
5716 error ("%qD is not an argument in %<uniform%> clause", t);
5717 else
5718 error ("%qE is not an argument in %<uniform%> clause", t);
5719 remove = true;
5720 break;
5722 goto check_dup_generic;
5724 case OMP_CLAUSE_NOWAIT:
5725 case OMP_CLAUSE_ORDERED:
5726 case OMP_CLAUSE_DEFAULT:
5727 case OMP_CLAUSE_UNTIED:
5728 case OMP_CLAUSE_COLLAPSE:
5729 case OMP_CLAUSE_MERGEABLE:
5730 case OMP_CLAUSE_PARALLEL:
5731 case OMP_CLAUSE_FOR:
5732 case OMP_CLAUSE_SECTIONS:
5733 case OMP_CLAUSE_TASKGROUP:
5734 case OMP_CLAUSE_PROC_BIND:
5735 break;
5737 case OMP_CLAUSE_INBRANCH:
5738 case OMP_CLAUSE_NOTINBRANCH:
5739 if (branch_seen)
5741 error ("%<inbranch%> clause is incompatible with "
5742 "%<notinbranch%>");
5743 remove = true;
5745 branch_seen = true;
5746 break;
5748 default:
5749 gcc_unreachable ();
5752 if (remove)
5753 *pc = OMP_CLAUSE_CHAIN (c);
5754 else
5755 pc = &OMP_CLAUSE_CHAIN (c);
5758 for (pc = &clauses, c = clauses; c ; c = *pc)
5760 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5761 bool remove = false;
5762 bool need_complete_non_reference = false;
5763 bool need_default_ctor = false;
5764 bool need_copy_ctor = false;
5765 bool need_copy_assignment = false;
5766 bool need_implicitly_determined = false;
5767 bool need_dtor = false;
5768 tree type, inner_type;
5770 switch (c_kind)
5772 case OMP_CLAUSE_SHARED:
5773 need_implicitly_determined = true;
5774 break;
5775 case OMP_CLAUSE_PRIVATE:
5776 need_complete_non_reference = true;
5777 need_default_ctor = true;
5778 need_dtor = true;
5779 need_implicitly_determined = true;
5780 break;
5781 case OMP_CLAUSE_FIRSTPRIVATE:
5782 need_complete_non_reference = true;
5783 need_copy_ctor = true;
5784 need_dtor = true;
5785 need_implicitly_determined = true;
5786 break;
5787 case OMP_CLAUSE_LASTPRIVATE:
5788 need_complete_non_reference = true;
5789 need_copy_assignment = true;
5790 need_implicitly_determined = true;
5791 break;
5792 case OMP_CLAUSE_REDUCTION:
5793 need_implicitly_determined = true;
5794 break;
5795 case OMP_CLAUSE_COPYPRIVATE:
5796 need_copy_assignment = true;
5797 break;
5798 case OMP_CLAUSE_COPYIN:
5799 need_copy_assignment = true;
5800 break;
5801 case OMP_CLAUSE_NOWAIT:
5802 if (copyprivate_seen)
5804 error_at (OMP_CLAUSE_LOCATION (c),
5805 "%<nowait%> clause must not be used together "
5806 "with %<copyprivate%>");
5807 *pc = OMP_CLAUSE_CHAIN (c);
5808 continue;
5810 /* FALLTHRU */
5811 default:
5812 pc = &OMP_CLAUSE_CHAIN (c);
5813 continue;
5816 t = OMP_CLAUSE_DECL (c);
5817 if (processing_template_decl
5818 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5820 pc = &OMP_CLAUSE_CHAIN (c);
5821 continue;
5824 switch (c_kind)
5826 case OMP_CLAUSE_LASTPRIVATE:
5827 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5829 need_default_ctor = true;
5830 need_dtor = true;
5832 break;
5834 case OMP_CLAUSE_REDUCTION:
5835 if (finish_omp_reduction_clause (c, &need_default_ctor,
5836 &need_dtor))
5837 remove = true;
5838 else
5839 t = OMP_CLAUSE_DECL (c);
5840 break;
5842 case OMP_CLAUSE_COPYIN:
5843 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5845 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5846 remove = true;
5848 break;
5850 default:
5851 break;
5854 if (need_complete_non_reference || need_copy_assignment)
5856 t = require_complete_type (t);
5857 if (t == error_mark_node)
5858 remove = true;
5859 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5860 && need_complete_non_reference)
5862 error ("%qE has reference type for %qs", t,
5863 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5864 remove = true;
5867 if (need_implicitly_determined)
5869 const char *share_name = NULL;
5871 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5872 share_name = "threadprivate";
5873 else switch (cxx_omp_predetermined_sharing (t))
5875 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5876 break;
5877 case OMP_CLAUSE_DEFAULT_SHARED:
5878 /* const vars may be specified in firstprivate clause. */
5879 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5880 && cxx_omp_const_qual_no_mutable (t))
5881 break;
5882 share_name = "shared";
5883 break;
5884 case OMP_CLAUSE_DEFAULT_PRIVATE:
5885 share_name = "private";
5886 break;
5887 default:
5888 gcc_unreachable ();
5890 if (share_name)
5892 error ("%qE is predetermined %qs for %qs",
5893 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5894 remove = true;
5898 /* We're interested in the base element, not arrays. */
5899 inner_type = type = TREE_TYPE (t);
5900 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5901 inner_type = TREE_TYPE (inner_type);
5903 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5904 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5905 inner_type = TREE_TYPE (inner_type);
5907 /* Check for special function availability by building a call to one.
5908 Save the results, because later we won't be in the right context
5909 for making these queries. */
5910 if (CLASS_TYPE_P (inner_type)
5911 && COMPLETE_TYPE_P (inner_type)
5912 && (need_default_ctor || need_copy_ctor
5913 || need_copy_assignment || need_dtor)
5914 && !type_dependent_expression_p (t)
5915 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5916 need_copy_ctor, need_copy_assignment,
5917 need_dtor))
5918 remove = true;
5920 if (remove)
5921 *pc = OMP_CLAUSE_CHAIN (c);
5922 else
5923 pc = &OMP_CLAUSE_CHAIN (c);
5926 bitmap_obstack_release (NULL);
5927 return clauses;
5930 /* For all variables in the tree_list VARS, mark them as thread local. */
5932 void
5933 finish_omp_threadprivate (tree vars)
5935 tree t;
5937 /* Mark every variable in VARS to be assigned thread local storage. */
5938 for (t = vars; t; t = TREE_CHAIN (t))
5940 tree v = TREE_PURPOSE (t);
5942 if (error_operand_p (v))
5944 else if (!VAR_P (v))
5945 error ("%<threadprivate%> %qD is not file, namespace "
5946 "or block scope variable", v);
5947 /* If V had already been marked threadprivate, it doesn't matter
5948 whether it had been used prior to this point. */
5949 else if (TREE_USED (v)
5950 && (DECL_LANG_SPECIFIC (v) == NULL
5951 || !CP_DECL_THREADPRIVATE_P (v)))
5952 error ("%qE declared %<threadprivate%> after first use", v);
5953 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5954 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5955 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5956 error ("%<threadprivate%> %qE has incomplete type", v);
5957 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5958 && CP_DECL_CONTEXT (v) != current_class_type)
5959 error ("%<threadprivate%> %qE directive not "
5960 "in %qT definition", v, CP_DECL_CONTEXT (v));
5961 else
5963 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5964 if (DECL_LANG_SPECIFIC (v) == NULL)
5966 retrofit_lang_decl (v);
5968 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5969 after the allocation of the lang_decl structure. */
5970 if (DECL_DISCRIMINATOR_P (v))
5971 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5974 if (! DECL_THREAD_LOCAL_P (v))
5976 set_decl_tls_model (v, decl_default_tls_model (v));
5977 /* If rtl has been already set for this var, call
5978 make_decl_rtl once again, so that encode_section_info
5979 has a chance to look at the new decl flags. */
5980 if (DECL_RTL_SET_P (v))
5981 make_decl_rtl (v);
5983 CP_DECL_THREADPRIVATE_P (v) = 1;
5988 /* Build an OpenMP structured block. */
5990 tree
5991 begin_omp_structured_block (void)
5993 return do_pushlevel (sk_omp);
5996 tree
5997 finish_omp_structured_block (tree block)
5999 return do_poplevel (block);
6002 /* Similarly, except force the retention of the BLOCK. */
6004 tree
6005 begin_omp_parallel (void)
6007 keep_next_level (true);
6008 return begin_omp_structured_block ();
6011 tree
6012 finish_omp_parallel (tree clauses, tree body)
6014 tree stmt;
6016 body = finish_omp_structured_block (body);
6018 stmt = make_node (OMP_PARALLEL);
6019 TREE_TYPE (stmt) = void_type_node;
6020 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6021 OMP_PARALLEL_BODY (stmt) = body;
6023 return add_stmt (stmt);
6026 tree
6027 begin_omp_task (void)
6029 keep_next_level (true);
6030 return begin_omp_structured_block ();
6033 tree
6034 finish_omp_task (tree clauses, tree body)
6036 tree stmt;
6038 body = finish_omp_structured_block (body);
6040 stmt = make_node (OMP_TASK);
6041 TREE_TYPE (stmt) = void_type_node;
6042 OMP_TASK_CLAUSES (stmt) = clauses;
6043 OMP_TASK_BODY (stmt) = body;
6045 return add_stmt (stmt);
6048 /* Helper function for finish_omp_for. Convert Ith random access iterator
6049 into integral iterator. Return FALSE if successful. */
6051 static bool
6052 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6053 tree condv, tree incrv, tree *body,
6054 tree *pre_body, tree clauses)
6056 tree diff, iter_init, iter_incr = NULL, last;
6057 tree incr_var = NULL, orig_pre_body, orig_body, c;
6058 tree decl = TREE_VEC_ELT (declv, i);
6059 tree init = TREE_VEC_ELT (initv, i);
6060 tree cond = TREE_VEC_ELT (condv, i);
6061 tree incr = TREE_VEC_ELT (incrv, i);
6062 tree iter = decl;
6063 location_t elocus = locus;
6065 if (init && EXPR_HAS_LOCATION (init))
6066 elocus = EXPR_LOCATION (init);
6068 switch (TREE_CODE (cond))
6070 case GT_EXPR:
6071 case GE_EXPR:
6072 case LT_EXPR:
6073 case LE_EXPR:
6074 if (TREE_OPERAND (cond, 1) == iter)
6075 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6076 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6077 if (TREE_OPERAND (cond, 0) != iter)
6078 cond = error_mark_node;
6079 else
6081 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6082 TREE_CODE (cond),
6083 iter, ERROR_MARK,
6084 TREE_OPERAND (cond, 1), ERROR_MARK,
6085 NULL, tf_warning_or_error);
6086 if (error_operand_p (tem))
6087 return true;
6089 break;
6090 default:
6091 cond = error_mark_node;
6092 break;
6094 if (cond == error_mark_node)
6096 error_at (elocus, "invalid controlling predicate");
6097 return true;
6099 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6100 ERROR_MARK, iter, ERROR_MARK, NULL,
6101 tf_warning_or_error);
6102 if (error_operand_p (diff))
6103 return true;
6104 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6106 error_at (elocus, "difference between %qE and %qD does not have integer type",
6107 TREE_OPERAND (cond, 1), iter);
6108 return true;
6111 switch (TREE_CODE (incr))
6113 case PREINCREMENT_EXPR:
6114 case PREDECREMENT_EXPR:
6115 case POSTINCREMENT_EXPR:
6116 case POSTDECREMENT_EXPR:
6117 if (TREE_OPERAND (incr, 0) != iter)
6119 incr = error_mark_node;
6120 break;
6122 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6123 TREE_CODE (incr), iter,
6124 tf_warning_or_error);
6125 if (error_operand_p (iter_incr))
6126 return true;
6127 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6128 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6129 incr = integer_one_node;
6130 else
6131 incr = integer_minus_one_node;
6132 break;
6133 case MODIFY_EXPR:
6134 if (TREE_OPERAND (incr, 0) != iter)
6135 incr = error_mark_node;
6136 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6137 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6139 tree rhs = TREE_OPERAND (incr, 1);
6140 if (TREE_OPERAND (rhs, 0) == iter)
6142 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6143 != INTEGER_TYPE)
6144 incr = error_mark_node;
6145 else
6147 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6148 iter, TREE_CODE (rhs),
6149 TREE_OPERAND (rhs, 1),
6150 tf_warning_or_error);
6151 if (error_operand_p (iter_incr))
6152 return true;
6153 incr = TREE_OPERAND (rhs, 1);
6154 incr = cp_convert (TREE_TYPE (diff), incr,
6155 tf_warning_or_error);
6156 if (TREE_CODE (rhs) == MINUS_EXPR)
6158 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6159 incr = fold_if_not_in_template (incr);
6161 if (TREE_CODE (incr) != INTEGER_CST
6162 && (TREE_CODE (incr) != NOP_EXPR
6163 || (TREE_CODE (TREE_OPERAND (incr, 0))
6164 != INTEGER_CST)))
6165 iter_incr = NULL;
6168 else if (TREE_OPERAND (rhs, 1) == iter)
6170 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6171 || TREE_CODE (rhs) != PLUS_EXPR)
6172 incr = error_mark_node;
6173 else
6175 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6176 PLUS_EXPR,
6177 TREE_OPERAND (rhs, 0),
6178 ERROR_MARK, iter,
6179 ERROR_MARK, NULL,
6180 tf_warning_or_error);
6181 if (error_operand_p (iter_incr))
6182 return true;
6183 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6184 iter, NOP_EXPR,
6185 iter_incr,
6186 tf_warning_or_error);
6187 if (error_operand_p (iter_incr))
6188 return true;
6189 incr = TREE_OPERAND (rhs, 0);
6190 iter_incr = NULL;
6193 else
6194 incr = error_mark_node;
6196 else
6197 incr = error_mark_node;
6198 break;
6199 default:
6200 incr = error_mark_node;
6201 break;
6204 if (incr == error_mark_node)
6206 error_at (elocus, "invalid increment expression");
6207 return true;
6210 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6211 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6212 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6213 && OMP_CLAUSE_DECL (c) == iter)
6214 break;
6216 decl = create_temporary_var (TREE_TYPE (diff));
6217 pushdecl (decl);
6218 add_decl_expr (decl);
6219 last = create_temporary_var (TREE_TYPE (diff));
6220 pushdecl (last);
6221 add_decl_expr (last);
6222 if (c && iter_incr == NULL)
6224 incr_var = create_temporary_var (TREE_TYPE (diff));
6225 pushdecl (incr_var);
6226 add_decl_expr (incr_var);
6228 gcc_assert (stmts_are_full_exprs_p ());
6230 orig_pre_body = *pre_body;
6231 *pre_body = push_stmt_list ();
6232 if (orig_pre_body)
6233 add_stmt (orig_pre_body);
6234 if (init != NULL)
6235 finish_expr_stmt (build_x_modify_expr (elocus,
6236 iter, NOP_EXPR, init,
6237 tf_warning_or_error));
6238 init = build_int_cst (TREE_TYPE (diff), 0);
6239 if (c && iter_incr == NULL)
6241 finish_expr_stmt (build_x_modify_expr (elocus,
6242 incr_var, NOP_EXPR,
6243 incr, tf_warning_or_error));
6244 incr = incr_var;
6245 iter_incr = build_x_modify_expr (elocus,
6246 iter, PLUS_EXPR, incr,
6247 tf_warning_or_error);
6249 finish_expr_stmt (build_x_modify_expr (elocus,
6250 last, NOP_EXPR, init,
6251 tf_warning_or_error));
6252 *pre_body = pop_stmt_list (*pre_body);
6254 cond = cp_build_binary_op (elocus,
6255 TREE_CODE (cond), decl, diff,
6256 tf_warning_or_error);
6257 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6258 elocus, incr, NULL_TREE);
6260 orig_body = *body;
6261 *body = push_stmt_list ();
6262 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6263 iter_init = build_x_modify_expr (elocus,
6264 iter, PLUS_EXPR, iter_init,
6265 tf_warning_or_error);
6266 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6267 finish_expr_stmt (iter_init);
6268 finish_expr_stmt (build_x_modify_expr (elocus,
6269 last, NOP_EXPR, decl,
6270 tf_warning_or_error));
6271 add_stmt (orig_body);
6272 *body = pop_stmt_list (*body);
6274 if (c)
6276 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6277 finish_expr_stmt (iter_incr);
6278 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6279 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6282 TREE_VEC_ELT (declv, i) = decl;
6283 TREE_VEC_ELT (initv, i) = init;
6284 TREE_VEC_ELT (condv, i) = cond;
6285 TREE_VEC_ELT (incrv, i) = incr;
6287 return false;
6290 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6291 are directly for their associated operands in the statement. DECL
6292 and INIT are a combo; if DECL is NULL then INIT ought to be a
6293 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6294 optional statements that need to go before the loop into its
6295 sk_omp scope. */
6297 tree
6298 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6299 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6301 tree omp_for = NULL, orig_incr = NULL;
6302 tree decl, init, cond, incr;
6303 location_t elocus;
6304 int i;
6306 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6307 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6308 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6309 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6311 decl = TREE_VEC_ELT (declv, i);
6312 init = TREE_VEC_ELT (initv, i);
6313 cond = TREE_VEC_ELT (condv, i);
6314 incr = TREE_VEC_ELT (incrv, i);
6315 elocus = locus;
6317 if (decl == NULL)
6319 if (init != NULL)
6320 switch (TREE_CODE (init))
6322 case MODIFY_EXPR:
6323 decl = TREE_OPERAND (init, 0);
6324 init = TREE_OPERAND (init, 1);
6325 break;
6326 case MODOP_EXPR:
6327 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6329 decl = TREE_OPERAND (init, 0);
6330 init = TREE_OPERAND (init, 2);
6332 break;
6333 default:
6334 break;
6337 if (decl == NULL)
6339 error_at (locus,
6340 "expected iteration declaration or initialization");
6341 return NULL;
6345 if (init && EXPR_HAS_LOCATION (init))
6346 elocus = EXPR_LOCATION (init);
6348 if (cond == NULL)
6350 error_at (elocus, "missing controlling predicate");
6351 return NULL;
6354 if (incr == NULL)
6356 error_at (elocus, "missing increment expression");
6357 return NULL;
6360 TREE_VEC_ELT (declv, i) = decl;
6361 TREE_VEC_ELT (initv, i) = init;
6364 if (dependent_omp_for_p (declv, initv, condv, incrv))
6366 tree stmt;
6368 stmt = make_node (code);
6370 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6372 /* This is really just a place-holder. We'll be decomposing this
6373 again and going through the cp_build_modify_expr path below when
6374 we instantiate the thing. */
6375 TREE_VEC_ELT (initv, i)
6376 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6377 TREE_VEC_ELT (initv, i));
6380 TREE_TYPE (stmt) = void_type_node;
6381 OMP_FOR_INIT (stmt) = initv;
6382 OMP_FOR_COND (stmt) = condv;
6383 OMP_FOR_INCR (stmt) = incrv;
6384 OMP_FOR_BODY (stmt) = body;
6385 OMP_FOR_PRE_BODY (stmt) = pre_body;
6386 OMP_FOR_CLAUSES (stmt) = clauses;
6388 SET_EXPR_LOCATION (stmt, locus);
6389 return add_stmt (stmt);
6392 if (processing_template_decl)
6393 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6395 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6397 decl = TREE_VEC_ELT (declv, i);
6398 init = TREE_VEC_ELT (initv, i);
6399 cond = TREE_VEC_ELT (condv, i);
6400 incr = TREE_VEC_ELT (incrv, i);
6401 if (orig_incr)
6402 TREE_VEC_ELT (orig_incr, i) = incr;
6403 elocus = locus;
6405 if (init && EXPR_HAS_LOCATION (init))
6406 elocus = EXPR_LOCATION (init);
6408 if (!DECL_P (decl))
6410 error_at (elocus, "expected iteration declaration or initialization");
6411 return NULL;
6414 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6416 if (orig_incr)
6417 TREE_VEC_ELT (orig_incr, i) = incr;
6418 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6419 TREE_CODE (TREE_OPERAND (incr, 1)),
6420 TREE_OPERAND (incr, 2),
6421 tf_warning_or_error);
6424 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6426 if (code == OMP_SIMD)
6428 error_at (elocus, "%<#pragma omp simd%> used with class "
6429 "iteration variable %qE", decl);
6430 return NULL;
6432 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6433 incrv, &body, &pre_body, clauses))
6434 return NULL;
6435 continue;
6438 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6439 && !TYPE_PTR_P (TREE_TYPE (decl)))
6441 error_at (elocus, "invalid type for iteration variable %qE", decl);
6442 return NULL;
6445 if (!processing_template_decl)
6447 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6448 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6450 else
6451 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6452 if (cond
6453 && TREE_SIDE_EFFECTS (cond)
6454 && COMPARISON_CLASS_P (cond)
6455 && !processing_template_decl)
6457 tree t = TREE_OPERAND (cond, 0);
6458 if (TREE_SIDE_EFFECTS (t)
6459 && t != decl
6460 && (TREE_CODE (t) != NOP_EXPR
6461 || TREE_OPERAND (t, 0) != decl))
6462 TREE_OPERAND (cond, 0)
6463 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6465 t = TREE_OPERAND (cond, 1);
6466 if (TREE_SIDE_EFFECTS (t)
6467 && t != decl
6468 && (TREE_CODE (t) != NOP_EXPR
6469 || TREE_OPERAND (t, 0) != decl))
6470 TREE_OPERAND (cond, 1)
6471 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6473 if (decl == error_mark_node || init == error_mark_node)
6474 return NULL;
6476 TREE_VEC_ELT (declv, i) = decl;
6477 TREE_VEC_ELT (initv, i) = init;
6478 TREE_VEC_ELT (condv, i) = cond;
6479 TREE_VEC_ELT (incrv, i) = incr;
6480 i++;
6483 if (IS_EMPTY_STMT (pre_body))
6484 pre_body = NULL;
6486 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6487 body, pre_body);
6489 if (omp_for == NULL)
6490 return NULL;
6492 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6494 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6495 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6497 if (TREE_CODE (incr) != MODIFY_EXPR)
6498 continue;
6500 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6501 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6502 && !processing_template_decl)
6504 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6505 if (TREE_SIDE_EFFECTS (t)
6506 && t != decl
6507 && (TREE_CODE (t) != NOP_EXPR
6508 || TREE_OPERAND (t, 0) != decl))
6509 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6510 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6512 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6513 if (TREE_SIDE_EFFECTS (t)
6514 && t != decl
6515 && (TREE_CODE (t) != NOP_EXPR
6516 || TREE_OPERAND (t, 0) != decl))
6517 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6518 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6521 if (orig_incr)
6522 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6524 if (omp_for != NULL)
6525 OMP_FOR_CLAUSES (omp_for) = clauses;
6526 return omp_for;
6529 void
6530 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6531 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6533 tree orig_lhs;
6534 tree orig_rhs;
6535 tree orig_v;
6536 tree orig_lhs1;
6537 tree orig_rhs1;
6538 bool dependent_p;
6539 tree stmt;
6541 orig_lhs = lhs;
6542 orig_rhs = rhs;
6543 orig_v = v;
6544 orig_lhs1 = lhs1;
6545 orig_rhs1 = rhs1;
6546 dependent_p = false;
6547 stmt = NULL_TREE;
6549 /* Even in a template, we can detect invalid uses of the atomic
6550 pragma if neither LHS nor RHS is type-dependent. */
6551 if (processing_template_decl)
6553 dependent_p = (type_dependent_expression_p (lhs)
6554 || (rhs && type_dependent_expression_p (rhs))
6555 || (v && type_dependent_expression_p (v))
6556 || (lhs1 && type_dependent_expression_p (lhs1))
6557 || (rhs1 && type_dependent_expression_p (rhs1)));
6558 if (!dependent_p)
6560 lhs = build_non_dependent_expr (lhs);
6561 if (rhs)
6562 rhs = build_non_dependent_expr (rhs);
6563 if (v)
6564 v = build_non_dependent_expr (v);
6565 if (lhs1)
6566 lhs1 = build_non_dependent_expr (lhs1);
6567 if (rhs1)
6568 rhs1 = build_non_dependent_expr (rhs1);
6571 if (!dependent_p)
6573 bool swapped = false;
6574 if (rhs1 && cp_tree_equal (lhs, rhs))
6576 tree tem = rhs;
6577 rhs = rhs1;
6578 rhs1 = tem;
6579 swapped = !commutative_tree_code (opcode);
6581 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6583 if (code == OMP_ATOMIC)
6584 error ("%<#pragma omp atomic update%> uses two different "
6585 "expressions for memory");
6586 else
6587 error ("%<#pragma omp atomic capture%> uses two different "
6588 "expressions for memory");
6589 return;
6591 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6593 if (code == OMP_ATOMIC)
6594 error ("%<#pragma omp atomic update%> uses two different "
6595 "expressions for memory");
6596 else
6597 error ("%<#pragma omp atomic capture%> uses two different "
6598 "expressions for memory");
6599 return;
6601 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6602 v, lhs1, rhs1, swapped, seq_cst);
6603 if (stmt == error_mark_node)
6604 return;
6606 if (processing_template_decl)
6608 if (code == OMP_ATOMIC_READ)
6610 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6611 OMP_ATOMIC_READ, orig_lhs);
6612 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6613 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6615 else
6617 if (opcode == NOP_EXPR)
6618 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6619 else
6620 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6621 if (orig_rhs1)
6622 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6623 COMPOUND_EXPR, orig_rhs1, stmt);
6624 if (code != OMP_ATOMIC)
6626 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6627 code, orig_lhs1, stmt);
6628 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6629 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6632 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6633 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6635 finish_expr_stmt (stmt);
6638 void
6639 finish_omp_barrier (void)
6641 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6642 vec<tree, va_gc> *vec = make_tree_vector ();
6643 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6644 release_tree_vector (vec);
6645 finish_expr_stmt (stmt);
6648 void
6649 finish_omp_flush (void)
6651 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6652 vec<tree, va_gc> *vec = make_tree_vector ();
6653 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6654 release_tree_vector (vec);
6655 finish_expr_stmt (stmt);
6658 void
6659 finish_omp_taskwait (void)
6661 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6662 vec<tree, va_gc> *vec = make_tree_vector ();
6663 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6664 release_tree_vector (vec);
6665 finish_expr_stmt (stmt);
6668 void
6669 finish_omp_taskyield (void)
6671 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6672 vec<tree, va_gc> *vec = make_tree_vector ();
6673 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6674 release_tree_vector (vec);
6675 finish_expr_stmt (stmt);
6678 void
6679 finish_omp_cancel (tree clauses)
6681 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6682 int mask = 0;
6683 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6684 mask = 1;
6685 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6686 mask = 2;
6687 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6688 mask = 4;
6689 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6690 mask = 8;
6691 else
6693 error ("%<#pragma omp cancel must specify one of "
6694 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6695 return;
6697 vec<tree, va_gc> *vec = make_tree_vector ();
6698 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6699 if (ifc != NULL_TREE)
6701 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6702 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6703 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6704 build_zero_cst (type));
6706 else
6707 ifc = boolean_true_node;
6708 vec->quick_push (build_int_cst (integer_type_node, mask));
6709 vec->quick_push (ifc);
6710 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6711 release_tree_vector (vec);
6712 finish_expr_stmt (stmt);
6715 void
6716 finish_omp_cancellation_point (tree clauses)
6718 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6719 int mask = 0;
6720 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6721 mask = 1;
6722 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6723 mask = 2;
6724 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6725 mask = 4;
6726 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6727 mask = 8;
6728 else
6730 error ("%<#pragma omp cancellation point must specify one of "
6731 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6732 return;
6734 vec<tree, va_gc> *vec
6735 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6736 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6737 release_tree_vector (vec);
6738 finish_expr_stmt (stmt);
6741 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6742 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6743 should create an extra compound stmt. */
6745 tree
6746 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6748 tree r;
6750 if (pcompound)
6751 *pcompound = begin_compound_stmt (0);
6753 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6755 /* Only add the statement to the function if support enabled. */
6756 if (flag_tm)
6757 add_stmt (r);
6758 else
6759 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6760 ? G_("%<__transaction_relaxed%> without "
6761 "transactional memory support enabled")
6762 : G_("%<__transaction_atomic%> without "
6763 "transactional memory support enabled")));
6765 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6766 TREE_SIDE_EFFECTS (r) = 1;
6767 return r;
6770 /* End a __transaction_atomic or __transaction_relaxed statement.
6771 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6772 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6773 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6775 void
6776 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6778 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6779 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6780 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6781 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6783 /* noexcept specifications are not allowed for function transactions. */
6784 gcc_assert (!(noex && compound_stmt));
6785 if (noex)
6787 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6788 noex);
6789 /* This may not be true when the STATEMENT_LIST is empty. */
6790 if (EXPR_P (body))
6791 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6792 TREE_SIDE_EFFECTS (body) = 1;
6793 TRANSACTION_EXPR_BODY (stmt) = body;
6796 if (compound_stmt)
6797 finish_compound_stmt (compound_stmt);
6800 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6801 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6802 condition. */
6804 tree
6805 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6807 tree ret;
6808 if (noex)
6810 expr = build_must_not_throw_expr (expr, noex);
6811 if (EXPR_P (expr))
6812 SET_EXPR_LOCATION (expr, loc);
6813 TREE_SIDE_EFFECTS (expr) = 1;
6815 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6816 if (flags & TM_STMT_ATTR_RELAXED)
6817 TRANSACTION_EXPR_RELAXED (ret) = 1;
6818 TREE_SIDE_EFFECTS (ret) = 1;
6819 SET_EXPR_LOCATION (ret, loc);
6820 return ret;
6823 void
6824 init_cp_semantics (void)
6828 /* Build a STATIC_ASSERT for a static assertion with the condition
6829 CONDITION and the message text MESSAGE. LOCATION is the location
6830 of the static assertion in the source code. When MEMBER_P, this
6831 static assertion is a member of a class. */
6832 void
6833 finish_static_assert (tree condition, tree message, location_t location,
6834 bool member_p)
6836 if (message == NULL_TREE
6837 || message == error_mark_node
6838 || condition == NULL_TREE
6839 || condition == error_mark_node)
6840 return;
6842 if (check_for_bare_parameter_packs (condition))
6843 condition = error_mark_node;
6845 if (type_dependent_expression_p (condition)
6846 || value_dependent_expression_p (condition))
6848 /* We're in a template; build a STATIC_ASSERT and put it in
6849 the right place. */
6850 tree assertion;
6852 assertion = make_node (STATIC_ASSERT);
6853 STATIC_ASSERT_CONDITION (assertion) = condition;
6854 STATIC_ASSERT_MESSAGE (assertion) = message;
6855 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6857 if (member_p)
6858 maybe_add_class_template_decl_list (current_class_type,
6859 assertion,
6860 /*friend_p=*/0);
6861 else
6862 add_stmt (assertion);
6864 return;
6867 /* Fold the expression and convert it to a boolean value. */
6868 condition = fold_non_dependent_expr (condition);
6869 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6870 condition = maybe_constant_value (condition);
6872 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6873 /* Do nothing; the condition is satisfied. */
6875 else
6877 location_t saved_loc = input_location;
6879 input_location = location;
6880 if (TREE_CODE (condition) == INTEGER_CST
6881 && integer_zerop (condition))
6882 /* Report the error. */
6883 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6884 else if (condition && condition != error_mark_node)
6886 error ("non-constant condition for static assertion");
6887 if (require_potential_rvalue_constant_expression (condition))
6888 cxx_constant_value (condition);
6890 input_location = saved_loc;
6894 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6895 suitable for use as a type-specifier.
6897 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6898 id-expression or a class member access, FALSE when it was parsed as
6899 a full expression. */
6901 tree
6902 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6903 tsubst_flags_t complain)
6905 tree type = NULL_TREE;
6907 if (!expr || error_operand_p (expr))
6908 return error_mark_node;
6910 if (TYPE_P (expr)
6911 || TREE_CODE (expr) == TYPE_DECL
6912 || (TREE_CODE (expr) == BIT_NOT_EXPR
6913 && TYPE_P (TREE_OPERAND (expr, 0))))
6915 if (complain & tf_error)
6916 error ("argument to decltype must be an expression");
6917 return error_mark_node;
6920 /* Depending on the resolution of DR 1172, we may later need to distinguish
6921 instantiation-dependent but not type-dependent expressions so that, say,
6922 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6923 if (instantiation_dependent_expression_p (expr))
6925 type = cxx_make_type (DECLTYPE_TYPE);
6926 DECLTYPE_TYPE_EXPR (type) = expr;
6927 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6928 = id_expression_or_member_access_p;
6929 SET_TYPE_STRUCTURAL_EQUALITY (type);
6931 return type;
6934 /* The type denoted by decltype(e) is defined as follows: */
6936 expr = resolve_nondeduced_context (expr);
6938 if (invalid_nonstatic_memfn_p (expr, complain))
6939 return error_mark_node;
6941 if (type_unknown_p (expr))
6943 if (complain & tf_error)
6944 error ("decltype cannot resolve address of overloaded function");
6945 return error_mark_node;
6948 /* To get the size of a static data member declared as an array of
6949 unknown bound, we need to instantiate it. */
6950 if (VAR_P (expr)
6951 && VAR_HAD_UNKNOWN_BOUND (expr)
6952 && DECL_TEMPLATE_INSTANTIATION (expr))
6953 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6955 if (id_expression_or_member_access_p)
6957 /* If e is an id-expression or a class member access (5.2.5
6958 [expr.ref]), decltype(e) is defined as the type of the entity
6959 named by e. If there is no such entity, or e names a set of
6960 overloaded functions, the program is ill-formed. */
6961 if (identifier_p (expr))
6962 expr = lookup_name (expr);
6964 if (INDIRECT_REF_P (expr))
6965 /* This can happen when the expression is, e.g., "a.b". Just
6966 look at the underlying operand. */
6967 expr = TREE_OPERAND (expr, 0);
6969 if (TREE_CODE (expr) == OFFSET_REF
6970 || TREE_CODE (expr) == MEMBER_REF
6971 || TREE_CODE (expr) == SCOPE_REF)
6972 /* We're only interested in the field itself. If it is a
6973 BASELINK, we will need to see through it in the next
6974 step. */
6975 expr = TREE_OPERAND (expr, 1);
6977 if (BASELINK_P (expr))
6978 /* See through BASELINK nodes to the underlying function. */
6979 expr = BASELINK_FUNCTIONS (expr);
6981 switch (TREE_CODE (expr))
6983 case FIELD_DECL:
6984 if (DECL_BIT_FIELD_TYPE (expr))
6986 type = DECL_BIT_FIELD_TYPE (expr);
6987 break;
6989 /* Fall through for fields that aren't bitfields. */
6991 case FUNCTION_DECL:
6992 case VAR_DECL:
6993 case CONST_DECL:
6994 case PARM_DECL:
6995 case RESULT_DECL:
6996 case TEMPLATE_PARM_INDEX:
6997 expr = mark_type_use (expr);
6998 type = TREE_TYPE (expr);
6999 break;
7001 case ERROR_MARK:
7002 type = error_mark_node;
7003 break;
7005 case COMPONENT_REF:
7006 case COMPOUND_EXPR:
7007 mark_type_use (expr);
7008 type = is_bitfield_expr_with_lowered_type (expr);
7009 if (!type)
7010 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7011 break;
7013 case BIT_FIELD_REF:
7014 gcc_unreachable ();
7016 case INTEGER_CST:
7017 case PTRMEM_CST:
7018 /* We can get here when the id-expression refers to an
7019 enumerator or non-type template parameter. */
7020 type = TREE_TYPE (expr);
7021 break;
7023 default:
7024 /* Handle instantiated template non-type arguments. */
7025 type = TREE_TYPE (expr);
7026 break;
7029 else
7031 /* Within a lambda-expression:
7033 Every occurrence of decltype((x)) where x is a possibly
7034 parenthesized id-expression that names an entity of
7035 automatic storage duration is treated as if x were
7036 transformed into an access to a corresponding data member
7037 of the closure type that would have been declared if x
7038 were a use of the denoted entity. */
7039 if (outer_automatic_var_p (expr)
7040 && current_function_decl
7041 && LAMBDA_FUNCTION_P (current_function_decl))
7042 type = capture_decltype (expr);
7043 else if (error_operand_p (expr))
7044 type = error_mark_node;
7045 else if (expr == current_class_ptr)
7046 /* If the expression is just "this", we want the
7047 cv-unqualified pointer for the "this" type. */
7048 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7049 else
7051 /* Otherwise, where T is the type of e, if e is an lvalue,
7052 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7053 cp_lvalue_kind clk = lvalue_kind (expr);
7054 type = unlowered_expr_type (expr);
7055 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7057 /* For vector types, pick a non-opaque variant. */
7058 if (TREE_CODE (type) == VECTOR_TYPE)
7059 type = strip_typedefs (type);
7061 if (clk != clk_none && !(clk & clk_class))
7062 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7066 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
7067 && (flag_iso || warn_vla > 0))
7069 if (complain & tf_warning_or_error)
7070 pedwarn (input_location, OPT_Wvla,
7071 "taking decltype of array of runtime bound");
7072 else
7073 return error_mark_node;
7076 return type;
7079 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7080 __has_nothrow_copy, depending on assign_p. */
7082 static bool
7083 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7085 tree fns;
7087 if (assign_p)
7089 int ix;
7090 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7091 if (ix < 0)
7092 return false;
7093 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7095 else if (TYPE_HAS_COPY_CTOR (type))
7097 /* If construction of the copy constructor was postponed, create
7098 it now. */
7099 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7100 lazily_declare_fn (sfk_copy_constructor, type);
7101 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7102 lazily_declare_fn (sfk_move_constructor, type);
7103 fns = CLASSTYPE_CONSTRUCTORS (type);
7105 else
7106 return false;
7108 for (; fns; fns = OVL_NEXT (fns))
7110 tree fn = OVL_CURRENT (fns);
7112 if (assign_p)
7114 if (copy_fn_p (fn) == 0)
7115 continue;
7117 else if (copy_fn_p (fn) <= 0)
7118 continue;
7120 maybe_instantiate_noexcept (fn);
7121 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7122 return false;
7125 return true;
7128 /* Actually evaluates the trait. */
7130 static bool
7131 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7133 enum tree_code type_code1;
7134 tree t;
7136 type_code1 = TREE_CODE (type1);
7138 switch (kind)
7140 case CPTK_HAS_NOTHROW_ASSIGN:
7141 type1 = strip_array_types (type1);
7142 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7143 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7144 || (CLASS_TYPE_P (type1)
7145 && classtype_has_nothrow_assign_or_copy_p (type1,
7146 true))));
7148 case CPTK_HAS_TRIVIAL_ASSIGN:
7149 /* ??? The standard seems to be missing the "or array of such a class
7150 type" wording for this trait. */
7151 type1 = strip_array_types (type1);
7152 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7153 && (trivial_type_p (type1)
7154 || (CLASS_TYPE_P (type1)
7155 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7157 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7158 type1 = strip_array_types (type1);
7159 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7160 || (CLASS_TYPE_P (type1)
7161 && (t = locate_ctor (type1))
7162 && (maybe_instantiate_noexcept (t),
7163 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7165 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7166 type1 = strip_array_types (type1);
7167 return (trivial_type_p (type1)
7168 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7170 case CPTK_HAS_NOTHROW_COPY:
7171 type1 = strip_array_types (type1);
7172 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7173 || (CLASS_TYPE_P (type1)
7174 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7176 case CPTK_HAS_TRIVIAL_COPY:
7177 /* ??? The standard seems to be missing the "or array of such a class
7178 type" wording for this trait. */
7179 type1 = strip_array_types (type1);
7180 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7181 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7183 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7184 type1 = strip_array_types (type1);
7185 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7186 || (CLASS_TYPE_P (type1)
7187 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7189 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7190 return type_has_virtual_destructor (type1);
7192 case CPTK_IS_ABSTRACT:
7193 return (ABSTRACT_CLASS_TYPE_P (type1));
7195 case CPTK_IS_BASE_OF:
7196 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7197 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7198 || DERIVED_FROM_P (type1, type2)));
7200 case CPTK_IS_CLASS:
7201 return (NON_UNION_CLASS_TYPE_P (type1));
7203 case CPTK_IS_CONVERTIBLE_TO:
7204 /* TODO */
7205 return false;
7207 case CPTK_IS_EMPTY:
7208 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7210 case CPTK_IS_ENUM:
7211 return (type_code1 == ENUMERAL_TYPE);
7213 case CPTK_IS_FINAL:
7214 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7216 case CPTK_IS_LITERAL_TYPE:
7217 return (literal_type_p (type1));
7219 case CPTK_IS_POD:
7220 return (pod_type_p (type1));
7222 case CPTK_IS_POLYMORPHIC:
7223 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7225 case CPTK_IS_STD_LAYOUT:
7226 return (std_layout_type_p (type1));
7228 case CPTK_IS_TRIVIAL:
7229 return (trivial_type_p (type1));
7231 case CPTK_IS_UNION:
7232 return (type_code1 == UNION_TYPE);
7234 default:
7235 gcc_unreachable ();
7236 return false;
7240 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7241 void, or a complete type, returns it, otherwise NULL_TREE. */
7243 static tree
7244 check_trait_type (tree type)
7246 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7247 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7248 return type;
7250 if (VOID_TYPE_P (type))
7251 return type;
7253 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7256 /* Process a trait expression. */
7258 tree
7259 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7261 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7262 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7263 || kind == CPTK_HAS_NOTHROW_COPY
7264 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7265 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7266 || kind == CPTK_HAS_TRIVIAL_COPY
7267 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7268 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7269 || kind == CPTK_IS_ABSTRACT
7270 || kind == CPTK_IS_BASE_OF
7271 || kind == CPTK_IS_CLASS
7272 || kind == CPTK_IS_CONVERTIBLE_TO
7273 || kind == CPTK_IS_EMPTY
7274 || kind == CPTK_IS_ENUM
7275 || kind == CPTK_IS_FINAL
7276 || kind == CPTK_IS_LITERAL_TYPE
7277 || kind == CPTK_IS_POD
7278 || kind == CPTK_IS_POLYMORPHIC
7279 || kind == CPTK_IS_STD_LAYOUT
7280 || kind == CPTK_IS_TRIVIAL
7281 || kind == CPTK_IS_UNION);
7283 if (kind == CPTK_IS_CONVERTIBLE_TO)
7285 sorry ("__is_convertible_to");
7286 return error_mark_node;
7289 if (type1 == error_mark_node
7290 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7291 && type2 == error_mark_node))
7292 return error_mark_node;
7294 if (processing_template_decl)
7296 tree trait_expr = make_node (TRAIT_EXPR);
7297 TREE_TYPE (trait_expr) = boolean_type_node;
7298 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7299 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7300 TRAIT_EXPR_KIND (trait_expr) = kind;
7301 return trait_expr;
7304 switch (kind)
7306 case CPTK_HAS_NOTHROW_ASSIGN:
7307 case CPTK_HAS_TRIVIAL_ASSIGN:
7308 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7309 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7310 case CPTK_HAS_NOTHROW_COPY:
7311 case CPTK_HAS_TRIVIAL_COPY:
7312 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7313 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7314 case CPTK_IS_ABSTRACT:
7315 case CPTK_IS_EMPTY:
7316 case CPTK_IS_FINAL:
7317 case CPTK_IS_LITERAL_TYPE:
7318 case CPTK_IS_POD:
7319 case CPTK_IS_POLYMORPHIC:
7320 case CPTK_IS_STD_LAYOUT:
7321 case CPTK_IS_TRIVIAL:
7322 if (!check_trait_type (type1))
7323 return error_mark_node;
7324 break;
7326 case CPTK_IS_BASE_OF:
7327 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7328 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7329 && !complete_type_or_else (type2, NULL_TREE))
7330 /* We already issued an error. */
7331 return error_mark_node;
7332 break;
7334 case CPTK_IS_CLASS:
7335 case CPTK_IS_ENUM:
7336 case CPTK_IS_UNION:
7337 break;
7339 case CPTK_IS_CONVERTIBLE_TO:
7340 default:
7341 gcc_unreachable ();
7344 return (trait_expr_value (kind, type1, type2)
7345 ? boolean_true_node : boolean_false_node);
7348 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7349 which is ignored for C++. */
7351 void
7352 set_float_const_decimal64 (void)
7356 void
7357 clear_float_const_decimal64 (void)
7361 bool
7362 float_const_decimal64_p (void)
7364 return 0;
7368 /* Return true if T is a literal type. */
7370 bool
7371 literal_type_p (tree t)
7373 if (SCALAR_TYPE_P (t)
7374 || TREE_CODE (t) == VECTOR_TYPE
7375 || TREE_CODE (t) == REFERENCE_TYPE)
7376 return true;
7377 if (CLASS_TYPE_P (t))
7379 t = complete_type (t);
7380 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7381 return CLASSTYPE_LITERAL_P (t);
7383 if (TREE_CODE (t) == ARRAY_TYPE)
7384 return literal_type_p (strip_array_types (t));
7385 return false;
7388 /* If DECL is a variable declared `constexpr', require its type
7389 be literal. Return the DECL if OK, otherwise NULL. */
7391 tree
7392 ensure_literal_type_for_constexpr_object (tree decl)
7394 tree type = TREE_TYPE (decl);
7395 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7396 && !processing_template_decl)
7398 tree stype = strip_array_types (type);
7399 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7400 /* Don't complain here, we'll complain about incompleteness
7401 when we try to initialize the variable. */;
7402 else if (!literal_type_p (type))
7404 error ("the type %qT of constexpr variable %qD is not literal",
7405 type, decl);
7406 explain_non_literal_class (type);
7407 return NULL;
7410 return decl;
7413 /* Representation of entries in the constexpr function definition table. */
7415 typedef struct GTY(()) constexpr_fundef {
7416 tree decl;
7417 tree body;
7418 } constexpr_fundef;
7420 /* This table holds all constexpr function definitions seen in
7421 the current translation unit. */
7423 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7425 /* Utility function used for managing the constexpr function table.
7426 Return true if the entries pointed to by P and Q are for the
7427 same constexpr function. */
7429 static inline int
7430 constexpr_fundef_equal (const void *p, const void *q)
7432 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7433 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7434 return lhs->decl == rhs->decl;
7437 /* Utility function used for managing the constexpr function table.
7438 Return a hash value for the entry pointed to by Q. */
7440 static inline hashval_t
7441 constexpr_fundef_hash (const void *p)
7443 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7444 return DECL_UID (fundef->decl);
7447 /* Return a previously saved definition of function FUN. */
7449 static constexpr_fundef *
7450 retrieve_constexpr_fundef (tree fun)
7452 constexpr_fundef fundef = { NULL, NULL };
7453 if (constexpr_fundef_table == NULL)
7454 return NULL;
7456 fundef.decl = fun;
7457 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7460 /* Check whether the parameter and return types of FUN are valid for a
7461 constexpr function, and complain if COMPLAIN. */
7463 static bool
7464 is_valid_constexpr_fn (tree fun, bool complain)
7466 bool ret = true;
7468 if (DECL_INHERITED_CTOR_BASE (fun)
7469 && TREE_CODE (fun) == TEMPLATE_DECL)
7471 ret = false;
7472 if (complain)
7473 error ("inherited constructor %qD is not constexpr",
7474 get_inherited_ctor (fun));
7476 else
7478 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
7479 parm != NULL_TREE; parm = TREE_CHAIN (parm))
7480 if (!literal_type_p (TREE_TYPE (parm)))
7482 ret = false;
7483 if (complain)
7485 error ("invalid type for parameter %d of constexpr "
7486 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7487 explain_non_literal_class (TREE_TYPE (parm));
7492 if (!DECL_CONSTRUCTOR_P (fun))
7494 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7495 if (!literal_type_p (rettype))
7497 ret = false;
7498 if (complain)
7500 error ("invalid return type %qT of constexpr function %q+D",
7501 rettype, fun);
7502 explain_non_literal_class (rettype);
7506 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7507 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7509 ret = false;
7510 if (complain)
7512 error ("enclosing class of constexpr non-static member "
7513 "function %q+#D is not a literal type", fun);
7514 explain_non_literal_class (DECL_CONTEXT (fun));
7518 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7520 ret = false;
7521 if (complain)
7522 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7525 return ret;
7528 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7529 for a member of an anonymous aggregate, INIT is the initializer for that
7530 member, and VEC_OUTER is the vector of constructor elements for the class
7531 whose constructor we are processing. Add the initializer to the vector
7532 and return true to indicate success. */
7534 static bool
7535 build_anon_member_initialization (tree member, tree init,
7536 vec<constructor_elt, va_gc> **vec_outer)
7538 /* MEMBER presents the relevant fields from the inside out, but we need
7539 to build up the initializer from the outside in so that we can reuse
7540 previously built CONSTRUCTORs if this is, say, the second field in an
7541 anonymous struct. So we use a vec as a stack. */
7542 auto_vec<tree, 2> fields;
7545 fields.safe_push (TREE_OPERAND (member, 1));
7546 member = TREE_OPERAND (member, 0);
7548 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7549 && TREE_CODE (member) == COMPONENT_REF);
7551 /* VEC has the constructor elements vector for the context of FIELD.
7552 If FIELD is an anonymous aggregate, we will push inside it. */
7553 vec<constructor_elt, va_gc> **vec = vec_outer;
7554 tree field;
7555 while (field = fields.pop(),
7556 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7558 tree ctor;
7559 /* If there is already an outer constructor entry for the anonymous
7560 aggregate FIELD, use it; otherwise, insert one. */
7561 if (vec_safe_is_empty (*vec)
7562 || (*vec)->last().index != field)
7564 ctor = build_constructor (TREE_TYPE (field), NULL);
7565 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7567 else
7568 ctor = (*vec)->last().value;
7569 vec = &CONSTRUCTOR_ELTS (ctor);
7572 /* Now we're at the innermost field, the one that isn't an anonymous
7573 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7574 gcc_assert (fields.is_empty());
7575 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7577 return true;
7580 /* Subroutine of build_constexpr_constructor_member_initializers.
7581 The expression tree T represents a data member initialization
7582 in a (constexpr) constructor definition. Build a pairing of
7583 the data member with its initializer, and prepend that pair
7584 to the existing initialization pair INITS. */
7586 static bool
7587 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7589 tree member, init;
7590 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7591 t = TREE_OPERAND (t, 0);
7592 if (TREE_CODE (t) == EXPR_STMT)
7593 t = TREE_OPERAND (t, 0);
7594 if (t == error_mark_node)
7595 return false;
7596 if (TREE_CODE (t) == STATEMENT_LIST)
7598 tree_stmt_iterator i;
7599 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7601 if (! build_data_member_initialization (tsi_stmt (i), vec))
7602 return false;
7604 return true;
7606 if (TREE_CODE (t) == CLEANUP_STMT)
7608 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7609 but we can in a constexpr constructor for a non-literal class. Just
7610 ignore it; either all the initialization will be constant, in which
7611 case the cleanup can't run, or it can't be constexpr.
7612 Still recurse into CLEANUP_BODY. */
7613 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7615 if (TREE_CODE (t) == CONVERT_EXPR)
7616 t = TREE_OPERAND (t, 0);
7617 if (TREE_CODE (t) == INIT_EXPR
7618 || TREE_CODE (t) == MODIFY_EXPR)
7620 member = TREE_OPERAND (t, 0);
7621 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7623 else if (TREE_CODE (t) == CALL_EXPR)
7625 member = CALL_EXPR_ARG (t, 0);
7626 /* We don't use build_cplus_new here because it complains about
7627 abstract bases. Leaving the call unwrapped means that it has the
7628 wrong type, but cxx_eval_constant_expression doesn't care. */
7629 init = break_out_target_exprs (t);
7631 else if (TREE_CODE (t) == DECL_EXPR)
7632 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7633 return true;
7634 else
7635 gcc_unreachable ();
7636 if (INDIRECT_REF_P (member))
7637 member = TREE_OPERAND (member, 0);
7638 if (TREE_CODE (member) == NOP_EXPR)
7640 tree op = member;
7641 STRIP_NOPS (op);
7642 if (TREE_CODE (op) == ADDR_EXPR)
7644 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7645 (TREE_TYPE (TREE_TYPE (op)),
7646 TREE_TYPE (TREE_TYPE (member))));
7647 /* Initializing a cv-qualified member; we need to look through
7648 the const_cast. */
7649 member = op;
7651 else if (op == current_class_ptr
7652 && (same_type_ignoring_top_level_qualifiers_p
7653 (TREE_TYPE (TREE_TYPE (member)),
7654 current_class_type)))
7655 /* Delegating constructor. */
7656 member = op;
7657 else
7659 /* This is an initializer for an empty base; keep it for now so
7660 we can check it in cxx_eval_bare_aggregate. */
7661 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7664 if (TREE_CODE (member) == ADDR_EXPR)
7665 member = TREE_OPERAND (member, 0);
7666 if (TREE_CODE (member) == COMPONENT_REF)
7668 tree aggr = TREE_OPERAND (member, 0);
7669 if (TREE_CODE (aggr) != COMPONENT_REF)
7670 /* Normal member initialization. */
7671 member = TREE_OPERAND (member, 1);
7672 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7673 /* Initializing a member of an anonymous union. */
7674 return build_anon_member_initialization (member, init, vec);
7675 else
7676 /* We're initializing a vtable pointer in a base. Leave it as
7677 COMPONENT_REF so we remember the path to get to the vfield. */
7678 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7681 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7682 return true;
7685 /* Make sure that there are no statements after LAST in the constructor
7686 body represented by LIST. */
7688 bool
7689 check_constexpr_ctor_body (tree last, tree list)
7691 bool ok = true;
7692 if (TREE_CODE (list) == STATEMENT_LIST)
7694 tree_stmt_iterator i = tsi_last (list);
7695 for (; !tsi_end_p (i); tsi_prev (&i))
7697 tree t = tsi_stmt (i);
7698 if (t == last)
7699 break;
7700 if (TREE_CODE (t) == BIND_EXPR)
7702 if (BIND_EXPR_VARS (t))
7704 ok = false;
7705 break;
7707 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7708 return false;
7709 else
7710 continue;
7712 /* We currently allow typedefs and static_assert.
7713 FIXME allow them in the standard, too. */
7714 if (TREE_CODE (t) != STATIC_ASSERT)
7716 ok = false;
7717 break;
7721 else if (list != last
7722 && TREE_CODE (list) != STATIC_ASSERT)
7723 ok = false;
7724 if (!ok)
7726 error ("constexpr constructor does not have empty body");
7727 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7729 return ok;
7732 /* V is a vector of constructor elements built up for the base and member
7733 initializers of a constructor for TYPE. They need to be in increasing
7734 offset order, which they might not be yet if TYPE has a primary base
7735 which is not first in the base-clause or a vptr and at least one base
7736 all of which are non-primary. */
7738 static vec<constructor_elt, va_gc> *
7739 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7741 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7742 tree field_type;
7743 unsigned i;
7744 constructor_elt *ce;
7746 if (pri)
7747 field_type = BINFO_TYPE (pri);
7748 else if (TYPE_CONTAINS_VPTR_P (type))
7749 field_type = vtbl_ptr_type_node;
7750 else
7751 return v;
7753 /* Find the element for the primary base or vptr and move it to the
7754 beginning of the vec. */
7755 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7756 if (TREE_TYPE (ce->index) == field_type)
7757 break;
7759 if (i > 0 && i < vec_safe_length (v))
7761 vec<constructor_elt, va_gc> &vref = *v;
7762 constructor_elt elt = vref[i];
7763 for (; i > 0; --i)
7764 vref[i] = vref[i-1];
7765 vref[0] = elt;
7768 return v;
7771 /* Build compile-time evalable representations of member-initializer list
7772 for a constexpr constructor. */
7774 static tree
7775 build_constexpr_constructor_member_initializers (tree type, tree body)
7777 vec<constructor_elt, va_gc> *vec = NULL;
7778 bool ok = true;
7779 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7780 || TREE_CODE (body) == EH_SPEC_BLOCK)
7781 body = TREE_OPERAND (body, 0);
7782 if (TREE_CODE (body) == STATEMENT_LIST)
7783 body = STATEMENT_LIST_HEAD (body)->stmt;
7784 body = BIND_EXPR_BODY (body);
7785 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7787 body = TREE_OPERAND (body, 0);
7788 if (TREE_CODE (body) == EXPR_STMT)
7789 body = TREE_OPERAND (body, 0);
7790 if (TREE_CODE (body) == INIT_EXPR
7791 && (same_type_ignoring_top_level_qualifiers_p
7792 (TREE_TYPE (TREE_OPERAND (body, 0)),
7793 current_class_type)))
7795 /* Trivial copy. */
7796 return TREE_OPERAND (body, 1);
7798 ok = build_data_member_initialization (body, &vec);
7800 else if (TREE_CODE (body) == STATEMENT_LIST)
7802 tree_stmt_iterator i;
7803 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7805 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7806 if (!ok)
7807 break;
7810 else if (TREE_CODE (body) == TRY_BLOCK)
7812 error ("body of %<constexpr%> constructor cannot be "
7813 "a function-try-block");
7814 return error_mark_node;
7816 else if (EXPR_P (body))
7817 ok = build_data_member_initialization (body, &vec);
7818 else
7819 gcc_assert (errorcount > 0);
7820 if (ok)
7822 if (vec_safe_length (vec) > 0)
7824 /* In a delegating constructor, return the target. */
7825 constructor_elt *ce = &(*vec)[0];
7826 if (ce->index == current_class_ptr)
7828 body = ce->value;
7829 vec_free (vec);
7830 return body;
7833 vec = sort_constexpr_mem_initializers (type, vec);
7834 return build_constructor (type, vec);
7836 else
7837 return error_mark_node;
7840 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7841 declared to be constexpr, or a sub-statement thereof. Returns the
7842 return value if suitable, error_mark_node for a statement not allowed in
7843 a constexpr function, or NULL_TREE if no return value was found. */
7845 static tree
7846 constexpr_fn_retval (tree body)
7848 switch (TREE_CODE (body))
7850 case STATEMENT_LIST:
7852 tree_stmt_iterator i;
7853 tree expr = NULL_TREE;
7854 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7856 tree s = constexpr_fn_retval (tsi_stmt (i));
7857 if (s == error_mark_node)
7858 return error_mark_node;
7859 else if (s == NULL_TREE)
7860 /* Keep iterating. */;
7861 else if (expr)
7862 /* Multiple return statements. */
7863 return error_mark_node;
7864 else
7865 expr = s;
7867 return expr;
7870 case RETURN_EXPR:
7871 return break_out_target_exprs (TREE_OPERAND (body, 0));
7873 case DECL_EXPR:
7874 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7875 return NULL_TREE;
7876 return error_mark_node;
7878 case CLEANUP_POINT_EXPR:
7879 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7881 case USING_STMT:
7882 return NULL_TREE;
7884 default:
7885 return error_mark_node;
7889 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7890 FUN; do the necessary transformations to turn it into a single expression
7891 that we can store in the hash table. */
7893 static tree
7894 massage_constexpr_body (tree fun, tree body)
7896 if (DECL_CONSTRUCTOR_P (fun))
7897 body = build_constexpr_constructor_member_initializers
7898 (DECL_CONTEXT (fun), body);
7899 else
7901 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7902 body = EH_SPEC_STMTS (body);
7903 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7904 body = TREE_OPERAND (body, 0);
7905 if (TREE_CODE (body) == BIND_EXPR)
7906 body = BIND_EXPR_BODY (body);
7907 body = constexpr_fn_retval (body);
7909 return body;
7912 /* FUN is a constexpr constructor with massaged body BODY. Return true
7913 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7915 static bool
7916 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7918 bool bad;
7919 tree field;
7920 unsigned i, nelts;
7921 tree ctype;
7923 if (TREE_CODE (body) != CONSTRUCTOR)
7924 return false;
7926 nelts = CONSTRUCTOR_NELTS (body);
7927 ctype = DECL_CONTEXT (fun);
7928 field = TYPE_FIELDS (ctype);
7930 if (TREE_CODE (ctype) == UNION_TYPE)
7932 if (nelts == 0 && next_initializable_field (field))
7934 if (complain)
7935 error ("%<constexpr%> constructor for union %qT must "
7936 "initialize exactly one non-static data member", ctype);
7937 return true;
7939 return false;
7942 bad = false;
7943 for (i = 0; i <= nelts; ++i)
7945 tree index;
7946 if (i == nelts)
7947 index = NULL_TREE;
7948 else
7950 index = CONSTRUCTOR_ELT (body, i)->index;
7951 /* Skip base and vtable inits. */
7952 if (TREE_CODE (index) != FIELD_DECL
7953 || DECL_ARTIFICIAL (index))
7954 continue;
7956 for (; field != index; field = DECL_CHAIN (field))
7958 tree ftype;
7959 if (TREE_CODE (field) != FIELD_DECL
7960 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7961 || DECL_ARTIFICIAL (field))
7962 continue;
7963 ftype = strip_array_types (TREE_TYPE (field));
7964 if (type_has_constexpr_default_constructor (ftype))
7966 /* It's OK to skip a member with a trivial constexpr ctor.
7967 A constexpr ctor that isn't trivial should have been
7968 added in by now. */
7969 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7970 || errorcount != 0);
7971 continue;
7973 if (!complain)
7974 return true;
7975 error ("uninitialized member %qD in %<constexpr%> constructor",
7976 field);
7977 bad = true;
7979 if (field == NULL_TREE)
7980 break;
7981 field = DECL_CHAIN (field);
7984 return bad;
7987 /* We are processing the definition of the constexpr function FUN.
7988 Check that its BODY fulfills the propriate requirements and
7989 enter it in the constexpr function definition table.
7990 For constructor BODY is actually the TREE_LIST of the
7991 member-initializer list. */
7993 tree
7994 register_constexpr_fundef (tree fun, tree body)
7996 constexpr_fundef entry;
7997 constexpr_fundef **slot;
7999 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
8000 return NULL;
8002 body = massage_constexpr_body (fun, body);
8003 if (body == NULL_TREE || body == error_mark_node)
8005 if (!DECL_CONSTRUCTOR_P (fun))
8006 error ("body of constexpr function %qD not a return-statement", fun);
8007 return NULL;
8010 if (!potential_rvalue_constant_expression (body))
8012 if (!DECL_GENERATED_P (fun))
8013 require_potential_rvalue_constant_expression (body);
8014 return NULL;
8017 if (DECL_CONSTRUCTOR_P (fun)
8018 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8019 return NULL;
8021 /* Create the constexpr function table if necessary. */
8022 if (constexpr_fundef_table == NULL)
8023 constexpr_fundef_table = htab_create_ggc (101,
8024 constexpr_fundef_hash,
8025 constexpr_fundef_equal,
8026 ggc_free);
8027 entry.decl = fun;
8028 entry.body = body;
8029 slot = (constexpr_fundef **)
8030 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8032 gcc_assert (*slot == NULL);
8033 *slot = ggc_alloc<constexpr_fundef> ();
8034 **slot = entry;
8036 return fun;
8039 /* FUN is a non-constexpr function called in a context that requires a
8040 constant expression. If it comes from a constexpr template, explain why
8041 the instantiation isn't constexpr. */
8043 void
8044 explain_invalid_constexpr_fn (tree fun)
8046 static hash_set<tree> *diagnosed;
8047 tree body;
8048 location_t save_loc;
8049 /* Only diagnose defaulted functions or instantiations. */
8050 if (!DECL_DEFAULTED_FN (fun)
8051 && !is_instantiation_of_constexpr (fun))
8052 return;
8053 if (diagnosed == NULL)
8054 diagnosed = new hash_set<tree>;
8055 if (diagnosed->add (fun))
8056 /* Already explained. */
8057 return;
8059 save_loc = input_location;
8060 input_location = DECL_SOURCE_LOCATION (fun);
8061 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8062 /* First check the declaration. */
8063 if (is_valid_constexpr_fn (fun, true))
8065 /* Then if it's OK, the body. */
8066 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8067 explain_implicit_non_constexpr (fun);
8068 else
8070 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8071 require_potential_rvalue_constant_expression (body);
8072 if (DECL_CONSTRUCTOR_P (fun))
8073 cx_check_missing_mem_inits (fun, body, true);
8076 input_location = save_loc;
8079 /* Objects of this type represent calls to constexpr functions
8080 along with the bindings of parameters to their arguments, for
8081 the purpose of compile time evaluation. */
8083 typedef struct GTY(()) constexpr_call {
8084 /* Description of the constexpr function definition. */
8085 constexpr_fundef *fundef;
8086 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8087 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8088 Note: This arrangement is made to accommodate the use of
8089 iterative_hash_template_arg (see pt.c). If you change this
8090 representation, also change the hash calculation in
8091 cxx_eval_call_expression. */
8092 tree bindings;
8093 /* Result of the call.
8094 NULL means the call is being evaluated.
8095 error_mark_node means that the evaluation was erroneous;
8096 otherwise, the actuall value of the call. */
8097 tree result;
8098 /* The hash of this call; we remember it here to avoid having to
8099 recalculate it when expanding the hash table. */
8100 hashval_t hash;
8101 } constexpr_call;
8103 /* A table of all constexpr calls that have been evaluated by the
8104 compiler in this translation unit. */
8106 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8108 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8109 bool, bool, bool *, bool *);
8111 /* Compute a hash value for a constexpr call representation. */
8113 static hashval_t
8114 constexpr_call_hash (const void *p)
8116 const constexpr_call *info = (const constexpr_call *) p;
8117 return info->hash;
8120 /* Return 1 if the objects pointed to by P and Q represent calls
8121 to the same constexpr function with the same arguments.
8122 Otherwise, return 0. */
8124 static int
8125 constexpr_call_equal (const void *p, const void *q)
8127 const constexpr_call *lhs = (const constexpr_call *) p;
8128 const constexpr_call *rhs = (const constexpr_call *) q;
8129 tree lhs_bindings;
8130 tree rhs_bindings;
8131 if (lhs == rhs)
8132 return 1;
8133 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8134 return 0;
8135 lhs_bindings = lhs->bindings;
8136 rhs_bindings = rhs->bindings;
8137 while (lhs_bindings != NULL && rhs_bindings != NULL)
8139 tree lhs_arg = TREE_VALUE (lhs_bindings);
8140 tree rhs_arg = TREE_VALUE (rhs_bindings);
8141 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8142 if (!cp_tree_equal (lhs_arg, rhs_arg))
8143 return 0;
8144 lhs_bindings = TREE_CHAIN (lhs_bindings);
8145 rhs_bindings = TREE_CHAIN (rhs_bindings);
8147 return lhs_bindings == rhs_bindings;
8150 /* Initialize the constexpr call table, if needed. */
8152 static void
8153 maybe_initialize_constexpr_call_table (void)
8155 if (constexpr_call_table == NULL)
8156 constexpr_call_table = htab_create_ggc (101,
8157 constexpr_call_hash,
8158 constexpr_call_equal,
8159 ggc_free);
8162 /* Return true if T designates the implied `this' parameter. */
8164 bool
8165 is_this_parameter (tree t)
8167 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8168 return false;
8169 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8170 return true;
8173 /* We have an expression tree T that represents a call, either CALL_EXPR
8174 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8175 retrun the _DECL for that function. */
8177 static tree
8178 get_function_named_in_call (tree t)
8180 tree fun = NULL;
8181 switch (TREE_CODE (t))
8183 case CALL_EXPR:
8184 fun = CALL_EXPR_FN (t);
8185 break;
8187 case AGGR_INIT_EXPR:
8188 fun = AGGR_INIT_EXPR_FN (t);
8189 break;
8191 default:
8192 gcc_unreachable();
8193 break;
8195 if (TREE_CODE (fun) == ADDR_EXPR
8196 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8197 fun = TREE_OPERAND (fun, 0);
8198 return fun;
8201 /* We have an expression tree T that represents a call, either CALL_EXPR
8202 or AGGR_INIT_EXPR. Return the Nth argument. */
8204 static inline tree
8205 get_nth_callarg (tree t, int n)
8207 switch (TREE_CODE (t))
8209 case CALL_EXPR:
8210 return CALL_EXPR_ARG (t, n);
8212 case AGGR_INIT_EXPR:
8213 return AGGR_INIT_EXPR_ARG (t, n);
8215 default:
8216 gcc_unreachable ();
8217 return NULL;
8221 /* Look up the binding of the function parameter T in a constexpr
8222 function call context CALL. */
8224 static tree
8225 lookup_parameter_binding (const constexpr_call *call, tree t)
8227 tree b = purpose_member (t, call->bindings);
8228 return TREE_VALUE (b);
8231 /* Attempt to evaluate T which represents a call to a builtin function.
8232 We assume here that all builtin functions evaluate to scalar types
8233 represented by _CST nodes. */
8235 static tree
8236 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8237 bool allow_non_constant, bool addr,
8238 bool *non_constant_p, bool *overflow_p)
8240 const int nargs = call_expr_nargs (t);
8241 tree *args = (tree *) alloca (nargs * sizeof (tree));
8242 tree new_call;
8243 int i;
8244 for (i = 0; i < nargs; ++i)
8246 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8247 allow_non_constant, addr,
8248 non_constant_p, overflow_p);
8249 if (allow_non_constant && *non_constant_p)
8250 return t;
8252 if (*non_constant_p)
8253 return t;
8254 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8255 CALL_EXPR_FN (t), nargs, args);
8256 new_call = fold (new_call);
8257 VERIFY_CONSTANT (new_call);
8258 return new_call;
8261 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8262 the type of the value to match. */
8264 static tree
8265 adjust_temp_type (tree type, tree temp)
8267 if (TREE_TYPE (temp) == type)
8268 return temp;
8269 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8270 if (TREE_CODE (temp) == CONSTRUCTOR)
8271 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8272 gcc_assert (scalarish_type_p (type));
8273 return cp_fold_convert (type, temp);
8276 /* Subroutine of cxx_eval_call_expression.
8277 We are processing a call expression (either CALL_EXPR or
8278 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8279 all arguments and bind their values to correspondings
8280 parameters, making up the NEW_CALL context. */
8282 static void
8283 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8284 constexpr_call *new_call,
8285 bool allow_non_constant,
8286 bool *non_constant_p, bool *overflow_p)
8288 const int nargs = call_expr_nargs (t);
8289 tree fun = new_call->fundef->decl;
8290 tree parms = DECL_ARGUMENTS (fun);
8291 int i;
8292 for (i = 0; i < nargs; ++i)
8294 tree x, arg;
8295 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8296 /* For member function, the first argument is a pointer to the implied
8297 object. And for an object construction, don't bind `this' before
8298 it is fully constructed. */
8299 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8300 goto next;
8301 x = get_nth_callarg (t, i);
8302 if (parms && DECL_BY_REFERENCE (parms))
8304 /* cp_genericize made this a reference for argument passing, but
8305 we don't want to treat it like one for constexpr evaluation. */
8306 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8307 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8308 type = TREE_TYPE (type);
8309 x = convert_from_reference (x);
8311 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8312 TREE_CODE (type) == REFERENCE_TYPE,
8313 non_constant_p, overflow_p);
8314 /* Don't VERIFY_CONSTANT here. */
8315 if (*non_constant_p && allow_non_constant)
8316 return;
8317 /* Just discard ellipsis args after checking their constantitude. */
8318 if (!parms)
8319 continue;
8320 if (*non_constant_p)
8321 /* Don't try to adjust the type of non-constant args. */
8322 goto next;
8324 /* Make sure the binding has the same type as the parm. */
8325 if (TREE_CODE (type) != REFERENCE_TYPE)
8326 arg = adjust_temp_type (type, arg);
8327 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8328 next:
8329 parms = TREE_CHAIN (parms);
8333 /* Variables and functions to manage constexpr call expansion context.
8334 These do not need to be marked for PCH or GC. */
8336 /* FIXME remember and print actual constant arguments. */
8337 static vec<tree> call_stack = vNULL;
8338 static int call_stack_tick;
8339 static int last_cx_error_tick;
8341 static bool
8342 push_cx_call_context (tree call)
8344 ++call_stack_tick;
8345 if (!EXPR_HAS_LOCATION (call))
8346 SET_EXPR_LOCATION (call, input_location);
8347 call_stack.safe_push (call);
8348 if (call_stack.length () > (unsigned) max_constexpr_depth)
8349 return false;
8350 return true;
8353 static void
8354 pop_cx_call_context (void)
8356 ++call_stack_tick;
8357 call_stack.pop ();
8360 vec<tree>
8361 cx_error_context (void)
8363 vec<tree> r = vNULL;
8364 if (call_stack_tick != last_cx_error_tick
8365 && !call_stack.is_empty ())
8366 r = call_stack;
8367 last_cx_error_tick = call_stack_tick;
8368 return r;
8371 /* Subroutine of cxx_eval_constant_expression.
8372 Evaluate the call expression tree T in the context of OLD_CALL expression
8373 evaluation. */
8375 static tree
8376 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8377 bool allow_non_constant, bool addr,
8378 bool *non_constant_p, bool *overflow_p)
8380 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8381 tree fun = get_function_named_in_call (t);
8382 tree result;
8383 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8384 constexpr_call **slot;
8385 constexpr_call *entry;
8386 bool depth_ok;
8388 if (TREE_CODE (fun) != FUNCTION_DECL)
8390 /* Might be a constexpr function pointer. */
8391 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8392 /*addr*/false, non_constant_p, overflow_p);
8393 if (TREE_CODE (fun) == ADDR_EXPR)
8394 fun = TREE_OPERAND (fun, 0);
8396 if (TREE_CODE (fun) != FUNCTION_DECL)
8398 if (!allow_non_constant && !*non_constant_p)
8399 error_at (loc, "expression %qE does not designate a constexpr "
8400 "function", fun);
8401 *non_constant_p = true;
8402 return t;
8404 if (DECL_CLONED_FUNCTION_P (fun))
8405 fun = DECL_CLONED_FUNCTION (fun);
8406 if (is_builtin_fn (fun))
8407 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8408 addr, non_constant_p, overflow_p);
8409 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8411 if (!allow_non_constant)
8413 error_at (loc, "call to non-constexpr function %qD", fun);
8414 explain_invalid_constexpr_fn (fun);
8416 *non_constant_p = true;
8417 return t;
8420 /* Shortcut trivial constructor/op=. */
8421 if (trivial_fn_p (fun))
8423 if (call_expr_nargs (t) == 2)
8425 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8426 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8427 addr, non_constant_p, overflow_p);
8429 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8430 && AGGR_INIT_ZERO_FIRST (t))
8431 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8434 /* If in direct recursive call, optimize definition search. */
8435 if (old_call != NULL && old_call->fundef->decl == fun)
8436 new_call.fundef = old_call->fundef;
8437 else
8439 new_call.fundef = retrieve_constexpr_fundef (fun);
8440 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8442 if (!allow_non_constant)
8444 if (DECL_INITIAL (fun))
8446 /* The definition of fun was somehow unsuitable. */
8447 error_at (loc, "%qD called in a constant expression", fun);
8448 explain_invalid_constexpr_fn (fun);
8450 else
8451 error_at (loc, "%qD used before its definition", fun);
8453 *non_constant_p = true;
8454 return t;
8457 cxx_bind_parameters_in_call (old_call, t, &new_call,
8458 allow_non_constant, non_constant_p, overflow_p);
8459 if (*non_constant_p)
8460 return t;
8462 depth_ok = push_cx_call_context (t);
8464 new_call.hash
8465 = iterative_hash_template_arg (new_call.bindings,
8466 constexpr_fundef_hash (new_call.fundef));
8468 /* If we have seen this call before, we are done. */
8469 maybe_initialize_constexpr_call_table ();
8470 slot = (constexpr_call **)
8471 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8472 entry = *slot;
8473 if (entry == NULL)
8475 /* We need to keep a pointer to the entry, not just the slot, as the
8476 slot can move in the call to cxx_eval_builtin_function_call. */
8477 *slot = entry = ggc_alloc<constexpr_call> ();
8478 *entry = new_call;
8480 /* Calls which are in progress have their result set to NULL
8481 so that we can detect circular dependencies. */
8482 else if (entry->result == NULL)
8484 if (!allow_non_constant)
8485 error ("call has circular dependency");
8486 *non_constant_p = true;
8487 entry->result = result = error_mark_node;
8490 if (!depth_ok)
8492 if (!allow_non_constant)
8493 error ("constexpr evaluation depth exceeds maximum of %d (use "
8494 "-fconstexpr-depth= to increase the maximum)",
8495 max_constexpr_depth);
8496 *non_constant_p = true;
8497 entry->result = result = error_mark_node;
8499 else
8501 result = entry->result;
8502 if (!result || result == error_mark_node)
8503 result = (cxx_eval_constant_expression
8504 (&new_call, new_call.fundef->body,
8505 allow_non_constant, addr,
8506 non_constant_p, overflow_p));
8507 if (result == error_mark_node)
8508 *non_constant_p = true;
8509 if (*non_constant_p)
8510 entry->result = result = error_mark_node;
8511 else
8513 /* If this was a call to initialize an object, set the type of
8514 the CONSTRUCTOR to the type of that object. */
8515 if (DECL_CONSTRUCTOR_P (fun))
8517 tree ob_arg = get_nth_callarg (t, 0);
8518 STRIP_NOPS (ob_arg);
8519 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8520 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8521 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8522 result);
8524 entry->result = result;
8528 pop_cx_call_context ();
8529 return unshare_expr (result);
8532 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8534 bool
8535 reduced_constant_expression_p (tree t)
8537 switch (TREE_CODE (t))
8539 case PTRMEM_CST:
8540 /* Even if we can't lower this yet, it's constant. */
8541 return true;
8543 case CONSTRUCTOR:
8544 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
8545 tree elt; unsigned HOST_WIDE_INT idx;
8546 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
8547 if (!reduced_constant_expression_p (elt))
8548 return false;
8549 return true;
8551 default:
8552 /* FIXME are we calling this too much? */
8553 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8557 /* Some expressions may have constant operands but are not constant
8558 themselves, such as 1/0. Call this function (or rather, the macro
8559 following it) to check for that condition.
8561 We only call this in places that require an arithmetic constant, not in
8562 places where we might have a non-constant expression that can be a
8563 component of a constant expression, such as the address of a constexpr
8564 variable that might be dereferenced later. */
8566 static bool
8567 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8568 bool *overflow_p)
8570 if (!*non_constant_p && !reduced_constant_expression_p (t))
8572 if (!allow_non_constant)
8573 error ("%q+E is not a constant expression", t);
8574 *non_constant_p = true;
8576 if (TREE_OVERFLOW_P (t))
8578 if (!allow_non_constant)
8580 permerror (input_location, "overflow in constant expression");
8581 /* If we're being permissive (and are in an enforcing
8582 context), ignore the overflow. */
8583 if (flag_permissive)
8584 return *non_constant_p;
8586 *overflow_p = true;
8588 return *non_constant_p;
8591 /* Subroutine of cxx_eval_constant_expression.
8592 Attempt to reduce the unary expression tree T to a compile time value.
8593 If successful, return the value. Otherwise issue a diagnostic
8594 and return error_mark_node. */
8596 static tree
8597 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8598 bool allow_non_constant, bool addr,
8599 bool *non_constant_p, bool *overflow_p)
8601 tree r;
8602 tree orig_arg = TREE_OPERAND (t, 0);
8603 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8604 addr, non_constant_p, overflow_p);
8605 VERIFY_CONSTANT (arg);
8606 if (arg == orig_arg)
8607 return t;
8608 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8609 VERIFY_CONSTANT (r);
8610 return r;
8613 /* Subroutine of cxx_eval_constant_expression.
8614 Like cxx_eval_unary_expression, except for binary expressions. */
8616 static tree
8617 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8618 bool allow_non_constant, bool addr,
8619 bool *non_constant_p, bool *overflow_p)
8621 tree r;
8622 tree orig_lhs = TREE_OPERAND (t, 0);
8623 tree orig_rhs = TREE_OPERAND (t, 1);
8624 tree lhs, rhs;
8625 lhs = cxx_eval_constant_expression (call, orig_lhs,
8626 allow_non_constant, addr,
8627 non_constant_p, overflow_p);
8628 VERIFY_CONSTANT (lhs);
8629 rhs = cxx_eval_constant_expression (call, orig_rhs,
8630 allow_non_constant, addr,
8631 non_constant_p, overflow_p);
8632 VERIFY_CONSTANT (rhs);
8633 if (lhs == orig_lhs && rhs == orig_rhs)
8634 return t;
8635 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8636 VERIFY_CONSTANT (r);
8637 return r;
8640 /* Subroutine of cxx_eval_constant_expression.
8641 Attempt to evaluate condition expressions. Dead branches are not
8642 looked into. */
8644 static tree
8645 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8646 bool allow_non_constant, bool addr,
8647 bool *non_constant_p, bool *overflow_p)
8649 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8650 allow_non_constant, addr,
8651 non_constant_p, overflow_p);
8652 VERIFY_CONSTANT (val);
8653 /* Don't VERIFY_CONSTANT the other operands. */
8654 if (integer_zerop (val))
8655 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8656 allow_non_constant, addr,
8657 non_constant_p, overflow_p);
8658 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8659 allow_non_constant, addr,
8660 non_constant_p, overflow_p);
8663 /* Subroutine of cxx_eval_constant_expression.
8664 Attempt to reduce a reference to an array slot. */
8666 static tree
8667 cxx_eval_array_reference (const constexpr_call *call, tree t,
8668 bool allow_non_constant, bool addr,
8669 bool *non_constant_p, bool *overflow_p)
8671 tree oldary = TREE_OPERAND (t, 0);
8672 tree ary = cxx_eval_constant_expression (call, oldary,
8673 allow_non_constant, addr,
8674 non_constant_p, overflow_p);
8675 tree index, oldidx;
8676 HOST_WIDE_INT i;
8677 tree elem_type;
8678 unsigned len, elem_nchars = 1;
8679 if (*non_constant_p)
8680 return t;
8681 oldidx = TREE_OPERAND (t, 1);
8682 index = cxx_eval_constant_expression (call, oldidx,
8683 allow_non_constant, false,
8684 non_constant_p, overflow_p);
8685 VERIFY_CONSTANT (index);
8686 if (addr && ary == oldary && index == oldidx)
8687 return t;
8688 else if (addr)
8689 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8690 elem_type = TREE_TYPE (TREE_TYPE (ary));
8691 if (TREE_CODE (ary) == CONSTRUCTOR)
8692 len = CONSTRUCTOR_NELTS (ary);
8693 else if (TREE_CODE (ary) == STRING_CST)
8695 elem_nchars = (TYPE_PRECISION (elem_type)
8696 / TYPE_PRECISION (char_type_node));
8697 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8699 else
8701 /* We can't do anything with other tree codes, so use
8702 VERIFY_CONSTANT to complain and fail. */
8703 VERIFY_CONSTANT (ary);
8704 gcc_unreachable ();
8706 if (compare_tree_int (index, len) >= 0)
8708 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8710 /* If it's within the array bounds but doesn't have an explicit
8711 initializer, it's value-initialized. */
8712 tree val = build_value_init (elem_type, tf_warning_or_error);
8713 return cxx_eval_constant_expression (call, val,
8714 allow_non_constant, addr,
8715 non_constant_p, overflow_p);
8718 if (!allow_non_constant)
8719 error ("array subscript out of bound");
8720 *non_constant_p = true;
8721 return t;
8723 else if (tree_int_cst_lt (index, integer_zero_node))
8725 if (!allow_non_constant)
8726 error ("negative array subscript");
8727 *non_constant_p = true;
8728 return t;
8730 i = tree_to_shwi (index);
8731 if (TREE_CODE (ary) == CONSTRUCTOR)
8732 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8733 else if (elem_nchars == 1)
8734 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8735 TREE_STRING_POINTER (ary)[i]);
8736 else
8738 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8739 return native_interpret_expr (type, (const unsigned char *)
8740 TREE_STRING_POINTER (ary)
8741 + i * elem_nchars, elem_nchars);
8743 /* Don't VERIFY_CONSTANT here. */
8746 /* Subroutine of cxx_eval_constant_expression.
8747 Attempt to reduce a field access of a value of class type. */
8749 static tree
8750 cxx_eval_component_reference (const constexpr_call *call, tree t,
8751 bool allow_non_constant, bool addr,
8752 bool *non_constant_p, bool *overflow_p)
8754 unsigned HOST_WIDE_INT i;
8755 tree field;
8756 tree value;
8757 tree part = TREE_OPERAND (t, 1);
8758 tree orig_whole = TREE_OPERAND (t, 0);
8759 tree whole = cxx_eval_constant_expression (call, orig_whole,
8760 allow_non_constant, addr,
8761 non_constant_p, overflow_p);
8762 if (whole == orig_whole)
8763 return t;
8764 if (addr)
8765 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8766 whole, part, NULL_TREE);
8767 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8768 CONSTRUCTOR. */
8769 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8771 if (!allow_non_constant)
8772 error ("%qE is not a constant expression", orig_whole);
8773 *non_constant_p = true;
8775 if (DECL_MUTABLE_P (part))
8777 if (!allow_non_constant)
8778 error ("mutable %qD is not usable in a constant expression", part);
8779 *non_constant_p = true;
8781 if (*non_constant_p)
8782 return t;
8783 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8785 if (field == part)
8786 return value;
8788 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8789 && CONSTRUCTOR_NELTS (whole) > 0)
8791 /* DR 1188 says we don't have to deal with this. */
8792 if (!allow_non_constant)
8793 error ("accessing %qD member instead of initialized %qD member in "
8794 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8795 *non_constant_p = true;
8796 return t;
8799 /* If there's no explicit init for this field, it's value-initialized. */
8800 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8801 return cxx_eval_constant_expression (call, value,
8802 allow_non_constant, addr,
8803 non_constant_p, overflow_p);
8806 /* Subroutine of cxx_eval_constant_expression.
8807 Attempt to reduce a field access of a value of class type that is
8808 expressed as a BIT_FIELD_REF. */
8810 static tree
8811 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8812 bool allow_non_constant, bool addr,
8813 bool *non_constant_p, bool *overflow_p)
8815 tree orig_whole = TREE_OPERAND (t, 0);
8816 tree retval, fldval, utype, mask;
8817 bool fld_seen = false;
8818 HOST_WIDE_INT istart, isize;
8819 tree whole = cxx_eval_constant_expression (call, orig_whole,
8820 allow_non_constant, addr,
8821 non_constant_p, overflow_p);
8822 tree start, field, value;
8823 unsigned HOST_WIDE_INT i;
8825 if (whole == orig_whole)
8826 return t;
8827 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8828 CONSTRUCTOR. */
8829 if (!*non_constant_p
8830 && TREE_CODE (whole) != VECTOR_CST
8831 && TREE_CODE (whole) != CONSTRUCTOR)
8833 if (!allow_non_constant)
8834 error ("%qE is not a constant expression", orig_whole);
8835 *non_constant_p = true;
8837 if (*non_constant_p)
8838 return t;
8840 if (TREE_CODE (whole) == VECTOR_CST)
8841 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8842 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8844 start = TREE_OPERAND (t, 2);
8845 istart = tree_to_shwi (start);
8846 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8847 utype = TREE_TYPE (t);
8848 if (!TYPE_UNSIGNED (utype))
8849 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8850 retval = build_int_cst (utype, 0);
8851 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8853 tree bitpos = bit_position (field);
8854 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8855 return value;
8856 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8857 && TREE_CODE (value) == INTEGER_CST
8858 && tree_fits_shwi_p (bitpos)
8859 && tree_fits_shwi_p (DECL_SIZE (field)))
8861 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8862 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8863 HOST_WIDE_INT shift;
8864 if (bit >= istart && bit + sz <= istart + isize)
8866 fldval = fold_convert (utype, value);
8867 mask = build_int_cst_type (utype, -1);
8868 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8869 size_int (TYPE_PRECISION (utype) - sz));
8870 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8871 size_int (TYPE_PRECISION (utype) - sz));
8872 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8873 shift = bit - istart;
8874 if (BYTES_BIG_ENDIAN)
8875 shift = TYPE_PRECISION (utype) - shift - sz;
8876 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8877 size_int (shift));
8878 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8879 fld_seen = true;
8883 if (fld_seen)
8884 return fold_convert (TREE_TYPE (t), retval);
8885 gcc_unreachable ();
8886 return error_mark_node;
8889 /* Subroutine of cxx_eval_constant_expression.
8890 Evaluate a short-circuited logical expression T in the context
8891 of a given constexpr CALL. BAILOUT_VALUE is the value for
8892 early return. CONTINUE_VALUE is used here purely for
8893 sanity check purposes. */
8895 static tree
8896 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8897 tree bailout_value, tree continue_value,
8898 bool allow_non_constant, bool addr,
8899 bool *non_constant_p, bool *overflow_p)
8901 tree r;
8902 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8903 allow_non_constant, addr,
8904 non_constant_p, overflow_p);
8905 VERIFY_CONSTANT (lhs);
8906 if (tree_int_cst_equal (lhs, bailout_value))
8907 return lhs;
8908 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8909 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8910 allow_non_constant, addr, non_constant_p, overflow_p);
8911 VERIFY_CONSTANT (r);
8912 return r;
8915 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8916 CONSTRUCTOR elements to initialize (part of) an object containing that
8917 field. Return a pointer to the constructor_elt corresponding to the
8918 initialization of the field. */
8920 static constructor_elt *
8921 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8923 tree aggr = TREE_OPERAND (ref, 0);
8924 tree field = TREE_OPERAND (ref, 1);
8925 HOST_WIDE_INT i;
8926 constructor_elt *ce;
8928 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8930 if (TREE_CODE (aggr) == COMPONENT_REF)
8932 constructor_elt *base_ce
8933 = base_field_constructor_elt (v, aggr);
8934 v = CONSTRUCTOR_ELTS (base_ce->value);
8937 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8938 if (ce->index == field)
8939 return ce;
8941 gcc_unreachable ();
8942 return NULL;
8945 /* Subroutine of cxx_eval_constant_expression.
8946 The expression tree T denotes a C-style array or a C-style
8947 aggregate. Reduce it to a constant expression. */
8949 static tree
8950 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8951 bool allow_non_constant, bool addr,
8952 bool *non_constant_p, bool *overflow_p)
8954 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8955 vec<constructor_elt, va_gc> *n;
8956 vec_alloc (n, vec_safe_length (v));
8957 constructor_elt *ce;
8958 HOST_WIDE_INT i;
8959 bool changed = false;
8960 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8961 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8963 tree elt = cxx_eval_constant_expression (call, ce->value,
8964 allow_non_constant, addr,
8965 non_constant_p, overflow_p);
8966 /* Don't VERIFY_CONSTANT here. */
8967 if (allow_non_constant && *non_constant_p)
8968 goto fail;
8969 if (elt != ce->value)
8970 changed = true;
8971 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8973 /* This is an initialization of a vfield inside a base
8974 subaggregate that we already initialized; push this
8975 initialization into the previous initialization. */
8976 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8977 inner->value = elt;
8979 else if (ce->index
8980 && (TREE_CODE (ce->index) == NOP_EXPR
8981 || TREE_CODE (ce->index) == POINTER_PLUS_EXPR))
8983 /* This is an initializer for an empty base; now that we've
8984 checked that it's constant, we can ignore it. */
8985 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8987 else
8988 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
8990 if (*non_constant_p || !changed)
8992 fail:
8993 vec_free (n);
8994 return t;
8996 t = build_constructor (TREE_TYPE (t), n);
8997 TREE_CONSTANT (t) = true;
8998 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
8999 t = fold (t);
9000 return t;
9003 /* Subroutine of cxx_eval_constant_expression.
9004 The expression tree T is a VEC_INIT_EXPR which denotes the desired
9005 initialization of a non-static data member of array type. Reduce it to a
9006 CONSTRUCTOR.
9008 Note that apart from value-initialization (when VALUE_INIT is true),
9009 this is only intended to support value-initialization and the
9010 initializations done by defaulted constructors for classes with
9011 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
9012 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
9013 for the copy/move constructor. */
9015 static tree
9016 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
9017 bool value_init, bool allow_non_constant, bool addr,
9018 bool *non_constant_p, bool *overflow_p)
9020 tree elttype = TREE_TYPE (atype);
9021 int max = tree_to_shwi (array_type_nelts (atype));
9022 vec<constructor_elt, va_gc> *n;
9023 vec_alloc (n, max + 1);
9024 bool pre_init = false;
9025 int i;
9027 /* For the default constructor, build up a call to the default
9028 constructor of the element type. We only need to handle class types
9029 here, as for a constructor to be constexpr, all members must be
9030 initialized, which for a defaulted default constructor means they must
9031 be of a class type with a constexpr default constructor. */
9032 if (TREE_CODE (elttype) == ARRAY_TYPE)
9033 /* We only do this at the lowest level. */;
9034 else if (value_init)
9036 init = build_value_init (elttype, tf_warning_or_error);
9037 init = cxx_eval_constant_expression
9038 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9039 pre_init = true;
9041 else if (!init)
9043 vec<tree, va_gc> *argvec = make_tree_vector ();
9044 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9045 &argvec, elttype, LOOKUP_NORMAL,
9046 tf_warning_or_error);
9047 release_tree_vector (argvec);
9048 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9049 addr, non_constant_p, overflow_p);
9050 pre_init = true;
9053 if (*non_constant_p && !allow_non_constant)
9054 goto fail;
9056 for (i = 0; i <= max; ++i)
9058 tree idx = build_int_cst (size_type_node, i);
9059 tree eltinit;
9060 if (TREE_CODE (elttype) == ARRAY_TYPE)
9062 /* A multidimensional array; recurse. */
9063 if (value_init || init == NULL_TREE)
9064 eltinit = NULL_TREE;
9065 else
9066 eltinit = cp_build_array_ref (input_location, init, idx,
9067 tf_warning_or_error);
9068 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9069 allow_non_constant, addr,
9070 non_constant_p, overflow_p);
9072 else if (pre_init)
9074 /* Initializing an element using value or default initialization
9075 we just pre-built above. */
9076 if (i == 0)
9077 eltinit = init;
9078 else
9079 eltinit = unshare_expr (init);
9081 else
9083 /* Copying an element. */
9084 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9085 (atype, TREE_TYPE (init)));
9086 eltinit = cp_build_array_ref (input_location, init, idx,
9087 tf_warning_or_error);
9088 if (!real_lvalue_p (init))
9089 eltinit = move (eltinit);
9090 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9091 eltinit = cxx_eval_constant_expression
9092 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9094 if (*non_constant_p && !allow_non_constant)
9095 goto fail;
9096 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9099 if (!*non_constant_p)
9101 init = build_constructor (atype, n);
9102 TREE_CONSTANT (init) = true;
9103 return init;
9106 fail:
9107 vec_free (n);
9108 return init;
9111 static tree
9112 cxx_eval_vec_init (const constexpr_call *call, tree t,
9113 bool allow_non_constant, bool addr,
9114 bool *non_constant_p, bool *overflow_p)
9116 tree atype = TREE_TYPE (t);
9117 tree init = VEC_INIT_EXPR_INIT (t);
9118 tree r = cxx_eval_vec_init_1 (call, atype, init,
9119 VEC_INIT_EXPR_VALUE_INIT (t),
9120 allow_non_constant, addr, non_constant_p, overflow_p);
9121 if (*non_constant_p)
9122 return t;
9123 else
9124 return r;
9127 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9128 match. We want to be less strict for simple *& folding; if we have a
9129 non-const temporary that we access through a const pointer, that should
9130 work. We handle this here rather than change fold_indirect_ref_1
9131 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9132 don't really make sense outside of constant expression evaluation. Also
9133 we want to allow folding to COMPONENT_REF, which could cause trouble
9134 with TBAA in fold_indirect_ref_1.
9136 Try to keep this function synced with fold_indirect_ref_1. */
9138 static tree
9139 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9141 tree sub, subtype;
9143 sub = op0;
9144 STRIP_NOPS (sub);
9145 subtype = TREE_TYPE (sub);
9146 if (!POINTER_TYPE_P (subtype))
9147 return NULL_TREE;
9149 if (TREE_CODE (sub) == ADDR_EXPR)
9151 tree op = TREE_OPERAND (sub, 0);
9152 tree optype = TREE_TYPE (op);
9154 /* *&CONST_DECL -> to the value of the const decl. */
9155 if (TREE_CODE (op) == CONST_DECL)
9156 return DECL_INITIAL (op);
9157 /* *&p => p; make sure to handle *&"str"[cst] here. */
9158 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9160 tree fop = fold_read_from_constant_string (op);
9161 if (fop)
9162 return fop;
9163 else
9164 return op;
9166 /* *(foo *)&fooarray => fooarray[0] */
9167 else if (TREE_CODE (optype) == ARRAY_TYPE
9168 && (same_type_ignoring_top_level_qualifiers_p
9169 (type, TREE_TYPE (optype))))
9171 tree type_domain = TYPE_DOMAIN (optype);
9172 tree min_val = size_zero_node;
9173 if (type_domain && TYPE_MIN_VALUE (type_domain))
9174 min_val = TYPE_MIN_VALUE (type_domain);
9175 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9176 NULL_TREE, NULL_TREE);
9178 /* *(foo *)&complexfoo => __real__ complexfoo */
9179 else if (TREE_CODE (optype) == COMPLEX_TYPE
9180 && (same_type_ignoring_top_level_qualifiers_p
9181 (type, TREE_TYPE (optype))))
9182 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9183 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9184 else if (TREE_CODE (optype) == VECTOR_TYPE
9185 && (same_type_ignoring_top_level_qualifiers_p
9186 (type, TREE_TYPE (optype))))
9188 tree part_width = TYPE_SIZE (type);
9189 tree index = bitsize_int (0);
9190 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9192 /* Also handle conversion to an empty base class, which
9193 is represented with a NOP_EXPR. */
9194 else if (is_empty_class (type)
9195 && CLASS_TYPE_P (optype)
9196 && DERIVED_FROM_P (type, optype))
9198 *empty_base = true;
9199 return op;
9201 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9202 else if (RECORD_OR_UNION_TYPE_P (optype))
9204 tree field = TYPE_FIELDS (optype);
9205 for (; field; field = DECL_CHAIN (field))
9206 if (TREE_CODE (field) == FIELD_DECL
9207 && integer_zerop (byte_position (field))
9208 && (same_type_ignoring_top_level_qualifiers_p
9209 (TREE_TYPE (field), type)))
9211 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9212 break;
9216 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9217 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9219 tree op00 = TREE_OPERAND (sub, 0);
9220 tree op01 = TREE_OPERAND (sub, 1);
9222 STRIP_NOPS (op00);
9223 if (TREE_CODE (op00) == ADDR_EXPR)
9225 tree op00type;
9226 op00 = TREE_OPERAND (op00, 0);
9227 op00type = TREE_TYPE (op00);
9229 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9230 if (TREE_CODE (op00type) == VECTOR_TYPE
9231 && (same_type_ignoring_top_level_qualifiers_p
9232 (type, TREE_TYPE (op00type))))
9234 HOST_WIDE_INT offset = tree_to_shwi (op01);
9235 tree part_width = TYPE_SIZE (type);
9236 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9237 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9238 tree index = bitsize_int (indexi);
9240 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9241 return fold_build3_loc (loc,
9242 BIT_FIELD_REF, type, op00,
9243 part_width, index);
9246 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9247 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9248 && (same_type_ignoring_top_level_qualifiers_p
9249 (type, TREE_TYPE (op00type))))
9251 tree size = TYPE_SIZE_UNIT (type);
9252 if (tree_int_cst_equal (size, op01))
9253 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9255 /* ((foo *)&fooarray)[1] => fooarray[1] */
9256 else if (TREE_CODE (op00type) == ARRAY_TYPE
9257 && (same_type_ignoring_top_level_qualifiers_p
9258 (type, TREE_TYPE (op00type))))
9260 tree type_domain = TYPE_DOMAIN (op00type);
9261 tree min_val = size_zero_node;
9262 if (type_domain && TYPE_MIN_VALUE (type_domain))
9263 min_val = TYPE_MIN_VALUE (type_domain);
9264 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9265 TYPE_SIZE_UNIT (type));
9266 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9267 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9268 NULL_TREE, NULL_TREE);
9270 /* Also handle conversion to an empty base class, which
9271 is represented with a NOP_EXPR. */
9272 else if (is_empty_class (type)
9273 && CLASS_TYPE_P (op00type)
9274 && DERIVED_FROM_P (type, op00type))
9276 *empty_base = true;
9277 return op00;
9279 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9280 else if (RECORD_OR_UNION_TYPE_P (op00type))
9282 tree field = TYPE_FIELDS (op00type);
9283 for (; field; field = DECL_CHAIN (field))
9284 if (TREE_CODE (field) == FIELD_DECL
9285 && tree_int_cst_equal (byte_position (field), op01)
9286 && (same_type_ignoring_top_level_qualifiers_p
9287 (TREE_TYPE (field), type)))
9289 return fold_build3 (COMPONENT_REF, type, op00,
9290 field, NULL_TREE);
9291 break;
9296 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9297 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9298 && (same_type_ignoring_top_level_qualifiers_p
9299 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9301 tree type_domain;
9302 tree min_val = size_zero_node;
9303 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9304 if (newsub)
9305 sub = newsub;
9306 else
9307 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9308 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9309 if (type_domain && TYPE_MIN_VALUE (type_domain))
9310 min_val = TYPE_MIN_VALUE (type_domain);
9311 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9312 NULL_TREE);
9315 return NULL_TREE;
9318 static tree
9319 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9320 bool allow_non_constant, bool addr,
9321 bool *non_constant_p, bool *overflow_p)
9323 tree orig_op0 = TREE_OPERAND (t, 0);
9324 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9325 /*addr*/false, non_constant_p, overflow_p);
9326 bool empty_base = false;
9327 tree r;
9329 /* Don't VERIFY_CONSTANT here. */
9330 if (*non_constant_p)
9331 return t;
9333 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9334 &empty_base);
9336 if (r)
9337 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9338 addr, non_constant_p, overflow_p);
9339 else
9341 tree sub = op0;
9342 STRIP_NOPS (sub);
9343 if (TREE_CODE (sub) == ADDR_EXPR)
9345 /* We couldn't fold to a constant value. Make sure it's not
9346 something we should have been able to fold. */
9347 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9348 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9349 /* DR 1188 says we don't have to deal with this. */
9350 if (!allow_non_constant)
9351 error ("accessing value of %qE through a %qT glvalue in a "
9352 "constant expression", build_fold_indirect_ref (sub),
9353 TREE_TYPE (t));
9354 *non_constant_p = true;
9355 return t;
9359 /* If we're pulling out the value of an empty base, make sure
9360 that the whole object is constant and then return an empty
9361 CONSTRUCTOR. */
9362 if (empty_base)
9364 VERIFY_CONSTANT (r);
9365 r = build_constructor (TREE_TYPE (t), NULL);
9366 TREE_CONSTANT (r) = true;
9369 if (r == NULL_TREE)
9371 if (addr && op0 != orig_op0)
9372 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9373 if (!addr)
9374 VERIFY_CONSTANT (t);
9375 return t;
9377 return r;
9380 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9381 Shared between potential_constant_expression and
9382 cxx_eval_constant_expression. */
9384 static void
9385 non_const_var_error (tree r)
9387 tree type = TREE_TYPE (r);
9388 error ("the value of %qD is not usable in a constant "
9389 "expression", r);
9390 /* Avoid error cascade. */
9391 if (DECL_INITIAL (r) == error_mark_node)
9392 return;
9393 if (DECL_DECLARED_CONSTEXPR_P (r))
9394 inform (DECL_SOURCE_LOCATION (r),
9395 "%qD used in its own initializer", r);
9396 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9398 if (!CP_TYPE_CONST_P (type))
9399 inform (DECL_SOURCE_LOCATION (r),
9400 "%q#D is not const", r);
9401 else if (CP_TYPE_VOLATILE_P (type))
9402 inform (DECL_SOURCE_LOCATION (r),
9403 "%q#D is volatile", r);
9404 else if (!DECL_INITIAL (r)
9405 || !TREE_CONSTANT (DECL_INITIAL (r)))
9406 inform (DECL_SOURCE_LOCATION (r),
9407 "%qD was not initialized with a constant "
9408 "expression", r);
9409 else
9410 gcc_unreachable ();
9412 else
9414 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9415 inform (DECL_SOURCE_LOCATION (r),
9416 "%qD was not declared %<constexpr%>", r);
9417 else
9418 inform (DECL_SOURCE_LOCATION (r),
9419 "%qD does not have integral or enumeration type",
9424 /* Subroutine of cxx_eval_constant_expression.
9425 Like cxx_eval_unary_expression, except for trinary expressions. */
9427 static tree
9428 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9429 bool allow_non_constant, bool addr,
9430 bool *non_constant_p, bool *overflow_p)
9432 int i;
9433 tree args[3];
9434 tree val;
9436 for (i = 0; i < 3; i++)
9438 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9439 allow_non_constant, addr,
9440 non_constant_p, overflow_p);
9441 VERIFY_CONSTANT (args[i]);
9444 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9445 args[0], args[1], args[2]);
9446 if (val == NULL_TREE)
9447 return t;
9448 VERIFY_CONSTANT (val);
9449 return val;
9452 /* Attempt to reduce the expression T to a constant value.
9453 On failure, issue diagnostic and return error_mark_node. */
9454 /* FIXME unify with c_fully_fold */
9456 static tree
9457 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9458 bool allow_non_constant, bool addr,
9459 bool *non_constant_p, bool *overflow_p)
9461 tree r = t;
9463 if (t == error_mark_node)
9465 *non_constant_p = true;
9466 return t;
9468 if (CONSTANT_CLASS_P (t))
9470 if (TREE_CODE (t) == PTRMEM_CST)
9471 t = cplus_expand_constant (t);
9472 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9473 *overflow_p = true;
9474 return t;
9476 if (TREE_CODE (t) != NOP_EXPR
9477 && reduced_constant_expression_p (t))
9478 return fold (t);
9480 switch (TREE_CODE (t))
9482 case VAR_DECL:
9483 if (addr)
9484 return t;
9485 /* else fall through. */
9486 case CONST_DECL:
9487 r = integral_constant_value (t);
9488 if (TREE_CODE (r) == TARGET_EXPR
9489 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9490 r = TARGET_EXPR_INITIAL (r);
9491 if (DECL_P (r))
9493 if (!allow_non_constant)
9494 non_const_var_error (r);
9495 *non_constant_p = true;
9497 break;
9499 case FUNCTION_DECL:
9500 case TEMPLATE_DECL:
9501 case LABEL_DECL:
9502 return t;
9504 case PARM_DECL:
9505 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9507 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9509 if (!allow_non_constant)
9510 sorry ("use of the value of the object being constructed "
9511 "in a constant expression");
9512 *non_constant_p = true;
9514 else
9515 r = lookup_parameter_binding (call, t);
9517 else if (addr)
9518 /* Defer in case this is only used for its type. */;
9519 else
9521 if (!allow_non_constant)
9522 error ("%qE is not a constant expression", t);
9523 *non_constant_p = true;
9525 break;
9527 case CALL_EXPR:
9528 case AGGR_INIT_EXPR:
9529 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9530 non_constant_p, overflow_p);
9531 break;
9533 case TARGET_EXPR:
9534 if (!literal_type_p (TREE_TYPE (t)))
9536 if (!allow_non_constant)
9538 error ("temporary of non-literal type %qT in a "
9539 "constant expression", TREE_TYPE (t));
9540 explain_non_literal_class (TREE_TYPE (t));
9542 *non_constant_p = true;
9543 break;
9545 /* else fall through. */
9546 case INIT_EXPR:
9547 /* Pass false for 'addr' because these codes indicate
9548 initialization of a temporary. */
9549 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9550 allow_non_constant, false,
9551 non_constant_p, overflow_p);
9552 if (!*non_constant_p)
9553 /* Adjust the type of the result to the type of the temporary. */
9554 r = adjust_temp_type (TREE_TYPE (t), r);
9555 break;
9557 case SCOPE_REF:
9558 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9559 allow_non_constant, addr,
9560 non_constant_p, overflow_p);
9561 break;
9563 case RETURN_EXPR:
9564 case NON_LVALUE_EXPR:
9565 case TRY_CATCH_EXPR:
9566 case CLEANUP_POINT_EXPR:
9567 case MUST_NOT_THROW_EXPR:
9568 case SAVE_EXPR:
9569 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9570 allow_non_constant, addr,
9571 non_constant_p, overflow_p);
9572 break;
9574 /* These differ from cxx_eval_unary_expression in that this doesn't
9575 check for a constant operand or result; an address can be
9576 constant without its operand being, and vice versa. */
9577 case INDIRECT_REF:
9578 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9579 non_constant_p, overflow_p);
9580 break;
9582 case ADDR_EXPR:
9584 tree oldop = TREE_OPERAND (t, 0);
9585 tree op = cxx_eval_constant_expression (call, oldop,
9586 allow_non_constant,
9587 /*addr*/true,
9588 non_constant_p, overflow_p);
9589 /* Don't VERIFY_CONSTANT here. */
9590 if (*non_constant_p)
9591 return t;
9592 /* This function does more aggressive folding than fold itself. */
9593 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9594 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9595 return t;
9596 break;
9599 case REALPART_EXPR:
9600 case IMAGPART_EXPR:
9601 case CONJ_EXPR:
9602 case FIX_TRUNC_EXPR:
9603 case FLOAT_EXPR:
9604 case NEGATE_EXPR:
9605 case ABS_EXPR:
9606 case BIT_NOT_EXPR:
9607 case TRUTH_NOT_EXPR:
9608 case FIXED_CONVERT_EXPR:
9609 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9610 non_constant_p, overflow_p);
9611 break;
9613 case SIZEOF_EXPR:
9614 if (SIZEOF_EXPR_TYPE_P (t))
9615 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9616 SIZEOF_EXPR, false);
9617 else if (TYPE_P (TREE_OPERAND (t, 0)))
9618 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9619 false);
9620 else
9621 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9622 false);
9623 if (r == error_mark_node)
9624 r = size_one_node;
9625 VERIFY_CONSTANT (r);
9626 break;
9628 case COMPOUND_EXPR:
9630 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9631 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9632 introduced by build_call_a. */
9633 tree op0 = TREE_OPERAND (t, 0);
9634 tree op1 = TREE_OPERAND (t, 1);
9635 STRIP_NOPS (op1);
9636 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9637 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9638 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9639 addr, non_constant_p, overflow_p);
9640 else
9642 /* Check that the LHS is constant and then discard it. */
9643 cxx_eval_constant_expression (call, op0, allow_non_constant,
9644 false, non_constant_p, overflow_p);
9645 op1 = TREE_OPERAND (t, 1);
9646 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9647 addr, non_constant_p, overflow_p);
9650 break;
9652 case POINTER_PLUS_EXPR:
9653 case PLUS_EXPR:
9654 case MINUS_EXPR:
9655 case MULT_EXPR:
9656 case TRUNC_DIV_EXPR:
9657 case CEIL_DIV_EXPR:
9658 case FLOOR_DIV_EXPR:
9659 case ROUND_DIV_EXPR:
9660 case TRUNC_MOD_EXPR:
9661 case CEIL_MOD_EXPR:
9662 case ROUND_MOD_EXPR:
9663 case RDIV_EXPR:
9664 case EXACT_DIV_EXPR:
9665 case MIN_EXPR:
9666 case MAX_EXPR:
9667 case LSHIFT_EXPR:
9668 case RSHIFT_EXPR:
9669 case LROTATE_EXPR:
9670 case RROTATE_EXPR:
9671 case BIT_IOR_EXPR:
9672 case BIT_XOR_EXPR:
9673 case BIT_AND_EXPR:
9674 case TRUTH_XOR_EXPR:
9675 case LT_EXPR:
9676 case LE_EXPR:
9677 case GT_EXPR:
9678 case GE_EXPR:
9679 case EQ_EXPR:
9680 case NE_EXPR:
9681 case UNORDERED_EXPR:
9682 case ORDERED_EXPR:
9683 case UNLT_EXPR:
9684 case UNLE_EXPR:
9685 case UNGT_EXPR:
9686 case UNGE_EXPR:
9687 case UNEQ_EXPR:
9688 case LTGT_EXPR:
9689 case RANGE_EXPR:
9690 case COMPLEX_EXPR:
9691 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9692 non_constant_p, overflow_p);
9693 break;
9695 /* fold can introduce non-IF versions of these; still treat them as
9696 short-circuiting. */
9697 case TRUTH_AND_EXPR:
9698 case TRUTH_ANDIF_EXPR:
9699 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9700 boolean_true_node,
9701 allow_non_constant, addr,
9702 non_constant_p, overflow_p);
9703 break;
9705 case TRUTH_OR_EXPR:
9706 case TRUTH_ORIF_EXPR:
9707 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9708 boolean_false_node,
9709 allow_non_constant, addr,
9710 non_constant_p, overflow_p);
9711 break;
9713 case ARRAY_REF:
9714 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9715 non_constant_p, overflow_p);
9716 break;
9718 case COMPONENT_REF:
9719 if (is_overloaded_fn (t))
9721 /* We can only get here in checking mode via
9722 build_non_dependent_expr, because any expression that
9723 calls or takes the address of the function will have
9724 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9725 gcc_checking_assert (allow_non_constant || errorcount);
9726 *non_constant_p = true;
9727 return t;
9729 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9730 non_constant_p, overflow_p);
9731 break;
9733 case BIT_FIELD_REF:
9734 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9735 non_constant_p, overflow_p);
9736 break;
9738 case COND_EXPR:
9739 case VEC_COND_EXPR:
9740 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9741 non_constant_p, overflow_p);
9742 break;
9744 case CONSTRUCTOR:
9745 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9746 non_constant_p, overflow_p);
9747 break;
9749 case VEC_INIT_EXPR:
9750 /* We can get this in a defaulted constructor for a class with a
9751 non-static data member of array type. Either the initializer will
9752 be NULL, meaning default-initialization, or it will be an lvalue
9753 or xvalue of the same type, meaning direct-initialization from the
9754 corresponding member. */
9755 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9756 non_constant_p, overflow_p);
9757 break;
9759 case FMA_EXPR:
9760 case VEC_PERM_EXPR:
9761 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9762 non_constant_p, overflow_p);
9763 break;
9765 case CONVERT_EXPR:
9766 case VIEW_CONVERT_EXPR:
9767 case NOP_EXPR:
9769 tree oldop = TREE_OPERAND (t, 0);
9770 tree op = cxx_eval_constant_expression (call, oldop,
9771 allow_non_constant, addr,
9772 non_constant_p, overflow_p);
9773 if (*non_constant_p)
9774 return t;
9775 if (POINTER_TYPE_P (TREE_TYPE (t))
9776 && TREE_CODE (op) == INTEGER_CST
9777 && !integer_zerop (op))
9779 if (!allow_non_constant)
9780 error_at (EXPR_LOC_OR_LOC (t, input_location),
9781 "reinterpret_cast from integer to pointer");
9782 *non_constant_p = true;
9783 return t;
9785 if (op == oldop)
9786 /* We didn't fold at the top so we could check for ptr-int
9787 conversion. */
9788 return fold (t);
9789 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9790 /* Conversion of an out-of-range value has implementation-defined
9791 behavior; the language considers it different from arithmetic
9792 overflow, which is undefined. */
9793 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9794 TREE_OVERFLOW (r) = false;
9796 break;
9798 case EMPTY_CLASS_EXPR:
9799 /* This is good enough for a function argument that might not get
9800 used, and they can't do anything with it, so just return it. */
9801 return t;
9803 case LAMBDA_EXPR:
9804 case PREINCREMENT_EXPR:
9805 case POSTINCREMENT_EXPR:
9806 case PREDECREMENT_EXPR:
9807 case POSTDECREMENT_EXPR:
9808 case NEW_EXPR:
9809 case VEC_NEW_EXPR:
9810 case DELETE_EXPR:
9811 case VEC_DELETE_EXPR:
9812 case THROW_EXPR:
9813 case MODIFY_EXPR:
9814 case MODOP_EXPR:
9815 /* GCC internal stuff. */
9816 case VA_ARG_EXPR:
9817 case OBJ_TYPE_REF:
9818 case WITH_CLEANUP_EXPR:
9819 case STATEMENT_LIST:
9820 case BIND_EXPR:
9821 case NON_DEPENDENT_EXPR:
9822 case BASELINK:
9823 case EXPR_STMT:
9824 case OFFSET_REF:
9825 if (!allow_non_constant)
9826 error_at (EXPR_LOC_OR_LOC (t, input_location),
9827 "expression %qE is not a constant-expression", t);
9828 *non_constant_p = true;
9829 break;
9831 default:
9832 internal_error ("unexpected expression %qE of kind %s", t,
9833 get_tree_code_name (TREE_CODE (t)));
9834 *non_constant_p = true;
9835 break;
9838 if (r == error_mark_node)
9839 *non_constant_p = true;
9841 if (*non_constant_p)
9842 return t;
9843 else
9844 return r;
9847 static tree
9848 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9850 bool non_constant_p = false;
9851 bool overflow_p = false;
9852 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9853 false, &non_constant_p, &overflow_p);
9855 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9857 if (TREE_CODE (t) != CONSTRUCTOR
9858 && cp_has_mutable_p (TREE_TYPE (t)))
9860 /* We allow a mutable type if the original expression was a
9861 CONSTRUCTOR so that we can do aggregate initialization of
9862 constexpr variables. */
9863 if (!allow_non_constant)
9864 error ("%qT cannot be the type of a complete constant expression "
9865 "because it has mutable sub-objects", TREE_TYPE (t));
9866 non_constant_p = true;
9869 /* Technically we should check this for all subexpressions, but that
9870 runs into problems with our internal representation of pointer
9871 subtraction and the 5.19 rules are still in flux. */
9872 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9873 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9874 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9876 if (!allow_non_constant)
9877 error ("conversion from pointer type %qT "
9878 "to arithmetic type %qT in a constant-expression",
9879 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9880 non_constant_p = true;
9883 if (!non_constant_p && overflow_p)
9884 non_constant_p = true;
9886 if (non_constant_p && !allow_non_constant)
9887 return error_mark_node;
9888 else if (non_constant_p && TREE_CONSTANT (r))
9890 /* This isn't actually constant, so unset TREE_CONSTANT. */
9891 if (EXPR_P (r))
9892 r = copy_node (r);
9893 else if (TREE_CODE (r) == CONSTRUCTOR)
9894 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9895 else
9896 r = build_nop (TREE_TYPE (r), r);
9897 TREE_CONSTANT (r) = false;
9899 else if (non_constant_p || r == t)
9900 return t;
9902 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9904 if (TREE_CODE (t) == TARGET_EXPR
9905 && TARGET_EXPR_INITIAL (t) == r)
9906 return t;
9907 else
9909 r = get_target_expr (r);
9910 TREE_CONSTANT (r) = true;
9911 return r;
9914 else
9915 return r;
9918 /* Returns true if T is a valid subexpression of a constant expression,
9919 even if it isn't itself a constant expression. */
9921 bool
9922 is_sub_constant_expr (tree t)
9924 bool non_constant_p = false;
9925 bool overflow_p = false;
9926 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9927 &overflow_p);
9928 return !non_constant_p && !overflow_p;
9931 /* If T represents a constant expression returns its reduced value.
9932 Otherwise return error_mark_node. If T is dependent, then
9933 return NULL. */
9935 tree
9936 cxx_constant_value (tree t)
9938 return cxx_eval_outermost_constant_expr (t, false);
9941 /* If T is a constant expression, returns its reduced value.
9942 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9943 Otherwise, returns a version of T without TREE_CONSTANT. */
9945 tree
9946 maybe_constant_value (tree t)
9948 tree r;
9950 if (instantiation_dependent_expression_p (t)
9951 || type_unknown_p (t)
9952 || BRACE_ENCLOSED_INITIALIZER_P (t)
9953 || !potential_constant_expression (t))
9955 if (TREE_OVERFLOW_P (t))
9957 t = build_nop (TREE_TYPE (t), t);
9958 TREE_CONSTANT (t) = false;
9960 return t;
9963 r = cxx_eval_outermost_constant_expr (t, true);
9964 #ifdef ENABLE_CHECKING
9965 /* cp_tree_equal looks through NOPs, so allow them. */
9966 gcc_assert (r == t
9967 || CONVERT_EXPR_P (t)
9968 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9969 || !cp_tree_equal (r, t));
9970 #endif
9971 return r;
9974 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9975 than wrapped in a TARGET_EXPR. */
9977 tree
9978 maybe_constant_init (tree t)
9980 if (TREE_CODE (t) == EXPR_STMT)
9981 t = TREE_OPERAND (t, 0);
9982 if (TREE_CODE (t) == CONVERT_EXPR
9983 && VOID_TYPE_P (TREE_TYPE (t)))
9984 t = TREE_OPERAND (t, 0);
9985 t = maybe_constant_value (t);
9986 if (TREE_CODE (t) == TARGET_EXPR)
9988 tree init = TARGET_EXPR_INITIAL (t);
9989 if (TREE_CODE (init) == CONSTRUCTOR)
9990 t = init;
9992 return t;
9995 #if 0
9996 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9997 /* Return true if the object referred to by REF has automatic or thread
9998 local storage. */
10000 enum { ck_ok, ck_bad, ck_unknown };
10001 static int
10002 check_automatic_or_tls (tree ref)
10004 enum machine_mode mode;
10005 HOST_WIDE_INT bitsize, bitpos;
10006 tree offset;
10007 int volatilep = 0, unsignedp = 0;
10008 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
10009 &mode, &unsignedp, &volatilep, false);
10010 duration_kind dk;
10012 /* If there isn't a decl in the middle, we don't know the linkage here,
10013 and this isn't a constant expression anyway. */
10014 if (!DECL_P (decl))
10015 return ck_unknown;
10016 dk = decl_storage_duration (decl);
10017 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
10019 #endif
10021 /* Return true if T denotes a potentially constant expression. Issue
10022 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
10023 an lvalue-rvalue conversion is implied.
10025 C++0x [expr.const] used to say
10027 6 An expression is a potential constant expression if it is
10028 a constant expression where all occurrences of function
10029 parameters are replaced by arbitrary constant expressions
10030 of the appropriate type.
10032 2 A conditional expression is a constant expression unless it
10033 involves one of the following as a potentially evaluated
10034 subexpression (3.2), but subexpressions of logical AND (5.14),
10035 logical OR (5.15), and conditional (5.16) operations that are
10036 not evaluated are not considered. */
10038 static bool
10039 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10041 enum { any = false, rval = true };
10042 int i;
10043 tree tmp;
10045 if (t == error_mark_node)
10046 return false;
10047 if (t == NULL_TREE)
10048 return true;
10049 if (TREE_THIS_VOLATILE (t))
10051 if (flags & tf_error)
10052 error ("expression %qE has side-effects", t);
10053 return false;
10055 if (CONSTANT_CLASS_P (t))
10056 return true;
10058 switch (TREE_CODE (t))
10060 case FUNCTION_DECL:
10061 case BASELINK:
10062 case TEMPLATE_DECL:
10063 case OVERLOAD:
10064 case TEMPLATE_ID_EXPR:
10065 case LABEL_DECL:
10066 case LABEL_EXPR:
10067 case CONST_DECL:
10068 case SIZEOF_EXPR:
10069 case ALIGNOF_EXPR:
10070 case OFFSETOF_EXPR:
10071 case NOEXCEPT_EXPR:
10072 case TEMPLATE_PARM_INDEX:
10073 case TRAIT_EXPR:
10074 case IDENTIFIER_NODE:
10075 case USERDEF_LITERAL:
10076 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10077 case FIELD_DECL:
10078 case PARM_DECL:
10079 case USING_DECL:
10080 return true;
10082 case AGGR_INIT_EXPR:
10083 case CALL_EXPR:
10084 /* -- an invocation of a function other than a constexpr function
10085 or a constexpr constructor. */
10087 tree fun = get_function_named_in_call (t);
10088 const int nargs = call_expr_nargs (t);
10089 i = 0;
10091 if (is_overloaded_fn (fun))
10093 if (TREE_CODE (fun) == FUNCTION_DECL)
10095 if (builtin_valid_in_constant_expr_p (fun))
10096 return true;
10097 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10098 /* Allow any built-in function; if the expansion
10099 isn't constant, we'll deal with that then. */
10100 && !is_builtin_fn (fun))
10102 if (flags & tf_error)
10104 error_at (EXPR_LOC_OR_LOC (t, input_location),
10105 "call to non-constexpr function %qD", fun);
10106 explain_invalid_constexpr_fn (fun);
10108 return false;
10110 /* A call to a non-static member function takes the address
10111 of the object as the first argument. But in a constant
10112 expression the address will be folded away, so look
10113 through it now. */
10114 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10115 && !DECL_CONSTRUCTOR_P (fun))
10117 tree x = get_nth_callarg (t, 0);
10118 if (is_this_parameter (x))
10120 if (DECL_CONTEXT (x) == NULL_TREE
10121 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10123 if (flags & tf_error)
10124 sorry ("calling a member function of the "
10125 "object being constructed in a constant "
10126 "expression");
10127 return false;
10129 /* Otherwise OK. */;
10131 else if (!potential_constant_expression_1 (x, rval, flags))
10132 return false;
10133 i = 1;
10136 else
10138 if (!potential_constant_expression_1 (fun, true, flags))
10139 return false;
10140 fun = get_first_fn (fun);
10142 /* Skip initial arguments to base constructors. */
10143 if (DECL_BASE_CONSTRUCTOR_P (fun))
10144 i = num_artificial_parms_for (fun);
10145 fun = DECL_ORIGIN (fun);
10147 else
10149 if (potential_constant_expression_1 (fun, rval, flags))
10150 /* Might end up being a constant function pointer. */;
10151 else
10152 return false;
10154 for (; i < nargs; ++i)
10156 tree x = get_nth_callarg (t, i);
10157 if (!potential_constant_expression_1 (x, rval, flags))
10158 return false;
10160 return true;
10163 case NON_LVALUE_EXPR:
10164 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10165 -- an lvalue of integral type that refers to a non-volatile
10166 const variable or static data member initialized with
10167 constant expressions, or
10169 -- an lvalue of literal type that refers to non-volatile
10170 object defined with constexpr, or that refers to a
10171 sub-object of such an object; */
10172 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10174 case VAR_DECL:
10175 if (want_rval && !decl_constant_var_p (t)
10176 && !dependent_type_p (TREE_TYPE (t)))
10178 if (flags & tf_error)
10179 non_const_var_error (t);
10180 return false;
10182 return true;
10184 case NOP_EXPR:
10185 case CONVERT_EXPR:
10186 case VIEW_CONVERT_EXPR:
10187 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10188 may change to something more specific to type-punning (DR 1312). */
10190 tree from = TREE_OPERAND (t, 0);
10191 if (POINTER_TYPE_P (TREE_TYPE (t))
10192 && TREE_CODE (from) == INTEGER_CST
10193 && !integer_zerop (from))
10195 if (flags & tf_error)
10196 error_at (EXPR_LOC_OR_LOC (t, input_location),
10197 "reinterpret_cast from integer to pointer");
10198 return false;
10200 return (potential_constant_expression_1
10201 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10204 case ADDR_EXPR:
10205 /* -- a unary operator & that is applied to an lvalue that
10206 designates an object with thread or automatic storage
10207 duration; */
10208 t = TREE_OPERAND (t, 0);
10209 #if 0
10210 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10211 any checking here, as we might dereference the pointer later. If
10212 we remove this code, also remove check_automatic_or_tls. */
10213 i = check_automatic_or_tls (t);
10214 if (i == ck_ok)
10215 return true;
10216 if (i == ck_bad)
10218 if (flags & tf_error)
10219 error ("address-of an object %qE with thread local or "
10220 "automatic storage is not a constant expression", t);
10221 return false;
10223 #endif
10224 return potential_constant_expression_1 (t, any, flags);
10226 case COMPONENT_REF:
10227 case BIT_FIELD_REF:
10228 case ARROW_EXPR:
10229 case OFFSET_REF:
10230 /* -- a class member access unless its postfix-expression is
10231 of literal type or of pointer to literal type. */
10232 /* This test would be redundant, as it follows from the
10233 postfix-expression being a potential constant expression. */
10234 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10235 want_rval, flags);
10237 case EXPR_PACK_EXPANSION:
10238 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10239 want_rval, flags);
10241 case INDIRECT_REF:
10243 tree x = TREE_OPERAND (t, 0);
10244 STRIP_NOPS (x);
10245 if (is_this_parameter (x))
10247 if (DECL_CONTEXT (x)
10248 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10250 if (flags & tf_error)
10251 error ("use of %<this%> in a constant expression");
10252 return false;
10254 if (want_rval && DECL_CONTEXT (x)
10255 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10257 if (flags & tf_error)
10258 sorry ("use of the value of the object being constructed "
10259 "in a constant expression");
10260 return false;
10262 return true;
10264 return potential_constant_expression_1 (x, rval, flags);
10267 case LAMBDA_EXPR:
10268 case DYNAMIC_CAST_EXPR:
10269 case PSEUDO_DTOR_EXPR:
10270 case PREINCREMENT_EXPR:
10271 case POSTINCREMENT_EXPR:
10272 case PREDECREMENT_EXPR:
10273 case POSTDECREMENT_EXPR:
10274 case NEW_EXPR:
10275 case VEC_NEW_EXPR:
10276 case DELETE_EXPR:
10277 case VEC_DELETE_EXPR:
10278 case THROW_EXPR:
10279 case MODIFY_EXPR:
10280 case MODOP_EXPR:
10281 case OMP_ATOMIC:
10282 case OMP_ATOMIC_READ:
10283 case OMP_ATOMIC_CAPTURE_OLD:
10284 case OMP_ATOMIC_CAPTURE_NEW:
10285 /* GCC internal stuff. */
10286 case VA_ARG_EXPR:
10287 case OBJ_TYPE_REF:
10288 case WITH_CLEANUP_EXPR:
10289 case CLEANUP_POINT_EXPR:
10290 case MUST_NOT_THROW_EXPR:
10291 case TRY_CATCH_EXPR:
10292 case STATEMENT_LIST:
10293 /* Don't bother trying to define a subset of statement-expressions to
10294 be constant-expressions, at least for now. */
10295 case STMT_EXPR:
10296 case EXPR_STMT:
10297 case BIND_EXPR:
10298 case TRANSACTION_EXPR:
10299 case IF_STMT:
10300 case DO_STMT:
10301 case FOR_STMT:
10302 case WHILE_STMT:
10303 case DECL_EXPR:
10304 if (flags & tf_error)
10305 error ("expression %qE is not a constant-expression", t);
10306 return false;
10308 case TYPEID_EXPR:
10309 /* -- a typeid expression whose operand is of polymorphic
10310 class type; */
10312 tree e = TREE_OPERAND (t, 0);
10313 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10314 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10316 if (flags & tf_error)
10317 error ("typeid-expression is not a constant expression "
10318 "because %qE is of polymorphic type", e);
10319 return false;
10321 return true;
10324 case MINUS_EXPR:
10325 /* -- a subtraction where both operands are pointers. */
10326 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10327 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10329 if (flags & tf_error)
10330 error ("difference of two pointer expressions is not "
10331 "a constant expression");
10332 return false;
10334 want_rval = true;
10335 goto binary;
10337 case LT_EXPR:
10338 case LE_EXPR:
10339 case GT_EXPR:
10340 case GE_EXPR:
10341 case EQ_EXPR:
10342 case NE_EXPR:
10343 /* -- a relational or equality operator where at least
10344 one of the operands is a pointer. */
10345 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10346 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10348 if (flags & tf_error)
10349 error ("pointer comparison expression is not a "
10350 "constant expression");
10351 return false;
10353 want_rval = true;
10354 goto binary;
10356 case BIT_NOT_EXPR:
10357 /* A destructor. */
10358 if (TYPE_P (TREE_OPERAND (t, 0)))
10359 return true;
10360 /* else fall through. */
10362 case REALPART_EXPR:
10363 case IMAGPART_EXPR:
10364 case CONJ_EXPR:
10365 case SAVE_EXPR:
10366 case FIX_TRUNC_EXPR:
10367 case FLOAT_EXPR:
10368 case NEGATE_EXPR:
10369 case ABS_EXPR:
10370 case TRUTH_NOT_EXPR:
10371 case FIXED_CONVERT_EXPR:
10372 case UNARY_PLUS_EXPR:
10373 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10374 flags);
10376 case CAST_EXPR:
10377 case CONST_CAST_EXPR:
10378 case STATIC_CAST_EXPR:
10379 case REINTERPRET_CAST_EXPR:
10380 case IMPLICIT_CONV_EXPR:
10381 if (cxx_dialect < cxx11
10382 && !dependent_type_p (TREE_TYPE (t))
10383 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10384 /* In C++98, a conversion to non-integral type can't be part of a
10385 constant expression. */
10387 if (flags & tf_error)
10388 error ("cast to non-integral type %qT in a constant expression",
10389 TREE_TYPE (t));
10390 return false;
10393 return (potential_constant_expression_1
10394 (TREE_OPERAND (t, 0),
10395 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10397 case PAREN_EXPR:
10398 case NON_DEPENDENT_EXPR:
10399 /* For convenience. */
10400 case RETURN_EXPR:
10401 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10402 want_rval, flags);
10404 case SCOPE_REF:
10405 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10406 want_rval, flags);
10408 case TARGET_EXPR:
10409 if (!literal_type_p (TREE_TYPE (t)))
10411 if (flags & tf_error)
10413 error ("temporary of non-literal type %qT in a "
10414 "constant expression", TREE_TYPE (t));
10415 explain_non_literal_class (TREE_TYPE (t));
10417 return false;
10419 case INIT_EXPR:
10420 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10421 rval, flags);
10423 case CONSTRUCTOR:
10425 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10426 constructor_elt *ce;
10427 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10428 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10429 return false;
10430 return true;
10433 case TREE_LIST:
10435 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10436 || DECL_P (TREE_PURPOSE (t)));
10437 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10438 flags))
10439 return false;
10440 if (TREE_CHAIN (t) == NULL_TREE)
10441 return true;
10442 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10443 flags);
10446 case TRUNC_DIV_EXPR:
10447 case CEIL_DIV_EXPR:
10448 case FLOOR_DIV_EXPR:
10449 case ROUND_DIV_EXPR:
10450 case TRUNC_MOD_EXPR:
10451 case CEIL_MOD_EXPR:
10452 case ROUND_MOD_EXPR:
10454 tree denom = TREE_OPERAND (t, 1);
10455 if (!potential_constant_expression_1 (denom, rval, flags))
10456 return false;
10457 /* We can't call cxx_eval_outermost_constant_expr on an expression
10458 that hasn't been through fold_non_dependent_expr yet. */
10459 if (!processing_template_decl)
10460 denom = cxx_eval_outermost_constant_expr (denom, true);
10461 if (integer_zerop (denom))
10463 if (flags & tf_error)
10464 error ("division by zero is not a constant-expression");
10465 return false;
10467 else
10469 want_rval = true;
10470 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10471 want_rval, flags);
10475 case COMPOUND_EXPR:
10477 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10478 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10479 introduced by build_call_a. */
10480 tree op0 = TREE_OPERAND (t, 0);
10481 tree op1 = TREE_OPERAND (t, 1);
10482 STRIP_NOPS (op1);
10483 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10484 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10485 return potential_constant_expression_1 (op0, want_rval, flags);
10486 else
10487 goto binary;
10490 /* If the first operand is the non-short-circuit constant, look at
10491 the second operand; otherwise we only care about the first one for
10492 potentiality. */
10493 case TRUTH_AND_EXPR:
10494 case TRUTH_ANDIF_EXPR:
10495 tmp = boolean_true_node;
10496 goto truth;
10497 case TRUTH_OR_EXPR:
10498 case TRUTH_ORIF_EXPR:
10499 tmp = boolean_false_node;
10500 truth:
10502 tree op = TREE_OPERAND (t, 0);
10503 if (!potential_constant_expression_1 (op, rval, flags))
10504 return false;
10505 if (!processing_template_decl)
10506 op = cxx_eval_outermost_constant_expr (op, true);
10507 if (tree_int_cst_equal (op, tmp))
10508 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10509 else
10510 return true;
10513 case PLUS_EXPR:
10514 case MULT_EXPR:
10515 case POINTER_PLUS_EXPR:
10516 case RDIV_EXPR:
10517 case EXACT_DIV_EXPR:
10518 case MIN_EXPR:
10519 case MAX_EXPR:
10520 case LSHIFT_EXPR:
10521 case RSHIFT_EXPR:
10522 case LROTATE_EXPR:
10523 case RROTATE_EXPR:
10524 case BIT_IOR_EXPR:
10525 case BIT_XOR_EXPR:
10526 case BIT_AND_EXPR:
10527 case TRUTH_XOR_EXPR:
10528 case UNORDERED_EXPR:
10529 case ORDERED_EXPR:
10530 case UNLT_EXPR:
10531 case UNLE_EXPR:
10532 case UNGT_EXPR:
10533 case UNGE_EXPR:
10534 case UNEQ_EXPR:
10535 case LTGT_EXPR:
10536 case RANGE_EXPR:
10537 case COMPLEX_EXPR:
10538 want_rval = true;
10539 /* Fall through. */
10540 case ARRAY_REF:
10541 case ARRAY_RANGE_REF:
10542 case MEMBER_REF:
10543 case DOTSTAR_EXPR:
10544 binary:
10545 for (i = 0; i < 2; ++i)
10546 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10547 want_rval, flags))
10548 return false;
10549 return true;
10551 case CILK_SYNC_STMT:
10552 case CILK_SPAWN_STMT:
10553 case ARRAY_NOTATION_REF:
10554 return false;
10556 case FMA_EXPR:
10557 case VEC_PERM_EXPR:
10558 for (i = 0; i < 3; ++i)
10559 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10560 true, flags))
10561 return false;
10562 return true;
10564 case COND_EXPR:
10565 case VEC_COND_EXPR:
10566 /* If the condition is a known constant, we know which of the legs we
10567 care about; otherwise we only require that the condition and
10568 either of the legs be potentially constant. */
10569 tmp = TREE_OPERAND (t, 0);
10570 if (!potential_constant_expression_1 (tmp, rval, flags))
10571 return false;
10572 if (!processing_template_decl)
10573 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10574 if (integer_zerop (tmp))
10575 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10576 want_rval, flags);
10577 else if (TREE_CODE (tmp) == INTEGER_CST)
10578 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10579 want_rval, flags);
10580 for (i = 1; i < 3; ++i)
10581 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10582 want_rval, tf_none))
10583 return true;
10584 if (flags & tf_error)
10585 error ("expression %qE is not a constant-expression", t);
10586 return false;
10588 case VEC_INIT_EXPR:
10589 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10590 return true;
10591 if (flags & tf_error)
10593 error ("non-constant array initialization");
10594 diagnose_non_constexpr_vec_init (t);
10596 return false;
10598 default:
10599 if (objc_is_property_ref (t))
10600 return false;
10602 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10603 gcc_unreachable();
10604 return false;
10608 /* The main entry point to the above. */
10610 bool
10611 potential_constant_expression (tree t)
10613 return potential_constant_expression_1 (t, false, tf_none);
10616 /* As above, but require a constant rvalue. */
10618 bool
10619 potential_rvalue_constant_expression (tree t)
10621 return potential_constant_expression_1 (t, true, tf_none);
10624 /* Like above, but complain about non-constant expressions. */
10626 bool
10627 require_potential_constant_expression (tree t)
10629 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10632 /* Cross product of the above. */
10634 bool
10635 require_potential_rvalue_constant_expression (tree t)
10637 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10640 /* Insert the deduced return type for an auto function. */
10642 void
10643 apply_deduced_return_type (tree fco, tree return_type)
10645 tree result;
10647 if (return_type == error_mark_node)
10648 return;
10650 if (LAMBDA_FUNCTION_P (fco))
10652 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10653 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10656 if (DECL_CONV_FN_P (fco))
10657 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10659 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10661 result = DECL_RESULT (fco);
10662 if (result == NULL_TREE)
10663 return;
10664 if (TREE_TYPE (result) == return_type)
10665 return;
10667 /* We already have a DECL_RESULT from start_preparsed_function.
10668 Now we need to redo the work it and allocate_struct_function
10669 did to reflect the new type. */
10670 gcc_assert (current_function_decl == fco);
10671 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10672 TYPE_MAIN_VARIANT (return_type));
10673 DECL_ARTIFICIAL (result) = 1;
10674 DECL_IGNORED_P (result) = 1;
10675 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10676 result);
10678 DECL_RESULT (fco) = result;
10680 if (!processing_template_decl)
10682 if (!VOID_TYPE_P (TREE_TYPE (result)))
10683 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
10684 bool aggr = aggregate_value_p (result, fco);
10685 #ifdef PCC_STATIC_STRUCT_RETURN
10686 cfun->returns_pcc_struct = aggr;
10687 #endif
10688 cfun->returns_struct = aggr;
10693 /* DECL is a local variable or parameter from the surrounding scope of a
10694 lambda-expression. Returns the decltype for a use of the capture field
10695 for DECL even if it hasn't been captured yet. */
10697 static tree
10698 capture_decltype (tree decl)
10700 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10701 /* FIXME do lookup instead of list walk? */
10702 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10703 tree type;
10705 if (cap)
10706 type = TREE_TYPE (TREE_PURPOSE (cap));
10707 else
10708 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10710 case CPLD_NONE:
10711 error ("%qD is not captured", decl);
10712 return error_mark_node;
10714 case CPLD_COPY:
10715 type = TREE_TYPE (decl);
10716 if (TREE_CODE (type) == REFERENCE_TYPE
10717 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10718 type = TREE_TYPE (type);
10719 break;
10721 case CPLD_REFERENCE:
10722 type = TREE_TYPE (decl);
10723 if (TREE_CODE (type) != REFERENCE_TYPE)
10724 type = build_reference_type (TREE_TYPE (decl));
10725 break;
10727 default:
10728 gcc_unreachable ();
10731 if (TREE_CODE (type) != REFERENCE_TYPE)
10733 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10734 type = cp_build_qualified_type (type, (cp_type_quals (type)
10735 |TYPE_QUAL_CONST));
10736 type = build_reference_type (type);
10738 return type;
10741 #include "gt-cp-semantics.h"