2014-01-16 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / semantics.c
blobeb04266aee81f10a76910c4506051c0ac4cc3b3a
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "pointer-set.h"
48 #include "hash-table.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "omp-low.h"
53 static bool verify_constant (tree, bool, bool *, bool *);
54 #define VERIFY_CONSTANT(X) \
55 do { \
56 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
57 return t; \
58 } while (0)
60 /* There routines provide a modular interface to perform many parsing
61 operations. They may therefore be used during actual parsing, or
62 during template instantiation, which may be regarded as a
63 degenerate form of parsing. */
65 static tree maybe_convert_cond (tree);
66 static tree finalize_nrv_r (tree *, int *, void *);
67 static tree capture_decltype (tree);
70 /* Deferred Access Checking Overview
71 ---------------------------------
73 Most C++ expressions and declarations require access checking
74 to be performed during parsing. However, in several cases,
75 this has to be treated differently.
77 For member declarations, access checking has to be deferred
78 until more information about the declaration is known. For
79 example:
81 class A {
82 typedef int X;
83 public:
84 X f();
87 A::X A::f();
88 A::X g();
90 When we are parsing the function return type `A::X', we don't
91 really know if this is allowed until we parse the function name.
93 Furthermore, some contexts require that access checking is
94 never performed at all. These include class heads, and template
95 instantiations.
97 Typical use of access checking functions is described here:
99 1. When we enter a context that requires certain access checking
100 mode, the function `push_deferring_access_checks' is called with
101 DEFERRING argument specifying the desired mode. Access checking
102 may be performed immediately (dk_no_deferred), deferred
103 (dk_deferred), or not performed (dk_no_check).
105 2. When a declaration such as a type, or a variable, is encountered,
106 the function `perform_or_defer_access_check' is called. It
107 maintains a vector of all deferred checks.
109 3. The global `current_class_type' or `current_function_decl' is then
110 setup by the parser. `enforce_access' relies on these information
111 to check access.
113 4. Upon exiting the context mentioned in step 1,
114 `perform_deferred_access_checks' is called to check all declaration
115 stored in the vector. `pop_deferring_access_checks' is then
116 called to restore the previous access checking mode.
118 In case of parsing error, we simply call `pop_deferring_access_checks'
119 without `perform_deferred_access_checks'. */
121 typedef struct GTY(()) deferred_access {
122 /* A vector representing name-lookups for which we have deferred
123 checking access controls. We cannot check the accessibility of
124 names used in a decl-specifier-seq until we know what is being
125 declared because code like:
127 class A {
128 class B {};
129 B* f();
132 A::B* A::f() { return 0; }
134 is valid, even though `A::B' is not generally accessible. */
135 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
137 /* The current mode of access checks. */
138 enum deferring_kind deferring_access_checks_kind;
140 } deferred_access;
142 /* Data for deferred access checking. */
143 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
144 static GTY(()) unsigned deferred_access_no_check;
146 /* Save the current deferred access states and start deferred
147 access checking iff DEFER_P is true. */
149 void
150 push_deferring_access_checks (deferring_kind deferring)
152 /* For context like template instantiation, access checking
153 disabling applies to all nested context. */
154 if (deferred_access_no_check || deferring == dk_no_check)
155 deferred_access_no_check++;
156 else
158 deferred_access e = {NULL, deferring};
159 vec_safe_push (deferred_access_stack, e);
163 /* Save the current deferred access states and start deferred access
164 checking, continuing the set of deferred checks in CHECKS. */
166 void
167 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
169 push_deferring_access_checks (dk_deferred);
170 if (!deferred_access_no_check)
171 deferred_access_stack->last().deferred_access_checks = checks;
174 /* Resume deferring access checks again after we stopped doing
175 this previously. */
177 void
178 resume_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
184 /* Stop deferring access checks. */
186 void
187 stop_deferring_access_checks (void)
189 if (!deferred_access_no_check)
190 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
193 /* Discard the current deferred access checks and restore the
194 previous states. */
196 void
197 pop_deferring_access_checks (void)
199 if (deferred_access_no_check)
200 deferred_access_no_check--;
201 else
202 deferred_access_stack->pop ();
205 /* Returns a TREE_LIST representing the deferred checks.
206 The TREE_PURPOSE of each node is the type through which the
207 access occurred; the TREE_VALUE is the declaration named.
210 vec<deferred_access_check, va_gc> *
211 get_deferred_access_checks (void)
213 if (deferred_access_no_check)
214 return NULL;
215 else
216 return (deferred_access_stack->last().deferred_access_checks);
219 /* Take current deferred checks and combine with the
220 previous states if we also defer checks previously.
221 Otherwise perform checks now. */
223 void
224 pop_to_parent_deferring_access_checks (void)
226 if (deferred_access_no_check)
227 deferred_access_no_check--;
228 else
230 vec<deferred_access_check, va_gc> *checks;
231 deferred_access *ptr;
233 checks = (deferred_access_stack->last ().deferred_access_checks);
235 deferred_access_stack->pop ();
236 ptr = &deferred_access_stack->last ();
237 if (ptr->deferring_access_checks_kind == dk_no_deferred)
239 /* Check access. */
240 perform_access_checks (checks, tf_warning_or_error);
242 else
244 /* Merge with parent. */
245 int i, j;
246 deferred_access_check *chk, *probe;
248 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
250 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
252 if (probe->binfo == chk->binfo &&
253 probe->decl == chk->decl &&
254 probe->diag_decl == chk->diag_decl)
255 goto found;
257 /* Insert into parent's checks. */
258 vec_safe_push (ptr->deferred_access_checks, *chk);
259 found:;
265 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
266 is the BINFO indicating the qualifying scope used to access the
267 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
268 or we aren't in SFINAE context or all the checks succeed return TRUE,
269 otherwise FALSE. */
271 bool
272 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
273 tsubst_flags_t complain)
275 int i;
276 deferred_access_check *chk;
277 location_t loc = input_location;
278 bool ok = true;
280 if (!checks)
281 return true;
283 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
285 input_location = chk->loc;
286 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
289 input_location = loc;
290 return (complain & tf_error) ? true : ok;
293 /* Perform the deferred access checks.
295 After performing the checks, we still have to keep the list
296 `deferred_access_stack->deferred_access_checks' since we may want
297 to check access for them again later in a different context.
298 For example:
300 class A {
301 typedef int X;
302 static X a;
304 A::X A::a, x; // No error for `A::a', error for `x'
306 We have to perform deferred access of `A::X', first with `A::a',
307 next with `x'. Return value like perform_access_checks above. */
309 bool
310 perform_deferred_access_checks (tsubst_flags_t complain)
312 return perform_access_checks (get_deferred_access_checks (), complain);
315 /* Defer checking the accessibility of DECL, when looked up in
316 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
317 Return value like perform_access_checks above. */
319 bool
320 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
321 tsubst_flags_t complain)
323 int i;
324 deferred_access *ptr;
325 deferred_access_check *chk;
328 /* Exit if we are in a context that no access checking is performed.
330 if (deferred_access_no_check)
331 return true;
333 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
335 ptr = &deferred_access_stack->last ();
337 /* If we are not supposed to defer access checks, just check now. */
338 if (ptr->deferring_access_checks_kind == dk_no_deferred)
340 bool ok = enforce_access (binfo, decl, diag_decl, complain);
341 return (complain & tf_error) ? true : ok;
344 /* See if we are already going to perform this check. */
345 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
347 if (chk->decl == decl && chk->binfo == binfo &&
348 chk->diag_decl == diag_decl)
350 return true;
353 /* If not, record the check. */
354 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
355 vec_safe_push (ptr->deferred_access_checks, new_access);
357 return true;
360 /* Returns nonzero if the current statement is a full expression,
361 i.e. temporaries created during that statement should be destroyed
362 at the end of the statement. */
365 stmts_are_full_exprs_p (void)
367 return current_stmt_tree ()->stmts_are_full_exprs_p;
370 /* T is a statement. Add it to the statement-tree. This is the C++
371 version. The C/ObjC frontends have a slightly different version of
372 this function. */
374 tree
375 add_stmt (tree t)
377 enum tree_code code = TREE_CODE (t);
379 if (EXPR_P (t) && code != LABEL_EXPR)
381 if (!EXPR_HAS_LOCATION (t))
382 SET_EXPR_LOCATION (t, input_location);
384 /* When we expand a statement-tree, we must know whether or not the
385 statements are full-expressions. We record that fact here. */
386 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
394 return t;
397 /* Returns the stmt_tree to which statements are currently being added. */
399 stmt_tree
400 current_stmt_tree (void)
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
409 static tree
410 maybe_cleanup_point_expr (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
432 /* Create a declaration statement for the declaration given by the DECL. */
434 void
435 add_decl_expr (tree decl)
437 tree r = build_stmt (input_location, DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
444 /* Finish a scope. */
446 tree
447 do_poplevel (tree stmt_list)
449 tree block = NULL;
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
454 stmt_list = pop_stmt_list (stmt_list);
456 if (!processing_template_decl)
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
462 return stmt_list;
465 /* Begin a new scope. */
467 static tree
468 do_pushlevel (scope_kind sk)
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
489 /* Begin a conditional that might contain a declaration. When generating
490 normal code, we want the declaration to appear before the statement
491 containing the conditional. When generating template code, we want the
492 conditional to be rendered as the raw DECL_EXPR. */
494 static void
495 begin_cond (tree *cond_p)
497 if (processing_template_decl)
498 *cond_p = push_stmt_list ();
501 /* Finish such a conditional. */
503 static void
504 finish_cond (tree *cond_p, tree expr)
506 if (processing_template_decl)
508 tree cond = pop_stmt_list (*cond_p);
510 if (expr == NULL_TREE)
511 /* Empty condition in 'for'. */
512 gcc_assert (empty_expr_stmt_p (cond));
513 else if (check_for_bare_parameter_packs (expr))
514 expr = error_mark_node;
515 else if (!empty_expr_stmt_p (cond))
516 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
518 *cond_p = expr;
521 /* If *COND_P specifies a conditional with a declaration, transform the
522 loop such that
523 while (A x = 42) { }
524 for (; A x = 42;) { }
525 becomes
526 while (true) { A x = 42; if (!x) break; }
527 for (;;) { A x = 42; if (!x) break; }
528 The statement list for BODY will be empty if the conditional did
529 not declare anything. */
531 static void
532 simplify_loop_decl_cond (tree *cond_p, tree body)
534 tree cond, if_stmt;
536 if (!TREE_SIDE_EFFECTS (body))
537 return;
539 cond = *cond_p;
540 *cond_p = boolean_true_node;
542 if_stmt = begin_if_stmt ();
543 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
544 finish_if_stmt_cond (cond, if_stmt);
545 finish_break_stmt ();
546 finish_then_clause (if_stmt);
547 finish_if_stmt (if_stmt);
550 /* Finish a goto-statement. */
552 tree
553 finish_goto_stmt (tree destination)
555 if (identifier_p (destination))
556 destination = lookup_label (destination);
558 /* We warn about unused labels with -Wunused. That means we have to
559 mark the used labels as used. */
560 if (TREE_CODE (destination) == LABEL_DECL)
561 TREE_USED (destination) = 1;
562 else
564 destination = mark_rvalue_use (destination);
565 if (!processing_template_decl)
567 destination = cp_convert (ptr_type_node, destination,
568 tf_warning_or_error);
569 if (error_operand_p (destination))
570 return NULL_TREE;
571 destination
572 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
573 destination);
577 check_goto (destination);
579 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
582 /* COND is the condition-expression for an if, while, etc.,
583 statement. Convert it to a boolean value, if appropriate.
584 In addition, verify sequence points if -Wsequence-point is enabled. */
586 static tree
587 maybe_convert_cond (tree cond)
589 /* Empty conditions remain empty. */
590 if (!cond)
591 return NULL_TREE;
593 /* Wait until we instantiate templates before doing conversion. */
594 if (processing_template_decl)
595 return cond;
597 if (warn_sequence_point)
598 verify_sequence_points (cond);
600 /* Do the conversion. */
601 cond = convert_from_reference (cond);
603 if (TREE_CODE (cond) == MODIFY_EXPR
604 && !TREE_NO_WARNING (cond)
605 && warn_parentheses)
607 warning (OPT_Wparentheses,
608 "suggest parentheses around assignment used as truth value");
609 TREE_NO_WARNING (cond) = 1;
612 return condition_conversion (cond);
615 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
617 tree
618 finish_expr_stmt (tree expr)
620 tree r = NULL_TREE;
622 if (expr != NULL_TREE)
624 if (!processing_template_decl)
626 if (warn_sequence_point)
627 verify_sequence_points (expr);
628 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
630 else if (!type_dependent_expression_p (expr))
631 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
632 tf_warning_or_error);
634 if (check_for_bare_parameter_packs (expr))
635 expr = error_mark_node;
637 /* Simplification of inner statement expressions, compound exprs,
638 etc can result in us already having an EXPR_STMT. */
639 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
641 if (TREE_CODE (expr) != EXPR_STMT)
642 expr = build_stmt (input_location, EXPR_STMT, expr);
643 expr = maybe_cleanup_point_expr_void (expr);
646 r = add_stmt (expr);
649 return r;
653 /* Begin an if-statement. Returns a newly created IF_STMT if
654 appropriate. */
656 tree
657 begin_if_stmt (void)
659 tree r, scope;
660 scope = do_pushlevel (sk_cond);
661 r = build_stmt (input_location, IF_STMT, NULL_TREE,
662 NULL_TREE, NULL_TREE, scope);
663 begin_cond (&IF_COND (r));
664 return r;
667 /* Process the COND of an if-statement, which may be given by
668 IF_STMT. */
670 void
671 finish_if_stmt_cond (tree cond, tree if_stmt)
673 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
674 add_stmt (if_stmt);
675 THEN_CLAUSE (if_stmt) = push_stmt_list ();
678 /* Finish the then-clause of an if-statement, which may be given by
679 IF_STMT. */
681 tree
682 finish_then_clause (tree if_stmt)
684 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
685 return if_stmt;
688 /* Begin the else-clause of an if-statement. */
690 void
691 begin_else_clause (tree if_stmt)
693 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
696 /* Finish the else-clause of an if-statement, which may be given by
697 IF_STMT. */
699 void
700 finish_else_clause (tree if_stmt)
702 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
705 /* Finish an if-statement. */
707 void
708 finish_if_stmt (tree if_stmt)
710 tree scope = IF_SCOPE (if_stmt);
711 IF_SCOPE (if_stmt) = NULL;
712 add_stmt (do_poplevel (scope));
715 /* Begin a while-statement. Returns a newly created WHILE_STMT if
716 appropriate. */
718 tree
719 begin_while_stmt (void)
721 tree r;
722 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
723 add_stmt (r);
724 WHILE_BODY (r) = do_pushlevel (sk_block);
725 begin_cond (&WHILE_COND (r));
726 return r;
729 /* Process the COND of a while-statement, which may be given by
730 WHILE_STMT. */
732 void
733 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
735 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
736 if (ivdep && cond != error_mark_node)
737 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
738 TREE_TYPE (WHILE_COND (while_stmt)),
739 WHILE_COND (while_stmt),
740 build_int_cst (integer_type_node,
741 annot_expr_ivdep_kind));
742 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
745 /* Finish a while-statement, which may be given by WHILE_STMT. */
747 void
748 finish_while_stmt (tree while_stmt)
750 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
753 /* Begin a do-statement. Returns a newly created DO_STMT if
754 appropriate. */
756 tree
757 begin_do_stmt (void)
759 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
760 add_stmt (r);
761 DO_BODY (r) = push_stmt_list ();
762 return r;
765 /* Finish the body of a do-statement, which may be given by DO_STMT. */
767 void
768 finish_do_body (tree do_stmt)
770 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
772 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
773 body = STATEMENT_LIST_TAIL (body)->stmt;
775 if (IS_EMPTY_STMT (body))
776 warning (OPT_Wempty_body,
777 "suggest explicit braces around empty body in %<do%> statement");
780 /* Finish a do-statement, which may be given by DO_STMT, and whose
781 COND is as indicated. */
783 void
784 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
786 cond = maybe_convert_cond (cond);
787 if (ivdep && cond != error_mark_node)
788 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
789 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
790 DO_COND (do_stmt) = cond;
793 /* Finish a return-statement. The EXPRESSION returned, if any, is as
794 indicated. */
796 tree
797 finish_return_stmt (tree expr)
799 tree r;
800 bool no_warning;
802 expr = check_return_expr (expr, &no_warning);
804 if (error_operand_p (expr)
805 || (flag_openmp && !check_omp_return ()))
806 return error_mark_node;
807 if (!processing_template_decl)
809 if (warn_sequence_point)
810 verify_sequence_points (expr);
812 if (DECL_DESTRUCTOR_P (current_function_decl)
813 || (DECL_CONSTRUCTOR_P (current_function_decl)
814 && targetm.cxx.cdtor_returns_this ()))
816 /* Similarly, all destructors must run destructors for
817 base-classes before returning. So, all returns in a
818 destructor get sent to the DTOR_LABEL; finish_function emits
819 code to return a value there. */
820 return finish_goto_stmt (cdtor_label);
824 r = build_stmt (input_location, RETURN_EXPR, expr);
825 TREE_NO_WARNING (r) |= no_warning;
826 r = maybe_cleanup_point_expr_void (r);
827 r = add_stmt (r);
829 return r;
832 /* Begin the scope of a for-statement or a range-for-statement.
833 Both the returned trees are to be used in a call to
834 begin_for_stmt or begin_range_for_stmt. */
836 tree
837 begin_for_scope (tree *init)
839 tree scope = NULL_TREE;
840 if (flag_new_for_scope > 0)
841 scope = do_pushlevel (sk_for);
843 if (processing_template_decl)
844 *init = push_stmt_list ();
845 else
846 *init = NULL_TREE;
848 return scope;
851 /* Begin a for-statement. Returns a new FOR_STMT.
852 SCOPE and INIT should be the return of begin_for_scope,
853 or both NULL_TREE */
855 tree
856 begin_for_stmt (tree scope, tree init)
858 tree r;
860 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
861 NULL_TREE, NULL_TREE, NULL_TREE);
863 if (scope == NULL_TREE)
865 gcc_assert (!init || !(flag_new_for_scope > 0));
866 if (!init)
867 scope = begin_for_scope (&init);
869 FOR_INIT_STMT (r) = init;
870 FOR_SCOPE (r) = scope;
872 return r;
875 /* Finish the for-init-statement of a for-statement, which may be
876 given by FOR_STMT. */
878 void
879 finish_for_init_stmt (tree for_stmt)
881 if (processing_template_decl)
882 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
883 add_stmt (for_stmt);
884 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
885 begin_cond (&FOR_COND (for_stmt));
888 /* Finish the COND of a for-statement, which may be given by
889 FOR_STMT. */
891 void
892 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
894 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
895 if (ivdep && cond != error_mark_node)
896 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
897 TREE_TYPE (FOR_COND (for_stmt)),
898 FOR_COND (for_stmt),
899 build_int_cst (integer_type_node,
900 annot_expr_ivdep_kind));
901 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
904 /* Finish the increment-EXPRESSION in a for-statement, which may be
905 given by FOR_STMT. */
907 void
908 finish_for_expr (tree expr, tree for_stmt)
910 if (!expr)
911 return;
912 /* If EXPR is an overloaded function, issue an error; there is no
913 context available to use to perform overload resolution. */
914 if (type_unknown_p (expr))
916 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
917 expr = error_mark_node;
919 if (!processing_template_decl)
921 if (warn_sequence_point)
922 verify_sequence_points (expr);
923 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
924 tf_warning_or_error);
926 else if (!type_dependent_expression_p (expr))
927 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
928 tf_warning_or_error);
929 expr = maybe_cleanup_point_expr_void (expr);
930 if (check_for_bare_parameter_packs (expr))
931 expr = error_mark_node;
932 FOR_EXPR (for_stmt) = expr;
935 /* Finish the body of a for-statement, which may be given by
936 FOR_STMT. The increment-EXPR for the loop must be
937 provided.
938 It can also finish RANGE_FOR_STMT. */
940 void
941 finish_for_stmt (tree for_stmt)
943 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
944 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
945 else
946 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
948 /* Pop the scope for the body of the loop. */
949 if (flag_new_for_scope > 0)
951 tree scope;
952 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
953 ? &RANGE_FOR_SCOPE (for_stmt)
954 : &FOR_SCOPE (for_stmt));
955 scope = *scope_ptr;
956 *scope_ptr = NULL;
957 add_stmt (do_poplevel (scope));
961 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
962 SCOPE and INIT should be the return of begin_for_scope,
963 or both NULL_TREE .
964 To finish it call finish_for_stmt(). */
966 tree
967 begin_range_for_stmt (tree scope, tree init)
969 tree r;
971 r = build_stmt (input_location, RANGE_FOR_STMT,
972 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
974 if (scope == NULL_TREE)
976 gcc_assert (!init || !(flag_new_for_scope > 0));
977 if (!init)
978 scope = begin_for_scope (&init);
981 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
982 pop it now. */
983 if (init)
984 pop_stmt_list (init);
985 RANGE_FOR_SCOPE (r) = scope;
987 return r;
990 /* Finish the head of a range-based for statement, which may
991 be given by RANGE_FOR_STMT. DECL must be the declaration
992 and EXPR must be the loop expression. */
994 void
995 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
997 RANGE_FOR_DECL (range_for_stmt) = decl;
998 RANGE_FOR_EXPR (range_for_stmt) = expr;
999 add_stmt (range_for_stmt);
1000 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1003 /* Finish a break-statement. */
1005 tree
1006 finish_break_stmt (void)
1008 /* In switch statements break is sometimes stylistically used after
1009 a return statement. This can lead to spurious warnings about
1010 control reaching the end of a non-void function when it is
1011 inlined. Note that we are calling block_may_fallthru with
1012 language specific tree nodes; this works because
1013 block_may_fallthru returns true when given something it does not
1014 understand. */
1015 if (!block_may_fallthru (cur_stmt_list))
1016 return void_zero_node;
1017 return add_stmt (build_stmt (input_location, BREAK_STMT));
1020 /* Finish a continue-statement. */
1022 tree
1023 finish_continue_stmt (void)
1025 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1028 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1029 appropriate. */
1031 tree
1032 begin_switch_stmt (void)
1034 tree r, scope;
1036 scope = do_pushlevel (sk_cond);
1037 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1039 begin_cond (&SWITCH_STMT_COND (r));
1041 return r;
1044 /* Finish the cond of a switch-statement. */
1046 void
1047 finish_switch_cond (tree cond, tree switch_stmt)
1049 tree orig_type = NULL;
1050 if (!processing_template_decl)
1052 /* Convert the condition to an integer or enumeration type. */
1053 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1054 if (cond == NULL_TREE)
1056 error ("switch quantity not an integer");
1057 cond = error_mark_node;
1059 orig_type = TREE_TYPE (cond);
1060 if (cond != error_mark_node)
1062 /* [stmt.switch]
1064 Integral promotions are performed. */
1065 cond = perform_integral_promotions (cond);
1066 cond = maybe_cleanup_point_expr (cond);
1069 if (check_for_bare_parameter_packs (cond))
1070 cond = error_mark_node;
1071 else if (!processing_template_decl && warn_sequence_point)
1072 verify_sequence_points (cond);
1074 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1075 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1076 add_stmt (switch_stmt);
1077 push_switch (switch_stmt);
1078 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1081 /* Finish the body of a switch-statement, which may be given by
1082 SWITCH_STMT. The COND to switch on is indicated. */
1084 void
1085 finish_switch_stmt (tree switch_stmt)
1087 tree scope;
1089 SWITCH_STMT_BODY (switch_stmt) =
1090 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1091 pop_switch ();
1093 scope = SWITCH_STMT_SCOPE (switch_stmt);
1094 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1095 add_stmt (do_poplevel (scope));
1098 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1099 appropriate. */
1101 tree
1102 begin_try_block (void)
1104 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1105 add_stmt (r);
1106 TRY_STMTS (r) = push_stmt_list ();
1107 return r;
1110 /* Likewise, for a function-try-block. The block returned in
1111 *COMPOUND_STMT is an artificial outer scope, containing the
1112 function-try-block. */
1114 tree
1115 begin_function_try_block (tree *compound_stmt)
1117 tree r;
1118 /* This outer scope does not exist in the C++ standard, but we need
1119 a place to put __FUNCTION__ and similar variables. */
1120 *compound_stmt = begin_compound_stmt (0);
1121 r = begin_try_block ();
1122 FN_TRY_BLOCK_P (r) = 1;
1123 return r;
1126 /* Finish a try-block, which may be given by TRY_BLOCK. */
1128 void
1129 finish_try_block (tree try_block)
1131 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1132 TRY_HANDLERS (try_block) = push_stmt_list ();
1135 /* Finish the body of a cleanup try-block, which may be given by
1136 TRY_BLOCK. */
1138 void
1139 finish_cleanup_try_block (tree try_block)
1141 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1144 /* Finish an implicitly generated try-block, with a cleanup is given
1145 by CLEANUP. */
1147 void
1148 finish_cleanup (tree cleanup, tree try_block)
1150 TRY_HANDLERS (try_block) = cleanup;
1151 CLEANUP_P (try_block) = 1;
1154 /* Likewise, for a function-try-block. */
1156 void
1157 finish_function_try_block (tree try_block)
1159 finish_try_block (try_block);
1160 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1161 the try block, but moving it inside. */
1162 in_function_try_handler = 1;
1165 /* Finish a handler-sequence for a try-block, which may be given by
1166 TRY_BLOCK. */
1168 void
1169 finish_handler_sequence (tree try_block)
1171 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1172 check_handlers (TRY_HANDLERS (try_block));
1175 /* Finish the handler-seq for a function-try-block, given by
1176 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1177 begin_function_try_block. */
1179 void
1180 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1182 in_function_try_handler = 0;
1183 finish_handler_sequence (try_block);
1184 finish_compound_stmt (compound_stmt);
1187 /* Begin a handler. Returns a HANDLER if appropriate. */
1189 tree
1190 begin_handler (void)
1192 tree r;
1194 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1195 add_stmt (r);
1197 /* Create a binding level for the eh_info and the exception object
1198 cleanup. */
1199 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1201 return r;
1204 /* Finish the handler-parameters for a handler, which may be given by
1205 HANDLER. DECL is the declaration for the catch parameter, or NULL
1206 if this is a `catch (...)' clause. */
1208 void
1209 finish_handler_parms (tree decl, tree handler)
1211 tree type = NULL_TREE;
1212 if (processing_template_decl)
1214 if (decl)
1216 decl = pushdecl (decl);
1217 decl = push_template_decl (decl);
1218 HANDLER_PARMS (handler) = decl;
1219 type = TREE_TYPE (decl);
1222 else
1223 type = expand_start_catch_block (decl);
1224 HANDLER_TYPE (handler) = type;
1227 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1228 the return value from the matching call to finish_handler_parms. */
1230 void
1231 finish_handler (tree handler)
1233 if (!processing_template_decl)
1234 expand_end_catch_block ();
1235 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1238 /* Begin a compound statement. FLAGS contains some bits that control the
1239 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1240 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1241 block of a function. If BCS_TRY_BLOCK is set, this is the block
1242 created on behalf of a TRY statement. Returns a token to be passed to
1243 finish_compound_stmt. */
1245 tree
1246 begin_compound_stmt (unsigned int flags)
1248 tree r;
1250 if (flags & BCS_NO_SCOPE)
1252 r = push_stmt_list ();
1253 STATEMENT_LIST_NO_SCOPE (r) = 1;
1255 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1256 But, if it's a statement-expression with a scopeless block, there's
1257 nothing to keep, and we don't want to accidentally keep a block
1258 *inside* the scopeless block. */
1259 keep_next_level (false);
1261 else
1262 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1264 /* When processing a template, we need to remember where the braces were,
1265 so that we can set up identical scopes when instantiating the template
1266 later. BIND_EXPR is a handy candidate for this.
1267 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1268 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1269 processing templates. */
1270 if (processing_template_decl)
1272 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1273 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1274 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1275 TREE_SIDE_EFFECTS (r) = 1;
1278 return r;
1281 /* Finish a compound-statement, which is given by STMT. */
1283 void
1284 finish_compound_stmt (tree stmt)
1286 if (TREE_CODE (stmt) == BIND_EXPR)
1288 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1289 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1290 discard the BIND_EXPR so it can be merged with the containing
1291 STATEMENT_LIST. */
1292 if (TREE_CODE (body) == STATEMENT_LIST
1293 && STATEMENT_LIST_HEAD (body) == NULL
1294 && !BIND_EXPR_BODY_BLOCK (stmt)
1295 && !BIND_EXPR_TRY_BLOCK (stmt))
1296 stmt = body;
1297 else
1298 BIND_EXPR_BODY (stmt) = body;
1300 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1301 stmt = pop_stmt_list (stmt);
1302 else
1304 /* Destroy any ObjC "super" receivers that may have been
1305 created. */
1306 objc_clear_super_receiver ();
1308 stmt = do_poplevel (stmt);
1311 /* ??? See c_end_compound_stmt wrt statement expressions. */
1312 add_stmt (stmt);
1315 /* Finish an asm-statement, whose components are a STRING, some
1316 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1317 LABELS. Also note whether the asm-statement should be
1318 considered volatile. */
1320 tree
1321 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1322 tree input_operands, tree clobbers, tree labels)
1324 tree r;
1325 tree t;
1326 int ninputs = list_length (input_operands);
1327 int noutputs = list_length (output_operands);
1329 if (!processing_template_decl)
1331 const char *constraint;
1332 const char **oconstraints;
1333 bool allows_mem, allows_reg, is_inout;
1334 tree operand;
1335 int i;
1337 oconstraints = XALLOCAVEC (const char *, noutputs);
1339 string = resolve_asm_operand_names (string, output_operands,
1340 input_operands, labels);
1342 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1344 operand = TREE_VALUE (t);
1346 /* ??? Really, this should not be here. Users should be using a
1347 proper lvalue, dammit. But there's a long history of using
1348 casts in the output operands. In cases like longlong.h, this
1349 becomes a primitive form of typechecking -- if the cast can be
1350 removed, then the output operand had a type of the proper width;
1351 otherwise we'll get an error. Gross, but ... */
1352 STRIP_NOPS (operand);
1354 operand = mark_lvalue_use (operand);
1356 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1357 operand = error_mark_node;
1359 if (operand != error_mark_node
1360 && (TREE_READONLY (operand)
1361 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1362 /* Functions are not modifiable, even though they are
1363 lvalues. */
1364 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1365 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1366 /* If it's an aggregate and any field is const, then it is
1367 effectively const. */
1368 || (CLASS_TYPE_P (TREE_TYPE (operand))
1369 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1370 cxx_readonly_error (operand, lv_asm);
1372 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1373 oconstraints[i] = constraint;
1375 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1376 &allows_mem, &allows_reg, &is_inout))
1378 /* If the operand is going to end up in memory,
1379 mark it addressable. */
1380 if (!allows_reg && !cxx_mark_addressable (operand))
1381 operand = error_mark_node;
1383 else
1384 operand = error_mark_node;
1386 TREE_VALUE (t) = operand;
1389 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1391 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1392 bool constraint_parsed
1393 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1394 oconstraints, &allows_mem, &allows_reg);
1395 /* If the operand is going to end up in memory, don't call
1396 decay_conversion. */
1397 if (constraint_parsed && !allows_reg && allows_mem)
1398 operand = mark_lvalue_use (TREE_VALUE (t));
1399 else
1400 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1402 /* If the type of the operand hasn't been determined (e.g.,
1403 because it involves an overloaded function), then issue
1404 an error message. There's no context available to
1405 resolve the overloading. */
1406 if (TREE_TYPE (operand) == unknown_type_node)
1408 error ("type of asm operand %qE could not be determined",
1409 TREE_VALUE (t));
1410 operand = error_mark_node;
1413 if (constraint_parsed)
1415 /* If the operand is going to end up in memory,
1416 mark it addressable. */
1417 if (!allows_reg && allows_mem)
1419 /* Strip the nops as we allow this case. FIXME, this really
1420 should be rejected or made deprecated. */
1421 STRIP_NOPS (operand);
1422 if (!cxx_mark_addressable (operand))
1423 operand = error_mark_node;
1425 else if (!allows_reg && !allows_mem)
1427 /* If constraint allows neither register nor memory,
1428 try harder to get a constant. */
1429 tree constop = maybe_constant_value (operand);
1430 if (TREE_CONSTANT (constop))
1431 operand = constop;
1434 else
1435 operand = error_mark_node;
1437 TREE_VALUE (t) = operand;
1441 r = build_stmt (input_location, ASM_EXPR, string,
1442 output_operands, input_operands,
1443 clobbers, labels);
1444 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1445 r = maybe_cleanup_point_expr_void (r);
1446 return add_stmt (r);
1449 /* Finish a label with the indicated NAME. Returns the new label. */
1451 tree
1452 finish_label_stmt (tree name)
1454 tree decl = define_label (input_location, name);
1456 if (decl == error_mark_node)
1457 return error_mark_node;
1459 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1461 return decl;
1464 /* Finish a series of declarations for local labels. G++ allows users
1465 to declare "local" labels, i.e., labels with scope. This extension
1466 is useful when writing code involving statement-expressions. */
1468 void
1469 finish_label_decl (tree name)
1471 if (!at_function_scope_p ())
1473 error ("__label__ declarations are only allowed in function scopes");
1474 return;
1477 add_decl_expr (declare_local_label (name));
1480 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1482 void
1483 finish_decl_cleanup (tree decl, tree cleanup)
1485 push_cleanup (decl, cleanup, false);
1488 /* If the current scope exits with an exception, run CLEANUP. */
1490 void
1491 finish_eh_cleanup (tree cleanup)
1493 push_cleanup (NULL, cleanup, true);
1496 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1497 order they were written by the user. Each node is as for
1498 emit_mem_initializers. */
1500 void
1501 finish_mem_initializers (tree mem_inits)
1503 /* Reorder the MEM_INITS so that they are in the order they appeared
1504 in the source program. */
1505 mem_inits = nreverse (mem_inits);
1507 if (processing_template_decl)
1509 tree mem;
1511 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1513 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1514 check for bare parameter packs in the TREE_VALUE, because
1515 any parameter packs in the TREE_VALUE have already been
1516 bound as part of the TREE_PURPOSE. See
1517 make_pack_expansion for more information. */
1518 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1519 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1520 TREE_VALUE (mem) = error_mark_node;
1523 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1524 CTOR_INITIALIZER, mem_inits));
1526 else
1527 emit_mem_initializers (mem_inits);
1530 /* Obfuscate EXPR if it looks like an id-expression or member access so
1531 that the call to finish_decltype in do_auto_deduction will give the
1532 right result. */
1534 tree
1535 force_paren_expr (tree expr)
1537 /* This is only needed for decltype(auto) in C++14. */
1538 if (cxx_dialect < cxx1y)
1539 return expr;
1541 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1542 && TREE_CODE (expr) != SCOPE_REF)
1543 return expr;
1545 if (processing_template_decl)
1546 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1547 else
1549 cp_lvalue_kind kind = lvalue_kind (expr);
1550 if ((kind & ~clk_class) != clk_none)
1552 tree type = unlowered_expr_type (expr);
1553 bool rval = !!(kind & clk_rvalueref);
1554 type = cp_build_reference_type (type, rval);
1555 expr = build_static_cast (type, expr, tf_warning_or_error);
1559 return expr;
1562 /* Finish a parenthesized expression EXPR. */
1564 tree
1565 finish_parenthesized_expr (tree expr)
1567 if (EXPR_P (expr))
1568 /* This inhibits warnings in c_common_truthvalue_conversion. */
1569 TREE_NO_WARNING (expr) = 1;
1571 if (TREE_CODE (expr) == OFFSET_REF
1572 || TREE_CODE (expr) == SCOPE_REF)
1573 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1574 enclosed in parentheses. */
1575 PTRMEM_OK_P (expr) = 0;
1577 if (TREE_CODE (expr) == STRING_CST)
1578 PAREN_STRING_LITERAL_P (expr) = 1;
1580 expr = force_paren_expr (expr);
1582 return expr;
1585 /* Finish a reference to a non-static data member (DECL) that is not
1586 preceded by `.' or `->'. */
1588 tree
1589 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1591 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1593 if (!object)
1595 tree scope = qualifying_scope;
1596 if (scope == NULL_TREE)
1597 scope = context_for_name_lookup (decl);
1598 object = maybe_dummy_object (scope, NULL);
1601 object = maybe_resolve_dummy (object);
1602 if (object == error_mark_node)
1603 return error_mark_node;
1605 /* DR 613: Can use non-static data members without an associated
1606 object in sizeof/decltype/alignof. */
1607 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1608 && (!processing_template_decl || !current_class_ref))
1610 if (current_function_decl
1611 && DECL_STATIC_FUNCTION_P (current_function_decl))
1612 error ("invalid use of member %q+D in static member function", decl);
1613 else
1614 error ("invalid use of non-static data member %q+D", decl);
1615 error ("from this location");
1617 return error_mark_node;
1620 if (current_class_ptr)
1621 TREE_USED (current_class_ptr) = 1;
1622 if (processing_template_decl && !qualifying_scope)
1624 tree type = TREE_TYPE (decl);
1626 if (TREE_CODE (type) == REFERENCE_TYPE)
1627 /* Quals on the object don't matter. */;
1628 else if (PACK_EXPANSION_P (type))
1629 /* Don't bother trying to represent this. */
1630 type = NULL_TREE;
1631 else
1633 /* Set the cv qualifiers. */
1634 int quals = cp_type_quals (TREE_TYPE (object));
1636 if (DECL_MUTABLE_P (decl))
1637 quals &= ~TYPE_QUAL_CONST;
1639 quals |= cp_type_quals (TREE_TYPE (decl));
1640 type = cp_build_qualified_type (type, quals);
1643 return (convert_from_reference
1644 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1646 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1647 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1648 for now. */
1649 else if (processing_template_decl)
1650 return build_qualified_name (TREE_TYPE (decl),
1651 qualifying_scope,
1652 decl,
1653 /*template_p=*/false);
1654 else
1656 tree access_type = TREE_TYPE (object);
1658 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1659 decl, tf_warning_or_error);
1661 /* If the data member was named `C::M', convert `*this' to `C'
1662 first. */
1663 if (qualifying_scope)
1665 tree binfo = NULL_TREE;
1666 object = build_scoped_ref (object, qualifying_scope,
1667 &binfo);
1670 return build_class_member_access_expr (object, decl,
1671 /*access_path=*/NULL_TREE,
1672 /*preserve_reference=*/false,
1673 tf_warning_or_error);
1677 /* If we are currently parsing a template and we encountered a typedef
1678 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1679 adds the typedef to a list tied to the current template.
1680 At template instantiation time, that list is walked and access check
1681 performed for each typedef.
1682 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1684 void
1685 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1686 tree context,
1687 location_t location)
1689 tree template_info = NULL;
1690 tree cs = current_scope ();
1692 if (!is_typedef_decl (typedef_decl)
1693 || !context
1694 || !CLASS_TYPE_P (context)
1695 || !cs)
1696 return;
1698 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1699 template_info = get_template_info (cs);
1701 if (template_info
1702 && TI_TEMPLATE (template_info)
1703 && !currently_open_class (context))
1704 append_type_to_template_for_access_check (cs, typedef_decl,
1705 context, location);
1708 /* DECL was the declaration to which a qualified-id resolved. Issue
1709 an error message if it is not accessible. If OBJECT_TYPE is
1710 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1711 type of `*x', or `x', respectively. If the DECL was named as
1712 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1714 void
1715 check_accessibility_of_qualified_id (tree decl,
1716 tree object_type,
1717 tree nested_name_specifier)
1719 tree scope;
1720 tree qualifying_type = NULL_TREE;
1722 /* If we are parsing a template declaration and if decl is a typedef,
1723 add it to a list tied to the template.
1724 At template instantiation time, that list will be walked and
1725 access check performed. */
1726 add_typedef_to_current_template_for_access_check (decl,
1727 nested_name_specifier
1728 ? nested_name_specifier
1729 : DECL_CONTEXT (decl),
1730 input_location);
1732 /* If we're not checking, return immediately. */
1733 if (deferred_access_no_check)
1734 return;
1736 /* Determine the SCOPE of DECL. */
1737 scope = context_for_name_lookup (decl);
1738 /* If the SCOPE is not a type, then DECL is not a member. */
1739 if (!TYPE_P (scope))
1740 return;
1741 /* Compute the scope through which DECL is being accessed. */
1742 if (object_type
1743 /* OBJECT_TYPE might not be a class type; consider:
1745 class A { typedef int I; };
1746 I *p;
1747 p->A::I::~I();
1749 In this case, we will have "A::I" as the DECL, but "I" as the
1750 OBJECT_TYPE. */
1751 && CLASS_TYPE_P (object_type)
1752 && DERIVED_FROM_P (scope, object_type))
1753 /* If we are processing a `->' or `.' expression, use the type of the
1754 left-hand side. */
1755 qualifying_type = object_type;
1756 else if (nested_name_specifier)
1758 /* If the reference is to a non-static member of the
1759 current class, treat it as if it were referenced through
1760 `this'. */
1761 if (DECL_NONSTATIC_MEMBER_P (decl)
1762 && current_class_ptr
1763 && DERIVED_FROM_P (scope, current_class_type))
1764 qualifying_type = current_class_type;
1765 /* Otherwise, use the type indicated by the
1766 nested-name-specifier. */
1767 else
1768 qualifying_type = nested_name_specifier;
1770 else
1771 /* Otherwise, the name must be from the current class or one of
1772 its bases. */
1773 qualifying_type = currently_open_derived_class (scope);
1775 if (qualifying_type
1776 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1777 or similar in a default argument value. */
1778 && CLASS_TYPE_P (qualifying_type)
1779 && !dependent_type_p (qualifying_type))
1780 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1781 decl, tf_warning_or_error);
1784 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1785 class named to the left of the "::" operator. DONE is true if this
1786 expression is a complete postfix-expression; it is false if this
1787 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1788 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1789 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1790 is true iff this qualified name appears as a template argument. */
1792 tree
1793 finish_qualified_id_expr (tree qualifying_class,
1794 tree expr,
1795 bool done,
1796 bool address_p,
1797 bool template_p,
1798 bool template_arg_p,
1799 tsubst_flags_t complain)
1801 gcc_assert (TYPE_P (qualifying_class));
1803 if (error_operand_p (expr))
1804 return error_mark_node;
1806 if ((DECL_P (expr) || BASELINK_P (expr))
1807 && !mark_used (expr, complain))
1808 return error_mark_node;
1810 if (template_p)
1811 check_template_keyword (expr);
1813 /* If EXPR occurs as the operand of '&', use special handling that
1814 permits a pointer-to-member. */
1815 if (address_p && done)
1817 if (TREE_CODE (expr) == SCOPE_REF)
1818 expr = TREE_OPERAND (expr, 1);
1819 expr = build_offset_ref (qualifying_class, expr,
1820 /*address_p=*/true, complain);
1821 return expr;
1824 /* No need to check access within an enum. */
1825 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1826 return expr;
1828 /* Within the scope of a class, turn references to non-static
1829 members into expression of the form "this->...". */
1830 if (template_arg_p)
1831 /* But, within a template argument, we do not want make the
1832 transformation, as there is no "this" pointer. */
1834 else if (TREE_CODE (expr) == FIELD_DECL)
1836 push_deferring_access_checks (dk_no_check);
1837 expr = finish_non_static_data_member (expr, NULL_TREE,
1838 qualifying_class);
1839 pop_deferring_access_checks ();
1841 else if (BASELINK_P (expr) && !processing_template_decl)
1843 /* See if any of the functions are non-static members. */
1844 /* If so, the expression may be relative to 'this'. */
1845 if (!shared_member_p (expr)
1846 && current_class_ptr
1847 && DERIVED_FROM_P (qualifying_class,
1848 current_nonlambda_class_type ()))
1849 expr = (build_class_member_access_expr
1850 (maybe_dummy_object (qualifying_class, NULL),
1851 expr,
1852 BASELINK_ACCESS_BINFO (expr),
1853 /*preserve_reference=*/false,
1854 complain));
1855 else if (done)
1856 /* The expression is a qualified name whose address is not
1857 being taken. */
1858 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1859 complain);
1861 else if (BASELINK_P (expr))
1863 else
1865 /* In a template, return a SCOPE_REF for most qualified-ids
1866 so that we can check access at instantiation time. But if
1867 we're looking at a member of the current instantiation, we
1868 know we have access and building up the SCOPE_REF confuses
1869 non-type template argument handling. */
1870 if (processing_template_decl
1871 && !currently_open_class (qualifying_class))
1872 expr = build_qualified_name (TREE_TYPE (expr),
1873 qualifying_class, expr,
1874 template_p);
1876 expr = convert_from_reference (expr);
1879 return expr;
1882 /* Begin a statement-expression. The value returned must be passed to
1883 finish_stmt_expr. */
1885 tree
1886 begin_stmt_expr (void)
1888 return push_stmt_list ();
1891 /* Process the final expression of a statement expression. EXPR can be
1892 NULL, if the final expression is empty. Return a STATEMENT_LIST
1893 containing all the statements in the statement-expression, or
1894 ERROR_MARK_NODE if there was an error. */
1896 tree
1897 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1899 if (error_operand_p (expr))
1901 /* The type of the statement-expression is the type of the last
1902 expression. */
1903 TREE_TYPE (stmt_expr) = error_mark_node;
1904 return error_mark_node;
1907 /* If the last statement does not have "void" type, then the value
1908 of the last statement is the value of the entire expression. */
1909 if (expr)
1911 tree type = TREE_TYPE (expr);
1913 if (processing_template_decl)
1915 expr = build_stmt (input_location, EXPR_STMT, expr);
1916 expr = add_stmt (expr);
1917 /* Mark the last statement so that we can recognize it as such at
1918 template-instantiation time. */
1919 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1921 else if (VOID_TYPE_P (type))
1923 /* Just treat this like an ordinary statement. */
1924 expr = finish_expr_stmt (expr);
1926 else
1928 /* It actually has a value we need to deal with. First, force it
1929 to be an rvalue so that we won't need to build up a copy
1930 constructor call later when we try to assign it to something. */
1931 expr = force_rvalue (expr, tf_warning_or_error);
1932 if (error_operand_p (expr))
1933 return error_mark_node;
1935 /* Update for array-to-pointer decay. */
1936 type = TREE_TYPE (expr);
1938 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1939 normal statement, but don't convert to void or actually add
1940 the EXPR_STMT. */
1941 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1942 expr = maybe_cleanup_point_expr (expr);
1943 add_stmt (expr);
1946 /* The type of the statement-expression is the type of the last
1947 expression. */
1948 TREE_TYPE (stmt_expr) = type;
1951 return stmt_expr;
1954 /* Finish a statement-expression. EXPR should be the value returned
1955 by the previous begin_stmt_expr. Returns an expression
1956 representing the statement-expression. */
1958 tree
1959 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1961 tree type;
1962 tree result;
1964 if (error_operand_p (stmt_expr))
1966 pop_stmt_list (stmt_expr);
1967 return error_mark_node;
1970 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1972 type = TREE_TYPE (stmt_expr);
1973 result = pop_stmt_list (stmt_expr);
1974 TREE_TYPE (result) = type;
1976 if (processing_template_decl)
1978 result = build_min (STMT_EXPR, type, result);
1979 TREE_SIDE_EFFECTS (result) = 1;
1980 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1982 else if (CLASS_TYPE_P (type))
1984 /* Wrap the statement-expression in a TARGET_EXPR so that the
1985 temporary object created by the final expression is destroyed at
1986 the end of the full-expression containing the
1987 statement-expression. */
1988 result = force_target_expr (type, result, tf_warning_or_error);
1991 return result;
1994 /* Returns the expression which provides the value of STMT_EXPR. */
1996 tree
1997 stmt_expr_value_expr (tree stmt_expr)
1999 tree t = STMT_EXPR_STMT (stmt_expr);
2001 if (TREE_CODE (t) == BIND_EXPR)
2002 t = BIND_EXPR_BODY (t);
2004 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2005 t = STATEMENT_LIST_TAIL (t)->stmt;
2007 if (TREE_CODE (t) == EXPR_STMT)
2008 t = EXPR_STMT_EXPR (t);
2010 return t;
2013 /* Return TRUE iff EXPR_STMT is an empty list of
2014 expression statements. */
2016 bool
2017 empty_expr_stmt_p (tree expr_stmt)
2019 tree body = NULL_TREE;
2021 if (expr_stmt == void_zero_node)
2022 return true;
2024 if (expr_stmt)
2026 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2027 body = EXPR_STMT_EXPR (expr_stmt);
2028 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2029 body = expr_stmt;
2032 if (body)
2034 if (TREE_CODE (body) == STATEMENT_LIST)
2035 return tsi_end_p (tsi_start (body));
2036 else
2037 return empty_expr_stmt_p (body);
2039 return false;
2042 /* Perform Koenig lookup. FN is the postfix-expression representing
2043 the function (or functions) to call; ARGS are the arguments to the
2044 call. Returns the functions to be considered by overload resolution. */
2046 tree
2047 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2048 tsubst_flags_t complain)
2050 tree identifier = NULL_TREE;
2051 tree functions = NULL_TREE;
2052 tree tmpl_args = NULL_TREE;
2053 bool template_id = false;
2055 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2057 /* Use a separate flag to handle null args. */
2058 template_id = true;
2059 tmpl_args = TREE_OPERAND (fn, 1);
2060 fn = TREE_OPERAND (fn, 0);
2063 /* Find the name of the overloaded function. */
2064 if (identifier_p (fn))
2065 identifier = fn;
2066 else if (is_overloaded_fn (fn))
2068 functions = fn;
2069 identifier = DECL_NAME (get_first_fn (functions));
2071 else if (DECL_P (fn))
2073 functions = fn;
2074 identifier = DECL_NAME (fn);
2077 /* A call to a namespace-scope function using an unqualified name.
2079 Do Koenig lookup -- unless any of the arguments are
2080 type-dependent. */
2081 if (!any_type_dependent_arguments_p (args)
2082 && !any_dependent_template_arguments_p (tmpl_args))
2084 fn = lookup_arg_dependent (identifier, functions, args);
2085 if (!fn)
2087 /* The unqualified name could not be resolved. */
2088 if (complain)
2089 fn = unqualified_fn_lookup_error (identifier);
2090 else
2091 fn = identifier;
2095 if (fn && template_id)
2096 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2098 return fn;
2101 /* Generate an expression for `FN (ARGS)'. This may change the
2102 contents of ARGS.
2104 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2105 as a virtual call, even if FN is virtual. (This flag is set when
2106 encountering an expression where the function name is explicitly
2107 qualified. For example a call to `X::f' never generates a virtual
2108 call.)
2110 Returns code for the call. */
2112 tree
2113 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2114 bool koenig_p, tsubst_flags_t complain)
2116 tree result;
2117 tree orig_fn;
2118 vec<tree, va_gc> *orig_args = NULL;
2120 if (fn == error_mark_node)
2121 return error_mark_node;
2123 gcc_assert (!TYPE_P (fn));
2125 orig_fn = fn;
2127 if (processing_template_decl)
2129 /* If the call expression is dependent, build a CALL_EXPR node
2130 with no type; type_dependent_expression_p recognizes
2131 expressions with no type as being dependent. */
2132 if (type_dependent_expression_p (fn)
2133 || any_type_dependent_arguments_p (*args)
2134 /* For a non-static member function that doesn't have an
2135 explicit object argument, we need to specifically
2136 test the type dependency of the "this" pointer because it
2137 is not included in *ARGS even though it is considered to
2138 be part of the list of arguments. Note that this is
2139 related to CWG issues 515 and 1005. */
2140 || (TREE_CODE (fn) != COMPONENT_REF
2141 && non_static_member_function_p (fn)
2142 && current_class_ref
2143 && type_dependent_expression_p (current_class_ref)))
2145 result = build_nt_call_vec (fn, *args);
2146 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2147 KOENIG_LOOKUP_P (result) = koenig_p;
2148 if (cfun)
2152 tree fndecl = OVL_CURRENT (fn);
2153 if (TREE_CODE (fndecl) != FUNCTION_DECL
2154 || !TREE_THIS_VOLATILE (fndecl))
2155 break;
2156 fn = OVL_NEXT (fn);
2158 while (fn);
2159 if (!fn)
2160 current_function_returns_abnormally = 1;
2162 return result;
2164 orig_args = make_tree_vector_copy (*args);
2165 if (!BASELINK_P (fn)
2166 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2167 && TREE_TYPE (fn) != unknown_type_node)
2168 fn = build_non_dependent_expr (fn);
2169 make_args_non_dependent (*args);
2172 if (TREE_CODE (fn) == COMPONENT_REF)
2174 tree member = TREE_OPERAND (fn, 1);
2175 if (BASELINK_P (member))
2177 tree object = TREE_OPERAND (fn, 0);
2178 return build_new_method_call (object, member,
2179 args, NULL_TREE,
2180 (disallow_virtual
2181 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2182 : LOOKUP_NORMAL),
2183 /*fn_p=*/NULL,
2184 complain);
2188 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2189 if (TREE_CODE (fn) == ADDR_EXPR
2190 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2191 fn = TREE_OPERAND (fn, 0);
2193 if (is_overloaded_fn (fn))
2194 fn = baselink_for_fns (fn);
2196 result = NULL_TREE;
2197 if (BASELINK_P (fn))
2199 tree object;
2201 /* A call to a member function. From [over.call.func]:
2203 If the keyword this is in scope and refers to the class of
2204 that member function, or a derived class thereof, then the
2205 function call is transformed into a qualified function call
2206 using (*this) as the postfix-expression to the left of the
2207 . operator.... [Otherwise] a contrived object of type T
2208 becomes the implied object argument.
2210 In this situation:
2212 struct A { void f(); };
2213 struct B : public A {};
2214 struct C : public A { void g() { B::f(); }};
2216 "the class of that member function" refers to `A'. But 11.2
2217 [class.access.base] says that we need to convert 'this' to B* as
2218 part of the access, so we pass 'B' to maybe_dummy_object. */
2220 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2221 NULL);
2223 if (processing_template_decl)
2225 if (type_dependent_expression_p (object))
2227 tree ret = build_nt_call_vec (orig_fn, orig_args);
2228 release_tree_vector (orig_args);
2229 return ret;
2231 object = build_non_dependent_expr (object);
2234 result = build_new_method_call (object, fn, args, NULL_TREE,
2235 (disallow_virtual
2236 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2237 : LOOKUP_NORMAL),
2238 /*fn_p=*/NULL,
2239 complain);
2241 else if (is_overloaded_fn (fn))
2243 /* If the function is an overloaded builtin, resolve it. */
2244 if (TREE_CODE (fn) == FUNCTION_DECL
2245 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2246 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2247 result = resolve_overloaded_builtin (input_location, fn, *args);
2249 if (!result)
2251 if (warn_sizeof_pointer_memaccess
2252 && !vec_safe_is_empty (*args)
2253 && !processing_template_decl)
2255 location_t sizeof_arg_loc[3];
2256 tree sizeof_arg[3];
2257 unsigned int i;
2258 for (i = 0; i < 3; i++)
2260 tree t;
2262 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2263 sizeof_arg[i] = NULL_TREE;
2264 if (i >= (*args)->length ())
2265 continue;
2266 t = (**args)[i];
2267 if (TREE_CODE (t) != SIZEOF_EXPR)
2268 continue;
2269 if (SIZEOF_EXPR_TYPE_P (t))
2270 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2271 else
2272 sizeof_arg[i] = TREE_OPERAND (t, 0);
2273 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2275 sizeof_pointer_memaccess_warning
2276 (sizeof_arg_loc, fn, *args,
2277 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2280 /* A call to a namespace-scope function. */
2281 result = build_new_function_call (fn, args, koenig_p, complain);
2284 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2286 if (!vec_safe_is_empty (*args))
2287 error ("arguments to destructor are not allowed");
2288 /* Mark the pseudo-destructor call as having side-effects so
2289 that we do not issue warnings about its use. */
2290 result = build1 (NOP_EXPR,
2291 void_type_node,
2292 TREE_OPERAND (fn, 0));
2293 TREE_SIDE_EFFECTS (result) = 1;
2295 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2296 /* If the "function" is really an object of class type, it might
2297 have an overloaded `operator ()'. */
2298 result = build_op_call (fn, args, complain);
2300 if (!result)
2301 /* A call where the function is unknown. */
2302 result = cp_build_function_call_vec (fn, args, complain);
2304 if (processing_template_decl && result != error_mark_node)
2306 if (INDIRECT_REF_P (result))
2307 result = TREE_OPERAND (result, 0);
2308 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2309 SET_EXPR_LOCATION (result, input_location);
2310 KOENIG_LOOKUP_P (result) = koenig_p;
2311 release_tree_vector (orig_args);
2312 result = convert_from_reference (result);
2315 if (koenig_p)
2317 /* Free garbage OVERLOADs from arg-dependent lookup. */
2318 tree next = NULL_TREE;
2319 for (fn = orig_fn;
2320 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2321 fn = next)
2323 if (processing_template_decl)
2324 /* In a template, we'll re-use them at instantiation time. */
2325 OVL_ARG_DEPENDENT (fn) = false;
2326 else
2328 next = OVL_CHAIN (fn);
2329 ggc_free (fn);
2334 return result;
2337 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2338 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2339 POSTDECREMENT_EXPR.) */
2341 tree
2342 finish_increment_expr (tree expr, enum tree_code code)
2344 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2347 /* Finish a use of `this'. Returns an expression for `this'. */
2349 tree
2350 finish_this_expr (void)
2352 tree result;
2354 if (current_class_ptr)
2356 tree type = TREE_TYPE (current_class_ref);
2358 /* In a lambda expression, 'this' refers to the captured 'this'. */
2359 if (LAMBDA_TYPE_P (type))
2360 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2361 else
2362 result = current_class_ptr;
2364 else if (current_function_decl
2365 && DECL_STATIC_FUNCTION_P (current_function_decl))
2367 error ("%<this%> is unavailable for static member functions");
2368 result = error_mark_node;
2370 else
2372 if (current_function_decl)
2373 error ("invalid use of %<this%> in non-member function");
2374 else
2375 error ("invalid use of %<this%> at top level");
2376 result = error_mark_node;
2379 /* The keyword 'this' is a prvalue expression. */
2380 result = rvalue (result);
2382 return result;
2385 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2386 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2387 the TYPE for the type given. If SCOPE is non-NULL, the expression
2388 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2390 tree
2391 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2392 location_t loc)
2394 if (object == error_mark_node || destructor == error_mark_node)
2395 return error_mark_node;
2397 gcc_assert (TYPE_P (destructor));
2399 if (!processing_template_decl)
2401 if (scope == error_mark_node)
2403 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2404 return error_mark_node;
2406 if (is_auto (destructor))
2407 destructor = TREE_TYPE (object);
2408 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2410 error_at (loc,
2411 "qualified type %qT does not match destructor name ~%qT",
2412 scope, destructor);
2413 return error_mark_node;
2417 /* [expr.pseudo] says both:
2419 The type designated by the pseudo-destructor-name shall be
2420 the same as the object type.
2422 and:
2424 The cv-unqualified versions of the object type and of the
2425 type designated by the pseudo-destructor-name shall be the
2426 same type.
2428 We implement the more generous second sentence, since that is
2429 what most other compilers do. */
2430 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2431 destructor))
2433 error_at (loc, "%qE is not of type %qT", object, destructor);
2434 return error_mark_node;
2438 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2439 scope, destructor);
2442 /* Finish an expression of the form CODE EXPR. */
2444 tree
2445 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2446 tsubst_flags_t complain)
2448 tree result = build_x_unary_op (loc, code, expr, complain);
2449 if ((complain & tf_warning)
2450 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2451 overflow_warning (input_location, result);
2453 return result;
2456 /* Finish a compound-literal expression. TYPE is the type to which
2457 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2459 tree
2460 finish_compound_literal (tree type, tree compound_literal,
2461 tsubst_flags_t complain)
2463 if (type == error_mark_node)
2464 return error_mark_node;
2466 if (TREE_CODE (type) == REFERENCE_TYPE)
2468 compound_literal
2469 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2470 complain);
2471 return cp_build_c_cast (type, compound_literal, complain);
2474 if (!TYPE_OBJ_P (type))
2476 if (complain & tf_error)
2477 error ("compound literal of non-object type %qT", type);
2478 return error_mark_node;
2481 if (processing_template_decl)
2483 TREE_TYPE (compound_literal) = type;
2484 /* Mark the expression as a compound literal. */
2485 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2486 return compound_literal;
2489 type = complete_type (type);
2491 if (TYPE_NON_AGGREGATE_CLASS (type))
2493 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2494 everywhere that deals with function arguments would be a pain, so
2495 just wrap it in a TREE_LIST. The parser set a flag so we know
2496 that it came from T{} rather than T({}). */
2497 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2498 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2499 return build_functional_cast (type, compound_literal, complain);
2502 if (TREE_CODE (type) == ARRAY_TYPE
2503 && check_array_initializer (NULL_TREE, type, compound_literal))
2504 return error_mark_node;
2505 compound_literal = reshape_init (type, compound_literal, complain);
2506 if (SCALAR_TYPE_P (type)
2507 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2508 && (complain & tf_warning_or_error))
2509 check_narrowing (type, compound_literal);
2510 if (TREE_CODE (type) == ARRAY_TYPE
2511 && TYPE_DOMAIN (type) == NULL_TREE)
2513 cp_complete_array_type_or_error (&type, compound_literal,
2514 false, complain);
2515 if (type == error_mark_node)
2516 return error_mark_node;
2518 compound_literal = digest_init (type, compound_literal, complain);
2519 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2520 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2521 /* Put static/constant array temporaries in static variables, but always
2522 represent class temporaries with TARGET_EXPR so we elide copies. */
2523 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2524 && TREE_CODE (type) == ARRAY_TYPE
2525 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2526 && !cp_unevaluated_operand
2527 && initializer_constant_valid_p (compound_literal, type))
2529 tree decl = create_temporary_var (type);
2530 DECL_INITIAL (decl) = compound_literal;
2531 TREE_STATIC (decl) = 1;
2532 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2534 /* 5.19 says that a constant expression can include an
2535 lvalue-rvalue conversion applied to "a glvalue of literal type
2536 that refers to a non-volatile temporary object initialized
2537 with a constant expression". Rather than try to communicate
2538 that this VAR_DECL is a temporary, just mark it constexpr. */
2539 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2540 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2541 TREE_CONSTANT (decl) = true;
2543 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2544 decl = pushdecl_top_level (decl);
2545 DECL_NAME (decl) = make_anon_name ();
2546 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2547 /* Make sure the destructor is callable. */
2548 tree clean = cxx_maybe_build_cleanup (decl, complain);
2549 if (clean == error_mark_node)
2550 return error_mark_node;
2551 return decl;
2553 else
2554 return get_target_expr_sfinae (compound_literal, complain);
2557 /* Return the declaration for the function-name variable indicated by
2558 ID. */
2560 tree
2561 finish_fname (tree id)
2563 tree decl;
2565 decl = fname_decl (input_location, C_RID_CODE (id), id);
2566 if (processing_template_decl && current_function_decl)
2567 decl = DECL_NAME (decl);
2568 return decl;
2571 /* Finish a translation unit. */
2573 void
2574 finish_translation_unit (void)
2576 /* In case there were missing closebraces,
2577 get us back to the global binding level. */
2578 pop_everything ();
2579 while (current_namespace != global_namespace)
2580 pop_namespace ();
2582 /* Do file scope __FUNCTION__ et al. */
2583 finish_fname_decls ();
2586 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2587 Returns the parameter. */
2589 tree
2590 finish_template_type_parm (tree aggr, tree identifier)
2592 if (aggr != class_type_node)
2594 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2595 aggr = class_type_node;
2598 return build_tree_list (aggr, identifier);
2601 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2602 Returns the parameter. */
2604 tree
2605 finish_template_template_parm (tree aggr, tree identifier)
2607 tree decl = build_decl (input_location,
2608 TYPE_DECL, identifier, NULL_TREE);
2609 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2610 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2611 DECL_TEMPLATE_RESULT (tmpl) = decl;
2612 DECL_ARTIFICIAL (decl) = 1;
2613 end_template_decl ();
2615 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2617 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2618 /*is_primary=*/true, /*is_partial=*/false,
2619 /*is_friend=*/0);
2621 return finish_template_type_parm (aggr, tmpl);
2624 /* ARGUMENT is the default-argument value for a template template
2625 parameter. If ARGUMENT is invalid, issue error messages and return
2626 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2628 tree
2629 check_template_template_default_arg (tree argument)
2631 if (TREE_CODE (argument) != TEMPLATE_DECL
2632 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2633 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2635 if (TREE_CODE (argument) == TYPE_DECL)
2636 error ("invalid use of type %qT as a default value for a template "
2637 "template-parameter", TREE_TYPE (argument));
2638 else
2639 error ("invalid default argument for a template template parameter");
2640 return error_mark_node;
2643 return argument;
2646 /* Begin a class definition, as indicated by T. */
2648 tree
2649 begin_class_definition (tree t)
2651 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2652 return error_mark_node;
2654 if (processing_template_parmlist)
2656 error ("definition of %q#T inside template parameter list", t);
2657 return error_mark_node;
2660 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2661 are passed the same as decimal scalar types. */
2662 if (TREE_CODE (t) == RECORD_TYPE
2663 && !processing_template_decl)
2665 tree ns = TYPE_CONTEXT (t);
2666 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2667 && DECL_CONTEXT (ns) == std_node
2668 && DECL_NAME (ns)
2669 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2671 const char *n = TYPE_NAME_STRING (t);
2672 if ((strcmp (n, "decimal32") == 0)
2673 || (strcmp (n, "decimal64") == 0)
2674 || (strcmp (n, "decimal128") == 0))
2675 TYPE_TRANSPARENT_AGGR (t) = 1;
2679 /* A non-implicit typename comes from code like:
2681 template <typename T> struct A {
2682 template <typename U> struct A<T>::B ...
2684 This is erroneous. */
2685 else if (TREE_CODE (t) == TYPENAME_TYPE)
2687 error ("invalid definition of qualified type %qT", t);
2688 t = error_mark_node;
2691 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2693 t = make_class_type (RECORD_TYPE);
2694 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2697 if (TYPE_BEING_DEFINED (t))
2699 t = make_class_type (TREE_CODE (t));
2700 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2702 maybe_process_partial_specialization (t);
2703 pushclass (t);
2704 TYPE_BEING_DEFINED (t) = 1;
2706 if (flag_pack_struct)
2708 tree v;
2709 TYPE_PACKED (t) = 1;
2710 /* Even though the type is being defined for the first time
2711 here, there might have been a forward declaration, so there
2712 might be cv-qualified variants of T. */
2713 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2714 TYPE_PACKED (v) = 1;
2716 /* Reset the interface data, at the earliest possible
2717 moment, as it might have been set via a class foo;
2718 before. */
2719 if (! TYPE_ANONYMOUS_P (t))
2721 struct c_fileinfo *finfo = \
2722 get_fileinfo (LOCATION_FILE (input_location));
2723 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2724 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2725 (t, finfo->interface_unknown);
2727 reset_specialization();
2729 /* Make a declaration for this class in its own scope. */
2730 build_self_reference ();
2732 return t;
2735 /* Finish the member declaration given by DECL. */
2737 void
2738 finish_member_declaration (tree decl)
2740 if (decl == error_mark_node || decl == NULL_TREE)
2741 return;
2743 if (decl == void_type_node)
2744 /* The COMPONENT was a friend, not a member, and so there's
2745 nothing for us to do. */
2746 return;
2748 /* We should see only one DECL at a time. */
2749 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2751 /* Set up access control for DECL. */
2752 TREE_PRIVATE (decl)
2753 = (current_access_specifier == access_private_node);
2754 TREE_PROTECTED (decl)
2755 = (current_access_specifier == access_protected_node);
2756 if (TREE_CODE (decl) == TEMPLATE_DECL)
2758 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2759 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2762 /* Mark the DECL as a member of the current class, unless it's
2763 a member of an enumeration. */
2764 if (TREE_CODE (decl) != CONST_DECL)
2765 DECL_CONTEXT (decl) = current_class_type;
2767 /* Check for bare parameter packs in the member variable declaration. */
2768 if (TREE_CODE (decl) == FIELD_DECL)
2770 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2771 TREE_TYPE (decl) = error_mark_node;
2772 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2773 DECL_ATTRIBUTES (decl) = NULL_TREE;
2776 /* [dcl.link]
2778 A C language linkage is ignored for the names of class members
2779 and the member function type of class member functions. */
2780 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2781 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2783 /* Put functions on the TYPE_METHODS list and everything else on the
2784 TYPE_FIELDS list. Note that these are built up in reverse order.
2785 We reverse them (to obtain declaration order) in finish_struct. */
2786 if (DECL_DECLARES_FUNCTION_P (decl))
2788 /* We also need to add this function to the
2789 CLASSTYPE_METHOD_VEC. */
2790 if (add_method (current_class_type, decl, NULL_TREE))
2792 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2793 TYPE_METHODS (current_class_type) = decl;
2795 maybe_add_class_template_decl_list (current_class_type, decl,
2796 /*friend_p=*/0);
2799 /* Enter the DECL into the scope of the class, if the class
2800 isn't a closure (whose fields are supposed to be unnamed). */
2801 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2802 || pushdecl_class_level (decl))
2804 if (TREE_CODE (decl) == USING_DECL)
2806 /* For now, ignore class-scope USING_DECLS, so that
2807 debugging backends do not see them. */
2808 DECL_IGNORED_P (decl) = 1;
2811 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2812 go at the beginning. The reason is that lookup_field_1
2813 searches the list in order, and we want a field name to
2814 override a type name so that the "struct stat hack" will
2815 work. In particular:
2817 struct S { enum E { }; int E } s;
2818 s.E = 3;
2820 is valid. In addition, the FIELD_DECLs must be maintained in
2821 declaration order so that class layout works as expected.
2822 However, we don't need that order until class layout, so we
2823 save a little time by putting FIELD_DECLs on in reverse order
2824 here, and then reversing them in finish_struct_1. (We could
2825 also keep a pointer to the correct insertion points in the
2826 list.) */
2828 if (TREE_CODE (decl) == TYPE_DECL)
2829 TYPE_FIELDS (current_class_type)
2830 = chainon (TYPE_FIELDS (current_class_type), decl);
2831 else
2833 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2834 TYPE_FIELDS (current_class_type) = decl;
2837 maybe_add_class_template_decl_list (current_class_type, decl,
2838 /*friend_p=*/0);
2841 if (pch_file)
2842 note_decl_for_pch (decl);
2845 /* DECL has been declared while we are building a PCH file. Perform
2846 actions that we might normally undertake lazily, but which can be
2847 performed now so that they do not have to be performed in
2848 translation units which include the PCH file. */
2850 void
2851 note_decl_for_pch (tree decl)
2853 gcc_assert (pch_file);
2855 /* There's a good chance that we'll have to mangle names at some
2856 point, even if only for emission in debugging information. */
2857 if (VAR_OR_FUNCTION_DECL_P (decl)
2858 && !processing_template_decl)
2859 mangle_decl (decl);
2862 /* Finish processing a complete template declaration. The PARMS are
2863 the template parameters. */
2865 void
2866 finish_template_decl (tree parms)
2868 if (parms)
2869 end_template_decl ();
2870 else
2871 end_specialization ();
2874 /* Finish processing a template-id (which names a type) of the form
2875 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2876 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2877 the scope of template-id indicated. */
2879 tree
2880 finish_template_type (tree name, tree args, int entering_scope)
2882 tree type;
2884 type = lookup_template_class (name, args,
2885 NULL_TREE, NULL_TREE, entering_scope,
2886 tf_warning_or_error | tf_user);
2887 if (type == error_mark_node)
2888 return type;
2889 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2890 return TYPE_STUB_DECL (type);
2891 else
2892 return TYPE_NAME (type);
2895 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2896 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2897 BASE_CLASS, or NULL_TREE if an error occurred. The
2898 ACCESS_SPECIFIER is one of
2899 access_{default,public,protected_private}_node. For a virtual base
2900 we set TREE_TYPE. */
2902 tree
2903 finish_base_specifier (tree base, tree access, bool virtual_p)
2905 tree result;
2907 if (base == error_mark_node)
2909 error ("invalid base-class specification");
2910 result = NULL_TREE;
2912 else if (! MAYBE_CLASS_TYPE_P (base))
2914 error ("%qT is not a class type", base);
2915 result = NULL_TREE;
2917 else
2919 if (cp_type_quals (base) != 0)
2921 /* DR 484: Can a base-specifier name a cv-qualified
2922 class type? */
2923 base = TYPE_MAIN_VARIANT (base);
2925 result = build_tree_list (access, base);
2926 if (virtual_p)
2927 TREE_TYPE (result) = integer_type_node;
2930 return result;
2933 /* If FNS is a member function, a set of member functions, or a
2934 template-id referring to one or more member functions, return a
2935 BASELINK for FNS, incorporating the current access context.
2936 Otherwise, return FNS unchanged. */
2938 tree
2939 baselink_for_fns (tree fns)
2941 tree scope;
2942 tree cl;
2944 if (BASELINK_P (fns)
2945 || error_operand_p (fns))
2946 return fns;
2948 scope = ovl_scope (fns);
2949 if (!CLASS_TYPE_P (scope))
2950 return fns;
2952 cl = currently_open_derived_class (scope);
2953 if (!cl)
2954 cl = scope;
2955 cl = TYPE_BINFO (cl);
2956 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2959 /* Returns true iff DECL is a variable from a function outside
2960 the current one. */
2962 static bool
2963 outer_var_p (tree decl)
2965 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
2966 && DECL_FUNCTION_SCOPE_P (decl)
2967 && (DECL_CONTEXT (decl) != current_function_decl
2968 || parsing_nsdmi ()));
2971 /* As above, but also checks that DECL is automatic. */
2973 static bool
2974 outer_automatic_var_p (tree decl)
2976 return (outer_var_p (decl)
2977 && !TREE_STATIC (decl));
2980 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2981 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2982 if non-NULL, is the type or namespace used to explicitly qualify
2983 ID_EXPRESSION. DECL is the entity to which that name has been
2984 resolved.
2986 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2987 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2988 be set to true if this expression isn't permitted in a
2989 constant-expression, but it is otherwise not set by this function.
2990 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2991 constant-expression, but a non-constant expression is also
2992 permissible.
2994 DONE is true if this expression is a complete postfix-expression;
2995 it is false if this expression is followed by '->', '[', '(', etc.
2996 ADDRESS_P is true iff this expression is the operand of '&'.
2997 TEMPLATE_P is true iff the qualified-id was of the form
2998 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2999 appears as a template argument.
3001 If an error occurs, and it is the kind of error that might cause
3002 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3003 is the caller's responsibility to issue the message. *ERROR_MSG
3004 will be a string with static storage duration, so the caller need
3005 not "free" it.
3007 Return an expression for the entity, after issuing appropriate
3008 diagnostics. This function is also responsible for transforming a
3009 reference to a non-static member into a COMPONENT_REF that makes
3010 the use of "this" explicit.
3012 Upon return, *IDK will be filled in appropriately. */
3013 tree
3014 finish_id_expression (tree id_expression,
3015 tree decl,
3016 tree scope,
3017 cp_id_kind *idk,
3018 bool integral_constant_expression_p,
3019 bool allow_non_integral_constant_expression_p,
3020 bool *non_integral_constant_expression_p,
3021 bool template_p,
3022 bool done,
3023 bool address_p,
3024 bool template_arg_p,
3025 const char **error_msg,
3026 location_t location)
3028 decl = strip_using_decl (decl);
3030 /* Initialize the output parameters. */
3031 *idk = CP_ID_KIND_NONE;
3032 *error_msg = NULL;
3034 if (id_expression == error_mark_node)
3035 return error_mark_node;
3036 /* If we have a template-id, then no further lookup is
3037 required. If the template-id was for a template-class, we
3038 will sometimes have a TYPE_DECL at this point. */
3039 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3040 || TREE_CODE (decl) == TYPE_DECL)
3042 /* Look up the name. */
3043 else
3045 if (decl == error_mark_node)
3047 /* Name lookup failed. */
3048 if (scope
3049 && (!TYPE_P (scope)
3050 || (!dependent_type_p (scope)
3051 && !(identifier_p (id_expression)
3052 && IDENTIFIER_TYPENAME_P (id_expression)
3053 && dependent_type_p (TREE_TYPE (id_expression))))))
3055 /* If the qualifying type is non-dependent (and the name
3056 does not name a conversion operator to a dependent
3057 type), issue an error. */
3058 qualified_name_lookup_error (scope, id_expression, decl, location);
3059 return error_mark_node;
3061 else if (!scope)
3063 /* It may be resolved via Koenig lookup. */
3064 *idk = CP_ID_KIND_UNQUALIFIED;
3065 return id_expression;
3067 else
3068 decl = id_expression;
3070 /* If DECL is a variable that would be out of scope under
3071 ANSI/ISO rules, but in scope in the ARM, name lookup
3072 will succeed. Issue a diagnostic here. */
3073 else
3074 decl = check_for_out_of_scope_variable (decl);
3076 /* Remember that the name was used in the definition of
3077 the current class so that we can check later to see if
3078 the meaning would have been different after the class
3079 was entirely defined. */
3080 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3081 maybe_note_name_used_in_class (id_expression, decl);
3083 /* Disallow uses of local variables from containing functions, except
3084 within lambda-expressions. */
3085 if (!outer_var_p (decl))
3086 /* OK */;
3087 else if (TREE_STATIC (decl)
3088 /* It's not a use (3.2) if we're in an unevaluated context. */
3089 || cp_unevaluated_operand)
3091 if (processing_template_decl)
3092 /* For a use of an outer static/unevaluated var, return the id
3093 so that we'll look it up again in the instantiation. */
3094 return id_expression;
3096 else
3098 tree context = DECL_CONTEXT (decl);
3099 tree containing_function = current_function_decl;
3100 tree lambda_stack = NULL_TREE;
3101 tree lambda_expr = NULL_TREE;
3102 tree initializer = convert_from_reference (decl);
3104 /* Mark it as used now even if the use is ill-formed. */
3105 mark_used (decl);
3107 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3108 support for an approach in which a reference to a local
3109 [constant] automatic variable in a nested class or lambda body
3110 would enter the expression as an rvalue, which would reduce
3111 the complexity of the problem"
3113 FIXME update for final resolution of core issue 696. */
3114 if (decl_constant_var_p (decl))
3116 if (processing_template_decl)
3117 /* In a template, the constant value may not be in a usable
3118 form, so look it up again at instantiation time. */
3119 return id_expression;
3120 else
3121 return integral_constant_value (decl);
3124 if (parsing_nsdmi ())
3125 containing_function = NULL_TREE;
3126 /* If we are in a lambda function, we can move out until we hit
3127 1. the context,
3128 2. a non-lambda function, or
3129 3. a non-default capturing lambda function. */
3130 else while (context != containing_function
3131 && LAMBDA_FUNCTION_P (containing_function))
3133 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3134 (DECL_CONTEXT (containing_function));
3136 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3137 == CPLD_NONE)
3138 break;
3140 lambda_stack = tree_cons (NULL_TREE,
3141 lambda_expr,
3142 lambda_stack);
3144 containing_function
3145 = decl_function_context (containing_function);
3148 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3149 && DECL_ANON_UNION_VAR_P (decl))
3151 error ("cannot capture member %qD of anonymous union", decl);
3152 return error_mark_node;
3154 if (context == containing_function)
3156 decl = add_default_capture (lambda_stack,
3157 /*id=*/DECL_NAME (decl),
3158 initializer);
3160 else if (lambda_expr)
3162 error ("%qD is not captured", decl);
3163 return error_mark_node;
3165 else
3167 error (VAR_P (decl)
3168 ? G_("use of local variable with automatic storage from containing function")
3169 : G_("use of parameter from containing function"));
3170 inform (input_location, "%q+#D declared here", decl);
3171 return error_mark_node;
3175 /* Also disallow uses of function parameters outside the function
3176 body, except inside an unevaluated context (i.e. decltype). */
3177 if (TREE_CODE (decl) == PARM_DECL
3178 && DECL_CONTEXT (decl) == NULL_TREE
3179 && !cp_unevaluated_operand)
3181 error ("use of parameter %qD outside function body", decl);
3182 return error_mark_node;
3186 /* If we didn't find anything, or what we found was a type,
3187 then this wasn't really an id-expression. */
3188 if (TREE_CODE (decl) == TEMPLATE_DECL
3189 && !DECL_FUNCTION_TEMPLATE_P (decl))
3191 *error_msg = "missing template arguments";
3192 return error_mark_node;
3194 else if (TREE_CODE (decl) == TYPE_DECL
3195 || TREE_CODE (decl) == NAMESPACE_DECL)
3197 *error_msg = "expected primary-expression";
3198 return error_mark_node;
3201 /* If the name resolved to a template parameter, there is no
3202 need to look it up again later. */
3203 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3204 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3206 tree r;
3208 *idk = CP_ID_KIND_NONE;
3209 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3210 decl = TEMPLATE_PARM_DECL (decl);
3211 r = convert_from_reference (DECL_INITIAL (decl));
3213 if (integral_constant_expression_p
3214 && !dependent_type_p (TREE_TYPE (decl))
3215 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3217 if (!allow_non_integral_constant_expression_p)
3218 error ("template parameter %qD of type %qT is not allowed in "
3219 "an integral constant expression because it is not of "
3220 "integral or enumeration type", decl, TREE_TYPE (decl));
3221 *non_integral_constant_expression_p = true;
3223 return r;
3225 else
3227 bool dependent_p;
3229 /* If the declaration was explicitly qualified indicate
3230 that. The semantics of `A::f(3)' are different than
3231 `f(3)' if `f' is virtual. */
3232 *idk = (scope
3233 ? CP_ID_KIND_QUALIFIED
3234 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3235 ? CP_ID_KIND_TEMPLATE_ID
3236 : CP_ID_KIND_UNQUALIFIED));
3239 /* [temp.dep.expr]
3241 An id-expression is type-dependent if it contains an
3242 identifier that was declared with a dependent type.
3244 The standard is not very specific about an id-expression that
3245 names a set of overloaded functions. What if some of them
3246 have dependent types and some of them do not? Presumably,
3247 such a name should be treated as a dependent name. */
3248 /* Assume the name is not dependent. */
3249 dependent_p = false;
3250 if (!processing_template_decl)
3251 /* No names are dependent outside a template. */
3253 else if (TREE_CODE (decl) == CONST_DECL)
3254 /* We don't want to treat enumerators as dependent. */
3256 /* A template-id where the name of the template was not resolved
3257 is definitely dependent. */
3258 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3259 && (identifier_p (TREE_OPERAND (decl, 0))))
3260 dependent_p = true;
3261 /* For anything except an overloaded function, just check its
3262 type. */
3263 else if (!is_overloaded_fn (decl))
3264 dependent_p
3265 = dependent_type_p (TREE_TYPE (decl));
3266 /* For a set of overloaded functions, check each of the
3267 functions. */
3268 else
3270 tree fns = decl;
3272 if (BASELINK_P (fns))
3273 fns = BASELINK_FUNCTIONS (fns);
3275 /* For a template-id, check to see if the template
3276 arguments are dependent. */
3277 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3279 tree args = TREE_OPERAND (fns, 1);
3280 dependent_p = any_dependent_template_arguments_p (args);
3281 /* The functions are those referred to by the
3282 template-id. */
3283 fns = TREE_OPERAND (fns, 0);
3286 /* If there are no dependent template arguments, go through
3287 the overloaded functions. */
3288 while (fns && !dependent_p)
3290 tree fn = OVL_CURRENT (fns);
3292 /* Member functions of dependent classes are
3293 dependent. */
3294 if (TREE_CODE (fn) == FUNCTION_DECL
3295 && type_dependent_expression_p (fn))
3296 dependent_p = true;
3297 else if (TREE_CODE (fn) == TEMPLATE_DECL
3298 && dependent_template_p (fn))
3299 dependent_p = true;
3301 fns = OVL_NEXT (fns);
3305 /* If the name was dependent on a template parameter, we will
3306 resolve the name at instantiation time. */
3307 if (dependent_p)
3309 /* Create a SCOPE_REF for qualified names, if the scope is
3310 dependent. */
3311 if (scope)
3313 if (TYPE_P (scope))
3315 if (address_p && done)
3316 decl = finish_qualified_id_expr (scope, decl,
3317 done, address_p,
3318 template_p,
3319 template_arg_p,
3320 tf_warning_or_error);
3321 else
3323 tree type = NULL_TREE;
3324 if (DECL_P (decl) && !dependent_scope_p (scope))
3325 type = TREE_TYPE (decl);
3326 decl = build_qualified_name (type,
3327 scope,
3328 id_expression,
3329 template_p);
3332 if (TREE_TYPE (decl))
3333 decl = convert_from_reference (decl);
3334 return decl;
3336 /* A TEMPLATE_ID already contains all the information we
3337 need. */
3338 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3339 return id_expression;
3340 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3341 /* If we found a variable, then name lookup during the
3342 instantiation will always resolve to the same VAR_DECL
3343 (or an instantiation thereof). */
3344 if (VAR_P (decl)
3345 || TREE_CODE (decl) == PARM_DECL)
3347 mark_used (decl);
3348 return convert_from_reference (decl);
3350 /* The same is true for FIELD_DECL, but we also need to
3351 make sure that the syntax is correct. */
3352 else if (TREE_CODE (decl) == FIELD_DECL)
3354 /* Since SCOPE is NULL here, this is an unqualified name.
3355 Access checking has been performed during name lookup
3356 already. Turn off checking to avoid duplicate errors. */
3357 push_deferring_access_checks (dk_no_check);
3358 decl = finish_non_static_data_member
3359 (decl, NULL_TREE,
3360 /*qualifying_scope=*/NULL_TREE);
3361 pop_deferring_access_checks ();
3362 return decl;
3364 return id_expression;
3367 if (TREE_CODE (decl) == NAMESPACE_DECL)
3369 error ("use of namespace %qD as expression", decl);
3370 return error_mark_node;
3372 else if (DECL_CLASS_TEMPLATE_P (decl))
3374 error ("use of class template %qT as expression", decl);
3375 return error_mark_node;
3377 else if (TREE_CODE (decl) == TREE_LIST)
3379 /* Ambiguous reference to base members. */
3380 error ("request for member %qD is ambiguous in "
3381 "multiple inheritance lattice", id_expression);
3382 print_candidates (decl);
3383 return error_mark_node;
3386 /* Mark variable-like entities as used. Functions are similarly
3387 marked either below or after overload resolution. */
3388 if ((VAR_P (decl)
3389 || TREE_CODE (decl) == PARM_DECL
3390 || TREE_CODE (decl) == CONST_DECL
3391 || TREE_CODE (decl) == RESULT_DECL)
3392 && !mark_used (decl))
3393 return error_mark_node;
3395 /* Only certain kinds of names are allowed in constant
3396 expression. Template parameters have already
3397 been handled above. */
3398 if (! error_operand_p (decl)
3399 && integral_constant_expression_p
3400 && ! decl_constant_var_p (decl)
3401 && TREE_CODE (decl) != CONST_DECL
3402 && ! builtin_valid_in_constant_expr_p (decl))
3404 if (!allow_non_integral_constant_expression_p)
3406 error ("%qD cannot appear in a constant-expression", decl);
3407 return error_mark_node;
3409 *non_integral_constant_expression_p = true;
3412 tree wrap;
3413 if (VAR_P (decl)
3414 && !cp_unevaluated_operand
3415 && DECL_THREAD_LOCAL_P (decl)
3416 && (wrap = get_tls_wrapper_fn (decl)))
3418 /* Replace an evaluated use of the thread_local variable with
3419 a call to its wrapper. */
3420 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3422 else if (scope)
3424 decl = (adjust_result_of_qualified_name_lookup
3425 (decl, scope, current_nonlambda_class_type()));
3427 if (TREE_CODE (decl) == FUNCTION_DECL)
3428 mark_used (decl);
3430 if (TYPE_P (scope))
3431 decl = finish_qualified_id_expr (scope,
3432 decl,
3433 done,
3434 address_p,
3435 template_p,
3436 template_arg_p,
3437 tf_warning_or_error);
3438 else
3439 decl = convert_from_reference (decl);
3441 else if (TREE_CODE (decl) == FIELD_DECL)
3443 /* Since SCOPE is NULL here, this is an unqualified name.
3444 Access checking has been performed during name lookup
3445 already. Turn off checking to avoid duplicate errors. */
3446 push_deferring_access_checks (dk_no_check);
3447 decl = finish_non_static_data_member (decl, NULL_TREE,
3448 /*qualifying_scope=*/NULL_TREE);
3449 pop_deferring_access_checks ();
3451 else if (is_overloaded_fn (decl))
3453 tree first_fn;
3455 first_fn = get_first_fn (decl);
3456 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3457 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3459 if (!really_overloaded_fn (decl)
3460 && !mark_used (first_fn))
3461 return error_mark_node;
3463 if (!template_arg_p
3464 && TREE_CODE (first_fn) == FUNCTION_DECL
3465 && DECL_FUNCTION_MEMBER_P (first_fn)
3466 && !shared_member_p (decl))
3468 /* A set of member functions. */
3469 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3470 return finish_class_member_access_expr (decl, id_expression,
3471 /*template_p=*/false,
3472 tf_warning_or_error);
3475 decl = baselink_for_fns (decl);
3477 else
3479 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3480 && DECL_CLASS_SCOPE_P (decl))
3482 tree context = context_for_name_lookup (decl);
3483 if (context != current_class_type)
3485 tree path = currently_open_derived_class (context);
3486 perform_or_defer_access_check (TYPE_BINFO (path),
3487 decl, decl,
3488 tf_warning_or_error);
3492 decl = convert_from_reference (decl);
3496 /* Handle references (c++/56130). */
3497 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3498 if (TREE_DEPRECATED (t))
3499 warn_deprecated_use (t, NULL_TREE);
3501 return decl;
3504 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3505 use as a type-specifier. */
3507 tree
3508 finish_typeof (tree expr)
3510 tree type;
3512 if (type_dependent_expression_p (expr))
3514 type = cxx_make_type (TYPEOF_TYPE);
3515 TYPEOF_TYPE_EXPR (type) = expr;
3516 SET_TYPE_STRUCTURAL_EQUALITY (type);
3518 return type;
3521 expr = mark_type_use (expr);
3523 type = unlowered_expr_type (expr);
3525 if (!type || type == unknown_type_node)
3527 error ("type of %qE is unknown", expr);
3528 return error_mark_node;
3531 return type;
3534 /* Implement the __underlying_type keyword: Return the underlying
3535 type of TYPE, suitable for use as a type-specifier. */
3537 tree
3538 finish_underlying_type (tree type)
3540 tree underlying_type;
3542 if (processing_template_decl)
3544 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3545 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3546 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3548 return underlying_type;
3551 complete_type (type);
3553 if (TREE_CODE (type) != ENUMERAL_TYPE)
3555 error ("%qT is not an enumeration type", type);
3556 return error_mark_node;
3559 underlying_type = ENUM_UNDERLYING_TYPE (type);
3561 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3562 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3563 See finish_enum_value_list for details. */
3564 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3565 underlying_type
3566 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3567 TYPE_UNSIGNED (underlying_type));
3569 return underlying_type;
3572 /* Implement the __direct_bases keyword: Return the direct base classes
3573 of type */
3575 tree
3576 calculate_direct_bases (tree type)
3578 vec<tree, va_gc> *vector = make_tree_vector();
3579 tree bases_vec = NULL_TREE;
3580 vec<tree, va_gc> *base_binfos;
3581 tree binfo;
3582 unsigned i;
3584 complete_type (type);
3586 if (!NON_UNION_CLASS_TYPE_P (type))
3587 return make_tree_vec (0);
3589 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3591 /* Virtual bases are initialized first */
3592 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3594 if (BINFO_VIRTUAL_P (binfo))
3596 vec_safe_push (vector, binfo);
3600 /* Now non-virtuals */
3601 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3603 if (!BINFO_VIRTUAL_P (binfo))
3605 vec_safe_push (vector, binfo);
3610 bases_vec = make_tree_vec (vector->length ());
3612 for (i = 0; i < vector->length (); ++i)
3614 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3616 return bases_vec;
3619 /* Implement the __bases keyword: Return the base classes
3620 of type */
3622 /* Find morally non-virtual base classes by walking binfo hierarchy */
3623 /* Virtual base classes are handled separately in finish_bases */
3625 static tree
3626 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3628 /* Don't walk bases of virtual bases */
3629 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3632 static tree
3633 dfs_calculate_bases_post (tree binfo, void *data_)
3635 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3636 if (!BINFO_VIRTUAL_P (binfo))
3638 vec_safe_push (*data, BINFO_TYPE (binfo));
3640 return NULL_TREE;
3643 /* Calculates the morally non-virtual base classes of a class */
3644 static vec<tree, va_gc> *
3645 calculate_bases_helper (tree type)
3647 vec<tree, va_gc> *vector = make_tree_vector();
3649 /* Now add non-virtual base classes in order of construction */
3650 dfs_walk_all (TYPE_BINFO (type),
3651 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3652 return vector;
3655 tree
3656 calculate_bases (tree type)
3658 vec<tree, va_gc> *vector = make_tree_vector();
3659 tree bases_vec = NULL_TREE;
3660 unsigned i;
3661 vec<tree, va_gc> *vbases;
3662 vec<tree, va_gc> *nonvbases;
3663 tree binfo;
3665 complete_type (type);
3667 if (!NON_UNION_CLASS_TYPE_P (type))
3668 return make_tree_vec (0);
3670 /* First go through virtual base classes */
3671 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3672 vec_safe_iterate (vbases, i, &binfo); i++)
3674 vec<tree, va_gc> *vbase_bases;
3675 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3676 vec_safe_splice (vector, vbase_bases);
3677 release_tree_vector (vbase_bases);
3680 /* Now for the non-virtual bases */
3681 nonvbases = calculate_bases_helper (type);
3682 vec_safe_splice (vector, nonvbases);
3683 release_tree_vector (nonvbases);
3685 /* Last element is entire class, so don't copy */
3686 bases_vec = make_tree_vec (vector->length () - 1);
3688 for (i = 0; i < vector->length () - 1; ++i)
3690 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3692 release_tree_vector (vector);
3693 return bases_vec;
3696 tree
3697 finish_bases (tree type, bool direct)
3699 tree bases = NULL_TREE;
3701 if (!processing_template_decl)
3703 /* Parameter packs can only be used in templates */
3704 error ("Parameter pack __bases only valid in template declaration");
3705 return error_mark_node;
3708 bases = cxx_make_type (BASES);
3709 BASES_TYPE (bases) = type;
3710 BASES_DIRECT (bases) = direct;
3711 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3713 return bases;
3716 /* Perform C++-specific checks for __builtin_offsetof before calling
3717 fold_offsetof. */
3719 tree
3720 finish_offsetof (tree expr)
3722 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3724 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3725 TREE_OPERAND (expr, 2));
3726 return error_mark_node;
3728 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3729 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3730 || TREE_TYPE (expr) == unknown_type_node)
3732 if (INDIRECT_REF_P (expr))
3733 error ("second operand of %<offsetof%> is neither a single "
3734 "identifier nor a sequence of member accesses and "
3735 "array references");
3736 else
3738 if (TREE_CODE (expr) == COMPONENT_REF
3739 || TREE_CODE (expr) == COMPOUND_EXPR)
3740 expr = TREE_OPERAND (expr, 1);
3741 error ("cannot apply %<offsetof%> to member function %qD", expr);
3743 return error_mark_node;
3745 if (REFERENCE_REF_P (expr))
3746 expr = TREE_OPERAND (expr, 0);
3747 if (TREE_CODE (expr) == COMPONENT_REF)
3749 tree object = TREE_OPERAND (expr, 0);
3750 if (!complete_type_or_else (TREE_TYPE (object), object))
3751 return error_mark_node;
3753 return fold_offsetof (expr);
3756 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3757 function is broken out from the above for the benefit of the tree-ssa
3758 project. */
3760 void
3761 simplify_aggr_init_expr (tree *tp)
3763 tree aggr_init_expr = *tp;
3765 /* Form an appropriate CALL_EXPR. */
3766 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3767 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3768 tree type = TREE_TYPE (slot);
3770 tree call_expr;
3771 enum style_t { ctor, arg, pcc } style;
3773 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3774 style = ctor;
3775 #ifdef PCC_STATIC_STRUCT_RETURN
3776 else if (1)
3777 style = pcc;
3778 #endif
3779 else
3781 gcc_assert (TREE_ADDRESSABLE (type));
3782 style = arg;
3785 call_expr = build_call_array_loc (input_location,
3786 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3788 aggr_init_expr_nargs (aggr_init_expr),
3789 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3790 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3792 if (style == ctor)
3794 /* Replace the first argument to the ctor with the address of the
3795 slot. */
3796 cxx_mark_addressable (slot);
3797 CALL_EXPR_ARG (call_expr, 0) =
3798 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3800 else if (style == arg)
3802 /* Just mark it addressable here, and leave the rest to
3803 expand_call{,_inline}. */
3804 cxx_mark_addressable (slot);
3805 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3806 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3808 else if (style == pcc)
3810 /* If we're using the non-reentrant PCC calling convention, then we
3811 need to copy the returned value out of the static buffer into the
3812 SLOT. */
3813 push_deferring_access_checks (dk_no_check);
3814 call_expr = build_aggr_init (slot, call_expr,
3815 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3816 tf_warning_or_error);
3817 pop_deferring_access_checks ();
3818 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3821 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3823 tree init = build_zero_init (type, NULL_TREE,
3824 /*static_storage_p=*/false);
3825 init = build2 (INIT_EXPR, void_type_node, slot, init);
3826 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3827 init, call_expr);
3830 *tp = call_expr;
3833 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3835 void
3836 emit_associated_thunks (tree fn)
3838 /* When we use vcall offsets, we emit thunks with the virtual
3839 functions to which they thunk. The whole point of vcall offsets
3840 is so that you can know statically the entire set of thunks that
3841 will ever be needed for a given virtual function, thereby
3842 enabling you to output all the thunks with the function itself. */
3843 if (DECL_VIRTUAL_P (fn)
3844 /* Do not emit thunks for extern template instantiations. */
3845 && ! DECL_REALLY_EXTERN (fn))
3847 tree thunk;
3849 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3851 if (!THUNK_ALIAS (thunk))
3853 use_thunk (thunk, /*emit_p=*/1);
3854 if (DECL_RESULT_THUNK_P (thunk))
3856 tree probe;
3858 for (probe = DECL_THUNKS (thunk);
3859 probe; probe = DECL_CHAIN (probe))
3860 use_thunk (probe, /*emit_p=*/1);
3863 else
3864 gcc_assert (!DECL_THUNKS (thunk));
3869 /* Returns true iff FUN is an instantiation of a constexpr function
3870 template. */
3872 static inline bool
3873 is_instantiation_of_constexpr (tree fun)
3875 return (DECL_TEMPLOID_INSTANTIATION (fun)
3876 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3877 (DECL_TI_TEMPLATE (fun))));
3880 /* Generate RTL for FN. */
3882 bool
3883 expand_or_defer_fn_1 (tree fn)
3885 /* When the parser calls us after finishing the body of a template
3886 function, we don't really want to expand the body. */
3887 if (processing_template_decl)
3889 /* Normally, collection only occurs in rest_of_compilation. So,
3890 if we don't collect here, we never collect junk generated
3891 during the processing of templates until we hit a
3892 non-template function. It's not safe to do this inside a
3893 nested class, though, as the parser may have local state that
3894 is not a GC root. */
3895 if (!function_depth)
3896 ggc_collect ();
3897 return false;
3900 gcc_assert (DECL_SAVED_TREE (fn));
3902 /* We make a decision about linkage for these functions at the end
3903 of the compilation. Until that point, we do not want the back
3904 end to output them -- but we do want it to see the bodies of
3905 these functions so that it can inline them as appropriate. */
3906 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3908 if (DECL_INTERFACE_KNOWN (fn))
3909 /* We've already made a decision as to how this function will
3910 be handled. */;
3911 else if (!at_eof)
3913 DECL_EXTERNAL (fn) = 1;
3914 DECL_NOT_REALLY_EXTERN (fn) = 1;
3915 note_vague_linkage_fn (fn);
3916 /* A non-template inline function with external linkage will
3917 always be COMDAT. As we must eventually determine the
3918 linkage of all functions, and as that causes writes to
3919 the data mapped in from the PCH file, it's advantageous
3920 to mark the functions at this point. */
3921 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3923 /* This function must have external linkage, as
3924 otherwise DECL_INTERFACE_KNOWN would have been
3925 set. */
3926 gcc_assert (TREE_PUBLIC (fn));
3927 comdat_linkage (fn);
3928 DECL_INTERFACE_KNOWN (fn) = 1;
3931 else
3932 import_export_decl (fn);
3934 /* If the user wants us to keep all inline functions, then mark
3935 this function as needed so that finish_file will make sure to
3936 output it later. Similarly, all dllexport'd functions must
3937 be emitted; there may be callers in other DLLs. */
3938 if ((flag_keep_inline_functions
3939 && DECL_DECLARED_INLINE_P (fn)
3940 && !DECL_REALLY_EXTERN (fn))
3941 || (flag_keep_inline_dllexport
3942 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
3944 mark_needed (fn);
3945 DECL_EXTERNAL (fn) = 0;
3949 /* If this is a constructor or destructor body, we have to clone
3950 it. */
3951 if (maybe_clone_body (fn))
3953 /* We don't want to process FN again, so pretend we've written
3954 it out, even though we haven't. */
3955 TREE_ASM_WRITTEN (fn) = 1;
3956 /* If this is an instantiation of a constexpr function, keep
3957 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3958 if (!is_instantiation_of_constexpr (fn))
3959 DECL_SAVED_TREE (fn) = NULL_TREE;
3960 return false;
3963 /* There's no reason to do any of the work here if we're only doing
3964 semantic analysis; this code just generates RTL. */
3965 if (flag_syntax_only)
3966 return false;
3968 return true;
3971 void
3972 expand_or_defer_fn (tree fn)
3974 if (expand_or_defer_fn_1 (fn))
3976 function_depth++;
3978 /* Expand or defer, at the whim of the compilation unit manager. */
3979 cgraph_finalize_function (fn, function_depth > 1);
3980 emit_associated_thunks (fn);
3982 function_depth--;
3986 struct nrv_data
3988 tree var;
3989 tree result;
3990 hash_table <pointer_hash <tree_node> > visited;
3993 /* Helper function for walk_tree, used by finalize_nrv below. */
3995 static tree
3996 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3998 struct nrv_data *dp = (struct nrv_data *)data;
3999 tree_node **slot;
4001 /* No need to walk into types. There wouldn't be any need to walk into
4002 non-statements, except that we have to consider STMT_EXPRs. */
4003 if (TYPE_P (*tp))
4004 *walk_subtrees = 0;
4005 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4006 but differs from using NULL_TREE in that it indicates that we care
4007 about the value of the RESULT_DECL. */
4008 else if (TREE_CODE (*tp) == RETURN_EXPR)
4009 TREE_OPERAND (*tp, 0) = dp->result;
4010 /* Change all cleanups for the NRV to only run when an exception is
4011 thrown. */
4012 else if (TREE_CODE (*tp) == CLEANUP_STMT
4013 && CLEANUP_DECL (*tp) == dp->var)
4014 CLEANUP_EH_ONLY (*tp) = 1;
4015 /* Replace the DECL_EXPR for the NRV with an initialization of the
4016 RESULT_DECL, if needed. */
4017 else if (TREE_CODE (*tp) == DECL_EXPR
4018 && DECL_EXPR_DECL (*tp) == dp->var)
4020 tree init;
4021 if (DECL_INITIAL (dp->var)
4022 && DECL_INITIAL (dp->var) != error_mark_node)
4023 init = build2 (INIT_EXPR, void_type_node, dp->result,
4024 DECL_INITIAL (dp->var));
4025 else
4026 init = build_empty_stmt (EXPR_LOCATION (*tp));
4027 DECL_INITIAL (dp->var) = NULL_TREE;
4028 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4029 *tp = init;
4031 /* And replace all uses of the NRV with the RESULT_DECL. */
4032 else if (*tp == dp->var)
4033 *tp = dp->result;
4035 /* Avoid walking into the same tree more than once. Unfortunately, we
4036 can't just use walk_tree_without duplicates because it would only call
4037 us for the first occurrence of dp->var in the function body. */
4038 slot = dp->visited.find_slot (*tp, INSERT);
4039 if (*slot)
4040 *walk_subtrees = 0;
4041 else
4042 *slot = *tp;
4044 /* Keep iterating. */
4045 return NULL_TREE;
4048 /* Called from finish_function to implement the named return value
4049 optimization by overriding all the RETURN_EXPRs and pertinent
4050 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4051 RESULT_DECL for the function. */
4053 void
4054 finalize_nrv (tree *tp, tree var, tree result)
4056 struct nrv_data data;
4058 /* Copy name from VAR to RESULT. */
4059 DECL_NAME (result) = DECL_NAME (var);
4060 /* Don't forget that we take its address. */
4061 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4062 /* Finally set DECL_VALUE_EXPR to avoid assigning
4063 a stack slot at -O0 for the original var and debug info
4064 uses RESULT location for VAR. */
4065 SET_DECL_VALUE_EXPR (var, result);
4066 DECL_HAS_VALUE_EXPR_P (var) = 1;
4068 data.var = var;
4069 data.result = result;
4070 data.visited.create (37);
4071 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4072 data.visited.dispose ();
4075 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4077 bool
4078 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4079 bool need_copy_ctor, bool need_copy_assignment,
4080 bool need_dtor)
4082 int save_errorcount = errorcount;
4083 tree info, t;
4085 /* Always allocate 3 elements for simplicity. These are the
4086 function decls for the ctor, dtor, and assignment op.
4087 This layout is known to the three lang hooks,
4088 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4089 and cxx_omp_clause_assign_op. */
4090 info = make_tree_vec (3);
4091 CP_OMP_CLAUSE_INFO (c) = info;
4093 if (need_default_ctor || need_copy_ctor)
4095 if (need_default_ctor)
4096 t = get_default_ctor (type);
4097 else
4098 t = get_copy_ctor (type, tf_warning_or_error);
4100 if (t && !trivial_fn_p (t))
4101 TREE_VEC_ELT (info, 0) = t;
4104 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4105 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4107 if (need_copy_assignment)
4109 t = get_copy_assign (type);
4111 if (t && !trivial_fn_p (t))
4112 TREE_VEC_ELT (info, 2) = t;
4115 return errorcount != save_errorcount;
4118 /* Helper function for handle_omp_array_sections. Called recursively
4119 to handle multiple array-section-subscripts. C is the clause,
4120 T current expression (initially OMP_CLAUSE_DECL), which is either
4121 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4122 expression if specified, TREE_VALUE length expression if specified,
4123 TREE_CHAIN is what it has been specified after, or some decl.
4124 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4125 set to true if any of the array-section-subscript could have length
4126 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4127 first array-section-subscript which is known not to have length
4128 of one. Given say:
4129 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4130 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4131 all are or may have length of 1, array-section-subscript [:2] is the
4132 first one knonwn not to have length 1. For array-section-subscript
4133 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4134 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4135 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4136 case though, as some lengths could be zero. */
4138 static tree
4139 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4140 bool &maybe_zero_len, unsigned int &first_non_one)
4142 tree ret, low_bound, length, type;
4143 if (TREE_CODE (t) != TREE_LIST)
4145 if (error_operand_p (t))
4146 return error_mark_node;
4147 if (type_dependent_expression_p (t))
4148 return NULL_TREE;
4149 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4151 if (processing_template_decl)
4152 return NULL_TREE;
4153 if (DECL_P (t))
4154 error_at (OMP_CLAUSE_LOCATION (c),
4155 "%qD is not a variable in %qs clause", t,
4156 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4157 else
4158 error_at (OMP_CLAUSE_LOCATION (c),
4159 "%qE is not a variable in %qs clause", t,
4160 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4161 return error_mark_node;
4163 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4164 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4166 error_at (OMP_CLAUSE_LOCATION (c),
4167 "%qD is threadprivate variable in %qs clause", t,
4168 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4169 return error_mark_node;
4171 t = convert_from_reference (t);
4172 return t;
4175 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4176 maybe_zero_len, first_non_one);
4177 if (ret == error_mark_node || ret == NULL_TREE)
4178 return ret;
4180 type = TREE_TYPE (ret);
4181 low_bound = TREE_PURPOSE (t);
4182 length = TREE_VALUE (t);
4183 if ((low_bound && type_dependent_expression_p (low_bound))
4184 || (length && type_dependent_expression_p (length)))
4185 return NULL_TREE;
4187 if (low_bound == error_mark_node || length == error_mark_node)
4188 return error_mark_node;
4190 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4192 error_at (OMP_CLAUSE_LOCATION (c),
4193 "low bound %qE of array section does not have integral type",
4194 low_bound);
4195 return error_mark_node;
4197 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4199 error_at (OMP_CLAUSE_LOCATION (c),
4200 "length %qE of array section does not have integral type",
4201 length);
4202 return error_mark_node;
4204 if (low_bound
4205 && TREE_CODE (low_bound) == INTEGER_CST
4206 && TYPE_PRECISION (TREE_TYPE (low_bound))
4207 > TYPE_PRECISION (sizetype))
4208 low_bound = fold_convert (sizetype, low_bound);
4209 if (length
4210 && TREE_CODE (length) == INTEGER_CST
4211 && TYPE_PRECISION (TREE_TYPE (length))
4212 > TYPE_PRECISION (sizetype))
4213 length = fold_convert (sizetype, length);
4214 if (low_bound == NULL_TREE)
4215 low_bound = integer_zero_node;
4217 if (length != NULL_TREE)
4219 if (!integer_nonzerop (length))
4220 maybe_zero_len = true;
4221 if (first_non_one == types.length ()
4222 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4223 first_non_one++;
4225 if (TREE_CODE (type) == ARRAY_TYPE)
4227 if (length == NULL_TREE
4228 && (TYPE_DOMAIN (type) == NULL_TREE
4229 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4231 error_at (OMP_CLAUSE_LOCATION (c),
4232 "for unknown bound array type length expression must "
4233 "be specified");
4234 return error_mark_node;
4236 if (TREE_CODE (low_bound) == INTEGER_CST
4237 && tree_int_cst_sgn (low_bound) == -1)
4239 error_at (OMP_CLAUSE_LOCATION (c),
4240 "negative low bound in array section in %qs clause",
4241 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4242 return error_mark_node;
4244 if (length != NULL_TREE
4245 && TREE_CODE (length) == INTEGER_CST
4246 && tree_int_cst_sgn (length) == -1)
4248 error_at (OMP_CLAUSE_LOCATION (c),
4249 "negative length in array section in %qs clause",
4250 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4251 return error_mark_node;
4253 if (TYPE_DOMAIN (type)
4254 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4255 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4256 == INTEGER_CST)
4258 tree size = size_binop (PLUS_EXPR,
4259 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4260 size_one_node);
4261 if (TREE_CODE (low_bound) == INTEGER_CST)
4263 if (tree_int_cst_lt (size, low_bound))
4265 error_at (OMP_CLAUSE_LOCATION (c),
4266 "low bound %qE above array section size "
4267 "in %qs clause", low_bound,
4268 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4269 return error_mark_node;
4271 if (tree_int_cst_equal (size, low_bound))
4272 maybe_zero_len = true;
4273 else if (length == NULL_TREE
4274 && first_non_one == types.length ()
4275 && tree_int_cst_equal
4276 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4277 low_bound))
4278 first_non_one++;
4280 else if (length == NULL_TREE)
4282 maybe_zero_len = true;
4283 if (first_non_one == types.length ())
4284 first_non_one++;
4286 if (length && TREE_CODE (length) == INTEGER_CST)
4288 if (tree_int_cst_lt (size, length))
4290 error_at (OMP_CLAUSE_LOCATION (c),
4291 "length %qE above array section size "
4292 "in %qs clause", length,
4293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4294 return error_mark_node;
4296 if (TREE_CODE (low_bound) == INTEGER_CST)
4298 tree lbpluslen
4299 = size_binop (PLUS_EXPR,
4300 fold_convert (sizetype, low_bound),
4301 fold_convert (sizetype, length));
4302 if (TREE_CODE (lbpluslen) == INTEGER_CST
4303 && tree_int_cst_lt (size, lbpluslen))
4305 error_at (OMP_CLAUSE_LOCATION (c),
4306 "high bound %qE above array section size "
4307 "in %qs clause", lbpluslen,
4308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4309 return error_mark_node;
4314 else if (length == NULL_TREE)
4316 maybe_zero_len = true;
4317 if (first_non_one == types.length ())
4318 first_non_one++;
4321 /* For [lb:] we will need to evaluate lb more than once. */
4322 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4324 tree lb = cp_save_expr (low_bound);
4325 if (lb != low_bound)
4327 TREE_PURPOSE (t) = lb;
4328 low_bound = lb;
4332 else if (TREE_CODE (type) == POINTER_TYPE)
4334 if (length == NULL_TREE)
4336 error_at (OMP_CLAUSE_LOCATION (c),
4337 "for pointer type length expression must be specified");
4338 return error_mark_node;
4340 /* If there is a pointer type anywhere but in the very first
4341 array-section-subscript, the array section can't be contiguous. */
4342 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4343 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4345 error_at (OMP_CLAUSE_LOCATION (c),
4346 "array section is not contiguous in %qs clause",
4347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4348 return error_mark_node;
4351 else
4353 error_at (OMP_CLAUSE_LOCATION (c),
4354 "%qE does not have pointer or array type", ret);
4355 return error_mark_node;
4357 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4358 types.safe_push (TREE_TYPE (ret));
4359 /* We will need to evaluate lb more than once. */
4360 tree lb = cp_save_expr (low_bound);
4361 if (lb != low_bound)
4363 TREE_PURPOSE (t) = lb;
4364 low_bound = lb;
4366 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4367 return ret;
4370 /* Handle array sections for clause C. */
4372 static bool
4373 handle_omp_array_sections (tree c)
4375 bool maybe_zero_len = false;
4376 unsigned int first_non_one = 0;
4377 auto_vec<tree> types;
4378 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4379 maybe_zero_len, first_non_one);
4380 if (first == error_mark_node)
4381 return true;
4382 if (first == NULL_TREE)
4383 return false;
4384 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4386 tree t = OMP_CLAUSE_DECL (c);
4387 tree tem = NULL_TREE;
4388 if (processing_template_decl)
4389 return false;
4390 /* Need to evaluate side effects in the length expressions
4391 if any. */
4392 while (TREE_CODE (t) == TREE_LIST)
4394 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4396 if (tem == NULL_TREE)
4397 tem = TREE_VALUE (t);
4398 else
4399 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4400 TREE_VALUE (t), tem);
4402 t = TREE_CHAIN (t);
4404 if (tem)
4405 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4406 OMP_CLAUSE_DECL (c) = first;
4408 else
4410 unsigned int num = types.length (), i;
4411 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4412 tree condition = NULL_TREE;
4414 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4415 maybe_zero_len = true;
4416 if (processing_template_decl && maybe_zero_len)
4417 return false;
4419 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4420 t = TREE_CHAIN (t))
4422 tree low_bound = TREE_PURPOSE (t);
4423 tree length = TREE_VALUE (t);
4425 i--;
4426 if (low_bound
4427 && TREE_CODE (low_bound) == INTEGER_CST
4428 && TYPE_PRECISION (TREE_TYPE (low_bound))
4429 > TYPE_PRECISION (sizetype))
4430 low_bound = fold_convert (sizetype, low_bound);
4431 if (length
4432 && TREE_CODE (length) == INTEGER_CST
4433 && TYPE_PRECISION (TREE_TYPE (length))
4434 > TYPE_PRECISION (sizetype))
4435 length = fold_convert (sizetype, length);
4436 if (low_bound == NULL_TREE)
4437 low_bound = integer_zero_node;
4438 if (!maybe_zero_len && i > first_non_one)
4440 if (integer_nonzerop (low_bound))
4441 goto do_warn_noncontiguous;
4442 if (length != NULL_TREE
4443 && TREE_CODE (length) == INTEGER_CST
4444 && TYPE_DOMAIN (types[i])
4445 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4446 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4447 == INTEGER_CST)
4449 tree size;
4450 size = size_binop (PLUS_EXPR,
4451 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4452 size_one_node);
4453 if (!tree_int_cst_equal (length, size))
4455 do_warn_noncontiguous:
4456 error_at (OMP_CLAUSE_LOCATION (c),
4457 "array section is not contiguous in %qs "
4458 "clause",
4459 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4460 return true;
4463 if (!processing_template_decl
4464 && length != NULL_TREE
4465 && TREE_SIDE_EFFECTS (length))
4467 if (side_effects == NULL_TREE)
4468 side_effects = length;
4469 else
4470 side_effects = build2 (COMPOUND_EXPR,
4471 TREE_TYPE (side_effects),
4472 length, side_effects);
4475 else if (processing_template_decl)
4476 continue;
4477 else
4479 tree l;
4481 if (i > first_non_one && length && integer_nonzerop (length))
4482 continue;
4483 if (length)
4484 l = fold_convert (sizetype, length);
4485 else
4487 l = size_binop (PLUS_EXPR,
4488 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4489 size_one_node);
4490 l = size_binop (MINUS_EXPR, l,
4491 fold_convert (sizetype, low_bound));
4493 if (i > first_non_one)
4495 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4496 size_zero_node);
4497 if (condition == NULL_TREE)
4498 condition = l;
4499 else
4500 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4501 l, condition);
4503 else if (size == NULL_TREE)
4505 size = size_in_bytes (TREE_TYPE (types[i]));
4506 size = size_binop (MULT_EXPR, size, l);
4507 if (condition)
4508 size = fold_build3 (COND_EXPR, sizetype, condition,
4509 size, size_zero_node);
4511 else
4512 size = size_binop (MULT_EXPR, size, l);
4515 if (!processing_template_decl)
4517 if (side_effects)
4518 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4519 OMP_CLAUSE_DECL (c) = first;
4520 OMP_CLAUSE_SIZE (c) = size;
4521 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4522 return false;
4523 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4524 OMP_CLAUSE_MAP);
4525 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4526 if (!cxx_mark_addressable (t))
4527 return false;
4528 OMP_CLAUSE_DECL (c2) = t;
4529 t = build_fold_addr_expr (first);
4530 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4531 ptrdiff_type_node, t);
4532 tree ptr = OMP_CLAUSE_DECL (c2);
4533 ptr = convert_from_reference (ptr);
4534 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4535 ptr = build_fold_addr_expr (ptr);
4536 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4537 ptrdiff_type_node, t,
4538 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4539 ptrdiff_type_node, ptr));
4540 OMP_CLAUSE_SIZE (c2) = t;
4541 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4542 OMP_CLAUSE_CHAIN (c) = c2;
4543 ptr = OMP_CLAUSE_DECL (c2);
4544 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4545 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4547 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4548 OMP_CLAUSE_MAP);
4549 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4550 OMP_CLAUSE_DECL (c3) = ptr;
4551 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4552 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4553 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4554 OMP_CLAUSE_CHAIN (c2) = c3;
4558 return false;
4561 /* Return identifier to look up for omp declare reduction. */
4563 tree
4564 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4566 const char *p = NULL;
4567 const char *m = NULL;
4568 switch (reduction_code)
4570 case PLUS_EXPR:
4571 case MULT_EXPR:
4572 case MINUS_EXPR:
4573 case BIT_AND_EXPR:
4574 case BIT_XOR_EXPR:
4575 case BIT_IOR_EXPR:
4576 case TRUTH_ANDIF_EXPR:
4577 case TRUTH_ORIF_EXPR:
4578 reduction_id = ansi_opname (reduction_code);
4579 break;
4580 case MIN_EXPR:
4581 p = "min";
4582 break;
4583 case MAX_EXPR:
4584 p = "max";
4585 break;
4586 default:
4587 break;
4590 if (p == NULL)
4592 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4593 return error_mark_node;
4594 p = IDENTIFIER_POINTER (reduction_id);
4597 if (type != NULL_TREE)
4598 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4600 const char prefix[] = "omp declare reduction ";
4601 size_t lenp = sizeof (prefix);
4602 if (strncmp (p, prefix, lenp - 1) == 0)
4603 lenp = 1;
4604 size_t len = strlen (p);
4605 size_t lenm = m ? strlen (m) + 1 : 0;
4606 char *name = XALLOCAVEC (char, lenp + len + lenm);
4607 if (lenp > 1)
4608 memcpy (name, prefix, lenp - 1);
4609 memcpy (name + lenp - 1, p, len + 1);
4610 if (m)
4612 name[lenp + len - 1] = '~';
4613 memcpy (name + lenp + len, m, lenm);
4615 return get_identifier (name);
4618 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4619 FUNCTION_DECL or NULL_TREE if not found. */
4621 static tree
4622 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4623 vec<tree> *ambiguousp)
4625 tree orig_id = id;
4626 tree baselink = NULL_TREE;
4627 if (identifier_p (id))
4629 cp_id_kind idk;
4630 bool nonint_cst_expression_p;
4631 const char *error_msg;
4632 id = omp_reduction_id (ERROR_MARK, id, type);
4633 tree decl = lookup_name (id);
4634 if (decl == NULL_TREE)
4635 decl = error_mark_node;
4636 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4637 &nonint_cst_expression_p, false, true, false,
4638 false, &error_msg, loc);
4639 if (idk == CP_ID_KIND_UNQUALIFIED
4640 && identifier_p (id))
4642 vec<tree, va_gc> *args = NULL;
4643 vec_safe_push (args, build_reference_type (type));
4644 id = perform_koenig_lookup (id, args, tf_none);
4647 else if (TREE_CODE (id) == SCOPE_REF)
4648 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4649 omp_reduction_id (ERROR_MARK,
4650 TREE_OPERAND (id, 1),
4651 type),
4652 false, false);
4653 tree fns = id;
4654 if (id && is_overloaded_fn (id))
4655 id = get_fns (id);
4656 for (; id; id = OVL_NEXT (id))
4658 tree fndecl = OVL_CURRENT (id);
4659 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4661 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4662 if (same_type_p (TREE_TYPE (argtype), type))
4663 break;
4666 if (id && BASELINK_P (fns))
4668 if (baselinkp)
4669 *baselinkp = fns;
4670 else
4671 baselink = fns;
4673 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4675 vec<tree> ambiguous = vNULL;
4676 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4677 unsigned int ix;
4678 if (ambiguousp == NULL)
4679 ambiguousp = &ambiguous;
4680 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4682 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4683 baselinkp ? baselinkp : &baselink,
4684 ambiguousp);
4685 if (id == NULL_TREE)
4686 continue;
4687 if (!ambiguousp->is_empty ())
4688 ambiguousp->safe_push (id);
4689 else if (ret != NULL_TREE)
4691 ambiguousp->safe_push (ret);
4692 ambiguousp->safe_push (id);
4693 ret = NULL_TREE;
4695 else
4696 ret = id;
4698 if (ambiguousp != &ambiguous)
4699 return ret;
4700 if (!ambiguous.is_empty ())
4702 const char *str = _("candidates are:");
4703 unsigned int idx;
4704 tree udr;
4705 error_at (loc, "user defined reduction lookup is ambiguous");
4706 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4708 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4709 if (idx == 0)
4710 str = get_spaces (str);
4712 ambiguous.release ();
4713 ret = error_mark_node;
4714 baselink = NULL_TREE;
4716 id = ret;
4718 if (id && baselink)
4719 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4720 id, id, tf_warning_or_error);
4721 return id;
4724 /* Helper function for cp_parser_omp_declare_reduction_exprs
4725 and tsubst_omp_udr.
4726 Remove CLEANUP_STMT for data (omp_priv variable).
4727 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4728 DECL_EXPR. */
4730 tree
4731 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4733 if (TYPE_P (*tp))
4734 *walk_subtrees = 0;
4735 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4736 *tp = CLEANUP_BODY (*tp);
4737 else if (TREE_CODE (*tp) == DECL_EXPR)
4739 tree decl = DECL_EXPR_DECL (*tp);
4740 if (!processing_template_decl
4741 && decl == (tree) data
4742 && DECL_INITIAL (decl)
4743 && DECL_INITIAL (decl) != error_mark_node)
4745 tree list = NULL_TREE;
4746 append_to_statement_list_force (*tp, &list);
4747 tree init_expr = build2 (INIT_EXPR, void_type_node,
4748 decl, DECL_INITIAL (decl));
4749 DECL_INITIAL (decl) = NULL_TREE;
4750 append_to_statement_list_force (init_expr, &list);
4751 *tp = list;
4754 return NULL_TREE;
4757 /* Data passed from cp_check_omp_declare_reduction to
4758 cp_check_omp_declare_reduction_r. */
4760 struct cp_check_omp_declare_reduction_data
4762 location_t loc;
4763 tree stmts[7];
4764 bool combiner_p;
4767 /* Helper function for cp_check_omp_declare_reduction, called via
4768 cp_walk_tree. */
4770 static tree
4771 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4773 struct cp_check_omp_declare_reduction_data *udr_data
4774 = (struct cp_check_omp_declare_reduction_data *) data;
4775 if (SSA_VAR_P (*tp)
4776 && !DECL_ARTIFICIAL (*tp)
4777 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4778 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4780 location_t loc = udr_data->loc;
4781 if (udr_data->combiner_p)
4782 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4783 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4784 *tp);
4785 else
4786 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4787 "to variable %qD which is not %<omp_priv%> nor "
4788 "%<omp_orig%>",
4789 *tp);
4790 return *tp;
4792 return NULL_TREE;
4795 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4797 void
4798 cp_check_omp_declare_reduction (tree udr)
4800 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4801 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4802 type = TREE_TYPE (type);
4803 int i;
4804 location_t loc = DECL_SOURCE_LOCATION (udr);
4806 if (type == error_mark_node)
4807 return;
4808 if (ARITHMETIC_TYPE_P (type))
4810 static enum tree_code predef_codes[]
4811 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4812 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4813 for (i = 0; i < 8; i++)
4815 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4816 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4817 const char *n2 = IDENTIFIER_POINTER (id);
4818 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4819 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4820 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4821 break;
4824 if (i == 8
4825 && TREE_CODE (type) != COMPLEX_EXPR)
4827 const char prefix_minmax[] = "omp declare reduction m";
4828 size_t prefix_size = sizeof (prefix_minmax) - 1;
4829 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4830 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4831 prefix_minmax, prefix_size) == 0
4832 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4833 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4834 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4835 i = 0;
4837 if (i < 8)
4839 error_at (loc, "predeclared arithmetic type %qT in "
4840 "%<#pragma omp declare reduction%>", type);
4841 return;
4844 else if (TREE_CODE (type) == FUNCTION_TYPE
4845 || TREE_CODE (type) == METHOD_TYPE
4846 || TREE_CODE (type) == ARRAY_TYPE)
4848 error_at (loc, "function or array type %qT in "
4849 "%<#pragma omp declare reduction%>", type);
4850 return;
4852 else if (TREE_CODE (type) == REFERENCE_TYPE)
4854 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4855 type);
4856 return;
4858 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4860 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4861 "%<#pragma omp declare reduction%>", type);
4862 return;
4865 tree body = DECL_SAVED_TREE (udr);
4866 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4867 return;
4869 tree_stmt_iterator tsi;
4870 struct cp_check_omp_declare_reduction_data data;
4871 memset (data.stmts, 0, sizeof data.stmts);
4872 for (i = 0, tsi = tsi_start (body);
4873 i < 7 && !tsi_end_p (tsi);
4874 i++, tsi_next (&tsi))
4875 data.stmts[i] = tsi_stmt (tsi);
4876 data.loc = loc;
4877 gcc_assert (tsi_end_p (tsi));
4878 if (i >= 3)
4880 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4881 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4882 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4883 return;
4884 data.combiner_p = true;
4885 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4886 &data, NULL))
4887 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4889 if (i >= 6)
4891 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4892 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4893 data.combiner_p = false;
4894 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4895 &data, NULL)
4896 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4897 cp_check_omp_declare_reduction_r, &data, NULL))
4898 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4899 if (i == 7)
4900 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4904 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4905 an inline call. But, remap
4906 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4907 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4909 static tree
4910 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4911 tree decl, tree placeholder)
4913 copy_body_data id;
4914 struct pointer_map_t *decl_map = pointer_map_create ();
4916 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
4917 *pointer_map_insert (decl_map, omp_decl2) = decl;
4918 memset (&id, 0, sizeof (id));
4919 id.src_fn = DECL_CONTEXT (omp_decl1);
4920 id.dst_fn = current_function_decl;
4921 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4922 id.decl_map = decl_map;
4924 id.copy_decl = copy_decl_no_change;
4925 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4926 id.transform_new_cfg = true;
4927 id.transform_return_to_modify = false;
4928 id.transform_lang_insert_block = NULL;
4929 id.eh_lp_nr = 0;
4930 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
4931 pointer_map_destroy (decl_map);
4932 return stmt;
4935 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
4936 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
4938 static tree
4939 find_omp_placeholder_r (tree *tp, int *, void *data)
4941 if (*tp == (tree) data)
4942 return *tp;
4943 return NULL_TREE;
4946 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
4947 Return true if there is some error and the clause should be removed. */
4949 static bool
4950 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
4952 tree t = OMP_CLAUSE_DECL (c);
4953 bool predefined = false;
4954 tree type = TREE_TYPE (t);
4955 if (TREE_CODE (type) == REFERENCE_TYPE)
4956 type = TREE_TYPE (type);
4957 if (ARITHMETIC_TYPE_P (type))
4958 switch (OMP_CLAUSE_REDUCTION_CODE (c))
4960 case PLUS_EXPR:
4961 case MULT_EXPR:
4962 case MINUS_EXPR:
4963 predefined = true;
4964 break;
4965 case MIN_EXPR:
4966 case MAX_EXPR:
4967 if (TREE_CODE (type) == COMPLEX_TYPE)
4968 break;
4969 predefined = true;
4970 break;
4971 case BIT_AND_EXPR:
4972 case BIT_IOR_EXPR:
4973 case BIT_XOR_EXPR:
4974 case TRUTH_ANDIF_EXPR:
4975 case TRUTH_ORIF_EXPR:
4976 if (FLOAT_TYPE_P (type))
4977 break;
4978 predefined = true;
4979 break;
4980 default:
4981 break;
4983 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
4985 error ("%qE has invalid type for %<reduction%>", t);
4986 return true;
4988 else if (!processing_template_decl)
4990 t = require_complete_type (t);
4991 if (t == error_mark_node)
4992 return true;
4993 OMP_CLAUSE_DECL (c) = t;
4996 if (predefined)
4998 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
4999 return false;
5001 else if (processing_template_decl)
5002 return false;
5004 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5006 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5007 if (TREE_CODE (type) == REFERENCE_TYPE)
5008 type = TREE_TYPE (type);
5009 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5010 if (id == NULL_TREE)
5011 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5012 NULL_TREE, NULL_TREE);
5013 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5014 if (id)
5016 if (id == error_mark_node)
5017 return true;
5018 id = OVL_CURRENT (id);
5019 mark_used (id);
5020 tree body = DECL_SAVED_TREE (id);
5021 if (TREE_CODE (body) == STATEMENT_LIST)
5023 tree_stmt_iterator tsi;
5024 tree placeholder = NULL_TREE;
5025 int i;
5026 tree stmts[7];
5027 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5028 atype = TREE_TYPE (atype);
5029 bool need_static_cast = !same_type_p (type, atype);
5030 memset (stmts, 0, sizeof stmts);
5031 for (i = 0, tsi = tsi_start (body);
5032 i < 7 && !tsi_end_p (tsi);
5033 i++, tsi_next (&tsi))
5034 stmts[i] = tsi_stmt (tsi);
5035 gcc_assert (tsi_end_p (tsi));
5037 if (i >= 3)
5039 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5040 && TREE_CODE (stmts[1]) == DECL_EXPR);
5041 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5042 DECL_ARTIFICIAL (placeholder) = 1;
5043 DECL_IGNORED_P (placeholder) = 1;
5044 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5045 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5046 cxx_mark_addressable (placeholder);
5047 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5048 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5049 != REFERENCE_TYPE)
5050 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5051 tree omp_out = placeholder;
5052 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5053 if (need_static_cast)
5055 tree rtype = build_reference_type (atype);
5056 omp_out = build_static_cast (rtype, omp_out,
5057 tf_warning_or_error);
5058 omp_in = build_static_cast (rtype, omp_in,
5059 tf_warning_or_error);
5060 if (omp_out == error_mark_node || omp_in == error_mark_node)
5061 return true;
5062 omp_out = convert_from_reference (omp_out);
5063 omp_in = convert_from_reference (omp_in);
5065 OMP_CLAUSE_REDUCTION_MERGE (c)
5066 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5067 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5069 if (i >= 6)
5071 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5072 && TREE_CODE (stmts[4]) == DECL_EXPR);
5073 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5074 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5075 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5076 cxx_mark_addressable (placeholder);
5077 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5078 tree omp_orig = placeholder;
5079 if (need_static_cast)
5081 if (i == 7)
5083 error_at (OMP_CLAUSE_LOCATION (c),
5084 "user defined reduction with constructor "
5085 "initializer for base class %qT", atype);
5086 return true;
5088 tree rtype = build_reference_type (atype);
5089 omp_priv = build_static_cast (rtype, omp_priv,
5090 tf_warning_or_error);
5091 omp_orig = build_static_cast (rtype, omp_orig,
5092 tf_warning_or_error);
5093 if (omp_priv == error_mark_node
5094 || omp_orig == error_mark_node)
5095 return true;
5096 omp_priv = convert_from_reference (omp_priv);
5097 omp_orig = convert_from_reference (omp_orig);
5099 if (i == 6)
5100 *need_default_ctor = true;
5101 OMP_CLAUSE_REDUCTION_INIT (c)
5102 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5103 DECL_EXPR_DECL (stmts[3]),
5104 omp_priv, omp_orig);
5105 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5106 find_omp_placeholder_r, placeholder, NULL))
5107 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5109 else if (i >= 3)
5111 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5112 *need_default_ctor = true;
5113 else
5115 tree init;
5116 tree v = convert_from_reference (t);
5117 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5118 init = build_constructor (TREE_TYPE (v), NULL);
5119 else
5120 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5121 OMP_CLAUSE_REDUCTION_INIT (c)
5122 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5127 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5128 *need_dtor = true;
5129 else
5131 error ("user defined reduction not found for %qD", t);
5132 return true;
5134 return false;
5137 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5138 Remove any elements from the list that are invalid. */
5140 tree
5141 finish_omp_clauses (tree clauses)
5143 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5144 bitmap_head aligned_head;
5145 tree c, t, *pc = &clauses;
5146 bool branch_seen = false;
5147 bool copyprivate_seen = false;
5149 bitmap_obstack_initialize (NULL);
5150 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5151 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5152 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5153 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5155 for (pc = &clauses, c = clauses; c ; c = *pc)
5157 bool remove = false;
5159 switch (OMP_CLAUSE_CODE (c))
5161 case OMP_CLAUSE_SHARED:
5162 goto check_dup_generic;
5163 case OMP_CLAUSE_PRIVATE:
5164 goto check_dup_generic;
5165 case OMP_CLAUSE_REDUCTION:
5166 goto check_dup_generic;
5167 case OMP_CLAUSE_COPYPRIVATE:
5168 copyprivate_seen = true;
5169 goto check_dup_generic;
5170 case OMP_CLAUSE_COPYIN:
5171 goto check_dup_generic;
5172 case OMP_CLAUSE_LINEAR:
5173 t = OMP_CLAUSE_DECL (c);
5174 if (!type_dependent_expression_p (t)
5175 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5176 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5178 error ("linear clause applied to non-integral non-pointer "
5179 "variable with %qT type", TREE_TYPE (t));
5180 remove = true;
5181 break;
5183 t = OMP_CLAUSE_LINEAR_STEP (c);
5184 if (t == NULL_TREE)
5185 t = integer_one_node;
5186 if (t == error_mark_node)
5188 remove = true;
5189 break;
5191 else if (!type_dependent_expression_p (t)
5192 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5194 error ("linear step expression must be integral");
5195 remove = true;
5196 break;
5198 else
5200 t = mark_rvalue_use (t);
5201 if (!processing_template_decl)
5203 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5204 t = maybe_constant_value (t);
5205 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5206 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5207 == POINTER_TYPE)
5209 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5210 OMP_CLAUSE_DECL (c), t);
5211 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5212 MINUS_EXPR, sizetype, t,
5213 OMP_CLAUSE_DECL (c));
5214 if (t == error_mark_node)
5216 remove = true;
5217 break;
5221 OMP_CLAUSE_LINEAR_STEP (c) = t;
5223 goto check_dup_generic;
5224 check_dup_generic:
5225 t = OMP_CLAUSE_DECL (c);
5226 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5228 if (processing_template_decl)
5229 break;
5230 if (DECL_P (t))
5231 error ("%qD is not a variable in clause %qs", t,
5232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5233 else
5234 error ("%qE is not a variable in clause %qs", t,
5235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5236 remove = true;
5238 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5239 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5240 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5242 error ("%qD appears more than once in data clauses", t);
5243 remove = true;
5245 else
5246 bitmap_set_bit (&generic_head, DECL_UID (t));
5247 break;
5249 case OMP_CLAUSE_FIRSTPRIVATE:
5250 t = OMP_CLAUSE_DECL (c);
5251 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5253 if (processing_template_decl)
5254 break;
5255 if (DECL_P (t))
5256 error ("%qD is not a variable in clause %<firstprivate%>", t);
5257 else
5258 error ("%qE is not a variable in clause %<firstprivate%>", t);
5259 remove = true;
5261 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5262 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5264 error ("%qD appears more than once in data clauses", t);
5265 remove = true;
5267 else
5268 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5269 break;
5271 case OMP_CLAUSE_LASTPRIVATE:
5272 t = OMP_CLAUSE_DECL (c);
5273 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5275 if (processing_template_decl)
5276 break;
5277 if (DECL_P (t))
5278 error ("%qD is not a variable in clause %<lastprivate%>", t);
5279 else
5280 error ("%qE is not a variable in clause %<lastprivate%>", t);
5281 remove = true;
5283 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5284 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5286 error ("%qD appears more than once in data clauses", t);
5287 remove = true;
5289 else
5290 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5291 break;
5293 case OMP_CLAUSE_IF:
5294 t = OMP_CLAUSE_IF_EXPR (c);
5295 t = maybe_convert_cond (t);
5296 if (t == error_mark_node)
5297 remove = true;
5298 else if (!processing_template_decl)
5299 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5300 OMP_CLAUSE_IF_EXPR (c) = t;
5301 break;
5303 case OMP_CLAUSE_FINAL:
5304 t = OMP_CLAUSE_FINAL_EXPR (c);
5305 t = maybe_convert_cond (t);
5306 if (t == error_mark_node)
5307 remove = true;
5308 else if (!processing_template_decl)
5309 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5310 OMP_CLAUSE_FINAL_EXPR (c) = t;
5311 break;
5313 case OMP_CLAUSE_NUM_THREADS:
5314 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5315 if (t == error_mark_node)
5316 remove = true;
5317 else if (!type_dependent_expression_p (t)
5318 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5320 error ("num_threads expression must be integral");
5321 remove = true;
5323 else
5325 t = mark_rvalue_use (t);
5326 if (!processing_template_decl)
5327 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5328 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5330 break;
5332 case OMP_CLAUSE_SCHEDULE:
5333 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5334 if (t == NULL)
5336 else if (t == error_mark_node)
5337 remove = true;
5338 else if (!type_dependent_expression_p (t)
5339 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5341 error ("schedule chunk size expression must be integral");
5342 remove = true;
5344 else
5346 t = mark_rvalue_use (t);
5347 if (!processing_template_decl)
5348 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5349 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5351 break;
5353 case OMP_CLAUSE_SIMDLEN:
5354 case OMP_CLAUSE_SAFELEN:
5355 t = OMP_CLAUSE_OPERAND (c, 0);
5356 if (t == error_mark_node)
5357 remove = true;
5358 else if (!type_dependent_expression_p (t)
5359 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5361 error ("%qs length expression must be integral",
5362 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5363 remove = true;
5365 else
5367 t = mark_rvalue_use (t);
5368 t = maybe_constant_value (t);
5369 if (!processing_template_decl)
5371 if (TREE_CODE (t) != INTEGER_CST
5372 || tree_int_cst_sgn (t) != 1)
5374 error ("%qs length expression must be positive constant"
5375 " integer expression",
5376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5377 remove = true;
5380 OMP_CLAUSE_OPERAND (c, 0) = t;
5382 break;
5384 case OMP_CLAUSE_NUM_TEAMS:
5385 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5386 if (t == error_mark_node)
5387 remove = true;
5388 else if (!type_dependent_expression_p (t)
5389 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5391 error ("%<num_teams%> expression must be integral");
5392 remove = true;
5394 else
5396 t = mark_rvalue_use (t);
5397 if (!processing_template_decl)
5398 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5399 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5401 break;
5403 case OMP_CLAUSE_THREAD_LIMIT:
5404 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5405 if (t == error_mark_node)
5406 remove = true;
5407 else if (!type_dependent_expression_p (t)
5408 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5410 error ("%<thread_limit%> expression must be integral");
5411 remove = true;
5413 else
5415 t = mark_rvalue_use (t);
5416 if (!processing_template_decl)
5417 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5418 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5420 break;
5422 case OMP_CLAUSE_DEVICE:
5423 t = OMP_CLAUSE_DEVICE_ID (c);
5424 if (t == error_mark_node)
5425 remove = true;
5426 else if (!type_dependent_expression_p (t)
5427 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5429 error ("%<device%> id must be integral");
5430 remove = true;
5432 else
5434 t = mark_rvalue_use (t);
5435 if (!processing_template_decl)
5436 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5437 OMP_CLAUSE_DEVICE_ID (c) = t;
5439 break;
5441 case OMP_CLAUSE_DIST_SCHEDULE:
5442 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5443 if (t == NULL)
5445 else if (t == error_mark_node)
5446 remove = true;
5447 else if (!type_dependent_expression_p (t)
5448 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5450 error ("%<dist_schedule%> chunk size expression must be "
5451 "integral");
5452 remove = true;
5454 else
5456 t = mark_rvalue_use (t);
5457 if (!processing_template_decl)
5458 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5459 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5461 break;
5463 case OMP_CLAUSE_ALIGNED:
5464 t = OMP_CLAUSE_DECL (c);
5465 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5467 if (processing_template_decl)
5468 break;
5469 if (DECL_P (t))
5470 error ("%qD is not a variable in %<aligned%> clause", t);
5471 else
5472 error ("%qE is not a variable in %<aligned%> clause", t);
5473 remove = true;
5475 else if (!type_dependent_expression_p (t)
5476 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5477 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5478 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5479 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5480 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5481 != ARRAY_TYPE))))
5483 error_at (OMP_CLAUSE_LOCATION (c),
5484 "%qE in %<aligned%> clause is neither a pointer nor "
5485 "an array nor a reference to pointer or array", t);
5486 remove = true;
5488 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5490 error ("%qD appears more than once in %<aligned%> clauses", t);
5491 remove = true;
5493 else
5494 bitmap_set_bit (&aligned_head, DECL_UID (t));
5495 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5496 if (t == error_mark_node)
5497 remove = true;
5498 else if (t == NULL_TREE)
5499 break;
5500 else if (!type_dependent_expression_p (t)
5501 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5503 error ("%<aligned%> clause alignment expression must "
5504 "be integral");
5505 remove = true;
5507 else
5509 t = mark_rvalue_use (t);
5510 t = maybe_constant_value (t);
5511 if (!processing_template_decl)
5513 if (TREE_CODE (t) != INTEGER_CST
5514 || tree_int_cst_sgn (t) != 1)
5516 error ("%<aligned%> clause alignment expression must be "
5517 "positive constant integer expression");
5518 remove = true;
5521 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5523 break;
5525 case OMP_CLAUSE_DEPEND:
5526 t = OMP_CLAUSE_DECL (c);
5527 if (TREE_CODE (t) == TREE_LIST)
5529 if (handle_omp_array_sections (c))
5530 remove = true;
5531 break;
5533 if (t == error_mark_node)
5534 remove = true;
5535 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5537 if (processing_template_decl)
5538 break;
5539 if (DECL_P (t))
5540 error ("%qD is not a variable in %<depend%> clause", t);
5541 else
5542 error ("%qE is not a variable in %<depend%> clause", t);
5543 remove = true;
5545 else if (!processing_template_decl
5546 && !cxx_mark_addressable (t))
5547 remove = true;
5548 break;
5550 case OMP_CLAUSE_MAP:
5551 case OMP_CLAUSE_TO:
5552 case OMP_CLAUSE_FROM:
5553 t = OMP_CLAUSE_DECL (c);
5554 if (TREE_CODE (t) == TREE_LIST)
5556 if (handle_omp_array_sections (c))
5557 remove = true;
5558 else
5560 t = OMP_CLAUSE_DECL (c);
5561 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5563 error_at (OMP_CLAUSE_LOCATION (c),
5564 "array section does not have mappable type "
5565 "in %qs clause",
5566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5567 remove = true;
5570 break;
5572 if (t == error_mark_node)
5573 remove = true;
5574 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5576 if (processing_template_decl)
5577 break;
5578 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5579 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5580 break;
5581 if (DECL_P (t))
5582 error ("%qD is not a variable in %qs clause", t,
5583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5584 else
5585 error ("%qE is not a variable in %qs clause", t,
5586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5587 remove = true;
5589 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5591 error ("%qD is threadprivate variable in %qs clause", t,
5592 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5593 remove = true;
5595 else if (!processing_template_decl
5596 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5597 && !cxx_mark_addressable (t))
5598 remove = true;
5599 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5600 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5601 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5602 == REFERENCE_TYPE)
5603 ? TREE_TYPE (TREE_TYPE (t))
5604 : TREE_TYPE (t)))
5606 error_at (OMP_CLAUSE_LOCATION (c),
5607 "%qD does not have a mappable type in %qs clause", t,
5608 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5609 remove = true;
5611 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5613 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5614 error ("%qD appears more than once in motion clauses", t);
5615 else
5616 error ("%qD appears more than once in map clauses", t);
5617 remove = true;
5619 else
5620 bitmap_set_bit (&generic_head, DECL_UID (t));
5621 break;
5623 case OMP_CLAUSE_UNIFORM:
5624 t = OMP_CLAUSE_DECL (c);
5625 if (TREE_CODE (t) != PARM_DECL)
5627 if (processing_template_decl)
5628 break;
5629 if (DECL_P (t))
5630 error ("%qD is not an argument in %<uniform%> clause", t);
5631 else
5632 error ("%qE is not an argument in %<uniform%> clause", t);
5633 remove = true;
5634 break;
5636 goto check_dup_generic;
5638 case OMP_CLAUSE_NOWAIT:
5639 case OMP_CLAUSE_ORDERED:
5640 case OMP_CLAUSE_DEFAULT:
5641 case OMP_CLAUSE_UNTIED:
5642 case OMP_CLAUSE_COLLAPSE:
5643 case OMP_CLAUSE_MERGEABLE:
5644 case OMP_CLAUSE_PARALLEL:
5645 case OMP_CLAUSE_FOR:
5646 case OMP_CLAUSE_SECTIONS:
5647 case OMP_CLAUSE_TASKGROUP:
5648 case OMP_CLAUSE_PROC_BIND:
5649 break;
5651 case OMP_CLAUSE_INBRANCH:
5652 case OMP_CLAUSE_NOTINBRANCH:
5653 if (branch_seen)
5655 error ("%<inbranch%> clause is incompatible with "
5656 "%<notinbranch%>");
5657 remove = true;
5659 branch_seen = true;
5660 break;
5662 default:
5663 gcc_unreachable ();
5666 if (remove)
5667 *pc = OMP_CLAUSE_CHAIN (c);
5668 else
5669 pc = &OMP_CLAUSE_CHAIN (c);
5672 for (pc = &clauses, c = clauses; c ; c = *pc)
5674 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5675 bool remove = false;
5676 bool need_complete_non_reference = false;
5677 bool need_default_ctor = false;
5678 bool need_copy_ctor = false;
5679 bool need_copy_assignment = false;
5680 bool need_implicitly_determined = false;
5681 bool need_dtor = false;
5682 tree type, inner_type;
5684 switch (c_kind)
5686 case OMP_CLAUSE_SHARED:
5687 need_implicitly_determined = true;
5688 break;
5689 case OMP_CLAUSE_PRIVATE:
5690 need_complete_non_reference = true;
5691 need_default_ctor = true;
5692 need_dtor = true;
5693 need_implicitly_determined = true;
5694 break;
5695 case OMP_CLAUSE_FIRSTPRIVATE:
5696 need_complete_non_reference = true;
5697 need_copy_ctor = true;
5698 need_dtor = true;
5699 need_implicitly_determined = true;
5700 break;
5701 case OMP_CLAUSE_LASTPRIVATE:
5702 need_complete_non_reference = true;
5703 need_copy_assignment = true;
5704 need_implicitly_determined = true;
5705 break;
5706 case OMP_CLAUSE_REDUCTION:
5707 need_implicitly_determined = true;
5708 break;
5709 case OMP_CLAUSE_COPYPRIVATE:
5710 need_copy_assignment = true;
5711 break;
5712 case OMP_CLAUSE_COPYIN:
5713 need_copy_assignment = true;
5714 break;
5715 case OMP_CLAUSE_NOWAIT:
5716 if (copyprivate_seen)
5718 error_at (OMP_CLAUSE_LOCATION (c),
5719 "%<nowait%> clause must not be used together "
5720 "with %<copyprivate%>");
5721 *pc = OMP_CLAUSE_CHAIN (c);
5722 continue;
5724 /* FALLTHRU */
5725 default:
5726 pc = &OMP_CLAUSE_CHAIN (c);
5727 continue;
5730 t = OMP_CLAUSE_DECL (c);
5731 if (processing_template_decl
5732 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5734 pc = &OMP_CLAUSE_CHAIN (c);
5735 continue;
5738 switch (c_kind)
5740 case OMP_CLAUSE_LASTPRIVATE:
5741 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5743 need_default_ctor = true;
5744 need_dtor = true;
5746 break;
5748 case OMP_CLAUSE_REDUCTION:
5749 if (finish_omp_reduction_clause (c, &need_default_ctor,
5750 &need_dtor))
5751 remove = true;
5752 else
5753 t = OMP_CLAUSE_DECL (c);
5754 break;
5756 case OMP_CLAUSE_COPYIN:
5757 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5759 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5760 remove = true;
5762 break;
5764 default:
5765 break;
5768 if (need_complete_non_reference || need_copy_assignment)
5770 t = require_complete_type (t);
5771 if (t == error_mark_node)
5772 remove = true;
5773 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5774 && need_complete_non_reference)
5776 error ("%qE has reference type for %qs", t,
5777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5778 remove = true;
5781 if (need_implicitly_determined)
5783 const char *share_name = NULL;
5785 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5786 share_name = "threadprivate";
5787 else switch (cxx_omp_predetermined_sharing (t))
5789 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5790 break;
5791 case OMP_CLAUSE_DEFAULT_SHARED:
5792 /* const vars may be specified in firstprivate clause. */
5793 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5794 && cxx_omp_const_qual_no_mutable (t))
5795 break;
5796 share_name = "shared";
5797 break;
5798 case OMP_CLAUSE_DEFAULT_PRIVATE:
5799 share_name = "private";
5800 break;
5801 default:
5802 gcc_unreachable ();
5804 if (share_name)
5806 error ("%qE is predetermined %qs for %qs",
5807 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5808 remove = true;
5812 /* We're interested in the base element, not arrays. */
5813 inner_type = type = TREE_TYPE (t);
5814 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5815 inner_type = TREE_TYPE (inner_type);
5817 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5818 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5819 inner_type = TREE_TYPE (inner_type);
5821 /* Check for special function availability by building a call to one.
5822 Save the results, because later we won't be in the right context
5823 for making these queries. */
5824 if (CLASS_TYPE_P (inner_type)
5825 && COMPLETE_TYPE_P (inner_type)
5826 && (need_default_ctor || need_copy_ctor
5827 || need_copy_assignment || need_dtor)
5828 && !type_dependent_expression_p (t)
5829 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5830 need_copy_ctor, need_copy_assignment,
5831 need_dtor))
5832 remove = true;
5834 if (remove)
5835 *pc = OMP_CLAUSE_CHAIN (c);
5836 else
5837 pc = &OMP_CLAUSE_CHAIN (c);
5840 bitmap_obstack_release (NULL);
5841 return clauses;
5844 /* For all variables in the tree_list VARS, mark them as thread local. */
5846 void
5847 finish_omp_threadprivate (tree vars)
5849 tree t;
5851 /* Mark every variable in VARS to be assigned thread local storage. */
5852 for (t = vars; t; t = TREE_CHAIN (t))
5854 tree v = TREE_PURPOSE (t);
5856 if (error_operand_p (v))
5858 else if (!VAR_P (v))
5859 error ("%<threadprivate%> %qD is not file, namespace "
5860 "or block scope variable", v);
5861 /* If V had already been marked threadprivate, it doesn't matter
5862 whether it had been used prior to this point. */
5863 else if (TREE_USED (v)
5864 && (DECL_LANG_SPECIFIC (v) == NULL
5865 || !CP_DECL_THREADPRIVATE_P (v)))
5866 error ("%qE declared %<threadprivate%> after first use", v);
5867 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5868 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5869 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5870 error ("%<threadprivate%> %qE has incomplete type", v);
5871 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5872 && CP_DECL_CONTEXT (v) != current_class_type)
5873 error ("%<threadprivate%> %qE directive not "
5874 "in %qT definition", v, CP_DECL_CONTEXT (v));
5875 else
5877 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5878 if (DECL_LANG_SPECIFIC (v) == NULL)
5880 retrofit_lang_decl (v);
5882 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5883 after the allocation of the lang_decl structure. */
5884 if (DECL_DISCRIMINATOR_P (v))
5885 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5888 if (! DECL_THREAD_LOCAL_P (v))
5890 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
5891 /* If rtl has been already set for this var, call
5892 make_decl_rtl once again, so that encode_section_info
5893 has a chance to look at the new decl flags. */
5894 if (DECL_RTL_SET_P (v))
5895 make_decl_rtl (v);
5897 CP_DECL_THREADPRIVATE_P (v) = 1;
5902 /* Build an OpenMP structured block. */
5904 tree
5905 begin_omp_structured_block (void)
5907 return do_pushlevel (sk_omp);
5910 tree
5911 finish_omp_structured_block (tree block)
5913 return do_poplevel (block);
5916 /* Similarly, except force the retention of the BLOCK. */
5918 tree
5919 begin_omp_parallel (void)
5921 keep_next_level (true);
5922 return begin_omp_structured_block ();
5925 tree
5926 finish_omp_parallel (tree clauses, tree body)
5928 tree stmt;
5930 body = finish_omp_structured_block (body);
5932 stmt = make_node (OMP_PARALLEL);
5933 TREE_TYPE (stmt) = void_type_node;
5934 OMP_PARALLEL_CLAUSES (stmt) = clauses;
5935 OMP_PARALLEL_BODY (stmt) = body;
5937 return add_stmt (stmt);
5940 tree
5941 begin_omp_task (void)
5943 keep_next_level (true);
5944 return begin_omp_structured_block ();
5947 tree
5948 finish_omp_task (tree clauses, tree body)
5950 tree stmt;
5952 body = finish_omp_structured_block (body);
5954 stmt = make_node (OMP_TASK);
5955 TREE_TYPE (stmt) = void_type_node;
5956 OMP_TASK_CLAUSES (stmt) = clauses;
5957 OMP_TASK_BODY (stmt) = body;
5959 return add_stmt (stmt);
5962 /* Helper function for finish_omp_for. Convert Ith random access iterator
5963 into integral iterator. Return FALSE if successful. */
5965 static bool
5966 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
5967 tree condv, tree incrv, tree *body,
5968 tree *pre_body, tree clauses)
5970 tree diff, iter_init, iter_incr = NULL, last;
5971 tree incr_var = NULL, orig_pre_body, orig_body, c;
5972 tree decl = TREE_VEC_ELT (declv, i);
5973 tree init = TREE_VEC_ELT (initv, i);
5974 tree cond = TREE_VEC_ELT (condv, i);
5975 tree incr = TREE_VEC_ELT (incrv, i);
5976 tree iter = decl;
5977 location_t elocus = locus;
5979 if (init && EXPR_HAS_LOCATION (init))
5980 elocus = EXPR_LOCATION (init);
5982 switch (TREE_CODE (cond))
5984 case GT_EXPR:
5985 case GE_EXPR:
5986 case LT_EXPR:
5987 case LE_EXPR:
5988 if (TREE_OPERAND (cond, 1) == iter)
5989 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
5990 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
5991 if (TREE_OPERAND (cond, 0) != iter)
5992 cond = error_mark_node;
5993 else
5995 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
5996 TREE_CODE (cond),
5997 iter, ERROR_MARK,
5998 TREE_OPERAND (cond, 1), ERROR_MARK,
5999 NULL, tf_warning_or_error);
6000 if (error_operand_p (tem))
6001 return true;
6003 break;
6004 default:
6005 cond = error_mark_node;
6006 break;
6008 if (cond == error_mark_node)
6010 error_at (elocus, "invalid controlling predicate");
6011 return true;
6013 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6014 ERROR_MARK, iter, ERROR_MARK, NULL,
6015 tf_warning_or_error);
6016 if (error_operand_p (diff))
6017 return true;
6018 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6020 error_at (elocus, "difference between %qE and %qD does not have integer type",
6021 TREE_OPERAND (cond, 1), iter);
6022 return true;
6025 switch (TREE_CODE (incr))
6027 case PREINCREMENT_EXPR:
6028 case PREDECREMENT_EXPR:
6029 case POSTINCREMENT_EXPR:
6030 case POSTDECREMENT_EXPR:
6031 if (TREE_OPERAND (incr, 0) != iter)
6033 incr = error_mark_node;
6034 break;
6036 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6037 TREE_CODE (incr), iter,
6038 tf_warning_or_error);
6039 if (error_operand_p (iter_incr))
6040 return true;
6041 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6042 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6043 incr = integer_one_node;
6044 else
6045 incr = integer_minus_one_node;
6046 break;
6047 case MODIFY_EXPR:
6048 if (TREE_OPERAND (incr, 0) != iter)
6049 incr = error_mark_node;
6050 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6051 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6053 tree rhs = TREE_OPERAND (incr, 1);
6054 if (TREE_OPERAND (rhs, 0) == iter)
6056 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6057 != INTEGER_TYPE)
6058 incr = error_mark_node;
6059 else
6061 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6062 iter, TREE_CODE (rhs),
6063 TREE_OPERAND (rhs, 1),
6064 tf_warning_or_error);
6065 if (error_operand_p (iter_incr))
6066 return true;
6067 incr = TREE_OPERAND (rhs, 1);
6068 incr = cp_convert (TREE_TYPE (diff), incr,
6069 tf_warning_or_error);
6070 if (TREE_CODE (rhs) == MINUS_EXPR)
6072 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6073 incr = fold_if_not_in_template (incr);
6075 if (TREE_CODE (incr) != INTEGER_CST
6076 && (TREE_CODE (incr) != NOP_EXPR
6077 || (TREE_CODE (TREE_OPERAND (incr, 0))
6078 != INTEGER_CST)))
6079 iter_incr = NULL;
6082 else if (TREE_OPERAND (rhs, 1) == iter)
6084 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6085 || TREE_CODE (rhs) != PLUS_EXPR)
6086 incr = error_mark_node;
6087 else
6089 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6090 PLUS_EXPR,
6091 TREE_OPERAND (rhs, 0),
6092 ERROR_MARK, iter,
6093 ERROR_MARK, NULL,
6094 tf_warning_or_error);
6095 if (error_operand_p (iter_incr))
6096 return true;
6097 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6098 iter, NOP_EXPR,
6099 iter_incr,
6100 tf_warning_or_error);
6101 if (error_operand_p (iter_incr))
6102 return true;
6103 incr = TREE_OPERAND (rhs, 0);
6104 iter_incr = NULL;
6107 else
6108 incr = error_mark_node;
6110 else
6111 incr = error_mark_node;
6112 break;
6113 default:
6114 incr = error_mark_node;
6115 break;
6118 if (incr == error_mark_node)
6120 error_at (elocus, "invalid increment expression");
6121 return true;
6124 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6125 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6127 && OMP_CLAUSE_DECL (c) == iter)
6128 break;
6130 decl = create_temporary_var (TREE_TYPE (diff));
6131 pushdecl (decl);
6132 add_decl_expr (decl);
6133 last = create_temporary_var (TREE_TYPE (diff));
6134 pushdecl (last);
6135 add_decl_expr (last);
6136 if (c && iter_incr == NULL)
6138 incr_var = create_temporary_var (TREE_TYPE (diff));
6139 pushdecl (incr_var);
6140 add_decl_expr (incr_var);
6142 gcc_assert (stmts_are_full_exprs_p ());
6144 orig_pre_body = *pre_body;
6145 *pre_body = push_stmt_list ();
6146 if (orig_pre_body)
6147 add_stmt (orig_pre_body);
6148 if (init != NULL)
6149 finish_expr_stmt (build_x_modify_expr (elocus,
6150 iter, NOP_EXPR, init,
6151 tf_warning_or_error));
6152 init = build_int_cst (TREE_TYPE (diff), 0);
6153 if (c && iter_incr == NULL)
6155 finish_expr_stmt (build_x_modify_expr (elocus,
6156 incr_var, NOP_EXPR,
6157 incr, tf_warning_or_error));
6158 incr = incr_var;
6159 iter_incr = build_x_modify_expr (elocus,
6160 iter, PLUS_EXPR, incr,
6161 tf_warning_or_error);
6163 finish_expr_stmt (build_x_modify_expr (elocus,
6164 last, NOP_EXPR, init,
6165 tf_warning_or_error));
6166 *pre_body = pop_stmt_list (*pre_body);
6168 cond = cp_build_binary_op (elocus,
6169 TREE_CODE (cond), decl, diff,
6170 tf_warning_or_error);
6171 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6172 elocus, incr, NULL_TREE);
6174 orig_body = *body;
6175 *body = push_stmt_list ();
6176 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6177 iter_init = build_x_modify_expr (elocus,
6178 iter, PLUS_EXPR, iter_init,
6179 tf_warning_or_error);
6180 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6181 finish_expr_stmt (iter_init);
6182 finish_expr_stmt (build_x_modify_expr (elocus,
6183 last, NOP_EXPR, decl,
6184 tf_warning_or_error));
6185 add_stmt (orig_body);
6186 *body = pop_stmt_list (*body);
6188 if (c)
6190 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6191 finish_expr_stmt (iter_incr);
6192 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6193 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6196 TREE_VEC_ELT (declv, i) = decl;
6197 TREE_VEC_ELT (initv, i) = init;
6198 TREE_VEC_ELT (condv, i) = cond;
6199 TREE_VEC_ELT (incrv, i) = incr;
6201 return false;
6204 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6205 are directly for their associated operands in the statement. DECL
6206 and INIT are a combo; if DECL is NULL then INIT ought to be a
6207 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6208 optional statements that need to go before the loop into its
6209 sk_omp scope. */
6211 tree
6212 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6213 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6215 tree omp_for = NULL, orig_incr = NULL;
6216 tree decl, init, cond, incr;
6217 location_t elocus;
6218 int i;
6220 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6221 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6222 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6223 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6225 decl = TREE_VEC_ELT (declv, i);
6226 init = TREE_VEC_ELT (initv, i);
6227 cond = TREE_VEC_ELT (condv, i);
6228 incr = TREE_VEC_ELT (incrv, i);
6229 elocus = locus;
6231 if (decl == NULL)
6233 if (init != NULL)
6234 switch (TREE_CODE (init))
6236 case MODIFY_EXPR:
6237 decl = TREE_OPERAND (init, 0);
6238 init = TREE_OPERAND (init, 1);
6239 break;
6240 case MODOP_EXPR:
6241 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6243 decl = TREE_OPERAND (init, 0);
6244 init = TREE_OPERAND (init, 2);
6246 break;
6247 default:
6248 break;
6251 if (decl == NULL)
6253 error_at (locus,
6254 "expected iteration declaration or initialization");
6255 return NULL;
6259 if (init && EXPR_HAS_LOCATION (init))
6260 elocus = EXPR_LOCATION (init);
6262 if (cond == NULL)
6264 error_at (elocus, "missing controlling predicate");
6265 return NULL;
6268 if (incr == NULL)
6270 error_at (elocus, "missing increment expression");
6271 return NULL;
6274 TREE_VEC_ELT (declv, i) = decl;
6275 TREE_VEC_ELT (initv, i) = init;
6278 if (dependent_omp_for_p (declv, initv, condv, incrv))
6280 tree stmt;
6282 stmt = make_node (code);
6284 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6286 /* This is really just a place-holder. We'll be decomposing this
6287 again and going through the cp_build_modify_expr path below when
6288 we instantiate the thing. */
6289 TREE_VEC_ELT (initv, i)
6290 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6291 TREE_VEC_ELT (initv, i));
6294 TREE_TYPE (stmt) = void_type_node;
6295 OMP_FOR_INIT (stmt) = initv;
6296 OMP_FOR_COND (stmt) = condv;
6297 OMP_FOR_INCR (stmt) = incrv;
6298 OMP_FOR_BODY (stmt) = body;
6299 OMP_FOR_PRE_BODY (stmt) = pre_body;
6300 OMP_FOR_CLAUSES (stmt) = clauses;
6302 SET_EXPR_LOCATION (stmt, locus);
6303 return add_stmt (stmt);
6306 if (processing_template_decl)
6307 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6309 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6311 decl = TREE_VEC_ELT (declv, i);
6312 init = TREE_VEC_ELT (initv, i);
6313 cond = TREE_VEC_ELT (condv, i);
6314 incr = TREE_VEC_ELT (incrv, i);
6315 if (orig_incr)
6316 TREE_VEC_ELT (orig_incr, i) = incr;
6317 elocus = locus;
6319 if (init && EXPR_HAS_LOCATION (init))
6320 elocus = EXPR_LOCATION (init);
6322 if (!DECL_P (decl))
6324 error_at (elocus, "expected iteration declaration or initialization");
6325 return NULL;
6328 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6330 if (orig_incr)
6331 TREE_VEC_ELT (orig_incr, i) = incr;
6332 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6333 TREE_CODE (TREE_OPERAND (incr, 1)),
6334 TREE_OPERAND (incr, 2),
6335 tf_warning_or_error);
6338 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6340 if (code == OMP_SIMD)
6342 error_at (elocus, "%<#pragma omp simd%> used with class "
6343 "iteration variable %qE", decl);
6344 return NULL;
6346 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6347 incrv, &body, &pre_body, clauses))
6348 return NULL;
6349 continue;
6352 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6353 && !TYPE_PTR_P (TREE_TYPE (decl)))
6355 error_at (elocus, "invalid type for iteration variable %qE", decl);
6356 return NULL;
6359 if (!processing_template_decl)
6361 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6362 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6364 else
6365 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6366 if (cond
6367 && TREE_SIDE_EFFECTS (cond)
6368 && COMPARISON_CLASS_P (cond)
6369 && !processing_template_decl)
6371 tree t = TREE_OPERAND (cond, 0);
6372 if (TREE_SIDE_EFFECTS (t)
6373 && t != decl
6374 && (TREE_CODE (t) != NOP_EXPR
6375 || TREE_OPERAND (t, 0) != decl))
6376 TREE_OPERAND (cond, 0)
6377 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6379 t = TREE_OPERAND (cond, 1);
6380 if (TREE_SIDE_EFFECTS (t)
6381 && t != decl
6382 && (TREE_CODE (t) != NOP_EXPR
6383 || TREE_OPERAND (t, 0) != decl))
6384 TREE_OPERAND (cond, 1)
6385 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6387 if (decl == error_mark_node || init == error_mark_node)
6388 return NULL;
6390 TREE_VEC_ELT (declv, i) = decl;
6391 TREE_VEC_ELT (initv, i) = init;
6392 TREE_VEC_ELT (condv, i) = cond;
6393 TREE_VEC_ELT (incrv, i) = incr;
6394 i++;
6397 if (IS_EMPTY_STMT (pre_body))
6398 pre_body = NULL;
6400 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6401 body, pre_body);
6403 if (omp_for == NULL)
6404 return NULL;
6406 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6408 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6409 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6411 if (TREE_CODE (incr) != MODIFY_EXPR)
6412 continue;
6414 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6415 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6416 && !processing_template_decl)
6418 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6419 if (TREE_SIDE_EFFECTS (t)
6420 && t != decl
6421 && (TREE_CODE (t) != NOP_EXPR
6422 || TREE_OPERAND (t, 0) != decl))
6423 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6424 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6426 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6427 if (TREE_SIDE_EFFECTS (t)
6428 && t != decl
6429 && (TREE_CODE (t) != NOP_EXPR
6430 || TREE_OPERAND (t, 0) != decl))
6431 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6432 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6435 if (orig_incr)
6436 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6438 if (omp_for != NULL)
6439 OMP_FOR_CLAUSES (omp_for) = clauses;
6440 return omp_for;
6443 void
6444 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6445 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6447 tree orig_lhs;
6448 tree orig_rhs;
6449 tree orig_v;
6450 tree orig_lhs1;
6451 tree orig_rhs1;
6452 bool dependent_p;
6453 tree stmt;
6455 orig_lhs = lhs;
6456 orig_rhs = rhs;
6457 orig_v = v;
6458 orig_lhs1 = lhs1;
6459 orig_rhs1 = rhs1;
6460 dependent_p = false;
6461 stmt = NULL_TREE;
6463 /* Even in a template, we can detect invalid uses of the atomic
6464 pragma if neither LHS nor RHS is type-dependent. */
6465 if (processing_template_decl)
6467 dependent_p = (type_dependent_expression_p (lhs)
6468 || (rhs && type_dependent_expression_p (rhs))
6469 || (v && type_dependent_expression_p (v))
6470 || (lhs1 && type_dependent_expression_p (lhs1))
6471 || (rhs1 && type_dependent_expression_p (rhs1)));
6472 if (!dependent_p)
6474 lhs = build_non_dependent_expr (lhs);
6475 if (rhs)
6476 rhs = build_non_dependent_expr (rhs);
6477 if (v)
6478 v = build_non_dependent_expr (v);
6479 if (lhs1)
6480 lhs1 = build_non_dependent_expr (lhs1);
6481 if (rhs1)
6482 rhs1 = build_non_dependent_expr (rhs1);
6485 if (!dependent_p)
6487 bool swapped = false;
6488 if (rhs1 && cp_tree_equal (lhs, rhs))
6490 tree tem = rhs;
6491 rhs = rhs1;
6492 rhs1 = tem;
6493 swapped = !commutative_tree_code (opcode);
6495 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6497 if (code == OMP_ATOMIC)
6498 error ("%<#pragma omp atomic update%> uses two different "
6499 "expressions for memory");
6500 else
6501 error ("%<#pragma omp atomic capture%> uses two different "
6502 "expressions for memory");
6503 return;
6505 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6507 if (code == OMP_ATOMIC)
6508 error ("%<#pragma omp atomic update%> uses two different "
6509 "expressions for memory");
6510 else
6511 error ("%<#pragma omp atomic capture%> uses two different "
6512 "expressions for memory");
6513 return;
6515 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6516 v, lhs1, rhs1, swapped, seq_cst);
6517 if (stmt == error_mark_node)
6518 return;
6520 if (processing_template_decl)
6522 if (code == OMP_ATOMIC_READ)
6524 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6525 OMP_ATOMIC_READ, orig_lhs);
6526 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6527 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6529 else
6531 if (opcode == NOP_EXPR)
6532 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6533 else
6534 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6535 if (orig_rhs1)
6536 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6537 COMPOUND_EXPR, orig_rhs1, stmt);
6538 if (code != OMP_ATOMIC)
6540 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6541 code, orig_lhs1, stmt);
6542 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6543 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6546 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6547 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6549 finish_expr_stmt (stmt);
6552 void
6553 finish_omp_barrier (void)
6555 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6556 vec<tree, va_gc> *vec = make_tree_vector ();
6557 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6558 release_tree_vector (vec);
6559 finish_expr_stmt (stmt);
6562 void
6563 finish_omp_flush (void)
6565 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6566 vec<tree, va_gc> *vec = make_tree_vector ();
6567 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6568 release_tree_vector (vec);
6569 finish_expr_stmt (stmt);
6572 void
6573 finish_omp_taskwait (void)
6575 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6576 vec<tree, va_gc> *vec = make_tree_vector ();
6577 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6578 release_tree_vector (vec);
6579 finish_expr_stmt (stmt);
6582 void
6583 finish_omp_taskyield (void)
6585 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6586 vec<tree, va_gc> *vec = make_tree_vector ();
6587 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6588 release_tree_vector (vec);
6589 finish_expr_stmt (stmt);
6592 void
6593 finish_omp_cancel (tree clauses)
6595 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6596 int mask = 0;
6597 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6598 mask = 1;
6599 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6600 mask = 2;
6601 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6602 mask = 4;
6603 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6604 mask = 8;
6605 else
6607 error ("%<#pragma omp cancel must specify one of "
6608 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6609 return;
6611 vec<tree, va_gc> *vec = make_tree_vector ();
6612 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6613 if (ifc != NULL_TREE)
6615 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6616 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6617 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6618 build_zero_cst (type));
6620 else
6621 ifc = boolean_true_node;
6622 vec->quick_push (build_int_cst (integer_type_node, mask));
6623 vec->quick_push (ifc);
6624 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6625 release_tree_vector (vec);
6626 finish_expr_stmt (stmt);
6629 void
6630 finish_omp_cancellation_point (tree clauses)
6632 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6633 int mask = 0;
6634 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6635 mask = 1;
6636 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6637 mask = 2;
6638 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6639 mask = 4;
6640 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6641 mask = 8;
6642 else
6644 error ("%<#pragma omp cancellation point must specify one of "
6645 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6646 return;
6648 vec<tree, va_gc> *vec
6649 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6650 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6651 release_tree_vector (vec);
6652 finish_expr_stmt (stmt);
6655 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6656 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6657 should create an extra compound stmt. */
6659 tree
6660 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6662 tree r;
6664 if (pcompound)
6665 *pcompound = begin_compound_stmt (0);
6667 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6669 /* Only add the statement to the function if support enabled. */
6670 if (flag_tm)
6671 add_stmt (r);
6672 else
6673 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6674 ? G_("%<__transaction_relaxed%> without "
6675 "transactional memory support enabled")
6676 : G_("%<__transaction_atomic%> without "
6677 "transactional memory support enabled")));
6679 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6680 TREE_SIDE_EFFECTS (r) = 1;
6681 return r;
6684 /* End a __transaction_atomic or __transaction_relaxed statement.
6685 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6686 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6687 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6689 void
6690 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6692 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6693 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6694 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6695 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6697 /* noexcept specifications are not allowed for function transactions. */
6698 gcc_assert (!(noex && compound_stmt));
6699 if (noex)
6701 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6702 noex);
6703 /* This may not be true when the STATEMENT_LIST is empty. */
6704 if (EXPR_P (body))
6705 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6706 TREE_SIDE_EFFECTS (body) = 1;
6707 TRANSACTION_EXPR_BODY (stmt) = body;
6710 if (compound_stmt)
6711 finish_compound_stmt (compound_stmt);
6714 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6715 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6716 condition. */
6718 tree
6719 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6721 tree ret;
6722 if (noex)
6724 expr = build_must_not_throw_expr (expr, noex);
6725 if (EXPR_P (expr))
6726 SET_EXPR_LOCATION (expr, loc);
6727 TREE_SIDE_EFFECTS (expr) = 1;
6729 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6730 if (flags & TM_STMT_ATTR_RELAXED)
6731 TRANSACTION_EXPR_RELAXED (ret) = 1;
6732 TREE_SIDE_EFFECTS (ret) = 1;
6733 SET_EXPR_LOCATION (ret, loc);
6734 return ret;
6737 void
6738 init_cp_semantics (void)
6742 /* Build a STATIC_ASSERT for a static assertion with the condition
6743 CONDITION and the message text MESSAGE. LOCATION is the location
6744 of the static assertion in the source code. When MEMBER_P, this
6745 static assertion is a member of a class. */
6746 void
6747 finish_static_assert (tree condition, tree message, location_t location,
6748 bool member_p)
6750 if (message == NULL_TREE
6751 || message == error_mark_node
6752 || condition == NULL_TREE
6753 || condition == error_mark_node)
6754 return;
6756 if (check_for_bare_parameter_packs (condition))
6757 condition = error_mark_node;
6759 if (type_dependent_expression_p (condition)
6760 || value_dependent_expression_p (condition))
6762 /* We're in a template; build a STATIC_ASSERT and put it in
6763 the right place. */
6764 tree assertion;
6766 assertion = make_node (STATIC_ASSERT);
6767 STATIC_ASSERT_CONDITION (assertion) = condition;
6768 STATIC_ASSERT_MESSAGE (assertion) = message;
6769 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6771 if (member_p)
6772 maybe_add_class_template_decl_list (current_class_type,
6773 assertion,
6774 /*friend_p=*/0);
6775 else
6776 add_stmt (assertion);
6778 return;
6781 /* Fold the expression and convert it to a boolean value. */
6782 condition = fold_non_dependent_expr (condition);
6783 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6784 condition = maybe_constant_value (condition);
6786 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6787 /* Do nothing; the condition is satisfied. */
6789 else
6791 location_t saved_loc = input_location;
6793 input_location = location;
6794 if (TREE_CODE (condition) == INTEGER_CST
6795 && integer_zerop (condition))
6796 /* Report the error. */
6797 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6798 else if (condition && condition != error_mark_node)
6800 error ("non-constant condition for static assertion");
6801 cxx_constant_value (condition);
6803 input_location = saved_loc;
6807 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6808 suitable for use as a type-specifier.
6810 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6811 id-expression or a class member access, FALSE when it was parsed as
6812 a full expression. */
6814 tree
6815 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6816 tsubst_flags_t complain)
6818 tree type = NULL_TREE;
6820 if (!expr || error_operand_p (expr))
6821 return error_mark_node;
6823 if (TYPE_P (expr)
6824 || TREE_CODE (expr) == TYPE_DECL
6825 || (TREE_CODE (expr) == BIT_NOT_EXPR
6826 && TYPE_P (TREE_OPERAND (expr, 0))))
6828 if (complain & tf_error)
6829 error ("argument to decltype must be an expression");
6830 return error_mark_node;
6833 /* Depending on the resolution of DR 1172, we may later need to distinguish
6834 instantiation-dependent but not type-dependent expressions so that, say,
6835 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6836 if (instantiation_dependent_expression_p (expr))
6838 type = cxx_make_type (DECLTYPE_TYPE);
6839 DECLTYPE_TYPE_EXPR (type) = expr;
6840 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6841 = id_expression_or_member_access_p;
6842 SET_TYPE_STRUCTURAL_EQUALITY (type);
6844 return type;
6847 /* The type denoted by decltype(e) is defined as follows: */
6849 expr = resolve_nondeduced_context (expr);
6851 if (invalid_nonstatic_memfn_p (expr, complain))
6852 return error_mark_node;
6854 if (type_unknown_p (expr))
6856 if (complain & tf_error)
6857 error ("decltype cannot resolve address of overloaded function");
6858 return error_mark_node;
6861 /* To get the size of a static data member declared as an array of
6862 unknown bound, we need to instantiate it. */
6863 if (VAR_P (expr)
6864 && VAR_HAD_UNKNOWN_BOUND (expr)
6865 && DECL_TEMPLATE_INSTANTIATION (expr))
6866 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6868 if (id_expression_or_member_access_p)
6870 /* If e is an id-expression or a class member access (5.2.5
6871 [expr.ref]), decltype(e) is defined as the type of the entity
6872 named by e. If there is no such entity, or e names a set of
6873 overloaded functions, the program is ill-formed. */
6874 if (identifier_p (expr))
6875 expr = lookup_name (expr);
6877 if (INDIRECT_REF_P (expr))
6878 /* This can happen when the expression is, e.g., "a.b". Just
6879 look at the underlying operand. */
6880 expr = TREE_OPERAND (expr, 0);
6882 if (TREE_CODE (expr) == OFFSET_REF
6883 || TREE_CODE (expr) == MEMBER_REF
6884 || TREE_CODE (expr) == SCOPE_REF)
6885 /* We're only interested in the field itself. If it is a
6886 BASELINK, we will need to see through it in the next
6887 step. */
6888 expr = TREE_OPERAND (expr, 1);
6890 if (BASELINK_P (expr))
6891 /* See through BASELINK nodes to the underlying function. */
6892 expr = BASELINK_FUNCTIONS (expr);
6894 switch (TREE_CODE (expr))
6896 case FIELD_DECL:
6897 if (DECL_BIT_FIELD_TYPE (expr))
6899 type = DECL_BIT_FIELD_TYPE (expr);
6900 break;
6902 /* Fall through for fields that aren't bitfields. */
6904 case FUNCTION_DECL:
6905 case VAR_DECL:
6906 case CONST_DECL:
6907 case PARM_DECL:
6908 case RESULT_DECL:
6909 case TEMPLATE_PARM_INDEX:
6910 expr = mark_type_use (expr);
6911 type = TREE_TYPE (expr);
6912 break;
6914 case ERROR_MARK:
6915 type = error_mark_node;
6916 break;
6918 case COMPONENT_REF:
6919 case COMPOUND_EXPR:
6920 mark_type_use (expr);
6921 type = is_bitfield_expr_with_lowered_type (expr);
6922 if (!type)
6923 type = TREE_TYPE (TREE_OPERAND (expr, 1));
6924 break;
6926 case BIT_FIELD_REF:
6927 gcc_unreachable ();
6929 case INTEGER_CST:
6930 case PTRMEM_CST:
6931 /* We can get here when the id-expression refers to an
6932 enumerator or non-type template parameter. */
6933 type = TREE_TYPE (expr);
6934 break;
6936 default:
6937 /* Handle instantiated template non-type arguments. */
6938 type = TREE_TYPE (expr);
6939 break;
6942 else
6944 /* Within a lambda-expression:
6946 Every occurrence of decltype((x)) where x is a possibly
6947 parenthesized id-expression that names an entity of
6948 automatic storage duration is treated as if x were
6949 transformed into an access to a corresponding data member
6950 of the closure type that would have been declared if x
6951 were a use of the denoted entity. */
6952 if (outer_automatic_var_p (expr)
6953 && current_function_decl
6954 && LAMBDA_FUNCTION_P (current_function_decl))
6955 type = capture_decltype (expr);
6956 else if (error_operand_p (expr))
6957 type = error_mark_node;
6958 else if (expr == current_class_ptr)
6959 /* If the expression is just "this", we want the
6960 cv-unqualified pointer for the "this" type. */
6961 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6962 else
6964 /* Otherwise, where T is the type of e, if e is an lvalue,
6965 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
6966 cp_lvalue_kind clk = lvalue_kind (expr);
6967 type = unlowered_expr_type (expr);
6968 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6970 /* For vector types, pick a non-opaque variant. */
6971 if (TREE_CODE (type) == VECTOR_TYPE)
6972 type = strip_typedefs (type);
6974 if (clk != clk_none && !(clk & clk_class))
6975 type = cp_build_reference_type (type, (clk & clk_rvalueref));
6979 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
6981 if (complain & tf_warning_or_error)
6982 pedwarn (input_location, OPT_Wvla,
6983 "taking decltype of array of runtime bound");
6984 else
6985 return error_mark_node;
6988 return type;
6991 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
6992 __has_nothrow_copy, depending on assign_p. */
6994 static bool
6995 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
6997 tree fns;
6999 if (assign_p)
7001 int ix;
7002 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7003 if (ix < 0)
7004 return false;
7005 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7007 else if (TYPE_HAS_COPY_CTOR (type))
7009 /* If construction of the copy constructor was postponed, create
7010 it now. */
7011 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7012 lazily_declare_fn (sfk_copy_constructor, type);
7013 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7014 lazily_declare_fn (sfk_move_constructor, type);
7015 fns = CLASSTYPE_CONSTRUCTORS (type);
7017 else
7018 return false;
7020 for (; fns; fns = OVL_NEXT (fns))
7022 tree fn = OVL_CURRENT (fns);
7024 if (assign_p)
7026 if (copy_fn_p (fn) == 0)
7027 continue;
7029 else if (copy_fn_p (fn) <= 0)
7030 continue;
7032 maybe_instantiate_noexcept (fn);
7033 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7034 return false;
7037 return true;
7040 /* Actually evaluates the trait. */
7042 static bool
7043 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7045 enum tree_code type_code1;
7046 tree t;
7048 type_code1 = TREE_CODE (type1);
7050 switch (kind)
7052 case CPTK_HAS_NOTHROW_ASSIGN:
7053 type1 = strip_array_types (type1);
7054 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7055 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7056 || (CLASS_TYPE_P (type1)
7057 && classtype_has_nothrow_assign_or_copy_p (type1,
7058 true))));
7060 case CPTK_HAS_TRIVIAL_ASSIGN:
7061 /* ??? The standard seems to be missing the "or array of such a class
7062 type" wording for this trait. */
7063 type1 = strip_array_types (type1);
7064 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7065 && (trivial_type_p (type1)
7066 || (CLASS_TYPE_P (type1)
7067 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7069 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7070 type1 = strip_array_types (type1);
7071 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7072 || (CLASS_TYPE_P (type1)
7073 && (t = locate_ctor (type1))
7074 && (maybe_instantiate_noexcept (t),
7075 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7077 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7078 type1 = strip_array_types (type1);
7079 return (trivial_type_p (type1)
7080 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7082 case CPTK_HAS_NOTHROW_COPY:
7083 type1 = strip_array_types (type1);
7084 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7085 || (CLASS_TYPE_P (type1)
7086 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7088 case CPTK_HAS_TRIVIAL_COPY:
7089 /* ??? The standard seems to be missing the "or array of such a class
7090 type" wording for this trait. */
7091 type1 = strip_array_types (type1);
7092 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7093 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7095 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7096 type1 = strip_array_types (type1);
7097 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7098 || (CLASS_TYPE_P (type1)
7099 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7101 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7102 return type_has_virtual_destructor (type1);
7104 case CPTK_IS_ABSTRACT:
7105 return (ABSTRACT_CLASS_TYPE_P (type1));
7107 case CPTK_IS_BASE_OF:
7108 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7109 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7110 || DERIVED_FROM_P (type1, type2)));
7112 case CPTK_IS_CLASS:
7113 return (NON_UNION_CLASS_TYPE_P (type1));
7115 case CPTK_IS_CONVERTIBLE_TO:
7116 /* TODO */
7117 return false;
7119 case CPTK_IS_EMPTY:
7120 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7122 case CPTK_IS_ENUM:
7123 return (type_code1 == ENUMERAL_TYPE);
7125 case CPTK_IS_FINAL:
7126 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7128 case CPTK_IS_LITERAL_TYPE:
7129 return (literal_type_p (type1));
7131 case CPTK_IS_POD:
7132 return (pod_type_p (type1));
7134 case CPTK_IS_POLYMORPHIC:
7135 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7137 case CPTK_IS_STD_LAYOUT:
7138 return (std_layout_type_p (type1));
7140 case CPTK_IS_TRIVIAL:
7141 return (trivial_type_p (type1));
7143 case CPTK_IS_UNION:
7144 return (type_code1 == UNION_TYPE);
7146 default:
7147 gcc_unreachable ();
7148 return false;
7152 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7153 void, or a complete type, returns it, otherwise NULL_TREE. */
7155 static tree
7156 check_trait_type (tree type)
7158 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7159 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7160 return type;
7162 if (VOID_TYPE_P (type))
7163 return type;
7165 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7168 /* Process a trait expression. */
7170 tree
7171 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7173 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7174 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7175 || kind == CPTK_HAS_NOTHROW_COPY
7176 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7177 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7178 || kind == CPTK_HAS_TRIVIAL_COPY
7179 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7180 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7181 || kind == CPTK_IS_ABSTRACT
7182 || kind == CPTK_IS_BASE_OF
7183 || kind == CPTK_IS_CLASS
7184 || kind == CPTK_IS_CONVERTIBLE_TO
7185 || kind == CPTK_IS_EMPTY
7186 || kind == CPTK_IS_ENUM
7187 || kind == CPTK_IS_FINAL
7188 || kind == CPTK_IS_LITERAL_TYPE
7189 || kind == CPTK_IS_POD
7190 || kind == CPTK_IS_POLYMORPHIC
7191 || kind == CPTK_IS_STD_LAYOUT
7192 || kind == CPTK_IS_TRIVIAL
7193 || kind == CPTK_IS_UNION);
7195 if (kind == CPTK_IS_CONVERTIBLE_TO)
7197 sorry ("__is_convertible_to");
7198 return error_mark_node;
7201 if (type1 == error_mark_node
7202 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7203 && type2 == error_mark_node))
7204 return error_mark_node;
7206 if (processing_template_decl)
7208 tree trait_expr = make_node (TRAIT_EXPR);
7209 TREE_TYPE (trait_expr) = boolean_type_node;
7210 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7211 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7212 TRAIT_EXPR_KIND (trait_expr) = kind;
7213 return trait_expr;
7216 switch (kind)
7218 case CPTK_HAS_NOTHROW_ASSIGN:
7219 case CPTK_HAS_TRIVIAL_ASSIGN:
7220 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7221 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7222 case CPTK_HAS_NOTHROW_COPY:
7223 case CPTK_HAS_TRIVIAL_COPY:
7224 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7225 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7226 case CPTK_IS_ABSTRACT:
7227 case CPTK_IS_EMPTY:
7228 case CPTK_IS_FINAL:
7229 case CPTK_IS_LITERAL_TYPE:
7230 case CPTK_IS_POD:
7231 case CPTK_IS_POLYMORPHIC:
7232 case CPTK_IS_STD_LAYOUT:
7233 case CPTK_IS_TRIVIAL:
7234 if (!check_trait_type (type1))
7235 return error_mark_node;
7236 break;
7238 case CPTK_IS_BASE_OF:
7239 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7240 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7241 && !complete_type_or_else (type2, NULL_TREE))
7242 /* We already issued an error. */
7243 return error_mark_node;
7244 break;
7246 case CPTK_IS_CLASS:
7247 case CPTK_IS_ENUM:
7248 case CPTK_IS_UNION:
7249 break;
7251 case CPTK_IS_CONVERTIBLE_TO:
7252 default:
7253 gcc_unreachable ();
7256 return (trait_expr_value (kind, type1, type2)
7257 ? boolean_true_node : boolean_false_node);
7260 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7261 which is ignored for C++. */
7263 void
7264 set_float_const_decimal64 (void)
7268 void
7269 clear_float_const_decimal64 (void)
7273 bool
7274 float_const_decimal64_p (void)
7276 return 0;
7280 /* Return true if T is a literal type. */
7282 bool
7283 literal_type_p (tree t)
7285 if (SCALAR_TYPE_P (t)
7286 || TREE_CODE (t) == VECTOR_TYPE
7287 || TREE_CODE (t) == REFERENCE_TYPE)
7288 return true;
7289 if (CLASS_TYPE_P (t))
7291 t = complete_type (t);
7292 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7293 return CLASSTYPE_LITERAL_P (t);
7295 if (TREE_CODE (t) == ARRAY_TYPE)
7296 return literal_type_p (strip_array_types (t));
7297 return false;
7300 /* If DECL is a variable declared `constexpr', require its type
7301 be literal. Return the DECL if OK, otherwise NULL. */
7303 tree
7304 ensure_literal_type_for_constexpr_object (tree decl)
7306 tree type = TREE_TYPE (decl);
7307 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7308 && !processing_template_decl)
7310 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
7311 /* Don't complain here, we'll complain about incompleteness
7312 when we try to initialize the variable. */;
7313 else if (!literal_type_p (type))
7315 error ("the type %qT of constexpr variable %qD is not literal",
7316 type, decl);
7317 explain_non_literal_class (type);
7318 return NULL;
7321 return decl;
7324 /* Representation of entries in the constexpr function definition table. */
7326 typedef struct GTY(()) constexpr_fundef {
7327 tree decl;
7328 tree body;
7329 } constexpr_fundef;
7331 /* This table holds all constexpr function definitions seen in
7332 the current translation unit. */
7334 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7336 /* Utility function used for managing the constexpr function table.
7337 Return true if the entries pointed to by P and Q are for the
7338 same constexpr function. */
7340 static inline int
7341 constexpr_fundef_equal (const void *p, const void *q)
7343 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7344 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7345 return lhs->decl == rhs->decl;
7348 /* Utility function used for managing the constexpr function table.
7349 Return a hash value for the entry pointed to by Q. */
7351 static inline hashval_t
7352 constexpr_fundef_hash (const void *p)
7354 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7355 return DECL_UID (fundef->decl);
7358 /* Return a previously saved definition of function FUN. */
7360 static constexpr_fundef *
7361 retrieve_constexpr_fundef (tree fun)
7363 constexpr_fundef fundef = { NULL, NULL };
7364 if (constexpr_fundef_table == NULL)
7365 return NULL;
7367 fundef.decl = fun;
7368 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7371 /* Check whether the parameter and return types of FUN are valid for a
7372 constexpr function, and complain if COMPLAIN. */
7374 static bool
7375 is_valid_constexpr_fn (tree fun, bool complain)
7377 tree parm = FUNCTION_FIRST_USER_PARM (fun);
7378 bool ret = true;
7379 for (; parm != NULL; parm = TREE_CHAIN (parm))
7380 if (!literal_type_p (TREE_TYPE (parm)))
7382 ret = false;
7383 if (complain)
7385 error ("invalid type for parameter %d of constexpr "
7386 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7387 explain_non_literal_class (TREE_TYPE (parm));
7391 if (!DECL_CONSTRUCTOR_P (fun))
7393 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7394 if (!literal_type_p (rettype))
7396 ret = false;
7397 if (complain)
7399 error ("invalid return type %qT of constexpr function %q+D",
7400 rettype, fun);
7401 explain_non_literal_class (rettype);
7405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7406 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7408 ret = false;
7409 if (complain)
7411 error ("enclosing class of constexpr non-static member "
7412 "function %q+#D is not a literal type", fun);
7413 explain_non_literal_class (DECL_CONTEXT (fun));
7417 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7419 ret = false;
7420 if (complain)
7421 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7424 return ret;
7427 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7428 for a member of an anonymous aggregate, INIT is the initializer for that
7429 member, and VEC_OUTER is the vector of constructor elements for the class
7430 whose constructor we are processing. Add the initializer to the vector
7431 and return true to indicate success. */
7433 static bool
7434 build_anon_member_initialization (tree member, tree init,
7435 vec<constructor_elt, va_gc> **vec_outer)
7437 /* MEMBER presents the relevant fields from the inside out, but we need
7438 to build up the initializer from the outside in so that we can reuse
7439 previously built CONSTRUCTORs if this is, say, the second field in an
7440 anonymous struct. So we use a vec as a stack. */
7441 auto_vec<tree, 2> fields;
7444 fields.safe_push (TREE_OPERAND (member, 1));
7445 member = TREE_OPERAND (member, 0);
7447 while (ANON_AGGR_TYPE_P (TREE_TYPE (member)));
7449 /* VEC has the constructor elements vector for the context of FIELD.
7450 If FIELD is an anonymous aggregate, we will push inside it. */
7451 vec<constructor_elt, va_gc> **vec = vec_outer;
7452 tree field;
7453 while (field = fields.pop(),
7454 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7456 tree ctor;
7457 /* If there is already an outer constructor entry for the anonymous
7458 aggregate FIELD, use it; otherwise, insert one. */
7459 if (vec_safe_is_empty (*vec)
7460 || (*vec)->last().index != field)
7462 ctor = build_constructor (TREE_TYPE (field), NULL);
7463 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7465 else
7466 ctor = (*vec)->last().value;
7467 vec = &CONSTRUCTOR_ELTS (ctor);
7470 /* Now we're at the innermost field, the one that isn't an anonymous
7471 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7472 gcc_assert (fields.is_empty());
7473 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7475 return true;
7478 /* Subroutine of build_constexpr_constructor_member_initializers.
7479 The expression tree T represents a data member initialization
7480 in a (constexpr) constructor definition. Build a pairing of
7481 the data member with its initializer, and prepend that pair
7482 to the existing initialization pair INITS. */
7484 static bool
7485 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7487 tree member, init;
7488 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7489 t = TREE_OPERAND (t, 0);
7490 if (TREE_CODE (t) == EXPR_STMT)
7491 t = TREE_OPERAND (t, 0);
7492 if (t == error_mark_node)
7493 return false;
7494 if (TREE_CODE (t) == STATEMENT_LIST)
7496 tree_stmt_iterator i;
7497 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7499 if (! build_data_member_initialization (tsi_stmt (i), vec))
7500 return false;
7502 return true;
7504 if (TREE_CODE (t) == CLEANUP_STMT)
7506 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7507 but we can in a constexpr constructor for a non-literal class. Just
7508 ignore it; either all the initialization will be constant, in which
7509 case the cleanup can't run, or it can't be constexpr.
7510 Still recurse into CLEANUP_BODY. */
7511 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7513 if (TREE_CODE (t) == CONVERT_EXPR)
7514 t = TREE_OPERAND (t, 0);
7515 if (TREE_CODE (t) == INIT_EXPR
7516 || TREE_CODE (t) == MODIFY_EXPR)
7518 member = TREE_OPERAND (t, 0);
7519 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7521 else if (TREE_CODE (t) == CALL_EXPR)
7523 member = CALL_EXPR_ARG (t, 0);
7524 /* We don't use build_cplus_new here because it complains about
7525 abstract bases. Leaving the call unwrapped means that it has the
7526 wrong type, but cxx_eval_constant_expression doesn't care. */
7527 init = break_out_target_exprs (t);
7529 else if (TREE_CODE (t) == DECL_EXPR)
7530 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7531 return true;
7532 else
7533 gcc_unreachable ();
7534 if (INDIRECT_REF_P (member))
7535 member = TREE_OPERAND (member, 0);
7536 if (TREE_CODE (member) == NOP_EXPR)
7538 tree op = member;
7539 STRIP_NOPS (op);
7540 if (TREE_CODE (op) == ADDR_EXPR)
7542 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7543 (TREE_TYPE (TREE_TYPE (op)),
7544 TREE_TYPE (TREE_TYPE (member))));
7545 /* Initializing a cv-qualified member; we need to look through
7546 the const_cast. */
7547 member = op;
7549 else if (op == current_class_ptr
7550 && (same_type_ignoring_top_level_qualifiers_p
7551 (TREE_TYPE (TREE_TYPE (member)),
7552 current_class_type)))
7553 /* Delegating constructor. */
7554 member = op;
7555 else
7557 /* This is an initializer for an empty base; keep it for now so
7558 we can check it in cxx_eval_bare_aggregate. */
7559 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7562 if (TREE_CODE (member) == ADDR_EXPR)
7563 member = TREE_OPERAND (member, 0);
7564 if (TREE_CODE (member) == COMPONENT_REF)
7566 tree aggr = TREE_OPERAND (member, 0);
7567 if (TREE_CODE (aggr) != COMPONENT_REF)
7568 /* Normal member initialization. */
7569 member = TREE_OPERAND (member, 1);
7570 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7571 /* Initializing a member of an anonymous union. */
7572 return build_anon_member_initialization (member, init, vec);
7573 else
7574 /* We're initializing a vtable pointer in a base. Leave it as
7575 COMPONENT_REF so we remember the path to get to the vfield. */
7576 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7579 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7580 return true;
7583 /* Make sure that there are no statements after LAST in the constructor
7584 body represented by LIST. */
7586 bool
7587 check_constexpr_ctor_body (tree last, tree list)
7589 bool ok = true;
7590 if (TREE_CODE (list) == STATEMENT_LIST)
7592 tree_stmt_iterator i = tsi_last (list);
7593 for (; !tsi_end_p (i); tsi_prev (&i))
7595 tree t = tsi_stmt (i);
7596 if (t == last)
7597 break;
7598 if (TREE_CODE (t) == BIND_EXPR)
7600 if (BIND_EXPR_VARS (t))
7602 ok = false;
7603 break;
7605 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7606 return false;
7607 else
7608 continue;
7610 /* We currently allow typedefs and static_assert.
7611 FIXME allow them in the standard, too. */
7612 if (TREE_CODE (t) != STATIC_ASSERT)
7614 ok = false;
7615 break;
7619 else if (list != last
7620 && TREE_CODE (list) != STATIC_ASSERT)
7621 ok = false;
7622 if (!ok)
7624 error ("constexpr constructor does not have empty body");
7625 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7627 return ok;
7630 /* V is a vector of constructor elements built up for the base and member
7631 initializers of a constructor for TYPE. They need to be in increasing
7632 offset order, which they might not be yet if TYPE has a primary base
7633 which is not first in the base-clause or a vptr and at least one base
7634 all of which are non-primary. */
7636 static vec<constructor_elt, va_gc> *
7637 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7639 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7640 tree field_type;
7641 constructor_elt elt;
7642 int i;
7644 if (pri)
7645 field_type = BINFO_TYPE (pri);
7646 else if (TYPE_CONTAINS_VPTR_P (type))
7647 field_type = vtbl_ptr_type_node;
7648 else
7649 return v;
7651 /* Find the element for the primary base or vptr and move it to the
7652 beginning of the vec. */
7653 vec<constructor_elt, va_gc> &vref = *v;
7654 for (i = 0; ; ++i)
7655 if (TREE_TYPE (vref[i].index) == field_type)
7656 break;
7658 if (i > 0)
7660 elt = vref[i];
7661 for (; i > 0; --i)
7662 vref[i] = vref[i-1];
7663 vref[0] = elt;
7666 return v;
7669 /* Build compile-time evalable representations of member-initializer list
7670 for a constexpr constructor. */
7672 static tree
7673 build_constexpr_constructor_member_initializers (tree type, tree body)
7675 vec<constructor_elt, va_gc> *vec = NULL;
7676 bool ok = true;
7677 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7678 || TREE_CODE (body) == EH_SPEC_BLOCK)
7679 body = TREE_OPERAND (body, 0);
7680 if (TREE_CODE (body) == STATEMENT_LIST)
7681 body = STATEMENT_LIST_HEAD (body)->stmt;
7682 body = BIND_EXPR_BODY (body);
7683 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7685 body = TREE_OPERAND (body, 0);
7686 if (TREE_CODE (body) == EXPR_STMT)
7687 body = TREE_OPERAND (body, 0);
7688 if (TREE_CODE (body) == INIT_EXPR
7689 && (same_type_ignoring_top_level_qualifiers_p
7690 (TREE_TYPE (TREE_OPERAND (body, 0)),
7691 current_class_type)))
7693 /* Trivial copy. */
7694 return TREE_OPERAND (body, 1);
7696 ok = build_data_member_initialization (body, &vec);
7698 else if (TREE_CODE (body) == STATEMENT_LIST)
7700 tree_stmt_iterator i;
7701 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7703 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7704 if (!ok)
7705 break;
7708 else if (TREE_CODE (body) == TRY_BLOCK)
7710 error ("body of %<constexpr%> constructor cannot be "
7711 "a function-try-block");
7712 return error_mark_node;
7714 else if (EXPR_P (body))
7715 ok = build_data_member_initialization (body, &vec);
7716 else
7717 gcc_assert (errorcount > 0);
7718 if (ok)
7720 if (vec_safe_length (vec) > 0)
7722 /* In a delegating constructor, return the target. */
7723 constructor_elt *ce = &(*vec)[0];
7724 if (ce->index == current_class_ptr)
7726 body = ce->value;
7727 vec_free (vec);
7728 return body;
7731 vec = sort_constexpr_mem_initializers (type, vec);
7732 return build_constructor (type, vec);
7734 else
7735 return error_mark_node;
7738 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7739 declared to be constexpr, or a sub-statement thereof. Returns the
7740 return value if suitable, error_mark_node for a statement not allowed in
7741 a constexpr function, or NULL_TREE if no return value was found. */
7743 static tree
7744 constexpr_fn_retval (tree body)
7746 switch (TREE_CODE (body))
7748 case STATEMENT_LIST:
7750 tree_stmt_iterator i;
7751 tree expr = NULL_TREE;
7752 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7754 tree s = constexpr_fn_retval (tsi_stmt (i));
7755 if (s == error_mark_node)
7756 return error_mark_node;
7757 else if (s == NULL_TREE)
7758 /* Keep iterating. */;
7759 else if (expr)
7760 /* Multiple return statements. */
7761 return error_mark_node;
7762 else
7763 expr = s;
7765 return expr;
7768 case RETURN_EXPR:
7769 return break_out_target_exprs (TREE_OPERAND (body, 0));
7771 case DECL_EXPR:
7772 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7773 return NULL_TREE;
7774 return error_mark_node;
7776 case CLEANUP_POINT_EXPR:
7777 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7779 case USING_STMT:
7780 return NULL_TREE;
7782 default:
7783 return error_mark_node;
7787 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7788 FUN; do the necessary transformations to turn it into a single expression
7789 that we can store in the hash table. */
7791 static tree
7792 massage_constexpr_body (tree fun, tree body)
7794 if (DECL_CONSTRUCTOR_P (fun))
7795 body = build_constexpr_constructor_member_initializers
7796 (DECL_CONTEXT (fun), body);
7797 else
7799 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7800 body = EH_SPEC_STMTS (body);
7801 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7802 body = TREE_OPERAND (body, 0);
7803 if (TREE_CODE (body) == BIND_EXPR)
7804 body = BIND_EXPR_BODY (body);
7805 body = constexpr_fn_retval (body);
7807 return body;
7810 /* FUN is a constexpr constructor with massaged body BODY. Return true
7811 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7813 static bool
7814 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7816 bool bad;
7817 tree field;
7818 unsigned i, nelts;
7819 tree ctype;
7821 if (TREE_CODE (body) != CONSTRUCTOR)
7822 return false;
7824 nelts = CONSTRUCTOR_NELTS (body);
7825 ctype = DECL_CONTEXT (fun);
7826 field = TYPE_FIELDS (ctype);
7828 if (TREE_CODE (ctype) == UNION_TYPE)
7830 if (nelts == 0 && next_initializable_field (field))
7832 if (complain)
7833 error ("%<constexpr%> constructor for union %qT must "
7834 "initialize exactly one non-static data member", ctype);
7835 return true;
7837 return false;
7840 bad = false;
7841 for (i = 0; i <= nelts; ++i)
7843 tree index;
7844 if (i == nelts)
7845 index = NULL_TREE;
7846 else
7848 index = CONSTRUCTOR_ELT (body, i)->index;
7849 /* Skip base and vtable inits. */
7850 if (TREE_CODE (index) != FIELD_DECL
7851 || DECL_ARTIFICIAL (index))
7852 continue;
7854 for (; field != index; field = DECL_CHAIN (field))
7856 tree ftype;
7857 if (TREE_CODE (field) != FIELD_DECL
7858 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7859 || DECL_ARTIFICIAL (field))
7860 continue;
7861 ftype = strip_array_types (TREE_TYPE (field));
7862 if (type_has_constexpr_default_constructor (ftype))
7864 /* It's OK to skip a member with a trivial constexpr ctor.
7865 A constexpr ctor that isn't trivial should have been
7866 added in by now. */
7867 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7868 || errorcount != 0);
7869 continue;
7871 if (!complain)
7872 return true;
7873 error ("uninitialized member %qD in %<constexpr%> constructor",
7874 field);
7875 bad = true;
7877 if (field == NULL_TREE)
7878 break;
7879 field = DECL_CHAIN (field);
7882 return bad;
7885 /* We are processing the definition of the constexpr function FUN.
7886 Check that its BODY fulfills the propriate requirements and
7887 enter it in the constexpr function definition table.
7888 For constructor BODY is actually the TREE_LIST of the
7889 member-initializer list. */
7891 tree
7892 register_constexpr_fundef (tree fun, tree body)
7894 constexpr_fundef entry;
7895 constexpr_fundef **slot;
7897 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
7898 return NULL;
7900 body = massage_constexpr_body (fun, body);
7901 if (body == NULL_TREE || body == error_mark_node)
7903 if (!DECL_CONSTRUCTOR_P (fun))
7904 error ("body of constexpr function %qD not a return-statement", fun);
7905 return NULL;
7908 if (!potential_rvalue_constant_expression (body))
7910 if (!DECL_GENERATED_P (fun))
7911 require_potential_rvalue_constant_expression (body);
7912 return NULL;
7915 if (DECL_CONSTRUCTOR_P (fun)
7916 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
7917 return NULL;
7919 /* Create the constexpr function table if necessary. */
7920 if (constexpr_fundef_table == NULL)
7921 constexpr_fundef_table = htab_create_ggc (101,
7922 constexpr_fundef_hash,
7923 constexpr_fundef_equal,
7924 ggc_free);
7925 entry.decl = fun;
7926 entry.body = body;
7927 slot = (constexpr_fundef **)
7928 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
7930 gcc_assert (*slot == NULL);
7931 *slot = ggc_alloc_constexpr_fundef ();
7932 **slot = entry;
7934 return fun;
7937 /* FUN is a non-constexpr function called in a context that requires a
7938 constant expression. If it comes from a constexpr template, explain why
7939 the instantiation isn't constexpr. */
7941 void
7942 explain_invalid_constexpr_fn (tree fun)
7944 static struct pointer_set_t *diagnosed;
7945 tree body;
7946 location_t save_loc;
7947 /* Only diagnose defaulted functions or instantiations. */
7948 if (!DECL_DEFAULTED_FN (fun)
7949 && !is_instantiation_of_constexpr (fun))
7950 return;
7951 if (diagnosed == NULL)
7952 diagnosed = pointer_set_create ();
7953 if (pointer_set_insert (diagnosed, fun) != 0)
7954 /* Already explained. */
7955 return;
7957 save_loc = input_location;
7958 input_location = DECL_SOURCE_LOCATION (fun);
7959 inform (0, "%q+D is not usable as a constexpr function because:", fun);
7960 /* First check the declaration. */
7961 if (is_valid_constexpr_fn (fun, true))
7963 /* Then if it's OK, the body. */
7964 if (DECL_DEFAULTED_FN (fun))
7965 explain_implicit_non_constexpr (fun);
7966 else
7968 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
7969 require_potential_rvalue_constant_expression (body);
7970 if (DECL_CONSTRUCTOR_P (fun))
7971 cx_check_missing_mem_inits (fun, body, true);
7974 input_location = save_loc;
7977 /* Objects of this type represent calls to constexpr functions
7978 along with the bindings of parameters to their arguments, for
7979 the purpose of compile time evaluation. */
7981 typedef struct GTY(()) constexpr_call {
7982 /* Description of the constexpr function definition. */
7983 constexpr_fundef *fundef;
7984 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
7985 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
7986 Note: This arrangement is made to accommodate the use of
7987 iterative_hash_template_arg (see pt.c). If you change this
7988 representation, also change the hash calculation in
7989 cxx_eval_call_expression. */
7990 tree bindings;
7991 /* Result of the call.
7992 NULL means the call is being evaluated.
7993 error_mark_node means that the evaluation was erroneous;
7994 otherwise, the actuall value of the call. */
7995 tree result;
7996 /* The hash of this call; we remember it here to avoid having to
7997 recalculate it when expanding the hash table. */
7998 hashval_t hash;
7999 } constexpr_call;
8001 /* A table of all constexpr calls that have been evaluated by the
8002 compiler in this translation unit. */
8004 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8006 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8007 bool, bool, bool *, bool *);
8009 /* Compute a hash value for a constexpr call representation. */
8011 static hashval_t
8012 constexpr_call_hash (const void *p)
8014 const constexpr_call *info = (const constexpr_call *) p;
8015 return info->hash;
8018 /* Return 1 if the objects pointed to by P and Q represent calls
8019 to the same constexpr function with the same arguments.
8020 Otherwise, return 0. */
8022 static int
8023 constexpr_call_equal (const void *p, const void *q)
8025 const constexpr_call *lhs = (const constexpr_call *) p;
8026 const constexpr_call *rhs = (const constexpr_call *) q;
8027 tree lhs_bindings;
8028 tree rhs_bindings;
8029 if (lhs == rhs)
8030 return 1;
8031 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8032 return 0;
8033 lhs_bindings = lhs->bindings;
8034 rhs_bindings = rhs->bindings;
8035 while (lhs_bindings != NULL && rhs_bindings != NULL)
8037 tree lhs_arg = TREE_VALUE (lhs_bindings);
8038 tree rhs_arg = TREE_VALUE (rhs_bindings);
8039 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8040 if (!cp_tree_equal (lhs_arg, rhs_arg))
8041 return 0;
8042 lhs_bindings = TREE_CHAIN (lhs_bindings);
8043 rhs_bindings = TREE_CHAIN (rhs_bindings);
8045 return lhs_bindings == rhs_bindings;
8048 /* Initialize the constexpr call table, if needed. */
8050 static void
8051 maybe_initialize_constexpr_call_table (void)
8053 if (constexpr_call_table == NULL)
8054 constexpr_call_table = htab_create_ggc (101,
8055 constexpr_call_hash,
8056 constexpr_call_equal,
8057 ggc_free);
8060 /* Return true if T designates the implied `this' parameter. */
8062 static inline bool
8063 is_this_parameter (tree t)
8065 return t == current_class_ptr;
8068 /* We have an expression tree T that represents a call, either CALL_EXPR
8069 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8070 retrun the _DECL for that function. */
8072 static tree
8073 get_function_named_in_call (tree t)
8075 tree fun = NULL;
8076 switch (TREE_CODE (t))
8078 case CALL_EXPR:
8079 fun = CALL_EXPR_FN (t);
8080 break;
8082 case AGGR_INIT_EXPR:
8083 fun = AGGR_INIT_EXPR_FN (t);
8084 break;
8086 default:
8087 gcc_unreachable();
8088 break;
8090 if (TREE_CODE (fun) == ADDR_EXPR
8091 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8092 fun = TREE_OPERAND (fun, 0);
8093 return fun;
8096 /* We have an expression tree T that represents a call, either CALL_EXPR
8097 or AGGR_INIT_EXPR. Return the Nth argument. */
8099 static inline tree
8100 get_nth_callarg (tree t, int n)
8102 switch (TREE_CODE (t))
8104 case CALL_EXPR:
8105 return CALL_EXPR_ARG (t, n);
8107 case AGGR_INIT_EXPR:
8108 return AGGR_INIT_EXPR_ARG (t, n);
8110 default:
8111 gcc_unreachable ();
8112 return NULL;
8116 /* Look up the binding of the function parameter T in a constexpr
8117 function call context CALL. */
8119 static tree
8120 lookup_parameter_binding (const constexpr_call *call, tree t)
8122 tree b = purpose_member (t, call->bindings);
8123 return TREE_VALUE (b);
8126 /* Attempt to evaluate T which represents a call to a builtin function.
8127 We assume here that all builtin functions evaluate to scalar types
8128 represented by _CST nodes. */
8130 static tree
8131 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8132 bool allow_non_constant, bool addr,
8133 bool *non_constant_p, bool *overflow_p)
8135 const int nargs = call_expr_nargs (t);
8136 tree *args = (tree *) alloca (nargs * sizeof (tree));
8137 tree new_call;
8138 int i;
8139 for (i = 0; i < nargs; ++i)
8141 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8142 allow_non_constant, addr,
8143 non_constant_p, overflow_p);
8144 if (allow_non_constant && *non_constant_p)
8145 return t;
8147 if (*non_constant_p)
8148 return t;
8149 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8150 CALL_EXPR_FN (t), nargs, args);
8151 new_call = fold (new_call);
8152 VERIFY_CONSTANT (new_call);
8153 return new_call;
8156 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8157 the type of the value to match. */
8159 static tree
8160 adjust_temp_type (tree type, tree temp)
8162 if (TREE_TYPE (temp) == type)
8163 return temp;
8164 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8165 if (TREE_CODE (temp) == CONSTRUCTOR)
8166 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8167 gcc_assert (scalarish_type_p (type));
8168 return cp_fold_convert (type, temp);
8171 /* Subroutine of cxx_eval_call_expression.
8172 We are processing a call expression (either CALL_EXPR or
8173 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8174 all arguments and bind their values to correspondings
8175 parameters, making up the NEW_CALL context. */
8177 static void
8178 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8179 constexpr_call *new_call,
8180 bool allow_non_constant,
8181 bool *non_constant_p, bool *overflow_p)
8183 const int nargs = call_expr_nargs (t);
8184 tree fun = new_call->fundef->decl;
8185 tree parms = DECL_ARGUMENTS (fun);
8186 int i;
8187 for (i = 0; i < nargs; ++i)
8189 tree x, arg;
8190 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8191 /* For member function, the first argument is a pointer to the implied
8192 object. And for an object construction, don't bind `this' before
8193 it is fully constructed. */
8194 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8195 goto next;
8196 x = get_nth_callarg (t, i);
8197 if (parms && DECL_BY_REFERENCE (parms))
8199 /* cp_genericize made this a reference for argument passing, but
8200 we don't want to treat it like one for constexpr evaluation. */
8201 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8202 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8203 type = TREE_TYPE (type);
8204 x = convert_from_reference (x);
8206 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8207 TREE_CODE (type) == REFERENCE_TYPE,
8208 non_constant_p, overflow_p);
8209 /* Don't VERIFY_CONSTANT here. */
8210 if (*non_constant_p && allow_non_constant)
8211 return;
8212 /* Just discard ellipsis args after checking their constantitude. */
8213 if (!parms)
8214 continue;
8215 if (*non_constant_p)
8216 /* Don't try to adjust the type of non-constant args. */
8217 goto next;
8219 /* Make sure the binding has the same type as the parm. */
8220 if (TREE_CODE (type) != REFERENCE_TYPE)
8221 arg = adjust_temp_type (type, arg);
8222 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8223 next:
8224 parms = TREE_CHAIN (parms);
8228 /* Variables and functions to manage constexpr call expansion context.
8229 These do not need to be marked for PCH or GC. */
8231 /* FIXME remember and print actual constant arguments. */
8232 static vec<tree> call_stack = vNULL;
8233 static int call_stack_tick;
8234 static int last_cx_error_tick;
8236 static bool
8237 push_cx_call_context (tree call)
8239 ++call_stack_tick;
8240 if (!EXPR_HAS_LOCATION (call))
8241 SET_EXPR_LOCATION (call, input_location);
8242 call_stack.safe_push (call);
8243 if (call_stack.length () > (unsigned) max_constexpr_depth)
8244 return false;
8245 return true;
8248 static void
8249 pop_cx_call_context (void)
8251 ++call_stack_tick;
8252 call_stack.pop ();
8255 vec<tree>
8256 cx_error_context (void)
8258 vec<tree> r = vNULL;
8259 if (call_stack_tick != last_cx_error_tick
8260 && !call_stack.is_empty ())
8261 r = call_stack;
8262 last_cx_error_tick = call_stack_tick;
8263 return r;
8266 /* Subroutine of cxx_eval_constant_expression.
8267 Evaluate the call expression tree T in the context of OLD_CALL expression
8268 evaluation. */
8270 static tree
8271 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8272 bool allow_non_constant, bool addr,
8273 bool *non_constant_p, bool *overflow_p)
8275 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8276 tree fun = get_function_named_in_call (t);
8277 tree result;
8278 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8279 constexpr_call **slot;
8280 constexpr_call *entry;
8281 bool depth_ok;
8283 if (TREE_CODE (fun) != FUNCTION_DECL)
8285 /* Might be a constexpr function pointer. */
8286 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8287 /*addr*/false, non_constant_p, overflow_p);
8288 if (TREE_CODE (fun) == ADDR_EXPR)
8289 fun = TREE_OPERAND (fun, 0);
8291 if (TREE_CODE (fun) != FUNCTION_DECL)
8293 if (!allow_non_constant && !*non_constant_p)
8294 error_at (loc, "expression %qE does not designate a constexpr "
8295 "function", fun);
8296 *non_constant_p = true;
8297 return t;
8299 if (DECL_CLONED_FUNCTION_P (fun))
8300 fun = DECL_CLONED_FUNCTION (fun);
8301 if (is_builtin_fn (fun))
8302 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8303 addr, non_constant_p, overflow_p);
8304 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8306 if (!allow_non_constant)
8308 error_at (loc, "call to non-constexpr function %qD", fun);
8309 explain_invalid_constexpr_fn (fun);
8311 *non_constant_p = true;
8312 return t;
8315 /* Shortcut trivial constructor/op=. */
8316 if (trivial_fn_p (fun))
8318 if (call_expr_nargs (t) == 2)
8320 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8321 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8322 addr, non_constant_p, overflow_p);
8324 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8325 && AGGR_INIT_ZERO_FIRST (t))
8326 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8329 /* If in direct recursive call, optimize definition search. */
8330 if (old_call != NULL && old_call->fundef->decl == fun)
8331 new_call.fundef = old_call->fundef;
8332 else
8334 new_call.fundef = retrieve_constexpr_fundef (fun);
8335 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8337 if (!allow_non_constant)
8339 if (DECL_INITIAL (fun))
8341 /* The definition of fun was somehow unsuitable. */
8342 error_at (loc, "%qD called in a constant expression", fun);
8343 explain_invalid_constexpr_fn (fun);
8345 else
8346 error_at (loc, "%qD used before its definition", fun);
8348 *non_constant_p = true;
8349 return t;
8352 cxx_bind_parameters_in_call (old_call, t, &new_call,
8353 allow_non_constant, non_constant_p, overflow_p);
8354 if (*non_constant_p)
8355 return t;
8357 depth_ok = push_cx_call_context (t);
8359 new_call.hash
8360 = iterative_hash_template_arg (new_call.bindings,
8361 constexpr_fundef_hash (new_call.fundef));
8363 /* If we have seen this call before, we are done. */
8364 maybe_initialize_constexpr_call_table ();
8365 slot = (constexpr_call **)
8366 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8367 entry = *slot;
8368 if (entry == NULL)
8370 /* We need to keep a pointer to the entry, not just the slot, as the
8371 slot can move in the call to cxx_eval_builtin_function_call. */
8372 *slot = entry = ggc_alloc_constexpr_call ();
8373 *entry = new_call;
8375 /* Calls which are in progress have their result set to NULL
8376 so that we can detect circular dependencies. */
8377 else if (entry->result == NULL)
8379 if (!allow_non_constant)
8380 error ("call has circular dependency");
8381 *non_constant_p = true;
8382 entry->result = result = error_mark_node;
8385 if (!depth_ok)
8387 if (!allow_non_constant)
8388 error ("constexpr evaluation depth exceeds maximum of %d (use "
8389 "-fconstexpr-depth= to increase the maximum)",
8390 max_constexpr_depth);
8391 *non_constant_p = true;
8392 entry->result = result = error_mark_node;
8394 else
8396 result = entry->result;
8397 if (!result || result == error_mark_node)
8398 result = (cxx_eval_constant_expression
8399 (&new_call, new_call.fundef->body,
8400 allow_non_constant, addr,
8401 non_constant_p, overflow_p));
8402 if (result == error_mark_node)
8403 *non_constant_p = true;
8404 if (*non_constant_p)
8405 entry->result = result = error_mark_node;
8406 else
8408 /* If this was a call to initialize an object, set the type of
8409 the CONSTRUCTOR to the type of that object. */
8410 if (DECL_CONSTRUCTOR_P (fun))
8412 tree ob_arg = get_nth_callarg (t, 0);
8413 STRIP_NOPS (ob_arg);
8414 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8415 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8416 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8417 result);
8419 entry->result = result;
8423 pop_cx_call_context ();
8424 return unshare_expr (result);
8427 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8429 bool
8430 reduced_constant_expression_p (tree t)
8432 if (TREE_CODE (t) == PTRMEM_CST)
8433 /* Even if we can't lower this yet, it's constant. */
8434 return true;
8435 /* FIXME are we calling this too much? */
8436 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8439 /* Some expressions may have constant operands but are not constant
8440 themselves, such as 1/0. Call this function (or rather, the macro
8441 following it) to check for that condition.
8443 We only call this in places that require an arithmetic constant, not in
8444 places where we might have a non-constant expression that can be a
8445 component of a constant expression, such as the address of a constexpr
8446 variable that might be dereferenced later. */
8448 static bool
8449 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8450 bool *overflow_p)
8452 if (!*non_constant_p && !reduced_constant_expression_p (t))
8454 if (!allow_non_constant)
8455 error ("%q+E is not a constant expression", t);
8456 *non_constant_p = true;
8458 if (TREE_OVERFLOW_P (t))
8460 if (!allow_non_constant)
8462 permerror (input_location, "overflow in constant expression");
8463 /* If we're being permissive (and are in an enforcing
8464 context), ignore the overflow. */
8465 if (flag_permissive)
8466 return *non_constant_p;
8468 *overflow_p = true;
8470 return *non_constant_p;
8473 /* Subroutine of cxx_eval_constant_expression.
8474 Attempt to reduce the unary expression tree T to a compile time value.
8475 If successful, return the value. Otherwise issue a diagnostic
8476 and return error_mark_node. */
8478 static tree
8479 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8480 bool allow_non_constant, bool addr,
8481 bool *non_constant_p, bool *overflow_p)
8483 tree r;
8484 tree orig_arg = TREE_OPERAND (t, 0);
8485 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8486 addr, non_constant_p, overflow_p);
8487 VERIFY_CONSTANT (arg);
8488 if (arg == orig_arg)
8489 return t;
8490 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8491 VERIFY_CONSTANT (r);
8492 return r;
8495 /* Subroutine of cxx_eval_constant_expression.
8496 Like cxx_eval_unary_expression, except for binary expressions. */
8498 static tree
8499 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8500 bool allow_non_constant, bool addr,
8501 bool *non_constant_p, bool *overflow_p)
8503 tree r;
8504 tree orig_lhs = TREE_OPERAND (t, 0);
8505 tree orig_rhs = TREE_OPERAND (t, 1);
8506 tree lhs, rhs;
8507 lhs = cxx_eval_constant_expression (call, orig_lhs,
8508 allow_non_constant, addr,
8509 non_constant_p, overflow_p);
8510 VERIFY_CONSTANT (lhs);
8511 rhs = cxx_eval_constant_expression (call, orig_rhs,
8512 allow_non_constant, addr,
8513 non_constant_p, overflow_p);
8514 VERIFY_CONSTANT (rhs);
8515 if (lhs == orig_lhs && rhs == orig_rhs)
8516 return t;
8517 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8518 VERIFY_CONSTANT (r);
8519 return r;
8522 /* Subroutine of cxx_eval_constant_expression.
8523 Attempt to evaluate condition expressions. Dead branches are not
8524 looked into. */
8526 static tree
8527 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8528 bool allow_non_constant, bool addr,
8529 bool *non_constant_p, bool *overflow_p)
8531 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8532 allow_non_constant, addr,
8533 non_constant_p, overflow_p);
8534 VERIFY_CONSTANT (val);
8535 /* Don't VERIFY_CONSTANT the other operands. */
8536 if (integer_zerop (val))
8537 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8538 allow_non_constant, addr,
8539 non_constant_p, overflow_p);
8540 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8541 allow_non_constant, addr,
8542 non_constant_p, overflow_p);
8545 /* Subroutine of cxx_eval_constant_expression.
8546 Attempt to reduce a reference to an array slot. */
8548 static tree
8549 cxx_eval_array_reference (const constexpr_call *call, tree t,
8550 bool allow_non_constant, bool addr,
8551 bool *non_constant_p, bool *overflow_p)
8553 tree oldary = TREE_OPERAND (t, 0);
8554 tree ary = cxx_eval_constant_expression (call, oldary,
8555 allow_non_constant, addr,
8556 non_constant_p, overflow_p);
8557 tree index, oldidx;
8558 HOST_WIDE_INT i;
8559 tree elem_type;
8560 unsigned len, elem_nchars = 1;
8561 if (*non_constant_p)
8562 return t;
8563 oldidx = TREE_OPERAND (t, 1);
8564 index = cxx_eval_constant_expression (call, oldidx,
8565 allow_non_constant, false,
8566 non_constant_p, overflow_p);
8567 VERIFY_CONSTANT (index);
8568 if (addr && ary == oldary && index == oldidx)
8569 return t;
8570 else if (addr)
8571 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8572 elem_type = TREE_TYPE (TREE_TYPE (ary));
8573 if (TREE_CODE (ary) == CONSTRUCTOR)
8574 len = CONSTRUCTOR_NELTS (ary);
8575 else if (TREE_CODE (ary) == STRING_CST)
8577 elem_nchars = (TYPE_PRECISION (elem_type)
8578 / TYPE_PRECISION (char_type_node));
8579 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8581 else
8583 /* We can't do anything with other tree codes, so use
8584 VERIFY_CONSTANT to complain and fail. */
8585 VERIFY_CONSTANT (ary);
8586 gcc_unreachable ();
8588 if (compare_tree_int (index, len) >= 0)
8590 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8592 /* If it's within the array bounds but doesn't have an explicit
8593 initializer, it's value-initialized. */
8594 tree val = build_value_init (elem_type, tf_warning_or_error);
8595 return cxx_eval_constant_expression (call, val,
8596 allow_non_constant, addr,
8597 non_constant_p, overflow_p);
8600 if (!allow_non_constant)
8601 error ("array subscript out of bound");
8602 *non_constant_p = true;
8603 return t;
8605 else if (tree_int_cst_lt (index, integer_zero_node))
8607 if (!allow_non_constant)
8608 error ("negative array subscript");
8609 *non_constant_p = true;
8610 return t;
8612 i = tree_to_shwi (index);
8613 if (TREE_CODE (ary) == CONSTRUCTOR)
8614 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8615 else if (elem_nchars == 1)
8616 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8617 TREE_STRING_POINTER (ary)[i]);
8618 else
8620 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8621 return native_interpret_expr (type, (const unsigned char *)
8622 TREE_STRING_POINTER (ary)
8623 + i * elem_nchars, elem_nchars);
8625 /* Don't VERIFY_CONSTANT here. */
8628 /* Subroutine of cxx_eval_constant_expression.
8629 Attempt to reduce a field access of a value of class type. */
8631 static tree
8632 cxx_eval_component_reference (const constexpr_call *call, tree t,
8633 bool allow_non_constant, bool addr,
8634 bool *non_constant_p, bool *overflow_p)
8636 unsigned HOST_WIDE_INT i;
8637 tree field;
8638 tree value;
8639 tree part = TREE_OPERAND (t, 1);
8640 tree orig_whole = TREE_OPERAND (t, 0);
8641 tree whole = cxx_eval_constant_expression (call, orig_whole,
8642 allow_non_constant, addr,
8643 non_constant_p, overflow_p);
8644 if (whole == orig_whole)
8645 return t;
8646 if (addr)
8647 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8648 whole, part, NULL_TREE);
8649 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8650 CONSTRUCTOR. */
8651 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8653 if (!allow_non_constant)
8654 error ("%qE is not a constant expression", orig_whole);
8655 *non_constant_p = true;
8657 if (DECL_MUTABLE_P (part))
8659 if (!allow_non_constant)
8660 error ("mutable %qD is not usable in a constant expression", part);
8661 *non_constant_p = true;
8663 if (*non_constant_p)
8664 return t;
8665 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8667 if (field == part)
8668 return value;
8670 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8671 && CONSTRUCTOR_NELTS (whole) > 0)
8673 /* DR 1188 says we don't have to deal with this. */
8674 if (!allow_non_constant)
8675 error ("accessing %qD member instead of initialized %qD member in "
8676 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8677 *non_constant_p = true;
8678 return t;
8681 /* If there's no explicit init for this field, it's value-initialized. */
8682 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8683 return cxx_eval_constant_expression (call, value,
8684 allow_non_constant, addr,
8685 non_constant_p, overflow_p);
8688 /* Subroutine of cxx_eval_constant_expression.
8689 Attempt to reduce a field access of a value of class type that is
8690 expressed as a BIT_FIELD_REF. */
8692 static tree
8693 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8694 bool allow_non_constant, bool addr,
8695 bool *non_constant_p, bool *overflow_p)
8697 tree orig_whole = TREE_OPERAND (t, 0);
8698 tree retval, fldval, utype, mask;
8699 bool fld_seen = false;
8700 HOST_WIDE_INT istart, isize;
8701 tree whole = cxx_eval_constant_expression (call, orig_whole,
8702 allow_non_constant, addr,
8703 non_constant_p, overflow_p);
8704 tree start, field, value;
8705 unsigned HOST_WIDE_INT i;
8707 if (whole == orig_whole)
8708 return t;
8709 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8710 CONSTRUCTOR. */
8711 if (!*non_constant_p
8712 && TREE_CODE (whole) != VECTOR_CST
8713 && TREE_CODE (whole) != CONSTRUCTOR)
8715 if (!allow_non_constant)
8716 error ("%qE is not a constant expression", orig_whole);
8717 *non_constant_p = true;
8719 if (*non_constant_p)
8720 return t;
8722 if (TREE_CODE (whole) == VECTOR_CST)
8723 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8724 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8726 start = TREE_OPERAND (t, 2);
8727 istart = tree_to_shwi (start);
8728 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8729 utype = TREE_TYPE (t);
8730 if (!TYPE_UNSIGNED (utype))
8731 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8732 retval = build_int_cst (utype, 0);
8733 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8735 tree bitpos = bit_position (field);
8736 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8737 return value;
8738 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8739 && TREE_CODE (value) == INTEGER_CST
8740 && tree_fits_shwi_p (bitpos)
8741 && tree_fits_shwi_p (DECL_SIZE (field)))
8743 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8744 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8745 HOST_WIDE_INT shift;
8746 if (bit >= istart && bit + sz <= istart + isize)
8748 fldval = fold_convert (utype, value);
8749 mask = build_int_cst_type (utype, -1);
8750 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8751 size_int (TYPE_PRECISION (utype) - sz));
8752 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8753 size_int (TYPE_PRECISION (utype) - sz));
8754 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8755 shift = bit - istart;
8756 if (BYTES_BIG_ENDIAN)
8757 shift = TYPE_PRECISION (utype) - shift - sz;
8758 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8759 size_int (shift));
8760 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8761 fld_seen = true;
8765 if (fld_seen)
8766 return fold_convert (TREE_TYPE (t), retval);
8767 gcc_unreachable ();
8768 return error_mark_node;
8771 /* Subroutine of cxx_eval_constant_expression.
8772 Evaluate a short-circuited logical expression T in the context
8773 of a given constexpr CALL. BAILOUT_VALUE is the value for
8774 early return. CONTINUE_VALUE is used here purely for
8775 sanity check purposes. */
8777 static tree
8778 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8779 tree bailout_value, tree continue_value,
8780 bool allow_non_constant, bool addr,
8781 bool *non_constant_p, bool *overflow_p)
8783 tree r;
8784 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8785 allow_non_constant, addr,
8786 non_constant_p, overflow_p);
8787 VERIFY_CONSTANT (lhs);
8788 if (tree_int_cst_equal (lhs, bailout_value))
8789 return lhs;
8790 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8791 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8792 allow_non_constant, addr, non_constant_p, overflow_p);
8793 VERIFY_CONSTANT (r);
8794 return r;
8797 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8798 CONSTRUCTOR elements to initialize (part of) an object containing that
8799 field. Return a pointer to the constructor_elt corresponding to the
8800 initialization of the field. */
8802 static constructor_elt *
8803 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8805 tree aggr = TREE_OPERAND (ref, 0);
8806 tree field = TREE_OPERAND (ref, 1);
8807 HOST_WIDE_INT i;
8808 constructor_elt *ce;
8810 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8812 if (TREE_CODE (aggr) == COMPONENT_REF)
8814 constructor_elt *base_ce
8815 = base_field_constructor_elt (v, aggr);
8816 v = CONSTRUCTOR_ELTS (base_ce->value);
8819 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8820 if (ce->index == field)
8821 return ce;
8823 gcc_unreachable ();
8824 return NULL;
8827 /* Subroutine of cxx_eval_constant_expression.
8828 The expression tree T denotes a C-style array or a C-style
8829 aggregate. Reduce it to a constant expression. */
8831 static tree
8832 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8833 bool allow_non_constant, bool addr,
8834 bool *non_constant_p, bool *overflow_p)
8836 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8837 vec<constructor_elt, va_gc> *n;
8838 vec_alloc (n, vec_safe_length (v));
8839 constructor_elt *ce;
8840 HOST_WIDE_INT i;
8841 bool changed = false;
8842 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8843 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8845 tree elt = cxx_eval_constant_expression (call, ce->value,
8846 allow_non_constant, addr,
8847 non_constant_p, overflow_p);
8848 /* Don't VERIFY_CONSTANT here. */
8849 if (allow_non_constant && *non_constant_p)
8850 goto fail;
8851 if (elt != ce->value)
8852 changed = true;
8853 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8855 /* This is an initialization of a vfield inside a base
8856 subaggregate that we already initialized; push this
8857 initialization into the previous initialization. */
8858 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8859 inner->value = elt;
8861 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
8863 /* This is an initializer for an empty base; now that we've
8864 checked that it's constant, we can ignore it. */
8865 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8867 else
8868 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
8870 if (*non_constant_p || !changed)
8872 fail:
8873 vec_free (n);
8874 return t;
8876 t = build_constructor (TREE_TYPE (t), n);
8877 TREE_CONSTANT (t) = true;
8878 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
8879 t = fold (t);
8880 return t;
8883 /* Subroutine of cxx_eval_constant_expression.
8884 The expression tree T is a VEC_INIT_EXPR which denotes the desired
8885 initialization of a non-static data member of array type. Reduce it to a
8886 CONSTRUCTOR.
8888 Note that apart from value-initialization (when VALUE_INIT is true),
8889 this is only intended to support value-initialization and the
8890 initializations done by defaulted constructors for classes with
8891 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
8892 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
8893 for the copy/move constructor. */
8895 static tree
8896 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
8897 bool value_init, bool allow_non_constant, bool addr,
8898 bool *non_constant_p, bool *overflow_p)
8900 tree elttype = TREE_TYPE (atype);
8901 int max = tree_to_shwi (array_type_nelts (atype));
8902 vec<constructor_elt, va_gc> *n;
8903 vec_alloc (n, max + 1);
8904 bool pre_init = false;
8905 int i;
8907 /* For the default constructor, build up a call to the default
8908 constructor of the element type. We only need to handle class types
8909 here, as for a constructor to be constexpr, all members must be
8910 initialized, which for a defaulted default constructor means they must
8911 be of a class type with a constexpr default constructor. */
8912 if (TREE_CODE (elttype) == ARRAY_TYPE)
8913 /* We only do this at the lowest level. */;
8914 else if (value_init)
8916 init = build_value_init (elttype, tf_warning_or_error);
8917 init = cxx_eval_constant_expression
8918 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
8919 pre_init = true;
8921 else if (!init)
8923 vec<tree, va_gc> *argvec = make_tree_vector ();
8924 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8925 &argvec, elttype, LOOKUP_NORMAL,
8926 tf_warning_or_error);
8927 release_tree_vector (argvec);
8928 init = cxx_eval_constant_expression (call, init, allow_non_constant,
8929 addr, non_constant_p, overflow_p);
8930 pre_init = true;
8933 if (*non_constant_p && !allow_non_constant)
8934 goto fail;
8936 for (i = 0; i <= max; ++i)
8938 tree idx = build_int_cst (size_type_node, i);
8939 tree eltinit;
8940 if (TREE_CODE (elttype) == ARRAY_TYPE)
8942 /* A multidimensional array; recurse. */
8943 if (value_init || init == NULL_TREE)
8944 eltinit = NULL_TREE;
8945 else
8946 eltinit = cp_build_array_ref (input_location, init, idx,
8947 tf_warning_or_error);
8948 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
8949 allow_non_constant, addr,
8950 non_constant_p, overflow_p);
8952 else if (pre_init)
8954 /* Initializing an element using value or default initialization
8955 we just pre-built above. */
8956 if (i == 0)
8957 eltinit = init;
8958 else
8959 eltinit = unshare_expr (init);
8961 else
8963 /* Copying an element. */
8964 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8965 (atype, TREE_TYPE (init)));
8966 eltinit = cp_build_array_ref (input_location, init, idx,
8967 tf_warning_or_error);
8968 if (!real_lvalue_p (init))
8969 eltinit = move (eltinit);
8970 eltinit = force_rvalue (eltinit, tf_warning_or_error);
8971 eltinit = cxx_eval_constant_expression
8972 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
8974 if (*non_constant_p && !allow_non_constant)
8975 goto fail;
8976 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
8979 if (!*non_constant_p)
8981 init = build_constructor (atype, n);
8982 TREE_CONSTANT (init) = true;
8983 return init;
8986 fail:
8987 vec_free (n);
8988 return init;
8991 static tree
8992 cxx_eval_vec_init (const constexpr_call *call, tree t,
8993 bool allow_non_constant, bool addr,
8994 bool *non_constant_p, bool *overflow_p)
8996 tree atype = TREE_TYPE (t);
8997 tree init = VEC_INIT_EXPR_INIT (t);
8998 tree r = cxx_eval_vec_init_1 (call, atype, init,
8999 VEC_INIT_EXPR_VALUE_INIT (t),
9000 allow_non_constant, addr, non_constant_p, overflow_p);
9001 if (*non_constant_p)
9002 return t;
9003 else
9004 return r;
9007 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9008 match. We want to be less strict for simple *& folding; if we have a
9009 non-const temporary that we access through a const pointer, that should
9010 work. We handle this here rather than change fold_indirect_ref_1
9011 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9012 don't really make sense outside of constant expression evaluation. Also
9013 we want to allow folding to COMPONENT_REF, which could cause trouble
9014 with TBAA in fold_indirect_ref_1.
9016 Try to keep this function synced with fold_indirect_ref_1. */
9018 static tree
9019 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9021 tree sub, subtype;
9023 sub = op0;
9024 STRIP_NOPS (sub);
9025 subtype = TREE_TYPE (sub);
9026 if (!POINTER_TYPE_P (subtype))
9027 return NULL_TREE;
9029 if (TREE_CODE (sub) == ADDR_EXPR)
9031 tree op = TREE_OPERAND (sub, 0);
9032 tree optype = TREE_TYPE (op);
9034 /* *&CONST_DECL -> to the value of the const decl. */
9035 if (TREE_CODE (op) == CONST_DECL)
9036 return DECL_INITIAL (op);
9037 /* *&p => p; make sure to handle *&"str"[cst] here. */
9038 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9040 tree fop = fold_read_from_constant_string (op);
9041 if (fop)
9042 return fop;
9043 else
9044 return op;
9046 /* *(foo *)&fooarray => fooarray[0] */
9047 else if (TREE_CODE (optype) == ARRAY_TYPE
9048 && (same_type_ignoring_top_level_qualifiers_p
9049 (type, TREE_TYPE (optype))))
9051 tree type_domain = TYPE_DOMAIN (optype);
9052 tree min_val = size_zero_node;
9053 if (type_domain && TYPE_MIN_VALUE (type_domain))
9054 min_val = TYPE_MIN_VALUE (type_domain);
9055 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9056 NULL_TREE, NULL_TREE);
9058 /* *(foo *)&complexfoo => __real__ complexfoo */
9059 else if (TREE_CODE (optype) == COMPLEX_TYPE
9060 && (same_type_ignoring_top_level_qualifiers_p
9061 (type, TREE_TYPE (optype))))
9062 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9063 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9064 else if (TREE_CODE (optype) == VECTOR_TYPE
9065 && (same_type_ignoring_top_level_qualifiers_p
9066 (type, TREE_TYPE (optype))))
9068 tree part_width = TYPE_SIZE (type);
9069 tree index = bitsize_int (0);
9070 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9072 /* Also handle conversion to an empty base class, which
9073 is represented with a NOP_EXPR. */
9074 else if (is_empty_class (type)
9075 && CLASS_TYPE_P (optype)
9076 && DERIVED_FROM_P (type, optype))
9078 *empty_base = true;
9079 return op;
9081 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9082 else if (RECORD_OR_UNION_TYPE_P (optype))
9084 tree field = TYPE_FIELDS (optype);
9085 for (; field; field = DECL_CHAIN (field))
9086 if (TREE_CODE (field) == FIELD_DECL
9087 && integer_zerop (byte_position (field))
9088 && (same_type_ignoring_top_level_qualifiers_p
9089 (TREE_TYPE (field), type)))
9091 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9092 break;
9096 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9097 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9099 tree op00 = TREE_OPERAND (sub, 0);
9100 tree op01 = TREE_OPERAND (sub, 1);
9102 STRIP_NOPS (op00);
9103 if (TREE_CODE (op00) == ADDR_EXPR)
9105 tree op00type;
9106 op00 = TREE_OPERAND (op00, 0);
9107 op00type = TREE_TYPE (op00);
9109 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9110 if (TREE_CODE (op00type) == VECTOR_TYPE
9111 && (same_type_ignoring_top_level_qualifiers_p
9112 (type, TREE_TYPE (op00type))))
9114 HOST_WIDE_INT offset = tree_to_shwi (op01);
9115 tree part_width = TYPE_SIZE (type);
9116 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9117 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9118 tree index = bitsize_int (indexi);
9120 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9121 return fold_build3_loc (loc,
9122 BIT_FIELD_REF, type, op00,
9123 part_width, index);
9126 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9127 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9128 && (same_type_ignoring_top_level_qualifiers_p
9129 (type, TREE_TYPE (op00type))))
9131 tree size = TYPE_SIZE_UNIT (type);
9132 if (tree_int_cst_equal (size, op01))
9133 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9135 /* ((foo *)&fooarray)[1] => fooarray[1] */
9136 else if (TREE_CODE (op00type) == ARRAY_TYPE
9137 && (same_type_ignoring_top_level_qualifiers_p
9138 (type, TREE_TYPE (op00type))))
9140 tree type_domain = TYPE_DOMAIN (op00type);
9141 tree min_val = size_zero_node;
9142 if (type_domain && TYPE_MIN_VALUE (type_domain))
9143 min_val = TYPE_MIN_VALUE (type_domain);
9144 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9145 TYPE_SIZE_UNIT (type));
9146 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9147 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9148 NULL_TREE, NULL_TREE);
9150 /* Also handle conversion to an empty base class, which
9151 is represented with a NOP_EXPR. */
9152 else if (is_empty_class (type)
9153 && CLASS_TYPE_P (op00type)
9154 && DERIVED_FROM_P (type, op00type))
9156 *empty_base = true;
9157 return op00;
9159 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9160 else if (RECORD_OR_UNION_TYPE_P (op00type))
9162 tree field = TYPE_FIELDS (op00type);
9163 for (; field; field = DECL_CHAIN (field))
9164 if (TREE_CODE (field) == FIELD_DECL
9165 && tree_int_cst_equal (byte_position (field), op01)
9166 && (same_type_ignoring_top_level_qualifiers_p
9167 (TREE_TYPE (field), type)))
9169 return fold_build3 (COMPONENT_REF, type, op00,
9170 field, NULL_TREE);
9171 break;
9176 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9177 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9178 && (same_type_ignoring_top_level_qualifiers_p
9179 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9181 tree type_domain;
9182 tree min_val = size_zero_node;
9183 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9184 if (newsub)
9185 sub = newsub;
9186 else
9187 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9188 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9189 if (type_domain && TYPE_MIN_VALUE (type_domain))
9190 min_val = TYPE_MIN_VALUE (type_domain);
9191 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9192 NULL_TREE);
9195 return NULL_TREE;
9198 static tree
9199 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9200 bool allow_non_constant, bool addr,
9201 bool *non_constant_p, bool *overflow_p)
9203 tree orig_op0 = TREE_OPERAND (t, 0);
9204 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9205 /*addr*/false, non_constant_p, overflow_p);
9206 bool empty_base = false;
9207 tree r;
9209 /* Don't VERIFY_CONSTANT here. */
9210 if (*non_constant_p)
9211 return t;
9213 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9214 &empty_base);
9216 if (r)
9217 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9218 addr, non_constant_p, overflow_p);
9219 else
9221 tree sub = op0;
9222 STRIP_NOPS (sub);
9223 if (TREE_CODE (sub) == ADDR_EXPR)
9225 /* We couldn't fold to a constant value. Make sure it's not
9226 something we should have been able to fold. */
9227 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9228 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9229 /* DR 1188 says we don't have to deal with this. */
9230 if (!allow_non_constant)
9231 error ("accessing value of %qE through a %qT glvalue in a "
9232 "constant expression", build_fold_indirect_ref (sub),
9233 TREE_TYPE (t));
9234 *non_constant_p = true;
9235 return t;
9239 /* If we're pulling out the value of an empty base, make sure
9240 that the whole object is constant and then return an empty
9241 CONSTRUCTOR. */
9242 if (empty_base)
9244 VERIFY_CONSTANT (r);
9245 r = build_constructor (TREE_TYPE (t), NULL);
9246 TREE_CONSTANT (r) = true;
9249 if (r == NULL_TREE)
9251 if (addr && op0 != orig_op0)
9252 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9253 if (!addr)
9254 VERIFY_CONSTANT (t);
9255 return t;
9257 return r;
9260 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9261 Shared between potential_constant_expression and
9262 cxx_eval_constant_expression. */
9264 static void
9265 non_const_var_error (tree r)
9267 tree type = TREE_TYPE (r);
9268 error ("the value of %qD is not usable in a constant "
9269 "expression", r);
9270 /* Avoid error cascade. */
9271 if (DECL_INITIAL (r) == error_mark_node)
9272 return;
9273 if (DECL_DECLARED_CONSTEXPR_P (r))
9274 inform (DECL_SOURCE_LOCATION (r),
9275 "%qD used in its own initializer", r);
9276 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9278 if (!CP_TYPE_CONST_P (type))
9279 inform (DECL_SOURCE_LOCATION (r),
9280 "%q#D is not const", r);
9281 else if (CP_TYPE_VOLATILE_P (type))
9282 inform (DECL_SOURCE_LOCATION (r),
9283 "%q#D is volatile", r);
9284 else if (!DECL_INITIAL (r)
9285 || !TREE_CONSTANT (DECL_INITIAL (r)))
9286 inform (DECL_SOURCE_LOCATION (r),
9287 "%qD was not initialized with a constant "
9288 "expression", r);
9289 else
9290 gcc_unreachable ();
9292 else
9294 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9295 inform (DECL_SOURCE_LOCATION (r),
9296 "%qD was not declared %<constexpr%>", r);
9297 else
9298 inform (DECL_SOURCE_LOCATION (r),
9299 "%qD does not have integral or enumeration type",
9304 /* Subroutine of cxx_eval_constant_expression.
9305 Like cxx_eval_unary_expression, except for trinary expressions. */
9307 static tree
9308 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9309 bool allow_non_constant, bool addr,
9310 bool *non_constant_p, bool *overflow_p)
9312 int i;
9313 tree args[3];
9314 tree val;
9316 for (i = 0; i < 3; i++)
9318 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9319 allow_non_constant, addr,
9320 non_constant_p, overflow_p);
9321 VERIFY_CONSTANT (args[i]);
9324 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9325 args[0], args[1], args[2]);
9326 if (val == NULL_TREE)
9327 return t;
9328 VERIFY_CONSTANT (val);
9329 return val;
9332 /* Attempt to reduce the expression T to a constant value.
9333 On failure, issue diagnostic and return error_mark_node. */
9334 /* FIXME unify with c_fully_fold */
9336 static tree
9337 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9338 bool allow_non_constant, bool addr,
9339 bool *non_constant_p, bool *overflow_p)
9341 tree r = t;
9343 if (t == error_mark_node)
9345 *non_constant_p = true;
9346 return t;
9348 if (CONSTANT_CLASS_P (t))
9350 if (TREE_CODE (t) == PTRMEM_CST)
9351 t = cplus_expand_constant (t);
9352 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9353 *overflow_p = true;
9354 return t;
9356 if (TREE_CODE (t) != NOP_EXPR
9357 && reduced_constant_expression_p (t))
9358 return fold (t);
9360 switch (TREE_CODE (t))
9362 case VAR_DECL:
9363 if (addr)
9364 return t;
9365 /* else fall through. */
9366 case CONST_DECL:
9367 r = integral_constant_value (t);
9368 if (TREE_CODE (r) == TARGET_EXPR
9369 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9370 r = TARGET_EXPR_INITIAL (r);
9371 if (DECL_P (r))
9373 if (!allow_non_constant)
9374 non_const_var_error (r);
9375 *non_constant_p = true;
9377 break;
9379 case FUNCTION_DECL:
9380 case TEMPLATE_DECL:
9381 case LABEL_DECL:
9382 return t;
9384 case PARM_DECL:
9385 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9387 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9389 if (!allow_non_constant)
9390 sorry ("use of the value of the object being constructed "
9391 "in a constant expression");
9392 *non_constant_p = true;
9394 else
9395 r = lookup_parameter_binding (call, t);
9397 else if (addr)
9398 /* Defer in case this is only used for its type. */;
9399 else
9401 if (!allow_non_constant)
9402 error ("%qE is not a constant expression", t);
9403 *non_constant_p = true;
9405 break;
9407 case CALL_EXPR:
9408 case AGGR_INIT_EXPR:
9409 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9410 non_constant_p, overflow_p);
9411 break;
9413 case TARGET_EXPR:
9414 if (!literal_type_p (TREE_TYPE (t)))
9416 if (!allow_non_constant)
9418 error ("temporary of non-literal type %qT in a "
9419 "constant expression", TREE_TYPE (t));
9420 explain_non_literal_class (TREE_TYPE (t));
9422 *non_constant_p = true;
9423 break;
9425 /* else fall through. */
9426 case INIT_EXPR:
9427 /* Pass false for 'addr' because these codes indicate
9428 initialization of a temporary. */
9429 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9430 allow_non_constant, false,
9431 non_constant_p, overflow_p);
9432 if (!*non_constant_p)
9433 /* Adjust the type of the result to the type of the temporary. */
9434 r = adjust_temp_type (TREE_TYPE (t), r);
9435 break;
9437 case SCOPE_REF:
9438 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9439 allow_non_constant, addr,
9440 non_constant_p, overflow_p);
9441 break;
9443 case RETURN_EXPR:
9444 case NON_LVALUE_EXPR:
9445 case TRY_CATCH_EXPR:
9446 case CLEANUP_POINT_EXPR:
9447 case MUST_NOT_THROW_EXPR:
9448 case SAVE_EXPR:
9449 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9450 allow_non_constant, addr,
9451 non_constant_p, overflow_p);
9452 break;
9454 /* These differ from cxx_eval_unary_expression in that this doesn't
9455 check for a constant operand or result; an address can be
9456 constant without its operand being, and vice versa. */
9457 case INDIRECT_REF:
9458 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9459 non_constant_p, overflow_p);
9460 break;
9462 case ADDR_EXPR:
9464 tree oldop = TREE_OPERAND (t, 0);
9465 tree op = cxx_eval_constant_expression (call, oldop,
9466 allow_non_constant,
9467 /*addr*/true,
9468 non_constant_p, overflow_p);
9469 /* Don't VERIFY_CONSTANT here. */
9470 if (*non_constant_p)
9471 return t;
9472 /* This function does more aggressive folding than fold itself. */
9473 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9474 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9475 return t;
9476 break;
9479 case REALPART_EXPR:
9480 case IMAGPART_EXPR:
9481 case CONJ_EXPR:
9482 case FIX_TRUNC_EXPR:
9483 case FLOAT_EXPR:
9484 case NEGATE_EXPR:
9485 case ABS_EXPR:
9486 case BIT_NOT_EXPR:
9487 case TRUTH_NOT_EXPR:
9488 case FIXED_CONVERT_EXPR:
9489 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9490 non_constant_p, overflow_p);
9491 break;
9493 case SIZEOF_EXPR:
9494 if (SIZEOF_EXPR_TYPE_P (t))
9495 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9496 SIZEOF_EXPR, false);
9497 else if (TYPE_P (TREE_OPERAND (t, 0)))
9498 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9499 false);
9500 else
9501 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9502 false);
9503 if (r == error_mark_node)
9504 r = size_one_node;
9505 VERIFY_CONSTANT (r);
9506 break;
9508 case COMPOUND_EXPR:
9510 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9511 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9512 introduced by build_call_a. */
9513 tree op0 = TREE_OPERAND (t, 0);
9514 tree op1 = TREE_OPERAND (t, 1);
9515 STRIP_NOPS (op1);
9516 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9517 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9518 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9519 addr, non_constant_p, overflow_p);
9520 else
9522 /* Check that the LHS is constant and then discard it. */
9523 cxx_eval_constant_expression (call, op0, allow_non_constant,
9524 false, non_constant_p, overflow_p);
9525 op1 = TREE_OPERAND (t, 1);
9526 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9527 addr, non_constant_p, overflow_p);
9530 break;
9532 case POINTER_PLUS_EXPR:
9533 case PLUS_EXPR:
9534 case MINUS_EXPR:
9535 case MULT_EXPR:
9536 case TRUNC_DIV_EXPR:
9537 case CEIL_DIV_EXPR:
9538 case FLOOR_DIV_EXPR:
9539 case ROUND_DIV_EXPR:
9540 case TRUNC_MOD_EXPR:
9541 case CEIL_MOD_EXPR:
9542 case ROUND_MOD_EXPR:
9543 case RDIV_EXPR:
9544 case EXACT_DIV_EXPR:
9545 case MIN_EXPR:
9546 case MAX_EXPR:
9547 case LSHIFT_EXPR:
9548 case RSHIFT_EXPR:
9549 case LROTATE_EXPR:
9550 case RROTATE_EXPR:
9551 case BIT_IOR_EXPR:
9552 case BIT_XOR_EXPR:
9553 case BIT_AND_EXPR:
9554 case TRUTH_XOR_EXPR:
9555 case LT_EXPR:
9556 case LE_EXPR:
9557 case GT_EXPR:
9558 case GE_EXPR:
9559 case EQ_EXPR:
9560 case NE_EXPR:
9561 case UNORDERED_EXPR:
9562 case ORDERED_EXPR:
9563 case UNLT_EXPR:
9564 case UNLE_EXPR:
9565 case UNGT_EXPR:
9566 case UNGE_EXPR:
9567 case UNEQ_EXPR:
9568 case LTGT_EXPR:
9569 case RANGE_EXPR:
9570 case COMPLEX_EXPR:
9571 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9572 non_constant_p, overflow_p);
9573 break;
9575 /* fold can introduce non-IF versions of these; still treat them as
9576 short-circuiting. */
9577 case TRUTH_AND_EXPR:
9578 case TRUTH_ANDIF_EXPR:
9579 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9580 boolean_true_node,
9581 allow_non_constant, addr,
9582 non_constant_p, overflow_p);
9583 break;
9585 case TRUTH_OR_EXPR:
9586 case TRUTH_ORIF_EXPR:
9587 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9588 boolean_false_node,
9589 allow_non_constant, addr,
9590 non_constant_p, overflow_p);
9591 break;
9593 case ARRAY_REF:
9594 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9595 non_constant_p, overflow_p);
9596 break;
9598 case COMPONENT_REF:
9599 if (is_overloaded_fn (t))
9601 /* We can only get here in checking mode via
9602 build_non_dependent_expr, because any expression that
9603 calls or takes the address of the function will have
9604 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9605 gcc_checking_assert (allow_non_constant);
9606 *non_constant_p = true;
9607 return t;
9609 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9610 non_constant_p, overflow_p);
9611 break;
9613 case BIT_FIELD_REF:
9614 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9615 non_constant_p, overflow_p);
9616 break;
9618 case COND_EXPR:
9619 case VEC_COND_EXPR:
9620 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9621 non_constant_p, overflow_p);
9622 break;
9624 case CONSTRUCTOR:
9625 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9626 non_constant_p, overflow_p);
9627 break;
9629 case VEC_INIT_EXPR:
9630 /* We can get this in a defaulted constructor for a class with a
9631 non-static data member of array type. Either the initializer will
9632 be NULL, meaning default-initialization, or it will be an lvalue
9633 or xvalue of the same type, meaning direct-initialization from the
9634 corresponding member. */
9635 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9636 non_constant_p, overflow_p);
9637 break;
9639 case FMA_EXPR:
9640 case VEC_PERM_EXPR:
9641 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9642 non_constant_p, overflow_p);
9643 break;
9645 case CONVERT_EXPR:
9646 case VIEW_CONVERT_EXPR:
9647 case NOP_EXPR:
9649 tree oldop = TREE_OPERAND (t, 0);
9650 tree op = cxx_eval_constant_expression (call, oldop,
9651 allow_non_constant, addr,
9652 non_constant_p, overflow_p);
9653 if (*non_constant_p)
9654 return t;
9655 if (POINTER_TYPE_P (TREE_TYPE (t))
9656 && TREE_CODE (op) == INTEGER_CST
9657 && !integer_zerop (op))
9659 if (!allow_non_constant)
9660 error_at (EXPR_LOC_OR_LOC (t, input_location),
9661 "reinterpret_cast from integer to pointer");
9662 *non_constant_p = true;
9663 return t;
9665 if (op == oldop)
9666 /* We didn't fold at the top so we could check for ptr-int
9667 conversion. */
9668 return fold (t);
9669 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9670 /* Conversion of an out-of-range value has implementation-defined
9671 behavior; the language considers it different from arithmetic
9672 overflow, which is undefined. */
9673 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9674 TREE_OVERFLOW (r) = false;
9676 break;
9678 case EMPTY_CLASS_EXPR:
9679 /* This is good enough for a function argument that might not get
9680 used, and they can't do anything with it, so just return it. */
9681 return t;
9683 case LAMBDA_EXPR:
9684 case PREINCREMENT_EXPR:
9685 case POSTINCREMENT_EXPR:
9686 case PREDECREMENT_EXPR:
9687 case POSTDECREMENT_EXPR:
9688 case NEW_EXPR:
9689 case VEC_NEW_EXPR:
9690 case DELETE_EXPR:
9691 case VEC_DELETE_EXPR:
9692 case THROW_EXPR:
9693 case MODIFY_EXPR:
9694 case MODOP_EXPR:
9695 /* GCC internal stuff. */
9696 case VA_ARG_EXPR:
9697 case OBJ_TYPE_REF:
9698 case WITH_CLEANUP_EXPR:
9699 case STATEMENT_LIST:
9700 case BIND_EXPR:
9701 case NON_DEPENDENT_EXPR:
9702 case BASELINK:
9703 case EXPR_STMT:
9704 case OFFSET_REF:
9705 if (!allow_non_constant)
9706 error_at (EXPR_LOC_OR_LOC (t, input_location),
9707 "expression %qE is not a constant-expression", t);
9708 *non_constant_p = true;
9709 break;
9711 default:
9712 internal_error ("unexpected expression %qE of kind %s", t,
9713 get_tree_code_name (TREE_CODE (t)));
9714 *non_constant_p = true;
9715 break;
9718 if (r == error_mark_node)
9719 *non_constant_p = true;
9721 if (*non_constant_p)
9722 return t;
9723 else
9724 return r;
9727 static tree
9728 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9730 bool non_constant_p = false;
9731 bool overflow_p = false;
9732 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9733 false, &non_constant_p, &overflow_p);
9735 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9737 if (TREE_CODE (t) != CONSTRUCTOR
9738 && cp_has_mutable_p (TREE_TYPE (t)))
9740 /* We allow a mutable type if the original expression was a
9741 CONSTRUCTOR so that we can do aggregate initialization of
9742 constexpr variables. */
9743 if (!allow_non_constant)
9744 error ("%qT cannot be the type of a complete constant expression "
9745 "because it has mutable sub-objects", TREE_TYPE (t));
9746 non_constant_p = true;
9749 /* Technically we should check this for all subexpressions, but that
9750 runs into problems with our internal representation of pointer
9751 subtraction and the 5.19 rules are still in flux. */
9752 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9753 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9754 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9756 if (!allow_non_constant)
9757 error ("conversion from pointer type %qT "
9758 "to arithmetic type %qT in a constant-expression",
9759 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9760 non_constant_p = true;
9763 if (!non_constant_p && overflow_p)
9764 non_constant_p = true;
9766 if (non_constant_p && !allow_non_constant)
9767 return error_mark_node;
9768 else if (non_constant_p && TREE_CONSTANT (r))
9770 /* This isn't actually constant, so unset TREE_CONSTANT. */
9771 if (EXPR_P (r))
9772 r = copy_node (r);
9773 else if (TREE_CODE (r) == CONSTRUCTOR)
9774 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9775 else
9776 r = build_nop (TREE_TYPE (r), r);
9777 TREE_CONSTANT (r) = false;
9779 else if (non_constant_p || r == t)
9780 return t;
9782 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9784 if (TREE_CODE (t) == TARGET_EXPR
9785 && TARGET_EXPR_INITIAL (t) == r)
9786 return t;
9787 else
9789 r = get_target_expr (r);
9790 TREE_CONSTANT (r) = true;
9791 return r;
9794 else
9795 return r;
9798 /* Returns true if T is a valid subexpression of a constant expression,
9799 even if it isn't itself a constant expression. */
9801 bool
9802 is_sub_constant_expr (tree t)
9804 bool non_constant_p = false;
9805 bool overflow_p = false;
9806 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9807 &overflow_p);
9808 return !non_constant_p && !overflow_p;
9811 /* If T represents a constant expression returns its reduced value.
9812 Otherwise return error_mark_node. If T is dependent, then
9813 return NULL. */
9815 tree
9816 cxx_constant_value (tree t)
9818 return cxx_eval_outermost_constant_expr (t, false);
9821 /* If T is a constant expression, returns its reduced value.
9822 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9823 Otherwise, returns a version of T without TREE_CONSTANT. */
9825 tree
9826 maybe_constant_value (tree t)
9828 tree r;
9830 if (instantiation_dependent_expression_p (t)
9831 || type_unknown_p (t)
9832 || BRACE_ENCLOSED_INITIALIZER_P (t)
9833 || !potential_constant_expression (t))
9835 if (TREE_OVERFLOW_P (t))
9837 t = build_nop (TREE_TYPE (t), t);
9838 TREE_CONSTANT (t) = false;
9840 return t;
9843 r = cxx_eval_outermost_constant_expr (t, true);
9844 #ifdef ENABLE_CHECKING
9845 /* cp_tree_equal looks through NOPs, so allow them. */
9846 gcc_assert (r == t
9847 || CONVERT_EXPR_P (t)
9848 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9849 || !cp_tree_equal (r, t));
9850 #endif
9851 return r;
9854 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9855 than wrapped in a TARGET_EXPR. */
9857 tree
9858 maybe_constant_init (tree t)
9860 t = maybe_constant_value (t);
9861 if (TREE_CODE (t) == TARGET_EXPR)
9863 tree init = TARGET_EXPR_INITIAL (t);
9864 if (TREE_CODE (init) == CONSTRUCTOR)
9865 t = init;
9867 return t;
9870 #if 0
9871 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9872 /* Return true if the object referred to by REF has automatic or thread
9873 local storage. */
9875 enum { ck_ok, ck_bad, ck_unknown };
9876 static int
9877 check_automatic_or_tls (tree ref)
9879 enum machine_mode mode;
9880 HOST_WIDE_INT bitsize, bitpos;
9881 tree offset;
9882 int volatilep = 0, unsignedp = 0;
9883 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9884 &mode, &unsignedp, &volatilep, false);
9885 duration_kind dk;
9887 /* If there isn't a decl in the middle, we don't know the linkage here,
9888 and this isn't a constant expression anyway. */
9889 if (!DECL_P (decl))
9890 return ck_unknown;
9891 dk = decl_storage_duration (decl);
9892 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9894 #endif
9896 /* Return true if T denotes a potentially constant expression. Issue
9897 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9898 an lvalue-rvalue conversion is implied.
9900 C++0x [expr.const] used to say
9902 6 An expression is a potential constant expression if it is
9903 a constant expression where all occurrences of function
9904 parameters are replaced by arbitrary constant expressions
9905 of the appropriate type.
9907 2 A conditional expression is a constant expression unless it
9908 involves one of the following as a potentially evaluated
9909 subexpression (3.2), but subexpressions of logical AND (5.14),
9910 logical OR (5.15), and conditional (5.16) operations that are
9911 not evaluated are not considered. */
9913 static bool
9914 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
9916 enum { any = false, rval = true };
9917 int i;
9918 tree tmp;
9920 if (t == error_mark_node)
9921 return false;
9922 if (t == NULL_TREE)
9923 return true;
9924 if (TREE_THIS_VOLATILE (t))
9926 if (flags & tf_error)
9927 error ("expression %qE has side-effects", t);
9928 return false;
9930 if (CONSTANT_CLASS_P (t))
9931 return true;
9933 switch (TREE_CODE (t))
9935 case FUNCTION_DECL:
9936 case BASELINK:
9937 case TEMPLATE_DECL:
9938 case OVERLOAD:
9939 case TEMPLATE_ID_EXPR:
9940 case LABEL_DECL:
9941 case LABEL_EXPR:
9942 case CONST_DECL:
9943 case SIZEOF_EXPR:
9944 case ALIGNOF_EXPR:
9945 case OFFSETOF_EXPR:
9946 case NOEXCEPT_EXPR:
9947 case TEMPLATE_PARM_INDEX:
9948 case TRAIT_EXPR:
9949 case IDENTIFIER_NODE:
9950 case USERDEF_LITERAL:
9951 /* We can see a FIELD_DECL in a pointer-to-member expression. */
9952 case FIELD_DECL:
9953 case PARM_DECL:
9954 case USING_DECL:
9955 return true;
9957 case AGGR_INIT_EXPR:
9958 case CALL_EXPR:
9959 /* -- an invocation of a function other than a constexpr function
9960 or a constexpr constructor. */
9962 tree fun = get_function_named_in_call (t);
9963 const int nargs = call_expr_nargs (t);
9964 i = 0;
9966 if (is_overloaded_fn (fun))
9968 if (TREE_CODE (fun) == FUNCTION_DECL)
9970 if (builtin_valid_in_constant_expr_p (fun))
9971 return true;
9972 if (!DECL_DECLARED_CONSTEXPR_P (fun)
9973 /* Allow any built-in function; if the expansion
9974 isn't constant, we'll deal with that then. */
9975 && !is_builtin_fn (fun))
9977 if (flags & tf_error)
9979 error_at (EXPR_LOC_OR_LOC (t, input_location),
9980 "call to non-constexpr function %qD", fun);
9981 explain_invalid_constexpr_fn (fun);
9983 return false;
9985 /* A call to a non-static member function takes the address
9986 of the object as the first argument. But in a constant
9987 expression the address will be folded away, so look
9988 through it now. */
9989 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
9990 && !DECL_CONSTRUCTOR_P (fun))
9992 tree x = get_nth_callarg (t, 0);
9993 if (is_this_parameter (x))
9995 if (DECL_CONTEXT (x) == NULL_TREE
9996 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
9998 if (flags & tf_error)
9999 sorry ("calling a member function of the "
10000 "object being constructed in a constant "
10001 "expression");
10002 return false;
10004 /* Otherwise OK. */;
10006 else if (!potential_constant_expression_1 (x, rval, flags))
10007 return false;
10008 i = 1;
10011 else
10013 if (!potential_constant_expression_1 (fun, true, flags))
10014 return false;
10015 fun = get_first_fn (fun);
10017 /* Skip initial arguments to base constructors. */
10018 if (DECL_BASE_CONSTRUCTOR_P (fun))
10019 i = num_artificial_parms_for (fun);
10020 fun = DECL_ORIGIN (fun);
10022 else
10024 if (potential_constant_expression_1 (fun, rval, flags))
10025 /* Might end up being a constant function pointer. */;
10026 else
10027 return false;
10029 for (; i < nargs; ++i)
10031 tree x = get_nth_callarg (t, i);
10032 if (!potential_constant_expression_1 (x, rval, flags))
10033 return false;
10035 return true;
10038 case NON_LVALUE_EXPR:
10039 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10040 -- an lvalue of integral type that refers to a non-volatile
10041 const variable or static data member initialized with
10042 constant expressions, or
10044 -- an lvalue of literal type that refers to non-volatile
10045 object defined with constexpr, or that refers to a
10046 sub-object of such an object; */
10047 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10049 case VAR_DECL:
10050 if (want_rval && !decl_constant_var_p (t)
10051 && !dependent_type_p (TREE_TYPE (t)))
10053 if (flags & tf_error)
10054 non_const_var_error (t);
10055 return false;
10057 return true;
10059 case NOP_EXPR:
10060 case CONVERT_EXPR:
10061 case VIEW_CONVERT_EXPR:
10062 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10063 may change to something more specific to type-punning (DR 1312). */
10065 tree from = TREE_OPERAND (t, 0);
10066 if (POINTER_TYPE_P (TREE_TYPE (t))
10067 && TREE_CODE (from) == INTEGER_CST
10068 && !integer_zerop (from))
10070 if (flags & tf_error)
10071 error_at (EXPR_LOC_OR_LOC (t, input_location),
10072 "reinterpret_cast from integer to pointer");
10073 return false;
10075 return (potential_constant_expression_1
10076 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10079 case ADDR_EXPR:
10080 /* -- a unary operator & that is applied to an lvalue that
10081 designates an object with thread or automatic storage
10082 duration; */
10083 t = TREE_OPERAND (t, 0);
10084 #if 0
10085 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10086 any checking here, as we might dereference the pointer later. If
10087 we remove this code, also remove check_automatic_or_tls. */
10088 i = check_automatic_or_tls (t);
10089 if (i == ck_ok)
10090 return true;
10091 if (i == ck_bad)
10093 if (flags & tf_error)
10094 error ("address-of an object %qE with thread local or "
10095 "automatic storage is not a constant expression", t);
10096 return false;
10098 #endif
10099 return potential_constant_expression_1 (t, any, flags);
10101 case COMPONENT_REF:
10102 case BIT_FIELD_REF:
10103 case ARROW_EXPR:
10104 case OFFSET_REF:
10105 /* -- a class member access unless its postfix-expression is
10106 of literal type or of pointer to literal type. */
10107 /* This test would be redundant, as it follows from the
10108 postfix-expression being a potential constant expression. */
10109 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10110 want_rval, flags);
10112 case EXPR_PACK_EXPANSION:
10113 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10114 want_rval, flags);
10116 case INDIRECT_REF:
10118 tree x = TREE_OPERAND (t, 0);
10119 STRIP_NOPS (x);
10120 if (is_this_parameter (x))
10122 if (DECL_CONTEXT (x)
10123 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10125 if (flags & tf_error)
10126 error ("use of %<this%> in a constant expression");
10127 return false;
10129 if (want_rval && DECL_CONTEXT (x)
10130 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10132 if (flags & tf_error)
10133 sorry ("use of the value of the object being constructed "
10134 "in a constant expression");
10135 return false;
10137 return true;
10139 return potential_constant_expression_1 (x, rval, flags);
10142 case LAMBDA_EXPR:
10143 case DYNAMIC_CAST_EXPR:
10144 case PSEUDO_DTOR_EXPR:
10145 case PREINCREMENT_EXPR:
10146 case POSTINCREMENT_EXPR:
10147 case PREDECREMENT_EXPR:
10148 case POSTDECREMENT_EXPR:
10149 case NEW_EXPR:
10150 case VEC_NEW_EXPR:
10151 case DELETE_EXPR:
10152 case VEC_DELETE_EXPR:
10153 case THROW_EXPR:
10154 case MODIFY_EXPR:
10155 case MODOP_EXPR:
10156 case OMP_ATOMIC:
10157 case OMP_ATOMIC_READ:
10158 case OMP_ATOMIC_CAPTURE_OLD:
10159 case OMP_ATOMIC_CAPTURE_NEW:
10160 /* GCC internal stuff. */
10161 case VA_ARG_EXPR:
10162 case OBJ_TYPE_REF:
10163 case WITH_CLEANUP_EXPR:
10164 case CLEANUP_POINT_EXPR:
10165 case MUST_NOT_THROW_EXPR:
10166 case TRY_CATCH_EXPR:
10167 case STATEMENT_LIST:
10168 /* Don't bother trying to define a subset of statement-expressions to
10169 be constant-expressions, at least for now. */
10170 case STMT_EXPR:
10171 case EXPR_STMT:
10172 case BIND_EXPR:
10173 case TRANSACTION_EXPR:
10174 case IF_STMT:
10175 case DO_STMT:
10176 case FOR_STMT:
10177 case WHILE_STMT:
10178 if (flags & tf_error)
10179 error ("expression %qE is not a constant-expression", t);
10180 return false;
10182 case TYPEID_EXPR:
10183 /* -- a typeid expression whose operand is of polymorphic
10184 class type; */
10186 tree e = TREE_OPERAND (t, 0);
10187 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10188 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10190 if (flags & tf_error)
10191 error ("typeid-expression is not a constant expression "
10192 "because %qE is of polymorphic type", e);
10193 return false;
10195 return true;
10198 case MINUS_EXPR:
10199 /* -- a subtraction where both operands are pointers. */
10200 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10201 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10203 if (flags & tf_error)
10204 error ("difference of two pointer expressions is not "
10205 "a constant expression");
10206 return false;
10208 want_rval = true;
10209 goto binary;
10211 case LT_EXPR:
10212 case LE_EXPR:
10213 case GT_EXPR:
10214 case GE_EXPR:
10215 case EQ_EXPR:
10216 case NE_EXPR:
10217 /* -- a relational or equality operator where at least
10218 one of the operands is a pointer. */
10219 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10220 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10222 if (flags & tf_error)
10223 error ("pointer comparison expression is not a "
10224 "constant expression");
10225 return false;
10227 want_rval = true;
10228 goto binary;
10230 case BIT_NOT_EXPR:
10231 /* A destructor. */
10232 if (TYPE_P (TREE_OPERAND (t, 0)))
10233 return true;
10234 /* else fall through. */
10236 case REALPART_EXPR:
10237 case IMAGPART_EXPR:
10238 case CONJ_EXPR:
10239 case SAVE_EXPR:
10240 case FIX_TRUNC_EXPR:
10241 case FLOAT_EXPR:
10242 case NEGATE_EXPR:
10243 case ABS_EXPR:
10244 case TRUTH_NOT_EXPR:
10245 case FIXED_CONVERT_EXPR:
10246 case UNARY_PLUS_EXPR:
10247 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10248 flags);
10250 case CAST_EXPR:
10251 case CONST_CAST_EXPR:
10252 case STATIC_CAST_EXPR:
10253 case REINTERPRET_CAST_EXPR:
10254 case IMPLICIT_CONV_EXPR:
10255 if (cxx_dialect < cxx11
10256 && !dependent_type_p (TREE_TYPE (t))
10257 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10258 /* In C++98, a conversion to non-integral type can't be part of a
10259 constant expression. */
10261 if (flags & tf_error)
10262 error ("cast to non-integral type %qT in a constant expression",
10263 TREE_TYPE (t));
10264 return false;
10267 return (potential_constant_expression_1
10268 (TREE_OPERAND (t, 0),
10269 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10271 case PAREN_EXPR:
10272 case NON_DEPENDENT_EXPR:
10273 /* For convenience. */
10274 case RETURN_EXPR:
10275 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10276 want_rval, flags);
10278 case SCOPE_REF:
10279 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10280 want_rval, flags);
10282 case TARGET_EXPR:
10283 if (!literal_type_p (TREE_TYPE (t)))
10285 if (flags & tf_error)
10287 error ("temporary of non-literal type %qT in a "
10288 "constant expression", TREE_TYPE (t));
10289 explain_non_literal_class (TREE_TYPE (t));
10291 return false;
10293 case INIT_EXPR:
10294 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10295 rval, flags);
10297 case CONSTRUCTOR:
10299 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10300 constructor_elt *ce;
10301 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10302 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10303 return false;
10304 return true;
10307 case TREE_LIST:
10309 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10310 || DECL_P (TREE_PURPOSE (t)));
10311 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10312 flags))
10313 return false;
10314 if (TREE_CHAIN (t) == NULL_TREE)
10315 return true;
10316 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10317 flags);
10320 case TRUNC_DIV_EXPR:
10321 case CEIL_DIV_EXPR:
10322 case FLOOR_DIV_EXPR:
10323 case ROUND_DIV_EXPR:
10324 case TRUNC_MOD_EXPR:
10325 case CEIL_MOD_EXPR:
10326 case ROUND_MOD_EXPR:
10328 tree denom = TREE_OPERAND (t, 1);
10329 if (!potential_constant_expression_1 (denom, rval, flags))
10330 return false;
10331 /* We can't call cxx_eval_outermost_constant_expr on an expression
10332 that hasn't been through fold_non_dependent_expr yet. */
10333 if (!processing_template_decl)
10334 denom = cxx_eval_outermost_constant_expr (denom, true);
10335 if (integer_zerop (denom))
10337 if (flags & tf_error)
10338 error ("division by zero is not a constant-expression");
10339 return false;
10341 else
10343 want_rval = true;
10344 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10345 want_rval, flags);
10349 case COMPOUND_EXPR:
10351 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10352 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10353 introduced by build_call_a. */
10354 tree op0 = TREE_OPERAND (t, 0);
10355 tree op1 = TREE_OPERAND (t, 1);
10356 STRIP_NOPS (op1);
10357 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10358 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10359 return potential_constant_expression_1 (op0, want_rval, flags);
10360 else
10361 goto binary;
10364 /* If the first operand is the non-short-circuit constant, look at
10365 the second operand; otherwise we only care about the first one for
10366 potentiality. */
10367 case TRUTH_AND_EXPR:
10368 case TRUTH_ANDIF_EXPR:
10369 tmp = boolean_true_node;
10370 goto truth;
10371 case TRUTH_OR_EXPR:
10372 case TRUTH_ORIF_EXPR:
10373 tmp = boolean_false_node;
10374 truth:
10376 tree op = TREE_OPERAND (t, 0);
10377 if (!potential_constant_expression_1 (op, rval, flags))
10378 return false;
10379 if (!processing_template_decl)
10380 op = cxx_eval_outermost_constant_expr (op, true);
10381 if (tree_int_cst_equal (op, tmp))
10382 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10383 else
10384 return true;
10387 case PLUS_EXPR:
10388 case MULT_EXPR:
10389 case POINTER_PLUS_EXPR:
10390 case RDIV_EXPR:
10391 case EXACT_DIV_EXPR:
10392 case MIN_EXPR:
10393 case MAX_EXPR:
10394 case LSHIFT_EXPR:
10395 case RSHIFT_EXPR:
10396 case LROTATE_EXPR:
10397 case RROTATE_EXPR:
10398 case BIT_IOR_EXPR:
10399 case BIT_XOR_EXPR:
10400 case BIT_AND_EXPR:
10401 case TRUTH_XOR_EXPR:
10402 case UNORDERED_EXPR:
10403 case ORDERED_EXPR:
10404 case UNLT_EXPR:
10405 case UNLE_EXPR:
10406 case UNGT_EXPR:
10407 case UNGE_EXPR:
10408 case UNEQ_EXPR:
10409 case LTGT_EXPR:
10410 case RANGE_EXPR:
10411 case COMPLEX_EXPR:
10412 want_rval = true;
10413 /* Fall through. */
10414 case ARRAY_REF:
10415 case ARRAY_RANGE_REF:
10416 case MEMBER_REF:
10417 case DOTSTAR_EXPR:
10418 binary:
10419 for (i = 0; i < 2; ++i)
10420 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10421 want_rval, flags))
10422 return false;
10423 return true;
10425 case CILK_SYNC_STMT:
10426 case CILK_SPAWN_STMT:
10427 case ARRAY_NOTATION_REF:
10428 return false;
10430 case FMA_EXPR:
10431 case VEC_PERM_EXPR:
10432 for (i = 0; i < 3; ++i)
10433 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10434 true, flags))
10435 return false;
10436 return true;
10438 case COND_EXPR:
10439 case VEC_COND_EXPR:
10440 /* If the condition is a known constant, we know which of the legs we
10441 care about; otherwise we only require that the condition and
10442 either of the legs be potentially constant. */
10443 tmp = TREE_OPERAND (t, 0);
10444 if (!potential_constant_expression_1 (tmp, rval, flags))
10445 return false;
10446 if (!processing_template_decl)
10447 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10448 if (integer_zerop (tmp))
10449 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10450 want_rval, flags);
10451 else if (TREE_CODE (tmp) == INTEGER_CST)
10452 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10453 want_rval, flags);
10454 for (i = 1; i < 3; ++i)
10455 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10456 want_rval, tf_none))
10457 return true;
10458 if (flags & tf_error)
10459 error ("expression %qE is not a constant-expression", t);
10460 return false;
10462 case VEC_INIT_EXPR:
10463 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10464 return true;
10465 if (flags & tf_error)
10467 error ("non-constant array initialization");
10468 diagnose_non_constexpr_vec_init (t);
10470 return false;
10472 default:
10473 if (objc_is_property_ref (t))
10474 return false;
10476 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10477 gcc_unreachable();
10478 return false;
10482 /* The main entry point to the above. */
10484 bool
10485 potential_constant_expression (tree t)
10487 return potential_constant_expression_1 (t, false, tf_none);
10490 /* As above, but require a constant rvalue. */
10492 bool
10493 potential_rvalue_constant_expression (tree t)
10495 return potential_constant_expression_1 (t, true, tf_none);
10498 /* Like above, but complain about non-constant expressions. */
10500 bool
10501 require_potential_constant_expression (tree t)
10503 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10506 /* Cross product of the above. */
10508 bool
10509 require_potential_rvalue_constant_expression (tree t)
10511 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10514 /* Insert the deduced return type for an auto function. */
10516 void
10517 apply_deduced_return_type (tree fco, tree return_type)
10519 tree result;
10521 if (return_type == error_mark_node)
10522 return;
10524 if (LAMBDA_FUNCTION_P (fco))
10526 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10527 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10530 if (DECL_CONV_FN_P (fco))
10531 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10533 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10535 result = DECL_RESULT (fco);
10536 if (result == NULL_TREE)
10537 return;
10538 if (TREE_TYPE (result) == return_type)
10539 return;
10541 /* We already have a DECL_RESULT from start_preparsed_function.
10542 Now we need to redo the work it and allocate_struct_function
10543 did to reflect the new type. */
10544 gcc_assert (current_function_decl == fco);
10545 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10546 TYPE_MAIN_VARIANT (return_type));
10547 DECL_ARTIFICIAL (result) = 1;
10548 DECL_IGNORED_P (result) = 1;
10549 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10550 result);
10552 DECL_RESULT (fco) = result;
10554 if (!processing_template_decl)
10556 bool aggr = aggregate_value_p (result, fco);
10557 #ifdef PCC_STATIC_STRUCT_RETURN
10558 cfun->returns_pcc_struct = aggr;
10559 #endif
10560 cfun->returns_struct = aggr;
10565 /* DECL is a local variable or parameter from the surrounding scope of a
10566 lambda-expression. Returns the decltype for a use of the capture field
10567 for DECL even if it hasn't been captured yet. */
10569 static tree
10570 capture_decltype (tree decl)
10572 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10573 /* FIXME do lookup instead of list walk? */
10574 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10575 tree type;
10577 if (cap)
10578 type = TREE_TYPE (TREE_PURPOSE (cap));
10579 else
10580 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10582 case CPLD_NONE:
10583 error ("%qD is not captured", decl);
10584 return error_mark_node;
10586 case CPLD_COPY:
10587 type = TREE_TYPE (decl);
10588 if (TREE_CODE (type) == REFERENCE_TYPE
10589 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10590 type = TREE_TYPE (type);
10591 break;
10593 case CPLD_REFERENCE:
10594 type = TREE_TYPE (decl);
10595 if (TREE_CODE (type) != REFERENCE_TYPE)
10596 type = build_reference_type (TREE_TYPE (decl));
10597 break;
10599 default:
10600 gcc_unreachable ();
10603 if (TREE_CODE (type) != REFERENCE_TYPE)
10605 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10606 type = cp_build_qualified_type (type, (cp_type_quals (type)
10607 |TYPE_QUAL_CONST));
10608 type = build_reference_type (type);
10610 return type;
10613 #include "gt-cp-semantics.h"