2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / cp / semantics.c
blob795086a5854331d82de802c06478c1ae084f6733
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "pointer-set.h"
48 #include "hash-table.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "omp-low.h"
53 static bool verify_constant (tree, bool, bool *, bool *);
54 #define VERIFY_CONSTANT(X) \
55 do { \
56 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
57 return t; \
58 } while (0)
60 /* There routines provide a modular interface to perform many parsing
61 operations. They may therefore be used during actual parsing, or
62 during template instantiation, which may be regarded as a
63 degenerate form of parsing. */
65 static tree maybe_convert_cond (tree);
66 static tree finalize_nrv_r (tree *, int *, void *);
67 static tree capture_decltype (tree);
70 /* Deferred Access Checking Overview
71 ---------------------------------
73 Most C++ expressions and declarations require access checking
74 to be performed during parsing. However, in several cases,
75 this has to be treated differently.
77 For member declarations, access checking has to be deferred
78 until more information about the declaration is known. For
79 example:
81 class A {
82 typedef int X;
83 public:
84 X f();
87 A::X A::f();
88 A::X g();
90 When we are parsing the function return type `A::X', we don't
91 really know if this is allowed until we parse the function name.
93 Furthermore, some contexts require that access checking is
94 never performed at all. These include class heads, and template
95 instantiations.
97 Typical use of access checking functions is described here:
99 1. When we enter a context that requires certain access checking
100 mode, the function `push_deferring_access_checks' is called with
101 DEFERRING argument specifying the desired mode. Access checking
102 may be performed immediately (dk_no_deferred), deferred
103 (dk_deferred), or not performed (dk_no_check).
105 2. When a declaration such as a type, or a variable, is encountered,
106 the function `perform_or_defer_access_check' is called. It
107 maintains a vector of all deferred checks.
109 3. The global `current_class_type' or `current_function_decl' is then
110 setup by the parser. `enforce_access' relies on these information
111 to check access.
113 4. Upon exiting the context mentioned in step 1,
114 `perform_deferred_access_checks' is called to check all declaration
115 stored in the vector. `pop_deferring_access_checks' is then
116 called to restore the previous access checking mode.
118 In case of parsing error, we simply call `pop_deferring_access_checks'
119 without `perform_deferred_access_checks'. */
121 typedef struct GTY(()) deferred_access {
122 /* A vector representing name-lookups for which we have deferred
123 checking access controls. We cannot check the accessibility of
124 names used in a decl-specifier-seq until we know what is being
125 declared because code like:
127 class A {
128 class B {};
129 B* f();
132 A::B* A::f() { return 0; }
134 is valid, even though `A::B' is not generally accessible. */
135 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
137 /* The current mode of access checks. */
138 enum deferring_kind deferring_access_checks_kind;
140 } deferred_access;
142 /* Data for deferred access checking. */
143 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
144 static GTY(()) unsigned deferred_access_no_check;
146 /* Save the current deferred access states and start deferred
147 access checking iff DEFER_P is true. */
149 void
150 push_deferring_access_checks (deferring_kind deferring)
152 /* For context like template instantiation, access checking
153 disabling applies to all nested context. */
154 if (deferred_access_no_check || deferring == dk_no_check)
155 deferred_access_no_check++;
156 else
158 deferred_access e = {NULL, deferring};
159 vec_safe_push (deferred_access_stack, e);
163 /* Save the current deferred access states and start deferred access
164 checking, continuing the set of deferred checks in CHECKS. */
166 void
167 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
169 push_deferring_access_checks (dk_deferred);
170 if (!deferred_access_no_check)
171 deferred_access_stack->last().deferred_access_checks = checks;
174 /* Resume deferring access checks again after we stopped doing
175 this previously. */
177 void
178 resume_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
184 /* Stop deferring access checks. */
186 void
187 stop_deferring_access_checks (void)
189 if (!deferred_access_no_check)
190 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
193 /* Discard the current deferred access checks and restore the
194 previous states. */
196 void
197 pop_deferring_access_checks (void)
199 if (deferred_access_no_check)
200 deferred_access_no_check--;
201 else
202 deferred_access_stack->pop ();
205 /* Returns a TREE_LIST representing the deferred checks.
206 The TREE_PURPOSE of each node is the type through which the
207 access occurred; the TREE_VALUE is the declaration named.
210 vec<deferred_access_check, va_gc> *
211 get_deferred_access_checks (void)
213 if (deferred_access_no_check)
214 return NULL;
215 else
216 return (deferred_access_stack->last().deferred_access_checks);
219 /* Take current deferred checks and combine with the
220 previous states if we also defer checks previously.
221 Otherwise perform checks now. */
223 void
224 pop_to_parent_deferring_access_checks (void)
226 if (deferred_access_no_check)
227 deferred_access_no_check--;
228 else
230 vec<deferred_access_check, va_gc> *checks;
231 deferred_access *ptr;
233 checks = (deferred_access_stack->last ().deferred_access_checks);
235 deferred_access_stack->pop ();
236 ptr = &deferred_access_stack->last ();
237 if (ptr->deferring_access_checks_kind == dk_no_deferred)
239 /* Check access. */
240 perform_access_checks (checks, tf_warning_or_error);
242 else
244 /* Merge with parent. */
245 int i, j;
246 deferred_access_check *chk, *probe;
248 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
250 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
252 if (probe->binfo == chk->binfo &&
253 probe->decl == chk->decl &&
254 probe->diag_decl == chk->diag_decl)
255 goto found;
257 /* Insert into parent's checks. */
258 vec_safe_push (ptr->deferred_access_checks, *chk);
259 found:;
265 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
266 is the BINFO indicating the qualifying scope used to access the
267 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
268 or we aren't in SFINAE context or all the checks succeed return TRUE,
269 otherwise FALSE. */
271 bool
272 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
273 tsubst_flags_t complain)
275 int i;
276 deferred_access_check *chk;
277 location_t loc = input_location;
278 bool ok = true;
280 if (!checks)
281 return true;
283 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
285 input_location = chk->loc;
286 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
289 input_location = loc;
290 return (complain & tf_error) ? true : ok;
293 /* Perform the deferred access checks.
295 After performing the checks, we still have to keep the list
296 `deferred_access_stack->deferred_access_checks' since we may want
297 to check access for them again later in a different context.
298 For example:
300 class A {
301 typedef int X;
302 static X a;
304 A::X A::a, x; // No error for `A::a', error for `x'
306 We have to perform deferred access of `A::X', first with `A::a',
307 next with `x'. Return value like perform_access_checks above. */
309 bool
310 perform_deferred_access_checks (tsubst_flags_t complain)
312 return perform_access_checks (get_deferred_access_checks (), complain);
315 /* Defer checking the accessibility of DECL, when looked up in
316 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
317 Return value like perform_access_checks above. */
319 bool
320 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
321 tsubst_flags_t complain)
323 int i;
324 deferred_access *ptr;
325 deferred_access_check *chk;
328 /* Exit if we are in a context that no access checking is performed.
330 if (deferred_access_no_check)
331 return true;
333 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
335 ptr = &deferred_access_stack->last ();
337 /* If we are not supposed to defer access checks, just check now. */
338 if (ptr->deferring_access_checks_kind == dk_no_deferred)
340 bool ok = enforce_access (binfo, decl, diag_decl, complain);
341 return (complain & tf_error) ? true : ok;
344 /* See if we are already going to perform this check. */
345 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
347 if (chk->decl == decl && chk->binfo == binfo &&
348 chk->diag_decl == diag_decl)
350 return true;
353 /* If not, record the check. */
354 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
355 vec_safe_push (ptr->deferred_access_checks, new_access);
357 return true;
360 /* Returns nonzero if the current statement is a full expression,
361 i.e. temporaries created during that statement should be destroyed
362 at the end of the statement. */
365 stmts_are_full_exprs_p (void)
367 return current_stmt_tree ()->stmts_are_full_exprs_p;
370 /* T is a statement. Add it to the statement-tree. This is the C++
371 version. The C/ObjC frontends have a slightly different version of
372 this function. */
374 tree
375 add_stmt (tree t)
377 enum tree_code code = TREE_CODE (t);
379 if (EXPR_P (t) && code != LABEL_EXPR)
381 if (!EXPR_HAS_LOCATION (t))
382 SET_EXPR_LOCATION (t, input_location);
384 /* When we expand a statement-tree, we must know whether or not the
385 statements are full-expressions. We record that fact here. */
386 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
389 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_zero_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 /* [stmt.switch]
1134 Integral promotions are performed. */
1135 cond = perform_integral_promotions (cond);
1136 cond = maybe_cleanup_point_expr (cond);
1139 if (check_for_bare_parameter_packs (cond))
1140 cond = error_mark_node;
1141 else if (!processing_template_decl && warn_sequence_point)
1142 verify_sequence_points (cond);
1144 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1145 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1146 add_stmt (switch_stmt);
1147 push_switch (switch_stmt);
1148 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1151 /* Finish the body of a switch-statement, which may be given by
1152 SWITCH_STMT. The COND to switch on is indicated. */
1154 void
1155 finish_switch_stmt (tree switch_stmt)
1157 tree scope;
1159 SWITCH_STMT_BODY (switch_stmt) =
1160 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1161 pop_switch ();
1163 scope = SWITCH_STMT_SCOPE (switch_stmt);
1164 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1165 add_stmt (do_poplevel (scope));
1168 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1169 appropriate. */
1171 tree
1172 begin_try_block (void)
1174 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1175 add_stmt (r);
1176 TRY_STMTS (r) = push_stmt_list ();
1177 return r;
1180 /* Likewise, for a function-try-block. The block returned in
1181 *COMPOUND_STMT is an artificial outer scope, containing the
1182 function-try-block. */
1184 tree
1185 begin_function_try_block (tree *compound_stmt)
1187 tree r;
1188 /* This outer scope does not exist in the C++ standard, but we need
1189 a place to put __FUNCTION__ and similar variables. */
1190 *compound_stmt = begin_compound_stmt (0);
1191 r = begin_try_block ();
1192 FN_TRY_BLOCK_P (r) = 1;
1193 return r;
1196 /* Finish a try-block, which may be given by TRY_BLOCK. */
1198 void
1199 finish_try_block (tree try_block)
1201 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1202 TRY_HANDLERS (try_block) = push_stmt_list ();
1205 /* Finish the body of a cleanup try-block, which may be given by
1206 TRY_BLOCK. */
1208 void
1209 finish_cleanup_try_block (tree try_block)
1211 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1214 /* Finish an implicitly generated try-block, with a cleanup is given
1215 by CLEANUP. */
1217 void
1218 finish_cleanup (tree cleanup, tree try_block)
1220 TRY_HANDLERS (try_block) = cleanup;
1221 CLEANUP_P (try_block) = 1;
1224 /* Likewise, for a function-try-block. */
1226 void
1227 finish_function_try_block (tree try_block)
1229 finish_try_block (try_block);
1230 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1231 the try block, but moving it inside. */
1232 in_function_try_handler = 1;
1235 /* Finish a handler-sequence for a try-block, which may be given by
1236 TRY_BLOCK. */
1238 void
1239 finish_handler_sequence (tree try_block)
1241 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1242 check_handlers (TRY_HANDLERS (try_block));
1245 /* Finish the handler-seq for a function-try-block, given by
1246 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1247 begin_function_try_block. */
1249 void
1250 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1252 in_function_try_handler = 0;
1253 finish_handler_sequence (try_block);
1254 finish_compound_stmt (compound_stmt);
1257 /* Begin a handler. Returns a HANDLER if appropriate. */
1259 tree
1260 begin_handler (void)
1262 tree r;
1264 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1265 add_stmt (r);
1267 /* Create a binding level for the eh_info and the exception object
1268 cleanup. */
1269 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1271 return r;
1274 /* Finish the handler-parameters for a handler, which may be given by
1275 HANDLER. DECL is the declaration for the catch parameter, or NULL
1276 if this is a `catch (...)' clause. */
1278 void
1279 finish_handler_parms (tree decl, tree handler)
1281 tree type = NULL_TREE;
1282 if (processing_template_decl)
1284 if (decl)
1286 decl = pushdecl (decl);
1287 decl = push_template_decl (decl);
1288 HANDLER_PARMS (handler) = decl;
1289 type = TREE_TYPE (decl);
1292 else
1293 type = expand_start_catch_block (decl);
1294 HANDLER_TYPE (handler) = type;
1297 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1298 the return value from the matching call to finish_handler_parms. */
1300 void
1301 finish_handler (tree handler)
1303 if (!processing_template_decl)
1304 expand_end_catch_block ();
1305 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1308 /* Begin a compound statement. FLAGS contains some bits that control the
1309 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1310 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1311 block of a function. If BCS_TRY_BLOCK is set, this is the block
1312 created on behalf of a TRY statement. Returns a token to be passed to
1313 finish_compound_stmt. */
1315 tree
1316 begin_compound_stmt (unsigned int flags)
1318 tree r;
1320 if (flags & BCS_NO_SCOPE)
1322 r = push_stmt_list ();
1323 STATEMENT_LIST_NO_SCOPE (r) = 1;
1325 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1326 But, if it's a statement-expression with a scopeless block, there's
1327 nothing to keep, and we don't want to accidentally keep a block
1328 *inside* the scopeless block. */
1329 keep_next_level (false);
1331 else
1332 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1334 /* When processing a template, we need to remember where the braces were,
1335 so that we can set up identical scopes when instantiating the template
1336 later. BIND_EXPR is a handy candidate for this.
1337 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1338 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1339 processing templates. */
1340 if (processing_template_decl)
1342 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1343 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1344 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1345 TREE_SIDE_EFFECTS (r) = 1;
1348 return r;
1351 /* Finish a compound-statement, which is given by STMT. */
1353 void
1354 finish_compound_stmt (tree stmt)
1356 if (TREE_CODE (stmt) == BIND_EXPR)
1358 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1359 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1360 discard the BIND_EXPR so it can be merged with the containing
1361 STATEMENT_LIST. */
1362 if (TREE_CODE (body) == STATEMENT_LIST
1363 && STATEMENT_LIST_HEAD (body) == NULL
1364 && !BIND_EXPR_BODY_BLOCK (stmt)
1365 && !BIND_EXPR_TRY_BLOCK (stmt))
1366 stmt = body;
1367 else
1368 BIND_EXPR_BODY (stmt) = body;
1370 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1371 stmt = pop_stmt_list (stmt);
1372 else
1374 /* Destroy any ObjC "super" receivers that may have been
1375 created. */
1376 objc_clear_super_receiver ();
1378 stmt = do_poplevel (stmt);
1381 /* ??? See c_end_compound_stmt wrt statement expressions. */
1382 add_stmt (stmt);
1385 /* Finish an asm-statement, whose components are a STRING, some
1386 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1387 LABELS. Also note whether the asm-statement should be
1388 considered volatile. */
1390 tree
1391 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1392 tree input_operands, tree clobbers, tree labels)
1394 tree r;
1395 tree t;
1396 int ninputs = list_length (input_operands);
1397 int noutputs = list_length (output_operands);
1399 if (!processing_template_decl)
1401 const char *constraint;
1402 const char **oconstraints;
1403 bool allows_mem, allows_reg, is_inout;
1404 tree operand;
1405 int i;
1407 oconstraints = XALLOCAVEC (const char *, noutputs);
1409 string = resolve_asm_operand_names (string, output_operands,
1410 input_operands, labels);
1412 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1414 operand = TREE_VALUE (t);
1416 /* ??? Really, this should not be here. Users should be using a
1417 proper lvalue, dammit. But there's a long history of using
1418 casts in the output operands. In cases like longlong.h, this
1419 becomes a primitive form of typechecking -- if the cast can be
1420 removed, then the output operand had a type of the proper width;
1421 otherwise we'll get an error. Gross, but ... */
1422 STRIP_NOPS (operand);
1424 operand = mark_lvalue_use (operand);
1426 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1427 operand = error_mark_node;
1429 if (operand != error_mark_node
1430 && (TREE_READONLY (operand)
1431 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1432 /* Functions are not modifiable, even though they are
1433 lvalues. */
1434 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1435 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1436 /* If it's an aggregate and any field is const, then it is
1437 effectively const. */
1438 || (CLASS_TYPE_P (TREE_TYPE (operand))
1439 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1440 cxx_readonly_error (operand, lv_asm);
1442 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1443 oconstraints[i] = constraint;
1445 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1446 &allows_mem, &allows_reg, &is_inout))
1448 /* If the operand is going to end up in memory,
1449 mark it addressable. */
1450 if (!allows_reg && !cxx_mark_addressable (operand))
1451 operand = error_mark_node;
1453 else
1454 operand = error_mark_node;
1456 TREE_VALUE (t) = operand;
1459 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1461 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1462 bool constraint_parsed
1463 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1464 oconstraints, &allows_mem, &allows_reg);
1465 /* If the operand is going to end up in memory, don't call
1466 decay_conversion. */
1467 if (constraint_parsed && !allows_reg && allows_mem)
1468 operand = mark_lvalue_use (TREE_VALUE (t));
1469 else
1470 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1472 /* If the type of the operand hasn't been determined (e.g.,
1473 because it involves an overloaded function), then issue
1474 an error message. There's no context available to
1475 resolve the overloading. */
1476 if (TREE_TYPE (operand) == unknown_type_node)
1478 error ("type of asm operand %qE could not be determined",
1479 TREE_VALUE (t));
1480 operand = error_mark_node;
1483 if (constraint_parsed)
1485 /* If the operand is going to end up in memory,
1486 mark it addressable. */
1487 if (!allows_reg && allows_mem)
1489 /* Strip the nops as we allow this case. FIXME, this really
1490 should be rejected or made deprecated. */
1491 STRIP_NOPS (operand);
1492 if (!cxx_mark_addressable (operand))
1493 operand = error_mark_node;
1495 else if (!allows_reg && !allows_mem)
1497 /* If constraint allows neither register nor memory,
1498 try harder to get a constant. */
1499 tree constop = maybe_constant_value (operand);
1500 if (TREE_CONSTANT (constop))
1501 operand = constop;
1504 else
1505 operand = error_mark_node;
1507 TREE_VALUE (t) = operand;
1511 r = build_stmt (input_location, ASM_EXPR, string,
1512 output_operands, input_operands,
1513 clobbers, labels);
1514 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1515 r = maybe_cleanup_point_expr_void (r);
1516 return add_stmt (r);
1519 /* Finish a label with the indicated NAME. Returns the new label. */
1521 tree
1522 finish_label_stmt (tree name)
1524 tree decl = define_label (input_location, name);
1526 if (decl == error_mark_node)
1527 return error_mark_node;
1529 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1531 return decl;
1534 /* Finish a series of declarations for local labels. G++ allows users
1535 to declare "local" labels, i.e., labels with scope. This extension
1536 is useful when writing code involving statement-expressions. */
1538 void
1539 finish_label_decl (tree name)
1541 if (!at_function_scope_p ())
1543 error ("__label__ declarations are only allowed in function scopes");
1544 return;
1547 add_decl_expr (declare_local_label (name));
1550 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1552 void
1553 finish_decl_cleanup (tree decl, tree cleanup)
1555 push_cleanup (decl, cleanup, false);
1558 /* If the current scope exits with an exception, run CLEANUP. */
1560 void
1561 finish_eh_cleanup (tree cleanup)
1563 push_cleanup (NULL, cleanup, true);
1566 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1567 order they were written by the user. Each node is as for
1568 emit_mem_initializers. */
1570 void
1571 finish_mem_initializers (tree mem_inits)
1573 /* Reorder the MEM_INITS so that they are in the order they appeared
1574 in the source program. */
1575 mem_inits = nreverse (mem_inits);
1577 if (processing_template_decl)
1579 tree mem;
1581 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1583 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1584 check for bare parameter packs in the TREE_VALUE, because
1585 any parameter packs in the TREE_VALUE have already been
1586 bound as part of the TREE_PURPOSE. See
1587 make_pack_expansion for more information. */
1588 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1589 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1590 TREE_VALUE (mem) = error_mark_node;
1593 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1594 CTOR_INITIALIZER, mem_inits));
1596 else
1597 emit_mem_initializers (mem_inits);
1600 /* Obfuscate EXPR if it looks like an id-expression or member access so
1601 that the call to finish_decltype in do_auto_deduction will give the
1602 right result. */
1604 tree
1605 force_paren_expr (tree expr)
1607 /* This is only needed for decltype(auto) in C++14. */
1608 if (cxx_dialect < cxx1y)
1609 return expr;
1611 /* If we're in unevaluated context, we can't be deducing a
1612 return/initializer type, so we don't need to mess with this. */
1613 if (cp_unevaluated_operand)
1614 return expr;
1616 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1617 && TREE_CODE (expr) != SCOPE_REF)
1618 return expr;
1620 if (TREE_CODE (expr) == COMPONENT_REF)
1621 REF_PARENTHESIZED_P (expr) = true;
1622 else if (type_dependent_expression_p (expr))
1623 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1624 else
1626 cp_lvalue_kind kind = lvalue_kind (expr);
1627 if ((kind & ~clk_class) != clk_none)
1629 tree type = unlowered_expr_type (expr);
1630 bool rval = !!(kind & clk_rvalueref);
1631 type = cp_build_reference_type (type, rval);
1632 expr = build_static_cast (type, expr, tf_error);
1636 return expr;
1639 /* Finish a parenthesized expression EXPR. */
1641 tree
1642 finish_parenthesized_expr (tree expr)
1644 if (EXPR_P (expr))
1645 /* This inhibits warnings in c_common_truthvalue_conversion. */
1646 TREE_NO_WARNING (expr) = 1;
1648 if (TREE_CODE (expr) == OFFSET_REF
1649 || TREE_CODE (expr) == SCOPE_REF)
1650 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1651 enclosed in parentheses. */
1652 PTRMEM_OK_P (expr) = 0;
1654 if (TREE_CODE (expr) == STRING_CST)
1655 PAREN_STRING_LITERAL_P (expr) = 1;
1657 expr = force_paren_expr (expr);
1659 return expr;
1662 /* Finish a reference to a non-static data member (DECL) that is not
1663 preceded by `.' or `->'. */
1665 tree
1666 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1668 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1670 if (!object)
1672 tree scope = qualifying_scope;
1673 if (scope == NULL_TREE)
1674 scope = context_for_name_lookup (decl);
1675 object = maybe_dummy_object (scope, NULL);
1678 object = maybe_resolve_dummy (object);
1679 if (object == error_mark_node)
1680 return error_mark_node;
1682 /* DR 613: Can use non-static data members without an associated
1683 object in sizeof/decltype/alignof. */
1684 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1685 && (!processing_template_decl || !current_class_ref))
1687 if (current_function_decl
1688 && DECL_STATIC_FUNCTION_P (current_function_decl))
1689 error ("invalid use of member %q+D in static member function", decl);
1690 else
1691 error ("invalid use of non-static data member %q+D", decl);
1692 error ("from this location");
1694 return error_mark_node;
1697 if (current_class_ptr)
1698 TREE_USED (current_class_ptr) = 1;
1699 if (processing_template_decl && !qualifying_scope)
1701 tree type = TREE_TYPE (decl);
1703 if (TREE_CODE (type) == REFERENCE_TYPE)
1704 /* Quals on the object don't matter. */;
1705 else if (PACK_EXPANSION_P (type))
1706 /* Don't bother trying to represent this. */
1707 type = NULL_TREE;
1708 else
1710 /* Set the cv qualifiers. */
1711 int quals = cp_type_quals (TREE_TYPE (object));
1713 if (DECL_MUTABLE_P (decl))
1714 quals &= ~TYPE_QUAL_CONST;
1716 quals |= cp_type_quals (TREE_TYPE (decl));
1717 type = cp_build_qualified_type (type, quals);
1720 return (convert_from_reference
1721 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1723 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1724 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1725 for now. */
1726 else if (processing_template_decl)
1727 return build_qualified_name (TREE_TYPE (decl),
1728 qualifying_scope,
1729 decl,
1730 /*template_p=*/false);
1731 else
1733 tree access_type = TREE_TYPE (object);
1735 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1736 decl, tf_warning_or_error);
1738 /* If the data member was named `C::M', convert `*this' to `C'
1739 first. */
1740 if (qualifying_scope)
1742 tree binfo = NULL_TREE;
1743 object = build_scoped_ref (object, qualifying_scope,
1744 &binfo);
1747 return build_class_member_access_expr (object, decl,
1748 /*access_path=*/NULL_TREE,
1749 /*preserve_reference=*/false,
1750 tf_warning_or_error);
1754 /* If we are currently parsing a template and we encountered a typedef
1755 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1756 adds the typedef to a list tied to the current template.
1757 At template instantiation time, that list is walked and access check
1758 performed for each typedef.
1759 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1761 void
1762 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1763 tree context,
1764 location_t location)
1766 tree template_info = NULL;
1767 tree cs = current_scope ();
1769 if (!is_typedef_decl (typedef_decl)
1770 || !context
1771 || !CLASS_TYPE_P (context)
1772 || !cs)
1773 return;
1775 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1776 template_info = get_template_info (cs);
1778 if (template_info
1779 && TI_TEMPLATE (template_info)
1780 && !currently_open_class (context))
1781 append_type_to_template_for_access_check (cs, typedef_decl,
1782 context, location);
1785 /* DECL was the declaration to which a qualified-id resolved. Issue
1786 an error message if it is not accessible. If OBJECT_TYPE is
1787 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1788 type of `*x', or `x', respectively. If the DECL was named as
1789 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1791 void
1792 check_accessibility_of_qualified_id (tree decl,
1793 tree object_type,
1794 tree nested_name_specifier)
1796 tree scope;
1797 tree qualifying_type = NULL_TREE;
1799 /* If we are parsing a template declaration and if decl is a typedef,
1800 add it to a list tied to the template.
1801 At template instantiation time, that list will be walked and
1802 access check performed. */
1803 add_typedef_to_current_template_for_access_check (decl,
1804 nested_name_specifier
1805 ? nested_name_specifier
1806 : DECL_CONTEXT (decl),
1807 input_location);
1809 /* If we're not checking, return immediately. */
1810 if (deferred_access_no_check)
1811 return;
1813 /* Determine the SCOPE of DECL. */
1814 scope = context_for_name_lookup (decl);
1815 /* If the SCOPE is not a type, then DECL is not a member. */
1816 if (!TYPE_P (scope))
1817 return;
1818 /* Compute the scope through which DECL is being accessed. */
1819 if (object_type
1820 /* OBJECT_TYPE might not be a class type; consider:
1822 class A { typedef int I; };
1823 I *p;
1824 p->A::I::~I();
1826 In this case, we will have "A::I" as the DECL, but "I" as the
1827 OBJECT_TYPE. */
1828 && CLASS_TYPE_P (object_type)
1829 && DERIVED_FROM_P (scope, object_type))
1830 /* If we are processing a `->' or `.' expression, use the type of the
1831 left-hand side. */
1832 qualifying_type = object_type;
1833 else if (nested_name_specifier)
1835 /* If the reference is to a non-static member of the
1836 current class, treat it as if it were referenced through
1837 `this'. */
1838 if (DECL_NONSTATIC_MEMBER_P (decl)
1839 && current_class_ptr
1840 && DERIVED_FROM_P (scope, current_class_type))
1841 qualifying_type = current_class_type;
1842 /* Otherwise, use the type indicated by the
1843 nested-name-specifier. */
1844 else
1845 qualifying_type = nested_name_specifier;
1847 else
1848 /* Otherwise, the name must be from the current class or one of
1849 its bases. */
1850 qualifying_type = currently_open_derived_class (scope);
1852 if (qualifying_type
1853 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1854 or similar in a default argument value. */
1855 && CLASS_TYPE_P (qualifying_type)
1856 && !dependent_type_p (qualifying_type))
1857 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1858 decl, tf_warning_or_error);
1861 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1862 class named to the left of the "::" operator. DONE is true if this
1863 expression is a complete postfix-expression; it is false if this
1864 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1865 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1866 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1867 is true iff this qualified name appears as a template argument. */
1869 tree
1870 finish_qualified_id_expr (tree qualifying_class,
1871 tree expr,
1872 bool done,
1873 bool address_p,
1874 bool template_p,
1875 bool template_arg_p,
1876 tsubst_flags_t complain)
1878 gcc_assert (TYPE_P (qualifying_class));
1880 if (error_operand_p (expr))
1881 return error_mark_node;
1883 if ((DECL_P (expr) || BASELINK_P (expr))
1884 && !mark_used (expr, complain))
1885 return error_mark_node;
1887 if (template_p)
1888 check_template_keyword (expr);
1890 /* If EXPR occurs as the operand of '&', use special handling that
1891 permits a pointer-to-member. */
1892 if (address_p && done)
1894 if (TREE_CODE (expr) == SCOPE_REF)
1895 expr = TREE_OPERAND (expr, 1);
1896 expr = build_offset_ref (qualifying_class, expr,
1897 /*address_p=*/true, complain);
1898 return expr;
1901 /* No need to check access within an enum. */
1902 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1903 return expr;
1905 /* Within the scope of a class, turn references to non-static
1906 members into expression of the form "this->...". */
1907 if (template_arg_p)
1908 /* But, within a template argument, we do not want make the
1909 transformation, as there is no "this" pointer. */
1911 else if (TREE_CODE (expr) == FIELD_DECL)
1913 push_deferring_access_checks (dk_no_check);
1914 expr = finish_non_static_data_member (expr, NULL_TREE,
1915 qualifying_class);
1916 pop_deferring_access_checks ();
1918 else if (BASELINK_P (expr) && !processing_template_decl)
1920 /* See if any of the functions are non-static members. */
1921 /* If so, the expression may be relative to 'this'. */
1922 if (!shared_member_p (expr)
1923 && current_class_ptr
1924 && DERIVED_FROM_P (qualifying_class,
1925 current_nonlambda_class_type ()))
1926 expr = (build_class_member_access_expr
1927 (maybe_dummy_object (qualifying_class, NULL),
1928 expr,
1929 BASELINK_ACCESS_BINFO (expr),
1930 /*preserve_reference=*/false,
1931 complain));
1932 else if (done)
1933 /* The expression is a qualified name whose address is not
1934 being taken. */
1935 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1936 complain);
1938 else if (BASELINK_P (expr))
1940 else
1942 /* In a template, return a SCOPE_REF for most qualified-ids
1943 so that we can check access at instantiation time. But if
1944 we're looking at a member of the current instantiation, we
1945 know we have access and building up the SCOPE_REF confuses
1946 non-type template argument handling. */
1947 if (processing_template_decl
1948 && !currently_open_class (qualifying_class))
1949 expr = build_qualified_name (TREE_TYPE (expr),
1950 qualifying_class, expr,
1951 template_p);
1953 expr = convert_from_reference (expr);
1956 return expr;
1959 /* Begin a statement-expression. The value returned must be passed to
1960 finish_stmt_expr. */
1962 tree
1963 begin_stmt_expr (void)
1965 return push_stmt_list ();
1968 /* Process the final expression of a statement expression. EXPR can be
1969 NULL, if the final expression is empty. Return a STATEMENT_LIST
1970 containing all the statements in the statement-expression, or
1971 ERROR_MARK_NODE if there was an error. */
1973 tree
1974 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1976 if (error_operand_p (expr))
1978 /* The type of the statement-expression is the type of the last
1979 expression. */
1980 TREE_TYPE (stmt_expr) = error_mark_node;
1981 return error_mark_node;
1984 /* If the last statement does not have "void" type, then the value
1985 of the last statement is the value of the entire expression. */
1986 if (expr)
1988 tree type = TREE_TYPE (expr);
1990 if (processing_template_decl)
1992 expr = build_stmt (input_location, EXPR_STMT, expr);
1993 expr = add_stmt (expr);
1994 /* Mark the last statement so that we can recognize it as such at
1995 template-instantiation time. */
1996 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1998 else if (VOID_TYPE_P (type))
2000 /* Just treat this like an ordinary statement. */
2001 expr = finish_expr_stmt (expr);
2003 else
2005 /* It actually has a value we need to deal with. First, force it
2006 to be an rvalue so that we won't need to build up a copy
2007 constructor call later when we try to assign it to something. */
2008 expr = force_rvalue (expr, tf_warning_or_error);
2009 if (error_operand_p (expr))
2010 return error_mark_node;
2012 /* Update for array-to-pointer decay. */
2013 type = TREE_TYPE (expr);
2015 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2016 normal statement, but don't convert to void or actually add
2017 the EXPR_STMT. */
2018 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2019 expr = maybe_cleanup_point_expr (expr);
2020 add_stmt (expr);
2023 /* The type of the statement-expression is the type of the last
2024 expression. */
2025 TREE_TYPE (stmt_expr) = type;
2028 return stmt_expr;
2031 /* Finish a statement-expression. EXPR should be the value returned
2032 by the previous begin_stmt_expr. Returns an expression
2033 representing the statement-expression. */
2035 tree
2036 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2038 tree type;
2039 tree result;
2041 if (error_operand_p (stmt_expr))
2043 pop_stmt_list (stmt_expr);
2044 return error_mark_node;
2047 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2049 type = TREE_TYPE (stmt_expr);
2050 result = pop_stmt_list (stmt_expr);
2051 TREE_TYPE (result) = type;
2053 if (processing_template_decl)
2055 result = build_min (STMT_EXPR, type, result);
2056 TREE_SIDE_EFFECTS (result) = 1;
2057 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2059 else if (CLASS_TYPE_P (type))
2061 /* Wrap the statement-expression in a TARGET_EXPR so that the
2062 temporary object created by the final expression is destroyed at
2063 the end of the full-expression containing the
2064 statement-expression. */
2065 result = force_target_expr (type, result, tf_warning_or_error);
2068 return result;
2071 /* Returns the expression which provides the value of STMT_EXPR. */
2073 tree
2074 stmt_expr_value_expr (tree stmt_expr)
2076 tree t = STMT_EXPR_STMT (stmt_expr);
2078 if (TREE_CODE (t) == BIND_EXPR)
2079 t = BIND_EXPR_BODY (t);
2081 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2082 t = STATEMENT_LIST_TAIL (t)->stmt;
2084 if (TREE_CODE (t) == EXPR_STMT)
2085 t = EXPR_STMT_EXPR (t);
2087 return t;
2090 /* Return TRUE iff EXPR_STMT is an empty list of
2091 expression statements. */
2093 bool
2094 empty_expr_stmt_p (tree expr_stmt)
2096 tree body = NULL_TREE;
2098 if (expr_stmt == void_zero_node)
2099 return true;
2101 if (expr_stmt)
2103 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2104 body = EXPR_STMT_EXPR (expr_stmt);
2105 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2106 body = expr_stmt;
2109 if (body)
2111 if (TREE_CODE (body) == STATEMENT_LIST)
2112 return tsi_end_p (tsi_start (body));
2113 else
2114 return empty_expr_stmt_p (body);
2116 return false;
2119 /* Perform Koenig lookup. FN is the postfix-expression representing
2120 the function (or functions) to call; ARGS are the arguments to the
2121 call. Returns the functions to be considered by overload resolution. */
2123 tree
2124 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2125 tsubst_flags_t complain)
2127 tree identifier = NULL_TREE;
2128 tree functions = NULL_TREE;
2129 tree tmpl_args = NULL_TREE;
2130 bool template_id = false;
2132 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2134 /* Use a separate flag to handle null args. */
2135 template_id = true;
2136 tmpl_args = TREE_OPERAND (fn, 1);
2137 fn = TREE_OPERAND (fn, 0);
2140 /* Find the name of the overloaded function. */
2141 if (identifier_p (fn))
2142 identifier = fn;
2143 else if (is_overloaded_fn (fn))
2145 functions = fn;
2146 identifier = DECL_NAME (get_first_fn (functions));
2148 else if (DECL_P (fn))
2150 functions = fn;
2151 identifier = DECL_NAME (fn);
2154 /* A call to a namespace-scope function using an unqualified name.
2156 Do Koenig lookup -- unless any of the arguments are
2157 type-dependent. */
2158 if (!any_type_dependent_arguments_p (args)
2159 && !any_dependent_template_arguments_p (tmpl_args))
2161 fn = lookup_arg_dependent (identifier, functions, args);
2162 if (!fn)
2164 /* The unqualified name could not be resolved. */
2165 if (complain)
2166 fn = unqualified_fn_lookup_error (identifier);
2167 else
2168 fn = identifier;
2172 if (fn && template_id)
2173 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2175 return fn;
2178 /* Generate an expression for `FN (ARGS)'. This may change the
2179 contents of ARGS.
2181 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2182 as a virtual call, even if FN is virtual. (This flag is set when
2183 encountering an expression where the function name is explicitly
2184 qualified. For example a call to `X::f' never generates a virtual
2185 call.)
2187 Returns code for the call. */
2189 tree
2190 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2191 bool koenig_p, tsubst_flags_t complain)
2193 tree result;
2194 tree orig_fn;
2195 vec<tree, va_gc> *orig_args = NULL;
2197 if (fn == error_mark_node)
2198 return error_mark_node;
2200 gcc_assert (!TYPE_P (fn));
2202 orig_fn = fn;
2204 if (processing_template_decl)
2206 /* If the call expression is dependent, build a CALL_EXPR node
2207 with no type; type_dependent_expression_p recognizes
2208 expressions with no type as being dependent. */
2209 if (type_dependent_expression_p (fn)
2210 || any_type_dependent_arguments_p (*args)
2211 /* For a non-static member function that doesn't have an
2212 explicit object argument, we need to specifically
2213 test the type dependency of the "this" pointer because it
2214 is not included in *ARGS even though it is considered to
2215 be part of the list of arguments. Note that this is
2216 related to CWG issues 515 and 1005. */
2217 || (TREE_CODE (fn) != COMPONENT_REF
2218 && non_static_member_function_p (fn)
2219 && current_class_ref
2220 && type_dependent_expression_p (current_class_ref)))
2222 result = build_nt_call_vec (fn, *args);
2223 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2224 KOENIG_LOOKUP_P (result) = koenig_p;
2225 if (cfun)
2229 tree fndecl = OVL_CURRENT (fn);
2230 if (TREE_CODE (fndecl) != FUNCTION_DECL
2231 || !TREE_THIS_VOLATILE (fndecl))
2232 break;
2233 fn = OVL_NEXT (fn);
2235 while (fn);
2236 if (!fn)
2237 current_function_returns_abnormally = 1;
2239 return result;
2241 orig_args = make_tree_vector_copy (*args);
2242 if (!BASELINK_P (fn)
2243 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2244 && TREE_TYPE (fn) != unknown_type_node)
2245 fn = build_non_dependent_expr (fn);
2246 make_args_non_dependent (*args);
2249 if (TREE_CODE (fn) == COMPONENT_REF)
2251 tree member = TREE_OPERAND (fn, 1);
2252 if (BASELINK_P (member))
2254 tree object = TREE_OPERAND (fn, 0);
2255 return build_new_method_call (object, member,
2256 args, NULL_TREE,
2257 (disallow_virtual
2258 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2259 : LOOKUP_NORMAL),
2260 /*fn_p=*/NULL,
2261 complain);
2265 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2266 if (TREE_CODE (fn) == ADDR_EXPR
2267 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2268 fn = TREE_OPERAND (fn, 0);
2270 if (is_overloaded_fn (fn))
2271 fn = baselink_for_fns (fn);
2273 result = NULL_TREE;
2274 if (BASELINK_P (fn))
2276 tree object;
2278 /* A call to a member function. From [over.call.func]:
2280 If the keyword this is in scope and refers to the class of
2281 that member function, or a derived class thereof, then the
2282 function call is transformed into a qualified function call
2283 using (*this) as the postfix-expression to the left of the
2284 . operator.... [Otherwise] a contrived object of type T
2285 becomes the implied object argument.
2287 In this situation:
2289 struct A { void f(); };
2290 struct B : public A {};
2291 struct C : public A { void g() { B::f(); }};
2293 "the class of that member function" refers to `A'. But 11.2
2294 [class.access.base] says that we need to convert 'this' to B* as
2295 part of the access, so we pass 'B' to maybe_dummy_object. */
2297 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2298 NULL);
2300 if (processing_template_decl)
2302 if (type_dependent_expression_p (object))
2304 tree ret = build_nt_call_vec (orig_fn, orig_args);
2305 release_tree_vector (orig_args);
2306 return ret;
2308 object = build_non_dependent_expr (object);
2311 result = build_new_method_call (object, fn, args, NULL_TREE,
2312 (disallow_virtual
2313 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2314 : LOOKUP_NORMAL),
2315 /*fn_p=*/NULL,
2316 complain);
2318 else if (is_overloaded_fn (fn))
2320 /* If the function is an overloaded builtin, resolve it. */
2321 if (TREE_CODE (fn) == FUNCTION_DECL
2322 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2323 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2324 result = resolve_overloaded_builtin (input_location, fn, *args);
2326 if (!result)
2328 if (warn_sizeof_pointer_memaccess
2329 && !vec_safe_is_empty (*args)
2330 && !processing_template_decl)
2332 location_t sizeof_arg_loc[3];
2333 tree sizeof_arg[3];
2334 unsigned int i;
2335 for (i = 0; i < 3; i++)
2337 tree t;
2339 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2340 sizeof_arg[i] = NULL_TREE;
2341 if (i >= (*args)->length ())
2342 continue;
2343 t = (**args)[i];
2344 if (TREE_CODE (t) != SIZEOF_EXPR)
2345 continue;
2346 if (SIZEOF_EXPR_TYPE_P (t))
2347 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2348 else
2349 sizeof_arg[i] = TREE_OPERAND (t, 0);
2350 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2352 sizeof_pointer_memaccess_warning
2353 (sizeof_arg_loc, fn, *args,
2354 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2357 /* A call to a namespace-scope function. */
2358 result = build_new_function_call (fn, args, koenig_p, complain);
2361 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2363 if (!vec_safe_is_empty (*args))
2364 error ("arguments to destructor are not allowed");
2365 /* Mark the pseudo-destructor call as having side-effects so
2366 that we do not issue warnings about its use. */
2367 result = build1 (NOP_EXPR,
2368 void_type_node,
2369 TREE_OPERAND (fn, 0));
2370 TREE_SIDE_EFFECTS (result) = 1;
2372 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2373 /* If the "function" is really an object of class type, it might
2374 have an overloaded `operator ()'. */
2375 result = build_op_call (fn, args, complain);
2377 if (!result)
2378 /* A call where the function is unknown. */
2379 result = cp_build_function_call_vec (fn, args, complain);
2381 if (processing_template_decl && result != error_mark_node)
2383 if (INDIRECT_REF_P (result))
2384 result = TREE_OPERAND (result, 0);
2385 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2386 SET_EXPR_LOCATION (result, input_location);
2387 KOENIG_LOOKUP_P (result) = koenig_p;
2388 release_tree_vector (orig_args);
2389 result = convert_from_reference (result);
2392 if (koenig_p)
2394 /* Free garbage OVERLOADs from arg-dependent lookup. */
2395 tree next = NULL_TREE;
2396 for (fn = orig_fn;
2397 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2398 fn = next)
2400 if (processing_template_decl)
2401 /* In a template, we'll re-use them at instantiation time. */
2402 OVL_ARG_DEPENDENT (fn) = false;
2403 else
2405 next = OVL_CHAIN (fn);
2406 ggc_free (fn);
2411 return result;
2414 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2415 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2416 POSTDECREMENT_EXPR.) */
2418 tree
2419 finish_increment_expr (tree expr, enum tree_code code)
2421 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2424 /* Finish a use of `this'. Returns an expression for `this'. */
2426 tree
2427 finish_this_expr (void)
2429 tree result;
2431 if (current_class_ptr)
2433 tree type = TREE_TYPE (current_class_ref);
2435 /* In a lambda expression, 'this' refers to the captured 'this'. */
2436 if (LAMBDA_TYPE_P (type))
2437 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2438 else
2439 result = current_class_ptr;
2441 else if (current_function_decl
2442 && DECL_STATIC_FUNCTION_P (current_function_decl))
2444 error ("%<this%> is unavailable for static member functions");
2445 result = error_mark_node;
2447 else
2449 if (current_function_decl)
2450 error ("invalid use of %<this%> in non-member function");
2451 else
2452 error ("invalid use of %<this%> at top level");
2453 result = error_mark_node;
2456 /* The keyword 'this' is a prvalue expression. */
2457 result = rvalue (result);
2459 return result;
2462 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2463 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2464 the TYPE for the type given. If SCOPE is non-NULL, the expression
2465 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2467 tree
2468 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2469 location_t loc)
2471 if (object == error_mark_node || destructor == error_mark_node)
2472 return error_mark_node;
2474 gcc_assert (TYPE_P (destructor));
2476 if (!processing_template_decl)
2478 if (scope == error_mark_node)
2480 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2481 return error_mark_node;
2483 if (is_auto (destructor))
2484 destructor = TREE_TYPE (object);
2485 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2487 error_at (loc,
2488 "qualified type %qT does not match destructor name ~%qT",
2489 scope, destructor);
2490 return error_mark_node;
2494 /* [expr.pseudo] says both:
2496 The type designated by the pseudo-destructor-name shall be
2497 the same as the object type.
2499 and:
2501 The cv-unqualified versions of the object type and of the
2502 type designated by the pseudo-destructor-name shall be the
2503 same type.
2505 We implement the more generous second sentence, since that is
2506 what most other compilers do. */
2507 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2508 destructor))
2510 error_at (loc, "%qE is not of type %qT", object, destructor);
2511 return error_mark_node;
2515 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2516 scope, destructor);
2519 /* Finish an expression of the form CODE EXPR. */
2521 tree
2522 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2523 tsubst_flags_t complain)
2525 tree result = build_x_unary_op (loc, code, expr, complain);
2526 if ((complain & tf_warning)
2527 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2528 overflow_warning (input_location, result);
2530 return result;
2533 /* Finish a compound-literal expression. TYPE is the type to which
2534 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2536 tree
2537 finish_compound_literal (tree type, tree compound_literal,
2538 tsubst_flags_t complain)
2540 if (type == error_mark_node)
2541 return error_mark_node;
2543 if (TREE_CODE (type) == REFERENCE_TYPE)
2545 compound_literal
2546 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2547 complain);
2548 return cp_build_c_cast (type, compound_literal, complain);
2551 if (!TYPE_OBJ_P (type))
2553 if (complain & tf_error)
2554 error ("compound literal of non-object type %qT", type);
2555 return error_mark_node;
2558 if (processing_template_decl)
2560 TREE_TYPE (compound_literal) = type;
2561 /* Mark the expression as a compound literal. */
2562 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2563 return compound_literal;
2566 type = complete_type (type);
2568 if (TYPE_NON_AGGREGATE_CLASS (type))
2570 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2571 everywhere that deals with function arguments would be a pain, so
2572 just wrap it in a TREE_LIST. The parser set a flag so we know
2573 that it came from T{} rather than T({}). */
2574 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2575 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2576 return build_functional_cast (type, compound_literal, complain);
2579 if (TREE_CODE (type) == ARRAY_TYPE
2580 && check_array_initializer (NULL_TREE, type, compound_literal))
2581 return error_mark_node;
2582 compound_literal = reshape_init (type, compound_literal, complain);
2583 if (SCALAR_TYPE_P (type)
2584 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2585 && (complain & tf_warning_or_error))
2586 check_narrowing (type, compound_literal);
2587 if (TREE_CODE (type) == ARRAY_TYPE
2588 && TYPE_DOMAIN (type) == NULL_TREE)
2590 cp_complete_array_type_or_error (&type, compound_literal,
2591 false, complain);
2592 if (type == error_mark_node)
2593 return error_mark_node;
2595 compound_literal = digest_init (type, compound_literal, complain);
2596 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2597 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2598 /* Put static/constant array temporaries in static variables, but always
2599 represent class temporaries with TARGET_EXPR so we elide copies. */
2600 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2601 && TREE_CODE (type) == ARRAY_TYPE
2602 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2603 && !cp_unevaluated_operand
2604 && initializer_constant_valid_p (compound_literal, type))
2606 tree decl = create_temporary_var (type);
2607 DECL_INITIAL (decl) = compound_literal;
2608 TREE_STATIC (decl) = 1;
2609 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2611 /* 5.19 says that a constant expression can include an
2612 lvalue-rvalue conversion applied to "a glvalue of literal type
2613 that refers to a non-volatile temporary object initialized
2614 with a constant expression". Rather than try to communicate
2615 that this VAR_DECL is a temporary, just mark it constexpr. */
2616 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2617 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2618 TREE_CONSTANT (decl) = true;
2620 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2621 decl = pushdecl_top_level (decl);
2622 DECL_NAME (decl) = make_anon_name ();
2623 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2624 /* Make sure the destructor is callable. */
2625 tree clean = cxx_maybe_build_cleanup (decl, complain);
2626 if (clean == error_mark_node)
2627 return error_mark_node;
2628 return decl;
2630 else
2631 return get_target_expr_sfinae (compound_literal, complain);
2634 /* Return the declaration for the function-name variable indicated by
2635 ID. */
2637 tree
2638 finish_fname (tree id)
2640 tree decl;
2642 decl = fname_decl (input_location, C_RID_CODE (id), id);
2643 if (processing_template_decl && current_function_decl
2644 && decl != error_mark_node)
2645 decl = DECL_NAME (decl);
2646 return decl;
2649 /* Finish a translation unit. */
2651 void
2652 finish_translation_unit (void)
2654 /* In case there were missing closebraces,
2655 get us back to the global binding level. */
2656 pop_everything ();
2657 while (current_namespace != global_namespace)
2658 pop_namespace ();
2660 /* Do file scope __FUNCTION__ et al. */
2661 finish_fname_decls ();
2664 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2665 Returns the parameter. */
2667 tree
2668 finish_template_type_parm (tree aggr, tree identifier)
2670 if (aggr != class_type_node)
2672 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2673 aggr = class_type_node;
2676 return build_tree_list (aggr, identifier);
2679 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2680 Returns the parameter. */
2682 tree
2683 finish_template_template_parm (tree aggr, tree identifier)
2685 tree decl = build_decl (input_location,
2686 TYPE_DECL, identifier, NULL_TREE);
2687 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2688 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2689 DECL_TEMPLATE_RESULT (tmpl) = decl;
2690 DECL_ARTIFICIAL (decl) = 1;
2691 end_template_decl ();
2693 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2695 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2696 /*is_primary=*/true, /*is_partial=*/false,
2697 /*is_friend=*/0);
2699 return finish_template_type_parm (aggr, tmpl);
2702 /* ARGUMENT is the default-argument value for a template template
2703 parameter. If ARGUMENT is invalid, issue error messages and return
2704 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2706 tree
2707 check_template_template_default_arg (tree argument)
2709 if (TREE_CODE (argument) != TEMPLATE_DECL
2710 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2711 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2713 if (TREE_CODE (argument) == TYPE_DECL)
2714 error ("invalid use of type %qT as a default value for a template "
2715 "template-parameter", TREE_TYPE (argument));
2716 else
2717 error ("invalid default argument for a template template parameter");
2718 return error_mark_node;
2721 return argument;
2724 /* Begin a class definition, as indicated by T. */
2726 tree
2727 begin_class_definition (tree t)
2729 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2730 return error_mark_node;
2732 if (processing_template_parmlist)
2734 error ("definition of %q#T inside template parameter list", t);
2735 return error_mark_node;
2738 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2739 are passed the same as decimal scalar types. */
2740 if (TREE_CODE (t) == RECORD_TYPE
2741 && !processing_template_decl)
2743 tree ns = TYPE_CONTEXT (t);
2744 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2745 && DECL_CONTEXT (ns) == std_node
2746 && DECL_NAME (ns)
2747 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2749 const char *n = TYPE_NAME_STRING (t);
2750 if ((strcmp (n, "decimal32") == 0)
2751 || (strcmp (n, "decimal64") == 0)
2752 || (strcmp (n, "decimal128") == 0))
2753 TYPE_TRANSPARENT_AGGR (t) = 1;
2757 /* A non-implicit typename comes from code like:
2759 template <typename T> struct A {
2760 template <typename U> struct A<T>::B ...
2762 This is erroneous. */
2763 else if (TREE_CODE (t) == TYPENAME_TYPE)
2765 error ("invalid definition of qualified type %qT", t);
2766 t = error_mark_node;
2769 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2771 t = make_class_type (RECORD_TYPE);
2772 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2775 if (TYPE_BEING_DEFINED (t))
2777 t = make_class_type (TREE_CODE (t));
2778 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2780 maybe_process_partial_specialization (t);
2781 pushclass (t);
2782 TYPE_BEING_DEFINED (t) = 1;
2783 class_binding_level->defining_class_p = 1;
2785 if (flag_pack_struct)
2787 tree v;
2788 TYPE_PACKED (t) = 1;
2789 /* Even though the type is being defined for the first time
2790 here, there might have been a forward declaration, so there
2791 might be cv-qualified variants of T. */
2792 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2793 TYPE_PACKED (v) = 1;
2795 /* Reset the interface data, at the earliest possible
2796 moment, as it might have been set via a class foo;
2797 before. */
2798 if (! TYPE_ANONYMOUS_P (t))
2800 struct c_fileinfo *finfo = \
2801 get_fileinfo (LOCATION_FILE (input_location));
2802 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2803 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2804 (t, finfo->interface_unknown);
2806 reset_specialization();
2808 /* Make a declaration for this class in its own scope. */
2809 build_self_reference ();
2811 return t;
2814 /* Finish the member declaration given by DECL. */
2816 void
2817 finish_member_declaration (tree decl)
2819 if (decl == error_mark_node || decl == NULL_TREE)
2820 return;
2822 if (decl == void_type_node)
2823 /* The COMPONENT was a friend, not a member, and so there's
2824 nothing for us to do. */
2825 return;
2827 /* We should see only one DECL at a time. */
2828 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2830 /* Set up access control for DECL. */
2831 TREE_PRIVATE (decl)
2832 = (current_access_specifier == access_private_node);
2833 TREE_PROTECTED (decl)
2834 = (current_access_specifier == access_protected_node);
2835 if (TREE_CODE (decl) == TEMPLATE_DECL)
2837 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2838 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2841 /* Mark the DECL as a member of the current class, unless it's
2842 a member of an enumeration. */
2843 if (TREE_CODE (decl) != CONST_DECL)
2844 DECL_CONTEXT (decl) = current_class_type;
2846 /* Check for bare parameter packs in the member variable declaration. */
2847 if (TREE_CODE (decl) == FIELD_DECL)
2849 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2850 TREE_TYPE (decl) = error_mark_node;
2851 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2852 DECL_ATTRIBUTES (decl) = NULL_TREE;
2855 /* [dcl.link]
2857 A C language linkage is ignored for the names of class members
2858 and the member function type of class member functions. */
2859 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2860 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2862 /* Put functions on the TYPE_METHODS list and everything else on the
2863 TYPE_FIELDS list. Note that these are built up in reverse order.
2864 We reverse them (to obtain declaration order) in finish_struct. */
2865 if (DECL_DECLARES_FUNCTION_P (decl))
2867 /* We also need to add this function to the
2868 CLASSTYPE_METHOD_VEC. */
2869 if (add_method (current_class_type, decl, NULL_TREE))
2871 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2872 TYPE_METHODS (current_class_type) = decl;
2874 maybe_add_class_template_decl_list (current_class_type, decl,
2875 /*friend_p=*/0);
2878 /* Enter the DECL into the scope of the class, if the class
2879 isn't a closure (whose fields are supposed to be unnamed). */
2880 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2881 || pushdecl_class_level (decl))
2883 if (TREE_CODE (decl) == USING_DECL)
2885 /* For now, ignore class-scope USING_DECLS, so that
2886 debugging backends do not see them. */
2887 DECL_IGNORED_P (decl) = 1;
2890 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2891 go at the beginning. The reason is that lookup_field_1
2892 searches the list in order, and we want a field name to
2893 override a type name so that the "struct stat hack" will
2894 work. In particular:
2896 struct S { enum E { }; int E } s;
2897 s.E = 3;
2899 is valid. In addition, the FIELD_DECLs must be maintained in
2900 declaration order so that class layout works as expected.
2901 However, we don't need that order until class layout, so we
2902 save a little time by putting FIELD_DECLs on in reverse order
2903 here, and then reversing them in finish_struct_1. (We could
2904 also keep a pointer to the correct insertion points in the
2905 list.) */
2907 if (TREE_CODE (decl) == TYPE_DECL)
2908 TYPE_FIELDS (current_class_type)
2909 = chainon (TYPE_FIELDS (current_class_type), decl);
2910 else
2912 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2913 TYPE_FIELDS (current_class_type) = decl;
2916 maybe_add_class_template_decl_list (current_class_type, decl,
2917 /*friend_p=*/0);
2920 if (pch_file)
2921 note_decl_for_pch (decl);
2924 /* DECL has been declared while we are building a PCH file. Perform
2925 actions that we might normally undertake lazily, but which can be
2926 performed now so that they do not have to be performed in
2927 translation units which include the PCH file. */
2929 void
2930 note_decl_for_pch (tree decl)
2932 gcc_assert (pch_file);
2934 /* There's a good chance that we'll have to mangle names at some
2935 point, even if only for emission in debugging information. */
2936 if (VAR_OR_FUNCTION_DECL_P (decl)
2937 && !processing_template_decl)
2938 mangle_decl (decl);
2941 /* Finish processing a complete template declaration. The PARMS are
2942 the template parameters. */
2944 void
2945 finish_template_decl (tree parms)
2947 if (parms)
2948 end_template_decl ();
2949 else
2950 end_specialization ();
2953 /* Finish processing a template-id (which names a type) of the form
2954 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2955 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2956 the scope of template-id indicated. */
2958 tree
2959 finish_template_type (tree name, tree args, int entering_scope)
2961 tree type;
2963 type = lookup_template_class (name, args,
2964 NULL_TREE, NULL_TREE, entering_scope,
2965 tf_warning_or_error | tf_user);
2966 if (type == error_mark_node)
2967 return type;
2968 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2969 return TYPE_STUB_DECL (type);
2970 else
2971 return TYPE_NAME (type);
2974 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2975 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2976 BASE_CLASS, or NULL_TREE if an error occurred. The
2977 ACCESS_SPECIFIER is one of
2978 access_{default,public,protected_private}_node. For a virtual base
2979 we set TREE_TYPE. */
2981 tree
2982 finish_base_specifier (tree base, tree access, bool virtual_p)
2984 tree result;
2986 if (base == error_mark_node)
2988 error ("invalid base-class specification");
2989 result = NULL_TREE;
2991 else if (! MAYBE_CLASS_TYPE_P (base))
2993 error ("%qT is not a class type", base);
2994 result = NULL_TREE;
2996 else
2998 if (cp_type_quals (base) != 0)
3000 /* DR 484: Can a base-specifier name a cv-qualified
3001 class type? */
3002 base = TYPE_MAIN_VARIANT (base);
3004 result = build_tree_list (access, base);
3005 if (virtual_p)
3006 TREE_TYPE (result) = integer_type_node;
3009 return result;
3012 /* If FNS is a member function, a set of member functions, or a
3013 template-id referring to one or more member functions, return a
3014 BASELINK for FNS, incorporating the current access context.
3015 Otherwise, return FNS unchanged. */
3017 tree
3018 baselink_for_fns (tree fns)
3020 tree scope;
3021 tree cl;
3023 if (BASELINK_P (fns)
3024 || error_operand_p (fns))
3025 return fns;
3027 scope = ovl_scope (fns);
3028 if (!CLASS_TYPE_P (scope))
3029 return fns;
3031 cl = currently_open_derived_class (scope);
3032 if (!cl)
3033 cl = scope;
3034 cl = TYPE_BINFO (cl);
3035 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3038 /* Returns true iff DECL is a variable from a function outside
3039 the current one. */
3041 static bool
3042 outer_var_p (tree decl)
3044 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3045 && DECL_FUNCTION_SCOPE_P (decl)
3046 && (DECL_CONTEXT (decl) != current_function_decl
3047 || parsing_nsdmi ()));
3050 /* As above, but also checks that DECL is automatic. */
3052 static bool
3053 outer_automatic_var_p (tree decl)
3055 return (outer_var_p (decl)
3056 && !TREE_STATIC (decl));
3059 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3060 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3061 if non-NULL, is the type or namespace used to explicitly qualify
3062 ID_EXPRESSION. DECL is the entity to which that name has been
3063 resolved.
3065 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3066 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3067 be set to true if this expression isn't permitted in a
3068 constant-expression, but it is otherwise not set by this function.
3069 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3070 constant-expression, but a non-constant expression is also
3071 permissible.
3073 DONE is true if this expression is a complete postfix-expression;
3074 it is false if this expression is followed by '->', '[', '(', etc.
3075 ADDRESS_P is true iff this expression is the operand of '&'.
3076 TEMPLATE_P is true iff the qualified-id was of the form
3077 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3078 appears as a template argument.
3080 If an error occurs, and it is the kind of error that might cause
3081 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3082 is the caller's responsibility to issue the message. *ERROR_MSG
3083 will be a string with static storage duration, so the caller need
3084 not "free" it.
3086 Return an expression for the entity, after issuing appropriate
3087 diagnostics. This function is also responsible for transforming a
3088 reference to a non-static member into a COMPONENT_REF that makes
3089 the use of "this" explicit.
3091 Upon return, *IDK will be filled in appropriately. */
3092 tree
3093 finish_id_expression (tree id_expression,
3094 tree decl,
3095 tree scope,
3096 cp_id_kind *idk,
3097 bool integral_constant_expression_p,
3098 bool allow_non_integral_constant_expression_p,
3099 bool *non_integral_constant_expression_p,
3100 bool template_p,
3101 bool done,
3102 bool address_p,
3103 bool template_arg_p,
3104 const char **error_msg,
3105 location_t location)
3107 decl = strip_using_decl (decl);
3109 /* Initialize the output parameters. */
3110 *idk = CP_ID_KIND_NONE;
3111 *error_msg = NULL;
3113 if (id_expression == error_mark_node)
3114 return error_mark_node;
3115 /* If we have a template-id, then no further lookup is
3116 required. If the template-id was for a template-class, we
3117 will sometimes have a TYPE_DECL at this point. */
3118 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3119 || TREE_CODE (decl) == TYPE_DECL)
3121 /* Look up the name. */
3122 else
3124 if (decl == error_mark_node)
3126 /* Name lookup failed. */
3127 if (scope
3128 && (!TYPE_P (scope)
3129 || (!dependent_type_p (scope)
3130 && !(identifier_p (id_expression)
3131 && IDENTIFIER_TYPENAME_P (id_expression)
3132 && dependent_type_p (TREE_TYPE (id_expression))))))
3134 /* If the qualifying type is non-dependent (and the name
3135 does not name a conversion operator to a dependent
3136 type), issue an error. */
3137 qualified_name_lookup_error (scope, id_expression, decl, location);
3138 return error_mark_node;
3140 else if (!scope)
3142 /* It may be resolved via Koenig lookup. */
3143 *idk = CP_ID_KIND_UNQUALIFIED;
3144 return id_expression;
3146 else
3147 decl = id_expression;
3149 /* If DECL is a variable that would be out of scope under
3150 ANSI/ISO rules, but in scope in the ARM, name lookup
3151 will succeed. Issue a diagnostic here. */
3152 else
3153 decl = check_for_out_of_scope_variable (decl);
3155 /* Remember that the name was used in the definition of
3156 the current class so that we can check later to see if
3157 the meaning would have been different after the class
3158 was entirely defined. */
3159 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3160 maybe_note_name_used_in_class (id_expression, decl);
3162 /* Disallow uses of local variables from containing functions, except
3163 within lambda-expressions. */
3164 if (!outer_var_p (decl))
3165 /* OK */;
3166 else if (TREE_STATIC (decl)
3167 /* It's not a use (3.2) if we're in an unevaluated context. */
3168 || cp_unevaluated_operand)
3170 if (processing_template_decl)
3171 /* For a use of an outer static/unevaluated var, return the id
3172 so that we'll look it up again in the instantiation. */
3173 return id_expression;
3175 else
3177 tree context = DECL_CONTEXT (decl);
3178 tree containing_function = current_function_decl;
3179 tree lambda_stack = NULL_TREE;
3180 tree lambda_expr = NULL_TREE;
3181 tree initializer = convert_from_reference (decl);
3183 /* Mark it as used now even if the use is ill-formed. */
3184 mark_used (decl);
3186 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3187 support for an approach in which a reference to a local
3188 [constant] automatic variable in a nested class or lambda body
3189 would enter the expression as an rvalue, which would reduce
3190 the complexity of the problem"
3192 FIXME update for final resolution of core issue 696. */
3193 if (decl_constant_var_p (decl))
3195 if (processing_template_decl)
3196 /* In a template, the constant value may not be in a usable
3197 form, so look it up again at instantiation time. */
3198 return id_expression;
3199 else
3200 return integral_constant_value (decl);
3203 if (parsing_nsdmi ())
3204 containing_function = NULL_TREE;
3205 /* If we are in a lambda function, we can move out until we hit
3206 1. the context,
3207 2. a non-lambda function, or
3208 3. a non-default capturing lambda function. */
3209 else while (context != containing_function
3210 && LAMBDA_FUNCTION_P (containing_function))
3212 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3213 (DECL_CONTEXT (containing_function));
3215 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3216 == CPLD_NONE)
3217 break;
3219 lambda_stack = tree_cons (NULL_TREE,
3220 lambda_expr,
3221 lambda_stack);
3223 containing_function
3224 = decl_function_context (containing_function);
3227 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3228 && DECL_ANON_UNION_VAR_P (decl))
3230 error ("cannot capture member %qD of anonymous union", decl);
3231 return error_mark_node;
3233 if (context == containing_function)
3235 decl = add_default_capture (lambda_stack,
3236 /*id=*/DECL_NAME (decl),
3237 initializer);
3239 else if (lambda_expr)
3241 error ("%qD is not captured", decl);
3242 return error_mark_node;
3244 else
3246 error (VAR_P (decl)
3247 ? G_("use of local variable with automatic storage from containing function")
3248 : G_("use of parameter from containing function"));
3249 inform (input_location, "%q+#D declared here", decl);
3250 return error_mark_node;
3254 /* Also disallow uses of function parameters outside the function
3255 body, except inside an unevaluated context (i.e. decltype). */
3256 if (TREE_CODE (decl) == PARM_DECL
3257 && DECL_CONTEXT (decl) == NULL_TREE
3258 && !cp_unevaluated_operand)
3260 *error_msg = "use of parameter outside function body";
3261 return error_mark_node;
3265 /* If we didn't find anything, or what we found was a type,
3266 then this wasn't really an id-expression. */
3267 if (TREE_CODE (decl) == TEMPLATE_DECL
3268 && !DECL_FUNCTION_TEMPLATE_P (decl))
3270 *error_msg = "missing template arguments";
3271 return error_mark_node;
3273 else if (TREE_CODE (decl) == TYPE_DECL
3274 || TREE_CODE (decl) == NAMESPACE_DECL)
3276 *error_msg = "expected primary-expression";
3277 return error_mark_node;
3280 /* If the name resolved to a template parameter, there is no
3281 need to look it up again later. */
3282 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3283 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3285 tree r;
3287 *idk = CP_ID_KIND_NONE;
3288 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3289 decl = TEMPLATE_PARM_DECL (decl);
3290 r = convert_from_reference (DECL_INITIAL (decl));
3292 if (integral_constant_expression_p
3293 && !dependent_type_p (TREE_TYPE (decl))
3294 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3296 if (!allow_non_integral_constant_expression_p)
3297 error ("template parameter %qD of type %qT is not allowed in "
3298 "an integral constant expression because it is not of "
3299 "integral or enumeration type", decl, TREE_TYPE (decl));
3300 *non_integral_constant_expression_p = true;
3302 return r;
3304 else
3306 bool dependent_p;
3308 /* If the declaration was explicitly qualified indicate
3309 that. The semantics of `A::f(3)' are different than
3310 `f(3)' if `f' is virtual. */
3311 *idk = (scope
3312 ? CP_ID_KIND_QUALIFIED
3313 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3314 ? CP_ID_KIND_TEMPLATE_ID
3315 : CP_ID_KIND_UNQUALIFIED));
3318 /* [temp.dep.expr]
3320 An id-expression is type-dependent if it contains an
3321 identifier that was declared with a dependent type.
3323 The standard is not very specific about an id-expression that
3324 names a set of overloaded functions. What if some of them
3325 have dependent types and some of them do not? Presumably,
3326 such a name should be treated as a dependent name. */
3327 /* Assume the name is not dependent. */
3328 dependent_p = false;
3329 if (!processing_template_decl)
3330 /* No names are dependent outside a template. */
3332 else if (TREE_CODE (decl) == CONST_DECL)
3333 /* We don't want to treat enumerators as dependent. */
3335 /* A template-id where the name of the template was not resolved
3336 is definitely dependent. */
3337 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3338 && (identifier_p (TREE_OPERAND (decl, 0))))
3339 dependent_p = true;
3340 /* For anything except an overloaded function, just check its
3341 type. */
3342 else if (!is_overloaded_fn (decl))
3343 dependent_p
3344 = dependent_type_p (TREE_TYPE (decl));
3345 /* For a set of overloaded functions, check each of the
3346 functions. */
3347 else
3349 tree fns = decl;
3351 if (BASELINK_P (fns))
3352 fns = BASELINK_FUNCTIONS (fns);
3354 /* For a template-id, check to see if the template
3355 arguments are dependent. */
3356 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3358 tree args = TREE_OPERAND (fns, 1);
3359 dependent_p = any_dependent_template_arguments_p (args);
3360 /* The functions are those referred to by the
3361 template-id. */
3362 fns = TREE_OPERAND (fns, 0);
3365 /* If there are no dependent template arguments, go through
3366 the overloaded functions. */
3367 while (fns && !dependent_p)
3369 tree fn = OVL_CURRENT (fns);
3371 /* Member functions of dependent classes are
3372 dependent. */
3373 if (TREE_CODE (fn) == FUNCTION_DECL
3374 && type_dependent_expression_p (fn))
3375 dependent_p = true;
3376 else if (TREE_CODE (fn) == TEMPLATE_DECL
3377 && dependent_template_p (fn))
3378 dependent_p = true;
3380 fns = OVL_NEXT (fns);
3384 /* If the name was dependent on a template parameter, we will
3385 resolve the name at instantiation time. */
3386 if (dependent_p)
3388 /* Create a SCOPE_REF for qualified names, if the scope is
3389 dependent. */
3390 if (scope)
3392 if (TYPE_P (scope))
3394 if (address_p && done)
3395 decl = finish_qualified_id_expr (scope, decl,
3396 done, address_p,
3397 template_p,
3398 template_arg_p,
3399 tf_warning_or_error);
3400 else
3402 tree type = NULL_TREE;
3403 if (DECL_P (decl) && !dependent_scope_p (scope))
3404 type = TREE_TYPE (decl);
3405 decl = build_qualified_name (type,
3406 scope,
3407 id_expression,
3408 template_p);
3411 if (TREE_TYPE (decl))
3412 decl = convert_from_reference (decl);
3413 return decl;
3415 /* A TEMPLATE_ID already contains all the information we
3416 need. */
3417 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3418 return id_expression;
3419 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3420 /* If we found a variable, then name lookup during the
3421 instantiation will always resolve to the same VAR_DECL
3422 (or an instantiation thereof). */
3423 if (VAR_P (decl)
3424 || TREE_CODE (decl) == PARM_DECL)
3426 mark_used (decl);
3427 return convert_from_reference (decl);
3429 /* The same is true for FIELD_DECL, but we also need to
3430 make sure that the syntax is correct. */
3431 else if (TREE_CODE (decl) == FIELD_DECL)
3433 /* Since SCOPE is NULL here, this is an unqualified name.
3434 Access checking has been performed during name lookup
3435 already. Turn off checking to avoid duplicate errors. */
3436 push_deferring_access_checks (dk_no_check);
3437 decl = finish_non_static_data_member
3438 (decl, NULL_TREE,
3439 /*qualifying_scope=*/NULL_TREE);
3440 pop_deferring_access_checks ();
3441 return decl;
3443 return id_expression;
3446 if (TREE_CODE (decl) == NAMESPACE_DECL)
3448 error ("use of namespace %qD as expression", decl);
3449 return error_mark_node;
3451 else if (DECL_CLASS_TEMPLATE_P (decl))
3453 error ("use of class template %qT as expression", decl);
3454 return error_mark_node;
3456 else if (TREE_CODE (decl) == TREE_LIST)
3458 /* Ambiguous reference to base members. */
3459 error ("request for member %qD is ambiguous in "
3460 "multiple inheritance lattice", id_expression);
3461 print_candidates (decl);
3462 return error_mark_node;
3465 /* Mark variable-like entities as used. Functions are similarly
3466 marked either below or after overload resolution. */
3467 if ((VAR_P (decl)
3468 || TREE_CODE (decl) == PARM_DECL
3469 || TREE_CODE (decl) == CONST_DECL
3470 || TREE_CODE (decl) == RESULT_DECL)
3471 && !mark_used (decl))
3472 return error_mark_node;
3474 /* Only certain kinds of names are allowed in constant
3475 expression. Template parameters have already
3476 been handled above. */
3477 if (! error_operand_p (decl)
3478 && integral_constant_expression_p
3479 && ! decl_constant_var_p (decl)
3480 && TREE_CODE (decl) != CONST_DECL
3481 && ! builtin_valid_in_constant_expr_p (decl))
3483 if (!allow_non_integral_constant_expression_p)
3485 error ("%qD cannot appear in a constant-expression", decl);
3486 return error_mark_node;
3488 *non_integral_constant_expression_p = true;
3491 tree wrap;
3492 if (VAR_P (decl)
3493 && !cp_unevaluated_operand
3494 && DECL_THREAD_LOCAL_P (decl)
3495 && (wrap = get_tls_wrapper_fn (decl)))
3497 /* Replace an evaluated use of the thread_local variable with
3498 a call to its wrapper. */
3499 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3501 else if (scope)
3503 decl = (adjust_result_of_qualified_name_lookup
3504 (decl, scope, current_nonlambda_class_type()));
3506 if (TREE_CODE (decl) == FUNCTION_DECL)
3507 mark_used (decl);
3509 if (TYPE_P (scope))
3510 decl = finish_qualified_id_expr (scope,
3511 decl,
3512 done,
3513 address_p,
3514 template_p,
3515 template_arg_p,
3516 tf_warning_or_error);
3517 else
3518 decl = convert_from_reference (decl);
3520 else if (TREE_CODE (decl) == FIELD_DECL)
3522 /* Since SCOPE is NULL here, this is an unqualified name.
3523 Access checking has been performed during name lookup
3524 already. Turn off checking to avoid duplicate errors. */
3525 push_deferring_access_checks (dk_no_check);
3526 decl = finish_non_static_data_member (decl, NULL_TREE,
3527 /*qualifying_scope=*/NULL_TREE);
3528 pop_deferring_access_checks ();
3530 else if (is_overloaded_fn (decl))
3532 tree first_fn;
3534 first_fn = get_first_fn (decl);
3535 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3536 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3538 if (!really_overloaded_fn (decl)
3539 && !mark_used (first_fn))
3540 return error_mark_node;
3542 if (!template_arg_p
3543 && TREE_CODE (first_fn) == FUNCTION_DECL
3544 && DECL_FUNCTION_MEMBER_P (first_fn)
3545 && !shared_member_p (decl))
3547 /* A set of member functions. */
3548 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3549 return finish_class_member_access_expr (decl, id_expression,
3550 /*template_p=*/false,
3551 tf_warning_or_error);
3554 decl = baselink_for_fns (decl);
3556 else
3558 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3559 && DECL_CLASS_SCOPE_P (decl))
3561 tree context = context_for_name_lookup (decl);
3562 if (context != current_class_type)
3564 tree path = currently_open_derived_class (context);
3565 perform_or_defer_access_check (TYPE_BINFO (path),
3566 decl, decl,
3567 tf_warning_or_error);
3571 decl = convert_from_reference (decl);
3575 /* Handle references (c++/56130). */
3576 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3577 if (TREE_DEPRECATED (t))
3578 warn_deprecated_use (t, NULL_TREE);
3580 return decl;
3583 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3584 use as a type-specifier. */
3586 tree
3587 finish_typeof (tree expr)
3589 tree type;
3591 if (type_dependent_expression_p (expr))
3593 type = cxx_make_type (TYPEOF_TYPE);
3594 TYPEOF_TYPE_EXPR (type) = expr;
3595 SET_TYPE_STRUCTURAL_EQUALITY (type);
3597 return type;
3600 expr = mark_type_use (expr);
3602 type = unlowered_expr_type (expr);
3604 if (!type || type == unknown_type_node)
3606 error ("type of %qE is unknown", expr);
3607 return error_mark_node;
3610 return type;
3613 /* Implement the __underlying_type keyword: Return the underlying
3614 type of TYPE, suitable for use as a type-specifier. */
3616 tree
3617 finish_underlying_type (tree type)
3619 tree underlying_type;
3621 if (processing_template_decl)
3623 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3624 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3625 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3627 return underlying_type;
3630 complete_type (type);
3632 if (TREE_CODE (type) != ENUMERAL_TYPE)
3634 error ("%qT is not an enumeration type", type);
3635 return error_mark_node;
3638 underlying_type = ENUM_UNDERLYING_TYPE (type);
3640 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3641 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3642 See finish_enum_value_list for details. */
3643 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3644 underlying_type
3645 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3646 TYPE_UNSIGNED (underlying_type));
3648 return underlying_type;
3651 /* Implement the __direct_bases keyword: Return the direct base classes
3652 of type */
3654 tree
3655 calculate_direct_bases (tree type)
3657 vec<tree, va_gc> *vector = make_tree_vector();
3658 tree bases_vec = NULL_TREE;
3659 vec<tree, va_gc> *base_binfos;
3660 tree binfo;
3661 unsigned i;
3663 complete_type (type);
3665 if (!NON_UNION_CLASS_TYPE_P (type))
3666 return make_tree_vec (0);
3668 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3670 /* Virtual bases are initialized first */
3671 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3673 if (BINFO_VIRTUAL_P (binfo))
3675 vec_safe_push (vector, binfo);
3679 /* Now non-virtuals */
3680 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3682 if (!BINFO_VIRTUAL_P (binfo))
3684 vec_safe_push (vector, binfo);
3689 bases_vec = make_tree_vec (vector->length ());
3691 for (i = 0; i < vector->length (); ++i)
3693 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3695 return bases_vec;
3698 /* Implement the __bases keyword: Return the base classes
3699 of type */
3701 /* Find morally non-virtual base classes by walking binfo hierarchy */
3702 /* Virtual base classes are handled separately in finish_bases */
3704 static tree
3705 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3707 /* Don't walk bases of virtual bases */
3708 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3711 static tree
3712 dfs_calculate_bases_post (tree binfo, void *data_)
3714 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3715 if (!BINFO_VIRTUAL_P (binfo))
3717 vec_safe_push (*data, BINFO_TYPE (binfo));
3719 return NULL_TREE;
3722 /* Calculates the morally non-virtual base classes of a class */
3723 static vec<tree, va_gc> *
3724 calculate_bases_helper (tree type)
3726 vec<tree, va_gc> *vector = make_tree_vector();
3728 /* Now add non-virtual base classes in order of construction */
3729 dfs_walk_all (TYPE_BINFO (type),
3730 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3731 return vector;
3734 tree
3735 calculate_bases (tree type)
3737 vec<tree, va_gc> *vector = make_tree_vector();
3738 tree bases_vec = NULL_TREE;
3739 unsigned i;
3740 vec<tree, va_gc> *vbases;
3741 vec<tree, va_gc> *nonvbases;
3742 tree binfo;
3744 complete_type (type);
3746 if (!NON_UNION_CLASS_TYPE_P (type))
3747 return make_tree_vec (0);
3749 /* First go through virtual base classes */
3750 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3751 vec_safe_iterate (vbases, i, &binfo); i++)
3753 vec<tree, va_gc> *vbase_bases;
3754 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3755 vec_safe_splice (vector, vbase_bases);
3756 release_tree_vector (vbase_bases);
3759 /* Now for the non-virtual bases */
3760 nonvbases = calculate_bases_helper (type);
3761 vec_safe_splice (vector, nonvbases);
3762 release_tree_vector (nonvbases);
3764 /* Last element is entire class, so don't copy */
3765 bases_vec = make_tree_vec (vector->length () - 1);
3767 for (i = 0; i < vector->length () - 1; ++i)
3769 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3771 release_tree_vector (vector);
3772 return bases_vec;
3775 tree
3776 finish_bases (tree type, bool direct)
3778 tree bases = NULL_TREE;
3780 if (!processing_template_decl)
3782 /* Parameter packs can only be used in templates */
3783 error ("Parameter pack __bases only valid in template declaration");
3784 return error_mark_node;
3787 bases = cxx_make_type (BASES);
3788 BASES_TYPE (bases) = type;
3789 BASES_DIRECT (bases) = direct;
3790 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3792 return bases;
3795 /* Perform C++-specific checks for __builtin_offsetof before calling
3796 fold_offsetof. */
3798 tree
3799 finish_offsetof (tree expr)
3801 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3803 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3804 TREE_OPERAND (expr, 2));
3805 return error_mark_node;
3807 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3808 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3809 || TREE_TYPE (expr) == unknown_type_node)
3811 if (INDIRECT_REF_P (expr))
3812 error ("second operand of %<offsetof%> is neither a single "
3813 "identifier nor a sequence of member accesses and "
3814 "array references");
3815 else
3817 if (TREE_CODE (expr) == COMPONENT_REF
3818 || TREE_CODE (expr) == COMPOUND_EXPR)
3819 expr = TREE_OPERAND (expr, 1);
3820 error ("cannot apply %<offsetof%> to member function %qD", expr);
3822 return error_mark_node;
3824 if (REFERENCE_REF_P (expr))
3825 expr = TREE_OPERAND (expr, 0);
3826 if (TREE_CODE (expr) == COMPONENT_REF)
3828 tree object = TREE_OPERAND (expr, 0);
3829 if (!complete_type_or_else (TREE_TYPE (object), object))
3830 return error_mark_node;
3832 return fold_offsetof (expr);
3835 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3836 function is broken out from the above for the benefit of the tree-ssa
3837 project. */
3839 void
3840 simplify_aggr_init_expr (tree *tp)
3842 tree aggr_init_expr = *tp;
3844 /* Form an appropriate CALL_EXPR. */
3845 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3846 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3847 tree type = TREE_TYPE (slot);
3849 tree call_expr;
3850 enum style_t { ctor, arg, pcc } style;
3852 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3853 style = ctor;
3854 #ifdef PCC_STATIC_STRUCT_RETURN
3855 else if (1)
3856 style = pcc;
3857 #endif
3858 else
3860 gcc_assert (TREE_ADDRESSABLE (type));
3861 style = arg;
3864 call_expr = build_call_array_loc (input_location,
3865 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3867 aggr_init_expr_nargs (aggr_init_expr),
3868 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3869 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3870 tree ret = call_expr;
3872 if (style == ctor)
3874 /* Replace the first argument to the ctor with the address of the
3875 slot. */
3876 cxx_mark_addressable (slot);
3877 CALL_EXPR_ARG (call_expr, 0) =
3878 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3880 else if (style == arg)
3882 /* Just mark it addressable here, and leave the rest to
3883 expand_call{,_inline}. */
3884 cxx_mark_addressable (slot);
3885 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3886 ret = build2 (INIT_EXPR, TREE_TYPE (ret), slot, ret);
3888 else if (style == pcc)
3890 /* If we're using the non-reentrant PCC calling convention, then we
3891 need to copy the returned value out of the static buffer into the
3892 SLOT. */
3893 push_deferring_access_checks (dk_no_check);
3894 ret = build_aggr_init (slot, ret,
3895 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3896 tf_warning_or_error);
3897 pop_deferring_access_checks ();
3898 ret = build2 (COMPOUND_EXPR, TREE_TYPE (slot), ret, slot);
3901 /* DR 1030 says that we need to evaluate the elements of an
3902 initializer-list in forward order even when it's used as arguments to
3903 a constructor. So if the target wants to evaluate them in reverse
3904 order and there's more than one argument other than 'this', force
3905 pre-evaluation. */
3906 if (PUSH_ARGS_REVERSED && CALL_EXPR_LIST_INIT_P (aggr_init_expr)
3907 && aggr_init_expr_nargs (aggr_init_expr) > 2)
3909 tree preinit;
3910 stabilize_call (call_expr, &preinit);
3911 if (preinit)
3912 ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), preinit, ret);
3915 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3917 tree init = build_zero_init (type, NULL_TREE,
3918 /*static_storage_p=*/false);
3919 init = build2 (INIT_EXPR, void_type_node, slot, init);
3920 ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), init, ret);
3923 *tp = ret;
3926 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3928 void
3929 emit_associated_thunks (tree fn)
3931 /* When we use vcall offsets, we emit thunks with the virtual
3932 functions to which they thunk. The whole point of vcall offsets
3933 is so that you can know statically the entire set of thunks that
3934 will ever be needed for a given virtual function, thereby
3935 enabling you to output all the thunks with the function itself. */
3936 if (DECL_VIRTUAL_P (fn)
3937 /* Do not emit thunks for extern template instantiations. */
3938 && ! DECL_REALLY_EXTERN (fn))
3940 tree thunk;
3942 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3944 if (!THUNK_ALIAS (thunk))
3946 use_thunk (thunk, /*emit_p=*/1);
3947 if (DECL_RESULT_THUNK_P (thunk))
3949 tree probe;
3951 for (probe = DECL_THUNKS (thunk);
3952 probe; probe = DECL_CHAIN (probe))
3953 use_thunk (probe, /*emit_p=*/1);
3956 else
3957 gcc_assert (!DECL_THUNKS (thunk));
3962 /* Returns true iff FUN is an instantiation of a constexpr function
3963 template. */
3965 static inline bool
3966 is_instantiation_of_constexpr (tree fun)
3968 return (DECL_TEMPLOID_INSTANTIATION (fun)
3969 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)));
3972 /* Generate RTL for FN. */
3974 bool
3975 expand_or_defer_fn_1 (tree fn)
3977 /* When the parser calls us after finishing the body of a template
3978 function, we don't really want to expand the body. */
3979 if (processing_template_decl)
3981 /* Normally, collection only occurs in rest_of_compilation. So,
3982 if we don't collect here, we never collect junk generated
3983 during the processing of templates until we hit a
3984 non-template function. It's not safe to do this inside a
3985 nested class, though, as the parser may have local state that
3986 is not a GC root. */
3987 if (!function_depth)
3988 ggc_collect ();
3989 return false;
3992 gcc_assert (DECL_SAVED_TREE (fn));
3994 /* We make a decision about linkage for these functions at the end
3995 of the compilation. Until that point, we do not want the back
3996 end to output them -- but we do want it to see the bodies of
3997 these functions so that it can inline them as appropriate. */
3998 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4000 if (DECL_INTERFACE_KNOWN (fn))
4001 /* We've already made a decision as to how this function will
4002 be handled. */;
4003 else if (!at_eof)
4004 tentative_decl_linkage (fn);
4005 else
4006 import_export_decl (fn);
4008 /* If the user wants us to keep all inline functions, then mark
4009 this function as needed so that finish_file will make sure to
4010 output it later. Similarly, all dllexport'd functions must
4011 be emitted; there may be callers in other DLLs. */
4012 if ((flag_keep_inline_functions
4013 && DECL_DECLARED_INLINE_P (fn)
4014 && !DECL_REALLY_EXTERN (fn))
4015 || (flag_keep_inline_dllexport
4016 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4018 mark_needed (fn);
4019 DECL_EXTERNAL (fn) = 0;
4023 /* If this is a constructor or destructor body, we have to clone
4024 it. */
4025 if (maybe_clone_body (fn))
4027 /* We don't want to process FN again, so pretend we've written
4028 it out, even though we haven't. */
4029 TREE_ASM_WRITTEN (fn) = 1;
4030 /* If this is an instantiation of a constexpr function, keep
4031 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4032 if (!is_instantiation_of_constexpr (fn))
4033 DECL_SAVED_TREE (fn) = NULL_TREE;
4034 return false;
4037 /* There's no reason to do any of the work here if we're only doing
4038 semantic analysis; this code just generates RTL. */
4039 if (flag_syntax_only)
4040 return false;
4042 return true;
4045 void
4046 expand_or_defer_fn (tree fn)
4048 if (expand_or_defer_fn_1 (fn))
4050 function_depth++;
4052 /* Expand or defer, at the whim of the compilation unit manager. */
4053 cgraph_finalize_function (fn, function_depth > 1);
4054 emit_associated_thunks (fn);
4056 function_depth--;
4060 struct nrv_data
4062 tree var;
4063 tree result;
4064 hash_table <pointer_hash <tree_node> > visited;
4067 /* Helper function for walk_tree, used by finalize_nrv below. */
4069 static tree
4070 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4072 struct nrv_data *dp = (struct nrv_data *)data;
4073 tree_node **slot;
4075 /* No need to walk into types. There wouldn't be any need to walk into
4076 non-statements, except that we have to consider STMT_EXPRs. */
4077 if (TYPE_P (*tp))
4078 *walk_subtrees = 0;
4079 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4080 but differs from using NULL_TREE in that it indicates that we care
4081 about the value of the RESULT_DECL. */
4082 else if (TREE_CODE (*tp) == RETURN_EXPR)
4083 TREE_OPERAND (*tp, 0) = dp->result;
4084 /* Change all cleanups for the NRV to only run when an exception is
4085 thrown. */
4086 else if (TREE_CODE (*tp) == CLEANUP_STMT
4087 && CLEANUP_DECL (*tp) == dp->var)
4088 CLEANUP_EH_ONLY (*tp) = 1;
4089 /* Replace the DECL_EXPR for the NRV with an initialization of the
4090 RESULT_DECL, if needed. */
4091 else if (TREE_CODE (*tp) == DECL_EXPR
4092 && DECL_EXPR_DECL (*tp) == dp->var)
4094 tree init;
4095 if (DECL_INITIAL (dp->var)
4096 && DECL_INITIAL (dp->var) != error_mark_node)
4097 init = build2 (INIT_EXPR, void_type_node, dp->result,
4098 DECL_INITIAL (dp->var));
4099 else
4100 init = build_empty_stmt (EXPR_LOCATION (*tp));
4101 DECL_INITIAL (dp->var) = NULL_TREE;
4102 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4103 *tp = init;
4105 /* And replace all uses of the NRV with the RESULT_DECL. */
4106 else if (*tp == dp->var)
4107 *tp = dp->result;
4109 /* Avoid walking into the same tree more than once. Unfortunately, we
4110 can't just use walk_tree_without duplicates because it would only call
4111 us for the first occurrence of dp->var in the function body. */
4112 slot = dp->visited.find_slot (*tp, INSERT);
4113 if (*slot)
4114 *walk_subtrees = 0;
4115 else
4116 *slot = *tp;
4118 /* Keep iterating. */
4119 return NULL_TREE;
4122 /* Called from finish_function to implement the named return value
4123 optimization by overriding all the RETURN_EXPRs and pertinent
4124 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4125 RESULT_DECL for the function. */
4127 void
4128 finalize_nrv (tree *tp, tree var, tree result)
4130 struct nrv_data data;
4132 /* Copy name from VAR to RESULT. */
4133 DECL_NAME (result) = DECL_NAME (var);
4134 /* Don't forget that we take its address. */
4135 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4136 /* Finally set DECL_VALUE_EXPR to avoid assigning
4137 a stack slot at -O0 for the original var and debug info
4138 uses RESULT location for VAR. */
4139 SET_DECL_VALUE_EXPR (var, result);
4140 DECL_HAS_VALUE_EXPR_P (var) = 1;
4142 data.var = var;
4143 data.result = result;
4144 data.visited.create (37);
4145 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4146 data.visited.dispose ();
4149 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4151 bool
4152 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4153 bool need_copy_ctor, bool need_copy_assignment,
4154 bool need_dtor)
4156 int save_errorcount = errorcount;
4157 tree info, t;
4159 /* Always allocate 3 elements for simplicity. These are the
4160 function decls for the ctor, dtor, and assignment op.
4161 This layout is known to the three lang hooks,
4162 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4163 and cxx_omp_clause_assign_op. */
4164 info = make_tree_vec (3);
4165 CP_OMP_CLAUSE_INFO (c) = info;
4167 if (need_default_ctor || need_copy_ctor)
4169 if (need_default_ctor)
4170 t = get_default_ctor (type);
4171 else
4172 t = get_copy_ctor (type, tf_warning_or_error);
4174 if (t && !trivial_fn_p (t))
4175 TREE_VEC_ELT (info, 0) = t;
4178 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4179 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4181 if (need_copy_assignment)
4183 t = get_copy_assign (type);
4185 if (t && !trivial_fn_p (t))
4186 TREE_VEC_ELT (info, 2) = t;
4189 return errorcount != save_errorcount;
4192 /* Helper function for handle_omp_array_sections. Called recursively
4193 to handle multiple array-section-subscripts. C is the clause,
4194 T current expression (initially OMP_CLAUSE_DECL), which is either
4195 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4196 expression if specified, TREE_VALUE length expression if specified,
4197 TREE_CHAIN is what it has been specified after, or some decl.
4198 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4199 set to true if any of the array-section-subscript could have length
4200 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4201 first array-section-subscript which is known not to have length
4202 of one. Given say:
4203 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4204 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4205 all are or may have length of 1, array-section-subscript [:2] is the
4206 first one knonwn not to have length 1. For array-section-subscript
4207 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4208 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4209 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4210 case though, as some lengths could be zero. */
4212 static tree
4213 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4214 bool &maybe_zero_len, unsigned int &first_non_one)
4216 tree ret, low_bound, length, type;
4217 if (TREE_CODE (t) != TREE_LIST)
4219 if (error_operand_p (t))
4220 return error_mark_node;
4221 if (type_dependent_expression_p (t))
4222 return NULL_TREE;
4223 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4225 if (processing_template_decl)
4226 return NULL_TREE;
4227 if (DECL_P (t))
4228 error_at (OMP_CLAUSE_LOCATION (c),
4229 "%qD is not a variable in %qs clause", t,
4230 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4231 else
4232 error_at (OMP_CLAUSE_LOCATION (c),
4233 "%qE is not a variable in %qs clause", t,
4234 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4235 return error_mark_node;
4237 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4238 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4240 error_at (OMP_CLAUSE_LOCATION (c),
4241 "%qD is threadprivate variable in %qs clause", t,
4242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4243 return error_mark_node;
4245 t = convert_from_reference (t);
4246 return t;
4249 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4250 maybe_zero_len, first_non_one);
4251 if (ret == error_mark_node || ret == NULL_TREE)
4252 return ret;
4254 type = TREE_TYPE (ret);
4255 low_bound = TREE_PURPOSE (t);
4256 length = TREE_VALUE (t);
4257 if ((low_bound && type_dependent_expression_p (low_bound))
4258 || (length && type_dependent_expression_p (length)))
4259 return NULL_TREE;
4261 if (low_bound == error_mark_node || length == error_mark_node)
4262 return error_mark_node;
4264 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4266 error_at (OMP_CLAUSE_LOCATION (c),
4267 "low bound %qE of array section does not have integral type",
4268 low_bound);
4269 return error_mark_node;
4271 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4273 error_at (OMP_CLAUSE_LOCATION (c),
4274 "length %qE of array section does not have integral type",
4275 length);
4276 return error_mark_node;
4278 if (low_bound
4279 && TREE_CODE (low_bound) == INTEGER_CST
4280 && TYPE_PRECISION (TREE_TYPE (low_bound))
4281 > TYPE_PRECISION (sizetype))
4282 low_bound = fold_convert (sizetype, low_bound);
4283 if (length
4284 && TREE_CODE (length) == INTEGER_CST
4285 && TYPE_PRECISION (TREE_TYPE (length))
4286 > TYPE_PRECISION (sizetype))
4287 length = fold_convert (sizetype, length);
4288 if (low_bound == NULL_TREE)
4289 low_bound = integer_zero_node;
4291 if (length != NULL_TREE)
4293 if (!integer_nonzerop (length))
4294 maybe_zero_len = true;
4295 if (first_non_one == types.length ()
4296 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4297 first_non_one++;
4299 if (TREE_CODE (type) == ARRAY_TYPE)
4301 if (length == NULL_TREE
4302 && (TYPE_DOMAIN (type) == NULL_TREE
4303 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4305 error_at (OMP_CLAUSE_LOCATION (c),
4306 "for unknown bound array type length expression must "
4307 "be specified");
4308 return error_mark_node;
4310 if (TREE_CODE (low_bound) == INTEGER_CST
4311 && tree_int_cst_sgn (low_bound) == -1)
4313 error_at (OMP_CLAUSE_LOCATION (c),
4314 "negative low bound in array section in %qs clause",
4315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4316 return error_mark_node;
4318 if (length != NULL_TREE
4319 && TREE_CODE (length) == INTEGER_CST
4320 && tree_int_cst_sgn (length) == -1)
4322 error_at (OMP_CLAUSE_LOCATION (c),
4323 "negative length in array section in %qs clause",
4324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4325 return error_mark_node;
4327 if (TYPE_DOMAIN (type)
4328 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4329 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4330 == INTEGER_CST)
4332 tree size = size_binop (PLUS_EXPR,
4333 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4334 size_one_node);
4335 if (TREE_CODE (low_bound) == INTEGER_CST)
4337 if (tree_int_cst_lt (size, low_bound))
4339 error_at (OMP_CLAUSE_LOCATION (c),
4340 "low bound %qE above array section size "
4341 "in %qs clause", low_bound,
4342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4343 return error_mark_node;
4345 if (tree_int_cst_equal (size, low_bound))
4346 maybe_zero_len = true;
4347 else if (length == NULL_TREE
4348 && first_non_one == types.length ()
4349 && tree_int_cst_equal
4350 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4351 low_bound))
4352 first_non_one++;
4354 else if (length == NULL_TREE)
4356 maybe_zero_len = true;
4357 if (first_non_one == types.length ())
4358 first_non_one++;
4360 if (length && TREE_CODE (length) == INTEGER_CST)
4362 if (tree_int_cst_lt (size, length))
4364 error_at (OMP_CLAUSE_LOCATION (c),
4365 "length %qE above array section size "
4366 "in %qs clause", length,
4367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4368 return error_mark_node;
4370 if (TREE_CODE (low_bound) == INTEGER_CST)
4372 tree lbpluslen
4373 = size_binop (PLUS_EXPR,
4374 fold_convert (sizetype, low_bound),
4375 fold_convert (sizetype, length));
4376 if (TREE_CODE (lbpluslen) == INTEGER_CST
4377 && tree_int_cst_lt (size, lbpluslen))
4379 error_at (OMP_CLAUSE_LOCATION (c),
4380 "high bound %qE above array section size "
4381 "in %qs clause", lbpluslen,
4382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4383 return error_mark_node;
4388 else if (length == NULL_TREE)
4390 maybe_zero_len = true;
4391 if (first_non_one == types.length ())
4392 first_non_one++;
4395 /* For [lb:] we will need to evaluate lb more than once. */
4396 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4398 tree lb = cp_save_expr (low_bound);
4399 if (lb != low_bound)
4401 TREE_PURPOSE (t) = lb;
4402 low_bound = lb;
4406 else if (TREE_CODE (type) == POINTER_TYPE)
4408 if (length == NULL_TREE)
4410 error_at (OMP_CLAUSE_LOCATION (c),
4411 "for pointer type length expression must be specified");
4412 return error_mark_node;
4414 /* If there is a pointer type anywhere but in the very first
4415 array-section-subscript, the array section can't be contiguous. */
4416 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4417 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4419 error_at (OMP_CLAUSE_LOCATION (c),
4420 "array section is not contiguous in %qs clause",
4421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4422 return error_mark_node;
4425 else
4427 error_at (OMP_CLAUSE_LOCATION (c),
4428 "%qE does not have pointer or array type", ret);
4429 return error_mark_node;
4431 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4432 types.safe_push (TREE_TYPE (ret));
4433 /* We will need to evaluate lb more than once. */
4434 tree lb = cp_save_expr (low_bound);
4435 if (lb != low_bound)
4437 TREE_PURPOSE (t) = lb;
4438 low_bound = lb;
4440 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4441 return ret;
4444 /* Handle array sections for clause C. */
4446 static bool
4447 handle_omp_array_sections (tree c)
4449 bool maybe_zero_len = false;
4450 unsigned int first_non_one = 0;
4451 auto_vec<tree> types;
4452 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4453 maybe_zero_len, first_non_one);
4454 if (first == error_mark_node)
4455 return true;
4456 if (first == NULL_TREE)
4457 return false;
4458 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4460 tree t = OMP_CLAUSE_DECL (c);
4461 tree tem = NULL_TREE;
4462 if (processing_template_decl)
4463 return false;
4464 /* Need to evaluate side effects in the length expressions
4465 if any. */
4466 while (TREE_CODE (t) == TREE_LIST)
4468 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4470 if (tem == NULL_TREE)
4471 tem = TREE_VALUE (t);
4472 else
4473 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4474 TREE_VALUE (t), tem);
4476 t = TREE_CHAIN (t);
4478 if (tem)
4479 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4480 OMP_CLAUSE_DECL (c) = first;
4482 else
4484 unsigned int num = types.length (), i;
4485 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4486 tree condition = NULL_TREE;
4488 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4489 maybe_zero_len = true;
4490 if (processing_template_decl && maybe_zero_len)
4491 return false;
4493 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4494 t = TREE_CHAIN (t))
4496 tree low_bound = TREE_PURPOSE (t);
4497 tree length = TREE_VALUE (t);
4499 i--;
4500 if (low_bound
4501 && TREE_CODE (low_bound) == INTEGER_CST
4502 && TYPE_PRECISION (TREE_TYPE (low_bound))
4503 > TYPE_PRECISION (sizetype))
4504 low_bound = fold_convert (sizetype, low_bound);
4505 if (length
4506 && TREE_CODE (length) == INTEGER_CST
4507 && TYPE_PRECISION (TREE_TYPE (length))
4508 > TYPE_PRECISION (sizetype))
4509 length = fold_convert (sizetype, length);
4510 if (low_bound == NULL_TREE)
4511 low_bound = integer_zero_node;
4512 if (!maybe_zero_len && i > first_non_one)
4514 if (integer_nonzerop (low_bound))
4515 goto do_warn_noncontiguous;
4516 if (length != NULL_TREE
4517 && TREE_CODE (length) == INTEGER_CST
4518 && TYPE_DOMAIN (types[i])
4519 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4520 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4521 == INTEGER_CST)
4523 tree size;
4524 size = size_binop (PLUS_EXPR,
4525 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4526 size_one_node);
4527 if (!tree_int_cst_equal (length, size))
4529 do_warn_noncontiguous:
4530 error_at (OMP_CLAUSE_LOCATION (c),
4531 "array section is not contiguous in %qs "
4532 "clause",
4533 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4534 return true;
4537 if (!processing_template_decl
4538 && length != NULL_TREE
4539 && TREE_SIDE_EFFECTS (length))
4541 if (side_effects == NULL_TREE)
4542 side_effects = length;
4543 else
4544 side_effects = build2 (COMPOUND_EXPR,
4545 TREE_TYPE (side_effects),
4546 length, side_effects);
4549 else if (processing_template_decl)
4550 continue;
4551 else
4553 tree l;
4555 if (i > first_non_one && length && integer_nonzerop (length))
4556 continue;
4557 if (length)
4558 l = fold_convert (sizetype, length);
4559 else
4561 l = size_binop (PLUS_EXPR,
4562 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4563 size_one_node);
4564 l = size_binop (MINUS_EXPR, l,
4565 fold_convert (sizetype, low_bound));
4567 if (i > first_non_one)
4569 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4570 size_zero_node);
4571 if (condition == NULL_TREE)
4572 condition = l;
4573 else
4574 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4575 l, condition);
4577 else if (size == NULL_TREE)
4579 size = size_in_bytes (TREE_TYPE (types[i]));
4580 size = size_binop (MULT_EXPR, size, l);
4581 if (condition)
4582 size = fold_build3 (COND_EXPR, sizetype, condition,
4583 size, size_zero_node);
4585 else
4586 size = size_binop (MULT_EXPR, size, l);
4589 if (!processing_template_decl)
4591 if (side_effects)
4592 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4593 OMP_CLAUSE_DECL (c) = first;
4594 OMP_CLAUSE_SIZE (c) = size;
4595 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4596 return false;
4597 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4598 OMP_CLAUSE_MAP);
4599 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4600 if (!cxx_mark_addressable (t))
4601 return false;
4602 OMP_CLAUSE_DECL (c2) = t;
4603 t = build_fold_addr_expr (first);
4604 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4605 ptrdiff_type_node, t);
4606 tree ptr = OMP_CLAUSE_DECL (c2);
4607 ptr = convert_from_reference (ptr);
4608 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4609 ptr = build_fold_addr_expr (ptr);
4610 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4611 ptrdiff_type_node, t,
4612 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4613 ptrdiff_type_node, ptr));
4614 OMP_CLAUSE_SIZE (c2) = t;
4615 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4616 OMP_CLAUSE_CHAIN (c) = c2;
4617 ptr = OMP_CLAUSE_DECL (c2);
4618 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4619 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4621 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4622 OMP_CLAUSE_MAP);
4623 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4624 OMP_CLAUSE_DECL (c3) = ptr;
4625 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4626 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4627 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4628 OMP_CLAUSE_CHAIN (c2) = c3;
4632 return false;
4635 /* Return identifier to look up for omp declare reduction. */
4637 tree
4638 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4640 const char *p = NULL;
4641 const char *m = NULL;
4642 switch (reduction_code)
4644 case PLUS_EXPR:
4645 case MULT_EXPR:
4646 case MINUS_EXPR:
4647 case BIT_AND_EXPR:
4648 case BIT_XOR_EXPR:
4649 case BIT_IOR_EXPR:
4650 case TRUTH_ANDIF_EXPR:
4651 case TRUTH_ORIF_EXPR:
4652 reduction_id = ansi_opname (reduction_code);
4653 break;
4654 case MIN_EXPR:
4655 p = "min";
4656 break;
4657 case MAX_EXPR:
4658 p = "max";
4659 break;
4660 default:
4661 break;
4664 if (p == NULL)
4666 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4667 return error_mark_node;
4668 p = IDENTIFIER_POINTER (reduction_id);
4671 if (type != NULL_TREE)
4672 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4674 const char prefix[] = "omp declare reduction ";
4675 size_t lenp = sizeof (prefix);
4676 if (strncmp (p, prefix, lenp - 1) == 0)
4677 lenp = 1;
4678 size_t len = strlen (p);
4679 size_t lenm = m ? strlen (m) + 1 : 0;
4680 char *name = XALLOCAVEC (char, lenp + len + lenm);
4681 if (lenp > 1)
4682 memcpy (name, prefix, lenp - 1);
4683 memcpy (name + lenp - 1, p, len + 1);
4684 if (m)
4686 name[lenp + len - 1] = '~';
4687 memcpy (name + lenp + len, m, lenm);
4689 return get_identifier (name);
4692 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4693 FUNCTION_DECL or NULL_TREE if not found. */
4695 static tree
4696 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4697 vec<tree> *ambiguousp)
4699 tree orig_id = id;
4700 tree baselink = NULL_TREE;
4701 if (identifier_p (id))
4703 cp_id_kind idk;
4704 bool nonint_cst_expression_p;
4705 const char *error_msg;
4706 id = omp_reduction_id (ERROR_MARK, id, type);
4707 tree decl = lookup_name (id);
4708 if (decl == NULL_TREE)
4709 decl = error_mark_node;
4710 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4711 &nonint_cst_expression_p, false, true, false,
4712 false, &error_msg, loc);
4713 if (idk == CP_ID_KIND_UNQUALIFIED
4714 && identifier_p (id))
4716 vec<tree, va_gc> *args = NULL;
4717 vec_safe_push (args, build_reference_type (type));
4718 id = perform_koenig_lookup (id, args, tf_none);
4721 else if (TREE_CODE (id) == SCOPE_REF)
4722 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4723 omp_reduction_id (ERROR_MARK,
4724 TREE_OPERAND (id, 1),
4725 type),
4726 false, false);
4727 tree fns = id;
4728 if (id && is_overloaded_fn (id))
4729 id = get_fns (id);
4730 for (; id; id = OVL_NEXT (id))
4732 tree fndecl = OVL_CURRENT (id);
4733 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4735 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4736 if (same_type_p (TREE_TYPE (argtype), type))
4737 break;
4740 if (id && BASELINK_P (fns))
4742 if (baselinkp)
4743 *baselinkp = fns;
4744 else
4745 baselink = fns;
4747 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4749 vec<tree> ambiguous = vNULL;
4750 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4751 unsigned int ix;
4752 if (ambiguousp == NULL)
4753 ambiguousp = &ambiguous;
4754 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4756 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4757 baselinkp ? baselinkp : &baselink,
4758 ambiguousp);
4759 if (id == NULL_TREE)
4760 continue;
4761 if (!ambiguousp->is_empty ())
4762 ambiguousp->safe_push (id);
4763 else if (ret != NULL_TREE)
4765 ambiguousp->safe_push (ret);
4766 ambiguousp->safe_push (id);
4767 ret = NULL_TREE;
4769 else
4770 ret = id;
4772 if (ambiguousp != &ambiguous)
4773 return ret;
4774 if (!ambiguous.is_empty ())
4776 const char *str = _("candidates are:");
4777 unsigned int idx;
4778 tree udr;
4779 error_at (loc, "user defined reduction lookup is ambiguous");
4780 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4782 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4783 if (idx == 0)
4784 str = get_spaces (str);
4786 ambiguous.release ();
4787 ret = error_mark_node;
4788 baselink = NULL_TREE;
4790 id = ret;
4792 if (id && baselink)
4793 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4794 id, id, tf_warning_or_error);
4795 return id;
4798 /* Helper function for cp_parser_omp_declare_reduction_exprs
4799 and tsubst_omp_udr.
4800 Remove CLEANUP_STMT for data (omp_priv variable).
4801 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4802 DECL_EXPR. */
4804 tree
4805 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4807 if (TYPE_P (*tp))
4808 *walk_subtrees = 0;
4809 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4810 *tp = CLEANUP_BODY (*tp);
4811 else if (TREE_CODE (*tp) == DECL_EXPR)
4813 tree decl = DECL_EXPR_DECL (*tp);
4814 if (!processing_template_decl
4815 && decl == (tree) data
4816 && DECL_INITIAL (decl)
4817 && DECL_INITIAL (decl) != error_mark_node)
4819 tree list = NULL_TREE;
4820 append_to_statement_list_force (*tp, &list);
4821 tree init_expr = build2 (INIT_EXPR, void_type_node,
4822 decl, DECL_INITIAL (decl));
4823 DECL_INITIAL (decl) = NULL_TREE;
4824 append_to_statement_list_force (init_expr, &list);
4825 *tp = list;
4828 return NULL_TREE;
4831 /* Data passed from cp_check_omp_declare_reduction to
4832 cp_check_omp_declare_reduction_r. */
4834 struct cp_check_omp_declare_reduction_data
4836 location_t loc;
4837 tree stmts[7];
4838 bool combiner_p;
4841 /* Helper function for cp_check_omp_declare_reduction, called via
4842 cp_walk_tree. */
4844 static tree
4845 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4847 struct cp_check_omp_declare_reduction_data *udr_data
4848 = (struct cp_check_omp_declare_reduction_data *) data;
4849 if (SSA_VAR_P (*tp)
4850 && !DECL_ARTIFICIAL (*tp)
4851 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4852 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4854 location_t loc = udr_data->loc;
4855 if (udr_data->combiner_p)
4856 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4857 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4858 *tp);
4859 else
4860 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4861 "to variable %qD which is not %<omp_priv%> nor "
4862 "%<omp_orig%>",
4863 *tp);
4864 return *tp;
4866 return NULL_TREE;
4869 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4871 void
4872 cp_check_omp_declare_reduction (tree udr)
4874 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4875 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4876 type = TREE_TYPE (type);
4877 int i;
4878 location_t loc = DECL_SOURCE_LOCATION (udr);
4880 if (type == error_mark_node)
4881 return;
4882 if (ARITHMETIC_TYPE_P (type))
4884 static enum tree_code predef_codes[]
4885 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4886 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4887 for (i = 0; i < 8; i++)
4889 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4890 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4891 const char *n2 = IDENTIFIER_POINTER (id);
4892 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4893 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4894 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4895 break;
4898 if (i == 8
4899 && TREE_CODE (type) != COMPLEX_EXPR)
4901 const char prefix_minmax[] = "omp declare reduction m";
4902 size_t prefix_size = sizeof (prefix_minmax) - 1;
4903 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4904 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4905 prefix_minmax, prefix_size) == 0
4906 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4907 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4908 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4909 i = 0;
4911 if (i < 8)
4913 error_at (loc, "predeclared arithmetic type %qT in "
4914 "%<#pragma omp declare reduction%>", type);
4915 return;
4918 else if (TREE_CODE (type) == FUNCTION_TYPE
4919 || TREE_CODE (type) == METHOD_TYPE
4920 || TREE_CODE (type) == ARRAY_TYPE)
4922 error_at (loc, "function or array type %qT in "
4923 "%<#pragma omp declare reduction%>", type);
4924 return;
4926 else if (TREE_CODE (type) == REFERENCE_TYPE)
4928 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4929 type);
4930 return;
4932 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4934 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4935 "%<#pragma omp declare reduction%>", type);
4936 return;
4939 tree body = DECL_SAVED_TREE (udr);
4940 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4941 return;
4943 tree_stmt_iterator tsi;
4944 struct cp_check_omp_declare_reduction_data data;
4945 memset (data.stmts, 0, sizeof data.stmts);
4946 for (i = 0, tsi = tsi_start (body);
4947 i < 7 && !tsi_end_p (tsi);
4948 i++, tsi_next (&tsi))
4949 data.stmts[i] = tsi_stmt (tsi);
4950 data.loc = loc;
4951 gcc_assert (tsi_end_p (tsi));
4952 if (i >= 3)
4954 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4955 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4956 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4957 return;
4958 data.combiner_p = true;
4959 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4960 &data, NULL))
4961 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4963 if (i >= 6)
4965 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4966 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4967 data.combiner_p = false;
4968 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4969 &data, NULL)
4970 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4971 cp_check_omp_declare_reduction_r, &data, NULL))
4972 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4973 if (i == 7)
4974 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4978 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4979 an inline call. But, remap
4980 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4981 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4983 static tree
4984 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4985 tree decl, tree placeholder)
4987 copy_body_data id;
4988 struct pointer_map_t *decl_map = pointer_map_create ();
4990 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
4991 *pointer_map_insert (decl_map, omp_decl2) = decl;
4992 memset (&id, 0, sizeof (id));
4993 id.src_fn = DECL_CONTEXT (omp_decl1);
4994 id.dst_fn = current_function_decl;
4995 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4996 id.decl_map = decl_map;
4998 id.copy_decl = copy_decl_no_change;
4999 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5000 id.transform_new_cfg = true;
5001 id.transform_return_to_modify = false;
5002 id.transform_lang_insert_block = NULL;
5003 id.eh_lp_nr = 0;
5004 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5005 pointer_map_destroy (decl_map);
5006 return stmt;
5009 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5010 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5012 static tree
5013 find_omp_placeholder_r (tree *tp, int *, void *data)
5015 if (*tp == (tree) data)
5016 return *tp;
5017 return NULL_TREE;
5020 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5021 Return true if there is some error and the clause should be removed. */
5023 static bool
5024 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5026 tree t = OMP_CLAUSE_DECL (c);
5027 bool predefined = false;
5028 tree type = TREE_TYPE (t);
5029 if (TREE_CODE (type) == REFERENCE_TYPE)
5030 type = TREE_TYPE (type);
5031 if (type == error_mark_node)
5032 return true;
5033 else if (ARITHMETIC_TYPE_P (type))
5034 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5036 case PLUS_EXPR:
5037 case MULT_EXPR:
5038 case MINUS_EXPR:
5039 predefined = true;
5040 break;
5041 case MIN_EXPR:
5042 case MAX_EXPR:
5043 if (TREE_CODE (type) == COMPLEX_TYPE)
5044 break;
5045 predefined = true;
5046 break;
5047 case BIT_AND_EXPR:
5048 case BIT_IOR_EXPR:
5049 case BIT_XOR_EXPR:
5050 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5051 break;
5052 predefined = true;
5053 break;
5054 case TRUTH_ANDIF_EXPR:
5055 case TRUTH_ORIF_EXPR:
5056 if (FLOAT_TYPE_P (type))
5057 break;
5058 predefined = true;
5059 break;
5060 default:
5061 break;
5063 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5065 error ("%qE has invalid type for %<reduction%>", t);
5066 return true;
5068 else if (!processing_template_decl)
5070 t = require_complete_type (t);
5071 if (t == error_mark_node)
5072 return true;
5073 OMP_CLAUSE_DECL (c) = t;
5076 if (predefined)
5078 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5079 return false;
5081 else if (processing_template_decl)
5082 return false;
5084 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5086 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5087 if (TREE_CODE (type) == REFERENCE_TYPE)
5088 type = TREE_TYPE (type);
5089 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5090 if (id == NULL_TREE)
5091 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5092 NULL_TREE, NULL_TREE);
5093 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5094 if (id)
5096 if (id == error_mark_node)
5097 return true;
5098 id = OVL_CURRENT (id);
5099 mark_used (id);
5100 tree body = DECL_SAVED_TREE (id);
5101 if (TREE_CODE (body) == STATEMENT_LIST)
5103 tree_stmt_iterator tsi;
5104 tree placeholder = NULL_TREE;
5105 int i;
5106 tree stmts[7];
5107 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5108 atype = TREE_TYPE (atype);
5109 bool need_static_cast = !same_type_p (type, atype);
5110 memset (stmts, 0, sizeof stmts);
5111 for (i = 0, tsi = tsi_start (body);
5112 i < 7 && !tsi_end_p (tsi);
5113 i++, tsi_next (&tsi))
5114 stmts[i] = tsi_stmt (tsi);
5115 gcc_assert (tsi_end_p (tsi));
5117 if (i >= 3)
5119 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5120 && TREE_CODE (stmts[1]) == DECL_EXPR);
5121 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5122 DECL_ARTIFICIAL (placeholder) = 1;
5123 DECL_IGNORED_P (placeholder) = 1;
5124 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5125 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5126 cxx_mark_addressable (placeholder);
5127 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5128 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5129 != REFERENCE_TYPE)
5130 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5131 tree omp_out = placeholder;
5132 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5133 if (need_static_cast)
5135 tree rtype = build_reference_type (atype);
5136 omp_out = build_static_cast (rtype, omp_out,
5137 tf_warning_or_error);
5138 omp_in = build_static_cast (rtype, omp_in,
5139 tf_warning_or_error);
5140 if (omp_out == error_mark_node || omp_in == error_mark_node)
5141 return true;
5142 omp_out = convert_from_reference (omp_out);
5143 omp_in = convert_from_reference (omp_in);
5145 OMP_CLAUSE_REDUCTION_MERGE (c)
5146 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5147 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5149 if (i >= 6)
5151 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5152 && TREE_CODE (stmts[4]) == DECL_EXPR);
5153 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5154 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5155 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5156 cxx_mark_addressable (placeholder);
5157 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5158 tree omp_orig = placeholder;
5159 if (need_static_cast)
5161 if (i == 7)
5163 error_at (OMP_CLAUSE_LOCATION (c),
5164 "user defined reduction with constructor "
5165 "initializer for base class %qT", atype);
5166 return true;
5168 tree rtype = build_reference_type (atype);
5169 omp_priv = build_static_cast (rtype, omp_priv,
5170 tf_warning_or_error);
5171 omp_orig = build_static_cast (rtype, omp_orig,
5172 tf_warning_or_error);
5173 if (omp_priv == error_mark_node
5174 || omp_orig == error_mark_node)
5175 return true;
5176 omp_priv = convert_from_reference (omp_priv);
5177 omp_orig = convert_from_reference (omp_orig);
5179 if (i == 6)
5180 *need_default_ctor = true;
5181 OMP_CLAUSE_REDUCTION_INIT (c)
5182 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5183 DECL_EXPR_DECL (stmts[3]),
5184 omp_priv, omp_orig);
5185 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5186 find_omp_placeholder_r, placeholder, NULL))
5187 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5189 else if (i >= 3)
5191 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5192 *need_default_ctor = true;
5193 else
5195 tree init;
5196 tree v = convert_from_reference (t);
5197 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5198 init = build_constructor (TREE_TYPE (v), NULL);
5199 else
5200 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5201 OMP_CLAUSE_REDUCTION_INIT (c)
5202 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5207 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5208 *need_dtor = true;
5209 else
5211 error ("user defined reduction not found for %qD", t);
5212 return true;
5214 return false;
5217 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5218 Remove any elements from the list that are invalid. */
5220 tree
5221 finish_omp_clauses (tree clauses)
5223 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5224 bitmap_head aligned_head;
5225 tree c, t, *pc = &clauses;
5226 bool branch_seen = false;
5227 bool copyprivate_seen = false;
5229 bitmap_obstack_initialize (NULL);
5230 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5231 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5232 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5233 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5235 for (pc = &clauses, c = clauses; c ; c = *pc)
5237 bool remove = false;
5239 switch (OMP_CLAUSE_CODE (c))
5241 case OMP_CLAUSE_SHARED:
5242 goto check_dup_generic;
5243 case OMP_CLAUSE_PRIVATE:
5244 goto check_dup_generic;
5245 case OMP_CLAUSE_REDUCTION:
5246 goto check_dup_generic;
5247 case OMP_CLAUSE_COPYPRIVATE:
5248 copyprivate_seen = true;
5249 goto check_dup_generic;
5250 case OMP_CLAUSE_COPYIN:
5251 goto check_dup_generic;
5252 case OMP_CLAUSE_LINEAR:
5253 t = OMP_CLAUSE_DECL (c);
5254 if (!type_dependent_expression_p (t)
5255 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5256 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5258 error ("linear clause applied to non-integral non-pointer "
5259 "variable with %qT type", TREE_TYPE (t));
5260 remove = true;
5261 break;
5263 t = OMP_CLAUSE_LINEAR_STEP (c);
5264 if (t == NULL_TREE)
5265 t = integer_one_node;
5266 if (t == error_mark_node)
5268 remove = true;
5269 break;
5271 else if (!type_dependent_expression_p (t)
5272 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5274 error ("linear step expression must be integral");
5275 remove = true;
5276 break;
5278 else
5280 t = mark_rvalue_use (t);
5281 if (!processing_template_decl)
5283 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5284 t = maybe_constant_value (t);
5285 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5286 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5287 == POINTER_TYPE)
5289 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5290 OMP_CLAUSE_DECL (c), t);
5291 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5292 MINUS_EXPR, sizetype, t,
5293 OMP_CLAUSE_DECL (c));
5294 if (t == error_mark_node)
5296 remove = true;
5297 break;
5301 OMP_CLAUSE_LINEAR_STEP (c) = t;
5303 goto check_dup_generic;
5304 check_dup_generic:
5305 t = OMP_CLAUSE_DECL (c);
5306 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5308 if (processing_template_decl)
5309 break;
5310 if (DECL_P (t))
5311 error ("%qD is not a variable in clause %qs", t,
5312 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5313 else
5314 error ("%qE is not a variable in clause %qs", t,
5315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5316 remove = true;
5318 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5319 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5320 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5322 error ("%qD appears more than once in data clauses", t);
5323 remove = true;
5325 else
5326 bitmap_set_bit (&generic_head, DECL_UID (t));
5327 break;
5329 case OMP_CLAUSE_FIRSTPRIVATE:
5330 t = OMP_CLAUSE_DECL (c);
5331 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5333 if (processing_template_decl)
5334 break;
5335 if (DECL_P (t))
5336 error ("%qD is not a variable in clause %<firstprivate%>", t);
5337 else
5338 error ("%qE is not a variable in clause %<firstprivate%>", t);
5339 remove = true;
5341 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5342 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5344 error ("%qD appears more than once in data clauses", t);
5345 remove = true;
5347 else
5348 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5349 break;
5351 case OMP_CLAUSE_LASTPRIVATE:
5352 t = OMP_CLAUSE_DECL (c);
5353 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5355 if (processing_template_decl)
5356 break;
5357 if (DECL_P (t))
5358 error ("%qD is not a variable in clause %<lastprivate%>", t);
5359 else
5360 error ("%qE is not a variable in clause %<lastprivate%>", t);
5361 remove = true;
5363 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5364 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5366 error ("%qD appears more than once in data clauses", t);
5367 remove = true;
5369 else
5370 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5371 break;
5373 case OMP_CLAUSE_IF:
5374 t = OMP_CLAUSE_IF_EXPR (c);
5375 t = maybe_convert_cond (t);
5376 if (t == error_mark_node)
5377 remove = true;
5378 else if (!processing_template_decl)
5379 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5380 OMP_CLAUSE_IF_EXPR (c) = t;
5381 break;
5383 case OMP_CLAUSE_FINAL:
5384 t = OMP_CLAUSE_FINAL_EXPR (c);
5385 t = maybe_convert_cond (t);
5386 if (t == error_mark_node)
5387 remove = true;
5388 else if (!processing_template_decl)
5389 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5390 OMP_CLAUSE_FINAL_EXPR (c) = t;
5391 break;
5393 case OMP_CLAUSE_NUM_THREADS:
5394 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5395 if (t == error_mark_node)
5396 remove = true;
5397 else if (!type_dependent_expression_p (t)
5398 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5400 error ("num_threads expression must be integral");
5401 remove = true;
5403 else
5405 t = mark_rvalue_use (t);
5406 if (!processing_template_decl)
5407 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5408 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5410 break;
5412 case OMP_CLAUSE_SCHEDULE:
5413 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5414 if (t == NULL)
5416 else if (t == error_mark_node)
5417 remove = true;
5418 else if (!type_dependent_expression_p (t)
5419 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5421 error ("schedule chunk size expression must be integral");
5422 remove = true;
5424 else
5426 t = mark_rvalue_use (t);
5427 if (!processing_template_decl)
5428 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5429 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5431 break;
5433 case OMP_CLAUSE_SIMDLEN:
5434 case OMP_CLAUSE_SAFELEN:
5435 t = OMP_CLAUSE_OPERAND (c, 0);
5436 if (t == error_mark_node)
5437 remove = true;
5438 else if (!type_dependent_expression_p (t)
5439 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5441 error ("%qs length expression must be integral",
5442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5443 remove = true;
5445 else
5447 t = mark_rvalue_use (t);
5448 t = maybe_constant_value (t);
5449 if (!processing_template_decl)
5451 if (TREE_CODE (t) != INTEGER_CST
5452 || tree_int_cst_sgn (t) != 1)
5454 error ("%qs length expression must be positive constant"
5455 " integer expression",
5456 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5457 remove = true;
5460 OMP_CLAUSE_OPERAND (c, 0) = t;
5462 break;
5464 case OMP_CLAUSE_NUM_TEAMS:
5465 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5466 if (t == error_mark_node)
5467 remove = true;
5468 else if (!type_dependent_expression_p (t)
5469 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5471 error ("%<num_teams%> expression must be integral");
5472 remove = true;
5474 else
5476 t = mark_rvalue_use (t);
5477 if (!processing_template_decl)
5478 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5479 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5481 break;
5483 case OMP_CLAUSE_THREAD_LIMIT:
5484 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5485 if (t == error_mark_node)
5486 remove = true;
5487 else if (!type_dependent_expression_p (t)
5488 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5490 error ("%<thread_limit%> expression must be integral");
5491 remove = true;
5493 else
5495 t = mark_rvalue_use (t);
5496 if (!processing_template_decl)
5497 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5498 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5500 break;
5502 case OMP_CLAUSE_DEVICE:
5503 t = OMP_CLAUSE_DEVICE_ID (c);
5504 if (t == error_mark_node)
5505 remove = true;
5506 else if (!type_dependent_expression_p (t)
5507 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5509 error ("%<device%> id must be integral");
5510 remove = true;
5512 else
5514 t = mark_rvalue_use (t);
5515 if (!processing_template_decl)
5516 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5517 OMP_CLAUSE_DEVICE_ID (c) = t;
5519 break;
5521 case OMP_CLAUSE_DIST_SCHEDULE:
5522 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5523 if (t == NULL)
5525 else if (t == error_mark_node)
5526 remove = true;
5527 else if (!type_dependent_expression_p (t)
5528 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5530 error ("%<dist_schedule%> chunk size expression must be "
5531 "integral");
5532 remove = true;
5534 else
5536 t = mark_rvalue_use (t);
5537 if (!processing_template_decl)
5538 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5539 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5541 break;
5543 case OMP_CLAUSE_ALIGNED:
5544 t = OMP_CLAUSE_DECL (c);
5545 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5547 if (processing_template_decl)
5548 break;
5549 if (DECL_P (t))
5550 error ("%qD is not a variable in %<aligned%> clause", t);
5551 else
5552 error ("%qE is not a variable in %<aligned%> clause", t);
5553 remove = true;
5555 else if (!type_dependent_expression_p (t)
5556 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5557 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5558 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5559 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5560 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5561 != ARRAY_TYPE))))
5563 error_at (OMP_CLAUSE_LOCATION (c),
5564 "%qE in %<aligned%> clause is neither a pointer nor "
5565 "an array nor a reference to pointer or array", t);
5566 remove = true;
5568 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5570 error ("%qD appears more than once in %<aligned%> clauses", t);
5571 remove = true;
5573 else
5574 bitmap_set_bit (&aligned_head, DECL_UID (t));
5575 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5576 if (t == error_mark_node)
5577 remove = true;
5578 else if (t == NULL_TREE)
5579 break;
5580 else if (!type_dependent_expression_p (t)
5581 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5583 error ("%<aligned%> clause alignment expression must "
5584 "be integral");
5585 remove = true;
5587 else
5589 t = mark_rvalue_use (t);
5590 t = maybe_constant_value (t);
5591 if (!processing_template_decl)
5593 if (TREE_CODE (t) != INTEGER_CST
5594 || tree_int_cst_sgn (t) != 1)
5596 error ("%<aligned%> clause alignment expression must be "
5597 "positive constant integer expression");
5598 remove = true;
5601 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5603 break;
5605 case OMP_CLAUSE_DEPEND:
5606 t = OMP_CLAUSE_DECL (c);
5607 if (TREE_CODE (t) == TREE_LIST)
5609 if (handle_omp_array_sections (c))
5610 remove = true;
5611 break;
5613 if (t == error_mark_node)
5614 remove = true;
5615 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5617 if (processing_template_decl)
5618 break;
5619 if (DECL_P (t))
5620 error ("%qD is not a variable in %<depend%> clause", t);
5621 else
5622 error ("%qE is not a variable in %<depend%> clause", t);
5623 remove = true;
5625 else if (!processing_template_decl
5626 && !cxx_mark_addressable (t))
5627 remove = true;
5628 break;
5630 case OMP_CLAUSE_MAP:
5631 case OMP_CLAUSE_TO:
5632 case OMP_CLAUSE_FROM:
5633 t = OMP_CLAUSE_DECL (c);
5634 if (TREE_CODE (t) == TREE_LIST)
5636 if (handle_omp_array_sections (c))
5637 remove = true;
5638 else
5640 t = OMP_CLAUSE_DECL (c);
5641 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5643 error_at (OMP_CLAUSE_LOCATION (c),
5644 "array section does not have mappable type "
5645 "in %qs clause",
5646 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5647 remove = true;
5650 break;
5652 if (t == error_mark_node)
5653 remove = true;
5654 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5656 if (processing_template_decl)
5657 break;
5658 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5659 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5660 break;
5661 if (DECL_P (t))
5662 error ("%qD is not a variable in %qs clause", t,
5663 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5664 else
5665 error ("%qE is not a variable in %qs clause", t,
5666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5667 remove = true;
5669 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5671 error ("%qD is threadprivate variable in %qs clause", t,
5672 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5673 remove = true;
5675 else if (!processing_template_decl
5676 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5677 && !cxx_mark_addressable (t))
5678 remove = true;
5679 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5680 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5681 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5682 == REFERENCE_TYPE)
5683 ? TREE_TYPE (TREE_TYPE (t))
5684 : TREE_TYPE (t)))
5686 error_at (OMP_CLAUSE_LOCATION (c),
5687 "%qD does not have a mappable type in %qs clause", t,
5688 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5689 remove = true;
5691 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5693 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5694 error ("%qD appears more than once in motion clauses", t);
5695 else
5696 error ("%qD appears more than once in map clauses", t);
5697 remove = true;
5699 else
5700 bitmap_set_bit (&generic_head, DECL_UID (t));
5701 break;
5703 case OMP_CLAUSE_UNIFORM:
5704 t = OMP_CLAUSE_DECL (c);
5705 if (TREE_CODE (t) != PARM_DECL)
5707 if (processing_template_decl)
5708 break;
5709 if (DECL_P (t))
5710 error ("%qD is not an argument in %<uniform%> clause", t);
5711 else
5712 error ("%qE is not an argument in %<uniform%> clause", t);
5713 remove = true;
5714 break;
5716 goto check_dup_generic;
5718 case OMP_CLAUSE_NOWAIT:
5719 case OMP_CLAUSE_ORDERED:
5720 case OMP_CLAUSE_DEFAULT:
5721 case OMP_CLAUSE_UNTIED:
5722 case OMP_CLAUSE_COLLAPSE:
5723 case OMP_CLAUSE_MERGEABLE:
5724 case OMP_CLAUSE_PARALLEL:
5725 case OMP_CLAUSE_FOR:
5726 case OMP_CLAUSE_SECTIONS:
5727 case OMP_CLAUSE_TASKGROUP:
5728 case OMP_CLAUSE_PROC_BIND:
5729 break;
5731 case OMP_CLAUSE_INBRANCH:
5732 case OMP_CLAUSE_NOTINBRANCH:
5733 if (branch_seen)
5735 error ("%<inbranch%> clause is incompatible with "
5736 "%<notinbranch%>");
5737 remove = true;
5739 branch_seen = true;
5740 break;
5742 default:
5743 gcc_unreachable ();
5746 if (remove)
5747 *pc = OMP_CLAUSE_CHAIN (c);
5748 else
5749 pc = &OMP_CLAUSE_CHAIN (c);
5752 for (pc = &clauses, c = clauses; c ; c = *pc)
5754 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5755 bool remove = false;
5756 bool need_complete_non_reference = false;
5757 bool need_default_ctor = false;
5758 bool need_copy_ctor = false;
5759 bool need_copy_assignment = false;
5760 bool need_implicitly_determined = false;
5761 bool need_dtor = false;
5762 tree type, inner_type;
5764 switch (c_kind)
5766 case OMP_CLAUSE_SHARED:
5767 need_implicitly_determined = true;
5768 break;
5769 case OMP_CLAUSE_PRIVATE:
5770 need_complete_non_reference = true;
5771 need_default_ctor = true;
5772 need_dtor = true;
5773 need_implicitly_determined = true;
5774 break;
5775 case OMP_CLAUSE_FIRSTPRIVATE:
5776 need_complete_non_reference = true;
5777 need_copy_ctor = true;
5778 need_dtor = true;
5779 need_implicitly_determined = true;
5780 break;
5781 case OMP_CLAUSE_LASTPRIVATE:
5782 need_complete_non_reference = true;
5783 need_copy_assignment = true;
5784 need_implicitly_determined = true;
5785 break;
5786 case OMP_CLAUSE_REDUCTION:
5787 need_implicitly_determined = true;
5788 break;
5789 case OMP_CLAUSE_COPYPRIVATE:
5790 need_copy_assignment = true;
5791 break;
5792 case OMP_CLAUSE_COPYIN:
5793 need_copy_assignment = true;
5794 break;
5795 case OMP_CLAUSE_NOWAIT:
5796 if (copyprivate_seen)
5798 error_at (OMP_CLAUSE_LOCATION (c),
5799 "%<nowait%> clause must not be used together "
5800 "with %<copyprivate%>");
5801 *pc = OMP_CLAUSE_CHAIN (c);
5802 continue;
5804 /* FALLTHRU */
5805 default:
5806 pc = &OMP_CLAUSE_CHAIN (c);
5807 continue;
5810 t = OMP_CLAUSE_DECL (c);
5811 if (processing_template_decl
5812 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5814 pc = &OMP_CLAUSE_CHAIN (c);
5815 continue;
5818 switch (c_kind)
5820 case OMP_CLAUSE_LASTPRIVATE:
5821 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5823 need_default_ctor = true;
5824 need_dtor = true;
5826 break;
5828 case OMP_CLAUSE_REDUCTION:
5829 if (finish_omp_reduction_clause (c, &need_default_ctor,
5830 &need_dtor))
5831 remove = true;
5832 else
5833 t = OMP_CLAUSE_DECL (c);
5834 break;
5836 case OMP_CLAUSE_COPYIN:
5837 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5839 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5840 remove = true;
5842 break;
5844 default:
5845 break;
5848 if (need_complete_non_reference || need_copy_assignment)
5850 t = require_complete_type (t);
5851 if (t == error_mark_node)
5852 remove = true;
5853 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5854 && need_complete_non_reference)
5856 error ("%qE has reference type for %qs", t,
5857 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5858 remove = true;
5861 if (need_implicitly_determined)
5863 const char *share_name = NULL;
5865 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5866 share_name = "threadprivate";
5867 else switch (cxx_omp_predetermined_sharing (t))
5869 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5870 break;
5871 case OMP_CLAUSE_DEFAULT_SHARED:
5872 /* const vars may be specified in firstprivate clause. */
5873 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5874 && cxx_omp_const_qual_no_mutable (t))
5875 break;
5876 share_name = "shared";
5877 break;
5878 case OMP_CLAUSE_DEFAULT_PRIVATE:
5879 share_name = "private";
5880 break;
5881 default:
5882 gcc_unreachable ();
5884 if (share_name)
5886 error ("%qE is predetermined %qs for %qs",
5887 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5888 remove = true;
5892 /* We're interested in the base element, not arrays. */
5893 inner_type = type = TREE_TYPE (t);
5894 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5895 inner_type = TREE_TYPE (inner_type);
5897 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5898 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5899 inner_type = TREE_TYPE (inner_type);
5901 /* Check for special function availability by building a call to one.
5902 Save the results, because later we won't be in the right context
5903 for making these queries. */
5904 if (CLASS_TYPE_P (inner_type)
5905 && COMPLETE_TYPE_P (inner_type)
5906 && (need_default_ctor || need_copy_ctor
5907 || need_copy_assignment || need_dtor)
5908 && !type_dependent_expression_p (t)
5909 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5910 need_copy_ctor, need_copy_assignment,
5911 need_dtor))
5912 remove = true;
5914 if (remove)
5915 *pc = OMP_CLAUSE_CHAIN (c);
5916 else
5917 pc = &OMP_CLAUSE_CHAIN (c);
5920 bitmap_obstack_release (NULL);
5921 return clauses;
5924 /* For all variables in the tree_list VARS, mark them as thread local. */
5926 void
5927 finish_omp_threadprivate (tree vars)
5929 tree t;
5931 /* Mark every variable in VARS to be assigned thread local storage. */
5932 for (t = vars; t; t = TREE_CHAIN (t))
5934 tree v = TREE_PURPOSE (t);
5936 if (error_operand_p (v))
5938 else if (!VAR_P (v))
5939 error ("%<threadprivate%> %qD is not file, namespace "
5940 "or block scope variable", v);
5941 /* If V had already been marked threadprivate, it doesn't matter
5942 whether it had been used prior to this point. */
5943 else if (TREE_USED (v)
5944 && (DECL_LANG_SPECIFIC (v) == NULL
5945 || !CP_DECL_THREADPRIVATE_P (v)))
5946 error ("%qE declared %<threadprivate%> after first use", v);
5947 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5948 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5949 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5950 error ("%<threadprivate%> %qE has incomplete type", v);
5951 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5952 && CP_DECL_CONTEXT (v) != current_class_type)
5953 error ("%<threadprivate%> %qE directive not "
5954 "in %qT definition", v, CP_DECL_CONTEXT (v));
5955 else
5957 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5958 if (DECL_LANG_SPECIFIC (v) == NULL)
5960 retrofit_lang_decl (v);
5962 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5963 after the allocation of the lang_decl structure. */
5964 if (DECL_DISCRIMINATOR_P (v))
5965 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5968 if (! DECL_THREAD_LOCAL_P (v))
5970 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
5971 /* If rtl has been already set for this var, call
5972 make_decl_rtl once again, so that encode_section_info
5973 has a chance to look at the new decl flags. */
5974 if (DECL_RTL_SET_P (v))
5975 make_decl_rtl (v);
5977 CP_DECL_THREADPRIVATE_P (v) = 1;
5982 /* Build an OpenMP structured block. */
5984 tree
5985 begin_omp_structured_block (void)
5987 return do_pushlevel (sk_omp);
5990 tree
5991 finish_omp_structured_block (tree block)
5993 return do_poplevel (block);
5996 /* Similarly, except force the retention of the BLOCK. */
5998 tree
5999 begin_omp_parallel (void)
6001 keep_next_level (true);
6002 return begin_omp_structured_block ();
6005 tree
6006 finish_omp_parallel (tree clauses, tree body)
6008 tree stmt;
6010 body = finish_omp_structured_block (body);
6012 stmt = make_node (OMP_PARALLEL);
6013 TREE_TYPE (stmt) = void_type_node;
6014 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6015 OMP_PARALLEL_BODY (stmt) = body;
6017 return add_stmt (stmt);
6020 tree
6021 begin_omp_task (void)
6023 keep_next_level (true);
6024 return begin_omp_structured_block ();
6027 tree
6028 finish_omp_task (tree clauses, tree body)
6030 tree stmt;
6032 body = finish_omp_structured_block (body);
6034 stmt = make_node (OMP_TASK);
6035 TREE_TYPE (stmt) = void_type_node;
6036 OMP_TASK_CLAUSES (stmt) = clauses;
6037 OMP_TASK_BODY (stmt) = body;
6039 return add_stmt (stmt);
6042 /* Helper function for finish_omp_for. Convert Ith random access iterator
6043 into integral iterator. Return FALSE if successful. */
6045 static bool
6046 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6047 tree condv, tree incrv, tree *body,
6048 tree *pre_body, tree clauses)
6050 tree diff, iter_init, iter_incr = NULL, last;
6051 tree incr_var = NULL, orig_pre_body, orig_body, c;
6052 tree decl = TREE_VEC_ELT (declv, i);
6053 tree init = TREE_VEC_ELT (initv, i);
6054 tree cond = TREE_VEC_ELT (condv, i);
6055 tree incr = TREE_VEC_ELT (incrv, i);
6056 tree iter = decl;
6057 location_t elocus = locus;
6059 if (init && EXPR_HAS_LOCATION (init))
6060 elocus = EXPR_LOCATION (init);
6062 switch (TREE_CODE (cond))
6064 case GT_EXPR:
6065 case GE_EXPR:
6066 case LT_EXPR:
6067 case LE_EXPR:
6068 if (TREE_OPERAND (cond, 1) == iter)
6069 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6070 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6071 if (TREE_OPERAND (cond, 0) != iter)
6072 cond = error_mark_node;
6073 else
6075 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6076 TREE_CODE (cond),
6077 iter, ERROR_MARK,
6078 TREE_OPERAND (cond, 1), ERROR_MARK,
6079 NULL, tf_warning_or_error);
6080 if (error_operand_p (tem))
6081 return true;
6083 break;
6084 default:
6085 cond = error_mark_node;
6086 break;
6088 if (cond == error_mark_node)
6090 error_at (elocus, "invalid controlling predicate");
6091 return true;
6093 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6094 ERROR_MARK, iter, ERROR_MARK, NULL,
6095 tf_warning_or_error);
6096 if (error_operand_p (diff))
6097 return true;
6098 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6100 error_at (elocus, "difference between %qE and %qD does not have integer type",
6101 TREE_OPERAND (cond, 1), iter);
6102 return true;
6105 switch (TREE_CODE (incr))
6107 case PREINCREMENT_EXPR:
6108 case PREDECREMENT_EXPR:
6109 case POSTINCREMENT_EXPR:
6110 case POSTDECREMENT_EXPR:
6111 if (TREE_OPERAND (incr, 0) != iter)
6113 incr = error_mark_node;
6114 break;
6116 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6117 TREE_CODE (incr), iter,
6118 tf_warning_or_error);
6119 if (error_operand_p (iter_incr))
6120 return true;
6121 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6122 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6123 incr = integer_one_node;
6124 else
6125 incr = integer_minus_one_node;
6126 break;
6127 case MODIFY_EXPR:
6128 if (TREE_OPERAND (incr, 0) != iter)
6129 incr = error_mark_node;
6130 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6131 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6133 tree rhs = TREE_OPERAND (incr, 1);
6134 if (TREE_OPERAND (rhs, 0) == iter)
6136 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6137 != INTEGER_TYPE)
6138 incr = error_mark_node;
6139 else
6141 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6142 iter, TREE_CODE (rhs),
6143 TREE_OPERAND (rhs, 1),
6144 tf_warning_or_error);
6145 if (error_operand_p (iter_incr))
6146 return true;
6147 incr = TREE_OPERAND (rhs, 1);
6148 incr = cp_convert (TREE_TYPE (diff), incr,
6149 tf_warning_or_error);
6150 if (TREE_CODE (rhs) == MINUS_EXPR)
6152 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6153 incr = fold_if_not_in_template (incr);
6155 if (TREE_CODE (incr) != INTEGER_CST
6156 && (TREE_CODE (incr) != NOP_EXPR
6157 || (TREE_CODE (TREE_OPERAND (incr, 0))
6158 != INTEGER_CST)))
6159 iter_incr = NULL;
6162 else if (TREE_OPERAND (rhs, 1) == iter)
6164 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6165 || TREE_CODE (rhs) != PLUS_EXPR)
6166 incr = error_mark_node;
6167 else
6169 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6170 PLUS_EXPR,
6171 TREE_OPERAND (rhs, 0),
6172 ERROR_MARK, iter,
6173 ERROR_MARK, NULL,
6174 tf_warning_or_error);
6175 if (error_operand_p (iter_incr))
6176 return true;
6177 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6178 iter, NOP_EXPR,
6179 iter_incr,
6180 tf_warning_or_error);
6181 if (error_operand_p (iter_incr))
6182 return true;
6183 incr = TREE_OPERAND (rhs, 0);
6184 iter_incr = NULL;
6187 else
6188 incr = error_mark_node;
6190 else
6191 incr = error_mark_node;
6192 break;
6193 default:
6194 incr = error_mark_node;
6195 break;
6198 if (incr == error_mark_node)
6200 error_at (elocus, "invalid increment expression");
6201 return true;
6204 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6205 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6206 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6207 && OMP_CLAUSE_DECL (c) == iter)
6208 break;
6210 decl = create_temporary_var (TREE_TYPE (diff));
6211 pushdecl (decl);
6212 add_decl_expr (decl);
6213 last = create_temporary_var (TREE_TYPE (diff));
6214 pushdecl (last);
6215 add_decl_expr (last);
6216 if (c && iter_incr == NULL)
6218 incr_var = create_temporary_var (TREE_TYPE (diff));
6219 pushdecl (incr_var);
6220 add_decl_expr (incr_var);
6222 gcc_assert (stmts_are_full_exprs_p ());
6224 orig_pre_body = *pre_body;
6225 *pre_body = push_stmt_list ();
6226 if (orig_pre_body)
6227 add_stmt (orig_pre_body);
6228 if (init != NULL)
6229 finish_expr_stmt (build_x_modify_expr (elocus,
6230 iter, NOP_EXPR, init,
6231 tf_warning_or_error));
6232 init = build_int_cst (TREE_TYPE (diff), 0);
6233 if (c && iter_incr == NULL)
6235 finish_expr_stmt (build_x_modify_expr (elocus,
6236 incr_var, NOP_EXPR,
6237 incr, tf_warning_or_error));
6238 incr = incr_var;
6239 iter_incr = build_x_modify_expr (elocus,
6240 iter, PLUS_EXPR, incr,
6241 tf_warning_or_error);
6243 finish_expr_stmt (build_x_modify_expr (elocus,
6244 last, NOP_EXPR, init,
6245 tf_warning_or_error));
6246 *pre_body = pop_stmt_list (*pre_body);
6248 cond = cp_build_binary_op (elocus,
6249 TREE_CODE (cond), decl, diff,
6250 tf_warning_or_error);
6251 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6252 elocus, incr, NULL_TREE);
6254 orig_body = *body;
6255 *body = push_stmt_list ();
6256 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6257 iter_init = build_x_modify_expr (elocus,
6258 iter, PLUS_EXPR, iter_init,
6259 tf_warning_or_error);
6260 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6261 finish_expr_stmt (iter_init);
6262 finish_expr_stmt (build_x_modify_expr (elocus,
6263 last, NOP_EXPR, decl,
6264 tf_warning_or_error));
6265 add_stmt (orig_body);
6266 *body = pop_stmt_list (*body);
6268 if (c)
6270 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6271 finish_expr_stmt (iter_incr);
6272 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6273 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6276 TREE_VEC_ELT (declv, i) = decl;
6277 TREE_VEC_ELT (initv, i) = init;
6278 TREE_VEC_ELT (condv, i) = cond;
6279 TREE_VEC_ELT (incrv, i) = incr;
6281 return false;
6284 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6285 are directly for their associated operands in the statement. DECL
6286 and INIT are a combo; if DECL is NULL then INIT ought to be a
6287 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6288 optional statements that need to go before the loop into its
6289 sk_omp scope. */
6291 tree
6292 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6293 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6295 tree omp_for = NULL, orig_incr = NULL;
6296 tree decl, init, cond, incr;
6297 location_t elocus;
6298 int i;
6300 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6301 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6302 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6303 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6305 decl = TREE_VEC_ELT (declv, i);
6306 init = TREE_VEC_ELT (initv, i);
6307 cond = TREE_VEC_ELT (condv, i);
6308 incr = TREE_VEC_ELT (incrv, i);
6309 elocus = locus;
6311 if (decl == NULL)
6313 if (init != NULL)
6314 switch (TREE_CODE (init))
6316 case MODIFY_EXPR:
6317 decl = TREE_OPERAND (init, 0);
6318 init = TREE_OPERAND (init, 1);
6319 break;
6320 case MODOP_EXPR:
6321 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6323 decl = TREE_OPERAND (init, 0);
6324 init = TREE_OPERAND (init, 2);
6326 break;
6327 default:
6328 break;
6331 if (decl == NULL)
6333 error_at (locus,
6334 "expected iteration declaration or initialization");
6335 return NULL;
6339 if (init && EXPR_HAS_LOCATION (init))
6340 elocus = EXPR_LOCATION (init);
6342 if (cond == NULL)
6344 error_at (elocus, "missing controlling predicate");
6345 return NULL;
6348 if (incr == NULL)
6350 error_at (elocus, "missing increment expression");
6351 return NULL;
6354 TREE_VEC_ELT (declv, i) = decl;
6355 TREE_VEC_ELT (initv, i) = init;
6358 if (dependent_omp_for_p (declv, initv, condv, incrv))
6360 tree stmt;
6362 stmt = make_node (code);
6364 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6366 /* This is really just a place-holder. We'll be decomposing this
6367 again and going through the cp_build_modify_expr path below when
6368 we instantiate the thing. */
6369 TREE_VEC_ELT (initv, i)
6370 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6371 TREE_VEC_ELT (initv, i));
6374 TREE_TYPE (stmt) = void_type_node;
6375 OMP_FOR_INIT (stmt) = initv;
6376 OMP_FOR_COND (stmt) = condv;
6377 OMP_FOR_INCR (stmt) = incrv;
6378 OMP_FOR_BODY (stmt) = body;
6379 OMP_FOR_PRE_BODY (stmt) = pre_body;
6380 OMP_FOR_CLAUSES (stmt) = clauses;
6382 SET_EXPR_LOCATION (stmt, locus);
6383 return add_stmt (stmt);
6386 if (processing_template_decl)
6387 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6389 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6391 decl = TREE_VEC_ELT (declv, i);
6392 init = TREE_VEC_ELT (initv, i);
6393 cond = TREE_VEC_ELT (condv, i);
6394 incr = TREE_VEC_ELT (incrv, i);
6395 if (orig_incr)
6396 TREE_VEC_ELT (orig_incr, i) = incr;
6397 elocus = locus;
6399 if (init && EXPR_HAS_LOCATION (init))
6400 elocus = EXPR_LOCATION (init);
6402 if (!DECL_P (decl))
6404 error_at (elocus, "expected iteration declaration or initialization");
6405 return NULL;
6408 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6410 if (orig_incr)
6411 TREE_VEC_ELT (orig_incr, i) = incr;
6412 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6413 TREE_CODE (TREE_OPERAND (incr, 1)),
6414 TREE_OPERAND (incr, 2),
6415 tf_warning_or_error);
6418 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6420 if (code == OMP_SIMD)
6422 error_at (elocus, "%<#pragma omp simd%> used with class "
6423 "iteration variable %qE", decl);
6424 return NULL;
6426 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6427 incrv, &body, &pre_body, clauses))
6428 return NULL;
6429 continue;
6432 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6433 && !TYPE_PTR_P (TREE_TYPE (decl)))
6435 error_at (elocus, "invalid type for iteration variable %qE", decl);
6436 return NULL;
6439 if (!processing_template_decl)
6441 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6442 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6444 else
6445 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6446 if (cond
6447 && TREE_SIDE_EFFECTS (cond)
6448 && COMPARISON_CLASS_P (cond)
6449 && !processing_template_decl)
6451 tree t = TREE_OPERAND (cond, 0);
6452 if (TREE_SIDE_EFFECTS (t)
6453 && t != decl
6454 && (TREE_CODE (t) != NOP_EXPR
6455 || TREE_OPERAND (t, 0) != decl))
6456 TREE_OPERAND (cond, 0)
6457 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6459 t = TREE_OPERAND (cond, 1);
6460 if (TREE_SIDE_EFFECTS (t)
6461 && t != decl
6462 && (TREE_CODE (t) != NOP_EXPR
6463 || TREE_OPERAND (t, 0) != decl))
6464 TREE_OPERAND (cond, 1)
6465 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6467 if (decl == error_mark_node || init == error_mark_node)
6468 return NULL;
6470 TREE_VEC_ELT (declv, i) = decl;
6471 TREE_VEC_ELT (initv, i) = init;
6472 TREE_VEC_ELT (condv, i) = cond;
6473 TREE_VEC_ELT (incrv, i) = incr;
6474 i++;
6477 if (IS_EMPTY_STMT (pre_body))
6478 pre_body = NULL;
6480 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6481 body, pre_body);
6483 if (omp_for == NULL)
6484 return NULL;
6486 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6488 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6489 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6491 if (TREE_CODE (incr) != MODIFY_EXPR)
6492 continue;
6494 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6495 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6496 && !processing_template_decl)
6498 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6499 if (TREE_SIDE_EFFECTS (t)
6500 && t != decl
6501 && (TREE_CODE (t) != NOP_EXPR
6502 || TREE_OPERAND (t, 0) != decl))
6503 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6504 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6506 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6507 if (TREE_SIDE_EFFECTS (t)
6508 && t != decl
6509 && (TREE_CODE (t) != NOP_EXPR
6510 || TREE_OPERAND (t, 0) != decl))
6511 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6512 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6515 if (orig_incr)
6516 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6518 if (omp_for != NULL)
6519 OMP_FOR_CLAUSES (omp_for) = clauses;
6520 return omp_for;
6523 void
6524 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6525 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6527 tree orig_lhs;
6528 tree orig_rhs;
6529 tree orig_v;
6530 tree orig_lhs1;
6531 tree orig_rhs1;
6532 bool dependent_p;
6533 tree stmt;
6535 orig_lhs = lhs;
6536 orig_rhs = rhs;
6537 orig_v = v;
6538 orig_lhs1 = lhs1;
6539 orig_rhs1 = rhs1;
6540 dependent_p = false;
6541 stmt = NULL_TREE;
6543 /* Even in a template, we can detect invalid uses of the atomic
6544 pragma if neither LHS nor RHS is type-dependent. */
6545 if (processing_template_decl)
6547 dependent_p = (type_dependent_expression_p (lhs)
6548 || (rhs && type_dependent_expression_p (rhs))
6549 || (v && type_dependent_expression_p (v))
6550 || (lhs1 && type_dependent_expression_p (lhs1))
6551 || (rhs1 && type_dependent_expression_p (rhs1)));
6552 if (!dependent_p)
6554 lhs = build_non_dependent_expr (lhs);
6555 if (rhs)
6556 rhs = build_non_dependent_expr (rhs);
6557 if (v)
6558 v = build_non_dependent_expr (v);
6559 if (lhs1)
6560 lhs1 = build_non_dependent_expr (lhs1);
6561 if (rhs1)
6562 rhs1 = build_non_dependent_expr (rhs1);
6565 if (!dependent_p)
6567 bool swapped = false;
6568 if (rhs1 && cp_tree_equal (lhs, rhs))
6570 tree tem = rhs;
6571 rhs = rhs1;
6572 rhs1 = tem;
6573 swapped = !commutative_tree_code (opcode);
6575 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6577 if (code == OMP_ATOMIC)
6578 error ("%<#pragma omp atomic update%> uses two different "
6579 "expressions for memory");
6580 else
6581 error ("%<#pragma omp atomic capture%> uses two different "
6582 "expressions for memory");
6583 return;
6585 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6587 if (code == OMP_ATOMIC)
6588 error ("%<#pragma omp atomic update%> uses two different "
6589 "expressions for memory");
6590 else
6591 error ("%<#pragma omp atomic capture%> uses two different "
6592 "expressions for memory");
6593 return;
6595 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6596 v, lhs1, rhs1, swapped, seq_cst);
6597 if (stmt == error_mark_node)
6598 return;
6600 if (processing_template_decl)
6602 if (code == OMP_ATOMIC_READ)
6604 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6605 OMP_ATOMIC_READ, orig_lhs);
6606 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6607 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6609 else
6611 if (opcode == NOP_EXPR)
6612 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6613 else
6614 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6615 if (orig_rhs1)
6616 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6617 COMPOUND_EXPR, orig_rhs1, stmt);
6618 if (code != OMP_ATOMIC)
6620 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6621 code, orig_lhs1, stmt);
6622 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6623 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6626 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6627 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6629 finish_expr_stmt (stmt);
6632 void
6633 finish_omp_barrier (void)
6635 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6636 vec<tree, va_gc> *vec = make_tree_vector ();
6637 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6638 release_tree_vector (vec);
6639 finish_expr_stmt (stmt);
6642 void
6643 finish_omp_flush (void)
6645 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6646 vec<tree, va_gc> *vec = make_tree_vector ();
6647 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6648 release_tree_vector (vec);
6649 finish_expr_stmt (stmt);
6652 void
6653 finish_omp_taskwait (void)
6655 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6656 vec<tree, va_gc> *vec = make_tree_vector ();
6657 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6658 release_tree_vector (vec);
6659 finish_expr_stmt (stmt);
6662 void
6663 finish_omp_taskyield (void)
6665 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6666 vec<tree, va_gc> *vec = make_tree_vector ();
6667 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6668 release_tree_vector (vec);
6669 finish_expr_stmt (stmt);
6672 void
6673 finish_omp_cancel (tree clauses)
6675 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6676 int mask = 0;
6677 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6678 mask = 1;
6679 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6680 mask = 2;
6681 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6682 mask = 4;
6683 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6684 mask = 8;
6685 else
6687 error ("%<#pragma omp cancel must specify one of "
6688 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6689 return;
6691 vec<tree, va_gc> *vec = make_tree_vector ();
6692 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6693 if (ifc != NULL_TREE)
6695 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6696 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6697 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6698 build_zero_cst (type));
6700 else
6701 ifc = boolean_true_node;
6702 vec->quick_push (build_int_cst (integer_type_node, mask));
6703 vec->quick_push (ifc);
6704 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6705 release_tree_vector (vec);
6706 finish_expr_stmt (stmt);
6709 void
6710 finish_omp_cancellation_point (tree clauses)
6712 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6713 int mask = 0;
6714 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6715 mask = 1;
6716 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6717 mask = 2;
6718 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6719 mask = 4;
6720 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6721 mask = 8;
6722 else
6724 error ("%<#pragma omp cancellation point must specify one of "
6725 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6726 return;
6728 vec<tree, va_gc> *vec
6729 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6730 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6731 release_tree_vector (vec);
6732 finish_expr_stmt (stmt);
6735 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6736 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6737 should create an extra compound stmt. */
6739 tree
6740 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6742 tree r;
6744 if (pcompound)
6745 *pcompound = begin_compound_stmt (0);
6747 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6749 /* Only add the statement to the function if support enabled. */
6750 if (flag_tm)
6751 add_stmt (r);
6752 else
6753 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6754 ? G_("%<__transaction_relaxed%> without "
6755 "transactional memory support enabled")
6756 : G_("%<__transaction_atomic%> without "
6757 "transactional memory support enabled")));
6759 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6760 TREE_SIDE_EFFECTS (r) = 1;
6761 return r;
6764 /* End a __transaction_atomic or __transaction_relaxed statement.
6765 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6766 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6767 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6769 void
6770 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6772 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6773 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6774 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6775 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6777 /* noexcept specifications are not allowed for function transactions. */
6778 gcc_assert (!(noex && compound_stmt));
6779 if (noex)
6781 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6782 noex);
6783 /* This may not be true when the STATEMENT_LIST is empty. */
6784 if (EXPR_P (body))
6785 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6786 TREE_SIDE_EFFECTS (body) = 1;
6787 TRANSACTION_EXPR_BODY (stmt) = body;
6790 if (compound_stmt)
6791 finish_compound_stmt (compound_stmt);
6794 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6795 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6796 condition. */
6798 tree
6799 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6801 tree ret;
6802 if (noex)
6804 expr = build_must_not_throw_expr (expr, noex);
6805 if (EXPR_P (expr))
6806 SET_EXPR_LOCATION (expr, loc);
6807 TREE_SIDE_EFFECTS (expr) = 1;
6809 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6810 if (flags & TM_STMT_ATTR_RELAXED)
6811 TRANSACTION_EXPR_RELAXED (ret) = 1;
6812 TREE_SIDE_EFFECTS (ret) = 1;
6813 SET_EXPR_LOCATION (ret, loc);
6814 return ret;
6817 void
6818 init_cp_semantics (void)
6822 /* Build a STATIC_ASSERT for a static assertion with the condition
6823 CONDITION and the message text MESSAGE. LOCATION is the location
6824 of the static assertion in the source code. When MEMBER_P, this
6825 static assertion is a member of a class. */
6826 void
6827 finish_static_assert (tree condition, tree message, location_t location,
6828 bool member_p)
6830 if (message == NULL_TREE
6831 || message == error_mark_node
6832 || condition == NULL_TREE
6833 || condition == error_mark_node)
6834 return;
6836 if (check_for_bare_parameter_packs (condition))
6837 condition = error_mark_node;
6839 if (type_dependent_expression_p (condition)
6840 || value_dependent_expression_p (condition))
6842 /* We're in a template; build a STATIC_ASSERT and put it in
6843 the right place. */
6844 tree assertion;
6846 assertion = make_node (STATIC_ASSERT);
6847 STATIC_ASSERT_CONDITION (assertion) = condition;
6848 STATIC_ASSERT_MESSAGE (assertion) = message;
6849 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6851 if (member_p)
6852 maybe_add_class_template_decl_list (current_class_type,
6853 assertion,
6854 /*friend_p=*/0);
6855 else
6856 add_stmt (assertion);
6858 return;
6861 /* Fold the expression and convert it to a boolean value. */
6862 condition = fold_non_dependent_expr (condition);
6863 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6864 condition = maybe_constant_value (condition);
6866 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6867 /* Do nothing; the condition is satisfied. */
6869 else
6871 location_t saved_loc = input_location;
6873 input_location = location;
6874 if (TREE_CODE (condition) == INTEGER_CST
6875 && integer_zerop (condition))
6876 /* Report the error. */
6877 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6878 else if (condition && condition != error_mark_node)
6880 error ("non-constant condition for static assertion");
6881 if (require_potential_rvalue_constant_expression (condition))
6882 cxx_constant_value (condition);
6884 input_location = saved_loc;
6888 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6889 suitable for use as a type-specifier.
6891 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6892 id-expression or a class member access, FALSE when it was parsed as
6893 a full expression. */
6895 tree
6896 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6897 tsubst_flags_t complain)
6899 tree type = NULL_TREE;
6901 if (!expr || error_operand_p (expr))
6902 return error_mark_node;
6904 if (TYPE_P (expr)
6905 || TREE_CODE (expr) == TYPE_DECL
6906 || (TREE_CODE (expr) == BIT_NOT_EXPR
6907 && TYPE_P (TREE_OPERAND (expr, 0))))
6909 if (complain & tf_error)
6910 error ("argument to decltype must be an expression");
6911 return error_mark_node;
6914 /* Depending on the resolution of DR 1172, we may later need to distinguish
6915 instantiation-dependent but not type-dependent expressions so that, say,
6916 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6917 if (instantiation_dependent_expression_p (expr))
6919 type = cxx_make_type (DECLTYPE_TYPE);
6920 DECLTYPE_TYPE_EXPR (type) = expr;
6921 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6922 = id_expression_or_member_access_p;
6923 SET_TYPE_STRUCTURAL_EQUALITY (type);
6925 return type;
6928 /* The type denoted by decltype(e) is defined as follows: */
6930 expr = resolve_nondeduced_context (expr);
6932 if (invalid_nonstatic_memfn_p (expr, complain))
6933 return error_mark_node;
6935 if (type_unknown_p (expr))
6937 if (complain & tf_error)
6938 error ("decltype cannot resolve address of overloaded function");
6939 return error_mark_node;
6942 /* To get the size of a static data member declared as an array of
6943 unknown bound, we need to instantiate it. */
6944 if (VAR_P (expr)
6945 && VAR_HAD_UNKNOWN_BOUND (expr)
6946 && DECL_TEMPLATE_INSTANTIATION (expr))
6947 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6949 if (id_expression_or_member_access_p)
6951 /* If e is an id-expression or a class member access (5.2.5
6952 [expr.ref]), decltype(e) is defined as the type of the entity
6953 named by e. If there is no such entity, or e names a set of
6954 overloaded functions, the program is ill-formed. */
6955 if (identifier_p (expr))
6956 expr = lookup_name (expr);
6958 if (INDIRECT_REF_P (expr))
6959 /* This can happen when the expression is, e.g., "a.b". Just
6960 look at the underlying operand. */
6961 expr = TREE_OPERAND (expr, 0);
6963 if (TREE_CODE (expr) == OFFSET_REF
6964 || TREE_CODE (expr) == MEMBER_REF
6965 || TREE_CODE (expr) == SCOPE_REF)
6966 /* We're only interested in the field itself. If it is a
6967 BASELINK, we will need to see through it in the next
6968 step. */
6969 expr = TREE_OPERAND (expr, 1);
6971 if (BASELINK_P (expr))
6972 /* See through BASELINK nodes to the underlying function. */
6973 expr = BASELINK_FUNCTIONS (expr);
6975 switch (TREE_CODE (expr))
6977 case FIELD_DECL:
6978 if (DECL_BIT_FIELD_TYPE (expr))
6980 type = DECL_BIT_FIELD_TYPE (expr);
6981 break;
6983 /* Fall through for fields that aren't bitfields. */
6985 case FUNCTION_DECL:
6986 case VAR_DECL:
6987 case CONST_DECL:
6988 case PARM_DECL:
6989 case RESULT_DECL:
6990 case TEMPLATE_PARM_INDEX:
6991 expr = mark_type_use (expr);
6992 type = TREE_TYPE (expr);
6993 break;
6995 case ERROR_MARK:
6996 type = error_mark_node;
6997 break;
6999 case COMPONENT_REF:
7000 case COMPOUND_EXPR:
7001 mark_type_use (expr);
7002 type = is_bitfield_expr_with_lowered_type (expr);
7003 if (!type)
7004 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7005 break;
7007 case BIT_FIELD_REF:
7008 gcc_unreachable ();
7010 case INTEGER_CST:
7011 case PTRMEM_CST:
7012 /* We can get here when the id-expression refers to an
7013 enumerator or non-type template parameter. */
7014 type = TREE_TYPE (expr);
7015 break;
7017 default:
7018 /* Handle instantiated template non-type arguments. */
7019 type = TREE_TYPE (expr);
7020 break;
7023 else
7025 /* Within a lambda-expression:
7027 Every occurrence of decltype((x)) where x is a possibly
7028 parenthesized id-expression that names an entity of
7029 automatic storage duration is treated as if x were
7030 transformed into an access to a corresponding data member
7031 of the closure type that would have been declared if x
7032 were a use of the denoted entity. */
7033 if (outer_automatic_var_p (expr)
7034 && current_function_decl
7035 && LAMBDA_FUNCTION_P (current_function_decl))
7036 type = capture_decltype (expr);
7037 else if (error_operand_p (expr))
7038 type = error_mark_node;
7039 else if (expr == current_class_ptr)
7040 /* If the expression is just "this", we want the
7041 cv-unqualified pointer for the "this" type. */
7042 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7043 else
7045 /* Otherwise, where T is the type of e, if e is an lvalue,
7046 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7047 cp_lvalue_kind clk = lvalue_kind (expr);
7048 type = unlowered_expr_type (expr);
7049 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7051 /* For vector types, pick a non-opaque variant. */
7052 if (TREE_CODE (type) == VECTOR_TYPE)
7053 type = strip_typedefs (type);
7055 if (clk != clk_none && !(clk & clk_class))
7056 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7060 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
7061 && (flag_iso || warn_vla > 0))
7063 if (complain & tf_warning_or_error)
7064 pedwarn (input_location, OPT_Wvla,
7065 "taking decltype of array of runtime bound");
7066 else
7067 return error_mark_node;
7070 return type;
7073 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7074 __has_nothrow_copy, depending on assign_p. */
7076 static bool
7077 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7079 tree fns;
7081 if (assign_p)
7083 int ix;
7084 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7085 if (ix < 0)
7086 return false;
7087 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7089 else if (TYPE_HAS_COPY_CTOR (type))
7091 /* If construction of the copy constructor was postponed, create
7092 it now. */
7093 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7094 lazily_declare_fn (sfk_copy_constructor, type);
7095 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7096 lazily_declare_fn (sfk_move_constructor, type);
7097 fns = CLASSTYPE_CONSTRUCTORS (type);
7099 else
7100 return false;
7102 for (; fns; fns = OVL_NEXT (fns))
7104 tree fn = OVL_CURRENT (fns);
7106 if (assign_p)
7108 if (copy_fn_p (fn) == 0)
7109 continue;
7111 else if (copy_fn_p (fn) <= 0)
7112 continue;
7114 maybe_instantiate_noexcept (fn);
7115 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7116 return false;
7119 return true;
7122 /* Actually evaluates the trait. */
7124 static bool
7125 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7127 enum tree_code type_code1;
7128 tree t;
7130 type_code1 = TREE_CODE (type1);
7132 switch (kind)
7134 case CPTK_HAS_NOTHROW_ASSIGN:
7135 type1 = strip_array_types (type1);
7136 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7137 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7138 || (CLASS_TYPE_P (type1)
7139 && classtype_has_nothrow_assign_or_copy_p (type1,
7140 true))));
7142 case CPTK_HAS_TRIVIAL_ASSIGN:
7143 /* ??? The standard seems to be missing the "or array of such a class
7144 type" wording for this trait. */
7145 type1 = strip_array_types (type1);
7146 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7147 && (trivial_type_p (type1)
7148 || (CLASS_TYPE_P (type1)
7149 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7151 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7152 type1 = strip_array_types (type1);
7153 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7154 || (CLASS_TYPE_P (type1)
7155 && (t = locate_ctor (type1))
7156 && (maybe_instantiate_noexcept (t),
7157 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7159 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7160 type1 = strip_array_types (type1);
7161 return (trivial_type_p (type1)
7162 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7164 case CPTK_HAS_NOTHROW_COPY:
7165 type1 = strip_array_types (type1);
7166 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7167 || (CLASS_TYPE_P (type1)
7168 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7170 case CPTK_HAS_TRIVIAL_COPY:
7171 /* ??? The standard seems to be missing the "or array of such a class
7172 type" wording for this trait. */
7173 type1 = strip_array_types (type1);
7174 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7175 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7177 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7178 type1 = strip_array_types (type1);
7179 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7180 || (CLASS_TYPE_P (type1)
7181 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7183 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7184 return type_has_virtual_destructor (type1);
7186 case CPTK_IS_ABSTRACT:
7187 return (ABSTRACT_CLASS_TYPE_P (type1));
7189 case CPTK_IS_BASE_OF:
7190 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7191 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7192 || DERIVED_FROM_P (type1, type2)));
7194 case CPTK_IS_CLASS:
7195 return (NON_UNION_CLASS_TYPE_P (type1));
7197 case CPTK_IS_CONVERTIBLE_TO:
7198 /* TODO */
7199 return false;
7201 case CPTK_IS_EMPTY:
7202 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7204 case CPTK_IS_ENUM:
7205 return (type_code1 == ENUMERAL_TYPE);
7207 case CPTK_IS_FINAL:
7208 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7210 case CPTK_IS_LITERAL_TYPE:
7211 return (literal_type_p (type1));
7213 case CPTK_IS_POD:
7214 return (pod_type_p (type1));
7216 case CPTK_IS_POLYMORPHIC:
7217 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7219 case CPTK_IS_STD_LAYOUT:
7220 return (std_layout_type_p (type1));
7222 case CPTK_IS_TRIVIAL:
7223 return (trivial_type_p (type1));
7225 case CPTK_IS_UNION:
7226 return (type_code1 == UNION_TYPE);
7228 default:
7229 gcc_unreachable ();
7230 return false;
7234 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7235 void, or a complete type, returns it, otherwise NULL_TREE. */
7237 static tree
7238 check_trait_type (tree type)
7240 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7241 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7242 return type;
7244 if (VOID_TYPE_P (type))
7245 return type;
7247 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7250 /* Process a trait expression. */
7252 tree
7253 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7255 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7256 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7257 || kind == CPTK_HAS_NOTHROW_COPY
7258 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7259 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7260 || kind == CPTK_HAS_TRIVIAL_COPY
7261 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7262 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7263 || kind == CPTK_IS_ABSTRACT
7264 || kind == CPTK_IS_BASE_OF
7265 || kind == CPTK_IS_CLASS
7266 || kind == CPTK_IS_CONVERTIBLE_TO
7267 || kind == CPTK_IS_EMPTY
7268 || kind == CPTK_IS_ENUM
7269 || kind == CPTK_IS_FINAL
7270 || kind == CPTK_IS_LITERAL_TYPE
7271 || kind == CPTK_IS_POD
7272 || kind == CPTK_IS_POLYMORPHIC
7273 || kind == CPTK_IS_STD_LAYOUT
7274 || kind == CPTK_IS_TRIVIAL
7275 || kind == CPTK_IS_UNION);
7277 if (kind == CPTK_IS_CONVERTIBLE_TO)
7279 sorry ("__is_convertible_to");
7280 return error_mark_node;
7283 if (type1 == error_mark_node
7284 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7285 && type2 == error_mark_node))
7286 return error_mark_node;
7288 if (processing_template_decl)
7290 tree trait_expr = make_node (TRAIT_EXPR);
7291 TREE_TYPE (trait_expr) = boolean_type_node;
7292 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7293 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7294 TRAIT_EXPR_KIND (trait_expr) = kind;
7295 return trait_expr;
7298 switch (kind)
7300 case CPTK_HAS_NOTHROW_ASSIGN:
7301 case CPTK_HAS_TRIVIAL_ASSIGN:
7302 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7303 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7304 case CPTK_HAS_NOTHROW_COPY:
7305 case CPTK_HAS_TRIVIAL_COPY:
7306 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7307 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7308 case CPTK_IS_ABSTRACT:
7309 case CPTK_IS_EMPTY:
7310 case CPTK_IS_FINAL:
7311 case CPTK_IS_LITERAL_TYPE:
7312 case CPTK_IS_POD:
7313 case CPTK_IS_POLYMORPHIC:
7314 case CPTK_IS_STD_LAYOUT:
7315 case CPTK_IS_TRIVIAL:
7316 if (!check_trait_type (type1))
7317 return error_mark_node;
7318 break;
7320 case CPTK_IS_BASE_OF:
7321 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7322 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7323 && !complete_type_or_else (type2, NULL_TREE))
7324 /* We already issued an error. */
7325 return error_mark_node;
7326 break;
7328 case CPTK_IS_CLASS:
7329 case CPTK_IS_ENUM:
7330 case CPTK_IS_UNION:
7331 break;
7333 case CPTK_IS_CONVERTIBLE_TO:
7334 default:
7335 gcc_unreachable ();
7338 return (trait_expr_value (kind, type1, type2)
7339 ? boolean_true_node : boolean_false_node);
7342 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7343 which is ignored for C++. */
7345 void
7346 set_float_const_decimal64 (void)
7350 void
7351 clear_float_const_decimal64 (void)
7355 bool
7356 float_const_decimal64_p (void)
7358 return 0;
7362 /* Return true if T is a literal type. */
7364 bool
7365 literal_type_p (tree t)
7367 if (SCALAR_TYPE_P (t)
7368 || TREE_CODE (t) == VECTOR_TYPE
7369 || TREE_CODE (t) == REFERENCE_TYPE)
7370 return true;
7371 if (CLASS_TYPE_P (t))
7373 t = complete_type (t);
7374 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7375 return CLASSTYPE_LITERAL_P (t);
7377 if (TREE_CODE (t) == ARRAY_TYPE)
7378 return literal_type_p (strip_array_types (t));
7379 return false;
7382 /* If DECL is a variable declared `constexpr', require its type
7383 be literal. Return the DECL if OK, otherwise NULL. */
7385 tree
7386 ensure_literal_type_for_constexpr_object (tree decl)
7388 tree type = TREE_TYPE (decl);
7389 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7390 && !processing_template_decl)
7392 tree stype = strip_array_types (type);
7393 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7394 /* Don't complain here, we'll complain about incompleteness
7395 when we try to initialize the variable. */;
7396 else if (!literal_type_p (type))
7398 error ("the type %qT of constexpr variable %qD is not literal",
7399 type, decl);
7400 explain_non_literal_class (type);
7401 return NULL;
7404 return decl;
7407 /* Representation of entries in the constexpr function definition table. */
7409 typedef struct GTY(()) constexpr_fundef {
7410 tree decl;
7411 tree body;
7412 } constexpr_fundef;
7414 /* This table holds all constexpr function definitions seen in
7415 the current translation unit. */
7417 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7419 /* Utility function used for managing the constexpr function table.
7420 Return true if the entries pointed to by P and Q are for the
7421 same constexpr function. */
7423 static inline int
7424 constexpr_fundef_equal (const void *p, const void *q)
7426 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7427 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7428 return lhs->decl == rhs->decl;
7431 /* Utility function used for managing the constexpr function table.
7432 Return a hash value for the entry pointed to by Q. */
7434 static inline hashval_t
7435 constexpr_fundef_hash (const void *p)
7437 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7438 return DECL_UID (fundef->decl);
7441 /* Return a previously saved definition of function FUN. */
7443 static constexpr_fundef *
7444 retrieve_constexpr_fundef (tree fun)
7446 constexpr_fundef fundef = { NULL, NULL };
7447 if (constexpr_fundef_table == NULL)
7448 return NULL;
7450 fundef.decl = fun;
7451 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7454 /* Check whether the parameter and return types of FUN are valid for a
7455 constexpr function, and complain if COMPLAIN. */
7457 static bool
7458 is_valid_constexpr_fn (tree fun, bool complain)
7460 bool ret = true;
7462 if (DECL_INHERITED_CTOR_BASE (fun)
7463 && TREE_CODE (fun) == TEMPLATE_DECL)
7465 ret = false;
7466 if (complain)
7467 error ("inherited constructor %qD is not constexpr",
7468 get_inherited_ctor (fun));
7470 else
7472 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
7473 parm != NULL_TREE; parm = TREE_CHAIN (parm))
7474 if (!literal_type_p (TREE_TYPE (parm)))
7476 ret = false;
7477 if (complain)
7479 error ("invalid type for parameter %d of constexpr "
7480 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7481 explain_non_literal_class (TREE_TYPE (parm));
7486 if (!DECL_CONSTRUCTOR_P (fun))
7488 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7489 if (!literal_type_p (rettype))
7491 ret = false;
7492 if (complain)
7494 error ("invalid return type %qT of constexpr function %q+D",
7495 rettype, fun);
7496 explain_non_literal_class (rettype);
7500 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7501 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7503 ret = false;
7504 if (complain)
7506 error ("enclosing class of constexpr non-static member "
7507 "function %q+#D is not a literal type", fun);
7508 explain_non_literal_class (DECL_CONTEXT (fun));
7512 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7514 ret = false;
7515 if (complain)
7516 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7519 return ret;
7522 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7523 for a member of an anonymous aggregate, INIT is the initializer for that
7524 member, and VEC_OUTER is the vector of constructor elements for the class
7525 whose constructor we are processing. Add the initializer to the vector
7526 and return true to indicate success. */
7528 static bool
7529 build_anon_member_initialization (tree member, tree init,
7530 vec<constructor_elt, va_gc> **vec_outer)
7532 /* MEMBER presents the relevant fields from the inside out, but we need
7533 to build up the initializer from the outside in so that we can reuse
7534 previously built CONSTRUCTORs if this is, say, the second field in an
7535 anonymous struct. So we use a vec as a stack. */
7536 auto_vec<tree, 2> fields;
7539 fields.safe_push (TREE_OPERAND (member, 1));
7540 member = TREE_OPERAND (member, 0);
7542 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7543 && TREE_CODE (member) == COMPONENT_REF);
7545 /* VEC has the constructor elements vector for the context of FIELD.
7546 If FIELD is an anonymous aggregate, we will push inside it. */
7547 vec<constructor_elt, va_gc> **vec = vec_outer;
7548 tree field;
7549 while (field = fields.pop(),
7550 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7552 tree ctor;
7553 /* If there is already an outer constructor entry for the anonymous
7554 aggregate FIELD, use it; otherwise, insert one. */
7555 if (vec_safe_is_empty (*vec)
7556 || (*vec)->last().index != field)
7558 ctor = build_constructor (TREE_TYPE (field), NULL);
7559 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7561 else
7562 ctor = (*vec)->last().value;
7563 vec = &CONSTRUCTOR_ELTS (ctor);
7566 /* Now we're at the innermost field, the one that isn't an anonymous
7567 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7568 gcc_assert (fields.is_empty());
7569 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7571 return true;
7574 /* Subroutine of build_constexpr_constructor_member_initializers.
7575 The expression tree T represents a data member initialization
7576 in a (constexpr) constructor definition. Build a pairing of
7577 the data member with its initializer, and prepend that pair
7578 to the existing initialization pair INITS. */
7580 static bool
7581 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7583 tree member, init;
7584 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7585 t = TREE_OPERAND (t, 0);
7586 if (TREE_CODE (t) == EXPR_STMT)
7587 t = TREE_OPERAND (t, 0);
7588 if (t == error_mark_node)
7589 return false;
7590 if (TREE_CODE (t) == STATEMENT_LIST)
7592 tree_stmt_iterator i;
7593 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7595 if (! build_data_member_initialization (tsi_stmt (i), vec))
7596 return false;
7598 return true;
7600 if (TREE_CODE (t) == CLEANUP_STMT)
7602 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7603 but we can in a constexpr constructor for a non-literal class. Just
7604 ignore it; either all the initialization will be constant, in which
7605 case the cleanup can't run, or it can't be constexpr.
7606 Still recurse into CLEANUP_BODY. */
7607 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7609 if (TREE_CODE (t) == CONVERT_EXPR)
7610 t = TREE_OPERAND (t, 0);
7611 if (TREE_CODE (t) == INIT_EXPR
7612 || TREE_CODE (t) == MODIFY_EXPR)
7614 member = TREE_OPERAND (t, 0);
7615 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7617 else if (TREE_CODE (t) == CALL_EXPR)
7619 member = CALL_EXPR_ARG (t, 0);
7620 /* We don't use build_cplus_new here because it complains about
7621 abstract bases. Leaving the call unwrapped means that it has the
7622 wrong type, but cxx_eval_constant_expression doesn't care. */
7623 init = break_out_target_exprs (t);
7625 else if (TREE_CODE (t) == DECL_EXPR)
7626 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7627 return true;
7628 else
7629 gcc_unreachable ();
7630 if (INDIRECT_REF_P (member))
7631 member = TREE_OPERAND (member, 0);
7632 if (TREE_CODE (member) == NOP_EXPR)
7634 tree op = member;
7635 STRIP_NOPS (op);
7636 if (TREE_CODE (op) == ADDR_EXPR)
7638 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7639 (TREE_TYPE (TREE_TYPE (op)),
7640 TREE_TYPE (TREE_TYPE (member))));
7641 /* Initializing a cv-qualified member; we need to look through
7642 the const_cast. */
7643 member = op;
7645 else if (op == current_class_ptr
7646 && (same_type_ignoring_top_level_qualifiers_p
7647 (TREE_TYPE (TREE_TYPE (member)),
7648 current_class_type)))
7649 /* Delegating constructor. */
7650 member = op;
7651 else
7653 /* This is an initializer for an empty base; keep it for now so
7654 we can check it in cxx_eval_bare_aggregate. */
7655 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7658 if (TREE_CODE (member) == ADDR_EXPR)
7659 member = TREE_OPERAND (member, 0);
7660 if (TREE_CODE (member) == COMPONENT_REF)
7662 tree aggr = TREE_OPERAND (member, 0);
7663 if (TREE_CODE (aggr) != COMPONENT_REF)
7664 /* Normal member initialization. */
7665 member = TREE_OPERAND (member, 1);
7666 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7667 /* Initializing a member of an anonymous union. */
7668 return build_anon_member_initialization (member, init, vec);
7669 else
7670 /* We're initializing a vtable pointer in a base. Leave it as
7671 COMPONENT_REF so we remember the path to get to the vfield. */
7672 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7675 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7676 return true;
7679 /* Make sure that there are no statements after LAST in the constructor
7680 body represented by LIST. */
7682 bool
7683 check_constexpr_ctor_body (tree last, tree list)
7685 bool ok = true;
7686 if (TREE_CODE (list) == STATEMENT_LIST)
7688 tree_stmt_iterator i = tsi_last (list);
7689 for (; !tsi_end_p (i); tsi_prev (&i))
7691 tree t = tsi_stmt (i);
7692 if (t == last)
7693 break;
7694 if (TREE_CODE (t) == BIND_EXPR)
7696 if (BIND_EXPR_VARS (t))
7698 ok = false;
7699 break;
7701 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7702 return false;
7703 else
7704 continue;
7706 /* We currently allow typedefs and static_assert.
7707 FIXME allow them in the standard, too. */
7708 if (TREE_CODE (t) != STATIC_ASSERT)
7710 ok = false;
7711 break;
7715 else if (list != last
7716 && TREE_CODE (list) != STATIC_ASSERT)
7717 ok = false;
7718 if (!ok)
7720 error ("constexpr constructor does not have empty body");
7721 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7723 return ok;
7726 /* V is a vector of constructor elements built up for the base and member
7727 initializers of a constructor for TYPE. They need to be in increasing
7728 offset order, which they might not be yet if TYPE has a primary base
7729 which is not first in the base-clause or a vptr and at least one base
7730 all of which are non-primary. */
7732 static vec<constructor_elt, va_gc> *
7733 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7735 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7736 tree field_type;
7737 unsigned i;
7738 constructor_elt *ce;
7740 if (pri)
7741 field_type = BINFO_TYPE (pri);
7742 else if (TYPE_CONTAINS_VPTR_P (type))
7743 field_type = vtbl_ptr_type_node;
7744 else
7745 return v;
7747 /* Find the element for the primary base or vptr and move it to the
7748 beginning of the vec. */
7749 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7750 if (TREE_TYPE (ce->index) == field_type)
7751 break;
7753 if (i > 0 && i < vec_safe_length (v))
7755 vec<constructor_elt, va_gc> &vref = *v;
7756 constructor_elt elt = vref[i];
7757 for (; i > 0; --i)
7758 vref[i] = vref[i-1];
7759 vref[0] = elt;
7762 return v;
7765 /* Build compile-time evalable representations of member-initializer list
7766 for a constexpr constructor. */
7768 static tree
7769 build_constexpr_constructor_member_initializers (tree type, tree body)
7771 vec<constructor_elt, va_gc> *vec = NULL;
7772 bool ok = true;
7773 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7774 || TREE_CODE (body) == EH_SPEC_BLOCK)
7775 body = TREE_OPERAND (body, 0);
7776 if (TREE_CODE (body) == STATEMENT_LIST)
7777 body = STATEMENT_LIST_HEAD (body)->stmt;
7778 body = BIND_EXPR_BODY (body);
7779 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7781 body = TREE_OPERAND (body, 0);
7782 if (TREE_CODE (body) == EXPR_STMT)
7783 body = TREE_OPERAND (body, 0);
7784 if (TREE_CODE (body) == INIT_EXPR
7785 && (same_type_ignoring_top_level_qualifiers_p
7786 (TREE_TYPE (TREE_OPERAND (body, 0)),
7787 current_class_type)))
7789 /* Trivial copy. */
7790 return TREE_OPERAND (body, 1);
7792 ok = build_data_member_initialization (body, &vec);
7794 else if (TREE_CODE (body) == STATEMENT_LIST)
7796 tree_stmt_iterator i;
7797 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7799 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7800 if (!ok)
7801 break;
7804 else if (TREE_CODE (body) == TRY_BLOCK)
7806 error ("body of %<constexpr%> constructor cannot be "
7807 "a function-try-block");
7808 return error_mark_node;
7810 else if (EXPR_P (body))
7811 ok = build_data_member_initialization (body, &vec);
7812 else
7813 gcc_assert (errorcount > 0);
7814 if (ok)
7816 if (vec_safe_length (vec) > 0)
7818 /* In a delegating constructor, return the target. */
7819 constructor_elt *ce = &(*vec)[0];
7820 if (ce->index == current_class_ptr)
7822 body = ce->value;
7823 vec_free (vec);
7824 return body;
7827 vec = sort_constexpr_mem_initializers (type, vec);
7828 return build_constructor (type, vec);
7830 else
7831 return error_mark_node;
7834 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7835 declared to be constexpr, or a sub-statement thereof. Returns the
7836 return value if suitable, error_mark_node for a statement not allowed in
7837 a constexpr function, or NULL_TREE if no return value was found. */
7839 static tree
7840 constexpr_fn_retval (tree body)
7842 switch (TREE_CODE (body))
7844 case STATEMENT_LIST:
7846 tree_stmt_iterator i;
7847 tree expr = NULL_TREE;
7848 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7850 tree s = constexpr_fn_retval (tsi_stmt (i));
7851 if (s == error_mark_node)
7852 return error_mark_node;
7853 else if (s == NULL_TREE)
7854 /* Keep iterating. */;
7855 else if (expr)
7856 /* Multiple return statements. */
7857 return error_mark_node;
7858 else
7859 expr = s;
7861 return expr;
7864 case RETURN_EXPR:
7865 return break_out_target_exprs (TREE_OPERAND (body, 0));
7867 case DECL_EXPR:
7868 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7869 return NULL_TREE;
7870 return error_mark_node;
7872 case CLEANUP_POINT_EXPR:
7873 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7875 case USING_STMT:
7876 return NULL_TREE;
7878 default:
7879 return error_mark_node;
7883 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7884 FUN; do the necessary transformations to turn it into a single expression
7885 that we can store in the hash table. */
7887 static tree
7888 massage_constexpr_body (tree fun, tree body)
7890 if (DECL_CONSTRUCTOR_P (fun))
7891 body = build_constexpr_constructor_member_initializers
7892 (DECL_CONTEXT (fun), body);
7893 else
7895 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7896 body = EH_SPEC_STMTS (body);
7897 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7898 body = TREE_OPERAND (body, 0);
7899 if (TREE_CODE (body) == BIND_EXPR)
7900 body = BIND_EXPR_BODY (body);
7901 body = constexpr_fn_retval (body);
7903 return body;
7906 /* FUN is a constexpr constructor with massaged body BODY. Return true
7907 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7909 static bool
7910 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7912 bool bad;
7913 tree field;
7914 unsigned i, nelts;
7915 tree ctype;
7917 if (TREE_CODE (body) != CONSTRUCTOR)
7918 return false;
7920 nelts = CONSTRUCTOR_NELTS (body);
7921 ctype = DECL_CONTEXT (fun);
7922 field = TYPE_FIELDS (ctype);
7924 if (TREE_CODE (ctype) == UNION_TYPE)
7926 if (nelts == 0 && next_initializable_field (field))
7928 if (complain)
7929 error ("%<constexpr%> constructor for union %qT must "
7930 "initialize exactly one non-static data member", ctype);
7931 return true;
7933 return false;
7936 bad = false;
7937 for (i = 0; i <= nelts; ++i)
7939 tree index;
7940 if (i == nelts)
7941 index = NULL_TREE;
7942 else
7944 index = CONSTRUCTOR_ELT (body, i)->index;
7945 /* Skip base and vtable inits. */
7946 if (TREE_CODE (index) != FIELD_DECL
7947 || DECL_ARTIFICIAL (index))
7948 continue;
7950 for (; field != index; field = DECL_CHAIN (field))
7952 tree ftype;
7953 if (TREE_CODE (field) != FIELD_DECL
7954 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7955 || DECL_ARTIFICIAL (field))
7956 continue;
7957 ftype = strip_array_types (TREE_TYPE (field));
7958 if (type_has_constexpr_default_constructor (ftype))
7960 /* It's OK to skip a member with a trivial constexpr ctor.
7961 A constexpr ctor that isn't trivial should have been
7962 added in by now. */
7963 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7964 || errorcount != 0);
7965 continue;
7967 if (!complain)
7968 return true;
7969 error ("uninitialized member %qD in %<constexpr%> constructor",
7970 field);
7971 bad = true;
7973 if (field == NULL_TREE)
7974 break;
7975 field = DECL_CHAIN (field);
7978 return bad;
7981 /* We are processing the definition of the constexpr function FUN.
7982 Check that its BODY fulfills the propriate requirements and
7983 enter it in the constexpr function definition table.
7984 For constructor BODY is actually the TREE_LIST of the
7985 member-initializer list. */
7987 tree
7988 register_constexpr_fundef (tree fun, tree body)
7990 constexpr_fundef entry;
7991 constexpr_fundef **slot;
7993 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
7994 return NULL;
7996 body = massage_constexpr_body (fun, body);
7997 if (body == NULL_TREE || body == error_mark_node)
7999 if (!DECL_CONSTRUCTOR_P (fun))
8000 error ("body of constexpr function %qD not a return-statement", fun);
8001 return NULL;
8004 if (!potential_rvalue_constant_expression (body))
8006 if (!DECL_GENERATED_P (fun))
8007 require_potential_rvalue_constant_expression (body);
8008 return NULL;
8011 if (DECL_CONSTRUCTOR_P (fun)
8012 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8013 return NULL;
8015 /* Create the constexpr function table if necessary. */
8016 if (constexpr_fundef_table == NULL)
8017 constexpr_fundef_table = htab_create_ggc (101,
8018 constexpr_fundef_hash,
8019 constexpr_fundef_equal,
8020 ggc_free);
8021 entry.decl = fun;
8022 entry.body = body;
8023 slot = (constexpr_fundef **)
8024 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8026 gcc_assert (*slot == NULL);
8027 *slot = ggc_alloc_constexpr_fundef ();
8028 **slot = entry;
8030 return fun;
8033 /* FUN is a non-constexpr function called in a context that requires a
8034 constant expression. If it comes from a constexpr template, explain why
8035 the instantiation isn't constexpr. */
8037 void
8038 explain_invalid_constexpr_fn (tree fun)
8040 static struct pointer_set_t *diagnosed;
8041 tree body;
8042 location_t save_loc;
8043 /* Only diagnose defaulted functions or instantiations. */
8044 if (!DECL_DEFAULTED_FN (fun)
8045 && !is_instantiation_of_constexpr (fun))
8046 return;
8047 if (diagnosed == NULL)
8048 diagnosed = pointer_set_create ();
8049 if (pointer_set_insert (diagnosed, fun) != 0)
8050 /* Already explained. */
8051 return;
8053 save_loc = input_location;
8054 input_location = DECL_SOURCE_LOCATION (fun);
8055 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8056 /* First check the declaration. */
8057 if (is_valid_constexpr_fn (fun, true))
8059 /* Then if it's OK, the body. */
8060 if (DECL_DEFAULTED_FN (fun))
8061 explain_implicit_non_constexpr (fun);
8062 else
8064 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8065 require_potential_rvalue_constant_expression (body);
8066 if (DECL_CONSTRUCTOR_P (fun))
8067 cx_check_missing_mem_inits (fun, body, true);
8070 input_location = save_loc;
8073 /* Objects of this type represent calls to constexpr functions
8074 along with the bindings of parameters to their arguments, for
8075 the purpose of compile time evaluation. */
8077 typedef struct GTY(()) constexpr_call {
8078 /* Description of the constexpr function definition. */
8079 constexpr_fundef *fundef;
8080 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8081 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8082 Note: This arrangement is made to accommodate the use of
8083 iterative_hash_template_arg (see pt.c). If you change this
8084 representation, also change the hash calculation in
8085 cxx_eval_call_expression. */
8086 tree bindings;
8087 /* Result of the call.
8088 NULL means the call is being evaluated.
8089 error_mark_node means that the evaluation was erroneous;
8090 otherwise, the actuall value of the call. */
8091 tree result;
8092 /* The hash of this call; we remember it here to avoid having to
8093 recalculate it when expanding the hash table. */
8094 hashval_t hash;
8095 } constexpr_call;
8097 /* A table of all constexpr calls that have been evaluated by the
8098 compiler in this translation unit. */
8100 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8102 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8103 bool, bool, bool *, bool *);
8105 /* Compute a hash value for a constexpr call representation. */
8107 static hashval_t
8108 constexpr_call_hash (const void *p)
8110 const constexpr_call *info = (const constexpr_call *) p;
8111 return info->hash;
8114 /* Return 1 if the objects pointed to by P and Q represent calls
8115 to the same constexpr function with the same arguments.
8116 Otherwise, return 0. */
8118 static int
8119 constexpr_call_equal (const void *p, const void *q)
8121 const constexpr_call *lhs = (const constexpr_call *) p;
8122 const constexpr_call *rhs = (const constexpr_call *) q;
8123 tree lhs_bindings;
8124 tree rhs_bindings;
8125 if (lhs == rhs)
8126 return 1;
8127 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8128 return 0;
8129 lhs_bindings = lhs->bindings;
8130 rhs_bindings = rhs->bindings;
8131 while (lhs_bindings != NULL && rhs_bindings != NULL)
8133 tree lhs_arg = TREE_VALUE (lhs_bindings);
8134 tree rhs_arg = TREE_VALUE (rhs_bindings);
8135 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8136 if (!cp_tree_equal (lhs_arg, rhs_arg))
8137 return 0;
8138 lhs_bindings = TREE_CHAIN (lhs_bindings);
8139 rhs_bindings = TREE_CHAIN (rhs_bindings);
8141 return lhs_bindings == rhs_bindings;
8144 /* Initialize the constexpr call table, if needed. */
8146 static void
8147 maybe_initialize_constexpr_call_table (void)
8149 if (constexpr_call_table == NULL)
8150 constexpr_call_table = htab_create_ggc (101,
8151 constexpr_call_hash,
8152 constexpr_call_equal,
8153 ggc_free);
8156 /* Return true if T designates the implied `this' parameter. */
8158 static inline bool
8159 is_this_parameter (tree t)
8161 return t == current_class_ptr;
8164 /* We have an expression tree T that represents a call, either CALL_EXPR
8165 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8166 retrun the _DECL for that function. */
8168 static tree
8169 get_function_named_in_call (tree t)
8171 tree fun = NULL;
8172 switch (TREE_CODE (t))
8174 case CALL_EXPR:
8175 fun = CALL_EXPR_FN (t);
8176 break;
8178 case AGGR_INIT_EXPR:
8179 fun = AGGR_INIT_EXPR_FN (t);
8180 break;
8182 default:
8183 gcc_unreachable();
8184 break;
8186 if (TREE_CODE (fun) == ADDR_EXPR
8187 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8188 fun = TREE_OPERAND (fun, 0);
8189 return fun;
8192 /* We have an expression tree T that represents a call, either CALL_EXPR
8193 or AGGR_INIT_EXPR. Return the Nth argument. */
8195 static inline tree
8196 get_nth_callarg (tree t, int n)
8198 switch (TREE_CODE (t))
8200 case CALL_EXPR:
8201 return CALL_EXPR_ARG (t, n);
8203 case AGGR_INIT_EXPR:
8204 return AGGR_INIT_EXPR_ARG (t, n);
8206 default:
8207 gcc_unreachable ();
8208 return NULL;
8212 /* Look up the binding of the function parameter T in a constexpr
8213 function call context CALL. */
8215 static tree
8216 lookup_parameter_binding (const constexpr_call *call, tree t)
8218 tree b = purpose_member (t, call->bindings);
8219 return TREE_VALUE (b);
8222 /* Attempt to evaluate T which represents a call to a builtin function.
8223 We assume here that all builtin functions evaluate to scalar types
8224 represented by _CST nodes. */
8226 static tree
8227 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8228 bool allow_non_constant, bool addr,
8229 bool *non_constant_p, bool *overflow_p)
8231 const int nargs = call_expr_nargs (t);
8232 tree *args = (tree *) alloca (nargs * sizeof (tree));
8233 tree new_call;
8234 int i;
8235 for (i = 0; i < nargs; ++i)
8237 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8238 allow_non_constant, addr,
8239 non_constant_p, overflow_p);
8240 if (allow_non_constant && *non_constant_p)
8241 return t;
8243 if (*non_constant_p)
8244 return t;
8245 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8246 CALL_EXPR_FN (t), nargs, args);
8247 new_call = fold (new_call);
8248 VERIFY_CONSTANT (new_call);
8249 return new_call;
8252 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8253 the type of the value to match. */
8255 static tree
8256 adjust_temp_type (tree type, tree temp)
8258 if (TREE_TYPE (temp) == type)
8259 return temp;
8260 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8261 if (TREE_CODE (temp) == CONSTRUCTOR)
8262 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8263 gcc_assert (scalarish_type_p (type));
8264 return cp_fold_convert (type, temp);
8267 /* Subroutine of cxx_eval_call_expression.
8268 We are processing a call expression (either CALL_EXPR or
8269 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8270 all arguments and bind their values to correspondings
8271 parameters, making up the NEW_CALL context. */
8273 static void
8274 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8275 constexpr_call *new_call,
8276 bool allow_non_constant,
8277 bool *non_constant_p, bool *overflow_p)
8279 const int nargs = call_expr_nargs (t);
8280 tree fun = new_call->fundef->decl;
8281 tree parms = DECL_ARGUMENTS (fun);
8282 int i;
8283 for (i = 0; i < nargs; ++i)
8285 tree x, arg;
8286 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8287 /* For member function, the first argument is a pointer to the implied
8288 object. And for an object construction, don't bind `this' before
8289 it is fully constructed. */
8290 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8291 goto next;
8292 x = get_nth_callarg (t, i);
8293 if (parms && DECL_BY_REFERENCE (parms))
8295 /* cp_genericize made this a reference for argument passing, but
8296 we don't want to treat it like one for constexpr evaluation. */
8297 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8298 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8299 type = TREE_TYPE (type);
8300 x = convert_from_reference (x);
8302 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8303 TREE_CODE (type) == REFERENCE_TYPE,
8304 non_constant_p, overflow_p);
8305 /* Don't VERIFY_CONSTANT here. */
8306 if (*non_constant_p && allow_non_constant)
8307 return;
8308 /* Just discard ellipsis args after checking their constantitude. */
8309 if (!parms)
8310 continue;
8311 if (*non_constant_p)
8312 /* Don't try to adjust the type of non-constant args. */
8313 goto next;
8315 /* Make sure the binding has the same type as the parm. */
8316 if (TREE_CODE (type) != REFERENCE_TYPE)
8317 arg = adjust_temp_type (type, arg);
8318 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8319 next:
8320 parms = TREE_CHAIN (parms);
8324 /* Variables and functions to manage constexpr call expansion context.
8325 These do not need to be marked for PCH or GC. */
8327 /* FIXME remember and print actual constant arguments. */
8328 static vec<tree> call_stack = vNULL;
8329 static int call_stack_tick;
8330 static int last_cx_error_tick;
8332 static bool
8333 push_cx_call_context (tree call)
8335 ++call_stack_tick;
8336 if (!EXPR_HAS_LOCATION (call))
8337 SET_EXPR_LOCATION (call, input_location);
8338 call_stack.safe_push (call);
8339 if (call_stack.length () > (unsigned) max_constexpr_depth)
8340 return false;
8341 return true;
8344 static void
8345 pop_cx_call_context (void)
8347 ++call_stack_tick;
8348 call_stack.pop ();
8351 vec<tree>
8352 cx_error_context (void)
8354 vec<tree> r = vNULL;
8355 if (call_stack_tick != last_cx_error_tick
8356 && !call_stack.is_empty ())
8357 r = call_stack;
8358 last_cx_error_tick = call_stack_tick;
8359 return r;
8362 /* Subroutine of cxx_eval_constant_expression.
8363 Evaluate the call expression tree T in the context of OLD_CALL expression
8364 evaluation. */
8366 static tree
8367 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8368 bool allow_non_constant, bool addr,
8369 bool *non_constant_p, bool *overflow_p)
8371 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8372 tree fun = get_function_named_in_call (t);
8373 tree result;
8374 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8375 constexpr_call **slot;
8376 constexpr_call *entry;
8377 bool depth_ok;
8379 if (TREE_CODE (fun) != FUNCTION_DECL)
8381 /* Might be a constexpr function pointer. */
8382 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8383 /*addr*/false, non_constant_p, overflow_p);
8384 if (TREE_CODE (fun) == ADDR_EXPR)
8385 fun = TREE_OPERAND (fun, 0);
8387 if (TREE_CODE (fun) != FUNCTION_DECL)
8389 if (!allow_non_constant && !*non_constant_p)
8390 error_at (loc, "expression %qE does not designate a constexpr "
8391 "function", fun);
8392 *non_constant_p = true;
8393 return t;
8395 if (DECL_CLONED_FUNCTION_P (fun))
8396 fun = DECL_CLONED_FUNCTION (fun);
8397 if (is_builtin_fn (fun))
8398 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8399 addr, non_constant_p, overflow_p);
8400 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8402 if (!allow_non_constant)
8404 error_at (loc, "call to non-constexpr function %qD", fun);
8405 explain_invalid_constexpr_fn (fun);
8407 *non_constant_p = true;
8408 return t;
8411 /* Shortcut trivial constructor/op=. */
8412 if (trivial_fn_p (fun))
8414 if (call_expr_nargs (t) == 2)
8416 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8417 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8418 addr, non_constant_p, overflow_p);
8420 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8421 && AGGR_INIT_ZERO_FIRST (t))
8422 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8425 /* If in direct recursive call, optimize definition search. */
8426 if (old_call != NULL && old_call->fundef->decl == fun)
8427 new_call.fundef = old_call->fundef;
8428 else
8430 new_call.fundef = retrieve_constexpr_fundef (fun);
8431 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8433 if (!allow_non_constant)
8435 if (DECL_INITIAL (fun))
8437 /* The definition of fun was somehow unsuitable. */
8438 error_at (loc, "%qD called in a constant expression", fun);
8439 explain_invalid_constexpr_fn (fun);
8441 else
8442 error_at (loc, "%qD used before its definition", fun);
8444 *non_constant_p = true;
8445 return t;
8448 cxx_bind_parameters_in_call (old_call, t, &new_call,
8449 allow_non_constant, non_constant_p, overflow_p);
8450 if (*non_constant_p)
8451 return t;
8453 depth_ok = push_cx_call_context (t);
8455 new_call.hash
8456 = iterative_hash_template_arg (new_call.bindings,
8457 constexpr_fundef_hash (new_call.fundef));
8459 /* If we have seen this call before, we are done. */
8460 maybe_initialize_constexpr_call_table ();
8461 slot = (constexpr_call **)
8462 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8463 entry = *slot;
8464 if (entry == NULL)
8466 /* We need to keep a pointer to the entry, not just the slot, as the
8467 slot can move in the call to cxx_eval_builtin_function_call. */
8468 *slot = entry = ggc_alloc_constexpr_call ();
8469 *entry = new_call;
8471 /* Calls which are in progress have their result set to NULL
8472 so that we can detect circular dependencies. */
8473 else if (entry->result == NULL)
8475 if (!allow_non_constant)
8476 error ("call has circular dependency");
8477 *non_constant_p = true;
8478 entry->result = result = error_mark_node;
8481 if (!depth_ok)
8483 if (!allow_non_constant)
8484 error ("constexpr evaluation depth exceeds maximum of %d (use "
8485 "-fconstexpr-depth= to increase the maximum)",
8486 max_constexpr_depth);
8487 *non_constant_p = true;
8488 entry->result = result = error_mark_node;
8490 else
8492 result = entry->result;
8493 if (!result || result == error_mark_node)
8494 result = (cxx_eval_constant_expression
8495 (&new_call, new_call.fundef->body,
8496 allow_non_constant, addr,
8497 non_constant_p, overflow_p));
8498 if (result == error_mark_node)
8499 *non_constant_p = true;
8500 if (*non_constant_p)
8501 entry->result = result = error_mark_node;
8502 else
8504 /* If this was a call to initialize an object, set the type of
8505 the CONSTRUCTOR to the type of that object. */
8506 if (DECL_CONSTRUCTOR_P (fun))
8508 tree ob_arg = get_nth_callarg (t, 0);
8509 STRIP_NOPS (ob_arg);
8510 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8511 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8512 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8513 result);
8515 entry->result = result;
8519 pop_cx_call_context ();
8520 return unshare_expr (result);
8523 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8525 bool
8526 reduced_constant_expression_p (tree t)
8528 if (TREE_CODE (t) == PTRMEM_CST)
8529 /* Even if we can't lower this yet, it's constant. */
8530 return true;
8531 /* FIXME are we calling this too much? */
8532 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8535 /* Some expressions may have constant operands but are not constant
8536 themselves, such as 1/0. Call this function (or rather, the macro
8537 following it) to check for that condition.
8539 We only call this in places that require an arithmetic constant, not in
8540 places where we might have a non-constant expression that can be a
8541 component of a constant expression, such as the address of a constexpr
8542 variable that might be dereferenced later. */
8544 static bool
8545 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8546 bool *overflow_p)
8548 if (!*non_constant_p && !reduced_constant_expression_p (t))
8550 if (!allow_non_constant)
8551 error ("%q+E is not a constant expression", t);
8552 *non_constant_p = true;
8554 if (TREE_OVERFLOW_P (t))
8556 if (!allow_non_constant)
8558 permerror (input_location, "overflow in constant expression");
8559 /* If we're being permissive (and are in an enforcing
8560 context), ignore the overflow. */
8561 if (flag_permissive)
8562 return *non_constant_p;
8564 *overflow_p = true;
8566 return *non_constant_p;
8569 /* Subroutine of cxx_eval_constant_expression.
8570 Attempt to reduce the unary expression tree T to a compile time value.
8571 If successful, return the value. Otherwise issue a diagnostic
8572 and return error_mark_node. */
8574 static tree
8575 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8576 bool allow_non_constant, bool addr,
8577 bool *non_constant_p, bool *overflow_p)
8579 tree r;
8580 tree orig_arg = TREE_OPERAND (t, 0);
8581 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8582 addr, non_constant_p, overflow_p);
8583 VERIFY_CONSTANT (arg);
8584 if (arg == orig_arg)
8585 return t;
8586 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8587 VERIFY_CONSTANT (r);
8588 return r;
8591 /* Subroutine of cxx_eval_constant_expression.
8592 Like cxx_eval_unary_expression, except for binary expressions. */
8594 static tree
8595 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8596 bool allow_non_constant, bool addr,
8597 bool *non_constant_p, bool *overflow_p)
8599 tree r;
8600 tree orig_lhs = TREE_OPERAND (t, 0);
8601 tree orig_rhs = TREE_OPERAND (t, 1);
8602 tree lhs, rhs;
8603 lhs = cxx_eval_constant_expression (call, orig_lhs,
8604 allow_non_constant, addr,
8605 non_constant_p, overflow_p);
8606 VERIFY_CONSTANT (lhs);
8607 rhs = cxx_eval_constant_expression (call, orig_rhs,
8608 allow_non_constant, addr,
8609 non_constant_p, overflow_p);
8610 VERIFY_CONSTANT (rhs);
8611 if (lhs == orig_lhs && rhs == orig_rhs)
8612 return t;
8613 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8614 VERIFY_CONSTANT (r);
8615 return r;
8618 /* Subroutine of cxx_eval_constant_expression.
8619 Attempt to evaluate condition expressions. Dead branches are not
8620 looked into. */
8622 static tree
8623 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8624 bool allow_non_constant, bool addr,
8625 bool *non_constant_p, bool *overflow_p)
8627 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8628 allow_non_constant, addr,
8629 non_constant_p, overflow_p);
8630 VERIFY_CONSTANT (val);
8631 /* Don't VERIFY_CONSTANT the other operands. */
8632 if (integer_zerop (val))
8633 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8634 allow_non_constant, addr,
8635 non_constant_p, overflow_p);
8636 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8637 allow_non_constant, addr,
8638 non_constant_p, overflow_p);
8641 /* Subroutine of cxx_eval_constant_expression.
8642 Attempt to reduce a reference to an array slot. */
8644 static tree
8645 cxx_eval_array_reference (const constexpr_call *call, tree t,
8646 bool allow_non_constant, bool addr,
8647 bool *non_constant_p, bool *overflow_p)
8649 tree oldary = TREE_OPERAND (t, 0);
8650 tree ary = cxx_eval_constant_expression (call, oldary,
8651 allow_non_constant, addr,
8652 non_constant_p, overflow_p);
8653 tree index, oldidx;
8654 HOST_WIDE_INT i;
8655 tree elem_type;
8656 unsigned len, elem_nchars = 1;
8657 if (*non_constant_p)
8658 return t;
8659 oldidx = TREE_OPERAND (t, 1);
8660 index = cxx_eval_constant_expression (call, oldidx,
8661 allow_non_constant, false,
8662 non_constant_p, overflow_p);
8663 VERIFY_CONSTANT (index);
8664 if (addr && ary == oldary && index == oldidx)
8665 return t;
8666 else if (addr)
8667 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8668 elem_type = TREE_TYPE (TREE_TYPE (ary));
8669 if (TREE_CODE (ary) == CONSTRUCTOR)
8670 len = CONSTRUCTOR_NELTS (ary);
8671 else if (TREE_CODE (ary) == STRING_CST)
8673 elem_nchars = (TYPE_PRECISION (elem_type)
8674 / TYPE_PRECISION (char_type_node));
8675 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8677 else
8679 /* We can't do anything with other tree codes, so use
8680 VERIFY_CONSTANT to complain and fail. */
8681 VERIFY_CONSTANT (ary);
8682 gcc_unreachable ();
8684 if (compare_tree_int (index, len) >= 0)
8686 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8688 /* If it's within the array bounds but doesn't have an explicit
8689 initializer, it's value-initialized. */
8690 tree val = build_value_init (elem_type, tf_warning_or_error);
8691 return cxx_eval_constant_expression (call, val,
8692 allow_non_constant, addr,
8693 non_constant_p, overflow_p);
8696 if (!allow_non_constant)
8697 error ("array subscript out of bound");
8698 *non_constant_p = true;
8699 return t;
8701 else if (tree_int_cst_lt (index, integer_zero_node))
8703 if (!allow_non_constant)
8704 error ("negative array subscript");
8705 *non_constant_p = true;
8706 return t;
8708 i = tree_to_shwi (index);
8709 if (TREE_CODE (ary) == CONSTRUCTOR)
8710 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8711 else if (elem_nchars == 1)
8712 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8713 TREE_STRING_POINTER (ary)[i]);
8714 else
8716 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8717 return native_interpret_expr (type, (const unsigned char *)
8718 TREE_STRING_POINTER (ary)
8719 + i * elem_nchars, elem_nchars);
8721 /* Don't VERIFY_CONSTANT here. */
8724 /* Subroutine of cxx_eval_constant_expression.
8725 Attempt to reduce a field access of a value of class type. */
8727 static tree
8728 cxx_eval_component_reference (const constexpr_call *call, tree t,
8729 bool allow_non_constant, bool addr,
8730 bool *non_constant_p, bool *overflow_p)
8732 unsigned HOST_WIDE_INT i;
8733 tree field;
8734 tree value;
8735 tree part = TREE_OPERAND (t, 1);
8736 tree orig_whole = TREE_OPERAND (t, 0);
8737 tree whole = cxx_eval_constant_expression (call, orig_whole,
8738 allow_non_constant, addr,
8739 non_constant_p, overflow_p);
8740 if (whole == orig_whole)
8741 return t;
8742 if (addr)
8743 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8744 whole, part, NULL_TREE);
8745 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8746 CONSTRUCTOR. */
8747 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8749 if (!allow_non_constant)
8750 error ("%qE is not a constant expression", orig_whole);
8751 *non_constant_p = true;
8753 if (DECL_MUTABLE_P (part))
8755 if (!allow_non_constant)
8756 error ("mutable %qD is not usable in a constant expression", part);
8757 *non_constant_p = true;
8759 if (*non_constant_p)
8760 return t;
8761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8763 if (field == part)
8764 return value;
8766 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8767 && CONSTRUCTOR_NELTS (whole) > 0)
8769 /* DR 1188 says we don't have to deal with this. */
8770 if (!allow_non_constant)
8771 error ("accessing %qD member instead of initialized %qD member in "
8772 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8773 *non_constant_p = true;
8774 return t;
8777 /* If there's no explicit init for this field, it's value-initialized. */
8778 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8779 return cxx_eval_constant_expression (call, value,
8780 allow_non_constant, addr,
8781 non_constant_p, overflow_p);
8784 /* Subroutine of cxx_eval_constant_expression.
8785 Attempt to reduce a field access of a value of class type that is
8786 expressed as a BIT_FIELD_REF. */
8788 static tree
8789 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8790 bool allow_non_constant, bool addr,
8791 bool *non_constant_p, bool *overflow_p)
8793 tree orig_whole = TREE_OPERAND (t, 0);
8794 tree retval, fldval, utype, mask;
8795 bool fld_seen = false;
8796 HOST_WIDE_INT istart, isize;
8797 tree whole = cxx_eval_constant_expression (call, orig_whole,
8798 allow_non_constant, addr,
8799 non_constant_p, overflow_p);
8800 tree start, field, value;
8801 unsigned HOST_WIDE_INT i;
8803 if (whole == orig_whole)
8804 return t;
8805 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8806 CONSTRUCTOR. */
8807 if (!*non_constant_p
8808 && TREE_CODE (whole) != VECTOR_CST
8809 && TREE_CODE (whole) != CONSTRUCTOR)
8811 if (!allow_non_constant)
8812 error ("%qE is not a constant expression", orig_whole);
8813 *non_constant_p = true;
8815 if (*non_constant_p)
8816 return t;
8818 if (TREE_CODE (whole) == VECTOR_CST)
8819 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8820 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8822 start = TREE_OPERAND (t, 2);
8823 istart = tree_to_shwi (start);
8824 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8825 utype = TREE_TYPE (t);
8826 if (!TYPE_UNSIGNED (utype))
8827 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8828 retval = build_int_cst (utype, 0);
8829 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8831 tree bitpos = bit_position (field);
8832 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8833 return value;
8834 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8835 && TREE_CODE (value) == INTEGER_CST
8836 && tree_fits_shwi_p (bitpos)
8837 && tree_fits_shwi_p (DECL_SIZE (field)))
8839 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8840 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8841 HOST_WIDE_INT shift;
8842 if (bit >= istart && bit + sz <= istart + isize)
8844 fldval = fold_convert (utype, value);
8845 mask = build_int_cst_type (utype, -1);
8846 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8847 size_int (TYPE_PRECISION (utype) - sz));
8848 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8849 size_int (TYPE_PRECISION (utype) - sz));
8850 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8851 shift = bit - istart;
8852 if (BYTES_BIG_ENDIAN)
8853 shift = TYPE_PRECISION (utype) - shift - sz;
8854 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8855 size_int (shift));
8856 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8857 fld_seen = true;
8861 if (fld_seen)
8862 return fold_convert (TREE_TYPE (t), retval);
8863 gcc_unreachable ();
8864 return error_mark_node;
8867 /* Subroutine of cxx_eval_constant_expression.
8868 Evaluate a short-circuited logical expression T in the context
8869 of a given constexpr CALL. BAILOUT_VALUE is the value for
8870 early return. CONTINUE_VALUE is used here purely for
8871 sanity check purposes. */
8873 static tree
8874 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8875 tree bailout_value, tree continue_value,
8876 bool allow_non_constant, bool addr,
8877 bool *non_constant_p, bool *overflow_p)
8879 tree r;
8880 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8881 allow_non_constant, addr,
8882 non_constant_p, overflow_p);
8883 VERIFY_CONSTANT (lhs);
8884 if (tree_int_cst_equal (lhs, bailout_value))
8885 return lhs;
8886 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8887 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8888 allow_non_constant, addr, non_constant_p, overflow_p);
8889 VERIFY_CONSTANT (r);
8890 return r;
8893 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8894 CONSTRUCTOR elements to initialize (part of) an object containing that
8895 field. Return a pointer to the constructor_elt corresponding to the
8896 initialization of the field. */
8898 static constructor_elt *
8899 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8901 tree aggr = TREE_OPERAND (ref, 0);
8902 tree field = TREE_OPERAND (ref, 1);
8903 HOST_WIDE_INT i;
8904 constructor_elt *ce;
8906 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8908 if (TREE_CODE (aggr) == COMPONENT_REF)
8910 constructor_elt *base_ce
8911 = base_field_constructor_elt (v, aggr);
8912 v = CONSTRUCTOR_ELTS (base_ce->value);
8915 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8916 if (ce->index == field)
8917 return ce;
8919 gcc_unreachable ();
8920 return NULL;
8923 /* Subroutine of cxx_eval_constant_expression.
8924 The expression tree T denotes a C-style array or a C-style
8925 aggregate. Reduce it to a constant expression. */
8927 static tree
8928 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8929 bool allow_non_constant, bool addr,
8930 bool *non_constant_p, bool *overflow_p)
8932 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8933 vec<constructor_elt, va_gc> *n;
8934 vec_alloc (n, vec_safe_length (v));
8935 constructor_elt *ce;
8936 HOST_WIDE_INT i;
8937 bool changed = false;
8938 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8939 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8941 tree elt = cxx_eval_constant_expression (call, ce->value,
8942 allow_non_constant, addr,
8943 non_constant_p, overflow_p);
8944 /* Don't VERIFY_CONSTANT here. */
8945 if (allow_non_constant && *non_constant_p)
8946 goto fail;
8947 if (elt != ce->value)
8948 changed = true;
8949 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8951 /* This is an initialization of a vfield inside a base
8952 subaggregate that we already initialized; push this
8953 initialization into the previous initialization. */
8954 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8955 inner->value = elt;
8957 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
8959 /* This is an initializer for an empty base; now that we've
8960 checked that it's constant, we can ignore it. */
8961 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8963 else
8964 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
8966 if (*non_constant_p || !changed)
8968 fail:
8969 vec_free (n);
8970 return t;
8972 t = build_constructor (TREE_TYPE (t), n);
8973 TREE_CONSTANT (t) = true;
8974 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
8975 t = fold (t);
8976 return t;
8979 /* Subroutine of cxx_eval_constant_expression.
8980 The expression tree T is a VEC_INIT_EXPR which denotes the desired
8981 initialization of a non-static data member of array type. Reduce it to a
8982 CONSTRUCTOR.
8984 Note that apart from value-initialization (when VALUE_INIT is true),
8985 this is only intended to support value-initialization and the
8986 initializations done by defaulted constructors for classes with
8987 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
8988 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
8989 for the copy/move constructor. */
8991 static tree
8992 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
8993 bool value_init, bool allow_non_constant, bool addr,
8994 bool *non_constant_p, bool *overflow_p)
8996 tree elttype = TREE_TYPE (atype);
8997 int max = tree_to_shwi (array_type_nelts (atype));
8998 vec<constructor_elt, va_gc> *n;
8999 vec_alloc (n, max + 1);
9000 bool pre_init = false;
9001 int i;
9003 /* For the default constructor, build up a call to the default
9004 constructor of the element type. We only need to handle class types
9005 here, as for a constructor to be constexpr, all members must be
9006 initialized, which for a defaulted default constructor means they must
9007 be of a class type with a constexpr default constructor. */
9008 if (TREE_CODE (elttype) == ARRAY_TYPE)
9009 /* We only do this at the lowest level. */;
9010 else if (value_init)
9012 init = build_value_init (elttype, tf_warning_or_error);
9013 init = cxx_eval_constant_expression
9014 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9015 pre_init = true;
9017 else if (!init)
9019 vec<tree, va_gc> *argvec = make_tree_vector ();
9020 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9021 &argvec, elttype, LOOKUP_NORMAL,
9022 tf_warning_or_error);
9023 release_tree_vector (argvec);
9024 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9025 addr, non_constant_p, overflow_p);
9026 pre_init = true;
9029 if (*non_constant_p && !allow_non_constant)
9030 goto fail;
9032 for (i = 0; i <= max; ++i)
9034 tree idx = build_int_cst (size_type_node, i);
9035 tree eltinit;
9036 if (TREE_CODE (elttype) == ARRAY_TYPE)
9038 /* A multidimensional array; recurse. */
9039 if (value_init || init == NULL_TREE)
9040 eltinit = NULL_TREE;
9041 else
9042 eltinit = cp_build_array_ref (input_location, init, idx,
9043 tf_warning_or_error);
9044 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9045 allow_non_constant, addr,
9046 non_constant_p, overflow_p);
9048 else if (pre_init)
9050 /* Initializing an element using value or default initialization
9051 we just pre-built above. */
9052 if (i == 0)
9053 eltinit = init;
9054 else
9055 eltinit = unshare_expr (init);
9057 else
9059 /* Copying an element. */
9060 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9061 (atype, TREE_TYPE (init)));
9062 eltinit = cp_build_array_ref (input_location, init, idx,
9063 tf_warning_or_error);
9064 if (!real_lvalue_p (init))
9065 eltinit = move (eltinit);
9066 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9067 eltinit = cxx_eval_constant_expression
9068 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9070 if (*non_constant_p && !allow_non_constant)
9071 goto fail;
9072 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9075 if (!*non_constant_p)
9077 init = build_constructor (atype, n);
9078 TREE_CONSTANT (init) = true;
9079 return init;
9082 fail:
9083 vec_free (n);
9084 return init;
9087 static tree
9088 cxx_eval_vec_init (const constexpr_call *call, tree t,
9089 bool allow_non_constant, bool addr,
9090 bool *non_constant_p, bool *overflow_p)
9092 tree atype = TREE_TYPE (t);
9093 tree init = VEC_INIT_EXPR_INIT (t);
9094 tree r = cxx_eval_vec_init_1 (call, atype, init,
9095 VEC_INIT_EXPR_VALUE_INIT (t),
9096 allow_non_constant, addr, non_constant_p, overflow_p);
9097 if (*non_constant_p)
9098 return t;
9099 else
9100 return r;
9103 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9104 match. We want to be less strict for simple *& folding; if we have a
9105 non-const temporary that we access through a const pointer, that should
9106 work. We handle this here rather than change fold_indirect_ref_1
9107 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9108 don't really make sense outside of constant expression evaluation. Also
9109 we want to allow folding to COMPONENT_REF, which could cause trouble
9110 with TBAA in fold_indirect_ref_1.
9112 Try to keep this function synced with fold_indirect_ref_1. */
9114 static tree
9115 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9117 tree sub, subtype;
9119 sub = op0;
9120 STRIP_NOPS (sub);
9121 subtype = TREE_TYPE (sub);
9122 if (!POINTER_TYPE_P (subtype))
9123 return NULL_TREE;
9125 if (TREE_CODE (sub) == ADDR_EXPR)
9127 tree op = TREE_OPERAND (sub, 0);
9128 tree optype = TREE_TYPE (op);
9130 /* *&CONST_DECL -> to the value of the const decl. */
9131 if (TREE_CODE (op) == CONST_DECL)
9132 return DECL_INITIAL (op);
9133 /* *&p => p; make sure to handle *&"str"[cst] here. */
9134 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9136 tree fop = fold_read_from_constant_string (op);
9137 if (fop)
9138 return fop;
9139 else
9140 return op;
9142 /* *(foo *)&fooarray => fooarray[0] */
9143 else if (TREE_CODE (optype) == ARRAY_TYPE
9144 && (same_type_ignoring_top_level_qualifiers_p
9145 (type, TREE_TYPE (optype))))
9147 tree type_domain = TYPE_DOMAIN (optype);
9148 tree min_val = size_zero_node;
9149 if (type_domain && TYPE_MIN_VALUE (type_domain))
9150 min_val = TYPE_MIN_VALUE (type_domain);
9151 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9152 NULL_TREE, NULL_TREE);
9154 /* *(foo *)&complexfoo => __real__ complexfoo */
9155 else if (TREE_CODE (optype) == COMPLEX_TYPE
9156 && (same_type_ignoring_top_level_qualifiers_p
9157 (type, TREE_TYPE (optype))))
9158 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9159 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9160 else if (TREE_CODE (optype) == VECTOR_TYPE
9161 && (same_type_ignoring_top_level_qualifiers_p
9162 (type, TREE_TYPE (optype))))
9164 tree part_width = TYPE_SIZE (type);
9165 tree index = bitsize_int (0);
9166 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9168 /* Also handle conversion to an empty base class, which
9169 is represented with a NOP_EXPR. */
9170 else if (is_empty_class (type)
9171 && CLASS_TYPE_P (optype)
9172 && DERIVED_FROM_P (type, optype))
9174 *empty_base = true;
9175 return op;
9177 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9178 else if (RECORD_OR_UNION_TYPE_P (optype))
9180 tree field = TYPE_FIELDS (optype);
9181 for (; field; field = DECL_CHAIN (field))
9182 if (TREE_CODE (field) == FIELD_DECL
9183 && integer_zerop (byte_position (field))
9184 && (same_type_ignoring_top_level_qualifiers_p
9185 (TREE_TYPE (field), type)))
9187 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9188 break;
9192 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9193 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9195 tree op00 = TREE_OPERAND (sub, 0);
9196 tree op01 = TREE_OPERAND (sub, 1);
9198 STRIP_NOPS (op00);
9199 if (TREE_CODE (op00) == ADDR_EXPR)
9201 tree op00type;
9202 op00 = TREE_OPERAND (op00, 0);
9203 op00type = TREE_TYPE (op00);
9205 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9206 if (TREE_CODE (op00type) == VECTOR_TYPE
9207 && (same_type_ignoring_top_level_qualifiers_p
9208 (type, TREE_TYPE (op00type))))
9210 HOST_WIDE_INT offset = tree_to_shwi (op01);
9211 tree part_width = TYPE_SIZE (type);
9212 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9213 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9214 tree index = bitsize_int (indexi);
9216 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9217 return fold_build3_loc (loc,
9218 BIT_FIELD_REF, type, op00,
9219 part_width, index);
9222 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9223 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9224 && (same_type_ignoring_top_level_qualifiers_p
9225 (type, TREE_TYPE (op00type))))
9227 tree size = TYPE_SIZE_UNIT (type);
9228 if (tree_int_cst_equal (size, op01))
9229 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9231 /* ((foo *)&fooarray)[1] => fooarray[1] */
9232 else if (TREE_CODE (op00type) == ARRAY_TYPE
9233 && (same_type_ignoring_top_level_qualifiers_p
9234 (type, TREE_TYPE (op00type))))
9236 tree type_domain = TYPE_DOMAIN (op00type);
9237 tree min_val = size_zero_node;
9238 if (type_domain && TYPE_MIN_VALUE (type_domain))
9239 min_val = TYPE_MIN_VALUE (type_domain);
9240 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9241 TYPE_SIZE_UNIT (type));
9242 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9243 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9244 NULL_TREE, NULL_TREE);
9246 /* Also handle conversion to an empty base class, which
9247 is represented with a NOP_EXPR. */
9248 else if (is_empty_class (type)
9249 && CLASS_TYPE_P (op00type)
9250 && DERIVED_FROM_P (type, op00type))
9252 *empty_base = true;
9253 return op00;
9255 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9256 else if (RECORD_OR_UNION_TYPE_P (op00type))
9258 tree field = TYPE_FIELDS (op00type);
9259 for (; field; field = DECL_CHAIN (field))
9260 if (TREE_CODE (field) == FIELD_DECL
9261 && tree_int_cst_equal (byte_position (field), op01)
9262 && (same_type_ignoring_top_level_qualifiers_p
9263 (TREE_TYPE (field), type)))
9265 return fold_build3 (COMPONENT_REF, type, op00,
9266 field, NULL_TREE);
9267 break;
9272 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9273 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9274 && (same_type_ignoring_top_level_qualifiers_p
9275 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9277 tree type_domain;
9278 tree min_val = size_zero_node;
9279 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9280 if (newsub)
9281 sub = newsub;
9282 else
9283 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9284 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9285 if (type_domain && TYPE_MIN_VALUE (type_domain))
9286 min_val = TYPE_MIN_VALUE (type_domain);
9287 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9288 NULL_TREE);
9291 return NULL_TREE;
9294 static tree
9295 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9296 bool allow_non_constant, bool addr,
9297 bool *non_constant_p, bool *overflow_p)
9299 tree orig_op0 = TREE_OPERAND (t, 0);
9300 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9301 /*addr*/false, non_constant_p, overflow_p);
9302 bool empty_base = false;
9303 tree r;
9305 /* Don't VERIFY_CONSTANT here. */
9306 if (*non_constant_p)
9307 return t;
9309 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9310 &empty_base);
9312 if (r)
9313 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9314 addr, non_constant_p, overflow_p);
9315 else
9317 tree sub = op0;
9318 STRIP_NOPS (sub);
9319 if (TREE_CODE (sub) == ADDR_EXPR)
9321 /* We couldn't fold to a constant value. Make sure it's not
9322 something we should have been able to fold. */
9323 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9324 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9325 /* DR 1188 says we don't have to deal with this. */
9326 if (!allow_non_constant)
9327 error ("accessing value of %qE through a %qT glvalue in a "
9328 "constant expression", build_fold_indirect_ref (sub),
9329 TREE_TYPE (t));
9330 *non_constant_p = true;
9331 return t;
9335 /* If we're pulling out the value of an empty base, make sure
9336 that the whole object is constant and then return an empty
9337 CONSTRUCTOR. */
9338 if (empty_base)
9340 VERIFY_CONSTANT (r);
9341 r = build_constructor (TREE_TYPE (t), NULL);
9342 TREE_CONSTANT (r) = true;
9345 if (r == NULL_TREE)
9347 if (addr && op0 != orig_op0)
9348 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9349 if (!addr)
9350 VERIFY_CONSTANT (t);
9351 return t;
9353 return r;
9356 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9357 Shared between potential_constant_expression and
9358 cxx_eval_constant_expression. */
9360 static void
9361 non_const_var_error (tree r)
9363 tree type = TREE_TYPE (r);
9364 error ("the value of %qD is not usable in a constant "
9365 "expression", r);
9366 /* Avoid error cascade. */
9367 if (DECL_INITIAL (r) == error_mark_node)
9368 return;
9369 if (DECL_DECLARED_CONSTEXPR_P (r))
9370 inform (DECL_SOURCE_LOCATION (r),
9371 "%qD used in its own initializer", r);
9372 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9374 if (!CP_TYPE_CONST_P (type))
9375 inform (DECL_SOURCE_LOCATION (r),
9376 "%q#D is not const", r);
9377 else if (CP_TYPE_VOLATILE_P (type))
9378 inform (DECL_SOURCE_LOCATION (r),
9379 "%q#D is volatile", r);
9380 else if (!DECL_INITIAL (r)
9381 || !TREE_CONSTANT (DECL_INITIAL (r)))
9382 inform (DECL_SOURCE_LOCATION (r),
9383 "%qD was not initialized with a constant "
9384 "expression", r);
9385 else
9386 gcc_unreachable ();
9388 else
9390 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9391 inform (DECL_SOURCE_LOCATION (r),
9392 "%qD was not declared %<constexpr%>", r);
9393 else
9394 inform (DECL_SOURCE_LOCATION (r),
9395 "%qD does not have integral or enumeration type",
9400 /* Subroutine of cxx_eval_constant_expression.
9401 Like cxx_eval_unary_expression, except for trinary expressions. */
9403 static tree
9404 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9405 bool allow_non_constant, bool addr,
9406 bool *non_constant_p, bool *overflow_p)
9408 int i;
9409 tree args[3];
9410 tree val;
9412 for (i = 0; i < 3; i++)
9414 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9415 allow_non_constant, addr,
9416 non_constant_p, overflow_p);
9417 VERIFY_CONSTANT (args[i]);
9420 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9421 args[0], args[1], args[2]);
9422 if (val == NULL_TREE)
9423 return t;
9424 VERIFY_CONSTANT (val);
9425 return val;
9428 /* Attempt to reduce the expression T to a constant value.
9429 On failure, issue diagnostic and return error_mark_node. */
9430 /* FIXME unify with c_fully_fold */
9432 static tree
9433 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9434 bool allow_non_constant, bool addr,
9435 bool *non_constant_p, bool *overflow_p)
9437 tree r = t;
9439 if (t == error_mark_node)
9441 *non_constant_p = true;
9442 return t;
9444 if (CONSTANT_CLASS_P (t))
9446 if (TREE_CODE (t) == PTRMEM_CST)
9447 t = cplus_expand_constant (t);
9448 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9449 *overflow_p = true;
9450 return t;
9452 if (TREE_CODE (t) != NOP_EXPR
9453 && reduced_constant_expression_p (t))
9454 return fold (t);
9456 switch (TREE_CODE (t))
9458 case VAR_DECL:
9459 if (addr)
9460 return t;
9461 /* else fall through. */
9462 case CONST_DECL:
9463 r = integral_constant_value (t);
9464 if (TREE_CODE (r) == TARGET_EXPR
9465 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9466 r = TARGET_EXPR_INITIAL (r);
9467 if (DECL_P (r))
9469 if (!allow_non_constant)
9470 non_const_var_error (r);
9471 *non_constant_p = true;
9473 break;
9475 case FUNCTION_DECL:
9476 case TEMPLATE_DECL:
9477 case LABEL_DECL:
9478 return t;
9480 case PARM_DECL:
9481 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9483 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9485 if (!allow_non_constant)
9486 sorry ("use of the value of the object being constructed "
9487 "in a constant expression");
9488 *non_constant_p = true;
9490 else
9491 r = lookup_parameter_binding (call, t);
9493 else if (addr)
9494 /* Defer in case this is only used for its type. */;
9495 else
9497 if (!allow_non_constant)
9498 error ("%qE is not a constant expression", t);
9499 *non_constant_p = true;
9501 break;
9503 case CALL_EXPR:
9504 case AGGR_INIT_EXPR:
9505 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9506 non_constant_p, overflow_p);
9507 break;
9509 case TARGET_EXPR:
9510 if (!literal_type_p (TREE_TYPE (t)))
9512 if (!allow_non_constant)
9514 error ("temporary of non-literal type %qT in a "
9515 "constant expression", TREE_TYPE (t));
9516 explain_non_literal_class (TREE_TYPE (t));
9518 *non_constant_p = true;
9519 break;
9521 /* else fall through. */
9522 case INIT_EXPR:
9523 /* Pass false for 'addr' because these codes indicate
9524 initialization of a temporary. */
9525 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9526 allow_non_constant, false,
9527 non_constant_p, overflow_p);
9528 if (!*non_constant_p)
9529 /* Adjust the type of the result to the type of the temporary. */
9530 r = adjust_temp_type (TREE_TYPE (t), r);
9531 break;
9533 case SCOPE_REF:
9534 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9535 allow_non_constant, addr,
9536 non_constant_p, overflow_p);
9537 break;
9539 case RETURN_EXPR:
9540 case NON_LVALUE_EXPR:
9541 case TRY_CATCH_EXPR:
9542 case CLEANUP_POINT_EXPR:
9543 case MUST_NOT_THROW_EXPR:
9544 case SAVE_EXPR:
9545 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9546 allow_non_constant, addr,
9547 non_constant_p, overflow_p);
9548 break;
9550 /* These differ from cxx_eval_unary_expression in that this doesn't
9551 check for a constant operand or result; an address can be
9552 constant without its operand being, and vice versa. */
9553 case INDIRECT_REF:
9554 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9555 non_constant_p, overflow_p);
9556 break;
9558 case ADDR_EXPR:
9560 tree oldop = TREE_OPERAND (t, 0);
9561 tree op = cxx_eval_constant_expression (call, oldop,
9562 allow_non_constant,
9563 /*addr*/true,
9564 non_constant_p, overflow_p);
9565 /* Don't VERIFY_CONSTANT here. */
9566 if (*non_constant_p)
9567 return t;
9568 /* This function does more aggressive folding than fold itself. */
9569 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9570 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9571 return t;
9572 break;
9575 case REALPART_EXPR:
9576 case IMAGPART_EXPR:
9577 case CONJ_EXPR:
9578 case FIX_TRUNC_EXPR:
9579 case FLOAT_EXPR:
9580 case NEGATE_EXPR:
9581 case ABS_EXPR:
9582 case BIT_NOT_EXPR:
9583 case TRUTH_NOT_EXPR:
9584 case FIXED_CONVERT_EXPR:
9585 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9586 non_constant_p, overflow_p);
9587 break;
9589 case SIZEOF_EXPR:
9590 if (SIZEOF_EXPR_TYPE_P (t))
9591 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9592 SIZEOF_EXPR, false);
9593 else if (TYPE_P (TREE_OPERAND (t, 0)))
9594 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9595 false);
9596 else
9597 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9598 false);
9599 if (r == error_mark_node)
9600 r = size_one_node;
9601 VERIFY_CONSTANT (r);
9602 break;
9604 case COMPOUND_EXPR:
9606 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9607 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9608 introduced by build_call_a. */
9609 tree op0 = TREE_OPERAND (t, 0);
9610 tree op1 = TREE_OPERAND (t, 1);
9611 STRIP_NOPS (op1);
9612 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9613 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9614 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9615 addr, non_constant_p, overflow_p);
9616 else
9618 /* Check that the LHS is constant and then discard it. */
9619 cxx_eval_constant_expression (call, op0, allow_non_constant,
9620 false, non_constant_p, overflow_p);
9621 op1 = TREE_OPERAND (t, 1);
9622 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9623 addr, non_constant_p, overflow_p);
9626 break;
9628 case POINTER_PLUS_EXPR:
9629 case PLUS_EXPR:
9630 case MINUS_EXPR:
9631 case MULT_EXPR:
9632 case TRUNC_DIV_EXPR:
9633 case CEIL_DIV_EXPR:
9634 case FLOOR_DIV_EXPR:
9635 case ROUND_DIV_EXPR:
9636 case TRUNC_MOD_EXPR:
9637 case CEIL_MOD_EXPR:
9638 case ROUND_MOD_EXPR:
9639 case RDIV_EXPR:
9640 case EXACT_DIV_EXPR:
9641 case MIN_EXPR:
9642 case MAX_EXPR:
9643 case LSHIFT_EXPR:
9644 case RSHIFT_EXPR:
9645 case LROTATE_EXPR:
9646 case RROTATE_EXPR:
9647 case BIT_IOR_EXPR:
9648 case BIT_XOR_EXPR:
9649 case BIT_AND_EXPR:
9650 case TRUTH_XOR_EXPR:
9651 case LT_EXPR:
9652 case LE_EXPR:
9653 case GT_EXPR:
9654 case GE_EXPR:
9655 case EQ_EXPR:
9656 case NE_EXPR:
9657 case UNORDERED_EXPR:
9658 case ORDERED_EXPR:
9659 case UNLT_EXPR:
9660 case UNLE_EXPR:
9661 case UNGT_EXPR:
9662 case UNGE_EXPR:
9663 case UNEQ_EXPR:
9664 case LTGT_EXPR:
9665 case RANGE_EXPR:
9666 case COMPLEX_EXPR:
9667 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9668 non_constant_p, overflow_p);
9669 break;
9671 /* fold can introduce non-IF versions of these; still treat them as
9672 short-circuiting. */
9673 case TRUTH_AND_EXPR:
9674 case TRUTH_ANDIF_EXPR:
9675 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9676 boolean_true_node,
9677 allow_non_constant, addr,
9678 non_constant_p, overflow_p);
9679 break;
9681 case TRUTH_OR_EXPR:
9682 case TRUTH_ORIF_EXPR:
9683 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9684 boolean_false_node,
9685 allow_non_constant, addr,
9686 non_constant_p, overflow_p);
9687 break;
9689 case ARRAY_REF:
9690 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9691 non_constant_p, overflow_p);
9692 break;
9694 case COMPONENT_REF:
9695 if (is_overloaded_fn (t))
9697 /* We can only get here in checking mode via
9698 build_non_dependent_expr, because any expression that
9699 calls or takes the address of the function will have
9700 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9701 gcc_checking_assert (allow_non_constant || errorcount);
9702 *non_constant_p = true;
9703 return t;
9705 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9706 non_constant_p, overflow_p);
9707 break;
9709 case BIT_FIELD_REF:
9710 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9711 non_constant_p, overflow_p);
9712 break;
9714 case COND_EXPR:
9715 case VEC_COND_EXPR:
9716 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9717 non_constant_p, overflow_p);
9718 break;
9720 case CONSTRUCTOR:
9721 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9722 non_constant_p, overflow_p);
9723 break;
9725 case VEC_INIT_EXPR:
9726 /* We can get this in a defaulted constructor for a class with a
9727 non-static data member of array type. Either the initializer will
9728 be NULL, meaning default-initialization, or it will be an lvalue
9729 or xvalue of the same type, meaning direct-initialization from the
9730 corresponding member. */
9731 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9732 non_constant_p, overflow_p);
9733 break;
9735 case FMA_EXPR:
9736 case VEC_PERM_EXPR:
9737 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9738 non_constant_p, overflow_p);
9739 break;
9741 case CONVERT_EXPR:
9742 case VIEW_CONVERT_EXPR:
9743 case NOP_EXPR:
9745 tree oldop = TREE_OPERAND (t, 0);
9746 tree op = cxx_eval_constant_expression (call, oldop,
9747 allow_non_constant, addr,
9748 non_constant_p, overflow_p);
9749 if (*non_constant_p)
9750 return t;
9751 if (POINTER_TYPE_P (TREE_TYPE (t))
9752 && TREE_CODE (op) == INTEGER_CST
9753 && !integer_zerop (op))
9755 if (!allow_non_constant)
9756 error_at (EXPR_LOC_OR_LOC (t, input_location),
9757 "reinterpret_cast from integer to pointer");
9758 *non_constant_p = true;
9759 return t;
9761 if (op == oldop)
9762 /* We didn't fold at the top so we could check for ptr-int
9763 conversion. */
9764 return fold (t);
9765 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9766 /* Conversion of an out-of-range value has implementation-defined
9767 behavior; the language considers it different from arithmetic
9768 overflow, which is undefined. */
9769 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9770 TREE_OVERFLOW (r) = false;
9772 break;
9774 case EMPTY_CLASS_EXPR:
9775 /* This is good enough for a function argument that might not get
9776 used, and they can't do anything with it, so just return it. */
9777 return t;
9779 case LAMBDA_EXPR:
9780 case PREINCREMENT_EXPR:
9781 case POSTINCREMENT_EXPR:
9782 case PREDECREMENT_EXPR:
9783 case POSTDECREMENT_EXPR:
9784 case NEW_EXPR:
9785 case VEC_NEW_EXPR:
9786 case DELETE_EXPR:
9787 case VEC_DELETE_EXPR:
9788 case THROW_EXPR:
9789 case MODIFY_EXPR:
9790 case MODOP_EXPR:
9791 /* GCC internal stuff. */
9792 case VA_ARG_EXPR:
9793 case OBJ_TYPE_REF:
9794 case WITH_CLEANUP_EXPR:
9795 case STATEMENT_LIST:
9796 case BIND_EXPR:
9797 case NON_DEPENDENT_EXPR:
9798 case BASELINK:
9799 case EXPR_STMT:
9800 case OFFSET_REF:
9801 if (!allow_non_constant)
9802 error_at (EXPR_LOC_OR_LOC (t, input_location),
9803 "expression %qE is not a constant-expression", t);
9804 *non_constant_p = true;
9805 break;
9807 default:
9808 internal_error ("unexpected expression %qE of kind %s", t,
9809 get_tree_code_name (TREE_CODE (t)));
9810 *non_constant_p = true;
9811 break;
9814 if (r == error_mark_node)
9815 *non_constant_p = true;
9817 if (*non_constant_p)
9818 return t;
9819 else
9820 return r;
9823 static tree
9824 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9826 bool non_constant_p = false;
9827 bool overflow_p = false;
9828 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9829 false, &non_constant_p, &overflow_p);
9831 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9833 if (TREE_CODE (t) != CONSTRUCTOR
9834 && cp_has_mutable_p (TREE_TYPE (t)))
9836 /* We allow a mutable type if the original expression was a
9837 CONSTRUCTOR so that we can do aggregate initialization of
9838 constexpr variables. */
9839 if (!allow_non_constant)
9840 error ("%qT cannot be the type of a complete constant expression "
9841 "because it has mutable sub-objects", TREE_TYPE (t));
9842 non_constant_p = true;
9845 /* Technically we should check this for all subexpressions, but that
9846 runs into problems with our internal representation of pointer
9847 subtraction and the 5.19 rules are still in flux. */
9848 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9849 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9850 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9852 if (!allow_non_constant)
9853 error ("conversion from pointer type %qT "
9854 "to arithmetic type %qT in a constant-expression",
9855 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9856 non_constant_p = true;
9859 if (!non_constant_p && overflow_p)
9860 non_constant_p = true;
9862 if (non_constant_p && !allow_non_constant)
9863 return error_mark_node;
9864 else if (non_constant_p && TREE_CONSTANT (r))
9866 /* This isn't actually constant, so unset TREE_CONSTANT. */
9867 if (EXPR_P (r))
9868 r = copy_node (r);
9869 else if (TREE_CODE (r) == CONSTRUCTOR)
9870 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9871 else
9872 r = build_nop (TREE_TYPE (r), r);
9873 TREE_CONSTANT (r) = false;
9875 else if (non_constant_p || r == t)
9876 return t;
9878 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9880 if (TREE_CODE (t) == TARGET_EXPR
9881 && TARGET_EXPR_INITIAL (t) == r)
9882 return t;
9883 else
9885 r = get_target_expr (r);
9886 TREE_CONSTANT (r) = true;
9887 return r;
9890 else
9891 return r;
9894 /* Returns true if T is a valid subexpression of a constant expression,
9895 even if it isn't itself a constant expression. */
9897 bool
9898 is_sub_constant_expr (tree t)
9900 bool non_constant_p = false;
9901 bool overflow_p = false;
9902 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9903 &overflow_p);
9904 return !non_constant_p && !overflow_p;
9907 /* If T represents a constant expression returns its reduced value.
9908 Otherwise return error_mark_node. If T is dependent, then
9909 return NULL. */
9911 tree
9912 cxx_constant_value (tree t)
9914 return cxx_eval_outermost_constant_expr (t, false);
9917 /* If T is a constant expression, returns its reduced value.
9918 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9919 Otherwise, returns a version of T without TREE_CONSTANT. */
9921 tree
9922 maybe_constant_value (tree t)
9924 tree r;
9926 if (instantiation_dependent_expression_p (t)
9927 || type_unknown_p (t)
9928 || BRACE_ENCLOSED_INITIALIZER_P (t)
9929 || !potential_constant_expression (t))
9931 if (TREE_OVERFLOW_P (t))
9933 t = build_nop (TREE_TYPE (t), t);
9934 TREE_CONSTANT (t) = false;
9936 return t;
9939 r = cxx_eval_outermost_constant_expr (t, true);
9940 #ifdef ENABLE_CHECKING
9941 /* cp_tree_equal looks through NOPs, so allow them. */
9942 gcc_assert (r == t
9943 || CONVERT_EXPR_P (t)
9944 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9945 || !cp_tree_equal (r, t));
9946 #endif
9947 return r;
9950 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9951 than wrapped in a TARGET_EXPR. */
9953 tree
9954 maybe_constant_init (tree t)
9956 t = maybe_constant_value (t);
9957 if (TREE_CODE (t) == TARGET_EXPR)
9959 tree init = TARGET_EXPR_INITIAL (t);
9960 if (TREE_CODE (init) == CONSTRUCTOR)
9961 t = init;
9963 return t;
9966 #if 0
9967 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9968 /* Return true if the object referred to by REF has automatic or thread
9969 local storage. */
9971 enum { ck_ok, ck_bad, ck_unknown };
9972 static int
9973 check_automatic_or_tls (tree ref)
9975 enum machine_mode mode;
9976 HOST_WIDE_INT bitsize, bitpos;
9977 tree offset;
9978 int volatilep = 0, unsignedp = 0;
9979 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9980 &mode, &unsignedp, &volatilep, false);
9981 duration_kind dk;
9983 /* If there isn't a decl in the middle, we don't know the linkage here,
9984 and this isn't a constant expression anyway. */
9985 if (!DECL_P (decl))
9986 return ck_unknown;
9987 dk = decl_storage_duration (decl);
9988 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9990 #endif
9992 /* Return true if T denotes a potentially constant expression. Issue
9993 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9994 an lvalue-rvalue conversion is implied.
9996 C++0x [expr.const] used to say
9998 6 An expression is a potential constant expression if it is
9999 a constant expression where all occurrences of function
10000 parameters are replaced by arbitrary constant expressions
10001 of the appropriate type.
10003 2 A conditional expression is a constant expression unless it
10004 involves one of the following as a potentially evaluated
10005 subexpression (3.2), but subexpressions of logical AND (5.14),
10006 logical OR (5.15), and conditional (5.16) operations that are
10007 not evaluated are not considered. */
10009 static bool
10010 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10012 enum { any = false, rval = true };
10013 int i;
10014 tree tmp;
10016 if (t == error_mark_node)
10017 return false;
10018 if (t == NULL_TREE)
10019 return true;
10020 if (TREE_THIS_VOLATILE (t))
10022 if (flags & tf_error)
10023 error ("expression %qE has side-effects", t);
10024 return false;
10026 if (CONSTANT_CLASS_P (t))
10027 return true;
10029 switch (TREE_CODE (t))
10031 case FUNCTION_DECL:
10032 case BASELINK:
10033 case TEMPLATE_DECL:
10034 case OVERLOAD:
10035 case TEMPLATE_ID_EXPR:
10036 case LABEL_DECL:
10037 case LABEL_EXPR:
10038 case CONST_DECL:
10039 case SIZEOF_EXPR:
10040 case ALIGNOF_EXPR:
10041 case OFFSETOF_EXPR:
10042 case NOEXCEPT_EXPR:
10043 case TEMPLATE_PARM_INDEX:
10044 case TRAIT_EXPR:
10045 case IDENTIFIER_NODE:
10046 case USERDEF_LITERAL:
10047 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10048 case FIELD_DECL:
10049 case PARM_DECL:
10050 case USING_DECL:
10051 return true;
10053 case AGGR_INIT_EXPR:
10054 case CALL_EXPR:
10055 /* -- an invocation of a function other than a constexpr function
10056 or a constexpr constructor. */
10058 tree fun = get_function_named_in_call (t);
10059 const int nargs = call_expr_nargs (t);
10060 i = 0;
10062 if (is_overloaded_fn (fun))
10064 if (TREE_CODE (fun) == FUNCTION_DECL)
10066 if (builtin_valid_in_constant_expr_p (fun))
10067 return true;
10068 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10069 /* Allow any built-in function; if the expansion
10070 isn't constant, we'll deal with that then. */
10071 && !is_builtin_fn (fun))
10073 if (flags & tf_error)
10075 error_at (EXPR_LOC_OR_LOC (t, input_location),
10076 "call to non-constexpr function %qD", fun);
10077 explain_invalid_constexpr_fn (fun);
10079 return false;
10081 /* A call to a non-static member function takes the address
10082 of the object as the first argument. But in a constant
10083 expression the address will be folded away, so look
10084 through it now. */
10085 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10086 && !DECL_CONSTRUCTOR_P (fun))
10088 tree x = get_nth_callarg (t, 0);
10089 if (is_this_parameter (x))
10091 if (DECL_CONTEXT (x) == NULL_TREE
10092 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10094 if (flags & tf_error)
10095 sorry ("calling a member function of the "
10096 "object being constructed in a constant "
10097 "expression");
10098 return false;
10100 /* Otherwise OK. */;
10102 else if (!potential_constant_expression_1 (x, rval, flags))
10103 return false;
10104 i = 1;
10107 else
10109 if (!potential_constant_expression_1 (fun, true, flags))
10110 return false;
10111 fun = get_first_fn (fun);
10113 /* Skip initial arguments to base constructors. */
10114 if (DECL_BASE_CONSTRUCTOR_P (fun))
10115 i = num_artificial_parms_for (fun);
10116 fun = DECL_ORIGIN (fun);
10118 else
10120 if (potential_constant_expression_1 (fun, rval, flags))
10121 /* Might end up being a constant function pointer. */;
10122 else
10123 return false;
10125 for (; i < nargs; ++i)
10127 tree x = get_nth_callarg (t, i);
10128 if (!potential_constant_expression_1 (x, rval, flags))
10129 return false;
10131 return true;
10134 case NON_LVALUE_EXPR:
10135 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10136 -- an lvalue of integral type that refers to a non-volatile
10137 const variable or static data member initialized with
10138 constant expressions, or
10140 -- an lvalue of literal type that refers to non-volatile
10141 object defined with constexpr, or that refers to a
10142 sub-object of such an object; */
10143 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10145 case VAR_DECL:
10146 if (want_rval && !decl_constant_var_p (t)
10147 && !dependent_type_p (TREE_TYPE (t)))
10149 if (flags & tf_error)
10150 non_const_var_error (t);
10151 return false;
10153 return true;
10155 case NOP_EXPR:
10156 case CONVERT_EXPR:
10157 case VIEW_CONVERT_EXPR:
10158 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10159 may change to something more specific to type-punning (DR 1312). */
10161 tree from = TREE_OPERAND (t, 0);
10162 if (POINTER_TYPE_P (TREE_TYPE (t))
10163 && TREE_CODE (from) == INTEGER_CST
10164 && !integer_zerop (from))
10166 if (flags & tf_error)
10167 error_at (EXPR_LOC_OR_LOC (t, input_location),
10168 "reinterpret_cast from integer to pointer");
10169 return false;
10171 return (potential_constant_expression_1
10172 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10175 case ADDR_EXPR:
10176 /* -- a unary operator & that is applied to an lvalue that
10177 designates an object with thread or automatic storage
10178 duration; */
10179 t = TREE_OPERAND (t, 0);
10180 #if 0
10181 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10182 any checking here, as we might dereference the pointer later. If
10183 we remove this code, also remove check_automatic_or_tls. */
10184 i = check_automatic_or_tls (t);
10185 if (i == ck_ok)
10186 return true;
10187 if (i == ck_bad)
10189 if (flags & tf_error)
10190 error ("address-of an object %qE with thread local or "
10191 "automatic storage is not a constant expression", t);
10192 return false;
10194 #endif
10195 return potential_constant_expression_1 (t, any, flags);
10197 case COMPONENT_REF:
10198 case BIT_FIELD_REF:
10199 case ARROW_EXPR:
10200 case OFFSET_REF:
10201 /* -- a class member access unless its postfix-expression is
10202 of literal type or of pointer to literal type. */
10203 /* This test would be redundant, as it follows from the
10204 postfix-expression being a potential constant expression. */
10205 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10206 want_rval, flags);
10208 case EXPR_PACK_EXPANSION:
10209 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10210 want_rval, flags);
10212 case INDIRECT_REF:
10214 tree x = TREE_OPERAND (t, 0);
10215 STRIP_NOPS (x);
10216 if (is_this_parameter (x))
10218 if (DECL_CONTEXT (x)
10219 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10221 if (flags & tf_error)
10222 error ("use of %<this%> in a constant expression");
10223 return false;
10225 if (want_rval && DECL_CONTEXT (x)
10226 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10228 if (flags & tf_error)
10229 sorry ("use of the value of the object being constructed "
10230 "in a constant expression");
10231 return false;
10233 return true;
10235 return potential_constant_expression_1 (x, rval, flags);
10238 case LAMBDA_EXPR:
10239 case DYNAMIC_CAST_EXPR:
10240 case PSEUDO_DTOR_EXPR:
10241 case PREINCREMENT_EXPR:
10242 case POSTINCREMENT_EXPR:
10243 case PREDECREMENT_EXPR:
10244 case POSTDECREMENT_EXPR:
10245 case NEW_EXPR:
10246 case VEC_NEW_EXPR:
10247 case DELETE_EXPR:
10248 case VEC_DELETE_EXPR:
10249 case THROW_EXPR:
10250 case MODIFY_EXPR:
10251 case MODOP_EXPR:
10252 case OMP_ATOMIC:
10253 case OMP_ATOMIC_READ:
10254 case OMP_ATOMIC_CAPTURE_OLD:
10255 case OMP_ATOMIC_CAPTURE_NEW:
10256 /* GCC internal stuff. */
10257 case VA_ARG_EXPR:
10258 case OBJ_TYPE_REF:
10259 case WITH_CLEANUP_EXPR:
10260 case CLEANUP_POINT_EXPR:
10261 case MUST_NOT_THROW_EXPR:
10262 case TRY_CATCH_EXPR:
10263 case STATEMENT_LIST:
10264 /* Don't bother trying to define a subset of statement-expressions to
10265 be constant-expressions, at least for now. */
10266 case STMT_EXPR:
10267 case EXPR_STMT:
10268 case BIND_EXPR:
10269 case TRANSACTION_EXPR:
10270 case IF_STMT:
10271 case DO_STMT:
10272 case FOR_STMT:
10273 case WHILE_STMT:
10274 case DECL_EXPR:
10275 if (flags & tf_error)
10276 error ("expression %qE is not a constant-expression", t);
10277 return false;
10279 case TYPEID_EXPR:
10280 /* -- a typeid expression whose operand is of polymorphic
10281 class type; */
10283 tree e = TREE_OPERAND (t, 0);
10284 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10285 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10287 if (flags & tf_error)
10288 error ("typeid-expression is not a constant expression "
10289 "because %qE is of polymorphic type", e);
10290 return false;
10292 return true;
10295 case MINUS_EXPR:
10296 /* -- a subtraction where both operands are pointers. */
10297 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10298 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10300 if (flags & tf_error)
10301 error ("difference of two pointer expressions is not "
10302 "a constant expression");
10303 return false;
10305 want_rval = true;
10306 goto binary;
10308 case LT_EXPR:
10309 case LE_EXPR:
10310 case GT_EXPR:
10311 case GE_EXPR:
10312 case EQ_EXPR:
10313 case NE_EXPR:
10314 /* -- a relational or equality operator where at least
10315 one of the operands is a pointer. */
10316 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10317 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10319 if (flags & tf_error)
10320 error ("pointer comparison expression is not a "
10321 "constant expression");
10322 return false;
10324 want_rval = true;
10325 goto binary;
10327 case BIT_NOT_EXPR:
10328 /* A destructor. */
10329 if (TYPE_P (TREE_OPERAND (t, 0)))
10330 return true;
10331 /* else fall through. */
10333 case REALPART_EXPR:
10334 case IMAGPART_EXPR:
10335 case CONJ_EXPR:
10336 case SAVE_EXPR:
10337 case FIX_TRUNC_EXPR:
10338 case FLOAT_EXPR:
10339 case NEGATE_EXPR:
10340 case ABS_EXPR:
10341 case TRUTH_NOT_EXPR:
10342 case FIXED_CONVERT_EXPR:
10343 case UNARY_PLUS_EXPR:
10344 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10345 flags);
10347 case CAST_EXPR:
10348 case CONST_CAST_EXPR:
10349 case STATIC_CAST_EXPR:
10350 case REINTERPRET_CAST_EXPR:
10351 case IMPLICIT_CONV_EXPR:
10352 if (cxx_dialect < cxx11
10353 && !dependent_type_p (TREE_TYPE (t))
10354 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10355 /* In C++98, a conversion to non-integral type can't be part of a
10356 constant expression. */
10358 if (flags & tf_error)
10359 error ("cast to non-integral type %qT in a constant expression",
10360 TREE_TYPE (t));
10361 return false;
10364 return (potential_constant_expression_1
10365 (TREE_OPERAND (t, 0),
10366 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10368 case PAREN_EXPR:
10369 case NON_DEPENDENT_EXPR:
10370 /* For convenience. */
10371 case RETURN_EXPR:
10372 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10373 want_rval, flags);
10375 case SCOPE_REF:
10376 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10377 want_rval, flags);
10379 case TARGET_EXPR:
10380 if (!literal_type_p (TREE_TYPE (t)))
10382 if (flags & tf_error)
10384 error ("temporary of non-literal type %qT in a "
10385 "constant expression", TREE_TYPE (t));
10386 explain_non_literal_class (TREE_TYPE (t));
10388 return false;
10390 case INIT_EXPR:
10391 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10392 rval, flags);
10394 case CONSTRUCTOR:
10396 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10397 constructor_elt *ce;
10398 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10399 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10400 return false;
10401 return true;
10404 case TREE_LIST:
10406 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10407 || DECL_P (TREE_PURPOSE (t)));
10408 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10409 flags))
10410 return false;
10411 if (TREE_CHAIN (t) == NULL_TREE)
10412 return true;
10413 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10414 flags);
10417 case TRUNC_DIV_EXPR:
10418 case CEIL_DIV_EXPR:
10419 case FLOOR_DIV_EXPR:
10420 case ROUND_DIV_EXPR:
10421 case TRUNC_MOD_EXPR:
10422 case CEIL_MOD_EXPR:
10423 case ROUND_MOD_EXPR:
10425 tree denom = TREE_OPERAND (t, 1);
10426 if (!potential_constant_expression_1 (denom, rval, flags))
10427 return false;
10428 /* We can't call cxx_eval_outermost_constant_expr on an expression
10429 that hasn't been through fold_non_dependent_expr yet. */
10430 if (!processing_template_decl)
10431 denom = cxx_eval_outermost_constant_expr (denom, true);
10432 if (integer_zerop (denom))
10434 if (flags & tf_error)
10435 error ("division by zero is not a constant-expression");
10436 return false;
10438 else
10440 want_rval = true;
10441 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10442 want_rval, flags);
10446 case COMPOUND_EXPR:
10448 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10449 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10450 introduced by build_call_a. */
10451 tree op0 = TREE_OPERAND (t, 0);
10452 tree op1 = TREE_OPERAND (t, 1);
10453 STRIP_NOPS (op1);
10454 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10455 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10456 return potential_constant_expression_1 (op0, want_rval, flags);
10457 else
10458 goto binary;
10461 /* If the first operand is the non-short-circuit constant, look at
10462 the second operand; otherwise we only care about the first one for
10463 potentiality. */
10464 case TRUTH_AND_EXPR:
10465 case TRUTH_ANDIF_EXPR:
10466 tmp = boolean_true_node;
10467 goto truth;
10468 case TRUTH_OR_EXPR:
10469 case TRUTH_ORIF_EXPR:
10470 tmp = boolean_false_node;
10471 truth:
10473 tree op = TREE_OPERAND (t, 0);
10474 if (!potential_constant_expression_1 (op, rval, flags))
10475 return false;
10476 if (!processing_template_decl)
10477 op = cxx_eval_outermost_constant_expr (op, true);
10478 if (tree_int_cst_equal (op, tmp))
10479 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10480 else
10481 return true;
10484 case PLUS_EXPR:
10485 case MULT_EXPR:
10486 case POINTER_PLUS_EXPR:
10487 case RDIV_EXPR:
10488 case EXACT_DIV_EXPR:
10489 case MIN_EXPR:
10490 case MAX_EXPR:
10491 case LSHIFT_EXPR:
10492 case RSHIFT_EXPR:
10493 case LROTATE_EXPR:
10494 case RROTATE_EXPR:
10495 case BIT_IOR_EXPR:
10496 case BIT_XOR_EXPR:
10497 case BIT_AND_EXPR:
10498 case TRUTH_XOR_EXPR:
10499 case UNORDERED_EXPR:
10500 case ORDERED_EXPR:
10501 case UNLT_EXPR:
10502 case UNLE_EXPR:
10503 case UNGT_EXPR:
10504 case UNGE_EXPR:
10505 case UNEQ_EXPR:
10506 case LTGT_EXPR:
10507 case RANGE_EXPR:
10508 case COMPLEX_EXPR:
10509 want_rval = true;
10510 /* Fall through. */
10511 case ARRAY_REF:
10512 case ARRAY_RANGE_REF:
10513 case MEMBER_REF:
10514 case DOTSTAR_EXPR:
10515 binary:
10516 for (i = 0; i < 2; ++i)
10517 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10518 want_rval, flags))
10519 return false;
10520 return true;
10522 case CILK_SYNC_STMT:
10523 case CILK_SPAWN_STMT:
10524 case ARRAY_NOTATION_REF:
10525 return false;
10527 case FMA_EXPR:
10528 case VEC_PERM_EXPR:
10529 for (i = 0; i < 3; ++i)
10530 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10531 true, flags))
10532 return false;
10533 return true;
10535 case COND_EXPR:
10536 case VEC_COND_EXPR:
10537 /* If the condition is a known constant, we know which of the legs we
10538 care about; otherwise we only require that the condition and
10539 either of the legs be potentially constant. */
10540 tmp = TREE_OPERAND (t, 0);
10541 if (!potential_constant_expression_1 (tmp, rval, flags))
10542 return false;
10543 if (!processing_template_decl)
10544 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10545 if (integer_zerop (tmp))
10546 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10547 want_rval, flags);
10548 else if (TREE_CODE (tmp) == INTEGER_CST)
10549 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10550 want_rval, flags);
10551 for (i = 1; i < 3; ++i)
10552 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10553 want_rval, tf_none))
10554 return true;
10555 if (flags & tf_error)
10556 error ("expression %qE is not a constant-expression", t);
10557 return false;
10559 case VEC_INIT_EXPR:
10560 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10561 return true;
10562 if (flags & tf_error)
10564 error ("non-constant array initialization");
10565 diagnose_non_constexpr_vec_init (t);
10567 return false;
10569 default:
10570 if (objc_is_property_ref (t))
10571 return false;
10573 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10574 gcc_unreachable();
10575 return false;
10579 /* The main entry point to the above. */
10581 bool
10582 potential_constant_expression (tree t)
10584 return potential_constant_expression_1 (t, false, tf_none);
10587 /* As above, but require a constant rvalue. */
10589 bool
10590 potential_rvalue_constant_expression (tree t)
10592 return potential_constant_expression_1 (t, true, tf_none);
10595 /* Like above, but complain about non-constant expressions. */
10597 bool
10598 require_potential_constant_expression (tree t)
10600 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10603 /* Cross product of the above. */
10605 bool
10606 require_potential_rvalue_constant_expression (tree t)
10608 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10611 /* Insert the deduced return type for an auto function. */
10613 void
10614 apply_deduced_return_type (tree fco, tree return_type)
10616 tree result;
10618 if (return_type == error_mark_node)
10619 return;
10621 if (LAMBDA_FUNCTION_P (fco))
10623 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10624 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10627 if (DECL_CONV_FN_P (fco))
10628 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10630 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10632 result = DECL_RESULT (fco);
10633 if (result == NULL_TREE)
10634 return;
10635 if (TREE_TYPE (result) == return_type)
10636 return;
10638 /* We already have a DECL_RESULT from start_preparsed_function.
10639 Now we need to redo the work it and allocate_struct_function
10640 did to reflect the new type. */
10641 gcc_assert (current_function_decl == fco);
10642 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10643 TYPE_MAIN_VARIANT (return_type));
10644 DECL_ARTIFICIAL (result) = 1;
10645 DECL_IGNORED_P (result) = 1;
10646 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10647 result);
10649 DECL_RESULT (fco) = result;
10651 if (!processing_template_decl)
10653 bool aggr = aggregate_value_p (result, fco);
10654 #ifdef PCC_STATIC_STRUCT_RETURN
10655 cfun->returns_pcc_struct = aggr;
10656 #endif
10657 cfun->returns_struct = aggr;
10662 /* DECL is a local variable or parameter from the surrounding scope of a
10663 lambda-expression. Returns the decltype for a use of the capture field
10664 for DECL even if it hasn't been captured yet. */
10666 static tree
10667 capture_decltype (tree decl)
10669 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10670 /* FIXME do lookup instead of list walk? */
10671 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10672 tree type;
10674 if (cap)
10675 type = TREE_TYPE (TREE_PURPOSE (cap));
10676 else
10677 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10679 case CPLD_NONE:
10680 error ("%qD is not captured", decl);
10681 return error_mark_node;
10683 case CPLD_COPY:
10684 type = TREE_TYPE (decl);
10685 if (TREE_CODE (type) == REFERENCE_TYPE
10686 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10687 type = TREE_TYPE (type);
10688 break;
10690 case CPLD_REFERENCE:
10691 type = TREE_TYPE (decl);
10692 if (TREE_CODE (type) != REFERENCE_TYPE)
10693 type = build_reference_type (TREE_TYPE (decl));
10694 break;
10696 default:
10697 gcc_unreachable ();
10700 if (TREE_CODE (type) != REFERENCE_TYPE)
10702 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10703 type = cp_build_qualified_type (type, (cp_type_quals (type)
10704 |TYPE_QUAL_CONST));
10705 type = build_reference_type (type);
10707 return type;
10710 #include "gt-cp-semantics.h"