2015-07-28 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / semantics.c
blob44f9f7acaa36b5457601376adb13f9bf7c5cda79
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-2015 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 "alias.h"
31 #include "tree.h"
32 #include "stmt.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "cp-tree.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "tree-inline.h"
40 #include "intl.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "timevar.h"
44 #include "diagnostic.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "cgraph.h"
48 #include "tree-iterator.h"
49 #include "target.h"
50 #include "gimplify.h"
51 #include "bitmap.h"
52 #include "omp-low.h"
53 #include "builtins.h"
54 #include "convert.h"
55 #include "gomp-constants.h"
57 /* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
60 degenerate form of parsing. */
62 static tree maybe_convert_cond (tree);
63 static tree finalize_nrv_r (tree *, int *, void *);
64 static tree capture_decltype (tree);
67 /* Deferred Access Checking Overview
68 ---------------------------------
70 Most C++ expressions and declarations require access checking
71 to be performed during parsing. However, in several cases,
72 this has to be treated differently.
74 For member declarations, access checking has to be deferred
75 until more information about the declaration is known. For
76 example:
78 class A {
79 typedef int X;
80 public:
81 X f();
84 A::X A::f();
85 A::X g();
87 When we are parsing the function return type `A::X', we don't
88 really know if this is allowed until we parse the function name.
90 Furthermore, some contexts require that access checking is
91 never performed at all. These include class heads, and template
92 instantiations.
94 Typical use of access checking functions is described here:
96 1. When we enter a context that requires certain access checking
97 mode, the function `push_deferring_access_checks' is called with
98 DEFERRING argument specifying the desired mode. Access checking
99 may be performed immediately (dk_no_deferred), deferred
100 (dk_deferred), or not performed (dk_no_check).
102 2. When a declaration such as a type, or a variable, is encountered,
103 the function `perform_or_defer_access_check' is called. It
104 maintains a vector of all deferred checks.
106 3. The global `current_class_type' or `current_function_decl' is then
107 setup by the parser. `enforce_access' relies on these information
108 to check access.
110 4. Upon exiting the context mentioned in step 1,
111 `perform_deferred_access_checks' is called to check all declaration
112 stored in the vector. `pop_deferring_access_checks' is then
113 called to restore the previous access checking mode.
115 In case of parsing error, we simply call `pop_deferring_access_checks'
116 without `perform_deferred_access_checks'. */
118 typedef struct GTY(()) deferred_access {
119 /* A vector representing name-lookups for which we have deferred
120 checking access controls. We cannot check the accessibility of
121 names used in a decl-specifier-seq until we know what is being
122 declared because code like:
124 class A {
125 class B {};
126 B* f();
129 A::B* A::f() { return 0; }
131 is valid, even though `A::B' is not generally accessible. */
132 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
134 /* The current mode of access checks. */
135 enum deferring_kind deferring_access_checks_kind;
137 } deferred_access;
139 /* Data for deferred access checking. */
140 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
141 static GTY(()) unsigned deferred_access_no_check;
143 /* Save the current deferred access states and start deferred
144 access checking iff DEFER_P is true. */
146 void
147 push_deferring_access_checks (deferring_kind deferring)
149 /* For context like template instantiation, access checking
150 disabling applies to all nested context. */
151 if (deferred_access_no_check || deferring == dk_no_check)
152 deferred_access_no_check++;
153 else
155 deferred_access e = {NULL, deferring};
156 vec_safe_push (deferred_access_stack, e);
160 /* Save the current deferred access states and start deferred access
161 checking, continuing the set of deferred checks in CHECKS. */
163 void
164 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
166 push_deferring_access_checks (dk_deferred);
167 if (!deferred_access_no_check)
168 deferred_access_stack->last().deferred_access_checks = checks;
171 /* Resume deferring access checks again after we stopped doing
172 this previously. */
174 void
175 resume_deferring_access_checks (void)
177 if (!deferred_access_no_check)
178 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
181 /* Stop deferring access checks. */
183 void
184 stop_deferring_access_checks (void)
186 if (!deferred_access_no_check)
187 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
190 /* Discard the current deferred access checks and restore the
191 previous states. */
193 void
194 pop_deferring_access_checks (void)
196 if (deferred_access_no_check)
197 deferred_access_no_check--;
198 else
199 deferred_access_stack->pop ();
202 /* Returns a TREE_LIST representing the deferred checks.
203 The TREE_PURPOSE of each node is the type through which the
204 access occurred; the TREE_VALUE is the declaration named.
207 vec<deferred_access_check, va_gc> *
208 get_deferred_access_checks (void)
210 if (deferred_access_no_check)
211 return NULL;
212 else
213 return (deferred_access_stack->last().deferred_access_checks);
216 /* Take current deferred checks and combine with the
217 previous states if we also defer checks previously.
218 Otherwise perform checks now. */
220 void
221 pop_to_parent_deferring_access_checks (void)
223 if (deferred_access_no_check)
224 deferred_access_no_check--;
225 else
227 vec<deferred_access_check, va_gc> *checks;
228 deferred_access *ptr;
230 checks = (deferred_access_stack->last ().deferred_access_checks);
232 deferred_access_stack->pop ();
233 ptr = &deferred_access_stack->last ();
234 if (ptr->deferring_access_checks_kind == dk_no_deferred)
236 /* Check access. */
237 perform_access_checks (checks, tf_warning_or_error);
239 else
241 /* Merge with parent. */
242 int i, j;
243 deferred_access_check *chk, *probe;
245 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
247 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
249 if (probe->binfo == chk->binfo &&
250 probe->decl == chk->decl &&
251 probe->diag_decl == chk->diag_decl)
252 goto found;
254 /* Insert into parent's checks. */
255 vec_safe_push (ptr->deferred_access_checks, *chk);
256 found:;
262 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
263 is the BINFO indicating the qualifying scope used to access the
264 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
265 or we aren't in SFINAE context or all the checks succeed return TRUE,
266 otherwise FALSE. */
268 bool
269 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
270 tsubst_flags_t complain)
272 int i;
273 deferred_access_check *chk;
274 location_t loc = input_location;
275 bool ok = true;
277 if (!checks)
278 return true;
280 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
282 input_location = chk->loc;
283 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
286 input_location = loc;
287 return (complain & tf_error) ? true : ok;
290 /* Perform the deferred access checks.
292 After performing the checks, we still have to keep the list
293 `deferred_access_stack->deferred_access_checks' since we may want
294 to check access for them again later in a different context.
295 For example:
297 class A {
298 typedef int X;
299 static X a;
301 A::X A::a, x; // No error for `A::a', error for `x'
303 We have to perform deferred access of `A::X', first with `A::a',
304 next with `x'. Return value like perform_access_checks above. */
306 bool
307 perform_deferred_access_checks (tsubst_flags_t complain)
309 return perform_access_checks (get_deferred_access_checks (), complain);
312 /* Defer checking the accessibility of DECL, when looked up in
313 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
314 Return value like perform_access_checks above. */
316 bool
317 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
318 tsubst_flags_t complain)
320 int i;
321 deferred_access *ptr;
322 deferred_access_check *chk;
325 /* Exit if we are in a context that no access checking is performed.
327 if (deferred_access_no_check)
328 return true;
330 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
332 ptr = &deferred_access_stack->last ();
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr->deferring_access_checks_kind == dk_no_deferred)
337 bool ok = enforce_access (binfo, decl, diag_decl, complain);
338 return (complain & tf_error) ? true : ok;
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
344 if (chk->decl == decl && chk->binfo == binfo &&
345 chk->diag_decl == diag_decl)
347 return true;
350 /* If not, record the check. */
351 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
352 vec_safe_push (ptr->deferred_access_checks, new_access);
354 return true;
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
362 stmts_are_full_exprs_p (void)
364 return current_stmt_tree ()->stmts_are_full_exprs_p;
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
369 this function. */
371 tree
372 add_stmt (tree t)
374 enum tree_code code = TREE_CODE (t);
376 if (EXPR_P (t) && code != LABEL_EXPR)
378 if (!EXPR_HAS_LOCATION (t))
379 SET_EXPR_LOCATION (t, input_location);
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
386 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
387 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
394 return t;
397 /* Returns the stmt_tree to which statements are currently being added. */
399 stmt_tree
400 current_stmt_tree (void)
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
409 static tree
410 maybe_cleanup_point_expr (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
432 /* Create a declaration statement for the declaration given by the DECL. */
434 void
435 add_decl_expr (tree decl)
437 tree r = build_stmt (input_location, DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
444 /* Finish a scope. */
446 tree
447 do_poplevel (tree stmt_list)
449 tree block = NULL;
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
454 stmt_list = pop_stmt_list (stmt_list);
456 if (!processing_template_decl)
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
462 return stmt_list;
465 /* Begin a new scope. */
467 static tree
468 do_pushlevel (scope_kind sk)
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
496 static void
497 begin_maybe_infinite_loop (tree cond)
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
506 cond = fold_non_dependent_expr (cond);
507 maybe_infinite = integer_nonzerop (cond);
509 vec_safe_push (cp_function_chain->infinite_loops,
510 maybe_infinite ? error_mark_node : NULL_TREE);
514 /* A break is a possible exit for the current loop. */
516 void
517 break_maybe_infinite_loop (void)
519 if (!cfun)
520 return;
521 cp_function_chain->infinite_loops->last() = NULL_TREE;
524 /* If we reach the end of the loop without seeing a possible exit, we have
525 an infinite loop. */
527 static void
528 end_maybe_infinite_loop (tree cond)
530 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
531 && !processing_template_decl))
532 return;
533 tree current = cp_function_chain->infinite_loops->pop();
534 if (current != NULL_TREE)
536 cond = fold_non_dependent_expr (cond);
537 if (integer_nonzerop (cond))
538 current_function_infinite_loop = 1;
543 /* Begin a conditional that might contain a declaration. When generating
544 normal code, we want the declaration to appear before the statement
545 containing the conditional. When generating template code, we want the
546 conditional to be rendered as the raw DECL_EXPR. */
548 static void
549 begin_cond (tree *cond_p)
551 if (processing_template_decl)
552 *cond_p = push_stmt_list ();
555 /* Finish such a conditional. */
557 static void
558 finish_cond (tree *cond_p, tree expr)
560 if (processing_template_decl)
562 tree cond = pop_stmt_list (*cond_p);
564 if (expr == NULL_TREE)
565 /* Empty condition in 'for'. */
566 gcc_assert (empty_expr_stmt_p (cond));
567 else if (check_for_bare_parameter_packs (expr))
568 expr = error_mark_node;
569 else if (!empty_expr_stmt_p (cond))
570 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
572 *cond_p = expr;
575 /* If *COND_P specifies a conditional with a declaration, transform the
576 loop such that
577 while (A x = 42) { }
578 for (; A x = 42;) { }
579 becomes
580 while (true) { A x = 42; if (!x) break; }
581 for (;;) { A x = 42; if (!x) break; }
582 The statement list for BODY will be empty if the conditional did
583 not declare anything. */
585 static void
586 simplify_loop_decl_cond (tree *cond_p, tree body)
588 tree cond, if_stmt;
590 if (!TREE_SIDE_EFFECTS (body))
591 return;
593 cond = *cond_p;
594 *cond_p = boolean_true_node;
596 if_stmt = begin_if_stmt ();
597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
598 finish_if_stmt_cond (cond, if_stmt);
599 finish_break_stmt ();
600 finish_then_clause (if_stmt);
601 finish_if_stmt (if_stmt);
604 /* Finish a goto-statement. */
606 tree
607 finish_goto_stmt (tree destination)
609 if (identifier_p (destination))
610 destination = lookup_label (destination);
612 /* We warn about unused labels with -Wunused. That means we have to
613 mark the used labels as used. */
614 if (TREE_CODE (destination) == LABEL_DECL)
615 TREE_USED (destination) = 1;
616 else
618 if (check_no_cilk (destination,
619 "Cilk array notation cannot be used as a computed goto expression",
620 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
621 destination = error_mark_node;
622 destination = mark_rvalue_use (destination);
623 if (!processing_template_decl)
625 destination = cp_convert (ptr_type_node, destination,
626 tf_warning_or_error);
627 if (error_operand_p (destination))
628 return NULL_TREE;
629 destination
630 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
631 destination);
635 check_goto (destination);
637 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
640 /* COND is the condition-expression for an if, while, etc.,
641 statement. Convert it to a boolean value, if appropriate.
642 In addition, verify sequence points if -Wsequence-point is enabled. */
644 static tree
645 maybe_convert_cond (tree cond)
647 /* Empty conditions remain empty. */
648 if (!cond)
649 return NULL_TREE;
651 /* Wait until we instantiate templates before doing conversion. */
652 if (processing_template_decl)
653 return cond;
655 if (warn_sequence_point)
656 verify_sequence_points (cond);
658 /* Do the conversion. */
659 cond = convert_from_reference (cond);
661 if (TREE_CODE (cond) == MODIFY_EXPR
662 && !TREE_NO_WARNING (cond)
663 && warn_parentheses)
665 warning (OPT_Wparentheses,
666 "suggest parentheses around assignment used as truth value");
667 TREE_NO_WARNING (cond) = 1;
670 return condition_conversion (cond);
673 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
675 tree
676 finish_expr_stmt (tree expr)
678 tree r = NULL_TREE;
680 if (expr != NULL_TREE)
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (input_location, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 begin_cond (&IF_COND (r));
722 return r;
725 /* Process the COND of an if-statement, which may be given by
726 IF_STMT. */
728 void
729 finish_if_stmt_cond (tree cond, tree if_stmt)
731 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
732 add_stmt (if_stmt);
733 THEN_CLAUSE (if_stmt) = push_stmt_list ();
736 /* Finish the then-clause of an if-statement, which may be given by
737 IF_STMT. */
739 tree
740 finish_then_clause (tree if_stmt)
742 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
743 return if_stmt;
746 /* Begin the else-clause of an if-statement. */
748 void
749 begin_else_clause (tree if_stmt)
751 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
754 /* Finish the else-clause of an if-statement, which may be given by
755 IF_STMT. */
757 void
758 finish_else_clause (tree if_stmt)
760 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
763 /* Finish an if-statement. */
765 void
766 finish_if_stmt (tree if_stmt)
768 tree scope = IF_SCOPE (if_stmt);
769 IF_SCOPE (if_stmt) = NULL;
770 add_stmt (do_poplevel (scope));
773 /* Begin a while-statement. Returns a newly created WHILE_STMT if
774 appropriate. */
776 tree
777 begin_while_stmt (void)
779 tree r;
780 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
781 add_stmt (r);
782 WHILE_BODY (r) = do_pushlevel (sk_block);
783 begin_cond (&WHILE_COND (r));
784 return r;
787 /* Process the COND of a while-statement, which may be given by
788 WHILE_STMT. */
790 void
791 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
793 if (check_no_cilk (cond,
794 "Cilk array notation cannot be used as a condition for while statement",
795 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
796 cond = error_mark_node;
797 cond = maybe_convert_cond (cond);
798 finish_cond (&WHILE_COND (while_stmt), cond);
799 begin_maybe_infinite_loop (cond);
800 if (ivdep && cond != error_mark_node)
801 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
802 TREE_TYPE (WHILE_COND (while_stmt)),
803 WHILE_COND (while_stmt),
804 build_int_cst (integer_type_node,
805 annot_expr_ivdep_kind));
806 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
809 /* Finish a while-statement, which may be given by WHILE_STMT. */
811 void
812 finish_while_stmt (tree while_stmt)
814 end_maybe_infinite_loop (boolean_true_node);
815 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
818 /* Begin a do-statement. Returns a newly created DO_STMT if
819 appropriate. */
821 tree
822 begin_do_stmt (void)
824 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
825 begin_maybe_infinite_loop (boolean_true_node);
826 add_stmt (r);
827 DO_BODY (r) = push_stmt_list ();
828 return r;
831 /* Finish the body of a do-statement, which may be given by DO_STMT. */
833 void
834 finish_do_body (tree do_stmt)
836 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
838 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
839 body = STATEMENT_LIST_TAIL (body)->stmt;
841 if (IS_EMPTY_STMT (body))
842 warning (OPT_Wempty_body,
843 "suggest explicit braces around empty body in %<do%> statement");
846 /* Finish a do-statement, which may be given by DO_STMT, and whose
847 COND is as indicated. */
849 void
850 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
852 if (check_no_cilk (cond,
853 "Cilk array notation cannot be used as a condition for a do-while statement",
854 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
855 cond = error_mark_node;
856 cond = maybe_convert_cond (cond);
857 end_maybe_infinite_loop (cond);
858 if (ivdep && cond != error_mark_node)
859 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
860 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
861 DO_COND (do_stmt) = cond;
864 /* Finish a return-statement. The EXPRESSION returned, if any, is as
865 indicated. */
867 tree
868 finish_return_stmt (tree expr)
870 tree r;
871 bool no_warning;
873 expr = check_return_expr (expr, &no_warning);
875 if (error_operand_p (expr)
876 || (flag_openmp && !check_omp_return ()))
878 /* Suppress -Wreturn-type for this function. */
879 if (warn_return_type)
880 TREE_NO_WARNING (current_function_decl) = true;
881 return error_mark_node;
884 if (!processing_template_decl)
886 if (warn_sequence_point)
887 verify_sequence_points (expr);
889 if (DECL_DESTRUCTOR_P (current_function_decl)
890 || (DECL_CONSTRUCTOR_P (current_function_decl)
891 && targetm.cxx.cdtor_returns_this ()))
893 /* Similarly, all destructors must run destructors for
894 base-classes before returning. So, all returns in a
895 destructor get sent to the DTOR_LABEL; finish_function emits
896 code to return a value there. */
897 return finish_goto_stmt (cdtor_label);
901 r = build_stmt (input_location, RETURN_EXPR, expr);
902 TREE_NO_WARNING (r) |= no_warning;
903 r = maybe_cleanup_point_expr_void (r);
904 r = add_stmt (r);
906 return r;
909 /* Begin the scope of a for-statement or a range-for-statement.
910 Both the returned trees are to be used in a call to
911 begin_for_stmt or begin_range_for_stmt. */
913 tree
914 begin_for_scope (tree *init)
916 tree scope = NULL_TREE;
917 if (flag_new_for_scope > 0)
918 scope = do_pushlevel (sk_for);
920 if (processing_template_decl)
921 *init = push_stmt_list ();
922 else
923 *init = NULL_TREE;
925 return scope;
928 /* Begin a for-statement. Returns a new FOR_STMT.
929 SCOPE and INIT should be the return of begin_for_scope,
930 or both NULL_TREE */
932 tree
933 begin_for_stmt (tree scope, tree init)
935 tree r;
937 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
938 NULL_TREE, NULL_TREE, NULL_TREE);
940 if (scope == NULL_TREE)
942 gcc_assert (!init || !(flag_new_for_scope > 0));
943 if (!init)
944 scope = begin_for_scope (&init);
946 FOR_INIT_STMT (r) = init;
947 FOR_SCOPE (r) = scope;
949 return r;
952 /* Finish the for-init-statement of a for-statement, which may be
953 given by FOR_STMT. */
955 void
956 finish_for_init_stmt (tree for_stmt)
958 if (processing_template_decl)
959 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
960 add_stmt (for_stmt);
961 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
962 begin_cond (&FOR_COND (for_stmt));
965 /* Finish the COND of a for-statement, which may be given by
966 FOR_STMT. */
968 void
969 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
971 if (check_no_cilk (cond,
972 "Cilk array notation cannot be used in a condition for a for-loop",
973 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
974 cond = error_mark_node;
975 cond = maybe_convert_cond (cond);
976 finish_cond (&FOR_COND (for_stmt), cond);
977 begin_maybe_infinite_loop (cond);
978 if (ivdep && cond != error_mark_node)
979 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
980 TREE_TYPE (FOR_COND (for_stmt)),
981 FOR_COND (for_stmt),
982 build_int_cst (integer_type_node,
983 annot_expr_ivdep_kind));
984 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
987 /* Finish the increment-EXPRESSION in a for-statement, which may be
988 given by FOR_STMT. */
990 void
991 finish_for_expr (tree expr, tree for_stmt)
993 if (!expr)
994 return;
995 /* If EXPR is an overloaded function, issue an error; there is no
996 context available to use to perform overload resolution. */
997 if (type_unknown_p (expr))
999 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1000 expr = error_mark_node;
1002 if (!processing_template_decl)
1004 if (warn_sequence_point)
1005 verify_sequence_points (expr);
1006 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1007 tf_warning_or_error);
1009 else if (!type_dependent_expression_p (expr))
1010 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1011 tf_warning_or_error);
1012 expr = maybe_cleanup_point_expr_void (expr);
1013 if (check_for_bare_parameter_packs (expr))
1014 expr = error_mark_node;
1015 FOR_EXPR (for_stmt) = expr;
1018 /* Finish the body of a for-statement, which may be given by
1019 FOR_STMT. The increment-EXPR for the loop must be
1020 provided.
1021 It can also finish RANGE_FOR_STMT. */
1023 void
1024 finish_for_stmt (tree for_stmt)
1026 end_maybe_infinite_loop (boolean_true_node);
1028 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1029 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1030 else
1031 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1033 /* Pop the scope for the body of the loop. */
1034 if (flag_new_for_scope > 0)
1036 tree scope;
1037 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1038 ? &RANGE_FOR_SCOPE (for_stmt)
1039 : &FOR_SCOPE (for_stmt));
1040 scope = *scope_ptr;
1041 *scope_ptr = NULL;
1042 add_stmt (do_poplevel (scope));
1046 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1047 SCOPE and INIT should be the return of begin_for_scope,
1048 or both NULL_TREE .
1049 To finish it call finish_for_stmt(). */
1051 tree
1052 begin_range_for_stmt (tree scope, tree init)
1054 tree r;
1056 begin_maybe_infinite_loop (boolean_false_node);
1058 r = build_stmt (input_location, RANGE_FOR_STMT,
1059 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1061 if (scope == NULL_TREE)
1063 gcc_assert (!init || !(flag_new_for_scope > 0));
1064 if (!init)
1065 scope = begin_for_scope (&init);
1068 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1069 pop it now. */
1070 if (init)
1071 pop_stmt_list (init);
1072 RANGE_FOR_SCOPE (r) = scope;
1074 return r;
1077 /* Finish the head of a range-based for statement, which may
1078 be given by RANGE_FOR_STMT. DECL must be the declaration
1079 and EXPR must be the loop expression. */
1081 void
1082 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1084 RANGE_FOR_DECL (range_for_stmt) = decl;
1085 RANGE_FOR_EXPR (range_for_stmt) = expr;
1086 add_stmt (range_for_stmt);
1087 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1090 /* Finish a break-statement. */
1092 tree
1093 finish_break_stmt (void)
1095 /* In switch statements break is sometimes stylistically used after
1096 a return statement. This can lead to spurious warnings about
1097 control reaching the end of a non-void function when it is
1098 inlined. Note that we are calling block_may_fallthru with
1099 language specific tree nodes; this works because
1100 block_may_fallthru returns true when given something it does not
1101 understand. */
1102 if (!block_may_fallthru (cur_stmt_list))
1103 return void_node;
1104 return add_stmt (build_stmt (input_location, BREAK_STMT));
1107 /* Finish a continue-statement. */
1109 tree
1110 finish_continue_stmt (void)
1112 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1115 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1116 appropriate. */
1118 tree
1119 begin_switch_stmt (void)
1121 tree r, scope;
1123 scope = do_pushlevel (sk_cond);
1124 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1126 begin_cond (&SWITCH_STMT_COND (r));
1128 return r;
1131 /* Finish the cond of a switch-statement. */
1133 void
1134 finish_switch_cond (tree cond, tree switch_stmt)
1136 tree orig_type = NULL;
1138 if (check_no_cilk (cond,
1139 "Cilk array notation cannot be used as a condition for switch statement",
1140 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1141 cond = error_mark_node;
1143 if (!processing_template_decl)
1145 /* Convert the condition to an integer or enumeration type. */
1146 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1147 if (cond == NULL_TREE)
1149 error ("switch quantity not an integer");
1150 cond = error_mark_node;
1152 /* We want unlowered type here to handle enum bit-fields. */
1153 orig_type = unlowered_expr_type (cond);
1154 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1155 orig_type = TREE_TYPE (cond);
1156 if (cond != error_mark_node)
1158 /* [stmt.switch]
1160 Integral promotions are performed. */
1161 cond = perform_integral_promotions (cond);
1162 cond = maybe_cleanup_point_expr (cond);
1165 if (check_for_bare_parameter_packs (cond))
1166 cond = error_mark_node;
1167 else if (!processing_template_decl && warn_sequence_point)
1168 verify_sequence_points (cond);
1170 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1171 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1172 add_stmt (switch_stmt);
1173 push_switch (switch_stmt);
1174 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1177 /* Finish the body of a switch-statement, which may be given by
1178 SWITCH_STMT. The COND to switch on is indicated. */
1180 void
1181 finish_switch_stmt (tree switch_stmt)
1183 tree scope;
1185 SWITCH_STMT_BODY (switch_stmt) =
1186 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1187 pop_switch ();
1189 scope = SWITCH_STMT_SCOPE (switch_stmt);
1190 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1191 add_stmt (do_poplevel (scope));
1194 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1195 appropriate. */
1197 tree
1198 begin_try_block (void)
1200 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1201 add_stmt (r);
1202 TRY_STMTS (r) = push_stmt_list ();
1203 return r;
1206 /* Likewise, for a function-try-block. The block returned in
1207 *COMPOUND_STMT is an artificial outer scope, containing the
1208 function-try-block. */
1210 tree
1211 begin_function_try_block (tree *compound_stmt)
1213 tree r;
1214 /* This outer scope does not exist in the C++ standard, but we need
1215 a place to put __FUNCTION__ and similar variables. */
1216 *compound_stmt = begin_compound_stmt (0);
1217 r = begin_try_block ();
1218 FN_TRY_BLOCK_P (r) = 1;
1219 return r;
1222 /* Finish a try-block, which may be given by TRY_BLOCK. */
1224 void
1225 finish_try_block (tree try_block)
1227 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1228 TRY_HANDLERS (try_block) = push_stmt_list ();
1231 /* Finish the body of a cleanup try-block, which may be given by
1232 TRY_BLOCK. */
1234 void
1235 finish_cleanup_try_block (tree try_block)
1237 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1240 /* Finish an implicitly generated try-block, with a cleanup is given
1241 by CLEANUP. */
1243 void
1244 finish_cleanup (tree cleanup, tree try_block)
1246 TRY_HANDLERS (try_block) = cleanup;
1247 CLEANUP_P (try_block) = 1;
1250 /* Likewise, for a function-try-block. */
1252 void
1253 finish_function_try_block (tree try_block)
1255 finish_try_block (try_block);
1256 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1257 the try block, but moving it inside. */
1258 in_function_try_handler = 1;
1261 /* Finish a handler-sequence for a try-block, which may be given by
1262 TRY_BLOCK. */
1264 void
1265 finish_handler_sequence (tree try_block)
1267 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1268 check_handlers (TRY_HANDLERS (try_block));
1271 /* Finish the handler-seq for a function-try-block, given by
1272 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1273 begin_function_try_block. */
1275 void
1276 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1278 in_function_try_handler = 0;
1279 finish_handler_sequence (try_block);
1280 finish_compound_stmt (compound_stmt);
1283 /* Begin a handler. Returns a HANDLER if appropriate. */
1285 tree
1286 begin_handler (void)
1288 tree r;
1290 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1291 add_stmt (r);
1293 /* Create a binding level for the eh_info and the exception object
1294 cleanup. */
1295 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1297 return r;
1300 /* Finish the handler-parameters for a handler, which may be given by
1301 HANDLER. DECL is the declaration for the catch parameter, or NULL
1302 if this is a `catch (...)' clause. */
1304 void
1305 finish_handler_parms (tree decl, tree handler)
1307 tree type = NULL_TREE;
1308 if (processing_template_decl)
1310 if (decl)
1312 decl = pushdecl (decl);
1313 decl = push_template_decl (decl);
1314 HANDLER_PARMS (handler) = decl;
1315 type = TREE_TYPE (decl);
1318 else
1319 type = expand_start_catch_block (decl);
1320 HANDLER_TYPE (handler) = type;
1323 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1324 the return value from the matching call to finish_handler_parms. */
1326 void
1327 finish_handler (tree handler)
1329 if (!processing_template_decl)
1330 expand_end_catch_block ();
1331 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1334 /* Begin a compound statement. FLAGS contains some bits that control the
1335 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1336 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1337 block of a function. If BCS_TRY_BLOCK is set, this is the block
1338 created on behalf of a TRY statement. Returns a token to be passed to
1339 finish_compound_stmt. */
1341 tree
1342 begin_compound_stmt (unsigned int flags)
1344 tree r;
1346 if (flags & BCS_NO_SCOPE)
1348 r = push_stmt_list ();
1349 STATEMENT_LIST_NO_SCOPE (r) = 1;
1351 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1352 But, if it's a statement-expression with a scopeless block, there's
1353 nothing to keep, and we don't want to accidentally keep a block
1354 *inside* the scopeless block. */
1355 keep_next_level (false);
1357 else
1358 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1360 /* When processing a template, we need to remember where the braces were,
1361 so that we can set up identical scopes when instantiating the template
1362 later. BIND_EXPR is a handy candidate for this.
1363 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1364 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1365 processing templates. */
1366 if (processing_template_decl)
1368 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1369 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1370 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1371 TREE_SIDE_EFFECTS (r) = 1;
1374 return r;
1377 /* Finish a compound-statement, which is given by STMT. */
1379 void
1380 finish_compound_stmt (tree stmt)
1382 if (TREE_CODE (stmt) == BIND_EXPR)
1384 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1385 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1386 discard the BIND_EXPR so it can be merged with the containing
1387 STATEMENT_LIST. */
1388 if (TREE_CODE (body) == STATEMENT_LIST
1389 && STATEMENT_LIST_HEAD (body) == NULL
1390 && !BIND_EXPR_BODY_BLOCK (stmt)
1391 && !BIND_EXPR_TRY_BLOCK (stmt))
1392 stmt = body;
1393 else
1394 BIND_EXPR_BODY (stmt) = body;
1396 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1397 stmt = pop_stmt_list (stmt);
1398 else
1400 /* Destroy any ObjC "super" receivers that may have been
1401 created. */
1402 objc_clear_super_receiver ();
1404 stmt = do_poplevel (stmt);
1407 /* ??? See c_end_compound_stmt wrt statement expressions. */
1408 add_stmt (stmt);
1411 /* Finish an asm-statement, whose components are a STRING, some
1412 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1413 LABELS. Also note whether the asm-statement should be
1414 considered volatile. */
1416 tree
1417 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1418 tree input_operands, tree clobbers, tree labels)
1420 tree r;
1421 tree t;
1422 int ninputs = list_length (input_operands);
1423 int noutputs = list_length (output_operands);
1425 if (!processing_template_decl)
1427 const char *constraint;
1428 const char **oconstraints;
1429 bool allows_mem, allows_reg, is_inout;
1430 tree operand;
1431 int i;
1433 oconstraints = XALLOCAVEC (const char *, noutputs);
1435 string = resolve_asm_operand_names (string, output_operands,
1436 input_operands, labels);
1438 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1440 operand = TREE_VALUE (t);
1442 /* ??? Really, this should not be here. Users should be using a
1443 proper lvalue, dammit. But there's a long history of using
1444 casts in the output operands. In cases like longlong.h, this
1445 becomes a primitive form of typechecking -- if the cast can be
1446 removed, then the output operand had a type of the proper width;
1447 otherwise we'll get an error. Gross, but ... */
1448 STRIP_NOPS (operand);
1450 operand = mark_lvalue_use (operand);
1452 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1453 operand = error_mark_node;
1455 if (operand != error_mark_node
1456 && (TREE_READONLY (operand)
1457 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1458 /* Functions are not modifiable, even though they are
1459 lvalues. */
1460 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1461 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1462 /* If it's an aggregate and any field is const, then it is
1463 effectively const. */
1464 || (CLASS_TYPE_P (TREE_TYPE (operand))
1465 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1466 cxx_readonly_error (operand, lv_asm);
1468 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1469 oconstraints[i] = constraint;
1471 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1472 &allows_mem, &allows_reg, &is_inout))
1474 /* If the operand is going to end up in memory,
1475 mark it addressable. */
1476 if (!allows_reg && !cxx_mark_addressable (operand))
1477 operand = error_mark_node;
1479 else
1480 operand = error_mark_node;
1482 TREE_VALUE (t) = operand;
1485 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1487 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1488 bool constraint_parsed
1489 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1490 oconstraints, &allows_mem, &allows_reg);
1491 /* If the operand is going to end up in memory, don't call
1492 decay_conversion. */
1493 if (constraint_parsed && !allows_reg && allows_mem)
1494 operand = mark_lvalue_use (TREE_VALUE (t));
1495 else
1496 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1498 /* If the type of the operand hasn't been determined (e.g.,
1499 because it involves an overloaded function), then issue
1500 an error message. There's no context available to
1501 resolve the overloading. */
1502 if (TREE_TYPE (operand) == unknown_type_node)
1504 error ("type of asm operand %qE could not be determined",
1505 TREE_VALUE (t));
1506 operand = error_mark_node;
1509 if (constraint_parsed)
1511 /* If the operand is going to end up in memory,
1512 mark it addressable. */
1513 if (!allows_reg && allows_mem)
1515 /* Strip the nops as we allow this case. FIXME, this really
1516 should be rejected or made deprecated. */
1517 STRIP_NOPS (operand);
1518 if (!cxx_mark_addressable (operand))
1519 operand = error_mark_node;
1521 else if (!allows_reg && !allows_mem)
1523 /* If constraint allows neither register nor memory,
1524 try harder to get a constant. */
1525 tree constop = maybe_constant_value (operand);
1526 if (TREE_CONSTANT (constop))
1527 operand = constop;
1530 else
1531 operand = error_mark_node;
1533 TREE_VALUE (t) = operand;
1537 r = build_stmt (input_location, ASM_EXPR, string,
1538 output_operands, input_operands,
1539 clobbers, labels);
1540 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1541 r = maybe_cleanup_point_expr_void (r);
1542 return add_stmt (r);
1545 /* Finish a label with the indicated NAME. Returns the new label. */
1547 tree
1548 finish_label_stmt (tree name)
1550 tree decl = define_label (input_location, name);
1552 if (decl == error_mark_node)
1553 return error_mark_node;
1555 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1557 return decl;
1560 /* Finish a series of declarations for local labels. G++ allows users
1561 to declare "local" labels, i.e., labels with scope. This extension
1562 is useful when writing code involving statement-expressions. */
1564 void
1565 finish_label_decl (tree name)
1567 if (!at_function_scope_p ())
1569 error ("__label__ declarations are only allowed in function scopes");
1570 return;
1573 add_decl_expr (declare_local_label (name));
1576 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1578 void
1579 finish_decl_cleanup (tree decl, tree cleanup)
1581 push_cleanup (decl, cleanup, false);
1584 /* If the current scope exits with an exception, run CLEANUP. */
1586 void
1587 finish_eh_cleanup (tree cleanup)
1589 push_cleanup (NULL, cleanup, true);
1592 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1593 order they were written by the user. Each node is as for
1594 emit_mem_initializers. */
1596 void
1597 finish_mem_initializers (tree mem_inits)
1599 /* Reorder the MEM_INITS so that they are in the order they appeared
1600 in the source program. */
1601 mem_inits = nreverse (mem_inits);
1603 if (processing_template_decl)
1605 tree mem;
1607 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1609 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1610 check for bare parameter packs in the TREE_VALUE, because
1611 any parameter packs in the TREE_VALUE have already been
1612 bound as part of the TREE_PURPOSE. See
1613 make_pack_expansion for more information. */
1614 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1615 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1616 TREE_VALUE (mem) = error_mark_node;
1619 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1620 CTOR_INITIALIZER, mem_inits));
1622 else
1623 emit_mem_initializers (mem_inits);
1626 /* Obfuscate EXPR if it looks like an id-expression or member access so
1627 that the call to finish_decltype in do_auto_deduction will give the
1628 right result. */
1630 tree
1631 force_paren_expr (tree expr)
1633 /* This is only needed for decltype(auto) in C++14. */
1634 if (cxx_dialect < cxx14)
1635 return expr;
1637 /* If we're in unevaluated context, we can't be deducing a
1638 return/initializer type, so we don't need to mess with this. */
1639 if (cp_unevaluated_operand)
1640 return expr;
1642 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1643 && TREE_CODE (expr) != SCOPE_REF)
1644 return expr;
1646 if (TREE_CODE (expr) == COMPONENT_REF)
1647 REF_PARENTHESIZED_P (expr) = true;
1648 else if (type_dependent_expression_p (expr))
1649 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1650 else
1652 cp_lvalue_kind kind = lvalue_kind (expr);
1653 if ((kind & ~clk_class) != clk_none)
1655 tree type = unlowered_expr_type (expr);
1656 bool rval = !!(kind & clk_rvalueref);
1657 type = cp_build_reference_type (type, rval);
1658 /* This inhibits warnings in, eg, cxx_mark_addressable
1659 (c++/60955). */
1660 warning_sentinel s (extra_warnings);
1661 expr = build_static_cast (type, expr, tf_error);
1662 if (expr != error_mark_node)
1663 REF_PARENTHESIZED_P (expr) = true;
1667 return expr;
1670 /* Finish a parenthesized expression EXPR. */
1672 tree
1673 finish_parenthesized_expr (tree expr)
1675 if (EXPR_P (expr))
1676 /* This inhibits warnings in c_common_truthvalue_conversion. */
1677 TREE_NO_WARNING (expr) = 1;
1679 if (TREE_CODE (expr) == OFFSET_REF
1680 || TREE_CODE (expr) == SCOPE_REF)
1681 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1682 enclosed in parentheses. */
1683 PTRMEM_OK_P (expr) = 0;
1685 if (TREE_CODE (expr) == STRING_CST)
1686 PAREN_STRING_LITERAL_P (expr) = 1;
1688 expr = force_paren_expr (expr);
1690 return expr;
1693 /* Finish a reference to a non-static data member (DECL) that is not
1694 preceded by `.' or `->'. */
1696 tree
1697 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1699 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1701 if (!object)
1703 tree scope = qualifying_scope;
1704 if (scope == NULL_TREE)
1705 scope = context_for_name_lookup (decl);
1706 object = maybe_dummy_object (scope, NULL);
1709 object = maybe_resolve_dummy (object, true);
1710 if (object == error_mark_node)
1711 return error_mark_node;
1713 /* DR 613/850: Can use non-static data members without an associated
1714 object in sizeof/decltype/alignof. */
1715 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1716 && (!processing_template_decl || !current_class_ref))
1718 if (current_function_decl
1719 && DECL_STATIC_FUNCTION_P (current_function_decl))
1720 error ("invalid use of member %qD in static member function", decl);
1721 else
1722 error ("invalid use of non-static data member %qD", decl);
1723 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1725 return error_mark_node;
1728 if (current_class_ptr)
1729 TREE_USED (current_class_ptr) = 1;
1730 if (processing_template_decl && !qualifying_scope)
1732 tree type = TREE_TYPE (decl);
1734 if (TREE_CODE (type) == REFERENCE_TYPE)
1735 /* Quals on the object don't matter. */;
1736 else if (PACK_EXPANSION_P (type))
1737 /* Don't bother trying to represent this. */
1738 type = NULL_TREE;
1739 else
1741 /* Set the cv qualifiers. */
1742 int quals = cp_type_quals (TREE_TYPE (object));
1744 if (DECL_MUTABLE_P (decl))
1745 quals &= ~TYPE_QUAL_CONST;
1747 quals |= cp_type_quals (TREE_TYPE (decl));
1748 type = cp_build_qualified_type (type, quals);
1751 return (convert_from_reference
1752 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1754 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1755 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1756 for now. */
1757 else if (processing_template_decl)
1758 return build_qualified_name (TREE_TYPE (decl),
1759 qualifying_scope,
1760 decl,
1761 /*template_p=*/false);
1762 else
1764 tree access_type = TREE_TYPE (object);
1766 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1767 decl, tf_warning_or_error);
1769 /* If the data member was named `C::M', convert `*this' to `C'
1770 first. */
1771 if (qualifying_scope)
1773 tree binfo = NULL_TREE;
1774 object = build_scoped_ref (object, qualifying_scope,
1775 &binfo);
1778 return build_class_member_access_expr (object, decl,
1779 /*access_path=*/NULL_TREE,
1780 /*preserve_reference=*/false,
1781 tf_warning_or_error);
1785 /* If we are currently parsing a template and we encountered a typedef
1786 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1787 adds the typedef to a list tied to the current template.
1788 At template instantiation time, that list is walked and access check
1789 performed for each typedef.
1790 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1792 void
1793 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1794 tree context,
1795 location_t location)
1797 tree template_info = NULL;
1798 tree cs = current_scope ();
1800 if (!is_typedef_decl (typedef_decl)
1801 || !context
1802 || !CLASS_TYPE_P (context)
1803 || !cs)
1804 return;
1806 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1807 template_info = get_template_info (cs);
1809 if (template_info
1810 && TI_TEMPLATE (template_info)
1811 && !currently_open_class (context))
1812 append_type_to_template_for_access_check (cs, typedef_decl,
1813 context, location);
1816 /* DECL was the declaration to which a qualified-id resolved. Issue
1817 an error message if it is not accessible. If OBJECT_TYPE is
1818 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1819 type of `*x', or `x', respectively. If the DECL was named as
1820 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1822 void
1823 check_accessibility_of_qualified_id (tree decl,
1824 tree object_type,
1825 tree nested_name_specifier)
1827 tree scope;
1828 tree qualifying_type = NULL_TREE;
1830 /* If we are parsing a template declaration and if decl is a typedef,
1831 add it to a list tied to the template.
1832 At template instantiation time, that list will be walked and
1833 access check performed. */
1834 add_typedef_to_current_template_for_access_check (decl,
1835 nested_name_specifier
1836 ? nested_name_specifier
1837 : DECL_CONTEXT (decl),
1838 input_location);
1840 /* If we're not checking, return immediately. */
1841 if (deferred_access_no_check)
1842 return;
1844 /* Determine the SCOPE of DECL. */
1845 scope = context_for_name_lookup (decl);
1846 /* If the SCOPE is not a type, then DECL is not a member. */
1847 if (!TYPE_P (scope))
1848 return;
1849 /* Compute the scope through which DECL is being accessed. */
1850 if (object_type
1851 /* OBJECT_TYPE might not be a class type; consider:
1853 class A { typedef int I; };
1854 I *p;
1855 p->A::I::~I();
1857 In this case, we will have "A::I" as the DECL, but "I" as the
1858 OBJECT_TYPE. */
1859 && CLASS_TYPE_P (object_type)
1860 && DERIVED_FROM_P (scope, object_type))
1861 /* If we are processing a `->' or `.' expression, use the type of the
1862 left-hand side. */
1863 qualifying_type = object_type;
1864 else if (nested_name_specifier)
1866 /* If the reference is to a non-static member of the
1867 current class, treat it as if it were referenced through
1868 `this'. */
1869 tree ct;
1870 if (DECL_NONSTATIC_MEMBER_P (decl)
1871 && current_class_ptr
1872 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1873 qualifying_type = ct;
1874 /* Otherwise, use the type indicated by the
1875 nested-name-specifier. */
1876 else
1877 qualifying_type = nested_name_specifier;
1879 else
1880 /* Otherwise, the name must be from the current class or one of
1881 its bases. */
1882 qualifying_type = currently_open_derived_class (scope);
1884 if (qualifying_type
1885 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1886 or similar in a default argument value. */
1887 && CLASS_TYPE_P (qualifying_type)
1888 && !dependent_type_p (qualifying_type))
1889 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1890 decl, tf_warning_or_error);
1893 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1894 class named to the left of the "::" operator. DONE is true if this
1895 expression is a complete postfix-expression; it is false if this
1896 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1897 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1898 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1899 is true iff this qualified name appears as a template argument. */
1901 tree
1902 finish_qualified_id_expr (tree qualifying_class,
1903 tree expr,
1904 bool done,
1905 bool address_p,
1906 bool template_p,
1907 bool template_arg_p,
1908 tsubst_flags_t complain)
1910 gcc_assert (TYPE_P (qualifying_class));
1912 if (error_operand_p (expr))
1913 return error_mark_node;
1915 if ((DECL_P (expr) || BASELINK_P (expr))
1916 && !mark_used (expr, complain))
1917 return error_mark_node;
1919 if (template_p)
1920 check_template_keyword (expr);
1922 /* If EXPR occurs as the operand of '&', use special handling that
1923 permits a pointer-to-member. */
1924 if (address_p && done)
1926 if (TREE_CODE (expr) == SCOPE_REF)
1927 expr = TREE_OPERAND (expr, 1);
1928 expr = build_offset_ref (qualifying_class, expr,
1929 /*address_p=*/true, complain);
1930 return expr;
1933 /* No need to check access within an enum. */
1934 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1935 return expr;
1937 /* Within the scope of a class, turn references to non-static
1938 members into expression of the form "this->...". */
1939 if (template_arg_p)
1940 /* But, within a template argument, we do not want make the
1941 transformation, as there is no "this" pointer. */
1943 else if (TREE_CODE (expr) == FIELD_DECL)
1945 push_deferring_access_checks (dk_no_check);
1946 expr = finish_non_static_data_member (expr, NULL_TREE,
1947 qualifying_class);
1948 pop_deferring_access_checks ();
1950 else if (BASELINK_P (expr) && !processing_template_decl)
1952 /* See if any of the functions are non-static members. */
1953 /* If so, the expression may be relative to 'this'. */
1954 if (!shared_member_p (expr)
1955 && current_class_ptr
1956 && DERIVED_FROM_P (qualifying_class,
1957 current_nonlambda_class_type ()))
1958 expr = (build_class_member_access_expr
1959 (maybe_dummy_object (qualifying_class, NULL),
1960 expr,
1961 BASELINK_ACCESS_BINFO (expr),
1962 /*preserve_reference=*/false,
1963 complain));
1964 else if (done)
1965 /* The expression is a qualified name whose address is not
1966 being taken. */
1967 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1968 complain);
1970 else if (BASELINK_P (expr))
1972 else
1974 /* In a template, return a SCOPE_REF for most qualified-ids
1975 so that we can check access at instantiation time. But if
1976 we're looking at a member of the current instantiation, we
1977 know we have access and building up the SCOPE_REF confuses
1978 non-type template argument handling. */
1979 if (processing_template_decl
1980 && !currently_open_class (qualifying_class))
1981 expr = build_qualified_name (TREE_TYPE (expr),
1982 qualifying_class, expr,
1983 template_p);
1985 expr = convert_from_reference (expr);
1988 return expr;
1991 /* Begin a statement-expression. The value returned must be passed to
1992 finish_stmt_expr. */
1994 tree
1995 begin_stmt_expr (void)
1997 return push_stmt_list ();
2000 /* Process the final expression of a statement expression. EXPR can be
2001 NULL, if the final expression is empty. Return a STATEMENT_LIST
2002 containing all the statements in the statement-expression, or
2003 ERROR_MARK_NODE if there was an error. */
2005 tree
2006 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2008 if (error_operand_p (expr))
2010 /* The type of the statement-expression is the type of the last
2011 expression. */
2012 TREE_TYPE (stmt_expr) = error_mark_node;
2013 return error_mark_node;
2016 /* If the last statement does not have "void" type, then the value
2017 of the last statement is the value of the entire expression. */
2018 if (expr)
2020 tree type = TREE_TYPE (expr);
2022 if (processing_template_decl)
2024 expr = build_stmt (input_location, EXPR_STMT, expr);
2025 expr = add_stmt (expr);
2026 /* Mark the last statement so that we can recognize it as such at
2027 template-instantiation time. */
2028 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2030 else if (VOID_TYPE_P (type))
2032 /* Just treat this like an ordinary statement. */
2033 expr = finish_expr_stmt (expr);
2035 else
2037 /* It actually has a value we need to deal with. First, force it
2038 to be an rvalue so that we won't need to build up a copy
2039 constructor call later when we try to assign it to something. */
2040 expr = force_rvalue (expr, tf_warning_or_error);
2041 if (error_operand_p (expr))
2042 return error_mark_node;
2044 /* Update for array-to-pointer decay. */
2045 type = TREE_TYPE (expr);
2047 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2048 normal statement, but don't convert to void or actually add
2049 the EXPR_STMT. */
2050 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2051 expr = maybe_cleanup_point_expr (expr);
2052 add_stmt (expr);
2055 /* The type of the statement-expression is the type of the last
2056 expression. */
2057 TREE_TYPE (stmt_expr) = type;
2060 return stmt_expr;
2063 /* Finish a statement-expression. EXPR should be the value returned
2064 by the previous begin_stmt_expr. Returns an expression
2065 representing the statement-expression. */
2067 tree
2068 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2070 tree type;
2071 tree result;
2073 if (error_operand_p (stmt_expr))
2075 pop_stmt_list (stmt_expr);
2076 return error_mark_node;
2079 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2081 type = TREE_TYPE (stmt_expr);
2082 result = pop_stmt_list (stmt_expr);
2083 TREE_TYPE (result) = type;
2085 if (processing_template_decl)
2087 result = build_min (STMT_EXPR, type, result);
2088 TREE_SIDE_EFFECTS (result) = 1;
2089 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2091 else if (CLASS_TYPE_P (type))
2093 /* Wrap the statement-expression in a TARGET_EXPR so that the
2094 temporary object created by the final expression is destroyed at
2095 the end of the full-expression containing the
2096 statement-expression. */
2097 result = force_target_expr (type, result, tf_warning_or_error);
2100 return result;
2103 /* Returns the expression which provides the value of STMT_EXPR. */
2105 tree
2106 stmt_expr_value_expr (tree stmt_expr)
2108 tree t = STMT_EXPR_STMT (stmt_expr);
2110 if (TREE_CODE (t) == BIND_EXPR)
2111 t = BIND_EXPR_BODY (t);
2113 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2114 t = STATEMENT_LIST_TAIL (t)->stmt;
2116 if (TREE_CODE (t) == EXPR_STMT)
2117 t = EXPR_STMT_EXPR (t);
2119 return t;
2122 /* Return TRUE iff EXPR_STMT is an empty list of
2123 expression statements. */
2125 bool
2126 empty_expr_stmt_p (tree expr_stmt)
2128 tree body = NULL_TREE;
2130 if (expr_stmt == void_node)
2131 return true;
2133 if (expr_stmt)
2135 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2136 body = EXPR_STMT_EXPR (expr_stmt);
2137 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2138 body = expr_stmt;
2141 if (body)
2143 if (TREE_CODE (body) == STATEMENT_LIST)
2144 return tsi_end_p (tsi_start (body));
2145 else
2146 return empty_expr_stmt_p (body);
2148 return false;
2151 /* Perform Koenig lookup. FN is the postfix-expression representing
2152 the function (or functions) to call; ARGS are the arguments to the
2153 call. Returns the functions to be considered by overload resolution. */
2155 tree
2156 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2157 tsubst_flags_t complain)
2159 tree identifier = NULL_TREE;
2160 tree functions = NULL_TREE;
2161 tree tmpl_args = NULL_TREE;
2162 bool template_id = false;
2164 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2166 /* Use a separate flag to handle null args. */
2167 template_id = true;
2168 tmpl_args = TREE_OPERAND (fn, 1);
2169 fn = TREE_OPERAND (fn, 0);
2172 /* Find the name of the overloaded function. */
2173 if (identifier_p (fn))
2174 identifier = fn;
2175 else if (is_overloaded_fn (fn))
2177 functions = fn;
2178 identifier = DECL_NAME (get_first_fn (functions));
2180 else if (DECL_P (fn))
2182 functions = fn;
2183 identifier = DECL_NAME (fn);
2186 /* A call to a namespace-scope function using an unqualified name.
2188 Do Koenig lookup -- unless any of the arguments are
2189 type-dependent. */
2190 if (!any_type_dependent_arguments_p (args)
2191 && !any_dependent_template_arguments_p (tmpl_args))
2193 fn = lookup_arg_dependent (identifier, functions, args);
2194 if (!fn)
2196 /* The unqualified name could not be resolved. */
2197 if (complain)
2198 fn = unqualified_fn_lookup_error (identifier);
2199 else
2200 fn = identifier;
2204 if (fn && template_id)
2205 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2207 return fn;
2210 /* Generate an expression for `FN (ARGS)'. This may change the
2211 contents of ARGS.
2213 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2214 as a virtual call, even if FN is virtual. (This flag is set when
2215 encountering an expression where the function name is explicitly
2216 qualified. For example a call to `X::f' never generates a virtual
2217 call.)
2219 Returns code for the call. */
2221 tree
2222 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2223 bool koenig_p, tsubst_flags_t complain)
2225 tree result;
2226 tree orig_fn;
2227 vec<tree, va_gc> *orig_args = NULL;
2229 if (fn == error_mark_node)
2230 return error_mark_node;
2232 gcc_assert (!TYPE_P (fn));
2234 orig_fn = fn;
2236 if (processing_template_decl)
2238 /* If the call expression is dependent, build a CALL_EXPR node
2239 with no type; type_dependent_expression_p recognizes
2240 expressions with no type as being dependent. */
2241 if (type_dependent_expression_p (fn)
2242 || any_type_dependent_arguments_p (*args)
2243 /* For a non-static member function that doesn't have an
2244 explicit object argument, we need to specifically
2245 test the type dependency of the "this" pointer because it
2246 is not included in *ARGS even though it is considered to
2247 be part of the list of arguments. Note that this is
2248 related to CWG issues 515 and 1005. */
2249 || (TREE_CODE (fn) != COMPONENT_REF
2250 && non_static_member_function_p (fn)
2251 && current_class_ref
2252 && type_dependent_expression_p (current_class_ref)))
2254 result = build_nt_call_vec (fn, *args);
2255 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2256 KOENIG_LOOKUP_P (result) = koenig_p;
2257 if (cfun)
2261 tree fndecl = OVL_CURRENT (fn);
2262 if (TREE_CODE (fndecl) != FUNCTION_DECL
2263 || !TREE_THIS_VOLATILE (fndecl))
2264 break;
2265 fn = OVL_NEXT (fn);
2267 while (fn);
2268 if (!fn)
2269 current_function_returns_abnormally = 1;
2271 return result;
2273 orig_args = make_tree_vector_copy (*args);
2274 if (!BASELINK_P (fn)
2275 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2276 && TREE_TYPE (fn) != unknown_type_node)
2277 fn = build_non_dependent_expr (fn);
2278 make_args_non_dependent (*args);
2281 if (TREE_CODE (fn) == COMPONENT_REF)
2283 tree member = TREE_OPERAND (fn, 1);
2284 if (BASELINK_P (member))
2286 tree object = TREE_OPERAND (fn, 0);
2287 return build_new_method_call (object, member,
2288 args, NULL_TREE,
2289 (disallow_virtual
2290 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2291 : LOOKUP_NORMAL),
2292 /*fn_p=*/NULL,
2293 complain);
2297 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2298 if (TREE_CODE (fn) == ADDR_EXPR
2299 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2300 fn = TREE_OPERAND (fn, 0);
2302 if (is_overloaded_fn (fn))
2303 fn = baselink_for_fns (fn);
2305 result = NULL_TREE;
2306 if (BASELINK_P (fn))
2308 tree object;
2310 /* A call to a member function. From [over.call.func]:
2312 If the keyword this is in scope and refers to the class of
2313 that member function, or a derived class thereof, then the
2314 function call is transformed into a qualified function call
2315 using (*this) as the postfix-expression to the left of the
2316 . operator.... [Otherwise] a contrived object of type T
2317 becomes the implied object argument.
2319 In this situation:
2321 struct A { void f(); };
2322 struct B : public A {};
2323 struct C : public A { void g() { B::f(); }};
2325 "the class of that member function" refers to `A'. But 11.2
2326 [class.access.base] says that we need to convert 'this' to B* as
2327 part of the access, so we pass 'B' to maybe_dummy_object. */
2329 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2330 NULL);
2332 if (processing_template_decl)
2334 if (type_dependent_expression_p (object))
2336 tree ret = build_nt_call_vec (orig_fn, orig_args);
2337 release_tree_vector (orig_args);
2338 return ret;
2340 object = build_non_dependent_expr (object);
2343 result = build_new_method_call (object, fn, args, NULL_TREE,
2344 (disallow_virtual
2345 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2346 : LOOKUP_NORMAL),
2347 /*fn_p=*/NULL,
2348 complain);
2350 else if (is_overloaded_fn (fn))
2352 /* If the function is an overloaded builtin, resolve it. */
2353 if (TREE_CODE (fn) == FUNCTION_DECL
2354 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2355 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2356 result = resolve_overloaded_builtin (input_location, fn, *args);
2358 if (!result)
2360 if (warn_sizeof_pointer_memaccess
2361 && (complain & tf_warning)
2362 && !vec_safe_is_empty (*args)
2363 && !processing_template_decl)
2365 location_t sizeof_arg_loc[3];
2366 tree sizeof_arg[3];
2367 unsigned int i;
2368 for (i = 0; i < 3; i++)
2370 tree t;
2372 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2373 sizeof_arg[i] = NULL_TREE;
2374 if (i >= (*args)->length ())
2375 continue;
2376 t = (**args)[i];
2377 if (TREE_CODE (t) != SIZEOF_EXPR)
2378 continue;
2379 if (SIZEOF_EXPR_TYPE_P (t))
2380 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2381 else
2382 sizeof_arg[i] = TREE_OPERAND (t, 0);
2383 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2385 sizeof_pointer_memaccess_warning
2386 (sizeof_arg_loc, fn, *args,
2387 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2390 /* A call to a namespace-scope function. */
2391 result = build_new_function_call (fn, args, koenig_p, complain);
2394 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2396 if (!vec_safe_is_empty (*args))
2397 error ("arguments to destructor are not allowed");
2398 /* Mark the pseudo-destructor call as having side-effects so
2399 that we do not issue warnings about its use. */
2400 result = build1 (NOP_EXPR,
2401 void_type_node,
2402 TREE_OPERAND (fn, 0));
2403 TREE_SIDE_EFFECTS (result) = 1;
2405 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2406 /* If the "function" is really an object of class type, it might
2407 have an overloaded `operator ()'. */
2408 result = build_op_call (fn, args, complain);
2410 if (!result)
2411 /* A call where the function is unknown. */
2412 result = cp_build_function_call_vec (fn, args, complain);
2414 if (processing_template_decl && result != error_mark_node)
2416 if (INDIRECT_REF_P (result))
2417 result = TREE_OPERAND (result, 0);
2418 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2419 SET_EXPR_LOCATION (result, input_location);
2420 KOENIG_LOOKUP_P (result) = koenig_p;
2421 release_tree_vector (orig_args);
2422 result = convert_from_reference (result);
2425 if (koenig_p)
2427 /* Free garbage OVERLOADs from arg-dependent lookup. */
2428 tree next = NULL_TREE;
2429 for (fn = orig_fn;
2430 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2431 fn = next)
2433 if (processing_template_decl)
2434 /* In a template, we'll re-use them at instantiation time. */
2435 OVL_ARG_DEPENDENT (fn) = false;
2436 else
2438 next = OVL_CHAIN (fn);
2439 ggc_free (fn);
2444 return result;
2447 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2448 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2449 POSTDECREMENT_EXPR.) */
2451 tree
2452 finish_increment_expr (tree expr, enum tree_code code)
2454 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2457 /* Finish a use of `this'. Returns an expression for `this'. */
2459 tree
2460 finish_this_expr (void)
2462 tree result = NULL_TREE;
2464 if (current_class_ptr)
2466 tree type = TREE_TYPE (current_class_ref);
2468 /* In a lambda expression, 'this' refers to the captured 'this'. */
2469 if (LAMBDA_TYPE_P (type))
2470 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2471 else
2472 result = current_class_ptr;
2475 if (result)
2476 /* The keyword 'this' is a prvalue expression. */
2477 return rvalue (result);
2479 tree fn = current_nonlambda_function ();
2480 if (fn && DECL_STATIC_FUNCTION_P (fn))
2481 error ("%<this%> is unavailable for static member functions");
2482 else if (fn)
2483 error ("invalid use of %<this%> in non-member function");
2484 else
2485 error ("invalid use of %<this%> at top level");
2486 return error_mark_node;
2489 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2490 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2491 the TYPE for the type given. If SCOPE is non-NULL, the expression
2492 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2494 tree
2495 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2496 location_t loc)
2498 if (object == error_mark_node || destructor == error_mark_node)
2499 return error_mark_node;
2501 gcc_assert (TYPE_P (destructor));
2503 if (!processing_template_decl)
2505 if (scope == error_mark_node)
2507 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2508 return error_mark_node;
2510 if (is_auto (destructor))
2511 destructor = TREE_TYPE (object);
2512 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2514 error_at (loc,
2515 "qualified type %qT does not match destructor name ~%qT",
2516 scope, destructor);
2517 return error_mark_node;
2521 /* [expr.pseudo] says both:
2523 The type designated by the pseudo-destructor-name shall be
2524 the same as the object type.
2526 and:
2528 The cv-unqualified versions of the object type and of the
2529 type designated by the pseudo-destructor-name shall be the
2530 same type.
2532 We implement the more generous second sentence, since that is
2533 what most other compilers do. */
2534 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2535 destructor))
2537 error_at (loc, "%qE is not of type %qT", object, destructor);
2538 return error_mark_node;
2542 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2543 scope, destructor);
2546 /* Finish an expression of the form CODE EXPR. */
2548 tree
2549 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2550 tsubst_flags_t complain)
2552 tree result = build_x_unary_op (loc, code, expr, complain);
2553 if ((complain & tf_warning)
2554 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2555 overflow_warning (input_location, result);
2557 return result;
2560 /* Finish a compound-literal expression. TYPE is the type to which
2561 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2563 tree
2564 finish_compound_literal (tree type, tree compound_literal,
2565 tsubst_flags_t complain)
2567 if (type == error_mark_node)
2568 return error_mark_node;
2570 if (TREE_CODE (type) == REFERENCE_TYPE)
2572 compound_literal
2573 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2574 complain);
2575 return cp_build_c_cast (type, compound_literal, complain);
2578 if (!TYPE_OBJ_P (type))
2580 if (complain & tf_error)
2581 error ("compound literal of non-object type %qT", type);
2582 return error_mark_node;
2585 if (processing_template_decl)
2587 TREE_TYPE (compound_literal) = type;
2588 /* Mark the expression as a compound literal. */
2589 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2590 return compound_literal;
2593 type = complete_type (type);
2595 if (TYPE_NON_AGGREGATE_CLASS (type))
2597 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2598 everywhere that deals with function arguments would be a pain, so
2599 just wrap it in a TREE_LIST. The parser set a flag so we know
2600 that it came from T{} rather than T({}). */
2601 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2602 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2603 return build_functional_cast (type, compound_literal, complain);
2606 if (TREE_CODE (type) == ARRAY_TYPE
2607 && check_array_initializer (NULL_TREE, type, compound_literal))
2608 return error_mark_node;
2609 compound_literal = reshape_init (type, compound_literal, complain);
2610 if (SCALAR_TYPE_P (type)
2611 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2612 && !check_narrowing (type, compound_literal, complain))
2613 return error_mark_node;
2614 if (TREE_CODE (type) == ARRAY_TYPE
2615 && TYPE_DOMAIN (type) == NULL_TREE)
2617 cp_complete_array_type_or_error (&type, compound_literal,
2618 false, complain);
2619 if (type == error_mark_node)
2620 return error_mark_node;
2622 compound_literal = digest_init (type, compound_literal, complain);
2623 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2624 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2625 /* Put static/constant array temporaries in static variables, but always
2626 represent class temporaries with TARGET_EXPR so we elide copies. */
2627 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2628 && TREE_CODE (type) == ARRAY_TYPE
2629 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2630 && initializer_constant_valid_p (compound_literal, type))
2632 tree decl = create_temporary_var (type);
2633 DECL_INITIAL (decl) = compound_literal;
2634 TREE_STATIC (decl) = 1;
2635 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2637 /* 5.19 says that a constant expression can include an
2638 lvalue-rvalue conversion applied to "a glvalue of literal type
2639 that refers to a non-volatile temporary object initialized
2640 with a constant expression". Rather than try to communicate
2641 that this VAR_DECL is a temporary, just mark it constexpr. */
2642 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2643 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2644 TREE_CONSTANT (decl) = true;
2646 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2647 decl = pushdecl_top_level (decl);
2648 DECL_NAME (decl) = make_anon_name ();
2649 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2650 /* Make sure the destructor is callable. */
2651 tree clean = cxx_maybe_build_cleanup (decl, complain);
2652 if (clean == error_mark_node)
2653 return error_mark_node;
2654 return decl;
2656 else
2657 return get_target_expr_sfinae (compound_literal, complain);
2660 /* Return the declaration for the function-name variable indicated by
2661 ID. */
2663 tree
2664 finish_fname (tree id)
2666 tree decl;
2668 decl = fname_decl (input_location, C_RID_CODE (id), id);
2669 if (processing_template_decl && current_function_decl
2670 && decl != error_mark_node)
2671 decl = DECL_NAME (decl);
2672 return decl;
2675 /* Finish a translation unit. */
2677 void
2678 finish_translation_unit (void)
2680 /* In case there were missing closebraces,
2681 get us back to the global binding level. */
2682 pop_everything ();
2683 while (current_namespace != global_namespace)
2684 pop_namespace ();
2686 /* Do file scope __FUNCTION__ et al. */
2687 finish_fname_decls ();
2690 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2691 Returns the parameter. */
2693 tree
2694 finish_template_type_parm (tree aggr, tree identifier)
2696 if (aggr != class_type_node)
2698 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2699 aggr = class_type_node;
2702 return build_tree_list (aggr, identifier);
2705 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2706 Returns the parameter. */
2708 tree
2709 finish_template_template_parm (tree aggr, tree identifier)
2711 tree decl = build_decl (input_location,
2712 TYPE_DECL, identifier, NULL_TREE);
2713 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2714 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2715 DECL_TEMPLATE_RESULT (tmpl) = decl;
2716 DECL_ARTIFICIAL (decl) = 1;
2717 end_template_decl ();
2719 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2721 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2722 /*is_primary=*/true, /*is_partial=*/false,
2723 /*is_friend=*/0);
2725 return finish_template_type_parm (aggr, tmpl);
2728 /* ARGUMENT is the default-argument value for a template template
2729 parameter. If ARGUMENT is invalid, issue error messages and return
2730 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2732 tree
2733 check_template_template_default_arg (tree argument)
2735 if (TREE_CODE (argument) != TEMPLATE_DECL
2736 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2737 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2739 if (TREE_CODE (argument) == TYPE_DECL)
2740 error ("invalid use of type %qT as a default value for a template "
2741 "template-parameter", TREE_TYPE (argument));
2742 else
2743 error ("invalid default argument for a template template parameter");
2744 return error_mark_node;
2747 return argument;
2750 /* Begin a class definition, as indicated by T. */
2752 tree
2753 begin_class_definition (tree t)
2755 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2756 return error_mark_node;
2758 if (processing_template_parmlist)
2760 error ("definition of %q#T inside template parameter list", t);
2761 return error_mark_node;
2764 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2765 are passed the same as decimal scalar types. */
2766 if (TREE_CODE (t) == RECORD_TYPE
2767 && !processing_template_decl)
2769 tree ns = TYPE_CONTEXT (t);
2770 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2771 && DECL_CONTEXT (ns) == std_node
2772 && DECL_NAME (ns)
2773 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2775 const char *n = TYPE_NAME_STRING (t);
2776 if ((strcmp (n, "decimal32") == 0)
2777 || (strcmp (n, "decimal64") == 0)
2778 || (strcmp (n, "decimal128") == 0))
2779 TYPE_TRANSPARENT_AGGR (t) = 1;
2783 /* A non-implicit typename comes from code like:
2785 template <typename T> struct A {
2786 template <typename U> struct A<T>::B ...
2788 This is erroneous. */
2789 else if (TREE_CODE (t) == TYPENAME_TYPE)
2791 error ("invalid definition of qualified type %qT", t);
2792 t = error_mark_node;
2795 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2797 t = make_class_type (RECORD_TYPE);
2798 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2801 if (TYPE_BEING_DEFINED (t))
2803 t = make_class_type (TREE_CODE (t));
2804 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2806 maybe_process_partial_specialization (t);
2807 pushclass (t);
2808 TYPE_BEING_DEFINED (t) = 1;
2809 class_binding_level->defining_class_p = 1;
2811 if (flag_pack_struct)
2813 tree v;
2814 TYPE_PACKED (t) = 1;
2815 /* Even though the type is being defined for the first time
2816 here, there might have been a forward declaration, so there
2817 might be cv-qualified variants of T. */
2818 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2819 TYPE_PACKED (v) = 1;
2821 /* Reset the interface data, at the earliest possible
2822 moment, as it might have been set via a class foo;
2823 before. */
2824 if (! TYPE_ANONYMOUS_P (t))
2826 struct c_fileinfo *finfo = \
2827 get_fileinfo (LOCATION_FILE (input_location));
2828 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2829 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2830 (t, finfo->interface_unknown);
2832 reset_specialization();
2834 /* Make a declaration for this class in its own scope. */
2835 build_self_reference ();
2837 return t;
2840 /* Finish the member declaration given by DECL. */
2842 void
2843 finish_member_declaration (tree decl)
2845 if (decl == error_mark_node || decl == NULL_TREE)
2846 return;
2848 if (decl == void_type_node)
2849 /* The COMPONENT was a friend, not a member, and so there's
2850 nothing for us to do. */
2851 return;
2853 /* We should see only one DECL at a time. */
2854 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2856 /* Set up access control for DECL. */
2857 TREE_PRIVATE (decl)
2858 = (current_access_specifier == access_private_node);
2859 TREE_PROTECTED (decl)
2860 = (current_access_specifier == access_protected_node);
2861 if (TREE_CODE (decl) == TEMPLATE_DECL)
2863 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2864 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2867 /* Mark the DECL as a member of the current class, unless it's
2868 a member of an enumeration. */
2869 if (TREE_CODE (decl) != CONST_DECL)
2870 DECL_CONTEXT (decl) = current_class_type;
2872 /* Check for bare parameter packs in the member variable declaration. */
2873 if (TREE_CODE (decl) == FIELD_DECL)
2875 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2876 TREE_TYPE (decl) = error_mark_node;
2877 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2878 DECL_ATTRIBUTES (decl) = NULL_TREE;
2881 /* [dcl.link]
2883 A C language linkage is ignored for the names of class members
2884 and the member function type of class member functions. */
2885 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2886 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2888 /* Put functions on the TYPE_METHODS list and everything else on the
2889 TYPE_FIELDS list. Note that these are built up in reverse order.
2890 We reverse them (to obtain declaration order) in finish_struct. */
2891 if (DECL_DECLARES_FUNCTION_P (decl))
2893 /* We also need to add this function to the
2894 CLASSTYPE_METHOD_VEC. */
2895 if (add_method (current_class_type, decl, NULL_TREE))
2897 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2898 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2899 TYPE_METHODS (current_class_type) = decl;
2901 maybe_add_class_template_decl_list (current_class_type, decl,
2902 /*friend_p=*/0);
2905 /* Enter the DECL into the scope of the class, if the class
2906 isn't a closure (whose fields are supposed to be unnamed). */
2907 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2908 || pushdecl_class_level (decl))
2910 if (TREE_CODE (decl) == USING_DECL)
2912 /* For now, ignore class-scope USING_DECLS, so that
2913 debugging backends do not see them. */
2914 DECL_IGNORED_P (decl) = 1;
2917 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2918 go at the beginning. The reason is that lookup_field_1
2919 searches the list in order, and we want a field name to
2920 override a type name so that the "struct stat hack" will
2921 work. In particular:
2923 struct S { enum E { }; int E } s;
2924 s.E = 3;
2926 is valid. In addition, the FIELD_DECLs must be maintained in
2927 declaration order so that class layout works as expected.
2928 However, we don't need that order until class layout, so we
2929 save a little time by putting FIELD_DECLs on in reverse order
2930 here, and then reversing them in finish_struct_1. (We could
2931 also keep a pointer to the correct insertion points in the
2932 list.) */
2934 if (TREE_CODE (decl) == TYPE_DECL)
2935 TYPE_FIELDS (current_class_type)
2936 = chainon (TYPE_FIELDS (current_class_type), decl);
2937 else
2939 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2940 TYPE_FIELDS (current_class_type) = decl;
2943 maybe_add_class_template_decl_list (current_class_type, decl,
2944 /*friend_p=*/0);
2947 if (pch_file)
2948 note_decl_for_pch (decl);
2951 /* DECL has been declared while we are building a PCH file. Perform
2952 actions that we might normally undertake lazily, but which can be
2953 performed now so that they do not have to be performed in
2954 translation units which include the PCH file. */
2956 void
2957 note_decl_for_pch (tree decl)
2959 gcc_assert (pch_file);
2961 /* There's a good chance that we'll have to mangle names at some
2962 point, even if only for emission in debugging information. */
2963 if (VAR_OR_FUNCTION_DECL_P (decl)
2964 && !processing_template_decl)
2965 mangle_decl (decl);
2968 /* Finish processing a complete template declaration. The PARMS are
2969 the template parameters. */
2971 void
2972 finish_template_decl (tree parms)
2974 if (parms)
2975 end_template_decl ();
2976 else
2977 end_specialization ();
2980 /* Finish processing a template-id (which names a type) of the form
2981 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2982 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2983 the scope of template-id indicated. */
2985 tree
2986 finish_template_type (tree name, tree args, int entering_scope)
2988 tree type;
2990 type = lookup_template_class (name, args,
2991 NULL_TREE, NULL_TREE, entering_scope,
2992 tf_warning_or_error | tf_user);
2993 if (type == error_mark_node)
2994 return type;
2995 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2996 return TYPE_STUB_DECL (type);
2997 else
2998 return TYPE_NAME (type);
3001 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3002 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3003 BASE_CLASS, or NULL_TREE if an error occurred. The
3004 ACCESS_SPECIFIER is one of
3005 access_{default,public,protected_private}_node. For a virtual base
3006 we set TREE_TYPE. */
3008 tree
3009 finish_base_specifier (tree base, tree access, bool virtual_p)
3011 tree result;
3013 if (base == error_mark_node)
3015 error ("invalid base-class specification");
3016 result = NULL_TREE;
3018 else if (! MAYBE_CLASS_TYPE_P (base))
3020 error ("%qT is not a class type", base);
3021 result = NULL_TREE;
3023 else
3025 if (cp_type_quals (base) != 0)
3027 /* DR 484: Can a base-specifier name a cv-qualified
3028 class type? */
3029 base = TYPE_MAIN_VARIANT (base);
3031 result = build_tree_list (access, base);
3032 if (virtual_p)
3033 TREE_TYPE (result) = integer_type_node;
3036 return result;
3039 /* If FNS is a member function, a set of member functions, or a
3040 template-id referring to one or more member functions, return a
3041 BASELINK for FNS, incorporating the current access context.
3042 Otherwise, return FNS unchanged. */
3044 tree
3045 baselink_for_fns (tree fns)
3047 tree scope;
3048 tree cl;
3050 if (BASELINK_P (fns)
3051 || error_operand_p (fns))
3052 return fns;
3054 scope = ovl_scope (fns);
3055 if (!CLASS_TYPE_P (scope))
3056 return fns;
3058 cl = currently_open_derived_class (scope);
3059 if (!cl)
3060 cl = scope;
3061 cl = TYPE_BINFO (cl);
3062 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3065 /* Returns true iff DECL is a variable from a function outside
3066 the current one. */
3068 static bool
3069 outer_var_p (tree decl)
3071 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3072 && DECL_FUNCTION_SCOPE_P (decl)
3073 && (DECL_CONTEXT (decl) != current_function_decl
3074 || parsing_nsdmi ()));
3077 /* As above, but also checks that DECL is automatic. */
3079 bool
3080 outer_automatic_var_p (tree decl)
3082 return (outer_var_p (decl)
3083 && !TREE_STATIC (decl));
3086 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3087 rewrite it for lambda capture. */
3089 tree
3090 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3092 if (cp_unevaluated_operand)
3093 /* It's not a use (3.2) if we're in an unevaluated context. */
3094 return decl;
3095 if (decl == error_mark_node)
3096 return decl;
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 if (!mark_used (decl, complain) && !(complain & tf_error))
3106 return error_mark_node;
3108 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3109 support for an approach in which a reference to a local
3110 [constant] automatic variable in a nested class or lambda body
3111 would enter the expression as an rvalue, which would reduce
3112 the complexity of the problem"
3114 FIXME update for final resolution of core issue 696. */
3115 if (decl_maybe_constant_var_p (decl))
3117 if (processing_template_decl)
3118 /* In a template, the constant value may not be in a usable
3119 form, so wait until instantiation time. */
3120 return decl;
3121 else if (decl_constant_var_p (decl))
3123 tree t = maybe_constant_value (convert_from_reference (decl));
3124 if (TREE_CONSTANT (t))
3125 return t;
3129 if (parsing_nsdmi ())
3130 containing_function = NULL_TREE;
3131 else
3132 /* If we are in a lambda function, we can move out until we hit
3133 1. the context,
3134 2. a non-lambda function, or
3135 3. a non-default capturing lambda function. */
3136 while (context != containing_function
3137 && LAMBDA_FUNCTION_P (containing_function))
3139 tree closure = DECL_CONTEXT (containing_function);
3140 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3142 if (TYPE_CLASS_SCOPE_P (closure))
3143 /* A lambda in an NSDMI (c++/64496). */
3144 break;
3146 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3147 == CPLD_NONE)
3148 break;
3150 lambda_stack = tree_cons (NULL_TREE,
3151 lambda_expr,
3152 lambda_stack);
3154 containing_function
3155 = decl_function_context (containing_function);
3158 if (lambda_expr && VAR_P (decl)
3159 && DECL_ANON_UNION_VAR_P (decl))
3161 if (complain & tf_error)
3162 error ("cannot capture member %qD of anonymous union", decl);
3163 return error_mark_node;
3165 if (context == containing_function)
3167 decl = add_default_capture (lambda_stack,
3168 /*id=*/DECL_NAME (decl),
3169 initializer);
3171 else if (lambda_expr)
3173 if (complain & tf_error)
3175 error ("%qD is not captured", decl);
3176 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3177 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3178 == CPLD_NONE)
3179 inform (location_of (closure),
3180 "the lambda has no capture-default");
3181 else if (TYPE_CLASS_SCOPE_P (closure))
3182 inform (0, "lambda in local class %q+T cannot "
3183 "capture variables from the enclosing context",
3184 TYPE_CONTEXT (closure));
3185 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3187 return error_mark_node;
3189 else
3191 if (complain & tf_error)
3192 error (VAR_P (decl)
3193 ? G_("use of local variable with automatic storage from containing function")
3194 : G_("use of parameter from containing function"));
3195 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3196 return error_mark_node;
3198 return decl;
3201 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3202 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3203 if non-NULL, is the type or namespace used to explicitly qualify
3204 ID_EXPRESSION. DECL is the entity to which that name has been
3205 resolved.
3207 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3208 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3209 be set to true if this expression isn't permitted in a
3210 constant-expression, but it is otherwise not set by this function.
3211 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3212 constant-expression, but a non-constant expression is also
3213 permissible.
3215 DONE is true if this expression is a complete postfix-expression;
3216 it is false if this expression is followed by '->', '[', '(', etc.
3217 ADDRESS_P is true iff this expression is the operand of '&'.
3218 TEMPLATE_P is true iff the qualified-id was of the form
3219 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3220 appears as a template argument.
3222 If an error occurs, and it is the kind of error that might cause
3223 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3224 is the caller's responsibility to issue the message. *ERROR_MSG
3225 will be a string with static storage duration, so the caller need
3226 not "free" it.
3228 Return an expression for the entity, after issuing appropriate
3229 diagnostics. This function is also responsible for transforming a
3230 reference to a non-static member into a COMPONENT_REF that makes
3231 the use of "this" explicit.
3233 Upon return, *IDK will be filled in appropriately. */
3234 tree
3235 finish_id_expression (tree id_expression,
3236 tree decl,
3237 tree scope,
3238 cp_id_kind *idk,
3239 bool integral_constant_expression_p,
3240 bool allow_non_integral_constant_expression_p,
3241 bool *non_integral_constant_expression_p,
3242 bool template_p,
3243 bool done,
3244 bool address_p,
3245 bool template_arg_p,
3246 const char **error_msg,
3247 location_t location)
3249 decl = strip_using_decl (decl);
3251 /* Initialize the output parameters. */
3252 *idk = CP_ID_KIND_NONE;
3253 *error_msg = NULL;
3255 if (id_expression == error_mark_node)
3256 return error_mark_node;
3257 /* If we have a template-id, then no further lookup is
3258 required. If the template-id was for a template-class, we
3259 will sometimes have a TYPE_DECL at this point. */
3260 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3261 || TREE_CODE (decl) == TYPE_DECL)
3263 /* Look up the name. */
3264 else
3266 if (decl == error_mark_node)
3268 /* Name lookup failed. */
3269 if (scope
3270 && (!TYPE_P (scope)
3271 || (!dependent_type_p (scope)
3272 && !(identifier_p (id_expression)
3273 && IDENTIFIER_TYPENAME_P (id_expression)
3274 && dependent_type_p (TREE_TYPE (id_expression))))))
3276 /* If the qualifying type is non-dependent (and the name
3277 does not name a conversion operator to a dependent
3278 type), issue an error. */
3279 qualified_name_lookup_error (scope, id_expression, decl, location);
3280 return error_mark_node;
3282 else if (!scope)
3284 /* It may be resolved via Koenig lookup. */
3285 *idk = CP_ID_KIND_UNQUALIFIED;
3286 return id_expression;
3288 else
3289 decl = id_expression;
3291 /* If DECL is a variable that would be out of scope under
3292 ANSI/ISO rules, but in scope in the ARM, name lookup
3293 will succeed. Issue a diagnostic here. */
3294 else
3295 decl = check_for_out_of_scope_variable (decl);
3297 /* Remember that the name was used in the definition of
3298 the current class so that we can check later to see if
3299 the meaning would have been different after the class
3300 was entirely defined. */
3301 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3302 maybe_note_name_used_in_class (id_expression, decl);
3304 /* Disallow uses of local variables from containing functions, except
3305 within lambda-expressions. */
3306 if (outer_automatic_var_p (decl))
3308 decl = process_outer_var_ref (decl, tf_warning_or_error);
3309 if (decl == error_mark_node)
3310 return error_mark_node;
3313 /* Also disallow uses of function parameters outside the function
3314 body, except inside an unevaluated context (i.e. decltype). */
3315 if (TREE_CODE (decl) == PARM_DECL
3316 && DECL_CONTEXT (decl) == NULL_TREE
3317 && !cp_unevaluated_operand)
3319 *error_msg = "use of parameter outside function body";
3320 return error_mark_node;
3324 /* If we didn't find anything, or what we found was a type,
3325 then this wasn't really an id-expression. */
3326 if (TREE_CODE (decl) == TEMPLATE_DECL
3327 && !DECL_FUNCTION_TEMPLATE_P (decl))
3329 *error_msg = "missing template arguments";
3330 return error_mark_node;
3332 else if (TREE_CODE (decl) == TYPE_DECL
3333 || TREE_CODE (decl) == NAMESPACE_DECL)
3335 *error_msg = "expected primary-expression";
3336 return error_mark_node;
3339 /* If the name resolved to a template parameter, there is no
3340 need to look it up again later. */
3341 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3342 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3344 tree r;
3346 *idk = CP_ID_KIND_NONE;
3347 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3348 decl = TEMPLATE_PARM_DECL (decl);
3349 r = convert_from_reference (DECL_INITIAL (decl));
3351 if (integral_constant_expression_p
3352 && !dependent_type_p (TREE_TYPE (decl))
3353 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3355 if (!allow_non_integral_constant_expression_p)
3356 error ("template parameter %qD of type %qT is not allowed in "
3357 "an integral constant expression because it is not of "
3358 "integral or enumeration type", decl, TREE_TYPE (decl));
3359 *non_integral_constant_expression_p = true;
3361 return r;
3363 else
3365 bool dependent_p;
3367 /* If the declaration was explicitly qualified indicate
3368 that. The semantics of `A::f(3)' are different than
3369 `f(3)' if `f' is virtual. */
3370 *idk = (scope
3371 ? CP_ID_KIND_QUALIFIED
3372 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3373 ? CP_ID_KIND_TEMPLATE_ID
3374 : CP_ID_KIND_UNQUALIFIED));
3377 /* [temp.dep.expr]
3379 An id-expression is type-dependent if it contains an
3380 identifier that was declared with a dependent type.
3382 The standard is not very specific about an id-expression that
3383 names a set of overloaded functions. What if some of them
3384 have dependent types and some of them do not? Presumably,
3385 such a name should be treated as a dependent name. */
3386 /* Assume the name is not dependent. */
3387 dependent_p = false;
3388 if (!processing_template_decl)
3389 /* No names are dependent outside a template. */
3391 else if (TREE_CODE (decl) == CONST_DECL)
3392 /* We don't want to treat enumerators as dependent. */
3394 /* A template-id where the name of the template was not resolved
3395 is definitely dependent. */
3396 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3397 && (identifier_p (TREE_OPERAND (decl, 0))))
3398 dependent_p = true;
3399 /* For anything except an overloaded function, just check its
3400 type. */
3401 else if (!is_overloaded_fn (decl))
3402 dependent_p
3403 = dependent_type_p (TREE_TYPE (decl));
3404 /* For a set of overloaded functions, check each of the
3405 functions. */
3406 else
3408 tree fns = decl;
3410 if (BASELINK_P (fns))
3411 fns = BASELINK_FUNCTIONS (fns);
3413 /* For a template-id, check to see if the template
3414 arguments are dependent. */
3415 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3417 tree args = TREE_OPERAND (fns, 1);
3418 dependent_p = any_dependent_template_arguments_p (args);
3419 /* The functions are those referred to by the
3420 template-id. */
3421 fns = TREE_OPERAND (fns, 0);
3424 /* If there are no dependent template arguments, go through
3425 the overloaded functions. */
3426 while (fns && !dependent_p)
3428 tree fn = OVL_CURRENT (fns);
3430 /* Member functions of dependent classes are
3431 dependent. */
3432 if (TREE_CODE (fn) == FUNCTION_DECL
3433 && type_dependent_expression_p (fn))
3434 dependent_p = true;
3435 else if (TREE_CODE (fn) == TEMPLATE_DECL
3436 && dependent_template_p (fn))
3437 dependent_p = true;
3439 fns = OVL_NEXT (fns);
3443 /* If the name was dependent on a template parameter, we will
3444 resolve the name at instantiation time. */
3445 if (dependent_p)
3447 /* Create a SCOPE_REF for qualified names, if the scope is
3448 dependent. */
3449 if (scope)
3451 if (TYPE_P (scope))
3453 if (address_p && done)
3454 decl = finish_qualified_id_expr (scope, decl,
3455 done, address_p,
3456 template_p,
3457 template_arg_p,
3458 tf_warning_or_error);
3459 else
3461 tree type = NULL_TREE;
3462 if (DECL_P (decl) && !dependent_scope_p (scope))
3463 type = TREE_TYPE (decl);
3464 decl = build_qualified_name (type,
3465 scope,
3466 id_expression,
3467 template_p);
3470 if (TREE_TYPE (decl))
3471 decl = convert_from_reference (decl);
3472 return decl;
3474 /* A TEMPLATE_ID already contains all the information we
3475 need. */
3476 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3477 return id_expression;
3478 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3479 /* If we found a variable, then name lookup during the
3480 instantiation will always resolve to the same VAR_DECL
3481 (or an instantiation thereof). */
3482 if (VAR_P (decl)
3483 || TREE_CODE (decl) == PARM_DECL)
3485 mark_used (decl);
3486 return convert_from_reference (decl);
3488 /* The same is true for FIELD_DECL, but we also need to
3489 make sure that the syntax is correct. */
3490 else if (TREE_CODE (decl) == FIELD_DECL)
3492 /* Since SCOPE is NULL here, this is an unqualified name.
3493 Access checking has been performed during name lookup
3494 already. Turn off checking to avoid duplicate errors. */
3495 push_deferring_access_checks (dk_no_check);
3496 decl = finish_non_static_data_member
3497 (decl, NULL_TREE,
3498 /*qualifying_scope=*/NULL_TREE);
3499 pop_deferring_access_checks ();
3500 return decl;
3502 return id_expression;
3505 if (TREE_CODE (decl) == NAMESPACE_DECL)
3507 error ("use of namespace %qD as expression", decl);
3508 return error_mark_node;
3510 else if (DECL_CLASS_TEMPLATE_P (decl))
3512 error ("use of class template %qT as expression", decl);
3513 return error_mark_node;
3515 else if (TREE_CODE (decl) == TREE_LIST)
3517 /* Ambiguous reference to base members. */
3518 error ("request for member %qD is ambiguous in "
3519 "multiple inheritance lattice", id_expression);
3520 print_candidates (decl);
3521 return error_mark_node;
3524 /* Mark variable-like entities as used. Functions are similarly
3525 marked either below or after overload resolution. */
3526 if ((VAR_P (decl)
3527 || TREE_CODE (decl) == PARM_DECL
3528 || TREE_CODE (decl) == CONST_DECL
3529 || TREE_CODE (decl) == RESULT_DECL)
3530 && !mark_used (decl))
3531 return error_mark_node;
3533 /* Only certain kinds of names are allowed in constant
3534 expression. Template parameters have already
3535 been handled above. */
3536 if (! error_operand_p (decl)
3537 && integral_constant_expression_p
3538 && ! decl_constant_var_p (decl)
3539 && TREE_CODE (decl) != CONST_DECL
3540 && ! builtin_valid_in_constant_expr_p (decl))
3542 if (!allow_non_integral_constant_expression_p)
3544 error ("%qD cannot appear in a constant-expression", decl);
3545 return error_mark_node;
3547 *non_integral_constant_expression_p = true;
3550 tree wrap;
3551 if (VAR_P (decl)
3552 && !cp_unevaluated_operand
3553 && !processing_template_decl
3554 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3555 && CP_DECL_THREAD_LOCAL_P (decl)
3556 && (wrap = get_tls_wrapper_fn (decl)))
3558 /* Replace an evaluated use of the thread_local variable with
3559 a call to its wrapper. */
3560 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3562 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3563 && variable_template_p (TREE_OPERAND (decl, 0)))
3565 decl = finish_template_variable (decl);
3566 mark_used (decl);
3568 else if (scope)
3570 decl = (adjust_result_of_qualified_name_lookup
3571 (decl, scope, current_nonlambda_class_type()));
3573 if (TREE_CODE (decl) == FUNCTION_DECL)
3574 mark_used (decl);
3576 if (TYPE_P (scope))
3577 decl = finish_qualified_id_expr (scope,
3578 decl,
3579 done,
3580 address_p,
3581 template_p,
3582 template_arg_p,
3583 tf_warning_or_error);
3584 else
3585 decl = convert_from_reference (decl);
3587 else if (TREE_CODE (decl) == FIELD_DECL)
3589 /* Since SCOPE is NULL here, this is an unqualified name.
3590 Access checking has been performed during name lookup
3591 already. Turn off checking to avoid duplicate errors. */
3592 push_deferring_access_checks (dk_no_check);
3593 decl = finish_non_static_data_member (decl, NULL_TREE,
3594 /*qualifying_scope=*/NULL_TREE);
3595 pop_deferring_access_checks ();
3597 else if (is_overloaded_fn (decl))
3599 tree first_fn;
3601 first_fn = get_first_fn (decl);
3602 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3603 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3605 if (!really_overloaded_fn (decl)
3606 && !mark_used (first_fn))
3607 return error_mark_node;
3609 if (!template_arg_p
3610 && TREE_CODE (first_fn) == FUNCTION_DECL
3611 && DECL_FUNCTION_MEMBER_P (first_fn)
3612 && !shared_member_p (decl))
3614 /* A set of member functions. */
3615 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3616 return finish_class_member_access_expr (decl, id_expression,
3617 /*template_p=*/false,
3618 tf_warning_or_error);
3621 decl = baselink_for_fns (decl);
3623 else
3625 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3626 && DECL_CLASS_SCOPE_P (decl))
3628 tree context = context_for_name_lookup (decl);
3629 if (context != current_class_type)
3631 tree path = currently_open_derived_class (context);
3632 perform_or_defer_access_check (TYPE_BINFO (path),
3633 decl, decl,
3634 tf_warning_or_error);
3638 decl = convert_from_reference (decl);
3642 return decl;
3645 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3646 use as a type-specifier. */
3648 tree
3649 finish_typeof (tree expr)
3651 tree type;
3653 if (type_dependent_expression_p (expr))
3655 type = cxx_make_type (TYPEOF_TYPE);
3656 TYPEOF_TYPE_EXPR (type) = expr;
3657 SET_TYPE_STRUCTURAL_EQUALITY (type);
3659 return type;
3662 expr = mark_type_use (expr);
3664 type = unlowered_expr_type (expr);
3666 if (!type || type == unknown_type_node)
3668 error ("type of %qE is unknown", expr);
3669 return error_mark_node;
3672 return type;
3675 /* Implement the __underlying_type keyword: Return the underlying
3676 type of TYPE, suitable for use as a type-specifier. */
3678 tree
3679 finish_underlying_type (tree type)
3681 tree underlying_type;
3683 if (processing_template_decl)
3685 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3686 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3687 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3689 return underlying_type;
3692 complete_type (type);
3694 if (TREE_CODE (type) != ENUMERAL_TYPE)
3696 error ("%qT is not an enumeration type", type);
3697 return error_mark_node;
3700 underlying_type = ENUM_UNDERLYING_TYPE (type);
3702 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3703 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3704 See finish_enum_value_list for details. */
3705 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3706 underlying_type
3707 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3708 TYPE_UNSIGNED (underlying_type));
3710 return underlying_type;
3713 /* Implement the __direct_bases keyword: Return the direct base classes
3714 of type */
3716 tree
3717 calculate_direct_bases (tree type)
3719 vec<tree, va_gc> *vector = make_tree_vector();
3720 tree bases_vec = NULL_TREE;
3721 vec<tree, va_gc> *base_binfos;
3722 tree binfo;
3723 unsigned i;
3725 complete_type (type);
3727 if (!NON_UNION_CLASS_TYPE_P (type))
3728 return make_tree_vec (0);
3730 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3732 /* Virtual bases are initialized first */
3733 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3735 if (BINFO_VIRTUAL_P (binfo))
3737 vec_safe_push (vector, binfo);
3741 /* Now non-virtuals */
3742 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3744 if (!BINFO_VIRTUAL_P (binfo))
3746 vec_safe_push (vector, binfo);
3751 bases_vec = make_tree_vec (vector->length ());
3753 for (i = 0; i < vector->length (); ++i)
3755 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3757 return bases_vec;
3760 /* Implement the __bases keyword: Return the base classes
3761 of type */
3763 /* Find morally non-virtual base classes by walking binfo hierarchy */
3764 /* Virtual base classes are handled separately in finish_bases */
3766 static tree
3767 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3769 /* Don't walk bases of virtual bases */
3770 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3773 static tree
3774 dfs_calculate_bases_post (tree binfo, void *data_)
3776 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3777 if (!BINFO_VIRTUAL_P (binfo))
3779 vec_safe_push (*data, BINFO_TYPE (binfo));
3781 return NULL_TREE;
3784 /* Calculates the morally non-virtual base classes of a class */
3785 static vec<tree, va_gc> *
3786 calculate_bases_helper (tree type)
3788 vec<tree, va_gc> *vector = make_tree_vector();
3790 /* Now add non-virtual base classes in order of construction */
3791 dfs_walk_all (TYPE_BINFO (type),
3792 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3793 return vector;
3796 tree
3797 calculate_bases (tree type)
3799 vec<tree, va_gc> *vector = make_tree_vector();
3800 tree bases_vec = NULL_TREE;
3801 unsigned i;
3802 vec<tree, va_gc> *vbases;
3803 vec<tree, va_gc> *nonvbases;
3804 tree binfo;
3806 complete_type (type);
3808 if (!NON_UNION_CLASS_TYPE_P (type))
3809 return make_tree_vec (0);
3811 /* First go through virtual base classes */
3812 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3813 vec_safe_iterate (vbases, i, &binfo); i++)
3815 vec<tree, va_gc> *vbase_bases;
3816 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3817 vec_safe_splice (vector, vbase_bases);
3818 release_tree_vector (vbase_bases);
3821 /* Now for the non-virtual bases */
3822 nonvbases = calculate_bases_helper (type);
3823 vec_safe_splice (vector, nonvbases);
3824 release_tree_vector (nonvbases);
3826 /* Last element is entire class, so don't copy */
3827 bases_vec = make_tree_vec (vector->length () - 1);
3829 for (i = 0; i < vector->length () - 1; ++i)
3831 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3833 release_tree_vector (vector);
3834 return bases_vec;
3837 tree
3838 finish_bases (tree type, bool direct)
3840 tree bases = NULL_TREE;
3842 if (!processing_template_decl)
3844 /* Parameter packs can only be used in templates */
3845 error ("Parameter pack __bases only valid in template declaration");
3846 return error_mark_node;
3849 bases = cxx_make_type (BASES);
3850 BASES_TYPE (bases) = type;
3851 BASES_DIRECT (bases) = direct;
3852 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3854 return bases;
3857 /* Perform C++-specific checks for __builtin_offsetof before calling
3858 fold_offsetof. */
3860 tree
3861 finish_offsetof (tree expr, location_t loc)
3863 /* If we're processing a template, we can't finish the semantics yet.
3864 Otherwise we can fold the entire expression now. */
3865 if (processing_template_decl)
3867 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3868 SET_EXPR_LOCATION (expr, loc);
3869 return expr;
3872 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3874 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3875 TREE_OPERAND (expr, 2));
3876 return error_mark_node;
3878 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3879 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3880 || TREE_TYPE (expr) == unknown_type_node)
3882 if (INDIRECT_REF_P (expr))
3883 error ("second operand of %<offsetof%> is neither a single "
3884 "identifier nor a sequence of member accesses and "
3885 "array references");
3886 else
3888 if (TREE_CODE (expr) == COMPONENT_REF
3889 || TREE_CODE (expr) == COMPOUND_EXPR)
3890 expr = TREE_OPERAND (expr, 1);
3891 error ("cannot apply %<offsetof%> to member function %qD", expr);
3893 return error_mark_node;
3895 if (REFERENCE_REF_P (expr))
3896 expr = TREE_OPERAND (expr, 0);
3897 if (TREE_CODE (expr) == COMPONENT_REF)
3899 tree object = TREE_OPERAND (expr, 0);
3900 if (!complete_type_or_else (TREE_TYPE (object), object))
3901 return error_mark_node;
3902 if (warn_invalid_offsetof
3903 && CLASS_TYPE_P (TREE_TYPE (object))
3904 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3905 && cp_unevaluated_operand == 0)
3906 pedwarn (loc, OPT_Winvalid_offsetof,
3907 "offsetof within non-standard-layout type %qT is undefined",
3908 TREE_TYPE (object));
3910 return fold_offsetof (expr);
3913 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3914 function is broken out from the above for the benefit of the tree-ssa
3915 project. */
3917 void
3918 simplify_aggr_init_expr (tree *tp)
3920 tree aggr_init_expr = *tp;
3922 /* Form an appropriate CALL_EXPR. */
3923 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3924 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3925 tree type = TREE_TYPE (slot);
3927 tree call_expr;
3928 enum style_t { ctor, arg, pcc } style;
3930 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3931 style = ctor;
3932 #ifdef PCC_STATIC_STRUCT_RETURN
3933 else if (1)
3934 style = pcc;
3935 #endif
3936 else
3938 gcc_assert (TREE_ADDRESSABLE (type));
3939 style = arg;
3942 call_expr = build_call_array_loc (input_location,
3943 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3945 aggr_init_expr_nargs (aggr_init_expr),
3946 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3947 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3948 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3950 if (style == ctor)
3952 /* Replace the first argument to the ctor with the address of the
3953 slot. */
3954 cxx_mark_addressable (slot);
3955 CALL_EXPR_ARG (call_expr, 0) =
3956 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3958 else if (style == arg)
3960 /* Just mark it addressable here, and leave the rest to
3961 expand_call{,_inline}. */
3962 cxx_mark_addressable (slot);
3963 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3964 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3966 else if (style == pcc)
3968 /* If we're using the non-reentrant PCC calling convention, then we
3969 need to copy the returned value out of the static buffer into the
3970 SLOT. */
3971 push_deferring_access_checks (dk_no_check);
3972 call_expr = build_aggr_init (slot, call_expr,
3973 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3974 tf_warning_or_error);
3975 pop_deferring_access_checks ();
3976 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3979 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3981 tree init = build_zero_init (type, NULL_TREE,
3982 /*static_storage_p=*/false);
3983 init = build2 (INIT_EXPR, void_type_node, slot, init);
3984 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3985 init, call_expr);
3988 *tp = call_expr;
3991 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3993 void
3994 emit_associated_thunks (tree fn)
3996 /* When we use vcall offsets, we emit thunks with the virtual
3997 functions to which they thunk. The whole point of vcall offsets
3998 is so that you can know statically the entire set of thunks that
3999 will ever be needed for a given virtual function, thereby
4000 enabling you to output all the thunks with the function itself. */
4001 if (DECL_VIRTUAL_P (fn)
4002 /* Do not emit thunks for extern template instantiations. */
4003 && ! DECL_REALLY_EXTERN (fn))
4005 tree thunk;
4007 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4009 if (!THUNK_ALIAS (thunk))
4011 use_thunk (thunk, /*emit_p=*/1);
4012 if (DECL_RESULT_THUNK_P (thunk))
4014 tree probe;
4016 for (probe = DECL_THUNKS (thunk);
4017 probe; probe = DECL_CHAIN (probe))
4018 use_thunk (probe, /*emit_p=*/1);
4021 else
4022 gcc_assert (!DECL_THUNKS (thunk));
4027 /* Generate RTL for FN. */
4029 bool
4030 expand_or_defer_fn_1 (tree fn)
4032 /* When the parser calls us after finishing the body of a template
4033 function, we don't really want to expand the body. */
4034 if (processing_template_decl)
4036 /* Normally, collection only occurs in rest_of_compilation. So,
4037 if we don't collect here, we never collect junk generated
4038 during the processing of templates until we hit a
4039 non-template function. It's not safe to do this inside a
4040 nested class, though, as the parser may have local state that
4041 is not a GC root. */
4042 if (!function_depth)
4043 ggc_collect ();
4044 return false;
4047 gcc_assert (DECL_SAVED_TREE (fn));
4049 /* We make a decision about linkage for these functions at the end
4050 of the compilation. Until that point, we do not want the back
4051 end to output them -- but we do want it to see the bodies of
4052 these functions so that it can inline them as appropriate. */
4053 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4055 if (DECL_INTERFACE_KNOWN (fn))
4056 /* We've already made a decision as to how this function will
4057 be handled. */;
4058 else if (!at_eof)
4059 tentative_decl_linkage (fn);
4060 else
4061 import_export_decl (fn);
4063 /* If the user wants us to keep all inline functions, then mark
4064 this function as needed so that finish_file will make sure to
4065 output it later. Similarly, all dllexport'd functions must
4066 be emitted; there may be callers in other DLLs. */
4067 if (DECL_DECLARED_INLINE_P (fn)
4068 && !DECL_REALLY_EXTERN (fn)
4069 && (flag_keep_inline_functions
4070 || (flag_keep_inline_dllexport
4071 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4073 mark_needed (fn);
4074 DECL_EXTERNAL (fn) = 0;
4078 /* If this is a constructor or destructor body, we have to clone
4079 it. */
4080 if (maybe_clone_body (fn))
4082 /* We don't want to process FN again, so pretend we've written
4083 it out, even though we haven't. */
4084 TREE_ASM_WRITTEN (fn) = 1;
4085 /* If this is an instantiation of a constexpr function, keep
4086 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4087 if (!is_instantiation_of_constexpr (fn))
4088 DECL_SAVED_TREE (fn) = NULL_TREE;
4089 return false;
4092 /* There's no reason to do any of the work here if we're only doing
4093 semantic analysis; this code just generates RTL. */
4094 if (flag_syntax_only)
4095 return false;
4097 return true;
4100 void
4101 expand_or_defer_fn (tree fn)
4103 if (expand_or_defer_fn_1 (fn))
4105 function_depth++;
4107 /* Expand or defer, at the whim of the compilation unit manager. */
4108 cgraph_node::finalize_function (fn, function_depth > 1);
4109 emit_associated_thunks (fn);
4111 function_depth--;
4115 struct nrv_data
4117 nrv_data () : visited (37) {}
4119 tree var;
4120 tree result;
4121 hash_table<nofree_ptr_hash <tree_node> > visited;
4124 /* Helper function for walk_tree, used by finalize_nrv below. */
4126 static tree
4127 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4129 struct nrv_data *dp = (struct nrv_data *)data;
4130 tree_node **slot;
4132 /* No need to walk into types. There wouldn't be any need to walk into
4133 non-statements, except that we have to consider STMT_EXPRs. */
4134 if (TYPE_P (*tp))
4135 *walk_subtrees = 0;
4136 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4137 but differs from using NULL_TREE in that it indicates that we care
4138 about the value of the RESULT_DECL. */
4139 else if (TREE_CODE (*tp) == RETURN_EXPR)
4140 TREE_OPERAND (*tp, 0) = dp->result;
4141 /* Change all cleanups for the NRV to only run when an exception is
4142 thrown. */
4143 else if (TREE_CODE (*tp) == CLEANUP_STMT
4144 && CLEANUP_DECL (*tp) == dp->var)
4145 CLEANUP_EH_ONLY (*tp) = 1;
4146 /* Replace the DECL_EXPR for the NRV with an initialization of the
4147 RESULT_DECL, if needed. */
4148 else if (TREE_CODE (*tp) == DECL_EXPR
4149 && DECL_EXPR_DECL (*tp) == dp->var)
4151 tree init;
4152 if (DECL_INITIAL (dp->var)
4153 && DECL_INITIAL (dp->var) != error_mark_node)
4154 init = build2 (INIT_EXPR, void_type_node, dp->result,
4155 DECL_INITIAL (dp->var));
4156 else
4157 init = build_empty_stmt (EXPR_LOCATION (*tp));
4158 DECL_INITIAL (dp->var) = NULL_TREE;
4159 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4160 *tp = init;
4162 /* And replace all uses of the NRV with the RESULT_DECL. */
4163 else if (*tp == dp->var)
4164 *tp = dp->result;
4166 /* Avoid walking into the same tree more than once. Unfortunately, we
4167 can't just use walk_tree_without duplicates because it would only call
4168 us for the first occurrence of dp->var in the function body. */
4169 slot = dp->visited.find_slot (*tp, INSERT);
4170 if (*slot)
4171 *walk_subtrees = 0;
4172 else
4173 *slot = *tp;
4175 /* Keep iterating. */
4176 return NULL_TREE;
4179 /* Called from finish_function to implement the named return value
4180 optimization by overriding all the RETURN_EXPRs and pertinent
4181 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4182 RESULT_DECL for the function. */
4184 void
4185 finalize_nrv (tree *tp, tree var, tree result)
4187 struct nrv_data data;
4189 /* Copy name from VAR to RESULT. */
4190 DECL_NAME (result) = DECL_NAME (var);
4191 /* Don't forget that we take its address. */
4192 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4193 /* Finally set DECL_VALUE_EXPR to avoid assigning
4194 a stack slot at -O0 for the original var and debug info
4195 uses RESULT location for VAR. */
4196 SET_DECL_VALUE_EXPR (var, result);
4197 DECL_HAS_VALUE_EXPR_P (var) = 1;
4199 data.var = var;
4200 data.result = result;
4201 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4204 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4206 bool
4207 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4208 bool need_copy_ctor, bool need_copy_assignment,
4209 bool need_dtor)
4211 int save_errorcount = errorcount;
4212 tree info, t;
4214 /* Always allocate 3 elements for simplicity. These are the
4215 function decls for the ctor, dtor, and assignment op.
4216 This layout is known to the three lang hooks,
4217 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4218 and cxx_omp_clause_assign_op. */
4219 info = make_tree_vec (3);
4220 CP_OMP_CLAUSE_INFO (c) = info;
4222 if (need_default_ctor || need_copy_ctor)
4224 if (need_default_ctor)
4225 t = get_default_ctor (type);
4226 else
4227 t = get_copy_ctor (type, tf_warning_or_error);
4229 if (t && !trivial_fn_p (t))
4230 TREE_VEC_ELT (info, 0) = t;
4233 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4234 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4236 if (need_copy_assignment)
4238 t = get_copy_assign (type);
4240 if (t && !trivial_fn_p (t))
4241 TREE_VEC_ELT (info, 2) = t;
4244 return errorcount != save_errorcount;
4247 /* Helper function for handle_omp_array_sections. Called recursively
4248 to handle multiple array-section-subscripts. C is the clause,
4249 T current expression (initially OMP_CLAUSE_DECL), which is either
4250 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4251 expression if specified, TREE_VALUE length expression if specified,
4252 TREE_CHAIN is what it has been specified after, or some decl.
4253 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4254 set to true if any of the array-section-subscript could have length
4255 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4256 first array-section-subscript which is known not to have length
4257 of one. Given say:
4258 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4259 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4260 all are or may have length of 1, array-section-subscript [:2] is the
4261 first one knonwn not to have length 1. For array-section-subscript
4262 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4263 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4264 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4265 case though, as some lengths could be zero. */
4267 static tree
4268 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4269 bool &maybe_zero_len, unsigned int &first_non_one)
4271 tree ret, low_bound, length, type;
4272 if (TREE_CODE (t) != TREE_LIST)
4274 if (error_operand_p (t))
4275 return error_mark_node;
4276 if (type_dependent_expression_p (t))
4277 return NULL_TREE;
4278 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4280 if (processing_template_decl)
4281 return NULL_TREE;
4282 if (DECL_P (t))
4283 error_at (OMP_CLAUSE_LOCATION (c),
4284 "%qD is not a variable in %qs clause", t,
4285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4286 else
4287 error_at (OMP_CLAUSE_LOCATION (c),
4288 "%qE is not a variable in %qs clause", t,
4289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4290 return error_mark_node;
4292 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4293 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4295 error_at (OMP_CLAUSE_LOCATION (c),
4296 "%qD is threadprivate variable in %qs clause", t,
4297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4298 return error_mark_node;
4300 t = convert_from_reference (t);
4301 return t;
4304 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4305 maybe_zero_len, first_non_one);
4306 if (ret == error_mark_node || ret == NULL_TREE)
4307 return ret;
4309 type = TREE_TYPE (ret);
4310 low_bound = TREE_PURPOSE (t);
4311 length = TREE_VALUE (t);
4312 if ((low_bound && type_dependent_expression_p (low_bound))
4313 || (length && type_dependent_expression_p (length)))
4314 return NULL_TREE;
4316 if (low_bound == error_mark_node || length == error_mark_node)
4317 return error_mark_node;
4319 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4321 error_at (OMP_CLAUSE_LOCATION (c),
4322 "low bound %qE of array section does not have integral type",
4323 low_bound);
4324 return error_mark_node;
4326 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4328 error_at (OMP_CLAUSE_LOCATION (c),
4329 "length %qE of array section does not have integral type",
4330 length);
4331 return error_mark_node;
4333 if (low_bound)
4334 low_bound = mark_rvalue_use (low_bound);
4335 if (length)
4336 length = mark_rvalue_use (length);
4337 if (low_bound
4338 && TREE_CODE (low_bound) == INTEGER_CST
4339 && TYPE_PRECISION (TREE_TYPE (low_bound))
4340 > TYPE_PRECISION (sizetype))
4341 low_bound = fold_convert (sizetype, low_bound);
4342 if (length
4343 && TREE_CODE (length) == INTEGER_CST
4344 && TYPE_PRECISION (TREE_TYPE (length))
4345 > TYPE_PRECISION (sizetype))
4346 length = fold_convert (sizetype, length);
4347 if (low_bound == NULL_TREE)
4348 low_bound = integer_zero_node;
4350 if (length != NULL_TREE)
4352 if (!integer_nonzerop (length))
4353 maybe_zero_len = true;
4354 if (first_non_one == types.length ()
4355 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4356 first_non_one++;
4358 if (TREE_CODE (type) == ARRAY_TYPE)
4360 if (length == NULL_TREE
4361 && (TYPE_DOMAIN (type) == NULL_TREE
4362 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4364 error_at (OMP_CLAUSE_LOCATION (c),
4365 "for unknown bound array type length expression must "
4366 "be specified");
4367 return error_mark_node;
4369 if (TREE_CODE (low_bound) == INTEGER_CST
4370 && tree_int_cst_sgn (low_bound) == -1)
4372 error_at (OMP_CLAUSE_LOCATION (c),
4373 "negative low bound in array section in %qs clause",
4374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4375 return error_mark_node;
4377 if (length != NULL_TREE
4378 && TREE_CODE (length) == INTEGER_CST
4379 && tree_int_cst_sgn (length) == -1)
4381 error_at (OMP_CLAUSE_LOCATION (c),
4382 "negative length in array section in %qs clause",
4383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4384 return error_mark_node;
4386 if (TYPE_DOMAIN (type)
4387 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4388 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4389 == INTEGER_CST)
4391 tree size = size_binop (PLUS_EXPR,
4392 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4393 size_one_node);
4394 if (TREE_CODE (low_bound) == INTEGER_CST)
4396 if (tree_int_cst_lt (size, low_bound))
4398 error_at (OMP_CLAUSE_LOCATION (c),
4399 "low bound %qE above array section size "
4400 "in %qs clause", low_bound,
4401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4402 return error_mark_node;
4404 if (tree_int_cst_equal (size, low_bound))
4405 maybe_zero_len = true;
4406 else if (length == NULL_TREE
4407 && first_non_one == types.length ()
4408 && tree_int_cst_equal
4409 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4410 low_bound))
4411 first_non_one++;
4413 else if (length == NULL_TREE)
4415 maybe_zero_len = true;
4416 if (first_non_one == types.length ())
4417 first_non_one++;
4419 if (length && TREE_CODE (length) == INTEGER_CST)
4421 if (tree_int_cst_lt (size, length))
4423 error_at (OMP_CLAUSE_LOCATION (c),
4424 "length %qE above array section size "
4425 "in %qs clause", length,
4426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4427 return error_mark_node;
4429 if (TREE_CODE (low_bound) == INTEGER_CST)
4431 tree lbpluslen
4432 = size_binop (PLUS_EXPR,
4433 fold_convert (sizetype, low_bound),
4434 fold_convert (sizetype, length));
4435 if (TREE_CODE (lbpluslen) == INTEGER_CST
4436 && tree_int_cst_lt (size, lbpluslen))
4438 error_at (OMP_CLAUSE_LOCATION (c),
4439 "high bound %qE above array section size "
4440 "in %qs clause", lbpluslen,
4441 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4442 return error_mark_node;
4447 else if (length == NULL_TREE)
4449 maybe_zero_len = true;
4450 if (first_non_one == types.length ())
4451 first_non_one++;
4454 /* For [lb:] we will need to evaluate lb more than once. */
4455 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4457 tree lb = cp_save_expr (low_bound);
4458 if (lb != low_bound)
4460 TREE_PURPOSE (t) = lb;
4461 low_bound = lb;
4465 else if (TREE_CODE (type) == POINTER_TYPE)
4467 if (length == NULL_TREE)
4469 error_at (OMP_CLAUSE_LOCATION (c),
4470 "for pointer type length expression must be specified");
4471 return error_mark_node;
4473 /* If there is a pointer type anywhere but in the very first
4474 array-section-subscript, the array section can't be contiguous. */
4475 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4476 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4478 error_at (OMP_CLAUSE_LOCATION (c),
4479 "array section is not contiguous in %qs clause",
4480 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4481 return error_mark_node;
4484 else
4486 error_at (OMP_CLAUSE_LOCATION (c),
4487 "%qE does not have pointer or array type", ret);
4488 return error_mark_node;
4490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4491 types.safe_push (TREE_TYPE (ret));
4492 /* We will need to evaluate lb more than once. */
4493 tree lb = cp_save_expr (low_bound);
4494 if (lb != low_bound)
4496 TREE_PURPOSE (t) = lb;
4497 low_bound = lb;
4499 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4500 return ret;
4503 /* Handle array sections for clause C. */
4505 static bool
4506 handle_omp_array_sections (tree c)
4508 bool maybe_zero_len = false;
4509 unsigned int first_non_one = 0;
4510 auto_vec<tree> types;
4511 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4512 maybe_zero_len, first_non_one);
4513 if (first == error_mark_node)
4514 return true;
4515 if (first == NULL_TREE)
4516 return false;
4517 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4519 tree t = OMP_CLAUSE_DECL (c);
4520 tree tem = NULL_TREE;
4521 if (processing_template_decl)
4522 return false;
4523 /* Need to evaluate side effects in the length expressions
4524 if any. */
4525 while (TREE_CODE (t) == TREE_LIST)
4527 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4529 if (tem == NULL_TREE)
4530 tem = TREE_VALUE (t);
4531 else
4532 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4533 TREE_VALUE (t), tem);
4535 t = TREE_CHAIN (t);
4537 if (tem)
4538 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4539 OMP_CLAUSE_DECL (c) = first;
4541 else
4543 unsigned int num = types.length (), i;
4544 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4545 tree condition = NULL_TREE;
4547 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4548 maybe_zero_len = true;
4549 if (processing_template_decl && maybe_zero_len)
4550 return false;
4552 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4553 t = TREE_CHAIN (t))
4555 tree low_bound = TREE_PURPOSE (t);
4556 tree length = TREE_VALUE (t);
4558 i--;
4559 if (low_bound
4560 && TREE_CODE (low_bound) == INTEGER_CST
4561 && TYPE_PRECISION (TREE_TYPE (low_bound))
4562 > TYPE_PRECISION (sizetype))
4563 low_bound = fold_convert (sizetype, low_bound);
4564 if (length
4565 && TREE_CODE (length) == INTEGER_CST
4566 && TYPE_PRECISION (TREE_TYPE (length))
4567 > TYPE_PRECISION (sizetype))
4568 length = fold_convert (sizetype, length);
4569 if (low_bound == NULL_TREE)
4570 low_bound = integer_zero_node;
4571 if (!maybe_zero_len && i > first_non_one)
4573 if (integer_nonzerop (low_bound))
4574 goto do_warn_noncontiguous;
4575 if (length != NULL_TREE
4576 && TREE_CODE (length) == INTEGER_CST
4577 && TYPE_DOMAIN (types[i])
4578 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4579 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4580 == INTEGER_CST)
4582 tree size;
4583 size = size_binop (PLUS_EXPR,
4584 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4585 size_one_node);
4586 if (!tree_int_cst_equal (length, size))
4588 do_warn_noncontiguous:
4589 error_at (OMP_CLAUSE_LOCATION (c),
4590 "array section is not contiguous in %qs "
4591 "clause",
4592 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4593 return true;
4596 if (!processing_template_decl
4597 && length != NULL_TREE
4598 && TREE_SIDE_EFFECTS (length))
4600 if (side_effects == NULL_TREE)
4601 side_effects = length;
4602 else
4603 side_effects = build2 (COMPOUND_EXPR,
4604 TREE_TYPE (side_effects),
4605 length, side_effects);
4608 else if (processing_template_decl)
4609 continue;
4610 else
4612 tree l;
4614 if (i > first_non_one && length && integer_nonzerop (length))
4615 continue;
4616 if (length)
4617 l = fold_convert (sizetype, length);
4618 else
4620 l = size_binop (PLUS_EXPR,
4621 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4622 size_one_node);
4623 l = size_binop (MINUS_EXPR, l,
4624 fold_convert (sizetype, low_bound));
4626 if (i > first_non_one)
4628 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4629 size_zero_node);
4630 if (condition == NULL_TREE)
4631 condition = l;
4632 else
4633 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4634 l, condition);
4636 else if (size == NULL_TREE)
4638 size = size_in_bytes (TREE_TYPE (types[i]));
4639 size = size_binop (MULT_EXPR, size, l);
4640 if (condition)
4641 size = fold_build3 (COND_EXPR, sizetype, condition,
4642 size, size_zero_node);
4644 else
4645 size = size_binop (MULT_EXPR, size, l);
4648 if (!processing_template_decl)
4650 if (side_effects)
4651 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4652 OMP_CLAUSE_DECL (c) = first;
4653 OMP_CLAUSE_SIZE (c) = size;
4654 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4655 return false;
4656 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4657 OMP_CLAUSE_MAP);
4658 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4659 if (!cxx_mark_addressable (t))
4660 return false;
4661 OMP_CLAUSE_DECL (c2) = t;
4662 t = build_fold_addr_expr (first);
4663 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4664 ptrdiff_type_node, t);
4665 tree ptr = OMP_CLAUSE_DECL (c2);
4666 ptr = convert_from_reference (ptr);
4667 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4668 ptr = build_fold_addr_expr (ptr);
4669 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4670 ptrdiff_type_node, t,
4671 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4672 ptrdiff_type_node, ptr));
4673 OMP_CLAUSE_SIZE (c2) = t;
4674 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4675 OMP_CLAUSE_CHAIN (c) = c2;
4676 ptr = OMP_CLAUSE_DECL (c2);
4677 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4678 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4680 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4681 OMP_CLAUSE_MAP);
4682 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4683 OMP_CLAUSE_DECL (c3) = ptr;
4684 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4685 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4686 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4687 OMP_CLAUSE_CHAIN (c2) = c3;
4691 return false;
4694 /* Return identifier to look up for omp declare reduction. */
4696 tree
4697 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4699 const char *p = NULL;
4700 const char *m = NULL;
4701 switch (reduction_code)
4703 case PLUS_EXPR:
4704 case MULT_EXPR:
4705 case MINUS_EXPR:
4706 case BIT_AND_EXPR:
4707 case BIT_XOR_EXPR:
4708 case BIT_IOR_EXPR:
4709 case TRUTH_ANDIF_EXPR:
4710 case TRUTH_ORIF_EXPR:
4711 reduction_id = ansi_opname (reduction_code);
4712 break;
4713 case MIN_EXPR:
4714 p = "min";
4715 break;
4716 case MAX_EXPR:
4717 p = "max";
4718 break;
4719 default:
4720 break;
4723 if (p == NULL)
4725 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4726 return error_mark_node;
4727 p = IDENTIFIER_POINTER (reduction_id);
4730 if (type != NULL_TREE)
4731 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4733 const char prefix[] = "omp declare reduction ";
4734 size_t lenp = sizeof (prefix);
4735 if (strncmp (p, prefix, lenp - 1) == 0)
4736 lenp = 1;
4737 size_t len = strlen (p);
4738 size_t lenm = m ? strlen (m) + 1 : 0;
4739 char *name = XALLOCAVEC (char, lenp + len + lenm);
4740 if (lenp > 1)
4741 memcpy (name, prefix, lenp - 1);
4742 memcpy (name + lenp - 1, p, len + 1);
4743 if (m)
4745 name[lenp + len - 1] = '~';
4746 memcpy (name + lenp + len, m, lenm);
4748 return get_identifier (name);
4751 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4752 FUNCTION_DECL or NULL_TREE if not found. */
4754 static tree
4755 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4756 vec<tree> *ambiguousp)
4758 tree orig_id = id;
4759 tree baselink = NULL_TREE;
4760 if (identifier_p (id))
4762 cp_id_kind idk;
4763 bool nonint_cst_expression_p;
4764 const char *error_msg;
4765 id = omp_reduction_id (ERROR_MARK, id, type);
4766 tree decl = lookup_name (id);
4767 if (decl == NULL_TREE)
4768 decl = error_mark_node;
4769 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4770 &nonint_cst_expression_p, false, true, false,
4771 false, &error_msg, loc);
4772 if (idk == CP_ID_KIND_UNQUALIFIED
4773 && identifier_p (id))
4775 vec<tree, va_gc> *args = NULL;
4776 vec_safe_push (args, build_reference_type (type));
4777 id = perform_koenig_lookup (id, args, tf_none);
4780 else if (TREE_CODE (id) == SCOPE_REF)
4781 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4782 omp_reduction_id (ERROR_MARK,
4783 TREE_OPERAND (id, 1),
4784 type),
4785 false, false);
4786 tree fns = id;
4787 if (id && is_overloaded_fn (id))
4788 id = get_fns (id);
4789 for (; id; id = OVL_NEXT (id))
4791 tree fndecl = OVL_CURRENT (id);
4792 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4794 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4795 if (same_type_p (TREE_TYPE (argtype), type))
4796 break;
4799 if (id && BASELINK_P (fns))
4801 if (baselinkp)
4802 *baselinkp = fns;
4803 else
4804 baselink = fns;
4806 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4808 vec<tree> ambiguous = vNULL;
4809 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4810 unsigned int ix;
4811 if (ambiguousp == NULL)
4812 ambiguousp = &ambiguous;
4813 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4815 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4816 baselinkp ? baselinkp : &baselink,
4817 ambiguousp);
4818 if (id == NULL_TREE)
4819 continue;
4820 if (!ambiguousp->is_empty ())
4821 ambiguousp->safe_push (id);
4822 else if (ret != NULL_TREE)
4824 ambiguousp->safe_push (ret);
4825 ambiguousp->safe_push (id);
4826 ret = NULL_TREE;
4828 else
4829 ret = id;
4831 if (ambiguousp != &ambiguous)
4832 return ret;
4833 if (!ambiguous.is_empty ())
4835 const char *str = _("candidates are:");
4836 unsigned int idx;
4837 tree udr;
4838 error_at (loc, "user defined reduction lookup is ambiguous");
4839 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4841 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4842 if (idx == 0)
4843 str = get_spaces (str);
4845 ambiguous.release ();
4846 ret = error_mark_node;
4847 baselink = NULL_TREE;
4849 id = ret;
4851 if (id && baselink)
4852 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4853 id, id, tf_warning_or_error);
4854 return id;
4857 /* Helper function for cp_parser_omp_declare_reduction_exprs
4858 and tsubst_omp_udr.
4859 Remove CLEANUP_STMT for data (omp_priv variable).
4860 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4861 DECL_EXPR. */
4863 tree
4864 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4866 if (TYPE_P (*tp))
4867 *walk_subtrees = 0;
4868 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4869 *tp = CLEANUP_BODY (*tp);
4870 else if (TREE_CODE (*tp) == DECL_EXPR)
4872 tree decl = DECL_EXPR_DECL (*tp);
4873 if (!processing_template_decl
4874 && decl == (tree) data
4875 && DECL_INITIAL (decl)
4876 && DECL_INITIAL (decl) != error_mark_node)
4878 tree list = NULL_TREE;
4879 append_to_statement_list_force (*tp, &list);
4880 tree init_expr = build2 (INIT_EXPR, void_type_node,
4881 decl, DECL_INITIAL (decl));
4882 DECL_INITIAL (decl) = NULL_TREE;
4883 append_to_statement_list_force (init_expr, &list);
4884 *tp = list;
4887 return NULL_TREE;
4890 /* Data passed from cp_check_omp_declare_reduction to
4891 cp_check_omp_declare_reduction_r. */
4893 struct cp_check_omp_declare_reduction_data
4895 location_t loc;
4896 tree stmts[7];
4897 bool combiner_p;
4900 /* Helper function for cp_check_omp_declare_reduction, called via
4901 cp_walk_tree. */
4903 static tree
4904 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4906 struct cp_check_omp_declare_reduction_data *udr_data
4907 = (struct cp_check_omp_declare_reduction_data *) data;
4908 if (SSA_VAR_P (*tp)
4909 && !DECL_ARTIFICIAL (*tp)
4910 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4911 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4913 location_t loc = udr_data->loc;
4914 if (udr_data->combiner_p)
4915 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4916 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4917 *tp);
4918 else
4919 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4920 "to variable %qD which is not %<omp_priv%> nor "
4921 "%<omp_orig%>",
4922 *tp);
4923 return *tp;
4925 return NULL_TREE;
4928 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4930 void
4931 cp_check_omp_declare_reduction (tree udr)
4933 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4934 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4935 type = TREE_TYPE (type);
4936 int i;
4937 location_t loc = DECL_SOURCE_LOCATION (udr);
4939 if (type == error_mark_node)
4940 return;
4941 if (ARITHMETIC_TYPE_P (type))
4943 static enum tree_code predef_codes[]
4944 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4945 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4946 for (i = 0; i < 8; i++)
4948 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4949 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4950 const char *n2 = IDENTIFIER_POINTER (id);
4951 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4952 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4953 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4954 break;
4957 if (i == 8
4958 && TREE_CODE (type) != COMPLEX_EXPR)
4960 const char prefix_minmax[] = "omp declare reduction m";
4961 size_t prefix_size = sizeof (prefix_minmax) - 1;
4962 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4963 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4964 prefix_minmax, prefix_size) == 0
4965 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4966 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4967 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4968 i = 0;
4970 if (i < 8)
4972 error_at (loc, "predeclared arithmetic type %qT in "
4973 "%<#pragma omp declare reduction%>", type);
4974 return;
4977 else if (TREE_CODE (type) == FUNCTION_TYPE
4978 || TREE_CODE (type) == METHOD_TYPE
4979 || TREE_CODE (type) == ARRAY_TYPE)
4981 error_at (loc, "function or array type %qT in "
4982 "%<#pragma omp declare reduction%>", type);
4983 return;
4985 else if (TREE_CODE (type) == REFERENCE_TYPE)
4987 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4988 type);
4989 return;
4991 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4993 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4994 "%<#pragma omp declare reduction%>", type);
4995 return;
4998 tree body = DECL_SAVED_TREE (udr);
4999 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5000 return;
5002 tree_stmt_iterator tsi;
5003 struct cp_check_omp_declare_reduction_data data;
5004 memset (data.stmts, 0, sizeof data.stmts);
5005 for (i = 0, tsi = tsi_start (body);
5006 i < 7 && !tsi_end_p (tsi);
5007 i++, tsi_next (&tsi))
5008 data.stmts[i] = tsi_stmt (tsi);
5009 data.loc = loc;
5010 gcc_assert (tsi_end_p (tsi));
5011 if (i >= 3)
5013 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5014 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5015 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5016 return;
5017 data.combiner_p = true;
5018 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5019 &data, NULL))
5020 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5022 if (i >= 6)
5024 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5025 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5026 data.combiner_p = false;
5027 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5028 &data, NULL)
5029 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5030 cp_check_omp_declare_reduction_r, &data, NULL))
5031 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5032 if (i == 7)
5033 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5037 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5038 an inline call. But, remap
5039 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5040 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5042 static tree
5043 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5044 tree decl, tree placeholder)
5046 copy_body_data id;
5047 hash_map<tree, tree> decl_map;
5049 decl_map.put (omp_decl1, placeholder);
5050 decl_map.put (omp_decl2, decl);
5051 memset (&id, 0, sizeof (id));
5052 id.src_fn = DECL_CONTEXT (omp_decl1);
5053 id.dst_fn = current_function_decl;
5054 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5055 id.decl_map = &decl_map;
5057 id.copy_decl = copy_decl_no_change;
5058 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5059 id.transform_new_cfg = true;
5060 id.transform_return_to_modify = false;
5061 id.transform_lang_insert_block = NULL;
5062 id.eh_lp_nr = 0;
5063 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5064 return stmt;
5067 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5068 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5070 static tree
5071 find_omp_placeholder_r (tree *tp, int *, void *data)
5073 if (*tp == (tree) data)
5074 return *tp;
5075 return NULL_TREE;
5078 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5079 Return true if there is some error and the clause should be removed. */
5081 static bool
5082 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5084 tree t = OMP_CLAUSE_DECL (c);
5085 bool predefined = false;
5086 tree type = TREE_TYPE (t);
5087 if (TREE_CODE (type) == REFERENCE_TYPE)
5088 type = TREE_TYPE (type);
5089 if (type == error_mark_node)
5090 return true;
5091 else if (ARITHMETIC_TYPE_P (type))
5092 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5094 case PLUS_EXPR:
5095 case MULT_EXPR:
5096 case MINUS_EXPR:
5097 predefined = true;
5098 break;
5099 case MIN_EXPR:
5100 case MAX_EXPR:
5101 if (TREE_CODE (type) == COMPLEX_TYPE)
5102 break;
5103 predefined = true;
5104 break;
5105 case BIT_AND_EXPR:
5106 case BIT_IOR_EXPR:
5107 case BIT_XOR_EXPR:
5108 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5109 break;
5110 predefined = true;
5111 break;
5112 case TRUTH_ANDIF_EXPR:
5113 case TRUTH_ORIF_EXPR:
5114 if (FLOAT_TYPE_P (type))
5115 break;
5116 predefined = true;
5117 break;
5118 default:
5119 break;
5121 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5123 error ("%qE has invalid type for %<reduction%>", t);
5124 return true;
5126 else if (!processing_template_decl)
5128 t = require_complete_type (t);
5129 if (t == error_mark_node)
5130 return true;
5131 OMP_CLAUSE_DECL (c) = t;
5134 if (predefined)
5136 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5137 return false;
5139 else if (processing_template_decl)
5140 return false;
5142 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5144 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5145 if (TREE_CODE (type) == REFERENCE_TYPE)
5146 type = TREE_TYPE (type);
5147 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5148 if (id == NULL_TREE)
5149 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5150 NULL_TREE, NULL_TREE);
5151 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5152 if (id)
5154 if (id == error_mark_node)
5155 return true;
5156 id = OVL_CURRENT (id);
5157 mark_used (id);
5158 tree body = DECL_SAVED_TREE (id);
5159 if (!body)
5160 return true;
5161 if (TREE_CODE (body) == STATEMENT_LIST)
5163 tree_stmt_iterator tsi;
5164 tree placeholder = NULL_TREE;
5165 int i;
5166 tree stmts[7];
5167 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5168 atype = TREE_TYPE (atype);
5169 bool need_static_cast = !same_type_p (type, atype);
5170 memset (stmts, 0, sizeof stmts);
5171 for (i = 0, tsi = tsi_start (body);
5172 i < 7 && !tsi_end_p (tsi);
5173 i++, tsi_next (&tsi))
5174 stmts[i] = tsi_stmt (tsi);
5175 gcc_assert (tsi_end_p (tsi));
5177 if (i >= 3)
5179 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5180 && TREE_CODE (stmts[1]) == DECL_EXPR);
5181 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5182 DECL_ARTIFICIAL (placeholder) = 1;
5183 DECL_IGNORED_P (placeholder) = 1;
5184 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5185 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5186 cxx_mark_addressable (placeholder);
5187 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5188 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5189 != REFERENCE_TYPE)
5190 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5191 tree omp_out = placeholder;
5192 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5193 if (need_static_cast)
5195 tree rtype = build_reference_type (atype);
5196 omp_out = build_static_cast (rtype, omp_out,
5197 tf_warning_or_error);
5198 omp_in = build_static_cast (rtype, omp_in,
5199 tf_warning_or_error);
5200 if (omp_out == error_mark_node || omp_in == error_mark_node)
5201 return true;
5202 omp_out = convert_from_reference (omp_out);
5203 omp_in = convert_from_reference (omp_in);
5205 OMP_CLAUSE_REDUCTION_MERGE (c)
5206 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5207 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5209 if (i >= 6)
5211 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5212 && TREE_CODE (stmts[4]) == DECL_EXPR);
5213 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5214 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5215 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5216 cxx_mark_addressable (placeholder);
5217 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5218 tree omp_orig = placeholder;
5219 if (need_static_cast)
5221 if (i == 7)
5223 error_at (OMP_CLAUSE_LOCATION (c),
5224 "user defined reduction with constructor "
5225 "initializer for base class %qT", atype);
5226 return true;
5228 tree rtype = build_reference_type (atype);
5229 omp_priv = build_static_cast (rtype, omp_priv,
5230 tf_warning_or_error);
5231 omp_orig = build_static_cast (rtype, omp_orig,
5232 tf_warning_or_error);
5233 if (omp_priv == error_mark_node
5234 || omp_orig == error_mark_node)
5235 return true;
5236 omp_priv = convert_from_reference (omp_priv);
5237 omp_orig = convert_from_reference (omp_orig);
5239 if (i == 6)
5240 *need_default_ctor = true;
5241 OMP_CLAUSE_REDUCTION_INIT (c)
5242 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5243 DECL_EXPR_DECL (stmts[3]),
5244 omp_priv, omp_orig);
5245 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5246 find_omp_placeholder_r, placeholder, NULL))
5247 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5249 else if (i >= 3)
5251 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5252 *need_default_ctor = true;
5253 else
5255 tree init;
5256 tree v = convert_from_reference (t);
5257 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5258 init = build_constructor (TREE_TYPE (v), NULL);
5259 else
5260 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5261 OMP_CLAUSE_REDUCTION_INIT (c)
5262 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5267 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5268 *need_dtor = true;
5269 else
5271 error ("user defined reduction not found for %qD", t);
5272 return true;
5274 return false;
5277 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5278 Remove any elements from the list that are invalid. */
5280 tree
5281 finish_omp_clauses (tree clauses)
5283 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5284 bitmap_head aligned_head;
5285 tree c, t, *pc;
5286 bool branch_seen = false;
5287 bool copyprivate_seen = false;
5289 bitmap_obstack_initialize (NULL);
5290 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5291 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5292 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5293 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5295 for (pc = &clauses, c = clauses; c ; c = *pc)
5297 bool remove = false;
5299 switch (OMP_CLAUSE_CODE (c))
5301 case OMP_CLAUSE_SHARED:
5302 goto check_dup_generic;
5303 case OMP_CLAUSE_PRIVATE:
5304 goto check_dup_generic;
5305 case OMP_CLAUSE_REDUCTION:
5306 goto check_dup_generic;
5307 case OMP_CLAUSE_COPYPRIVATE:
5308 copyprivate_seen = true;
5309 goto check_dup_generic;
5310 case OMP_CLAUSE_COPYIN:
5311 goto check_dup_generic;
5312 case OMP_CLAUSE_LINEAR:
5313 t = OMP_CLAUSE_DECL (c);
5314 if (!type_dependent_expression_p (t)
5315 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5316 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5318 error ("linear clause applied to non-integral non-pointer "
5319 "variable with %qT type", TREE_TYPE (t));
5320 remove = true;
5321 break;
5323 t = OMP_CLAUSE_LINEAR_STEP (c);
5324 if (t == NULL_TREE)
5325 t = integer_one_node;
5326 if (t == error_mark_node)
5328 remove = true;
5329 break;
5331 else if (!type_dependent_expression_p (t)
5332 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5334 error ("linear step expression must be integral");
5335 remove = true;
5336 break;
5338 else
5340 t = mark_rvalue_use (t);
5341 if (!processing_template_decl)
5343 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5344 t = maybe_constant_value (t);
5345 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5346 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5347 == POINTER_TYPE)
5349 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5350 OMP_CLAUSE_DECL (c), t);
5351 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5352 MINUS_EXPR, sizetype, t,
5353 OMP_CLAUSE_DECL (c));
5354 if (t == error_mark_node)
5356 remove = true;
5357 break;
5360 else
5361 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5363 OMP_CLAUSE_LINEAR_STEP (c) = t;
5365 goto check_dup_generic;
5366 check_dup_generic:
5367 t = OMP_CLAUSE_DECL (c);
5368 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5370 if (processing_template_decl)
5371 break;
5372 if (DECL_P (t))
5373 error ("%qD is not a variable in clause %qs", t,
5374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5375 else
5376 error ("%qE is not a variable in clause %qs", t,
5377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5378 remove = true;
5380 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5381 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5382 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5384 error ("%qD appears more than once in data clauses", t);
5385 remove = true;
5387 else
5388 bitmap_set_bit (&generic_head, DECL_UID (t));
5389 break;
5391 case OMP_CLAUSE_FIRSTPRIVATE:
5392 t = OMP_CLAUSE_DECL (c);
5393 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5395 if (processing_template_decl)
5396 break;
5397 if (DECL_P (t))
5398 error ("%qD is not a variable in clause %<firstprivate%>", t);
5399 else
5400 error ("%qE is not a variable in clause %<firstprivate%>", t);
5401 remove = true;
5403 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5404 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5406 error ("%qD appears more than once in data clauses", t);
5407 remove = true;
5409 else
5410 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5411 break;
5413 case OMP_CLAUSE_LASTPRIVATE:
5414 t = OMP_CLAUSE_DECL (c);
5415 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5417 if (processing_template_decl)
5418 break;
5419 if (DECL_P (t))
5420 error ("%qD is not a variable in clause %<lastprivate%>", t);
5421 else
5422 error ("%qE is not a variable in clause %<lastprivate%>", t);
5423 remove = true;
5425 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5426 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5428 error ("%qD appears more than once in data clauses", t);
5429 remove = true;
5431 else
5432 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5433 break;
5435 case OMP_CLAUSE_IF:
5436 t = OMP_CLAUSE_IF_EXPR (c);
5437 t = maybe_convert_cond (t);
5438 if (t == error_mark_node)
5439 remove = true;
5440 else if (!processing_template_decl)
5441 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5442 OMP_CLAUSE_IF_EXPR (c) = t;
5443 break;
5445 case OMP_CLAUSE_FINAL:
5446 t = OMP_CLAUSE_FINAL_EXPR (c);
5447 t = maybe_convert_cond (t);
5448 if (t == error_mark_node)
5449 remove = true;
5450 else if (!processing_template_decl)
5451 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5452 OMP_CLAUSE_FINAL_EXPR (c) = t;
5453 break;
5455 case OMP_CLAUSE_NUM_THREADS:
5456 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5457 if (t == error_mark_node)
5458 remove = true;
5459 else if (!type_dependent_expression_p (t)
5460 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5462 error ("num_threads expression must be integral");
5463 remove = true;
5465 else
5467 t = mark_rvalue_use (t);
5468 if (!processing_template_decl)
5469 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5470 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5472 break;
5474 case OMP_CLAUSE_SCHEDULE:
5475 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5476 if (t == NULL)
5478 else if (t == error_mark_node)
5479 remove = true;
5480 else if (!type_dependent_expression_p (t)
5481 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5482 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5483 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5485 error ("schedule chunk size expression must be integral");
5486 remove = true;
5488 else
5490 t = mark_rvalue_use (t);
5491 if (!processing_template_decl)
5493 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5494 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5496 t = convert_to_integer (long_integer_type_node, t);
5497 if (t == error_mark_node)
5499 remove = true;
5500 break;
5503 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5505 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5507 break;
5509 case OMP_CLAUSE_SIMDLEN:
5510 case OMP_CLAUSE_SAFELEN:
5511 t = OMP_CLAUSE_OPERAND (c, 0);
5512 if (t == error_mark_node)
5513 remove = true;
5514 else if (!type_dependent_expression_p (t)
5515 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5517 error ("%qs length expression must be integral",
5518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5519 remove = true;
5521 else
5523 t = mark_rvalue_use (t);
5524 t = maybe_constant_value (t);
5525 if (!processing_template_decl)
5527 if (TREE_CODE (t) != INTEGER_CST
5528 || tree_int_cst_sgn (t) != 1)
5530 error ("%qs length expression must be positive constant"
5531 " integer expression",
5532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5533 remove = true;
5536 OMP_CLAUSE_OPERAND (c, 0) = t;
5538 break;
5540 case OMP_CLAUSE_NUM_TEAMS:
5541 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5542 if (t == error_mark_node)
5543 remove = true;
5544 else if (!type_dependent_expression_p (t)
5545 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5547 error ("%<num_teams%> expression must be integral");
5548 remove = true;
5550 else
5552 t = mark_rvalue_use (t);
5553 if (!processing_template_decl)
5554 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5555 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5557 break;
5559 case OMP_CLAUSE_ASYNC:
5560 t = OMP_CLAUSE_ASYNC_EXPR (c);
5561 if (t == error_mark_node)
5562 remove = true;
5563 else if (!type_dependent_expression_p (t)
5564 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5566 error ("%<async%> expression must be integral");
5567 remove = true;
5569 else
5571 t = mark_rvalue_use (t);
5572 if (!processing_template_decl)
5573 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5574 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5576 break;
5578 case OMP_CLAUSE_VECTOR_LENGTH:
5579 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5580 t = maybe_convert_cond (t);
5581 if (t == error_mark_node)
5582 remove = true;
5583 else if (!processing_template_decl)
5584 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5585 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5586 break;
5588 case OMP_CLAUSE_WAIT:
5589 t = OMP_CLAUSE_WAIT_EXPR (c);
5590 if (t == error_mark_node)
5591 remove = true;
5592 else if (!processing_template_decl)
5593 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5594 OMP_CLAUSE_WAIT_EXPR (c) = t;
5595 break;
5597 case OMP_CLAUSE_THREAD_LIMIT:
5598 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5599 if (t == error_mark_node)
5600 remove = true;
5601 else if (!type_dependent_expression_p (t)
5602 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5604 error ("%<thread_limit%> expression must be integral");
5605 remove = true;
5607 else
5609 t = mark_rvalue_use (t);
5610 if (!processing_template_decl)
5611 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5612 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5614 break;
5616 case OMP_CLAUSE_DEVICE:
5617 t = OMP_CLAUSE_DEVICE_ID (c);
5618 if (t == error_mark_node)
5619 remove = true;
5620 else if (!type_dependent_expression_p (t)
5621 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5623 error ("%<device%> id must be integral");
5624 remove = true;
5626 else
5628 t = mark_rvalue_use (t);
5629 if (!processing_template_decl)
5630 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5631 OMP_CLAUSE_DEVICE_ID (c) = t;
5633 break;
5635 case OMP_CLAUSE_DIST_SCHEDULE:
5636 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5637 if (t == NULL)
5639 else if (t == error_mark_node)
5640 remove = true;
5641 else if (!type_dependent_expression_p (t)
5642 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5644 error ("%<dist_schedule%> chunk size expression must be "
5645 "integral");
5646 remove = true;
5648 else
5650 t = mark_rvalue_use (t);
5651 if (!processing_template_decl)
5652 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5653 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5655 break;
5657 case OMP_CLAUSE_ALIGNED:
5658 t = OMP_CLAUSE_DECL (c);
5659 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5661 if (processing_template_decl)
5662 break;
5663 if (DECL_P (t))
5664 error ("%qD is not a variable in %<aligned%> clause", t);
5665 else
5666 error ("%qE is not a variable in %<aligned%> clause", t);
5667 remove = true;
5669 else if (!type_dependent_expression_p (t)
5670 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5671 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5672 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5673 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5674 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5675 != ARRAY_TYPE))))
5677 error_at (OMP_CLAUSE_LOCATION (c),
5678 "%qE in %<aligned%> clause is neither a pointer nor "
5679 "an array nor a reference to pointer or array", t);
5680 remove = true;
5682 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5684 error ("%qD appears more than once in %<aligned%> clauses", t);
5685 remove = true;
5687 else
5688 bitmap_set_bit (&aligned_head, DECL_UID (t));
5689 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5690 if (t == error_mark_node)
5691 remove = true;
5692 else if (t == NULL_TREE)
5693 break;
5694 else if (!type_dependent_expression_p (t)
5695 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5697 error ("%<aligned%> clause alignment expression must "
5698 "be integral");
5699 remove = true;
5701 else
5703 t = mark_rvalue_use (t);
5704 t = maybe_constant_value (t);
5705 if (!processing_template_decl)
5707 if (TREE_CODE (t) != INTEGER_CST
5708 || tree_int_cst_sgn (t) != 1)
5710 error ("%<aligned%> clause alignment expression must be "
5711 "positive constant integer expression");
5712 remove = true;
5715 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5717 break;
5719 case OMP_CLAUSE_DEPEND:
5720 t = OMP_CLAUSE_DECL (c);
5721 if (TREE_CODE (t) == TREE_LIST)
5723 if (handle_omp_array_sections (c))
5724 remove = true;
5725 break;
5727 if (t == error_mark_node)
5728 remove = true;
5729 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5731 if (processing_template_decl)
5732 break;
5733 if (DECL_P (t))
5734 error ("%qD is not a variable in %<depend%> clause", t);
5735 else
5736 error ("%qE is not a variable in %<depend%> clause", t);
5737 remove = true;
5739 else if (!processing_template_decl
5740 && !cxx_mark_addressable (t))
5741 remove = true;
5742 break;
5744 case OMP_CLAUSE_MAP:
5745 case OMP_CLAUSE_TO:
5746 case OMP_CLAUSE_FROM:
5747 case OMP_CLAUSE__CACHE_:
5748 t = OMP_CLAUSE_DECL (c);
5749 if (TREE_CODE (t) == TREE_LIST)
5751 if (handle_omp_array_sections (c))
5752 remove = true;
5753 else
5755 t = OMP_CLAUSE_DECL (c);
5756 if (TREE_CODE (t) != TREE_LIST
5757 && !type_dependent_expression_p (t)
5758 && !cp_omp_mappable_type (TREE_TYPE (t)))
5760 error_at (OMP_CLAUSE_LOCATION (c),
5761 "array section does not have mappable type "
5762 "in %qs clause",
5763 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5764 remove = true;
5767 break;
5769 if (t == error_mark_node)
5770 remove = true;
5771 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5773 if (processing_template_decl)
5774 break;
5775 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5776 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5777 break;
5778 if (DECL_P (t))
5779 error ("%qD is not a variable in %qs clause", t,
5780 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5781 else
5782 error ("%qE is not a variable in %qs clause", t,
5783 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5784 remove = true;
5786 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5788 error ("%qD is threadprivate variable in %qs clause", t,
5789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5790 remove = true;
5792 else if (!processing_template_decl
5793 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5794 && !cxx_mark_addressable (t))
5795 remove = true;
5796 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5797 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5798 && !type_dependent_expression_p (t)
5799 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5800 == REFERENCE_TYPE)
5801 ? TREE_TYPE (TREE_TYPE (t))
5802 : TREE_TYPE (t)))
5804 error_at (OMP_CLAUSE_LOCATION (c),
5805 "%qD does not have a mappable type in %qs clause", t,
5806 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5807 remove = true;
5809 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5811 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5812 error ("%qD appears more than once in motion clauses", t);
5813 else
5814 error ("%qD appears more than once in map clauses", t);
5815 remove = true;
5817 else
5818 bitmap_set_bit (&generic_head, DECL_UID (t));
5819 break;
5821 case OMP_CLAUSE_UNIFORM:
5822 t = OMP_CLAUSE_DECL (c);
5823 if (TREE_CODE (t) != PARM_DECL)
5825 if (processing_template_decl)
5826 break;
5827 if (DECL_P (t))
5828 error ("%qD is not an argument in %<uniform%> clause", t);
5829 else
5830 error ("%qE is not an argument in %<uniform%> clause", t);
5831 remove = true;
5832 break;
5834 goto check_dup_generic;
5836 case OMP_CLAUSE_NOWAIT:
5837 case OMP_CLAUSE_ORDERED:
5838 case OMP_CLAUSE_DEFAULT:
5839 case OMP_CLAUSE_UNTIED:
5840 case OMP_CLAUSE_COLLAPSE:
5841 case OMP_CLAUSE_MERGEABLE:
5842 case OMP_CLAUSE_PARALLEL:
5843 case OMP_CLAUSE_FOR:
5844 case OMP_CLAUSE_SECTIONS:
5845 case OMP_CLAUSE_TASKGROUP:
5846 case OMP_CLAUSE_PROC_BIND:
5847 case OMP_CLAUSE__CILK_FOR_COUNT_:
5848 break;
5850 case OMP_CLAUSE_INBRANCH:
5851 case OMP_CLAUSE_NOTINBRANCH:
5852 if (branch_seen)
5854 error ("%<inbranch%> clause is incompatible with "
5855 "%<notinbranch%>");
5856 remove = true;
5858 branch_seen = true;
5859 break;
5861 default:
5862 gcc_unreachable ();
5865 if (remove)
5866 *pc = OMP_CLAUSE_CHAIN (c);
5867 else
5868 pc = &OMP_CLAUSE_CHAIN (c);
5871 for (pc = &clauses, c = clauses; c ; c = *pc)
5873 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5874 bool remove = false;
5875 bool need_complete_non_reference = false;
5876 bool need_default_ctor = false;
5877 bool need_copy_ctor = false;
5878 bool need_copy_assignment = false;
5879 bool need_implicitly_determined = false;
5880 bool need_dtor = false;
5881 tree type, inner_type;
5883 switch (c_kind)
5885 case OMP_CLAUSE_SHARED:
5886 need_implicitly_determined = true;
5887 break;
5888 case OMP_CLAUSE_PRIVATE:
5889 need_complete_non_reference = true;
5890 need_default_ctor = true;
5891 need_dtor = true;
5892 need_implicitly_determined = true;
5893 break;
5894 case OMP_CLAUSE_FIRSTPRIVATE:
5895 need_complete_non_reference = true;
5896 need_copy_ctor = true;
5897 need_dtor = true;
5898 need_implicitly_determined = true;
5899 break;
5900 case OMP_CLAUSE_LASTPRIVATE:
5901 need_complete_non_reference = true;
5902 need_copy_assignment = true;
5903 need_implicitly_determined = true;
5904 break;
5905 case OMP_CLAUSE_REDUCTION:
5906 need_implicitly_determined = true;
5907 break;
5908 case OMP_CLAUSE_COPYPRIVATE:
5909 need_copy_assignment = true;
5910 break;
5911 case OMP_CLAUSE_COPYIN:
5912 need_copy_assignment = true;
5913 break;
5914 case OMP_CLAUSE_NOWAIT:
5915 if (copyprivate_seen)
5917 error_at (OMP_CLAUSE_LOCATION (c),
5918 "%<nowait%> clause must not be used together "
5919 "with %<copyprivate%>");
5920 *pc = OMP_CLAUSE_CHAIN (c);
5921 continue;
5923 /* FALLTHRU */
5924 default:
5925 pc = &OMP_CLAUSE_CHAIN (c);
5926 continue;
5929 t = OMP_CLAUSE_DECL (c);
5930 if (processing_template_decl
5931 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5933 pc = &OMP_CLAUSE_CHAIN (c);
5934 continue;
5937 switch (c_kind)
5939 case OMP_CLAUSE_LASTPRIVATE:
5940 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5942 need_default_ctor = true;
5943 need_dtor = true;
5945 break;
5947 case OMP_CLAUSE_REDUCTION:
5948 if (finish_omp_reduction_clause (c, &need_default_ctor,
5949 &need_dtor))
5950 remove = true;
5951 else
5952 t = OMP_CLAUSE_DECL (c);
5953 break;
5955 case OMP_CLAUSE_COPYIN:
5956 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
5958 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5959 remove = true;
5961 break;
5963 default:
5964 break;
5967 if (need_complete_non_reference || need_copy_assignment)
5969 t = require_complete_type (t);
5970 if (t == error_mark_node)
5971 remove = true;
5972 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5973 && need_complete_non_reference)
5975 error ("%qE has reference type for %qs", t,
5976 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5977 remove = true;
5980 if (need_implicitly_determined)
5982 const char *share_name = NULL;
5984 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5985 share_name = "threadprivate";
5986 else switch (cxx_omp_predetermined_sharing (t))
5988 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5989 break;
5990 case OMP_CLAUSE_DEFAULT_SHARED:
5991 /* const vars may be specified in firstprivate clause. */
5992 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5993 && cxx_omp_const_qual_no_mutable (t))
5994 break;
5995 share_name = "shared";
5996 break;
5997 case OMP_CLAUSE_DEFAULT_PRIVATE:
5998 share_name = "private";
5999 break;
6000 default:
6001 gcc_unreachable ();
6003 if (share_name)
6005 error ("%qE is predetermined %qs for %qs",
6006 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6007 remove = true;
6011 /* We're interested in the base element, not arrays. */
6012 inner_type = type = TREE_TYPE (t);
6013 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6014 inner_type = TREE_TYPE (inner_type);
6016 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6017 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6018 inner_type = TREE_TYPE (inner_type);
6020 /* Check for special function availability by building a call to one.
6021 Save the results, because later we won't be in the right context
6022 for making these queries. */
6023 if (CLASS_TYPE_P (inner_type)
6024 && COMPLETE_TYPE_P (inner_type)
6025 && (need_default_ctor || need_copy_ctor
6026 || need_copy_assignment || need_dtor)
6027 && !type_dependent_expression_p (t)
6028 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6029 need_copy_ctor, need_copy_assignment,
6030 need_dtor))
6031 remove = true;
6033 if (remove)
6034 *pc = OMP_CLAUSE_CHAIN (c);
6035 else
6036 pc = &OMP_CLAUSE_CHAIN (c);
6039 bitmap_obstack_release (NULL);
6040 return clauses;
6043 /* For all variables in the tree_list VARS, mark them as thread local. */
6045 void
6046 finish_omp_threadprivate (tree vars)
6048 tree t;
6050 /* Mark every variable in VARS to be assigned thread local storage. */
6051 for (t = vars; t; t = TREE_CHAIN (t))
6053 tree v = TREE_PURPOSE (t);
6055 if (error_operand_p (v))
6057 else if (!VAR_P (v))
6058 error ("%<threadprivate%> %qD is not file, namespace "
6059 "or block scope variable", v);
6060 /* If V had already been marked threadprivate, it doesn't matter
6061 whether it had been used prior to this point. */
6062 else if (TREE_USED (v)
6063 && (DECL_LANG_SPECIFIC (v) == NULL
6064 || !CP_DECL_THREADPRIVATE_P (v)))
6065 error ("%qE declared %<threadprivate%> after first use", v);
6066 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6067 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6068 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6069 error ("%<threadprivate%> %qE has incomplete type", v);
6070 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6071 && CP_DECL_CONTEXT (v) != current_class_type)
6072 error ("%<threadprivate%> %qE directive not "
6073 "in %qT definition", v, CP_DECL_CONTEXT (v));
6074 else
6076 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6077 if (DECL_LANG_SPECIFIC (v) == NULL)
6079 retrofit_lang_decl (v);
6081 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6082 after the allocation of the lang_decl structure. */
6083 if (DECL_DISCRIMINATOR_P (v))
6084 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6087 if (! CP_DECL_THREAD_LOCAL_P (v))
6089 CP_DECL_THREAD_LOCAL_P (v) = true;
6090 set_decl_tls_model (v, decl_default_tls_model (v));
6091 /* If rtl has been already set for this var, call
6092 make_decl_rtl once again, so that encode_section_info
6093 has a chance to look at the new decl flags. */
6094 if (DECL_RTL_SET_P (v))
6095 make_decl_rtl (v);
6097 CP_DECL_THREADPRIVATE_P (v) = 1;
6102 /* Build an OpenMP structured block. */
6104 tree
6105 begin_omp_structured_block (void)
6107 return do_pushlevel (sk_omp);
6110 tree
6111 finish_omp_structured_block (tree block)
6113 return do_poplevel (block);
6116 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6117 statement. LOC is the location of the OACC_DATA. */
6119 tree
6120 finish_oacc_data (tree clauses, tree block)
6122 tree stmt;
6124 block = finish_omp_structured_block (block);
6126 stmt = make_node (OACC_DATA);
6127 TREE_TYPE (stmt) = void_type_node;
6128 OACC_DATA_CLAUSES (stmt) = clauses;
6129 OACC_DATA_BODY (stmt) = block;
6131 return add_stmt (stmt);
6134 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6135 statement. LOC is the location of the OACC_KERNELS. */
6137 tree
6138 finish_oacc_kernels (tree clauses, tree block)
6140 tree stmt;
6142 block = finish_omp_structured_block (block);
6144 stmt = make_node (OACC_KERNELS);
6145 TREE_TYPE (stmt) = void_type_node;
6146 OACC_KERNELS_CLAUSES (stmt) = clauses;
6147 OACC_KERNELS_BODY (stmt) = block;
6149 return add_stmt (stmt);
6152 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6153 statement. LOC is the location of the OACC_PARALLEL. */
6155 tree
6156 finish_oacc_parallel (tree clauses, tree block)
6158 tree stmt;
6160 block = finish_omp_structured_block (block);
6162 stmt = make_node (OACC_PARALLEL);
6163 TREE_TYPE (stmt) = void_type_node;
6164 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6165 OACC_PARALLEL_BODY (stmt) = block;
6167 return add_stmt (stmt);
6170 /* Similarly, except force the retention of the BLOCK. */
6172 tree
6173 begin_omp_parallel (void)
6175 keep_next_level (true);
6176 return begin_omp_structured_block ();
6179 tree
6180 finish_omp_parallel (tree clauses, tree body)
6182 tree stmt;
6184 body = finish_omp_structured_block (body);
6186 stmt = make_node (OMP_PARALLEL);
6187 TREE_TYPE (stmt) = void_type_node;
6188 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6189 OMP_PARALLEL_BODY (stmt) = body;
6191 return add_stmt (stmt);
6194 tree
6195 begin_omp_task (void)
6197 keep_next_level (true);
6198 return begin_omp_structured_block ();
6201 tree
6202 finish_omp_task (tree clauses, tree body)
6204 tree stmt;
6206 body = finish_omp_structured_block (body);
6208 stmt = make_node (OMP_TASK);
6209 TREE_TYPE (stmt) = void_type_node;
6210 OMP_TASK_CLAUSES (stmt) = clauses;
6211 OMP_TASK_BODY (stmt) = body;
6213 return add_stmt (stmt);
6216 /* Helper function for finish_omp_for. Convert Ith random access iterator
6217 into integral iterator. Return FALSE if successful. */
6219 static bool
6220 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6221 tree condv, tree incrv, tree *body,
6222 tree *pre_body, tree clauses, tree *lastp)
6224 tree diff, iter_init, iter_incr = NULL, last;
6225 tree incr_var = NULL, orig_pre_body, orig_body, c;
6226 tree decl = TREE_VEC_ELT (declv, i);
6227 tree init = TREE_VEC_ELT (initv, i);
6228 tree cond = TREE_VEC_ELT (condv, i);
6229 tree incr = TREE_VEC_ELT (incrv, i);
6230 tree iter = decl;
6231 location_t elocus = locus;
6233 if (init && EXPR_HAS_LOCATION (init))
6234 elocus = EXPR_LOCATION (init);
6236 switch (TREE_CODE (cond))
6238 case GT_EXPR:
6239 case GE_EXPR:
6240 case LT_EXPR:
6241 case LE_EXPR:
6242 case NE_EXPR:
6243 if (TREE_OPERAND (cond, 1) == iter)
6244 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6245 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6246 if (TREE_OPERAND (cond, 0) != iter)
6247 cond = error_mark_node;
6248 else
6250 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6251 TREE_CODE (cond),
6252 iter, ERROR_MARK,
6253 TREE_OPERAND (cond, 1), ERROR_MARK,
6254 NULL, tf_warning_or_error);
6255 if (error_operand_p (tem))
6256 return true;
6258 break;
6259 default:
6260 cond = error_mark_node;
6261 break;
6263 if (cond == error_mark_node)
6265 error_at (elocus, "invalid controlling predicate");
6266 return true;
6268 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6269 ERROR_MARK, iter, ERROR_MARK, NULL,
6270 tf_warning_or_error);
6271 if (error_operand_p (diff))
6272 return true;
6273 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6275 error_at (elocus, "difference between %qE and %qD does not have integer type",
6276 TREE_OPERAND (cond, 1), iter);
6277 return true;
6280 switch (TREE_CODE (incr))
6282 case PREINCREMENT_EXPR:
6283 case PREDECREMENT_EXPR:
6284 case POSTINCREMENT_EXPR:
6285 case POSTDECREMENT_EXPR:
6286 if (TREE_OPERAND (incr, 0) != iter)
6288 incr = error_mark_node;
6289 break;
6291 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6292 TREE_CODE (incr), iter,
6293 tf_warning_or_error);
6294 if (error_operand_p (iter_incr))
6295 return true;
6296 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6297 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6298 incr = integer_one_node;
6299 else
6300 incr = integer_minus_one_node;
6301 break;
6302 case MODIFY_EXPR:
6303 if (TREE_OPERAND (incr, 0) != iter)
6304 incr = error_mark_node;
6305 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6306 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6308 tree rhs = TREE_OPERAND (incr, 1);
6309 if (TREE_OPERAND (rhs, 0) == iter)
6311 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6312 != INTEGER_TYPE)
6313 incr = error_mark_node;
6314 else
6316 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6317 iter, TREE_CODE (rhs),
6318 TREE_OPERAND (rhs, 1),
6319 tf_warning_or_error);
6320 if (error_operand_p (iter_incr))
6321 return true;
6322 incr = TREE_OPERAND (rhs, 1);
6323 incr = cp_convert (TREE_TYPE (diff), incr,
6324 tf_warning_or_error);
6325 if (TREE_CODE (rhs) == MINUS_EXPR)
6327 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6328 incr = fold_if_not_in_template (incr);
6330 if (TREE_CODE (incr) != INTEGER_CST
6331 && (TREE_CODE (incr) != NOP_EXPR
6332 || (TREE_CODE (TREE_OPERAND (incr, 0))
6333 != INTEGER_CST)))
6334 iter_incr = NULL;
6337 else if (TREE_OPERAND (rhs, 1) == iter)
6339 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6340 || TREE_CODE (rhs) != PLUS_EXPR)
6341 incr = error_mark_node;
6342 else
6344 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6345 PLUS_EXPR,
6346 TREE_OPERAND (rhs, 0),
6347 ERROR_MARK, iter,
6348 ERROR_MARK, NULL,
6349 tf_warning_or_error);
6350 if (error_operand_p (iter_incr))
6351 return true;
6352 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6353 iter, NOP_EXPR,
6354 iter_incr,
6355 tf_warning_or_error);
6356 if (error_operand_p (iter_incr))
6357 return true;
6358 incr = TREE_OPERAND (rhs, 0);
6359 iter_incr = NULL;
6362 else
6363 incr = error_mark_node;
6365 else
6366 incr = error_mark_node;
6367 break;
6368 default:
6369 incr = error_mark_node;
6370 break;
6373 if (incr == error_mark_node)
6375 error_at (elocus, "invalid increment expression");
6376 return true;
6379 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6380 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6381 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6382 && OMP_CLAUSE_DECL (c) == iter)
6383 break;
6385 decl = create_temporary_var (TREE_TYPE (diff));
6386 pushdecl (decl);
6387 add_decl_expr (decl);
6388 last = create_temporary_var (TREE_TYPE (diff));
6389 pushdecl (last);
6390 add_decl_expr (last);
6391 if (c && iter_incr == NULL)
6393 incr_var = create_temporary_var (TREE_TYPE (diff));
6394 pushdecl (incr_var);
6395 add_decl_expr (incr_var);
6397 gcc_assert (stmts_are_full_exprs_p ());
6399 orig_pre_body = *pre_body;
6400 *pre_body = push_stmt_list ();
6401 if (orig_pre_body)
6402 add_stmt (orig_pre_body);
6403 if (init != NULL)
6404 finish_expr_stmt (build_x_modify_expr (elocus,
6405 iter, NOP_EXPR, init,
6406 tf_warning_or_error));
6407 init = build_int_cst (TREE_TYPE (diff), 0);
6408 if (c && iter_incr == NULL)
6410 finish_expr_stmt (build_x_modify_expr (elocus,
6411 incr_var, NOP_EXPR,
6412 incr, tf_warning_or_error));
6413 incr = incr_var;
6414 iter_incr = build_x_modify_expr (elocus,
6415 iter, PLUS_EXPR, incr,
6416 tf_warning_or_error);
6418 finish_expr_stmt (build_x_modify_expr (elocus,
6419 last, NOP_EXPR, init,
6420 tf_warning_or_error));
6421 *pre_body = pop_stmt_list (*pre_body);
6423 cond = cp_build_binary_op (elocus,
6424 TREE_CODE (cond), decl, diff,
6425 tf_warning_or_error);
6426 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6427 elocus, incr, NULL_TREE);
6429 orig_body = *body;
6430 *body = push_stmt_list ();
6431 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6432 iter_init = build_x_modify_expr (elocus,
6433 iter, PLUS_EXPR, iter_init,
6434 tf_warning_or_error);
6435 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6436 finish_expr_stmt (iter_init);
6437 finish_expr_stmt (build_x_modify_expr (elocus,
6438 last, NOP_EXPR, decl,
6439 tf_warning_or_error));
6440 add_stmt (orig_body);
6441 *body = pop_stmt_list (*body);
6443 if (c)
6445 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6446 finish_expr_stmt (iter_incr);
6447 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6448 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6451 TREE_VEC_ELT (declv, i) = decl;
6452 TREE_VEC_ELT (initv, i) = init;
6453 TREE_VEC_ELT (condv, i) = cond;
6454 TREE_VEC_ELT (incrv, i) = incr;
6455 *lastp = last;
6457 return false;
6460 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6461 are directly for their associated operands in the statement. DECL
6462 and INIT are a combo; if DECL is NULL then INIT ought to be a
6463 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6464 optional statements that need to go before the loop into its
6465 sk_omp scope. */
6467 tree
6468 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6469 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6471 tree omp_for = NULL, orig_incr = NULL;
6472 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6473 tree last = NULL_TREE;
6474 location_t elocus;
6475 int i;
6477 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6478 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6479 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6480 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6482 decl = TREE_VEC_ELT (declv, i);
6483 init = TREE_VEC_ELT (initv, i);
6484 cond = TREE_VEC_ELT (condv, i);
6485 incr = TREE_VEC_ELT (incrv, i);
6486 elocus = locus;
6488 if (decl == NULL)
6490 if (init != NULL)
6491 switch (TREE_CODE (init))
6493 case MODIFY_EXPR:
6494 decl = TREE_OPERAND (init, 0);
6495 init = TREE_OPERAND (init, 1);
6496 break;
6497 case MODOP_EXPR:
6498 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6500 decl = TREE_OPERAND (init, 0);
6501 init = TREE_OPERAND (init, 2);
6503 break;
6504 default:
6505 break;
6508 if (decl == NULL)
6510 error_at (locus,
6511 "expected iteration declaration or initialization");
6512 return NULL;
6516 if (init && EXPR_HAS_LOCATION (init))
6517 elocus = EXPR_LOCATION (init);
6519 if (cond == NULL)
6521 error_at (elocus, "missing controlling predicate");
6522 return NULL;
6525 if (incr == NULL)
6527 error_at (elocus, "missing increment expression");
6528 return NULL;
6531 TREE_VEC_ELT (declv, i) = decl;
6532 TREE_VEC_ELT (initv, i) = init;
6535 if (dependent_omp_for_p (declv, initv, condv, incrv))
6537 tree stmt;
6539 stmt = make_node (code);
6541 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6543 /* This is really just a place-holder. We'll be decomposing this
6544 again and going through the cp_build_modify_expr path below when
6545 we instantiate the thing. */
6546 TREE_VEC_ELT (initv, i)
6547 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6548 TREE_VEC_ELT (initv, i));
6551 TREE_TYPE (stmt) = void_type_node;
6552 OMP_FOR_INIT (stmt) = initv;
6553 OMP_FOR_COND (stmt) = condv;
6554 OMP_FOR_INCR (stmt) = incrv;
6555 OMP_FOR_BODY (stmt) = body;
6556 OMP_FOR_PRE_BODY (stmt) = pre_body;
6557 OMP_FOR_CLAUSES (stmt) = clauses;
6559 SET_EXPR_LOCATION (stmt, locus);
6560 return add_stmt (stmt);
6563 if (processing_template_decl)
6564 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6566 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6568 decl = TREE_VEC_ELT (declv, i);
6569 init = TREE_VEC_ELT (initv, i);
6570 cond = TREE_VEC_ELT (condv, i);
6571 incr = TREE_VEC_ELT (incrv, i);
6572 if (orig_incr)
6573 TREE_VEC_ELT (orig_incr, i) = incr;
6574 elocus = locus;
6576 if (init && EXPR_HAS_LOCATION (init))
6577 elocus = EXPR_LOCATION (init);
6579 if (!DECL_P (decl))
6581 error_at (elocus, "expected iteration declaration or initialization");
6582 return NULL;
6585 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6587 if (orig_incr)
6588 TREE_VEC_ELT (orig_incr, i) = incr;
6589 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6590 TREE_CODE (TREE_OPERAND (incr, 1)),
6591 TREE_OPERAND (incr, 2),
6592 tf_warning_or_error);
6595 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6597 if (code == OMP_SIMD)
6599 error_at (elocus, "%<#pragma omp simd%> used with class "
6600 "iteration variable %qE", decl);
6601 return NULL;
6603 if (code == CILK_FOR && i == 0)
6604 orig_decl = decl;
6605 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6606 incrv, &body, &pre_body,
6607 clauses, &last))
6608 return NULL;
6609 continue;
6612 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6613 && !TYPE_PTR_P (TREE_TYPE (decl)))
6615 error_at (elocus, "invalid type for iteration variable %qE", decl);
6616 return NULL;
6619 if (!processing_template_decl)
6621 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6622 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6624 else
6625 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6626 if (cond
6627 && TREE_SIDE_EFFECTS (cond)
6628 && COMPARISON_CLASS_P (cond)
6629 && !processing_template_decl)
6631 tree t = TREE_OPERAND (cond, 0);
6632 if (TREE_SIDE_EFFECTS (t)
6633 && t != decl
6634 && (TREE_CODE (t) != NOP_EXPR
6635 || TREE_OPERAND (t, 0) != decl))
6636 TREE_OPERAND (cond, 0)
6637 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6639 t = TREE_OPERAND (cond, 1);
6640 if (TREE_SIDE_EFFECTS (t)
6641 && t != decl
6642 && (TREE_CODE (t) != NOP_EXPR
6643 || TREE_OPERAND (t, 0) != decl))
6644 TREE_OPERAND (cond, 1)
6645 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6647 if (decl == error_mark_node || init == error_mark_node)
6648 return NULL;
6650 TREE_VEC_ELT (declv, i) = decl;
6651 TREE_VEC_ELT (initv, i) = init;
6652 TREE_VEC_ELT (condv, i) = cond;
6653 TREE_VEC_ELT (incrv, i) = incr;
6654 i++;
6657 if (IS_EMPTY_STMT (pre_body))
6658 pre_body = NULL;
6660 if (code == CILK_FOR && !processing_template_decl)
6661 block = push_stmt_list ();
6663 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6664 body, pre_body);
6666 if (omp_for == NULL)
6668 if (block)
6669 pop_stmt_list (block);
6670 return NULL;
6673 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6675 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6676 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6678 if (TREE_CODE (incr) != MODIFY_EXPR)
6679 continue;
6681 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6682 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6683 && !processing_template_decl)
6685 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6686 if (TREE_SIDE_EFFECTS (t)
6687 && t != decl
6688 && (TREE_CODE (t) != NOP_EXPR
6689 || TREE_OPERAND (t, 0) != decl))
6690 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6691 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6693 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6694 if (TREE_SIDE_EFFECTS (t)
6695 && t != decl
6696 && (TREE_CODE (t) != NOP_EXPR
6697 || TREE_OPERAND (t, 0) != decl))
6698 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6699 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6702 if (orig_incr)
6703 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6705 OMP_FOR_CLAUSES (omp_for) = clauses;
6707 if (block)
6709 tree omp_par = make_node (OMP_PARALLEL);
6710 TREE_TYPE (omp_par) = void_type_node;
6711 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6712 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6713 TREE_SIDE_EFFECTS (bind) = 1;
6714 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6715 OMP_PARALLEL_BODY (omp_par) = bind;
6716 if (OMP_FOR_PRE_BODY (omp_for))
6718 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6719 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6721 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6722 decl = TREE_OPERAND (init, 0);
6723 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6724 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6725 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6726 clauses = OMP_FOR_CLAUSES (omp_for);
6727 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6728 for (pc = &clauses; *pc; )
6729 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6731 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6732 OMP_FOR_CLAUSES (omp_for) = *pc;
6733 *pc = OMP_CLAUSE_CHAIN (*pc);
6734 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6736 else
6738 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6739 pc = &OMP_CLAUSE_CHAIN (*pc);
6741 if (TREE_CODE (t) != INTEGER_CST)
6743 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6744 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6745 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6746 OMP_CLAUSE_CHAIN (c) = clauses;
6747 clauses = c;
6749 if (TREE_CODE (incr) == MODIFY_EXPR)
6751 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6752 if (TREE_CODE (t) != INTEGER_CST)
6754 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6755 = get_temp_regvar (TREE_TYPE (t), t);
6756 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6757 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6758 OMP_CLAUSE_CHAIN (c) = clauses;
6759 clauses = c;
6762 t = TREE_OPERAND (init, 1);
6763 if (TREE_CODE (t) != INTEGER_CST)
6765 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6766 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6767 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6768 OMP_CLAUSE_CHAIN (c) = clauses;
6769 clauses = c;
6771 if (orig_decl && orig_decl != decl)
6773 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6774 OMP_CLAUSE_DECL (c) = orig_decl;
6775 OMP_CLAUSE_CHAIN (c) = clauses;
6776 clauses = c;
6778 if (last)
6780 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6781 OMP_CLAUSE_DECL (c) = last;
6782 OMP_CLAUSE_CHAIN (c) = clauses;
6783 clauses = c;
6785 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6786 OMP_CLAUSE_DECL (c) = decl;
6787 OMP_CLAUSE_CHAIN (c) = clauses;
6788 clauses = c;
6789 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6790 OMP_CLAUSE_OPERAND (c, 0)
6791 = cilk_for_number_of_iterations (omp_for);
6792 OMP_CLAUSE_CHAIN (c) = clauses;
6793 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6794 add_stmt (omp_par);
6795 return omp_par;
6797 else if (code == CILK_FOR && processing_template_decl)
6799 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6800 if (orig_decl && orig_decl != decl)
6802 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6803 OMP_CLAUSE_DECL (c) = orig_decl;
6804 OMP_CLAUSE_CHAIN (c) = clauses;
6805 clauses = c;
6807 if (last)
6809 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6810 OMP_CLAUSE_DECL (c) = last;
6811 OMP_CLAUSE_CHAIN (c) = clauses;
6812 clauses = c;
6814 OMP_FOR_CLAUSES (omp_for) = clauses;
6816 return omp_for;
6819 void
6820 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6821 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6823 tree orig_lhs;
6824 tree orig_rhs;
6825 tree orig_v;
6826 tree orig_lhs1;
6827 tree orig_rhs1;
6828 bool dependent_p;
6829 tree stmt;
6831 orig_lhs = lhs;
6832 orig_rhs = rhs;
6833 orig_v = v;
6834 orig_lhs1 = lhs1;
6835 orig_rhs1 = rhs1;
6836 dependent_p = false;
6837 stmt = NULL_TREE;
6839 /* Even in a template, we can detect invalid uses of the atomic
6840 pragma if neither LHS nor RHS is type-dependent. */
6841 if (processing_template_decl)
6843 dependent_p = (type_dependent_expression_p (lhs)
6844 || (rhs && type_dependent_expression_p (rhs))
6845 || (v && type_dependent_expression_p (v))
6846 || (lhs1 && type_dependent_expression_p (lhs1))
6847 || (rhs1 && type_dependent_expression_p (rhs1)));
6848 if (!dependent_p)
6850 lhs = build_non_dependent_expr (lhs);
6851 if (rhs)
6852 rhs = build_non_dependent_expr (rhs);
6853 if (v)
6854 v = build_non_dependent_expr (v);
6855 if (lhs1)
6856 lhs1 = build_non_dependent_expr (lhs1);
6857 if (rhs1)
6858 rhs1 = build_non_dependent_expr (rhs1);
6861 if (!dependent_p)
6863 bool swapped = false;
6864 if (rhs1 && cp_tree_equal (lhs, rhs))
6866 std::swap (rhs, rhs1);
6867 swapped = !commutative_tree_code (opcode);
6869 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6871 if (code == OMP_ATOMIC)
6872 error ("%<#pragma omp atomic update%> uses two different "
6873 "expressions for memory");
6874 else
6875 error ("%<#pragma omp atomic capture%> uses two different "
6876 "expressions for memory");
6877 return;
6879 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6881 if (code == OMP_ATOMIC)
6882 error ("%<#pragma omp atomic update%> uses two different "
6883 "expressions for memory");
6884 else
6885 error ("%<#pragma omp atomic capture%> uses two different "
6886 "expressions for memory");
6887 return;
6889 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6890 v, lhs1, rhs1, swapped, seq_cst);
6891 if (stmt == error_mark_node)
6892 return;
6894 if (processing_template_decl)
6896 if (code == OMP_ATOMIC_READ)
6898 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6899 OMP_ATOMIC_READ, orig_lhs);
6900 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6901 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6903 else
6905 if (opcode == NOP_EXPR)
6906 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6907 else
6908 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6909 if (orig_rhs1)
6910 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6911 COMPOUND_EXPR, orig_rhs1, stmt);
6912 if (code != OMP_ATOMIC)
6914 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6915 code, orig_lhs1, stmt);
6916 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6917 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6920 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6921 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6923 finish_expr_stmt (stmt);
6926 void
6927 finish_omp_barrier (void)
6929 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6930 vec<tree, va_gc> *vec = make_tree_vector ();
6931 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6932 release_tree_vector (vec);
6933 finish_expr_stmt (stmt);
6936 void
6937 finish_omp_flush (void)
6939 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6940 vec<tree, va_gc> *vec = make_tree_vector ();
6941 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6942 release_tree_vector (vec);
6943 finish_expr_stmt (stmt);
6946 void
6947 finish_omp_taskwait (void)
6949 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6950 vec<tree, va_gc> *vec = make_tree_vector ();
6951 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6952 release_tree_vector (vec);
6953 finish_expr_stmt (stmt);
6956 void
6957 finish_omp_taskyield (void)
6959 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6960 vec<tree, va_gc> *vec = make_tree_vector ();
6961 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6962 release_tree_vector (vec);
6963 finish_expr_stmt (stmt);
6966 void
6967 finish_omp_cancel (tree clauses)
6969 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6970 int mask = 0;
6971 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6972 mask = 1;
6973 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6974 mask = 2;
6975 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6976 mask = 4;
6977 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6978 mask = 8;
6979 else
6981 error ("%<#pragma omp cancel must specify one of "
6982 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6983 return;
6985 vec<tree, va_gc> *vec = make_tree_vector ();
6986 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6987 if (ifc != NULL_TREE)
6989 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6990 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6991 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6992 build_zero_cst (type));
6994 else
6995 ifc = boolean_true_node;
6996 vec->quick_push (build_int_cst (integer_type_node, mask));
6997 vec->quick_push (ifc);
6998 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6999 release_tree_vector (vec);
7000 finish_expr_stmt (stmt);
7003 void
7004 finish_omp_cancellation_point (tree clauses)
7006 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7007 int mask = 0;
7008 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7009 mask = 1;
7010 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7011 mask = 2;
7012 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7013 mask = 4;
7014 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7015 mask = 8;
7016 else
7018 error ("%<#pragma omp cancellation point must specify one of "
7019 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7020 return;
7022 vec<tree, va_gc> *vec
7023 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7024 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7025 release_tree_vector (vec);
7026 finish_expr_stmt (stmt);
7029 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7030 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7031 should create an extra compound stmt. */
7033 tree
7034 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7036 tree r;
7038 if (pcompound)
7039 *pcompound = begin_compound_stmt (0);
7041 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7043 /* Only add the statement to the function if support enabled. */
7044 if (flag_tm)
7045 add_stmt (r);
7046 else
7047 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7048 ? G_("%<__transaction_relaxed%> without "
7049 "transactional memory support enabled")
7050 : G_("%<__transaction_atomic%> without "
7051 "transactional memory support enabled")));
7053 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7054 TREE_SIDE_EFFECTS (r) = 1;
7055 return r;
7058 /* End a __transaction_atomic or __transaction_relaxed statement.
7059 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7060 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7061 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7063 void
7064 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7066 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7067 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7068 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7069 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7071 /* noexcept specifications are not allowed for function transactions. */
7072 gcc_assert (!(noex && compound_stmt));
7073 if (noex)
7075 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7076 noex);
7077 /* This may not be true when the STATEMENT_LIST is empty. */
7078 if (EXPR_P (body))
7079 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7080 TREE_SIDE_EFFECTS (body) = 1;
7081 TRANSACTION_EXPR_BODY (stmt) = body;
7084 if (compound_stmt)
7085 finish_compound_stmt (compound_stmt);
7088 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7089 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7090 condition. */
7092 tree
7093 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7095 tree ret;
7096 if (noex)
7098 expr = build_must_not_throw_expr (expr, noex);
7099 if (EXPR_P (expr))
7100 SET_EXPR_LOCATION (expr, loc);
7101 TREE_SIDE_EFFECTS (expr) = 1;
7103 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7104 if (flags & TM_STMT_ATTR_RELAXED)
7105 TRANSACTION_EXPR_RELAXED (ret) = 1;
7106 TREE_SIDE_EFFECTS (ret) = 1;
7107 SET_EXPR_LOCATION (ret, loc);
7108 return ret;
7111 void
7112 init_cp_semantics (void)
7116 /* Build a STATIC_ASSERT for a static assertion with the condition
7117 CONDITION and the message text MESSAGE. LOCATION is the location
7118 of the static assertion in the source code. When MEMBER_P, this
7119 static assertion is a member of a class. */
7120 void
7121 finish_static_assert (tree condition, tree message, location_t location,
7122 bool member_p)
7124 if (message == NULL_TREE
7125 || message == error_mark_node
7126 || condition == NULL_TREE
7127 || condition == error_mark_node)
7128 return;
7130 if (check_for_bare_parameter_packs (condition))
7131 condition = error_mark_node;
7133 if (type_dependent_expression_p (condition)
7134 || value_dependent_expression_p (condition))
7136 /* We're in a template; build a STATIC_ASSERT and put it in
7137 the right place. */
7138 tree assertion;
7140 assertion = make_node (STATIC_ASSERT);
7141 STATIC_ASSERT_CONDITION (assertion) = condition;
7142 STATIC_ASSERT_MESSAGE (assertion) = message;
7143 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7145 if (member_p)
7146 maybe_add_class_template_decl_list (current_class_type,
7147 assertion,
7148 /*friend_p=*/0);
7149 else
7150 add_stmt (assertion);
7152 return;
7155 /* Fold the expression and convert it to a boolean value. */
7156 condition = instantiate_non_dependent_expr (condition);
7157 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7158 condition = maybe_constant_value (condition);
7160 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7161 /* Do nothing; the condition is satisfied. */
7163 else
7165 location_t saved_loc = input_location;
7167 input_location = location;
7168 if (TREE_CODE (condition) == INTEGER_CST
7169 && integer_zerop (condition))
7171 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
7172 (TREE_TYPE (TREE_TYPE (message))));
7173 int len = TREE_STRING_LENGTH (message) / sz - 1;
7174 /* Report the error. */
7175 if (len == 0)
7176 error ("static assertion failed");
7177 else
7178 error ("static assertion failed: %s",
7179 TREE_STRING_POINTER (message));
7181 else if (condition && condition != error_mark_node)
7183 error ("non-constant condition for static assertion");
7184 if (require_potential_rvalue_constant_expression (condition))
7185 cxx_constant_value (condition);
7187 input_location = saved_loc;
7191 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7192 suitable for use as a type-specifier.
7194 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7195 id-expression or a class member access, FALSE when it was parsed as
7196 a full expression. */
7198 tree
7199 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7200 tsubst_flags_t complain)
7202 tree type = NULL_TREE;
7204 if (!expr || error_operand_p (expr))
7205 return error_mark_node;
7207 if (TYPE_P (expr)
7208 || TREE_CODE (expr) == TYPE_DECL
7209 || (TREE_CODE (expr) == BIT_NOT_EXPR
7210 && TYPE_P (TREE_OPERAND (expr, 0))))
7212 if (complain & tf_error)
7213 error ("argument to decltype must be an expression");
7214 return error_mark_node;
7217 /* Depending on the resolution of DR 1172, we may later need to distinguish
7218 instantiation-dependent but not type-dependent expressions so that, say,
7219 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7220 if (instantiation_dependent_expression_p (expr))
7222 type = cxx_make_type (DECLTYPE_TYPE);
7223 DECLTYPE_TYPE_EXPR (type) = expr;
7224 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7225 = id_expression_or_member_access_p;
7226 SET_TYPE_STRUCTURAL_EQUALITY (type);
7228 return type;
7231 /* The type denoted by decltype(e) is defined as follows: */
7233 expr = resolve_nondeduced_context (expr);
7235 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
7236 return error_mark_node;
7238 if (type_unknown_p (expr))
7240 if (complain & tf_error)
7241 error ("decltype cannot resolve address of overloaded function");
7242 return error_mark_node;
7245 /* To get the size of a static data member declared as an array of
7246 unknown bound, we need to instantiate it. */
7247 if (VAR_P (expr)
7248 && VAR_HAD_UNKNOWN_BOUND (expr)
7249 && DECL_TEMPLATE_INSTANTIATION (expr))
7250 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7252 if (id_expression_or_member_access_p)
7254 /* If e is an id-expression or a class member access (5.2.5
7255 [expr.ref]), decltype(e) is defined as the type of the entity
7256 named by e. If there is no such entity, or e names a set of
7257 overloaded functions, the program is ill-formed. */
7258 if (identifier_p (expr))
7259 expr = lookup_name (expr);
7261 if (INDIRECT_REF_P (expr))
7262 /* This can happen when the expression is, e.g., "a.b". Just
7263 look at the underlying operand. */
7264 expr = TREE_OPERAND (expr, 0);
7266 if (TREE_CODE (expr) == OFFSET_REF
7267 || TREE_CODE (expr) == MEMBER_REF
7268 || TREE_CODE (expr) == SCOPE_REF)
7269 /* We're only interested in the field itself. If it is a
7270 BASELINK, we will need to see through it in the next
7271 step. */
7272 expr = TREE_OPERAND (expr, 1);
7274 if (BASELINK_P (expr))
7275 /* See through BASELINK nodes to the underlying function. */
7276 expr = BASELINK_FUNCTIONS (expr);
7278 switch (TREE_CODE (expr))
7280 case FIELD_DECL:
7281 if (DECL_BIT_FIELD_TYPE (expr))
7283 type = DECL_BIT_FIELD_TYPE (expr);
7284 break;
7286 /* Fall through for fields that aren't bitfields. */
7288 case FUNCTION_DECL:
7289 case VAR_DECL:
7290 case CONST_DECL:
7291 case PARM_DECL:
7292 case RESULT_DECL:
7293 case TEMPLATE_PARM_INDEX:
7294 expr = mark_type_use (expr);
7295 type = TREE_TYPE (expr);
7296 break;
7298 case ERROR_MARK:
7299 type = error_mark_node;
7300 break;
7302 case COMPONENT_REF:
7303 case COMPOUND_EXPR:
7304 mark_type_use (expr);
7305 type = is_bitfield_expr_with_lowered_type (expr);
7306 if (!type)
7307 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7308 break;
7310 case BIT_FIELD_REF:
7311 gcc_unreachable ();
7313 case INTEGER_CST:
7314 case PTRMEM_CST:
7315 /* We can get here when the id-expression refers to an
7316 enumerator or non-type template parameter. */
7317 type = TREE_TYPE (expr);
7318 break;
7320 default:
7321 /* Handle instantiated template non-type arguments. */
7322 type = TREE_TYPE (expr);
7323 break;
7326 else
7328 /* Within a lambda-expression:
7330 Every occurrence of decltype((x)) where x is a possibly
7331 parenthesized id-expression that names an entity of
7332 automatic storage duration is treated as if x were
7333 transformed into an access to a corresponding data member
7334 of the closure type that would have been declared if x
7335 were a use of the denoted entity. */
7336 if (outer_automatic_var_p (expr)
7337 && current_function_decl
7338 && LAMBDA_FUNCTION_P (current_function_decl))
7339 type = capture_decltype (expr);
7340 else if (error_operand_p (expr))
7341 type = error_mark_node;
7342 else if (expr == current_class_ptr)
7343 /* If the expression is just "this", we want the
7344 cv-unqualified pointer for the "this" type. */
7345 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7346 else
7348 /* Otherwise, where T is the type of e, if e is an lvalue,
7349 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7350 cp_lvalue_kind clk = lvalue_kind (expr);
7351 type = unlowered_expr_type (expr);
7352 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7354 /* For vector types, pick a non-opaque variant. */
7355 if (VECTOR_TYPE_P (type))
7356 type = strip_typedefs (type);
7358 if (clk != clk_none && !(clk & clk_class))
7359 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7363 return type;
7366 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7367 __has_nothrow_copy, depending on assign_p. */
7369 static bool
7370 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7372 tree fns;
7374 if (assign_p)
7376 int ix;
7377 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7378 if (ix < 0)
7379 return false;
7380 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7382 else if (TYPE_HAS_COPY_CTOR (type))
7384 /* If construction of the copy constructor was postponed, create
7385 it now. */
7386 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7387 lazily_declare_fn (sfk_copy_constructor, type);
7388 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7389 lazily_declare_fn (sfk_move_constructor, type);
7390 fns = CLASSTYPE_CONSTRUCTORS (type);
7392 else
7393 return false;
7395 for (; fns; fns = OVL_NEXT (fns))
7397 tree fn = OVL_CURRENT (fns);
7399 if (assign_p)
7401 if (copy_fn_p (fn) == 0)
7402 continue;
7404 else if (copy_fn_p (fn) <= 0)
7405 continue;
7407 maybe_instantiate_noexcept (fn);
7408 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7409 return false;
7412 return true;
7415 /* Actually evaluates the trait. */
7417 static bool
7418 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7420 enum tree_code type_code1;
7421 tree t;
7423 type_code1 = TREE_CODE (type1);
7425 switch (kind)
7427 case CPTK_HAS_NOTHROW_ASSIGN:
7428 type1 = strip_array_types (type1);
7429 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7430 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7431 || (CLASS_TYPE_P (type1)
7432 && classtype_has_nothrow_assign_or_copy_p (type1,
7433 true))));
7435 case CPTK_HAS_TRIVIAL_ASSIGN:
7436 /* ??? The standard seems to be missing the "or array of such a class
7437 type" wording for this trait. */
7438 type1 = strip_array_types (type1);
7439 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7440 && (trivial_type_p (type1)
7441 || (CLASS_TYPE_P (type1)
7442 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7444 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7445 type1 = strip_array_types (type1);
7446 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7447 || (CLASS_TYPE_P (type1)
7448 && (t = locate_ctor (type1))
7449 && (maybe_instantiate_noexcept (t),
7450 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7452 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7453 type1 = strip_array_types (type1);
7454 return (trivial_type_p (type1)
7455 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7457 case CPTK_HAS_NOTHROW_COPY:
7458 type1 = strip_array_types (type1);
7459 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7460 || (CLASS_TYPE_P (type1)
7461 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7463 case CPTK_HAS_TRIVIAL_COPY:
7464 /* ??? The standard seems to be missing the "or array of such a class
7465 type" wording for this trait. */
7466 type1 = strip_array_types (type1);
7467 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7468 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7470 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7471 type1 = strip_array_types (type1);
7472 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7473 || (CLASS_TYPE_P (type1)
7474 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7476 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7477 return type_has_virtual_destructor (type1);
7479 case CPTK_IS_ABSTRACT:
7480 return (ABSTRACT_CLASS_TYPE_P (type1));
7482 case CPTK_IS_BASE_OF:
7483 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7484 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7485 || DERIVED_FROM_P (type1, type2)));
7487 case CPTK_IS_CLASS:
7488 return (NON_UNION_CLASS_TYPE_P (type1));
7490 case CPTK_IS_EMPTY:
7491 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7493 case CPTK_IS_ENUM:
7494 return (type_code1 == ENUMERAL_TYPE);
7496 case CPTK_IS_FINAL:
7497 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7499 case CPTK_IS_LITERAL_TYPE:
7500 return (literal_type_p (type1));
7502 case CPTK_IS_POD:
7503 return (pod_type_p (type1));
7505 case CPTK_IS_POLYMORPHIC:
7506 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7508 case CPTK_IS_STD_LAYOUT:
7509 return (std_layout_type_p (type1));
7511 case CPTK_IS_TRIVIAL:
7512 return (trivial_type_p (type1));
7514 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7515 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7517 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7518 return is_trivially_xible (INIT_EXPR, type1, type2);
7520 case CPTK_IS_TRIVIALLY_COPYABLE:
7521 return (trivially_copyable_p (type1));
7523 case CPTK_IS_UNION:
7524 return (type_code1 == UNION_TYPE);
7526 default:
7527 gcc_unreachable ();
7528 return false;
7532 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7533 void, or a complete type, returns true, otherwise false. */
7535 static bool
7536 check_trait_type (tree type)
7538 if (type == NULL_TREE)
7539 return true;
7541 if (TREE_CODE (type) == TREE_LIST)
7542 return (check_trait_type (TREE_VALUE (type))
7543 && check_trait_type (TREE_CHAIN (type)));
7545 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7546 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7547 return true;
7549 if (VOID_TYPE_P (type))
7550 return true;
7552 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7555 /* Process a trait expression. */
7557 tree
7558 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7560 if (type1 == error_mark_node
7561 || type2 == error_mark_node)
7562 return error_mark_node;
7564 if (processing_template_decl)
7566 tree trait_expr = make_node (TRAIT_EXPR);
7567 TREE_TYPE (trait_expr) = boolean_type_node;
7568 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7569 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7570 TRAIT_EXPR_KIND (trait_expr) = kind;
7571 return trait_expr;
7574 switch (kind)
7576 case CPTK_HAS_NOTHROW_ASSIGN:
7577 case CPTK_HAS_TRIVIAL_ASSIGN:
7578 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7579 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7580 case CPTK_HAS_NOTHROW_COPY:
7581 case CPTK_HAS_TRIVIAL_COPY:
7582 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7583 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7584 case CPTK_IS_ABSTRACT:
7585 case CPTK_IS_EMPTY:
7586 case CPTK_IS_FINAL:
7587 case CPTK_IS_LITERAL_TYPE:
7588 case CPTK_IS_POD:
7589 case CPTK_IS_POLYMORPHIC:
7590 case CPTK_IS_STD_LAYOUT:
7591 case CPTK_IS_TRIVIAL:
7592 case CPTK_IS_TRIVIALLY_COPYABLE:
7593 if (!check_trait_type (type1))
7594 return error_mark_node;
7595 break;
7597 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7598 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7599 if (!check_trait_type (type1)
7600 || !check_trait_type (type2))
7601 return error_mark_node;
7602 break;
7604 case CPTK_IS_BASE_OF:
7605 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7606 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7607 && !complete_type_or_else (type2, NULL_TREE))
7608 /* We already issued an error. */
7609 return error_mark_node;
7610 break;
7612 case CPTK_IS_CLASS:
7613 case CPTK_IS_ENUM:
7614 case CPTK_IS_UNION:
7615 break;
7617 default:
7618 gcc_unreachable ();
7621 return (trait_expr_value (kind, type1, type2)
7622 ? boolean_true_node : boolean_false_node);
7625 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7626 which is ignored for C++. */
7628 void
7629 set_float_const_decimal64 (void)
7633 void
7634 clear_float_const_decimal64 (void)
7638 bool
7639 float_const_decimal64_p (void)
7641 return 0;
7645 /* Return true if T designates the implied `this' parameter. */
7647 bool
7648 is_this_parameter (tree t)
7650 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7651 return false;
7652 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7653 return true;
7656 /* Insert the deduced return type for an auto function. */
7658 void
7659 apply_deduced_return_type (tree fco, tree return_type)
7661 tree result;
7663 if (return_type == error_mark_node)
7664 return;
7666 if (LAMBDA_FUNCTION_P (fco))
7668 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7669 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7672 if (DECL_CONV_FN_P (fco))
7673 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7675 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7677 result = DECL_RESULT (fco);
7678 if (result == NULL_TREE)
7679 return;
7680 if (TREE_TYPE (result) == return_type)
7681 return;
7683 /* We already have a DECL_RESULT from start_preparsed_function.
7684 Now we need to redo the work it and allocate_struct_function
7685 did to reflect the new type. */
7686 gcc_assert (current_function_decl == fco);
7687 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7688 TYPE_MAIN_VARIANT (return_type));
7689 DECL_ARTIFICIAL (result) = 1;
7690 DECL_IGNORED_P (result) = 1;
7691 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7692 result);
7694 DECL_RESULT (fco) = result;
7696 if (!processing_template_decl)
7698 if (!VOID_TYPE_P (TREE_TYPE (result)))
7699 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7700 bool aggr = aggregate_value_p (result, fco);
7701 #ifdef PCC_STATIC_STRUCT_RETURN
7702 cfun->returns_pcc_struct = aggr;
7703 #endif
7704 cfun->returns_struct = aggr;
7709 /* DECL is a local variable or parameter from the surrounding scope of a
7710 lambda-expression. Returns the decltype for a use of the capture field
7711 for DECL even if it hasn't been captured yet. */
7713 static tree
7714 capture_decltype (tree decl)
7716 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7717 /* FIXME do lookup instead of list walk? */
7718 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7719 tree type;
7721 if (cap)
7722 type = TREE_TYPE (TREE_PURPOSE (cap));
7723 else
7724 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7726 case CPLD_NONE:
7727 error ("%qD is not captured", decl);
7728 return error_mark_node;
7730 case CPLD_COPY:
7731 type = TREE_TYPE (decl);
7732 if (TREE_CODE (type) == REFERENCE_TYPE
7733 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7734 type = TREE_TYPE (type);
7735 break;
7737 case CPLD_REFERENCE:
7738 type = TREE_TYPE (decl);
7739 if (TREE_CODE (type) != REFERENCE_TYPE)
7740 type = build_reference_type (TREE_TYPE (decl));
7741 break;
7743 default:
7744 gcc_unreachable ();
7747 if (TREE_CODE (type) != REFERENCE_TYPE)
7749 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7750 type = cp_build_qualified_type (type, (cp_type_quals (type)
7751 |TYPE_QUAL_CONST));
7752 type = build_reference_type (type);
7754 return type;
7757 #include "gt-cp-semantics.h"