PR c++/64487
[official-gcc.git] / gcc / cp / semantics.c
blob4365a53acd6cbb7e89909afd8fe8a3a184afe03b
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 "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "hash-map.h"
45 #include "is-a.h"
46 #include "plugin-api.h"
47 #include "vec.h"
48 #include "hashtab.h"
49 #include "hash-set.h"
50 #include "machmode.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "tree-iterator.h"
57 #include "target.h"
58 #include "hash-table.h"
59 #include "gimplify.h"
60 #include "bitmap.h"
61 #include "omp-low.h"
62 #include "builtins.h"
63 #include "convert.h"
65 /* There routines provide a modular interface to perform many parsing
66 operations. They may therefore be used during actual parsing, or
67 during template instantiation, which may be regarded as a
68 degenerate form of parsing. */
70 static tree maybe_convert_cond (tree);
71 static tree finalize_nrv_r (tree *, int *, void *);
72 static tree capture_decltype (tree);
75 /* Deferred Access Checking Overview
76 ---------------------------------
78 Most C++ expressions and declarations require access checking
79 to be performed during parsing. However, in several cases,
80 this has to be treated differently.
82 For member declarations, access checking has to be deferred
83 until more information about the declaration is known. For
84 example:
86 class A {
87 typedef int X;
88 public:
89 X f();
92 A::X A::f();
93 A::X g();
95 When we are parsing the function return type `A::X', we don't
96 really know if this is allowed until we parse the function name.
98 Furthermore, some contexts require that access checking is
99 never performed at all. These include class heads, and template
100 instantiations.
102 Typical use of access checking functions is described here:
104 1. When we enter a context that requires certain access checking
105 mode, the function `push_deferring_access_checks' is called with
106 DEFERRING argument specifying the desired mode. Access checking
107 may be performed immediately (dk_no_deferred), deferred
108 (dk_deferred), or not performed (dk_no_check).
110 2. When a declaration such as a type, or a variable, is encountered,
111 the function `perform_or_defer_access_check' is called. It
112 maintains a vector of all deferred checks.
114 3. The global `current_class_type' or `current_function_decl' is then
115 setup by the parser. `enforce_access' relies on these information
116 to check access.
118 4. Upon exiting the context mentioned in step 1,
119 `perform_deferred_access_checks' is called to check all declaration
120 stored in the vector. `pop_deferring_access_checks' is then
121 called to restore the previous access checking mode.
123 In case of parsing error, we simply call `pop_deferring_access_checks'
124 without `perform_deferred_access_checks'. */
126 typedef struct GTY(()) deferred_access {
127 /* A vector representing name-lookups for which we have deferred
128 checking access controls. We cannot check the accessibility of
129 names used in a decl-specifier-seq until we know what is being
130 declared because code like:
132 class A {
133 class B {};
134 B* f();
137 A::B* A::f() { return 0; }
139 is valid, even though `A::B' is not generally accessible. */
140 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
142 /* The current mode of access checks. */
143 enum deferring_kind deferring_access_checks_kind;
145 } deferred_access;
147 /* Data for deferred access checking. */
148 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
149 static GTY(()) unsigned deferred_access_no_check;
151 /* Save the current deferred access states and start deferred
152 access checking iff DEFER_P is true. */
154 void
155 push_deferring_access_checks (deferring_kind deferring)
157 /* For context like template instantiation, access checking
158 disabling applies to all nested context. */
159 if (deferred_access_no_check || deferring == dk_no_check)
160 deferred_access_no_check++;
161 else
163 deferred_access e = {NULL, deferring};
164 vec_safe_push (deferred_access_stack, e);
168 /* Save the current deferred access states and start deferred access
169 checking, continuing the set of deferred checks in CHECKS. */
171 void
172 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
174 push_deferring_access_checks (dk_deferred);
175 if (!deferred_access_no_check)
176 deferred_access_stack->last().deferred_access_checks = checks;
179 /* Resume deferring access checks again after we stopped doing
180 this previously. */
182 void
183 resume_deferring_access_checks (void)
185 if (!deferred_access_no_check)
186 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
189 /* Stop deferring access checks. */
191 void
192 stop_deferring_access_checks (void)
194 if (!deferred_access_no_check)
195 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
198 /* Discard the current deferred access checks and restore the
199 previous states. */
201 void
202 pop_deferring_access_checks (void)
204 if (deferred_access_no_check)
205 deferred_access_no_check--;
206 else
207 deferred_access_stack->pop ();
210 /* Returns a TREE_LIST representing the deferred checks.
211 The TREE_PURPOSE of each node is the type through which the
212 access occurred; the TREE_VALUE is the declaration named.
215 vec<deferred_access_check, va_gc> *
216 get_deferred_access_checks (void)
218 if (deferred_access_no_check)
219 return NULL;
220 else
221 return (deferred_access_stack->last().deferred_access_checks);
224 /* Take current deferred checks and combine with the
225 previous states if we also defer checks previously.
226 Otherwise perform checks now. */
228 void
229 pop_to_parent_deferring_access_checks (void)
231 if (deferred_access_no_check)
232 deferred_access_no_check--;
233 else
235 vec<deferred_access_check, va_gc> *checks;
236 deferred_access *ptr;
238 checks = (deferred_access_stack->last ().deferred_access_checks);
240 deferred_access_stack->pop ();
241 ptr = &deferred_access_stack->last ();
242 if (ptr->deferring_access_checks_kind == dk_no_deferred)
244 /* Check access. */
245 perform_access_checks (checks, tf_warning_or_error);
247 else
249 /* Merge with parent. */
250 int i, j;
251 deferred_access_check *chk, *probe;
253 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
255 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
257 if (probe->binfo == chk->binfo &&
258 probe->decl == chk->decl &&
259 probe->diag_decl == chk->diag_decl)
260 goto found;
262 /* Insert into parent's checks. */
263 vec_safe_push (ptr->deferred_access_checks, *chk);
264 found:;
270 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
271 is the BINFO indicating the qualifying scope used to access the
272 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
273 or we aren't in SFINAE context or all the checks succeed return TRUE,
274 otherwise FALSE. */
276 bool
277 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
278 tsubst_flags_t complain)
280 int i;
281 deferred_access_check *chk;
282 location_t loc = input_location;
283 bool ok = true;
285 if (!checks)
286 return true;
288 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
290 input_location = chk->loc;
291 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
294 input_location = loc;
295 return (complain & tf_error) ? true : ok;
298 /* Perform the deferred access checks.
300 After performing the checks, we still have to keep the list
301 `deferred_access_stack->deferred_access_checks' since we may want
302 to check access for them again later in a different context.
303 For example:
305 class A {
306 typedef int X;
307 static X a;
309 A::X A::a, x; // No error for `A::a', error for `x'
311 We have to perform deferred access of `A::X', first with `A::a',
312 next with `x'. Return value like perform_access_checks above. */
314 bool
315 perform_deferred_access_checks (tsubst_flags_t complain)
317 return perform_access_checks (get_deferred_access_checks (), complain);
320 /* Defer checking the accessibility of DECL, when looked up in
321 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
322 Return value like perform_access_checks above. */
324 bool
325 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
326 tsubst_flags_t complain)
328 int i;
329 deferred_access *ptr;
330 deferred_access_check *chk;
333 /* Exit if we are in a context that no access checking is performed.
335 if (deferred_access_no_check)
336 return true;
338 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
340 ptr = &deferred_access_stack->last ();
342 /* If we are not supposed to defer access checks, just check now. */
343 if (ptr->deferring_access_checks_kind == dk_no_deferred)
345 bool ok = enforce_access (binfo, decl, diag_decl, complain);
346 return (complain & tf_error) ? true : ok;
349 /* See if we are already going to perform this check. */
350 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
352 if (chk->decl == decl && chk->binfo == binfo &&
353 chk->diag_decl == diag_decl)
355 return true;
358 /* If not, record the check. */
359 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
360 vec_safe_push (ptr->deferred_access_checks, new_access);
362 return true;
365 /* Returns nonzero if the current statement is a full expression,
366 i.e. temporaries created during that statement should be destroyed
367 at the end of the statement. */
370 stmts_are_full_exprs_p (void)
372 return current_stmt_tree ()->stmts_are_full_exprs_p;
375 /* T is a statement. Add it to the statement-tree. This is the C++
376 version. The C/ObjC frontends have a slightly different version of
377 this function. */
379 tree
380 add_stmt (tree t)
382 enum tree_code code = TREE_CODE (t);
384 if (EXPR_P (t) && code != LABEL_EXPR)
386 if (!EXPR_HAS_LOCATION (t))
387 SET_EXPR_LOCATION (t, input_location);
389 /* When we expand a statement-tree, we must know whether or not the
390 statements are full-expressions. We record that fact here. */
391 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
394 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
395 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
397 /* Add T to the statement-tree. Non-side-effect statements need to be
398 recorded during statement expressions. */
399 gcc_checking_assert (!stmt_list_stack->is_empty ());
400 append_to_statement_list_force (t, &cur_stmt_list);
402 return t;
405 /* Returns the stmt_tree to which statements are currently being added. */
407 stmt_tree
408 current_stmt_tree (void)
410 return (cfun
411 ? &cfun->language->base.x_stmt_tree
412 : &scope_chain->x_stmt_tree);
415 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
417 static tree
418 maybe_cleanup_point_expr (tree expr)
420 if (!processing_template_decl && stmts_are_full_exprs_p ())
421 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
422 return expr;
425 /* Like maybe_cleanup_point_expr except have the type of the new expression be
426 void so we don't need to create a temporary variable to hold the inner
427 expression. The reason why we do this is because the original type might be
428 an aggregate and we cannot create a temporary variable for that type. */
430 tree
431 maybe_cleanup_point_expr_void (tree expr)
433 if (!processing_template_decl && stmts_are_full_exprs_p ())
434 expr = fold_build_cleanup_point_expr (void_type_node, expr);
435 return expr;
440 /* Create a declaration statement for the declaration given by the DECL. */
442 void
443 add_decl_expr (tree decl)
445 tree r = build_stmt (input_location, DECL_EXPR, decl);
446 if (DECL_INITIAL (decl)
447 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
448 r = maybe_cleanup_point_expr_void (r);
449 add_stmt (r);
452 /* Finish a scope. */
454 tree
455 do_poplevel (tree stmt_list)
457 tree block = NULL;
459 if (stmts_are_full_exprs_p ())
460 block = poplevel (kept_level_p (), 1, 0);
462 stmt_list = pop_stmt_list (stmt_list);
464 if (!processing_template_decl)
466 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
467 /* ??? See c_end_compound_stmt re statement expressions. */
470 return stmt_list;
473 /* Begin a new scope. */
475 static tree
476 do_pushlevel (scope_kind sk)
478 tree ret = push_stmt_list ();
479 if (stmts_are_full_exprs_p ())
480 begin_scope (sk, NULL);
481 return ret;
484 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
485 when the current scope is exited. EH_ONLY is true when this is not
486 meant to apply to normal control flow transfer. */
488 void
489 push_cleanup (tree decl, tree cleanup, bool eh_only)
491 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
492 CLEANUP_EH_ONLY (stmt) = eh_only;
493 add_stmt (stmt);
494 CLEANUP_BODY (stmt) = push_stmt_list ();
497 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
498 the current loops, represented by 'NULL_TREE' if we've seen a possible
499 exit, and 'error_mark_node' if not. This is currently used only to
500 suppress the warning about a function with no return statements, and
501 therefore we don't bother noting returns as possible exits. We also
502 don't bother with gotos. */
504 static void
505 begin_maybe_infinite_loop (tree cond)
507 /* Only track this while parsing a function, not during instantiation. */
508 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
509 && !processing_template_decl))
510 return;
511 bool maybe_infinite = true;
512 if (cond)
514 cond = fold_non_dependent_expr (cond);
515 maybe_infinite = integer_nonzerop (cond);
517 vec_safe_push (cp_function_chain->infinite_loops,
518 maybe_infinite ? error_mark_node : NULL_TREE);
522 /* A break is a possible exit for the current loop. */
524 void
525 break_maybe_infinite_loop (void)
527 if (!cfun)
528 return;
529 cp_function_chain->infinite_loops->last() = NULL_TREE;
532 /* If we reach the end of the loop without seeing a possible exit, we have
533 an infinite loop. */
535 static void
536 end_maybe_infinite_loop (tree cond)
538 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
539 && !processing_template_decl))
540 return;
541 tree current = cp_function_chain->infinite_loops->pop();
542 if (current != NULL_TREE)
544 cond = fold_non_dependent_expr (cond);
545 if (integer_nonzerop (cond))
546 current_function_infinite_loop = 1;
551 /* Begin a conditional that might contain a declaration. When generating
552 normal code, we want the declaration to appear before the statement
553 containing the conditional. When generating template code, we want the
554 conditional to be rendered as the raw DECL_EXPR. */
556 static void
557 begin_cond (tree *cond_p)
559 if (processing_template_decl)
560 *cond_p = push_stmt_list ();
563 /* Finish such a conditional. */
565 static void
566 finish_cond (tree *cond_p, tree expr)
568 if (processing_template_decl)
570 tree cond = pop_stmt_list (*cond_p);
572 if (expr == NULL_TREE)
573 /* Empty condition in 'for'. */
574 gcc_assert (empty_expr_stmt_p (cond));
575 else if (check_for_bare_parameter_packs (expr))
576 expr = error_mark_node;
577 else if (!empty_expr_stmt_p (cond))
578 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
580 *cond_p = expr;
583 /* If *COND_P specifies a conditional with a declaration, transform the
584 loop such that
585 while (A x = 42) { }
586 for (; A x = 42;) { }
587 becomes
588 while (true) { A x = 42; if (!x) break; }
589 for (;;) { A x = 42; if (!x) break; }
590 The statement list for BODY will be empty if the conditional did
591 not declare anything. */
593 static void
594 simplify_loop_decl_cond (tree *cond_p, tree body)
596 tree cond, if_stmt;
598 if (!TREE_SIDE_EFFECTS (body))
599 return;
601 cond = *cond_p;
602 *cond_p = boolean_true_node;
604 if_stmt = begin_if_stmt ();
605 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
606 finish_if_stmt_cond (cond, if_stmt);
607 finish_break_stmt ();
608 finish_then_clause (if_stmt);
609 finish_if_stmt (if_stmt);
612 /* Finish a goto-statement. */
614 tree
615 finish_goto_stmt (tree destination)
617 if (identifier_p (destination))
618 destination = lookup_label (destination);
620 /* We warn about unused labels with -Wunused. That means we have to
621 mark the used labels as used. */
622 if (TREE_CODE (destination) == LABEL_DECL)
623 TREE_USED (destination) = 1;
624 else
626 if (check_no_cilk (destination,
627 "Cilk array notation cannot be used as a computed goto expression",
628 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
629 destination = error_mark_node;
630 destination = mark_rvalue_use (destination);
631 if (!processing_template_decl)
633 destination = cp_convert (ptr_type_node, destination,
634 tf_warning_or_error);
635 if (error_operand_p (destination))
636 return NULL_TREE;
637 destination
638 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
639 destination);
643 check_goto (destination);
645 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
648 /* COND is the condition-expression for an if, while, etc.,
649 statement. Convert it to a boolean value, if appropriate.
650 In addition, verify sequence points if -Wsequence-point is enabled. */
652 static tree
653 maybe_convert_cond (tree cond)
655 /* Empty conditions remain empty. */
656 if (!cond)
657 return NULL_TREE;
659 /* Wait until we instantiate templates before doing conversion. */
660 if (processing_template_decl)
661 return cond;
663 if (warn_sequence_point)
664 verify_sequence_points (cond);
666 /* Do the conversion. */
667 cond = convert_from_reference (cond);
669 if (TREE_CODE (cond) == MODIFY_EXPR
670 && !TREE_NO_WARNING (cond)
671 && warn_parentheses)
673 warning (OPT_Wparentheses,
674 "suggest parentheses around assignment used as truth value");
675 TREE_NO_WARNING (cond) = 1;
678 return condition_conversion (cond);
681 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
683 tree
684 finish_expr_stmt (tree expr)
686 tree r = NULL_TREE;
688 if (expr != NULL_TREE)
690 if (!processing_template_decl)
692 if (warn_sequence_point)
693 verify_sequence_points (expr);
694 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
696 else if (!type_dependent_expression_p (expr))
697 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
698 tf_warning_or_error);
700 if (check_for_bare_parameter_packs (expr))
701 expr = error_mark_node;
703 /* Simplification of inner statement expressions, compound exprs,
704 etc can result in us already having an EXPR_STMT. */
705 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
707 if (TREE_CODE (expr) != EXPR_STMT)
708 expr = build_stmt (input_location, EXPR_STMT, expr);
709 expr = maybe_cleanup_point_expr_void (expr);
712 r = add_stmt (expr);
715 return r;
719 /* Begin an if-statement. Returns a newly created IF_STMT if
720 appropriate. */
722 tree
723 begin_if_stmt (void)
725 tree r, scope;
726 scope = do_pushlevel (sk_cond);
727 r = build_stmt (input_location, IF_STMT, NULL_TREE,
728 NULL_TREE, NULL_TREE, scope);
729 begin_cond (&IF_COND (r));
730 return r;
733 /* Process the COND of an if-statement, which may be given by
734 IF_STMT. */
736 void
737 finish_if_stmt_cond (tree cond, tree if_stmt)
739 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
740 add_stmt (if_stmt);
741 THEN_CLAUSE (if_stmt) = push_stmt_list ();
744 /* Finish the then-clause of an if-statement, which may be given by
745 IF_STMT. */
747 tree
748 finish_then_clause (tree if_stmt)
750 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
751 return if_stmt;
754 /* Begin the else-clause of an if-statement. */
756 void
757 begin_else_clause (tree if_stmt)
759 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
762 /* Finish the else-clause of an if-statement, which may be given by
763 IF_STMT. */
765 void
766 finish_else_clause (tree if_stmt)
768 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
771 /* Finish an if-statement. */
773 void
774 finish_if_stmt (tree if_stmt)
776 tree scope = IF_SCOPE (if_stmt);
777 IF_SCOPE (if_stmt) = NULL;
778 add_stmt (do_poplevel (scope));
781 /* Begin a while-statement. Returns a newly created WHILE_STMT if
782 appropriate. */
784 tree
785 begin_while_stmt (void)
787 tree r;
788 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
789 add_stmt (r);
790 WHILE_BODY (r) = do_pushlevel (sk_block);
791 begin_cond (&WHILE_COND (r));
792 return r;
795 /* Process the COND of a while-statement, which may be given by
796 WHILE_STMT. */
798 void
799 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
801 if (check_no_cilk (cond,
802 "Cilk array notation cannot be used as a condition for while statement",
803 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
804 cond = error_mark_node;
805 cond = maybe_convert_cond (cond);
806 finish_cond (&WHILE_COND (while_stmt), cond);
807 begin_maybe_infinite_loop (cond);
808 if (ivdep && cond != error_mark_node)
809 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
810 TREE_TYPE (WHILE_COND (while_stmt)),
811 WHILE_COND (while_stmt),
812 build_int_cst (integer_type_node,
813 annot_expr_ivdep_kind));
814 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
817 /* Finish a while-statement, which may be given by WHILE_STMT. */
819 void
820 finish_while_stmt (tree while_stmt)
822 end_maybe_infinite_loop (boolean_true_node);
823 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
826 /* Begin a do-statement. Returns a newly created DO_STMT if
827 appropriate. */
829 tree
830 begin_do_stmt (void)
832 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
833 begin_maybe_infinite_loop (boolean_true_node);
834 add_stmt (r);
835 DO_BODY (r) = push_stmt_list ();
836 return r;
839 /* Finish the body of a do-statement, which may be given by DO_STMT. */
841 void
842 finish_do_body (tree do_stmt)
844 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
846 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
847 body = STATEMENT_LIST_TAIL (body)->stmt;
849 if (IS_EMPTY_STMT (body))
850 warning (OPT_Wempty_body,
851 "suggest explicit braces around empty body in %<do%> statement");
854 /* Finish a do-statement, which may be given by DO_STMT, and whose
855 COND is as indicated. */
857 void
858 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
860 if (check_no_cilk (cond,
861 "Cilk array notation cannot be used as a condition for a do-while statement",
862 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
863 cond = error_mark_node;
864 cond = maybe_convert_cond (cond);
865 end_maybe_infinite_loop (cond);
866 if (ivdep && cond != error_mark_node)
867 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
868 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
869 DO_COND (do_stmt) = cond;
872 /* Finish a return-statement. The EXPRESSION returned, if any, is as
873 indicated. */
875 tree
876 finish_return_stmt (tree expr)
878 tree r;
879 bool no_warning;
881 expr = check_return_expr (expr, &no_warning);
883 if (error_operand_p (expr)
884 || (flag_openmp && !check_omp_return ()))
885 return error_mark_node;
886 if (!processing_template_decl)
888 if (warn_sequence_point)
889 verify_sequence_points (expr);
891 if (DECL_DESTRUCTOR_P (current_function_decl)
892 || (DECL_CONSTRUCTOR_P (current_function_decl)
893 && targetm.cxx.cdtor_returns_this ()))
895 /* Similarly, all destructors must run destructors for
896 base-classes before returning. So, all returns in a
897 destructor get sent to the DTOR_LABEL; finish_function emits
898 code to return a value there. */
899 return finish_goto_stmt (cdtor_label);
903 r = build_stmt (input_location, RETURN_EXPR, expr);
904 TREE_NO_WARNING (r) |= no_warning;
905 r = maybe_cleanup_point_expr_void (r);
906 r = add_stmt (r);
908 return r;
911 /* Begin the scope of a for-statement or a range-for-statement.
912 Both the returned trees are to be used in a call to
913 begin_for_stmt or begin_range_for_stmt. */
915 tree
916 begin_for_scope (tree *init)
918 tree scope = NULL_TREE;
919 if (flag_new_for_scope > 0)
920 scope = do_pushlevel (sk_for);
922 if (processing_template_decl)
923 *init = push_stmt_list ();
924 else
925 *init = NULL_TREE;
927 return scope;
930 /* Begin a for-statement. Returns a new FOR_STMT.
931 SCOPE and INIT should be the return of begin_for_scope,
932 or both NULL_TREE */
934 tree
935 begin_for_stmt (tree scope, tree init)
937 tree r;
939 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
940 NULL_TREE, NULL_TREE, NULL_TREE);
942 if (scope == NULL_TREE)
944 gcc_assert (!init || !(flag_new_for_scope > 0));
945 if (!init)
946 scope = begin_for_scope (&init);
948 FOR_INIT_STMT (r) = init;
949 FOR_SCOPE (r) = scope;
951 return r;
954 /* Finish the for-init-statement of a for-statement, which may be
955 given by FOR_STMT. */
957 void
958 finish_for_init_stmt (tree for_stmt)
960 if (processing_template_decl)
961 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
962 add_stmt (for_stmt);
963 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
964 begin_cond (&FOR_COND (for_stmt));
967 /* Finish the COND of a for-statement, which may be given by
968 FOR_STMT. */
970 void
971 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
973 if (check_no_cilk (cond,
974 "Cilk array notation cannot be used in a condition for a for-loop",
975 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
976 cond = error_mark_node;
977 cond = maybe_convert_cond (cond);
978 finish_cond (&FOR_COND (for_stmt), cond);
979 begin_maybe_infinite_loop (cond);
980 if (ivdep && cond != error_mark_node)
981 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
982 TREE_TYPE (FOR_COND (for_stmt)),
983 FOR_COND (for_stmt),
984 build_int_cst (integer_type_node,
985 annot_expr_ivdep_kind));
986 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
989 /* Finish the increment-EXPRESSION in a for-statement, which may be
990 given by FOR_STMT. */
992 void
993 finish_for_expr (tree expr, tree for_stmt)
995 if (!expr)
996 return;
997 /* If EXPR is an overloaded function, issue an error; there is no
998 context available to use to perform overload resolution. */
999 if (type_unknown_p (expr))
1001 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1002 expr = error_mark_node;
1004 if (!processing_template_decl)
1006 if (warn_sequence_point)
1007 verify_sequence_points (expr);
1008 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1009 tf_warning_or_error);
1011 else if (!type_dependent_expression_p (expr))
1012 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1013 tf_warning_or_error);
1014 expr = maybe_cleanup_point_expr_void (expr);
1015 if (check_for_bare_parameter_packs (expr))
1016 expr = error_mark_node;
1017 FOR_EXPR (for_stmt) = expr;
1020 /* Finish the body of a for-statement, which may be given by
1021 FOR_STMT. The increment-EXPR for the loop must be
1022 provided.
1023 It can also finish RANGE_FOR_STMT. */
1025 void
1026 finish_for_stmt (tree for_stmt)
1028 end_maybe_infinite_loop (boolean_true_node);
1030 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1031 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1032 else
1033 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1035 /* Pop the scope for the body of the loop. */
1036 if (flag_new_for_scope > 0)
1038 tree scope;
1039 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1040 ? &RANGE_FOR_SCOPE (for_stmt)
1041 : &FOR_SCOPE (for_stmt));
1042 scope = *scope_ptr;
1043 *scope_ptr = NULL;
1044 add_stmt (do_poplevel (scope));
1048 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1049 SCOPE and INIT should be the return of begin_for_scope,
1050 or both NULL_TREE .
1051 To finish it call finish_for_stmt(). */
1053 tree
1054 begin_range_for_stmt (tree scope, tree init)
1056 tree r;
1058 begin_maybe_infinite_loop (boolean_false_node);
1060 r = build_stmt (input_location, RANGE_FOR_STMT,
1061 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1063 if (scope == NULL_TREE)
1065 gcc_assert (!init || !(flag_new_for_scope > 0));
1066 if (!init)
1067 scope = begin_for_scope (&init);
1070 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1071 pop it now. */
1072 if (init)
1073 pop_stmt_list (init);
1074 RANGE_FOR_SCOPE (r) = scope;
1076 return r;
1079 /* Finish the head of a range-based for statement, which may
1080 be given by RANGE_FOR_STMT. DECL must be the declaration
1081 and EXPR must be the loop expression. */
1083 void
1084 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1086 RANGE_FOR_DECL (range_for_stmt) = decl;
1087 RANGE_FOR_EXPR (range_for_stmt) = expr;
1088 add_stmt (range_for_stmt);
1089 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1092 /* Finish a break-statement. */
1094 tree
1095 finish_break_stmt (void)
1097 /* In switch statements break is sometimes stylistically used after
1098 a return statement. This can lead to spurious warnings about
1099 control reaching the end of a non-void function when it is
1100 inlined. Note that we are calling block_may_fallthru with
1101 language specific tree nodes; this works because
1102 block_may_fallthru returns true when given something it does not
1103 understand. */
1104 if (!block_may_fallthru (cur_stmt_list))
1105 return void_node;
1106 return add_stmt (build_stmt (input_location, BREAK_STMT));
1109 /* Finish a continue-statement. */
1111 tree
1112 finish_continue_stmt (void)
1114 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1117 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1118 appropriate. */
1120 tree
1121 begin_switch_stmt (void)
1123 tree r, scope;
1125 scope = do_pushlevel (sk_cond);
1126 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1128 begin_cond (&SWITCH_STMT_COND (r));
1130 return r;
1133 /* Finish the cond of a switch-statement. */
1135 void
1136 finish_switch_cond (tree cond, tree switch_stmt)
1138 tree orig_type = NULL;
1140 if (check_no_cilk (cond,
1141 "Cilk array notation cannot be used as a condition for switch statement",
1142 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1143 cond = error_mark_node;
1145 if (!processing_template_decl)
1147 /* Convert the condition to an integer or enumeration type. */
1148 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1149 if (cond == NULL_TREE)
1151 error ("switch quantity not an integer");
1152 cond = error_mark_node;
1154 /* We want unlowered type here to handle enum bit-fields. */
1155 orig_type = unlowered_expr_type (cond);
1156 if (cond != error_mark_node)
1158 /* Warn if the condition has boolean value. */
1159 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1160 warning_at (input_location, OPT_Wswitch_bool,
1161 "switch condition has type bool");
1163 /* [stmt.switch]
1165 Integral promotions are performed. */
1166 cond = perform_integral_promotions (cond);
1167 cond = maybe_cleanup_point_expr (cond);
1170 if (check_for_bare_parameter_packs (cond))
1171 cond = error_mark_node;
1172 else if (!processing_template_decl && warn_sequence_point)
1173 verify_sequence_points (cond);
1175 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1176 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1177 add_stmt (switch_stmt);
1178 push_switch (switch_stmt);
1179 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1182 /* Finish the body of a switch-statement, which may be given by
1183 SWITCH_STMT. The COND to switch on is indicated. */
1185 void
1186 finish_switch_stmt (tree switch_stmt)
1188 tree scope;
1190 SWITCH_STMT_BODY (switch_stmt) =
1191 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1192 pop_switch ();
1194 scope = SWITCH_STMT_SCOPE (switch_stmt);
1195 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1196 add_stmt (do_poplevel (scope));
1199 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1200 appropriate. */
1202 tree
1203 begin_try_block (void)
1205 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1206 add_stmt (r);
1207 TRY_STMTS (r) = push_stmt_list ();
1208 return r;
1211 /* Likewise, for a function-try-block. The block returned in
1212 *COMPOUND_STMT is an artificial outer scope, containing the
1213 function-try-block. */
1215 tree
1216 begin_function_try_block (tree *compound_stmt)
1218 tree r;
1219 /* This outer scope does not exist in the C++ standard, but we need
1220 a place to put __FUNCTION__ and similar variables. */
1221 *compound_stmt = begin_compound_stmt (0);
1222 r = begin_try_block ();
1223 FN_TRY_BLOCK_P (r) = 1;
1224 return r;
1227 /* Finish a try-block, which may be given by TRY_BLOCK. */
1229 void
1230 finish_try_block (tree try_block)
1232 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1233 TRY_HANDLERS (try_block) = push_stmt_list ();
1236 /* Finish the body of a cleanup try-block, which may be given by
1237 TRY_BLOCK. */
1239 void
1240 finish_cleanup_try_block (tree try_block)
1242 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1245 /* Finish an implicitly generated try-block, with a cleanup is given
1246 by CLEANUP. */
1248 void
1249 finish_cleanup (tree cleanup, tree try_block)
1251 TRY_HANDLERS (try_block) = cleanup;
1252 CLEANUP_P (try_block) = 1;
1255 /* Likewise, for a function-try-block. */
1257 void
1258 finish_function_try_block (tree try_block)
1260 finish_try_block (try_block);
1261 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1262 the try block, but moving it inside. */
1263 in_function_try_handler = 1;
1266 /* Finish a handler-sequence for a try-block, which may be given by
1267 TRY_BLOCK. */
1269 void
1270 finish_handler_sequence (tree try_block)
1272 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1273 check_handlers (TRY_HANDLERS (try_block));
1276 /* Finish the handler-seq for a function-try-block, given by
1277 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1278 begin_function_try_block. */
1280 void
1281 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1283 in_function_try_handler = 0;
1284 finish_handler_sequence (try_block);
1285 finish_compound_stmt (compound_stmt);
1288 /* Begin a handler. Returns a HANDLER if appropriate. */
1290 tree
1291 begin_handler (void)
1293 tree r;
1295 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1296 add_stmt (r);
1298 /* Create a binding level for the eh_info and the exception object
1299 cleanup. */
1300 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1302 return r;
1305 /* Finish the handler-parameters for a handler, which may be given by
1306 HANDLER. DECL is the declaration for the catch parameter, or NULL
1307 if this is a `catch (...)' clause. */
1309 void
1310 finish_handler_parms (tree decl, tree handler)
1312 tree type = NULL_TREE;
1313 if (processing_template_decl)
1315 if (decl)
1317 decl = pushdecl (decl);
1318 decl = push_template_decl (decl);
1319 HANDLER_PARMS (handler) = decl;
1320 type = TREE_TYPE (decl);
1323 else
1324 type = expand_start_catch_block (decl);
1325 HANDLER_TYPE (handler) = type;
1328 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1329 the return value from the matching call to finish_handler_parms. */
1331 void
1332 finish_handler (tree handler)
1334 if (!processing_template_decl)
1335 expand_end_catch_block ();
1336 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1339 /* Begin a compound statement. FLAGS contains some bits that control the
1340 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1341 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1342 block of a function. If BCS_TRY_BLOCK is set, this is the block
1343 created on behalf of a TRY statement. Returns a token to be passed to
1344 finish_compound_stmt. */
1346 tree
1347 begin_compound_stmt (unsigned int flags)
1349 tree r;
1351 if (flags & BCS_NO_SCOPE)
1353 r = push_stmt_list ();
1354 STATEMENT_LIST_NO_SCOPE (r) = 1;
1356 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1357 But, if it's a statement-expression with a scopeless block, there's
1358 nothing to keep, and we don't want to accidentally keep a block
1359 *inside* the scopeless block. */
1360 keep_next_level (false);
1362 else
1363 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1365 /* When processing a template, we need to remember where the braces were,
1366 so that we can set up identical scopes when instantiating the template
1367 later. BIND_EXPR is a handy candidate for this.
1368 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1369 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1370 processing templates. */
1371 if (processing_template_decl)
1373 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1374 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1375 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1376 TREE_SIDE_EFFECTS (r) = 1;
1379 return r;
1382 /* Finish a compound-statement, which is given by STMT. */
1384 void
1385 finish_compound_stmt (tree stmt)
1387 if (TREE_CODE (stmt) == BIND_EXPR)
1389 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1390 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1391 discard the BIND_EXPR so it can be merged with the containing
1392 STATEMENT_LIST. */
1393 if (TREE_CODE (body) == STATEMENT_LIST
1394 && STATEMENT_LIST_HEAD (body) == NULL
1395 && !BIND_EXPR_BODY_BLOCK (stmt)
1396 && !BIND_EXPR_TRY_BLOCK (stmt))
1397 stmt = body;
1398 else
1399 BIND_EXPR_BODY (stmt) = body;
1401 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1402 stmt = pop_stmt_list (stmt);
1403 else
1405 /* Destroy any ObjC "super" receivers that may have been
1406 created. */
1407 objc_clear_super_receiver ();
1409 stmt = do_poplevel (stmt);
1412 /* ??? See c_end_compound_stmt wrt statement expressions. */
1413 add_stmt (stmt);
1416 /* Finish an asm-statement, whose components are a STRING, some
1417 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1418 LABELS. Also note whether the asm-statement should be
1419 considered volatile. */
1421 tree
1422 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1423 tree input_operands, tree clobbers, tree labels)
1425 tree r;
1426 tree t;
1427 int ninputs = list_length (input_operands);
1428 int noutputs = list_length (output_operands);
1430 if (!processing_template_decl)
1432 const char *constraint;
1433 const char **oconstraints;
1434 bool allows_mem, allows_reg, is_inout;
1435 tree operand;
1436 int i;
1438 oconstraints = XALLOCAVEC (const char *, noutputs);
1440 string = resolve_asm_operand_names (string, output_operands,
1441 input_operands, labels);
1443 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1445 operand = TREE_VALUE (t);
1447 /* ??? Really, this should not be here. Users should be using a
1448 proper lvalue, dammit. But there's a long history of using
1449 casts in the output operands. In cases like longlong.h, this
1450 becomes a primitive form of typechecking -- if the cast can be
1451 removed, then the output operand had a type of the proper width;
1452 otherwise we'll get an error. Gross, but ... */
1453 STRIP_NOPS (operand);
1455 operand = mark_lvalue_use (operand);
1457 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1458 operand = error_mark_node;
1460 if (operand != error_mark_node
1461 && (TREE_READONLY (operand)
1462 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1463 /* Functions are not modifiable, even though they are
1464 lvalues. */
1465 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1466 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1467 /* If it's an aggregate and any field is const, then it is
1468 effectively const. */
1469 || (CLASS_TYPE_P (TREE_TYPE (operand))
1470 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1471 cxx_readonly_error (operand, lv_asm);
1473 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1474 oconstraints[i] = constraint;
1476 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1477 &allows_mem, &allows_reg, &is_inout))
1479 /* If the operand is going to end up in memory,
1480 mark it addressable. */
1481 if (!allows_reg && !cxx_mark_addressable (operand))
1482 operand = error_mark_node;
1484 else
1485 operand = error_mark_node;
1487 TREE_VALUE (t) = operand;
1490 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1492 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1493 bool constraint_parsed
1494 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1495 oconstraints, &allows_mem, &allows_reg);
1496 /* If the operand is going to end up in memory, don't call
1497 decay_conversion. */
1498 if (constraint_parsed && !allows_reg && allows_mem)
1499 operand = mark_lvalue_use (TREE_VALUE (t));
1500 else
1501 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1503 /* If the type of the operand hasn't been determined (e.g.,
1504 because it involves an overloaded function), then issue
1505 an error message. There's no context available to
1506 resolve the overloading. */
1507 if (TREE_TYPE (operand) == unknown_type_node)
1509 error ("type of asm operand %qE could not be determined",
1510 TREE_VALUE (t));
1511 operand = error_mark_node;
1514 if (constraint_parsed)
1516 /* If the operand is going to end up in memory,
1517 mark it addressable. */
1518 if (!allows_reg && allows_mem)
1520 /* Strip the nops as we allow this case. FIXME, this really
1521 should be rejected or made deprecated. */
1522 STRIP_NOPS (operand);
1523 if (!cxx_mark_addressable (operand))
1524 operand = error_mark_node;
1526 else if (!allows_reg && !allows_mem)
1528 /* If constraint allows neither register nor memory,
1529 try harder to get a constant. */
1530 tree constop = maybe_constant_value (operand);
1531 if (TREE_CONSTANT (constop))
1532 operand = constop;
1535 else
1536 operand = error_mark_node;
1538 TREE_VALUE (t) = operand;
1542 r = build_stmt (input_location, ASM_EXPR, string,
1543 output_operands, input_operands,
1544 clobbers, labels);
1545 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1546 r = maybe_cleanup_point_expr_void (r);
1547 return add_stmt (r);
1550 /* Finish a label with the indicated NAME. Returns the new label. */
1552 tree
1553 finish_label_stmt (tree name)
1555 tree decl = define_label (input_location, name);
1557 if (decl == error_mark_node)
1558 return error_mark_node;
1560 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1562 return decl;
1565 /* Finish a series of declarations for local labels. G++ allows users
1566 to declare "local" labels, i.e., labels with scope. This extension
1567 is useful when writing code involving statement-expressions. */
1569 void
1570 finish_label_decl (tree name)
1572 if (!at_function_scope_p ())
1574 error ("__label__ declarations are only allowed in function scopes");
1575 return;
1578 add_decl_expr (declare_local_label (name));
1581 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1583 void
1584 finish_decl_cleanup (tree decl, tree cleanup)
1586 push_cleanup (decl, cleanup, false);
1589 /* If the current scope exits with an exception, run CLEANUP. */
1591 void
1592 finish_eh_cleanup (tree cleanup)
1594 push_cleanup (NULL, cleanup, true);
1597 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1598 order they were written by the user. Each node is as for
1599 emit_mem_initializers. */
1601 void
1602 finish_mem_initializers (tree mem_inits)
1604 /* Reorder the MEM_INITS so that they are in the order they appeared
1605 in the source program. */
1606 mem_inits = nreverse (mem_inits);
1608 if (processing_template_decl)
1610 tree mem;
1612 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1614 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1615 check for bare parameter packs in the TREE_VALUE, because
1616 any parameter packs in the TREE_VALUE have already been
1617 bound as part of the TREE_PURPOSE. See
1618 make_pack_expansion for more information. */
1619 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1620 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1621 TREE_VALUE (mem) = error_mark_node;
1624 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1625 CTOR_INITIALIZER, mem_inits));
1627 else
1628 emit_mem_initializers (mem_inits);
1631 /* Obfuscate EXPR if it looks like an id-expression or member access so
1632 that the call to finish_decltype in do_auto_deduction will give the
1633 right result. */
1635 tree
1636 force_paren_expr (tree expr)
1638 /* This is only needed for decltype(auto) in C++14. */
1639 if (cxx_dialect < cxx14)
1640 return expr;
1642 /* If we're in unevaluated context, we can't be deducing a
1643 return/initializer type, so we don't need to mess with this. */
1644 if (cp_unevaluated_operand)
1645 return expr;
1647 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1648 && TREE_CODE (expr) != SCOPE_REF)
1649 return expr;
1651 if (TREE_CODE (expr) == COMPONENT_REF)
1652 REF_PARENTHESIZED_P (expr) = true;
1653 else if (type_dependent_expression_p (expr))
1654 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1655 else
1657 cp_lvalue_kind kind = lvalue_kind (expr);
1658 if ((kind & ~clk_class) != clk_none)
1660 tree type = unlowered_expr_type (expr);
1661 bool rval = !!(kind & clk_rvalueref);
1662 type = cp_build_reference_type (type, rval);
1663 /* This inhibits warnings in, eg, cxx_mark_addressable
1664 (c++/60955). */
1665 warning_sentinel s (extra_warnings);
1666 expr = build_static_cast (type, expr, tf_error);
1667 if (expr != error_mark_node)
1668 REF_PARENTHESIZED_P (expr) = true;
1672 return expr;
1675 /* Finish a parenthesized expression EXPR. */
1677 tree
1678 finish_parenthesized_expr (tree expr)
1680 if (EXPR_P (expr))
1681 /* This inhibits warnings in c_common_truthvalue_conversion. */
1682 TREE_NO_WARNING (expr) = 1;
1684 if (TREE_CODE (expr) == OFFSET_REF
1685 || TREE_CODE (expr) == SCOPE_REF)
1686 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1687 enclosed in parentheses. */
1688 PTRMEM_OK_P (expr) = 0;
1690 if (TREE_CODE (expr) == STRING_CST)
1691 PAREN_STRING_LITERAL_P (expr) = 1;
1693 expr = force_paren_expr (expr);
1695 return expr;
1698 /* Finish a reference to a non-static data member (DECL) that is not
1699 preceded by `.' or `->'. */
1701 tree
1702 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1704 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1706 if (!object)
1708 tree scope = qualifying_scope;
1709 if (scope == NULL_TREE)
1710 scope = context_for_name_lookup (decl);
1711 object = maybe_dummy_object (scope, NULL);
1714 object = maybe_resolve_dummy (object, true);
1715 if (object == error_mark_node)
1716 return error_mark_node;
1718 /* DR 613/850: Can use non-static data members without an associated
1719 object in sizeof/decltype/alignof. */
1720 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1721 && (!processing_template_decl || !current_class_ref))
1723 if (current_function_decl
1724 && DECL_STATIC_FUNCTION_P (current_function_decl))
1725 error ("invalid use of member %qD in static member function", decl);
1726 else
1727 error ("invalid use of non-static data member %qD", decl);
1728 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1730 return error_mark_node;
1733 if (current_class_ptr)
1734 TREE_USED (current_class_ptr) = 1;
1735 if (processing_template_decl && !qualifying_scope)
1737 tree type = TREE_TYPE (decl);
1739 if (TREE_CODE (type) == REFERENCE_TYPE)
1740 /* Quals on the object don't matter. */;
1741 else if (PACK_EXPANSION_P (type))
1742 /* Don't bother trying to represent this. */
1743 type = NULL_TREE;
1744 else
1746 /* Set the cv qualifiers. */
1747 int quals = cp_type_quals (TREE_TYPE (object));
1749 if (DECL_MUTABLE_P (decl))
1750 quals &= ~TYPE_QUAL_CONST;
1752 quals |= cp_type_quals (TREE_TYPE (decl));
1753 type = cp_build_qualified_type (type, quals);
1756 return (convert_from_reference
1757 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1759 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1760 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1761 for now. */
1762 else if (processing_template_decl)
1763 return build_qualified_name (TREE_TYPE (decl),
1764 qualifying_scope,
1765 decl,
1766 /*template_p=*/false);
1767 else
1769 tree access_type = TREE_TYPE (object);
1771 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1772 decl, tf_warning_or_error);
1774 /* If the data member was named `C::M', convert `*this' to `C'
1775 first. */
1776 if (qualifying_scope)
1778 tree binfo = NULL_TREE;
1779 object = build_scoped_ref (object, qualifying_scope,
1780 &binfo);
1783 return build_class_member_access_expr (object, decl,
1784 /*access_path=*/NULL_TREE,
1785 /*preserve_reference=*/false,
1786 tf_warning_or_error);
1790 /* If we are currently parsing a template and we encountered a typedef
1791 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1792 adds the typedef to a list tied to the current template.
1793 At template instantiation time, that list is walked and access check
1794 performed for each typedef.
1795 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1797 void
1798 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1799 tree context,
1800 location_t location)
1802 tree template_info = NULL;
1803 tree cs = current_scope ();
1805 if (!is_typedef_decl (typedef_decl)
1806 || !context
1807 || !CLASS_TYPE_P (context)
1808 || !cs)
1809 return;
1811 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1812 template_info = get_template_info (cs);
1814 if (template_info
1815 && TI_TEMPLATE (template_info)
1816 && !currently_open_class (context))
1817 append_type_to_template_for_access_check (cs, typedef_decl,
1818 context, location);
1821 /* DECL was the declaration to which a qualified-id resolved. Issue
1822 an error message if it is not accessible. If OBJECT_TYPE is
1823 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1824 type of `*x', or `x', respectively. If the DECL was named as
1825 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1827 void
1828 check_accessibility_of_qualified_id (tree decl,
1829 tree object_type,
1830 tree nested_name_specifier)
1832 tree scope;
1833 tree qualifying_type = NULL_TREE;
1835 /* If we are parsing a template declaration and if decl is a typedef,
1836 add it to a list tied to the template.
1837 At template instantiation time, that list will be walked and
1838 access check performed. */
1839 add_typedef_to_current_template_for_access_check (decl,
1840 nested_name_specifier
1841 ? nested_name_specifier
1842 : DECL_CONTEXT (decl),
1843 input_location);
1845 /* If we're not checking, return immediately. */
1846 if (deferred_access_no_check)
1847 return;
1849 /* Determine the SCOPE of DECL. */
1850 scope = context_for_name_lookup (decl);
1851 /* If the SCOPE is not a type, then DECL is not a member. */
1852 if (!TYPE_P (scope))
1853 return;
1854 /* Compute the scope through which DECL is being accessed. */
1855 if (object_type
1856 /* OBJECT_TYPE might not be a class type; consider:
1858 class A { typedef int I; };
1859 I *p;
1860 p->A::I::~I();
1862 In this case, we will have "A::I" as the DECL, but "I" as the
1863 OBJECT_TYPE. */
1864 && CLASS_TYPE_P (object_type)
1865 && DERIVED_FROM_P (scope, object_type))
1866 /* If we are processing a `->' or `.' expression, use the type of the
1867 left-hand side. */
1868 qualifying_type = object_type;
1869 else if (nested_name_specifier)
1871 /* If the reference is to a non-static member of the
1872 current class, treat it as if it were referenced through
1873 `this'. */
1874 tree ct;
1875 if (DECL_NONSTATIC_MEMBER_P (decl)
1876 && current_class_ptr
1877 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1878 qualifying_type = ct;
1879 /* Otherwise, use the type indicated by the
1880 nested-name-specifier. */
1881 else
1882 qualifying_type = nested_name_specifier;
1884 else
1885 /* Otherwise, the name must be from the current class or one of
1886 its bases. */
1887 qualifying_type = currently_open_derived_class (scope);
1889 if (qualifying_type
1890 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1891 or similar in a default argument value. */
1892 && CLASS_TYPE_P (qualifying_type)
1893 && !dependent_type_p (qualifying_type))
1894 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1895 decl, tf_warning_or_error);
1898 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1899 class named to the left of the "::" operator. DONE is true if this
1900 expression is a complete postfix-expression; it is false if this
1901 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1902 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1903 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1904 is true iff this qualified name appears as a template argument. */
1906 tree
1907 finish_qualified_id_expr (tree qualifying_class,
1908 tree expr,
1909 bool done,
1910 bool address_p,
1911 bool template_p,
1912 bool template_arg_p,
1913 tsubst_flags_t complain)
1915 gcc_assert (TYPE_P (qualifying_class));
1917 if (error_operand_p (expr))
1918 return error_mark_node;
1920 if ((DECL_P (expr) || BASELINK_P (expr))
1921 && !mark_used (expr, complain))
1922 return error_mark_node;
1924 if (template_p)
1925 check_template_keyword (expr);
1927 /* If EXPR occurs as the operand of '&', use special handling that
1928 permits a pointer-to-member. */
1929 if (address_p && done)
1931 if (TREE_CODE (expr) == SCOPE_REF)
1932 expr = TREE_OPERAND (expr, 1);
1933 expr = build_offset_ref (qualifying_class, expr,
1934 /*address_p=*/true, complain);
1935 return expr;
1938 /* No need to check access within an enum. */
1939 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1940 return expr;
1942 /* Within the scope of a class, turn references to non-static
1943 members into expression of the form "this->...". */
1944 if (template_arg_p)
1945 /* But, within a template argument, we do not want make the
1946 transformation, as there is no "this" pointer. */
1948 else if (TREE_CODE (expr) == FIELD_DECL)
1950 push_deferring_access_checks (dk_no_check);
1951 expr = finish_non_static_data_member (expr, NULL_TREE,
1952 qualifying_class);
1953 pop_deferring_access_checks ();
1955 else if (BASELINK_P (expr) && !processing_template_decl)
1957 /* See if any of the functions are non-static members. */
1958 /* If so, the expression may be relative to 'this'. */
1959 if (!shared_member_p (expr)
1960 && current_class_ptr
1961 && DERIVED_FROM_P (qualifying_class,
1962 current_nonlambda_class_type ()))
1963 expr = (build_class_member_access_expr
1964 (maybe_dummy_object (qualifying_class, NULL),
1965 expr,
1966 BASELINK_ACCESS_BINFO (expr),
1967 /*preserve_reference=*/false,
1968 complain));
1969 else if (done)
1970 /* The expression is a qualified name whose address is not
1971 being taken. */
1972 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1973 complain);
1975 else if (BASELINK_P (expr))
1977 else
1979 /* In a template, return a SCOPE_REF for most qualified-ids
1980 so that we can check access at instantiation time. But if
1981 we're looking at a member of the current instantiation, we
1982 know we have access and building up the SCOPE_REF confuses
1983 non-type template argument handling. */
1984 if (processing_template_decl
1985 && !currently_open_class (qualifying_class))
1986 expr = build_qualified_name (TREE_TYPE (expr),
1987 qualifying_class, expr,
1988 template_p);
1990 expr = convert_from_reference (expr);
1993 return expr;
1996 /* Begin a statement-expression. The value returned must be passed to
1997 finish_stmt_expr. */
1999 tree
2000 begin_stmt_expr (void)
2002 return push_stmt_list ();
2005 /* Process the final expression of a statement expression. EXPR can be
2006 NULL, if the final expression is empty. Return a STATEMENT_LIST
2007 containing all the statements in the statement-expression, or
2008 ERROR_MARK_NODE if there was an error. */
2010 tree
2011 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2013 if (error_operand_p (expr))
2015 /* The type of the statement-expression is the type of the last
2016 expression. */
2017 TREE_TYPE (stmt_expr) = error_mark_node;
2018 return error_mark_node;
2021 /* If the last statement does not have "void" type, then the value
2022 of the last statement is the value of the entire expression. */
2023 if (expr)
2025 tree type = TREE_TYPE (expr);
2027 if (processing_template_decl)
2029 expr = build_stmt (input_location, EXPR_STMT, expr);
2030 expr = add_stmt (expr);
2031 /* Mark the last statement so that we can recognize it as such at
2032 template-instantiation time. */
2033 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2035 else if (VOID_TYPE_P (type))
2037 /* Just treat this like an ordinary statement. */
2038 expr = finish_expr_stmt (expr);
2040 else
2042 /* It actually has a value we need to deal with. First, force it
2043 to be an rvalue so that we won't need to build up a copy
2044 constructor call later when we try to assign it to something. */
2045 expr = force_rvalue (expr, tf_warning_or_error);
2046 if (error_operand_p (expr))
2047 return error_mark_node;
2049 /* Update for array-to-pointer decay. */
2050 type = TREE_TYPE (expr);
2052 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2053 normal statement, but don't convert to void or actually add
2054 the EXPR_STMT. */
2055 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2056 expr = maybe_cleanup_point_expr (expr);
2057 add_stmt (expr);
2060 /* The type of the statement-expression is the type of the last
2061 expression. */
2062 TREE_TYPE (stmt_expr) = type;
2065 return stmt_expr;
2068 /* Finish a statement-expression. EXPR should be the value returned
2069 by the previous begin_stmt_expr. Returns an expression
2070 representing the statement-expression. */
2072 tree
2073 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2075 tree type;
2076 tree result;
2078 if (error_operand_p (stmt_expr))
2080 pop_stmt_list (stmt_expr);
2081 return error_mark_node;
2084 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2086 type = TREE_TYPE (stmt_expr);
2087 result = pop_stmt_list (stmt_expr);
2088 TREE_TYPE (result) = type;
2090 if (processing_template_decl)
2092 result = build_min (STMT_EXPR, type, result);
2093 TREE_SIDE_EFFECTS (result) = 1;
2094 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2096 else if (CLASS_TYPE_P (type))
2098 /* Wrap the statement-expression in a TARGET_EXPR so that the
2099 temporary object created by the final expression is destroyed at
2100 the end of the full-expression containing the
2101 statement-expression. */
2102 result = force_target_expr (type, result, tf_warning_or_error);
2105 return result;
2108 /* Returns the expression which provides the value of STMT_EXPR. */
2110 tree
2111 stmt_expr_value_expr (tree stmt_expr)
2113 tree t = STMT_EXPR_STMT (stmt_expr);
2115 if (TREE_CODE (t) == BIND_EXPR)
2116 t = BIND_EXPR_BODY (t);
2118 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2119 t = STATEMENT_LIST_TAIL (t)->stmt;
2121 if (TREE_CODE (t) == EXPR_STMT)
2122 t = EXPR_STMT_EXPR (t);
2124 return t;
2127 /* Return TRUE iff EXPR_STMT is an empty list of
2128 expression statements. */
2130 bool
2131 empty_expr_stmt_p (tree expr_stmt)
2133 tree body = NULL_TREE;
2135 if (expr_stmt == void_node)
2136 return true;
2138 if (expr_stmt)
2140 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2141 body = EXPR_STMT_EXPR (expr_stmt);
2142 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2143 body = expr_stmt;
2146 if (body)
2148 if (TREE_CODE (body) == STATEMENT_LIST)
2149 return tsi_end_p (tsi_start (body));
2150 else
2151 return empty_expr_stmt_p (body);
2153 return false;
2156 /* Perform Koenig lookup. FN is the postfix-expression representing
2157 the function (or functions) to call; ARGS are the arguments to the
2158 call. Returns the functions to be considered by overload resolution. */
2160 tree
2161 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2162 tsubst_flags_t complain)
2164 tree identifier = NULL_TREE;
2165 tree functions = NULL_TREE;
2166 tree tmpl_args = NULL_TREE;
2167 bool template_id = false;
2169 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2171 /* Use a separate flag to handle null args. */
2172 template_id = true;
2173 tmpl_args = TREE_OPERAND (fn, 1);
2174 fn = TREE_OPERAND (fn, 0);
2177 /* Find the name of the overloaded function. */
2178 if (identifier_p (fn))
2179 identifier = fn;
2180 else if (is_overloaded_fn (fn))
2182 functions = fn;
2183 identifier = DECL_NAME (get_first_fn (functions));
2185 else if (DECL_P (fn))
2187 functions = fn;
2188 identifier = DECL_NAME (fn);
2191 /* A call to a namespace-scope function using an unqualified name.
2193 Do Koenig lookup -- unless any of the arguments are
2194 type-dependent. */
2195 if (!any_type_dependent_arguments_p (args)
2196 && !any_dependent_template_arguments_p (tmpl_args))
2198 fn = lookup_arg_dependent (identifier, functions, args);
2199 if (!fn)
2201 /* The unqualified name could not be resolved. */
2202 if (complain)
2203 fn = unqualified_fn_lookup_error (identifier);
2204 else
2205 fn = identifier;
2209 if (fn && template_id)
2210 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2212 return fn;
2215 /* Generate an expression for `FN (ARGS)'. This may change the
2216 contents of ARGS.
2218 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2219 as a virtual call, even if FN is virtual. (This flag is set when
2220 encountering an expression where the function name is explicitly
2221 qualified. For example a call to `X::f' never generates a virtual
2222 call.)
2224 Returns code for the call. */
2226 tree
2227 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2228 bool koenig_p, tsubst_flags_t complain)
2230 tree result;
2231 tree orig_fn;
2232 vec<tree, va_gc> *orig_args = NULL;
2234 if (fn == error_mark_node)
2235 return error_mark_node;
2237 gcc_assert (!TYPE_P (fn));
2239 orig_fn = fn;
2241 if (processing_template_decl)
2243 /* If the call expression is dependent, build a CALL_EXPR node
2244 with no type; type_dependent_expression_p recognizes
2245 expressions with no type as being dependent. */
2246 if (type_dependent_expression_p (fn)
2247 || any_type_dependent_arguments_p (*args)
2248 /* For a non-static member function that doesn't have an
2249 explicit object argument, we need to specifically
2250 test the type dependency of the "this" pointer because it
2251 is not included in *ARGS even though it is considered to
2252 be part of the list of arguments. Note that this is
2253 related to CWG issues 515 and 1005. */
2254 || (TREE_CODE (fn) != COMPONENT_REF
2255 && non_static_member_function_p (fn)
2256 && current_class_ref
2257 && type_dependent_expression_p (current_class_ref)))
2259 result = build_nt_call_vec (fn, *args);
2260 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2261 KOENIG_LOOKUP_P (result) = koenig_p;
2262 if (cfun)
2266 tree fndecl = OVL_CURRENT (fn);
2267 if (TREE_CODE (fndecl) != FUNCTION_DECL
2268 || !TREE_THIS_VOLATILE (fndecl))
2269 break;
2270 fn = OVL_NEXT (fn);
2272 while (fn);
2273 if (!fn)
2274 current_function_returns_abnormally = 1;
2276 return result;
2278 orig_args = make_tree_vector_copy (*args);
2279 if (!BASELINK_P (fn)
2280 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2281 && TREE_TYPE (fn) != unknown_type_node)
2282 fn = build_non_dependent_expr (fn);
2283 make_args_non_dependent (*args);
2286 if (TREE_CODE (fn) == COMPONENT_REF)
2288 tree member = TREE_OPERAND (fn, 1);
2289 if (BASELINK_P (member))
2291 tree object = TREE_OPERAND (fn, 0);
2292 return build_new_method_call (object, member,
2293 args, NULL_TREE,
2294 (disallow_virtual
2295 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2296 : LOOKUP_NORMAL),
2297 /*fn_p=*/NULL,
2298 complain);
2302 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2303 if (TREE_CODE (fn) == ADDR_EXPR
2304 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2305 fn = TREE_OPERAND (fn, 0);
2307 if (is_overloaded_fn (fn))
2308 fn = baselink_for_fns (fn);
2310 result = NULL_TREE;
2311 if (BASELINK_P (fn))
2313 tree object;
2315 /* A call to a member function. From [over.call.func]:
2317 If the keyword this is in scope and refers to the class of
2318 that member function, or a derived class thereof, then the
2319 function call is transformed into a qualified function call
2320 using (*this) as the postfix-expression to the left of the
2321 . operator.... [Otherwise] a contrived object of type T
2322 becomes the implied object argument.
2324 In this situation:
2326 struct A { void f(); };
2327 struct B : public A {};
2328 struct C : public A { void g() { B::f(); }};
2330 "the class of that member function" refers to `A'. But 11.2
2331 [class.access.base] says that we need to convert 'this' to B* as
2332 part of the access, so we pass 'B' to maybe_dummy_object. */
2334 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2335 NULL);
2337 if (processing_template_decl)
2339 if (type_dependent_expression_p (object))
2341 tree ret = build_nt_call_vec (orig_fn, orig_args);
2342 release_tree_vector (orig_args);
2343 return ret;
2345 object = build_non_dependent_expr (object);
2348 result = build_new_method_call (object, fn, args, NULL_TREE,
2349 (disallow_virtual
2350 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2351 : LOOKUP_NORMAL),
2352 /*fn_p=*/NULL,
2353 complain);
2355 else if (is_overloaded_fn (fn))
2357 /* If the function is an overloaded builtin, resolve it. */
2358 if (TREE_CODE (fn) == FUNCTION_DECL
2359 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2360 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2361 result = resolve_overloaded_builtin (input_location, fn, *args);
2363 if (!result)
2365 if (warn_sizeof_pointer_memaccess
2366 && !vec_safe_is_empty (*args)
2367 && !processing_template_decl)
2369 location_t sizeof_arg_loc[3];
2370 tree sizeof_arg[3];
2371 unsigned int i;
2372 for (i = 0; i < 3; i++)
2374 tree t;
2376 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2377 sizeof_arg[i] = NULL_TREE;
2378 if (i >= (*args)->length ())
2379 continue;
2380 t = (**args)[i];
2381 if (TREE_CODE (t) != SIZEOF_EXPR)
2382 continue;
2383 if (SIZEOF_EXPR_TYPE_P (t))
2384 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2385 else
2386 sizeof_arg[i] = TREE_OPERAND (t, 0);
2387 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2389 sizeof_pointer_memaccess_warning
2390 (sizeof_arg_loc, fn, *args,
2391 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2394 /* A call to a namespace-scope function. */
2395 result = build_new_function_call (fn, args, koenig_p, complain);
2398 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2400 if (!vec_safe_is_empty (*args))
2401 error ("arguments to destructor are not allowed");
2402 /* Mark the pseudo-destructor call as having side-effects so
2403 that we do not issue warnings about its use. */
2404 result = build1 (NOP_EXPR,
2405 void_type_node,
2406 TREE_OPERAND (fn, 0));
2407 TREE_SIDE_EFFECTS (result) = 1;
2409 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2410 /* If the "function" is really an object of class type, it might
2411 have an overloaded `operator ()'. */
2412 result = build_op_call (fn, args, complain);
2414 if (!result)
2415 /* A call where the function is unknown. */
2416 result = cp_build_function_call_vec (fn, args, complain);
2418 if (processing_template_decl && result != error_mark_node)
2420 if (INDIRECT_REF_P (result))
2421 result = TREE_OPERAND (result, 0);
2422 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2423 SET_EXPR_LOCATION (result, input_location);
2424 KOENIG_LOOKUP_P (result) = koenig_p;
2425 release_tree_vector (orig_args);
2426 result = convert_from_reference (result);
2429 if (koenig_p)
2431 /* Free garbage OVERLOADs from arg-dependent lookup. */
2432 tree next = NULL_TREE;
2433 for (fn = orig_fn;
2434 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2435 fn = next)
2437 if (processing_template_decl)
2438 /* In a template, we'll re-use them at instantiation time. */
2439 OVL_ARG_DEPENDENT (fn) = false;
2440 else
2442 next = OVL_CHAIN (fn);
2443 ggc_free (fn);
2448 return result;
2451 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
2453 tree
2454 finish_template_variable (tree var)
2456 return instantiate_template (TREE_OPERAND (var, 0), TREE_OPERAND (var, 1),
2457 tf_error);
2460 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2461 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2462 POSTDECREMENT_EXPR.) */
2464 tree
2465 finish_increment_expr (tree expr, enum tree_code code)
2467 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2470 /* Finish a use of `this'. Returns an expression for `this'. */
2472 tree
2473 finish_this_expr (void)
2475 tree result = NULL_TREE;
2477 if (current_class_ptr)
2479 tree type = TREE_TYPE (current_class_ref);
2481 /* In a lambda expression, 'this' refers to the captured 'this'. */
2482 if (LAMBDA_TYPE_P (type))
2483 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2484 else
2485 result = current_class_ptr;
2488 if (result)
2489 /* The keyword 'this' is a prvalue expression. */
2490 return rvalue (result);
2492 tree fn = current_nonlambda_function ();
2493 if (fn && DECL_STATIC_FUNCTION_P (fn))
2494 error ("%<this%> is unavailable for static member functions");
2495 else if (fn)
2496 error ("invalid use of %<this%> in non-member function");
2497 else
2498 error ("invalid use of %<this%> at top level");
2499 return error_mark_node;
2502 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2503 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2504 the TYPE for the type given. If SCOPE is non-NULL, the expression
2505 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2507 tree
2508 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2509 location_t loc)
2511 if (object == error_mark_node || destructor == error_mark_node)
2512 return error_mark_node;
2514 gcc_assert (TYPE_P (destructor));
2516 if (!processing_template_decl)
2518 if (scope == error_mark_node)
2520 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2521 return error_mark_node;
2523 if (is_auto (destructor))
2524 destructor = TREE_TYPE (object);
2525 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2527 error_at (loc,
2528 "qualified type %qT does not match destructor name ~%qT",
2529 scope, destructor);
2530 return error_mark_node;
2534 /* [expr.pseudo] says both:
2536 The type designated by the pseudo-destructor-name shall be
2537 the same as the object type.
2539 and:
2541 The cv-unqualified versions of the object type and of the
2542 type designated by the pseudo-destructor-name shall be the
2543 same type.
2545 We implement the more generous second sentence, since that is
2546 what most other compilers do. */
2547 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2548 destructor))
2550 error_at (loc, "%qE is not of type %qT", object, destructor);
2551 return error_mark_node;
2555 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2556 scope, destructor);
2559 /* Finish an expression of the form CODE EXPR. */
2561 tree
2562 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2563 tsubst_flags_t complain)
2565 tree result = build_x_unary_op (loc, code, expr, complain);
2566 if ((complain & tf_warning)
2567 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2568 overflow_warning (input_location, result);
2570 return result;
2573 /* Finish a compound-literal expression. TYPE is the type to which
2574 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2576 tree
2577 finish_compound_literal (tree type, tree compound_literal,
2578 tsubst_flags_t complain)
2580 if (type == error_mark_node)
2581 return error_mark_node;
2583 if (TREE_CODE (type) == REFERENCE_TYPE)
2585 compound_literal
2586 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2587 complain);
2588 return cp_build_c_cast (type, compound_literal, complain);
2591 if (!TYPE_OBJ_P (type))
2593 if (complain & tf_error)
2594 error ("compound literal of non-object type %qT", type);
2595 return error_mark_node;
2598 if (processing_template_decl)
2600 TREE_TYPE (compound_literal) = type;
2601 /* Mark the expression as a compound literal. */
2602 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2603 return compound_literal;
2606 type = complete_type (type);
2608 if (TYPE_NON_AGGREGATE_CLASS (type))
2610 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2611 everywhere that deals with function arguments would be a pain, so
2612 just wrap it in a TREE_LIST. The parser set a flag so we know
2613 that it came from T{} rather than T({}). */
2614 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2615 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2616 return build_functional_cast (type, compound_literal, complain);
2619 if (TREE_CODE (type) == ARRAY_TYPE
2620 && check_array_initializer (NULL_TREE, type, compound_literal))
2621 return error_mark_node;
2622 compound_literal = reshape_init (type, compound_literal, complain);
2623 if (SCALAR_TYPE_P (type)
2624 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2625 && !check_narrowing (type, compound_literal, complain))
2626 return error_mark_node;
2627 if (TREE_CODE (type) == ARRAY_TYPE
2628 && TYPE_DOMAIN (type) == NULL_TREE)
2630 cp_complete_array_type_or_error (&type, compound_literal,
2631 false, complain);
2632 if (type == error_mark_node)
2633 return error_mark_node;
2635 compound_literal = digest_init (type, compound_literal, complain);
2636 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2637 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2638 /* Put static/constant array temporaries in static variables, but always
2639 represent class temporaries with TARGET_EXPR so we elide copies. */
2640 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2641 && TREE_CODE (type) == ARRAY_TYPE
2642 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2643 && initializer_constant_valid_p (compound_literal, type))
2645 tree decl = create_temporary_var (type);
2646 DECL_INITIAL (decl) = compound_literal;
2647 TREE_STATIC (decl) = 1;
2648 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2650 /* 5.19 says that a constant expression can include an
2651 lvalue-rvalue conversion applied to "a glvalue of literal type
2652 that refers to a non-volatile temporary object initialized
2653 with a constant expression". Rather than try to communicate
2654 that this VAR_DECL is a temporary, just mark it constexpr. */
2655 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2656 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2657 TREE_CONSTANT (decl) = true;
2659 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2660 decl = pushdecl_top_level (decl);
2661 DECL_NAME (decl) = make_anon_name ();
2662 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2663 /* Make sure the destructor is callable. */
2664 tree clean = cxx_maybe_build_cleanup (decl, complain);
2665 if (clean == error_mark_node)
2666 return error_mark_node;
2667 return decl;
2669 else
2670 return get_target_expr_sfinae (compound_literal, complain);
2673 /* Return the declaration for the function-name variable indicated by
2674 ID. */
2676 tree
2677 finish_fname (tree id)
2679 tree decl;
2681 decl = fname_decl (input_location, C_RID_CODE (id), id);
2682 if (processing_template_decl && current_function_decl
2683 && decl != error_mark_node)
2684 decl = DECL_NAME (decl);
2685 return decl;
2688 /* Finish a translation unit. */
2690 void
2691 finish_translation_unit (void)
2693 /* In case there were missing closebraces,
2694 get us back to the global binding level. */
2695 pop_everything ();
2696 while (current_namespace != global_namespace)
2697 pop_namespace ();
2699 /* Do file scope __FUNCTION__ et al. */
2700 finish_fname_decls ();
2703 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2704 Returns the parameter. */
2706 tree
2707 finish_template_type_parm (tree aggr, tree identifier)
2709 if (aggr != class_type_node)
2711 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2712 aggr = class_type_node;
2715 return build_tree_list (aggr, identifier);
2718 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2719 Returns the parameter. */
2721 tree
2722 finish_template_template_parm (tree aggr, tree identifier)
2724 tree decl = build_decl (input_location,
2725 TYPE_DECL, identifier, NULL_TREE);
2726 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2727 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2728 DECL_TEMPLATE_RESULT (tmpl) = decl;
2729 DECL_ARTIFICIAL (decl) = 1;
2730 end_template_decl ();
2732 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2734 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2735 /*is_primary=*/true, /*is_partial=*/false,
2736 /*is_friend=*/0);
2738 return finish_template_type_parm (aggr, tmpl);
2741 /* ARGUMENT is the default-argument value for a template template
2742 parameter. If ARGUMENT is invalid, issue error messages and return
2743 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2745 tree
2746 check_template_template_default_arg (tree argument)
2748 if (TREE_CODE (argument) != TEMPLATE_DECL
2749 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2750 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2752 if (TREE_CODE (argument) == TYPE_DECL)
2753 error ("invalid use of type %qT as a default value for a template "
2754 "template-parameter", TREE_TYPE (argument));
2755 else
2756 error ("invalid default argument for a template template parameter");
2757 return error_mark_node;
2760 return argument;
2763 /* Begin a class definition, as indicated by T. */
2765 tree
2766 begin_class_definition (tree t)
2768 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2769 return error_mark_node;
2771 if (processing_template_parmlist)
2773 error ("definition of %q#T inside template parameter list", t);
2774 return error_mark_node;
2777 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2778 are passed the same as decimal scalar types. */
2779 if (TREE_CODE (t) == RECORD_TYPE
2780 && !processing_template_decl)
2782 tree ns = TYPE_CONTEXT (t);
2783 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2784 && DECL_CONTEXT (ns) == std_node
2785 && DECL_NAME (ns)
2786 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2788 const char *n = TYPE_NAME_STRING (t);
2789 if ((strcmp (n, "decimal32") == 0)
2790 || (strcmp (n, "decimal64") == 0)
2791 || (strcmp (n, "decimal128") == 0))
2792 TYPE_TRANSPARENT_AGGR (t) = 1;
2796 /* A non-implicit typename comes from code like:
2798 template <typename T> struct A {
2799 template <typename U> struct A<T>::B ...
2801 This is erroneous. */
2802 else if (TREE_CODE (t) == TYPENAME_TYPE)
2804 error ("invalid definition of qualified type %qT", t);
2805 t = error_mark_node;
2808 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2810 t = make_class_type (RECORD_TYPE);
2811 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2814 if (TYPE_BEING_DEFINED (t))
2816 t = make_class_type (TREE_CODE (t));
2817 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2819 maybe_process_partial_specialization (t);
2820 pushclass (t);
2821 TYPE_BEING_DEFINED (t) = 1;
2822 class_binding_level->defining_class_p = 1;
2824 if (flag_pack_struct)
2826 tree v;
2827 TYPE_PACKED (t) = 1;
2828 /* Even though the type is being defined for the first time
2829 here, there might have been a forward declaration, so there
2830 might be cv-qualified variants of T. */
2831 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2832 TYPE_PACKED (v) = 1;
2834 /* Reset the interface data, at the earliest possible
2835 moment, as it might have been set via a class foo;
2836 before. */
2837 if (! TYPE_ANONYMOUS_P (t))
2839 struct c_fileinfo *finfo = \
2840 get_fileinfo (LOCATION_FILE (input_location));
2841 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2842 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2843 (t, finfo->interface_unknown);
2845 reset_specialization();
2847 /* Make a declaration for this class in its own scope. */
2848 build_self_reference ();
2850 return t;
2853 /* Finish the member declaration given by DECL. */
2855 void
2856 finish_member_declaration (tree decl)
2858 if (decl == error_mark_node || decl == NULL_TREE)
2859 return;
2861 if (decl == void_type_node)
2862 /* The COMPONENT was a friend, not a member, and so there's
2863 nothing for us to do. */
2864 return;
2866 /* We should see only one DECL at a time. */
2867 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2869 /* Set up access control for DECL. */
2870 TREE_PRIVATE (decl)
2871 = (current_access_specifier == access_private_node);
2872 TREE_PROTECTED (decl)
2873 = (current_access_specifier == access_protected_node);
2874 if (TREE_CODE (decl) == TEMPLATE_DECL)
2876 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2877 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2880 /* Mark the DECL as a member of the current class, unless it's
2881 a member of an enumeration. */
2882 if (TREE_CODE (decl) != CONST_DECL)
2883 DECL_CONTEXT (decl) = current_class_type;
2885 /* Check for bare parameter packs in the member variable declaration. */
2886 if (TREE_CODE (decl) == FIELD_DECL)
2888 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2889 TREE_TYPE (decl) = error_mark_node;
2890 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2891 DECL_ATTRIBUTES (decl) = NULL_TREE;
2894 /* [dcl.link]
2896 A C language linkage is ignored for the names of class members
2897 and the member function type of class member functions. */
2898 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2899 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2901 /* Put functions on the TYPE_METHODS list and everything else on the
2902 TYPE_FIELDS list. Note that these are built up in reverse order.
2903 We reverse them (to obtain declaration order) in finish_struct. */
2904 if (DECL_DECLARES_FUNCTION_P (decl))
2906 /* We also need to add this function to the
2907 CLASSTYPE_METHOD_VEC. */
2908 if (add_method (current_class_type, decl, NULL_TREE))
2910 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2911 TYPE_METHODS (current_class_type) = decl;
2913 maybe_add_class_template_decl_list (current_class_type, decl,
2914 /*friend_p=*/0);
2917 /* Enter the DECL into the scope of the class, if the class
2918 isn't a closure (whose fields are supposed to be unnamed). */
2919 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2920 || pushdecl_class_level (decl))
2922 if (TREE_CODE (decl) == USING_DECL)
2924 /* For now, ignore class-scope USING_DECLS, so that
2925 debugging backends do not see them. */
2926 DECL_IGNORED_P (decl) = 1;
2929 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2930 go at the beginning. The reason is that lookup_field_1
2931 searches the list in order, and we want a field name to
2932 override a type name so that the "struct stat hack" will
2933 work. In particular:
2935 struct S { enum E { }; int E } s;
2936 s.E = 3;
2938 is valid. In addition, the FIELD_DECLs must be maintained in
2939 declaration order so that class layout works as expected.
2940 However, we don't need that order until class layout, so we
2941 save a little time by putting FIELD_DECLs on in reverse order
2942 here, and then reversing them in finish_struct_1. (We could
2943 also keep a pointer to the correct insertion points in the
2944 list.) */
2946 if (TREE_CODE (decl) == TYPE_DECL)
2947 TYPE_FIELDS (current_class_type)
2948 = chainon (TYPE_FIELDS (current_class_type), decl);
2949 else
2951 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2952 TYPE_FIELDS (current_class_type) = decl;
2955 maybe_add_class_template_decl_list (current_class_type, decl,
2956 /*friend_p=*/0);
2959 if (pch_file)
2960 note_decl_for_pch (decl);
2963 /* DECL has been declared while we are building a PCH file. Perform
2964 actions that we might normally undertake lazily, but which can be
2965 performed now so that they do not have to be performed in
2966 translation units which include the PCH file. */
2968 void
2969 note_decl_for_pch (tree decl)
2971 gcc_assert (pch_file);
2973 /* There's a good chance that we'll have to mangle names at some
2974 point, even if only for emission in debugging information. */
2975 if (VAR_OR_FUNCTION_DECL_P (decl)
2976 && !processing_template_decl)
2977 mangle_decl (decl);
2980 /* Finish processing a complete template declaration. The PARMS are
2981 the template parameters. */
2983 void
2984 finish_template_decl (tree parms)
2986 if (parms)
2987 end_template_decl ();
2988 else
2989 end_specialization ();
2992 /* Finish processing a template-id (which names a type) of the form
2993 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2994 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2995 the scope of template-id indicated. */
2997 tree
2998 finish_template_type (tree name, tree args, int entering_scope)
3000 tree type;
3002 type = lookup_template_class (name, args,
3003 NULL_TREE, NULL_TREE, entering_scope,
3004 tf_warning_or_error | tf_user);
3005 if (type == error_mark_node)
3006 return type;
3007 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3008 return TYPE_STUB_DECL (type);
3009 else
3010 return TYPE_NAME (type);
3013 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3014 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3015 BASE_CLASS, or NULL_TREE if an error occurred. The
3016 ACCESS_SPECIFIER is one of
3017 access_{default,public,protected_private}_node. For a virtual base
3018 we set TREE_TYPE. */
3020 tree
3021 finish_base_specifier (tree base, tree access, bool virtual_p)
3023 tree result;
3025 if (base == error_mark_node)
3027 error ("invalid base-class specification");
3028 result = NULL_TREE;
3030 else if (! MAYBE_CLASS_TYPE_P (base))
3032 error ("%qT is not a class type", base);
3033 result = NULL_TREE;
3035 else
3037 if (cp_type_quals (base) != 0)
3039 /* DR 484: Can a base-specifier name a cv-qualified
3040 class type? */
3041 base = TYPE_MAIN_VARIANT (base);
3043 result = build_tree_list (access, base);
3044 if (virtual_p)
3045 TREE_TYPE (result) = integer_type_node;
3048 return result;
3051 /* If FNS is a member function, a set of member functions, or a
3052 template-id referring to one or more member functions, return a
3053 BASELINK for FNS, incorporating the current access context.
3054 Otherwise, return FNS unchanged. */
3056 tree
3057 baselink_for_fns (tree fns)
3059 tree scope;
3060 tree cl;
3062 if (BASELINK_P (fns)
3063 || error_operand_p (fns))
3064 return fns;
3066 scope = ovl_scope (fns);
3067 if (!CLASS_TYPE_P (scope))
3068 return fns;
3070 cl = currently_open_derived_class (scope);
3071 if (!cl)
3072 cl = scope;
3073 cl = TYPE_BINFO (cl);
3074 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3077 /* Returns true iff DECL is a variable from a function outside
3078 the current one. */
3080 static bool
3081 outer_var_p (tree decl)
3083 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3084 && DECL_FUNCTION_SCOPE_P (decl)
3085 && (DECL_CONTEXT (decl) != current_function_decl
3086 || parsing_nsdmi ()));
3089 /* As above, but also checks that DECL is automatic. */
3091 bool
3092 outer_automatic_var_p (tree decl)
3094 return (outer_var_p (decl)
3095 && !TREE_STATIC (decl));
3098 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3099 rewrite it for lambda capture. */
3101 tree
3102 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3104 if (cp_unevaluated_operand)
3105 /* It's not a use (3.2) if we're in an unevaluated context. */
3106 return decl;
3108 tree context = DECL_CONTEXT (decl);
3109 tree containing_function = current_function_decl;
3110 tree lambda_stack = NULL_TREE;
3111 tree lambda_expr = NULL_TREE;
3112 tree initializer = convert_from_reference (decl);
3114 /* Mark it as used now even if the use is ill-formed. */
3115 mark_used (decl);
3117 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3118 support for an approach in which a reference to a local
3119 [constant] automatic variable in a nested class or lambda body
3120 would enter the expression as an rvalue, which would reduce
3121 the complexity of the problem"
3123 FIXME update for final resolution of core issue 696. */
3124 if (decl_maybe_constant_var_p (decl))
3126 if (processing_template_decl)
3127 /* In a template, the constant value may not be in a usable
3128 form, so wait until instantiation time. */
3129 return decl;
3130 else if (decl_constant_var_p (decl))
3131 return scalar_constant_value (decl);
3134 if (parsing_nsdmi ())
3135 containing_function = NULL_TREE;
3136 else
3137 /* If we are in a lambda function, we can move out until we hit
3138 1. the context,
3139 2. a non-lambda function, or
3140 3. a non-default capturing lambda function. */
3141 while (context != containing_function
3142 && LAMBDA_FUNCTION_P (containing_function))
3144 tree closure = DECL_CONTEXT (containing_function);
3145 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3147 if (TYPE_CLASS_SCOPE_P (closure))
3148 /* A lambda in an NSDMI (c++/64496). */
3149 break;
3151 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3152 == CPLD_NONE)
3153 break;
3155 lambda_stack = tree_cons (NULL_TREE,
3156 lambda_expr,
3157 lambda_stack);
3159 containing_function
3160 = decl_function_context (containing_function);
3163 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3164 && DECL_ANON_UNION_VAR_P (decl))
3166 if (complain & tf_error)
3167 error ("cannot capture member %qD of anonymous union", decl);
3168 return error_mark_node;
3170 if (context == containing_function)
3172 decl = add_default_capture (lambda_stack,
3173 /*id=*/DECL_NAME (decl),
3174 initializer);
3176 else if (lambda_expr)
3178 if (complain & tf_error)
3180 error ("%qD is not captured", decl);
3181 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3182 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3183 == CPLD_NONE)
3184 inform (location_of (closure),
3185 "the lambda has no capture-default");
3186 else if (TYPE_CLASS_SCOPE_P (closure))
3187 inform (0, "lambda in local class %q+T cannot "
3188 "capture variables from the enclosing context",
3189 TYPE_CONTEXT (closure));
3190 inform (input_location, "%q+#D declared here", decl);
3192 return error_mark_node;
3194 else
3196 if (complain & tf_error)
3197 error (VAR_P (decl)
3198 ? G_("use of local variable with automatic storage from containing function")
3199 : G_("use of parameter from containing function"));
3200 inform (input_location, "%q+#D declared here", decl);
3201 return error_mark_node;
3203 return decl;
3206 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3207 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3208 if non-NULL, is the type or namespace used to explicitly qualify
3209 ID_EXPRESSION. DECL is the entity to which that name has been
3210 resolved.
3212 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3213 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3214 be set to true if this expression isn't permitted in a
3215 constant-expression, but it is otherwise not set by this function.
3216 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3217 constant-expression, but a non-constant expression is also
3218 permissible.
3220 DONE is true if this expression is a complete postfix-expression;
3221 it is false if this expression is followed by '->', '[', '(', etc.
3222 ADDRESS_P is true iff this expression is the operand of '&'.
3223 TEMPLATE_P is true iff the qualified-id was of the form
3224 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3225 appears as a template argument.
3227 If an error occurs, and it is the kind of error that might cause
3228 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3229 is the caller's responsibility to issue the message. *ERROR_MSG
3230 will be a string with static storage duration, so the caller need
3231 not "free" it.
3233 Return an expression for the entity, after issuing appropriate
3234 diagnostics. This function is also responsible for transforming a
3235 reference to a non-static member into a COMPONENT_REF that makes
3236 the use of "this" explicit.
3238 Upon return, *IDK will be filled in appropriately. */
3239 tree
3240 finish_id_expression (tree id_expression,
3241 tree decl,
3242 tree scope,
3243 cp_id_kind *idk,
3244 bool integral_constant_expression_p,
3245 bool allow_non_integral_constant_expression_p,
3246 bool *non_integral_constant_expression_p,
3247 bool template_p,
3248 bool done,
3249 bool address_p,
3250 bool template_arg_p,
3251 const char **error_msg,
3252 location_t location)
3254 decl = strip_using_decl (decl);
3256 /* Initialize the output parameters. */
3257 *idk = CP_ID_KIND_NONE;
3258 *error_msg = NULL;
3260 if (id_expression == error_mark_node)
3261 return error_mark_node;
3262 /* If we have a template-id, then no further lookup is
3263 required. If the template-id was for a template-class, we
3264 will sometimes have a TYPE_DECL at this point. */
3265 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3266 || TREE_CODE (decl) == TYPE_DECL)
3268 /* Look up the name. */
3269 else
3271 if (decl == error_mark_node)
3273 /* Name lookup failed. */
3274 if (scope
3275 && (!TYPE_P (scope)
3276 || (!dependent_type_p (scope)
3277 && !(identifier_p (id_expression)
3278 && IDENTIFIER_TYPENAME_P (id_expression)
3279 && dependent_type_p (TREE_TYPE (id_expression))))))
3281 /* If the qualifying type is non-dependent (and the name
3282 does not name a conversion operator to a dependent
3283 type), issue an error. */
3284 qualified_name_lookup_error (scope, id_expression, decl, location);
3285 return error_mark_node;
3287 else if (!scope)
3289 /* It may be resolved via Koenig lookup. */
3290 *idk = CP_ID_KIND_UNQUALIFIED;
3291 return id_expression;
3293 else
3294 decl = id_expression;
3296 /* If DECL is a variable that would be out of scope under
3297 ANSI/ISO rules, but in scope in the ARM, name lookup
3298 will succeed. Issue a diagnostic here. */
3299 else
3300 decl = check_for_out_of_scope_variable (decl);
3302 /* Remember that the name was used in the definition of
3303 the current class so that we can check later to see if
3304 the meaning would have been different after the class
3305 was entirely defined. */
3306 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3307 maybe_note_name_used_in_class (id_expression, decl);
3309 /* Disallow uses of local variables from containing functions, except
3310 within lambda-expressions. */
3311 if (outer_automatic_var_p (decl))
3313 decl = process_outer_var_ref (decl, tf_warning_or_error);
3314 if (decl == error_mark_node)
3315 return error_mark_node;
3318 /* Also disallow uses of function parameters outside the function
3319 body, except inside an unevaluated context (i.e. decltype). */
3320 if (TREE_CODE (decl) == PARM_DECL
3321 && DECL_CONTEXT (decl) == NULL_TREE
3322 && !cp_unevaluated_operand)
3324 *error_msg = "use of parameter outside function body";
3325 return error_mark_node;
3329 /* If we didn't find anything, or what we found was a type,
3330 then this wasn't really an id-expression. */
3331 if (TREE_CODE (decl) == TEMPLATE_DECL
3332 && !DECL_FUNCTION_TEMPLATE_P (decl))
3334 *error_msg = "missing template arguments";
3335 return error_mark_node;
3337 else if (TREE_CODE (decl) == TYPE_DECL
3338 || TREE_CODE (decl) == NAMESPACE_DECL)
3340 *error_msg = "expected primary-expression";
3341 return error_mark_node;
3344 /* If the name resolved to a template parameter, there is no
3345 need to look it up again later. */
3346 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3347 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3349 tree r;
3351 *idk = CP_ID_KIND_NONE;
3352 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3353 decl = TEMPLATE_PARM_DECL (decl);
3354 r = convert_from_reference (DECL_INITIAL (decl));
3356 if (integral_constant_expression_p
3357 && !dependent_type_p (TREE_TYPE (decl))
3358 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3360 if (!allow_non_integral_constant_expression_p)
3361 error ("template parameter %qD of type %qT is not allowed in "
3362 "an integral constant expression because it is not of "
3363 "integral or enumeration type", decl, TREE_TYPE (decl));
3364 *non_integral_constant_expression_p = true;
3366 return r;
3368 else
3370 bool dependent_p;
3372 /* If the declaration was explicitly qualified indicate
3373 that. The semantics of `A::f(3)' are different than
3374 `f(3)' if `f' is virtual. */
3375 *idk = (scope
3376 ? CP_ID_KIND_QUALIFIED
3377 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3378 ? CP_ID_KIND_TEMPLATE_ID
3379 : CP_ID_KIND_UNQUALIFIED));
3382 /* [temp.dep.expr]
3384 An id-expression is type-dependent if it contains an
3385 identifier that was declared with a dependent type.
3387 The standard is not very specific about an id-expression that
3388 names a set of overloaded functions. What if some of them
3389 have dependent types and some of them do not? Presumably,
3390 such a name should be treated as a dependent name. */
3391 /* Assume the name is not dependent. */
3392 dependent_p = false;
3393 if (!processing_template_decl)
3394 /* No names are dependent outside a template. */
3396 else if (TREE_CODE (decl) == CONST_DECL)
3397 /* We don't want to treat enumerators as dependent. */
3399 /* A template-id where the name of the template was not resolved
3400 is definitely dependent. */
3401 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3402 && (identifier_p (TREE_OPERAND (decl, 0))))
3403 dependent_p = true;
3404 /* For anything except an overloaded function, just check its
3405 type. */
3406 else if (!is_overloaded_fn (decl))
3407 dependent_p
3408 = dependent_type_p (TREE_TYPE (decl));
3409 /* For a set of overloaded functions, check each of the
3410 functions. */
3411 else
3413 tree fns = decl;
3415 if (BASELINK_P (fns))
3416 fns = BASELINK_FUNCTIONS (fns);
3418 /* For a template-id, check to see if the template
3419 arguments are dependent. */
3420 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3422 tree args = TREE_OPERAND (fns, 1);
3423 dependent_p = any_dependent_template_arguments_p (args);
3424 /* The functions are those referred to by the
3425 template-id. */
3426 fns = TREE_OPERAND (fns, 0);
3429 /* If there are no dependent template arguments, go through
3430 the overloaded functions. */
3431 while (fns && !dependent_p)
3433 tree fn = OVL_CURRENT (fns);
3435 /* Member functions of dependent classes are
3436 dependent. */
3437 if (TREE_CODE (fn) == FUNCTION_DECL
3438 && type_dependent_expression_p (fn))
3439 dependent_p = true;
3440 else if (TREE_CODE (fn) == TEMPLATE_DECL
3441 && dependent_template_p (fn))
3442 dependent_p = true;
3444 fns = OVL_NEXT (fns);
3448 /* If the name was dependent on a template parameter, we will
3449 resolve the name at instantiation time. */
3450 if (dependent_p)
3452 /* Create a SCOPE_REF for qualified names, if the scope is
3453 dependent. */
3454 if (scope)
3456 if (TYPE_P (scope))
3458 if (address_p && done)
3459 decl = finish_qualified_id_expr (scope, decl,
3460 done, address_p,
3461 template_p,
3462 template_arg_p,
3463 tf_warning_or_error);
3464 else
3466 tree type = NULL_TREE;
3467 if (DECL_P (decl) && !dependent_scope_p (scope))
3468 type = TREE_TYPE (decl);
3469 decl = build_qualified_name (type,
3470 scope,
3471 id_expression,
3472 template_p);
3475 if (TREE_TYPE (decl))
3476 decl = convert_from_reference (decl);
3477 return decl;
3479 /* A TEMPLATE_ID already contains all the information we
3480 need. */
3481 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3482 return id_expression;
3483 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3484 /* If we found a variable, then name lookup during the
3485 instantiation will always resolve to the same VAR_DECL
3486 (or an instantiation thereof). */
3487 if (VAR_P (decl)
3488 || TREE_CODE (decl) == PARM_DECL)
3490 mark_used (decl);
3491 return convert_from_reference (decl);
3493 /* The same is true for FIELD_DECL, but we also need to
3494 make sure that the syntax is correct. */
3495 else if (TREE_CODE (decl) == FIELD_DECL)
3497 /* Since SCOPE is NULL here, this is an unqualified name.
3498 Access checking has been performed during name lookup
3499 already. Turn off checking to avoid duplicate errors. */
3500 push_deferring_access_checks (dk_no_check);
3501 decl = finish_non_static_data_member
3502 (decl, NULL_TREE,
3503 /*qualifying_scope=*/NULL_TREE);
3504 pop_deferring_access_checks ();
3505 return decl;
3507 return id_expression;
3510 if (TREE_CODE (decl) == NAMESPACE_DECL)
3512 error ("use of namespace %qD as expression", decl);
3513 return error_mark_node;
3515 else if (DECL_CLASS_TEMPLATE_P (decl))
3517 error ("use of class template %qT as expression", decl);
3518 return error_mark_node;
3520 else if (TREE_CODE (decl) == TREE_LIST)
3522 /* Ambiguous reference to base members. */
3523 error ("request for member %qD is ambiguous in "
3524 "multiple inheritance lattice", id_expression);
3525 print_candidates (decl);
3526 return error_mark_node;
3529 /* Mark variable-like entities as used. Functions are similarly
3530 marked either below or after overload resolution. */
3531 if ((VAR_P (decl)
3532 || TREE_CODE (decl) == PARM_DECL
3533 || TREE_CODE (decl) == CONST_DECL
3534 || TREE_CODE (decl) == RESULT_DECL)
3535 && !mark_used (decl))
3536 return error_mark_node;
3538 /* Only certain kinds of names are allowed in constant
3539 expression. Template parameters have already
3540 been handled above. */
3541 if (! error_operand_p (decl)
3542 && integral_constant_expression_p
3543 && ! decl_constant_var_p (decl)
3544 && TREE_CODE (decl) != CONST_DECL
3545 && ! builtin_valid_in_constant_expr_p (decl))
3547 if (!allow_non_integral_constant_expression_p)
3549 error ("%qD cannot appear in a constant-expression", decl);
3550 return error_mark_node;
3552 *non_integral_constant_expression_p = true;
3555 tree wrap;
3556 if (VAR_P (decl)
3557 && !cp_unevaluated_operand
3558 && !processing_template_decl
3559 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3560 && DECL_THREAD_LOCAL_P (decl)
3561 && (wrap = get_tls_wrapper_fn (decl)))
3563 /* Replace an evaluated use of the thread_local variable with
3564 a call to its wrapper. */
3565 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3567 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3568 && variable_template_p (TREE_OPERAND (decl, 0)))
3570 decl = finish_template_variable (decl);
3571 mark_used (decl);
3573 else if (scope)
3575 decl = (adjust_result_of_qualified_name_lookup
3576 (decl, scope, current_nonlambda_class_type()));
3578 if (TREE_CODE (decl) == FUNCTION_DECL)
3579 mark_used (decl);
3581 if (TYPE_P (scope))
3582 decl = finish_qualified_id_expr (scope,
3583 decl,
3584 done,
3585 address_p,
3586 template_p,
3587 template_arg_p,
3588 tf_warning_or_error);
3589 else
3590 decl = convert_from_reference (decl);
3592 else if (TREE_CODE (decl) == FIELD_DECL)
3594 /* Since SCOPE is NULL here, this is an unqualified name.
3595 Access checking has been performed during name lookup
3596 already. Turn off checking to avoid duplicate errors. */
3597 push_deferring_access_checks (dk_no_check);
3598 decl = finish_non_static_data_member (decl, NULL_TREE,
3599 /*qualifying_scope=*/NULL_TREE);
3600 pop_deferring_access_checks ();
3602 else if (is_overloaded_fn (decl))
3604 tree first_fn;
3606 first_fn = get_first_fn (decl);
3607 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3608 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3610 if (!really_overloaded_fn (decl)
3611 && !mark_used (first_fn))
3612 return error_mark_node;
3614 if (!template_arg_p
3615 && TREE_CODE (first_fn) == FUNCTION_DECL
3616 && DECL_FUNCTION_MEMBER_P (first_fn)
3617 && !shared_member_p (decl))
3619 /* A set of member functions. */
3620 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3621 return finish_class_member_access_expr (decl, id_expression,
3622 /*template_p=*/false,
3623 tf_warning_or_error);
3626 decl = baselink_for_fns (decl);
3628 else
3630 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3631 && DECL_CLASS_SCOPE_P (decl))
3633 tree context = context_for_name_lookup (decl);
3634 if (context != current_class_type)
3636 tree path = currently_open_derived_class (context);
3637 perform_or_defer_access_check (TYPE_BINFO (path),
3638 decl, decl,
3639 tf_warning_or_error);
3643 decl = convert_from_reference (decl);
3647 /* Handle references (c++/56130). */
3648 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3649 if (TREE_DEPRECATED (t))
3650 warn_deprecated_use (t, NULL_TREE);
3652 return decl;
3655 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3656 use as a type-specifier. */
3658 tree
3659 finish_typeof (tree expr)
3661 tree type;
3663 if (type_dependent_expression_p (expr))
3665 type = cxx_make_type (TYPEOF_TYPE);
3666 TYPEOF_TYPE_EXPR (type) = expr;
3667 SET_TYPE_STRUCTURAL_EQUALITY (type);
3669 return type;
3672 expr = mark_type_use (expr);
3674 type = unlowered_expr_type (expr);
3676 if (!type || type == unknown_type_node)
3678 error ("type of %qE is unknown", expr);
3679 return error_mark_node;
3682 return type;
3685 /* Implement the __underlying_type keyword: Return the underlying
3686 type of TYPE, suitable for use as a type-specifier. */
3688 tree
3689 finish_underlying_type (tree type)
3691 tree underlying_type;
3693 if (processing_template_decl)
3695 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3696 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3697 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3699 return underlying_type;
3702 complete_type (type);
3704 if (TREE_CODE (type) != ENUMERAL_TYPE)
3706 error ("%qT is not an enumeration type", type);
3707 return error_mark_node;
3710 underlying_type = ENUM_UNDERLYING_TYPE (type);
3712 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3713 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3714 See finish_enum_value_list for details. */
3715 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3716 underlying_type
3717 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3718 TYPE_UNSIGNED (underlying_type));
3720 return underlying_type;
3723 /* Implement the __direct_bases keyword: Return the direct base classes
3724 of type */
3726 tree
3727 calculate_direct_bases (tree type)
3729 vec<tree, va_gc> *vector = make_tree_vector();
3730 tree bases_vec = NULL_TREE;
3731 vec<tree, va_gc> *base_binfos;
3732 tree binfo;
3733 unsigned i;
3735 complete_type (type);
3737 if (!NON_UNION_CLASS_TYPE_P (type))
3738 return make_tree_vec (0);
3740 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3742 /* Virtual bases are initialized first */
3743 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3745 if (BINFO_VIRTUAL_P (binfo))
3747 vec_safe_push (vector, binfo);
3751 /* Now non-virtuals */
3752 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3754 if (!BINFO_VIRTUAL_P (binfo))
3756 vec_safe_push (vector, binfo);
3761 bases_vec = make_tree_vec (vector->length ());
3763 for (i = 0; i < vector->length (); ++i)
3765 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3767 return bases_vec;
3770 /* Implement the __bases keyword: Return the base classes
3771 of type */
3773 /* Find morally non-virtual base classes by walking binfo hierarchy */
3774 /* Virtual base classes are handled separately in finish_bases */
3776 static tree
3777 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3779 /* Don't walk bases of virtual bases */
3780 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3783 static tree
3784 dfs_calculate_bases_post (tree binfo, void *data_)
3786 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3787 if (!BINFO_VIRTUAL_P (binfo))
3789 vec_safe_push (*data, BINFO_TYPE (binfo));
3791 return NULL_TREE;
3794 /* Calculates the morally non-virtual base classes of a class */
3795 static vec<tree, va_gc> *
3796 calculate_bases_helper (tree type)
3798 vec<tree, va_gc> *vector = make_tree_vector();
3800 /* Now add non-virtual base classes in order of construction */
3801 dfs_walk_all (TYPE_BINFO (type),
3802 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3803 return vector;
3806 tree
3807 calculate_bases (tree type)
3809 vec<tree, va_gc> *vector = make_tree_vector();
3810 tree bases_vec = NULL_TREE;
3811 unsigned i;
3812 vec<tree, va_gc> *vbases;
3813 vec<tree, va_gc> *nonvbases;
3814 tree binfo;
3816 complete_type (type);
3818 if (!NON_UNION_CLASS_TYPE_P (type))
3819 return make_tree_vec (0);
3821 /* First go through virtual base classes */
3822 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3823 vec_safe_iterate (vbases, i, &binfo); i++)
3825 vec<tree, va_gc> *vbase_bases;
3826 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3827 vec_safe_splice (vector, vbase_bases);
3828 release_tree_vector (vbase_bases);
3831 /* Now for the non-virtual bases */
3832 nonvbases = calculate_bases_helper (type);
3833 vec_safe_splice (vector, nonvbases);
3834 release_tree_vector (nonvbases);
3836 /* Last element is entire class, so don't copy */
3837 bases_vec = make_tree_vec (vector->length () - 1);
3839 for (i = 0; i < vector->length () - 1; ++i)
3841 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3843 release_tree_vector (vector);
3844 return bases_vec;
3847 tree
3848 finish_bases (tree type, bool direct)
3850 tree bases = NULL_TREE;
3852 if (!processing_template_decl)
3854 /* Parameter packs can only be used in templates */
3855 error ("Parameter pack __bases only valid in template declaration");
3856 return error_mark_node;
3859 bases = cxx_make_type (BASES);
3860 BASES_TYPE (bases) = type;
3861 BASES_DIRECT (bases) = direct;
3862 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3864 return bases;
3867 /* Perform C++-specific checks for __builtin_offsetof before calling
3868 fold_offsetof. */
3870 tree
3871 finish_offsetof (tree expr, location_t loc)
3873 /* If we're processing a template, we can't finish the semantics yet.
3874 Otherwise we can fold the entire expression now. */
3875 if (processing_template_decl)
3877 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3878 SET_EXPR_LOCATION (expr, loc);
3879 return expr;
3882 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3884 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3885 TREE_OPERAND (expr, 2));
3886 return error_mark_node;
3888 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3889 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3890 || TREE_TYPE (expr) == unknown_type_node)
3892 if (INDIRECT_REF_P (expr))
3893 error ("second operand of %<offsetof%> is neither a single "
3894 "identifier nor a sequence of member accesses and "
3895 "array references");
3896 else
3898 if (TREE_CODE (expr) == COMPONENT_REF
3899 || TREE_CODE (expr) == COMPOUND_EXPR)
3900 expr = TREE_OPERAND (expr, 1);
3901 error ("cannot apply %<offsetof%> to member function %qD", expr);
3903 return error_mark_node;
3905 if (REFERENCE_REF_P (expr))
3906 expr = TREE_OPERAND (expr, 0);
3907 if (TREE_CODE (expr) == COMPONENT_REF)
3909 tree object = TREE_OPERAND (expr, 0);
3910 if (!complete_type_or_else (TREE_TYPE (object), object))
3911 return error_mark_node;
3912 if (warn_invalid_offsetof
3913 && CLASS_TYPE_P (TREE_TYPE (object))
3914 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3915 && cp_unevaluated_operand == 0)
3916 pedwarn (loc, OPT_Winvalid_offsetof,
3917 "offsetof within non-standard-layout type %qT is undefined",
3918 TREE_TYPE (object));
3920 return fold_offsetof (expr);
3923 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3924 function is broken out from the above for the benefit of the tree-ssa
3925 project. */
3927 void
3928 simplify_aggr_init_expr (tree *tp)
3930 tree aggr_init_expr = *tp;
3932 /* Form an appropriate CALL_EXPR. */
3933 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3934 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3935 tree type = TREE_TYPE (slot);
3937 tree call_expr;
3938 enum style_t { ctor, arg, pcc } style;
3940 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3941 style = ctor;
3942 #ifdef PCC_STATIC_STRUCT_RETURN
3943 else if (1)
3944 style = pcc;
3945 #endif
3946 else
3948 gcc_assert (TREE_ADDRESSABLE (type));
3949 style = arg;
3952 call_expr = build_call_array_loc (input_location,
3953 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3955 aggr_init_expr_nargs (aggr_init_expr),
3956 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3957 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3958 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3960 if (style == ctor)
3962 /* Replace the first argument to the ctor with the address of the
3963 slot. */
3964 cxx_mark_addressable (slot);
3965 CALL_EXPR_ARG (call_expr, 0) =
3966 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3968 else if (style == arg)
3970 /* Just mark it addressable here, and leave the rest to
3971 expand_call{,_inline}. */
3972 cxx_mark_addressable (slot);
3973 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3974 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3976 else if (style == pcc)
3978 /* If we're using the non-reentrant PCC calling convention, then we
3979 need to copy the returned value out of the static buffer into the
3980 SLOT. */
3981 push_deferring_access_checks (dk_no_check);
3982 call_expr = build_aggr_init (slot, call_expr,
3983 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3984 tf_warning_or_error);
3985 pop_deferring_access_checks ();
3986 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3989 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3991 tree init = build_zero_init (type, NULL_TREE,
3992 /*static_storage_p=*/false);
3993 init = build2 (INIT_EXPR, void_type_node, slot, init);
3994 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3995 init, call_expr);
3998 *tp = call_expr;
4001 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4003 void
4004 emit_associated_thunks (tree fn)
4006 /* When we use vcall offsets, we emit thunks with the virtual
4007 functions to which they thunk. The whole point of vcall offsets
4008 is so that you can know statically the entire set of thunks that
4009 will ever be needed for a given virtual function, thereby
4010 enabling you to output all the thunks with the function itself. */
4011 if (DECL_VIRTUAL_P (fn)
4012 /* Do not emit thunks for extern template instantiations. */
4013 && ! DECL_REALLY_EXTERN (fn))
4015 tree thunk;
4017 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4019 if (!THUNK_ALIAS (thunk))
4021 use_thunk (thunk, /*emit_p=*/1);
4022 if (DECL_RESULT_THUNK_P (thunk))
4024 tree probe;
4026 for (probe = DECL_THUNKS (thunk);
4027 probe; probe = DECL_CHAIN (probe))
4028 use_thunk (probe, /*emit_p=*/1);
4031 else
4032 gcc_assert (!DECL_THUNKS (thunk));
4037 /* Generate RTL for FN. */
4039 bool
4040 expand_or_defer_fn_1 (tree fn)
4042 /* When the parser calls us after finishing the body of a template
4043 function, we don't really want to expand the body. */
4044 if (processing_template_decl)
4046 /* Normally, collection only occurs in rest_of_compilation. So,
4047 if we don't collect here, we never collect junk generated
4048 during the processing of templates until we hit a
4049 non-template function. It's not safe to do this inside a
4050 nested class, though, as the parser may have local state that
4051 is not a GC root. */
4052 if (!function_depth)
4053 ggc_collect ();
4054 return false;
4057 gcc_assert (DECL_SAVED_TREE (fn));
4059 /* We make a decision about linkage for these functions at the end
4060 of the compilation. Until that point, we do not want the back
4061 end to output them -- but we do want it to see the bodies of
4062 these functions so that it can inline them as appropriate. */
4063 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4065 if (DECL_INTERFACE_KNOWN (fn))
4066 /* We've already made a decision as to how this function will
4067 be handled. */;
4068 else if (!at_eof)
4069 tentative_decl_linkage (fn);
4070 else
4071 import_export_decl (fn);
4073 /* If the user wants us to keep all inline functions, then mark
4074 this function as needed so that finish_file will make sure to
4075 output it later. Similarly, all dllexport'd functions must
4076 be emitted; there may be callers in other DLLs. */
4077 if (DECL_DECLARED_INLINE_P (fn)
4078 && !DECL_REALLY_EXTERN (fn)
4079 && (flag_keep_inline_functions
4080 || (flag_keep_inline_dllexport
4081 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4083 mark_needed (fn);
4084 DECL_EXTERNAL (fn) = 0;
4088 /* If this is a constructor or destructor body, we have to clone
4089 it. */
4090 if (maybe_clone_body (fn))
4092 /* We don't want to process FN again, so pretend we've written
4093 it out, even though we haven't. */
4094 TREE_ASM_WRITTEN (fn) = 1;
4095 /* If this is an instantiation of a constexpr function, keep
4096 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4097 if (!is_instantiation_of_constexpr (fn))
4098 DECL_SAVED_TREE (fn) = NULL_TREE;
4099 return false;
4102 /* There's no reason to do any of the work here if we're only doing
4103 semantic analysis; this code just generates RTL. */
4104 if (flag_syntax_only)
4105 return false;
4107 return true;
4110 void
4111 expand_or_defer_fn (tree fn)
4113 if (expand_or_defer_fn_1 (fn))
4115 function_depth++;
4117 /* Expand or defer, at the whim of the compilation unit manager. */
4118 cgraph_node::finalize_function (fn, function_depth > 1);
4119 emit_associated_thunks (fn);
4121 function_depth--;
4125 struct nrv_data
4127 nrv_data () : visited (37) {}
4129 tree var;
4130 tree result;
4131 hash_table<pointer_hash <tree_node> > visited;
4134 /* Helper function for walk_tree, used by finalize_nrv below. */
4136 static tree
4137 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4139 struct nrv_data *dp = (struct nrv_data *)data;
4140 tree_node **slot;
4142 /* No need to walk into types. There wouldn't be any need to walk into
4143 non-statements, except that we have to consider STMT_EXPRs. */
4144 if (TYPE_P (*tp))
4145 *walk_subtrees = 0;
4146 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4147 but differs from using NULL_TREE in that it indicates that we care
4148 about the value of the RESULT_DECL. */
4149 else if (TREE_CODE (*tp) == RETURN_EXPR)
4150 TREE_OPERAND (*tp, 0) = dp->result;
4151 /* Change all cleanups for the NRV to only run when an exception is
4152 thrown. */
4153 else if (TREE_CODE (*tp) == CLEANUP_STMT
4154 && CLEANUP_DECL (*tp) == dp->var)
4155 CLEANUP_EH_ONLY (*tp) = 1;
4156 /* Replace the DECL_EXPR for the NRV with an initialization of the
4157 RESULT_DECL, if needed. */
4158 else if (TREE_CODE (*tp) == DECL_EXPR
4159 && DECL_EXPR_DECL (*tp) == dp->var)
4161 tree init;
4162 if (DECL_INITIAL (dp->var)
4163 && DECL_INITIAL (dp->var) != error_mark_node)
4164 init = build2 (INIT_EXPR, void_type_node, dp->result,
4165 DECL_INITIAL (dp->var));
4166 else
4167 init = build_empty_stmt (EXPR_LOCATION (*tp));
4168 DECL_INITIAL (dp->var) = NULL_TREE;
4169 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4170 *tp = init;
4172 /* And replace all uses of the NRV with the RESULT_DECL. */
4173 else if (*tp == dp->var)
4174 *tp = dp->result;
4176 /* Avoid walking into the same tree more than once. Unfortunately, we
4177 can't just use walk_tree_without duplicates because it would only call
4178 us for the first occurrence of dp->var in the function body. */
4179 slot = dp->visited.find_slot (*tp, INSERT);
4180 if (*slot)
4181 *walk_subtrees = 0;
4182 else
4183 *slot = *tp;
4185 /* Keep iterating. */
4186 return NULL_TREE;
4189 /* Called from finish_function to implement the named return value
4190 optimization by overriding all the RETURN_EXPRs and pertinent
4191 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4192 RESULT_DECL for the function. */
4194 void
4195 finalize_nrv (tree *tp, tree var, tree result)
4197 struct nrv_data data;
4199 /* Copy name from VAR to RESULT. */
4200 DECL_NAME (result) = DECL_NAME (var);
4201 /* Don't forget that we take its address. */
4202 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4203 /* Finally set DECL_VALUE_EXPR to avoid assigning
4204 a stack slot at -O0 for the original var and debug info
4205 uses RESULT location for VAR. */
4206 SET_DECL_VALUE_EXPR (var, result);
4207 DECL_HAS_VALUE_EXPR_P (var) = 1;
4209 data.var = var;
4210 data.result = result;
4211 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4214 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4216 bool
4217 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4218 bool need_copy_ctor, bool need_copy_assignment,
4219 bool need_dtor)
4221 int save_errorcount = errorcount;
4222 tree info, t;
4224 /* Always allocate 3 elements for simplicity. These are the
4225 function decls for the ctor, dtor, and assignment op.
4226 This layout is known to the three lang hooks,
4227 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4228 and cxx_omp_clause_assign_op. */
4229 info = make_tree_vec (3);
4230 CP_OMP_CLAUSE_INFO (c) = info;
4232 if (need_default_ctor || need_copy_ctor)
4234 if (need_default_ctor)
4235 t = get_default_ctor (type);
4236 else
4237 t = get_copy_ctor (type, tf_warning_or_error);
4239 if (t && !trivial_fn_p (t))
4240 TREE_VEC_ELT (info, 0) = t;
4243 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4244 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4246 if (need_copy_assignment)
4248 t = get_copy_assign (type);
4250 if (t && !trivial_fn_p (t))
4251 TREE_VEC_ELT (info, 2) = t;
4254 return errorcount != save_errorcount;
4257 /* Helper function for handle_omp_array_sections. Called recursively
4258 to handle multiple array-section-subscripts. C is the clause,
4259 T current expression (initially OMP_CLAUSE_DECL), which is either
4260 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4261 expression if specified, TREE_VALUE length expression if specified,
4262 TREE_CHAIN is what it has been specified after, or some decl.
4263 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4264 set to true if any of the array-section-subscript could have length
4265 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4266 first array-section-subscript which is known not to have length
4267 of one. Given say:
4268 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4269 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4270 all are or may have length of 1, array-section-subscript [:2] is the
4271 first one knonwn not to have length 1. For array-section-subscript
4272 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4273 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4274 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4275 case though, as some lengths could be zero. */
4277 static tree
4278 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4279 bool &maybe_zero_len, unsigned int &first_non_one)
4281 tree ret, low_bound, length, type;
4282 if (TREE_CODE (t) != TREE_LIST)
4284 if (error_operand_p (t))
4285 return error_mark_node;
4286 if (type_dependent_expression_p (t))
4287 return NULL_TREE;
4288 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4290 if (processing_template_decl)
4291 return NULL_TREE;
4292 if (DECL_P (t))
4293 error_at (OMP_CLAUSE_LOCATION (c),
4294 "%qD is not a variable in %qs clause", t,
4295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4296 else
4297 error_at (OMP_CLAUSE_LOCATION (c),
4298 "%qE is not a variable in %qs clause", t,
4299 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4300 return error_mark_node;
4302 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4303 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4305 error_at (OMP_CLAUSE_LOCATION (c),
4306 "%qD is threadprivate variable in %qs clause", t,
4307 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4308 return error_mark_node;
4310 t = convert_from_reference (t);
4311 return t;
4314 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4315 maybe_zero_len, first_non_one);
4316 if (ret == error_mark_node || ret == NULL_TREE)
4317 return ret;
4319 type = TREE_TYPE (ret);
4320 low_bound = TREE_PURPOSE (t);
4321 length = TREE_VALUE (t);
4322 if ((low_bound && type_dependent_expression_p (low_bound))
4323 || (length && type_dependent_expression_p (length)))
4324 return NULL_TREE;
4326 if (low_bound == error_mark_node || length == error_mark_node)
4327 return error_mark_node;
4329 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4331 error_at (OMP_CLAUSE_LOCATION (c),
4332 "low bound %qE of array section does not have integral type",
4333 low_bound);
4334 return error_mark_node;
4336 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4338 error_at (OMP_CLAUSE_LOCATION (c),
4339 "length %qE of array section does not have integral type",
4340 length);
4341 return error_mark_node;
4343 if (low_bound)
4344 low_bound = mark_rvalue_use (low_bound);
4345 if (length)
4346 length = mark_rvalue_use (length);
4347 if (low_bound
4348 && TREE_CODE (low_bound) == INTEGER_CST
4349 && TYPE_PRECISION (TREE_TYPE (low_bound))
4350 > TYPE_PRECISION (sizetype))
4351 low_bound = fold_convert (sizetype, low_bound);
4352 if (length
4353 && TREE_CODE (length) == INTEGER_CST
4354 && TYPE_PRECISION (TREE_TYPE (length))
4355 > TYPE_PRECISION (sizetype))
4356 length = fold_convert (sizetype, length);
4357 if (low_bound == NULL_TREE)
4358 low_bound = integer_zero_node;
4360 if (length != NULL_TREE)
4362 if (!integer_nonzerop (length))
4363 maybe_zero_len = true;
4364 if (first_non_one == types.length ()
4365 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4366 first_non_one++;
4368 if (TREE_CODE (type) == ARRAY_TYPE)
4370 if (length == NULL_TREE
4371 && (TYPE_DOMAIN (type) == NULL_TREE
4372 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4374 error_at (OMP_CLAUSE_LOCATION (c),
4375 "for unknown bound array type length expression must "
4376 "be specified");
4377 return error_mark_node;
4379 if (TREE_CODE (low_bound) == INTEGER_CST
4380 && tree_int_cst_sgn (low_bound) == -1)
4382 error_at (OMP_CLAUSE_LOCATION (c),
4383 "negative low bound in array section in %qs clause",
4384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4385 return error_mark_node;
4387 if (length != NULL_TREE
4388 && TREE_CODE (length) == INTEGER_CST
4389 && tree_int_cst_sgn (length) == -1)
4391 error_at (OMP_CLAUSE_LOCATION (c),
4392 "negative length in array section in %qs clause",
4393 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4394 return error_mark_node;
4396 if (TYPE_DOMAIN (type)
4397 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4398 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4399 == INTEGER_CST)
4401 tree size = size_binop (PLUS_EXPR,
4402 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4403 size_one_node);
4404 if (TREE_CODE (low_bound) == INTEGER_CST)
4406 if (tree_int_cst_lt (size, low_bound))
4408 error_at (OMP_CLAUSE_LOCATION (c),
4409 "low bound %qE above array section size "
4410 "in %qs clause", low_bound,
4411 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4412 return error_mark_node;
4414 if (tree_int_cst_equal (size, low_bound))
4415 maybe_zero_len = true;
4416 else if (length == NULL_TREE
4417 && first_non_one == types.length ()
4418 && tree_int_cst_equal
4419 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4420 low_bound))
4421 first_non_one++;
4423 else if (length == NULL_TREE)
4425 maybe_zero_len = true;
4426 if (first_non_one == types.length ())
4427 first_non_one++;
4429 if (length && TREE_CODE (length) == INTEGER_CST)
4431 if (tree_int_cst_lt (size, length))
4433 error_at (OMP_CLAUSE_LOCATION (c),
4434 "length %qE above array section size "
4435 "in %qs clause", length,
4436 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4437 return error_mark_node;
4439 if (TREE_CODE (low_bound) == INTEGER_CST)
4441 tree lbpluslen
4442 = size_binop (PLUS_EXPR,
4443 fold_convert (sizetype, low_bound),
4444 fold_convert (sizetype, length));
4445 if (TREE_CODE (lbpluslen) == INTEGER_CST
4446 && tree_int_cst_lt (size, lbpluslen))
4448 error_at (OMP_CLAUSE_LOCATION (c),
4449 "high bound %qE above array section size "
4450 "in %qs clause", lbpluslen,
4451 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4452 return error_mark_node;
4457 else if (length == NULL_TREE)
4459 maybe_zero_len = true;
4460 if (first_non_one == types.length ())
4461 first_non_one++;
4464 /* For [lb:] we will need to evaluate lb more than once. */
4465 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4467 tree lb = cp_save_expr (low_bound);
4468 if (lb != low_bound)
4470 TREE_PURPOSE (t) = lb;
4471 low_bound = lb;
4475 else if (TREE_CODE (type) == POINTER_TYPE)
4477 if (length == NULL_TREE)
4479 error_at (OMP_CLAUSE_LOCATION (c),
4480 "for pointer type length expression must be specified");
4481 return error_mark_node;
4483 /* If there is a pointer type anywhere but in the very first
4484 array-section-subscript, the array section can't be contiguous. */
4485 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4486 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4488 error_at (OMP_CLAUSE_LOCATION (c),
4489 "array section is not contiguous in %qs clause",
4490 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4491 return error_mark_node;
4494 else
4496 error_at (OMP_CLAUSE_LOCATION (c),
4497 "%qE does not have pointer or array type", ret);
4498 return error_mark_node;
4500 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4501 types.safe_push (TREE_TYPE (ret));
4502 /* We will need to evaluate lb more than once. */
4503 tree lb = cp_save_expr (low_bound);
4504 if (lb != low_bound)
4506 TREE_PURPOSE (t) = lb;
4507 low_bound = lb;
4509 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4510 return ret;
4513 /* Handle array sections for clause C. */
4515 static bool
4516 handle_omp_array_sections (tree c)
4518 bool maybe_zero_len = false;
4519 unsigned int first_non_one = 0;
4520 auto_vec<tree> types;
4521 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4522 maybe_zero_len, first_non_one);
4523 if (first == error_mark_node)
4524 return true;
4525 if (first == NULL_TREE)
4526 return false;
4527 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4529 tree t = OMP_CLAUSE_DECL (c);
4530 tree tem = NULL_TREE;
4531 if (processing_template_decl)
4532 return false;
4533 /* Need to evaluate side effects in the length expressions
4534 if any. */
4535 while (TREE_CODE (t) == TREE_LIST)
4537 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4539 if (tem == NULL_TREE)
4540 tem = TREE_VALUE (t);
4541 else
4542 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4543 TREE_VALUE (t), tem);
4545 t = TREE_CHAIN (t);
4547 if (tem)
4548 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4549 OMP_CLAUSE_DECL (c) = first;
4551 else
4553 unsigned int num = types.length (), i;
4554 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4555 tree condition = NULL_TREE;
4557 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4558 maybe_zero_len = true;
4559 if (processing_template_decl && maybe_zero_len)
4560 return false;
4562 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4563 t = TREE_CHAIN (t))
4565 tree low_bound = TREE_PURPOSE (t);
4566 tree length = TREE_VALUE (t);
4568 i--;
4569 if (low_bound
4570 && TREE_CODE (low_bound) == INTEGER_CST
4571 && TYPE_PRECISION (TREE_TYPE (low_bound))
4572 > TYPE_PRECISION (sizetype))
4573 low_bound = fold_convert (sizetype, low_bound);
4574 if (length
4575 && TREE_CODE (length) == INTEGER_CST
4576 && TYPE_PRECISION (TREE_TYPE (length))
4577 > TYPE_PRECISION (sizetype))
4578 length = fold_convert (sizetype, length);
4579 if (low_bound == NULL_TREE)
4580 low_bound = integer_zero_node;
4581 if (!maybe_zero_len && i > first_non_one)
4583 if (integer_nonzerop (low_bound))
4584 goto do_warn_noncontiguous;
4585 if (length != NULL_TREE
4586 && TREE_CODE (length) == INTEGER_CST
4587 && TYPE_DOMAIN (types[i])
4588 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4589 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4590 == INTEGER_CST)
4592 tree size;
4593 size = size_binop (PLUS_EXPR,
4594 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4595 size_one_node);
4596 if (!tree_int_cst_equal (length, size))
4598 do_warn_noncontiguous:
4599 error_at (OMP_CLAUSE_LOCATION (c),
4600 "array section is not contiguous in %qs "
4601 "clause",
4602 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4603 return true;
4606 if (!processing_template_decl
4607 && length != NULL_TREE
4608 && TREE_SIDE_EFFECTS (length))
4610 if (side_effects == NULL_TREE)
4611 side_effects = length;
4612 else
4613 side_effects = build2 (COMPOUND_EXPR,
4614 TREE_TYPE (side_effects),
4615 length, side_effects);
4618 else if (processing_template_decl)
4619 continue;
4620 else
4622 tree l;
4624 if (i > first_non_one && length && integer_nonzerop (length))
4625 continue;
4626 if (length)
4627 l = fold_convert (sizetype, length);
4628 else
4630 l = size_binop (PLUS_EXPR,
4631 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4632 size_one_node);
4633 l = size_binop (MINUS_EXPR, l,
4634 fold_convert (sizetype, low_bound));
4636 if (i > first_non_one)
4638 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4639 size_zero_node);
4640 if (condition == NULL_TREE)
4641 condition = l;
4642 else
4643 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4644 l, condition);
4646 else if (size == NULL_TREE)
4648 size = size_in_bytes (TREE_TYPE (types[i]));
4649 size = size_binop (MULT_EXPR, size, l);
4650 if (condition)
4651 size = fold_build3 (COND_EXPR, sizetype, condition,
4652 size, size_zero_node);
4654 else
4655 size = size_binop (MULT_EXPR, size, l);
4658 if (!processing_template_decl)
4660 if (side_effects)
4661 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4662 OMP_CLAUSE_DECL (c) = first;
4663 OMP_CLAUSE_SIZE (c) = size;
4664 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4665 return false;
4666 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4667 OMP_CLAUSE_MAP);
4668 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4669 if (!cxx_mark_addressable (t))
4670 return false;
4671 OMP_CLAUSE_DECL (c2) = t;
4672 t = build_fold_addr_expr (first);
4673 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4674 ptrdiff_type_node, t);
4675 tree ptr = OMP_CLAUSE_DECL (c2);
4676 ptr = convert_from_reference (ptr);
4677 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4678 ptr = build_fold_addr_expr (ptr);
4679 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4680 ptrdiff_type_node, t,
4681 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4682 ptrdiff_type_node, ptr));
4683 OMP_CLAUSE_SIZE (c2) = t;
4684 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4685 OMP_CLAUSE_CHAIN (c) = c2;
4686 ptr = OMP_CLAUSE_DECL (c2);
4687 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4688 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4690 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4691 OMP_CLAUSE_MAP);
4692 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4693 OMP_CLAUSE_DECL (c3) = ptr;
4694 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4695 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4696 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4697 OMP_CLAUSE_CHAIN (c2) = c3;
4701 return false;
4704 /* Return identifier to look up for omp declare reduction. */
4706 tree
4707 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4709 const char *p = NULL;
4710 const char *m = NULL;
4711 switch (reduction_code)
4713 case PLUS_EXPR:
4714 case MULT_EXPR:
4715 case MINUS_EXPR:
4716 case BIT_AND_EXPR:
4717 case BIT_XOR_EXPR:
4718 case BIT_IOR_EXPR:
4719 case TRUTH_ANDIF_EXPR:
4720 case TRUTH_ORIF_EXPR:
4721 reduction_id = ansi_opname (reduction_code);
4722 break;
4723 case MIN_EXPR:
4724 p = "min";
4725 break;
4726 case MAX_EXPR:
4727 p = "max";
4728 break;
4729 default:
4730 break;
4733 if (p == NULL)
4735 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4736 return error_mark_node;
4737 p = IDENTIFIER_POINTER (reduction_id);
4740 if (type != NULL_TREE)
4741 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4743 const char prefix[] = "omp declare reduction ";
4744 size_t lenp = sizeof (prefix);
4745 if (strncmp (p, prefix, lenp - 1) == 0)
4746 lenp = 1;
4747 size_t len = strlen (p);
4748 size_t lenm = m ? strlen (m) + 1 : 0;
4749 char *name = XALLOCAVEC (char, lenp + len + lenm);
4750 if (lenp > 1)
4751 memcpy (name, prefix, lenp - 1);
4752 memcpy (name + lenp - 1, p, len + 1);
4753 if (m)
4755 name[lenp + len - 1] = '~';
4756 memcpy (name + lenp + len, m, lenm);
4758 return get_identifier (name);
4761 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4762 FUNCTION_DECL or NULL_TREE if not found. */
4764 static tree
4765 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4766 vec<tree> *ambiguousp)
4768 tree orig_id = id;
4769 tree baselink = NULL_TREE;
4770 if (identifier_p (id))
4772 cp_id_kind idk;
4773 bool nonint_cst_expression_p;
4774 const char *error_msg;
4775 id = omp_reduction_id (ERROR_MARK, id, type);
4776 tree decl = lookup_name (id);
4777 if (decl == NULL_TREE)
4778 decl = error_mark_node;
4779 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4780 &nonint_cst_expression_p, false, true, false,
4781 false, &error_msg, loc);
4782 if (idk == CP_ID_KIND_UNQUALIFIED
4783 && identifier_p (id))
4785 vec<tree, va_gc> *args = NULL;
4786 vec_safe_push (args, build_reference_type (type));
4787 id = perform_koenig_lookup (id, args, tf_none);
4790 else if (TREE_CODE (id) == SCOPE_REF)
4791 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4792 omp_reduction_id (ERROR_MARK,
4793 TREE_OPERAND (id, 1),
4794 type),
4795 false, false);
4796 tree fns = id;
4797 if (id && is_overloaded_fn (id))
4798 id = get_fns (id);
4799 for (; id; id = OVL_NEXT (id))
4801 tree fndecl = OVL_CURRENT (id);
4802 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4804 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4805 if (same_type_p (TREE_TYPE (argtype), type))
4806 break;
4809 if (id && BASELINK_P (fns))
4811 if (baselinkp)
4812 *baselinkp = fns;
4813 else
4814 baselink = fns;
4816 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4818 vec<tree> ambiguous = vNULL;
4819 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4820 unsigned int ix;
4821 if (ambiguousp == NULL)
4822 ambiguousp = &ambiguous;
4823 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4825 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4826 baselinkp ? baselinkp : &baselink,
4827 ambiguousp);
4828 if (id == NULL_TREE)
4829 continue;
4830 if (!ambiguousp->is_empty ())
4831 ambiguousp->safe_push (id);
4832 else if (ret != NULL_TREE)
4834 ambiguousp->safe_push (ret);
4835 ambiguousp->safe_push (id);
4836 ret = NULL_TREE;
4838 else
4839 ret = id;
4841 if (ambiguousp != &ambiguous)
4842 return ret;
4843 if (!ambiguous.is_empty ())
4845 const char *str = _("candidates are:");
4846 unsigned int idx;
4847 tree udr;
4848 error_at (loc, "user defined reduction lookup is ambiguous");
4849 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4851 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4852 if (idx == 0)
4853 str = get_spaces (str);
4855 ambiguous.release ();
4856 ret = error_mark_node;
4857 baselink = NULL_TREE;
4859 id = ret;
4861 if (id && baselink)
4862 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4863 id, id, tf_warning_or_error);
4864 return id;
4867 /* Helper function for cp_parser_omp_declare_reduction_exprs
4868 and tsubst_omp_udr.
4869 Remove CLEANUP_STMT for data (omp_priv variable).
4870 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4871 DECL_EXPR. */
4873 tree
4874 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4876 if (TYPE_P (*tp))
4877 *walk_subtrees = 0;
4878 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4879 *tp = CLEANUP_BODY (*tp);
4880 else if (TREE_CODE (*tp) == DECL_EXPR)
4882 tree decl = DECL_EXPR_DECL (*tp);
4883 if (!processing_template_decl
4884 && decl == (tree) data
4885 && DECL_INITIAL (decl)
4886 && DECL_INITIAL (decl) != error_mark_node)
4888 tree list = NULL_TREE;
4889 append_to_statement_list_force (*tp, &list);
4890 tree init_expr = build2 (INIT_EXPR, void_type_node,
4891 decl, DECL_INITIAL (decl));
4892 DECL_INITIAL (decl) = NULL_TREE;
4893 append_to_statement_list_force (init_expr, &list);
4894 *tp = list;
4897 return NULL_TREE;
4900 /* Data passed from cp_check_omp_declare_reduction to
4901 cp_check_omp_declare_reduction_r. */
4903 struct cp_check_omp_declare_reduction_data
4905 location_t loc;
4906 tree stmts[7];
4907 bool combiner_p;
4910 /* Helper function for cp_check_omp_declare_reduction, called via
4911 cp_walk_tree. */
4913 static tree
4914 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4916 struct cp_check_omp_declare_reduction_data *udr_data
4917 = (struct cp_check_omp_declare_reduction_data *) data;
4918 if (SSA_VAR_P (*tp)
4919 && !DECL_ARTIFICIAL (*tp)
4920 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4921 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4923 location_t loc = udr_data->loc;
4924 if (udr_data->combiner_p)
4925 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4926 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4927 *tp);
4928 else
4929 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4930 "to variable %qD which is not %<omp_priv%> nor "
4931 "%<omp_orig%>",
4932 *tp);
4933 return *tp;
4935 return NULL_TREE;
4938 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4940 void
4941 cp_check_omp_declare_reduction (tree udr)
4943 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4944 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4945 type = TREE_TYPE (type);
4946 int i;
4947 location_t loc = DECL_SOURCE_LOCATION (udr);
4949 if (type == error_mark_node)
4950 return;
4951 if (ARITHMETIC_TYPE_P (type))
4953 static enum tree_code predef_codes[]
4954 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4955 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4956 for (i = 0; i < 8; i++)
4958 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4959 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4960 const char *n2 = IDENTIFIER_POINTER (id);
4961 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4962 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4963 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4964 break;
4967 if (i == 8
4968 && TREE_CODE (type) != COMPLEX_EXPR)
4970 const char prefix_minmax[] = "omp declare reduction m";
4971 size_t prefix_size = sizeof (prefix_minmax) - 1;
4972 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4973 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4974 prefix_minmax, prefix_size) == 0
4975 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4976 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4977 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4978 i = 0;
4980 if (i < 8)
4982 error_at (loc, "predeclared arithmetic type %qT in "
4983 "%<#pragma omp declare reduction%>", type);
4984 return;
4987 else if (TREE_CODE (type) == FUNCTION_TYPE
4988 || TREE_CODE (type) == METHOD_TYPE
4989 || TREE_CODE (type) == ARRAY_TYPE)
4991 error_at (loc, "function or array type %qT in "
4992 "%<#pragma omp declare reduction%>", type);
4993 return;
4995 else if (TREE_CODE (type) == REFERENCE_TYPE)
4997 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4998 type);
4999 return;
5001 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5003 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5004 "%<#pragma omp declare reduction%>", type);
5005 return;
5008 tree body = DECL_SAVED_TREE (udr);
5009 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5010 return;
5012 tree_stmt_iterator tsi;
5013 struct cp_check_omp_declare_reduction_data data;
5014 memset (data.stmts, 0, sizeof data.stmts);
5015 for (i = 0, tsi = tsi_start (body);
5016 i < 7 && !tsi_end_p (tsi);
5017 i++, tsi_next (&tsi))
5018 data.stmts[i] = tsi_stmt (tsi);
5019 data.loc = loc;
5020 gcc_assert (tsi_end_p (tsi));
5021 if (i >= 3)
5023 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5024 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5025 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5026 return;
5027 data.combiner_p = true;
5028 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5029 &data, NULL))
5030 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5032 if (i >= 6)
5034 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5035 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5036 data.combiner_p = false;
5037 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5038 &data, NULL)
5039 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5040 cp_check_omp_declare_reduction_r, &data, NULL))
5041 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5042 if (i == 7)
5043 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5047 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5048 an inline call. But, remap
5049 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5050 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5052 static tree
5053 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5054 tree decl, tree placeholder)
5056 copy_body_data id;
5057 hash_map<tree, tree> decl_map;
5059 decl_map.put (omp_decl1, placeholder);
5060 decl_map.put (omp_decl2, decl);
5061 memset (&id, 0, sizeof (id));
5062 id.src_fn = DECL_CONTEXT (omp_decl1);
5063 id.dst_fn = current_function_decl;
5064 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5065 id.decl_map = &decl_map;
5067 id.copy_decl = copy_decl_no_change;
5068 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5069 id.transform_new_cfg = true;
5070 id.transform_return_to_modify = false;
5071 id.transform_lang_insert_block = NULL;
5072 id.eh_lp_nr = 0;
5073 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5074 return stmt;
5077 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5078 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5080 static tree
5081 find_omp_placeholder_r (tree *tp, int *, void *data)
5083 if (*tp == (tree) data)
5084 return *tp;
5085 return NULL_TREE;
5088 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5089 Return true if there is some error and the clause should be removed. */
5091 static bool
5092 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5094 tree t = OMP_CLAUSE_DECL (c);
5095 bool predefined = false;
5096 tree type = TREE_TYPE (t);
5097 if (TREE_CODE (type) == REFERENCE_TYPE)
5098 type = TREE_TYPE (type);
5099 if (type == error_mark_node)
5100 return true;
5101 else if (ARITHMETIC_TYPE_P (type))
5102 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5104 case PLUS_EXPR:
5105 case MULT_EXPR:
5106 case MINUS_EXPR:
5107 predefined = true;
5108 break;
5109 case MIN_EXPR:
5110 case MAX_EXPR:
5111 if (TREE_CODE (type) == COMPLEX_TYPE)
5112 break;
5113 predefined = true;
5114 break;
5115 case BIT_AND_EXPR:
5116 case BIT_IOR_EXPR:
5117 case BIT_XOR_EXPR:
5118 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5119 break;
5120 predefined = true;
5121 break;
5122 case TRUTH_ANDIF_EXPR:
5123 case TRUTH_ORIF_EXPR:
5124 if (FLOAT_TYPE_P (type))
5125 break;
5126 predefined = true;
5127 break;
5128 default:
5129 break;
5131 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5133 error ("%qE has invalid type for %<reduction%>", t);
5134 return true;
5136 else if (!processing_template_decl)
5138 t = require_complete_type (t);
5139 if (t == error_mark_node)
5140 return true;
5141 OMP_CLAUSE_DECL (c) = t;
5144 if (predefined)
5146 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5147 return false;
5149 else if (processing_template_decl)
5150 return false;
5152 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5154 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5155 if (TREE_CODE (type) == REFERENCE_TYPE)
5156 type = TREE_TYPE (type);
5157 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5158 if (id == NULL_TREE)
5159 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5160 NULL_TREE, NULL_TREE);
5161 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5162 if (id)
5164 if (id == error_mark_node)
5165 return true;
5166 id = OVL_CURRENT (id);
5167 mark_used (id);
5168 tree body = DECL_SAVED_TREE (id);
5169 if (!body)
5170 return true;
5171 if (TREE_CODE (body) == STATEMENT_LIST)
5173 tree_stmt_iterator tsi;
5174 tree placeholder = NULL_TREE;
5175 int i;
5176 tree stmts[7];
5177 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5178 atype = TREE_TYPE (atype);
5179 bool need_static_cast = !same_type_p (type, atype);
5180 memset (stmts, 0, sizeof stmts);
5181 for (i = 0, tsi = tsi_start (body);
5182 i < 7 && !tsi_end_p (tsi);
5183 i++, tsi_next (&tsi))
5184 stmts[i] = tsi_stmt (tsi);
5185 gcc_assert (tsi_end_p (tsi));
5187 if (i >= 3)
5189 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5190 && TREE_CODE (stmts[1]) == DECL_EXPR);
5191 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5192 DECL_ARTIFICIAL (placeholder) = 1;
5193 DECL_IGNORED_P (placeholder) = 1;
5194 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5195 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5196 cxx_mark_addressable (placeholder);
5197 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5198 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5199 != REFERENCE_TYPE)
5200 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5201 tree omp_out = placeholder;
5202 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5203 if (need_static_cast)
5205 tree rtype = build_reference_type (atype);
5206 omp_out = build_static_cast (rtype, omp_out,
5207 tf_warning_or_error);
5208 omp_in = build_static_cast (rtype, omp_in,
5209 tf_warning_or_error);
5210 if (omp_out == error_mark_node || omp_in == error_mark_node)
5211 return true;
5212 omp_out = convert_from_reference (omp_out);
5213 omp_in = convert_from_reference (omp_in);
5215 OMP_CLAUSE_REDUCTION_MERGE (c)
5216 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5217 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5219 if (i >= 6)
5221 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5222 && TREE_CODE (stmts[4]) == DECL_EXPR);
5223 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5224 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5225 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5226 cxx_mark_addressable (placeholder);
5227 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5228 tree omp_orig = placeholder;
5229 if (need_static_cast)
5231 if (i == 7)
5233 error_at (OMP_CLAUSE_LOCATION (c),
5234 "user defined reduction with constructor "
5235 "initializer for base class %qT", atype);
5236 return true;
5238 tree rtype = build_reference_type (atype);
5239 omp_priv = build_static_cast (rtype, omp_priv,
5240 tf_warning_or_error);
5241 omp_orig = build_static_cast (rtype, omp_orig,
5242 tf_warning_or_error);
5243 if (omp_priv == error_mark_node
5244 || omp_orig == error_mark_node)
5245 return true;
5246 omp_priv = convert_from_reference (omp_priv);
5247 omp_orig = convert_from_reference (omp_orig);
5249 if (i == 6)
5250 *need_default_ctor = true;
5251 OMP_CLAUSE_REDUCTION_INIT (c)
5252 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5253 DECL_EXPR_DECL (stmts[3]),
5254 omp_priv, omp_orig);
5255 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5256 find_omp_placeholder_r, placeholder, NULL))
5257 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5259 else if (i >= 3)
5261 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5262 *need_default_ctor = true;
5263 else
5265 tree init;
5266 tree v = convert_from_reference (t);
5267 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5268 init = build_constructor (TREE_TYPE (v), NULL);
5269 else
5270 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5271 OMP_CLAUSE_REDUCTION_INIT (c)
5272 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5277 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5278 *need_dtor = true;
5279 else
5281 error ("user defined reduction not found for %qD", t);
5282 return true;
5284 return false;
5287 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5288 Remove any elements from the list that are invalid. */
5290 tree
5291 finish_omp_clauses (tree clauses)
5293 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5294 bitmap_head aligned_head;
5295 tree c, t, *pc;
5296 bool branch_seen = false;
5297 bool copyprivate_seen = false;
5299 bitmap_obstack_initialize (NULL);
5300 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5301 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5302 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5303 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5305 for (pc = &clauses, c = clauses; c ; c = *pc)
5307 bool remove = false;
5309 switch (OMP_CLAUSE_CODE (c))
5311 case OMP_CLAUSE_SHARED:
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_PRIVATE:
5314 goto check_dup_generic;
5315 case OMP_CLAUSE_REDUCTION:
5316 goto check_dup_generic;
5317 case OMP_CLAUSE_COPYPRIVATE:
5318 copyprivate_seen = true;
5319 goto check_dup_generic;
5320 case OMP_CLAUSE_COPYIN:
5321 goto check_dup_generic;
5322 case OMP_CLAUSE_LINEAR:
5323 t = OMP_CLAUSE_DECL (c);
5324 if (!type_dependent_expression_p (t)
5325 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5326 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5328 error ("linear clause applied to non-integral non-pointer "
5329 "variable with %qT type", TREE_TYPE (t));
5330 remove = true;
5331 break;
5333 t = OMP_CLAUSE_LINEAR_STEP (c);
5334 if (t == NULL_TREE)
5335 t = integer_one_node;
5336 if (t == error_mark_node)
5338 remove = true;
5339 break;
5341 else if (!type_dependent_expression_p (t)
5342 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5344 error ("linear step expression must be integral");
5345 remove = true;
5346 break;
5348 else
5350 t = mark_rvalue_use (t);
5351 if (!processing_template_decl)
5353 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5354 t = maybe_constant_value (t);
5355 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5356 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5357 == POINTER_TYPE)
5359 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5360 OMP_CLAUSE_DECL (c), t);
5361 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5362 MINUS_EXPR, sizetype, t,
5363 OMP_CLAUSE_DECL (c));
5364 if (t == error_mark_node)
5366 remove = true;
5367 break;
5370 else
5371 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5373 OMP_CLAUSE_LINEAR_STEP (c) = t;
5375 goto check_dup_generic;
5376 check_dup_generic:
5377 t = OMP_CLAUSE_DECL (c);
5378 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5380 if (processing_template_decl)
5381 break;
5382 if (DECL_P (t))
5383 error ("%qD is not a variable in clause %qs", t,
5384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5385 else
5386 error ("%qE is not a variable in clause %qs", t,
5387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5388 remove = true;
5390 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5391 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5392 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5394 error ("%qD appears more than once in data clauses", t);
5395 remove = true;
5397 else
5398 bitmap_set_bit (&generic_head, DECL_UID (t));
5399 break;
5401 case OMP_CLAUSE_FIRSTPRIVATE:
5402 t = OMP_CLAUSE_DECL (c);
5403 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5405 if (processing_template_decl)
5406 break;
5407 if (DECL_P (t))
5408 error ("%qD is not a variable in clause %<firstprivate%>", t);
5409 else
5410 error ("%qE is not a variable in clause %<firstprivate%>", t);
5411 remove = true;
5413 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5416 error ("%qD appears more than once in data clauses", t);
5417 remove = true;
5419 else
5420 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5421 break;
5423 case OMP_CLAUSE_LASTPRIVATE:
5424 t = OMP_CLAUSE_DECL (c);
5425 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5427 if (processing_template_decl)
5428 break;
5429 if (DECL_P (t))
5430 error ("%qD is not a variable in clause %<lastprivate%>", t);
5431 else
5432 error ("%qE is not a variable in clause %<lastprivate%>", t);
5433 remove = true;
5435 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5436 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5438 error ("%qD appears more than once in data clauses", t);
5439 remove = true;
5441 else
5442 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5443 break;
5445 case OMP_CLAUSE_IF:
5446 t = OMP_CLAUSE_IF_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_IF_EXPR (c) = t;
5453 break;
5455 case OMP_CLAUSE_FINAL:
5456 t = OMP_CLAUSE_FINAL_EXPR (c);
5457 t = maybe_convert_cond (t);
5458 if (t == error_mark_node)
5459 remove = true;
5460 else if (!processing_template_decl)
5461 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5462 OMP_CLAUSE_FINAL_EXPR (c) = t;
5463 break;
5465 case OMP_CLAUSE_NUM_THREADS:
5466 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5467 if (t == error_mark_node)
5468 remove = true;
5469 else if (!type_dependent_expression_p (t)
5470 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5472 error ("num_threads expression must be integral");
5473 remove = true;
5475 else
5477 t = mark_rvalue_use (t);
5478 if (!processing_template_decl)
5479 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5480 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5482 break;
5484 case OMP_CLAUSE_SCHEDULE:
5485 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5486 if (t == NULL)
5488 else if (t == error_mark_node)
5489 remove = true;
5490 else if (!type_dependent_expression_p (t)
5491 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5492 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5493 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5495 error ("schedule chunk size expression must be integral");
5496 remove = true;
5498 else
5500 t = mark_rvalue_use (t);
5501 if (!processing_template_decl)
5503 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5504 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5506 t = convert_to_integer (long_integer_type_node, t);
5507 if (t == error_mark_node)
5509 remove = true;
5510 break;
5513 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5515 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5517 break;
5519 case OMP_CLAUSE_SIMDLEN:
5520 case OMP_CLAUSE_SAFELEN:
5521 t = OMP_CLAUSE_OPERAND (c, 0);
5522 if (t == error_mark_node)
5523 remove = true;
5524 else if (!type_dependent_expression_p (t)
5525 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5527 error ("%qs length expression must be integral",
5528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5529 remove = true;
5531 else
5533 t = mark_rvalue_use (t);
5534 t = maybe_constant_value (t);
5535 if (!processing_template_decl)
5537 if (TREE_CODE (t) != INTEGER_CST
5538 || tree_int_cst_sgn (t) != 1)
5540 error ("%qs length expression must be positive constant"
5541 " integer expression",
5542 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5543 remove = true;
5546 OMP_CLAUSE_OPERAND (c, 0) = t;
5548 break;
5550 case OMP_CLAUSE_NUM_TEAMS:
5551 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5552 if (t == error_mark_node)
5553 remove = true;
5554 else if (!type_dependent_expression_p (t)
5555 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5557 error ("%<num_teams%> expression must be integral");
5558 remove = true;
5560 else
5562 t = mark_rvalue_use (t);
5563 if (!processing_template_decl)
5564 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5565 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5567 break;
5569 case OMP_CLAUSE_THREAD_LIMIT:
5570 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5571 if (t == error_mark_node)
5572 remove = true;
5573 else if (!type_dependent_expression_p (t)
5574 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5576 error ("%<thread_limit%> expression must be integral");
5577 remove = true;
5579 else
5581 t = mark_rvalue_use (t);
5582 if (!processing_template_decl)
5583 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5584 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5586 break;
5588 case OMP_CLAUSE_DEVICE:
5589 t = OMP_CLAUSE_DEVICE_ID (c);
5590 if (t == error_mark_node)
5591 remove = true;
5592 else if (!type_dependent_expression_p (t)
5593 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5595 error ("%<device%> id must be integral");
5596 remove = true;
5598 else
5600 t = mark_rvalue_use (t);
5601 if (!processing_template_decl)
5602 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5603 OMP_CLAUSE_DEVICE_ID (c) = t;
5605 break;
5607 case OMP_CLAUSE_DIST_SCHEDULE:
5608 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5609 if (t == NULL)
5611 else if (t == error_mark_node)
5612 remove = true;
5613 else if (!type_dependent_expression_p (t)
5614 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5616 error ("%<dist_schedule%> chunk size expression must be "
5617 "integral");
5618 remove = true;
5620 else
5622 t = mark_rvalue_use (t);
5623 if (!processing_template_decl)
5624 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5625 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5627 break;
5629 case OMP_CLAUSE_ALIGNED:
5630 t = OMP_CLAUSE_DECL (c);
5631 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5633 if (processing_template_decl)
5634 break;
5635 if (DECL_P (t))
5636 error ("%qD is not a variable in %<aligned%> clause", t);
5637 else
5638 error ("%qE is not a variable in %<aligned%> clause", t);
5639 remove = true;
5641 else if (!type_dependent_expression_p (t)
5642 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5643 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5644 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5645 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5646 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5647 != ARRAY_TYPE))))
5649 error_at (OMP_CLAUSE_LOCATION (c),
5650 "%qE in %<aligned%> clause is neither a pointer nor "
5651 "an array nor a reference to pointer or array", t);
5652 remove = true;
5654 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5656 error ("%qD appears more than once in %<aligned%> clauses", t);
5657 remove = true;
5659 else
5660 bitmap_set_bit (&aligned_head, DECL_UID (t));
5661 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5662 if (t == error_mark_node)
5663 remove = true;
5664 else if (t == NULL_TREE)
5665 break;
5666 else if (!type_dependent_expression_p (t)
5667 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5669 error ("%<aligned%> clause alignment expression must "
5670 "be integral");
5671 remove = true;
5673 else
5675 t = mark_rvalue_use (t);
5676 t = maybe_constant_value (t);
5677 if (!processing_template_decl)
5679 if (TREE_CODE (t) != INTEGER_CST
5680 || tree_int_cst_sgn (t) != 1)
5682 error ("%<aligned%> clause alignment expression must be "
5683 "positive constant integer expression");
5684 remove = true;
5687 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5689 break;
5691 case OMP_CLAUSE_DEPEND:
5692 t = OMP_CLAUSE_DECL (c);
5693 if (TREE_CODE (t) == TREE_LIST)
5695 if (handle_omp_array_sections (c))
5696 remove = true;
5697 break;
5699 if (t == error_mark_node)
5700 remove = true;
5701 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5703 if (processing_template_decl)
5704 break;
5705 if (DECL_P (t))
5706 error ("%qD is not a variable in %<depend%> clause", t);
5707 else
5708 error ("%qE is not a variable in %<depend%> clause", t);
5709 remove = true;
5711 else if (!processing_template_decl
5712 && !cxx_mark_addressable (t))
5713 remove = true;
5714 break;
5716 case OMP_CLAUSE_MAP:
5717 case OMP_CLAUSE_TO:
5718 case OMP_CLAUSE_FROM:
5719 t = OMP_CLAUSE_DECL (c);
5720 if (TREE_CODE (t) == TREE_LIST)
5722 if (handle_omp_array_sections (c))
5723 remove = true;
5724 else
5726 t = OMP_CLAUSE_DECL (c);
5727 if (TREE_CODE (t) != TREE_LIST
5728 && !type_dependent_expression_p (t)
5729 && !cp_omp_mappable_type (TREE_TYPE (t)))
5731 error_at (OMP_CLAUSE_LOCATION (c),
5732 "array section does not have mappable type "
5733 "in %qs clause",
5734 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5735 remove = true;
5738 break;
5740 if (t == error_mark_node)
5741 remove = true;
5742 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5744 if (processing_template_decl)
5745 break;
5746 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5747 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5748 break;
5749 if (DECL_P (t))
5750 error ("%qD is not a variable in %qs clause", t,
5751 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5752 else
5753 error ("%qE is not a variable in %qs clause", t,
5754 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5755 remove = true;
5757 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5759 error ("%qD is threadprivate variable in %qs clause", t,
5760 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5761 remove = true;
5763 else if (!processing_template_decl
5764 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5765 && !cxx_mark_addressable (t))
5766 remove = true;
5767 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5768 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5769 && !type_dependent_expression_p (t)
5770 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5771 == REFERENCE_TYPE)
5772 ? TREE_TYPE (TREE_TYPE (t))
5773 : TREE_TYPE (t)))
5775 error_at (OMP_CLAUSE_LOCATION (c),
5776 "%qD does not have a mappable type in %qs clause", t,
5777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5778 remove = true;
5780 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5782 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5783 error ("%qD appears more than once in motion clauses", t);
5784 else
5785 error ("%qD appears more than once in map clauses", t);
5786 remove = true;
5788 else
5789 bitmap_set_bit (&generic_head, DECL_UID (t));
5790 break;
5792 case OMP_CLAUSE_UNIFORM:
5793 t = OMP_CLAUSE_DECL (c);
5794 if (TREE_CODE (t) != PARM_DECL)
5796 if (processing_template_decl)
5797 break;
5798 if (DECL_P (t))
5799 error ("%qD is not an argument in %<uniform%> clause", t);
5800 else
5801 error ("%qE is not an argument in %<uniform%> clause", t);
5802 remove = true;
5803 break;
5805 goto check_dup_generic;
5807 case OMP_CLAUSE_NOWAIT:
5808 case OMP_CLAUSE_ORDERED:
5809 case OMP_CLAUSE_DEFAULT:
5810 case OMP_CLAUSE_UNTIED:
5811 case OMP_CLAUSE_COLLAPSE:
5812 case OMP_CLAUSE_MERGEABLE:
5813 case OMP_CLAUSE_PARALLEL:
5814 case OMP_CLAUSE_FOR:
5815 case OMP_CLAUSE_SECTIONS:
5816 case OMP_CLAUSE_TASKGROUP:
5817 case OMP_CLAUSE_PROC_BIND:
5818 case OMP_CLAUSE__CILK_FOR_COUNT_:
5819 break;
5821 case OMP_CLAUSE_INBRANCH:
5822 case OMP_CLAUSE_NOTINBRANCH:
5823 if (branch_seen)
5825 error ("%<inbranch%> clause is incompatible with "
5826 "%<notinbranch%>");
5827 remove = true;
5829 branch_seen = true;
5830 break;
5832 default:
5833 gcc_unreachable ();
5836 if (remove)
5837 *pc = OMP_CLAUSE_CHAIN (c);
5838 else
5839 pc = &OMP_CLAUSE_CHAIN (c);
5842 for (pc = &clauses, c = clauses; c ; c = *pc)
5844 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5845 bool remove = false;
5846 bool need_complete_non_reference = false;
5847 bool need_default_ctor = false;
5848 bool need_copy_ctor = false;
5849 bool need_copy_assignment = false;
5850 bool need_implicitly_determined = false;
5851 bool need_dtor = false;
5852 tree type, inner_type;
5854 switch (c_kind)
5856 case OMP_CLAUSE_SHARED:
5857 need_implicitly_determined = true;
5858 break;
5859 case OMP_CLAUSE_PRIVATE:
5860 need_complete_non_reference = true;
5861 need_default_ctor = true;
5862 need_dtor = true;
5863 need_implicitly_determined = true;
5864 break;
5865 case OMP_CLAUSE_FIRSTPRIVATE:
5866 need_complete_non_reference = true;
5867 need_copy_ctor = true;
5868 need_dtor = true;
5869 need_implicitly_determined = true;
5870 break;
5871 case OMP_CLAUSE_LASTPRIVATE:
5872 need_complete_non_reference = true;
5873 need_copy_assignment = true;
5874 need_implicitly_determined = true;
5875 break;
5876 case OMP_CLAUSE_REDUCTION:
5877 need_implicitly_determined = true;
5878 break;
5879 case OMP_CLAUSE_COPYPRIVATE:
5880 need_copy_assignment = true;
5881 break;
5882 case OMP_CLAUSE_COPYIN:
5883 need_copy_assignment = true;
5884 break;
5885 case OMP_CLAUSE_NOWAIT:
5886 if (copyprivate_seen)
5888 error_at (OMP_CLAUSE_LOCATION (c),
5889 "%<nowait%> clause must not be used together "
5890 "with %<copyprivate%>");
5891 *pc = OMP_CLAUSE_CHAIN (c);
5892 continue;
5894 /* FALLTHRU */
5895 default:
5896 pc = &OMP_CLAUSE_CHAIN (c);
5897 continue;
5900 t = OMP_CLAUSE_DECL (c);
5901 if (processing_template_decl
5902 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5904 pc = &OMP_CLAUSE_CHAIN (c);
5905 continue;
5908 switch (c_kind)
5910 case OMP_CLAUSE_LASTPRIVATE:
5911 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5913 need_default_ctor = true;
5914 need_dtor = true;
5916 break;
5918 case OMP_CLAUSE_REDUCTION:
5919 if (finish_omp_reduction_clause (c, &need_default_ctor,
5920 &need_dtor))
5921 remove = true;
5922 else
5923 t = OMP_CLAUSE_DECL (c);
5924 break;
5926 case OMP_CLAUSE_COPYIN:
5927 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5929 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5930 remove = true;
5932 break;
5934 default:
5935 break;
5938 if (need_complete_non_reference || need_copy_assignment)
5940 t = require_complete_type (t);
5941 if (t == error_mark_node)
5942 remove = true;
5943 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5944 && need_complete_non_reference)
5946 error ("%qE has reference type for %qs", t,
5947 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5948 remove = true;
5951 if (need_implicitly_determined)
5953 const char *share_name = NULL;
5955 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5956 share_name = "threadprivate";
5957 else switch (cxx_omp_predetermined_sharing (t))
5959 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5960 break;
5961 case OMP_CLAUSE_DEFAULT_SHARED:
5962 /* const vars may be specified in firstprivate clause. */
5963 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5964 && cxx_omp_const_qual_no_mutable (t))
5965 break;
5966 share_name = "shared";
5967 break;
5968 case OMP_CLAUSE_DEFAULT_PRIVATE:
5969 share_name = "private";
5970 break;
5971 default:
5972 gcc_unreachable ();
5974 if (share_name)
5976 error ("%qE is predetermined %qs for %qs",
5977 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5978 remove = true;
5982 /* We're interested in the base element, not arrays. */
5983 inner_type = type = TREE_TYPE (t);
5984 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5985 inner_type = TREE_TYPE (inner_type);
5987 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5988 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5989 inner_type = TREE_TYPE (inner_type);
5991 /* Check for special function availability by building a call to one.
5992 Save the results, because later we won't be in the right context
5993 for making these queries. */
5994 if (CLASS_TYPE_P (inner_type)
5995 && COMPLETE_TYPE_P (inner_type)
5996 && (need_default_ctor || need_copy_ctor
5997 || need_copy_assignment || need_dtor)
5998 && !type_dependent_expression_p (t)
5999 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6000 need_copy_ctor, need_copy_assignment,
6001 need_dtor))
6002 remove = true;
6004 if (remove)
6005 *pc = OMP_CLAUSE_CHAIN (c);
6006 else
6007 pc = &OMP_CLAUSE_CHAIN (c);
6010 bitmap_obstack_release (NULL);
6011 return clauses;
6014 /* For all variables in the tree_list VARS, mark them as thread local. */
6016 void
6017 finish_omp_threadprivate (tree vars)
6019 tree t;
6021 /* Mark every variable in VARS to be assigned thread local storage. */
6022 for (t = vars; t; t = TREE_CHAIN (t))
6024 tree v = TREE_PURPOSE (t);
6026 if (error_operand_p (v))
6028 else if (!VAR_P (v))
6029 error ("%<threadprivate%> %qD is not file, namespace "
6030 "or block scope variable", v);
6031 /* If V had already been marked threadprivate, it doesn't matter
6032 whether it had been used prior to this point. */
6033 else if (TREE_USED (v)
6034 && (DECL_LANG_SPECIFIC (v) == NULL
6035 || !CP_DECL_THREADPRIVATE_P (v)))
6036 error ("%qE declared %<threadprivate%> after first use", v);
6037 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6038 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6039 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6040 error ("%<threadprivate%> %qE has incomplete type", v);
6041 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6042 && CP_DECL_CONTEXT (v) != current_class_type)
6043 error ("%<threadprivate%> %qE directive not "
6044 "in %qT definition", v, CP_DECL_CONTEXT (v));
6045 else
6047 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6048 if (DECL_LANG_SPECIFIC (v) == NULL)
6050 retrofit_lang_decl (v);
6052 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6053 after the allocation of the lang_decl structure. */
6054 if (DECL_DISCRIMINATOR_P (v))
6055 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6058 if (! DECL_THREAD_LOCAL_P (v))
6060 set_decl_tls_model (v, decl_default_tls_model (v));
6061 /* If rtl has been already set for this var, call
6062 make_decl_rtl once again, so that encode_section_info
6063 has a chance to look at the new decl flags. */
6064 if (DECL_RTL_SET_P (v))
6065 make_decl_rtl (v);
6067 CP_DECL_THREADPRIVATE_P (v) = 1;
6072 /* Build an OpenMP structured block. */
6074 tree
6075 begin_omp_structured_block (void)
6077 return do_pushlevel (sk_omp);
6080 tree
6081 finish_omp_structured_block (tree block)
6083 return do_poplevel (block);
6086 /* Similarly, except force the retention of the BLOCK. */
6088 tree
6089 begin_omp_parallel (void)
6091 keep_next_level (true);
6092 return begin_omp_structured_block ();
6095 tree
6096 finish_omp_parallel (tree clauses, tree body)
6098 tree stmt;
6100 body = finish_omp_structured_block (body);
6102 stmt = make_node (OMP_PARALLEL);
6103 TREE_TYPE (stmt) = void_type_node;
6104 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6105 OMP_PARALLEL_BODY (stmt) = body;
6107 return add_stmt (stmt);
6110 tree
6111 begin_omp_task (void)
6113 keep_next_level (true);
6114 return begin_omp_structured_block ();
6117 tree
6118 finish_omp_task (tree clauses, tree body)
6120 tree stmt;
6122 body = finish_omp_structured_block (body);
6124 stmt = make_node (OMP_TASK);
6125 TREE_TYPE (stmt) = void_type_node;
6126 OMP_TASK_CLAUSES (stmt) = clauses;
6127 OMP_TASK_BODY (stmt) = body;
6129 return add_stmt (stmt);
6132 /* Helper function for finish_omp_for. Convert Ith random access iterator
6133 into integral iterator. Return FALSE if successful. */
6135 static bool
6136 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6137 tree condv, tree incrv, tree *body,
6138 tree *pre_body, tree clauses, tree *lastp)
6140 tree diff, iter_init, iter_incr = NULL, last;
6141 tree incr_var = NULL, orig_pre_body, orig_body, c;
6142 tree decl = TREE_VEC_ELT (declv, i);
6143 tree init = TREE_VEC_ELT (initv, i);
6144 tree cond = TREE_VEC_ELT (condv, i);
6145 tree incr = TREE_VEC_ELT (incrv, i);
6146 tree iter = decl;
6147 location_t elocus = locus;
6149 if (init && EXPR_HAS_LOCATION (init))
6150 elocus = EXPR_LOCATION (init);
6152 switch (TREE_CODE (cond))
6154 case GT_EXPR:
6155 case GE_EXPR:
6156 case LT_EXPR:
6157 case LE_EXPR:
6158 case NE_EXPR:
6159 if (TREE_OPERAND (cond, 1) == iter)
6160 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6161 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6162 if (TREE_OPERAND (cond, 0) != iter)
6163 cond = error_mark_node;
6164 else
6166 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6167 TREE_CODE (cond),
6168 iter, ERROR_MARK,
6169 TREE_OPERAND (cond, 1), ERROR_MARK,
6170 NULL, tf_warning_or_error);
6171 if (error_operand_p (tem))
6172 return true;
6174 break;
6175 default:
6176 cond = error_mark_node;
6177 break;
6179 if (cond == error_mark_node)
6181 error_at (elocus, "invalid controlling predicate");
6182 return true;
6184 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6185 ERROR_MARK, iter, ERROR_MARK, NULL,
6186 tf_warning_or_error);
6187 if (error_operand_p (diff))
6188 return true;
6189 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6191 error_at (elocus, "difference between %qE and %qD does not have integer type",
6192 TREE_OPERAND (cond, 1), iter);
6193 return true;
6196 switch (TREE_CODE (incr))
6198 case PREINCREMENT_EXPR:
6199 case PREDECREMENT_EXPR:
6200 case POSTINCREMENT_EXPR:
6201 case POSTDECREMENT_EXPR:
6202 if (TREE_OPERAND (incr, 0) != iter)
6204 incr = error_mark_node;
6205 break;
6207 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6208 TREE_CODE (incr), iter,
6209 tf_warning_or_error);
6210 if (error_operand_p (iter_incr))
6211 return true;
6212 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6213 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6214 incr = integer_one_node;
6215 else
6216 incr = integer_minus_one_node;
6217 break;
6218 case MODIFY_EXPR:
6219 if (TREE_OPERAND (incr, 0) != iter)
6220 incr = error_mark_node;
6221 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6222 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6224 tree rhs = TREE_OPERAND (incr, 1);
6225 if (TREE_OPERAND (rhs, 0) == iter)
6227 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6228 != INTEGER_TYPE)
6229 incr = error_mark_node;
6230 else
6232 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6233 iter, TREE_CODE (rhs),
6234 TREE_OPERAND (rhs, 1),
6235 tf_warning_or_error);
6236 if (error_operand_p (iter_incr))
6237 return true;
6238 incr = TREE_OPERAND (rhs, 1);
6239 incr = cp_convert (TREE_TYPE (diff), incr,
6240 tf_warning_or_error);
6241 if (TREE_CODE (rhs) == MINUS_EXPR)
6243 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6244 incr = fold_if_not_in_template (incr);
6246 if (TREE_CODE (incr) != INTEGER_CST
6247 && (TREE_CODE (incr) != NOP_EXPR
6248 || (TREE_CODE (TREE_OPERAND (incr, 0))
6249 != INTEGER_CST)))
6250 iter_incr = NULL;
6253 else if (TREE_OPERAND (rhs, 1) == iter)
6255 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6256 || TREE_CODE (rhs) != PLUS_EXPR)
6257 incr = error_mark_node;
6258 else
6260 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6261 PLUS_EXPR,
6262 TREE_OPERAND (rhs, 0),
6263 ERROR_MARK, iter,
6264 ERROR_MARK, NULL,
6265 tf_warning_or_error);
6266 if (error_operand_p (iter_incr))
6267 return true;
6268 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6269 iter, NOP_EXPR,
6270 iter_incr,
6271 tf_warning_or_error);
6272 if (error_operand_p (iter_incr))
6273 return true;
6274 incr = TREE_OPERAND (rhs, 0);
6275 iter_incr = NULL;
6278 else
6279 incr = error_mark_node;
6281 else
6282 incr = error_mark_node;
6283 break;
6284 default:
6285 incr = error_mark_node;
6286 break;
6289 if (incr == error_mark_node)
6291 error_at (elocus, "invalid increment expression");
6292 return true;
6295 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6296 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6297 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6298 && OMP_CLAUSE_DECL (c) == iter)
6299 break;
6301 decl = create_temporary_var (TREE_TYPE (diff));
6302 pushdecl (decl);
6303 add_decl_expr (decl);
6304 last = create_temporary_var (TREE_TYPE (diff));
6305 pushdecl (last);
6306 add_decl_expr (last);
6307 if (c && iter_incr == NULL)
6309 incr_var = create_temporary_var (TREE_TYPE (diff));
6310 pushdecl (incr_var);
6311 add_decl_expr (incr_var);
6313 gcc_assert (stmts_are_full_exprs_p ());
6315 orig_pre_body = *pre_body;
6316 *pre_body = push_stmt_list ();
6317 if (orig_pre_body)
6318 add_stmt (orig_pre_body);
6319 if (init != NULL)
6320 finish_expr_stmt (build_x_modify_expr (elocus,
6321 iter, NOP_EXPR, init,
6322 tf_warning_or_error));
6323 init = build_int_cst (TREE_TYPE (diff), 0);
6324 if (c && iter_incr == NULL)
6326 finish_expr_stmt (build_x_modify_expr (elocus,
6327 incr_var, NOP_EXPR,
6328 incr, tf_warning_or_error));
6329 incr = incr_var;
6330 iter_incr = build_x_modify_expr (elocus,
6331 iter, PLUS_EXPR, incr,
6332 tf_warning_or_error);
6334 finish_expr_stmt (build_x_modify_expr (elocus,
6335 last, NOP_EXPR, init,
6336 tf_warning_or_error));
6337 *pre_body = pop_stmt_list (*pre_body);
6339 cond = cp_build_binary_op (elocus,
6340 TREE_CODE (cond), decl, diff,
6341 tf_warning_or_error);
6342 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6343 elocus, incr, NULL_TREE);
6345 orig_body = *body;
6346 *body = push_stmt_list ();
6347 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6348 iter_init = build_x_modify_expr (elocus,
6349 iter, PLUS_EXPR, iter_init,
6350 tf_warning_or_error);
6351 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6352 finish_expr_stmt (iter_init);
6353 finish_expr_stmt (build_x_modify_expr (elocus,
6354 last, NOP_EXPR, decl,
6355 tf_warning_or_error));
6356 add_stmt (orig_body);
6357 *body = pop_stmt_list (*body);
6359 if (c)
6361 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6362 finish_expr_stmt (iter_incr);
6363 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6364 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6367 TREE_VEC_ELT (declv, i) = decl;
6368 TREE_VEC_ELT (initv, i) = init;
6369 TREE_VEC_ELT (condv, i) = cond;
6370 TREE_VEC_ELT (incrv, i) = incr;
6371 *lastp = last;
6373 return false;
6376 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6377 are directly for their associated operands in the statement. DECL
6378 and INIT are a combo; if DECL is NULL then INIT ought to be a
6379 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6380 optional statements that need to go before the loop into its
6381 sk_omp scope. */
6383 tree
6384 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6385 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6387 tree omp_for = NULL, orig_incr = NULL;
6388 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6389 tree last = NULL_TREE;
6390 location_t elocus;
6391 int i;
6393 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6394 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6395 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6396 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6398 decl = TREE_VEC_ELT (declv, i);
6399 init = TREE_VEC_ELT (initv, i);
6400 cond = TREE_VEC_ELT (condv, i);
6401 incr = TREE_VEC_ELT (incrv, i);
6402 elocus = locus;
6404 if (decl == NULL)
6406 if (init != NULL)
6407 switch (TREE_CODE (init))
6409 case MODIFY_EXPR:
6410 decl = TREE_OPERAND (init, 0);
6411 init = TREE_OPERAND (init, 1);
6412 break;
6413 case MODOP_EXPR:
6414 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6416 decl = TREE_OPERAND (init, 0);
6417 init = TREE_OPERAND (init, 2);
6419 break;
6420 default:
6421 break;
6424 if (decl == NULL)
6426 error_at (locus,
6427 "expected iteration declaration or initialization");
6428 return NULL;
6432 if (init && EXPR_HAS_LOCATION (init))
6433 elocus = EXPR_LOCATION (init);
6435 if (cond == NULL)
6437 error_at (elocus, "missing controlling predicate");
6438 return NULL;
6441 if (incr == NULL)
6443 error_at (elocus, "missing increment expression");
6444 return NULL;
6447 TREE_VEC_ELT (declv, i) = decl;
6448 TREE_VEC_ELT (initv, i) = init;
6451 if (dependent_omp_for_p (declv, initv, condv, incrv))
6453 tree stmt;
6455 stmt = make_node (code);
6457 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6459 /* This is really just a place-holder. We'll be decomposing this
6460 again and going through the cp_build_modify_expr path below when
6461 we instantiate the thing. */
6462 TREE_VEC_ELT (initv, i)
6463 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6464 TREE_VEC_ELT (initv, i));
6467 TREE_TYPE (stmt) = void_type_node;
6468 OMP_FOR_INIT (stmt) = initv;
6469 OMP_FOR_COND (stmt) = condv;
6470 OMP_FOR_INCR (stmt) = incrv;
6471 OMP_FOR_BODY (stmt) = body;
6472 OMP_FOR_PRE_BODY (stmt) = pre_body;
6473 OMP_FOR_CLAUSES (stmt) = clauses;
6475 SET_EXPR_LOCATION (stmt, locus);
6476 return add_stmt (stmt);
6479 if (processing_template_decl)
6480 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6482 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6484 decl = TREE_VEC_ELT (declv, i);
6485 init = TREE_VEC_ELT (initv, i);
6486 cond = TREE_VEC_ELT (condv, i);
6487 incr = TREE_VEC_ELT (incrv, i);
6488 if (orig_incr)
6489 TREE_VEC_ELT (orig_incr, i) = incr;
6490 elocus = locus;
6492 if (init && EXPR_HAS_LOCATION (init))
6493 elocus = EXPR_LOCATION (init);
6495 if (!DECL_P (decl))
6497 error_at (elocus, "expected iteration declaration or initialization");
6498 return NULL;
6501 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6503 if (orig_incr)
6504 TREE_VEC_ELT (orig_incr, i) = incr;
6505 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6506 TREE_CODE (TREE_OPERAND (incr, 1)),
6507 TREE_OPERAND (incr, 2),
6508 tf_warning_or_error);
6511 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6513 if (code == OMP_SIMD)
6515 error_at (elocus, "%<#pragma omp simd%> used with class "
6516 "iteration variable %qE", decl);
6517 return NULL;
6519 if (code == CILK_FOR && i == 0)
6520 orig_decl = decl;
6521 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6522 incrv, &body, &pre_body,
6523 clauses, &last))
6524 return NULL;
6525 continue;
6528 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6529 && !TYPE_PTR_P (TREE_TYPE (decl)))
6531 error_at (elocus, "invalid type for iteration variable %qE", decl);
6532 return NULL;
6535 if (!processing_template_decl)
6537 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6538 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6540 else
6541 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6542 if (cond
6543 && TREE_SIDE_EFFECTS (cond)
6544 && COMPARISON_CLASS_P (cond)
6545 && !processing_template_decl)
6547 tree t = TREE_OPERAND (cond, 0);
6548 if (TREE_SIDE_EFFECTS (t)
6549 && t != decl
6550 && (TREE_CODE (t) != NOP_EXPR
6551 || TREE_OPERAND (t, 0) != decl))
6552 TREE_OPERAND (cond, 0)
6553 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6555 t = TREE_OPERAND (cond, 1);
6556 if (TREE_SIDE_EFFECTS (t)
6557 && t != decl
6558 && (TREE_CODE (t) != NOP_EXPR
6559 || TREE_OPERAND (t, 0) != decl))
6560 TREE_OPERAND (cond, 1)
6561 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6563 if (decl == error_mark_node || init == error_mark_node)
6564 return NULL;
6566 TREE_VEC_ELT (declv, i) = decl;
6567 TREE_VEC_ELT (initv, i) = init;
6568 TREE_VEC_ELT (condv, i) = cond;
6569 TREE_VEC_ELT (incrv, i) = incr;
6570 i++;
6573 if (IS_EMPTY_STMT (pre_body))
6574 pre_body = NULL;
6576 if (code == CILK_FOR && !processing_template_decl)
6577 block = push_stmt_list ();
6579 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6580 body, pre_body);
6582 if (omp_for == NULL)
6584 if (block)
6585 pop_stmt_list (block);
6586 return NULL;
6589 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6591 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6592 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6594 if (TREE_CODE (incr) != MODIFY_EXPR)
6595 continue;
6597 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6598 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6599 && !processing_template_decl)
6601 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6602 if (TREE_SIDE_EFFECTS (t)
6603 && t != decl
6604 && (TREE_CODE (t) != NOP_EXPR
6605 || TREE_OPERAND (t, 0) != decl))
6606 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6607 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6609 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6610 if (TREE_SIDE_EFFECTS (t)
6611 && t != decl
6612 && (TREE_CODE (t) != NOP_EXPR
6613 || TREE_OPERAND (t, 0) != decl))
6614 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6615 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6618 if (orig_incr)
6619 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6621 OMP_FOR_CLAUSES (omp_for) = clauses;
6623 if (block)
6625 tree omp_par = make_node (OMP_PARALLEL);
6626 TREE_TYPE (omp_par) = void_type_node;
6627 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6628 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6629 TREE_SIDE_EFFECTS (bind) = 1;
6630 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6631 OMP_PARALLEL_BODY (omp_par) = bind;
6632 if (OMP_FOR_PRE_BODY (omp_for))
6634 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6635 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6637 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6638 decl = TREE_OPERAND (init, 0);
6639 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6640 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6641 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6642 clauses = OMP_FOR_CLAUSES (omp_for);
6643 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6644 for (pc = &clauses; *pc; )
6645 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6647 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6648 OMP_FOR_CLAUSES (omp_for) = *pc;
6649 *pc = OMP_CLAUSE_CHAIN (*pc);
6650 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6652 else
6654 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6655 pc = &OMP_CLAUSE_CHAIN (*pc);
6657 if (TREE_CODE (t) != INTEGER_CST)
6659 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6660 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6661 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6662 OMP_CLAUSE_CHAIN (c) = clauses;
6663 clauses = c;
6665 if (TREE_CODE (incr) == MODIFY_EXPR)
6667 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6668 if (TREE_CODE (t) != INTEGER_CST)
6670 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6671 = get_temp_regvar (TREE_TYPE (t), t);
6672 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6673 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6674 OMP_CLAUSE_CHAIN (c) = clauses;
6675 clauses = c;
6678 t = TREE_OPERAND (init, 1);
6679 if (TREE_CODE (t) != INTEGER_CST)
6681 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6682 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6683 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6684 OMP_CLAUSE_CHAIN (c) = clauses;
6685 clauses = c;
6687 if (orig_decl && orig_decl != decl)
6689 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6690 OMP_CLAUSE_DECL (c) = orig_decl;
6691 OMP_CLAUSE_CHAIN (c) = clauses;
6692 clauses = c;
6694 if (last)
6696 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6697 OMP_CLAUSE_DECL (c) = last;
6698 OMP_CLAUSE_CHAIN (c) = clauses;
6699 clauses = c;
6701 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6702 OMP_CLAUSE_DECL (c) = decl;
6703 OMP_CLAUSE_CHAIN (c) = clauses;
6704 clauses = c;
6705 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6706 OMP_CLAUSE_OPERAND (c, 0)
6707 = cilk_for_number_of_iterations (omp_for);
6708 OMP_CLAUSE_CHAIN (c) = clauses;
6709 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6710 add_stmt (omp_par);
6711 return omp_par;
6713 else if (code == CILK_FOR && processing_template_decl)
6715 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6716 if (orig_decl && orig_decl != decl)
6718 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6719 OMP_CLAUSE_DECL (c) = orig_decl;
6720 OMP_CLAUSE_CHAIN (c) = clauses;
6721 clauses = c;
6723 if (last)
6725 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6726 OMP_CLAUSE_DECL (c) = last;
6727 OMP_CLAUSE_CHAIN (c) = clauses;
6728 clauses = c;
6730 OMP_FOR_CLAUSES (omp_for) = clauses;
6732 return omp_for;
6735 void
6736 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6737 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6739 tree orig_lhs;
6740 tree orig_rhs;
6741 tree orig_v;
6742 tree orig_lhs1;
6743 tree orig_rhs1;
6744 bool dependent_p;
6745 tree stmt;
6747 orig_lhs = lhs;
6748 orig_rhs = rhs;
6749 orig_v = v;
6750 orig_lhs1 = lhs1;
6751 orig_rhs1 = rhs1;
6752 dependent_p = false;
6753 stmt = NULL_TREE;
6755 /* Even in a template, we can detect invalid uses of the atomic
6756 pragma if neither LHS nor RHS is type-dependent. */
6757 if (processing_template_decl)
6759 dependent_p = (type_dependent_expression_p (lhs)
6760 || (rhs && type_dependent_expression_p (rhs))
6761 || (v && type_dependent_expression_p (v))
6762 || (lhs1 && type_dependent_expression_p (lhs1))
6763 || (rhs1 && type_dependent_expression_p (rhs1)));
6764 if (!dependent_p)
6766 lhs = build_non_dependent_expr (lhs);
6767 if (rhs)
6768 rhs = build_non_dependent_expr (rhs);
6769 if (v)
6770 v = build_non_dependent_expr (v);
6771 if (lhs1)
6772 lhs1 = build_non_dependent_expr (lhs1);
6773 if (rhs1)
6774 rhs1 = build_non_dependent_expr (rhs1);
6777 if (!dependent_p)
6779 bool swapped = false;
6780 if (rhs1 && cp_tree_equal (lhs, rhs))
6782 tree tem = rhs;
6783 rhs = rhs1;
6784 rhs1 = tem;
6785 swapped = !commutative_tree_code (opcode);
6787 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6789 if (code == OMP_ATOMIC)
6790 error ("%<#pragma omp atomic update%> uses two different "
6791 "expressions for memory");
6792 else
6793 error ("%<#pragma omp atomic capture%> uses two different "
6794 "expressions for memory");
6795 return;
6797 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6799 if (code == OMP_ATOMIC)
6800 error ("%<#pragma omp atomic update%> uses two different "
6801 "expressions for memory");
6802 else
6803 error ("%<#pragma omp atomic capture%> uses two different "
6804 "expressions for memory");
6805 return;
6807 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6808 v, lhs1, rhs1, swapped, seq_cst);
6809 if (stmt == error_mark_node)
6810 return;
6812 if (processing_template_decl)
6814 if (code == OMP_ATOMIC_READ)
6816 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6817 OMP_ATOMIC_READ, orig_lhs);
6818 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6819 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6821 else
6823 if (opcode == NOP_EXPR)
6824 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6825 else
6826 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6827 if (orig_rhs1)
6828 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6829 COMPOUND_EXPR, orig_rhs1, stmt);
6830 if (code != OMP_ATOMIC)
6832 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6833 code, orig_lhs1, stmt);
6834 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6835 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6838 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6839 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6841 finish_expr_stmt (stmt);
6844 void
6845 finish_omp_barrier (void)
6847 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6848 vec<tree, va_gc> *vec = make_tree_vector ();
6849 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6850 release_tree_vector (vec);
6851 finish_expr_stmt (stmt);
6854 void
6855 finish_omp_flush (void)
6857 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6858 vec<tree, va_gc> *vec = make_tree_vector ();
6859 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6860 release_tree_vector (vec);
6861 finish_expr_stmt (stmt);
6864 void
6865 finish_omp_taskwait (void)
6867 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6868 vec<tree, va_gc> *vec = make_tree_vector ();
6869 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6870 release_tree_vector (vec);
6871 finish_expr_stmt (stmt);
6874 void
6875 finish_omp_taskyield (void)
6877 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6878 vec<tree, va_gc> *vec = make_tree_vector ();
6879 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6880 release_tree_vector (vec);
6881 finish_expr_stmt (stmt);
6884 void
6885 finish_omp_cancel (tree clauses)
6887 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6888 int mask = 0;
6889 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6890 mask = 1;
6891 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6892 mask = 2;
6893 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6894 mask = 4;
6895 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6896 mask = 8;
6897 else
6899 error ("%<#pragma omp cancel must specify one of "
6900 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6901 return;
6903 vec<tree, va_gc> *vec = make_tree_vector ();
6904 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6905 if (ifc != NULL_TREE)
6907 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6908 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6909 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6910 build_zero_cst (type));
6912 else
6913 ifc = boolean_true_node;
6914 vec->quick_push (build_int_cst (integer_type_node, mask));
6915 vec->quick_push (ifc);
6916 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6917 release_tree_vector (vec);
6918 finish_expr_stmt (stmt);
6921 void
6922 finish_omp_cancellation_point (tree clauses)
6924 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6925 int mask = 0;
6926 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6927 mask = 1;
6928 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6929 mask = 2;
6930 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6931 mask = 4;
6932 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6933 mask = 8;
6934 else
6936 error ("%<#pragma omp cancellation point must specify one of "
6937 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6938 return;
6940 vec<tree, va_gc> *vec
6941 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6942 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6943 release_tree_vector (vec);
6944 finish_expr_stmt (stmt);
6947 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6948 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6949 should create an extra compound stmt. */
6951 tree
6952 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6954 tree r;
6956 if (pcompound)
6957 *pcompound = begin_compound_stmt (0);
6959 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6961 /* Only add the statement to the function if support enabled. */
6962 if (flag_tm)
6963 add_stmt (r);
6964 else
6965 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6966 ? G_("%<__transaction_relaxed%> without "
6967 "transactional memory support enabled")
6968 : G_("%<__transaction_atomic%> without "
6969 "transactional memory support enabled")));
6971 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6972 TREE_SIDE_EFFECTS (r) = 1;
6973 return r;
6976 /* End a __transaction_atomic or __transaction_relaxed statement.
6977 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6978 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6979 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6981 void
6982 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6984 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6985 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6986 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6987 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6989 /* noexcept specifications are not allowed for function transactions. */
6990 gcc_assert (!(noex && compound_stmt));
6991 if (noex)
6993 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6994 noex);
6995 /* This may not be true when the STATEMENT_LIST is empty. */
6996 if (EXPR_P (body))
6997 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6998 TREE_SIDE_EFFECTS (body) = 1;
6999 TRANSACTION_EXPR_BODY (stmt) = body;
7002 if (compound_stmt)
7003 finish_compound_stmt (compound_stmt);
7006 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7007 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7008 condition. */
7010 tree
7011 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7013 tree ret;
7014 if (noex)
7016 expr = build_must_not_throw_expr (expr, noex);
7017 if (EXPR_P (expr))
7018 SET_EXPR_LOCATION (expr, loc);
7019 TREE_SIDE_EFFECTS (expr) = 1;
7021 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7022 if (flags & TM_STMT_ATTR_RELAXED)
7023 TRANSACTION_EXPR_RELAXED (ret) = 1;
7024 TREE_SIDE_EFFECTS (ret) = 1;
7025 SET_EXPR_LOCATION (ret, loc);
7026 return ret;
7029 void
7030 init_cp_semantics (void)
7034 /* Build a STATIC_ASSERT for a static assertion with the condition
7035 CONDITION and the message text MESSAGE. LOCATION is the location
7036 of the static assertion in the source code. When MEMBER_P, this
7037 static assertion is a member of a class. */
7038 void
7039 finish_static_assert (tree condition, tree message, location_t location,
7040 bool member_p)
7042 if (message == NULL_TREE
7043 || message == error_mark_node
7044 || condition == NULL_TREE
7045 || condition == error_mark_node)
7046 return;
7048 if (check_for_bare_parameter_packs (condition))
7049 condition = error_mark_node;
7051 if (type_dependent_expression_p (condition)
7052 || value_dependent_expression_p (condition))
7054 /* We're in a template; build a STATIC_ASSERT and put it in
7055 the right place. */
7056 tree assertion;
7058 assertion = make_node (STATIC_ASSERT);
7059 STATIC_ASSERT_CONDITION (assertion) = condition;
7060 STATIC_ASSERT_MESSAGE (assertion) = message;
7061 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7063 if (member_p)
7064 maybe_add_class_template_decl_list (current_class_type,
7065 assertion,
7066 /*friend_p=*/0);
7067 else
7068 add_stmt (assertion);
7070 return;
7073 /* Fold the expression and convert it to a boolean value. */
7074 condition = instantiate_non_dependent_expr (condition);
7075 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7076 condition = maybe_constant_value (condition);
7078 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7079 /* Do nothing; the condition is satisfied. */
7081 else
7083 location_t saved_loc = input_location;
7085 input_location = location;
7086 if (TREE_CODE (condition) == INTEGER_CST
7087 && integer_zerop (condition))
7088 /* Report the error. */
7089 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7090 else if (condition && condition != error_mark_node)
7092 error ("non-constant condition for static assertion");
7093 if (require_potential_rvalue_constant_expression (condition))
7094 cxx_constant_value (condition);
7096 input_location = saved_loc;
7100 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7101 suitable for use as a type-specifier.
7103 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7104 id-expression or a class member access, FALSE when it was parsed as
7105 a full expression. */
7107 tree
7108 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7109 tsubst_flags_t complain)
7111 tree type = NULL_TREE;
7113 if (!expr || error_operand_p (expr))
7114 return error_mark_node;
7116 if (TYPE_P (expr)
7117 || TREE_CODE (expr) == TYPE_DECL
7118 || (TREE_CODE (expr) == BIT_NOT_EXPR
7119 && TYPE_P (TREE_OPERAND (expr, 0))))
7121 if (complain & tf_error)
7122 error ("argument to decltype must be an expression");
7123 return error_mark_node;
7126 /* Depending on the resolution of DR 1172, we may later need to distinguish
7127 instantiation-dependent but not type-dependent expressions so that, say,
7128 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7129 if (instantiation_dependent_expression_p (expr))
7131 type = cxx_make_type (DECLTYPE_TYPE);
7132 DECLTYPE_TYPE_EXPR (type) = expr;
7133 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7134 = id_expression_or_member_access_p;
7135 SET_TYPE_STRUCTURAL_EQUALITY (type);
7137 return type;
7140 /* The type denoted by decltype(e) is defined as follows: */
7142 expr = resolve_nondeduced_context (expr);
7144 if (invalid_nonstatic_memfn_p (expr, complain))
7145 return error_mark_node;
7147 if (type_unknown_p (expr))
7149 if (complain & tf_error)
7150 error ("decltype cannot resolve address of overloaded function");
7151 return error_mark_node;
7154 /* To get the size of a static data member declared as an array of
7155 unknown bound, we need to instantiate it. */
7156 if (VAR_P (expr)
7157 && VAR_HAD_UNKNOWN_BOUND (expr)
7158 && DECL_TEMPLATE_INSTANTIATION (expr))
7159 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7161 if (id_expression_or_member_access_p)
7163 /* If e is an id-expression or a class member access (5.2.5
7164 [expr.ref]), decltype(e) is defined as the type of the entity
7165 named by e. If there is no such entity, or e names a set of
7166 overloaded functions, the program is ill-formed. */
7167 if (identifier_p (expr))
7168 expr = lookup_name (expr);
7170 if (INDIRECT_REF_P (expr))
7171 /* This can happen when the expression is, e.g., "a.b". Just
7172 look at the underlying operand. */
7173 expr = TREE_OPERAND (expr, 0);
7175 if (TREE_CODE (expr) == OFFSET_REF
7176 || TREE_CODE (expr) == MEMBER_REF
7177 || TREE_CODE (expr) == SCOPE_REF)
7178 /* We're only interested in the field itself. If it is a
7179 BASELINK, we will need to see through it in the next
7180 step. */
7181 expr = TREE_OPERAND (expr, 1);
7183 if (BASELINK_P (expr))
7184 /* See through BASELINK nodes to the underlying function. */
7185 expr = BASELINK_FUNCTIONS (expr);
7187 switch (TREE_CODE (expr))
7189 case FIELD_DECL:
7190 if (DECL_BIT_FIELD_TYPE (expr))
7192 type = DECL_BIT_FIELD_TYPE (expr);
7193 break;
7195 /* Fall through for fields that aren't bitfields. */
7197 case FUNCTION_DECL:
7198 case VAR_DECL:
7199 case CONST_DECL:
7200 case PARM_DECL:
7201 case RESULT_DECL:
7202 case TEMPLATE_PARM_INDEX:
7203 expr = mark_type_use (expr);
7204 type = TREE_TYPE (expr);
7205 break;
7207 case ERROR_MARK:
7208 type = error_mark_node;
7209 break;
7211 case COMPONENT_REF:
7212 case COMPOUND_EXPR:
7213 mark_type_use (expr);
7214 type = is_bitfield_expr_with_lowered_type (expr);
7215 if (!type)
7216 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7217 break;
7219 case BIT_FIELD_REF:
7220 gcc_unreachable ();
7222 case INTEGER_CST:
7223 case PTRMEM_CST:
7224 /* We can get here when the id-expression refers to an
7225 enumerator or non-type template parameter. */
7226 type = TREE_TYPE (expr);
7227 break;
7229 default:
7230 /* Handle instantiated template non-type arguments. */
7231 type = TREE_TYPE (expr);
7232 break;
7235 else
7237 /* Within a lambda-expression:
7239 Every occurrence of decltype((x)) where x is a possibly
7240 parenthesized id-expression that names an entity of
7241 automatic storage duration is treated as if x were
7242 transformed into an access to a corresponding data member
7243 of the closure type that would have been declared if x
7244 were a use of the denoted entity. */
7245 if (outer_automatic_var_p (expr)
7246 && current_function_decl
7247 && LAMBDA_FUNCTION_P (current_function_decl))
7248 type = capture_decltype (expr);
7249 else if (error_operand_p (expr))
7250 type = error_mark_node;
7251 else if (expr == current_class_ptr)
7252 /* If the expression is just "this", we want the
7253 cv-unqualified pointer for the "this" type. */
7254 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7255 else
7257 /* Otherwise, where T is the type of e, if e is an lvalue,
7258 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7259 cp_lvalue_kind clk = lvalue_kind (expr);
7260 type = unlowered_expr_type (expr);
7261 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7263 /* For vector types, pick a non-opaque variant. */
7264 if (TREE_CODE (type) == VECTOR_TYPE)
7265 type = strip_typedefs (type);
7267 if (clk != clk_none && !(clk & clk_class))
7268 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7272 return type;
7275 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7276 __has_nothrow_copy, depending on assign_p. */
7278 static bool
7279 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7281 tree fns;
7283 if (assign_p)
7285 int ix;
7286 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7287 if (ix < 0)
7288 return false;
7289 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7291 else if (TYPE_HAS_COPY_CTOR (type))
7293 /* If construction of the copy constructor was postponed, create
7294 it now. */
7295 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7296 lazily_declare_fn (sfk_copy_constructor, type);
7297 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7298 lazily_declare_fn (sfk_move_constructor, type);
7299 fns = CLASSTYPE_CONSTRUCTORS (type);
7301 else
7302 return false;
7304 for (; fns; fns = OVL_NEXT (fns))
7306 tree fn = OVL_CURRENT (fns);
7308 if (assign_p)
7310 if (copy_fn_p (fn) == 0)
7311 continue;
7313 else if (copy_fn_p (fn) <= 0)
7314 continue;
7316 maybe_instantiate_noexcept (fn);
7317 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7318 return false;
7321 return true;
7324 /* Actually evaluates the trait. */
7326 static bool
7327 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7329 enum tree_code type_code1;
7330 tree t;
7332 type_code1 = TREE_CODE (type1);
7334 switch (kind)
7336 case CPTK_HAS_NOTHROW_ASSIGN:
7337 type1 = strip_array_types (type1);
7338 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7339 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7340 || (CLASS_TYPE_P (type1)
7341 && classtype_has_nothrow_assign_or_copy_p (type1,
7342 true))));
7344 case CPTK_HAS_TRIVIAL_ASSIGN:
7345 /* ??? The standard seems to be missing the "or array of such a class
7346 type" wording for this trait. */
7347 type1 = strip_array_types (type1);
7348 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7349 && (trivial_type_p (type1)
7350 || (CLASS_TYPE_P (type1)
7351 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7353 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7354 type1 = strip_array_types (type1);
7355 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7356 || (CLASS_TYPE_P (type1)
7357 && (t = locate_ctor (type1))
7358 && (maybe_instantiate_noexcept (t),
7359 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7361 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7362 type1 = strip_array_types (type1);
7363 return (trivial_type_p (type1)
7364 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7366 case CPTK_HAS_NOTHROW_COPY:
7367 type1 = strip_array_types (type1);
7368 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7369 || (CLASS_TYPE_P (type1)
7370 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7372 case CPTK_HAS_TRIVIAL_COPY:
7373 /* ??? The standard seems to be missing the "or array of such a class
7374 type" wording for this trait. */
7375 type1 = strip_array_types (type1);
7376 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7377 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7379 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7380 type1 = strip_array_types (type1);
7381 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7382 || (CLASS_TYPE_P (type1)
7383 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7385 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7386 return type_has_virtual_destructor (type1);
7388 case CPTK_IS_ABSTRACT:
7389 return (ABSTRACT_CLASS_TYPE_P (type1));
7391 case CPTK_IS_BASE_OF:
7392 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7393 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7394 || DERIVED_FROM_P (type1, type2)));
7396 case CPTK_IS_CLASS:
7397 return (NON_UNION_CLASS_TYPE_P (type1));
7399 case CPTK_IS_EMPTY:
7400 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7402 case CPTK_IS_ENUM:
7403 return (type_code1 == ENUMERAL_TYPE);
7405 case CPTK_IS_FINAL:
7406 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7408 case CPTK_IS_LITERAL_TYPE:
7409 return (literal_type_p (type1));
7411 case CPTK_IS_POD:
7412 return (pod_type_p (type1));
7414 case CPTK_IS_POLYMORPHIC:
7415 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7417 case CPTK_IS_STD_LAYOUT:
7418 return (std_layout_type_p (type1));
7420 case CPTK_IS_TRIVIAL:
7421 return (trivial_type_p (type1));
7423 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7424 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7426 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7427 return is_trivially_xible (INIT_EXPR, type1, type2);
7429 case CPTK_IS_TRIVIALLY_COPYABLE:
7430 return (trivially_copyable_p (type1));
7432 case CPTK_IS_UNION:
7433 return (type_code1 == UNION_TYPE);
7435 default:
7436 gcc_unreachable ();
7437 return false;
7441 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7442 void, or a complete type, returns true, otherwise false. */
7444 static bool
7445 check_trait_type (tree type)
7447 if (type == NULL_TREE)
7448 return true;
7450 if (TREE_CODE (type) == TREE_LIST)
7451 return (check_trait_type (TREE_VALUE (type))
7452 && check_trait_type (TREE_CHAIN (type)));
7454 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7455 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7456 return true;
7458 if (VOID_TYPE_P (type))
7459 return true;
7461 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7464 /* Process a trait expression. */
7466 tree
7467 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7469 if (type1 == error_mark_node
7470 || type2 == error_mark_node)
7471 return error_mark_node;
7473 if (processing_template_decl)
7475 tree trait_expr = make_node (TRAIT_EXPR);
7476 TREE_TYPE (trait_expr) = boolean_type_node;
7477 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7478 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7479 TRAIT_EXPR_KIND (trait_expr) = kind;
7480 return trait_expr;
7483 switch (kind)
7485 case CPTK_HAS_NOTHROW_ASSIGN:
7486 case CPTK_HAS_TRIVIAL_ASSIGN:
7487 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7488 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7489 case CPTK_HAS_NOTHROW_COPY:
7490 case CPTK_HAS_TRIVIAL_COPY:
7491 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7492 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7493 case CPTK_IS_ABSTRACT:
7494 case CPTK_IS_EMPTY:
7495 case CPTK_IS_FINAL:
7496 case CPTK_IS_LITERAL_TYPE:
7497 case CPTK_IS_POD:
7498 case CPTK_IS_POLYMORPHIC:
7499 case CPTK_IS_STD_LAYOUT:
7500 case CPTK_IS_TRIVIAL:
7501 case CPTK_IS_TRIVIALLY_COPYABLE:
7502 if (!check_trait_type (type1))
7503 return error_mark_node;
7504 break;
7506 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7507 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7508 if (!check_trait_type (type1)
7509 || !check_trait_type (type2))
7510 return error_mark_node;
7511 break;
7513 case CPTK_IS_BASE_OF:
7514 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7515 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7516 && !complete_type_or_else (type2, NULL_TREE))
7517 /* We already issued an error. */
7518 return error_mark_node;
7519 break;
7521 case CPTK_IS_CLASS:
7522 case CPTK_IS_ENUM:
7523 case CPTK_IS_UNION:
7524 break;
7526 default:
7527 gcc_unreachable ();
7530 return (trait_expr_value (kind, type1, type2)
7531 ? boolean_true_node : boolean_false_node);
7534 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7535 which is ignored for C++. */
7537 void
7538 set_float_const_decimal64 (void)
7542 void
7543 clear_float_const_decimal64 (void)
7547 bool
7548 float_const_decimal64_p (void)
7550 return 0;
7554 /* Return true if T designates the implied `this' parameter. */
7556 bool
7557 is_this_parameter (tree t)
7559 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7560 return false;
7561 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7562 return true;
7565 /* Insert the deduced return type for an auto function. */
7567 void
7568 apply_deduced_return_type (tree fco, tree return_type)
7570 tree result;
7572 if (return_type == error_mark_node)
7573 return;
7575 if (LAMBDA_FUNCTION_P (fco))
7577 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7578 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7581 if (DECL_CONV_FN_P (fco))
7582 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7584 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7586 result = DECL_RESULT (fco);
7587 if (result == NULL_TREE)
7588 return;
7589 if (TREE_TYPE (result) == return_type)
7590 return;
7592 /* We already have a DECL_RESULT from start_preparsed_function.
7593 Now we need to redo the work it and allocate_struct_function
7594 did to reflect the new type. */
7595 gcc_assert (current_function_decl == fco);
7596 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7597 TYPE_MAIN_VARIANT (return_type));
7598 DECL_ARTIFICIAL (result) = 1;
7599 DECL_IGNORED_P (result) = 1;
7600 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7601 result);
7603 DECL_RESULT (fco) = result;
7605 if (!processing_template_decl)
7607 if (!VOID_TYPE_P (TREE_TYPE (result)))
7608 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7609 bool aggr = aggregate_value_p (result, fco);
7610 #ifdef PCC_STATIC_STRUCT_RETURN
7611 cfun->returns_pcc_struct = aggr;
7612 #endif
7613 cfun->returns_struct = aggr;
7618 /* DECL is a local variable or parameter from the surrounding scope of a
7619 lambda-expression. Returns the decltype for a use of the capture field
7620 for DECL even if it hasn't been captured yet. */
7622 static tree
7623 capture_decltype (tree decl)
7625 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7626 /* FIXME do lookup instead of list walk? */
7627 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7628 tree type;
7630 if (cap)
7631 type = TREE_TYPE (TREE_PURPOSE (cap));
7632 else
7633 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7635 case CPLD_NONE:
7636 error ("%qD is not captured", decl);
7637 return error_mark_node;
7639 case CPLD_COPY:
7640 type = TREE_TYPE (decl);
7641 if (TREE_CODE (type) == REFERENCE_TYPE
7642 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7643 type = TREE_TYPE (type);
7644 break;
7646 case CPLD_REFERENCE:
7647 type = TREE_TYPE (decl);
7648 if (TREE_CODE (type) != REFERENCE_TYPE)
7649 type = build_reference_type (TREE_TYPE (decl));
7650 break;
7652 default:
7653 gcc_unreachable ();
7656 if (TREE_CODE (type) != REFERENCE_TYPE)
7658 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7659 type = cp_build_qualified_type (type, (cp_type_quals (type)
7660 |TYPE_QUAL_CONST));
7661 type = build_reference_type (type);
7663 return type;
7666 #include "gt-cp-semantics.h"