2015-06-25 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / cp / semantics.c
blob2f3303a96fbe090e43af07496f242ff0df08f0ab
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2015 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "tree.h"
33 #include "stmt.h"
34 #include "varasm.h"
35 #include "stor-layout.h"
36 #include "stringpool.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "tree-inline.h"
41 #include "intl.h"
42 #include "toplev.h"
43 #include "flags.h"
44 #include "timevar.h"
45 #include "diagnostic.h"
46 #include "hard-reg-set.h"
47 #include "function.h"
48 #include "cgraph.h"
49 #include "tree-iterator.h"
50 #include "target.h"
51 #include "gimplify.h"
52 #include "bitmap.h"
53 #include "omp-low.h"
54 #include "builtins.h"
55 #include "convert.h"
56 #include "gomp-constants.h"
58 /* There routines provide a modular interface to perform many parsing
59 operations. They may therefore be used during actual parsing, or
60 during template instantiation, which may be regarded as a
61 degenerate form of parsing. */
63 static tree maybe_convert_cond (tree);
64 static tree finalize_nrv_r (tree *, int *, void *);
65 static tree capture_decltype (tree);
68 /* Deferred Access Checking Overview
69 ---------------------------------
71 Most C++ expressions and declarations require access checking
72 to be performed during parsing. However, in several cases,
73 this has to be treated differently.
75 For member declarations, access checking has to be deferred
76 until more information about the declaration is known. For
77 example:
79 class A {
80 typedef int X;
81 public:
82 X f();
85 A::X A::f();
86 A::X g();
88 When we are parsing the function return type `A::X', we don't
89 really know if this is allowed until we parse the function name.
91 Furthermore, some contexts require that access checking is
92 never performed at all. These include class heads, and template
93 instantiations.
95 Typical use of access checking functions is described here:
97 1. When we enter a context that requires certain access checking
98 mode, the function `push_deferring_access_checks' is called with
99 DEFERRING argument specifying the desired mode. Access checking
100 may be performed immediately (dk_no_deferred), deferred
101 (dk_deferred), or not performed (dk_no_check).
103 2. When a declaration such as a type, or a variable, is encountered,
104 the function `perform_or_defer_access_check' is called. It
105 maintains a vector of all deferred checks.
107 3. The global `current_class_type' or `current_function_decl' is then
108 setup by the parser. `enforce_access' relies on these information
109 to check access.
111 4. Upon exiting the context mentioned in step 1,
112 `perform_deferred_access_checks' is called to check all declaration
113 stored in the vector. `pop_deferring_access_checks' is then
114 called to restore the previous access checking mode.
116 In case of parsing error, we simply call `pop_deferring_access_checks'
117 without `perform_deferred_access_checks'. */
119 typedef struct GTY(()) deferred_access {
120 /* A vector representing name-lookups for which we have deferred
121 checking access controls. We cannot check the accessibility of
122 names used in a decl-specifier-seq until we know what is being
123 declared because code like:
125 class A {
126 class B {};
127 B* f();
130 A::B* A::f() { return 0; }
132 is valid, even though `A::B' is not generally accessible. */
133 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
135 /* The current mode of access checks. */
136 enum deferring_kind deferring_access_checks_kind;
138 } deferred_access;
140 /* Data for deferred access checking. */
141 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
142 static GTY(()) unsigned deferred_access_no_check;
144 /* Save the current deferred access states and start deferred
145 access checking iff DEFER_P is true. */
147 void
148 push_deferring_access_checks (deferring_kind deferring)
150 /* For context like template instantiation, access checking
151 disabling applies to all nested context. */
152 if (deferred_access_no_check || deferring == dk_no_check)
153 deferred_access_no_check++;
154 else
156 deferred_access e = {NULL, deferring};
157 vec_safe_push (deferred_access_stack, e);
161 /* Save the current deferred access states and start deferred access
162 checking, continuing the set of deferred checks in CHECKS. */
164 void
165 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
167 push_deferring_access_checks (dk_deferred);
168 if (!deferred_access_no_check)
169 deferred_access_stack->last().deferred_access_checks = checks;
172 /* Resume deferring access checks again after we stopped doing
173 this previously. */
175 void
176 resume_deferring_access_checks (void)
178 if (!deferred_access_no_check)
179 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
182 /* Stop deferring access checks. */
184 void
185 stop_deferring_access_checks (void)
187 if (!deferred_access_no_check)
188 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
191 /* Discard the current deferred access checks and restore the
192 previous states. */
194 void
195 pop_deferring_access_checks (void)
197 if (deferred_access_no_check)
198 deferred_access_no_check--;
199 else
200 deferred_access_stack->pop ();
203 /* Returns a TREE_LIST representing the deferred checks.
204 The TREE_PURPOSE of each node is the type through which the
205 access occurred; the TREE_VALUE is the declaration named.
208 vec<deferred_access_check, va_gc> *
209 get_deferred_access_checks (void)
211 if (deferred_access_no_check)
212 return NULL;
213 else
214 return (deferred_access_stack->last().deferred_access_checks);
217 /* Take current deferred checks and combine with the
218 previous states if we also defer checks previously.
219 Otherwise perform checks now. */
221 void
222 pop_to_parent_deferring_access_checks (void)
224 if (deferred_access_no_check)
225 deferred_access_no_check--;
226 else
228 vec<deferred_access_check, va_gc> *checks;
229 deferred_access *ptr;
231 checks = (deferred_access_stack->last ().deferred_access_checks);
233 deferred_access_stack->pop ();
234 ptr = &deferred_access_stack->last ();
235 if (ptr->deferring_access_checks_kind == dk_no_deferred)
237 /* Check access. */
238 perform_access_checks (checks, tf_warning_or_error);
240 else
242 /* Merge with parent. */
243 int i, j;
244 deferred_access_check *chk, *probe;
246 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
248 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
250 if (probe->binfo == chk->binfo &&
251 probe->decl == chk->decl &&
252 probe->diag_decl == chk->diag_decl)
253 goto found;
255 /* Insert into parent's checks. */
256 vec_safe_push (ptr->deferred_access_checks, *chk);
257 found:;
263 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
264 is the BINFO indicating the qualifying scope used to access the
265 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
266 or we aren't in SFINAE context or all the checks succeed return TRUE,
267 otherwise FALSE. */
269 bool
270 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
271 tsubst_flags_t complain)
273 int i;
274 deferred_access_check *chk;
275 location_t loc = input_location;
276 bool ok = true;
278 if (!checks)
279 return true;
281 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
283 input_location = chk->loc;
284 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
287 input_location = loc;
288 return (complain & tf_error) ? true : ok;
291 /* Perform the deferred access checks.
293 After performing the checks, we still have to keep the list
294 `deferred_access_stack->deferred_access_checks' since we may want
295 to check access for them again later in a different context.
296 For example:
298 class A {
299 typedef int X;
300 static X a;
302 A::X A::a, x; // No error for `A::a', error for `x'
304 We have to perform deferred access of `A::X', first with `A::a',
305 next with `x'. Return value like perform_access_checks above. */
307 bool
308 perform_deferred_access_checks (tsubst_flags_t complain)
310 return perform_access_checks (get_deferred_access_checks (), complain);
313 /* Defer checking the accessibility of DECL, when looked up in
314 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
315 Return value like perform_access_checks above. */
317 bool
318 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
319 tsubst_flags_t complain)
321 int i;
322 deferred_access *ptr;
323 deferred_access_check *chk;
326 /* Exit if we are in a context that no access checking is performed.
328 if (deferred_access_no_check)
329 return true;
331 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
333 ptr = &deferred_access_stack->last ();
335 /* If we are not supposed to defer access checks, just check now. */
336 if (ptr->deferring_access_checks_kind == dk_no_deferred)
338 bool ok = enforce_access (binfo, decl, diag_decl, complain);
339 return (complain & tf_error) ? true : ok;
342 /* See if we are already going to perform this check. */
343 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
345 if (chk->decl == decl && chk->binfo == binfo &&
346 chk->diag_decl == diag_decl)
348 return true;
351 /* If not, record the check. */
352 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
353 vec_safe_push (ptr->deferred_access_checks, new_access);
355 return true;
358 /* Returns nonzero if the current statement is a full expression,
359 i.e. temporaries created during that statement should be destroyed
360 at the end of the statement. */
363 stmts_are_full_exprs_p (void)
365 return current_stmt_tree ()->stmts_are_full_exprs_p;
368 /* T is a statement. Add it to the statement-tree. This is the C++
369 version. The C/ObjC frontends have a slightly different version of
370 this function. */
372 tree
373 add_stmt (tree t)
375 enum tree_code code = TREE_CODE (t);
377 if (EXPR_P (t) && code != LABEL_EXPR)
379 if (!EXPR_HAS_LOCATION (t))
380 SET_EXPR_LOCATION (t, input_location);
382 /* When we expand a statement-tree, we must know whether or not the
383 statements are full-expressions. We record that fact here. */
384 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
387 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
388 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
390 /* Add T to the statement-tree. Non-side-effect statements need to be
391 recorded during statement expressions. */
392 gcc_checking_assert (!stmt_list_stack->is_empty ());
393 append_to_statement_list_force (t, &cur_stmt_list);
395 return t;
398 /* Returns the stmt_tree to which statements are currently being added. */
400 stmt_tree
401 current_stmt_tree (void)
403 return (cfun
404 ? &cfun->language->base.x_stmt_tree
405 : &scope_chain->x_stmt_tree);
408 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
410 static tree
411 maybe_cleanup_point_expr (tree expr)
413 if (!processing_template_decl && stmts_are_full_exprs_p ())
414 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
415 return expr;
418 /* Like maybe_cleanup_point_expr except have the type of the new expression be
419 void so we don't need to create a temporary variable to hold the inner
420 expression. The reason why we do this is because the original type might be
421 an aggregate and we cannot create a temporary variable for that type. */
423 tree
424 maybe_cleanup_point_expr_void (tree expr)
426 if (!processing_template_decl && stmts_are_full_exprs_p ())
427 expr = fold_build_cleanup_point_expr (void_type_node, expr);
428 return expr;
433 /* Create a declaration statement for the declaration given by the DECL. */
435 void
436 add_decl_expr (tree decl)
438 tree r = build_stmt (input_location, DECL_EXPR, decl);
439 if (DECL_INITIAL (decl)
440 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
441 r = maybe_cleanup_point_expr_void (r);
442 add_stmt (r);
445 /* Finish a scope. */
447 tree
448 do_poplevel (tree stmt_list)
450 tree block = NULL;
452 if (stmts_are_full_exprs_p ())
453 block = poplevel (kept_level_p (), 1, 0);
455 stmt_list = pop_stmt_list (stmt_list);
457 if (!processing_template_decl)
459 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
460 /* ??? See c_end_compound_stmt re statement expressions. */
463 return stmt_list;
466 /* Begin a new scope. */
468 static tree
469 do_pushlevel (scope_kind sk)
471 tree ret = push_stmt_list ();
472 if (stmts_are_full_exprs_p ())
473 begin_scope (sk, NULL);
474 return ret;
477 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
478 when the current scope is exited. EH_ONLY is true when this is not
479 meant to apply to normal control flow transfer. */
481 void
482 push_cleanup (tree decl, tree cleanup, bool eh_only)
484 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
485 CLEANUP_EH_ONLY (stmt) = eh_only;
486 add_stmt (stmt);
487 CLEANUP_BODY (stmt) = push_stmt_list ();
490 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
491 the current loops, represented by 'NULL_TREE' if we've seen a possible
492 exit, and 'error_mark_node' if not. This is currently used only to
493 suppress the warning about a function with no return statements, and
494 therefore we don't bother noting returns as possible exits. We also
495 don't bother with gotos. */
497 static void
498 begin_maybe_infinite_loop (tree cond)
500 /* Only track this while parsing a function, not during instantiation. */
501 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
502 && !processing_template_decl))
503 return;
504 bool maybe_infinite = true;
505 if (cond)
507 cond = fold_non_dependent_expr (cond);
508 maybe_infinite = integer_nonzerop (cond);
510 vec_safe_push (cp_function_chain->infinite_loops,
511 maybe_infinite ? error_mark_node : NULL_TREE);
515 /* A break is a possible exit for the current loop. */
517 void
518 break_maybe_infinite_loop (void)
520 if (!cfun)
521 return;
522 cp_function_chain->infinite_loops->last() = NULL_TREE;
525 /* If we reach the end of the loop without seeing a possible exit, we have
526 an infinite loop. */
528 static void
529 end_maybe_infinite_loop (tree cond)
531 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
532 && !processing_template_decl))
533 return;
534 tree current = cp_function_chain->infinite_loops->pop();
535 if (current != NULL_TREE)
537 cond = fold_non_dependent_expr (cond);
538 if (integer_nonzerop (cond))
539 current_function_infinite_loop = 1;
544 /* Begin a conditional that might contain a declaration. When generating
545 normal code, we want the declaration to appear before the statement
546 containing the conditional. When generating template code, we want the
547 conditional to be rendered as the raw DECL_EXPR. */
549 static void
550 begin_cond (tree *cond_p)
552 if (processing_template_decl)
553 *cond_p = push_stmt_list ();
556 /* Finish such a conditional. */
558 static void
559 finish_cond (tree *cond_p, tree expr)
561 if (processing_template_decl)
563 tree cond = pop_stmt_list (*cond_p);
565 if (expr == NULL_TREE)
566 /* Empty condition in 'for'. */
567 gcc_assert (empty_expr_stmt_p (cond));
568 else if (check_for_bare_parameter_packs (expr))
569 expr = error_mark_node;
570 else if (!empty_expr_stmt_p (cond))
571 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
573 *cond_p = expr;
576 /* If *COND_P specifies a conditional with a declaration, transform the
577 loop such that
578 while (A x = 42) { }
579 for (; A x = 42;) { }
580 becomes
581 while (true) { A x = 42; if (!x) break; }
582 for (;;) { A x = 42; if (!x) break; }
583 The statement list for BODY will be empty if the conditional did
584 not declare anything. */
586 static void
587 simplify_loop_decl_cond (tree *cond_p, tree body)
589 tree cond, if_stmt;
591 if (!TREE_SIDE_EFFECTS (body))
592 return;
594 cond = *cond_p;
595 *cond_p = boolean_true_node;
597 if_stmt = begin_if_stmt ();
598 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
599 finish_if_stmt_cond (cond, if_stmt);
600 finish_break_stmt ();
601 finish_then_clause (if_stmt);
602 finish_if_stmt (if_stmt);
605 /* Finish a goto-statement. */
607 tree
608 finish_goto_stmt (tree destination)
610 if (identifier_p (destination))
611 destination = lookup_label (destination);
613 /* We warn about unused labels with -Wunused. That means we have to
614 mark the used labels as used. */
615 if (TREE_CODE (destination) == LABEL_DECL)
616 TREE_USED (destination) = 1;
617 else
619 if (check_no_cilk (destination,
620 "Cilk array notation cannot be used as a computed goto expression",
621 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
622 destination = error_mark_node;
623 destination = mark_rvalue_use (destination);
624 if (!processing_template_decl)
626 destination = cp_convert (ptr_type_node, destination,
627 tf_warning_or_error);
628 if (error_operand_p (destination))
629 return NULL_TREE;
630 destination
631 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
632 destination);
636 check_goto (destination);
638 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
641 /* COND is the condition-expression for an if, while, etc.,
642 statement. Convert it to a boolean value, if appropriate.
643 In addition, verify sequence points if -Wsequence-point is enabled. */
645 static tree
646 maybe_convert_cond (tree cond)
648 /* Empty conditions remain empty. */
649 if (!cond)
650 return NULL_TREE;
652 /* Wait until we instantiate templates before doing conversion. */
653 if (processing_template_decl)
654 return cond;
656 if (warn_sequence_point)
657 verify_sequence_points (cond);
659 /* Do the conversion. */
660 cond = convert_from_reference (cond);
662 if (TREE_CODE (cond) == MODIFY_EXPR
663 && !TREE_NO_WARNING (cond)
664 && warn_parentheses)
666 warning (OPT_Wparentheses,
667 "suggest parentheses around assignment used as truth value");
668 TREE_NO_WARNING (cond) = 1;
671 return condition_conversion (cond);
674 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
676 tree
677 finish_expr_stmt (tree expr)
679 tree r = NULL_TREE;
681 if (expr != NULL_TREE)
683 if (!processing_template_decl)
685 if (warn_sequence_point)
686 verify_sequence_points (expr);
687 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
689 else if (!type_dependent_expression_p (expr))
690 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
691 tf_warning_or_error);
693 if (check_for_bare_parameter_packs (expr))
694 expr = error_mark_node;
696 /* Simplification of inner statement expressions, compound exprs,
697 etc can result in us already having an EXPR_STMT. */
698 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
700 if (TREE_CODE (expr) != EXPR_STMT)
701 expr = build_stmt (input_location, EXPR_STMT, expr);
702 expr = maybe_cleanup_point_expr_void (expr);
705 r = add_stmt (expr);
708 return r;
712 /* Begin an if-statement. Returns a newly created IF_STMT if
713 appropriate. */
715 tree
716 begin_if_stmt (void)
718 tree r, scope;
719 scope = do_pushlevel (sk_cond);
720 r = build_stmt (input_location, IF_STMT, NULL_TREE,
721 NULL_TREE, NULL_TREE, scope);
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 void
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
733 add_stmt (if_stmt);
734 THEN_CLAUSE (if_stmt) = push_stmt_list ();
737 /* Finish the then-clause of an if-statement, which may be given by
738 IF_STMT. */
740 tree
741 finish_then_clause (tree if_stmt)
743 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
744 return if_stmt;
747 /* Begin the else-clause of an if-statement. */
749 void
750 begin_else_clause (tree if_stmt)
752 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
755 /* Finish the else-clause of an if-statement, which may be given by
756 IF_STMT. */
758 void
759 finish_else_clause (tree if_stmt)
761 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
764 /* Finish an if-statement. */
766 void
767 finish_if_stmt (tree if_stmt)
769 tree scope = IF_SCOPE (if_stmt);
770 IF_SCOPE (if_stmt) = NULL;
771 add_stmt (do_poplevel (scope));
774 /* Begin a while-statement. Returns a newly created WHILE_STMT if
775 appropriate. */
777 tree
778 begin_while_stmt (void)
780 tree r;
781 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
782 add_stmt (r);
783 WHILE_BODY (r) = do_pushlevel (sk_block);
784 begin_cond (&WHILE_COND (r));
785 return r;
788 /* Process the COND of a while-statement, which may be given by
789 WHILE_STMT. */
791 void
792 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
794 if (check_no_cilk (cond,
795 "Cilk array notation cannot be used as a condition for while statement",
796 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
797 cond = error_mark_node;
798 cond = maybe_convert_cond (cond);
799 finish_cond (&WHILE_COND (while_stmt), cond);
800 begin_maybe_infinite_loop (cond);
801 if (ivdep && cond != error_mark_node)
802 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
803 TREE_TYPE (WHILE_COND (while_stmt)),
804 WHILE_COND (while_stmt),
805 build_int_cst (integer_type_node,
806 annot_expr_ivdep_kind));
807 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
810 /* Finish a while-statement, which may be given by WHILE_STMT. */
812 void
813 finish_while_stmt (tree while_stmt)
815 end_maybe_infinite_loop (boolean_true_node);
816 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
819 /* Begin a do-statement. Returns a newly created DO_STMT if
820 appropriate. */
822 tree
823 begin_do_stmt (void)
825 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
826 begin_maybe_infinite_loop (boolean_true_node);
827 add_stmt (r);
828 DO_BODY (r) = push_stmt_list ();
829 return r;
832 /* Finish the body of a do-statement, which may be given by DO_STMT. */
834 void
835 finish_do_body (tree do_stmt)
837 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
839 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
840 body = STATEMENT_LIST_TAIL (body)->stmt;
842 if (IS_EMPTY_STMT (body))
843 warning (OPT_Wempty_body,
844 "suggest explicit braces around empty body in %<do%> statement");
847 /* Finish a do-statement, which may be given by DO_STMT, and whose
848 COND is as indicated. */
850 void
851 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
853 if (check_no_cilk (cond,
854 "Cilk array notation cannot be used as a condition for a do-while statement",
855 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
856 cond = error_mark_node;
857 cond = maybe_convert_cond (cond);
858 end_maybe_infinite_loop (cond);
859 if (ivdep && cond != error_mark_node)
860 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
861 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
862 DO_COND (do_stmt) = cond;
865 /* Finish a return-statement. The EXPRESSION returned, if any, is as
866 indicated. */
868 tree
869 finish_return_stmt (tree expr)
871 tree r;
872 bool no_warning;
874 expr = check_return_expr (expr, &no_warning);
876 if (error_operand_p (expr)
877 || (flag_openmp && !check_omp_return ()))
879 /* Suppress -Wreturn-type for this function. */
880 if (warn_return_type)
881 TREE_NO_WARNING (current_function_decl) = true;
882 return error_mark_node;
885 if (!processing_template_decl)
887 if (warn_sequence_point)
888 verify_sequence_points (expr);
890 if (DECL_DESTRUCTOR_P (current_function_decl)
891 || (DECL_CONSTRUCTOR_P (current_function_decl)
892 && targetm.cxx.cdtor_returns_this ()))
894 /* Similarly, all destructors must run destructors for
895 base-classes before returning. So, all returns in a
896 destructor get sent to the DTOR_LABEL; finish_function emits
897 code to return a value there. */
898 return finish_goto_stmt (cdtor_label);
902 r = build_stmt (input_location, RETURN_EXPR, expr);
903 TREE_NO_WARNING (r) |= no_warning;
904 r = maybe_cleanup_point_expr_void (r);
905 r = add_stmt (r);
907 return r;
910 /* Begin the scope of a for-statement or a range-for-statement.
911 Both the returned trees are to be used in a call to
912 begin_for_stmt or begin_range_for_stmt. */
914 tree
915 begin_for_scope (tree *init)
917 tree scope = NULL_TREE;
918 if (flag_new_for_scope > 0)
919 scope = do_pushlevel (sk_for);
921 if (processing_template_decl)
922 *init = push_stmt_list ();
923 else
924 *init = NULL_TREE;
926 return scope;
929 /* Begin a for-statement. Returns a new FOR_STMT.
930 SCOPE and INIT should be the return of begin_for_scope,
931 or both NULL_TREE */
933 tree
934 begin_for_stmt (tree scope, tree init)
936 tree r;
938 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
939 NULL_TREE, NULL_TREE, NULL_TREE);
941 if (scope == NULL_TREE)
943 gcc_assert (!init || !(flag_new_for_scope > 0));
944 if (!init)
945 scope = begin_for_scope (&init);
947 FOR_INIT_STMT (r) = init;
948 FOR_SCOPE (r) = scope;
950 return r;
953 /* Finish the for-init-statement of a for-statement, which may be
954 given by FOR_STMT. */
956 void
957 finish_for_init_stmt (tree for_stmt)
959 if (processing_template_decl)
960 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
961 add_stmt (for_stmt);
962 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
963 begin_cond (&FOR_COND (for_stmt));
966 /* Finish the COND of a for-statement, which may be given by
967 FOR_STMT. */
969 void
970 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
972 if (check_no_cilk (cond,
973 "Cilk array notation cannot be used in a condition for a for-loop",
974 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
975 cond = error_mark_node;
976 cond = maybe_convert_cond (cond);
977 finish_cond (&FOR_COND (for_stmt), cond);
978 begin_maybe_infinite_loop (cond);
979 if (ivdep && cond != error_mark_node)
980 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
981 TREE_TYPE (FOR_COND (for_stmt)),
982 FOR_COND (for_stmt),
983 build_int_cst (integer_type_node,
984 annot_expr_ivdep_kind));
985 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
988 /* Finish the increment-EXPRESSION in a for-statement, which may be
989 given by FOR_STMT. */
991 void
992 finish_for_expr (tree expr, tree for_stmt)
994 if (!expr)
995 return;
996 /* If EXPR is an overloaded function, issue an error; there is no
997 context available to use to perform overload resolution. */
998 if (type_unknown_p (expr))
1000 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1001 expr = error_mark_node;
1003 if (!processing_template_decl)
1005 if (warn_sequence_point)
1006 verify_sequence_points (expr);
1007 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1008 tf_warning_or_error);
1010 else if (!type_dependent_expression_p (expr))
1011 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1012 tf_warning_or_error);
1013 expr = maybe_cleanup_point_expr_void (expr);
1014 if (check_for_bare_parameter_packs (expr))
1015 expr = error_mark_node;
1016 FOR_EXPR (for_stmt) = expr;
1019 /* Finish the body of a for-statement, which may be given by
1020 FOR_STMT. The increment-EXPR for the loop must be
1021 provided.
1022 It can also finish RANGE_FOR_STMT. */
1024 void
1025 finish_for_stmt (tree for_stmt)
1027 end_maybe_infinite_loop (boolean_true_node);
1029 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1030 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1031 else
1032 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1034 /* Pop the scope for the body of the loop. */
1035 if (flag_new_for_scope > 0)
1037 tree scope;
1038 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1039 ? &RANGE_FOR_SCOPE (for_stmt)
1040 : &FOR_SCOPE (for_stmt));
1041 scope = *scope_ptr;
1042 *scope_ptr = NULL;
1043 add_stmt (do_poplevel (scope));
1047 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1048 SCOPE and INIT should be the return of begin_for_scope,
1049 or both NULL_TREE .
1050 To finish it call finish_for_stmt(). */
1052 tree
1053 begin_range_for_stmt (tree scope, tree init)
1055 tree r;
1057 begin_maybe_infinite_loop (boolean_false_node);
1059 r = build_stmt (input_location, RANGE_FOR_STMT,
1060 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1062 if (scope == NULL_TREE)
1064 gcc_assert (!init || !(flag_new_for_scope > 0));
1065 if (!init)
1066 scope = begin_for_scope (&init);
1069 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1070 pop it now. */
1071 if (init)
1072 pop_stmt_list (init);
1073 RANGE_FOR_SCOPE (r) = scope;
1075 return r;
1078 /* Finish the head of a range-based for statement, which may
1079 be given by RANGE_FOR_STMT. DECL must be the declaration
1080 and EXPR must be the loop expression. */
1082 void
1083 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1085 RANGE_FOR_DECL (range_for_stmt) = decl;
1086 RANGE_FOR_EXPR (range_for_stmt) = expr;
1087 add_stmt (range_for_stmt);
1088 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1091 /* Finish a break-statement. */
1093 tree
1094 finish_break_stmt (void)
1096 /* In switch statements break is sometimes stylistically used after
1097 a return statement. This can lead to spurious warnings about
1098 control reaching the end of a non-void function when it is
1099 inlined. Note that we are calling block_may_fallthru with
1100 language specific tree nodes; this works because
1101 block_may_fallthru returns true when given something it does not
1102 understand. */
1103 if (!block_may_fallthru (cur_stmt_list))
1104 return void_node;
1105 return add_stmt (build_stmt (input_location, BREAK_STMT));
1108 /* Finish a continue-statement. */
1110 tree
1111 finish_continue_stmt (void)
1113 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1116 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1117 appropriate. */
1119 tree
1120 begin_switch_stmt (void)
1122 tree r, scope;
1124 scope = do_pushlevel (sk_cond);
1125 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1127 begin_cond (&SWITCH_STMT_COND (r));
1129 return r;
1132 /* Finish the cond of a switch-statement. */
1134 void
1135 finish_switch_cond (tree cond, tree switch_stmt)
1137 tree orig_type = NULL;
1139 if (check_no_cilk (cond,
1140 "Cilk array notation cannot be used as a condition for switch statement",
1141 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1142 cond = error_mark_node;
1144 if (!processing_template_decl)
1146 /* Convert the condition to an integer or enumeration type. */
1147 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1148 if (cond == NULL_TREE)
1150 error ("switch quantity not an integer");
1151 cond = error_mark_node;
1153 /* We want unlowered type here to handle enum bit-fields. */
1154 orig_type = unlowered_expr_type (cond);
1155 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1156 orig_type = TREE_TYPE (cond);
1157 if (cond != error_mark_node)
1159 /* Warn if the condition has boolean value. */
1160 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1161 warning_at (input_location, OPT_Wswitch_bool,
1162 "switch condition has type bool");
1164 /* [stmt.switch]
1166 Integral promotions are performed. */
1167 cond = perform_integral_promotions (cond);
1168 cond = maybe_cleanup_point_expr (cond);
1171 if (check_for_bare_parameter_packs (cond))
1172 cond = error_mark_node;
1173 else if (!processing_template_decl && warn_sequence_point)
1174 verify_sequence_points (cond);
1176 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1177 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1178 add_stmt (switch_stmt);
1179 push_switch (switch_stmt);
1180 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1183 /* Finish the body of a switch-statement, which may be given by
1184 SWITCH_STMT. The COND to switch on is indicated. */
1186 void
1187 finish_switch_stmt (tree switch_stmt)
1189 tree scope;
1191 SWITCH_STMT_BODY (switch_stmt) =
1192 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1193 pop_switch ();
1195 scope = SWITCH_STMT_SCOPE (switch_stmt);
1196 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1197 add_stmt (do_poplevel (scope));
1200 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1201 appropriate. */
1203 tree
1204 begin_try_block (void)
1206 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1207 add_stmt (r);
1208 TRY_STMTS (r) = push_stmt_list ();
1209 return r;
1212 /* Likewise, for a function-try-block. The block returned in
1213 *COMPOUND_STMT is an artificial outer scope, containing the
1214 function-try-block. */
1216 tree
1217 begin_function_try_block (tree *compound_stmt)
1219 tree r;
1220 /* This outer scope does not exist in the C++ standard, but we need
1221 a place to put __FUNCTION__ and similar variables. */
1222 *compound_stmt = begin_compound_stmt (0);
1223 r = begin_try_block ();
1224 FN_TRY_BLOCK_P (r) = 1;
1225 return r;
1228 /* Finish a try-block, which may be given by TRY_BLOCK. */
1230 void
1231 finish_try_block (tree try_block)
1233 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1234 TRY_HANDLERS (try_block) = push_stmt_list ();
1237 /* Finish the body of a cleanup try-block, which may be given by
1238 TRY_BLOCK. */
1240 void
1241 finish_cleanup_try_block (tree try_block)
1243 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1246 /* Finish an implicitly generated try-block, with a cleanup is given
1247 by CLEANUP. */
1249 void
1250 finish_cleanup (tree cleanup, tree try_block)
1252 TRY_HANDLERS (try_block) = cleanup;
1253 CLEANUP_P (try_block) = 1;
1256 /* Likewise, for a function-try-block. */
1258 void
1259 finish_function_try_block (tree try_block)
1261 finish_try_block (try_block);
1262 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1263 the try block, but moving it inside. */
1264 in_function_try_handler = 1;
1267 /* Finish a handler-sequence for a try-block, which may be given by
1268 TRY_BLOCK. */
1270 void
1271 finish_handler_sequence (tree try_block)
1273 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1274 check_handlers (TRY_HANDLERS (try_block));
1277 /* Finish the handler-seq for a function-try-block, given by
1278 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1279 begin_function_try_block. */
1281 void
1282 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1284 in_function_try_handler = 0;
1285 finish_handler_sequence (try_block);
1286 finish_compound_stmt (compound_stmt);
1289 /* Begin a handler. Returns a HANDLER if appropriate. */
1291 tree
1292 begin_handler (void)
1294 tree r;
1296 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1297 add_stmt (r);
1299 /* Create a binding level for the eh_info and the exception object
1300 cleanup. */
1301 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1303 return r;
1306 /* Finish the handler-parameters for a handler, which may be given by
1307 HANDLER. DECL is the declaration for the catch parameter, or NULL
1308 if this is a `catch (...)' clause. */
1310 void
1311 finish_handler_parms (tree decl, tree handler)
1313 tree type = NULL_TREE;
1314 if (processing_template_decl)
1316 if (decl)
1318 decl = pushdecl (decl);
1319 decl = push_template_decl (decl);
1320 HANDLER_PARMS (handler) = decl;
1321 type = TREE_TYPE (decl);
1324 else
1325 type = expand_start_catch_block (decl);
1326 HANDLER_TYPE (handler) = type;
1329 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1330 the return value from the matching call to finish_handler_parms. */
1332 void
1333 finish_handler (tree handler)
1335 if (!processing_template_decl)
1336 expand_end_catch_block ();
1337 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1340 /* Begin a compound statement. FLAGS contains some bits that control the
1341 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1342 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1343 block of a function. If BCS_TRY_BLOCK is set, this is the block
1344 created on behalf of a TRY statement. Returns a token to be passed to
1345 finish_compound_stmt. */
1347 tree
1348 begin_compound_stmt (unsigned int flags)
1350 tree r;
1352 if (flags & BCS_NO_SCOPE)
1354 r = push_stmt_list ();
1355 STATEMENT_LIST_NO_SCOPE (r) = 1;
1357 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1358 But, if it's a statement-expression with a scopeless block, there's
1359 nothing to keep, and we don't want to accidentally keep a block
1360 *inside* the scopeless block. */
1361 keep_next_level (false);
1363 else
1364 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1366 /* When processing a template, we need to remember where the braces were,
1367 so that we can set up identical scopes when instantiating the template
1368 later. BIND_EXPR is a handy candidate for this.
1369 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1370 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1371 processing templates. */
1372 if (processing_template_decl)
1374 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1375 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1376 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1377 TREE_SIDE_EFFECTS (r) = 1;
1380 return r;
1383 /* Finish a compound-statement, which is given by STMT. */
1385 void
1386 finish_compound_stmt (tree stmt)
1388 if (TREE_CODE (stmt) == BIND_EXPR)
1390 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1391 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1392 discard the BIND_EXPR so it can be merged with the containing
1393 STATEMENT_LIST. */
1394 if (TREE_CODE (body) == STATEMENT_LIST
1395 && STATEMENT_LIST_HEAD (body) == NULL
1396 && !BIND_EXPR_BODY_BLOCK (stmt)
1397 && !BIND_EXPR_TRY_BLOCK (stmt))
1398 stmt = body;
1399 else
1400 BIND_EXPR_BODY (stmt) = body;
1402 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1403 stmt = pop_stmt_list (stmt);
1404 else
1406 /* Destroy any ObjC "super" receivers that may have been
1407 created. */
1408 objc_clear_super_receiver ();
1410 stmt = do_poplevel (stmt);
1413 /* ??? See c_end_compound_stmt wrt statement expressions. */
1414 add_stmt (stmt);
1417 /* Finish an asm-statement, whose components are a STRING, some
1418 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1419 LABELS. Also note whether the asm-statement should be
1420 considered volatile. */
1422 tree
1423 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1424 tree input_operands, tree clobbers, tree labels)
1426 tree r;
1427 tree t;
1428 int ninputs = list_length (input_operands);
1429 int noutputs = list_length (output_operands);
1431 if (!processing_template_decl)
1433 const char *constraint;
1434 const char **oconstraints;
1435 bool allows_mem, allows_reg, is_inout;
1436 tree operand;
1437 int i;
1439 oconstraints = XALLOCAVEC (const char *, noutputs);
1441 string = resolve_asm_operand_names (string, output_operands,
1442 input_operands, labels);
1444 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1446 operand = TREE_VALUE (t);
1448 /* ??? Really, this should not be here. Users should be using a
1449 proper lvalue, dammit. But there's a long history of using
1450 casts in the output operands. In cases like longlong.h, this
1451 becomes a primitive form of typechecking -- if the cast can be
1452 removed, then the output operand had a type of the proper width;
1453 otherwise we'll get an error. Gross, but ... */
1454 STRIP_NOPS (operand);
1456 operand = mark_lvalue_use (operand);
1458 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1459 operand = error_mark_node;
1461 if (operand != error_mark_node
1462 && (TREE_READONLY (operand)
1463 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1464 /* Functions are not modifiable, even though they are
1465 lvalues. */
1466 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1467 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1468 /* If it's an aggregate and any field is const, then it is
1469 effectively const. */
1470 || (CLASS_TYPE_P (TREE_TYPE (operand))
1471 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1472 cxx_readonly_error (operand, lv_asm);
1474 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1475 oconstraints[i] = constraint;
1477 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1478 &allows_mem, &allows_reg, &is_inout))
1480 /* If the operand is going to end up in memory,
1481 mark it addressable. */
1482 if (!allows_reg && !cxx_mark_addressable (operand))
1483 operand = error_mark_node;
1485 else
1486 operand = error_mark_node;
1488 TREE_VALUE (t) = operand;
1491 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1493 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1494 bool constraint_parsed
1495 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1496 oconstraints, &allows_mem, &allows_reg);
1497 /* If the operand is going to end up in memory, don't call
1498 decay_conversion. */
1499 if (constraint_parsed && !allows_reg && allows_mem)
1500 operand = mark_lvalue_use (TREE_VALUE (t));
1501 else
1502 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1504 /* If the type of the operand hasn't been determined (e.g.,
1505 because it involves an overloaded function), then issue
1506 an error message. There's no context available to
1507 resolve the overloading. */
1508 if (TREE_TYPE (operand) == unknown_type_node)
1510 error ("type of asm operand %qE could not be determined",
1511 TREE_VALUE (t));
1512 operand = error_mark_node;
1515 if (constraint_parsed)
1517 /* If the operand is going to end up in memory,
1518 mark it addressable. */
1519 if (!allows_reg && allows_mem)
1521 /* Strip the nops as we allow this case. FIXME, this really
1522 should be rejected or made deprecated. */
1523 STRIP_NOPS (operand);
1524 if (!cxx_mark_addressable (operand))
1525 operand = error_mark_node;
1527 else if (!allows_reg && !allows_mem)
1529 /* If constraint allows neither register nor memory,
1530 try harder to get a constant. */
1531 tree constop = maybe_constant_value (operand);
1532 if (TREE_CONSTANT (constop))
1533 operand = constop;
1536 else
1537 operand = error_mark_node;
1539 TREE_VALUE (t) = operand;
1543 r = build_stmt (input_location, ASM_EXPR, string,
1544 output_operands, input_operands,
1545 clobbers, labels);
1546 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1547 r = maybe_cleanup_point_expr_void (r);
1548 return add_stmt (r);
1551 /* Finish a label with the indicated NAME. Returns the new label. */
1553 tree
1554 finish_label_stmt (tree name)
1556 tree decl = define_label (input_location, name);
1558 if (decl == error_mark_node)
1559 return error_mark_node;
1561 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1563 return decl;
1566 /* Finish a series of declarations for local labels. G++ allows users
1567 to declare "local" labels, i.e., labels with scope. This extension
1568 is useful when writing code involving statement-expressions. */
1570 void
1571 finish_label_decl (tree name)
1573 if (!at_function_scope_p ())
1575 error ("__label__ declarations are only allowed in function scopes");
1576 return;
1579 add_decl_expr (declare_local_label (name));
1582 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1584 void
1585 finish_decl_cleanup (tree decl, tree cleanup)
1587 push_cleanup (decl, cleanup, false);
1590 /* If the current scope exits with an exception, run CLEANUP. */
1592 void
1593 finish_eh_cleanup (tree cleanup)
1595 push_cleanup (NULL, cleanup, true);
1598 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1599 order they were written by the user. Each node is as for
1600 emit_mem_initializers. */
1602 void
1603 finish_mem_initializers (tree mem_inits)
1605 /* Reorder the MEM_INITS so that they are in the order they appeared
1606 in the source program. */
1607 mem_inits = nreverse (mem_inits);
1609 if (processing_template_decl)
1611 tree mem;
1613 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1615 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1616 check for bare parameter packs in the TREE_VALUE, because
1617 any parameter packs in the TREE_VALUE have already been
1618 bound as part of the TREE_PURPOSE. See
1619 make_pack_expansion for more information. */
1620 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1621 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1622 TREE_VALUE (mem) = error_mark_node;
1625 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1626 CTOR_INITIALIZER, mem_inits));
1628 else
1629 emit_mem_initializers (mem_inits);
1632 /* Obfuscate EXPR if it looks like an id-expression or member access so
1633 that the call to finish_decltype in do_auto_deduction will give the
1634 right result. */
1636 tree
1637 force_paren_expr (tree expr)
1639 /* This is only needed for decltype(auto) in C++14. */
1640 if (cxx_dialect < cxx14)
1641 return expr;
1643 /* If we're in unevaluated context, we can't be deducing a
1644 return/initializer type, so we don't need to mess with this. */
1645 if (cp_unevaluated_operand)
1646 return expr;
1648 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1649 && TREE_CODE (expr) != SCOPE_REF)
1650 return expr;
1652 if (TREE_CODE (expr) == COMPONENT_REF)
1653 REF_PARENTHESIZED_P (expr) = true;
1654 else if (type_dependent_expression_p (expr))
1655 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1656 else
1658 cp_lvalue_kind kind = lvalue_kind (expr);
1659 if ((kind & ~clk_class) != clk_none)
1661 tree type = unlowered_expr_type (expr);
1662 bool rval = !!(kind & clk_rvalueref);
1663 type = cp_build_reference_type (type, rval);
1664 /* This inhibits warnings in, eg, cxx_mark_addressable
1665 (c++/60955). */
1666 warning_sentinel s (extra_warnings);
1667 expr = build_static_cast (type, expr, tf_error);
1668 if (expr != error_mark_node)
1669 REF_PARENTHESIZED_P (expr) = true;
1673 return expr;
1676 /* Finish a parenthesized expression EXPR. */
1678 tree
1679 finish_parenthesized_expr (tree expr)
1681 if (EXPR_P (expr))
1682 /* This inhibits warnings in c_common_truthvalue_conversion. */
1683 TREE_NO_WARNING (expr) = 1;
1685 if (TREE_CODE (expr) == OFFSET_REF
1686 || TREE_CODE (expr) == SCOPE_REF)
1687 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1688 enclosed in parentheses. */
1689 PTRMEM_OK_P (expr) = 0;
1691 if (TREE_CODE (expr) == STRING_CST)
1692 PAREN_STRING_LITERAL_P (expr) = 1;
1694 expr = force_paren_expr (expr);
1696 return expr;
1699 /* Finish a reference to a non-static data member (DECL) that is not
1700 preceded by `.' or `->'. */
1702 tree
1703 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1705 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1707 if (!object)
1709 tree scope = qualifying_scope;
1710 if (scope == NULL_TREE)
1711 scope = context_for_name_lookup (decl);
1712 object = maybe_dummy_object (scope, NULL);
1715 object = maybe_resolve_dummy (object, true);
1716 if (object == error_mark_node)
1717 return error_mark_node;
1719 /* DR 613/850: Can use non-static data members without an associated
1720 object in sizeof/decltype/alignof. */
1721 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1722 && (!processing_template_decl || !current_class_ref))
1724 if (current_function_decl
1725 && DECL_STATIC_FUNCTION_P (current_function_decl))
1726 error ("invalid use of member %qD in static member function", decl);
1727 else
1728 error ("invalid use of non-static data member %qD", decl);
1729 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1731 return error_mark_node;
1734 if (current_class_ptr)
1735 TREE_USED (current_class_ptr) = 1;
1736 if (processing_template_decl && !qualifying_scope)
1738 tree type = TREE_TYPE (decl);
1740 if (TREE_CODE (type) == REFERENCE_TYPE)
1741 /* Quals on the object don't matter. */;
1742 else if (PACK_EXPANSION_P (type))
1743 /* Don't bother trying to represent this. */
1744 type = NULL_TREE;
1745 else
1747 /* Set the cv qualifiers. */
1748 int quals = cp_type_quals (TREE_TYPE (object));
1750 if (DECL_MUTABLE_P (decl))
1751 quals &= ~TYPE_QUAL_CONST;
1753 quals |= cp_type_quals (TREE_TYPE (decl));
1754 type = cp_build_qualified_type (type, quals);
1757 return (convert_from_reference
1758 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1760 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1761 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1762 for now. */
1763 else if (processing_template_decl)
1764 return build_qualified_name (TREE_TYPE (decl),
1765 qualifying_scope,
1766 decl,
1767 /*template_p=*/false);
1768 else
1770 tree access_type = TREE_TYPE (object);
1772 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1773 decl, tf_warning_or_error);
1775 /* If the data member was named `C::M', convert `*this' to `C'
1776 first. */
1777 if (qualifying_scope)
1779 tree binfo = NULL_TREE;
1780 object = build_scoped_ref (object, qualifying_scope,
1781 &binfo);
1784 return build_class_member_access_expr (object, decl,
1785 /*access_path=*/NULL_TREE,
1786 /*preserve_reference=*/false,
1787 tf_warning_or_error);
1791 /* If we are currently parsing a template and we encountered a typedef
1792 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1793 adds the typedef to a list tied to the current template.
1794 At template instantiation time, that list is walked and access check
1795 performed for each typedef.
1796 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1798 void
1799 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1800 tree context,
1801 location_t location)
1803 tree template_info = NULL;
1804 tree cs = current_scope ();
1806 if (!is_typedef_decl (typedef_decl)
1807 || !context
1808 || !CLASS_TYPE_P (context)
1809 || !cs)
1810 return;
1812 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1813 template_info = get_template_info (cs);
1815 if (template_info
1816 && TI_TEMPLATE (template_info)
1817 && !currently_open_class (context))
1818 append_type_to_template_for_access_check (cs, typedef_decl,
1819 context, location);
1822 /* DECL was the declaration to which a qualified-id resolved. Issue
1823 an error message if it is not accessible. If OBJECT_TYPE is
1824 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1825 type of `*x', or `x', respectively. If the DECL was named as
1826 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1828 void
1829 check_accessibility_of_qualified_id (tree decl,
1830 tree object_type,
1831 tree nested_name_specifier)
1833 tree scope;
1834 tree qualifying_type = NULL_TREE;
1836 /* If we are parsing a template declaration and if decl is a typedef,
1837 add it to a list tied to the template.
1838 At template instantiation time, that list will be walked and
1839 access check performed. */
1840 add_typedef_to_current_template_for_access_check (decl,
1841 nested_name_specifier
1842 ? nested_name_specifier
1843 : DECL_CONTEXT (decl),
1844 input_location);
1846 /* If we're not checking, return immediately. */
1847 if (deferred_access_no_check)
1848 return;
1850 /* Determine the SCOPE of DECL. */
1851 scope = context_for_name_lookup (decl);
1852 /* If the SCOPE is not a type, then DECL is not a member. */
1853 if (!TYPE_P (scope))
1854 return;
1855 /* Compute the scope through which DECL is being accessed. */
1856 if (object_type
1857 /* OBJECT_TYPE might not be a class type; consider:
1859 class A { typedef int I; };
1860 I *p;
1861 p->A::I::~I();
1863 In this case, we will have "A::I" as the DECL, but "I" as the
1864 OBJECT_TYPE. */
1865 && CLASS_TYPE_P (object_type)
1866 && DERIVED_FROM_P (scope, object_type))
1867 /* If we are processing a `->' or `.' expression, use the type of the
1868 left-hand side. */
1869 qualifying_type = object_type;
1870 else if (nested_name_specifier)
1872 /* If the reference is to a non-static member of the
1873 current class, treat it as if it were referenced through
1874 `this'. */
1875 tree ct;
1876 if (DECL_NONSTATIC_MEMBER_P (decl)
1877 && current_class_ptr
1878 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1879 qualifying_type = ct;
1880 /* Otherwise, use the type indicated by the
1881 nested-name-specifier. */
1882 else
1883 qualifying_type = nested_name_specifier;
1885 else
1886 /* Otherwise, the name must be from the current class or one of
1887 its bases. */
1888 qualifying_type = currently_open_derived_class (scope);
1890 if (qualifying_type
1891 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1892 or similar in a default argument value. */
1893 && CLASS_TYPE_P (qualifying_type)
1894 && !dependent_type_p (qualifying_type))
1895 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1896 decl, tf_warning_or_error);
1899 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1900 class named to the left of the "::" operator. DONE is true if this
1901 expression is a complete postfix-expression; it is false if this
1902 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1903 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1904 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1905 is true iff this qualified name appears as a template argument. */
1907 tree
1908 finish_qualified_id_expr (tree qualifying_class,
1909 tree expr,
1910 bool done,
1911 bool address_p,
1912 bool template_p,
1913 bool template_arg_p,
1914 tsubst_flags_t complain)
1916 gcc_assert (TYPE_P (qualifying_class));
1918 if (error_operand_p (expr))
1919 return error_mark_node;
1921 if ((DECL_P (expr) || BASELINK_P (expr))
1922 && !mark_used (expr, complain))
1923 return error_mark_node;
1925 if (template_p)
1926 check_template_keyword (expr);
1928 /* If EXPR occurs as the operand of '&', use special handling that
1929 permits a pointer-to-member. */
1930 if (address_p && done)
1932 if (TREE_CODE (expr) == SCOPE_REF)
1933 expr = TREE_OPERAND (expr, 1);
1934 expr = build_offset_ref (qualifying_class, expr,
1935 /*address_p=*/true, complain);
1936 return expr;
1939 /* No need to check access within an enum. */
1940 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1941 return expr;
1943 /* Within the scope of a class, turn references to non-static
1944 members into expression of the form "this->...". */
1945 if (template_arg_p)
1946 /* But, within a template argument, we do not want make the
1947 transformation, as there is no "this" pointer. */
1949 else if (TREE_CODE (expr) == FIELD_DECL)
1951 push_deferring_access_checks (dk_no_check);
1952 expr = finish_non_static_data_member (expr, NULL_TREE,
1953 qualifying_class);
1954 pop_deferring_access_checks ();
1956 else if (BASELINK_P (expr) && !processing_template_decl)
1958 /* See if any of the functions are non-static members. */
1959 /* If so, the expression may be relative to 'this'. */
1960 if (!shared_member_p (expr)
1961 && current_class_ptr
1962 && DERIVED_FROM_P (qualifying_class,
1963 current_nonlambda_class_type ()))
1964 expr = (build_class_member_access_expr
1965 (maybe_dummy_object (qualifying_class, NULL),
1966 expr,
1967 BASELINK_ACCESS_BINFO (expr),
1968 /*preserve_reference=*/false,
1969 complain));
1970 else if (done)
1971 /* The expression is a qualified name whose address is not
1972 being taken. */
1973 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1974 complain);
1976 else if (BASELINK_P (expr))
1978 else
1980 /* In a template, return a SCOPE_REF for most qualified-ids
1981 so that we can check access at instantiation time. But if
1982 we're looking at a member of the current instantiation, we
1983 know we have access and building up the SCOPE_REF confuses
1984 non-type template argument handling. */
1985 if (processing_template_decl
1986 && !currently_open_class (qualifying_class))
1987 expr = build_qualified_name (TREE_TYPE (expr),
1988 qualifying_class, expr,
1989 template_p);
1991 expr = convert_from_reference (expr);
1994 return expr;
1997 /* Begin a statement-expression. The value returned must be passed to
1998 finish_stmt_expr. */
2000 tree
2001 begin_stmt_expr (void)
2003 return push_stmt_list ();
2006 /* Process the final expression of a statement expression. EXPR can be
2007 NULL, if the final expression is empty. Return a STATEMENT_LIST
2008 containing all the statements in the statement-expression, or
2009 ERROR_MARK_NODE if there was an error. */
2011 tree
2012 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2014 if (error_operand_p (expr))
2016 /* The type of the statement-expression is the type of the last
2017 expression. */
2018 TREE_TYPE (stmt_expr) = error_mark_node;
2019 return error_mark_node;
2022 /* If the last statement does not have "void" type, then the value
2023 of the last statement is the value of the entire expression. */
2024 if (expr)
2026 tree type = TREE_TYPE (expr);
2028 if (processing_template_decl)
2030 expr = build_stmt (input_location, EXPR_STMT, expr);
2031 expr = add_stmt (expr);
2032 /* Mark the last statement so that we can recognize it as such at
2033 template-instantiation time. */
2034 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2036 else if (VOID_TYPE_P (type))
2038 /* Just treat this like an ordinary statement. */
2039 expr = finish_expr_stmt (expr);
2041 else
2043 /* It actually has a value we need to deal with. First, force it
2044 to be an rvalue so that we won't need to build up a copy
2045 constructor call later when we try to assign it to something. */
2046 expr = force_rvalue (expr, tf_warning_or_error);
2047 if (error_operand_p (expr))
2048 return error_mark_node;
2050 /* Update for array-to-pointer decay. */
2051 type = TREE_TYPE (expr);
2053 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2054 normal statement, but don't convert to void or actually add
2055 the EXPR_STMT. */
2056 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2057 expr = maybe_cleanup_point_expr (expr);
2058 add_stmt (expr);
2061 /* The type of the statement-expression is the type of the last
2062 expression. */
2063 TREE_TYPE (stmt_expr) = type;
2066 return stmt_expr;
2069 /* Finish a statement-expression. EXPR should be the value returned
2070 by the previous begin_stmt_expr. Returns an expression
2071 representing the statement-expression. */
2073 tree
2074 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2076 tree type;
2077 tree result;
2079 if (error_operand_p (stmt_expr))
2081 pop_stmt_list (stmt_expr);
2082 return error_mark_node;
2085 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2087 type = TREE_TYPE (stmt_expr);
2088 result = pop_stmt_list (stmt_expr);
2089 TREE_TYPE (result) = type;
2091 if (processing_template_decl)
2093 result = build_min (STMT_EXPR, type, result);
2094 TREE_SIDE_EFFECTS (result) = 1;
2095 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2097 else if (CLASS_TYPE_P (type))
2099 /* Wrap the statement-expression in a TARGET_EXPR so that the
2100 temporary object created by the final expression is destroyed at
2101 the end of the full-expression containing the
2102 statement-expression. */
2103 result = force_target_expr (type, result, tf_warning_or_error);
2106 return result;
2109 /* Returns the expression which provides the value of STMT_EXPR. */
2111 tree
2112 stmt_expr_value_expr (tree stmt_expr)
2114 tree t = STMT_EXPR_STMT (stmt_expr);
2116 if (TREE_CODE (t) == BIND_EXPR)
2117 t = BIND_EXPR_BODY (t);
2119 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2120 t = STATEMENT_LIST_TAIL (t)->stmt;
2122 if (TREE_CODE (t) == EXPR_STMT)
2123 t = EXPR_STMT_EXPR (t);
2125 return t;
2128 /* Return TRUE iff EXPR_STMT is an empty list of
2129 expression statements. */
2131 bool
2132 empty_expr_stmt_p (tree expr_stmt)
2134 tree body = NULL_TREE;
2136 if (expr_stmt == void_node)
2137 return true;
2139 if (expr_stmt)
2141 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2142 body = EXPR_STMT_EXPR (expr_stmt);
2143 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2144 body = expr_stmt;
2147 if (body)
2149 if (TREE_CODE (body) == STATEMENT_LIST)
2150 return tsi_end_p (tsi_start (body));
2151 else
2152 return empty_expr_stmt_p (body);
2154 return false;
2157 /* Perform Koenig lookup. FN is the postfix-expression representing
2158 the function (or functions) to call; ARGS are the arguments to the
2159 call. Returns the functions to be considered by overload resolution. */
2161 tree
2162 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2163 tsubst_flags_t complain)
2165 tree identifier = NULL_TREE;
2166 tree functions = NULL_TREE;
2167 tree tmpl_args = NULL_TREE;
2168 bool template_id = false;
2170 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2172 /* Use a separate flag to handle null args. */
2173 template_id = true;
2174 tmpl_args = TREE_OPERAND (fn, 1);
2175 fn = TREE_OPERAND (fn, 0);
2178 /* Find the name of the overloaded function. */
2179 if (identifier_p (fn))
2180 identifier = fn;
2181 else if (is_overloaded_fn (fn))
2183 functions = fn;
2184 identifier = DECL_NAME (get_first_fn (functions));
2186 else if (DECL_P (fn))
2188 functions = fn;
2189 identifier = DECL_NAME (fn);
2192 /* A call to a namespace-scope function using an unqualified name.
2194 Do Koenig lookup -- unless any of the arguments are
2195 type-dependent. */
2196 if (!any_type_dependent_arguments_p (args)
2197 && !any_dependent_template_arguments_p (tmpl_args))
2199 fn = lookup_arg_dependent (identifier, functions, args);
2200 if (!fn)
2202 /* The unqualified name could not be resolved. */
2203 if (complain)
2204 fn = unqualified_fn_lookup_error (identifier);
2205 else
2206 fn = identifier;
2210 if (fn && template_id)
2211 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2213 return fn;
2216 /* Generate an expression for `FN (ARGS)'. This may change the
2217 contents of ARGS.
2219 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2220 as a virtual call, even if FN is virtual. (This flag is set when
2221 encountering an expression where the function name is explicitly
2222 qualified. For example a call to `X::f' never generates a virtual
2223 call.)
2225 Returns code for the call. */
2227 tree
2228 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2229 bool koenig_p, tsubst_flags_t complain)
2231 tree result;
2232 tree orig_fn;
2233 vec<tree, va_gc> *orig_args = NULL;
2235 if (fn == error_mark_node)
2236 return error_mark_node;
2238 gcc_assert (!TYPE_P (fn));
2240 orig_fn = fn;
2242 if (processing_template_decl)
2244 /* If the call expression is dependent, build a CALL_EXPR node
2245 with no type; type_dependent_expression_p recognizes
2246 expressions with no type as being dependent. */
2247 if (type_dependent_expression_p (fn)
2248 || any_type_dependent_arguments_p (*args)
2249 /* For a non-static member function that doesn't have an
2250 explicit object argument, we need to specifically
2251 test the type dependency of the "this" pointer because it
2252 is not included in *ARGS even though it is considered to
2253 be part of the list of arguments. Note that this is
2254 related to CWG issues 515 and 1005. */
2255 || (TREE_CODE (fn) != COMPONENT_REF
2256 && non_static_member_function_p (fn)
2257 && current_class_ref
2258 && type_dependent_expression_p (current_class_ref)))
2260 result = build_nt_call_vec (fn, *args);
2261 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2262 KOENIG_LOOKUP_P (result) = koenig_p;
2263 if (cfun)
2267 tree fndecl = OVL_CURRENT (fn);
2268 if (TREE_CODE (fndecl) != FUNCTION_DECL
2269 || !TREE_THIS_VOLATILE (fndecl))
2270 break;
2271 fn = OVL_NEXT (fn);
2273 while (fn);
2274 if (!fn)
2275 current_function_returns_abnormally = 1;
2277 return result;
2279 orig_args = make_tree_vector_copy (*args);
2280 if (!BASELINK_P (fn)
2281 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2282 && TREE_TYPE (fn) != unknown_type_node)
2283 fn = build_non_dependent_expr (fn);
2284 make_args_non_dependent (*args);
2287 if (TREE_CODE (fn) == COMPONENT_REF)
2289 tree member = TREE_OPERAND (fn, 1);
2290 if (BASELINK_P (member))
2292 tree object = TREE_OPERAND (fn, 0);
2293 return build_new_method_call (object, member,
2294 args, NULL_TREE,
2295 (disallow_virtual
2296 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2297 : LOOKUP_NORMAL),
2298 /*fn_p=*/NULL,
2299 complain);
2303 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2304 if (TREE_CODE (fn) == ADDR_EXPR
2305 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2306 fn = TREE_OPERAND (fn, 0);
2308 if (is_overloaded_fn (fn))
2309 fn = baselink_for_fns (fn);
2311 result = NULL_TREE;
2312 if (BASELINK_P (fn))
2314 tree object;
2316 /* A call to a member function. From [over.call.func]:
2318 If the keyword this is in scope and refers to the class of
2319 that member function, or a derived class thereof, then the
2320 function call is transformed into a qualified function call
2321 using (*this) as the postfix-expression to the left of the
2322 . operator.... [Otherwise] a contrived object of type T
2323 becomes the implied object argument.
2325 In this situation:
2327 struct A { void f(); };
2328 struct B : public A {};
2329 struct C : public A { void g() { B::f(); }};
2331 "the class of that member function" refers to `A'. But 11.2
2332 [class.access.base] says that we need to convert 'this' to B* as
2333 part of the access, so we pass 'B' to maybe_dummy_object. */
2335 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2336 NULL);
2338 if (processing_template_decl)
2340 if (type_dependent_expression_p (object))
2342 tree ret = build_nt_call_vec (orig_fn, orig_args);
2343 release_tree_vector (orig_args);
2344 return ret;
2346 object = build_non_dependent_expr (object);
2349 result = build_new_method_call (object, fn, args, NULL_TREE,
2350 (disallow_virtual
2351 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2352 : LOOKUP_NORMAL),
2353 /*fn_p=*/NULL,
2354 complain);
2356 else if (is_overloaded_fn (fn))
2358 /* If the function is an overloaded builtin, resolve it. */
2359 if (TREE_CODE (fn) == FUNCTION_DECL
2360 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2361 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2362 result = resolve_overloaded_builtin (input_location, fn, *args);
2364 if (!result)
2366 if (warn_sizeof_pointer_memaccess
2367 && (complain & tf_warning)
2368 && !vec_safe_is_empty (*args)
2369 && !processing_template_decl)
2371 location_t sizeof_arg_loc[3];
2372 tree sizeof_arg[3];
2373 unsigned int i;
2374 for (i = 0; i < 3; i++)
2376 tree t;
2378 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2379 sizeof_arg[i] = NULL_TREE;
2380 if (i >= (*args)->length ())
2381 continue;
2382 t = (**args)[i];
2383 if (TREE_CODE (t) != SIZEOF_EXPR)
2384 continue;
2385 if (SIZEOF_EXPR_TYPE_P (t))
2386 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2387 else
2388 sizeof_arg[i] = TREE_OPERAND (t, 0);
2389 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2391 sizeof_pointer_memaccess_warning
2392 (sizeof_arg_loc, fn, *args,
2393 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2396 /* A call to a namespace-scope function. */
2397 result = build_new_function_call (fn, args, koenig_p, complain);
2400 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2402 if (!vec_safe_is_empty (*args))
2403 error ("arguments to destructor are not allowed");
2404 /* Mark the pseudo-destructor call as having side-effects so
2405 that we do not issue warnings about its use. */
2406 result = build1 (NOP_EXPR,
2407 void_type_node,
2408 TREE_OPERAND (fn, 0));
2409 TREE_SIDE_EFFECTS (result) = 1;
2411 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2412 /* If the "function" is really an object of class type, it might
2413 have an overloaded `operator ()'. */
2414 result = build_op_call (fn, args, complain);
2416 if (!result)
2417 /* A call where the function is unknown. */
2418 result = cp_build_function_call_vec (fn, args, complain);
2420 if (processing_template_decl && result != error_mark_node)
2422 if (INDIRECT_REF_P (result))
2423 result = TREE_OPERAND (result, 0);
2424 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2425 SET_EXPR_LOCATION (result, input_location);
2426 KOENIG_LOOKUP_P (result) = koenig_p;
2427 release_tree_vector (orig_args);
2428 result = convert_from_reference (result);
2431 if (koenig_p)
2433 /* Free garbage OVERLOADs from arg-dependent lookup. */
2434 tree next = NULL_TREE;
2435 for (fn = orig_fn;
2436 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2437 fn = next)
2439 if (processing_template_decl)
2440 /* In a template, we'll re-use them at instantiation time. */
2441 OVL_ARG_DEPENDENT (fn) = false;
2442 else
2444 next = OVL_CHAIN (fn);
2445 ggc_free (fn);
2450 return result;
2453 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2454 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2455 POSTDECREMENT_EXPR.) */
2457 tree
2458 finish_increment_expr (tree expr, enum tree_code code)
2460 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2463 /* Finish a use of `this'. Returns an expression for `this'. */
2465 tree
2466 finish_this_expr (void)
2468 tree result = NULL_TREE;
2470 if (current_class_ptr)
2472 tree type = TREE_TYPE (current_class_ref);
2474 /* In a lambda expression, 'this' refers to the captured 'this'. */
2475 if (LAMBDA_TYPE_P (type))
2476 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2477 else
2478 result = current_class_ptr;
2481 if (result)
2482 /* The keyword 'this' is a prvalue expression. */
2483 return rvalue (result);
2485 tree fn = current_nonlambda_function ();
2486 if (fn && DECL_STATIC_FUNCTION_P (fn))
2487 error ("%<this%> is unavailable for static member functions");
2488 else if (fn)
2489 error ("invalid use of %<this%> in non-member function");
2490 else
2491 error ("invalid use of %<this%> at top level");
2492 return error_mark_node;
2495 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2496 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2497 the TYPE for the type given. If SCOPE is non-NULL, the expression
2498 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2500 tree
2501 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2502 location_t loc)
2504 if (object == error_mark_node || destructor == error_mark_node)
2505 return error_mark_node;
2507 gcc_assert (TYPE_P (destructor));
2509 if (!processing_template_decl)
2511 if (scope == error_mark_node)
2513 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2514 return error_mark_node;
2516 if (is_auto (destructor))
2517 destructor = TREE_TYPE (object);
2518 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2520 error_at (loc,
2521 "qualified type %qT does not match destructor name ~%qT",
2522 scope, destructor);
2523 return error_mark_node;
2527 /* [expr.pseudo] says both:
2529 The type designated by the pseudo-destructor-name shall be
2530 the same as the object type.
2532 and:
2534 The cv-unqualified versions of the object type and of the
2535 type designated by the pseudo-destructor-name shall be the
2536 same type.
2538 We implement the more generous second sentence, since that is
2539 what most other compilers do. */
2540 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2541 destructor))
2543 error_at (loc, "%qE is not of type %qT", object, destructor);
2544 return error_mark_node;
2548 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2549 scope, destructor);
2552 /* Finish an expression of the form CODE EXPR. */
2554 tree
2555 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2556 tsubst_flags_t complain)
2558 tree result = build_x_unary_op (loc, code, expr, complain);
2559 if ((complain & tf_warning)
2560 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2561 overflow_warning (input_location, result);
2563 return result;
2566 /* Finish a compound-literal expression. TYPE is the type to which
2567 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2569 tree
2570 finish_compound_literal (tree type, tree compound_literal,
2571 tsubst_flags_t complain)
2573 if (type == error_mark_node)
2574 return error_mark_node;
2576 if (TREE_CODE (type) == REFERENCE_TYPE)
2578 compound_literal
2579 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2580 complain);
2581 return cp_build_c_cast (type, compound_literal, complain);
2584 if (!TYPE_OBJ_P (type))
2586 if (complain & tf_error)
2587 error ("compound literal of non-object type %qT", type);
2588 return error_mark_node;
2591 if (processing_template_decl)
2593 TREE_TYPE (compound_literal) = type;
2594 /* Mark the expression as a compound literal. */
2595 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2596 return compound_literal;
2599 type = complete_type (type);
2601 if (TYPE_NON_AGGREGATE_CLASS (type))
2603 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2604 everywhere that deals with function arguments would be a pain, so
2605 just wrap it in a TREE_LIST. The parser set a flag so we know
2606 that it came from T{} rather than T({}). */
2607 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2608 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2609 return build_functional_cast (type, compound_literal, complain);
2612 if (TREE_CODE (type) == ARRAY_TYPE
2613 && check_array_initializer (NULL_TREE, type, compound_literal))
2614 return error_mark_node;
2615 compound_literal = reshape_init (type, compound_literal, complain);
2616 if (SCALAR_TYPE_P (type)
2617 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2618 && !check_narrowing (type, compound_literal, complain))
2619 return error_mark_node;
2620 if (TREE_CODE (type) == ARRAY_TYPE
2621 && TYPE_DOMAIN (type) == NULL_TREE)
2623 cp_complete_array_type_or_error (&type, compound_literal,
2624 false, complain);
2625 if (type == error_mark_node)
2626 return error_mark_node;
2628 compound_literal = digest_init (type, compound_literal, complain);
2629 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2630 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2631 /* Put static/constant array temporaries in static variables, but always
2632 represent class temporaries with TARGET_EXPR so we elide copies. */
2633 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2634 && TREE_CODE (type) == ARRAY_TYPE
2635 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2636 && initializer_constant_valid_p (compound_literal, type))
2638 tree decl = create_temporary_var (type);
2639 DECL_INITIAL (decl) = compound_literal;
2640 TREE_STATIC (decl) = 1;
2641 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2643 /* 5.19 says that a constant expression can include an
2644 lvalue-rvalue conversion applied to "a glvalue of literal type
2645 that refers to a non-volatile temporary object initialized
2646 with a constant expression". Rather than try to communicate
2647 that this VAR_DECL is a temporary, just mark it constexpr. */
2648 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2649 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2650 TREE_CONSTANT (decl) = true;
2652 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2653 decl = pushdecl_top_level (decl);
2654 DECL_NAME (decl) = make_anon_name ();
2655 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2656 /* Make sure the destructor is callable. */
2657 tree clean = cxx_maybe_build_cleanup (decl, complain);
2658 if (clean == error_mark_node)
2659 return error_mark_node;
2660 return decl;
2662 else
2663 return get_target_expr_sfinae (compound_literal, complain);
2666 /* Return the declaration for the function-name variable indicated by
2667 ID. */
2669 tree
2670 finish_fname (tree id)
2672 tree decl;
2674 decl = fname_decl (input_location, C_RID_CODE (id), id);
2675 if (processing_template_decl && current_function_decl
2676 && decl != error_mark_node)
2677 decl = DECL_NAME (decl);
2678 return decl;
2681 /* Finish a translation unit. */
2683 void
2684 finish_translation_unit (void)
2686 /* In case there were missing closebraces,
2687 get us back to the global binding level. */
2688 pop_everything ();
2689 while (current_namespace != global_namespace)
2690 pop_namespace ();
2692 /* Do file scope __FUNCTION__ et al. */
2693 finish_fname_decls ();
2696 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2697 Returns the parameter. */
2699 tree
2700 finish_template_type_parm (tree aggr, tree identifier)
2702 if (aggr != class_type_node)
2704 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2705 aggr = class_type_node;
2708 return build_tree_list (aggr, identifier);
2711 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2712 Returns the parameter. */
2714 tree
2715 finish_template_template_parm (tree aggr, tree identifier)
2717 tree decl = build_decl (input_location,
2718 TYPE_DECL, identifier, NULL_TREE);
2719 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2720 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2721 DECL_TEMPLATE_RESULT (tmpl) = decl;
2722 DECL_ARTIFICIAL (decl) = 1;
2723 end_template_decl ();
2725 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2727 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2728 /*is_primary=*/true, /*is_partial=*/false,
2729 /*is_friend=*/0);
2731 return finish_template_type_parm (aggr, tmpl);
2734 /* ARGUMENT is the default-argument value for a template template
2735 parameter. If ARGUMENT is invalid, issue error messages and return
2736 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2738 tree
2739 check_template_template_default_arg (tree argument)
2741 if (TREE_CODE (argument) != TEMPLATE_DECL
2742 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2743 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2745 if (TREE_CODE (argument) == TYPE_DECL)
2746 error ("invalid use of type %qT as a default value for a template "
2747 "template-parameter", TREE_TYPE (argument));
2748 else
2749 error ("invalid default argument for a template template parameter");
2750 return error_mark_node;
2753 return argument;
2756 /* Begin a class definition, as indicated by T. */
2758 tree
2759 begin_class_definition (tree t)
2761 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2762 return error_mark_node;
2764 if (processing_template_parmlist)
2766 error ("definition of %q#T inside template parameter list", t);
2767 return error_mark_node;
2770 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2771 are passed the same as decimal scalar types. */
2772 if (TREE_CODE (t) == RECORD_TYPE
2773 && !processing_template_decl)
2775 tree ns = TYPE_CONTEXT (t);
2776 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2777 && DECL_CONTEXT (ns) == std_node
2778 && DECL_NAME (ns)
2779 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2781 const char *n = TYPE_NAME_STRING (t);
2782 if ((strcmp (n, "decimal32") == 0)
2783 || (strcmp (n, "decimal64") == 0)
2784 || (strcmp (n, "decimal128") == 0))
2785 TYPE_TRANSPARENT_AGGR (t) = 1;
2789 /* A non-implicit typename comes from code like:
2791 template <typename T> struct A {
2792 template <typename U> struct A<T>::B ...
2794 This is erroneous. */
2795 else if (TREE_CODE (t) == TYPENAME_TYPE)
2797 error ("invalid definition of qualified type %qT", t);
2798 t = error_mark_node;
2801 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2803 t = make_class_type (RECORD_TYPE);
2804 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2807 if (TYPE_BEING_DEFINED (t))
2809 t = make_class_type (TREE_CODE (t));
2810 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2812 maybe_process_partial_specialization (t);
2813 pushclass (t);
2814 TYPE_BEING_DEFINED (t) = 1;
2815 class_binding_level->defining_class_p = 1;
2817 if (flag_pack_struct)
2819 tree v;
2820 TYPE_PACKED (t) = 1;
2821 /* Even though the type is being defined for the first time
2822 here, there might have been a forward declaration, so there
2823 might be cv-qualified variants of T. */
2824 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2825 TYPE_PACKED (v) = 1;
2827 /* Reset the interface data, at the earliest possible
2828 moment, as it might have been set via a class foo;
2829 before. */
2830 if (! TYPE_ANONYMOUS_P (t))
2832 struct c_fileinfo *finfo = \
2833 get_fileinfo (LOCATION_FILE (input_location));
2834 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2835 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2836 (t, finfo->interface_unknown);
2838 reset_specialization();
2840 /* Make a declaration for this class in its own scope. */
2841 build_self_reference ();
2843 return t;
2846 /* Finish the member declaration given by DECL. */
2848 void
2849 finish_member_declaration (tree decl)
2851 if (decl == error_mark_node || decl == NULL_TREE)
2852 return;
2854 if (decl == void_type_node)
2855 /* The COMPONENT was a friend, not a member, and so there's
2856 nothing for us to do. */
2857 return;
2859 /* We should see only one DECL at a time. */
2860 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2862 /* Set up access control for DECL. */
2863 TREE_PRIVATE (decl)
2864 = (current_access_specifier == access_private_node);
2865 TREE_PROTECTED (decl)
2866 = (current_access_specifier == access_protected_node);
2867 if (TREE_CODE (decl) == TEMPLATE_DECL)
2869 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2870 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2873 /* Mark the DECL as a member of the current class, unless it's
2874 a member of an enumeration. */
2875 if (TREE_CODE (decl) != CONST_DECL)
2876 DECL_CONTEXT (decl) = current_class_type;
2878 /* Check for bare parameter packs in the member variable declaration. */
2879 if (TREE_CODE (decl) == FIELD_DECL)
2881 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2882 TREE_TYPE (decl) = error_mark_node;
2883 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2884 DECL_ATTRIBUTES (decl) = NULL_TREE;
2887 /* [dcl.link]
2889 A C language linkage is ignored for the names of class members
2890 and the member function type of class member functions. */
2891 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2892 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2894 /* Put functions on the TYPE_METHODS list and everything else on the
2895 TYPE_FIELDS list. Note that these are built up in reverse order.
2896 We reverse them (to obtain declaration order) in finish_struct. */
2897 if (DECL_DECLARES_FUNCTION_P (decl))
2899 /* We also need to add this function to the
2900 CLASSTYPE_METHOD_VEC. */
2901 if (add_method (current_class_type, decl, NULL_TREE))
2903 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2904 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2905 TYPE_METHODS (current_class_type) = decl;
2907 maybe_add_class_template_decl_list (current_class_type, decl,
2908 /*friend_p=*/0);
2911 /* Enter the DECL into the scope of the class, if the class
2912 isn't a closure (whose fields are supposed to be unnamed). */
2913 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2914 || pushdecl_class_level (decl))
2916 if (TREE_CODE (decl) == USING_DECL)
2918 /* For now, ignore class-scope USING_DECLS, so that
2919 debugging backends do not see them. */
2920 DECL_IGNORED_P (decl) = 1;
2923 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2924 go at the beginning. The reason is that lookup_field_1
2925 searches the list in order, and we want a field name to
2926 override a type name so that the "struct stat hack" will
2927 work. In particular:
2929 struct S { enum E { }; int E } s;
2930 s.E = 3;
2932 is valid. In addition, the FIELD_DECLs must be maintained in
2933 declaration order so that class layout works as expected.
2934 However, we don't need that order until class layout, so we
2935 save a little time by putting FIELD_DECLs on in reverse order
2936 here, and then reversing them in finish_struct_1. (We could
2937 also keep a pointer to the correct insertion points in the
2938 list.) */
2940 if (TREE_CODE (decl) == TYPE_DECL)
2941 TYPE_FIELDS (current_class_type)
2942 = chainon (TYPE_FIELDS (current_class_type), decl);
2943 else
2945 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2946 TYPE_FIELDS (current_class_type) = decl;
2949 maybe_add_class_template_decl_list (current_class_type, decl,
2950 /*friend_p=*/0);
2953 if (pch_file)
2954 note_decl_for_pch (decl);
2957 /* DECL has been declared while we are building a PCH file. Perform
2958 actions that we might normally undertake lazily, but which can be
2959 performed now so that they do not have to be performed in
2960 translation units which include the PCH file. */
2962 void
2963 note_decl_for_pch (tree decl)
2965 gcc_assert (pch_file);
2967 /* There's a good chance that we'll have to mangle names at some
2968 point, even if only for emission in debugging information. */
2969 if (VAR_OR_FUNCTION_DECL_P (decl)
2970 && !processing_template_decl)
2971 mangle_decl (decl);
2974 /* Finish processing a complete template declaration. The PARMS are
2975 the template parameters. */
2977 void
2978 finish_template_decl (tree parms)
2980 if (parms)
2981 end_template_decl ();
2982 else
2983 end_specialization ();
2986 /* Finish processing a template-id (which names a type) of the form
2987 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2988 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2989 the scope of template-id indicated. */
2991 tree
2992 finish_template_type (tree name, tree args, int entering_scope)
2994 tree type;
2996 type = lookup_template_class (name, args,
2997 NULL_TREE, NULL_TREE, entering_scope,
2998 tf_warning_or_error | tf_user);
2999 if (type == error_mark_node)
3000 return type;
3001 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3002 return TYPE_STUB_DECL (type);
3003 else
3004 return TYPE_NAME (type);
3007 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3008 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3009 BASE_CLASS, or NULL_TREE if an error occurred. The
3010 ACCESS_SPECIFIER is one of
3011 access_{default,public,protected_private}_node. For a virtual base
3012 we set TREE_TYPE. */
3014 tree
3015 finish_base_specifier (tree base, tree access, bool virtual_p)
3017 tree result;
3019 if (base == error_mark_node)
3021 error ("invalid base-class specification");
3022 result = NULL_TREE;
3024 else if (! MAYBE_CLASS_TYPE_P (base))
3026 error ("%qT is not a class type", base);
3027 result = NULL_TREE;
3029 else
3031 if (cp_type_quals (base) != 0)
3033 /* DR 484: Can a base-specifier name a cv-qualified
3034 class type? */
3035 base = TYPE_MAIN_VARIANT (base);
3037 result = build_tree_list (access, base);
3038 if (virtual_p)
3039 TREE_TYPE (result) = integer_type_node;
3042 return result;
3045 /* If FNS is a member function, a set of member functions, or a
3046 template-id referring to one or more member functions, return a
3047 BASELINK for FNS, incorporating the current access context.
3048 Otherwise, return FNS unchanged. */
3050 tree
3051 baselink_for_fns (tree fns)
3053 tree scope;
3054 tree cl;
3056 if (BASELINK_P (fns)
3057 || error_operand_p (fns))
3058 return fns;
3060 scope = ovl_scope (fns);
3061 if (!CLASS_TYPE_P (scope))
3062 return fns;
3064 cl = currently_open_derived_class (scope);
3065 if (!cl)
3066 cl = scope;
3067 cl = TYPE_BINFO (cl);
3068 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3071 /* Returns true iff DECL is a variable from a function outside
3072 the current one. */
3074 static bool
3075 outer_var_p (tree decl)
3077 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3078 && DECL_FUNCTION_SCOPE_P (decl)
3079 && (DECL_CONTEXT (decl) != current_function_decl
3080 || parsing_nsdmi ()));
3083 /* As above, but also checks that DECL is automatic. */
3085 bool
3086 outer_automatic_var_p (tree decl)
3088 return (outer_var_p (decl)
3089 && !TREE_STATIC (decl));
3092 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3093 rewrite it for lambda capture. */
3095 tree
3096 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3098 if (cp_unevaluated_operand)
3099 /* It's not a use (3.2) if we're in an unevaluated context. */
3100 return decl;
3101 if (decl == error_mark_node)
3102 return decl;
3104 tree context = DECL_CONTEXT (decl);
3105 tree containing_function = current_function_decl;
3106 tree lambda_stack = NULL_TREE;
3107 tree lambda_expr = NULL_TREE;
3108 tree initializer = convert_from_reference (decl);
3110 /* Mark it as used now even if the use is ill-formed. */
3111 if (!mark_used (decl, complain) && !(complain & tf_error))
3112 return error_mark_node;
3114 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3115 support for an approach in which a reference to a local
3116 [constant] automatic variable in a nested class or lambda body
3117 would enter the expression as an rvalue, which would reduce
3118 the complexity of the problem"
3120 FIXME update for final resolution of core issue 696. */
3121 if (decl_maybe_constant_var_p (decl))
3123 if (processing_template_decl)
3124 /* In a template, the constant value may not be in a usable
3125 form, so wait until instantiation time. */
3126 return decl;
3127 else if (decl_constant_var_p (decl))
3129 tree t = maybe_constant_value (convert_from_reference (decl));
3130 if (TREE_CONSTANT (t))
3131 return t;
3135 if (parsing_nsdmi ())
3136 containing_function = NULL_TREE;
3137 else
3138 /* If we are in a lambda function, we can move out until we hit
3139 1. the context,
3140 2. a non-lambda function, or
3141 3. a non-default capturing lambda function. */
3142 while (context != containing_function
3143 && LAMBDA_FUNCTION_P (containing_function))
3145 tree closure = DECL_CONTEXT (containing_function);
3146 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3148 if (TYPE_CLASS_SCOPE_P (closure))
3149 /* A lambda in an NSDMI (c++/64496). */
3150 break;
3152 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3153 == CPLD_NONE)
3154 break;
3156 lambda_stack = tree_cons (NULL_TREE,
3157 lambda_expr,
3158 lambda_stack);
3160 containing_function
3161 = decl_function_context (containing_function);
3164 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3165 && DECL_ANON_UNION_VAR_P (decl))
3167 if (complain & tf_error)
3168 error ("cannot capture member %qD of anonymous union", decl);
3169 return error_mark_node;
3171 if (context == containing_function)
3173 decl = add_default_capture (lambda_stack,
3174 /*id=*/DECL_NAME (decl),
3175 initializer);
3177 else if (lambda_expr)
3179 if (complain & tf_error)
3181 error ("%qD is not captured", decl);
3182 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3183 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3184 == CPLD_NONE)
3185 inform (location_of (closure),
3186 "the lambda has no capture-default");
3187 else if (TYPE_CLASS_SCOPE_P (closure))
3188 inform (0, "lambda in local class %q+T cannot "
3189 "capture variables from the enclosing context",
3190 TYPE_CONTEXT (closure));
3191 inform (input_location, "%q+#D declared here", decl);
3193 return error_mark_node;
3195 else
3197 if (complain & tf_error)
3198 error (VAR_P (decl)
3199 ? G_("use of local variable with automatic storage from containing function")
3200 : G_("use of parameter from containing function"));
3201 inform (input_location, "%q+#D declared here", decl);
3202 return error_mark_node;
3204 return decl;
3207 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3208 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3209 if non-NULL, is the type or namespace used to explicitly qualify
3210 ID_EXPRESSION. DECL is the entity to which that name has been
3211 resolved.
3213 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3214 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3215 be set to true if this expression isn't permitted in a
3216 constant-expression, but it is otherwise not set by this function.
3217 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3218 constant-expression, but a non-constant expression is also
3219 permissible.
3221 DONE is true if this expression is a complete postfix-expression;
3222 it is false if this expression is followed by '->', '[', '(', etc.
3223 ADDRESS_P is true iff this expression is the operand of '&'.
3224 TEMPLATE_P is true iff the qualified-id was of the form
3225 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3226 appears as a template argument.
3228 If an error occurs, and it is the kind of error that might cause
3229 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3230 is the caller's responsibility to issue the message. *ERROR_MSG
3231 will be a string with static storage duration, so the caller need
3232 not "free" it.
3234 Return an expression for the entity, after issuing appropriate
3235 diagnostics. This function is also responsible for transforming a
3236 reference to a non-static member into a COMPONENT_REF that makes
3237 the use of "this" explicit.
3239 Upon return, *IDK will be filled in appropriately. */
3240 tree
3241 finish_id_expression (tree id_expression,
3242 tree decl,
3243 tree scope,
3244 cp_id_kind *idk,
3245 bool integral_constant_expression_p,
3246 bool allow_non_integral_constant_expression_p,
3247 bool *non_integral_constant_expression_p,
3248 bool template_p,
3249 bool done,
3250 bool address_p,
3251 bool template_arg_p,
3252 const char **error_msg,
3253 location_t location)
3255 decl = strip_using_decl (decl);
3257 /* Initialize the output parameters. */
3258 *idk = CP_ID_KIND_NONE;
3259 *error_msg = NULL;
3261 if (id_expression == error_mark_node)
3262 return error_mark_node;
3263 /* If we have a template-id, then no further lookup is
3264 required. If the template-id was for a template-class, we
3265 will sometimes have a TYPE_DECL at this point. */
3266 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3267 || TREE_CODE (decl) == TYPE_DECL)
3269 /* Look up the name. */
3270 else
3272 if (decl == error_mark_node)
3274 /* Name lookup failed. */
3275 if (scope
3276 && (!TYPE_P (scope)
3277 || (!dependent_type_p (scope)
3278 && !(identifier_p (id_expression)
3279 && IDENTIFIER_TYPENAME_P (id_expression)
3280 && dependent_type_p (TREE_TYPE (id_expression))))))
3282 /* If the qualifying type is non-dependent (and the name
3283 does not name a conversion operator to a dependent
3284 type), issue an error. */
3285 qualified_name_lookup_error (scope, id_expression, decl, location);
3286 return error_mark_node;
3288 else if (!scope)
3290 /* It may be resolved via Koenig lookup. */
3291 *idk = CP_ID_KIND_UNQUALIFIED;
3292 return id_expression;
3294 else
3295 decl = id_expression;
3297 /* If DECL is a variable that would be out of scope under
3298 ANSI/ISO rules, but in scope in the ARM, name lookup
3299 will succeed. Issue a diagnostic here. */
3300 else
3301 decl = check_for_out_of_scope_variable (decl);
3303 /* Remember that the name was used in the definition of
3304 the current class so that we can check later to see if
3305 the meaning would have been different after the class
3306 was entirely defined. */
3307 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3308 maybe_note_name_used_in_class (id_expression, decl);
3310 /* Disallow uses of local variables from containing functions, except
3311 within lambda-expressions. */
3312 if (outer_automatic_var_p (decl))
3314 decl = process_outer_var_ref (decl, tf_warning_or_error);
3315 if (decl == error_mark_node)
3316 return error_mark_node;
3319 /* Also disallow uses of function parameters outside the function
3320 body, except inside an unevaluated context (i.e. decltype). */
3321 if (TREE_CODE (decl) == PARM_DECL
3322 && DECL_CONTEXT (decl) == NULL_TREE
3323 && !cp_unevaluated_operand)
3325 *error_msg = "use of parameter outside function body";
3326 return error_mark_node;
3330 /* If we didn't find anything, or what we found was a type,
3331 then this wasn't really an id-expression. */
3332 if (TREE_CODE (decl) == TEMPLATE_DECL
3333 && !DECL_FUNCTION_TEMPLATE_P (decl))
3335 *error_msg = "missing template arguments";
3336 return error_mark_node;
3338 else if (TREE_CODE (decl) == TYPE_DECL
3339 || TREE_CODE (decl) == NAMESPACE_DECL)
3341 *error_msg = "expected primary-expression";
3342 return error_mark_node;
3345 /* If the name resolved to a template parameter, there is no
3346 need to look it up again later. */
3347 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3348 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3350 tree r;
3352 *idk = CP_ID_KIND_NONE;
3353 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3354 decl = TEMPLATE_PARM_DECL (decl);
3355 r = convert_from_reference (DECL_INITIAL (decl));
3357 if (integral_constant_expression_p
3358 && !dependent_type_p (TREE_TYPE (decl))
3359 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3361 if (!allow_non_integral_constant_expression_p)
3362 error ("template parameter %qD of type %qT is not allowed in "
3363 "an integral constant expression because it is not of "
3364 "integral or enumeration type", decl, TREE_TYPE (decl));
3365 *non_integral_constant_expression_p = true;
3367 return r;
3369 else
3371 bool dependent_p;
3373 /* If the declaration was explicitly qualified indicate
3374 that. The semantics of `A::f(3)' are different than
3375 `f(3)' if `f' is virtual. */
3376 *idk = (scope
3377 ? CP_ID_KIND_QUALIFIED
3378 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3379 ? CP_ID_KIND_TEMPLATE_ID
3380 : CP_ID_KIND_UNQUALIFIED));
3383 /* [temp.dep.expr]
3385 An id-expression is type-dependent if it contains an
3386 identifier that was declared with a dependent type.
3388 The standard is not very specific about an id-expression that
3389 names a set of overloaded functions. What if some of them
3390 have dependent types and some of them do not? Presumably,
3391 such a name should be treated as a dependent name. */
3392 /* Assume the name is not dependent. */
3393 dependent_p = false;
3394 if (!processing_template_decl)
3395 /* No names are dependent outside a template. */
3397 else if (TREE_CODE (decl) == CONST_DECL)
3398 /* We don't want to treat enumerators as dependent. */
3400 /* A template-id where the name of the template was not resolved
3401 is definitely dependent. */
3402 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3403 && (identifier_p (TREE_OPERAND (decl, 0))))
3404 dependent_p = true;
3405 /* For anything except an overloaded function, just check its
3406 type. */
3407 else if (!is_overloaded_fn (decl))
3408 dependent_p
3409 = dependent_type_p (TREE_TYPE (decl));
3410 /* For a set of overloaded functions, check each of the
3411 functions. */
3412 else
3414 tree fns = decl;
3416 if (BASELINK_P (fns))
3417 fns = BASELINK_FUNCTIONS (fns);
3419 /* For a template-id, check to see if the template
3420 arguments are dependent. */
3421 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3423 tree args = TREE_OPERAND (fns, 1);
3424 dependent_p = any_dependent_template_arguments_p (args);
3425 /* The functions are those referred to by the
3426 template-id. */
3427 fns = TREE_OPERAND (fns, 0);
3430 /* If there are no dependent template arguments, go through
3431 the overloaded functions. */
3432 while (fns && !dependent_p)
3434 tree fn = OVL_CURRENT (fns);
3436 /* Member functions of dependent classes are
3437 dependent. */
3438 if (TREE_CODE (fn) == FUNCTION_DECL
3439 && type_dependent_expression_p (fn))
3440 dependent_p = true;
3441 else if (TREE_CODE (fn) == TEMPLATE_DECL
3442 && dependent_template_p (fn))
3443 dependent_p = true;
3445 fns = OVL_NEXT (fns);
3449 /* If the name was dependent on a template parameter, we will
3450 resolve the name at instantiation time. */
3451 if (dependent_p)
3453 /* Create a SCOPE_REF for qualified names, if the scope is
3454 dependent. */
3455 if (scope)
3457 if (TYPE_P (scope))
3459 if (address_p && done)
3460 decl = finish_qualified_id_expr (scope, decl,
3461 done, address_p,
3462 template_p,
3463 template_arg_p,
3464 tf_warning_or_error);
3465 else
3467 tree type = NULL_TREE;
3468 if (DECL_P (decl) && !dependent_scope_p (scope))
3469 type = TREE_TYPE (decl);
3470 decl = build_qualified_name (type,
3471 scope,
3472 id_expression,
3473 template_p);
3476 if (TREE_TYPE (decl))
3477 decl = convert_from_reference (decl);
3478 return decl;
3480 /* A TEMPLATE_ID already contains all the information we
3481 need. */
3482 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3483 return id_expression;
3484 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3485 /* If we found a variable, then name lookup during the
3486 instantiation will always resolve to the same VAR_DECL
3487 (or an instantiation thereof). */
3488 if (VAR_P (decl)
3489 || TREE_CODE (decl) == PARM_DECL)
3491 mark_used (decl);
3492 return convert_from_reference (decl);
3494 /* The same is true for FIELD_DECL, but we also need to
3495 make sure that the syntax is correct. */
3496 else if (TREE_CODE (decl) == FIELD_DECL)
3498 /* Since SCOPE is NULL here, this is an unqualified name.
3499 Access checking has been performed during name lookup
3500 already. Turn off checking to avoid duplicate errors. */
3501 push_deferring_access_checks (dk_no_check);
3502 decl = finish_non_static_data_member
3503 (decl, NULL_TREE,
3504 /*qualifying_scope=*/NULL_TREE);
3505 pop_deferring_access_checks ();
3506 return decl;
3508 return id_expression;
3511 if (TREE_CODE (decl) == NAMESPACE_DECL)
3513 error ("use of namespace %qD as expression", decl);
3514 return error_mark_node;
3516 else if (DECL_CLASS_TEMPLATE_P (decl))
3518 error ("use of class template %qT as expression", decl);
3519 return error_mark_node;
3521 else if (TREE_CODE (decl) == TREE_LIST)
3523 /* Ambiguous reference to base members. */
3524 error ("request for member %qD is ambiguous in "
3525 "multiple inheritance lattice", id_expression);
3526 print_candidates (decl);
3527 return error_mark_node;
3530 /* Mark variable-like entities as used. Functions are similarly
3531 marked either below or after overload resolution. */
3532 if ((VAR_P (decl)
3533 || TREE_CODE (decl) == PARM_DECL
3534 || TREE_CODE (decl) == CONST_DECL
3535 || TREE_CODE (decl) == RESULT_DECL)
3536 && !mark_used (decl))
3537 return error_mark_node;
3539 /* Only certain kinds of names are allowed in constant
3540 expression. Template parameters have already
3541 been handled above. */
3542 if (! error_operand_p (decl)
3543 && integral_constant_expression_p
3544 && ! decl_constant_var_p (decl)
3545 && TREE_CODE (decl) != CONST_DECL
3546 && ! builtin_valid_in_constant_expr_p (decl))
3548 if (!allow_non_integral_constant_expression_p)
3550 error ("%qD cannot appear in a constant-expression", decl);
3551 return error_mark_node;
3553 *non_integral_constant_expression_p = true;
3556 tree wrap;
3557 if (VAR_P (decl)
3558 && !cp_unevaluated_operand
3559 && !processing_template_decl
3560 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3561 && DECL_THREAD_LOCAL_P (decl)
3562 && (wrap = get_tls_wrapper_fn (decl)))
3564 /* Replace an evaluated use of the thread_local variable with
3565 a call to its wrapper. */
3566 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3568 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3569 && variable_template_p (TREE_OPERAND (decl, 0)))
3571 decl = finish_template_variable (decl);
3572 mark_used (decl);
3574 else if (scope)
3576 decl = (adjust_result_of_qualified_name_lookup
3577 (decl, scope, current_nonlambda_class_type()));
3579 if (TREE_CODE (decl) == FUNCTION_DECL)
3580 mark_used (decl);
3582 if (TYPE_P (scope))
3583 decl = finish_qualified_id_expr (scope,
3584 decl,
3585 done,
3586 address_p,
3587 template_p,
3588 template_arg_p,
3589 tf_warning_or_error);
3590 else
3591 decl = convert_from_reference (decl);
3593 else if (TREE_CODE (decl) == FIELD_DECL)
3595 /* Since SCOPE is NULL here, this is an unqualified name.
3596 Access checking has been performed during name lookup
3597 already. Turn off checking to avoid duplicate errors. */
3598 push_deferring_access_checks (dk_no_check);
3599 decl = finish_non_static_data_member (decl, NULL_TREE,
3600 /*qualifying_scope=*/NULL_TREE);
3601 pop_deferring_access_checks ();
3603 else if (is_overloaded_fn (decl))
3605 tree first_fn;
3607 first_fn = get_first_fn (decl);
3608 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3609 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3611 if (!really_overloaded_fn (decl)
3612 && !mark_used (first_fn))
3613 return error_mark_node;
3615 if (!template_arg_p
3616 && TREE_CODE (first_fn) == FUNCTION_DECL
3617 && DECL_FUNCTION_MEMBER_P (first_fn)
3618 && !shared_member_p (decl))
3620 /* A set of member functions. */
3621 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3622 return finish_class_member_access_expr (decl, id_expression,
3623 /*template_p=*/false,
3624 tf_warning_or_error);
3627 decl = baselink_for_fns (decl);
3629 else
3631 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3632 && DECL_CLASS_SCOPE_P (decl))
3634 tree context = context_for_name_lookup (decl);
3635 if (context != current_class_type)
3637 tree path = currently_open_derived_class (context);
3638 perform_or_defer_access_check (TYPE_BINFO (path),
3639 decl, decl,
3640 tf_warning_or_error);
3644 decl = convert_from_reference (decl);
3648 return decl;
3651 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3652 use as a type-specifier. */
3654 tree
3655 finish_typeof (tree expr)
3657 tree type;
3659 if (type_dependent_expression_p (expr))
3661 type = cxx_make_type (TYPEOF_TYPE);
3662 TYPEOF_TYPE_EXPR (type) = expr;
3663 SET_TYPE_STRUCTURAL_EQUALITY (type);
3665 return type;
3668 expr = mark_type_use (expr);
3670 type = unlowered_expr_type (expr);
3672 if (!type || type == unknown_type_node)
3674 error ("type of %qE is unknown", expr);
3675 return error_mark_node;
3678 return type;
3681 /* Implement the __underlying_type keyword: Return the underlying
3682 type of TYPE, suitable for use as a type-specifier. */
3684 tree
3685 finish_underlying_type (tree type)
3687 tree underlying_type;
3689 if (processing_template_decl)
3691 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3692 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3693 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3695 return underlying_type;
3698 complete_type (type);
3700 if (TREE_CODE (type) != ENUMERAL_TYPE)
3702 error ("%qT is not an enumeration type", type);
3703 return error_mark_node;
3706 underlying_type = ENUM_UNDERLYING_TYPE (type);
3708 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3709 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3710 See finish_enum_value_list for details. */
3711 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3712 underlying_type
3713 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3714 TYPE_UNSIGNED (underlying_type));
3716 return underlying_type;
3719 /* Implement the __direct_bases keyword: Return the direct base classes
3720 of type */
3722 tree
3723 calculate_direct_bases (tree type)
3725 vec<tree, va_gc> *vector = make_tree_vector();
3726 tree bases_vec = NULL_TREE;
3727 vec<tree, va_gc> *base_binfos;
3728 tree binfo;
3729 unsigned i;
3731 complete_type (type);
3733 if (!NON_UNION_CLASS_TYPE_P (type))
3734 return make_tree_vec (0);
3736 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3738 /* Virtual bases are initialized first */
3739 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3741 if (BINFO_VIRTUAL_P (binfo))
3743 vec_safe_push (vector, binfo);
3747 /* Now non-virtuals */
3748 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3750 if (!BINFO_VIRTUAL_P (binfo))
3752 vec_safe_push (vector, binfo);
3757 bases_vec = make_tree_vec (vector->length ());
3759 for (i = 0; i < vector->length (); ++i)
3761 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3763 return bases_vec;
3766 /* Implement the __bases keyword: Return the base classes
3767 of type */
3769 /* Find morally non-virtual base classes by walking binfo hierarchy */
3770 /* Virtual base classes are handled separately in finish_bases */
3772 static tree
3773 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3775 /* Don't walk bases of virtual bases */
3776 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3779 static tree
3780 dfs_calculate_bases_post (tree binfo, void *data_)
3782 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3783 if (!BINFO_VIRTUAL_P (binfo))
3785 vec_safe_push (*data, BINFO_TYPE (binfo));
3787 return NULL_TREE;
3790 /* Calculates the morally non-virtual base classes of a class */
3791 static vec<tree, va_gc> *
3792 calculate_bases_helper (tree type)
3794 vec<tree, va_gc> *vector = make_tree_vector();
3796 /* Now add non-virtual base classes in order of construction */
3797 dfs_walk_all (TYPE_BINFO (type),
3798 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3799 return vector;
3802 tree
3803 calculate_bases (tree type)
3805 vec<tree, va_gc> *vector = make_tree_vector();
3806 tree bases_vec = NULL_TREE;
3807 unsigned i;
3808 vec<tree, va_gc> *vbases;
3809 vec<tree, va_gc> *nonvbases;
3810 tree binfo;
3812 complete_type (type);
3814 if (!NON_UNION_CLASS_TYPE_P (type))
3815 return make_tree_vec (0);
3817 /* First go through virtual base classes */
3818 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3819 vec_safe_iterate (vbases, i, &binfo); i++)
3821 vec<tree, va_gc> *vbase_bases;
3822 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3823 vec_safe_splice (vector, vbase_bases);
3824 release_tree_vector (vbase_bases);
3827 /* Now for the non-virtual bases */
3828 nonvbases = calculate_bases_helper (type);
3829 vec_safe_splice (vector, nonvbases);
3830 release_tree_vector (nonvbases);
3832 /* Last element is entire class, so don't copy */
3833 bases_vec = make_tree_vec (vector->length () - 1);
3835 for (i = 0; i < vector->length () - 1; ++i)
3837 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3839 release_tree_vector (vector);
3840 return bases_vec;
3843 tree
3844 finish_bases (tree type, bool direct)
3846 tree bases = NULL_TREE;
3848 if (!processing_template_decl)
3850 /* Parameter packs can only be used in templates */
3851 error ("Parameter pack __bases only valid in template declaration");
3852 return error_mark_node;
3855 bases = cxx_make_type (BASES);
3856 BASES_TYPE (bases) = type;
3857 BASES_DIRECT (bases) = direct;
3858 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3860 return bases;
3863 /* Perform C++-specific checks for __builtin_offsetof before calling
3864 fold_offsetof. */
3866 tree
3867 finish_offsetof (tree expr, location_t loc)
3869 /* If we're processing a template, we can't finish the semantics yet.
3870 Otherwise we can fold the entire expression now. */
3871 if (processing_template_decl)
3873 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3874 SET_EXPR_LOCATION (expr, loc);
3875 return expr;
3878 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3880 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3881 TREE_OPERAND (expr, 2));
3882 return error_mark_node;
3884 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3885 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3886 || TREE_TYPE (expr) == unknown_type_node)
3888 if (INDIRECT_REF_P (expr))
3889 error ("second operand of %<offsetof%> is neither a single "
3890 "identifier nor a sequence of member accesses and "
3891 "array references");
3892 else
3894 if (TREE_CODE (expr) == COMPONENT_REF
3895 || TREE_CODE (expr) == COMPOUND_EXPR)
3896 expr = TREE_OPERAND (expr, 1);
3897 error ("cannot apply %<offsetof%> to member function %qD", expr);
3899 return error_mark_node;
3901 if (REFERENCE_REF_P (expr))
3902 expr = TREE_OPERAND (expr, 0);
3903 if (TREE_CODE (expr) == COMPONENT_REF)
3905 tree object = TREE_OPERAND (expr, 0);
3906 if (!complete_type_or_else (TREE_TYPE (object), object))
3907 return error_mark_node;
3908 if (warn_invalid_offsetof
3909 && CLASS_TYPE_P (TREE_TYPE (object))
3910 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3911 && cp_unevaluated_operand == 0)
3912 pedwarn (loc, OPT_Winvalid_offsetof,
3913 "offsetof within non-standard-layout type %qT is undefined",
3914 TREE_TYPE (object));
3916 return fold_offsetof (expr);
3919 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3920 function is broken out from the above for the benefit of the tree-ssa
3921 project. */
3923 void
3924 simplify_aggr_init_expr (tree *tp)
3926 tree aggr_init_expr = *tp;
3928 /* Form an appropriate CALL_EXPR. */
3929 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3930 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3931 tree type = TREE_TYPE (slot);
3933 tree call_expr;
3934 enum style_t { ctor, arg, pcc } style;
3936 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3937 style = ctor;
3938 #ifdef PCC_STATIC_STRUCT_RETURN
3939 else if (1)
3940 style = pcc;
3941 #endif
3942 else
3944 gcc_assert (TREE_ADDRESSABLE (type));
3945 style = arg;
3948 call_expr = build_call_array_loc (input_location,
3949 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3951 aggr_init_expr_nargs (aggr_init_expr),
3952 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3953 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3954 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3956 if (style == ctor)
3958 /* Replace the first argument to the ctor with the address of the
3959 slot. */
3960 cxx_mark_addressable (slot);
3961 CALL_EXPR_ARG (call_expr, 0) =
3962 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3964 else if (style == arg)
3966 /* Just mark it addressable here, and leave the rest to
3967 expand_call{,_inline}. */
3968 cxx_mark_addressable (slot);
3969 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3970 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3972 else if (style == pcc)
3974 /* If we're using the non-reentrant PCC calling convention, then we
3975 need to copy the returned value out of the static buffer into the
3976 SLOT. */
3977 push_deferring_access_checks (dk_no_check);
3978 call_expr = build_aggr_init (slot, call_expr,
3979 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3980 tf_warning_or_error);
3981 pop_deferring_access_checks ();
3982 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3985 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3987 tree init = build_zero_init (type, NULL_TREE,
3988 /*static_storage_p=*/false);
3989 init = build2 (INIT_EXPR, void_type_node, slot, init);
3990 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3991 init, call_expr);
3994 *tp = call_expr;
3997 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3999 void
4000 emit_associated_thunks (tree fn)
4002 /* When we use vcall offsets, we emit thunks with the virtual
4003 functions to which they thunk. The whole point of vcall offsets
4004 is so that you can know statically the entire set of thunks that
4005 will ever be needed for a given virtual function, thereby
4006 enabling you to output all the thunks with the function itself. */
4007 if (DECL_VIRTUAL_P (fn)
4008 /* Do not emit thunks for extern template instantiations. */
4009 && ! DECL_REALLY_EXTERN (fn))
4011 tree thunk;
4013 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4015 if (!THUNK_ALIAS (thunk))
4017 use_thunk (thunk, /*emit_p=*/1);
4018 if (DECL_RESULT_THUNK_P (thunk))
4020 tree probe;
4022 for (probe = DECL_THUNKS (thunk);
4023 probe; probe = DECL_CHAIN (probe))
4024 use_thunk (probe, /*emit_p=*/1);
4027 else
4028 gcc_assert (!DECL_THUNKS (thunk));
4033 /* Generate RTL for FN. */
4035 bool
4036 expand_or_defer_fn_1 (tree fn)
4038 /* When the parser calls us after finishing the body of a template
4039 function, we don't really want to expand the body. */
4040 if (processing_template_decl)
4042 /* Normally, collection only occurs in rest_of_compilation. So,
4043 if we don't collect here, we never collect junk generated
4044 during the processing of templates until we hit a
4045 non-template function. It's not safe to do this inside a
4046 nested class, though, as the parser may have local state that
4047 is not a GC root. */
4048 if (!function_depth)
4049 ggc_collect ();
4050 return false;
4053 gcc_assert (DECL_SAVED_TREE (fn));
4055 /* We make a decision about linkage for these functions at the end
4056 of the compilation. Until that point, we do not want the back
4057 end to output them -- but we do want it to see the bodies of
4058 these functions so that it can inline them as appropriate. */
4059 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4061 if (DECL_INTERFACE_KNOWN (fn))
4062 /* We've already made a decision as to how this function will
4063 be handled. */;
4064 else if (!at_eof)
4065 tentative_decl_linkage (fn);
4066 else
4067 import_export_decl (fn);
4069 /* If the user wants us to keep all inline functions, then mark
4070 this function as needed so that finish_file will make sure to
4071 output it later. Similarly, all dllexport'd functions must
4072 be emitted; there may be callers in other DLLs. */
4073 if (DECL_DECLARED_INLINE_P (fn)
4074 && !DECL_REALLY_EXTERN (fn)
4075 && (flag_keep_inline_functions
4076 || (flag_keep_inline_dllexport
4077 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4079 mark_needed (fn);
4080 DECL_EXTERNAL (fn) = 0;
4084 /* If this is a constructor or destructor body, we have to clone
4085 it. */
4086 if (maybe_clone_body (fn))
4088 /* We don't want to process FN again, so pretend we've written
4089 it out, even though we haven't. */
4090 TREE_ASM_WRITTEN (fn) = 1;
4091 /* If this is an instantiation of a constexpr function, keep
4092 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4093 if (!is_instantiation_of_constexpr (fn))
4094 DECL_SAVED_TREE (fn) = NULL_TREE;
4095 return false;
4098 /* There's no reason to do any of the work here if we're only doing
4099 semantic analysis; this code just generates RTL. */
4100 if (flag_syntax_only)
4101 return false;
4103 return true;
4106 void
4107 expand_or_defer_fn (tree fn)
4109 if (expand_or_defer_fn_1 (fn))
4111 function_depth++;
4113 /* Expand or defer, at the whim of the compilation unit manager. */
4114 cgraph_node::finalize_function (fn, function_depth > 1);
4115 emit_associated_thunks (fn);
4117 function_depth--;
4121 struct nrv_data
4123 nrv_data () : visited (37) {}
4125 tree var;
4126 tree result;
4127 hash_table<nofree_ptr_hash <tree_node> > visited;
4130 /* Helper function for walk_tree, used by finalize_nrv below. */
4132 static tree
4133 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4135 struct nrv_data *dp = (struct nrv_data *)data;
4136 tree_node **slot;
4138 /* No need to walk into types. There wouldn't be any need to walk into
4139 non-statements, except that we have to consider STMT_EXPRs. */
4140 if (TYPE_P (*tp))
4141 *walk_subtrees = 0;
4142 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4143 but differs from using NULL_TREE in that it indicates that we care
4144 about the value of the RESULT_DECL. */
4145 else if (TREE_CODE (*tp) == RETURN_EXPR)
4146 TREE_OPERAND (*tp, 0) = dp->result;
4147 /* Change all cleanups for the NRV to only run when an exception is
4148 thrown. */
4149 else if (TREE_CODE (*tp) == CLEANUP_STMT
4150 && CLEANUP_DECL (*tp) == dp->var)
4151 CLEANUP_EH_ONLY (*tp) = 1;
4152 /* Replace the DECL_EXPR for the NRV with an initialization of the
4153 RESULT_DECL, if needed. */
4154 else if (TREE_CODE (*tp) == DECL_EXPR
4155 && DECL_EXPR_DECL (*tp) == dp->var)
4157 tree init;
4158 if (DECL_INITIAL (dp->var)
4159 && DECL_INITIAL (dp->var) != error_mark_node)
4160 init = build2 (INIT_EXPR, void_type_node, dp->result,
4161 DECL_INITIAL (dp->var));
4162 else
4163 init = build_empty_stmt (EXPR_LOCATION (*tp));
4164 DECL_INITIAL (dp->var) = NULL_TREE;
4165 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4166 *tp = init;
4168 /* And replace all uses of the NRV with the RESULT_DECL. */
4169 else if (*tp == dp->var)
4170 *tp = dp->result;
4172 /* Avoid walking into the same tree more than once. Unfortunately, we
4173 can't just use walk_tree_without duplicates because it would only call
4174 us for the first occurrence of dp->var in the function body. */
4175 slot = dp->visited.find_slot (*tp, INSERT);
4176 if (*slot)
4177 *walk_subtrees = 0;
4178 else
4179 *slot = *tp;
4181 /* Keep iterating. */
4182 return NULL_TREE;
4185 /* Called from finish_function to implement the named return value
4186 optimization by overriding all the RETURN_EXPRs and pertinent
4187 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4188 RESULT_DECL for the function. */
4190 void
4191 finalize_nrv (tree *tp, tree var, tree result)
4193 struct nrv_data data;
4195 /* Copy name from VAR to RESULT. */
4196 DECL_NAME (result) = DECL_NAME (var);
4197 /* Don't forget that we take its address. */
4198 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4199 /* Finally set DECL_VALUE_EXPR to avoid assigning
4200 a stack slot at -O0 for the original var and debug info
4201 uses RESULT location for VAR. */
4202 SET_DECL_VALUE_EXPR (var, result);
4203 DECL_HAS_VALUE_EXPR_P (var) = 1;
4205 data.var = var;
4206 data.result = result;
4207 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4210 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4212 bool
4213 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4214 bool need_copy_ctor, bool need_copy_assignment,
4215 bool need_dtor)
4217 int save_errorcount = errorcount;
4218 tree info, t;
4220 /* Always allocate 3 elements for simplicity. These are the
4221 function decls for the ctor, dtor, and assignment op.
4222 This layout is known to the three lang hooks,
4223 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4224 and cxx_omp_clause_assign_op. */
4225 info = make_tree_vec (3);
4226 CP_OMP_CLAUSE_INFO (c) = info;
4228 if (need_default_ctor || need_copy_ctor)
4230 if (need_default_ctor)
4231 t = get_default_ctor (type);
4232 else
4233 t = get_copy_ctor (type, tf_warning_or_error);
4235 if (t && !trivial_fn_p (t))
4236 TREE_VEC_ELT (info, 0) = t;
4239 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4240 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4242 if (need_copy_assignment)
4244 t = get_copy_assign (type);
4246 if (t && !trivial_fn_p (t))
4247 TREE_VEC_ELT (info, 2) = t;
4250 return errorcount != save_errorcount;
4253 /* Helper function for handle_omp_array_sections. Called recursively
4254 to handle multiple array-section-subscripts. C is the clause,
4255 T current expression (initially OMP_CLAUSE_DECL), which is either
4256 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4257 expression if specified, TREE_VALUE length expression if specified,
4258 TREE_CHAIN is what it has been specified after, or some decl.
4259 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4260 set to true if any of the array-section-subscript could have length
4261 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4262 first array-section-subscript which is known not to have length
4263 of one. Given say:
4264 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4265 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4266 all are or may have length of 1, array-section-subscript [:2] is the
4267 first one knonwn not to have length 1. For array-section-subscript
4268 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4269 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4270 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4271 case though, as some lengths could be zero. */
4273 static tree
4274 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4275 bool &maybe_zero_len, unsigned int &first_non_one)
4277 tree ret, low_bound, length, type;
4278 if (TREE_CODE (t) != TREE_LIST)
4280 if (error_operand_p (t))
4281 return error_mark_node;
4282 if (type_dependent_expression_p (t))
4283 return NULL_TREE;
4284 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4286 if (processing_template_decl)
4287 return NULL_TREE;
4288 if (DECL_P (t))
4289 error_at (OMP_CLAUSE_LOCATION (c),
4290 "%qD is not a variable in %qs clause", t,
4291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4292 else
4293 error_at (OMP_CLAUSE_LOCATION (c),
4294 "%qE is not a variable in %qs clause", t,
4295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4296 return error_mark_node;
4298 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4299 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4301 error_at (OMP_CLAUSE_LOCATION (c),
4302 "%qD is threadprivate variable in %qs clause", t,
4303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4304 return error_mark_node;
4306 t = convert_from_reference (t);
4307 return t;
4310 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4311 maybe_zero_len, first_non_one);
4312 if (ret == error_mark_node || ret == NULL_TREE)
4313 return ret;
4315 type = TREE_TYPE (ret);
4316 low_bound = TREE_PURPOSE (t);
4317 length = TREE_VALUE (t);
4318 if ((low_bound && type_dependent_expression_p (low_bound))
4319 || (length && type_dependent_expression_p (length)))
4320 return NULL_TREE;
4322 if (low_bound == error_mark_node || length == error_mark_node)
4323 return error_mark_node;
4325 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4327 error_at (OMP_CLAUSE_LOCATION (c),
4328 "low bound %qE of array section does not have integral type",
4329 low_bound);
4330 return error_mark_node;
4332 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4334 error_at (OMP_CLAUSE_LOCATION (c),
4335 "length %qE of array section does not have integral type",
4336 length);
4337 return error_mark_node;
4339 if (low_bound)
4340 low_bound = mark_rvalue_use (low_bound);
4341 if (length)
4342 length = mark_rvalue_use (length);
4343 if (low_bound
4344 && TREE_CODE (low_bound) == INTEGER_CST
4345 && TYPE_PRECISION (TREE_TYPE (low_bound))
4346 > TYPE_PRECISION (sizetype))
4347 low_bound = fold_convert (sizetype, low_bound);
4348 if (length
4349 && TREE_CODE (length) == INTEGER_CST
4350 && TYPE_PRECISION (TREE_TYPE (length))
4351 > TYPE_PRECISION (sizetype))
4352 length = fold_convert (sizetype, length);
4353 if (low_bound == NULL_TREE)
4354 low_bound = integer_zero_node;
4356 if (length != NULL_TREE)
4358 if (!integer_nonzerop (length))
4359 maybe_zero_len = true;
4360 if (first_non_one == types.length ()
4361 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4362 first_non_one++;
4364 if (TREE_CODE (type) == ARRAY_TYPE)
4366 if (length == NULL_TREE
4367 && (TYPE_DOMAIN (type) == NULL_TREE
4368 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4370 error_at (OMP_CLAUSE_LOCATION (c),
4371 "for unknown bound array type length expression must "
4372 "be specified");
4373 return error_mark_node;
4375 if (TREE_CODE (low_bound) == INTEGER_CST
4376 && tree_int_cst_sgn (low_bound) == -1)
4378 error_at (OMP_CLAUSE_LOCATION (c),
4379 "negative low bound in array section in %qs clause",
4380 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4381 return error_mark_node;
4383 if (length != NULL_TREE
4384 && TREE_CODE (length) == INTEGER_CST
4385 && tree_int_cst_sgn (length) == -1)
4387 error_at (OMP_CLAUSE_LOCATION (c),
4388 "negative length in array section in %qs clause",
4389 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4390 return error_mark_node;
4392 if (TYPE_DOMAIN (type)
4393 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4394 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4395 == INTEGER_CST)
4397 tree size = size_binop (PLUS_EXPR,
4398 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4399 size_one_node);
4400 if (TREE_CODE (low_bound) == INTEGER_CST)
4402 if (tree_int_cst_lt (size, low_bound))
4404 error_at (OMP_CLAUSE_LOCATION (c),
4405 "low bound %qE above array section size "
4406 "in %qs clause", low_bound,
4407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4408 return error_mark_node;
4410 if (tree_int_cst_equal (size, low_bound))
4411 maybe_zero_len = true;
4412 else if (length == NULL_TREE
4413 && first_non_one == types.length ()
4414 && tree_int_cst_equal
4415 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4416 low_bound))
4417 first_non_one++;
4419 else if (length == NULL_TREE)
4421 maybe_zero_len = true;
4422 if (first_non_one == types.length ())
4423 first_non_one++;
4425 if (length && TREE_CODE (length) == INTEGER_CST)
4427 if (tree_int_cst_lt (size, length))
4429 error_at (OMP_CLAUSE_LOCATION (c),
4430 "length %qE above array section size "
4431 "in %qs clause", length,
4432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4433 return error_mark_node;
4435 if (TREE_CODE (low_bound) == INTEGER_CST)
4437 tree lbpluslen
4438 = size_binop (PLUS_EXPR,
4439 fold_convert (sizetype, low_bound),
4440 fold_convert (sizetype, length));
4441 if (TREE_CODE (lbpluslen) == INTEGER_CST
4442 && tree_int_cst_lt (size, lbpluslen))
4444 error_at (OMP_CLAUSE_LOCATION (c),
4445 "high bound %qE above array section size "
4446 "in %qs clause", lbpluslen,
4447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4448 return error_mark_node;
4453 else if (length == NULL_TREE)
4455 maybe_zero_len = true;
4456 if (first_non_one == types.length ())
4457 first_non_one++;
4460 /* For [lb:] we will need to evaluate lb more than once. */
4461 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4463 tree lb = cp_save_expr (low_bound);
4464 if (lb != low_bound)
4466 TREE_PURPOSE (t) = lb;
4467 low_bound = lb;
4471 else if (TREE_CODE (type) == POINTER_TYPE)
4473 if (length == NULL_TREE)
4475 error_at (OMP_CLAUSE_LOCATION (c),
4476 "for pointer type length expression must be specified");
4477 return error_mark_node;
4479 /* If there is a pointer type anywhere but in the very first
4480 array-section-subscript, the array section can't be contiguous. */
4481 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4482 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4484 error_at (OMP_CLAUSE_LOCATION (c),
4485 "array section is not contiguous in %qs clause",
4486 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4487 return error_mark_node;
4490 else
4492 error_at (OMP_CLAUSE_LOCATION (c),
4493 "%qE does not have pointer or array type", ret);
4494 return error_mark_node;
4496 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4497 types.safe_push (TREE_TYPE (ret));
4498 /* We will need to evaluate lb more than once. */
4499 tree lb = cp_save_expr (low_bound);
4500 if (lb != low_bound)
4502 TREE_PURPOSE (t) = lb;
4503 low_bound = lb;
4505 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4506 return ret;
4509 /* Handle array sections for clause C. */
4511 static bool
4512 handle_omp_array_sections (tree c)
4514 bool maybe_zero_len = false;
4515 unsigned int first_non_one = 0;
4516 auto_vec<tree> types;
4517 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4518 maybe_zero_len, first_non_one);
4519 if (first == error_mark_node)
4520 return true;
4521 if (first == NULL_TREE)
4522 return false;
4523 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4525 tree t = OMP_CLAUSE_DECL (c);
4526 tree tem = NULL_TREE;
4527 if (processing_template_decl)
4528 return false;
4529 /* Need to evaluate side effects in the length expressions
4530 if any. */
4531 while (TREE_CODE (t) == TREE_LIST)
4533 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4535 if (tem == NULL_TREE)
4536 tem = TREE_VALUE (t);
4537 else
4538 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4539 TREE_VALUE (t), tem);
4541 t = TREE_CHAIN (t);
4543 if (tem)
4544 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4545 OMP_CLAUSE_DECL (c) = first;
4547 else
4549 unsigned int num = types.length (), i;
4550 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4551 tree condition = NULL_TREE;
4553 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4554 maybe_zero_len = true;
4555 if (processing_template_decl && maybe_zero_len)
4556 return false;
4558 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4559 t = TREE_CHAIN (t))
4561 tree low_bound = TREE_PURPOSE (t);
4562 tree length = TREE_VALUE (t);
4564 i--;
4565 if (low_bound
4566 && TREE_CODE (low_bound) == INTEGER_CST
4567 && TYPE_PRECISION (TREE_TYPE (low_bound))
4568 > TYPE_PRECISION (sizetype))
4569 low_bound = fold_convert (sizetype, low_bound);
4570 if (length
4571 && TREE_CODE (length) == INTEGER_CST
4572 && TYPE_PRECISION (TREE_TYPE (length))
4573 > TYPE_PRECISION (sizetype))
4574 length = fold_convert (sizetype, length);
4575 if (low_bound == NULL_TREE)
4576 low_bound = integer_zero_node;
4577 if (!maybe_zero_len && i > first_non_one)
4579 if (integer_nonzerop (low_bound))
4580 goto do_warn_noncontiguous;
4581 if (length != NULL_TREE
4582 && TREE_CODE (length) == INTEGER_CST
4583 && TYPE_DOMAIN (types[i])
4584 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4585 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4586 == INTEGER_CST)
4588 tree size;
4589 size = size_binop (PLUS_EXPR,
4590 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4591 size_one_node);
4592 if (!tree_int_cst_equal (length, size))
4594 do_warn_noncontiguous:
4595 error_at (OMP_CLAUSE_LOCATION (c),
4596 "array section is not contiguous in %qs "
4597 "clause",
4598 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4599 return true;
4602 if (!processing_template_decl
4603 && length != NULL_TREE
4604 && TREE_SIDE_EFFECTS (length))
4606 if (side_effects == NULL_TREE)
4607 side_effects = length;
4608 else
4609 side_effects = build2 (COMPOUND_EXPR,
4610 TREE_TYPE (side_effects),
4611 length, side_effects);
4614 else if (processing_template_decl)
4615 continue;
4616 else
4618 tree l;
4620 if (i > first_non_one && length && integer_nonzerop (length))
4621 continue;
4622 if (length)
4623 l = fold_convert (sizetype, length);
4624 else
4626 l = size_binop (PLUS_EXPR,
4627 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4628 size_one_node);
4629 l = size_binop (MINUS_EXPR, l,
4630 fold_convert (sizetype, low_bound));
4632 if (i > first_non_one)
4634 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4635 size_zero_node);
4636 if (condition == NULL_TREE)
4637 condition = l;
4638 else
4639 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4640 l, condition);
4642 else if (size == NULL_TREE)
4644 size = size_in_bytes (TREE_TYPE (types[i]));
4645 size = size_binop (MULT_EXPR, size, l);
4646 if (condition)
4647 size = fold_build3 (COND_EXPR, sizetype, condition,
4648 size, size_zero_node);
4650 else
4651 size = size_binop (MULT_EXPR, size, l);
4654 if (!processing_template_decl)
4656 if (side_effects)
4657 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4658 OMP_CLAUSE_DECL (c) = first;
4659 OMP_CLAUSE_SIZE (c) = size;
4660 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4661 return false;
4662 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4663 OMP_CLAUSE_MAP);
4664 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4665 if (!cxx_mark_addressable (t))
4666 return false;
4667 OMP_CLAUSE_DECL (c2) = t;
4668 t = build_fold_addr_expr (first);
4669 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4670 ptrdiff_type_node, t);
4671 tree ptr = OMP_CLAUSE_DECL (c2);
4672 ptr = convert_from_reference (ptr);
4673 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4674 ptr = build_fold_addr_expr (ptr);
4675 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4676 ptrdiff_type_node, t,
4677 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4678 ptrdiff_type_node, ptr));
4679 OMP_CLAUSE_SIZE (c2) = t;
4680 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4681 OMP_CLAUSE_CHAIN (c) = c2;
4682 ptr = OMP_CLAUSE_DECL (c2);
4683 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4684 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4686 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4687 OMP_CLAUSE_MAP);
4688 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4689 OMP_CLAUSE_DECL (c3) = ptr;
4690 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4691 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4692 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4693 OMP_CLAUSE_CHAIN (c2) = c3;
4697 return false;
4700 /* Return identifier to look up for omp declare reduction. */
4702 tree
4703 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4705 const char *p = NULL;
4706 const char *m = NULL;
4707 switch (reduction_code)
4709 case PLUS_EXPR:
4710 case MULT_EXPR:
4711 case MINUS_EXPR:
4712 case BIT_AND_EXPR:
4713 case BIT_XOR_EXPR:
4714 case BIT_IOR_EXPR:
4715 case TRUTH_ANDIF_EXPR:
4716 case TRUTH_ORIF_EXPR:
4717 reduction_id = ansi_opname (reduction_code);
4718 break;
4719 case MIN_EXPR:
4720 p = "min";
4721 break;
4722 case MAX_EXPR:
4723 p = "max";
4724 break;
4725 default:
4726 break;
4729 if (p == NULL)
4731 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4732 return error_mark_node;
4733 p = IDENTIFIER_POINTER (reduction_id);
4736 if (type != NULL_TREE)
4737 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4739 const char prefix[] = "omp declare reduction ";
4740 size_t lenp = sizeof (prefix);
4741 if (strncmp (p, prefix, lenp - 1) == 0)
4742 lenp = 1;
4743 size_t len = strlen (p);
4744 size_t lenm = m ? strlen (m) + 1 : 0;
4745 char *name = XALLOCAVEC (char, lenp + len + lenm);
4746 if (lenp > 1)
4747 memcpy (name, prefix, lenp - 1);
4748 memcpy (name + lenp - 1, p, len + 1);
4749 if (m)
4751 name[lenp + len - 1] = '~';
4752 memcpy (name + lenp + len, m, lenm);
4754 return get_identifier (name);
4757 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4758 FUNCTION_DECL or NULL_TREE if not found. */
4760 static tree
4761 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4762 vec<tree> *ambiguousp)
4764 tree orig_id = id;
4765 tree baselink = NULL_TREE;
4766 if (identifier_p (id))
4768 cp_id_kind idk;
4769 bool nonint_cst_expression_p;
4770 const char *error_msg;
4771 id = omp_reduction_id (ERROR_MARK, id, type);
4772 tree decl = lookup_name (id);
4773 if (decl == NULL_TREE)
4774 decl = error_mark_node;
4775 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4776 &nonint_cst_expression_p, false, true, false,
4777 false, &error_msg, loc);
4778 if (idk == CP_ID_KIND_UNQUALIFIED
4779 && identifier_p (id))
4781 vec<tree, va_gc> *args = NULL;
4782 vec_safe_push (args, build_reference_type (type));
4783 id = perform_koenig_lookup (id, args, tf_none);
4786 else if (TREE_CODE (id) == SCOPE_REF)
4787 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4788 omp_reduction_id (ERROR_MARK,
4789 TREE_OPERAND (id, 1),
4790 type),
4791 false, false);
4792 tree fns = id;
4793 if (id && is_overloaded_fn (id))
4794 id = get_fns (id);
4795 for (; id; id = OVL_NEXT (id))
4797 tree fndecl = OVL_CURRENT (id);
4798 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4800 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4801 if (same_type_p (TREE_TYPE (argtype), type))
4802 break;
4805 if (id && BASELINK_P (fns))
4807 if (baselinkp)
4808 *baselinkp = fns;
4809 else
4810 baselink = fns;
4812 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4814 vec<tree> ambiguous = vNULL;
4815 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4816 unsigned int ix;
4817 if (ambiguousp == NULL)
4818 ambiguousp = &ambiguous;
4819 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4821 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4822 baselinkp ? baselinkp : &baselink,
4823 ambiguousp);
4824 if (id == NULL_TREE)
4825 continue;
4826 if (!ambiguousp->is_empty ())
4827 ambiguousp->safe_push (id);
4828 else if (ret != NULL_TREE)
4830 ambiguousp->safe_push (ret);
4831 ambiguousp->safe_push (id);
4832 ret = NULL_TREE;
4834 else
4835 ret = id;
4837 if (ambiguousp != &ambiguous)
4838 return ret;
4839 if (!ambiguous.is_empty ())
4841 const char *str = _("candidates are:");
4842 unsigned int idx;
4843 tree udr;
4844 error_at (loc, "user defined reduction lookup is ambiguous");
4845 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4847 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4848 if (idx == 0)
4849 str = get_spaces (str);
4851 ambiguous.release ();
4852 ret = error_mark_node;
4853 baselink = NULL_TREE;
4855 id = ret;
4857 if (id && baselink)
4858 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4859 id, id, tf_warning_or_error);
4860 return id;
4863 /* Helper function for cp_parser_omp_declare_reduction_exprs
4864 and tsubst_omp_udr.
4865 Remove CLEANUP_STMT for data (omp_priv variable).
4866 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4867 DECL_EXPR. */
4869 tree
4870 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4872 if (TYPE_P (*tp))
4873 *walk_subtrees = 0;
4874 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4875 *tp = CLEANUP_BODY (*tp);
4876 else if (TREE_CODE (*tp) == DECL_EXPR)
4878 tree decl = DECL_EXPR_DECL (*tp);
4879 if (!processing_template_decl
4880 && decl == (tree) data
4881 && DECL_INITIAL (decl)
4882 && DECL_INITIAL (decl) != error_mark_node)
4884 tree list = NULL_TREE;
4885 append_to_statement_list_force (*tp, &list);
4886 tree init_expr = build2 (INIT_EXPR, void_type_node,
4887 decl, DECL_INITIAL (decl));
4888 DECL_INITIAL (decl) = NULL_TREE;
4889 append_to_statement_list_force (init_expr, &list);
4890 *tp = list;
4893 return NULL_TREE;
4896 /* Data passed from cp_check_omp_declare_reduction to
4897 cp_check_omp_declare_reduction_r. */
4899 struct cp_check_omp_declare_reduction_data
4901 location_t loc;
4902 tree stmts[7];
4903 bool combiner_p;
4906 /* Helper function for cp_check_omp_declare_reduction, called via
4907 cp_walk_tree. */
4909 static tree
4910 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4912 struct cp_check_omp_declare_reduction_data *udr_data
4913 = (struct cp_check_omp_declare_reduction_data *) data;
4914 if (SSA_VAR_P (*tp)
4915 && !DECL_ARTIFICIAL (*tp)
4916 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4917 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4919 location_t loc = udr_data->loc;
4920 if (udr_data->combiner_p)
4921 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4922 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4923 *tp);
4924 else
4925 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4926 "to variable %qD which is not %<omp_priv%> nor "
4927 "%<omp_orig%>",
4928 *tp);
4929 return *tp;
4931 return NULL_TREE;
4934 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4936 void
4937 cp_check_omp_declare_reduction (tree udr)
4939 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4940 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4941 type = TREE_TYPE (type);
4942 int i;
4943 location_t loc = DECL_SOURCE_LOCATION (udr);
4945 if (type == error_mark_node)
4946 return;
4947 if (ARITHMETIC_TYPE_P (type))
4949 static enum tree_code predef_codes[]
4950 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4951 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4952 for (i = 0; i < 8; i++)
4954 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4955 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4956 const char *n2 = IDENTIFIER_POINTER (id);
4957 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4958 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4959 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4960 break;
4963 if (i == 8
4964 && TREE_CODE (type) != COMPLEX_EXPR)
4966 const char prefix_minmax[] = "omp declare reduction m";
4967 size_t prefix_size = sizeof (prefix_minmax) - 1;
4968 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4969 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4970 prefix_minmax, prefix_size) == 0
4971 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4972 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4973 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4974 i = 0;
4976 if (i < 8)
4978 error_at (loc, "predeclared arithmetic type %qT in "
4979 "%<#pragma omp declare reduction%>", type);
4980 return;
4983 else if (TREE_CODE (type) == FUNCTION_TYPE
4984 || TREE_CODE (type) == METHOD_TYPE
4985 || TREE_CODE (type) == ARRAY_TYPE)
4987 error_at (loc, "function or array type %qT in "
4988 "%<#pragma omp declare reduction%>", type);
4989 return;
4991 else if (TREE_CODE (type) == REFERENCE_TYPE)
4993 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4994 type);
4995 return;
4997 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4999 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5000 "%<#pragma omp declare reduction%>", type);
5001 return;
5004 tree body = DECL_SAVED_TREE (udr);
5005 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5006 return;
5008 tree_stmt_iterator tsi;
5009 struct cp_check_omp_declare_reduction_data data;
5010 memset (data.stmts, 0, sizeof data.stmts);
5011 for (i = 0, tsi = tsi_start (body);
5012 i < 7 && !tsi_end_p (tsi);
5013 i++, tsi_next (&tsi))
5014 data.stmts[i] = tsi_stmt (tsi);
5015 data.loc = loc;
5016 gcc_assert (tsi_end_p (tsi));
5017 if (i >= 3)
5019 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5020 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5021 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5022 return;
5023 data.combiner_p = true;
5024 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5025 &data, NULL))
5026 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5028 if (i >= 6)
5030 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5031 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5032 data.combiner_p = false;
5033 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5034 &data, NULL)
5035 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5036 cp_check_omp_declare_reduction_r, &data, NULL))
5037 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5038 if (i == 7)
5039 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5043 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5044 an inline call. But, remap
5045 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5046 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5048 static tree
5049 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5050 tree decl, tree placeholder)
5052 copy_body_data id;
5053 hash_map<tree, tree> decl_map;
5055 decl_map.put (omp_decl1, placeholder);
5056 decl_map.put (omp_decl2, decl);
5057 memset (&id, 0, sizeof (id));
5058 id.src_fn = DECL_CONTEXT (omp_decl1);
5059 id.dst_fn = current_function_decl;
5060 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5061 id.decl_map = &decl_map;
5063 id.copy_decl = copy_decl_no_change;
5064 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5065 id.transform_new_cfg = true;
5066 id.transform_return_to_modify = false;
5067 id.transform_lang_insert_block = NULL;
5068 id.eh_lp_nr = 0;
5069 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5070 return stmt;
5073 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5074 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5076 static tree
5077 find_omp_placeholder_r (tree *tp, int *, void *data)
5079 if (*tp == (tree) data)
5080 return *tp;
5081 return NULL_TREE;
5084 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5085 Return true if there is some error and the clause should be removed. */
5087 static bool
5088 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5090 tree t = OMP_CLAUSE_DECL (c);
5091 bool predefined = false;
5092 tree type = TREE_TYPE (t);
5093 if (TREE_CODE (type) == REFERENCE_TYPE)
5094 type = TREE_TYPE (type);
5095 if (type == error_mark_node)
5096 return true;
5097 else if (ARITHMETIC_TYPE_P (type))
5098 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5100 case PLUS_EXPR:
5101 case MULT_EXPR:
5102 case MINUS_EXPR:
5103 predefined = true;
5104 break;
5105 case MIN_EXPR:
5106 case MAX_EXPR:
5107 if (TREE_CODE (type) == COMPLEX_TYPE)
5108 break;
5109 predefined = true;
5110 break;
5111 case BIT_AND_EXPR:
5112 case BIT_IOR_EXPR:
5113 case BIT_XOR_EXPR:
5114 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5115 break;
5116 predefined = true;
5117 break;
5118 case TRUTH_ANDIF_EXPR:
5119 case TRUTH_ORIF_EXPR:
5120 if (FLOAT_TYPE_P (type))
5121 break;
5122 predefined = true;
5123 break;
5124 default:
5125 break;
5127 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5129 error ("%qE has invalid type for %<reduction%>", t);
5130 return true;
5132 else if (!processing_template_decl)
5134 t = require_complete_type (t);
5135 if (t == error_mark_node)
5136 return true;
5137 OMP_CLAUSE_DECL (c) = t;
5140 if (predefined)
5142 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5143 return false;
5145 else if (processing_template_decl)
5146 return false;
5148 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5150 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5151 if (TREE_CODE (type) == REFERENCE_TYPE)
5152 type = TREE_TYPE (type);
5153 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5154 if (id == NULL_TREE)
5155 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5156 NULL_TREE, NULL_TREE);
5157 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5158 if (id)
5160 if (id == error_mark_node)
5161 return true;
5162 id = OVL_CURRENT (id);
5163 mark_used (id);
5164 tree body = DECL_SAVED_TREE (id);
5165 if (!body)
5166 return true;
5167 if (TREE_CODE (body) == STATEMENT_LIST)
5169 tree_stmt_iterator tsi;
5170 tree placeholder = NULL_TREE;
5171 int i;
5172 tree stmts[7];
5173 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5174 atype = TREE_TYPE (atype);
5175 bool need_static_cast = !same_type_p (type, atype);
5176 memset (stmts, 0, sizeof stmts);
5177 for (i = 0, tsi = tsi_start (body);
5178 i < 7 && !tsi_end_p (tsi);
5179 i++, tsi_next (&tsi))
5180 stmts[i] = tsi_stmt (tsi);
5181 gcc_assert (tsi_end_p (tsi));
5183 if (i >= 3)
5185 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5186 && TREE_CODE (stmts[1]) == DECL_EXPR);
5187 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5188 DECL_ARTIFICIAL (placeholder) = 1;
5189 DECL_IGNORED_P (placeholder) = 1;
5190 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5191 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5192 cxx_mark_addressable (placeholder);
5193 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5194 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5195 != REFERENCE_TYPE)
5196 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5197 tree omp_out = placeholder;
5198 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5199 if (need_static_cast)
5201 tree rtype = build_reference_type (atype);
5202 omp_out = build_static_cast (rtype, omp_out,
5203 tf_warning_or_error);
5204 omp_in = build_static_cast (rtype, omp_in,
5205 tf_warning_or_error);
5206 if (omp_out == error_mark_node || omp_in == error_mark_node)
5207 return true;
5208 omp_out = convert_from_reference (omp_out);
5209 omp_in = convert_from_reference (omp_in);
5211 OMP_CLAUSE_REDUCTION_MERGE (c)
5212 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5213 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5215 if (i >= 6)
5217 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5218 && TREE_CODE (stmts[4]) == DECL_EXPR);
5219 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5220 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5221 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5222 cxx_mark_addressable (placeholder);
5223 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5224 tree omp_orig = placeholder;
5225 if (need_static_cast)
5227 if (i == 7)
5229 error_at (OMP_CLAUSE_LOCATION (c),
5230 "user defined reduction with constructor "
5231 "initializer for base class %qT", atype);
5232 return true;
5234 tree rtype = build_reference_type (atype);
5235 omp_priv = build_static_cast (rtype, omp_priv,
5236 tf_warning_or_error);
5237 omp_orig = build_static_cast (rtype, omp_orig,
5238 tf_warning_or_error);
5239 if (omp_priv == error_mark_node
5240 || omp_orig == error_mark_node)
5241 return true;
5242 omp_priv = convert_from_reference (omp_priv);
5243 omp_orig = convert_from_reference (omp_orig);
5245 if (i == 6)
5246 *need_default_ctor = true;
5247 OMP_CLAUSE_REDUCTION_INIT (c)
5248 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5249 DECL_EXPR_DECL (stmts[3]),
5250 omp_priv, omp_orig);
5251 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5252 find_omp_placeholder_r, placeholder, NULL))
5253 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5255 else if (i >= 3)
5257 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5258 *need_default_ctor = true;
5259 else
5261 tree init;
5262 tree v = convert_from_reference (t);
5263 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5264 init = build_constructor (TREE_TYPE (v), NULL);
5265 else
5266 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5267 OMP_CLAUSE_REDUCTION_INIT (c)
5268 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5273 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5274 *need_dtor = true;
5275 else
5277 error ("user defined reduction not found for %qD", t);
5278 return true;
5280 return false;
5283 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5284 Remove any elements from the list that are invalid. */
5286 tree
5287 finish_omp_clauses (tree clauses)
5289 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5290 bitmap_head aligned_head;
5291 tree c, t, *pc;
5292 bool branch_seen = false;
5293 bool copyprivate_seen = false;
5295 bitmap_obstack_initialize (NULL);
5296 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5297 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5298 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5299 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5301 for (pc = &clauses, c = clauses; c ; c = *pc)
5303 bool remove = false;
5305 switch (OMP_CLAUSE_CODE (c))
5307 case OMP_CLAUSE_SHARED:
5308 goto check_dup_generic;
5309 case OMP_CLAUSE_PRIVATE:
5310 goto check_dup_generic;
5311 case OMP_CLAUSE_REDUCTION:
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_COPYPRIVATE:
5314 copyprivate_seen = true;
5315 goto check_dup_generic;
5316 case OMP_CLAUSE_COPYIN:
5317 goto check_dup_generic;
5318 case OMP_CLAUSE_LINEAR:
5319 t = OMP_CLAUSE_DECL (c);
5320 if (!type_dependent_expression_p (t)
5321 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5322 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5324 error ("linear clause applied to non-integral non-pointer "
5325 "variable with %qT type", TREE_TYPE (t));
5326 remove = true;
5327 break;
5329 t = OMP_CLAUSE_LINEAR_STEP (c);
5330 if (t == NULL_TREE)
5331 t = integer_one_node;
5332 if (t == error_mark_node)
5334 remove = true;
5335 break;
5337 else if (!type_dependent_expression_p (t)
5338 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5340 error ("linear step expression must be integral");
5341 remove = true;
5342 break;
5344 else
5346 t = mark_rvalue_use (t);
5347 if (!processing_template_decl)
5349 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5350 t = maybe_constant_value (t);
5351 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5352 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5353 == POINTER_TYPE)
5355 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5356 OMP_CLAUSE_DECL (c), t);
5357 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5358 MINUS_EXPR, sizetype, t,
5359 OMP_CLAUSE_DECL (c));
5360 if (t == error_mark_node)
5362 remove = true;
5363 break;
5366 else
5367 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5369 OMP_CLAUSE_LINEAR_STEP (c) = t;
5371 goto check_dup_generic;
5372 check_dup_generic:
5373 t = OMP_CLAUSE_DECL (c);
5374 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5376 if (processing_template_decl)
5377 break;
5378 if (DECL_P (t))
5379 error ("%qD is not a variable in clause %qs", t,
5380 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5381 else
5382 error ("%qE is not a variable in clause %qs", t,
5383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5384 remove = true;
5386 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5387 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5388 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5390 error ("%qD appears more than once in data clauses", t);
5391 remove = true;
5393 else
5394 bitmap_set_bit (&generic_head, DECL_UID (t));
5395 break;
5397 case OMP_CLAUSE_FIRSTPRIVATE:
5398 t = OMP_CLAUSE_DECL (c);
5399 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5401 if (processing_template_decl)
5402 break;
5403 if (DECL_P (t))
5404 error ("%qD is not a variable in clause %<firstprivate%>", t);
5405 else
5406 error ("%qE is not a variable in clause %<firstprivate%>", t);
5407 remove = true;
5409 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5410 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5412 error ("%qD appears more than once in data clauses", t);
5413 remove = true;
5415 else
5416 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5417 break;
5419 case OMP_CLAUSE_LASTPRIVATE:
5420 t = OMP_CLAUSE_DECL (c);
5421 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5423 if (processing_template_decl)
5424 break;
5425 if (DECL_P (t))
5426 error ("%qD is not a variable in clause %<lastprivate%>", t);
5427 else
5428 error ("%qE is not a variable in clause %<lastprivate%>", t);
5429 remove = true;
5431 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5432 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5434 error ("%qD appears more than once in data clauses", t);
5435 remove = true;
5437 else
5438 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5439 break;
5441 case OMP_CLAUSE_IF:
5442 t = OMP_CLAUSE_IF_EXPR (c);
5443 t = maybe_convert_cond (t);
5444 if (t == error_mark_node)
5445 remove = true;
5446 else if (!processing_template_decl)
5447 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5448 OMP_CLAUSE_IF_EXPR (c) = t;
5449 break;
5451 case OMP_CLAUSE_FINAL:
5452 t = OMP_CLAUSE_FINAL_EXPR (c);
5453 t = maybe_convert_cond (t);
5454 if (t == error_mark_node)
5455 remove = true;
5456 else if (!processing_template_decl)
5457 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5458 OMP_CLAUSE_FINAL_EXPR (c) = t;
5459 break;
5461 case OMP_CLAUSE_NUM_THREADS:
5462 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5463 if (t == error_mark_node)
5464 remove = true;
5465 else if (!type_dependent_expression_p (t)
5466 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5468 error ("num_threads expression must be integral");
5469 remove = true;
5471 else
5473 t = mark_rvalue_use (t);
5474 if (!processing_template_decl)
5475 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5476 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5478 break;
5480 case OMP_CLAUSE_SCHEDULE:
5481 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5482 if (t == NULL)
5484 else if (t == error_mark_node)
5485 remove = true;
5486 else if (!type_dependent_expression_p (t)
5487 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5488 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5489 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5491 error ("schedule chunk size expression must be integral");
5492 remove = true;
5494 else
5496 t = mark_rvalue_use (t);
5497 if (!processing_template_decl)
5499 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5500 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5502 t = convert_to_integer (long_integer_type_node, t);
5503 if (t == error_mark_node)
5505 remove = true;
5506 break;
5509 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5511 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5513 break;
5515 case OMP_CLAUSE_SIMDLEN:
5516 case OMP_CLAUSE_SAFELEN:
5517 t = OMP_CLAUSE_OPERAND (c, 0);
5518 if (t == error_mark_node)
5519 remove = true;
5520 else if (!type_dependent_expression_p (t)
5521 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5523 error ("%qs length expression must be integral",
5524 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5525 remove = true;
5527 else
5529 t = mark_rvalue_use (t);
5530 t = maybe_constant_value (t);
5531 if (!processing_template_decl)
5533 if (TREE_CODE (t) != INTEGER_CST
5534 || tree_int_cst_sgn (t) != 1)
5536 error ("%qs length expression must be positive constant"
5537 " integer expression",
5538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5539 remove = true;
5542 OMP_CLAUSE_OPERAND (c, 0) = t;
5544 break;
5546 case OMP_CLAUSE_NUM_TEAMS:
5547 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5548 if (t == error_mark_node)
5549 remove = true;
5550 else if (!type_dependent_expression_p (t)
5551 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5553 error ("%<num_teams%> expression must be integral");
5554 remove = true;
5556 else
5558 t = mark_rvalue_use (t);
5559 if (!processing_template_decl)
5560 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5561 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5563 break;
5565 case OMP_CLAUSE_ASYNC:
5566 t = OMP_CLAUSE_ASYNC_EXPR (c);
5567 if (t == error_mark_node)
5568 remove = true;
5569 else if (!type_dependent_expression_p (t)
5570 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5572 error ("%<async%> expression must be integral");
5573 remove = true;
5575 else
5577 t = mark_rvalue_use (t);
5578 if (!processing_template_decl)
5579 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5580 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5582 break;
5584 case OMP_CLAUSE_VECTOR_LENGTH:
5585 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5586 t = maybe_convert_cond (t);
5587 if (t == error_mark_node)
5588 remove = true;
5589 else if (!processing_template_decl)
5590 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5591 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5592 break;
5594 case OMP_CLAUSE_WAIT:
5595 t = OMP_CLAUSE_WAIT_EXPR (c);
5596 if (t == error_mark_node)
5597 remove = true;
5598 else if (!processing_template_decl)
5599 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5600 OMP_CLAUSE_WAIT_EXPR (c) = t;
5601 break;
5603 case OMP_CLAUSE_THREAD_LIMIT:
5604 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5605 if (t == error_mark_node)
5606 remove = true;
5607 else if (!type_dependent_expression_p (t)
5608 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5610 error ("%<thread_limit%> expression must be integral");
5611 remove = true;
5613 else
5615 t = mark_rvalue_use (t);
5616 if (!processing_template_decl)
5617 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5618 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5620 break;
5622 case OMP_CLAUSE_DEVICE:
5623 t = OMP_CLAUSE_DEVICE_ID (c);
5624 if (t == error_mark_node)
5625 remove = true;
5626 else if (!type_dependent_expression_p (t)
5627 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5629 error ("%<device%> id must be integral");
5630 remove = true;
5632 else
5634 t = mark_rvalue_use (t);
5635 if (!processing_template_decl)
5636 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5637 OMP_CLAUSE_DEVICE_ID (c) = t;
5639 break;
5641 case OMP_CLAUSE_DIST_SCHEDULE:
5642 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5643 if (t == NULL)
5645 else if (t == error_mark_node)
5646 remove = true;
5647 else if (!type_dependent_expression_p (t)
5648 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5650 error ("%<dist_schedule%> chunk size expression must be "
5651 "integral");
5652 remove = true;
5654 else
5656 t = mark_rvalue_use (t);
5657 if (!processing_template_decl)
5658 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5659 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5661 break;
5663 case OMP_CLAUSE_ALIGNED:
5664 t = OMP_CLAUSE_DECL (c);
5665 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5667 if (processing_template_decl)
5668 break;
5669 if (DECL_P (t))
5670 error ("%qD is not a variable in %<aligned%> clause", t);
5671 else
5672 error ("%qE is not a variable in %<aligned%> clause", t);
5673 remove = true;
5675 else if (!type_dependent_expression_p (t)
5676 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5677 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5678 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5679 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5680 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5681 != ARRAY_TYPE))))
5683 error_at (OMP_CLAUSE_LOCATION (c),
5684 "%qE in %<aligned%> clause is neither a pointer nor "
5685 "an array nor a reference to pointer or array", t);
5686 remove = true;
5688 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5690 error ("%qD appears more than once in %<aligned%> clauses", t);
5691 remove = true;
5693 else
5694 bitmap_set_bit (&aligned_head, DECL_UID (t));
5695 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5696 if (t == error_mark_node)
5697 remove = true;
5698 else if (t == NULL_TREE)
5699 break;
5700 else if (!type_dependent_expression_p (t)
5701 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5703 error ("%<aligned%> clause alignment expression must "
5704 "be integral");
5705 remove = true;
5707 else
5709 t = mark_rvalue_use (t);
5710 t = maybe_constant_value (t);
5711 if (!processing_template_decl)
5713 if (TREE_CODE (t) != INTEGER_CST
5714 || tree_int_cst_sgn (t) != 1)
5716 error ("%<aligned%> clause alignment expression must be "
5717 "positive constant integer expression");
5718 remove = true;
5721 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5723 break;
5725 case OMP_CLAUSE_DEPEND:
5726 t = OMP_CLAUSE_DECL (c);
5727 if (TREE_CODE (t) == TREE_LIST)
5729 if (handle_omp_array_sections (c))
5730 remove = true;
5731 break;
5733 if (t == error_mark_node)
5734 remove = true;
5735 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5737 if (processing_template_decl)
5738 break;
5739 if (DECL_P (t))
5740 error ("%qD is not a variable in %<depend%> clause", t);
5741 else
5742 error ("%qE is not a variable in %<depend%> clause", t);
5743 remove = true;
5745 else if (!processing_template_decl
5746 && !cxx_mark_addressable (t))
5747 remove = true;
5748 break;
5750 case OMP_CLAUSE_MAP:
5751 case OMP_CLAUSE_TO:
5752 case OMP_CLAUSE_FROM:
5753 case OMP_CLAUSE__CACHE_:
5754 t = OMP_CLAUSE_DECL (c);
5755 if (TREE_CODE (t) == TREE_LIST)
5757 if (handle_omp_array_sections (c))
5758 remove = true;
5759 else
5761 t = OMP_CLAUSE_DECL (c);
5762 if (TREE_CODE (t) != TREE_LIST
5763 && !type_dependent_expression_p (t)
5764 && !cp_omp_mappable_type (TREE_TYPE (t)))
5766 error_at (OMP_CLAUSE_LOCATION (c),
5767 "array section does not have mappable type "
5768 "in %qs clause",
5769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5770 remove = true;
5773 break;
5775 if (t == error_mark_node)
5776 remove = true;
5777 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5779 if (processing_template_decl)
5780 break;
5781 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5782 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5783 break;
5784 if (DECL_P (t))
5785 error ("%qD is not a variable in %qs clause", t,
5786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5787 else
5788 error ("%qE is not a variable in %qs clause", t,
5789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5790 remove = true;
5792 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5794 error ("%qD is threadprivate variable in %qs clause", t,
5795 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5796 remove = true;
5798 else if (!processing_template_decl
5799 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5800 && !cxx_mark_addressable (t))
5801 remove = true;
5802 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5803 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5804 && !type_dependent_expression_p (t)
5805 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5806 == REFERENCE_TYPE)
5807 ? TREE_TYPE (TREE_TYPE (t))
5808 : TREE_TYPE (t)))
5810 error_at (OMP_CLAUSE_LOCATION (c),
5811 "%qD does not have a mappable type in %qs clause", t,
5812 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5813 remove = true;
5815 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5817 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5818 error ("%qD appears more than once in motion clauses", t);
5819 else
5820 error ("%qD appears more than once in map clauses", t);
5821 remove = true;
5823 else
5824 bitmap_set_bit (&generic_head, DECL_UID (t));
5825 break;
5827 case OMP_CLAUSE_UNIFORM:
5828 t = OMP_CLAUSE_DECL (c);
5829 if (TREE_CODE (t) != PARM_DECL)
5831 if (processing_template_decl)
5832 break;
5833 if (DECL_P (t))
5834 error ("%qD is not an argument in %<uniform%> clause", t);
5835 else
5836 error ("%qE is not an argument in %<uniform%> clause", t);
5837 remove = true;
5838 break;
5840 goto check_dup_generic;
5842 case OMP_CLAUSE_NOWAIT:
5843 case OMP_CLAUSE_ORDERED:
5844 case OMP_CLAUSE_DEFAULT:
5845 case OMP_CLAUSE_UNTIED:
5846 case OMP_CLAUSE_COLLAPSE:
5847 case OMP_CLAUSE_MERGEABLE:
5848 case OMP_CLAUSE_PARALLEL:
5849 case OMP_CLAUSE_FOR:
5850 case OMP_CLAUSE_SECTIONS:
5851 case OMP_CLAUSE_TASKGROUP:
5852 case OMP_CLAUSE_PROC_BIND:
5853 case OMP_CLAUSE__CILK_FOR_COUNT_:
5854 break;
5856 case OMP_CLAUSE_INBRANCH:
5857 case OMP_CLAUSE_NOTINBRANCH:
5858 if (branch_seen)
5860 error ("%<inbranch%> clause is incompatible with "
5861 "%<notinbranch%>");
5862 remove = true;
5864 branch_seen = true;
5865 break;
5867 default:
5868 gcc_unreachable ();
5871 if (remove)
5872 *pc = OMP_CLAUSE_CHAIN (c);
5873 else
5874 pc = &OMP_CLAUSE_CHAIN (c);
5877 for (pc = &clauses, c = clauses; c ; c = *pc)
5879 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5880 bool remove = false;
5881 bool need_complete_non_reference = false;
5882 bool need_default_ctor = false;
5883 bool need_copy_ctor = false;
5884 bool need_copy_assignment = false;
5885 bool need_implicitly_determined = false;
5886 bool need_dtor = false;
5887 tree type, inner_type;
5889 switch (c_kind)
5891 case OMP_CLAUSE_SHARED:
5892 need_implicitly_determined = true;
5893 break;
5894 case OMP_CLAUSE_PRIVATE:
5895 need_complete_non_reference = true;
5896 need_default_ctor = true;
5897 need_dtor = true;
5898 need_implicitly_determined = true;
5899 break;
5900 case OMP_CLAUSE_FIRSTPRIVATE:
5901 need_complete_non_reference = true;
5902 need_copy_ctor = true;
5903 need_dtor = true;
5904 need_implicitly_determined = true;
5905 break;
5906 case OMP_CLAUSE_LASTPRIVATE:
5907 need_complete_non_reference = true;
5908 need_copy_assignment = true;
5909 need_implicitly_determined = true;
5910 break;
5911 case OMP_CLAUSE_REDUCTION:
5912 need_implicitly_determined = true;
5913 break;
5914 case OMP_CLAUSE_COPYPRIVATE:
5915 need_copy_assignment = true;
5916 break;
5917 case OMP_CLAUSE_COPYIN:
5918 need_copy_assignment = true;
5919 break;
5920 case OMP_CLAUSE_NOWAIT:
5921 if (copyprivate_seen)
5923 error_at (OMP_CLAUSE_LOCATION (c),
5924 "%<nowait%> clause must not be used together "
5925 "with %<copyprivate%>");
5926 *pc = OMP_CLAUSE_CHAIN (c);
5927 continue;
5929 /* FALLTHRU */
5930 default:
5931 pc = &OMP_CLAUSE_CHAIN (c);
5932 continue;
5935 t = OMP_CLAUSE_DECL (c);
5936 if (processing_template_decl
5937 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5939 pc = &OMP_CLAUSE_CHAIN (c);
5940 continue;
5943 switch (c_kind)
5945 case OMP_CLAUSE_LASTPRIVATE:
5946 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5948 need_default_ctor = true;
5949 need_dtor = true;
5951 break;
5953 case OMP_CLAUSE_REDUCTION:
5954 if (finish_omp_reduction_clause (c, &need_default_ctor,
5955 &need_dtor))
5956 remove = true;
5957 else
5958 t = OMP_CLAUSE_DECL (c);
5959 break;
5961 case OMP_CLAUSE_COPYIN:
5962 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5964 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5965 remove = true;
5967 break;
5969 default:
5970 break;
5973 if (need_complete_non_reference || need_copy_assignment)
5975 t = require_complete_type (t);
5976 if (t == error_mark_node)
5977 remove = true;
5978 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5979 && need_complete_non_reference)
5981 error ("%qE has reference type for %qs", t,
5982 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5983 remove = true;
5986 if (need_implicitly_determined)
5988 const char *share_name = NULL;
5990 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5991 share_name = "threadprivate";
5992 else switch (cxx_omp_predetermined_sharing (t))
5994 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5995 break;
5996 case OMP_CLAUSE_DEFAULT_SHARED:
5997 /* const vars may be specified in firstprivate clause. */
5998 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5999 && cxx_omp_const_qual_no_mutable (t))
6000 break;
6001 share_name = "shared";
6002 break;
6003 case OMP_CLAUSE_DEFAULT_PRIVATE:
6004 share_name = "private";
6005 break;
6006 default:
6007 gcc_unreachable ();
6009 if (share_name)
6011 error ("%qE is predetermined %qs for %qs",
6012 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6013 remove = true;
6017 /* We're interested in the base element, not arrays. */
6018 inner_type = type = TREE_TYPE (t);
6019 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6020 inner_type = TREE_TYPE (inner_type);
6022 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6023 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6024 inner_type = TREE_TYPE (inner_type);
6026 /* Check for special function availability by building a call to one.
6027 Save the results, because later we won't be in the right context
6028 for making these queries. */
6029 if (CLASS_TYPE_P (inner_type)
6030 && COMPLETE_TYPE_P (inner_type)
6031 && (need_default_ctor || need_copy_ctor
6032 || need_copy_assignment || need_dtor)
6033 && !type_dependent_expression_p (t)
6034 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6035 need_copy_ctor, need_copy_assignment,
6036 need_dtor))
6037 remove = true;
6039 if (remove)
6040 *pc = OMP_CLAUSE_CHAIN (c);
6041 else
6042 pc = &OMP_CLAUSE_CHAIN (c);
6045 bitmap_obstack_release (NULL);
6046 return clauses;
6049 /* For all variables in the tree_list VARS, mark them as thread local. */
6051 void
6052 finish_omp_threadprivate (tree vars)
6054 tree t;
6056 /* Mark every variable in VARS to be assigned thread local storage. */
6057 for (t = vars; t; t = TREE_CHAIN (t))
6059 tree v = TREE_PURPOSE (t);
6061 if (error_operand_p (v))
6063 else if (!VAR_P (v))
6064 error ("%<threadprivate%> %qD is not file, namespace "
6065 "or block scope variable", v);
6066 /* If V had already been marked threadprivate, it doesn't matter
6067 whether it had been used prior to this point. */
6068 else if (TREE_USED (v)
6069 && (DECL_LANG_SPECIFIC (v) == NULL
6070 || !CP_DECL_THREADPRIVATE_P (v)))
6071 error ("%qE declared %<threadprivate%> after first use", v);
6072 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6073 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6074 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6075 error ("%<threadprivate%> %qE has incomplete type", v);
6076 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6077 && CP_DECL_CONTEXT (v) != current_class_type)
6078 error ("%<threadprivate%> %qE directive not "
6079 "in %qT definition", v, CP_DECL_CONTEXT (v));
6080 else
6082 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6083 if (DECL_LANG_SPECIFIC (v) == NULL)
6085 retrofit_lang_decl (v);
6087 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6088 after the allocation of the lang_decl structure. */
6089 if (DECL_DISCRIMINATOR_P (v))
6090 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6093 if (! DECL_THREAD_LOCAL_P (v))
6095 set_decl_tls_model (v, decl_default_tls_model (v));
6096 /* If rtl has been already set for this var, call
6097 make_decl_rtl once again, so that encode_section_info
6098 has a chance to look at the new decl flags. */
6099 if (DECL_RTL_SET_P (v))
6100 make_decl_rtl (v);
6102 CP_DECL_THREADPRIVATE_P (v) = 1;
6107 /* Build an OpenMP structured block. */
6109 tree
6110 begin_omp_structured_block (void)
6112 return do_pushlevel (sk_omp);
6115 tree
6116 finish_omp_structured_block (tree block)
6118 return do_poplevel (block);
6121 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6122 statement. LOC is the location of the OACC_DATA. */
6124 tree
6125 finish_oacc_data (tree clauses, tree block)
6127 tree stmt;
6129 block = finish_omp_structured_block (block);
6131 stmt = make_node (OACC_DATA);
6132 TREE_TYPE (stmt) = void_type_node;
6133 OACC_DATA_CLAUSES (stmt) = clauses;
6134 OACC_DATA_BODY (stmt) = block;
6136 return add_stmt (stmt);
6139 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6140 statement. LOC is the location of the OACC_KERNELS. */
6142 tree
6143 finish_oacc_kernels (tree clauses, tree block)
6145 tree stmt;
6147 block = finish_omp_structured_block (block);
6149 stmt = make_node (OACC_KERNELS);
6150 TREE_TYPE (stmt) = void_type_node;
6151 OACC_KERNELS_CLAUSES (stmt) = clauses;
6152 OACC_KERNELS_BODY (stmt) = block;
6154 return add_stmt (stmt);
6157 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6158 statement. LOC is the location of the OACC_PARALLEL. */
6160 tree
6161 finish_oacc_parallel (tree clauses, tree block)
6163 tree stmt;
6165 block = finish_omp_structured_block (block);
6167 stmt = make_node (OACC_PARALLEL);
6168 TREE_TYPE (stmt) = void_type_node;
6169 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6170 OACC_PARALLEL_BODY (stmt) = block;
6172 return add_stmt (stmt);
6175 /* Similarly, except force the retention of the BLOCK. */
6177 tree
6178 begin_omp_parallel (void)
6180 keep_next_level (true);
6181 return begin_omp_structured_block ();
6184 tree
6185 finish_omp_parallel (tree clauses, tree body)
6187 tree stmt;
6189 body = finish_omp_structured_block (body);
6191 stmt = make_node (OMP_PARALLEL);
6192 TREE_TYPE (stmt) = void_type_node;
6193 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6194 OMP_PARALLEL_BODY (stmt) = body;
6196 return add_stmt (stmt);
6199 tree
6200 begin_omp_task (void)
6202 keep_next_level (true);
6203 return begin_omp_structured_block ();
6206 tree
6207 finish_omp_task (tree clauses, tree body)
6209 tree stmt;
6211 body = finish_omp_structured_block (body);
6213 stmt = make_node (OMP_TASK);
6214 TREE_TYPE (stmt) = void_type_node;
6215 OMP_TASK_CLAUSES (stmt) = clauses;
6216 OMP_TASK_BODY (stmt) = body;
6218 return add_stmt (stmt);
6221 /* Helper function for finish_omp_for. Convert Ith random access iterator
6222 into integral iterator. Return FALSE if successful. */
6224 static bool
6225 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6226 tree condv, tree incrv, tree *body,
6227 tree *pre_body, tree clauses, tree *lastp)
6229 tree diff, iter_init, iter_incr = NULL, last;
6230 tree incr_var = NULL, orig_pre_body, orig_body, c;
6231 tree decl = TREE_VEC_ELT (declv, i);
6232 tree init = TREE_VEC_ELT (initv, i);
6233 tree cond = TREE_VEC_ELT (condv, i);
6234 tree incr = TREE_VEC_ELT (incrv, i);
6235 tree iter = decl;
6236 location_t elocus = locus;
6238 if (init && EXPR_HAS_LOCATION (init))
6239 elocus = EXPR_LOCATION (init);
6241 switch (TREE_CODE (cond))
6243 case GT_EXPR:
6244 case GE_EXPR:
6245 case LT_EXPR:
6246 case LE_EXPR:
6247 case NE_EXPR:
6248 if (TREE_OPERAND (cond, 1) == iter)
6249 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6250 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6251 if (TREE_OPERAND (cond, 0) != iter)
6252 cond = error_mark_node;
6253 else
6255 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6256 TREE_CODE (cond),
6257 iter, ERROR_MARK,
6258 TREE_OPERAND (cond, 1), ERROR_MARK,
6259 NULL, tf_warning_or_error);
6260 if (error_operand_p (tem))
6261 return true;
6263 break;
6264 default:
6265 cond = error_mark_node;
6266 break;
6268 if (cond == error_mark_node)
6270 error_at (elocus, "invalid controlling predicate");
6271 return true;
6273 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6274 ERROR_MARK, iter, ERROR_MARK, NULL,
6275 tf_warning_or_error);
6276 if (error_operand_p (diff))
6277 return true;
6278 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6280 error_at (elocus, "difference between %qE and %qD does not have integer type",
6281 TREE_OPERAND (cond, 1), iter);
6282 return true;
6285 switch (TREE_CODE (incr))
6287 case PREINCREMENT_EXPR:
6288 case PREDECREMENT_EXPR:
6289 case POSTINCREMENT_EXPR:
6290 case POSTDECREMENT_EXPR:
6291 if (TREE_OPERAND (incr, 0) != iter)
6293 incr = error_mark_node;
6294 break;
6296 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6297 TREE_CODE (incr), iter,
6298 tf_warning_or_error);
6299 if (error_operand_p (iter_incr))
6300 return true;
6301 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6302 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6303 incr = integer_one_node;
6304 else
6305 incr = integer_minus_one_node;
6306 break;
6307 case MODIFY_EXPR:
6308 if (TREE_OPERAND (incr, 0) != iter)
6309 incr = error_mark_node;
6310 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6311 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6313 tree rhs = TREE_OPERAND (incr, 1);
6314 if (TREE_OPERAND (rhs, 0) == iter)
6316 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6317 != INTEGER_TYPE)
6318 incr = error_mark_node;
6319 else
6321 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6322 iter, TREE_CODE (rhs),
6323 TREE_OPERAND (rhs, 1),
6324 tf_warning_or_error);
6325 if (error_operand_p (iter_incr))
6326 return true;
6327 incr = TREE_OPERAND (rhs, 1);
6328 incr = cp_convert (TREE_TYPE (diff), incr,
6329 tf_warning_or_error);
6330 if (TREE_CODE (rhs) == MINUS_EXPR)
6332 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6333 incr = fold_if_not_in_template (incr);
6335 if (TREE_CODE (incr) != INTEGER_CST
6336 && (TREE_CODE (incr) != NOP_EXPR
6337 || (TREE_CODE (TREE_OPERAND (incr, 0))
6338 != INTEGER_CST)))
6339 iter_incr = NULL;
6342 else if (TREE_OPERAND (rhs, 1) == iter)
6344 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6345 || TREE_CODE (rhs) != PLUS_EXPR)
6346 incr = error_mark_node;
6347 else
6349 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6350 PLUS_EXPR,
6351 TREE_OPERAND (rhs, 0),
6352 ERROR_MARK, iter,
6353 ERROR_MARK, NULL,
6354 tf_warning_or_error);
6355 if (error_operand_p (iter_incr))
6356 return true;
6357 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6358 iter, NOP_EXPR,
6359 iter_incr,
6360 tf_warning_or_error);
6361 if (error_operand_p (iter_incr))
6362 return true;
6363 incr = TREE_OPERAND (rhs, 0);
6364 iter_incr = NULL;
6367 else
6368 incr = error_mark_node;
6370 else
6371 incr = error_mark_node;
6372 break;
6373 default:
6374 incr = error_mark_node;
6375 break;
6378 if (incr == error_mark_node)
6380 error_at (elocus, "invalid increment expression");
6381 return true;
6384 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6385 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6386 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6387 && OMP_CLAUSE_DECL (c) == iter)
6388 break;
6390 decl = create_temporary_var (TREE_TYPE (diff));
6391 pushdecl (decl);
6392 add_decl_expr (decl);
6393 last = create_temporary_var (TREE_TYPE (diff));
6394 pushdecl (last);
6395 add_decl_expr (last);
6396 if (c && iter_incr == NULL)
6398 incr_var = create_temporary_var (TREE_TYPE (diff));
6399 pushdecl (incr_var);
6400 add_decl_expr (incr_var);
6402 gcc_assert (stmts_are_full_exprs_p ());
6404 orig_pre_body = *pre_body;
6405 *pre_body = push_stmt_list ();
6406 if (orig_pre_body)
6407 add_stmt (orig_pre_body);
6408 if (init != NULL)
6409 finish_expr_stmt (build_x_modify_expr (elocus,
6410 iter, NOP_EXPR, init,
6411 tf_warning_or_error));
6412 init = build_int_cst (TREE_TYPE (diff), 0);
6413 if (c && iter_incr == NULL)
6415 finish_expr_stmt (build_x_modify_expr (elocus,
6416 incr_var, NOP_EXPR,
6417 incr, tf_warning_or_error));
6418 incr = incr_var;
6419 iter_incr = build_x_modify_expr (elocus,
6420 iter, PLUS_EXPR, incr,
6421 tf_warning_or_error);
6423 finish_expr_stmt (build_x_modify_expr (elocus,
6424 last, NOP_EXPR, init,
6425 tf_warning_or_error));
6426 *pre_body = pop_stmt_list (*pre_body);
6428 cond = cp_build_binary_op (elocus,
6429 TREE_CODE (cond), decl, diff,
6430 tf_warning_or_error);
6431 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6432 elocus, incr, NULL_TREE);
6434 orig_body = *body;
6435 *body = push_stmt_list ();
6436 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6437 iter_init = build_x_modify_expr (elocus,
6438 iter, PLUS_EXPR, iter_init,
6439 tf_warning_or_error);
6440 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6441 finish_expr_stmt (iter_init);
6442 finish_expr_stmt (build_x_modify_expr (elocus,
6443 last, NOP_EXPR, decl,
6444 tf_warning_or_error));
6445 add_stmt (orig_body);
6446 *body = pop_stmt_list (*body);
6448 if (c)
6450 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6451 finish_expr_stmt (iter_incr);
6452 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6453 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6456 TREE_VEC_ELT (declv, i) = decl;
6457 TREE_VEC_ELT (initv, i) = init;
6458 TREE_VEC_ELT (condv, i) = cond;
6459 TREE_VEC_ELT (incrv, i) = incr;
6460 *lastp = last;
6462 return false;
6465 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6466 are directly for their associated operands in the statement. DECL
6467 and INIT are a combo; if DECL is NULL then INIT ought to be a
6468 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6469 optional statements that need to go before the loop into its
6470 sk_omp scope. */
6472 tree
6473 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6474 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6476 tree omp_for = NULL, orig_incr = NULL;
6477 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6478 tree last = NULL_TREE;
6479 location_t elocus;
6480 int i;
6482 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6483 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6484 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6485 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6487 decl = TREE_VEC_ELT (declv, i);
6488 init = TREE_VEC_ELT (initv, i);
6489 cond = TREE_VEC_ELT (condv, i);
6490 incr = TREE_VEC_ELT (incrv, i);
6491 elocus = locus;
6493 if (decl == NULL)
6495 if (init != NULL)
6496 switch (TREE_CODE (init))
6498 case MODIFY_EXPR:
6499 decl = TREE_OPERAND (init, 0);
6500 init = TREE_OPERAND (init, 1);
6501 break;
6502 case MODOP_EXPR:
6503 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6505 decl = TREE_OPERAND (init, 0);
6506 init = TREE_OPERAND (init, 2);
6508 break;
6509 default:
6510 break;
6513 if (decl == NULL)
6515 error_at (locus,
6516 "expected iteration declaration or initialization");
6517 return NULL;
6521 if (init && EXPR_HAS_LOCATION (init))
6522 elocus = EXPR_LOCATION (init);
6524 if (cond == NULL)
6526 error_at (elocus, "missing controlling predicate");
6527 return NULL;
6530 if (incr == NULL)
6532 error_at (elocus, "missing increment expression");
6533 return NULL;
6536 TREE_VEC_ELT (declv, i) = decl;
6537 TREE_VEC_ELT (initv, i) = init;
6540 if (dependent_omp_for_p (declv, initv, condv, incrv))
6542 tree stmt;
6544 stmt = make_node (code);
6546 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6548 /* This is really just a place-holder. We'll be decomposing this
6549 again and going through the cp_build_modify_expr path below when
6550 we instantiate the thing. */
6551 TREE_VEC_ELT (initv, i)
6552 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6553 TREE_VEC_ELT (initv, i));
6556 TREE_TYPE (stmt) = void_type_node;
6557 OMP_FOR_INIT (stmt) = initv;
6558 OMP_FOR_COND (stmt) = condv;
6559 OMP_FOR_INCR (stmt) = incrv;
6560 OMP_FOR_BODY (stmt) = body;
6561 OMP_FOR_PRE_BODY (stmt) = pre_body;
6562 OMP_FOR_CLAUSES (stmt) = clauses;
6564 SET_EXPR_LOCATION (stmt, locus);
6565 return add_stmt (stmt);
6568 if (processing_template_decl)
6569 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6571 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6573 decl = TREE_VEC_ELT (declv, i);
6574 init = TREE_VEC_ELT (initv, i);
6575 cond = TREE_VEC_ELT (condv, i);
6576 incr = TREE_VEC_ELT (incrv, i);
6577 if (orig_incr)
6578 TREE_VEC_ELT (orig_incr, i) = incr;
6579 elocus = locus;
6581 if (init && EXPR_HAS_LOCATION (init))
6582 elocus = EXPR_LOCATION (init);
6584 if (!DECL_P (decl))
6586 error_at (elocus, "expected iteration declaration or initialization");
6587 return NULL;
6590 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6592 if (orig_incr)
6593 TREE_VEC_ELT (orig_incr, i) = incr;
6594 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6595 TREE_CODE (TREE_OPERAND (incr, 1)),
6596 TREE_OPERAND (incr, 2),
6597 tf_warning_or_error);
6600 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6602 if (code == OMP_SIMD)
6604 error_at (elocus, "%<#pragma omp simd%> used with class "
6605 "iteration variable %qE", decl);
6606 return NULL;
6608 if (code == CILK_FOR && i == 0)
6609 orig_decl = decl;
6610 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6611 incrv, &body, &pre_body,
6612 clauses, &last))
6613 return NULL;
6614 continue;
6617 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6618 && !TYPE_PTR_P (TREE_TYPE (decl)))
6620 error_at (elocus, "invalid type for iteration variable %qE", decl);
6621 return NULL;
6624 if (!processing_template_decl)
6626 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6627 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6629 else
6630 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6631 if (cond
6632 && TREE_SIDE_EFFECTS (cond)
6633 && COMPARISON_CLASS_P (cond)
6634 && !processing_template_decl)
6636 tree t = TREE_OPERAND (cond, 0);
6637 if (TREE_SIDE_EFFECTS (t)
6638 && t != decl
6639 && (TREE_CODE (t) != NOP_EXPR
6640 || TREE_OPERAND (t, 0) != decl))
6641 TREE_OPERAND (cond, 0)
6642 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6644 t = TREE_OPERAND (cond, 1);
6645 if (TREE_SIDE_EFFECTS (t)
6646 && t != decl
6647 && (TREE_CODE (t) != NOP_EXPR
6648 || TREE_OPERAND (t, 0) != decl))
6649 TREE_OPERAND (cond, 1)
6650 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6652 if (decl == error_mark_node || init == error_mark_node)
6653 return NULL;
6655 TREE_VEC_ELT (declv, i) = decl;
6656 TREE_VEC_ELT (initv, i) = init;
6657 TREE_VEC_ELT (condv, i) = cond;
6658 TREE_VEC_ELT (incrv, i) = incr;
6659 i++;
6662 if (IS_EMPTY_STMT (pre_body))
6663 pre_body = NULL;
6665 if (code == CILK_FOR && !processing_template_decl)
6666 block = push_stmt_list ();
6668 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6669 body, pre_body);
6671 if (omp_for == NULL)
6673 if (block)
6674 pop_stmt_list (block);
6675 return NULL;
6678 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6680 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6681 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6683 if (TREE_CODE (incr) != MODIFY_EXPR)
6684 continue;
6686 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6687 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6688 && !processing_template_decl)
6690 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6691 if (TREE_SIDE_EFFECTS (t)
6692 && t != decl
6693 && (TREE_CODE (t) != NOP_EXPR
6694 || TREE_OPERAND (t, 0) != decl))
6695 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6696 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6698 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6699 if (TREE_SIDE_EFFECTS (t)
6700 && t != decl
6701 && (TREE_CODE (t) != NOP_EXPR
6702 || TREE_OPERAND (t, 0) != decl))
6703 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6704 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6707 if (orig_incr)
6708 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6710 OMP_FOR_CLAUSES (omp_for) = clauses;
6712 if (block)
6714 tree omp_par = make_node (OMP_PARALLEL);
6715 TREE_TYPE (omp_par) = void_type_node;
6716 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6717 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6718 TREE_SIDE_EFFECTS (bind) = 1;
6719 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6720 OMP_PARALLEL_BODY (omp_par) = bind;
6721 if (OMP_FOR_PRE_BODY (omp_for))
6723 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6724 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6726 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6727 decl = TREE_OPERAND (init, 0);
6728 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6729 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6730 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6731 clauses = OMP_FOR_CLAUSES (omp_for);
6732 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6733 for (pc = &clauses; *pc; )
6734 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6736 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6737 OMP_FOR_CLAUSES (omp_for) = *pc;
6738 *pc = OMP_CLAUSE_CHAIN (*pc);
6739 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6741 else
6743 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6744 pc = &OMP_CLAUSE_CHAIN (*pc);
6746 if (TREE_CODE (t) != INTEGER_CST)
6748 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6749 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6750 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6751 OMP_CLAUSE_CHAIN (c) = clauses;
6752 clauses = c;
6754 if (TREE_CODE (incr) == MODIFY_EXPR)
6756 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6757 if (TREE_CODE (t) != INTEGER_CST)
6759 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6760 = get_temp_regvar (TREE_TYPE (t), t);
6761 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6762 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6763 OMP_CLAUSE_CHAIN (c) = clauses;
6764 clauses = c;
6767 t = TREE_OPERAND (init, 1);
6768 if (TREE_CODE (t) != INTEGER_CST)
6770 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6771 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6772 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6773 OMP_CLAUSE_CHAIN (c) = clauses;
6774 clauses = c;
6776 if (orig_decl && orig_decl != decl)
6778 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6779 OMP_CLAUSE_DECL (c) = orig_decl;
6780 OMP_CLAUSE_CHAIN (c) = clauses;
6781 clauses = c;
6783 if (last)
6785 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6786 OMP_CLAUSE_DECL (c) = last;
6787 OMP_CLAUSE_CHAIN (c) = clauses;
6788 clauses = c;
6790 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6791 OMP_CLAUSE_DECL (c) = decl;
6792 OMP_CLAUSE_CHAIN (c) = clauses;
6793 clauses = c;
6794 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6795 OMP_CLAUSE_OPERAND (c, 0)
6796 = cilk_for_number_of_iterations (omp_for);
6797 OMP_CLAUSE_CHAIN (c) = clauses;
6798 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6799 add_stmt (omp_par);
6800 return omp_par;
6802 else if (code == CILK_FOR && processing_template_decl)
6804 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6805 if (orig_decl && orig_decl != decl)
6807 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6808 OMP_CLAUSE_DECL (c) = orig_decl;
6809 OMP_CLAUSE_CHAIN (c) = clauses;
6810 clauses = c;
6812 if (last)
6814 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6815 OMP_CLAUSE_DECL (c) = last;
6816 OMP_CLAUSE_CHAIN (c) = clauses;
6817 clauses = c;
6819 OMP_FOR_CLAUSES (omp_for) = clauses;
6821 return omp_for;
6824 void
6825 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6826 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6828 tree orig_lhs;
6829 tree orig_rhs;
6830 tree orig_v;
6831 tree orig_lhs1;
6832 tree orig_rhs1;
6833 bool dependent_p;
6834 tree stmt;
6836 orig_lhs = lhs;
6837 orig_rhs = rhs;
6838 orig_v = v;
6839 orig_lhs1 = lhs1;
6840 orig_rhs1 = rhs1;
6841 dependent_p = false;
6842 stmt = NULL_TREE;
6844 /* Even in a template, we can detect invalid uses of the atomic
6845 pragma if neither LHS nor RHS is type-dependent. */
6846 if (processing_template_decl)
6848 dependent_p = (type_dependent_expression_p (lhs)
6849 || (rhs && type_dependent_expression_p (rhs))
6850 || (v && type_dependent_expression_p (v))
6851 || (lhs1 && type_dependent_expression_p (lhs1))
6852 || (rhs1 && type_dependent_expression_p (rhs1)));
6853 if (!dependent_p)
6855 lhs = build_non_dependent_expr (lhs);
6856 if (rhs)
6857 rhs = build_non_dependent_expr (rhs);
6858 if (v)
6859 v = build_non_dependent_expr (v);
6860 if (lhs1)
6861 lhs1 = build_non_dependent_expr (lhs1);
6862 if (rhs1)
6863 rhs1 = build_non_dependent_expr (rhs1);
6866 if (!dependent_p)
6868 bool swapped = false;
6869 if (rhs1 && cp_tree_equal (lhs, rhs))
6871 std::swap (rhs, rhs1);
6872 swapped = !commutative_tree_code (opcode);
6874 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6876 if (code == OMP_ATOMIC)
6877 error ("%<#pragma omp atomic update%> uses two different "
6878 "expressions for memory");
6879 else
6880 error ("%<#pragma omp atomic capture%> uses two different "
6881 "expressions for memory");
6882 return;
6884 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6886 if (code == OMP_ATOMIC)
6887 error ("%<#pragma omp atomic update%> uses two different "
6888 "expressions for memory");
6889 else
6890 error ("%<#pragma omp atomic capture%> uses two different "
6891 "expressions for memory");
6892 return;
6894 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6895 v, lhs1, rhs1, swapped, seq_cst);
6896 if (stmt == error_mark_node)
6897 return;
6899 if (processing_template_decl)
6901 if (code == OMP_ATOMIC_READ)
6903 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6904 OMP_ATOMIC_READ, orig_lhs);
6905 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6906 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6908 else
6910 if (opcode == NOP_EXPR)
6911 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6912 else
6913 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6914 if (orig_rhs1)
6915 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6916 COMPOUND_EXPR, orig_rhs1, stmt);
6917 if (code != OMP_ATOMIC)
6919 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6920 code, orig_lhs1, stmt);
6921 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6922 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6925 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6926 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6928 finish_expr_stmt (stmt);
6931 void
6932 finish_omp_barrier (void)
6934 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6935 vec<tree, va_gc> *vec = make_tree_vector ();
6936 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6937 release_tree_vector (vec);
6938 finish_expr_stmt (stmt);
6941 void
6942 finish_omp_flush (void)
6944 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6945 vec<tree, va_gc> *vec = make_tree_vector ();
6946 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6947 release_tree_vector (vec);
6948 finish_expr_stmt (stmt);
6951 void
6952 finish_omp_taskwait (void)
6954 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6955 vec<tree, va_gc> *vec = make_tree_vector ();
6956 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6957 release_tree_vector (vec);
6958 finish_expr_stmt (stmt);
6961 void
6962 finish_omp_taskyield (void)
6964 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6965 vec<tree, va_gc> *vec = make_tree_vector ();
6966 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6967 release_tree_vector (vec);
6968 finish_expr_stmt (stmt);
6971 void
6972 finish_omp_cancel (tree clauses)
6974 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6975 int mask = 0;
6976 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6977 mask = 1;
6978 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6979 mask = 2;
6980 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6981 mask = 4;
6982 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6983 mask = 8;
6984 else
6986 error ("%<#pragma omp cancel must specify one of "
6987 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6988 return;
6990 vec<tree, va_gc> *vec = make_tree_vector ();
6991 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6992 if (ifc != NULL_TREE)
6994 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6995 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6996 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6997 build_zero_cst (type));
6999 else
7000 ifc = boolean_true_node;
7001 vec->quick_push (build_int_cst (integer_type_node, mask));
7002 vec->quick_push (ifc);
7003 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7004 release_tree_vector (vec);
7005 finish_expr_stmt (stmt);
7008 void
7009 finish_omp_cancellation_point (tree clauses)
7011 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7012 int mask = 0;
7013 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7014 mask = 1;
7015 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7016 mask = 2;
7017 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7018 mask = 4;
7019 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7020 mask = 8;
7021 else
7023 error ("%<#pragma omp cancellation point must specify one of "
7024 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7025 return;
7027 vec<tree, va_gc> *vec
7028 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7029 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7030 release_tree_vector (vec);
7031 finish_expr_stmt (stmt);
7034 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7035 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7036 should create an extra compound stmt. */
7038 tree
7039 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7041 tree r;
7043 if (pcompound)
7044 *pcompound = begin_compound_stmt (0);
7046 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7048 /* Only add the statement to the function if support enabled. */
7049 if (flag_tm)
7050 add_stmt (r);
7051 else
7052 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7053 ? G_("%<__transaction_relaxed%> without "
7054 "transactional memory support enabled")
7055 : G_("%<__transaction_atomic%> without "
7056 "transactional memory support enabled")));
7058 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7059 TREE_SIDE_EFFECTS (r) = 1;
7060 return r;
7063 /* End a __transaction_atomic or __transaction_relaxed statement.
7064 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7065 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7066 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7068 void
7069 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7071 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7072 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7073 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7074 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7076 /* noexcept specifications are not allowed for function transactions. */
7077 gcc_assert (!(noex && compound_stmt));
7078 if (noex)
7080 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7081 noex);
7082 /* This may not be true when the STATEMENT_LIST is empty. */
7083 if (EXPR_P (body))
7084 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7085 TREE_SIDE_EFFECTS (body) = 1;
7086 TRANSACTION_EXPR_BODY (stmt) = body;
7089 if (compound_stmt)
7090 finish_compound_stmt (compound_stmt);
7093 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7094 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7095 condition. */
7097 tree
7098 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7100 tree ret;
7101 if (noex)
7103 expr = build_must_not_throw_expr (expr, noex);
7104 if (EXPR_P (expr))
7105 SET_EXPR_LOCATION (expr, loc);
7106 TREE_SIDE_EFFECTS (expr) = 1;
7108 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7109 if (flags & TM_STMT_ATTR_RELAXED)
7110 TRANSACTION_EXPR_RELAXED (ret) = 1;
7111 TREE_SIDE_EFFECTS (ret) = 1;
7112 SET_EXPR_LOCATION (ret, loc);
7113 return ret;
7116 void
7117 init_cp_semantics (void)
7121 /* Build a STATIC_ASSERT for a static assertion with the condition
7122 CONDITION and the message text MESSAGE. LOCATION is the location
7123 of the static assertion in the source code. When MEMBER_P, this
7124 static assertion is a member of a class. */
7125 void
7126 finish_static_assert (tree condition, tree message, location_t location,
7127 bool member_p)
7129 if (message == NULL_TREE
7130 || message == error_mark_node
7131 || condition == NULL_TREE
7132 || condition == error_mark_node)
7133 return;
7135 if (check_for_bare_parameter_packs (condition))
7136 condition = error_mark_node;
7138 if (type_dependent_expression_p (condition)
7139 || value_dependent_expression_p (condition))
7141 /* We're in a template; build a STATIC_ASSERT and put it in
7142 the right place. */
7143 tree assertion;
7145 assertion = make_node (STATIC_ASSERT);
7146 STATIC_ASSERT_CONDITION (assertion) = condition;
7147 STATIC_ASSERT_MESSAGE (assertion) = message;
7148 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7150 if (member_p)
7151 maybe_add_class_template_decl_list (current_class_type,
7152 assertion,
7153 /*friend_p=*/0);
7154 else
7155 add_stmt (assertion);
7157 return;
7160 /* Fold the expression and convert it to a boolean value. */
7161 condition = instantiate_non_dependent_expr (condition);
7162 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7163 condition = maybe_constant_value (condition);
7165 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7166 /* Do nothing; the condition is satisfied. */
7168 else
7170 location_t saved_loc = input_location;
7172 input_location = location;
7173 if (TREE_CODE (condition) == INTEGER_CST
7174 && integer_zerop (condition))
7176 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
7177 (TREE_TYPE (TREE_TYPE (message))));
7178 int len = TREE_STRING_LENGTH (message) / sz - 1;
7179 /* Report the error. */
7180 if (len == 0)
7181 error ("static assertion failed");
7182 else
7183 error ("static assertion failed: %s",
7184 TREE_STRING_POINTER (message));
7186 else if (condition && condition != error_mark_node)
7188 error ("non-constant condition for static assertion");
7189 if (require_potential_rvalue_constant_expression (condition))
7190 cxx_constant_value (condition);
7192 input_location = saved_loc;
7196 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7197 suitable for use as a type-specifier.
7199 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7200 id-expression or a class member access, FALSE when it was parsed as
7201 a full expression. */
7203 tree
7204 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7205 tsubst_flags_t complain)
7207 tree type = NULL_TREE;
7209 if (!expr || error_operand_p (expr))
7210 return error_mark_node;
7212 if (TYPE_P (expr)
7213 || TREE_CODE (expr) == TYPE_DECL
7214 || (TREE_CODE (expr) == BIT_NOT_EXPR
7215 && TYPE_P (TREE_OPERAND (expr, 0))))
7217 if (complain & tf_error)
7218 error ("argument to decltype must be an expression");
7219 return error_mark_node;
7222 /* Depending on the resolution of DR 1172, we may later need to distinguish
7223 instantiation-dependent but not type-dependent expressions so that, say,
7224 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7225 if (instantiation_dependent_expression_p (expr))
7227 type = cxx_make_type (DECLTYPE_TYPE);
7228 DECLTYPE_TYPE_EXPR (type) = expr;
7229 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7230 = id_expression_or_member_access_p;
7231 SET_TYPE_STRUCTURAL_EQUALITY (type);
7233 return type;
7236 /* The type denoted by decltype(e) is defined as follows: */
7238 expr = resolve_nondeduced_context (expr);
7240 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
7241 return error_mark_node;
7243 if (type_unknown_p (expr))
7245 if (complain & tf_error)
7246 error ("decltype cannot resolve address of overloaded function");
7247 return error_mark_node;
7250 /* To get the size of a static data member declared as an array of
7251 unknown bound, we need to instantiate it. */
7252 if (VAR_P (expr)
7253 && VAR_HAD_UNKNOWN_BOUND (expr)
7254 && DECL_TEMPLATE_INSTANTIATION (expr))
7255 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7257 if (id_expression_or_member_access_p)
7259 /* If e is an id-expression or a class member access (5.2.5
7260 [expr.ref]), decltype(e) is defined as the type of the entity
7261 named by e. If there is no such entity, or e names a set of
7262 overloaded functions, the program is ill-formed. */
7263 if (identifier_p (expr))
7264 expr = lookup_name (expr);
7266 if (INDIRECT_REF_P (expr))
7267 /* This can happen when the expression is, e.g., "a.b". Just
7268 look at the underlying operand. */
7269 expr = TREE_OPERAND (expr, 0);
7271 if (TREE_CODE (expr) == OFFSET_REF
7272 || TREE_CODE (expr) == MEMBER_REF
7273 || TREE_CODE (expr) == SCOPE_REF)
7274 /* We're only interested in the field itself. If it is a
7275 BASELINK, we will need to see through it in the next
7276 step. */
7277 expr = TREE_OPERAND (expr, 1);
7279 if (BASELINK_P (expr))
7280 /* See through BASELINK nodes to the underlying function. */
7281 expr = BASELINK_FUNCTIONS (expr);
7283 switch (TREE_CODE (expr))
7285 case FIELD_DECL:
7286 if (DECL_BIT_FIELD_TYPE (expr))
7288 type = DECL_BIT_FIELD_TYPE (expr);
7289 break;
7291 /* Fall through for fields that aren't bitfields. */
7293 case FUNCTION_DECL:
7294 case VAR_DECL:
7295 case CONST_DECL:
7296 case PARM_DECL:
7297 case RESULT_DECL:
7298 case TEMPLATE_PARM_INDEX:
7299 expr = mark_type_use (expr);
7300 type = TREE_TYPE (expr);
7301 break;
7303 case ERROR_MARK:
7304 type = error_mark_node;
7305 break;
7307 case COMPONENT_REF:
7308 case COMPOUND_EXPR:
7309 mark_type_use (expr);
7310 type = is_bitfield_expr_with_lowered_type (expr);
7311 if (!type)
7312 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7313 break;
7315 case BIT_FIELD_REF:
7316 gcc_unreachable ();
7318 case INTEGER_CST:
7319 case PTRMEM_CST:
7320 /* We can get here when the id-expression refers to an
7321 enumerator or non-type template parameter. */
7322 type = TREE_TYPE (expr);
7323 break;
7325 default:
7326 /* Handle instantiated template non-type arguments. */
7327 type = TREE_TYPE (expr);
7328 break;
7331 else
7333 /* Within a lambda-expression:
7335 Every occurrence of decltype((x)) where x is a possibly
7336 parenthesized id-expression that names an entity of
7337 automatic storage duration is treated as if x were
7338 transformed into an access to a corresponding data member
7339 of the closure type that would have been declared if x
7340 were a use of the denoted entity. */
7341 if (outer_automatic_var_p (expr)
7342 && current_function_decl
7343 && LAMBDA_FUNCTION_P (current_function_decl))
7344 type = capture_decltype (expr);
7345 else if (error_operand_p (expr))
7346 type = error_mark_node;
7347 else if (expr == current_class_ptr)
7348 /* If the expression is just "this", we want the
7349 cv-unqualified pointer for the "this" type. */
7350 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7351 else
7353 /* Otherwise, where T is the type of e, if e is an lvalue,
7354 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7355 cp_lvalue_kind clk = lvalue_kind (expr);
7356 type = unlowered_expr_type (expr);
7357 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7359 /* For vector types, pick a non-opaque variant. */
7360 if (TREE_CODE (type) == VECTOR_TYPE)
7361 type = strip_typedefs (type);
7363 if (clk != clk_none && !(clk & clk_class))
7364 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7368 return type;
7371 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7372 __has_nothrow_copy, depending on assign_p. */
7374 static bool
7375 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7377 tree fns;
7379 if (assign_p)
7381 int ix;
7382 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7383 if (ix < 0)
7384 return false;
7385 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7387 else if (TYPE_HAS_COPY_CTOR (type))
7389 /* If construction of the copy constructor was postponed, create
7390 it now. */
7391 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7392 lazily_declare_fn (sfk_copy_constructor, type);
7393 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7394 lazily_declare_fn (sfk_move_constructor, type);
7395 fns = CLASSTYPE_CONSTRUCTORS (type);
7397 else
7398 return false;
7400 for (; fns; fns = OVL_NEXT (fns))
7402 tree fn = OVL_CURRENT (fns);
7404 if (assign_p)
7406 if (copy_fn_p (fn) == 0)
7407 continue;
7409 else if (copy_fn_p (fn) <= 0)
7410 continue;
7412 maybe_instantiate_noexcept (fn);
7413 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7414 return false;
7417 return true;
7420 /* Actually evaluates the trait. */
7422 static bool
7423 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7425 enum tree_code type_code1;
7426 tree t;
7428 type_code1 = TREE_CODE (type1);
7430 switch (kind)
7432 case CPTK_HAS_NOTHROW_ASSIGN:
7433 type1 = strip_array_types (type1);
7434 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7435 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7436 || (CLASS_TYPE_P (type1)
7437 && classtype_has_nothrow_assign_or_copy_p (type1,
7438 true))));
7440 case CPTK_HAS_TRIVIAL_ASSIGN:
7441 /* ??? The standard seems to be missing the "or array of such a class
7442 type" wording for this trait. */
7443 type1 = strip_array_types (type1);
7444 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7445 && (trivial_type_p (type1)
7446 || (CLASS_TYPE_P (type1)
7447 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7449 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7450 type1 = strip_array_types (type1);
7451 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7452 || (CLASS_TYPE_P (type1)
7453 && (t = locate_ctor (type1))
7454 && (maybe_instantiate_noexcept (t),
7455 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7457 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7458 type1 = strip_array_types (type1);
7459 return (trivial_type_p (type1)
7460 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7462 case CPTK_HAS_NOTHROW_COPY:
7463 type1 = strip_array_types (type1);
7464 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7465 || (CLASS_TYPE_P (type1)
7466 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7468 case CPTK_HAS_TRIVIAL_COPY:
7469 /* ??? The standard seems to be missing the "or array of such a class
7470 type" wording for this trait. */
7471 type1 = strip_array_types (type1);
7472 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7473 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7475 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7476 type1 = strip_array_types (type1);
7477 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7478 || (CLASS_TYPE_P (type1)
7479 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7481 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7482 return type_has_virtual_destructor (type1);
7484 case CPTK_IS_ABSTRACT:
7485 return (ABSTRACT_CLASS_TYPE_P (type1));
7487 case CPTK_IS_BASE_OF:
7488 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7489 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7490 || DERIVED_FROM_P (type1, type2)));
7492 case CPTK_IS_CLASS:
7493 return (NON_UNION_CLASS_TYPE_P (type1));
7495 case CPTK_IS_EMPTY:
7496 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7498 case CPTK_IS_ENUM:
7499 return (type_code1 == ENUMERAL_TYPE);
7501 case CPTK_IS_FINAL:
7502 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7504 case CPTK_IS_LITERAL_TYPE:
7505 return (literal_type_p (type1));
7507 case CPTK_IS_POD:
7508 return (pod_type_p (type1));
7510 case CPTK_IS_POLYMORPHIC:
7511 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7513 case CPTK_IS_STD_LAYOUT:
7514 return (std_layout_type_p (type1));
7516 case CPTK_IS_TRIVIAL:
7517 return (trivial_type_p (type1));
7519 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7520 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7522 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7523 return is_trivially_xible (INIT_EXPR, type1, type2);
7525 case CPTK_IS_TRIVIALLY_COPYABLE:
7526 return (trivially_copyable_p (type1));
7528 case CPTK_IS_UNION:
7529 return (type_code1 == UNION_TYPE);
7531 default:
7532 gcc_unreachable ();
7533 return false;
7537 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7538 void, or a complete type, returns true, otherwise false. */
7540 static bool
7541 check_trait_type (tree type)
7543 if (type == NULL_TREE)
7544 return true;
7546 if (TREE_CODE (type) == TREE_LIST)
7547 return (check_trait_type (TREE_VALUE (type))
7548 && check_trait_type (TREE_CHAIN (type)));
7550 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7551 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7552 return true;
7554 if (VOID_TYPE_P (type))
7555 return true;
7557 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7560 /* Process a trait expression. */
7562 tree
7563 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7565 if (type1 == error_mark_node
7566 || type2 == error_mark_node)
7567 return error_mark_node;
7569 if (processing_template_decl)
7571 tree trait_expr = make_node (TRAIT_EXPR);
7572 TREE_TYPE (trait_expr) = boolean_type_node;
7573 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7574 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7575 TRAIT_EXPR_KIND (trait_expr) = kind;
7576 return trait_expr;
7579 switch (kind)
7581 case CPTK_HAS_NOTHROW_ASSIGN:
7582 case CPTK_HAS_TRIVIAL_ASSIGN:
7583 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7584 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7585 case CPTK_HAS_NOTHROW_COPY:
7586 case CPTK_HAS_TRIVIAL_COPY:
7587 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7588 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7589 case CPTK_IS_ABSTRACT:
7590 case CPTK_IS_EMPTY:
7591 case CPTK_IS_FINAL:
7592 case CPTK_IS_LITERAL_TYPE:
7593 case CPTK_IS_POD:
7594 case CPTK_IS_POLYMORPHIC:
7595 case CPTK_IS_STD_LAYOUT:
7596 case CPTK_IS_TRIVIAL:
7597 case CPTK_IS_TRIVIALLY_COPYABLE:
7598 if (!check_trait_type (type1))
7599 return error_mark_node;
7600 break;
7602 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7603 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7604 if (!check_trait_type (type1)
7605 || !check_trait_type (type2))
7606 return error_mark_node;
7607 break;
7609 case CPTK_IS_BASE_OF:
7610 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7611 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7612 && !complete_type_or_else (type2, NULL_TREE))
7613 /* We already issued an error. */
7614 return error_mark_node;
7615 break;
7617 case CPTK_IS_CLASS:
7618 case CPTK_IS_ENUM:
7619 case CPTK_IS_UNION:
7620 break;
7622 default:
7623 gcc_unreachable ();
7626 return (trait_expr_value (kind, type1, type2)
7627 ? boolean_true_node : boolean_false_node);
7630 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7631 which is ignored for C++. */
7633 void
7634 set_float_const_decimal64 (void)
7638 void
7639 clear_float_const_decimal64 (void)
7643 bool
7644 float_const_decimal64_p (void)
7646 return 0;
7650 /* Return true if T designates the implied `this' parameter. */
7652 bool
7653 is_this_parameter (tree t)
7655 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7656 return false;
7657 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7658 return true;
7661 /* Insert the deduced return type for an auto function. */
7663 void
7664 apply_deduced_return_type (tree fco, tree return_type)
7666 tree result;
7668 if (return_type == error_mark_node)
7669 return;
7671 if (LAMBDA_FUNCTION_P (fco))
7673 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7674 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7677 if (DECL_CONV_FN_P (fco))
7678 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7680 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7682 result = DECL_RESULT (fco);
7683 if (result == NULL_TREE)
7684 return;
7685 if (TREE_TYPE (result) == return_type)
7686 return;
7688 /* We already have a DECL_RESULT from start_preparsed_function.
7689 Now we need to redo the work it and allocate_struct_function
7690 did to reflect the new type. */
7691 gcc_assert (current_function_decl == fco);
7692 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7693 TYPE_MAIN_VARIANT (return_type));
7694 DECL_ARTIFICIAL (result) = 1;
7695 DECL_IGNORED_P (result) = 1;
7696 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7697 result);
7699 DECL_RESULT (fco) = result;
7701 if (!processing_template_decl)
7703 if (!VOID_TYPE_P (TREE_TYPE (result)))
7704 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7705 bool aggr = aggregate_value_p (result, fco);
7706 #ifdef PCC_STATIC_STRUCT_RETURN
7707 cfun->returns_pcc_struct = aggr;
7708 #endif
7709 cfun->returns_struct = aggr;
7714 /* DECL is a local variable or parameter from the surrounding scope of a
7715 lambda-expression. Returns the decltype for a use of the capture field
7716 for DECL even if it hasn't been captured yet. */
7718 static tree
7719 capture_decltype (tree decl)
7721 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7722 /* FIXME do lookup instead of list walk? */
7723 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7724 tree type;
7726 if (cap)
7727 type = TREE_TYPE (TREE_PURPOSE (cap));
7728 else
7729 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7731 case CPLD_NONE:
7732 error ("%qD is not captured", decl);
7733 return error_mark_node;
7735 case CPLD_COPY:
7736 type = TREE_TYPE (decl);
7737 if (TREE_CODE (type) == REFERENCE_TYPE
7738 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7739 type = TREE_TYPE (type);
7740 break;
7742 case CPLD_REFERENCE:
7743 type = TREE_TYPE (decl);
7744 if (TREE_CODE (type) != REFERENCE_TYPE)
7745 type = build_reference_type (TREE_TYPE (decl));
7746 break;
7748 default:
7749 gcc_unreachable ();
7752 if (TREE_CODE (type) != REFERENCE_TYPE)
7754 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7755 type = cp_build_qualified_type (type, (cp_type_quals (type)
7756 |TYPE_QUAL_CONST));
7757 type = build_reference_type (type);
7759 return type;
7762 #include "gt-cp-semantics.h"