svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cp / semantics.c
blob2457a6fb955a45ef6e05746f7511625110dfdbdd
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "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_sfinae (cond, tf_none);
515 cond = maybe_constant_value (cond);
516 maybe_infinite = integer_nonzerop (cond);
518 vec_safe_push (cp_function_chain->infinite_loops,
519 maybe_infinite ? error_mark_node : NULL_TREE);
523 /* A break is a possible exit for the current loop. */
525 void
526 break_maybe_infinite_loop (void)
528 if (!cfun)
529 return;
530 cp_function_chain->infinite_loops->last() = NULL_TREE;
533 /* If we reach the end of the loop without seeing a possible exit, we have
534 an infinite loop. */
536 static void
537 end_maybe_infinite_loop (tree cond)
539 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
540 && !processing_template_decl))
541 return;
542 tree current = cp_function_chain->infinite_loops->pop();
543 if (current != NULL_TREE)
545 cond = fold_non_dependent_expr (cond);
546 cond = maybe_constant_value (cond);
547 if (integer_nonzerop (cond))
548 current_function_infinite_loop = 1;
553 /* Begin a conditional that might contain a declaration. When generating
554 normal code, we want the declaration to appear before the statement
555 containing the conditional. When generating template code, we want the
556 conditional to be rendered as the raw DECL_EXPR. */
558 static void
559 begin_cond (tree *cond_p)
561 if (processing_template_decl)
562 *cond_p = push_stmt_list ();
565 /* Finish such a conditional. */
567 static void
568 finish_cond (tree *cond_p, tree expr)
570 if (processing_template_decl)
572 tree cond = pop_stmt_list (*cond_p);
574 if (expr == NULL_TREE)
575 /* Empty condition in 'for'. */
576 gcc_assert (empty_expr_stmt_p (cond));
577 else if (check_for_bare_parameter_packs (expr))
578 expr = error_mark_node;
579 else if (!empty_expr_stmt_p (cond))
580 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
582 *cond_p = expr;
585 /* If *COND_P specifies a conditional with a declaration, transform the
586 loop such that
587 while (A x = 42) { }
588 for (; A x = 42;) { }
589 becomes
590 while (true) { A x = 42; if (!x) break; }
591 for (;;) { A x = 42; if (!x) break; }
592 The statement list for BODY will be empty if the conditional did
593 not declare anything. */
595 static void
596 simplify_loop_decl_cond (tree *cond_p, tree body)
598 tree cond, if_stmt;
600 if (!TREE_SIDE_EFFECTS (body))
601 return;
603 cond = *cond_p;
604 *cond_p = boolean_true_node;
606 if_stmt = begin_if_stmt ();
607 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
608 finish_if_stmt_cond (cond, if_stmt);
609 finish_break_stmt ();
610 finish_then_clause (if_stmt);
611 finish_if_stmt (if_stmt);
614 /* Finish a goto-statement. */
616 tree
617 finish_goto_stmt (tree destination)
619 if (identifier_p (destination))
620 destination = lookup_label (destination);
622 /* We warn about unused labels with -Wunused. That means we have to
623 mark the used labels as used. */
624 if (TREE_CODE (destination) == LABEL_DECL)
625 TREE_USED (destination) = 1;
626 else
628 destination = mark_rvalue_use (destination);
629 if (!processing_template_decl)
631 destination = cp_convert (ptr_type_node, destination,
632 tf_warning_or_error);
633 if (error_operand_p (destination))
634 return NULL_TREE;
635 destination
636 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
637 destination);
641 check_goto (destination);
643 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
646 /* COND is the condition-expression for an if, while, etc.,
647 statement. Convert it to a boolean value, if appropriate.
648 In addition, verify sequence points if -Wsequence-point is enabled. */
650 static tree
651 maybe_convert_cond (tree cond)
653 /* Empty conditions remain empty. */
654 if (!cond)
655 return NULL_TREE;
657 /* Wait until we instantiate templates before doing conversion. */
658 if (processing_template_decl)
659 return cond;
661 if (warn_sequence_point)
662 verify_sequence_points (cond);
664 /* Do the conversion. */
665 cond = convert_from_reference (cond);
667 if (TREE_CODE (cond) == MODIFY_EXPR
668 && !TREE_NO_WARNING (cond)
669 && warn_parentheses)
671 warning (OPT_Wparentheses,
672 "suggest parentheses around assignment used as truth value");
673 TREE_NO_WARNING (cond) = 1;
676 return condition_conversion (cond);
679 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
681 tree
682 finish_expr_stmt (tree expr)
684 tree r = NULL_TREE;
686 if (expr != NULL_TREE)
688 if (!processing_template_decl)
690 if (warn_sequence_point)
691 verify_sequence_points (expr);
692 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
694 else if (!type_dependent_expression_p (expr))
695 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
696 tf_warning_or_error);
698 if (check_for_bare_parameter_packs (expr))
699 expr = error_mark_node;
701 /* Simplification of inner statement expressions, compound exprs,
702 etc can result in us already having an EXPR_STMT. */
703 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
705 if (TREE_CODE (expr) != EXPR_STMT)
706 expr = build_stmt (input_location, EXPR_STMT, expr);
707 expr = maybe_cleanup_point_expr_void (expr);
710 r = add_stmt (expr);
713 return r;
717 /* Begin an if-statement. Returns a newly created IF_STMT if
718 appropriate. */
720 tree
721 begin_if_stmt (void)
723 tree r, scope;
724 scope = do_pushlevel (sk_cond);
725 r = build_stmt (input_location, IF_STMT, NULL_TREE,
726 NULL_TREE, NULL_TREE, scope);
727 begin_cond (&IF_COND (r));
728 return r;
731 /* Process the COND of an if-statement, which may be given by
732 IF_STMT. */
734 void
735 finish_if_stmt_cond (tree cond, tree if_stmt)
737 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
738 add_stmt (if_stmt);
739 THEN_CLAUSE (if_stmt) = push_stmt_list ();
742 /* Finish the then-clause of an if-statement, which may be given by
743 IF_STMT. */
745 tree
746 finish_then_clause (tree if_stmt)
748 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
749 return if_stmt;
752 /* Begin the else-clause of an if-statement. */
754 void
755 begin_else_clause (tree if_stmt)
757 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
760 /* Finish the else-clause of an if-statement, which may be given by
761 IF_STMT. */
763 void
764 finish_else_clause (tree if_stmt)
766 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
769 /* Finish an if-statement. */
771 void
772 finish_if_stmt (tree if_stmt)
774 tree scope = IF_SCOPE (if_stmt);
775 IF_SCOPE (if_stmt) = NULL;
776 add_stmt (do_poplevel (scope));
779 /* Begin a while-statement. Returns a newly created WHILE_STMT if
780 appropriate. */
782 tree
783 begin_while_stmt (void)
785 tree r;
786 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
787 add_stmt (r);
788 WHILE_BODY (r) = do_pushlevel (sk_block);
789 begin_cond (&WHILE_COND (r));
790 return r;
793 /* Process the COND of a while-statement, which may be given by
794 WHILE_STMT. */
796 void
797 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
799 cond = maybe_convert_cond (cond);
800 finish_cond (&WHILE_COND (while_stmt), cond);
801 begin_maybe_infinite_loop (cond);
802 if (ivdep && cond != error_mark_node)
803 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
804 TREE_TYPE (WHILE_COND (while_stmt)),
805 WHILE_COND (while_stmt),
806 build_int_cst (integer_type_node,
807 annot_expr_ivdep_kind));
808 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
811 /* Finish a while-statement, which may be given by WHILE_STMT. */
813 void
814 finish_while_stmt (tree while_stmt)
816 end_maybe_infinite_loop (boolean_true_node);
817 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
820 /* Begin a do-statement. Returns a newly created DO_STMT if
821 appropriate. */
823 tree
824 begin_do_stmt (void)
826 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
827 begin_maybe_infinite_loop (boolean_true_node);
828 add_stmt (r);
829 DO_BODY (r) = push_stmt_list ();
830 return r;
833 /* Finish the body of a do-statement, which may be given by DO_STMT. */
835 void
836 finish_do_body (tree do_stmt)
838 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
840 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
841 body = STATEMENT_LIST_TAIL (body)->stmt;
843 if (IS_EMPTY_STMT (body))
844 warning (OPT_Wempty_body,
845 "suggest explicit braces around empty body in %<do%> statement");
848 /* Finish a do-statement, which may be given by DO_STMT, and whose
849 COND is as indicated. */
851 void
852 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
854 cond = maybe_convert_cond (cond);
855 end_maybe_infinite_loop (cond);
856 if (ivdep && cond != error_mark_node)
857 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
858 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
859 DO_COND (do_stmt) = cond;
862 /* Finish a return-statement. The EXPRESSION returned, if any, is as
863 indicated. */
865 tree
866 finish_return_stmt (tree expr)
868 tree r;
869 bool no_warning;
871 expr = check_return_expr (expr, &no_warning);
873 if (error_operand_p (expr)
874 || (flag_openmp && !check_omp_return ()))
875 return error_mark_node;
876 if (!processing_template_decl)
878 if (warn_sequence_point)
879 verify_sequence_points (expr);
881 if (DECL_DESTRUCTOR_P (current_function_decl)
882 || (DECL_CONSTRUCTOR_P (current_function_decl)
883 && targetm.cxx.cdtor_returns_this ()))
885 /* Similarly, all destructors must run destructors for
886 base-classes before returning. So, all returns in a
887 destructor get sent to the DTOR_LABEL; finish_function emits
888 code to return a value there. */
889 return finish_goto_stmt (cdtor_label);
893 r = build_stmt (input_location, RETURN_EXPR, expr);
894 TREE_NO_WARNING (r) |= no_warning;
895 r = maybe_cleanup_point_expr_void (r);
896 r = add_stmt (r);
898 return r;
901 /* Begin the scope of a for-statement or a range-for-statement.
902 Both the returned trees are to be used in a call to
903 begin_for_stmt or begin_range_for_stmt. */
905 tree
906 begin_for_scope (tree *init)
908 tree scope = NULL_TREE;
909 if (flag_new_for_scope > 0)
910 scope = do_pushlevel (sk_for);
912 if (processing_template_decl)
913 *init = push_stmt_list ();
914 else
915 *init = NULL_TREE;
917 return scope;
920 /* Begin a for-statement. Returns a new FOR_STMT.
921 SCOPE and INIT should be the return of begin_for_scope,
922 or both NULL_TREE */
924 tree
925 begin_for_stmt (tree scope, tree init)
927 tree r;
929 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
930 NULL_TREE, NULL_TREE, NULL_TREE);
932 if (scope == NULL_TREE)
934 gcc_assert (!init || !(flag_new_for_scope > 0));
935 if (!init)
936 scope = begin_for_scope (&init);
938 FOR_INIT_STMT (r) = init;
939 FOR_SCOPE (r) = scope;
941 return r;
944 /* Finish the for-init-statement of a for-statement, which may be
945 given by FOR_STMT. */
947 void
948 finish_for_init_stmt (tree for_stmt)
950 if (processing_template_decl)
951 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
952 add_stmt (for_stmt);
953 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
954 begin_cond (&FOR_COND (for_stmt));
957 /* Finish the COND of a for-statement, which may be given by
958 FOR_STMT. */
960 void
961 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
963 cond = maybe_convert_cond (cond);
964 finish_cond (&FOR_COND (for_stmt), cond);
965 begin_maybe_infinite_loop (cond);
966 if (ivdep && cond != error_mark_node)
967 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
968 TREE_TYPE (FOR_COND (for_stmt)),
969 FOR_COND (for_stmt),
970 build_int_cst (integer_type_node,
971 annot_expr_ivdep_kind));
972 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
975 /* Finish the increment-EXPRESSION in a for-statement, which may be
976 given by FOR_STMT. */
978 void
979 finish_for_expr (tree expr, tree for_stmt)
981 if (!expr)
982 return;
983 /* If EXPR is an overloaded function, issue an error; there is no
984 context available to use to perform overload resolution. */
985 if (type_unknown_p (expr))
987 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
988 expr = error_mark_node;
990 if (!processing_template_decl)
992 if (warn_sequence_point)
993 verify_sequence_points (expr);
994 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
995 tf_warning_or_error);
997 else if (!type_dependent_expression_p (expr))
998 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
999 tf_warning_or_error);
1000 expr = maybe_cleanup_point_expr_void (expr);
1001 if (check_for_bare_parameter_packs (expr))
1002 expr = error_mark_node;
1003 FOR_EXPR (for_stmt) = expr;
1006 /* Finish the body of a for-statement, which may be given by
1007 FOR_STMT. The increment-EXPR for the loop must be
1008 provided.
1009 It can also finish RANGE_FOR_STMT. */
1011 void
1012 finish_for_stmt (tree for_stmt)
1014 end_maybe_infinite_loop (boolean_true_node);
1016 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1017 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1018 else
1019 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1021 /* Pop the scope for the body of the loop. */
1022 if (flag_new_for_scope > 0)
1024 tree scope;
1025 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1026 ? &RANGE_FOR_SCOPE (for_stmt)
1027 : &FOR_SCOPE (for_stmt));
1028 scope = *scope_ptr;
1029 *scope_ptr = NULL;
1030 add_stmt (do_poplevel (scope));
1034 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1035 SCOPE and INIT should be the return of begin_for_scope,
1036 or both NULL_TREE .
1037 To finish it call finish_for_stmt(). */
1039 tree
1040 begin_range_for_stmt (tree scope, tree init)
1042 tree r;
1044 begin_maybe_infinite_loop (boolean_false_node);
1046 r = build_stmt (input_location, RANGE_FOR_STMT,
1047 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1049 if (scope == NULL_TREE)
1051 gcc_assert (!init || !(flag_new_for_scope > 0));
1052 if (!init)
1053 scope = begin_for_scope (&init);
1056 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1057 pop it now. */
1058 if (init)
1059 pop_stmt_list (init);
1060 RANGE_FOR_SCOPE (r) = scope;
1062 return r;
1065 /* Finish the head of a range-based for statement, which may
1066 be given by RANGE_FOR_STMT. DECL must be the declaration
1067 and EXPR must be the loop expression. */
1069 void
1070 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1072 RANGE_FOR_DECL (range_for_stmt) = decl;
1073 RANGE_FOR_EXPR (range_for_stmt) = expr;
1074 add_stmt (range_for_stmt);
1075 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1078 /* Finish a break-statement. */
1080 tree
1081 finish_break_stmt (void)
1083 /* In switch statements break is sometimes stylistically used after
1084 a return statement. This can lead to spurious warnings about
1085 control reaching the end of a non-void function when it is
1086 inlined. Note that we are calling block_may_fallthru with
1087 language specific tree nodes; this works because
1088 block_may_fallthru returns true when given something it does not
1089 understand. */
1090 if (!block_may_fallthru (cur_stmt_list))
1091 return void_node;
1092 return add_stmt (build_stmt (input_location, BREAK_STMT));
1095 /* Finish a continue-statement. */
1097 tree
1098 finish_continue_stmt (void)
1100 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1103 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1104 appropriate. */
1106 tree
1107 begin_switch_stmt (void)
1109 tree r, scope;
1111 scope = do_pushlevel (sk_cond);
1112 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1114 begin_cond (&SWITCH_STMT_COND (r));
1116 return r;
1119 /* Finish the cond of a switch-statement. */
1121 void
1122 finish_switch_cond (tree cond, tree switch_stmt)
1124 tree orig_type = NULL;
1125 if (!processing_template_decl)
1127 /* Convert the condition to an integer or enumeration type. */
1128 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1129 if (cond == NULL_TREE)
1131 error ("switch quantity not an integer");
1132 cond = error_mark_node;
1134 /* We want unlowered type here to handle enum bit-fields. */
1135 orig_type = unlowered_expr_type (cond);
1136 if (cond != error_mark_node)
1138 /* Warn if the condition has boolean value. */
1139 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1140 warning_at (input_location, OPT_Wswitch_bool,
1141 "switch condition has type bool");
1143 /* [stmt.switch]
1145 Integral promotions are performed. */
1146 cond = perform_integral_promotions (cond);
1147 cond = maybe_cleanup_point_expr (cond);
1150 if (check_for_bare_parameter_packs (cond))
1151 cond = error_mark_node;
1152 else if (!processing_template_decl && warn_sequence_point)
1153 verify_sequence_points (cond);
1155 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1156 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1157 add_stmt (switch_stmt);
1158 push_switch (switch_stmt);
1159 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1162 /* Finish the body of a switch-statement, which may be given by
1163 SWITCH_STMT. The COND to switch on is indicated. */
1165 void
1166 finish_switch_stmt (tree switch_stmt)
1168 tree scope;
1170 SWITCH_STMT_BODY (switch_stmt) =
1171 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1172 pop_switch ();
1174 scope = SWITCH_STMT_SCOPE (switch_stmt);
1175 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1176 add_stmt (do_poplevel (scope));
1179 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1180 appropriate. */
1182 tree
1183 begin_try_block (void)
1185 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1186 add_stmt (r);
1187 TRY_STMTS (r) = push_stmt_list ();
1188 return r;
1191 /* Likewise, for a function-try-block. The block returned in
1192 *COMPOUND_STMT is an artificial outer scope, containing the
1193 function-try-block. */
1195 tree
1196 begin_function_try_block (tree *compound_stmt)
1198 tree r;
1199 /* This outer scope does not exist in the C++ standard, but we need
1200 a place to put __FUNCTION__ and similar variables. */
1201 *compound_stmt = begin_compound_stmt (0);
1202 r = begin_try_block ();
1203 FN_TRY_BLOCK_P (r) = 1;
1204 return r;
1207 /* Finish a try-block, which may be given by TRY_BLOCK. */
1209 void
1210 finish_try_block (tree try_block)
1212 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1213 TRY_HANDLERS (try_block) = push_stmt_list ();
1216 /* Finish the body of a cleanup try-block, which may be given by
1217 TRY_BLOCK. */
1219 void
1220 finish_cleanup_try_block (tree try_block)
1222 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1225 /* Finish an implicitly generated try-block, with a cleanup is given
1226 by CLEANUP. */
1228 void
1229 finish_cleanup (tree cleanup, tree try_block)
1231 TRY_HANDLERS (try_block) = cleanup;
1232 CLEANUP_P (try_block) = 1;
1235 /* Likewise, for a function-try-block. */
1237 void
1238 finish_function_try_block (tree try_block)
1240 finish_try_block (try_block);
1241 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1242 the try block, but moving it inside. */
1243 in_function_try_handler = 1;
1246 /* Finish a handler-sequence for a try-block, which may be given by
1247 TRY_BLOCK. */
1249 void
1250 finish_handler_sequence (tree try_block)
1252 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1253 check_handlers (TRY_HANDLERS (try_block));
1256 /* Finish the handler-seq for a function-try-block, given by
1257 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1258 begin_function_try_block. */
1260 void
1261 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1263 in_function_try_handler = 0;
1264 finish_handler_sequence (try_block);
1265 finish_compound_stmt (compound_stmt);
1268 /* Begin a handler. Returns a HANDLER if appropriate. */
1270 tree
1271 begin_handler (void)
1273 tree r;
1275 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1276 add_stmt (r);
1278 /* Create a binding level for the eh_info and the exception object
1279 cleanup. */
1280 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1282 return r;
1285 /* Finish the handler-parameters for a handler, which may be given by
1286 HANDLER. DECL is the declaration for the catch parameter, or NULL
1287 if this is a `catch (...)' clause. */
1289 void
1290 finish_handler_parms (tree decl, tree handler)
1292 tree type = NULL_TREE;
1293 if (processing_template_decl)
1295 if (decl)
1297 decl = pushdecl (decl);
1298 decl = push_template_decl (decl);
1299 HANDLER_PARMS (handler) = decl;
1300 type = TREE_TYPE (decl);
1303 else
1304 type = expand_start_catch_block (decl);
1305 HANDLER_TYPE (handler) = type;
1308 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1309 the return value from the matching call to finish_handler_parms. */
1311 void
1312 finish_handler (tree handler)
1314 if (!processing_template_decl)
1315 expand_end_catch_block ();
1316 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1319 /* Begin a compound statement. FLAGS contains some bits that control the
1320 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1321 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1322 block of a function. If BCS_TRY_BLOCK is set, this is the block
1323 created on behalf of a TRY statement. Returns a token to be passed to
1324 finish_compound_stmt. */
1326 tree
1327 begin_compound_stmt (unsigned int flags)
1329 tree r;
1331 if (flags & BCS_NO_SCOPE)
1333 r = push_stmt_list ();
1334 STATEMENT_LIST_NO_SCOPE (r) = 1;
1336 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1337 But, if it's a statement-expression with a scopeless block, there's
1338 nothing to keep, and we don't want to accidentally keep a block
1339 *inside* the scopeless block. */
1340 keep_next_level (false);
1342 else
1343 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1345 /* When processing a template, we need to remember where the braces were,
1346 so that we can set up identical scopes when instantiating the template
1347 later. BIND_EXPR is a handy candidate for this.
1348 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1349 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1350 processing templates. */
1351 if (processing_template_decl)
1353 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1354 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1355 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1356 TREE_SIDE_EFFECTS (r) = 1;
1359 return r;
1362 /* Finish a compound-statement, which is given by STMT. */
1364 void
1365 finish_compound_stmt (tree stmt)
1367 if (TREE_CODE (stmt) == BIND_EXPR)
1369 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1370 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1371 discard the BIND_EXPR so it can be merged with the containing
1372 STATEMENT_LIST. */
1373 if (TREE_CODE (body) == STATEMENT_LIST
1374 && STATEMENT_LIST_HEAD (body) == NULL
1375 && !BIND_EXPR_BODY_BLOCK (stmt)
1376 && !BIND_EXPR_TRY_BLOCK (stmt))
1377 stmt = body;
1378 else
1379 BIND_EXPR_BODY (stmt) = body;
1381 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1382 stmt = pop_stmt_list (stmt);
1383 else
1385 /* Destroy any ObjC "super" receivers that may have been
1386 created. */
1387 objc_clear_super_receiver ();
1389 stmt = do_poplevel (stmt);
1392 /* ??? See c_end_compound_stmt wrt statement expressions. */
1393 add_stmt (stmt);
1396 /* Finish an asm-statement, whose components are a STRING, some
1397 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1398 LABELS. Also note whether the asm-statement should be
1399 considered volatile. */
1401 tree
1402 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1403 tree input_operands, tree clobbers, tree labels)
1405 tree r;
1406 tree t;
1407 int ninputs = list_length (input_operands);
1408 int noutputs = list_length (output_operands);
1410 if (!processing_template_decl)
1412 const char *constraint;
1413 const char **oconstraints;
1414 bool allows_mem, allows_reg, is_inout;
1415 tree operand;
1416 int i;
1418 oconstraints = XALLOCAVEC (const char *, noutputs);
1420 string = resolve_asm_operand_names (string, output_operands,
1421 input_operands, labels);
1423 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1425 operand = TREE_VALUE (t);
1427 /* ??? Really, this should not be here. Users should be using a
1428 proper lvalue, dammit. But there's a long history of using
1429 casts in the output operands. In cases like longlong.h, this
1430 becomes a primitive form of typechecking -- if the cast can be
1431 removed, then the output operand had a type of the proper width;
1432 otherwise we'll get an error. Gross, but ... */
1433 STRIP_NOPS (operand);
1435 operand = mark_lvalue_use (operand);
1437 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1438 operand = error_mark_node;
1440 if (operand != error_mark_node
1441 && (TREE_READONLY (operand)
1442 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1443 /* Functions are not modifiable, even though they are
1444 lvalues. */
1445 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1446 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1447 /* If it's an aggregate and any field is const, then it is
1448 effectively const. */
1449 || (CLASS_TYPE_P (TREE_TYPE (operand))
1450 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1451 cxx_readonly_error (operand, lv_asm);
1453 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1454 oconstraints[i] = constraint;
1456 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1457 &allows_mem, &allows_reg, &is_inout))
1459 /* If the operand is going to end up in memory,
1460 mark it addressable. */
1461 if (!allows_reg && !cxx_mark_addressable (operand))
1462 operand = error_mark_node;
1464 else
1465 operand = error_mark_node;
1467 TREE_VALUE (t) = operand;
1470 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1472 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1473 bool constraint_parsed
1474 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1475 oconstraints, &allows_mem, &allows_reg);
1476 /* If the operand is going to end up in memory, don't call
1477 decay_conversion. */
1478 if (constraint_parsed && !allows_reg && allows_mem)
1479 operand = mark_lvalue_use (TREE_VALUE (t));
1480 else
1481 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1483 /* If the type of the operand hasn't been determined (e.g.,
1484 because it involves an overloaded function), then issue
1485 an error message. There's no context available to
1486 resolve the overloading. */
1487 if (TREE_TYPE (operand) == unknown_type_node)
1489 error ("type of asm operand %qE could not be determined",
1490 TREE_VALUE (t));
1491 operand = error_mark_node;
1494 if (constraint_parsed)
1496 /* If the operand is going to end up in memory,
1497 mark it addressable. */
1498 if (!allows_reg && allows_mem)
1500 /* Strip the nops as we allow this case. FIXME, this really
1501 should be rejected or made deprecated. */
1502 STRIP_NOPS (operand);
1503 if (!cxx_mark_addressable (operand))
1504 operand = error_mark_node;
1506 else if (!allows_reg && !allows_mem)
1508 /* If constraint allows neither register nor memory,
1509 try harder to get a constant. */
1510 tree constop = maybe_constant_value (operand);
1511 if (TREE_CONSTANT (constop))
1512 operand = constop;
1515 else
1516 operand = error_mark_node;
1518 TREE_VALUE (t) = operand;
1522 r = build_stmt (input_location, ASM_EXPR, string,
1523 output_operands, input_operands,
1524 clobbers, labels);
1525 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1526 r = maybe_cleanup_point_expr_void (r);
1527 return add_stmt (r);
1530 /* Finish a label with the indicated NAME. Returns the new label. */
1532 tree
1533 finish_label_stmt (tree name)
1535 tree decl = define_label (input_location, name);
1537 if (decl == error_mark_node)
1538 return error_mark_node;
1540 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1542 return decl;
1545 /* Finish a series of declarations for local labels. G++ allows users
1546 to declare "local" labels, i.e., labels with scope. This extension
1547 is useful when writing code involving statement-expressions. */
1549 void
1550 finish_label_decl (tree name)
1552 if (!at_function_scope_p ())
1554 error ("__label__ declarations are only allowed in function scopes");
1555 return;
1558 add_decl_expr (declare_local_label (name));
1561 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1563 void
1564 finish_decl_cleanup (tree decl, tree cleanup)
1566 push_cleanup (decl, cleanup, false);
1569 /* If the current scope exits with an exception, run CLEANUP. */
1571 void
1572 finish_eh_cleanup (tree cleanup)
1574 push_cleanup (NULL, cleanup, true);
1577 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1578 order they were written by the user. Each node is as for
1579 emit_mem_initializers. */
1581 void
1582 finish_mem_initializers (tree mem_inits)
1584 /* Reorder the MEM_INITS so that they are in the order they appeared
1585 in the source program. */
1586 mem_inits = nreverse (mem_inits);
1588 if (processing_template_decl)
1590 tree mem;
1592 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1594 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1595 check for bare parameter packs in the TREE_VALUE, because
1596 any parameter packs in the TREE_VALUE have already been
1597 bound as part of the TREE_PURPOSE. See
1598 make_pack_expansion for more information. */
1599 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1600 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1601 TREE_VALUE (mem) = error_mark_node;
1604 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1605 CTOR_INITIALIZER, mem_inits));
1607 else
1608 emit_mem_initializers (mem_inits);
1611 /* Obfuscate EXPR if it looks like an id-expression or member access so
1612 that the call to finish_decltype in do_auto_deduction will give the
1613 right result. */
1615 tree
1616 force_paren_expr (tree expr)
1618 /* This is only needed for decltype(auto) in C++14. */
1619 if (cxx_dialect < cxx14)
1620 return expr;
1622 /* If we're in unevaluated context, we can't be deducing a
1623 return/initializer type, so we don't need to mess with this. */
1624 if (cp_unevaluated_operand)
1625 return expr;
1627 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1628 && TREE_CODE (expr) != SCOPE_REF)
1629 return expr;
1631 if (TREE_CODE (expr) == COMPONENT_REF)
1632 REF_PARENTHESIZED_P (expr) = true;
1633 else if (type_dependent_expression_p (expr))
1634 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1635 else
1637 cp_lvalue_kind kind = lvalue_kind (expr);
1638 if ((kind & ~clk_class) != clk_none)
1640 tree type = unlowered_expr_type (expr);
1641 bool rval = !!(kind & clk_rvalueref);
1642 type = cp_build_reference_type (type, rval);
1643 expr = build_static_cast (type, expr, tf_error);
1644 if (expr != error_mark_node)
1645 REF_PARENTHESIZED_P (expr) = true;
1649 return expr;
1652 /* Finish a parenthesized expression EXPR. */
1654 tree
1655 finish_parenthesized_expr (tree expr)
1657 if (EXPR_P (expr))
1658 /* This inhibits warnings in c_common_truthvalue_conversion. */
1659 TREE_NO_WARNING (expr) = 1;
1661 if (TREE_CODE (expr) == OFFSET_REF
1662 || TREE_CODE (expr) == SCOPE_REF)
1663 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1664 enclosed in parentheses. */
1665 PTRMEM_OK_P (expr) = 0;
1667 if (TREE_CODE (expr) == STRING_CST)
1668 PAREN_STRING_LITERAL_P (expr) = 1;
1670 expr = force_paren_expr (expr);
1672 return expr;
1675 /* Finish a reference to a non-static data member (DECL) that is not
1676 preceded by `.' or `->'. */
1678 tree
1679 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1681 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1683 if (!object)
1685 tree scope = qualifying_scope;
1686 if (scope == NULL_TREE)
1687 scope = context_for_name_lookup (decl);
1688 object = maybe_dummy_object (scope, NULL);
1691 object = maybe_resolve_dummy (object, true);
1692 if (object == error_mark_node)
1693 return error_mark_node;
1695 /* DR 613/850: Can use non-static data members without an associated
1696 object in sizeof/decltype/alignof. */
1697 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1698 && (!processing_template_decl || !current_class_ref))
1700 if (current_function_decl
1701 && DECL_STATIC_FUNCTION_P (current_function_decl))
1702 error ("invalid use of member %qD in static member function", decl);
1703 else
1704 error ("invalid use of non-static data member %qD", decl);
1705 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1707 return error_mark_node;
1710 if (current_class_ptr)
1711 TREE_USED (current_class_ptr) = 1;
1712 if (processing_template_decl && !qualifying_scope)
1714 tree type = TREE_TYPE (decl);
1716 if (TREE_CODE (type) == REFERENCE_TYPE)
1717 /* Quals on the object don't matter. */;
1718 else if (PACK_EXPANSION_P (type))
1719 /* Don't bother trying to represent this. */
1720 type = NULL_TREE;
1721 else
1723 /* Set the cv qualifiers. */
1724 int quals = cp_type_quals (TREE_TYPE (object));
1726 if (DECL_MUTABLE_P (decl))
1727 quals &= ~TYPE_QUAL_CONST;
1729 quals |= cp_type_quals (TREE_TYPE (decl));
1730 type = cp_build_qualified_type (type, quals);
1733 return (convert_from_reference
1734 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1736 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1737 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1738 for now. */
1739 else if (processing_template_decl)
1740 return build_qualified_name (TREE_TYPE (decl),
1741 qualifying_scope,
1742 decl,
1743 /*template_p=*/false);
1744 else
1746 tree access_type = TREE_TYPE (object);
1748 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1749 decl, tf_warning_or_error);
1751 /* If the data member was named `C::M', convert `*this' to `C'
1752 first. */
1753 if (qualifying_scope)
1755 tree binfo = NULL_TREE;
1756 object = build_scoped_ref (object, qualifying_scope,
1757 &binfo);
1760 return build_class_member_access_expr (object, decl,
1761 /*access_path=*/NULL_TREE,
1762 /*preserve_reference=*/false,
1763 tf_warning_or_error);
1767 /* If we are currently parsing a template and we encountered a typedef
1768 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1769 adds the typedef to a list tied to the current template.
1770 At template instantiation time, that list is walked and access check
1771 performed for each typedef.
1772 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1774 void
1775 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1776 tree context,
1777 location_t location)
1779 tree template_info = NULL;
1780 tree cs = current_scope ();
1782 if (!is_typedef_decl (typedef_decl)
1783 || !context
1784 || !CLASS_TYPE_P (context)
1785 || !cs)
1786 return;
1788 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1789 template_info = get_template_info (cs);
1791 if (template_info
1792 && TI_TEMPLATE (template_info)
1793 && !currently_open_class (context))
1794 append_type_to_template_for_access_check (cs, typedef_decl,
1795 context, location);
1798 /* DECL was the declaration to which a qualified-id resolved. Issue
1799 an error message if it is not accessible. If OBJECT_TYPE is
1800 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1801 type of `*x', or `x', respectively. If the DECL was named as
1802 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1804 void
1805 check_accessibility_of_qualified_id (tree decl,
1806 tree object_type,
1807 tree nested_name_specifier)
1809 tree scope;
1810 tree qualifying_type = NULL_TREE;
1812 /* If we are parsing a template declaration and if decl is a typedef,
1813 add it to a list tied to the template.
1814 At template instantiation time, that list will be walked and
1815 access check performed. */
1816 add_typedef_to_current_template_for_access_check (decl,
1817 nested_name_specifier
1818 ? nested_name_specifier
1819 : DECL_CONTEXT (decl),
1820 input_location);
1822 /* If we're not checking, return immediately. */
1823 if (deferred_access_no_check)
1824 return;
1826 /* Determine the SCOPE of DECL. */
1827 scope = context_for_name_lookup (decl);
1828 /* If the SCOPE is not a type, then DECL is not a member. */
1829 if (!TYPE_P (scope))
1830 return;
1831 /* Compute the scope through which DECL is being accessed. */
1832 if (object_type
1833 /* OBJECT_TYPE might not be a class type; consider:
1835 class A { typedef int I; };
1836 I *p;
1837 p->A::I::~I();
1839 In this case, we will have "A::I" as the DECL, but "I" as the
1840 OBJECT_TYPE. */
1841 && CLASS_TYPE_P (object_type)
1842 && DERIVED_FROM_P (scope, object_type))
1843 /* If we are processing a `->' or `.' expression, use the type of the
1844 left-hand side. */
1845 qualifying_type = object_type;
1846 else if (nested_name_specifier)
1848 /* If the reference is to a non-static member of the
1849 current class, treat it as if it were referenced through
1850 `this'. */
1851 tree ct;
1852 if (DECL_NONSTATIC_MEMBER_P (decl)
1853 && current_class_ptr
1854 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1855 qualifying_type = ct;
1856 /* Otherwise, use the type indicated by the
1857 nested-name-specifier. */
1858 else
1859 qualifying_type = nested_name_specifier;
1861 else
1862 /* Otherwise, the name must be from the current class or one of
1863 its bases. */
1864 qualifying_type = currently_open_derived_class (scope);
1866 if (qualifying_type
1867 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1868 or similar in a default argument value. */
1869 && CLASS_TYPE_P (qualifying_type)
1870 && !dependent_type_p (qualifying_type))
1871 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1872 decl, tf_warning_or_error);
1875 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1876 class named to the left of the "::" operator. DONE is true if this
1877 expression is a complete postfix-expression; it is false if this
1878 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1879 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1880 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1881 is true iff this qualified name appears as a template argument. */
1883 tree
1884 finish_qualified_id_expr (tree qualifying_class,
1885 tree expr,
1886 bool done,
1887 bool address_p,
1888 bool template_p,
1889 bool template_arg_p,
1890 tsubst_flags_t complain)
1892 gcc_assert (TYPE_P (qualifying_class));
1894 if (error_operand_p (expr))
1895 return error_mark_node;
1897 if ((DECL_P (expr) || BASELINK_P (expr))
1898 && !mark_used (expr, complain))
1899 return error_mark_node;
1901 if (template_p)
1902 check_template_keyword (expr);
1904 /* If EXPR occurs as the operand of '&', use special handling that
1905 permits a pointer-to-member. */
1906 if (address_p && done)
1908 if (TREE_CODE (expr) == SCOPE_REF)
1909 expr = TREE_OPERAND (expr, 1);
1910 expr = build_offset_ref (qualifying_class, expr,
1911 /*address_p=*/true, complain);
1912 return expr;
1915 /* No need to check access within an enum. */
1916 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1917 return expr;
1919 /* Within the scope of a class, turn references to non-static
1920 members into expression of the form "this->...". */
1921 if (template_arg_p)
1922 /* But, within a template argument, we do not want make the
1923 transformation, as there is no "this" pointer. */
1925 else if (TREE_CODE (expr) == FIELD_DECL)
1927 push_deferring_access_checks (dk_no_check);
1928 expr = finish_non_static_data_member (expr, NULL_TREE,
1929 qualifying_class);
1930 pop_deferring_access_checks ();
1932 else if (BASELINK_P (expr) && !processing_template_decl)
1934 /* See if any of the functions are non-static members. */
1935 /* If so, the expression may be relative to 'this'. */
1936 if (!shared_member_p (expr)
1937 && current_class_ptr
1938 && DERIVED_FROM_P (qualifying_class,
1939 current_nonlambda_class_type ()))
1940 expr = (build_class_member_access_expr
1941 (maybe_dummy_object (qualifying_class, NULL),
1942 expr,
1943 BASELINK_ACCESS_BINFO (expr),
1944 /*preserve_reference=*/false,
1945 complain));
1946 else if (done)
1947 /* The expression is a qualified name whose address is not
1948 being taken. */
1949 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1950 complain);
1952 else if (BASELINK_P (expr))
1954 else
1956 /* In a template, return a SCOPE_REF for most qualified-ids
1957 so that we can check access at instantiation time. But if
1958 we're looking at a member of the current instantiation, we
1959 know we have access and building up the SCOPE_REF confuses
1960 non-type template argument handling. */
1961 if (processing_template_decl
1962 && !currently_open_class (qualifying_class))
1963 expr = build_qualified_name (TREE_TYPE (expr),
1964 qualifying_class, expr,
1965 template_p);
1967 expr = convert_from_reference (expr);
1970 return expr;
1973 /* Begin a statement-expression. The value returned must be passed to
1974 finish_stmt_expr. */
1976 tree
1977 begin_stmt_expr (void)
1979 return push_stmt_list ();
1982 /* Process the final expression of a statement expression. EXPR can be
1983 NULL, if the final expression is empty. Return a STATEMENT_LIST
1984 containing all the statements in the statement-expression, or
1985 ERROR_MARK_NODE if there was an error. */
1987 tree
1988 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1990 if (error_operand_p (expr))
1992 /* The type of the statement-expression is the type of the last
1993 expression. */
1994 TREE_TYPE (stmt_expr) = error_mark_node;
1995 return error_mark_node;
1998 /* If the last statement does not have "void" type, then the value
1999 of the last statement is the value of the entire expression. */
2000 if (expr)
2002 tree type = TREE_TYPE (expr);
2004 if (processing_template_decl)
2006 expr = build_stmt (input_location, EXPR_STMT, expr);
2007 expr = add_stmt (expr);
2008 /* Mark the last statement so that we can recognize it as such at
2009 template-instantiation time. */
2010 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2012 else if (VOID_TYPE_P (type))
2014 /* Just treat this like an ordinary statement. */
2015 expr = finish_expr_stmt (expr);
2017 else
2019 /* It actually has a value we need to deal with. First, force it
2020 to be an rvalue so that we won't need to build up a copy
2021 constructor call later when we try to assign it to something. */
2022 expr = force_rvalue (expr, tf_warning_or_error);
2023 if (error_operand_p (expr))
2024 return error_mark_node;
2026 /* Update for array-to-pointer decay. */
2027 type = TREE_TYPE (expr);
2029 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2030 normal statement, but don't convert to void or actually add
2031 the EXPR_STMT. */
2032 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2033 expr = maybe_cleanup_point_expr (expr);
2034 add_stmt (expr);
2037 /* The type of the statement-expression is the type of the last
2038 expression. */
2039 TREE_TYPE (stmt_expr) = type;
2042 return stmt_expr;
2045 /* Finish a statement-expression. EXPR should be the value returned
2046 by the previous begin_stmt_expr. Returns an expression
2047 representing the statement-expression. */
2049 tree
2050 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2052 tree type;
2053 tree result;
2055 if (error_operand_p (stmt_expr))
2057 pop_stmt_list (stmt_expr);
2058 return error_mark_node;
2061 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2063 type = TREE_TYPE (stmt_expr);
2064 result = pop_stmt_list (stmt_expr);
2065 TREE_TYPE (result) = type;
2067 if (processing_template_decl)
2069 result = build_min (STMT_EXPR, type, result);
2070 TREE_SIDE_EFFECTS (result) = 1;
2071 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2073 else if (CLASS_TYPE_P (type))
2075 /* Wrap the statement-expression in a TARGET_EXPR so that the
2076 temporary object created by the final expression is destroyed at
2077 the end of the full-expression containing the
2078 statement-expression. */
2079 result = force_target_expr (type, result, tf_warning_or_error);
2082 return result;
2085 /* Returns the expression which provides the value of STMT_EXPR. */
2087 tree
2088 stmt_expr_value_expr (tree stmt_expr)
2090 tree t = STMT_EXPR_STMT (stmt_expr);
2092 if (TREE_CODE (t) == BIND_EXPR)
2093 t = BIND_EXPR_BODY (t);
2095 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2096 t = STATEMENT_LIST_TAIL (t)->stmt;
2098 if (TREE_CODE (t) == EXPR_STMT)
2099 t = EXPR_STMT_EXPR (t);
2101 return t;
2104 /* Return TRUE iff EXPR_STMT is an empty list of
2105 expression statements. */
2107 bool
2108 empty_expr_stmt_p (tree expr_stmt)
2110 tree body = NULL_TREE;
2112 if (expr_stmt == void_node)
2113 return true;
2115 if (expr_stmt)
2117 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2118 body = EXPR_STMT_EXPR (expr_stmt);
2119 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2120 body = expr_stmt;
2123 if (body)
2125 if (TREE_CODE (body) == STATEMENT_LIST)
2126 return tsi_end_p (tsi_start (body));
2127 else
2128 return empty_expr_stmt_p (body);
2130 return false;
2133 /* Perform Koenig lookup. FN is the postfix-expression representing
2134 the function (or functions) to call; ARGS are the arguments to the
2135 call. Returns the functions to be considered by overload resolution. */
2137 tree
2138 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2139 tsubst_flags_t complain)
2141 tree identifier = NULL_TREE;
2142 tree functions = NULL_TREE;
2143 tree tmpl_args = NULL_TREE;
2144 bool template_id = false;
2146 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2148 /* Use a separate flag to handle null args. */
2149 template_id = true;
2150 tmpl_args = TREE_OPERAND (fn, 1);
2151 fn = TREE_OPERAND (fn, 0);
2154 /* Find the name of the overloaded function. */
2155 if (identifier_p (fn))
2156 identifier = fn;
2157 else if (is_overloaded_fn (fn))
2159 functions = fn;
2160 identifier = DECL_NAME (get_first_fn (functions));
2162 else if (DECL_P (fn))
2164 functions = fn;
2165 identifier = DECL_NAME (fn);
2168 /* A call to a namespace-scope function using an unqualified name.
2170 Do Koenig lookup -- unless any of the arguments are
2171 type-dependent. */
2172 if (!any_type_dependent_arguments_p (args)
2173 && !any_dependent_template_arguments_p (tmpl_args))
2175 fn = lookup_arg_dependent (identifier, functions, args);
2176 if (!fn)
2178 /* The unqualified name could not be resolved. */
2179 if (complain)
2180 fn = unqualified_fn_lookup_error (identifier);
2181 else
2182 fn = identifier;
2186 if (fn && template_id)
2187 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2189 return fn;
2192 /* Generate an expression for `FN (ARGS)'. This may change the
2193 contents of ARGS.
2195 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2196 as a virtual call, even if FN is virtual. (This flag is set when
2197 encountering an expression where the function name is explicitly
2198 qualified. For example a call to `X::f' never generates a virtual
2199 call.)
2201 Returns code for the call. */
2203 tree
2204 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2205 bool koenig_p, tsubst_flags_t complain)
2207 tree result;
2208 tree orig_fn;
2209 vec<tree, va_gc> *orig_args = NULL;
2211 if (fn == error_mark_node)
2212 return error_mark_node;
2214 gcc_assert (!TYPE_P (fn));
2216 orig_fn = fn;
2218 if (processing_template_decl)
2220 /* If the call expression is dependent, build a CALL_EXPR node
2221 with no type; type_dependent_expression_p recognizes
2222 expressions with no type as being dependent. */
2223 if (type_dependent_expression_p (fn)
2224 || any_type_dependent_arguments_p (*args)
2225 /* For a non-static member function that doesn't have an
2226 explicit object argument, we need to specifically
2227 test the type dependency of the "this" pointer because it
2228 is not included in *ARGS even though it is considered to
2229 be part of the list of arguments. Note that this is
2230 related to CWG issues 515 and 1005. */
2231 || (TREE_CODE (fn) != COMPONENT_REF
2232 && non_static_member_function_p (fn)
2233 && current_class_ref
2234 && type_dependent_expression_p (current_class_ref)))
2236 result = build_nt_call_vec (fn, *args);
2237 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2238 KOENIG_LOOKUP_P (result) = koenig_p;
2239 if (cfun)
2243 tree fndecl = OVL_CURRENT (fn);
2244 if (TREE_CODE (fndecl) != FUNCTION_DECL
2245 || !TREE_THIS_VOLATILE (fndecl))
2246 break;
2247 fn = OVL_NEXT (fn);
2249 while (fn);
2250 if (!fn)
2251 current_function_returns_abnormally = 1;
2253 return result;
2255 orig_args = make_tree_vector_copy (*args);
2256 if (!BASELINK_P (fn)
2257 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2258 && TREE_TYPE (fn) != unknown_type_node)
2259 fn = build_non_dependent_expr (fn);
2260 make_args_non_dependent (*args);
2263 if (TREE_CODE (fn) == COMPONENT_REF)
2265 tree member = TREE_OPERAND (fn, 1);
2266 if (BASELINK_P (member))
2268 tree object = TREE_OPERAND (fn, 0);
2269 return build_new_method_call (object, member,
2270 args, NULL_TREE,
2271 (disallow_virtual
2272 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2273 : LOOKUP_NORMAL),
2274 /*fn_p=*/NULL,
2275 complain);
2279 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2280 if (TREE_CODE (fn) == ADDR_EXPR
2281 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2282 fn = TREE_OPERAND (fn, 0);
2284 if (is_overloaded_fn (fn))
2285 fn = baselink_for_fns (fn);
2287 result = NULL_TREE;
2288 if (BASELINK_P (fn))
2290 tree object;
2292 /* A call to a member function. From [over.call.func]:
2294 If the keyword this is in scope and refers to the class of
2295 that member function, or a derived class thereof, then the
2296 function call is transformed into a qualified function call
2297 using (*this) as the postfix-expression to the left of the
2298 . operator.... [Otherwise] a contrived object of type T
2299 becomes the implied object argument.
2301 In this situation:
2303 struct A { void f(); };
2304 struct B : public A {};
2305 struct C : public A { void g() { B::f(); }};
2307 "the class of that member function" refers to `A'. But 11.2
2308 [class.access.base] says that we need to convert 'this' to B* as
2309 part of the access, so we pass 'B' to maybe_dummy_object. */
2311 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2312 NULL);
2314 if (processing_template_decl)
2316 if (type_dependent_expression_p (object))
2318 tree ret = build_nt_call_vec (orig_fn, orig_args);
2319 release_tree_vector (orig_args);
2320 return ret;
2322 object = build_non_dependent_expr (object);
2325 result = build_new_method_call (object, fn, args, NULL_TREE,
2326 (disallow_virtual
2327 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2328 : LOOKUP_NORMAL),
2329 /*fn_p=*/NULL,
2330 complain);
2332 else if (is_overloaded_fn (fn))
2334 /* If the function is an overloaded builtin, resolve it. */
2335 if (TREE_CODE (fn) == FUNCTION_DECL
2336 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2337 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2338 result = resolve_overloaded_builtin (input_location, fn, *args);
2340 if (!result)
2342 if (warn_sizeof_pointer_memaccess
2343 && !vec_safe_is_empty (*args)
2344 && !processing_template_decl)
2346 location_t sizeof_arg_loc[3];
2347 tree sizeof_arg[3];
2348 unsigned int i;
2349 for (i = 0; i < 3; i++)
2351 tree t;
2353 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2354 sizeof_arg[i] = NULL_TREE;
2355 if (i >= (*args)->length ())
2356 continue;
2357 t = (**args)[i];
2358 if (TREE_CODE (t) != SIZEOF_EXPR)
2359 continue;
2360 if (SIZEOF_EXPR_TYPE_P (t))
2361 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2362 else
2363 sizeof_arg[i] = TREE_OPERAND (t, 0);
2364 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2366 sizeof_pointer_memaccess_warning
2367 (sizeof_arg_loc, fn, *args,
2368 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2371 /* A call to a namespace-scope function. */
2372 result = build_new_function_call (fn, args, koenig_p, complain);
2375 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2377 if (!vec_safe_is_empty (*args))
2378 error ("arguments to destructor are not allowed");
2379 /* Mark the pseudo-destructor call as having side-effects so
2380 that we do not issue warnings about its use. */
2381 result = build1 (NOP_EXPR,
2382 void_type_node,
2383 TREE_OPERAND (fn, 0));
2384 TREE_SIDE_EFFECTS (result) = 1;
2386 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2387 /* If the "function" is really an object of class type, it might
2388 have an overloaded `operator ()'. */
2389 result = build_op_call (fn, args, complain);
2391 if (!result)
2392 /* A call where the function is unknown. */
2393 result = cp_build_function_call_vec (fn, args, complain);
2395 if (processing_template_decl && result != error_mark_node)
2397 if (INDIRECT_REF_P (result))
2398 result = TREE_OPERAND (result, 0);
2399 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2400 SET_EXPR_LOCATION (result, input_location);
2401 KOENIG_LOOKUP_P (result) = koenig_p;
2402 release_tree_vector (orig_args);
2403 result = convert_from_reference (result);
2406 if (koenig_p)
2408 /* Free garbage OVERLOADs from arg-dependent lookup. */
2409 tree next = NULL_TREE;
2410 for (fn = orig_fn;
2411 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2412 fn = next)
2414 if (processing_template_decl)
2415 /* In a template, we'll re-use them at instantiation time. */
2416 OVL_ARG_DEPENDENT (fn) = false;
2417 else
2419 next = OVL_CHAIN (fn);
2420 ggc_free (fn);
2425 return result;
2428 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
2430 tree
2431 finish_template_variable (tree var)
2433 return instantiate_template (TREE_OPERAND (var, 0), TREE_OPERAND (var, 1),
2434 tf_error);
2437 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2438 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2439 POSTDECREMENT_EXPR.) */
2441 tree
2442 finish_increment_expr (tree expr, enum tree_code code)
2444 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2447 /* Finish a use of `this'. Returns an expression for `this'. */
2449 tree
2450 finish_this_expr (void)
2452 tree result = NULL_TREE;
2454 if (current_class_ptr)
2456 tree type = TREE_TYPE (current_class_ref);
2458 /* In a lambda expression, 'this' refers to the captured 'this'. */
2459 if (LAMBDA_TYPE_P (type))
2460 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2461 else
2462 result = current_class_ptr;
2465 if (result)
2466 /* The keyword 'this' is a prvalue expression. */
2467 return rvalue (result);
2469 tree fn = current_nonlambda_function ();
2470 if (fn && DECL_STATIC_FUNCTION_P (fn))
2471 error ("%<this%> is unavailable for static member functions");
2472 else if (fn)
2473 error ("invalid use of %<this%> in non-member function");
2474 else
2475 error ("invalid use of %<this%> at top level");
2476 return error_mark_node;
2479 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2480 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2481 the TYPE for the type given. If SCOPE is non-NULL, the expression
2482 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2484 tree
2485 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2486 location_t loc)
2488 if (object == error_mark_node || destructor == error_mark_node)
2489 return error_mark_node;
2491 gcc_assert (TYPE_P (destructor));
2493 if (!processing_template_decl)
2495 if (scope == error_mark_node)
2497 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2498 return error_mark_node;
2500 if (is_auto (destructor))
2501 destructor = TREE_TYPE (object);
2502 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2504 error_at (loc,
2505 "qualified type %qT does not match destructor name ~%qT",
2506 scope, destructor);
2507 return error_mark_node;
2511 /* [expr.pseudo] says both:
2513 The type designated by the pseudo-destructor-name shall be
2514 the same as the object type.
2516 and:
2518 The cv-unqualified versions of the object type and of the
2519 type designated by the pseudo-destructor-name shall be the
2520 same type.
2522 We implement the more generous second sentence, since that is
2523 what most other compilers do. */
2524 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2525 destructor))
2527 error_at (loc, "%qE is not of type %qT", object, destructor);
2528 return error_mark_node;
2532 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2533 scope, destructor);
2536 /* Finish an expression of the form CODE EXPR. */
2538 tree
2539 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2540 tsubst_flags_t complain)
2542 tree result = build_x_unary_op (loc, code, expr, complain);
2543 if ((complain & tf_warning)
2544 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2545 overflow_warning (input_location, result);
2547 return result;
2550 /* Finish a compound-literal expression. TYPE is the type to which
2551 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2553 tree
2554 finish_compound_literal (tree type, tree compound_literal,
2555 tsubst_flags_t complain)
2557 if (type == error_mark_node)
2558 return error_mark_node;
2560 if (TREE_CODE (type) == REFERENCE_TYPE)
2562 compound_literal
2563 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2564 complain);
2565 return cp_build_c_cast (type, compound_literal, complain);
2568 if (!TYPE_OBJ_P (type))
2570 if (complain & tf_error)
2571 error ("compound literal of non-object type %qT", type);
2572 return error_mark_node;
2575 if (processing_template_decl)
2577 TREE_TYPE (compound_literal) = type;
2578 /* Mark the expression as a compound literal. */
2579 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2580 return compound_literal;
2583 type = complete_type (type);
2585 if (TYPE_NON_AGGREGATE_CLASS (type))
2587 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2588 everywhere that deals with function arguments would be a pain, so
2589 just wrap it in a TREE_LIST. The parser set a flag so we know
2590 that it came from T{} rather than T({}). */
2591 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2592 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2593 return build_functional_cast (type, compound_literal, complain);
2596 if (TREE_CODE (type) == ARRAY_TYPE
2597 && check_array_initializer (NULL_TREE, type, compound_literal))
2598 return error_mark_node;
2599 compound_literal = reshape_init (type, compound_literal, complain);
2600 if (SCALAR_TYPE_P (type)
2601 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2602 && !check_narrowing (type, compound_literal, complain))
2603 return error_mark_node;
2604 if (TREE_CODE (type) == ARRAY_TYPE
2605 && TYPE_DOMAIN (type) == NULL_TREE)
2607 cp_complete_array_type_or_error (&type, compound_literal,
2608 false, complain);
2609 if (type == error_mark_node)
2610 return error_mark_node;
2612 compound_literal = digest_init (type, compound_literal, complain);
2613 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2614 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2615 /* Put static/constant array temporaries in static variables, but always
2616 represent class temporaries with TARGET_EXPR so we elide copies. */
2617 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2618 && TREE_CODE (type) == ARRAY_TYPE
2619 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2620 && initializer_constant_valid_p (compound_literal, type))
2622 tree decl = create_temporary_var (type);
2623 DECL_INITIAL (decl) = compound_literal;
2624 TREE_STATIC (decl) = 1;
2625 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2627 /* 5.19 says that a constant expression can include an
2628 lvalue-rvalue conversion applied to "a glvalue of literal type
2629 that refers to a non-volatile temporary object initialized
2630 with a constant expression". Rather than try to communicate
2631 that this VAR_DECL is a temporary, just mark it constexpr. */
2632 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2633 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2634 TREE_CONSTANT (decl) = true;
2636 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2637 decl = pushdecl_top_level (decl);
2638 DECL_NAME (decl) = make_anon_name ();
2639 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2640 /* Make sure the destructor is callable. */
2641 tree clean = cxx_maybe_build_cleanup (decl, complain);
2642 if (clean == error_mark_node)
2643 return error_mark_node;
2644 return decl;
2646 else
2647 return get_target_expr_sfinae (compound_literal, complain);
2650 /* Return the declaration for the function-name variable indicated by
2651 ID. */
2653 tree
2654 finish_fname (tree id)
2656 tree decl;
2658 decl = fname_decl (input_location, C_RID_CODE (id), id);
2659 if (processing_template_decl && current_function_decl
2660 && decl != error_mark_node)
2661 decl = DECL_NAME (decl);
2662 return decl;
2665 /* Finish a translation unit. */
2667 void
2668 finish_translation_unit (void)
2670 /* In case there were missing closebraces,
2671 get us back to the global binding level. */
2672 pop_everything ();
2673 while (current_namespace != global_namespace)
2674 pop_namespace ();
2676 /* Do file scope __FUNCTION__ et al. */
2677 finish_fname_decls ();
2680 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2681 Returns the parameter. */
2683 tree
2684 finish_template_type_parm (tree aggr, tree identifier)
2686 if (aggr != class_type_node)
2688 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2689 aggr = class_type_node;
2692 return build_tree_list (aggr, identifier);
2695 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2696 Returns the parameter. */
2698 tree
2699 finish_template_template_parm (tree aggr, tree identifier)
2701 tree decl = build_decl (input_location,
2702 TYPE_DECL, identifier, NULL_TREE);
2703 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2704 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2705 DECL_TEMPLATE_RESULT (tmpl) = decl;
2706 DECL_ARTIFICIAL (decl) = 1;
2707 end_template_decl ();
2709 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2711 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2712 /*is_primary=*/true, /*is_partial=*/false,
2713 /*is_friend=*/0);
2715 return finish_template_type_parm (aggr, tmpl);
2718 /* ARGUMENT is the default-argument value for a template template
2719 parameter. If ARGUMENT is invalid, issue error messages and return
2720 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2722 tree
2723 check_template_template_default_arg (tree argument)
2725 if (TREE_CODE (argument) != TEMPLATE_DECL
2726 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2727 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2729 if (TREE_CODE (argument) == TYPE_DECL)
2730 error ("invalid use of type %qT as a default value for a template "
2731 "template-parameter", TREE_TYPE (argument));
2732 else
2733 error ("invalid default argument for a template template parameter");
2734 return error_mark_node;
2737 return argument;
2740 /* Begin a class definition, as indicated by T. */
2742 tree
2743 begin_class_definition (tree t)
2745 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2746 return error_mark_node;
2748 if (processing_template_parmlist)
2750 error ("definition of %q#T inside template parameter list", t);
2751 return error_mark_node;
2754 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2755 are passed the same as decimal scalar types. */
2756 if (TREE_CODE (t) == RECORD_TYPE
2757 && !processing_template_decl)
2759 tree ns = TYPE_CONTEXT (t);
2760 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2761 && DECL_CONTEXT (ns) == std_node
2762 && DECL_NAME (ns)
2763 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2765 const char *n = TYPE_NAME_STRING (t);
2766 if ((strcmp (n, "decimal32") == 0)
2767 || (strcmp (n, "decimal64") == 0)
2768 || (strcmp (n, "decimal128") == 0))
2769 TYPE_TRANSPARENT_AGGR (t) = 1;
2773 /* A non-implicit typename comes from code like:
2775 template <typename T> struct A {
2776 template <typename U> struct A<T>::B ...
2778 This is erroneous. */
2779 else if (TREE_CODE (t) == TYPENAME_TYPE)
2781 error ("invalid definition of qualified type %qT", t);
2782 t = error_mark_node;
2785 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2787 t = make_class_type (RECORD_TYPE);
2788 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2791 if (TYPE_BEING_DEFINED (t))
2793 t = make_class_type (TREE_CODE (t));
2794 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2796 maybe_process_partial_specialization (t);
2797 pushclass (t);
2798 TYPE_BEING_DEFINED (t) = 1;
2799 class_binding_level->defining_class_p = 1;
2801 if (flag_pack_struct)
2803 tree v;
2804 TYPE_PACKED (t) = 1;
2805 /* Even though the type is being defined for the first time
2806 here, there might have been a forward declaration, so there
2807 might be cv-qualified variants of T. */
2808 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2809 TYPE_PACKED (v) = 1;
2811 /* Reset the interface data, at the earliest possible
2812 moment, as it might have been set via a class foo;
2813 before. */
2814 if (! TYPE_ANONYMOUS_P (t))
2816 struct c_fileinfo *finfo = \
2817 get_fileinfo (LOCATION_FILE (input_location));
2818 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2819 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2820 (t, finfo->interface_unknown);
2822 reset_specialization();
2824 /* Make a declaration for this class in its own scope. */
2825 build_self_reference ();
2827 return t;
2830 /* Finish the member declaration given by DECL. */
2832 void
2833 finish_member_declaration (tree decl)
2835 if (decl == error_mark_node || decl == NULL_TREE)
2836 return;
2838 if (decl == void_type_node)
2839 /* The COMPONENT was a friend, not a member, and so there's
2840 nothing for us to do. */
2841 return;
2843 /* We should see only one DECL at a time. */
2844 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2846 /* Set up access control for DECL. */
2847 TREE_PRIVATE (decl)
2848 = (current_access_specifier == access_private_node);
2849 TREE_PROTECTED (decl)
2850 = (current_access_specifier == access_protected_node);
2851 if (TREE_CODE (decl) == TEMPLATE_DECL)
2853 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2854 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2857 /* Mark the DECL as a member of the current class, unless it's
2858 a member of an enumeration. */
2859 if (TREE_CODE (decl) != CONST_DECL)
2860 DECL_CONTEXT (decl) = current_class_type;
2862 /* Check for bare parameter packs in the member variable declaration. */
2863 if (TREE_CODE (decl) == FIELD_DECL)
2865 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2866 TREE_TYPE (decl) = error_mark_node;
2867 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2868 DECL_ATTRIBUTES (decl) = NULL_TREE;
2871 /* [dcl.link]
2873 A C language linkage is ignored for the names of class members
2874 and the member function type of class member functions. */
2875 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2876 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2878 /* Put functions on the TYPE_METHODS list and everything else on the
2879 TYPE_FIELDS list. Note that these are built up in reverse order.
2880 We reverse them (to obtain declaration order) in finish_struct. */
2881 if (DECL_DECLARES_FUNCTION_P (decl))
2883 /* We also need to add this function to the
2884 CLASSTYPE_METHOD_VEC. */
2885 if (add_method (current_class_type, decl, NULL_TREE))
2887 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2888 TYPE_METHODS (current_class_type) = decl;
2890 maybe_add_class_template_decl_list (current_class_type, decl,
2891 /*friend_p=*/0);
2894 /* Enter the DECL into the scope of the class, if the class
2895 isn't a closure (whose fields are supposed to be unnamed). */
2896 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2897 || pushdecl_class_level (decl))
2899 if (TREE_CODE (decl) == USING_DECL)
2901 /* For now, ignore class-scope USING_DECLS, so that
2902 debugging backends do not see them. */
2903 DECL_IGNORED_P (decl) = 1;
2906 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2907 go at the beginning. The reason is that lookup_field_1
2908 searches the list in order, and we want a field name to
2909 override a type name so that the "struct stat hack" will
2910 work. In particular:
2912 struct S { enum E { }; int E } s;
2913 s.E = 3;
2915 is valid. In addition, the FIELD_DECLs must be maintained in
2916 declaration order so that class layout works as expected.
2917 However, we don't need that order until class layout, so we
2918 save a little time by putting FIELD_DECLs on in reverse order
2919 here, and then reversing them in finish_struct_1. (We could
2920 also keep a pointer to the correct insertion points in the
2921 list.) */
2923 if (TREE_CODE (decl) == TYPE_DECL)
2924 TYPE_FIELDS (current_class_type)
2925 = chainon (TYPE_FIELDS (current_class_type), decl);
2926 else
2928 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2929 TYPE_FIELDS (current_class_type) = decl;
2932 maybe_add_class_template_decl_list (current_class_type, decl,
2933 /*friend_p=*/0);
2936 if (pch_file)
2937 note_decl_for_pch (decl);
2940 /* DECL has been declared while we are building a PCH file. Perform
2941 actions that we might normally undertake lazily, but which can be
2942 performed now so that they do not have to be performed in
2943 translation units which include the PCH file. */
2945 void
2946 note_decl_for_pch (tree decl)
2948 gcc_assert (pch_file);
2950 /* There's a good chance that we'll have to mangle names at some
2951 point, even if only for emission in debugging information. */
2952 if (VAR_OR_FUNCTION_DECL_P (decl)
2953 && !processing_template_decl)
2954 mangle_decl (decl);
2957 /* Finish processing a complete template declaration. The PARMS are
2958 the template parameters. */
2960 void
2961 finish_template_decl (tree parms)
2963 if (parms)
2964 end_template_decl ();
2965 else
2966 end_specialization ();
2969 /* Finish processing a template-id (which names a type) of the form
2970 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2971 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2972 the scope of template-id indicated. */
2974 tree
2975 finish_template_type (tree name, tree args, int entering_scope)
2977 tree type;
2979 type = lookup_template_class (name, args,
2980 NULL_TREE, NULL_TREE, entering_scope,
2981 tf_warning_or_error | tf_user);
2982 if (type == error_mark_node)
2983 return type;
2984 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2985 return TYPE_STUB_DECL (type);
2986 else
2987 return TYPE_NAME (type);
2990 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2991 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2992 BASE_CLASS, or NULL_TREE if an error occurred. The
2993 ACCESS_SPECIFIER is one of
2994 access_{default,public,protected_private}_node. For a virtual base
2995 we set TREE_TYPE. */
2997 tree
2998 finish_base_specifier (tree base, tree access, bool virtual_p)
3000 tree result;
3002 if (base == error_mark_node)
3004 error ("invalid base-class specification");
3005 result = NULL_TREE;
3007 else if (! MAYBE_CLASS_TYPE_P (base))
3009 error ("%qT is not a class type", base);
3010 result = NULL_TREE;
3012 else
3014 if (cp_type_quals (base) != 0)
3016 /* DR 484: Can a base-specifier name a cv-qualified
3017 class type? */
3018 base = TYPE_MAIN_VARIANT (base);
3020 result = build_tree_list (access, base);
3021 if (virtual_p)
3022 TREE_TYPE (result) = integer_type_node;
3025 return result;
3028 /* If FNS is a member function, a set of member functions, or a
3029 template-id referring to one or more member functions, return a
3030 BASELINK for FNS, incorporating the current access context.
3031 Otherwise, return FNS unchanged. */
3033 tree
3034 baselink_for_fns (tree fns)
3036 tree scope;
3037 tree cl;
3039 if (BASELINK_P (fns)
3040 || error_operand_p (fns))
3041 return fns;
3043 scope = ovl_scope (fns);
3044 if (!CLASS_TYPE_P (scope))
3045 return fns;
3047 cl = currently_open_derived_class (scope);
3048 if (!cl)
3049 cl = scope;
3050 cl = TYPE_BINFO (cl);
3051 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3054 /* Returns true iff DECL is a variable from a function outside
3055 the current one. */
3057 static bool
3058 outer_var_p (tree decl)
3060 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3061 && DECL_FUNCTION_SCOPE_P (decl)
3062 && (DECL_CONTEXT (decl) != current_function_decl
3063 || parsing_nsdmi ()));
3066 /* As above, but also checks that DECL is automatic. */
3068 bool
3069 outer_automatic_var_p (tree decl)
3071 return (outer_var_p (decl)
3072 && !TREE_STATIC (decl));
3075 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3076 rewrite it for lambda capture. */
3078 tree
3079 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3081 if (cp_unevaluated_operand)
3082 /* It's not a use (3.2) if we're in an unevaluated context. */
3083 return decl;
3085 tree context = DECL_CONTEXT (decl);
3086 tree containing_function = current_function_decl;
3087 tree lambda_stack = NULL_TREE;
3088 tree lambda_expr = NULL_TREE;
3089 tree initializer = convert_from_reference (decl);
3091 /* Mark it as used now even if the use is ill-formed. */
3092 mark_used (decl);
3094 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3095 support for an approach in which a reference to a local
3096 [constant] automatic variable in a nested class or lambda body
3097 would enter the expression as an rvalue, which would reduce
3098 the complexity of the problem"
3100 FIXME update for final resolution of core issue 696. */
3101 if (decl_maybe_constant_var_p (decl))
3103 if (processing_template_decl)
3104 /* In a template, the constant value may not be in a usable
3105 form, so wait until instantiation time. */
3106 return decl;
3107 else if (decl_constant_var_p (decl))
3108 return integral_constant_value (decl);
3111 if (parsing_nsdmi ())
3112 containing_function = NULL_TREE;
3113 else
3114 /* If we are in a lambda function, we can move out until we hit
3115 1. the context,
3116 2. a non-lambda function, or
3117 3. a non-default capturing lambda function. */
3118 while (context != containing_function
3119 && LAMBDA_FUNCTION_P (containing_function))
3121 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3122 (DECL_CONTEXT (containing_function));
3124 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3125 == CPLD_NONE)
3126 break;
3128 lambda_stack = tree_cons (NULL_TREE,
3129 lambda_expr,
3130 lambda_stack);
3132 containing_function
3133 = decl_function_context (containing_function);
3136 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3137 && DECL_ANON_UNION_VAR_P (decl))
3139 if (complain & tf_error)
3140 error ("cannot capture member %qD of anonymous union", decl);
3141 return error_mark_node;
3143 if (context == containing_function)
3145 decl = add_default_capture (lambda_stack,
3146 /*id=*/DECL_NAME (decl),
3147 initializer);
3149 else if (lambda_expr)
3151 if (complain & tf_error)
3152 error ("%qD is not captured", decl);
3153 return error_mark_node;
3155 else
3157 if (complain & tf_error)
3158 error (VAR_P (decl)
3159 ? G_("use of local variable with automatic storage from containing function")
3160 : G_("use of parameter from containing function"));
3161 inform (input_location, "%q+#D declared here", decl);
3162 return error_mark_node;
3164 return decl;
3167 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3168 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3169 if non-NULL, is the type or namespace used to explicitly qualify
3170 ID_EXPRESSION. DECL is the entity to which that name has been
3171 resolved.
3173 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3174 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3175 be set to true if this expression isn't permitted in a
3176 constant-expression, but it is otherwise not set by this function.
3177 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3178 constant-expression, but a non-constant expression is also
3179 permissible.
3181 DONE is true if this expression is a complete postfix-expression;
3182 it is false if this expression is followed by '->', '[', '(', etc.
3183 ADDRESS_P is true iff this expression is the operand of '&'.
3184 TEMPLATE_P is true iff the qualified-id was of the form
3185 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3186 appears as a template argument.
3188 If an error occurs, and it is the kind of error that might cause
3189 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3190 is the caller's responsibility to issue the message. *ERROR_MSG
3191 will be a string with static storage duration, so the caller need
3192 not "free" it.
3194 Return an expression for the entity, after issuing appropriate
3195 diagnostics. This function is also responsible for transforming a
3196 reference to a non-static member into a COMPONENT_REF that makes
3197 the use of "this" explicit.
3199 Upon return, *IDK will be filled in appropriately. */
3200 tree
3201 finish_id_expression (tree id_expression,
3202 tree decl,
3203 tree scope,
3204 cp_id_kind *idk,
3205 bool integral_constant_expression_p,
3206 bool allow_non_integral_constant_expression_p,
3207 bool *non_integral_constant_expression_p,
3208 bool template_p,
3209 bool done,
3210 bool address_p,
3211 bool template_arg_p,
3212 const char **error_msg,
3213 location_t location)
3215 decl = strip_using_decl (decl);
3217 /* Initialize the output parameters. */
3218 *idk = CP_ID_KIND_NONE;
3219 *error_msg = NULL;
3221 if (id_expression == error_mark_node)
3222 return error_mark_node;
3223 /* If we have a template-id, then no further lookup is
3224 required. If the template-id was for a template-class, we
3225 will sometimes have a TYPE_DECL at this point. */
3226 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3227 || TREE_CODE (decl) == TYPE_DECL)
3229 /* Look up the name. */
3230 else
3232 if (decl == error_mark_node)
3234 /* Name lookup failed. */
3235 if (scope
3236 && (!TYPE_P (scope)
3237 || (!dependent_type_p (scope)
3238 && !(identifier_p (id_expression)
3239 && IDENTIFIER_TYPENAME_P (id_expression)
3240 && dependent_type_p (TREE_TYPE (id_expression))))))
3242 /* If the qualifying type is non-dependent (and the name
3243 does not name a conversion operator to a dependent
3244 type), issue an error. */
3245 qualified_name_lookup_error (scope, id_expression, decl, location);
3246 return error_mark_node;
3248 else if (!scope)
3250 /* It may be resolved via Koenig lookup. */
3251 *idk = CP_ID_KIND_UNQUALIFIED;
3252 return id_expression;
3254 else
3255 decl = id_expression;
3257 /* If DECL is a variable that would be out of scope under
3258 ANSI/ISO rules, but in scope in the ARM, name lookup
3259 will succeed. Issue a diagnostic here. */
3260 else
3261 decl = check_for_out_of_scope_variable (decl);
3263 /* Remember that the name was used in the definition of
3264 the current class so that we can check later to see if
3265 the meaning would have been different after the class
3266 was entirely defined. */
3267 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3268 maybe_note_name_used_in_class (id_expression, decl);
3270 /* Disallow uses of local variables from containing functions, except
3271 within lambda-expressions. */
3272 if (outer_automatic_var_p (decl))
3274 decl = process_outer_var_ref (decl, tf_warning_or_error);
3275 if (decl == error_mark_node)
3276 return error_mark_node;
3279 /* Also disallow uses of function parameters outside the function
3280 body, except inside an unevaluated context (i.e. decltype). */
3281 if (TREE_CODE (decl) == PARM_DECL
3282 && DECL_CONTEXT (decl) == NULL_TREE
3283 && !cp_unevaluated_operand)
3285 *error_msg = "use of parameter outside function body";
3286 return error_mark_node;
3290 /* If we didn't find anything, or what we found was a type,
3291 then this wasn't really an id-expression. */
3292 if (TREE_CODE (decl) == TEMPLATE_DECL
3293 && !DECL_FUNCTION_TEMPLATE_P (decl))
3295 *error_msg = "missing template arguments";
3296 return error_mark_node;
3298 else if (TREE_CODE (decl) == TYPE_DECL
3299 || TREE_CODE (decl) == NAMESPACE_DECL)
3301 *error_msg = "expected primary-expression";
3302 return error_mark_node;
3305 /* If the name resolved to a template parameter, there is no
3306 need to look it up again later. */
3307 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3308 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3310 tree r;
3312 *idk = CP_ID_KIND_NONE;
3313 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3314 decl = TEMPLATE_PARM_DECL (decl);
3315 r = convert_from_reference (DECL_INITIAL (decl));
3317 if (integral_constant_expression_p
3318 && !dependent_type_p (TREE_TYPE (decl))
3319 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3321 if (!allow_non_integral_constant_expression_p)
3322 error ("template parameter %qD of type %qT is not allowed in "
3323 "an integral constant expression because it is not of "
3324 "integral or enumeration type", decl, TREE_TYPE (decl));
3325 *non_integral_constant_expression_p = true;
3327 return r;
3329 else
3331 bool dependent_p;
3333 /* If the declaration was explicitly qualified indicate
3334 that. The semantics of `A::f(3)' are different than
3335 `f(3)' if `f' is virtual. */
3336 *idk = (scope
3337 ? CP_ID_KIND_QUALIFIED
3338 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3339 ? CP_ID_KIND_TEMPLATE_ID
3340 : CP_ID_KIND_UNQUALIFIED));
3343 /* [temp.dep.expr]
3345 An id-expression is type-dependent if it contains an
3346 identifier that was declared with a dependent type.
3348 The standard is not very specific about an id-expression that
3349 names a set of overloaded functions. What if some of them
3350 have dependent types and some of them do not? Presumably,
3351 such a name should be treated as a dependent name. */
3352 /* Assume the name is not dependent. */
3353 dependent_p = false;
3354 if (!processing_template_decl)
3355 /* No names are dependent outside a template. */
3357 else if (TREE_CODE (decl) == CONST_DECL)
3358 /* We don't want to treat enumerators as dependent. */
3360 /* A template-id where the name of the template was not resolved
3361 is definitely dependent. */
3362 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3363 && (identifier_p (TREE_OPERAND (decl, 0))))
3364 dependent_p = true;
3365 /* For anything except an overloaded function, just check its
3366 type. */
3367 else if (!is_overloaded_fn (decl))
3368 dependent_p
3369 = dependent_type_p (TREE_TYPE (decl));
3370 /* For a set of overloaded functions, check each of the
3371 functions. */
3372 else
3374 tree fns = decl;
3376 if (BASELINK_P (fns))
3377 fns = BASELINK_FUNCTIONS (fns);
3379 /* For a template-id, check to see if the template
3380 arguments are dependent. */
3381 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3383 tree args = TREE_OPERAND (fns, 1);
3384 dependent_p = any_dependent_template_arguments_p (args);
3385 /* The functions are those referred to by the
3386 template-id. */
3387 fns = TREE_OPERAND (fns, 0);
3390 /* If there are no dependent template arguments, go through
3391 the overloaded functions. */
3392 while (fns && !dependent_p)
3394 tree fn = OVL_CURRENT (fns);
3396 /* Member functions of dependent classes are
3397 dependent. */
3398 if (TREE_CODE (fn) == FUNCTION_DECL
3399 && type_dependent_expression_p (fn))
3400 dependent_p = true;
3401 else if (TREE_CODE (fn) == TEMPLATE_DECL
3402 && dependent_template_p (fn))
3403 dependent_p = true;
3405 fns = OVL_NEXT (fns);
3409 /* If the name was dependent on a template parameter, we will
3410 resolve the name at instantiation time. */
3411 if (dependent_p)
3413 /* Create a SCOPE_REF for qualified names, if the scope is
3414 dependent. */
3415 if (scope)
3417 if (TYPE_P (scope))
3419 if (address_p && done)
3420 decl = finish_qualified_id_expr (scope, decl,
3421 done, address_p,
3422 template_p,
3423 template_arg_p,
3424 tf_warning_or_error);
3425 else
3427 tree type = NULL_TREE;
3428 if (DECL_P (decl) && !dependent_scope_p (scope))
3429 type = TREE_TYPE (decl);
3430 decl = build_qualified_name (type,
3431 scope,
3432 id_expression,
3433 template_p);
3436 if (TREE_TYPE (decl))
3437 decl = convert_from_reference (decl);
3438 return decl;
3440 /* A TEMPLATE_ID already contains all the information we
3441 need. */
3442 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3443 return id_expression;
3444 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3445 /* If we found a variable, then name lookup during the
3446 instantiation will always resolve to the same VAR_DECL
3447 (or an instantiation thereof). */
3448 if (VAR_P (decl)
3449 || TREE_CODE (decl) == PARM_DECL)
3451 mark_used (decl);
3452 return convert_from_reference (decl);
3454 /* The same is true for FIELD_DECL, but we also need to
3455 make sure that the syntax is correct. */
3456 else if (TREE_CODE (decl) == FIELD_DECL)
3458 /* Since SCOPE is NULL here, this is an unqualified name.
3459 Access checking has been performed during name lookup
3460 already. Turn off checking to avoid duplicate errors. */
3461 push_deferring_access_checks (dk_no_check);
3462 decl = finish_non_static_data_member
3463 (decl, NULL_TREE,
3464 /*qualifying_scope=*/NULL_TREE);
3465 pop_deferring_access_checks ();
3466 return decl;
3468 return id_expression;
3471 if (TREE_CODE (decl) == NAMESPACE_DECL)
3473 error ("use of namespace %qD as expression", decl);
3474 return error_mark_node;
3476 else if (DECL_CLASS_TEMPLATE_P (decl))
3478 error ("use of class template %qT as expression", decl);
3479 return error_mark_node;
3481 else if (TREE_CODE (decl) == TREE_LIST)
3483 /* Ambiguous reference to base members. */
3484 error ("request for member %qD is ambiguous in "
3485 "multiple inheritance lattice", id_expression);
3486 print_candidates (decl);
3487 return error_mark_node;
3490 /* Mark variable-like entities as used. Functions are similarly
3491 marked either below or after overload resolution. */
3492 if ((VAR_P (decl)
3493 || TREE_CODE (decl) == PARM_DECL
3494 || TREE_CODE (decl) == CONST_DECL
3495 || TREE_CODE (decl) == RESULT_DECL)
3496 && !mark_used (decl))
3497 return error_mark_node;
3499 /* Only certain kinds of names are allowed in constant
3500 expression. Template parameters have already
3501 been handled above. */
3502 if (! error_operand_p (decl)
3503 && integral_constant_expression_p
3504 && ! decl_constant_var_p (decl)
3505 && TREE_CODE (decl) != CONST_DECL
3506 && ! builtin_valid_in_constant_expr_p (decl))
3508 if (!allow_non_integral_constant_expression_p)
3510 error ("%qD cannot appear in a constant-expression", decl);
3511 return error_mark_node;
3513 *non_integral_constant_expression_p = true;
3516 tree wrap;
3517 if (VAR_P (decl)
3518 && !cp_unevaluated_operand
3519 && !processing_template_decl
3520 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3521 && DECL_THREAD_LOCAL_P (decl)
3522 && (wrap = get_tls_wrapper_fn (decl)))
3524 /* Replace an evaluated use of the thread_local variable with
3525 a call to its wrapper. */
3526 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3528 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3529 && variable_template_p (TREE_OPERAND (decl, 0)))
3531 decl = finish_template_variable (decl);
3532 mark_used (decl);
3534 else if (scope)
3536 decl = (adjust_result_of_qualified_name_lookup
3537 (decl, scope, current_nonlambda_class_type()));
3539 if (TREE_CODE (decl) == FUNCTION_DECL)
3540 mark_used (decl);
3542 if (TYPE_P (scope))
3543 decl = finish_qualified_id_expr (scope,
3544 decl,
3545 done,
3546 address_p,
3547 template_p,
3548 template_arg_p,
3549 tf_warning_or_error);
3550 else
3551 decl = convert_from_reference (decl);
3553 else if (TREE_CODE (decl) == FIELD_DECL)
3555 /* Since SCOPE is NULL here, this is an unqualified name.
3556 Access checking has been performed during name lookup
3557 already. Turn off checking to avoid duplicate errors. */
3558 push_deferring_access_checks (dk_no_check);
3559 decl = finish_non_static_data_member (decl, NULL_TREE,
3560 /*qualifying_scope=*/NULL_TREE);
3561 pop_deferring_access_checks ();
3563 else if (is_overloaded_fn (decl))
3565 tree first_fn;
3567 first_fn = get_first_fn (decl);
3568 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3569 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3571 if (!really_overloaded_fn (decl)
3572 && !mark_used (first_fn))
3573 return error_mark_node;
3575 if (!template_arg_p
3576 && TREE_CODE (first_fn) == FUNCTION_DECL
3577 && DECL_FUNCTION_MEMBER_P (first_fn)
3578 && !shared_member_p (decl))
3580 /* A set of member functions. */
3581 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3582 return finish_class_member_access_expr (decl, id_expression,
3583 /*template_p=*/false,
3584 tf_warning_or_error);
3587 decl = baselink_for_fns (decl);
3589 else
3591 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3592 && DECL_CLASS_SCOPE_P (decl))
3594 tree context = context_for_name_lookup (decl);
3595 if (context != current_class_type)
3597 tree path = currently_open_derived_class (context);
3598 perform_or_defer_access_check (TYPE_BINFO (path),
3599 decl, decl,
3600 tf_warning_or_error);
3604 decl = convert_from_reference (decl);
3608 /* Handle references (c++/56130). */
3609 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3610 if (TREE_DEPRECATED (t))
3611 warn_deprecated_use (t, NULL_TREE);
3613 return decl;
3616 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3617 use as a type-specifier. */
3619 tree
3620 finish_typeof (tree expr)
3622 tree type;
3624 if (type_dependent_expression_p (expr))
3626 type = cxx_make_type (TYPEOF_TYPE);
3627 TYPEOF_TYPE_EXPR (type) = expr;
3628 SET_TYPE_STRUCTURAL_EQUALITY (type);
3630 return type;
3633 expr = mark_type_use (expr);
3635 type = unlowered_expr_type (expr);
3637 if (!type || type == unknown_type_node)
3639 error ("type of %qE is unknown", expr);
3640 return error_mark_node;
3643 return type;
3646 /* Implement the __underlying_type keyword: Return the underlying
3647 type of TYPE, suitable for use as a type-specifier. */
3649 tree
3650 finish_underlying_type (tree type)
3652 tree underlying_type;
3654 if (processing_template_decl)
3656 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3657 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3658 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3660 return underlying_type;
3663 complete_type (type);
3665 if (TREE_CODE (type) != ENUMERAL_TYPE)
3667 error ("%qT is not an enumeration type", type);
3668 return error_mark_node;
3671 underlying_type = ENUM_UNDERLYING_TYPE (type);
3673 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3674 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3675 See finish_enum_value_list for details. */
3676 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3677 underlying_type
3678 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3679 TYPE_UNSIGNED (underlying_type));
3681 return underlying_type;
3684 /* Implement the __direct_bases keyword: Return the direct base classes
3685 of type */
3687 tree
3688 calculate_direct_bases (tree type)
3690 vec<tree, va_gc> *vector = make_tree_vector();
3691 tree bases_vec = NULL_TREE;
3692 vec<tree, va_gc> *base_binfos;
3693 tree binfo;
3694 unsigned i;
3696 complete_type (type);
3698 if (!NON_UNION_CLASS_TYPE_P (type))
3699 return make_tree_vec (0);
3701 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3703 /* Virtual bases are initialized first */
3704 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3706 if (BINFO_VIRTUAL_P (binfo))
3708 vec_safe_push (vector, binfo);
3712 /* Now non-virtuals */
3713 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3715 if (!BINFO_VIRTUAL_P (binfo))
3717 vec_safe_push (vector, binfo);
3722 bases_vec = make_tree_vec (vector->length ());
3724 for (i = 0; i < vector->length (); ++i)
3726 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3728 return bases_vec;
3731 /* Implement the __bases keyword: Return the base classes
3732 of type */
3734 /* Find morally non-virtual base classes by walking binfo hierarchy */
3735 /* Virtual base classes are handled separately in finish_bases */
3737 static tree
3738 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3740 /* Don't walk bases of virtual bases */
3741 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3744 static tree
3745 dfs_calculate_bases_post (tree binfo, void *data_)
3747 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3748 if (!BINFO_VIRTUAL_P (binfo))
3750 vec_safe_push (*data, BINFO_TYPE (binfo));
3752 return NULL_TREE;
3755 /* Calculates the morally non-virtual base classes of a class */
3756 static vec<tree, va_gc> *
3757 calculate_bases_helper (tree type)
3759 vec<tree, va_gc> *vector = make_tree_vector();
3761 /* Now add non-virtual base classes in order of construction */
3762 dfs_walk_all (TYPE_BINFO (type),
3763 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3764 return vector;
3767 tree
3768 calculate_bases (tree type)
3770 vec<tree, va_gc> *vector = make_tree_vector();
3771 tree bases_vec = NULL_TREE;
3772 unsigned i;
3773 vec<tree, va_gc> *vbases;
3774 vec<tree, va_gc> *nonvbases;
3775 tree binfo;
3777 complete_type (type);
3779 if (!NON_UNION_CLASS_TYPE_P (type))
3780 return make_tree_vec (0);
3782 /* First go through virtual base classes */
3783 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3784 vec_safe_iterate (vbases, i, &binfo); i++)
3786 vec<tree, va_gc> *vbase_bases;
3787 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3788 vec_safe_splice (vector, vbase_bases);
3789 release_tree_vector (vbase_bases);
3792 /* Now for the non-virtual bases */
3793 nonvbases = calculate_bases_helper (type);
3794 vec_safe_splice (vector, nonvbases);
3795 release_tree_vector (nonvbases);
3797 /* Last element is entire class, so don't copy */
3798 bases_vec = make_tree_vec (vector->length () - 1);
3800 for (i = 0; i < vector->length () - 1; ++i)
3802 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3804 release_tree_vector (vector);
3805 return bases_vec;
3808 tree
3809 finish_bases (tree type, bool direct)
3811 tree bases = NULL_TREE;
3813 if (!processing_template_decl)
3815 /* Parameter packs can only be used in templates */
3816 error ("Parameter pack __bases only valid in template declaration");
3817 return error_mark_node;
3820 bases = cxx_make_type (BASES);
3821 BASES_TYPE (bases) = type;
3822 BASES_DIRECT (bases) = direct;
3823 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3825 return bases;
3828 /* Perform C++-specific checks for __builtin_offsetof before calling
3829 fold_offsetof. */
3831 tree
3832 finish_offsetof (tree expr, location_t loc)
3834 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3836 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3837 TREE_OPERAND (expr, 2));
3838 return error_mark_node;
3840 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3841 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3842 || TREE_TYPE (expr) == unknown_type_node)
3844 if (INDIRECT_REF_P (expr))
3845 error ("second operand of %<offsetof%> is neither a single "
3846 "identifier nor a sequence of member accesses and "
3847 "array references");
3848 else
3850 if (TREE_CODE (expr) == COMPONENT_REF
3851 || TREE_CODE (expr) == COMPOUND_EXPR)
3852 expr = TREE_OPERAND (expr, 1);
3853 error ("cannot apply %<offsetof%> to member function %qD", expr);
3855 return error_mark_node;
3857 if (REFERENCE_REF_P (expr))
3858 expr = TREE_OPERAND (expr, 0);
3859 if (TREE_CODE (expr) == COMPONENT_REF)
3861 tree object = TREE_OPERAND (expr, 0);
3862 if (!complete_type_or_else (TREE_TYPE (object), object))
3863 return error_mark_node;
3864 if (warn_invalid_offsetof
3865 && CLASS_TYPE_P (TREE_TYPE (object))
3866 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3867 && cp_unevaluated_operand == 0)
3868 pedwarn (loc, OPT_Winvalid_offsetof,
3869 "offsetof within non-standard-layout type %qT is undefined",
3870 TREE_TYPE (object));
3872 return fold_offsetof (expr);
3875 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3876 function is broken out from the above for the benefit of the tree-ssa
3877 project. */
3879 void
3880 simplify_aggr_init_expr (tree *tp)
3882 tree aggr_init_expr = *tp;
3884 /* Form an appropriate CALL_EXPR. */
3885 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3886 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3887 tree type = TREE_TYPE (slot);
3889 tree call_expr;
3890 enum style_t { ctor, arg, pcc } style;
3892 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3893 style = ctor;
3894 #ifdef PCC_STATIC_STRUCT_RETURN
3895 else if (1)
3896 style = pcc;
3897 #endif
3898 else
3900 gcc_assert (TREE_ADDRESSABLE (type));
3901 style = arg;
3904 call_expr = build_call_array_loc (input_location,
3905 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3907 aggr_init_expr_nargs (aggr_init_expr),
3908 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3909 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3910 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3912 if (style == ctor)
3914 /* Replace the first argument to the ctor with the address of the
3915 slot. */
3916 cxx_mark_addressable (slot);
3917 CALL_EXPR_ARG (call_expr, 0) =
3918 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3920 else if (style == arg)
3922 /* Just mark it addressable here, and leave the rest to
3923 expand_call{,_inline}. */
3924 cxx_mark_addressable (slot);
3925 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3926 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3928 else if (style == pcc)
3930 /* If we're using the non-reentrant PCC calling convention, then we
3931 need to copy the returned value out of the static buffer into the
3932 SLOT. */
3933 push_deferring_access_checks (dk_no_check);
3934 call_expr = build_aggr_init (slot, call_expr,
3935 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3936 tf_warning_or_error);
3937 pop_deferring_access_checks ();
3938 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3941 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3943 tree init = build_zero_init (type, NULL_TREE,
3944 /*static_storage_p=*/false);
3945 init = build2 (INIT_EXPR, void_type_node, slot, init);
3946 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3947 init, call_expr);
3950 *tp = call_expr;
3953 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3955 void
3956 emit_associated_thunks (tree fn)
3958 /* When we use vcall offsets, we emit thunks with the virtual
3959 functions to which they thunk. The whole point of vcall offsets
3960 is so that you can know statically the entire set of thunks that
3961 will ever be needed for a given virtual function, thereby
3962 enabling you to output all the thunks with the function itself. */
3963 if (DECL_VIRTUAL_P (fn)
3964 /* Do not emit thunks for extern template instantiations. */
3965 && ! DECL_REALLY_EXTERN (fn))
3967 tree thunk;
3969 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3971 if (!THUNK_ALIAS (thunk))
3973 use_thunk (thunk, /*emit_p=*/1);
3974 if (DECL_RESULT_THUNK_P (thunk))
3976 tree probe;
3978 for (probe = DECL_THUNKS (thunk);
3979 probe; probe = DECL_CHAIN (probe))
3980 use_thunk (probe, /*emit_p=*/1);
3983 else
3984 gcc_assert (!DECL_THUNKS (thunk));
3989 /* Generate RTL for FN. */
3991 bool
3992 expand_or_defer_fn_1 (tree fn)
3994 /* When the parser calls us after finishing the body of a template
3995 function, we don't really want to expand the body. */
3996 if (processing_template_decl)
3998 /* Normally, collection only occurs in rest_of_compilation. So,
3999 if we don't collect here, we never collect junk generated
4000 during the processing of templates until we hit a
4001 non-template function. It's not safe to do this inside a
4002 nested class, though, as the parser may have local state that
4003 is not a GC root. */
4004 if (!function_depth)
4005 ggc_collect ();
4006 return false;
4009 gcc_assert (DECL_SAVED_TREE (fn));
4011 /* We make a decision about linkage for these functions at the end
4012 of the compilation. Until that point, we do not want the back
4013 end to output them -- but we do want it to see the bodies of
4014 these functions so that it can inline them as appropriate. */
4015 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4017 if (DECL_INTERFACE_KNOWN (fn))
4018 /* We've already made a decision as to how this function will
4019 be handled. */;
4020 else if (!at_eof)
4021 tentative_decl_linkage (fn);
4022 else
4023 import_export_decl (fn);
4025 /* If the user wants us to keep all inline functions, then mark
4026 this function as needed so that finish_file will make sure to
4027 output it later. Similarly, all dllexport'd functions must
4028 be emitted; there may be callers in other DLLs. */
4029 if (DECL_DECLARED_INLINE_P (fn)
4030 && !DECL_REALLY_EXTERN (fn)
4031 && (flag_keep_inline_functions
4032 || (flag_keep_inline_dllexport
4033 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4035 mark_needed (fn);
4036 DECL_EXTERNAL (fn) = 0;
4040 /* If this is a constructor or destructor body, we have to clone
4041 it. */
4042 if (maybe_clone_body (fn))
4044 /* We don't want to process FN again, so pretend we've written
4045 it out, even though we haven't. */
4046 TREE_ASM_WRITTEN (fn) = 1;
4047 /* If this is an instantiation of a constexpr function, keep
4048 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4049 if (!is_instantiation_of_constexpr (fn))
4050 DECL_SAVED_TREE (fn) = NULL_TREE;
4051 return false;
4054 /* There's no reason to do any of the work here if we're only doing
4055 semantic analysis; this code just generates RTL. */
4056 if (flag_syntax_only)
4057 return false;
4059 return true;
4062 void
4063 expand_or_defer_fn (tree fn)
4065 if (expand_or_defer_fn_1 (fn))
4067 function_depth++;
4069 /* Expand or defer, at the whim of the compilation unit manager. */
4070 cgraph_node::finalize_function (fn, function_depth > 1);
4071 emit_associated_thunks (fn);
4073 function_depth--;
4077 struct nrv_data
4079 nrv_data () : visited (37) {}
4081 tree var;
4082 tree result;
4083 hash_table<pointer_hash <tree_node> > visited;
4086 /* Helper function for walk_tree, used by finalize_nrv below. */
4088 static tree
4089 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4091 struct nrv_data *dp = (struct nrv_data *)data;
4092 tree_node **slot;
4094 /* No need to walk into types. There wouldn't be any need to walk into
4095 non-statements, except that we have to consider STMT_EXPRs. */
4096 if (TYPE_P (*tp))
4097 *walk_subtrees = 0;
4098 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4099 but differs from using NULL_TREE in that it indicates that we care
4100 about the value of the RESULT_DECL. */
4101 else if (TREE_CODE (*tp) == RETURN_EXPR)
4102 TREE_OPERAND (*tp, 0) = dp->result;
4103 /* Change all cleanups for the NRV to only run when an exception is
4104 thrown. */
4105 else if (TREE_CODE (*tp) == CLEANUP_STMT
4106 && CLEANUP_DECL (*tp) == dp->var)
4107 CLEANUP_EH_ONLY (*tp) = 1;
4108 /* Replace the DECL_EXPR for the NRV with an initialization of the
4109 RESULT_DECL, if needed. */
4110 else if (TREE_CODE (*tp) == DECL_EXPR
4111 && DECL_EXPR_DECL (*tp) == dp->var)
4113 tree init;
4114 if (DECL_INITIAL (dp->var)
4115 && DECL_INITIAL (dp->var) != error_mark_node)
4116 init = build2 (INIT_EXPR, void_type_node, dp->result,
4117 DECL_INITIAL (dp->var));
4118 else
4119 init = build_empty_stmt (EXPR_LOCATION (*tp));
4120 DECL_INITIAL (dp->var) = NULL_TREE;
4121 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4122 *tp = init;
4124 /* And replace all uses of the NRV with the RESULT_DECL. */
4125 else if (*tp == dp->var)
4126 *tp = dp->result;
4128 /* Avoid walking into the same tree more than once. Unfortunately, we
4129 can't just use walk_tree_without duplicates because it would only call
4130 us for the first occurrence of dp->var in the function body. */
4131 slot = dp->visited.find_slot (*tp, INSERT);
4132 if (*slot)
4133 *walk_subtrees = 0;
4134 else
4135 *slot = *tp;
4137 /* Keep iterating. */
4138 return NULL_TREE;
4141 /* Called from finish_function to implement the named return value
4142 optimization by overriding all the RETURN_EXPRs and pertinent
4143 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4144 RESULT_DECL for the function. */
4146 void
4147 finalize_nrv (tree *tp, tree var, tree result)
4149 struct nrv_data data;
4151 /* Copy name from VAR to RESULT. */
4152 DECL_NAME (result) = DECL_NAME (var);
4153 /* Don't forget that we take its address. */
4154 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4155 /* Finally set DECL_VALUE_EXPR to avoid assigning
4156 a stack slot at -O0 for the original var and debug info
4157 uses RESULT location for VAR. */
4158 SET_DECL_VALUE_EXPR (var, result);
4159 DECL_HAS_VALUE_EXPR_P (var) = 1;
4161 data.var = var;
4162 data.result = result;
4163 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4166 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4168 bool
4169 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4170 bool need_copy_ctor, bool need_copy_assignment,
4171 bool need_dtor)
4173 int save_errorcount = errorcount;
4174 tree info, t;
4176 /* Always allocate 3 elements for simplicity. These are the
4177 function decls for the ctor, dtor, and assignment op.
4178 This layout is known to the three lang hooks,
4179 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4180 and cxx_omp_clause_assign_op. */
4181 info = make_tree_vec (3);
4182 CP_OMP_CLAUSE_INFO (c) = info;
4184 if (need_default_ctor || need_copy_ctor)
4186 if (need_default_ctor)
4187 t = get_default_ctor (type);
4188 else
4189 t = get_copy_ctor (type, tf_warning_or_error);
4191 if (t && !trivial_fn_p (t))
4192 TREE_VEC_ELT (info, 0) = t;
4195 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4196 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4198 if (need_copy_assignment)
4200 t = get_copy_assign (type);
4202 if (t && !trivial_fn_p (t))
4203 TREE_VEC_ELT (info, 2) = t;
4206 return errorcount != save_errorcount;
4209 /* Helper function for handle_omp_array_sections. Called recursively
4210 to handle multiple array-section-subscripts. C is the clause,
4211 T current expression (initially OMP_CLAUSE_DECL), which is either
4212 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4213 expression if specified, TREE_VALUE length expression if specified,
4214 TREE_CHAIN is what it has been specified after, or some decl.
4215 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4216 set to true if any of the array-section-subscript could have length
4217 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4218 first array-section-subscript which is known not to have length
4219 of one. Given say:
4220 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4221 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4222 all are or may have length of 1, array-section-subscript [:2] is the
4223 first one knonwn not to have length 1. For array-section-subscript
4224 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4225 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4226 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4227 case though, as some lengths could be zero. */
4229 static tree
4230 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4231 bool &maybe_zero_len, unsigned int &first_non_one)
4233 tree ret, low_bound, length, type;
4234 if (TREE_CODE (t) != TREE_LIST)
4236 if (error_operand_p (t))
4237 return error_mark_node;
4238 if (type_dependent_expression_p (t))
4239 return NULL_TREE;
4240 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4242 if (processing_template_decl)
4243 return NULL_TREE;
4244 if (DECL_P (t))
4245 error_at (OMP_CLAUSE_LOCATION (c),
4246 "%qD is not a variable in %qs clause", t,
4247 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4248 else
4249 error_at (OMP_CLAUSE_LOCATION (c),
4250 "%qE is not a variable in %qs clause", t,
4251 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4252 return error_mark_node;
4254 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4255 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4257 error_at (OMP_CLAUSE_LOCATION (c),
4258 "%qD is threadprivate variable in %qs clause", t,
4259 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4260 return error_mark_node;
4262 t = convert_from_reference (t);
4263 return t;
4266 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4267 maybe_zero_len, first_non_one);
4268 if (ret == error_mark_node || ret == NULL_TREE)
4269 return ret;
4271 type = TREE_TYPE (ret);
4272 low_bound = TREE_PURPOSE (t);
4273 length = TREE_VALUE (t);
4274 if ((low_bound && type_dependent_expression_p (low_bound))
4275 || (length && type_dependent_expression_p (length)))
4276 return NULL_TREE;
4278 if (low_bound == error_mark_node || length == error_mark_node)
4279 return error_mark_node;
4281 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4283 error_at (OMP_CLAUSE_LOCATION (c),
4284 "low bound %qE of array section does not have integral type",
4285 low_bound);
4286 return error_mark_node;
4288 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4290 error_at (OMP_CLAUSE_LOCATION (c),
4291 "length %qE of array section does not have integral type",
4292 length);
4293 return error_mark_node;
4295 if (low_bound)
4296 low_bound = mark_rvalue_use (low_bound);
4297 if (length)
4298 length = mark_rvalue_use (length);
4299 if (low_bound
4300 && TREE_CODE (low_bound) == INTEGER_CST
4301 && TYPE_PRECISION (TREE_TYPE (low_bound))
4302 > TYPE_PRECISION (sizetype))
4303 low_bound = fold_convert (sizetype, low_bound);
4304 if (length
4305 && TREE_CODE (length) == INTEGER_CST
4306 && TYPE_PRECISION (TREE_TYPE (length))
4307 > TYPE_PRECISION (sizetype))
4308 length = fold_convert (sizetype, length);
4309 if (low_bound == NULL_TREE)
4310 low_bound = integer_zero_node;
4312 if (length != NULL_TREE)
4314 if (!integer_nonzerop (length))
4315 maybe_zero_len = true;
4316 if (first_non_one == types.length ()
4317 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4318 first_non_one++;
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4322 if (length == NULL_TREE
4323 && (TYPE_DOMAIN (type) == NULL_TREE
4324 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4326 error_at (OMP_CLAUSE_LOCATION (c),
4327 "for unknown bound array type length expression must "
4328 "be specified");
4329 return error_mark_node;
4331 if (TREE_CODE (low_bound) == INTEGER_CST
4332 && tree_int_cst_sgn (low_bound) == -1)
4334 error_at (OMP_CLAUSE_LOCATION (c),
4335 "negative low bound in array section in %qs clause",
4336 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4337 return error_mark_node;
4339 if (length != NULL_TREE
4340 && TREE_CODE (length) == INTEGER_CST
4341 && tree_int_cst_sgn (length) == -1)
4343 error_at (OMP_CLAUSE_LOCATION (c),
4344 "negative length in array section in %qs clause",
4345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4346 return error_mark_node;
4348 if (TYPE_DOMAIN (type)
4349 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4350 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4351 == INTEGER_CST)
4353 tree size = size_binop (PLUS_EXPR,
4354 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4355 size_one_node);
4356 if (TREE_CODE (low_bound) == INTEGER_CST)
4358 if (tree_int_cst_lt (size, low_bound))
4360 error_at (OMP_CLAUSE_LOCATION (c),
4361 "low bound %qE above array section size "
4362 "in %qs clause", low_bound,
4363 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4364 return error_mark_node;
4366 if (tree_int_cst_equal (size, low_bound))
4367 maybe_zero_len = true;
4368 else if (length == NULL_TREE
4369 && first_non_one == types.length ()
4370 && tree_int_cst_equal
4371 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4372 low_bound))
4373 first_non_one++;
4375 else if (length == NULL_TREE)
4377 maybe_zero_len = true;
4378 if (first_non_one == types.length ())
4379 first_non_one++;
4381 if (length && TREE_CODE (length) == INTEGER_CST)
4383 if (tree_int_cst_lt (size, length))
4385 error_at (OMP_CLAUSE_LOCATION (c),
4386 "length %qE above array section size "
4387 "in %qs clause", length,
4388 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4389 return error_mark_node;
4391 if (TREE_CODE (low_bound) == INTEGER_CST)
4393 tree lbpluslen
4394 = size_binop (PLUS_EXPR,
4395 fold_convert (sizetype, low_bound),
4396 fold_convert (sizetype, length));
4397 if (TREE_CODE (lbpluslen) == INTEGER_CST
4398 && tree_int_cst_lt (size, lbpluslen))
4400 error_at (OMP_CLAUSE_LOCATION (c),
4401 "high bound %qE above array section size "
4402 "in %qs clause", lbpluslen,
4403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4404 return error_mark_node;
4409 else if (length == NULL_TREE)
4411 maybe_zero_len = true;
4412 if (first_non_one == types.length ())
4413 first_non_one++;
4416 /* For [lb:] we will need to evaluate lb more than once. */
4417 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4419 tree lb = cp_save_expr (low_bound);
4420 if (lb != low_bound)
4422 TREE_PURPOSE (t) = lb;
4423 low_bound = lb;
4427 else if (TREE_CODE (type) == POINTER_TYPE)
4429 if (length == NULL_TREE)
4431 error_at (OMP_CLAUSE_LOCATION (c),
4432 "for pointer type length expression must be specified");
4433 return error_mark_node;
4435 /* If there is a pointer type anywhere but in the very first
4436 array-section-subscript, the array section can't be contiguous. */
4437 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4438 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4440 error_at (OMP_CLAUSE_LOCATION (c),
4441 "array section is not contiguous in %qs clause",
4442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4443 return error_mark_node;
4446 else
4448 error_at (OMP_CLAUSE_LOCATION (c),
4449 "%qE does not have pointer or array type", ret);
4450 return error_mark_node;
4452 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4453 types.safe_push (TREE_TYPE (ret));
4454 /* We will need to evaluate lb more than once. */
4455 tree lb = cp_save_expr (low_bound);
4456 if (lb != low_bound)
4458 TREE_PURPOSE (t) = lb;
4459 low_bound = lb;
4461 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4462 return ret;
4465 /* Handle array sections for clause C. */
4467 static bool
4468 handle_omp_array_sections (tree c)
4470 bool maybe_zero_len = false;
4471 unsigned int first_non_one = 0;
4472 auto_vec<tree> types;
4473 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4474 maybe_zero_len, first_non_one);
4475 if (first == error_mark_node)
4476 return true;
4477 if (first == NULL_TREE)
4478 return false;
4479 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4481 tree t = OMP_CLAUSE_DECL (c);
4482 tree tem = NULL_TREE;
4483 if (processing_template_decl)
4484 return false;
4485 /* Need to evaluate side effects in the length expressions
4486 if any. */
4487 while (TREE_CODE (t) == TREE_LIST)
4489 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4491 if (tem == NULL_TREE)
4492 tem = TREE_VALUE (t);
4493 else
4494 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4495 TREE_VALUE (t), tem);
4497 t = TREE_CHAIN (t);
4499 if (tem)
4500 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4501 OMP_CLAUSE_DECL (c) = first;
4503 else
4505 unsigned int num = types.length (), i;
4506 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4507 tree condition = NULL_TREE;
4509 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4510 maybe_zero_len = true;
4511 if (processing_template_decl && maybe_zero_len)
4512 return false;
4514 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4515 t = TREE_CHAIN (t))
4517 tree low_bound = TREE_PURPOSE (t);
4518 tree length = TREE_VALUE (t);
4520 i--;
4521 if (low_bound
4522 && TREE_CODE (low_bound) == INTEGER_CST
4523 && TYPE_PRECISION (TREE_TYPE (low_bound))
4524 > TYPE_PRECISION (sizetype))
4525 low_bound = fold_convert (sizetype, low_bound);
4526 if (length
4527 && TREE_CODE (length) == INTEGER_CST
4528 && TYPE_PRECISION (TREE_TYPE (length))
4529 > TYPE_PRECISION (sizetype))
4530 length = fold_convert (sizetype, length);
4531 if (low_bound == NULL_TREE)
4532 low_bound = integer_zero_node;
4533 if (!maybe_zero_len && i > first_non_one)
4535 if (integer_nonzerop (low_bound))
4536 goto do_warn_noncontiguous;
4537 if (length != NULL_TREE
4538 && TREE_CODE (length) == INTEGER_CST
4539 && TYPE_DOMAIN (types[i])
4540 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4541 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4542 == INTEGER_CST)
4544 tree size;
4545 size = size_binop (PLUS_EXPR,
4546 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4547 size_one_node);
4548 if (!tree_int_cst_equal (length, size))
4550 do_warn_noncontiguous:
4551 error_at (OMP_CLAUSE_LOCATION (c),
4552 "array section is not contiguous in %qs "
4553 "clause",
4554 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4555 return true;
4558 if (!processing_template_decl
4559 && length != NULL_TREE
4560 && TREE_SIDE_EFFECTS (length))
4562 if (side_effects == NULL_TREE)
4563 side_effects = length;
4564 else
4565 side_effects = build2 (COMPOUND_EXPR,
4566 TREE_TYPE (side_effects),
4567 length, side_effects);
4570 else if (processing_template_decl)
4571 continue;
4572 else
4574 tree l;
4576 if (i > first_non_one && length && integer_nonzerop (length))
4577 continue;
4578 if (length)
4579 l = fold_convert (sizetype, length);
4580 else
4582 l = size_binop (PLUS_EXPR,
4583 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4584 size_one_node);
4585 l = size_binop (MINUS_EXPR, l,
4586 fold_convert (sizetype, low_bound));
4588 if (i > first_non_one)
4590 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4591 size_zero_node);
4592 if (condition == NULL_TREE)
4593 condition = l;
4594 else
4595 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4596 l, condition);
4598 else if (size == NULL_TREE)
4600 size = size_in_bytes (TREE_TYPE (types[i]));
4601 size = size_binop (MULT_EXPR, size, l);
4602 if (condition)
4603 size = fold_build3 (COND_EXPR, sizetype, condition,
4604 size, size_zero_node);
4606 else
4607 size = size_binop (MULT_EXPR, size, l);
4610 if (!processing_template_decl)
4612 if (side_effects)
4613 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4614 OMP_CLAUSE_DECL (c) = first;
4615 OMP_CLAUSE_SIZE (c) = size;
4616 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4617 return false;
4618 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4619 OMP_CLAUSE_MAP);
4620 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4621 if (!cxx_mark_addressable (t))
4622 return false;
4623 OMP_CLAUSE_DECL (c2) = t;
4624 t = build_fold_addr_expr (first);
4625 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4626 ptrdiff_type_node, t);
4627 tree ptr = OMP_CLAUSE_DECL (c2);
4628 ptr = convert_from_reference (ptr);
4629 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4630 ptr = build_fold_addr_expr (ptr);
4631 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4632 ptrdiff_type_node, t,
4633 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4634 ptrdiff_type_node, ptr));
4635 OMP_CLAUSE_SIZE (c2) = t;
4636 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4637 OMP_CLAUSE_CHAIN (c) = c2;
4638 ptr = OMP_CLAUSE_DECL (c2);
4639 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4640 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4642 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4643 OMP_CLAUSE_MAP);
4644 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4645 OMP_CLAUSE_DECL (c3) = ptr;
4646 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4647 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4648 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4649 OMP_CLAUSE_CHAIN (c2) = c3;
4653 return false;
4656 /* Return identifier to look up for omp declare reduction. */
4658 tree
4659 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4661 const char *p = NULL;
4662 const char *m = NULL;
4663 switch (reduction_code)
4665 case PLUS_EXPR:
4666 case MULT_EXPR:
4667 case MINUS_EXPR:
4668 case BIT_AND_EXPR:
4669 case BIT_XOR_EXPR:
4670 case BIT_IOR_EXPR:
4671 case TRUTH_ANDIF_EXPR:
4672 case TRUTH_ORIF_EXPR:
4673 reduction_id = ansi_opname (reduction_code);
4674 break;
4675 case MIN_EXPR:
4676 p = "min";
4677 break;
4678 case MAX_EXPR:
4679 p = "max";
4680 break;
4681 default:
4682 break;
4685 if (p == NULL)
4687 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4688 return error_mark_node;
4689 p = IDENTIFIER_POINTER (reduction_id);
4692 if (type != NULL_TREE)
4693 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4695 const char prefix[] = "omp declare reduction ";
4696 size_t lenp = sizeof (prefix);
4697 if (strncmp (p, prefix, lenp - 1) == 0)
4698 lenp = 1;
4699 size_t len = strlen (p);
4700 size_t lenm = m ? strlen (m) + 1 : 0;
4701 char *name = XALLOCAVEC (char, lenp + len + lenm);
4702 if (lenp > 1)
4703 memcpy (name, prefix, lenp - 1);
4704 memcpy (name + lenp - 1, p, len + 1);
4705 if (m)
4707 name[lenp + len - 1] = '~';
4708 memcpy (name + lenp + len, m, lenm);
4710 return get_identifier (name);
4713 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4714 FUNCTION_DECL or NULL_TREE if not found. */
4716 static tree
4717 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4718 vec<tree> *ambiguousp)
4720 tree orig_id = id;
4721 tree baselink = NULL_TREE;
4722 if (identifier_p (id))
4724 cp_id_kind idk;
4725 bool nonint_cst_expression_p;
4726 const char *error_msg;
4727 id = omp_reduction_id (ERROR_MARK, id, type);
4728 tree decl = lookup_name (id);
4729 if (decl == NULL_TREE)
4730 decl = error_mark_node;
4731 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4732 &nonint_cst_expression_p, false, true, false,
4733 false, &error_msg, loc);
4734 if (idk == CP_ID_KIND_UNQUALIFIED
4735 && identifier_p (id))
4737 vec<tree, va_gc> *args = NULL;
4738 vec_safe_push (args, build_reference_type (type));
4739 id = perform_koenig_lookup (id, args, tf_none);
4742 else if (TREE_CODE (id) == SCOPE_REF)
4743 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4744 omp_reduction_id (ERROR_MARK,
4745 TREE_OPERAND (id, 1),
4746 type),
4747 false, false);
4748 tree fns = id;
4749 if (id && is_overloaded_fn (id))
4750 id = get_fns (id);
4751 for (; id; id = OVL_NEXT (id))
4753 tree fndecl = OVL_CURRENT (id);
4754 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4756 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4757 if (same_type_p (TREE_TYPE (argtype), type))
4758 break;
4761 if (id && BASELINK_P (fns))
4763 if (baselinkp)
4764 *baselinkp = fns;
4765 else
4766 baselink = fns;
4768 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4770 vec<tree> ambiguous = vNULL;
4771 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4772 unsigned int ix;
4773 if (ambiguousp == NULL)
4774 ambiguousp = &ambiguous;
4775 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4777 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4778 baselinkp ? baselinkp : &baselink,
4779 ambiguousp);
4780 if (id == NULL_TREE)
4781 continue;
4782 if (!ambiguousp->is_empty ())
4783 ambiguousp->safe_push (id);
4784 else if (ret != NULL_TREE)
4786 ambiguousp->safe_push (ret);
4787 ambiguousp->safe_push (id);
4788 ret = NULL_TREE;
4790 else
4791 ret = id;
4793 if (ambiguousp != &ambiguous)
4794 return ret;
4795 if (!ambiguous.is_empty ())
4797 const char *str = _("candidates are:");
4798 unsigned int idx;
4799 tree udr;
4800 error_at (loc, "user defined reduction lookup is ambiguous");
4801 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4803 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4804 if (idx == 0)
4805 str = get_spaces (str);
4807 ambiguous.release ();
4808 ret = error_mark_node;
4809 baselink = NULL_TREE;
4811 id = ret;
4813 if (id && baselink)
4814 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4815 id, id, tf_warning_or_error);
4816 return id;
4819 /* Helper function for cp_parser_omp_declare_reduction_exprs
4820 and tsubst_omp_udr.
4821 Remove CLEANUP_STMT for data (omp_priv variable).
4822 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4823 DECL_EXPR. */
4825 tree
4826 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4828 if (TYPE_P (*tp))
4829 *walk_subtrees = 0;
4830 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4831 *tp = CLEANUP_BODY (*tp);
4832 else if (TREE_CODE (*tp) == DECL_EXPR)
4834 tree decl = DECL_EXPR_DECL (*tp);
4835 if (!processing_template_decl
4836 && decl == (tree) data
4837 && DECL_INITIAL (decl)
4838 && DECL_INITIAL (decl) != error_mark_node)
4840 tree list = NULL_TREE;
4841 append_to_statement_list_force (*tp, &list);
4842 tree init_expr = build2 (INIT_EXPR, void_type_node,
4843 decl, DECL_INITIAL (decl));
4844 DECL_INITIAL (decl) = NULL_TREE;
4845 append_to_statement_list_force (init_expr, &list);
4846 *tp = list;
4849 return NULL_TREE;
4852 /* Data passed from cp_check_omp_declare_reduction to
4853 cp_check_omp_declare_reduction_r. */
4855 struct cp_check_omp_declare_reduction_data
4857 location_t loc;
4858 tree stmts[7];
4859 bool combiner_p;
4862 /* Helper function for cp_check_omp_declare_reduction, called via
4863 cp_walk_tree. */
4865 static tree
4866 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4868 struct cp_check_omp_declare_reduction_data *udr_data
4869 = (struct cp_check_omp_declare_reduction_data *) data;
4870 if (SSA_VAR_P (*tp)
4871 && !DECL_ARTIFICIAL (*tp)
4872 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4873 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4875 location_t loc = udr_data->loc;
4876 if (udr_data->combiner_p)
4877 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4878 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4879 *tp);
4880 else
4881 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4882 "to variable %qD which is not %<omp_priv%> nor "
4883 "%<omp_orig%>",
4884 *tp);
4885 return *tp;
4887 return NULL_TREE;
4890 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4892 void
4893 cp_check_omp_declare_reduction (tree udr)
4895 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4896 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4897 type = TREE_TYPE (type);
4898 int i;
4899 location_t loc = DECL_SOURCE_LOCATION (udr);
4901 if (type == error_mark_node)
4902 return;
4903 if (ARITHMETIC_TYPE_P (type))
4905 static enum tree_code predef_codes[]
4906 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4907 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4908 for (i = 0; i < 8; i++)
4910 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4911 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4912 const char *n2 = IDENTIFIER_POINTER (id);
4913 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4914 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4915 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4916 break;
4919 if (i == 8
4920 && TREE_CODE (type) != COMPLEX_EXPR)
4922 const char prefix_minmax[] = "omp declare reduction m";
4923 size_t prefix_size = sizeof (prefix_minmax) - 1;
4924 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4925 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4926 prefix_minmax, prefix_size) == 0
4927 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4928 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4929 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4930 i = 0;
4932 if (i < 8)
4934 error_at (loc, "predeclared arithmetic type %qT in "
4935 "%<#pragma omp declare reduction%>", type);
4936 return;
4939 else if (TREE_CODE (type) == FUNCTION_TYPE
4940 || TREE_CODE (type) == METHOD_TYPE
4941 || TREE_CODE (type) == ARRAY_TYPE)
4943 error_at (loc, "function or array type %qT in "
4944 "%<#pragma omp declare reduction%>", type);
4945 return;
4947 else if (TREE_CODE (type) == REFERENCE_TYPE)
4949 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4950 type);
4951 return;
4953 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4955 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4956 "%<#pragma omp declare reduction%>", type);
4957 return;
4960 tree body = DECL_SAVED_TREE (udr);
4961 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4962 return;
4964 tree_stmt_iterator tsi;
4965 struct cp_check_omp_declare_reduction_data data;
4966 memset (data.stmts, 0, sizeof data.stmts);
4967 for (i = 0, tsi = tsi_start (body);
4968 i < 7 && !tsi_end_p (tsi);
4969 i++, tsi_next (&tsi))
4970 data.stmts[i] = tsi_stmt (tsi);
4971 data.loc = loc;
4972 gcc_assert (tsi_end_p (tsi));
4973 if (i >= 3)
4975 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4976 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4977 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4978 return;
4979 data.combiner_p = true;
4980 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4981 &data, NULL))
4982 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4984 if (i >= 6)
4986 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4987 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4988 data.combiner_p = false;
4989 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4990 &data, NULL)
4991 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4992 cp_check_omp_declare_reduction_r, &data, NULL))
4993 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4994 if (i == 7)
4995 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4999 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5000 an inline call. But, remap
5001 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5002 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5004 static tree
5005 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5006 tree decl, tree placeholder)
5008 copy_body_data id;
5009 hash_map<tree, tree> decl_map;
5011 decl_map.put (omp_decl1, placeholder);
5012 decl_map.put (omp_decl2, decl);
5013 memset (&id, 0, sizeof (id));
5014 id.src_fn = DECL_CONTEXT (omp_decl1);
5015 id.dst_fn = current_function_decl;
5016 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5017 id.decl_map = &decl_map;
5019 id.copy_decl = copy_decl_no_change;
5020 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5021 id.transform_new_cfg = true;
5022 id.transform_return_to_modify = false;
5023 id.transform_lang_insert_block = NULL;
5024 id.eh_lp_nr = 0;
5025 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5026 return stmt;
5029 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5030 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5032 static tree
5033 find_omp_placeholder_r (tree *tp, int *, void *data)
5035 if (*tp == (tree) data)
5036 return *tp;
5037 return NULL_TREE;
5040 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5041 Return true if there is some error and the clause should be removed. */
5043 static bool
5044 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5046 tree t = OMP_CLAUSE_DECL (c);
5047 bool predefined = false;
5048 tree type = TREE_TYPE (t);
5049 if (TREE_CODE (type) == REFERENCE_TYPE)
5050 type = TREE_TYPE (type);
5051 if (type == error_mark_node)
5052 return true;
5053 else if (ARITHMETIC_TYPE_P (type))
5054 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5056 case PLUS_EXPR:
5057 case MULT_EXPR:
5058 case MINUS_EXPR:
5059 predefined = true;
5060 break;
5061 case MIN_EXPR:
5062 case MAX_EXPR:
5063 if (TREE_CODE (type) == COMPLEX_TYPE)
5064 break;
5065 predefined = true;
5066 break;
5067 case BIT_AND_EXPR:
5068 case BIT_IOR_EXPR:
5069 case BIT_XOR_EXPR:
5070 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5071 break;
5072 predefined = true;
5073 break;
5074 case TRUTH_ANDIF_EXPR:
5075 case TRUTH_ORIF_EXPR:
5076 if (FLOAT_TYPE_P (type))
5077 break;
5078 predefined = true;
5079 break;
5080 default:
5081 break;
5083 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5085 error ("%qE has invalid type for %<reduction%>", t);
5086 return true;
5088 else if (!processing_template_decl)
5090 t = require_complete_type (t);
5091 if (t == error_mark_node)
5092 return true;
5093 OMP_CLAUSE_DECL (c) = t;
5096 if (predefined)
5098 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5099 return false;
5101 else if (processing_template_decl)
5102 return false;
5104 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5106 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5107 if (TREE_CODE (type) == REFERENCE_TYPE)
5108 type = TREE_TYPE (type);
5109 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5110 if (id == NULL_TREE)
5111 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5112 NULL_TREE, NULL_TREE);
5113 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5114 if (id)
5116 if (id == error_mark_node)
5117 return true;
5118 id = OVL_CURRENT (id);
5119 mark_used (id);
5120 tree body = DECL_SAVED_TREE (id);
5121 if (TREE_CODE (body) == STATEMENT_LIST)
5123 tree_stmt_iterator tsi;
5124 tree placeholder = NULL_TREE;
5125 int i;
5126 tree stmts[7];
5127 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5128 atype = TREE_TYPE (atype);
5129 bool need_static_cast = !same_type_p (type, atype);
5130 memset (stmts, 0, sizeof stmts);
5131 for (i = 0, tsi = tsi_start (body);
5132 i < 7 && !tsi_end_p (tsi);
5133 i++, tsi_next (&tsi))
5134 stmts[i] = tsi_stmt (tsi);
5135 gcc_assert (tsi_end_p (tsi));
5137 if (i >= 3)
5139 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5140 && TREE_CODE (stmts[1]) == DECL_EXPR);
5141 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5142 DECL_ARTIFICIAL (placeholder) = 1;
5143 DECL_IGNORED_P (placeholder) = 1;
5144 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5145 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5146 cxx_mark_addressable (placeholder);
5147 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5148 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5149 != REFERENCE_TYPE)
5150 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5151 tree omp_out = placeholder;
5152 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5153 if (need_static_cast)
5155 tree rtype = build_reference_type (atype);
5156 omp_out = build_static_cast (rtype, omp_out,
5157 tf_warning_or_error);
5158 omp_in = build_static_cast (rtype, omp_in,
5159 tf_warning_or_error);
5160 if (omp_out == error_mark_node || omp_in == error_mark_node)
5161 return true;
5162 omp_out = convert_from_reference (omp_out);
5163 omp_in = convert_from_reference (omp_in);
5165 OMP_CLAUSE_REDUCTION_MERGE (c)
5166 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5167 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5169 if (i >= 6)
5171 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5172 && TREE_CODE (stmts[4]) == DECL_EXPR);
5173 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5174 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5175 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5176 cxx_mark_addressable (placeholder);
5177 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5178 tree omp_orig = placeholder;
5179 if (need_static_cast)
5181 if (i == 7)
5183 error_at (OMP_CLAUSE_LOCATION (c),
5184 "user defined reduction with constructor "
5185 "initializer for base class %qT", atype);
5186 return true;
5188 tree rtype = build_reference_type (atype);
5189 omp_priv = build_static_cast (rtype, omp_priv,
5190 tf_warning_or_error);
5191 omp_orig = build_static_cast (rtype, omp_orig,
5192 tf_warning_or_error);
5193 if (omp_priv == error_mark_node
5194 || omp_orig == error_mark_node)
5195 return true;
5196 omp_priv = convert_from_reference (omp_priv);
5197 omp_orig = convert_from_reference (omp_orig);
5199 if (i == 6)
5200 *need_default_ctor = true;
5201 OMP_CLAUSE_REDUCTION_INIT (c)
5202 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5203 DECL_EXPR_DECL (stmts[3]),
5204 omp_priv, omp_orig);
5205 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5206 find_omp_placeholder_r, placeholder, NULL))
5207 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5209 else if (i >= 3)
5211 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5212 *need_default_ctor = true;
5213 else
5215 tree init;
5216 tree v = convert_from_reference (t);
5217 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5218 init = build_constructor (TREE_TYPE (v), NULL);
5219 else
5220 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5221 OMP_CLAUSE_REDUCTION_INIT (c)
5222 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5227 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5228 *need_dtor = true;
5229 else
5231 error ("user defined reduction not found for %qD", t);
5232 return true;
5234 return false;
5237 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5238 Remove any elements from the list that are invalid. */
5240 tree
5241 finish_omp_clauses (tree clauses)
5243 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5244 bitmap_head aligned_head;
5245 tree c, t, *pc;
5246 bool branch_seen = false;
5247 bool copyprivate_seen = false;
5249 bitmap_obstack_initialize (NULL);
5250 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5251 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5252 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5253 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5255 for (pc = &clauses, c = clauses; c ; c = *pc)
5257 bool remove = false;
5259 switch (OMP_CLAUSE_CODE (c))
5261 case OMP_CLAUSE_SHARED:
5262 goto check_dup_generic;
5263 case OMP_CLAUSE_PRIVATE:
5264 goto check_dup_generic;
5265 case OMP_CLAUSE_REDUCTION:
5266 goto check_dup_generic;
5267 case OMP_CLAUSE_COPYPRIVATE:
5268 copyprivate_seen = true;
5269 goto check_dup_generic;
5270 case OMP_CLAUSE_COPYIN:
5271 goto check_dup_generic;
5272 case OMP_CLAUSE_LINEAR:
5273 t = OMP_CLAUSE_DECL (c);
5274 if (!type_dependent_expression_p (t)
5275 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5276 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5278 error ("linear clause applied to non-integral non-pointer "
5279 "variable with %qT type", TREE_TYPE (t));
5280 remove = true;
5281 break;
5283 t = OMP_CLAUSE_LINEAR_STEP (c);
5284 if (t == NULL_TREE)
5285 t = integer_one_node;
5286 if (t == error_mark_node)
5288 remove = true;
5289 break;
5291 else if (!type_dependent_expression_p (t)
5292 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5294 error ("linear step expression must be integral");
5295 remove = true;
5296 break;
5298 else
5300 t = mark_rvalue_use (t);
5301 if (!processing_template_decl)
5303 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5304 t = maybe_constant_value (t);
5305 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5306 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5307 == POINTER_TYPE)
5309 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5310 OMP_CLAUSE_DECL (c), t);
5311 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5312 MINUS_EXPR, sizetype, t,
5313 OMP_CLAUSE_DECL (c));
5314 if (t == error_mark_node)
5316 remove = true;
5317 break;
5320 else
5321 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5323 OMP_CLAUSE_LINEAR_STEP (c) = t;
5325 goto check_dup_generic;
5326 check_dup_generic:
5327 t = OMP_CLAUSE_DECL (c);
5328 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5330 if (processing_template_decl)
5331 break;
5332 if (DECL_P (t))
5333 error ("%qD is not a variable in clause %qs", t,
5334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5335 else
5336 error ("%qE is not a variable in clause %qs", t,
5337 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5338 remove = true;
5340 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5341 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5342 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5344 error ("%qD appears more than once in data clauses", t);
5345 remove = true;
5347 else
5348 bitmap_set_bit (&generic_head, DECL_UID (t));
5349 break;
5351 case OMP_CLAUSE_FIRSTPRIVATE:
5352 t = OMP_CLAUSE_DECL (c);
5353 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5355 if (processing_template_decl)
5356 break;
5357 if (DECL_P (t))
5358 error ("%qD is not a variable in clause %<firstprivate%>", t);
5359 else
5360 error ("%qE is not a variable in clause %<firstprivate%>", t);
5361 remove = true;
5363 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5364 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5366 error ("%qD appears more than once in data clauses", t);
5367 remove = true;
5369 else
5370 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5371 break;
5373 case OMP_CLAUSE_LASTPRIVATE:
5374 t = OMP_CLAUSE_DECL (c);
5375 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5377 if (processing_template_decl)
5378 break;
5379 if (DECL_P (t))
5380 error ("%qD is not a variable in clause %<lastprivate%>", t);
5381 else
5382 error ("%qE is not a variable in clause %<lastprivate%>", t);
5383 remove = true;
5385 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5386 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5388 error ("%qD appears more than once in data clauses", t);
5389 remove = true;
5391 else
5392 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5393 break;
5395 case OMP_CLAUSE_IF:
5396 t = OMP_CLAUSE_IF_EXPR (c);
5397 t = maybe_convert_cond (t);
5398 if (t == error_mark_node)
5399 remove = true;
5400 else if (!processing_template_decl)
5401 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5402 OMP_CLAUSE_IF_EXPR (c) = t;
5403 break;
5405 case OMP_CLAUSE_FINAL:
5406 t = OMP_CLAUSE_FINAL_EXPR (c);
5407 t = maybe_convert_cond (t);
5408 if (t == error_mark_node)
5409 remove = true;
5410 else if (!processing_template_decl)
5411 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5412 OMP_CLAUSE_FINAL_EXPR (c) = t;
5413 break;
5415 case OMP_CLAUSE_NUM_THREADS:
5416 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5417 if (t == error_mark_node)
5418 remove = true;
5419 else if (!type_dependent_expression_p (t)
5420 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5422 error ("num_threads expression must be integral");
5423 remove = true;
5425 else
5427 t = mark_rvalue_use (t);
5428 if (!processing_template_decl)
5429 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5430 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5432 break;
5434 case OMP_CLAUSE_SCHEDULE:
5435 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5436 if (t == NULL)
5438 else if (t == error_mark_node)
5439 remove = true;
5440 else if (!type_dependent_expression_p (t)
5441 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5442 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5443 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5445 error ("schedule chunk size expression must be integral");
5446 remove = true;
5448 else
5450 t = mark_rvalue_use (t);
5451 if (!processing_template_decl)
5453 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5454 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5456 t = convert_to_integer (long_integer_type_node, t);
5457 if (t == error_mark_node)
5459 remove = true;
5460 break;
5463 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5465 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5467 break;
5469 case OMP_CLAUSE_SIMDLEN:
5470 case OMP_CLAUSE_SAFELEN:
5471 t = OMP_CLAUSE_OPERAND (c, 0);
5472 if (t == error_mark_node)
5473 remove = true;
5474 else if (!type_dependent_expression_p (t)
5475 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5477 error ("%qs length expression must be integral",
5478 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5479 remove = true;
5481 else
5483 t = mark_rvalue_use (t);
5484 t = maybe_constant_value (t);
5485 if (!processing_template_decl)
5487 if (TREE_CODE (t) != INTEGER_CST
5488 || tree_int_cst_sgn (t) != 1)
5490 error ("%qs length expression must be positive constant"
5491 " integer expression",
5492 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5493 remove = true;
5496 OMP_CLAUSE_OPERAND (c, 0) = t;
5498 break;
5500 case OMP_CLAUSE_NUM_TEAMS:
5501 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5502 if (t == error_mark_node)
5503 remove = true;
5504 else if (!type_dependent_expression_p (t)
5505 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5507 error ("%<num_teams%> expression must be integral");
5508 remove = true;
5510 else
5512 t = mark_rvalue_use (t);
5513 if (!processing_template_decl)
5514 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5515 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5517 break;
5519 case OMP_CLAUSE_ASYNC:
5520 t = OMP_CLAUSE_ASYNC_EXPR (c);
5521 if (t == error_mark_node)
5522 remove = true;
5523 else if (!type_dependent_expression_p (t)
5524 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5526 error ("%<async%> expression must be integral");
5527 remove = true;
5529 else
5531 t = mark_rvalue_use (t);
5532 if (!processing_template_decl)
5533 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5534 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5536 break;
5538 case OMP_CLAUSE_VECTOR_LENGTH:
5539 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5540 t = maybe_convert_cond (t);
5541 if (t == error_mark_node)
5542 remove = true;
5543 else if (!processing_template_decl)
5544 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5545 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5546 break;
5548 case OMP_CLAUSE_WAIT:
5549 t = OMP_CLAUSE_WAIT_EXPR (c);
5550 if (t == error_mark_node)
5551 remove = true;
5552 else if (!processing_template_decl)
5553 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5554 OMP_CLAUSE_WAIT_EXPR (c) = t;
5555 break;
5557 case OMP_CLAUSE_THREAD_LIMIT:
5558 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5559 if (t == error_mark_node)
5560 remove = true;
5561 else if (!type_dependent_expression_p (t)
5562 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5564 error ("%<thread_limit%> expression must be integral");
5565 remove = true;
5567 else
5569 t = mark_rvalue_use (t);
5570 if (!processing_template_decl)
5571 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5572 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5574 break;
5576 case OMP_CLAUSE_DEVICE:
5577 t = OMP_CLAUSE_DEVICE_ID (c);
5578 if (t == error_mark_node)
5579 remove = true;
5580 else if (!type_dependent_expression_p (t)
5581 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5583 error ("%<device%> id must be integral");
5584 remove = true;
5586 else
5588 t = mark_rvalue_use (t);
5589 if (!processing_template_decl)
5590 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5591 OMP_CLAUSE_DEVICE_ID (c) = t;
5593 break;
5595 case OMP_CLAUSE_DIST_SCHEDULE:
5596 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5597 if (t == NULL)
5599 else if (t == error_mark_node)
5600 remove = true;
5601 else if (!type_dependent_expression_p (t)
5602 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5604 error ("%<dist_schedule%> chunk size expression must be "
5605 "integral");
5606 remove = true;
5608 else
5610 t = mark_rvalue_use (t);
5611 if (!processing_template_decl)
5612 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5613 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5615 break;
5617 case OMP_CLAUSE_ALIGNED:
5618 t = OMP_CLAUSE_DECL (c);
5619 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5621 if (processing_template_decl)
5622 break;
5623 if (DECL_P (t))
5624 error ("%qD is not a variable in %<aligned%> clause", t);
5625 else
5626 error ("%qE is not a variable in %<aligned%> clause", t);
5627 remove = true;
5629 else if (!type_dependent_expression_p (t)
5630 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5631 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5632 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5633 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5634 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5635 != ARRAY_TYPE))))
5637 error_at (OMP_CLAUSE_LOCATION (c),
5638 "%qE in %<aligned%> clause is neither a pointer nor "
5639 "an array nor a reference to pointer or array", t);
5640 remove = true;
5642 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5644 error ("%qD appears more than once in %<aligned%> clauses", t);
5645 remove = true;
5647 else
5648 bitmap_set_bit (&aligned_head, DECL_UID (t));
5649 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5650 if (t == error_mark_node)
5651 remove = true;
5652 else if (t == NULL_TREE)
5653 break;
5654 else if (!type_dependent_expression_p (t)
5655 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5657 error ("%<aligned%> clause alignment expression must "
5658 "be integral");
5659 remove = true;
5661 else
5663 t = mark_rvalue_use (t);
5664 t = maybe_constant_value (t);
5665 if (!processing_template_decl)
5667 if (TREE_CODE (t) != INTEGER_CST
5668 || tree_int_cst_sgn (t) != 1)
5670 error ("%<aligned%> clause alignment expression must be "
5671 "positive constant integer expression");
5672 remove = true;
5675 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5677 break;
5679 case OMP_CLAUSE_DEPEND:
5680 t = OMP_CLAUSE_DECL (c);
5681 if (TREE_CODE (t) == TREE_LIST)
5683 if (handle_omp_array_sections (c))
5684 remove = true;
5685 break;
5687 if (t == error_mark_node)
5688 remove = true;
5689 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5691 if (processing_template_decl)
5692 break;
5693 if (DECL_P (t))
5694 error ("%qD is not a variable in %<depend%> clause", t);
5695 else
5696 error ("%qE is not a variable in %<depend%> clause", t);
5697 remove = true;
5699 else if (!processing_template_decl
5700 && !cxx_mark_addressable (t))
5701 remove = true;
5702 break;
5704 case OMP_CLAUSE_MAP:
5705 case OMP_CLAUSE_TO:
5706 case OMP_CLAUSE_FROM:
5707 t = OMP_CLAUSE_DECL (c);
5708 if (TREE_CODE (t) == TREE_LIST)
5710 if (handle_omp_array_sections (c))
5711 remove = true;
5712 else
5714 t = OMP_CLAUSE_DECL (c);
5715 if (TREE_CODE (t) != TREE_LIST
5716 && !type_dependent_expression_p (t)
5717 && !cp_omp_mappable_type (TREE_TYPE (t)))
5719 error_at (OMP_CLAUSE_LOCATION (c),
5720 "array section does not have mappable type "
5721 "in %qs clause",
5722 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5723 remove = true;
5726 break;
5728 if (t == error_mark_node)
5729 remove = true;
5730 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5732 if (processing_template_decl)
5733 break;
5734 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5735 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5736 break;
5737 if (DECL_P (t))
5738 error ("%qD is not a variable in %qs clause", t,
5739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5740 else
5741 error ("%qE is not a variable in %qs clause", t,
5742 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5743 remove = true;
5745 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5747 error ("%qD is threadprivate variable in %qs clause", t,
5748 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5749 remove = true;
5751 else if (!processing_template_decl
5752 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5753 && !cxx_mark_addressable (t))
5754 remove = true;
5755 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5756 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5757 && !type_dependent_expression_p (t)
5758 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5759 == REFERENCE_TYPE)
5760 ? TREE_TYPE (TREE_TYPE (t))
5761 : TREE_TYPE (t)))
5763 error_at (OMP_CLAUSE_LOCATION (c),
5764 "%qD does not have a mappable type in %qs clause", t,
5765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5766 remove = true;
5768 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5770 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5771 error ("%qD appears more than once in motion clauses", t);
5772 else
5773 error ("%qD appears more than once in map clauses", t);
5774 remove = true;
5776 else
5777 bitmap_set_bit (&generic_head, DECL_UID (t));
5778 break;
5780 case OMP_CLAUSE_UNIFORM:
5781 t = OMP_CLAUSE_DECL (c);
5782 if (TREE_CODE (t) != PARM_DECL)
5784 if (processing_template_decl)
5785 break;
5786 if (DECL_P (t))
5787 error ("%qD is not an argument in %<uniform%> clause", t);
5788 else
5789 error ("%qE is not an argument in %<uniform%> clause", t);
5790 remove = true;
5791 break;
5793 goto check_dup_generic;
5795 case OMP_CLAUSE_NOWAIT:
5796 case OMP_CLAUSE_ORDERED:
5797 case OMP_CLAUSE_DEFAULT:
5798 case OMP_CLAUSE_UNTIED:
5799 case OMP_CLAUSE_COLLAPSE:
5800 case OMP_CLAUSE_MERGEABLE:
5801 case OMP_CLAUSE_PARALLEL:
5802 case OMP_CLAUSE_FOR:
5803 case OMP_CLAUSE_SECTIONS:
5804 case OMP_CLAUSE_TASKGROUP:
5805 case OMP_CLAUSE_PROC_BIND:
5806 case OMP_CLAUSE__CILK_FOR_COUNT_:
5807 break;
5809 case OMP_CLAUSE_INBRANCH:
5810 case OMP_CLAUSE_NOTINBRANCH:
5811 if (branch_seen)
5813 error ("%<inbranch%> clause is incompatible with "
5814 "%<notinbranch%>");
5815 remove = true;
5817 branch_seen = true;
5818 break;
5820 default:
5821 gcc_unreachable ();
5824 if (remove)
5825 *pc = OMP_CLAUSE_CHAIN (c);
5826 else
5827 pc = &OMP_CLAUSE_CHAIN (c);
5830 for (pc = &clauses, c = clauses; c ; c = *pc)
5832 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5833 bool remove = false;
5834 bool need_complete_non_reference = false;
5835 bool need_default_ctor = false;
5836 bool need_copy_ctor = false;
5837 bool need_copy_assignment = false;
5838 bool need_implicitly_determined = false;
5839 bool need_dtor = false;
5840 tree type, inner_type;
5842 switch (c_kind)
5844 case OMP_CLAUSE_SHARED:
5845 need_implicitly_determined = true;
5846 break;
5847 case OMP_CLAUSE_PRIVATE:
5848 need_complete_non_reference = true;
5849 need_default_ctor = true;
5850 need_dtor = true;
5851 need_implicitly_determined = true;
5852 break;
5853 case OMP_CLAUSE_FIRSTPRIVATE:
5854 need_complete_non_reference = true;
5855 need_copy_ctor = true;
5856 need_dtor = true;
5857 need_implicitly_determined = true;
5858 break;
5859 case OMP_CLAUSE_LASTPRIVATE:
5860 need_complete_non_reference = true;
5861 need_copy_assignment = true;
5862 need_implicitly_determined = true;
5863 break;
5864 case OMP_CLAUSE_REDUCTION:
5865 need_implicitly_determined = true;
5866 break;
5867 case OMP_CLAUSE_COPYPRIVATE:
5868 need_copy_assignment = true;
5869 break;
5870 case OMP_CLAUSE_COPYIN:
5871 need_copy_assignment = true;
5872 break;
5873 case OMP_CLAUSE_NOWAIT:
5874 if (copyprivate_seen)
5876 error_at (OMP_CLAUSE_LOCATION (c),
5877 "%<nowait%> clause must not be used together "
5878 "with %<copyprivate%>");
5879 *pc = OMP_CLAUSE_CHAIN (c);
5880 continue;
5882 /* FALLTHRU */
5883 default:
5884 pc = &OMP_CLAUSE_CHAIN (c);
5885 continue;
5888 t = OMP_CLAUSE_DECL (c);
5889 if (processing_template_decl
5890 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5892 pc = &OMP_CLAUSE_CHAIN (c);
5893 continue;
5896 switch (c_kind)
5898 case OMP_CLAUSE_LASTPRIVATE:
5899 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5901 need_default_ctor = true;
5902 need_dtor = true;
5904 break;
5906 case OMP_CLAUSE_REDUCTION:
5907 if (finish_omp_reduction_clause (c, &need_default_ctor,
5908 &need_dtor))
5909 remove = true;
5910 else
5911 t = OMP_CLAUSE_DECL (c);
5912 break;
5914 case OMP_CLAUSE_COPYIN:
5915 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5917 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5918 remove = true;
5920 break;
5922 default:
5923 break;
5926 if (need_complete_non_reference || need_copy_assignment)
5928 t = require_complete_type (t);
5929 if (t == error_mark_node)
5930 remove = true;
5931 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5932 && need_complete_non_reference)
5934 error ("%qE has reference type for %qs", t,
5935 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5936 remove = true;
5939 if (need_implicitly_determined)
5941 const char *share_name = NULL;
5943 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5944 share_name = "threadprivate";
5945 else switch (cxx_omp_predetermined_sharing (t))
5947 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5948 break;
5949 case OMP_CLAUSE_DEFAULT_SHARED:
5950 /* const vars may be specified in firstprivate clause. */
5951 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5952 && cxx_omp_const_qual_no_mutable (t))
5953 break;
5954 share_name = "shared";
5955 break;
5956 case OMP_CLAUSE_DEFAULT_PRIVATE:
5957 share_name = "private";
5958 break;
5959 default:
5960 gcc_unreachable ();
5962 if (share_name)
5964 error ("%qE is predetermined %qs for %qs",
5965 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5966 remove = true;
5970 /* We're interested in the base element, not arrays. */
5971 inner_type = type = TREE_TYPE (t);
5972 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5973 inner_type = TREE_TYPE (inner_type);
5975 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5976 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5977 inner_type = TREE_TYPE (inner_type);
5979 /* Check for special function availability by building a call to one.
5980 Save the results, because later we won't be in the right context
5981 for making these queries. */
5982 if (CLASS_TYPE_P (inner_type)
5983 && COMPLETE_TYPE_P (inner_type)
5984 && (need_default_ctor || need_copy_ctor
5985 || need_copy_assignment || need_dtor)
5986 && !type_dependent_expression_p (t)
5987 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5988 need_copy_ctor, need_copy_assignment,
5989 need_dtor))
5990 remove = true;
5992 if (remove)
5993 *pc = OMP_CLAUSE_CHAIN (c);
5994 else
5995 pc = &OMP_CLAUSE_CHAIN (c);
5998 bitmap_obstack_release (NULL);
5999 return clauses;
6002 /* For all variables in the tree_list VARS, mark them as thread local. */
6004 void
6005 finish_omp_threadprivate (tree vars)
6007 tree t;
6009 /* Mark every variable in VARS to be assigned thread local storage. */
6010 for (t = vars; t; t = TREE_CHAIN (t))
6012 tree v = TREE_PURPOSE (t);
6014 if (error_operand_p (v))
6016 else if (!VAR_P (v))
6017 error ("%<threadprivate%> %qD is not file, namespace "
6018 "or block scope variable", v);
6019 /* If V had already been marked threadprivate, it doesn't matter
6020 whether it had been used prior to this point. */
6021 else if (TREE_USED (v)
6022 && (DECL_LANG_SPECIFIC (v) == NULL
6023 || !CP_DECL_THREADPRIVATE_P (v)))
6024 error ("%qE declared %<threadprivate%> after first use", v);
6025 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6026 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6027 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6028 error ("%<threadprivate%> %qE has incomplete type", v);
6029 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6030 && CP_DECL_CONTEXT (v) != current_class_type)
6031 error ("%<threadprivate%> %qE directive not "
6032 "in %qT definition", v, CP_DECL_CONTEXT (v));
6033 else
6035 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6036 if (DECL_LANG_SPECIFIC (v) == NULL)
6038 retrofit_lang_decl (v);
6040 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6041 after the allocation of the lang_decl structure. */
6042 if (DECL_DISCRIMINATOR_P (v))
6043 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6046 if (! DECL_THREAD_LOCAL_P (v))
6048 set_decl_tls_model (v, decl_default_tls_model (v));
6049 /* If rtl has been already set for this var, call
6050 make_decl_rtl once again, so that encode_section_info
6051 has a chance to look at the new decl flags. */
6052 if (DECL_RTL_SET_P (v))
6053 make_decl_rtl (v);
6055 CP_DECL_THREADPRIVATE_P (v) = 1;
6060 /* Build an OpenMP structured block. */
6062 tree
6063 begin_omp_structured_block (void)
6065 return do_pushlevel (sk_omp);
6068 tree
6069 finish_omp_structured_block (tree block)
6071 return do_poplevel (block);
6074 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6075 statement. LOC is the location of the OACC_DATA. */
6077 tree
6078 finish_oacc_data (tree clauses, tree block)
6080 tree stmt;
6082 block = finish_omp_structured_block (block);
6084 stmt = make_node (OACC_DATA);
6085 TREE_TYPE (stmt) = void_type_node;
6086 OACC_DATA_CLAUSES (stmt) = clauses;
6087 OACC_DATA_BODY (stmt) = block;
6089 return add_stmt (stmt);
6092 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6093 statement. LOC is the location of the OACC_KERNELS. */
6095 tree
6096 finish_oacc_kernels (tree clauses, tree block)
6098 tree stmt;
6100 block = finish_omp_structured_block (block);
6102 stmt = make_node (OACC_KERNELS);
6103 TREE_TYPE (stmt) = void_type_node;
6104 OACC_KERNELS_CLAUSES (stmt) = clauses;
6105 OACC_KERNELS_BODY (stmt) = block;
6107 return add_stmt (stmt);
6110 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6111 statement. LOC is the location of the OACC_PARALLEL. */
6113 tree
6114 finish_oacc_parallel (tree clauses, tree block)
6116 tree stmt;
6118 block = finish_omp_structured_block (block);
6120 stmt = make_node (OACC_PARALLEL);
6121 TREE_TYPE (stmt) = void_type_node;
6122 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6123 OACC_PARALLEL_BODY (stmt) = block;
6125 return add_stmt (stmt);
6128 /* Similarly, except force the retention of the BLOCK. */
6130 tree
6131 begin_omp_parallel (void)
6133 keep_next_level (true);
6134 return begin_omp_structured_block ();
6137 tree
6138 finish_omp_parallel (tree clauses, tree body)
6140 tree stmt;
6142 body = finish_omp_structured_block (body);
6144 stmt = make_node (OMP_PARALLEL);
6145 TREE_TYPE (stmt) = void_type_node;
6146 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6147 OMP_PARALLEL_BODY (stmt) = body;
6149 return add_stmt (stmt);
6152 tree
6153 begin_omp_task (void)
6155 keep_next_level (true);
6156 return begin_omp_structured_block ();
6159 tree
6160 finish_omp_task (tree clauses, tree body)
6162 tree stmt;
6164 body = finish_omp_structured_block (body);
6166 stmt = make_node (OMP_TASK);
6167 TREE_TYPE (stmt) = void_type_node;
6168 OMP_TASK_CLAUSES (stmt) = clauses;
6169 OMP_TASK_BODY (stmt) = body;
6171 return add_stmt (stmt);
6174 /* Helper function for finish_omp_for. Convert Ith random access iterator
6175 into integral iterator. Return FALSE if successful. */
6177 static bool
6178 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6179 tree condv, tree incrv, tree *body,
6180 tree *pre_body, tree clauses, tree *lastp)
6182 tree diff, iter_init, iter_incr = NULL, last;
6183 tree incr_var = NULL, orig_pre_body, orig_body, c;
6184 tree decl = TREE_VEC_ELT (declv, i);
6185 tree init = TREE_VEC_ELT (initv, i);
6186 tree cond = TREE_VEC_ELT (condv, i);
6187 tree incr = TREE_VEC_ELT (incrv, i);
6188 tree iter = decl;
6189 location_t elocus = locus;
6191 if (init && EXPR_HAS_LOCATION (init))
6192 elocus = EXPR_LOCATION (init);
6194 switch (TREE_CODE (cond))
6196 case GT_EXPR:
6197 case GE_EXPR:
6198 case LT_EXPR:
6199 case LE_EXPR:
6200 case NE_EXPR:
6201 if (TREE_OPERAND (cond, 1) == iter)
6202 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6203 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6204 if (TREE_OPERAND (cond, 0) != iter)
6205 cond = error_mark_node;
6206 else
6208 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6209 TREE_CODE (cond),
6210 iter, ERROR_MARK,
6211 TREE_OPERAND (cond, 1), ERROR_MARK,
6212 NULL, tf_warning_or_error);
6213 if (error_operand_p (tem))
6214 return true;
6216 break;
6217 default:
6218 cond = error_mark_node;
6219 break;
6221 if (cond == error_mark_node)
6223 error_at (elocus, "invalid controlling predicate");
6224 return true;
6226 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6227 ERROR_MARK, iter, ERROR_MARK, NULL,
6228 tf_warning_or_error);
6229 if (error_operand_p (diff))
6230 return true;
6231 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6233 error_at (elocus, "difference between %qE and %qD does not have integer type",
6234 TREE_OPERAND (cond, 1), iter);
6235 return true;
6238 switch (TREE_CODE (incr))
6240 case PREINCREMENT_EXPR:
6241 case PREDECREMENT_EXPR:
6242 case POSTINCREMENT_EXPR:
6243 case POSTDECREMENT_EXPR:
6244 if (TREE_OPERAND (incr, 0) != iter)
6246 incr = error_mark_node;
6247 break;
6249 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6250 TREE_CODE (incr), iter,
6251 tf_warning_or_error);
6252 if (error_operand_p (iter_incr))
6253 return true;
6254 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6255 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6256 incr = integer_one_node;
6257 else
6258 incr = integer_minus_one_node;
6259 break;
6260 case MODIFY_EXPR:
6261 if (TREE_OPERAND (incr, 0) != iter)
6262 incr = error_mark_node;
6263 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6264 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6266 tree rhs = TREE_OPERAND (incr, 1);
6267 if (TREE_OPERAND (rhs, 0) == iter)
6269 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6270 != INTEGER_TYPE)
6271 incr = error_mark_node;
6272 else
6274 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6275 iter, TREE_CODE (rhs),
6276 TREE_OPERAND (rhs, 1),
6277 tf_warning_or_error);
6278 if (error_operand_p (iter_incr))
6279 return true;
6280 incr = TREE_OPERAND (rhs, 1);
6281 incr = cp_convert (TREE_TYPE (diff), incr,
6282 tf_warning_or_error);
6283 if (TREE_CODE (rhs) == MINUS_EXPR)
6285 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6286 incr = fold_if_not_in_template (incr);
6288 if (TREE_CODE (incr) != INTEGER_CST
6289 && (TREE_CODE (incr) != NOP_EXPR
6290 || (TREE_CODE (TREE_OPERAND (incr, 0))
6291 != INTEGER_CST)))
6292 iter_incr = NULL;
6295 else if (TREE_OPERAND (rhs, 1) == iter)
6297 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6298 || TREE_CODE (rhs) != PLUS_EXPR)
6299 incr = error_mark_node;
6300 else
6302 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6303 PLUS_EXPR,
6304 TREE_OPERAND (rhs, 0),
6305 ERROR_MARK, iter,
6306 ERROR_MARK, NULL,
6307 tf_warning_or_error);
6308 if (error_operand_p (iter_incr))
6309 return true;
6310 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6311 iter, NOP_EXPR,
6312 iter_incr,
6313 tf_warning_or_error);
6314 if (error_operand_p (iter_incr))
6315 return true;
6316 incr = TREE_OPERAND (rhs, 0);
6317 iter_incr = NULL;
6320 else
6321 incr = error_mark_node;
6323 else
6324 incr = error_mark_node;
6325 break;
6326 default:
6327 incr = error_mark_node;
6328 break;
6331 if (incr == error_mark_node)
6333 error_at (elocus, "invalid increment expression");
6334 return true;
6337 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6338 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6339 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6340 && OMP_CLAUSE_DECL (c) == iter)
6341 break;
6343 decl = create_temporary_var (TREE_TYPE (diff));
6344 pushdecl (decl);
6345 add_decl_expr (decl);
6346 last = create_temporary_var (TREE_TYPE (diff));
6347 pushdecl (last);
6348 add_decl_expr (last);
6349 if (c && iter_incr == NULL)
6351 incr_var = create_temporary_var (TREE_TYPE (diff));
6352 pushdecl (incr_var);
6353 add_decl_expr (incr_var);
6355 gcc_assert (stmts_are_full_exprs_p ());
6357 orig_pre_body = *pre_body;
6358 *pre_body = push_stmt_list ();
6359 if (orig_pre_body)
6360 add_stmt (orig_pre_body);
6361 if (init != NULL)
6362 finish_expr_stmt (build_x_modify_expr (elocus,
6363 iter, NOP_EXPR, init,
6364 tf_warning_or_error));
6365 init = build_int_cst (TREE_TYPE (diff), 0);
6366 if (c && iter_incr == NULL)
6368 finish_expr_stmt (build_x_modify_expr (elocus,
6369 incr_var, NOP_EXPR,
6370 incr, tf_warning_or_error));
6371 incr = incr_var;
6372 iter_incr = build_x_modify_expr (elocus,
6373 iter, PLUS_EXPR, incr,
6374 tf_warning_or_error);
6376 finish_expr_stmt (build_x_modify_expr (elocus,
6377 last, NOP_EXPR, init,
6378 tf_warning_or_error));
6379 *pre_body = pop_stmt_list (*pre_body);
6381 cond = cp_build_binary_op (elocus,
6382 TREE_CODE (cond), decl, diff,
6383 tf_warning_or_error);
6384 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6385 elocus, incr, NULL_TREE);
6387 orig_body = *body;
6388 *body = push_stmt_list ();
6389 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6390 iter_init = build_x_modify_expr (elocus,
6391 iter, PLUS_EXPR, iter_init,
6392 tf_warning_or_error);
6393 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6394 finish_expr_stmt (iter_init);
6395 finish_expr_stmt (build_x_modify_expr (elocus,
6396 last, NOP_EXPR, decl,
6397 tf_warning_or_error));
6398 add_stmt (orig_body);
6399 *body = pop_stmt_list (*body);
6401 if (c)
6403 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6404 finish_expr_stmt (iter_incr);
6405 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6406 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6409 TREE_VEC_ELT (declv, i) = decl;
6410 TREE_VEC_ELT (initv, i) = init;
6411 TREE_VEC_ELT (condv, i) = cond;
6412 TREE_VEC_ELT (incrv, i) = incr;
6413 *lastp = last;
6415 return false;
6418 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6419 are directly for their associated operands in the statement. DECL
6420 and INIT are a combo; if DECL is NULL then INIT ought to be a
6421 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6422 optional statements that need to go before the loop into its
6423 sk_omp scope. */
6425 tree
6426 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6427 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6429 tree omp_for = NULL, orig_incr = NULL;
6430 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6431 tree last = NULL_TREE;
6432 location_t elocus;
6433 int i;
6435 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6436 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6437 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6438 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6440 decl = TREE_VEC_ELT (declv, i);
6441 init = TREE_VEC_ELT (initv, i);
6442 cond = TREE_VEC_ELT (condv, i);
6443 incr = TREE_VEC_ELT (incrv, i);
6444 elocus = locus;
6446 if (decl == NULL)
6448 if (init != NULL)
6449 switch (TREE_CODE (init))
6451 case MODIFY_EXPR:
6452 decl = TREE_OPERAND (init, 0);
6453 init = TREE_OPERAND (init, 1);
6454 break;
6455 case MODOP_EXPR:
6456 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6458 decl = TREE_OPERAND (init, 0);
6459 init = TREE_OPERAND (init, 2);
6461 break;
6462 default:
6463 break;
6466 if (decl == NULL)
6468 error_at (locus,
6469 "expected iteration declaration or initialization");
6470 return NULL;
6474 if (init && EXPR_HAS_LOCATION (init))
6475 elocus = EXPR_LOCATION (init);
6477 if (cond == NULL)
6479 error_at (elocus, "missing controlling predicate");
6480 return NULL;
6483 if (incr == NULL)
6485 error_at (elocus, "missing increment expression");
6486 return NULL;
6489 TREE_VEC_ELT (declv, i) = decl;
6490 TREE_VEC_ELT (initv, i) = init;
6493 if (dependent_omp_for_p (declv, initv, condv, incrv))
6495 tree stmt;
6497 stmt = make_node (code);
6499 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6501 /* This is really just a place-holder. We'll be decomposing this
6502 again and going through the cp_build_modify_expr path below when
6503 we instantiate the thing. */
6504 TREE_VEC_ELT (initv, i)
6505 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6506 TREE_VEC_ELT (initv, i));
6509 TREE_TYPE (stmt) = void_type_node;
6510 OMP_FOR_INIT (stmt) = initv;
6511 OMP_FOR_COND (stmt) = condv;
6512 OMP_FOR_INCR (stmt) = incrv;
6513 OMP_FOR_BODY (stmt) = body;
6514 OMP_FOR_PRE_BODY (stmt) = pre_body;
6515 OMP_FOR_CLAUSES (stmt) = clauses;
6517 SET_EXPR_LOCATION (stmt, locus);
6518 return add_stmt (stmt);
6521 if (processing_template_decl)
6522 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6524 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6526 decl = TREE_VEC_ELT (declv, i);
6527 init = TREE_VEC_ELT (initv, i);
6528 cond = TREE_VEC_ELT (condv, i);
6529 incr = TREE_VEC_ELT (incrv, i);
6530 if (orig_incr)
6531 TREE_VEC_ELT (orig_incr, i) = incr;
6532 elocus = locus;
6534 if (init && EXPR_HAS_LOCATION (init))
6535 elocus = EXPR_LOCATION (init);
6537 if (!DECL_P (decl))
6539 error_at (elocus, "expected iteration declaration or initialization");
6540 return NULL;
6543 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6545 if (orig_incr)
6546 TREE_VEC_ELT (orig_incr, i) = incr;
6547 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6548 TREE_CODE (TREE_OPERAND (incr, 1)),
6549 TREE_OPERAND (incr, 2),
6550 tf_warning_or_error);
6553 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6555 if (code == OMP_SIMD)
6557 error_at (elocus, "%<#pragma omp simd%> used with class "
6558 "iteration variable %qE", decl);
6559 return NULL;
6561 if (code == CILK_FOR && i == 0)
6562 orig_decl = decl;
6563 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6564 incrv, &body, &pre_body,
6565 clauses, &last))
6566 return NULL;
6567 continue;
6570 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6571 && !TYPE_PTR_P (TREE_TYPE (decl)))
6573 error_at (elocus, "invalid type for iteration variable %qE", decl);
6574 return NULL;
6577 if (!processing_template_decl)
6579 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6580 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6582 else
6583 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6584 if (cond
6585 && TREE_SIDE_EFFECTS (cond)
6586 && COMPARISON_CLASS_P (cond)
6587 && !processing_template_decl)
6589 tree t = TREE_OPERAND (cond, 0);
6590 if (TREE_SIDE_EFFECTS (t)
6591 && t != decl
6592 && (TREE_CODE (t) != NOP_EXPR
6593 || TREE_OPERAND (t, 0) != decl))
6594 TREE_OPERAND (cond, 0)
6595 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6597 t = TREE_OPERAND (cond, 1);
6598 if (TREE_SIDE_EFFECTS (t)
6599 && t != decl
6600 && (TREE_CODE (t) != NOP_EXPR
6601 || TREE_OPERAND (t, 0) != decl))
6602 TREE_OPERAND (cond, 1)
6603 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6605 if (decl == error_mark_node || init == error_mark_node)
6606 return NULL;
6608 TREE_VEC_ELT (declv, i) = decl;
6609 TREE_VEC_ELT (initv, i) = init;
6610 TREE_VEC_ELT (condv, i) = cond;
6611 TREE_VEC_ELT (incrv, i) = incr;
6612 i++;
6615 if (IS_EMPTY_STMT (pre_body))
6616 pre_body = NULL;
6618 if (code == CILK_FOR && !processing_template_decl)
6619 block = push_stmt_list ();
6621 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6622 body, pre_body);
6624 if (omp_for == NULL)
6626 if (block)
6627 pop_stmt_list (block);
6628 return NULL;
6631 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6633 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6634 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6636 if (TREE_CODE (incr) != MODIFY_EXPR)
6637 continue;
6639 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6640 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6641 && !processing_template_decl)
6643 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6644 if (TREE_SIDE_EFFECTS (t)
6645 && t != decl
6646 && (TREE_CODE (t) != NOP_EXPR
6647 || TREE_OPERAND (t, 0) != decl))
6648 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6649 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6651 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6652 if (TREE_SIDE_EFFECTS (t)
6653 && t != decl
6654 && (TREE_CODE (t) != NOP_EXPR
6655 || TREE_OPERAND (t, 0) != decl))
6656 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6657 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6660 if (orig_incr)
6661 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6663 OMP_FOR_CLAUSES (omp_for) = clauses;
6665 if (block)
6667 tree omp_par = make_node (OMP_PARALLEL);
6668 TREE_TYPE (omp_par) = void_type_node;
6669 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6670 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6671 TREE_SIDE_EFFECTS (bind) = 1;
6672 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6673 OMP_PARALLEL_BODY (omp_par) = bind;
6674 if (OMP_FOR_PRE_BODY (omp_for))
6676 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6677 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6679 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6680 decl = TREE_OPERAND (init, 0);
6681 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6682 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6683 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6684 clauses = OMP_FOR_CLAUSES (omp_for);
6685 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6686 for (pc = &clauses; *pc; )
6687 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6689 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6690 OMP_FOR_CLAUSES (omp_for) = *pc;
6691 *pc = OMP_CLAUSE_CHAIN (*pc);
6692 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6694 else
6696 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6697 pc = &OMP_CLAUSE_CHAIN (*pc);
6699 if (TREE_CODE (t) != INTEGER_CST)
6701 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6702 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6703 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6704 OMP_CLAUSE_CHAIN (c) = clauses;
6705 clauses = c;
6707 if (TREE_CODE (incr) == MODIFY_EXPR)
6709 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6710 if (TREE_CODE (t) != INTEGER_CST)
6712 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6713 = get_temp_regvar (TREE_TYPE (t), t);
6714 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6715 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6716 OMP_CLAUSE_CHAIN (c) = clauses;
6717 clauses = c;
6720 t = TREE_OPERAND (init, 1);
6721 if (TREE_CODE (t) != INTEGER_CST)
6723 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6724 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6725 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6726 OMP_CLAUSE_CHAIN (c) = clauses;
6727 clauses = c;
6729 if (orig_decl && orig_decl != decl)
6731 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6732 OMP_CLAUSE_DECL (c) = orig_decl;
6733 OMP_CLAUSE_CHAIN (c) = clauses;
6734 clauses = c;
6736 if (last)
6738 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6739 OMP_CLAUSE_DECL (c) = last;
6740 OMP_CLAUSE_CHAIN (c) = clauses;
6741 clauses = c;
6743 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6744 OMP_CLAUSE_DECL (c) = decl;
6745 OMP_CLAUSE_CHAIN (c) = clauses;
6746 clauses = c;
6747 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6748 OMP_CLAUSE_OPERAND (c, 0)
6749 = cilk_for_number_of_iterations (omp_for);
6750 OMP_CLAUSE_CHAIN (c) = clauses;
6751 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6752 add_stmt (omp_par);
6753 return omp_par;
6755 else if (code == CILK_FOR && processing_template_decl)
6757 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6758 if (orig_decl && orig_decl != decl)
6760 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6761 OMP_CLAUSE_DECL (c) = orig_decl;
6762 OMP_CLAUSE_CHAIN (c) = clauses;
6763 clauses = c;
6765 if (last)
6767 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6768 OMP_CLAUSE_DECL (c) = last;
6769 OMP_CLAUSE_CHAIN (c) = clauses;
6770 clauses = c;
6772 OMP_FOR_CLAUSES (omp_for) = clauses;
6774 return omp_for;
6777 void
6778 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6779 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6781 tree orig_lhs;
6782 tree orig_rhs;
6783 tree orig_v;
6784 tree orig_lhs1;
6785 tree orig_rhs1;
6786 bool dependent_p;
6787 tree stmt;
6789 orig_lhs = lhs;
6790 orig_rhs = rhs;
6791 orig_v = v;
6792 orig_lhs1 = lhs1;
6793 orig_rhs1 = rhs1;
6794 dependent_p = false;
6795 stmt = NULL_TREE;
6797 /* Even in a template, we can detect invalid uses of the atomic
6798 pragma if neither LHS nor RHS is type-dependent. */
6799 if (processing_template_decl)
6801 dependent_p = (type_dependent_expression_p (lhs)
6802 || (rhs && type_dependent_expression_p (rhs))
6803 || (v && type_dependent_expression_p (v))
6804 || (lhs1 && type_dependent_expression_p (lhs1))
6805 || (rhs1 && type_dependent_expression_p (rhs1)));
6806 if (!dependent_p)
6808 lhs = build_non_dependent_expr (lhs);
6809 if (rhs)
6810 rhs = build_non_dependent_expr (rhs);
6811 if (v)
6812 v = build_non_dependent_expr (v);
6813 if (lhs1)
6814 lhs1 = build_non_dependent_expr (lhs1);
6815 if (rhs1)
6816 rhs1 = build_non_dependent_expr (rhs1);
6819 if (!dependent_p)
6821 bool swapped = false;
6822 if (rhs1 && cp_tree_equal (lhs, rhs))
6824 tree tem = rhs;
6825 rhs = rhs1;
6826 rhs1 = tem;
6827 swapped = !commutative_tree_code (opcode);
6829 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6831 if (code == OMP_ATOMIC)
6832 error ("%<#pragma omp atomic update%> uses two different "
6833 "expressions for memory");
6834 else
6835 error ("%<#pragma omp atomic capture%> uses two different "
6836 "expressions for memory");
6837 return;
6839 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6841 if (code == OMP_ATOMIC)
6842 error ("%<#pragma omp atomic update%> uses two different "
6843 "expressions for memory");
6844 else
6845 error ("%<#pragma omp atomic capture%> uses two different "
6846 "expressions for memory");
6847 return;
6849 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6850 v, lhs1, rhs1, swapped, seq_cst);
6851 if (stmt == error_mark_node)
6852 return;
6854 if (processing_template_decl)
6856 if (code == OMP_ATOMIC_READ)
6858 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6859 OMP_ATOMIC_READ, orig_lhs);
6860 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6861 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6863 else
6865 if (opcode == NOP_EXPR)
6866 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6867 else
6868 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6869 if (orig_rhs1)
6870 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6871 COMPOUND_EXPR, orig_rhs1, stmt);
6872 if (code != OMP_ATOMIC)
6874 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6875 code, orig_lhs1, stmt);
6876 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6877 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6880 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6881 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6883 finish_expr_stmt (stmt);
6886 void
6887 finish_omp_barrier (void)
6889 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6890 vec<tree, va_gc> *vec = make_tree_vector ();
6891 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6892 release_tree_vector (vec);
6893 finish_expr_stmt (stmt);
6896 void
6897 finish_omp_flush (void)
6899 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6900 vec<tree, va_gc> *vec = make_tree_vector ();
6901 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6902 release_tree_vector (vec);
6903 finish_expr_stmt (stmt);
6906 void
6907 finish_omp_taskwait (void)
6909 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6910 vec<tree, va_gc> *vec = make_tree_vector ();
6911 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6912 release_tree_vector (vec);
6913 finish_expr_stmt (stmt);
6916 void
6917 finish_omp_taskyield (void)
6919 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6920 vec<tree, va_gc> *vec = make_tree_vector ();
6921 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6922 release_tree_vector (vec);
6923 finish_expr_stmt (stmt);
6926 void
6927 finish_omp_cancel (tree clauses)
6929 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6930 int mask = 0;
6931 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6932 mask = 1;
6933 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6934 mask = 2;
6935 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6936 mask = 4;
6937 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6938 mask = 8;
6939 else
6941 error ("%<#pragma omp cancel must specify one of "
6942 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6943 return;
6945 vec<tree, va_gc> *vec = make_tree_vector ();
6946 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6947 if (ifc != NULL_TREE)
6949 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6950 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6951 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6952 build_zero_cst (type));
6954 else
6955 ifc = boolean_true_node;
6956 vec->quick_push (build_int_cst (integer_type_node, mask));
6957 vec->quick_push (ifc);
6958 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6959 release_tree_vector (vec);
6960 finish_expr_stmt (stmt);
6963 void
6964 finish_omp_cancellation_point (tree clauses)
6966 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6967 int mask = 0;
6968 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6969 mask = 1;
6970 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6971 mask = 2;
6972 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6973 mask = 4;
6974 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6975 mask = 8;
6976 else
6978 error ("%<#pragma omp cancellation point must specify one of "
6979 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6980 return;
6982 vec<tree, va_gc> *vec
6983 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6984 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6985 release_tree_vector (vec);
6986 finish_expr_stmt (stmt);
6989 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6990 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6991 should create an extra compound stmt. */
6993 tree
6994 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6996 tree r;
6998 if (pcompound)
6999 *pcompound = begin_compound_stmt (0);
7001 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7003 /* Only add the statement to the function if support enabled. */
7004 if (flag_tm)
7005 add_stmt (r);
7006 else
7007 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7008 ? G_("%<__transaction_relaxed%> without "
7009 "transactional memory support enabled")
7010 : G_("%<__transaction_atomic%> without "
7011 "transactional memory support enabled")));
7013 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7014 TREE_SIDE_EFFECTS (r) = 1;
7015 return r;
7018 /* End a __transaction_atomic or __transaction_relaxed statement.
7019 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7020 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7021 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7023 void
7024 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7026 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7027 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7028 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7029 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7031 /* noexcept specifications are not allowed for function transactions. */
7032 gcc_assert (!(noex && compound_stmt));
7033 if (noex)
7035 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7036 noex);
7037 /* This may not be true when the STATEMENT_LIST is empty. */
7038 if (EXPR_P (body))
7039 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7040 TREE_SIDE_EFFECTS (body) = 1;
7041 TRANSACTION_EXPR_BODY (stmt) = body;
7044 if (compound_stmt)
7045 finish_compound_stmt (compound_stmt);
7048 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7049 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7050 condition. */
7052 tree
7053 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7055 tree ret;
7056 if (noex)
7058 expr = build_must_not_throw_expr (expr, noex);
7059 if (EXPR_P (expr))
7060 SET_EXPR_LOCATION (expr, loc);
7061 TREE_SIDE_EFFECTS (expr) = 1;
7063 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7064 if (flags & TM_STMT_ATTR_RELAXED)
7065 TRANSACTION_EXPR_RELAXED (ret) = 1;
7066 TREE_SIDE_EFFECTS (ret) = 1;
7067 SET_EXPR_LOCATION (ret, loc);
7068 return ret;
7071 void
7072 init_cp_semantics (void)
7076 /* Build a STATIC_ASSERT for a static assertion with the condition
7077 CONDITION and the message text MESSAGE. LOCATION is the location
7078 of the static assertion in the source code. When MEMBER_P, this
7079 static assertion is a member of a class. */
7080 void
7081 finish_static_assert (tree condition, tree message, location_t location,
7082 bool member_p)
7084 if (message == NULL_TREE
7085 || message == error_mark_node
7086 || condition == NULL_TREE
7087 || condition == error_mark_node)
7088 return;
7090 if (check_for_bare_parameter_packs (condition))
7091 condition = error_mark_node;
7093 if (type_dependent_expression_p (condition)
7094 || value_dependent_expression_p (condition))
7096 /* We're in a template; build a STATIC_ASSERT and put it in
7097 the right place. */
7098 tree assertion;
7100 assertion = make_node (STATIC_ASSERT);
7101 STATIC_ASSERT_CONDITION (assertion) = condition;
7102 STATIC_ASSERT_MESSAGE (assertion) = message;
7103 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7105 if (member_p)
7106 maybe_add_class_template_decl_list (current_class_type,
7107 assertion,
7108 /*friend_p=*/0);
7109 else
7110 add_stmt (assertion);
7112 return;
7115 /* Fold the expression and convert it to a boolean value. */
7116 condition = fold_non_dependent_expr (condition);
7117 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7118 condition = maybe_constant_value (condition);
7120 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7121 /* Do nothing; the condition is satisfied. */
7123 else
7125 location_t saved_loc = input_location;
7127 input_location = location;
7128 if (TREE_CODE (condition) == INTEGER_CST
7129 && integer_zerop (condition))
7130 /* Report the error. */
7131 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7132 else if (condition && condition != error_mark_node)
7134 error ("non-constant condition for static assertion");
7135 if (require_potential_rvalue_constant_expression (condition))
7136 cxx_constant_value (condition);
7138 input_location = saved_loc;
7142 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7143 suitable for use as a type-specifier.
7145 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7146 id-expression or a class member access, FALSE when it was parsed as
7147 a full expression. */
7149 tree
7150 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7151 tsubst_flags_t complain)
7153 tree type = NULL_TREE;
7155 if (!expr || error_operand_p (expr))
7156 return error_mark_node;
7158 if (TYPE_P (expr)
7159 || TREE_CODE (expr) == TYPE_DECL
7160 || (TREE_CODE (expr) == BIT_NOT_EXPR
7161 && TYPE_P (TREE_OPERAND (expr, 0))))
7163 if (complain & tf_error)
7164 error ("argument to decltype must be an expression");
7165 return error_mark_node;
7168 /* Depending on the resolution of DR 1172, we may later need to distinguish
7169 instantiation-dependent but not type-dependent expressions so that, say,
7170 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7171 if (instantiation_dependent_expression_p (expr))
7173 type = cxx_make_type (DECLTYPE_TYPE);
7174 DECLTYPE_TYPE_EXPR (type) = expr;
7175 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7176 = id_expression_or_member_access_p;
7177 SET_TYPE_STRUCTURAL_EQUALITY (type);
7179 return type;
7182 /* The type denoted by decltype(e) is defined as follows: */
7184 expr = resolve_nondeduced_context (expr);
7186 if (invalid_nonstatic_memfn_p (expr, complain))
7187 return error_mark_node;
7189 if (type_unknown_p (expr))
7191 if (complain & tf_error)
7192 error ("decltype cannot resolve address of overloaded function");
7193 return error_mark_node;
7196 /* To get the size of a static data member declared as an array of
7197 unknown bound, we need to instantiate it. */
7198 if (VAR_P (expr)
7199 && VAR_HAD_UNKNOWN_BOUND (expr)
7200 && DECL_TEMPLATE_INSTANTIATION (expr))
7201 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7203 if (id_expression_or_member_access_p)
7205 /* If e is an id-expression or a class member access (5.2.5
7206 [expr.ref]), decltype(e) is defined as the type of the entity
7207 named by e. If there is no such entity, or e names a set of
7208 overloaded functions, the program is ill-formed. */
7209 if (identifier_p (expr))
7210 expr = lookup_name (expr);
7212 if (INDIRECT_REF_P (expr))
7213 /* This can happen when the expression is, e.g., "a.b". Just
7214 look at the underlying operand. */
7215 expr = TREE_OPERAND (expr, 0);
7217 if (TREE_CODE (expr) == OFFSET_REF
7218 || TREE_CODE (expr) == MEMBER_REF
7219 || TREE_CODE (expr) == SCOPE_REF)
7220 /* We're only interested in the field itself. If it is a
7221 BASELINK, we will need to see through it in the next
7222 step. */
7223 expr = TREE_OPERAND (expr, 1);
7225 if (BASELINK_P (expr))
7226 /* See through BASELINK nodes to the underlying function. */
7227 expr = BASELINK_FUNCTIONS (expr);
7229 switch (TREE_CODE (expr))
7231 case FIELD_DECL:
7232 if (DECL_BIT_FIELD_TYPE (expr))
7234 type = DECL_BIT_FIELD_TYPE (expr);
7235 break;
7237 /* Fall through for fields that aren't bitfields. */
7239 case FUNCTION_DECL:
7240 case VAR_DECL:
7241 case CONST_DECL:
7242 case PARM_DECL:
7243 case RESULT_DECL:
7244 case TEMPLATE_PARM_INDEX:
7245 expr = mark_type_use (expr);
7246 type = TREE_TYPE (expr);
7247 break;
7249 case ERROR_MARK:
7250 type = error_mark_node;
7251 break;
7253 case COMPONENT_REF:
7254 case COMPOUND_EXPR:
7255 mark_type_use (expr);
7256 type = is_bitfield_expr_with_lowered_type (expr);
7257 if (!type)
7258 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7259 break;
7261 case BIT_FIELD_REF:
7262 gcc_unreachable ();
7264 case INTEGER_CST:
7265 case PTRMEM_CST:
7266 /* We can get here when the id-expression refers to an
7267 enumerator or non-type template parameter. */
7268 type = TREE_TYPE (expr);
7269 break;
7271 default:
7272 /* Handle instantiated template non-type arguments. */
7273 type = TREE_TYPE (expr);
7274 break;
7277 else
7279 /* Within a lambda-expression:
7281 Every occurrence of decltype((x)) where x is a possibly
7282 parenthesized id-expression that names an entity of
7283 automatic storage duration is treated as if x were
7284 transformed into an access to a corresponding data member
7285 of the closure type that would have been declared if x
7286 were a use of the denoted entity. */
7287 if (outer_automatic_var_p (expr)
7288 && current_function_decl
7289 && LAMBDA_FUNCTION_P (current_function_decl))
7290 type = capture_decltype (expr);
7291 else if (error_operand_p (expr))
7292 type = error_mark_node;
7293 else if (expr == current_class_ptr)
7294 /* If the expression is just "this", we want the
7295 cv-unqualified pointer for the "this" type. */
7296 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7297 else
7299 /* Otherwise, where T is the type of e, if e is an lvalue,
7300 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7301 cp_lvalue_kind clk = lvalue_kind (expr);
7302 type = unlowered_expr_type (expr);
7303 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7305 /* For vector types, pick a non-opaque variant. */
7306 if (TREE_CODE (type) == VECTOR_TYPE)
7307 type = strip_typedefs (type);
7309 if (clk != clk_none && !(clk & clk_class))
7310 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7314 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
7315 && (flag_iso || warn_vla > 0))
7317 if (complain & tf_warning_or_error)
7318 pedwarn (input_location, OPT_Wvla,
7319 "taking decltype of array of runtime bound");
7320 else
7321 return error_mark_node;
7324 return type;
7327 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7328 __has_nothrow_copy, depending on assign_p. */
7330 static bool
7331 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7333 tree fns;
7335 if (assign_p)
7337 int ix;
7338 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7339 if (ix < 0)
7340 return false;
7341 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7343 else if (TYPE_HAS_COPY_CTOR (type))
7345 /* If construction of the copy constructor was postponed, create
7346 it now. */
7347 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7348 lazily_declare_fn (sfk_copy_constructor, type);
7349 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7350 lazily_declare_fn (sfk_move_constructor, type);
7351 fns = CLASSTYPE_CONSTRUCTORS (type);
7353 else
7354 return false;
7356 for (; fns; fns = OVL_NEXT (fns))
7358 tree fn = OVL_CURRENT (fns);
7360 if (assign_p)
7362 if (copy_fn_p (fn) == 0)
7363 continue;
7365 else if (copy_fn_p (fn) <= 0)
7366 continue;
7368 maybe_instantiate_noexcept (fn);
7369 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7370 return false;
7373 return true;
7376 /* Actually evaluates the trait. */
7378 static bool
7379 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7381 enum tree_code type_code1;
7382 tree t;
7384 type_code1 = TREE_CODE (type1);
7386 switch (kind)
7388 case CPTK_HAS_NOTHROW_ASSIGN:
7389 type1 = strip_array_types (type1);
7390 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7391 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7392 || (CLASS_TYPE_P (type1)
7393 && classtype_has_nothrow_assign_or_copy_p (type1,
7394 true))));
7396 case CPTK_HAS_TRIVIAL_ASSIGN:
7397 /* ??? The standard seems to be missing the "or array of such a class
7398 type" wording for this trait. */
7399 type1 = strip_array_types (type1);
7400 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7401 && (trivial_type_p (type1)
7402 || (CLASS_TYPE_P (type1)
7403 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7405 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7406 type1 = strip_array_types (type1);
7407 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7408 || (CLASS_TYPE_P (type1)
7409 && (t = locate_ctor (type1))
7410 && (maybe_instantiate_noexcept (t),
7411 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7413 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7414 type1 = strip_array_types (type1);
7415 return (trivial_type_p (type1)
7416 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7418 case CPTK_HAS_NOTHROW_COPY:
7419 type1 = strip_array_types (type1);
7420 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7421 || (CLASS_TYPE_P (type1)
7422 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7424 case CPTK_HAS_TRIVIAL_COPY:
7425 /* ??? The standard seems to be missing the "or array of such a class
7426 type" wording for this trait. */
7427 type1 = strip_array_types (type1);
7428 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7429 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7431 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7432 type1 = strip_array_types (type1);
7433 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7434 || (CLASS_TYPE_P (type1)
7435 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7437 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7438 return type_has_virtual_destructor (type1);
7440 case CPTK_IS_ABSTRACT:
7441 return (ABSTRACT_CLASS_TYPE_P (type1));
7443 case CPTK_IS_BASE_OF:
7444 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7445 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7446 || DERIVED_FROM_P (type1, type2)));
7448 case CPTK_IS_CLASS:
7449 return (NON_UNION_CLASS_TYPE_P (type1));
7451 case CPTK_IS_EMPTY:
7452 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7454 case CPTK_IS_ENUM:
7455 return (type_code1 == ENUMERAL_TYPE);
7457 case CPTK_IS_FINAL:
7458 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7460 case CPTK_IS_LITERAL_TYPE:
7461 return (literal_type_p (type1));
7463 case CPTK_IS_POD:
7464 return (pod_type_p (type1));
7466 case CPTK_IS_POLYMORPHIC:
7467 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7469 case CPTK_IS_STD_LAYOUT:
7470 return (std_layout_type_p (type1));
7472 case CPTK_IS_TRIVIAL:
7473 return (trivial_type_p (type1));
7475 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7476 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7478 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7479 return is_trivially_xible (INIT_EXPR, type1, type2);
7481 case CPTK_IS_TRIVIALLY_COPYABLE:
7482 return (trivially_copyable_p (type1));
7484 case CPTK_IS_UNION:
7485 return (type_code1 == UNION_TYPE);
7487 default:
7488 gcc_unreachable ();
7489 return false;
7493 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7494 void, or a complete type, returns true, otherwise false. */
7496 static bool
7497 check_trait_type (tree type)
7499 if (type == NULL_TREE)
7500 return true;
7502 if (TREE_CODE (type) == TREE_LIST)
7503 return (check_trait_type (TREE_VALUE (type))
7504 && check_trait_type (TREE_CHAIN (type)));
7506 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7507 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7508 return true;
7510 if (VOID_TYPE_P (type))
7511 return true;
7513 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7516 /* Process a trait expression. */
7518 tree
7519 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7521 if (type1 == error_mark_node
7522 || type2 == error_mark_node)
7523 return error_mark_node;
7525 if (processing_template_decl)
7527 tree trait_expr = make_node (TRAIT_EXPR);
7528 TREE_TYPE (trait_expr) = boolean_type_node;
7529 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7530 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7531 TRAIT_EXPR_KIND (trait_expr) = kind;
7532 return trait_expr;
7535 switch (kind)
7537 case CPTK_HAS_NOTHROW_ASSIGN:
7538 case CPTK_HAS_TRIVIAL_ASSIGN:
7539 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7540 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7541 case CPTK_HAS_NOTHROW_COPY:
7542 case CPTK_HAS_TRIVIAL_COPY:
7543 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7544 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7545 case CPTK_IS_ABSTRACT:
7546 case CPTK_IS_EMPTY:
7547 case CPTK_IS_FINAL:
7548 case CPTK_IS_LITERAL_TYPE:
7549 case CPTK_IS_POD:
7550 case CPTK_IS_POLYMORPHIC:
7551 case CPTK_IS_STD_LAYOUT:
7552 case CPTK_IS_TRIVIAL:
7553 case CPTK_IS_TRIVIALLY_COPYABLE:
7554 if (!check_trait_type (type1))
7555 return error_mark_node;
7556 break;
7558 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7559 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7560 if (!check_trait_type (type1)
7561 || !check_trait_type (type2))
7562 return error_mark_node;
7563 break;
7565 case CPTK_IS_BASE_OF:
7566 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7567 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7568 && !complete_type_or_else (type2, NULL_TREE))
7569 /* We already issued an error. */
7570 return error_mark_node;
7571 break;
7573 case CPTK_IS_CLASS:
7574 case CPTK_IS_ENUM:
7575 case CPTK_IS_UNION:
7576 break;
7578 default:
7579 gcc_unreachable ();
7582 return (trait_expr_value (kind, type1, type2)
7583 ? boolean_true_node : boolean_false_node);
7586 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7587 which is ignored for C++. */
7589 void
7590 set_float_const_decimal64 (void)
7594 void
7595 clear_float_const_decimal64 (void)
7599 bool
7600 float_const_decimal64_p (void)
7602 return 0;
7606 /* Return true if T designates the implied `this' parameter. */
7608 bool
7609 is_this_parameter (tree t)
7611 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7612 return false;
7613 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7614 return true;
7617 /* Insert the deduced return type for an auto function. */
7619 void
7620 apply_deduced_return_type (tree fco, tree return_type)
7622 tree result;
7624 if (return_type == error_mark_node)
7625 return;
7627 if (LAMBDA_FUNCTION_P (fco))
7629 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7630 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7633 if (DECL_CONV_FN_P (fco))
7634 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7636 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7638 result = DECL_RESULT (fco);
7639 if (result == NULL_TREE)
7640 return;
7641 if (TREE_TYPE (result) == return_type)
7642 return;
7644 /* We already have a DECL_RESULT from start_preparsed_function.
7645 Now we need to redo the work it and allocate_struct_function
7646 did to reflect the new type. */
7647 gcc_assert (current_function_decl == fco);
7648 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7649 TYPE_MAIN_VARIANT (return_type));
7650 DECL_ARTIFICIAL (result) = 1;
7651 DECL_IGNORED_P (result) = 1;
7652 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7653 result);
7655 DECL_RESULT (fco) = result;
7657 if (!processing_template_decl)
7659 if (!VOID_TYPE_P (TREE_TYPE (result)))
7660 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7661 bool aggr = aggregate_value_p (result, fco);
7662 #ifdef PCC_STATIC_STRUCT_RETURN
7663 cfun->returns_pcc_struct = aggr;
7664 #endif
7665 cfun->returns_struct = aggr;
7670 /* DECL is a local variable or parameter from the surrounding scope of a
7671 lambda-expression. Returns the decltype for a use of the capture field
7672 for DECL even if it hasn't been captured yet. */
7674 static tree
7675 capture_decltype (tree decl)
7677 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7678 /* FIXME do lookup instead of list walk? */
7679 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7680 tree type;
7682 if (cap)
7683 type = TREE_TYPE (TREE_PURPOSE (cap));
7684 else
7685 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7687 case CPLD_NONE:
7688 error ("%qD is not captured", decl);
7689 return error_mark_node;
7691 case CPLD_COPY:
7692 type = TREE_TYPE (decl);
7693 if (TREE_CODE (type) == REFERENCE_TYPE
7694 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7695 type = TREE_TYPE (type);
7696 break;
7698 case CPLD_REFERENCE:
7699 type = TREE_TYPE (decl);
7700 if (TREE_CODE (type) != REFERENCE_TYPE)
7701 type = build_reference_type (TREE_TYPE (decl));
7702 break;
7704 default:
7705 gcc_unreachable ();
7708 if (TREE_CODE (type) != REFERENCE_TYPE)
7710 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7711 type = cp_build_qualified_type (type, (cp_type_quals (type)
7712 |TYPE_QUAL_CONST));
7713 type = build_reference_type (type);
7715 return type;
7718 #include "gt-cp-semantics.h"