2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / semantics.c
blobaeb5f7ba29848db5c039a0db46027ffe0c0c2d38
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 "plugin-api.h"
47 #include "hard-reg-set.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "tree-iterator.h"
52 #include "target.h"
53 #include "gimplify.h"
54 #include "bitmap.h"
55 #include "omp-low.h"
56 #include "builtins.h"
57 #include "convert.h"
58 #include "gomp-constants.h"
60 /* There routines provide a modular interface to perform many parsing
61 operations. They may therefore be used during actual parsing, or
62 during template instantiation, which may be regarded as a
63 degenerate form of parsing. */
65 static tree maybe_convert_cond (tree);
66 static tree finalize_nrv_r (tree *, int *, void *);
67 static tree capture_decltype (tree);
70 /* Deferred Access Checking Overview
71 ---------------------------------
73 Most C++ expressions and declarations require access checking
74 to be performed during parsing. However, in several cases,
75 this has to be treated differently.
77 For member declarations, access checking has to be deferred
78 until more information about the declaration is known. For
79 example:
81 class A {
82 typedef int X;
83 public:
84 X f();
87 A::X A::f();
88 A::X g();
90 When we are parsing the function return type `A::X', we don't
91 really know if this is allowed until we parse the function name.
93 Furthermore, some contexts require that access checking is
94 never performed at all. These include class heads, and template
95 instantiations.
97 Typical use of access checking functions is described here:
99 1. When we enter a context that requires certain access checking
100 mode, the function `push_deferring_access_checks' is called with
101 DEFERRING argument specifying the desired mode. Access checking
102 may be performed immediately (dk_no_deferred), deferred
103 (dk_deferred), or not performed (dk_no_check).
105 2. When a declaration such as a type, or a variable, is encountered,
106 the function `perform_or_defer_access_check' is called. It
107 maintains a vector of all deferred checks.
109 3. The global `current_class_type' or `current_function_decl' is then
110 setup by the parser. `enforce_access' relies on these information
111 to check access.
113 4. Upon exiting the context mentioned in step 1,
114 `perform_deferred_access_checks' is called to check all declaration
115 stored in the vector. `pop_deferring_access_checks' is then
116 called to restore the previous access checking mode.
118 In case of parsing error, we simply call `pop_deferring_access_checks'
119 without `perform_deferred_access_checks'. */
121 typedef struct GTY(()) deferred_access {
122 /* A vector representing name-lookups for which we have deferred
123 checking access controls. We cannot check the accessibility of
124 names used in a decl-specifier-seq until we know what is being
125 declared because code like:
127 class A {
128 class B {};
129 B* f();
132 A::B* A::f() { return 0; }
134 is valid, even though `A::B' is not generally accessible. */
135 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
137 /* The current mode of access checks. */
138 enum deferring_kind deferring_access_checks_kind;
140 } deferred_access;
142 /* Data for deferred access checking. */
143 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
144 static GTY(()) unsigned deferred_access_no_check;
146 /* Save the current deferred access states and start deferred
147 access checking iff DEFER_P is true. */
149 void
150 push_deferring_access_checks (deferring_kind deferring)
152 /* For context like template instantiation, access checking
153 disabling applies to all nested context. */
154 if (deferred_access_no_check || deferring == dk_no_check)
155 deferred_access_no_check++;
156 else
158 deferred_access e = {NULL, deferring};
159 vec_safe_push (deferred_access_stack, e);
163 /* Save the current deferred access states and start deferred access
164 checking, continuing the set of deferred checks in CHECKS. */
166 void
167 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
169 push_deferring_access_checks (dk_deferred);
170 if (!deferred_access_no_check)
171 deferred_access_stack->last().deferred_access_checks = checks;
174 /* Resume deferring access checks again after we stopped doing
175 this previously. */
177 void
178 resume_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
184 /* Stop deferring access checks. */
186 void
187 stop_deferring_access_checks (void)
189 if (!deferred_access_no_check)
190 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
193 /* Discard the current deferred access checks and restore the
194 previous states. */
196 void
197 pop_deferring_access_checks (void)
199 if (deferred_access_no_check)
200 deferred_access_no_check--;
201 else
202 deferred_access_stack->pop ();
205 /* Returns a TREE_LIST representing the deferred checks.
206 The TREE_PURPOSE of each node is the type through which the
207 access occurred; the TREE_VALUE is the declaration named.
210 vec<deferred_access_check, va_gc> *
211 get_deferred_access_checks (void)
213 if (deferred_access_no_check)
214 return NULL;
215 else
216 return (deferred_access_stack->last().deferred_access_checks);
219 /* Take current deferred checks and combine with the
220 previous states if we also defer checks previously.
221 Otherwise perform checks now. */
223 void
224 pop_to_parent_deferring_access_checks (void)
226 if (deferred_access_no_check)
227 deferred_access_no_check--;
228 else
230 vec<deferred_access_check, va_gc> *checks;
231 deferred_access *ptr;
233 checks = (deferred_access_stack->last ().deferred_access_checks);
235 deferred_access_stack->pop ();
236 ptr = &deferred_access_stack->last ();
237 if (ptr->deferring_access_checks_kind == dk_no_deferred)
239 /* Check access. */
240 perform_access_checks (checks, tf_warning_or_error);
242 else
244 /* Merge with parent. */
245 int i, j;
246 deferred_access_check *chk, *probe;
248 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
250 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
252 if (probe->binfo == chk->binfo &&
253 probe->decl == chk->decl &&
254 probe->diag_decl == chk->diag_decl)
255 goto found;
257 /* Insert into parent's checks. */
258 vec_safe_push (ptr->deferred_access_checks, *chk);
259 found:;
265 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
266 is the BINFO indicating the qualifying scope used to access the
267 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
268 or we aren't in SFINAE context or all the checks succeed return TRUE,
269 otherwise FALSE. */
271 bool
272 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
273 tsubst_flags_t complain)
275 int i;
276 deferred_access_check *chk;
277 location_t loc = input_location;
278 bool ok = true;
280 if (!checks)
281 return true;
283 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
285 input_location = chk->loc;
286 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
289 input_location = loc;
290 return (complain & tf_error) ? true : ok;
293 /* Perform the deferred access checks.
295 After performing the checks, we still have to keep the list
296 `deferred_access_stack->deferred_access_checks' since we may want
297 to check access for them again later in a different context.
298 For example:
300 class A {
301 typedef int X;
302 static X a;
304 A::X A::a, x; // No error for `A::a', error for `x'
306 We have to perform deferred access of `A::X', first with `A::a',
307 next with `x'. Return value like perform_access_checks above. */
309 bool
310 perform_deferred_access_checks (tsubst_flags_t complain)
312 return perform_access_checks (get_deferred_access_checks (), complain);
315 /* Defer checking the accessibility of DECL, when looked up in
316 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
317 Return value like perform_access_checks above. */
319 bool
320 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
321 tsubst_flags_t complain)
323 int i;
324 deferred_access *ptr;
325 deferred_access_check *chk;
328 /* Exit if we are in a context that no access checking is performed.
330 if (deferred_access_no_check)
331 return true;
333 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
335 ptr = &deferred_access_stack->last ();
337 /* If we are not supposed to defer access checks, just check now. */
338 if (ptr->deferring_access_checks_kind == dk_no_deferred)
340 bool ok = enforce_access (binfo, decl, diag_decl, complain);
341 return (complain & tf_error) ? true : ok;
344 /* See if we are already going to perform this check. */
345 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
347 if (chk->decl == decl && chk->binfo == binfo &&
348 chk->diag_decl == diag_decl)
350 return true;
353 /* If not, record the check. */
354 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
355 vec_safe_push (ptr->deferred_access_checks, new_access);
357 return true;
360 /* Returns nonzero if the current statement is a full expression,
361 i.e. temporaries created during that statement should be destroyed
362 at the end of the statement. */
365 stmts_are_full_exprs_p (void)
367 return current_stmt_tree ()->stmts_are_full_exprs_p;
370 /* T is a statement. Add it to the statement-tree. This is the C++
371 version. The C/ObjC frontends have a slightly different version of
372 this function. */
374 tree
375 add_stmt (tree t)
377 enum tree_code code = TREE_CODE (t);
379 if (EXPR_P (t) && code != LABEL_EXPR)
381 if (!EXPR_HAS_LOCATION (t))
382 SET_EXPR_LOCATION (t, input_location);
384 /* When we expand a statement-tree, we must know whether or not the
385 statements are full-expressions. We record that fact here. */
386 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
389 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
390 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
392 /* Add T to the statement-tree. Non-side-effect statements need to be
393 recorded during statement expressions. */
394 gcc_checking_assert (!stmt_list_stack->is_empty ());
395 append_to_statement_list_force (t, &cur_stmt_list);
397 return t;
400 /* Returns the stmt_tree to which statements are currently being added. */
402 stmt_tree
403 current_stmt_tree (void)
405 return (cfun
406 ? &cfun->language->base.x_stmt_tree
407 : &scope_chain->x_stmt_tree);
410 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
412 static tree
413 maybe_cleanup_point_expr (tree expr)
415 if (!processing_template_decl && stmts_are_full_exprs_p ())
416 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
417 return expr;
420 /* Like maybe_cleanup_point_expr except have the type of the new expression be
421 void so we don't need to create a temporary variable to hold the inner
422 expression. The reason why we do this is because the original type might be
423 an aggregate and we cannot create a temporary variable for that type. */
425 tree
426 maybe_cleanup_point_expr_void (tree expr)
428 if (!processing_template_decl && stmts_are_full_exprs_p ())
429 expr = fold_build_cleanup_point_expr (void_type_node, expr);
430 return expr;
435 /* Create a declaration statement for the declaration given by the DECL. */
437 void
438 add_decl_expr (tree decl)
440 tree r = build_stmt (input_location, DECL_EXPR, decl);
441 if (DECL_INITIAL (decl)
442 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
443 r = maybe_cleanup_point_expr_void (r);
444 add_stmt (r);
447 /* Finish a scope. */
449 tree
450 do_poplevel (tree stmt_list)
452 tree block = NULL;
454 if (stmts_are_full_exprs_p ())
455 block = poplevel (kept_level_p (), 1, 0);
457 stmt_list = pop_stmt_list (stmt_list);
459 if (!processing_template_decl)
461 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
462 /* ??? See c_end_compound_stmt re statement expressions. */
465 return stmt_list;
468 /* Begin a new scope. */
470 static tree
471 do_pushlevel (scope_kind sk)
473 tree ret = push_stmt_list ();
474 if (stmts_are_full_exprs_p ())
475 begin_scope (sk, NULL);
476 return ret;
479 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
480 when the current scope is exited. EH_ONLY is true when this is not
481 meant to apply to normal control flow transfer. */
483 void
484 push_cleanup (tree decl, tree cleanup, bool eh_only)
486 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
487 CLEANUP_EH_ONLY (stmt) = eh_only;
488 add_stmt (stmt);
489 CLEANUP_BODY (stmt) = push_stmt_list ();
492 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
493 the current loops, represented by 'NULL_TREE' if we've seen a possible
494 exit, and 'error_mark_node' if not. This is currently used only to
495 suppress the warning about a function with no return statements, and
496 therefore we don't bother noting returns as possible exits. We also
497 don't bother with gotos. */
499 static void
500 begin_maybe_infinite_loop (tree cond)
502 /* Only track this while parsing a function, not during instantiation. */
503 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
504 && !processing_template_decl))
505 return;
506 bool maybe_infinite = true;
507 if (cond)
509 cond = fold_non_dependent_expr (cond);
510 maybe_infinite = integer_nonzerop (cond);
512 vec_safe_push (cp_function_chain->infinite_loops,
513 maybe_infinite ? error_mark_node : NULL_TREE);
517 /* A break is a possible exit for the current loop. */
519 void
520 break_maybe_infinite_loop (void)
522 if (!cfun)
523 return;
524 cp_function_chain->infinite_loops->last() = NULL_TREE;
527 /* If we reach the end of the loop without seeing a possible exit, we have
528 an infinite loop. */
530 static void
531 end_maybe_infinite_loop (tree cond)
533 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
534 && !processing_template_decl))
535 return;
536 tree current = cp_function_chain->infinite_loops->pop();
537 if (current != NULL_TREE)
539 cond = fold_non_dependent_expr (cond);
540 if (integer_nonzerop (cond))
541 current_function_infinite_loop = 1;
546 /* Begin a conditional that might contain a declaration. When generating
547 normal code, we want the declaration to appear before the statement
548 containing the conditional. When generating template code, we want the
549 conditional to be rendered as the raw DECL_EXPR. */
551 static void
552 begin_cond (tree *cond_p)
554 if (processing_template_decl)
555 *cond_p = push_stmt_list ();
558 /* Finish such a conditional. */
560 static void
561 finish_cond (tree *cond_p, tree expr)
563 if (processing_template_decl)
565 tree cond = pop_stmt_list (*cond_p);
567 if (expr == NULL_TREE)
568 /* Empty condition in 'for'. */
569 gcc_assert (empty_expr_stmt_p (cond));
570 else if (check_for_bare_parameter_packs (expr))
571 expr = error_mark_node;
572 else if (!empty_expr_stmt_p (cond))
573 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
575 *cond_p = expr;
578 /* If *COND_P specifies a conditional with a declaration, transform the
579 loop such that
580 while (A x = 42) { }
581 for (; A x = 42;) { }
582 becomes
583 while (true) { A x = 42; if (!x) break; }
584 for (;;) { A x = 42; if (!x) break; }
585 The statement list for BODY will be empty if the conditional did
586 not declare anything. */
588 static void
589 simplify_loop_decl_cond (tree *cond_p, tree body)
591 tree cond, if_stmt;
593 if (!TREE_SIDE_EFFECTS (body))
594 return;
596 cond = *cond_p;
597 *cond_p = boolean_true_node;
599 if_stmt = begin_if_stmt ();
600 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
601 finish_if_stmt_cond (cond, if_stmt);
602 finish_break_stmt ();
603 finish_then_clause (if_stmt);
604 finish_if_stmt (if_stmt);
607 /* Finish a goto-statement. */
609 tree
610 finish_goto_stmt (tree destination)
612 if (identifier_p (destination))
613 destination = lookup_label (destination);
615 /* We warn about unused labels with -Wunused. That means we have to
616 mark the used labels as used. */
617 if (TREE_CODE (destination) == LABEL_DECL)
618 TREE_USED (destination) = 1;
619 else
621 if (check_no_cilk (destination,
622 "Cilk array notation cannot be used as a computed goto expression",
623 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
624 destination = error_mark_node;
625 destination = mark_rvalue_use (destination);
626 if (!processing_template_decl)
628 destination = cp_convert (ptr_type_node, destination,
629 tf_warning_or_error);
630 if (error_operand_p (destination))
631 return NULL_TREE;
632 destination
633 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
634 destination);
638 check_goto (destination);
640 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
643 /* COND is the condition-expression for an if, while, etc.,
644 statement. Convert it to a boolean value, if appropriate.
645 In addition, verify sequence points if -Wsequence-point is enabled. */
647 static tree
648 maybe_convert_cond (tree cond)
650 /* Empty conditions remain empty. */
651 if (!cond)
652 return NULL_TREE;
654 /* Wait until we instantiate templates before doing conversion. */
655 if (processing_template_decl)
656 return cond;
658 if (warn_sequence_point)
659 verify_sequence_points (cond);
661 /* Do the conversion. */
662 cond = convert_from_reference (cond);
664 if (TREE_CODE (cond) == MODIFY_EXPR
665 && !TREE_NO_WARNING (cond)
666 && warn_parentheses)
668 warning (OPT_Wparentheses,
669 "suggest parentheses around assignment used as truth value");
670 TREE_NO_WARNING (cond) = 1;
673 return condition_conversion (cond);
676 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
678 tree
679 finish_expr_stmt (tree expr)
681 tree r = NULL_TREE;
683 if (expr != NULL_TREE)
685 if (!processing_template_decl)
687 if (warn_sequence_point)
688 verify_sequence_points (expr);
689 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
691 else if (!type_dependent_expression_p (expr))
692 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
693 tf_warning_or_error);
695 if (check_for_bare_parameter_packs (expr))
696 expr = error_mark_node;
698 /* Simplification of inner statement expressions, compound exprs,
699 etc can result in us already having an EXPR_STMT. */
700 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
702 if (TREE_CODE (expr) != EXPR_STMT)
703 expr = build_stmt (input_location, EXPR_STMT, expr);
704 expr = maybe_cleanup_point_expr_void (expr);
707 r = add_stmt (expr);
710 return r;
714 /* Begin an if-statement. Returns a newly created IF_STMT if
715 appropriate. */
717 tree
718 begin_if_stmt (void)
720 tree r, scope;
721 scope = do_pushlevel (sk_cond);
722 r = build_stmt (input_location, IF_STMT, NULL_TREE,
723 NULL_TREE, NULL_TREE, scope);
724 begin_cond (&IF_COND (r));
725 return r;
728 /* Process the COND of an if-statement, which may be given by
729 IF_STMT. */
731 void
732 finish_if_stmt_cond (tree cond, tree if_stmt)
734 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
735 add_stmt (if_stmt);
736 THEN_CLAUSE (if_stmt) = push_stmt_list ();
739 /* Finish the then-clause of an if-statement, which may be given by
740 IF_STMT. */
742 tree
743 finish_then_clause (tree if_stmt)
745 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
746 return if_stmt;
749 /* Begin the else-clause of an if-statement. */
751 void
752 begin_else_clause (tree if_stmt)
754 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
757 /* Finish the else-clause of an if-statement, which may be given by
758 IF_STMT. */
760 void
761 finish_else_clause (tree if_stmt)
763 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
766 /* Finish an if-statement. */
768 void
769 finish_if_stmt (tree if_stmt)
771 tree scope = IF_SCOPE (if_stmt);
772 IF_SCOPE (if_stmt) = NULL;
773 add_stmt (do_poplevel (scope));
776 /* Begin a while-statement. Returns a newly created WHILE_STMT if
777 appropriate. */
779 tree
780 begin_while_stmt (void)
782 tree r;
783 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
784 add_stmt (r);
785 WHILE_BODY (r) = do_pushlevel (sk_block);
786 begin_cond (&WHILE_COND (r));
787 return r;
790 /* Process the COND of a while-statement, which may be given by
791 WHILE_STMT. */
793 void
794 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
796 if (check_no_cilk (cond,
797 "Cilk array notation cannot be used as a condition for while statement",
798 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
799 cond = error_mark_node;
800 cond = maybe_convert_cond (cond);
801 finish_cond (&WHILE_COND (while_stmt), cond);
802 begin_maybe_infinite_loop (cond);
803 if (ivdep && cond != error_mark_node)
804 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
805 TREE_TYPE (WHILE_COND (while_stmt)),
806 WHILE_COND (while_stmt),
807 build_int_cst (integer_type_node,
808 annot_expr_ivdep_kind));
809 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
812 /* Finish a while-statement, which may be given by WHILE_STMT. */
814 void
815 finish_while_stmt (tree while_stmt)
817 end_maybe_infinite_loop (boolean_true_node);
818 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
821 /* Begin a do-statement. Returns a newly created DO_STMT if
822 appropriate. */
824 tree
825 begin_do_stmt (void)
827 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
828 begin_maybe_infinite_loop (boolean_true_node);
829 add_stmt (r);
830 DO_BODY (r) = push_stmt_list ();
831 return r;
834 /* Finish the body of a do-statement, which may be given by DO_STMT. */
836 void
837 finish_do_body (tree do_stmt)
839 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
841 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
842 body = STATEMENT_LIST_TAIL (body)->stmt;
844 if (IS_EMPTY_STMT (body))
845 warning (OPT_Wempty_body,
846 "suggest explicit braces around empty body in %<do%> statement");
849 /* Finish a do-statement, which may be given by DO_STMT, and whose
850 COND is as indicated. */
852 void
853 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
855 if (check_no_cilk (cond,
856 "Cilk array notation cannot be used as a condition for a do-while statement",
857 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
858 cond = error_mark_node;
859 cond = maybe_convert_cond (cond);
860 end_maybe_infinite_loop (cond);
861 if (ivdep && cond != error_mark_node)
862 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
863 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
864 DO_COND (do_stmt) = cond;
867 /* Finish a return-statement. The EXPRESSION returned, if any, is as
868 indicated. */
870 tree
871 finish_return_stmt (tree expr)
873 tree r;
874 bool no_warning;
876 expr = check_return_expr (expr, &no_warning);
878 if (error_operand_p (expr)
879 || (flag_openmp && !check_omp_return ()))
881 /* Suppress -Wreturn-type for this function. */
882 if (warn_return_type)
883 TREE_NO_WARNING (current_function_decl) = true;
884 return error_mark_node;
887 if (!processing_template_decl)
889 if (warn_sequence_point)
890 verify_sequence_points (expr);
892 if (DECL_DESTRUCTOR_P (current_function_decl)
893 || (DECL_CONSTRUCTOR_P (current_function_decl)
894 && targetm.cxx.cdtor_returns_this ()))
896 /* Similarly, all destructors must run destructors for
897 base-classes before returning. So, all returns in a
898 destructor get sent to the DTOR_LABEL; finish_function emits
899 code to return a value there. */
900 return finish_goto_stmt (cdtor_label);
904 r = build_stmt (input_location, RETURN_EXPR, expr);
905 TREE_NO_WARNING (r) |= no_warning;
906 r = maybe_cleanup_point_expr_void (r);
907 r = add_stmt (r);
909 return r;
912 /* Begin the scope of a for-statement or a range-for-statement.
913 Both the returned trees are to be used in a call to
914 begin_for_stmt or begin_range_for_stmt. */
916 tree
917 begin_for_scope (tree *init)
919 tree scope = NULL_TREE;
920 if (flag_new_for_scope > 0)
921 scope = do_pushlevel (sk_for);
923 if (processing_template_decl)
924 *init = push_stmt_list ();
925 else
926 *init = NULL_TREE;
928 return scope;
931 /* Begin a for-statement. Returns a new FOR_STMT.
932 SCOPE and INIT should be the return of begin_for_scope,
933 or both NULL_TREE */
935 tree
936 begin_for_stmt (tree scope, tree init)
938 tree r;
940 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
941 NULL_TREE, NULL_TREE, NULL_TREE);
943 if (scope == NULL_TREE)
945 gcc_assert (!init || !(flag_new_for_scope > 0));
946 if (!init)
947 scope = begin_for_scope (&init);
949 FOR_INIT_STMT (r) = init;
950 FOR_SCOPE (r) = scope;
952 return r;
955 /* Finish the for-init-statement of a for-statement, which may be
956 given by FOR_STMT. */
958 void
959 finish_for_init_stmt (tree for_stmt)
961 if (processing_template_decl)
962 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
963 add_stmt (for_stmt);
964 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
965 begin_cond (&FOR_COND (for_stmt));
968 /* Finish the COND of a for-statement, which may be given by
969 FOR_STMT. */
971 void
972 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
974 if (check_no_cilk (cond,
975 "Cilk array notation cannot be used in a condition for a for-loop",
976 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
977 cond = error_mark_node;
978 cond = maybe_convert_cond (cond);
979 finish_cond (&FOR_COND (for_stmt), cond);
980 begin_maybe_infinite_loop (cond);
981 if (ivdep && cond != error_mark_node)
982 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
983 TREE_TYPE (FOR_COND (for_stmt)),
984 FOR_COND (for_stmt),
985 build_int_cst (integer_type_node,
986 annot_expr_ivdep_kind));
987 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
990 /* Finish the increment-EXPRESSION in a for-statement, which may be
991 given by FOR_STMT. */
993 void
994 finish_for_expr (tree expr, tree for_stmt)
996 if (!expr)
997 return;
998 /* If EXPR is an overloaded function, issue an error; there is no
999 context available to use to perform overload resolution. */
1000 if (type_unknown_p (expr))
1002 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1003 expr = error_mark_node;
1005 if (!processing_template_decl)
1007 if (warn_sequence_point)
1008 verify_sequence_points (expr);
1009 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1010 tf_warning_or_error);
1012 else if (!type_dependent_expression_p (expr))
1013 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1014 tf_warning_or_error);
1015 expr = maybe_cleanup_point_expr_void (expr);
1016 if (check_for_bare_parameter_packs (expr))
1017 expr = error_mark_node;
1018 FOR_EXPR (for_stmt) = expr;
1021 /* Finish the body of a for-statement, which may be given by
1022 FOR_STMT. The increment-EXPR for the loop must be
1023 provided.
1024 It can also finish RANGE_FOR_STMT. */
1026 void
1027 finish_for_stmt (tree for_stmt)
1029 end_maybe_infinite_loop (boolean_true_node);
1031 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1032 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1033 else
1034 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1036 /* Pop the scope for the body of the loop. */
1037 if (flag_new_for_scope > 0)
1039 tree scope;
1040 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1041 ? &RANGE_FOR_SCOPE (for_stmt)
1042 : &FOR_SCOPE (for_stmt));
1043 scope = *scope_ptr;
1044 *scope_ptr = NULL;
1045 add_stmt (do_poplevel (scope));
1049 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1050 SCOPE and INIT should be the return of begin_for_scope,
1051 or both NULL_TREE .
1052 To finish it call finish_for_stmt(). */
1054 tree
1055 begin_range_for_stmt (tree scope, tree init)
1057 tree r;
1059 begin_maybe_infinite_loop (boolean_false_node);
1061 r = build_stmt (input_location, RANGE_FOR_STMT,
1062 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1064 if (scope == NULL_TREE)
1066 gcc_assert (!init || !(flag_new_for_scope > 0));
1067 if (!init)
1068 scope = begin_for_scope (&init);
1071 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1072 pop it now. */
1073 if (init)
1074 pop_stmt_list (init);
1075 RANGE_FOR_SCOPE (r) = scope;
1077 return r;
1080 /* Finish the head of a range-based for statement, which may
1081 be given by RANGE_FOR_STMT. DECL must be the declaration
1082 and EXPR must be the loop expression. */
1084 void
1085 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1087 RANGE_FOR_DECL (range_for_stmt) = decl;
1088 RANGE_FOR_EXPR (range_for_stmt) = expr;
1089 add_stmt (range_for_stmt);
1090 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1093 /* Finish a break-statement. */
1095 tree
1096 finish_break_stmt (void)
1098 /* In switch statements break is sometimes stylistically used after
1099 a return statement. This can lead to spurious warnings about
1100 control reaching the end of a non-void function when it is
1101 inlined. Note that we are calling block_may_fallthru with
1102 language specific tree nodes; this works because
1103 block_may_fallthru returns true when given something it does not
1104 understand. */
1105 if (!block_may_fallthru (cur_stmt_list))
1106 return void_node;
1107 return add_stmt (build_stmt (input_location, BREAK_STMT));
1110 /* Finish a continue-statement. */
1112 tree
1113 finish_continue_stmt (void)
1115 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1118 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1119 appropriate. */
1121 tree
1122 begin_switch_stmt (void)
1124 tree r, scope;
1126 scope = do_pushlevel (sk_cond);
1127 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1129 begin_cond (&SWITCH_STMT_COND (r));
1131 return r;
1134 /* Finish the cond of a switch-statement. */
1136 void
1137 finish_switch_cond (tree cond, tree switch_stmt)
1139 tree orig_type = NULL;
1141 if (check_no_cilk (cond,
1142 "Cilk array notation cannot be used as a condition for switch statement",
1143 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1144 cond = error_mark_node;
1146 if (!processing_template_decl)
1148 /* Convert the condition to an integer or enumeration type. */
1149 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1150 if (cond == NULL_TREE)
1152 error ("switch quantity not an integer");
1153 cond = error_mark_node;
1155 /* We want unlowered type here to handle enum bit-fields. */
1156 orig_type = unlowered_expr_type (cond);
1157 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1158 orig_type = TREE_TYPE (cond);
1159 if (cond != error_mark_node)
1161 /* Warn if the condition has boolean value. */
1162 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1163 warning_at (input_location, OPT_Wswitch_bool,
1164 "switch condition has type bool");
1166 /* [stmt.switch]
1168 Integral promotions are performed. */
1169 cond = perform_integral_promotions (cond);
1170 cond = maybe_cleanup_point_expr (cond);
1173 if (check_for_bare_parameter_packs (cond))
1174 cond = error_mark_node;
1175 else if (!processing_template_decl && warn_sequence_point)
1176 verify_sequence_points (cond);
1178 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1179 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1180 add_stmt (switch_stmt);
1181 push_switch (switch_stmt);
1182 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1185 /* Finish the body of a switch-statement, which may be given by
1186 SWITCH_STMT. The COND to switch on is indicated. */
1188 void
1189 finish_switch_stmt (tree switch_stmt)
1191 tree scope;
1193 SWITCH_STMT_BODY (switch_stmt) =
1194 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1195 pop_switch ();
1197 scope = SWITCH_STMT_SCOPE (switch_stmt);
1198 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1199 add_stmt (do_poplevel (scope));
1202 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1203 appropriate. */
1205 tree
1206 begin_try_block (void)
1208 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1209 add_stmt (r);
1210 TRY_STMTS (r) = push_stmt_list ();
1211 return r;
1214 /* Likewise, for a function-try-block. The block returned in
1215 *COMPOUND_STMT is an artificial outer scope, containing the
1216 function-try-block. */
1218 tree
1219 begin_function_try_block (tree *compound_stmt)
1221 tree r;
1222 /* This outer scope does not exist in the C++ standard, but we need
1223 a place to put __FUNCTION__ and similar variables. */
1224 *compound_stmt = begin_compound_stmt (0);
1225 r = begin_try_block ();
1226 FN_TRY_BLOCK_P (r) = 1;
1227 return r;
1230 /* Finish a try-block, which may be given by TRY_BLOCK. */
1232 void
1233 finish_try_block (tree try_block)
1235 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1236 TRY_HANDLERS (try_block) = push_stmt_list ();
1239 /* Finish the body of a cleanup try-block, which may be given by
1240 TRY_BLOCK. */
1242 void
1243 finish_cleanup_try_block (tree try_block)
1245 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1248 /* Finish an implicitly generated try-block, with a cleanup is given
1249 by CLEANUP. */
1251 void
1252 finish_cleanup (tree cleanup, tree try_block)
1254 TRY_HANDLERS (try_block) = cleanup;
1255 CLEANUP_P (try_block) = 1;
1258 /* Likewise, for a function-try-block. */
1260 void
1261 finish_function_try_block (tree try_block)
1263 finish_try_block (try_block);
1264 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1265 the try block, but moving it inside. */
1266 in_function_try_handler = 1;
1269 /* Finish a handler-sequence for a try-block, which may be given by
1270 TRY_BLOCK. */
1272 void
1273 finish_handler_sequence (tree try_block)
1275 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1276 check_handlers (TRY_HANDLERS (try_block));
1279 /* Finish the handler-seq for a function-try-block, given by
1280 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1281 begin_function_try_block. */
1283 void
1284 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1286 in_function_try_handler = 0;
1287 finish_handler_sequence (try_block);
1288 finish_compound_stmt (compound_stmt);
1291 /* Begin a handler. Returns a HANDLER if appropriate. */
1293 tree
1294 begin_handler (void)
1296 tree r;
1298 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1299 add_stmt (r);
1301 /* Create a binding level for the eh_info and the exception object
1302 cleanup. */
1303 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1305 return r;
1308 /* Finish the handler-parameters for a handler, which may be given by
1309 HANDLER. DECL is the declaration for the catch parameter, or NULL
1310 if this is a `catch (...)' clause. */
1312 void
1313 finish_handler_parms (tree decl, tree handler)
1315 tree type = NULL_TREE;
1316 if (processing_template_decl)
1318 if (decl)
1320 decl = pushdecl (decl);
1321 decl = push_template_decl (decl);
1322 HANDLER_PARMS (handler) = decl;
1323 type = TREE_TYPE (decl);
1326 else
1327 type = expand_start_catch_block (decl);
1328 HANDLER_TYPE (handler) = type;
1331 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1332 the return value from the matching call to finish_handler_parms. */
1334 void
1335 finish_handler (tree handler)
1337 if (!processing_template_decl)
1338 expand_end_catch_block ();
1339 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1342 /* Begin a compound statement. FLAGS contains some bits that control the
1343 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1344 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1345 block of a function. If BCS_TRY_BLOCK is set, this is the block
1346 created on behalf of a TRY statement. Returns a token to be passed to
1347 finish_compound_stmt. */
1349 tree
1350 begin_compound_stmt (unsigned int flags)
1352 tree r;
1354 if (flags & BCS_NO_SCOPE)
1356 r = push_stmt_list ();
1357 STATEMENT_LIST_NO_SCOPE (r) = 1;
1359 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1360 But, if it's a statement-expression with a scopeless block, there's
1361 nothing to keep, and we don't want to accidentally keep a block
1362 *inside* the scopeless block. */
1363 keep_next_level (false);
1365 else
1366 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1368 /* When processing a template, we need to remember where the braces were,
1369 so that we can set up identical scopes when instantiating the template
1370 later. BIND_EXPR is a handy candidate for this.
1371 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1372 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1373 processing templates. */
1374 if (processing_template_decl)
1376 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1377 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1378 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1379 TREE_SIDE_EFFECTS (r) = 1;
1382 return r;
1385 /* Finish a compound-statement, which is given by STMT. */
1387 void
1388 finish_compound_stmt (tree stmt)
1390 if (TREE_CODE (stmt) == BIND_EXPR)
1392 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1393 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1394 discard the BIND_EXPR so it can be merged with the containing
1395 STATEMENT_LIST. */
1396 if (TREE_CODE (body) == STATEMENT_LIST
1397 && STATEMENT_LIST_HEAD (body) == NULL
1398 && !BIND_EXPR_BODY_BLOCK (stmt)
1399 && !BIND_EXPR_TRY_BLOCK (stmt))
1400 stmt = body;
1401 else
1402 BIND_EXPR_BODY (stmt) = body;
1404 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1405 stmt = pop_stmt_list (stmt);
1406 else
1408 /* Destroy any ObjC "super" receivers that may have been
1409 created. */
1410 objc_clear_super_receiver ();
1412 stmt = do_poplevel (stmt);
1415 /* ??? See c_end_compound_stmt wrt statement expressions. */
1416 add_stmt (stmt);
1419 /* Finish an asm-statement, whose components are a STRING, some
1420 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1421 LABELS. Also note whether the asm-statement should be
1422 considered volatile. */
1424 tree
1425 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1426 tree input_operands, tree clobbers, tree labels)
1428 tree r;
1429 tree t;
1430 int ninputs = list_length (input_operands);
1431 int noutputs = list_length (output_operands);
1433 if (!processing_template_decl)
1435 const char *constraint;
1436 const char **oconstraints;
1437 bool allows_mem, allows_reg, is_inout;
1438 tree operand;
1439 int i;
1441 oconstraints = XALLOCAVEC (const char *, noutputs);
1443 string = resolve_asm_operand_names (string, output_operands,
1444 input_operands, labels);
1446 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1448 operand = TREE_VALUE (t);
1450 /* ??? Really, this should not be here. Users should be using a
1451 proper lvalue, dammit. But there's a long history of using
1452 casts in the output operands. In cases like longlong.h, this
1453 becomes a primitive form of typechecking -- if the cast can be
1454 removed, then the output operand had a type of the proper width;
1455 otherwise we'll get an error. Gross, but ... */
1456 STRIP_NOPS (operand);
1458 operand = mark_lvalue_use (operand);
1460 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1461 operand = error_mark_node;
1463 if (operand != error_mark_node
1464 && (TREE_READONLY (operand)
1465 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1466 /* Functions are not modifiable, even though they are
1467 lvalues. */
1468 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1469 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1470 /* If it's an aggregate and any field is const, then it is
1471 effectively const. */
1472 || (CLASS_TYPE_P (TREE_TYPE (operand))
1473 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1474 cxx_readonly_error (operand, lv_asm);
1476 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1477 oconstraints[i] = constraint;
1479 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1480 &allows_mem, &allows_reg, &is_inout))
1482 /* If the operand is going to end up in memory,
1483 mark it addressable. */
1484 if (!allows_reg && !cxx_mark_addressable (operand))
1485 operand = error_mark_node;
1487 else
1488 operand = error_mark_node;
1490 TREE_VALUE (t) = operand;
1493 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1495 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1496 bool constraint_parsed
1497 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1498 oconstraints, &allows_mem, &allows_reg);
1499 /* If the operand is going to end up in memory, don't call
1500 decay_conversion. */
1501 if (constraint_parsed && !allows_reg && allows_mem)
1502 operand = mark_lvalue_use (TREE_VALUE (t));
1503 else
1504 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1506 /* If the type of the operand hasn't been determined (e.g.,
1507 because it involves an overloaded function), then issue
1508 an error message. There's no context available to
1509 resolve the overloading. */
1510 if (TREE_TYPE (operand) == unknown_type_node)
1512 error ("type of asm operand %qE could not be determined",
1513 TREE_VALUE (t));
1514 operand = error_mark_node;
1517 if (constraint_parsed)
1519 /* If the operand is going to end up in memory,
1520 mark it addressable. */
1521 if (!allows_reg && allows_mem)
1523 /* Strip the nops as we allow this case. FIXME, this really
1524 should be rejected or made deprecated. */
1525 STRIP_NOPS (operand);
1526 if (!cxx_mark_addressable (operand))
1527 operand = error_mark_node;
1529 else if (!allows_reg && !allows_mem)
1531 /* If constraint allows neither register nor memory,
1532 try harder to get a constant. */
1533 tree constop = maybe_constant_value (operand);
1534 if (TREE_CONSTANT (constop))
1535 operand = constop;
1538 else
1539 operand = error_mark_node;
1541 TREE_VALUE (t) = operand;
1545 r = build_stmt (input_location, ASM_EXPR, string,
1546 output_operands, input_operands,
1547 clobbers, labels);
1548 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1549 r = maybe_cleanup_point_expr_void (r);
1550 return add_stmt (r);
1553 /* Finish a label with the indicated NAME. Returns the new label. */
1555 tree
1556 finish_label_stmt (tree name)
1558 tree decl = define_label (input_location, name);
1560 if (decl == error_mark_node)
1561 return error_mark_node;
1563 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1565 return decl;
1568 /* Finish a series of declarations for local labels. G++ allows users
1569 to declare "local" labels, i.e., labels with scope. This extension
1570 is useful when writing code involving statement-expressions. */
1572 void
1573 finish_label_decl (tree name)
1575 if (!at_function_scope_p ())
1577 error ("__label__ declarations are only allowed in function scopes");
1578 return;
1581 add_decl_expr (declare_local_label (name));
1584 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1586 void
1587 finish_decl_cleanup (tree decl, tree cleanup)
1589 push_cleanup (decl, cleanup, false);
1592 /* If the current scope exits with an exception, run CLEANUP. */
1594 void
1595 finish_eh_cleanup (tree cleanup)
1597 push_cleanup (NULL, cleanup, true);
1600 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1601 order they were written by the user. Each node is as for
1602 emit_mem_initializers. */
1604 void
1605 finish_mem_initializers (tree mem_inits)
1607 /* Reorder the MEM_INITS so that they are in the order they appeared
1608 in the source program. */
1609 mem_inits = nreverse (mem_inits);
1611 if (processing_template_decl)
1613 tree mem;
1615 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1617 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1618 check for bare parameter packs in the TREE_VALUE, because
1619 any parameter packs in the TREE_VALUE have already been
1620 bound as part of the TREE_PURPOSE. See
1621 make_pack_expansion for more information. */
1622 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1623 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1624 TREE_VALUE (mem) = error_mark_node;
1627 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1628 CTOR_INITIALIZER, mem_inits));
1630 else
1631 emit_mem_initializers (mem_inits);
1634 /* Obfuscate EXPR if it looks like an id-expression or member access so
1635 that the call to finish_decltype in do_auto_deduction will give the
1636 right result. */
1638 tree
1639 force_paren_expr (tree expr)
1641 /* This is only needed for decltype(auto) in C++14. */
1642 if (cxx_dialect < cxx14)
1643 return expr;
1645 /* If we're in unevaluated context, we can't be deducing a
1646 return/initializer type, so we don't need to mess with this. */
1647 if (cp_unevaluated_operand)
1648 return expr;
1650 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1651 && TREE_CODE (expr) != SCOPE_REF)
1652 return expr;
1654 if (TREE_CODE (expr) == COMPONENT_REF)
1655 REF_PARENTHESIZED_P (expr) = true;
1656 else if (type_dependent_expression_p (expr))
1657 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1658 else
1660 cp_lvalue_kind kind = lvalue_kind (expr);
1661 if ((kind & ~clk_class) != clk_none)
1663 tree type = unlowered_expr_type (expr);
1664 bool rval = !!(kind & clk_rvalueref);
1665 type = cp_build_reference_type (type, rval);
1666 /* This inhibits warnings in, eg, cxx_mark_addressable
1667 (c++/60955). */
1668 warning_sentinel s (extra_warnings);
1669 expr = build_static_cast (type, expr, tf_error);
1670 if (expr != error_mark_node)
1671 REF_PARENTHESIZED_P (expr) = true;
1675 return expr;
1678 /* Finish a parenthesized expression EXPR. */
1680 tree
1681 finish_parenthesized_expr (tree expr)
1683 if (EXPR_P (expr))
1684 /* This inhibits warnings in c_common_truthvalue_conversion. */
1685 TREE_NO_WARNING (expr) = 1;
1687 if (TREE_CODE (expr) == OFFSET_REF
1688 || TREE_CODE (expr) == SCOPE_REF)
1689 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1690 enclosed in parentheses. */
1691 PTRMEM_OK_P (expr) = 0;
1693 if (TREE_CODE (expr) == STRING_CST)
1694 PAREN_STRING_LITERAL_P (expr) = 1;
1696 expr = force_paren_expr (expr);
1698 return expr;
1701 /* Finish a reference to a non-static data member (DECL) that is not
1702 preceded by `.' or `->'. */
1704 tree
1705 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1707 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1709 if (!object)
1711 tree scope = qualifying_scope;
1712 if (scope == NULL_TREE)
1713 scope = context_for_name_lookup (decl);
1714 object = maybe_dummy_object (scope, NULL);
1717 object = maybe_resolve_dummy (object, true);
1718 if (object == error_mark_node)
1719 return error_mark_node;
1721 /* DR 613/850: Can use non-static data members without an associated
1722 object in sizeof/decltype/alignof. */
1723 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1724 && (!processing_template_decl || !current_class_ref))
1726 if (current_function_decl
1727 && DECL_STATIC_FUNCTION_P (current_function_decl))
1728 error ("invalid use of member %qD in static member function", decl);
1729 else
1730 error ("invalid use of non-static data member %qD", decl);
1731 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1733 return error_mark_node;
1736 if (current_class_ptr)
1737 TREE_USED (current_class_ptr) = 1;
1738 if (processing_template_decl && !qualifying_scope)
1740 tree type = TREE_TYPE (decl);
1742 if (TREE_CODE (type) == REFERENCE_TYPE)
1743 /* Quals on the object don't matter. */;
1744 else if (PACK_EXPANSION_P (type))
1745 /* Don't bother trying to represent this. */
1746 type = NULL_TREE;
1747 else
1749 /* Set the cv qualifiers. */
1750 int quals = cp_type_quals (TREE_TYPE (object));
1752 if (DECL_MUTABLE_P (decl))
1753 quals &= ~TYPE_QUAL_CONST;
1755 quals |= cp_type_quals (TREE_TYPE (decl));
1756 type = cp_build_qualified_type (type, quals);
1759 return (convert_from_reference
1760 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1762 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1763 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1764 for now. */
1765 else if (processing_template_decl)
1766 return build_qualified_name (TREE_TYPE (decl),
1767 qualifying_scope,
1768 decl,
1769 /*template_p=*/false);
1770 else
1772 tree access_type = TREE_TYPE (object);
1774 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1775 decl, tf_warning_or_error);
1777 /* If the data member was named `C::M', convert `*this' to `C'
1778 first. */
1779 if (qualifying_scope)
1781 tree binfo = NULL_TREE;
1782 object = build_scoped_ref (object, qualifying_scope,
1783 &binfo);
1786 return build_class_member_access_expr (object, decl,
1787 /*access_path=*/NULL_TREE,
1788 /*preserve_reference=*/false,
1789 tf_warning_or_error);
1793 /* If we are currently parsing a template and we encountered a typedef
1794 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1795 adds the typedef to a list tied to the current template.
1796 At template instantiation time, that list is walked and access check
1797 performed for each typedef.
1798 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1800 void
1801 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1802 tree context,
1803 location_t location)
1805 tree template_info = NULL;
1806 tree cs = current_scope ();
1808 if (!is_typedef_decl (typedef_decl)
1809 || !context
1810 || !CLASS_TYPE_P (context)
1811 || !cs)
1812 return;
1814 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1815 template_info = get_template_info (cs);
1817 if (template_info
1818 && TI_TEMPLATE (template_info)
1819 && !currently_open_class (context))
1820 append_type_to_template_for_access_check (cs, typedef_decl,
1821 context, location);
1824 /* DECL was the declaration to which a qualified-id resolved. Issue
1825 an error message if it is not accessible. If OBJECT_TYPE is
1826 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1827 type of `*x', or `x', respectively. If the DECL was named as
1828 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1830 void
1831 check_accessibility_of_qualified_id (tree decl,
1832 tree object_type,
1833 tree nested_name_specifier)
1835 tree scope;
1836 tree qualifying_type = NULL_TREE;
1838 /* If we are parsing a template declaration and if decl is a typedef,
1839 add it to a list tied to the template.
1840 At template instantiation time, that list will be walked and
1841 access check performed. */
1842 add_typedef_to_current_template_for_access_check (decl,
1843 nested_name_specifier
1844 ? nested_name_specifier
1845 : DECL_CONTEXT (decl),
1846 input_location);
1848 /* If we're not checking, return immediately. */
1849 if (deferred_access_no_check)
1850 return;
1852 /* Determine the SCOPE of DECL. */
1853 scope = context_for_name_lookup (decl);
1854 /* If the SCOPE is not a type, then DECL is not a member. */
1855 if (!TYPE_P (scope))
1856 return;
1857 /* Compute the scope through which DECL is being accessed. */
1858 if (object_type
1859 /* OBJECT_TYPE might not be a class type; consider:
1861 class A { typedef int I; };
1862 I *p;
1863 p->A::I::~I();
1865 In this case, we will have "A::I" as the DECL, but "I" as the
1866 OBJECT_TYPE. */
1867 && CLASS_TYPE_P (object_type)
1868 && DERIVED_FROM_P (scope, object_type))
1869 /* If we are processing a `->' or `.' expression, use the type of the
1870 left-hand side. */
1871 qualifying_type = object_type;
1872 else if (nested_name_specifier)
1874 /* If the reference is to a non-static member of the
1875 current class, treat it as if it were referenced through
1876 `this'. */
1877 tree ct;
1878 if (DECL_NONSTATIC_MEMBER_P (decl)
1879 && current_class_ptr
1880 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1881 qualifying_type = ct;
1882 /* Otherwise, use the type indicated by the
1883 nested-name-specifier. */
1884 else
1885 qualifying_type = nested_name_specifier;
1887 else
1888 /* Otherwise, the name must be from the current class or one of
1889 its bases. */
1890 qualifying_type = currently_open_derived_class (scope);
1892 if (qualifying_type
1893 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1894 or similar in a default argument value. */
1895 && CLASS_TYPE_P (qualifying_type)
1896 && !dependent_type_p (qualifying_type))
1897 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1898 decl, tf_warning_or_error);
1901 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1902 class named to the left of the "::" operator. DONE is true if this
1903 expression is a complete postfix-expression; it is false if this
1904 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1905 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1906 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1907 is true iff this qualified name appears as a template argument. */
1909 tree
1910 finish_qualified_id_expr (tree qualifying_class,
1911 tree expr,
1912 bool done,
1913 bool address_p,
1914 bool template_p,
1915 bool template_arg_p,
1916 tsubst_flags_t complain)
1918 gcc_assert (TYPE_P (qualifying_class));
1920 if (error_operand_p (expr))
1921 return error_mark_node;
1923 if ((DECL_P (expr) || BASELINK_P (expr))
1924 && !mark_used (expr, complain))
1925 return error_mark_node;
1927 if (template_p)
1928 check_template_keyword (expr);
1930 /* If EXPR occurs as the operand of '&', use special handling that
1931 permits a pointer-to-member. */
1932 if (address_p && done)
1934 if (TREE_CODE (expr) == SCOPE_REF)
1935 expr = TREE_OPERAND (expr, 1);
1936 expr = build_offset_ref (qualifying_class, expr,
1937 /*address_p=*/true, complain);
1938 return expr;
1941 /* No need to check access within an enum. */
1942 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1943 return expr;
1945 /* Within the scope of a class, turn references to non-static
1946 members into expression of the form "this->...". */
1947 if (template_arg_p)
1948 /* But, within a template argument, we do not want make the
1949 transformation, as there is no "this" pointer. */
1951 else if (TREE_CODE (expr) == FIELD_DECL)
1953 push_deferring_access_checks (dk_no_check);
1954 expr = finish_non_static_data_member (expr, NULL_TREE,
1955 qualifying_class);
1956 pop_deferring_access_checks ();
1958 else if (BASELINK_P (expr) && !processing_template_decl)
1960 /* See if any of the functions are non-static members. */
1961 /* If so, the expression may be relative to 'this'. */
1962 if (!shared_member_p (expr)
1963 && current_class_ptr
1964 && DERIVED_FROM_P (qualifying_class,
1965 current_nonlambda_class_type ()))
1966 expr = (build_class_member_access_expr
1967 (maybe_dummy_object (qualifying_class, NULL),
1968 expr,
1969 BASELINK_ACCESS_BINFO (expr),
1970 /*preserve_reference=*/false,
1971 complain));
1972 else if (done)
1973 /* The expression is a qualified name whose address is not
1974 being taken. */
1975 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1976 complain);
1978 else if (BASELINK_P (expr))
1980 else
1982 /* In a template, return a SCOPE_REF for most qualified-ids
1983 so that we can check access at instantiation time. But if
1984 we're looking at a member of the current instantiation, we
1985 know we have access and building up the SCOPE_REF confuses
1986 non-type template argument handling. */
1987 if (processing_template_decl
1988 && !currently_open_class (qualifying_class))
1989 expr = build_qualified_name (TREE_TYPE (expr),
1990 qualifying_class, expr,
1991 template_p);
1993 expr = convert_from_reference (expr);
1996 return expr;
1999 /* Begin a statement-expression. The value returned must be passed to
2000 finish_stmt_expr. */
2002 tree
2003 begin_stmt_expr (void)
2005 return push_stmt_list ();
2008 /* Process the final expression of a statement expression. EXPR can be
2009 NULL, if the final expression is empty. Return a STATEMENT_LIST
2010 containing all the statements in the statement-expression, or
2011 ERROR_MARK_NODE if there was an error. */
2013 tree
2014 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2016 if (error_operand_p (expr))
2018 /* The type of the statement-expression is the type of the last
2019 expression. */
2020 TREE_TYPE (stmt_expr) = error_mark_node;
2021 return error_mark_node;
2024 /* If the last statement does not have "void" type, then the value
2025 of the last statement is the value of the entire expression. */
2026 if (expr)
2028 tree type = TREE_TYPE (expr);
2030 if (processing_template_decl)
2032 expr = build_stmt (input_location, EXPR_STMT, expr);
2033 expr = add_stmt (expr);
2034 /* Mark the last statement so that we can recognize it as such at
2035 template-instantiation time. */
2036 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2038 else if (VOID_TYPE_P (type))
2040 /* Just treat this like an ordinary statement. */
2041 expr = finish_expr_stmt (expr);
2043 else
2045 /* It actually has a value we need to deal with. First, force it
2046 to be an rvalue so that we won't need to build up a copy
2047 constructor call later when we try to assign it to something. */
2048 expr = force_rvalue (expr, tf_warning_or_error);
2049 if (error_operand_p (expr))
2050 return error_mark_node;
2052 /* Update for array-to-pointer decay. */
2053 type = TREE_TYPE (expr);
2055 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2056 normal statement, but don't convert to void or actually add
2057 the EXPR_STMT. */
2058 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2059 expr = maybe_cleanup_point_expr (expr);
2060 add_stmt (expr);
2063 /* The type of the statement-expression is the type of the last
2064 expression. */
2065 TREE_TYPE (stmt_expr) = type;
2068 return stmt_expr;
2071 /* Finish a statement-expression. EXPR should be the value returned
2072 by the previous begin_stmt_expr. Returns an expression
2073 representing the statement-expression. */
2075 tree
2076 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2078 tree type;
2079 tree result;
2081 if (error_operand_p (stmt_expr))
2083 pop_stmt_list (stmt_expr);
2084 return error_mark_node;
2087 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2089 type = TREE_TYPE (stmt_expr);
2090 result = pop_stmt_list (stmt_expr);
2091 TREE_TYPE (result) = type;
2093 if (processing_template_decl)
2095 result = build_min (STMT_EXPR, type, result);
2096 TREE_SIDE_EFFECTS (result) = 1;
2097 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2099 else if (CLASS_TYPE_P (type))
2101 /* Wrap the statement-expression in a TARGET_EXPR so that the
2102 temporary object created by the final expression is destroyed at
2103 the end of the full-expression containing the
2104 statement-expression. */
2105 result = force_target_expr (type, result, tf_warning_or_error);
2108 return result;
2111 /* Returns the expression which provides the value of STMT_EXPR. */
2113 tree
2114 stmt_expr_value_expr (tree stmt_expr)
2116 tree t = STMT_EXPR_STMT (stmt_expr);
2118 if (TREE_CODE (t) == BIND_EXPR)
2119 t = BIND_EXPR_BODY (t);
2121 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2122 t = STATEMENT_LIST_TAIL (t)->stmt;
2124 if (TREE_CODE (t) == EXPR_STMT)
2125 t = EXPR_STMT_EXPR (t);
2127 return t;
2130 /* Return TRUE iff EXPR_STMT is an empty list of
2131 expression statements. */
2133 bool
2134 empty_expr_stmt_p (tree expr_stmt)
2136 tree body = NULL_TREE;
2138 if (expr_stmt == void_node)
2139 return true;
2141 if (expr_stmt)
2143 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2144 body = EXPR_STMT_EXPR (expr_stmt);
2145 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2146 body = expr_stmt;
2149 if (body)
2151 if (TREE_CODE (body) == STATEMENT_LIST)
2152 return tsi_end_p (tsi_start (body));
2153 else
2154 return empty_expr_stmt_p (body);
2156 return false;
2159 /* Perform Koenig lookup. FN is the postfix-expression representing
2160 the function (or functions) to call; ARGS are the arguments to the
2161 call. Returns the functions to be considered by overload resolution. */
2163 tree
2164 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2165 tsubst_flags_t complain)
2167 tree identifier = NULL_TREE;
2168 tree functions = NULL_TREE;
2169 tree tmpl_args = NULL_TREE;
2170 bool template_id = false;
2172 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2174 /* Use a separate flag to handle null args. */
2175 template_id = true;
2176 tmpl_args = TREE_OPERAND (fn, 1);
2177 fn = TREE_OPERAND (fn, 0);
2180 /* Find the name of the overloaded function. */
2181 if (identifier_p (fn))
2182 identifier = fn;
2183 else if (is_overloaded_fn (fn))
2185 functions = fn;
2186 identifier = DECL_NAME (get_first_fn (functions));
2188 else if (DECL_P (fn))
2190 functions = fn;
2191 identifier = DECL_NAME (fn);
2194 /* A call to a namespace-scope function using an unqualified name.
2196 Do Koenig lookup -- unless any of the arguments are
2197 type-dependent. */
2198 if (!any_type_dependent_arguments_p (args)
2199 && !any_dependent_template_arguments_p (tmpl_args))
2201 fn = lookup_arg_dependent (identifier, functions, args);
2202 if (!fn)
2204 /* The unqualified name could not be resolved. */
2205 if (complain)
2206 fn = unqualified_fn_lookup_error (identifier);
2207 else
2208 fn = identifier;
2212 if (fn && template_id)
2213 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2215 return fn;
2218 /* Generate an expression for `FN (ARGS)'. This may change the
2219 contents of ARGS.
2221 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2222 as a virtual call, even if FN is virtual. (This flag is set when
2223 encountering an expression where the function name is explicitly
2224 qualified. For example a call to `X::f' never generates a virtual
2225 call.)
2227 Returns code for the call. */
2229 tree
2230 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2231 bool koenig_p, tsubst_flags_t complain)
2233 tree result;
2234 tree orig_fn;
2235 vec<tree, va_gc> *orig_args = NULL;
2237 if (fn == error_mark_node)
2238 return error_mark_node;
2240 gcc_assert (!TYPE_P (fn));
2242 orig_fn = fn;
2244 if (processing_template_decl)
2246 /* If the call expression is dependent, build a CALL_EXPR node
2247 with no type; type_dependent_expression_p recognizes
2248 expressions with no type as being dependent. */
2249 if (type_dependent_expression_p (fn)
2250 || any_type_dependent_arguments_p (*args)
2251 /* For a non-static member function that doesn't have an
2252 explicit object argument, we need to specifically
2253 test the type dependency of the "this" pointer because it
2254 is not included in *ARGS even though it is considered to
2255 be part of the list of arguments. Note that this is
2256 related to CWG issues 515 and 1005. */
2257 || (TREE_CODE (fn) != COMPONENT_REF
2258 && non_static_member_function_p (fn)
2259 && current_class_ref
2260 && type_dependent_expression_p (current_class_ref)))
2262 result = build_nt_call_vec (fn, *args);
2263 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2264 KOENIG_LOOKUP_P (result) = koenig_p;
2265 if (cfun)
2269 tree fndecl = OVL_CURRENT (fn);
2270 if (TREE_CODE (fndecl) != FUNCTION_DECL
2271 || !TREE_THIS_VOLATILE (fndecl))
2272 break;
2273 fn = OVL_NEXT (fn);
2275 while (fn);
2276 if (!fn)
2277 current_function_returns_abnormally = 1;
2279 return result;
2281 orig_args = make_tree_vector_copy (*args);
2282 if (!BASELINK_P (fn)
2283 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2284 && TREE_TYPE (fn) != unknown_type_node)
2285 fn = build_non_dependent_expr (fn);
2286 make_args_non_dependent (*args);
2289 if (TREE_CODE (fn) == COMPONENT_REF)
2291 tree member = TREE_OPERAND (fn, 1);
2292 if (BASELINK_P (member))
2294 tree object = TREE_OPERAND (fn, 0);
2295 return build_new_method_call (object, member,
2296 args, NULL_TREE,
2297 (disallow_virtual
2298 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2299 : LOOKUP_NORMAL),
2300 /*fn_p=*/NULL,
2301 complain);
2305 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2306 if (TREE_CODE (fn) == ADDR_EXPR
2307 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2308 fn = TREE_OPERAND (fn, 0);
2310 if (is_overloaded_fn (fn))
2311 fn = baselink_for_fns (fn);
2313 result = NULL_TREE;
2314 if (BASELINK_P (fn))
2316 tree object;
2318 /* A call to a member function. From [over.call.func]:
2320 If the keyword this is in scope and refers to the class of
2321 that member function, or a derived class thereof, then the
2322 function call is transformed into a qualified function call
2323 using (*this) as the postfix-expression to the left of the
2324 . operator.... [Otherwise] a contrived object of type T
2325 becomes the implied object argument.
2327 In this situation:
2329 struct A { void f(); };
2330 struct B : public A {};
2331 struct C : public A { void g() { B::f(); }};
2333 "the class of that member function" refers to `A'. But 11.2
2334 [class.access.base] says that we need to convert 'this' to B* as
2335 part of the access, so we pass 'B' to maybe_dummy_object. */
2337 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2338 NULL);
2340 if (processing_template_decl)
2342 if (type_dependent_expression_p (object))
2344 tree ret = build_nt_call_vec (orig_fn, orig_args);
2345 release_tree_vector (orig_args);
2346 return ret;
2348 object = build_non_dependent_expr (object);
2351 result = build_new_method_call (object, fn, args, NULL_TREE,
2352 (disallow_virtual
2353 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2354 : LOOKUP_NORMAL),
2355 /*fn_p=*/NULL,
2356 complain);
2358 else if (is_overloaded_fn (fn))
2360 /* If the function is an overloaded builtin, resolve it. */
2361 if (TREE_CODE (fn) == FUNCTION_DECL
2362 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2363 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2364 result = resolve_overloaded_builtin (input_location, fn, *args);
2366 if (!result)
2368 if (warn_sizeof_pointer_memaccess
2369 && (complain & tf_warning)
2370 && !vec_safe_is_empty (*args)
2371 && !processing_template_decl)
2373 location_t sizeof_arg_loc[3];
2374 tree sizeof_arg[3];
2375 unsigned int i;
2376 for (i = 0; i < 3; i++)
2378 tree t;
2380 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2381 sizeof_arg[i] = NULL_TREE;
2382 if (i >= (*args)->length ())
2383 continue;
2384 t = (**args)[i];
2385 if (TREE_CODE (t) != SIZEOF_EXPR)
2386 continue;
2387 if (SIZEOF_EXPR_TYPE_P (t))
2388 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2389 else
2390 sizeof_arg[i] = TREE_OPERAND (t, 0);
2391 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2393 sizeof_pointer_memaccess_warning
2394 (sizeof_arg_loc, fn, *args,
2395 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2398 /* A call to a namespace-scope function. */
2399 result = build_new_function_call (fn, args, koenig_p, complain);
2402 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2404 if (!vec_safe_is_empty (*args))
2405 error ("arguments to destructor are not allowed");
2406 /* Mark the pseudo-destructor call as having side-effects so
2407 that we do not issue warnings about its use. */
2408 result = build1 (NOP_EXPR,
2409 void_type_node,
2410 TREE_OPERAND (fn, 0));
2411 TREE_SIDE_EFFECTS (result) = 1;
2413 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2414 /* If the "function" is really an object of class type, it might
2415 have an overloaded `operator ()'. */
2416 result = build_op_call (fn, args, complain);
2418 if (!result)
2419 /* A call where the function is unknown. */
2420 result = cp_build_function_call_vec (fn, args, complain);
2422 if (processing_template_decl && result != error_mark_node)
2424 if (INDIRECT_REF_P (result))
2425 result = TREE_OPERAND (result, 0);
2426 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2427 SET_EXPR_LOCATION (result, input_location);
2428 KOENIG_LOOKUP_P (result) = koenig_p;
2429 release_tree_vector (orig_args);
2430 result = convert_from_reference (result);
2433 if (koenig_p)
2435 /* Free garbage OVERLOADs from arg-dependent lookup. */
2436 tree next = NULL_TREE;
2437 for (fn = orig_fn;
2438 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2439 fn = next)
2441 if (processing_template_decl)
2442 /* In a template, we'll re-use them at instantiation time. */
2443 OVL_ARG_DEPENDENT (fn) = false;
2444 else
2446 next = OVL_CHAIN (fn);
2447 ggc_free (fn);
2452 return result;
2455 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2456 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2457 POSTDECREMENT_EXPR.) */
2459 tree
2460 finish_increment_expr (tree expr, enum tree_code code)
2462 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2465 /* Finish a use of `this'. Returns an expression for `this'. */
2467 tree
2468 finish_this_expr (void)
2470 tree result = NULL_TREE;
2472 if (current_class_ptr)
2474 tree type = TREE_TYPE (current_class_ref);
2476 /* In a lambda expression, 'this' refers to the captured 'this'. */
2477 if (LAMBDA_TYPE_P (type))
2478 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2479 else
2480 result = current_class_ptr;
2483 if (result)
2484 /* The keyword 'this' is a prvalue expression. */
2485 return rvalue (result);
2487 tree fn = current_nonlambda_function ();
2488 if (fn && DECL_STATIC_FUNCTION_P (fn))
2489 error ("%<this%> is unavailable for static member functions");
2490 else if (fn)
2491 error ("invalid use of %<this%> in non-member function");
2492 else
2493 error ("invalid use of %<this%> at top level");
2494 return error_mark_node;
2497 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2498 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2499 the TYPE for the type given. If SCOPE is non-NULL, the expression
2500 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2502 tree
2503 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2504 location_t loc)
2506 if (object == error_mark_node || destructor == error_mark_node)
2507 return error_mark_node;
2509 gcc_assert (TYPE_P (destructor));
2511 if (!processing_template_decl)
2513 if (scope == error_mark_node)
2515 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2516 return error_mark_node;
2518 if (is_auto (destructor))
2519 destructor = TREE_TYPE (object);
2520 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2522 error_at (loc,
2523 "qualified type %qT does not match destructor name ~%qT",
2524 scope, destructor);
2525 return error_mark_node;
2529 /* [expr.pseudo] says both:
2531 The type designated by the pseudo-destructor-name shall be
2532 the same as the object type.
2534 and:
2536 The cv-unqualified versions of the object type and of the
2537 type designated by the pseudo-destructor-name shall be the
2538 same type.
2540 We implement the more generous second sentence, since that is
2541 what most other compilers do. */
2542 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2543 destructor))
2545 error_at (loc, "%qE is not of type %qT", object, destructor);
2546 return error_mark_node;
2550 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2551 scope, destructor);
2554 /* Finish an expression of the form CODE EXPR. */
2556 tree
2557 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2558 tsubst_flags_t complain)
2560 tree result = build_x_unary_op (loc, code, expr, complain);
2561 if ((complain & tf_warning)
2562 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2563 overflow_warning (input_location, result);
2565 return result;
2568 /* Finish a compound-literal expression. TYPE is the type to which
2569 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2571 tree
2572 finish_compound_literal (tree type, tree compound_literal,
2573 tsubst_flags_t complain)
2575 if (type == error_mark_node)
2576 return error_mark_node;
2578 if (TREE_CODE (type) == REFERENCE_TYPE)
2580 compound_literal
2581 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2582 complain);
2583 return cp_build_c_cast (type, compound_literal, complain);
2586 if (!TYPE_OBJ_P (type))
2588 if (complain & tf_error)
2589 error ("compound literal of non-object type %qT", type);
2590 return error_mark_node;
2593 if (processing_template_decl)
2595 TREE_TYPE (compound_literal) = type;
2596 /* Mark the expression as a compound literal. */
2597 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2598 return compound_literal;
2601 type = complete_type (type);
2603 if (TYPE_NON_AGGREGATE_CLASS (type))
2605 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2606 everywhere that deals with function arguments would be a pain, so
2607 just wrap it in a TREE_LIST. The parser set a flag so we know
2608 that it came from T{} rather than T({}). */
2609 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2610 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2611 return build_functional_cast (type, compound_literal, complain);
2614 if (TREE_CODE (type) == ARRAY_TYPE
2615 && check_array_initializer (NULL_TREE, type, compound_literal))
2616 return error_mark_node;
2617 compound_literal = reshape_init (type, compound_literal, complain);
2618 if (SCALAR_TYPE_P (type)
2619 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2620 && !check_narrowing (type, compound_literal, complain))
2621 return error_mark_node;
2622 if (TREE_CODE (type) == ARRAY_TYPE
2623 && TYPE_DOMAIN (type) == NULL_TREE)
2625 cp_complete_array_type_or_error (&type, compound_literal,
2626 false, complain);
2627 if (type == error_mark_node)
2628 return error_mark_node;
2630 compound_literal = digest_init (type, compound_literal, complain);
2631 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2632 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2633 /* Put static/constant array temporaries in static variables, but always
2634 represent class temporaries with TARGET_EXPR so we elide copies. */
2635 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2636 && TREE_CODE (type) == ARRAY_TYPE
2637 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2638 && initializer_constant_valid_p (compound_literal, type))
2640 tree decl = create_temporary_var (type);
2641 DECL_INITIAL (decl) = compound_literal;
2642 TREE_STATIC (decl) = 1;
2643 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2645 /* 5.19 says that a constant expression can include an
2646 lvalue-rvalue conversion applied to "a glvalue of literal type
2647 that refers to a non-volatile temporary object initialized
2648 with a constant expression". Rather than try to communicate
2649 that this VAR_DECL is a temporary, just mark it constexpr. */
2650 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2651 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2652 TREE_CONSTANT (decl) = true;
2654 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2655 decl = pushdecl_top_level (decl);
2656 DECL_NAME (decl) = make_anon_name ();
2657 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2658 /* Make sure the destructor is callable. */
2659 tree clean = cxx_maybe_build_cleanup (decl, complain);
2660 if (clean == error_mark_node)
2661 return error_mark_node;
2662 return decl;
2664 else
2665 return get_target_expr_sfinae (compound_literal, complain);
2668 /* Return the declaration for the function-name variable indicated by
2669 ID. */
2671 tree
2672 finish_fname (tree id)
2674 tree decl;
2676 decl = fname_decl (input_location, C_RID_CODE (id), id);
2677 if (processing_template_decl && current_function_decl
2678 && decl != error_mark_node)
2679 decl = DECL_NAME (decl);
2680 return decl;
2683 /* Finish a translation unit. */
2685 void
2686 finish_translation_unit (void)
2688 /* In case there were missing closebraces,
2689 get us back to the global binding level. */
2690 pop_everything ();
2691 while (current_namespace != global_namespace)
2692 pop_namespace ();
2694 /* Do file scope __FUNCTION__ et al. */
2695 finish_fname_decls ();
2698 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2699 Returns the parameter. */
2701 tree
2702 finish_template_type_parm (tree aggr, tree identifier)
2704 if (aggr != class_type_node)
2706 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2707 aggr = class_type_node;
2710 return build_tree_list (aggr, identifier);
2713 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2714 Returns the parameter. */
2716 tree
2717 finish_template_template_parm (tree aggr, tree identifier)
2719 tree decl = build_decl (input_location,
2720 TYPE_DECL, identifier, NULL_TREE);
2721 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2722 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2723 DECL_TEMPLATE_RESULT (tmpl) = decl;
2724 DECL_ARTIFICIAL (decl) = 1;
2725 end_template_decl ();
2727 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2729 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2730 /*is_primary=*/true, /*is_partial=*/false,
2731 /*is_friend=*/0);
2733 return finish_template_type_parm (aggr, tmpl);
2736 /* ARGUMENT is the default-argument value for a template template
2737 parameter. If ARGUMENT is invalid, issue error messages and return
2738 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2740 tree
2741 check_template_template_default_arg (tree argument)
2743 if (TREE_CODE (argument) != TEMPLATE_DECL
2744 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2745 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2747 if (TREE_CODE (argument) == TYPE_DECL)
2748 error ("invalid use of type %qT as a default value for a template "
2749 "template-parameter", TREE_TYPE (argument));
2750 else
2751 error ("invalid default argument for a template template parameter");
2752 return error_mark_node;
2755 return argument;
2758 /* Begin a class definition, as indicated by T. */
2760 tree
2761 begin_class_definition (tree t)
2763 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2764 return error_mark_node;
2766 if (processing_template_parmlist)
2768 error ("definition of %q#T inside template parameter list", t);
2769 return error_mark_node;
2772 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2773 are passed the same as decimal scalar types. */
2774 if (TREE_CODE (t) == RECORD_TYPE
2775 && !processing_template_decl)
2777 tree ns = TYPE_CONTEXT (t);
2778 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2779 && DECL_CONTEXT (ns) == std_node
2780 && DECL_NAME (ns)
2781 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2783 const char *n = TYPE_NAME_STRING (t);
2784 if ((strcmp (n, "decimal32") == 0)
2785 || (strcmp (n, "decimal64") == 0)
2786 || (strcmp (n, "decimal128") == 0))
2787 TYPE_TRANSPARENT_AGGR (t) = 1;
2791 /* A non-implicit typename comes from code like:
2793 template <typename T> struct A {
2794 template <typename U> struct A<T>::B ...
2796 This is erroneous. */
2797 else if (TREE_CODE (t) == TYPENAME_TYPE)
2799 error ("invalid definition of qualified type %qT", t);
2800 t = error_mark_node;
2803 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2805 t = make_class_type (RECORD_TYPE);
2806 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2809 if (TYPE_BEING_DEFINED (t))
2811 t = make_class_type (TREE_CODE (t));
2812 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2814 maybe_process_partial_specialization (t);
2815 pushclass (t);
2816 TYPE_BEING_DEFINED (t) = 1;
2817 class_binding_level->defining_class_p = 1;
2819 if (flag_pack_struct)
2821 tree v;
2822 TYPE_PACKED (t) = 1;
2823 /* Even though the type is being defined for the first time
2824 here, there might have been a forward declaration, so there
2825 might be cv-qualified variants of T. */
2826 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2827 TYPE_PACKED (v) = 1;
2829 /* Reset the interface data, at the earliest possible
2830 moment, as it might have been set via a class foo;
2831 before. */
2832 if (! TYPE_ANONYMOUS_P (t))
2834 struct c_fileinfo *finfo = \
2835 get_fileinfo (LOCATION_FILE (input_location));
2836 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2837 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2838 (t, finfo->interface_unknown);
2840 reset_specialization();
2842 /* Make a declaration for this class in its own scope. */
2843 build_self_reference ();
2845 return t;
2848 /* Finish the member declaration given by DECL. */
2850 void
2851 finish_member_declaration (tree decl)
2853 if (decl == error_mark_node || decl == NULL_TREE)
2854 return;
2856 if (decl == void_type_node)
2857 /* The COMPONENT was a friend, not a member, and so there's
2858 nothing for us to do. */
2859 return;
2861 /* We should see only one DECL at a time. */
2862 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2864 /* Set up access control for DECL. */
2865 TREE_PRIVATE (decl)
2866 = (current_access_specifier == access_private_node);
2867 TREE_PROTECTED (decl)
2868 = (current_access_specifier == access_protected_node);
2869 if (TREE_CODE (decl) == TEMPLATE_DECL)
2871 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2872 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2875 /* Mark the DECL as a member of the current class, unless it's
2876 a member of an enumeration. */
2877 if (TREE_CODE (decl) != CONST_DECL)
2878 DECL_CONTEXT (decl) = current_class_type;
2880 /* Check for bare parameter packs in the member variable declaration. */
2881 if (TREE_CODE (decl) == FIELD_DECL)
2883 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2884 TREE_TYPE (decl) = error_mark_node;
2885 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2886 DECL_ATTRIBUTES (decl) = NULL_TREE;
2889 /* [dcl.link]
2891 A C language linkage is ignored for the names of class members
2892 and the member function type of class member functions. */
2893 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2894 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2896 /* Put functions on the TYPE_METHODS list and everything else on the
2897 TYPE_FIELDS list. Note that these are built up in reverse order.
2898 We reverse them (to obtain declaration order) in finish_struct. */
2899 if (DECL_DECLARES_FUNCTION_P (decl))
2901 /* We also need to add this function to the
2902 CLASSTYPE_METHOD_VEC. */
2903 if (add_method (current_class_type, decl, NULL_TREE))
2905 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2906 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2907 TYPE_METHODS (current_class_type) = decl;
2909 maybe_add_class_template_decl_list (current_class_type, decl,
2910 /*friend_p=*/0);
2913 /* Enter the DECL into the scope of the class, if the class
2914 isn't a closure (whose fields are supposed to be unnamed). */
2915 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2916 || pushdecl_class_level (decl))
2918 if (TREE_CODE (decl) == USING_DECL)
2920 /* For now, ignore class-scope USING_DECLS, so that
2921 debugging backends do not see them. */
2922 DECL_IGNORED_P (decl) = 1;
2925 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2926 go at the beginning. The reason is that lookup_field_1
2927 searches the list in order, and we want a field name to
2928 override a type name so that the "struct stat hack" will
2929 work. In particular:
2931 struct S { enum E { }; int E } s;
2932 s.E = 3;
2934 is valid. In addition, the FIELD_DECLs must be maintained in
2935 declaration order so that class layout works as expected.
2936 However, we don't need that order until class layout, so we
2937 save a little time by putting FIELD_DECLs on in reverse order
2938 here, and then reversing them in finish_struct_1. (We could
2939 also keep a pointer to the correct insertion points in the
2940 list.) */
2942 if (TREE_CODE (decl) == TYPE_DECL)
2943 TYPE_FIELDS (current_class_type)
2944 = chainon (TYPE_FIELDS (current_class_type), decl);
2945 else
2947 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2948 TYPE_FIELDS (current_class_type) = decl;
2951 maybe_add_class_template_decl_list (current_class_type, decl,
2952 /*friend_p=*/0);
2955 if (pch_file)
2956 note_decl_for_pch (decl);
2959 /* DECL has been declared while we are building a PCH file. Perform
2960 actions that we might normally undertake lazily, but which can be
2961 performed now so that they do not have to be performed in
2962 translation units which include the PCH file. */
2964 void
2965 note_decl_for_pch (tree decl)
2967 gcc_assert (pch_file);
2969 /* There's a good chance that we'll have to mangle names at some
2970 point, even if only for emission in debugging information. */
2971 if (VAR_OR_FUNCTION_DECL_P (decl)
2972 && !processing_template_decl)
2973 mangle_decl (decl);
2976 /* Finish processing a complete template declaration. The PARMS are
2977 the template parameters. */
2979 void
2980 finish_template_decl (tree parms)
2982 if (parms)
2983 end_template_decl ();
2984 else
2985 end_specialization ();
2988 /* Finish processing a template-id (which names a type) of the form
2989 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2990 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2991 the scope of template-id indicated. */
2993 tree
2994 finish_template_type (tree name, tree args, int entering_scope)
2996 tree type;
2998 type = lookup_template_class (name, args,
2999 NULL_TREE, NULL_TREE, entering_scope,
3000 tf_warning_or_error | tf_user);
3001 if (type == error_mark_node)
3002 return type;
3003 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3004 return TYPE_STUB_DECL (type);
3005 else
3006 return TYPE_NAME (type);
3009 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3010 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3011 BASE_CLASS, or NULL_TREE if an error occurred. The
3012 ACCESS_SPECIFIER is one of
3013 access_{default,public,protected_private}_node. For a virtual base
3014 we set TREE_TYPE. */
3016 tree
3017 finish_base_specifier (tree base, tree access, bool virtual_p)
3019 tree result;
3021 if (base == error_mark_node)
3023 error ("invalid base-class specification");
3024 result = NULL_TREE;
3026 else if (! MAYBE_CLASS_TYPE_P (base))
3028 error ("%qT is not a class type", base);
3029 result = NULL_TREE;
3031 else
3033 if (cp_type_quals (base) != 0)
3035 /* DR 484: Can a base-specifier name a cv-qualified
3036 class type? */
3037 base = TYPE_MAIN_VARIANT (base);
3039 result = build_tree_list (access, base);
3040 if (virtual_p)
3041 TREE_TYPE (result) = integer_type_node;
3044 return result;
3047 /* If FNS is a member function, a set of member functions, or a
3048 template-id referring to one or more member functions, return a
3049 BASELINK for FNS, incorporating the current access context.
3050 Otherwise, return FNS unchanged. */
3052 tree
3053 baselink_for_fns (tree fns)
3055 tree scope;
3056 tree cl;
3058 if (BASELINK_P (fns)
3059 || error_operand_p (fns))
3060 return fns;
3062 scope = ovl_scope (fns);
3063 if (!CLASS_TYPE_P (scope))
3064 return fns;
3066 cl = currently_open_derived_class (scope);
3067 if (!cl)
3068 cl = scope;
3069 cl = TYPE_BINFO (cl);
3070 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3073 /* Returns true iff DECL is a variable from a function outside
3074 the current one. */
3076 static bool
3077 outer_var_p (tree decl)
3079 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3080 && DECL_FUNCTION_SCOPE_P (decl)
3081 && (DECL_CONTEXT (decl) != current_function_decl
3082 || parsing_nsdmi ()));
3085 /* As above, but also checks that DECL is automatic. */
3087 bool
3088 outer_automatic_var_p (tree decl)
3090 return (outer_var_p (decl)
3091 && !TREE_STATIC (decl));
3094 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3095 rewrite it for lambda capture. */
3097 tree
3098 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3100 if (cp_unevaluated_operand)
3101 /* It's not a use (3.2) if we're in an unevaluated context. */
3102 return decl;
3103 if (decl == error_mark_node)
3104 return decl;
3106 tree context = DECL_CONTEXT (decl);
3107 tree containing_function = current_function_decl;
3108 tree lambda_stack = NULL_TREE;
3109 tree lambda_expr = NULL_TREE;
3110 tree initializer = convert_from_reference (decl);
3112 /* Mark it as used now even if the use is ill-formed. */
3113 if (!mark_used (decl, complain) && !(complain & tf_error))
3114 return error_mark_node;
3116 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3117 support for an approach in which a reference to a local
3118 [constant] automatic variable in a nested class or lambda body
3119 would enter the expression as an rvalue, which would reduce
3120 the complexity of the problem"
3122 FIXME update for final resolution of core issue 696. */
3123 if (decl_maybe_constant_var_p (decl))
3125 if (processing_template_decl)
3126 /* In a template, the constant value may not be in a usable
3127 form, so wait until instantiation time. */
3128 return decl;
3129 else if (decl_constant_var_p (decl))
3131 tree t = maybe_constant_value (convert_from_reference (decl));
3132 if (TREE_CONSTANT (t))
3133 return t;
3137 if (parsing_nsdmi ())
3138 containing_function = NULL_TREE;
3139 else
3140 /* If we are in a lambda function, we can move out until we hit
3141 1. the context,
3142 2. a non-lambda function, or
3143 3. a non-default capturing lambda function. */
3144 while (context != containing_function
3145 && LAMBDA_FUNCTION_P (containing_function))
3147 tree closure = DECL_CONTEXT (containing_function);
3148 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3150 if (TYPE_CLASS_SCOPE_P (closure))
3151 /* A lambda in an NSDMI (c++/64496). */
3152 break;
3154 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3155 == CPLD_NONE)
3156 break;
3158 lambda_stack = tree_cons (NULL_TREE,
3159 lambda_expr,
3160 lambda_stack);
3162 containing_function
3163 = decl_function_context (containing_function);
3166 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3167 && DECL_ANON_UNION_VAR_P (decl))
3169 if (complain & tf_error)
3170 error ("cannot capture member %qD of anonymous union", decl);
3171 return error_mark_node;
3173 if (context == containing_function)
3175 decl = add_default_capture (lambda_stack,
3176 /*id=*/DECL_NAME (decl),
3177 initializer);
3179 else if (lambda_expr)
3181 if (complain & tf_error)
3183 error ("%qD is not captured", decl);
3184 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3185 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3186 == CPLD_NONE)
3187 inform (location_of (closure),
3188 "the lambda has no capture-default");
3189 else if (TYPE_CLASS_SCOPE_P (closure))
3190 inform (0, "lambda in local class %q+T cannot "
3191 "capture variables from the enclosing context",
3192 TYPE_CONTEXT (closure));
3193 inform (input_location, "%q+#D declared here", decl);
3195 return error_mark_node;
3197 else
3199 if (complain & tf_error)
3200 error (VAR_P (decl)
3201 ? G_("use of local variable with automatic storage from containing function")
3202 : G_("use of parameter from containing function"));
3203 inform (input_location, "%q+#D declared here", decl);
3204 return error_mark_node;
3206 return decl;
3209 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3210 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3211 if non-NULL, is the type or namespace used to explicitly qualify
3212 ID_EXPRESSION. DECL is the entity to which that name has been
3213 resolved.
3215 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3216 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3217 be set to true if this expression isn't permitted in a
3218 constant-expression, but it is otherwise not set by this function.
3219 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3220 constant-expression, but a non-constant expression is also
3221 permissible.
3223 DONE is true if this expression is a complete postfix-expression;
3224 it is false if this expression is followed by '->', '[', '(', etc.
3225 ADDRESS_P is true iff this expression is the operand of '&'.
3226 TEMPLATE_P is true iff the qualified-id was of the form
3227 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3228 appears as a template argument.
3230 If an error occurs, and it is the kind of error that might cause
3231 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3232 is the caller's responsibility to issue the message. *ERROR_MSG
3233 will be a string with static storage duration, so the caller need
3234 not "free" it.
3236 Return an expression for the entity, after issuing appropriate
3237 diagnostics. This function is also responsible for transforming a
3238 reference to a non-static member into a COMPONENT_REF that makes
3239 the use of "this" explicit.
3241 Upon return, *IDK will be filled in appropriately. */
3242 tree
3243 finish_id_expression (tree id_expression,
3244 tree decl,
3245 tree scope,
3246 cp_id_kind *idk,
3247 bool integral_constant_expression_p,
3248 bool allow_non_integral_constant_expression_p,
3249 bool *non_integral_constant_expression_p,
3250 bool template_p,
3251 bool done,
3252 bool address_p,
3253 bool template_arg_p,
3254 const char **error_msg,
3255 location_t location)
3257 decl = strip_using_decl (decl);
3259 /* Initialize the output parameters. */
3260 *idk = CP_ID_KIND_NONE;
3261 *error_msg = NULL;
3263 if (id_expression == error_mark_node)
3264 return error_mark_node;
3265 /* If we have a template-id, then no further lookup is
3266 required. If the template-id was for a template-class, we
3267 will sometimes have a TYPE_DECL at this point. */
3268 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3269 || TREE_CODE (decl) == TYPE_DECL)
3271 /* Look up the name. */
3272 else
3274 if (decl == error_mark_node)
3276 /* Name lookup failed. */
3277 if (scope
3278 && (!TYPE_P (scope)
3279 || (!dependent_type_p (scope)
3280 && !(identifier_p (id_expression)
3281 && IDENTIFIER_TYPENAME_P (id_expression)
3282 && dependent_type_p (TREE_TYPE (id_expression))))))
3284 /* If the qualifying type is non-dependent (and the name
3285 does not name a conversion operator to a dependent
3286 type), issue an error. */
3287 qualified_name_lookup_error (scope, id_expression, decl, location);
3288 return error_mark_node;
3290 else if (!scope)
3292 /* It may be resolved via Koenig lookup. */
3293 *idk = CP_ID_KIND_UNQUALIFIED;
3294 return id_expression;
3296 else
3297 decl = id_expression;
3299 /* If DECL is a variable that would be out of scope under
3300 ANSI/ISO rules, but in scope in the ARM, name lookup
3301 will succeed. Issue a diagnostic here. */
3302 else
3303 decl = check_for_out_of_scope_variable (decl);
3305 /* Remember that the name was used in the definition of
3306 the current class so that we can check later to see if
3307 the meaning would have been different after the class
3308 was entirely defined. */
3309 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3310 maybe_note_name_used_in_class (id_expression, decl);
3312 /* Disallow uses of local variables from containing functions, except
3313 within lambda-expressions. */
3314 if (outer_automatic_var_p (decl))
3316 decl = process_outer_var_ref (decl, tf_warning_or_error);
3317 if (decl == error_mark_node)
3318 return error_mark_node;
3321 /* Also disallow uses of function parameters outside the function
3322 body, except inside an unevaluated context (i.e. decltype). */
3323 if (TREE_CODE (decl) == PARM_DECL
3324 && DECL_CONTEXT (decl) == NULL_TREE
3325 && !cp_unevaluated_operand)
3327 *error_msg = "use of parameter outside function body";
3328 return error_mark_node;
3332 /* If we didn't find anything, or what we found was a type,
3333 then this wasn't really an id-expression. */
3334 if (TREE_CODE (decl) == TEMPLATE_DECL
3335 && !DECL_FUNCTION_TEMPLATE_P (decl))
3337 *error_msg = "missing template arguments";
3338 return error_mark_node;
3340 else if (TREE_CODE (decl) == TYPE_DECL
3341 || TREE_CODE (decl) == NAMESPACE_DECL)
3343 *error_msg = "expected primary-expression";
3344 return error_mark_node;
3347 /* If the name resolved to a template parameter, there is no
3348 need to look it up again later. */
3349 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3350 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3352 tree r;
3354 *idk = CP_ID_KIND_NONE;
3355 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3356 decl = TEMPLATE_PARM_DECL (decl);
3357 r = convert_from_reference (DECL_INITIAL (decl));
3359 if (integral_constant_expression_p
3360 && !dependent_type_p (TREE_TYPE (decl))
3361 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3363 if (!allow_non_integral_constant_expression_p)
3364 error ("template parameter %qD of type %qT is not allowed in "
3365 "an integral constant expression because it is not of "
3366 "integral or enumeration type", decl, TREE_TYPE (decl));
3367 *non_integral_constant_expression_p = true;
3369 return r;
3371 else
3373 bool dependent_p;
3375 /* If the declaration was explicitly qualified indicate
3376 that. The semantics of `A::f(3)' are different than
3377 `f(3)' if `f' is virtual. */
3378 *idk = (scope
3379 ? CP_ID_KIND_QUALIFIED
3380 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3381 ? CP_ID_KIND_TEMPLATE_ID
3382 : CP_ID_KIND_UNQUALIFIED));
3385 /* [temp.dep.expr]
3387 An id-expression is type-dependent if it contains an
3388 identifier that was declared with a dependent type.
3390 The standard is not very specific about an id-expression that
3391 names a set of overloaded functions. What if some of them
3392 have dependent types and some of them do not? Presumably,
3393 such a name should be treated as a dependent name. */
3394 /* Assume the name is not dependent. */
3395 dependent_p = false;
3396 if (!processing_template_decl)
3397 /* No names are dependent outside a template. */
3399 else if (TREE_CODE (decl) == CONST_DECL)
3400 /* We don't want to treat enumerators as dependent. */
3402 /* A template-id where the name of the template was not resolved
3403 is definitely dependent. */
3404 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3405 && (identifier_p (TREE_OPERAND (decl, 0))))
3406 dependent_p = true;
3407 /* For anything except an overloaded function, just check its
3408 type. */
3409 else if (!is_overloaded_fn (decl))
3410 dependent_p
3411 = dependent_type_p (TREE_TYPE (decl));
3412 /* For a set of overloaded functions, check each of the
3413 functions. */
3414 else
3416 tree fns = decl;
3418 if (BASELINK_P (fns))
3419 fns = BASELINK_FUNCTIONS (fns);
3421 /* For a template-id, check to see if the template
3422 arguments are dependent. */
3423 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3425 tree args = TREE_OPERAND (fns, 1);
3426 dependent_p = any_dependent_template_arguments_p (args);
3427 /* The functions are those referred to by the
3428 template-id. */
3429 fns = TREE_OPERAND (fns, 0);
3432 /* If there are no dependent template arguments, go through
3433 the overloaded functions. */
3434 while (fns && !dependent_p)
3436 tree fn = OVL_CURRENT (fns);
3438 /* Member functions of dependent classes are
3439 dependent. */
3440 if (TREE_CODE (fn) == FUNCTION_DECL
3441 && type_dependent_expression_p (fn))
3442 dependent_p = true;
3443 else if (TREE_CODE (fn) == TEMPLATE_DECL
3444 && dependent_template_p (fn))
3445 dependent_p = true;
3447 fns = OVL_NEXT (fns);
3451 /* If the name was dependent on a template parameter, we will
3452 resolve the name at instantiation time. */
3453 if (dependent_p)
3455 /* Create a SCOPE_REF for qualified names, if the scope is
3456 dependent. */
3457 if (scope)
3459 if (TYPE_P (scope))
3461 if (address_p && done)
3462 decl = finish_qualified_id_expr (scope, decl,
3463 done, address_p,
3464 template_p,
3465 template_arg_p,
3466 tf_warning_or_error);
3467 else
3469 tree type = NULL_TREE;
3470 if (DECL_P (decl) && !dependent_scope_p (scope))
3471 type = TREE_TYPE (decl);
3472 decl = build_qualified_name (type,
3473 scope,
3474 id_expression,
3475 template_p);
3478 if (TREE_TYPE (decl))
3479 decl = convert_from_reference (decl);
3480 return decl;
3482 /* A TEMPLATE_ID already contains all the information we
3483 need. */
3484 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3485 return id_expression;
3486 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3487 /* If we found a variable, then name lookup during the
3488 instantiation will always resolve to the same VAR_DECL
3489 (or an instantiation thereof). */
3490 if (VAR_P (decl)
3491 || TREE_CODE (decl) == PARM_DECL)
3493 mark_used (decl);
3494 return convert_from_reference (decl);
3496 /* The same is true for FIELD_DECL, but we also need to
3497 make sure that the syntax is correct. */
3498 else if (TREE_CODE (decl) == FIELD_DECL)
3500 /* Since SCOPE is NULL here, this is an unqualified name.
3501 Access checking has been performed during name lookup
3502 already. Turn off checking to avoid duplicate errors. */
3503 push_deferring_access_checks (dk_no_check);
3504 decl = finish_non_static_data_member
3505 (decl, NULL_TREE,
3506 /*qualifying_scope=*/NULL_TREE);
3507 pop_deferring_access_checks ();
3508 return decl;
3510 return id_expression;
3513 if (TREE_CODE (decl) == NAMESPACE_DECL)
3515 error ("use of namespace %qD as expression", decl);
3516 return error_mark_node;
3518 else if (DECL_CLASS_TEMPLATE_P (decl))
3520 error ("use of class template %qT as expression", decl);
3521 return error_mark_node;
3523 else if (TREE_CODE (decl) == TREE_LIST)
3525 /* Ambiguous reference to base members. */
3526 error ("request for member %qD is ambiguous in "
3527 "multiple inheritance lattice", id_expression);
3528 print_candidates (decl);
3529 return error_mark_node;
3532 /* Mark variable-like entities as used. Functions are similarly
3533 marked either below or after overload resolution. */
3534 if ((VAR_P (decl)
3535 || TREE_CODE (decl) == PARM_DECL
3536 || TREE_CODE (decl) == CONST_DECL
3537 || TREE_CODE (decl) == RESULT_DECL)
3538 && !mark_used (decl))
3539 return error_mark_node;
3541 /* Only certain kinds of names are allowed in constant
3542 expression. Template parameters have already
3543 been handled above. */
3544 if (! error_operand_p (decl)
3545 && integral_constant_expression_p
3546 && ! decl_constant_var_p (decl)
3547 && TREE_CODE (decl) != CONST_DECL
3548 && ! builtin_valid_in_constant_expr_p (decl))
3550 if (!allow_non_integral_constant_expression_p)
3552 error ("%qD cannot appear in a constant-expression", decl);
3553 return error_mark_node;
3555 *non_integral_constant_expression_p = true;
3558 tree wrap;
3559 if (VAR_P (decl)
3560 && !cp_unevaluated_operand
3561 && !processing_template_decl
3562 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3563 && DECL_THREAD_LOCAL_P (decl)
3564 && (wrap = get_tls_wrapper_fn (decl)))
3566 /* Replace an evaluated use of the thread_local variable with
3567 a call to its wrapper. */
3568 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3570 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3571 && variable_template_p (TREE_OPERAND (decl, 0)))
3573 decl = finish_template_variable (decl);
3574 mark_used (decl);
3576 else if (scope)
3578 decl = (adjust_result_of_qualified_name_lookup
3579 (decl, scope, current_nonlambda_class_type()));
3581 if (TREE_CODE (decl) == FUNCTION_DECL)
3582 mark_used (decl);
3584 if (TYPE_P (scope))
3585 decl = finish_qualified_id_expr (scope,
3586 decl,
3587 done,
3588 address_p,
3589 template_p,
3590 template_arg_p,
3591 tf_warning_or_error);
3592 else
3593 decl = convert_from_reference (decl);
3595 else if (TREE_CODE (decl) == FIELD_DECL)
3597 /* Since SCOPE is NULL here, this is an unqualified name.
3598 Access checking has been performed during name lookup
3599 already. Turn off checking to avoid duplicate errors. */
3600 push_deferring_access_checks (dk_no_check);
3601 decl = finish_non_static_data_member (decl, NULL_TREE,
3602 /*qualifying_scope=*/NULL_TREE);
3603 pop_deferring_access_checks ();
3605 else if (is_overloaded_fn (decl))
3607 tree first_fn;
3609 first_fn = get_first_fn (decl);
3610 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3611 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3613 if (!really_overloaded_fn (decl)
3614 && !mark_used (first_fn))
3615 return error_mark_node;
3617 if (!template_arg_p
3618 && TREE_CODE (first_fn) == FUNCTION_DECL
3619 && DECL_FUNCTION_MEMBER_P (first_fn)
3620 && !shared_member_p (decl))
3622 /* A set of member functions. */
3623 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3624 return finish_class_member_access_expr (decl, id_expression,
3625 /*template_p=*/false,
3626 tf_warning_or_error);
3629 decl = baselink_for_fns (decl);
3631 else
3633 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3634 && DECL_CLASS_SCOPE_P (decl))
3636 tree context = context_for_name_lookup (decl);
3637 if (context != current_class_type)
3639 tree path = currently_open_derived_class (context);
3640 perform_or_defer_access_check (TYPE_BINFO (path),
3641 decl, decl,
3642 tf_warning_or_error);
3646 decl = convert_from_reference (decl);
3650 return decl;
3653 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3654 use as a type-specifier. */
3656 tree
3657 finish_typeof (tree expr)
3659 tree type;
3661 if (type_dependent_expression_p (expr))
3663 type = cxx_make_type (TYPEOF_TYPE);
3664 TYPEOF_TYPE_EXPR (type) = expr;
3665 SET_TYPE_STRUCTURAL_EQUALITY (type);
3667 return type;
3670 expr = mark_type_use (expr);
3672 type = unlowered_expr_type (expr);
3674 if (!type || type == unknown_type_node)
3676 error ("type of %qE is unknown", expr);
3677 return error_mark_node;
3680 return type;
3683 /* Implement the __underlying_type keyword: Return the underlying
3684 type of TYPE, suitable for use as a type-specifier. */
3686 tree
3687 finish_underlying_type (tree type)
3689 tree underlying_type;
3691 if (processing_template_decl)
3693 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3694 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3695 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3697 return underlying_type;
3700 complete_type (type);
3702 if (TREE_CODE (type) != ENUMERAL_TYPE)
3704 error ("%qT is not an enumeration type", type);
3705 return error_mark_node;
3708 underlying_type = ENUM_UNDERLYING_TYPE (type);
3710 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3711 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3712 See finish_enum_value_list for details. */
3713 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3714 underlying_type
3715 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3716 TYPE_UNSIGNED (underlying_type));
3718 return underlying_type;
3721 /* Implement the __direct_bases keyword: Return the direct base classes
3722 of type */
3724 tree
3725 calculate_direct_bases (tree type)
3727 vec<tree, va_gc> *vector = make_tree_vector();
3728 tree bases_vec = NULL_TREE;
3729 vec<tree, va_gc> *base_binfos;
3730 tree binfo;
3731 unsigned i;
3733 complete_type (type);
3735 if (!NON_UNION_CLASS_TYPE_P (type))
3736 return make_tree_vec (0);
3738 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3740 /* Virtual bases are initialized first */
3741 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3743 if (BINFO_VIRTUAL_P (binfo))
3745 vec_safe_push (vector, binfo);
3749 /* Now non-virtuals */
3750 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3752 if (!BINFO_VIRTUAL_P (binfo))
3754 vec_safe_push (vector, binfo);
3759 bases_vec = make_tree_vec (vector->length ());
3761 for (i = 0; i < vector->length (); ++i)
3763 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3765 return bases_vec;
3768 /* Implement the __bases keyword: Return the base classes
3769 of type */
3771 /* Find morally non-virtual base classes by walking binfo hierarchy */
3772 /* Virtual base classes are handled separately in finish_bases */
3774 static tree
3775 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3777 /* Don't walk bases of virtual bases */
3778 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3781 static tree
3782 dfs_calculate_bases_post (tree binfo, void *data_)
3784 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3785 if (!BINFO_VIRTUAL_P (binfo))
3787 vec_safe_push (*data, BINFO_TYPE (binfo));
3789 return NULL_TREE;
3792 /* Calculates the morally non-virtual base classes of a class */
3793 static vec<tree, va_gc> *
3794 calculate_bases_helper (tree type)
3796 vec<tree, va_gc> *vector = make_tree_vector();
3798 /* Now add non-virtual base classes in order of construction */
3799 dfs_walk_all (TYPE_BINFO (type),
3800 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3801 return vector;
3804 tree
3805 calculate_bases (tree type)
3807 vec<tree, va_gc> *vector = make_tree_vector();
3808 tree bases_vec = NULL_TREE;
3809 unsigned i;
3810 vec<tree, va_gc> *vbases;
3811 vec<tree, va_gc> *nonvbases;
3812 tree binfo;
3814 complete_type (type);
3816 if (!NON_UNION_CLASS_TYPE_P (type))
3817 return make_tree_vec (0);
3819 /* First go through virtual base classes */
3820 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3821 vec_safe_iterate (vbases, i, &binfo); i++)
3823 vec<tree, va_gc> *vbase_bases;
3824 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3825 vec_safe_splice (vector, vbase_bases);
3826 release_tree_vector (vbase_bases);
3829 /* Now for the non-virtual bases */
3830 nonvbases = calculate_bases_helper (type);
3831 vec_safe_splice (vector, nonvbases);
3832 release_tree_vector (nonvbases);
3834 /* Last element is entire class, so don't copy */
3835 bases_vec = make_tree_vec (vector->length () - 1);
3837 for (i = 0; i < vector->length () - 1; ++i)
3839 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3841 release_tree_vector (vector);
3842 return bases_vec;
3845 tree
3846 finish_bases (tree type, bool direct)
3848 tree bases = NULL_TREE;
3850 if (!processing_template_decl)
3852 /* Parameter packs can only be used in templates */
3853 error ("Parameter pack __bases only valid in template declaration");
3854 return error_mark_node;
3857 bases = cxx_make_type (BASES);
3858 BASES_TYPE (bases) = type;
3859 BASES_DIRECT (bases) = direct;
3860 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3862 return bases;
3865 /* Perform C++-specific checks for __builtin_offsetof before calling
3866 fold_offsetof. */
3868 tree
3869 finish_offsetof (tree expr, location_t loc)
3871 /* If we're processing a template, we can't finish the semantics yet.
3872 Otherwise we can fold the entire expression now. */
3873 if (processing_template_decl)
3875 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3876 SET_EXPR_LOCATION (expr, loc);
3877 return expr;
3880 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3882 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3883 TREE_OPERAND (expr, 2));
3884 return error_mark_node;
3886 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3887 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3888 || TREE_TYPE (expr) == unknown_type_node)
3890 if (INDIRECT_REF_P (expr))
3891 error ("second operand of %<offsetof%> is neither a single "
3892 "identifier nor a sequence of member accesses and "
3893 "array references");
3894 else
3896 if (TREE_CODE (expr) == COMPONENT_REF
3897 || TREE_CODE (expr) == COMPOUND_EXPR)
3898 expr = TREE_OPERAND (expr, 1);
3899 error ("cannot apply %<offsetof%> to member function %qD", expr);
3901 return error_mark_node;
3903 if (REFERENCE_REF_P (expr))
3904 expr = TREE_OPERAND (expr, 0);
3905 if (TREE_CODE (expr) == COMPONENT_REF)
3907 tree object = TREE_OPERAND (expr, 0);
3908 if (!complete_type_or_else (TREE_TYPE (object), object))
3909 return error_mark_node;
3910 if (warn_invalid_offsetof
3911 && CLASS_TYPE_P (TREE_TYPE (object))
3912 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3913 && cp_unevaluated_operand == 0)
3914 pedwarn (loc, OPT_Winvalid_offsetof,
3915 "offsetof within non-standard-layout type %qT is undefined",
3916 TREE_TYPE (object));
3918 return fold_offsetof (expr);
3921 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3922 function is broken out from the above for the benefit of the tree-ssa
3923 project. */
3925 void
3926 simplify_aggr_init_expr (tree *tp)
3928 tree aggr_init_expr = *tp;
3930 /* Form an appropriate CALL_EXPR. */
3931 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3932 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3933 tree type = TREE_TYPE (slot);
3935 tree call_expr;
3936 enum style_t { ctor, arg, pcc } style;
3938 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3939 style = ctor;
3940 #ifdef PCC_STATIC_STRUCT_RETURN
3941 else if (1)
3942 style = pcc;
3943 #endif
3944 else
3946 gcc_assert (TREE_ADDRESSABLE (type));
3947 style = arg;
3950 call_expr = build_call_array_loc (input_location,
3951 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3953 aggr_init_expr_nargs (aggr_init_expr),
3954 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3955 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3956 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3958 if (style == ctor)
3960 /* Replace the first argument to the ctor with the address of the
3961 slot. */
3962 cxx_mark_addressable (slot);
3963 CALL_EXPR_ARG (call_expr, 0) =
3964 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3966 else if (style == arg)
3968 /* Just mark it addressable here, and leave the rest to
3969 expand_call{,_inline}. */
3970 cxx_mark_addressable (slot);
3971 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3972 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3974 else if (style == pcc)
3976 /* If we're using the non-reentrant PCC calling convention, then we
3977 need to copy the returned value out of the static buffer into the
3978 SLOT. */
3979 push_deferring_access_checks (dk_no_check);
3980 call_expr = build_aggr_init (slot, call_expr,
3981 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3982 tf_warning_or_error);
3983 pop_deferring_access_checks ();
3984 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3987 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3989 tree init = build_zero_init (type, NULL_TREE,
3990 /*static_storage_p=*/false);
3991 init = build2 (INIT_EXPR, void_type_node, slot, init);
3992 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3993 init, call_expr);
3996 *tp = call_expr;
3999 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4001 void
4002 emit_associated_thunks (tree fn)
4004 /* When we use vcall offsets, we emit thunks with the virtual
4005 functions to which they thunk. The whole point of vcall offsets
4006 is so that you can know statically the entire set of thunks that
4007 will ever be needed for a given virtual function, thereby
4008 enabling you to output all the thunks with the function itself. */
4009 if (DECL_VIRTUAL_P (fn)
4010 /* Do not emit thunks for extern template instantiations. */
4011 && ! DECL_REALLY_EXTERN (fn))
4013 tree thunk;
4015 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4017 if (!THUNK_ALIAS (thunk))
4019 use_thunk (thunk, /*emit_p=*/1);
4020 if (DECL_RESULT_THUNK_P (thunk))
4022 tree probe;
4024 for (probe = DECL_THUNKS (thunk);
4025 probe; probe = DECL_CHAIN (probe))
4026 use_thunk (probe, /*emit_p=*/1);
4029 else
4030 gcc_assert (!DECL_THUNKS (thunk));
4035 /* Generate RTL for FN. */
4037 bool
4038 expand_or_defer_fn_1 (tree fn)
4040 /* When the parser calls us after finishing the body of a template
4041 function, we don't really want to expand the body. */
4042 if (processing_template_decl)
4044 /* Normally, collection only occurs in rest_of_compilation. So,
4045 if we don't collect here, we never collect junk generated
4046 during the processing of templates until we hit a
4047 non-template function. It's not safe to do this inside a
4048 nested class, though, as the parser may have local state that
4049 is not a GC root. */
4050 if (!function_depth)
4051 ggc_collect ();
4052 return false;
4055 gcc_assert (DECL_SAVED_TREE (fn));
4057 /* We make a decision about linkage for these functions at the end
4058 of the compilation. Until that point, we do not want the back
4059 end to output them -- but we do want it to see the bodies of
4060 these functions so that it can inline them as appropriate. */
4061 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4063 if (DECL_INTERFACE_KNOWN (fn))
4064 /* We've already made a decision as to how this function will
4065 be handled. */;
4066 else if (!at_eof)
4067 tentative_decl_linkage (fn);
4068 else
4069 import_export_decl (fn);
4071 /* If the user wants us to keep all inline functions, then mark
4072 this function as needed so that finish_file will make sure to
4073 output it later. Similarly, all dllexport'd functions must
4074 be emitted; there may be callers in other DLLs. */
4075 if (DECL_DECLARED_INLINE_P (fn)
4076 && !DECL_REALLY_EXTERN (fn)
4077 && (flag_keep_inline_functions
4078 || (flag_keep_inline_dllexport
4079 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4081 mark_needed (fn);
4082 DECL_EXTERNAL (fn) = 0;
4086 /* If this is a constructor or destructor body, we have to clone
4087 it. */
4088 if (maybe_clone_body (fn))
4090 /* We don't want to process FN again, so pretend we've written
4091 it out, even though we haven't. */
4092 TREE_ASM_WRITTEN (fn) = 1;
4093 /* If this is an instantiation of a constexpr function, keep
4094 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4095 if (!is_instantiation_of_constexpr (fn))
4096 DECL_SAVED_TREE (fn) = NULL_TREE;
4097 return false;
4100 /* There's no reason to do any of the work here if we're only doing
4101 semantic analysis; this code just generates RTL. */
4102 if (flag_syntax_only)
4103 return false;
4105 return true;
4108 void
4109 expand_or_defer_fn (tree fn)
4111 if (expand_or_defer_fn_1 (fn))
4113 function_depth++;
4115 /* Expand or defer, at the whim of the compilation unit manager. */
4116 cgraph_node::finalize_function (fn, function_depth > 1);
4117 emit_associated_thunks (fn);
4119 function_depth--;
4123 struct nrv_data
4125 nrv_data () : visited (37) {}
4127 tree var;
4128 tree result;
4129 hash_table<pointer_hash <tree_node> > visited;
4132 /* Helper function for walk_tree, used by finalize_nrv below. */
4134 static tree
4135 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4137 struct nrv_data *dp = (struct nrv_data *)data;
4138 tree_node **slot;
4140 /* No need to walk into types. There wouldn't be any need to walk into
4141 non-statements, except that we have to consider STMT_EXPRs. */
4142 if (TYPE_P (*tp))
4143 *walk_subtrees = 0;
4144 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4145 but differs from using NULL_TREE in that it indicates that we care
4146 about the value of the RESULT_DECL. */
4147 else if (TREE_CODE (*tp) == RETURN_EXPR)
4148 TREE_OPERAND (*tp, 0) = dp->result;
4149 /* Change all cleanups for the NRV to only run when an exception is
4150 thrown. */
4151 else if (TREE_CODE (*tp) == CLEANUP_STMT
4152 && CLEANUP_DECL (*tp) == dp->var)
4153 CLEANUP_EH_ONLY (*tp) = 1;
4154 /* Replace the DECL_EXPR for the NRV with an initialization of the
4155 RESULT_DECL, if needed. */
4156 else if (TREE_CODE (*tp) == DECL_EXPR
4157 && DECL_EXPR_DECL (*tp) == dp->var)
4159 tree init;
4160 if (DECL_INITIAL (dp->var)
4161 && DECL_INITIAL (dp->var) != error_mark_node)
4162 init = build2 (INIT_EXPR, void_type_node, dp->result,
4163 DECL_INITIAL (dp->var));
4164 else
4165 init = build_empty_stmt (EXPR_LOCATION (*tp));
4166 DECL_INITIAL (dp->var) = NULL_TREE;
4167 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4168 *tp = init;
4170 /* And replace all uses of the NRV with the RESULT_DECL. */
4171 else if (*tp == dp->var)
4172 *tp = dp->result;
4174 /* Avoid walking into the same tree more than once. Unfortunately, we
4175 can't just use walk_tree_without duplicates because it would only call
4176 us for the first occurrence of dp->var in the function body. */
4177 slot = dp->visited.find_slot (*tp, INSERT);
4178 if (*slot)
4179 *walk_subtrees = 0;
4180 else
4181 *slot = *tp;
4183 /* Keep iterating. */
4184 return NULL_TREE;
4187 /* Called from finish_function to implement the named return value
4188 optimization by overriding all the RETURN_EXPRs and pertinent
4189 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4190 RESULT_DECL for the function. */
4192 void
4193 finalize_nrv (tree *tp, tree var, tree result)
4195 struct nrv_data data;
4197 /* Copy name from VAR to RESULT. */
4198 DECL_NAME (result) = DECL_NAME (var);
4199 /* Don't forget that we take its address. */
4200 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4201 /* Finally set DECL_VALUE_EXPR to avoid assigning
4202 a stack slot at -O0 for the original var and debug info
4203 uses RESULT location for VAR. */
4204 SET_DECL_VALUE_EXPR (var, result);
4205 DECL_HAS_VALUE_EXPR_P (var) = 1;
4207 data.var = var;
4208 data.result = result;
4209 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4212 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4214 bool
4215 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4216 bool need_copy_ctor, bool need_copy_assignment,
4217 bool need_dtor)
4219 int save_errorcount = errorcount;
4220 tree info, t;
4222 /* Always allocate 3 elements for simplicity. These are the
4223 function decls for the ctor, dtor, and assignment op.
4224 This layout is known to the three lang hooks,
4225 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4226 and cxx_omp_clause_assign_op. */
4227 info = make_tree_vec (3);
4228 CP_OMP_CLAUSE_INFO (c) = info;
4230 if (need_default_ctor || need_copy_ctor)
4232 if (need_default_ctor)
4233 t = get_default_ctor (type);
4234 else
4235 t = get_copy_ctor (type, tf_warning_or_error);
4237 if (t && !trivial_fn_p (t))
4238 TREE_VEC_ELT (info, 0) = t;
4241 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4242 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4244 if (need_copy_assignment)
4246 t = get_copy_assign (type);
4248 if (t && !trivial_fn_p (t))
4249 TREE_VEC_ELT (info, 2) = t;
4252 return errorcount != save_errorcount;
4255 /* Helper function for handle_omp_array_sections. Called recursively
4256 to handle multiple array-section-subscripts. C is the clause,
4257 T current expression (initially OMP_CLAUSE_DECL), which is either
4258 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4259 expression if specified, TREE_VALUE length expression if specified,
4260 TREE_CHAIN is what it has been specified after, or some decl.
4261 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4262 set to true if any of the array-section-subscript could have length
4263 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4264 first array-section-subscript which is known not to have length
4265 of one. Given say:
4266 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4267 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4268 all are or may have length of 1, array-section-subscript [:2] is the
4269 first one knonwn not to have length 1. For array-section-subscript
4270 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4271 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4272 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4273 case though, as some lengths could be zero. */
4275 static tree
4276 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4277 bool &maybe_zero_len, unsigned int &first_non_one)
4279 tree ret, low_bound, length, type;
4280 if (TREE_CODE (t) != TREE_LIST)
4282 if (error_operand_p (t))
4283 return error_mark_node;
4284 if (type_dependent_expression_p (t))
4285 return NULL_TREE;
4286 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4288 if (processing_template_decl)
4289 return NULL_TREE;
4290 if (DECL_P (t))
4291 error_at (OMP_CLAUSE_LOCATION (c),
4292 "%qD is not a variable in %qs clause", t,
4293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4294 else
4295 error_at (OMP_CLAUSE_LOCATION (c),
4296 "%qE is not a variable in %qs clause", t,
4297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4298 return error_mark_node;
4300 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4301 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4303 error_at (OMP_CLAUSE_LOCATION (c),
4304 "%qD is threadprivate variable in %qs clause", t,
4305 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4306 return error_mark_node;
4308 t = convert_from_reference (t);
4309 return t;
4312 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4313 maybe_zero_len, first_non_one);
4314 if (ret == error_mark_node || ret == NULL_TREE)
4315 return ret;
4317 type = TREE_TYPE (ret);
4318 low_bound = TREE_PURPOSE (t);
4319 length = TREE_VALUE (t);
4320 if ((low_bound && type_dependent_expression_p (low_bound))
4321 || (length && type_dependent_expression_p (length)))
4322 return NULL_TREE;
4324 if (low_bound == error_mark_node || length == error_mark_node)
4325 return error_mark_node;
4327 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4329 error_at (OMP_CLAUSE_LOCATION (c),
4330 "low bound %qE of array section does not have integral type",
4331 low_bound);
4332 return error_mark_node;
4334 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4336 error_at (OMP_CLAUSE_LOCATION (c),
4337 "length %qE of array section does not have integral type",
4338 length);
4339 return error_mark_node;
4341 if (low_bound)
4342 low_bound = mark_rvalue_use (low_bound);
4343 if (length)
4344 length = mark_rvalue_use (length);
4345 if (low_bound
4346 && TREE_CODE (low_bound) == INTEGER_CST
4347 && TYPE_PRECISION (TREE_TYPE (low_bound))
4348 > TYPE_PRECISION (sizetype))
4349 low_bound = fold_convert (sizetype, low_bound);
4350 if (length
4351 && TREE_CODE (length) == INTEGER_CST
4352 && TYPE_PRECISION (TREE_TYPE (length))
4353 > TYPE_PRECISION (sizetype))
4354 length = fold_convert (sizetype, length);
4355 if (low_bound == NULL_TREE)
4356 low_bound = integer_zero_node;
4358 if (length != NULL_TREE)
4360 if (!integer_nonzerop (length))
4361 maybe_zero_len = true;
4362 if (first_non_one == types.length ()
4363 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4364 first_non_one++;
4366 if (TREE_CODE (type) == ARRAY_TYPE)
4368 if (length == NULL_TREE
4369 && (TYPE_DOMAIN (type) == NULL_TREE
4370 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4372 error_at (OMP_CLAUSE_LOCATION (c),
4373 "for unknown bound array type length expression must "
4374 "be specified");
4375 return error_mark_node;
4377 if (TREE_CODE (low_bound) == INTEGER_CST
4378 && tree_int_cst_sgn (low_bound) == -1)
4380 error_at (OMP_CLAUSE_LOCATION (c),
4381 "negative low bound in array section in %qs clause",
4382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4383 return error_mark_node;
4385 if (length != NULL_TREE
4386 && TREE_CODE (length) == INTEGER_CST
4387 && tree_int_cst_sgn (length) == -1)
4389 error_at (OMP_CLAUSE_LOCATION (c),
4390 "negative length in array section in %qs clause",
4391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4392 return error_mark_node;
4394 if (TYPE_DOMAIN (type)
4395 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4396 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4397 == INTEGER_CST)
4399 tree size = size_binop (PLUS_EXPR,
4400 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4401 size_one_node);
4402 if (TREE_CODE (low_bound) == INTEGER_CST)
4404 if (tree_int_cst_lt (size, low_bound))
4406 error_at (OMP_CLAUSE_LOCATION (c),
4407 "low bound %qE above array section size "
4408 "in %qs clause", low_bound,
4409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4410 return error_mark_node;
4412 if (tree_int_cst_equal (size, low_bound))
4413 maybe_zero_len = true;
4414 else if (length == NULL_TREE
4415 && first_non_one == types.length ()
4416 && tree_int_cst_equal
4417 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4418 low_bound))
4419 first_non_one++;
4421 else if (length == NULL_TREE)
4423 maybe_zero_len = true;
4424 if (first_non_one == types.length ())
4425 first_non_one++;
4427 if (length && TREE_CODE (length) == INTEGER_CST)
4429 if (tree_int_cst_lt (size, length))
4431 error_at (OMP_CLAUSE_LOCATION (c),
4432 "length %qE above array section size "
4433 "in %qs clause", length,
4434 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4435 return error_mark_node;
4437 if (TREE_CODE (low_bound) == INTEGER_CST)
4439 tree lbpluslen
4440 = size_binop (PLUS_EXPR,
4441 fold_convert (sizetype, low_bound),
4442 fold_convert (sizetype, length));
4443 if (TREE_CODE (lbpluslen) == INTEGER_CST
4444 && tree_int_cst_lt (size, lbpluslen))
4446 error_at (OMP_CLAUSE_LOCATION (c),
4447 "high bound %qE above array section size "
4448 "in %qs clause", lbpluslen,
4449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4450 return error_mark_node;
4455 else if (length == NULL_TREE)
4457 maybe_zero_len = true;
4458 if (first_non_one == types.length ())
4459 first_non_one++;
4462 /* For [lb:] we will need to evaluate lb more than once. */
4463 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4465 tree lb = cp_save_expr (low_bound);
4466 if (lb != low_bound)
4468 TREE_PURPOSE (t) = lb;
4469 low_bound = lb;
4473 else if (TREE_CODE (type) == POINTER_TYPE)
4475 if (length == NULL_TREE)
4477 error_at (OMP_CLAUSE_LOCATION (c),
4478 "for pointer type length expression must be specified");
4479 return error_mark_node;
4481 /* If there is a pointer type anywhere but in the very first
4482 array-section-subscript, the array section can't be contiguous. */
4483 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4484 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4486 error_at (OMP_CLAUSE_LOCATION (c),
4487 "array section is not contiguous in %qs clause",
4488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4489 return error_mark_node;
4492 else
4494 error_at (OMP_CLAUSE_LOCATION (c),
4495 "%qE does not have pointer or array type", ret);
4496 return error_mark_node;
4498 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4499 types.safe_push (TREE_TYPE (ret));
4500 /* We will need to evaluate lb more than once. */
4501 tree lb = cp_save_expr (low_bound);
4502 if (lb != low_bound)
4504 TREE_PURPOSE (t) = lb;
4505 low_bound = lb;
4507 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4508 return ret;
4511 /* Handle array sections for clause C. */
4513 static bool
4514 handle_omp_array_sections (tree c)
4516 bool maybe_zero_len = false;
4517 unsigned int first_non_one = 0;
4518 auto_vec<tree> types;
4519 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4520 maybe_zero_len, first_non_one);
4521 if (first == error_mark_node)
4522 return true;
4523 if (first == NULL_TREE)
4524 return false;
4525 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4527 tree t = OMP_CLAUSE_DECL (c);
4528 tree tem = NULL_TREE;
4529 if (processing_template_decl)
4530 return false;
4531 /* Need to evaluate side effects in the length expressions
4532 if any. */
4533 while (TREE_CODE (t) == TREE_LIST)
4535 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4537 if (tem == NULL_TREE)
4538 tem = TREE_VALUE (t);
4539 else
4540 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4541 TREE_VALUE (t), tem);
4543 t = TREE_CHAIN (t);
4545 if (tem)
4546 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4547 OMP_CLAUSE_DECL (c) = first;
4549 else
4551 unsigned int num = types.length (), i;
4552 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4553 tree condition = NULL_TREE;
4555 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4556 maybe_zero_len = true;
4557 if (processing_template_decl && maybe_zero_len)
4558 return false;
4560 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4561 t = TREE_CHAIN (t))
4563 tree low_bound = TREE_PURPOSE (t);
4564 tree length = TREE_VALUE (t);
4566 i--;
4567 if (low_bound
4568 && TREE_CODE (low_bound) == INTEGER_CST
4569 && TYPE_PRECISION (TREE_TYPE (low_bound))
4570 > TYPE_PRECISION (sizetype))
4571 low_bound = fold_convert (sizetype, low_bound);
4572 if (length
4573 && TREE_CODE (length) == INTEGER_CST
4574 && TYPE_PRECISION (TREE_TYPE (length))
4575 > TYPE_PRECISION (sizetype))
4576 length = fold_convert (sizetype, length);
4577 if (low_bound == NULL_TREE)
4578 low_bound = integer_zero_node;
4579 if (!maybe_zero_len && i > first_non_one)
4581 if (integer_nonzerop (low_bound))
4582 goto do_warn_noncontiguous;
4583 if (length != NULL_TREE
4584 && TREE_CODE (length) == INTEGER_CST
4585 && TYPE_DOMAIN (types[i])
4586 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4587 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4588 == INTEGER_CST)
4590 tree size;
4591 size = size_binop (PLUS_EXPR,
4592 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4593 size_one_node);
4594 if (!tree_int_cst_equal (length, size))
4596 do_warn_noncontiguous:
4597 error_at (OMP_CLAUSE_LOCATION (c),
4598 "array section is not contiguous in %qs "
4599 "clause",
4600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4601 return true;
4604 if (!processing_template_decl
4605 && length != NULL_TREE
4606 && TREE_SIDE_EFFECTS (length))
4608 if (side_effects == NULL_TREE)
4609 side_effects = length;
4610 else
4611 side_effects = build2 (COMPOUND_EXPR,
4612 TREE_TYPE (side_effects),
4613 length, side_effects);
4616 else if (processing_template_decl)
4617 continue;
4618 else
4620 tree l;
4622 if (i > first_non_one && length && integer_nonzerop (length))
4623 continue;
4624 if (length)
4625 l = fold_convert (sizetype, length);
4626 else
4628 l = size_binop (PLUS_EXPR,
4629 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4630 size_one_node);
4631 l = size_binop (MINUS_EXPR, l,
4632 fold_convert (sizetype, low_bound));
4634 if (i > first_non_one)
4636 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4637 size_zero_node);
4638 if (condition == NULL_TREE)
4639 condition = l;
4640 else
4641 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4642 l, condition);
4644 else if (size == NULL_TREE)
4646 size = size_in_bytes (TREE_TYPE (types[i]));
4647 size = size_binop (MULT_EXPR, size, l);
4648 if (condition)
4649 size = fold_build3 (COND_EXPR, sizetype, condition,
4650 size, size_zero_node);
4652 else
4653 size = size_binop (MULT_EXPR, size, l);
4656 if (!processing_template_decl)
4658 if (side_effects)
4659 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4660 OMP_CLAUSE_DECL (c) = first;
4661 OMP_CLAUSE_SIZE (c) = size;
4662 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4663 return false;
4664 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4665 OMP_CLAUSE_MAP);
4666 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4667 if (!cxx_mark_addressable (t))
4668 return false;
4669 OMP_CLAUSE_DECL (c2) = t;
4670 t = build_fold_addr_expr (first);
4671 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4672 ptrdiff_type_node, t);
4673 tree ptr = OMP_CLAUSE_DECL (c2);
4674 ptr = convert_from_reference (ptr);
4675 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4676 ptr = build_fold_addr_expr (ptr);
4677 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4678 ptrdiff_type_node, t,
4679 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4680 ptrdiff_type_node, ptr));
4681 OMP_CLAUSE_SIZE (c2) = t;
4682 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4683 OMP_CLAUSE_CHAIN (c) = c2;
4684 ptr = OMP_CLAUSE_DECL (c2);
4685 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4686 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4688 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4689 OMP_CLAUSE_MAP);
4690 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4691 OMP_CLAUSE_DECL (c3) = ptr;
4692 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4693 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4694 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4695 OMP_CLAUSE_CHAIN (c2) = c3;
4699 return false;
4702 /* Return identifier to look up for omp declare reduction. */
4704 tree
4705 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4707 const char *p = NULL;
4708 const char *m = NULL;
4709 switch (reduction_code)
4711 case PLUS_EXPR:
4712 case MULT_EXPR:
4713 case MINUS_EXPR:
4714 case BIT_AND_EXPR:
4715 case BIT_XOR_EXPR:
4716 case BIT_IOR_EXPR:
4717 case TRUTH_ANDIF_EXPR:
4718 case TRUTH_ORIF_EXPR:
4719 reduction_id = ansi_opname (reduction_code);
4720 break;
4721 case MIN_EXPR:
4722 p = "min";
4723 break;
4724 case MAX_EXPR:
4725 p = "max";
4726 break;
4727 default:
4728 break;
4731 if (p == NULL)
4733 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4734 return error_mark_node;
4735 p = IDENTIFIER_POINTER (reduction_id);
4738 if (type != NULL_TREE)
4739 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4741 const char prefix[] = "omp declare reduction ";
4742 size_t lenp = sizeof (prefix);
4743 if (strncmp (p, prefix, lenp - 1) == 0)
4744 lenp = 1;
4745 size_t len = strlen (p);
4746 size_t lenm = m ? strlen (m) + 1 : 0;
4747 char *name = XALLOCAVEC (char, lenp + len + lenm);
4748 if (lenp > 1)
4749 memcpy (name, prefix, lenp - 1);
4750 memcpy (name + lenp - 1, p, len + 1);
4751 if (m)
4753 name[lenp + len - 1] = '~';
4754 memcpy (name + lenp + len, m, lenm);
4756 return get_identifier (name);
4759 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4760 FUNCTION_DECL or NULL_TREE if not found. */
4762 static tree
4763 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4764 vec<tree> *ambiguousp)
4766 tree orig_id = id;
4767 tree baselink = NULL_TREE;
4768 if (identifier_p (id))
4770 cp_id_kind idk;
4771 bool nonint_cst_expression_p;
4772 const char *error_msg;
4773 id = omp_reduction_id (ERROR_MARK, id, type);
4774 tree decl = lookup_name (id);
4775 if (decl == NULL_TREE)
4776 decl = error_mark_node;
4777 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4778 &nonint_cst_expression_p, false, true, false,
4779 false, &error_msg, loc);
4780 if (idk == CP_ID_KIND_UNQUALIFIED
4781 && identifier_p (id))
4783 vec<tree, va_gc> *args = NULL;
4784 vec_safe_push (args, build_reference_type (type));
4785 id = perform_koenig_lookup (id, args, tf_none);
4788 else if (TREE_CODE (id) == SCOPE_REF)
4789 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4790 omp_reduction_id (ERROR_MARK,
4791 TREE_OPERAND (id, 1),
4792 type),
4793 false, false);
4794 tree fns = id;
4795 if (id && is_overloaded_fn (id))
4796 id = get_fns (id);
4797 for (; id; id = OVL_NEXT (id))
4799 tree fndecl = OVL_CURRENT (id);
4800 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4802 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4803 if (same_type_p (TREE_TYPE (argtype), type))
4804 break;
4807 if (id && BASELINK_P (fns))
4809 if (baselinkp)
4810 *baselinkp = fns;
4811 else
4812 baselink = fns;
4814 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4816 vec<tree> ambiguous = vNULL;
4817 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4818 unsigned int ix;
4819 if (ambiguousp == NULL)
4820 ambiguousp = &ambiguous;
4821 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4823 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4824 baselinkp ? baselinkp : &baselink,
4825 ambiguousp);
4826 if (id == NULL_TREE)
4827 continue;
4828 if (!ambiguousp->is_empty ())
4829 ambiguousp->safe_push (id);
4830 else if (ret != NULL_TREE)
4832 ambiguousp->safe_push (ret);
4833 ambiguousp->safe_push (id);
4834 ret = NULL_TREE;
4836 else
4837 ret = id;
4839 if (ambiguousp != &ambiguous)
4840 return ret;
4841 if (!ambiguous.is_empty ())
4843 const char *str = _("candidates are:");
4844 unsigned int idx;
4845 tree udr;
4846 error_at (loc, "user defined reduction lookup is ambiguous");
4847 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4849 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4850 if (idx == 0)
4851 str = get_spaces (str);
4853 ambiguous.release ();
4854 ret = error_mark_node;
4855 baselink = NULL_TREE;
4857 id = ret;
4859 if (id && baselink)
4860 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4861 id, id, tf_warning_or_error);
4862 return id;
4865 /* Helper function for cp_parser_omp_declare_reduction_exprs
4866 and tsubst_omp_udr.
4867 Remove CLEANUP_STMT for data (omp_priv variable).
4868 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4869 DECL_EXPR. */
4871 tree
4872 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4874 if (TYPE_P (*tp))
4875 *walk_subtrees = 0;
4876 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4877 *tp = CLEANUP_BODY (*tp);
4878 else if (TREE_CODE (*tp) == DECL_EXPR)
4880 tree decl = DECL_EXPR_DECL (*tp);
4881 if (!processing_template_decl
4882 && decl == (tree) data
4883 && DECL_INITIAL (decl)
4884 && DECL_INITIAL (decl) != error_mark_node)
4886 tree list = NULL_TREE;
4887 append_to_statement_list_force (*tp, &list);
4888 tree init_expr = build2 (INIT_EXPR, void_type_node,
4889 decl, DECL_INITIAL (decl));
4890 DECL_INITIAL (decl) = NULL_TREE;
4891 append_to_statement_list_force (init_expr, &list);
4892 *tp = list;
4895 return NULL_TREE;
4898 /* Data passed from cp_check_omp_declare_reduction to
4899 cp_check_omp_declare_reduction_r. */
4901 struct cp_check_omp_declare_reduction_data
4903 location_t loc;
4904 tree stmts[7];
4905 bool combiner_p;
4908 /* Helper function for cp_check_omp_declare_reduction, called via
4909 cp_walk_tree. */
4911 static tree
4912 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4914 struct cp_check_omp_declare_reduction_data *udr_data
4915 = (struct cp_check_omp_declare_reduction_data *) data;
4916 if (SSA_VAR_P (*tp)
4917 && !DECL_ARTIFICIAL (*tp)
4918 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4919 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4921 location_t loc = udr_data->loc;
4922 if (udr_data->combiner_p)
4923 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4924 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4925 *tp);
4926 else
4927 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4928 "to variable %qD which is not %<omp_priv%> nor "
4929 "%<omp_orig%>",
4930 *tp);
4931 return *tp;
4933 return NULL_TREE;
4936 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4938 void
4939 cp_check_omp_declare_reduction (tree udr)
4941 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4942 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4943 type = TREE_TYPE (type);
4944 int i;
4945 location_t loc = DECL_SOURCE_LOCATION (udr);
4947 if (type == error_mark_node)
4948 return;
4949 if (ARITHMETIC_TYPE_P (type))
4951 static enum tree_code predef_codes[]
4952 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4953 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4954 for (i = 0; i < 8; i++)
4956 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4957 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4958 const char *n2 = IDENTIFIER_POINTER (id);
4959 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4960 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4961 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4962 break;
4965 if (i == 8
4966 && TREE_CODE (type) != COMPLEX_EXPR)
4968 const char prefix_minmax[] = "omp declare reduction m";
4969 size_t prefix_size = sizeof (prefix_minmax) - 1;
4970 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4971 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4972 prefix_minmax, prefix_size) == 0
4973 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4974 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4975 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4976 i = 0;
4978 if (i < 8)
4980 error_at (loc, "predeclared arithmetic type %qT in "
4981 "%<#pragma omp declare reduction%>", type);
4982 return;
4985 else if (TREE_CODE (type) == FUNCTION_TYPE
4986 || TREE_CODE (type) == METHOD_TYPE
4987 || TREE_CODE (type) == ARRAY_TYPE)
4989 error_at (loc, "function or array type %qT in "
4990 "%<#pragma omp declare reduction%>", type);
4991 return;
4993 else if (TREE_CODE (type) == REFERENCE_TYPE)
4995 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4996 type);
4997 return;
4999 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5001 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5002 "%<#pragma omp declare reduction%>", type);
5003 return;
5006 tree body = DECL_SAVED_TREE (udr);
5007 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5008 return;
5010 tree_stmt_iterator tsi;
5011 struct cp_check_omp_declare_reduction_data data;
5012 memset (data.stmts, 0, sizeof data.stmts);
5013 for (i = 0, tsi = tsi_start (body);
5014 i < 7 && !tsi_end_p (tsi);
5015 i++, tsi_next (&tsi))
5016 data.stmts[i] = tsi_stmt (tsi);
5017 data.loc = loc;
5018 gcc_assert (tsi_end_p (tsi));
5019 if (i >= 3)
5021 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5022 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5023 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5024 return;
5025 data.combiner_p = true;
5026 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5027 &data, NULL))
5028 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5030 if (i >= 6)
5032 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5033 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5034 data.combiner_p = false;
5035 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5036 &data, NULL)
5037 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5038 cp_check_omp_declare_reduction_r, &data, NULL))
5039 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5040 if (i == 7)
5041 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5045 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5046 an inline call. But, remap
5047 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5048 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5050 static tree
5051 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5052 tree decl, tree placeholder)
5054 copy_body_data id;
5055 hash_map<tree, tree> decl_map;
5057 decl_map.put (omp_decl1, placeholder);
5058 decl_map.put (omp_decl2, decl);
5059 memset (&id, 0, sizeof (id));
5060 id.src_fn = DECL_CONTEXT (omp_decl1);
5061 id.dst_fn = current_function_decl;
5062 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5063 id.decl_map = &decl_map;
5065 id.copy_decl = copy_decl_no_change;
5066 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5067 id.transform_new_cfg = true;
5068 id.transform_return_to_modify = false;
5069 id.transform_lang_insert_block = NULL;
5070 id.eh_lp_nr = 0;
5071 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5072 return stmt;
5075 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5076 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5078 static tree
5079 find_omp_placeholder_r (tree *tp, int *, void *data)
5081 if (*tp == (tree) data)
5082 return *tp;
5083 return NULL_TREE;
5086 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5087 Return true if there is some error and the clause should be removed. */
5089 static bool
5090 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5092 tree t = OMP_CLAUSE_DECL (c);
5093 bool predefined = false;
5094 tree type = TREE_TYPE (t);
5095 if (TREE_CODE (type) == REFERENCE_TYPE)
5096 type = TREE_TYPE (type);
5097 if (type == error_mark_node)
5098 return true;
5099 else if (ARITHMETIC_TYPE_P (type))
5100 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5102 case PLUS_EXPR:
5103 case MULT_EXPR:
5104 case MINUS_EXPR:
5105 predefined = true;
5106 break;
5107 case MIN_EXPR:
5108 case MAX_EXPR:
5109 if (TREE_CODE (type) == COMPLEX_TYPE)
5110 break;
5111 predefined = true;
5112 break;
5113 case BIT_AND_EXPR:
5114 case BIT_IOR_EXPR:
5115 case BIT_XOR_EXPR:
5116 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5117 break;
5118 predefined = true;
5119 break;
5120 case TRUTH_ANDIF_EXPR:
5121 case TRUTH_ORIF_EXPR:
5122 if (FLOAT_TYPE_P (type))
5123 break;
5124 predefined = true;
5125 break;
5126 default:
5127 break;
5129 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5131 error ("%qE has invalid type for %<reduction%>", t);
5132 return true;
5134 else if (!processing_template_decl)
5136 t = require_complete_type (t);
5137 if (t == error_mark_node)
5138 return true;
5139 OMP_CLAUSE_DECL (c) = t;
5142 if (predefined)
5144 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5145 return false;
5147 else if (processing_template_decl)
5148 return false;
5150 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5152 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5153 if (TREE_CODE (type) == REFERENCE_TYPE)
5154 type = TREE_TYPE (type);
5155 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5156 if (id == NULL_TREE)
5157 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5158 NULL_TREE, NULL_TREE);
5159 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5160 if (id)
5162 if (id == error_mark_node)
5163 return true;
5164 id = OVL_CURRENT (id);
5165 mark_used (id);
5166 tree body = DECL_SAVED_TREE (id);
5167 if (!body)
5168 return true;
5169 if (TREE_CODE (body) == STATEMENT_LIST)
5171 tree_stmt_iterator tsi;
5172 tree placeholder = NULL_TREE;
5173 int i;
5174 tree stmts[7];
5175 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5176 atype = TREE_TYPE (atype);
5177 bool need_static_cast = !same_type_p (type, atype);
5178 memset (stmts, 0, sizeof stmts);
5179 for (i = 0, tsi = tsi_start (body);
5180 i < 7 && !tsi_end_p (tsi);
5181 i++, tsi_next (&tsi))
5182 stmts[i] = tsi_stmt (tsi);
5183 gcc_assert (tsi_end_p (tsi));
5185 if (i >= 3)
5187 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5188 && TREE_CODE (stmts[1]) == DECL_EXPR);
5189 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5190 DECL_ARTIFICIAL (placeholder) = 1;
5191 DECL_IGNORED_P (placeholder) = 1;
5192 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5193 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5194 cxx_mark_addressable (placeholder);
5195 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5196 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5197 != REFERENCE_TYPE)
5198 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5199 tree omp_out = placeholder;
5200 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5201 if (need_static_cast)
5203 tree rtype = build_reference_type (atype);
5204 omp_out = build_static_cast (rtype, omp_out,
5205 tf_warning_or_error);
5206 omp_in = build_static_cast (rtype, omp_in,
5207 tf_warning_or_error);
5208 if (omp_out == error_mark_node || omp_in == error_mark_node)
5209 return true;
5210 omp_out = convert_from_reference (omp_out);
5211 omp_in = convert_from_reference (omp_in);
5213 OMP_CLAUSE_REDUCTION_MERGE (c)
5214 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5215 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5217 if (i >= 6)
5219 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5220 && TREE_CODE (stmts[4]) == DECL_EXPR);
5221 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5222 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5223 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5224 cxx_mark_addressable (placeholder);
5225 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5226 tree omp_orig = placeholder;
5227 if (need_static_cast)
5229 if (i == 7)
5231 error_at (OMP_CLAUSE_LOCATION (c),
5232 "user defined reduction with constructor "
5233 "initializer for base class %qT", atype);
5234 return true;
5236 tree rtype = build_reference_type (atype);
5237 omp_priv = build_static_cast (rtype, omp_priv,
5238 tf_warning_or_error);
5239 omp_orig = build_static_cast (rtype, omp_orig,
5240 tf_warning_or_error);
5241 if (omp_priv == error_mark_node
5242 || omp_orig == error_mark_node)
5243 return true;
5244 omp_priv = convert_from_reference (omp_priv);
5245 omp_orig = convert_from_reference (omp_orig);
5247 if (i == 6)
5248 *need_default_ctor = true;
5249 OMP_CLAUSE_REDUCTION_INIT (c)
5250 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5251 DECL_EXPR_DECL (stmts[3]),
5252 omp_priv, omp_orig);
5253 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5254 find_omp_placeholder_r, placeholder, NULL))
5255 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5257 else if (i >= 3)
5259 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5260 *need_default_ctor = true;
5261 else
5263 tree init;
5264 tree v = convert_from_reference (t);
5265 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5266 init = build_constructor (TREE_TYPE (v), NULL);
5267 else
5268 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5269 OMP_CLAUSE_REDUCTION_INIT (c)
5270 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5275 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5276 *need_dtor = true;
5277 else
5279 error ("user defined reduction not found for %qD", t);
5280 return true;
5282 return false;
5285 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5286 Remove any elements from the list that are invalid. */
5288 tree
5289 finish_omp_clauses (tree clauses)
5291 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5292 bitmap_head aligned_head;
5293 tree c, t, *pc;
5294 bool branch_seen = false;
5295 bool copyprivate_seen = false;
5297 bitmap_obstack_initialize (NULL);
5298 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5299 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5300 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5301 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5303 for (pc = &clauses, c = clauses; c ; c = *pc)
5305 bool remove = false;
5307 switch (OMP_CLAUSE_CODE (c))
5309 case OMP_CLAUSE_SHARED:
5310 goto check_dup_generic;
5311 case OMP_CLAUSE_PRIVATE:
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_REDUCTION:
5314 goto check_dup_generic;
5315 case OMP_CLAUSE_COPYPRIVATE:
5316 copyprivate_seen = true;
5317 goto check_dup_generic;
5318 case OMP_CLAUSE_COPYIN:
5319 goto check_dup_generic;
5320 case OMP_CLAUSE_LINEAR:
5321 t = OMP_CLAUSE_DECL (c);
5322 if (!type_dependent_expression_p (t)
5323 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5324 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5326 error ("linear clause applied to non-integral non-pointer "
5327 "variable with %qT type", TREE_TYPE (t));
5328 remove = true;
5329 break;
5331 t = OMP_CLAUSE_LINEAR_STEP (c);
5332 if (t == NULL_TREE)
5333 t = integer_one_node;
5334 if (t == error_mark_node)
5336 remove = true;
5337 break;
5339 else if (!type_dependent_expression_p (t)
5340 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5342 error ("linear step expression must be integral");
5343 remove = true;
5344 break;
5346 else
5348 t = mark_rvalue_use (t);
5349 if (!processing_template_decl)
5351 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5352 t = maybe_constant_value (t);
5353 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5354 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5355 == POINTER_TYPE)
5357 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5358 OMP_CLAUSE_DECL (c), t);
5359 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5360 MINUS_EXPR, sizetype, t,
5361 OMP_CLAUSE_DECL (c));
5362 if (t == error_mark_node)
5364 remove = true;
5365 break;
5368 else
5369 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5371 OMP_CLAUSE_LINEAR_STEP (c) = t;
5373 goto check_dup_generic;
5374 check_dup_generic:
5375 t = OMP_CLAUSE_DECL (c);
5376 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5378 if (processing_template_decl)
5379 break;
5380 if (DECL_P (t))
5381 error ("%qD is not a variable in clause %qs", t,
5382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5383 else
5384 error ("%qE is not a variable in clause %qs", t,
5385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5386 remove = true;
5388 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5389 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5390 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5392 error ("%qD appears more than once in data clauses", t);
5393 remove = true;
5395 else
5396 bitmap_set_bit (&generic_head, DECL_UID (t));
5397 break;
5399 case OMP_CLAUSE_FIRSTPRIVATE:
5400 t = OMP_CLAUSE_DECL (c);
5401 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5403 if (processing_template_decl)
5404 break;
5405 if (DECL_P (t))
5406 error ("%qD is not a variable in clause %<firstprivate%>", t);
5407 else
5408 error ("%qE is not a variable in clause %<firstprivate%>", t);
5409 remove = true;
5411 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5412 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5414 error ("%qD appears more than once in data clauses", t);
5415 remove = true;
5417 else
5418 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5419 break;
5421 case OMP_CLAUSE_LASTPRIVATE:
5422 t = OMP_CLAUSE_DECL (c);
5423 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5425 if (processing_template_decl)
5426 break;
5427 if (DECL_P (t))
5428 error ("%qD is not a variable in clause %<lastprivate%>", t);
5429 else
5430 error ("%qE is not a variable in clause %<lastprivate%>", t);
5431 remove = true;
5433 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5434 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5436 error ("%qD appears more than once in data clauses", t);
5437 remove = true;
5439 else
5440 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5441 break;
5443 case OMP_CLAUSE_IF:
5444 t = OMP_CLAUSE_IF_EXPR (c);
5445 t = maybe_convert_cond (t);
5446 if (t == error_mark_node)
5447 remove = true;
5448 else if (!processing_template_decl)
5449 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5450 OMP_CLAUSE_IF_EXPR (c) = t;
5451 break;
5453 case OMP_CLAUSE_FINAL:
5454 t = OMP_CLAUSE_FINAL_EXPR (c);
5455 t = maybe_convert_cond (t);
5456 if (t == error_mark_node)
5457 remove = true;
5458 else if (!processing_template_decl)
5459 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5460 OMP_CLAUSE_FINAL_EXPR (c) = t;
5461 break;
5463 case OMP_CLAUSE_NUM_THREADS:
5464 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5465 if (t == error_mark_node)
5466 remove = true;
5467 else if (!type_dependent_expression_p (t)
5468 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5470 error ("num_threads expression must be integral");
5471 remove = true;
5473 else
5475 t = mark_rvalue_use (t);
5476 if (!processing_template_decl)
5477 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5478 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5480 break;
5482 case OMP_CLAUSE_SCHEDULE:
5483 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5484 if (t == NULL)
5486 else if (t == error_mark_node)
5487 remove = true;
5488 else if (!type_dependent_expression_p (t)
5489 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5490 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5491 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5493 error ("schedule chunk size expression must be integral");
5494 remove = true;
5496 else
5498 t = mark_rvalue_use (t);
5499 if (!processing_template_decl)
5501 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5502 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5504 t = convert_to_integer (long_integer_type_node, t);
5505 if (t == error_mark_node)
5507 remove = true;
5508 break;
5511 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5513 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5515 break;
5517 case OMP_CLAUSE_SIMDLEN:
5518 case OMP_CLAUSE_SAFELEN:
5519 t = OMP_CLAUSE_OPERAND (c, 0);
5520 if (t == error_mark_node)
5521 remove = true;
5522 else if (!type_dependent_expression_p (t)
5523 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5525 error ("%qs length expression must be integral",
5526 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5527 remove = true;
5529 else
5531 t = mark_rvalue_use (t);
5532 t = maybe_constant_value (t);
5533 if (!processing_template_decl)
5535 if (TREE_CODE (t) != INTEGER_CST
5536 || tree_int_cst_sgn (t) != 1)
5538 error ("%qs length expression must be positive constant"
5539 " integer expression",
5540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5541 remove = true;
5544 OMP_CLAUSE_OPERAND (c, 0) = t;
5546 break;
5548 case OMP_CLAUSE_NUM_TEAMS:
5549 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5550 if (t == error_mark_node)
5551 remove = true;
5552 else if (!type_dependent_expression_p (t)
5553 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5555 error ("%<num_teams%> expression must be integral");
5556 remove = true;
5558 else
5560 t = mark_rvalue_use (t);
5561 if (!processing_template_decl)
5562 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5563 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5565 break;
5567 case OMP_CLAUSE_ASYNC:
5568 t = OMP_CLAUSE_ASYNC_EXPR (c);
5569 if (t == error_mark_node)
5570 remove = true;
5571 else if (!type_dependent_expression_p (t)
5572 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5574 error ("%<async%> expression must be integral");
5575 remove = true;
5577 else
5579 t = mark_rvalue_use (t);
5580 if (!processing_template_decl)
5581 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5582 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5584 break;
5586 case OMP_CLAUSE_VECTOR_LENGTH:
5587 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5588 t = maybe_convert_cond (t);
5589 if (t == error_mark_node)
5590 remove = true;
5591 else if (!processing_template_decl)
5592 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5593 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5594 break;
5596 case OMP_CLAUSE_WAIT:
5597 t = OMP_CLAUSE_WAIT_EXPR (c);
5598 if (t == error_mark_node)
5599 remove = true;
5600 else if (!processing_template_decl)
5601 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5602 OMP_CLAUSE_WAIT_EXPR (c) = t;
5603 break;
5605 case OMP_CLAUSE_THREAD_LIMIT:
5606 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5607 if (t == error_mark_node)
5608 remove = true;
5609 else if (!type_dependent_expression_p (t)
5610 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5612 error ("%<thread_limit%> expression must be integral");
5613 remove = true;
5615 else
5617 t = mark_rvalue_use (t);
5618 if (!processing_template_decl)
5619 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5620 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5622 break;
5624 case OMP_CLAUSE_DEVICE:
5625 t = OMP_CLAUSE_DEVICE_ID (c);
5626 if (t == error_mark_node)
5627 remove = true;
5628 else if (!type_dependent_expression_p (t)
5629 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5631 error ("%<device%> id must be integral");
5632 remove = true;
5634 else
5636 t = mark_rvalue_use (t);
5637 if (!processing_template_decl)
5638 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5639 OMP_CLAUSE_DEVICE_ID (c) = t;
5641 break;
5643 case OMP_CLAUSE_DIST_SCHEDULE:
5644 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5645 if (t == NULL)
5647 else if (t == error_mark_node)
5648 remove = true;
5649 else if (!type_dependent_expression_p (t)
5650 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5652 error ("%<dist_schedule%> chunk size expression must be "
5653 "integral");
5654 remove = true;
5656 else
5658 t = mark_rvalue_use (t);
5659 if (!processing_template_decl)
5660 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5661 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5663 break;
5665 case OMP_CLAUSE_ALIGNED:
5666 t = OMP_CLAUSE_DECL (c);
5667 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5669 if (processing_template_decl)
5670 break;
5671 if (DECL_P (t))
5672 error ("%qD is not a variable in %<aligned%> clause", t);
5673 else
5674 error ("%qE is not a variable in %<aligned%> clause", t);
5675 remove = true;
5677 else if (!type_dependent_expression_p (t)
5678 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5679 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5680 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5681 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5682 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5683 != ARRAY_TYPE))))
5685 error_at (OMP_CLAUSE_LOCATION (c),
5686 "%qE in %<aligned%> clause is neither a pointer nor "
5687 "an array nor a reference to pointer or array", t);
5688 remove = true;
5690 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5692 error ("%qD appears more than once in %<aligned%> clauses", t);
5693 remove = true;
5695 else
5696 bitmap_set_bit (&aligned_head, DECL_UID (t));
5697 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5698 if (t == error_mark_node)
5699 remove = true;
5700 else if (t == NULL_TREE)
5701 break;
5702 else if (!type_dependent_expression_p (t)
5703 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5705 error ("%<aligned%> clause alignment expression must "
5706 "be integral");
5707 remove = true;
5709 else
5711 t = mark_rvalue_use (t);
5712 t = maybe_constant_value (t);
5713 if (!processing_template_decl)
5715 if (TREE_CODE (t) != INTEGER_CST
5716 || tree_int_cst_sgn (t) != 1)
5718 error ("%<aligned%> clause alignment expression must be "
5719 "positive constant integer expression");
5720 remove = true;
5723 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5725 break;
5727 case OMP_CLAUSE_DEPEND:
5728 t = OMP_CLAUSE_DECL (c);
5729 if (TREE_CODE (t) == TREE_LIST)
5731 if (handle_omp_array_sections (c))
5732 remove = true;
5733 break;
5735 if (t == error_mark_node)
5736 remove = true;
5737 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5739 if (processing_template_decl)
5740 break;
5741 if (DECL_P (t))
5742 error ("%qD is not a variable in %<depend%> clause", t);
5743 else
5744 error ("%qE is not a variable in %<depend%> clause", t);
5745 remove = true;
5747 else if (!processing_template_decl
5748 && !cxx_mark_addressable (t))
5749 remove = true;
5750 break;
5752 case OMP_CLAUSE_MAP:
5753 case OMP_CLAUSE_TO:
5754 case OMP_CLAUSE_FROM:
5755 case OMP_CLAUSE__CACHE_:
5756 t = OMP_CLAUSE_DECL (c);
5757 if (TREE_CODE (t) == TREE_LIST)
5759 if (handle_omp_array_sections (c))
5760 remove = true;
5761 else
5763 t = OMP_CLAUSE_DECL (c);
5764 if (TREE_CODE (t) != TREE_LIST
5765 && !type_dependent_expression_p (t)
5766 && !cp_omp_mappable_type (TREE_TYPE (t)))
5768 error_at (OMP_CLAUSE_LOCATION (c),
5769 "array section does not have mappable type "
5770 "in %qs clause",
5771 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5772 remove = true;
5775 break;
5777 if (t == error_mark_node)
5778 remove = true;
5779 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5781 if (processing_template_decl)
5782 break;
5783 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5784 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5785 break;
5786 if (DECL_P (t))
5787 error ("%qD is not a variable in %qs clause", t,
5788 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5789 else
5790 error ("%qE is not a variable in %qs clause", t,
5791 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5792 remove = true;
5794 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5796 error ("%qD is threadprivate variable in %qs clause", t,
5797 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5798 remove = true;
5800 else if (!processing_template_decl
5801 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5802 && !cxx_mark_addressable (t))
5803 remove = true;
5804 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5805 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5806 && !type_dependent_expression_p (t)
5807 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5808 == REFERENCE_TYPE)
5809 ? TREE_TYPE (TREE_TYPE (t))
5810 : TREE_TYPE (t)))
5812 error_at (OMP_CLAUSE_LOCATION (c),
5813 "%qD does not have a mappable type in %qs clause", t,
5814 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5815 remove = true;
5817 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5819 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5820 error ("%qD appears more than once in motion clauses", t);
5821 else
5822 error ("%qD appears more than once in map clauses", t);
5823 remove = true;
5825 else
5826 bitmap_set_bit (&generic_head, DECL_UID (t));
5827 break;
5829 case OMP_CLAUSE_UNIFORM:
5830 t = OMP_CLAUSE_DECL (c);
5831 if (TREE_CODE (t) != PARM_DECL)
5833 if (processing_template_decl)
5834 break;
5835 if (DECL_P (t))
5836 error ("%qD is not an argument in %<uniform%> clause", t);
5837 else
5838 error ("%qE is not an argument in %<uniform%> clause", t);
5839 remove = true;
5840 break;
5842 goto check_dup_generic;
5844 case OMP_CLAUSE_NOWAIT:
5845 case OMP_CLAUSE_ORDERED:
5846 case OMP_CLAUSE_DEFAULT:
5847 case OMP_CLAUSE_UNTIED:
5848 case OMP_CLAUSE_COLLAPSE:
5849 case OMP_CLAUSE_MERGEABLE:
5850 case OMP_CLAUSE_PARALLEL:
5851 case OMP_CLAUSE_FOR:
5852 case OMP_CLAUSE_SECTIONS:
5853 case OMP_CLAUSE_TASKGROUP:
5854 case OMP_CLAUSE_PROC_BIND:
5855 case OMP_CLAUSE__CILK_FOR_COUNT_:
5856 break;
5858 case OMP_CLAUSE_INBRANCH:
5859 case OMP_CLAUSE_NOTINBRANCH:
5860 if (branch_seen)
5862 error ("%<inbranch%> clause is incompatible with "
5863 "%<notinbranch%>");
5864 remove = true;
5866 branch_seen = true;
5867 break;
5869 default:
5870 gcc_unreachable ();
5873 if (remove)
5874 *pc = OMP_CLAUSE_CHAIN (c);
5875 else
5876 pc = &OMP_CLAUSE_CHAIN (c);
5879 for (pc = &clauses, c = clauses; c ; c = *pc)
5881 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5882 bool remove = false;
5883 bool need_complete_non_reference = false;
5884 bool need_default_ctor = false;
5885 bool need_copy_ctor = false;
5886 bool need_copy_assignment = false;
5887 bool need_implicitly_determined = false;
5888 bool need_dtor = false;
5889 tree type, inner_type;
5891 switch (c_kind)
5893 case OMP_CLAUSE_SHARED:
5894 need_implicitly_determined = true;
5895 break;
5896 case OMP_CLAUSE_PRIVATE:
5897 need_complete_non_reference = true;
5898 need_default_ctor = true;
5899 need_dtor = true;
5900 need_implicitly_determined = true;
5901 break;
5902 case OMP_CLAUSE_FIRSTPRIVATE:
5903 need_complete_non_reference = true;
5904 need_copy_ctor = true;
5905 need_dtor = true;
5906 need_implicitly_determined = true;
5907 break;
5908 case OMP_CLAUSE_LASTPRIVATE:
5909 need_complete_non_reference = true;
5910 need_copy_assignment = true;
5911 need_implicitly_determined = true;
5912 break;
5913 case OMP_CLAUSE_REDUCTION:
5914 need_implicitly_determined = true;
5915 break;
5916 case OMP_CLAUSE_COPYPRIVATE:
5917 need_copy_assignment = true;
5918 break;
5919 case OMP_CLAUSE_COPYIN:
5920 need_copy_assignment = true;
5921 break;
5922 case OMP_CLAUSE_NOWAIT:
5923 if (copyprivate_seen)
5925 error_at (OMP_CLAUSE_LOCATION (c),
5926 "%<nowait%> clause must not be used together "
5927 "with %<copyprivate%>");
5928 *pc = OMP_CLAUSE_CHAIN (c);
5929 continue;
5931 /* FALLTHRU */
5932 default:
5933 pc = &OMP_CLAUSE_CHAIN (c);
5934 continue;
5937 t = OMP_CLAUSE_DECL (c);
5938 if (processing_template_decl
5939 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5941 pc = &OMP_CLAUSE_CHAIN (c);
5942 continue;
5945 switch (c_kind)
5947 case OMP_CLAUSE_LASTPRIVATE:
5948 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5950 need_default_ctor = true;
5951 need_dtor = true;
5953 break;
5955 case OMP_CLAUSE_REDUCTION:
5956 if (finish_omp_reduction_clause (c, &need_default_ctor,
5957 &need_dtor))
5958 remove = true;
5959 else
5960 t = OMP_CLAUSE_DECL (c);
5961 break;
5963 case OMP_CLAUSE_COPYIN:
5964 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5966 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5967 remove = true;
5969 break;
5971 default:
5972 break;
5975 if (need_complete_non_reference || need_copy_assignment)
5977 t = require_complete_type (t);
5978 if (t == error_mark_node)
5979 remove = true;
5980 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5981 && need_complete_non_reference)
5983 error ("%qE has reference type for %qs", t,
5984 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5985 remove = true;
5988 if (need_implicitly_determined)
5990 const char *share_name = NULL;
5992 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5993 share_name = "threadprivate";
5994 else switch (cxx_omp_predetermined_sharing (t))
5996 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5997 break;
5998 case OMP_CLAUSE_DEFAULT_SHARED:
5999 /* const vars may be specified in firstprivate clause. */
6000 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6001 && cxx_omp_const_qual_no_mutable (t))
6002 break;
6003 share_name = "shared";
6004 break;
6005 case OMP_CLAUSE_DEFAULT_PRIVATE:
6006 share_name = "private";
6007 break;
6008 default:
6009 gcc_unreachable ();
6011 if (share_name)
6013 error ("%qE is predetermined %qs for %qs",
6014 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6015 remove = true;
6019 /* We're interested in the base element, not arrays. */
6020 inner_type = type = TREE_TYPE (t);
6021 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6022 inner_type = TREE_TYPE (inner_type);
6024 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6025 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6026 inner_type = TREE_TYPE (inner_type);
6028 /* Check for special function availability by building a call to one.
6029 Save the results, because later we won't be in the right context
6030 for making these queries. */
6031 if (CLASS_TYPE_P (inner_type)
6032 && COMPLETE_TYPE_P (inner_type)
6033 && (need_default_ctor || need_copy_ctor
6034 || need_copy_assignment || need_dtor)
6035 && !type_dependent_expression_p (t)
6036 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6037 need_copy_ctor, need_copy_assignment,
6038 need_dtor))
6039 remove = true;
6041 if (remove)
6042 *pc = OMP_CLAUSE_CHAIN (c);
6043 else
6044 pc = &OMP_CLAUSE_CHAIN (c);
6047 bitmap_obstack_release (NULL);
6048 return clauses;
6051 /* For all variables in the tree_list VARS, mark them as thread local. */
6053 void
6054 finish_omp_threadprivate (tree vars)
6056 tree t;
6058 /* Mark every variable in VARS to be assigned thread local storage. */
6059 for (t = vars; t; t = TREE_CHAIN (t))
6061 tree v = TREE_PURPOSE (t);
6063 if (error_operand_p (v))
6065 else if (!VAR_P (v))
6066 error ("%<threadprivate%> %qD is not file, namespace "
6067 "or block scope variable", v);
6068 /* If V had already been marked threadprivate, it doesn't matter
6069 whether it had been used prior to this point. */
6070 else if (TREE_USED (v)
6071 && (DECL_LANG_SPECIFIC (v) == NULL
6072 || !CP_DECL_THREADPRIVATE_P (v)))
6073 error ("%qE declared %<threadprivate%> after first use", v);
6074 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6075 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6076 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6077 error ("%<threadprivate%> %qE has incomplete type", v);
6078 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6079 && CP_DECL_CONTEXT (v) != current_class_type)
6080 error ("%<threadprivate%> %qE directive not "
6081 "in %qT definition", v, CP_DECL_CONTEXT (v));
6082 else
6084 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6085 if (DECL_LANG_SPECIFIC (v) == NULL)
6087 retrofit_lang_decl (v);
6089 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6090 after the allocation of the lang_decl structure. */
6091 if (DECL_DISCRIMINATOR_P (v))
6092 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6095 if (! DECL_THREAD_LOCAL_P (v))
6097 set_decl_tls_model (v, decl_default_tls_model (v));
6098 /* If rtl has been already set for this var, call
6099 make_decl_rtl once again, so that encode_section_info
6100 has a chance to look at the new decl flags. */
6101 if (DECL_RTL_SET_P (v))
6102 make_decl_rtl (v);
6104 CP_DECL_THREADPRIVATE_P (v) = 1;
6109 /* Build an OpenMP structured block. */
6111 tree
6112 begin_omp_structured_block (void)
6114 return do_pushlevel (sk_omp);
6117 tree
6118 finish_omp_structured_block (tree block)
6120 return do_poplevel (block);
6123 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6124 statement. LOC is the location of the OACC_DATA. */
6126 tree
6127 finish_oacc_data (tree clauses, tree block)
6129 tree stmt;
6131 block = finish_omp_structured_block (block);
6133 stmt = make_node (OACC_DATA);
6134 TREE_TYPE (stmt) = void_type_node;
6135 OACC_DATA_CLAUSES (stmt) = clauses;
6136 OACC_DATA_BODY (stmt) = block;
6138 return add_stmt (stmt);
6141 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6142 statement. LOC is the location of the OACC_KERNELS. */
6144 tree
6145 finish_oacc_kernels (tree clauses, tree block)
6147 tree stmt;
6149 block = finish_omp_structured_block (block);
6151 stmt = make_node (OACC_KERNELS);
6152 TREE_TYPE (stmt) = void_type_node;
6153 OACC_KERNELS_CLAUSES (stmt) = clauses;
6154 OACC_KERNELS_BODY (stmt) = block;
6156 return add_stmt (stmt);
6159 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6160 statement. LOC is the location of the OACC_PARALLEL. */
6162 tree
6163 finish_oacc_parallel (tree clauses, tree block)
6165 tree stmt;
6167 block = finish_omp_structured_block (block);
6169 stmt = make_node (OACC_PARALLEL);
6170 TREE_TYPE (stmt) = void_type_node;
6171 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6172 OACC_PARALLEL_BODY (stmt) = block;
6174 return add_stmt (stmt);
6177 /* Similarly, except force the retention of the BLOCK. */
6179 tree
6180 begin_omp_parallel (void)
6182 keep_next_level (true);
6183 return begin_omp_structured_block ();
6186 tree
6187 finish_omp_parallel (tree clauses, tree body)
6189 tree stmt;
6191 body = finish_omp_structured_block (body);
6193 stmt = make_node (OMP_PARALLEL);
6194 TREE_TYPE (stmt) = void_type_node;
6195 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6196 OMP_PARALLEL_BODY (stmt) = body;
6198 return add_stmt (stmt);
6201 tree
6202 begin_omp_task (void)
6204 keep_next_level (true);
6205 return begin_omp_structured_block ();
6208 tree
6209 finish_omp_task (tree clauses, tree body)
6211 tree stmt;
6213 body = finish_omp_structured_block (body);
6215 stmt = make_node (OMP_TASK);
6216 TREE_TYPE (stmt) = void_type_node;
6217 OMP_TASK_CLAUSES (stmt) = clauses;
6218 OMP_TASK_BODY (stmt) = body;
6220 return add_stmt (stmt);
6223 /* Helper function for finish_omp_for. Convert Ith random access iterator
6224 into integral iterator. Return FALSE if successful. */
6226 static bool
6227 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6228 tree condv, tree incrv, tree *body,
6229 tree *pre_body, tree clauses, tree *lastp)
6231 tree diff, iter_init, iter_incr = NULL, last;
6232 tree incr_var = NULL, orig_pre_body, orig_body, c;
6233 tree decl = TREE_VEC_ELT (declv, i);
6234 tree init = TREE_VEC_ELT (initv, i);
6235 tree cond = TREE_VEC_ELT (condv, i);
6236 tree incr = TREE_VEC_ELT (incrv, i);
6237 tree iter = decl;
6238 location_t elocus = locus;
6240 if (init && EXPR_HAS_LOCATION (init))
6241 elocus = EXPR_LOCATION (init);
6243 switch (TREE_CODE (cond))
6245 case GT_EXPR:
6246 case GE_EXPR:
6247 case LT_EXPR:
6248 case LE_EXPR:
6249 case NE_EXPR:
6250 if (TREE_OPERAND (cond, 1) == iter)
6251 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6252 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6253 if (TREE_OPERAND (cond, 0) != iter)
6254 cond = error_mark_node;
6255 else
6257 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6258 TREE_CODE (cond),
6259 iter, ERROR_MARK,
6260 TREE_OPERAND (cond, 1), ERROR_MARK,
6261 NULL, tf_warning_or_error);
6262 if (error_operand_p (tem))
6263 return true;
6265 break;
6266 default:
6267 cond = error_mark_node;
6268 break;
6270 if (cond == error_mark_node)
6272 error_at (elocus, "invalid controlling predicate");
6273 return true;
6275 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6276 ERROR_MARK, iter, ERROR_MARK, NULL,
6277 tf_warning_or_error);
6278 if (error_operand_p (diff))
6279 return true;
6280 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6282 error_at (elocus, "difference between %qE and %qD does not have integer type",
6283 TREE_OPERAND (cond, 1), iter);
6284 return true;
6287 switch (TREE_CODE (incr))
6289 case PREINCREMENT_EXPR:
6290 case PREDECREMENT_EXPR:
6291 case POSTINCREMENT_EXPR:
6292 case POSTDECREMENT_EXPR:
6293 if (TREE_OPERAND (incr, 0) != iter)
6295 incr = error_mark_node;
6296 break;
6298 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6299 TREE_CODE (incr), iter,
6300 tf_warning_or_error);
6301 if (error_operand_p (iter_incr))
6302 return true;
6303 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6304 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6305 incr = integer_one_node;
6306 else
6307 incr = integer_minus_one_node;
6308 break;
6309 case MODIFY_EXPR:
6310 if (TREE_OPERAND (incr, 0) != iter)
6311 incr = error_mark_node;
6312 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6313 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6315 tree rhs = TREE_OPERAND (incr, 1);
6316 if (TREE_OPERAND (rhs, 0) == iter)
6318 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6319 != INTEGER_TYPE)
6320 incr = error_mark_node;
6321 else
6323 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6324 iter, TREE_CODE (rhs),
6325 TREE_OPERAND (rhs, 1),
6326 tf_warning_or_error);
6327 if (error_operand_p (iter_incr))
6328 return true;
6329 incr = TREE_OPERAND (rhs, 1);
6330 incr = cp_convert (TREE_TYPE (diff), incr,
6331 tf_warning_or_error);
6332 if (TREE_CODE (rhs) == MINUS_EXPR)
6334 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6335 incr = fold_if_not_in_template (incr);
6337 if (TREE_CODE (incr) != INTEGER_CST
6338 && (TREE_CODE (incr) != NOP_EXPR
6339 || (TREE_CODE (TREE_OPERAND (incr, 0))
6340 != INTEGER_CST)))
6341 iter_incr = NULL;
6344 else if (TREE_OPERAND (rhs, 1) == iter)
6346 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6347 || TREE_CODE (rhs) != PLUS_EXPR)
6348 incr = error_mark_node;
6349 else
6351 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6352 PLUS_EXPR,
6353 TREE_OPERAND (rhs, 0),
6354 ERROR_MARK, iter,
6355 ERROR_MARK, NULL,
6356 tf_warning_or_error);
6357 if (error_operand_p (iter_incr))
6358 return true;
6359 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6360 iter, NOP_EXPR,
6361 iter_incr,
6362 tf_warning_or_error);
6363 if (error_operand_p (iter_incr))
6364 return true;
6365 incr = TREE_OPERAND (rhs, 0);
6366 iter_incr = NULL;
6369 else
6370 incr = error_mark_node;
6372 else
6373 incr = error_mark_node;
6374 break;
6375 default:
6376 incr = error_mark_node;
6377 break;
6380 if (incr == error_mark_node)
6382 error_at (elocus, "invalid increment expression");
6383 return true;
6386 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6387 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6389 && OMP_CLAUSE_DECL (c) == iter)
6390 break;
6392 decl = create_temporary_var (TREE_TYPE (diff));
6393 pushdecl (decl);
6394 add_decl_expr (decl);
6395 last = create_temporary_var (TREE_TYPE (diff));
6396 pushdecl (last);
6397 add_decl_expr (last);
6398 if (c && iter_incr == NULL)
6400 incr_var = create_temporary_var (TREE_TYPE (diff));
6401 pushdecl (incr_var);
6402 add_decl_expr (incr_var);
6404 gcc_assert (stmts_are_full_exprs_p ());
6406 orig_pre_body = *pre_body;
6407 *pre_body = push_stmt_list ();
6408 if (orig_pre_body)
6409 add_stmt (orig_pre_body);
6410 if (init != NULL)
6411 finish_expr_stmt (build_x_modify_expr (elocus,
6412 iter, NOP_EXPR, init,
6413 tf_warning_or_error));
6414 init = build_int_cst (TREE_TYPE (diff), 0);
6415 if (c && iter_incr == NULL)
6417 finish_expr_stmt (build_x_modify_expr (elocus,
6418 incr_var, NOP_EXPR,
6419 incr, tf_warning_or_error));
6420 incr = incr_var;
6421 iter_incr = build_x_modify_expr (elocus,
6422 iter, PLUS_EXPR, incr,
6423 tf_warning_or_error);
6425 finish_expr_stmt (build_x_modify_expr (elocus,
6426 last, NOP_EXPR, init,
6427 tf_warning_or_error));
6428 *pre_body = pop_stmt_list (*pre_body);
6430 cond = cp_build_binary_op (elocus,
6431 TREE_CODE (cond), decl, diff,
6432 tf_warning_or_error);
6433 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6434 elocus, incr, NULL_TREE);
6436 orig_body = *body;
6437 *body = push_stmt_list ();
6438 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6439 iter_init = build_x_modify_expr (elocus,
6440 iter, PLUS_EXPR, iter_init,
6441 tf_warning_or_error);
6442 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6443 finish_expr_stmt (iter_init);
6444 finish_expr_stmt (build_x_modify_expr (elocus,
6445 last, NOP_EXPR, decl,
6446 tf_warning_or_error));
6447 add_stmt (orig_body);
6448 *body = pop_stmt_list (*body);
6450 if (c)
6452 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6453 finish_expr_stmt (iter_incr);
6454 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6455 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6458 TREE_VEC_ELT (declv, i) = decl;
6459 TREE_VEC_ELT (initv, i) = init;
6460 TREE_VEC_ELT (condv, i) = cond;
6461 TREE_VEC_ELT (incrv, i) = incr;
6462 *lastp = last;
6464 return false;
6467 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6468 are directly for their associated operands in the statement. DECL
6469 and INIT are a combo; if DECL is NULL then INIT ought to be a
6470 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6471 optional statements that need to go before the loop into its
6472 sk_omp scope. */
6474 tree
6475 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6476 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6478 tree omp_for = NULL, orig_incr = NULL;
6479 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6480 tree last = NULL_TREE;
6481 location_t elocus;
6482 int i;
6484 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6485 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6486 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6487 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6489 decl = TREE_VEC_ELT (declv, i);
6490 init = TREE_VEC_ELT (initv, i);
6491 cond = TREE_VEC_ELT (condv, i);
6492 incr = TREE_VEC_ELT (incrv, i);
6493 elocus = locus;
6495 if (decl == NULL)
6497 if (init != NULL)
6498 switch (TREE_CODE (init))
6500 case MODIFY_EXPR:
6501 decl = TREE_OPERAND (init, 0);
6502 init = TREE_OPERAND (init, 1);
6503 break;
6504 case MODOP_EXPR:
6505 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6507 decl = TREE_OPERAND (init, 0);
6508 init = TREE_OPERAND (init, 2);
6510 break;
6511 default:
6512 break;
6515 if (decl == NULL)
6517 error_at (locus,
6518 "expected iteration declaration or initialization");
6519 return NULL;
6523 if (init && EXPR_HAS_LOCATION (init))
6524 elocus = EXPR_LOCATION (init);
6526 if (cond == NULL)
6528 error_at (elocus, "missing controlling predicate");
6529 return NULL;
6532 if (incr == NULL)
6534 error_at (elocus, "missing increment expression");
6535 return NULL;
6538 TREE_VEC_ELT (declv, i) = decl;
6539 TREE_VEC_ELT (initv, i) = init;
6542 if (dependent_omp_for_p (declv, initv, condv, incrv))
6544 tree stmt;
6546 stmt = make_node (code);
6548 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6550 /* This is really just a place-holder. We'll be decomposing this
6551 again and going through the cp_build_modify_expr path below when
6552 we instantiate the thing. */
6553 TREE_VEC_ELT (initv, i)
6554 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6555 TREE_VEC_ELT (initv, i));
6558 TREE_TYPE (stmt) = void_type_node;
6559 OMP_FOR_INIT (stmt) = initv;
6560 OMP_FOR_COND (stmt) = condv;
6561 OMP_FOR_INCR (stmt) = incrv;
6562 OMP_FOR_BODY (stmt) = body;
6563 OMP_FOR_PRE_BODY (stmt) = pre_body;
6564 OMP_FOR_CLAUSES (stmt) = clauses;
6566 SET_EXPR_LOCATION (stmt, locus);
6567 return add_stmt (stmt);
6570 if (processing_template_decl)
6571 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6573 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6575 decl = TREE_VEC_ELT (declv, i);
6576 init = TREE_VEC_ELT (initv, i);
6577 cond = TREE_VEC_ELT (condv, i);
6578 incr = TREE_VEC_ELT (incrv, i);
6579 if (orig_incr)
6580 TREE_VEC_ELT (orig_incr, i) = incr;
6581 elocus = locus;
6583 if (init && EXPR_HAS_LOCATION (init))
6584 elocus = EXPR_LOCATION (init);
6586 if (!DECL_P (decl))
6588 error_at (elocus, "expected iteration declaration or initialization");
6589 return NULL;
6592 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6594 if (orig_incr)
6595 TREE_VEC_ELT (orig_incr, i) = incr;
6596 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6597 TREE_CODE (TREE_OPERAND (incr, 1)),
6598 TREE_OPERAND (incr, 2),
6599 tf_warning_or_error);
6602 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6604 if (code == OMP_SIMD)
6606 error_at (elocus, "%<#pragma omp simd%> used with class "
6607 "iteration variable %qE", decl);
6608 return NULL;
6610 if (code == CILK_FOR && i == 0)
6611 orig_decl = decl;
6612 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6613 incrv, &body, &pre_body,
6614 clauses, &last))
6615 return NULL;
6616 continue;
6619 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6620 && !TYPE_PTR_P (TREE_TYPE (decl)))
6622 error_at (elocus, "invalid type for iteration variable %qE", decl);
6623 return NULL;
6626 if (!processing_template_decl)
6628 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6629 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6631 else
6632 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6633 if (cond
6634 && TREE_SIDE_EFFECTS (cond)
6635 && COMPARISON_CLASS_P (cond)
6636 && !processing_template_decl)
6638 tree t = TREE_OPERAND (cond, 0);
6639 if (TREE_SIDE_EFFECTS (t)
6640 && t != decl
6641 && (TREE_CODE (t) != NOP_EXPR
6642 || TREE_OPERAND (t, 0) != decl))
6643 TREE_OPERAND (cond, 0)
6644 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6646 t = TREE_OPERAND (cond, 1);
6647 if (TREE_SIDE_EFFECTS (t)
6648 && t != decl
6649 && (TREE_CODE (t) != NOP_EXPR
6650 || TREE_OPERAND (t, 0) != decl))
6651 TREE_OPERAND (cond, 1)
6652 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6654 if (decl == error_mark_node || init == error_mark_node)
6655 return NULL;
6657 TREE_VEC_ELT (declv, i) = decl;
6658 TREE_VEC_ELT (initv, i) = init;
6659 TREE_VEC_ELT (condv, i) = cond;
6660 TREE_VEC_ELT (incrv, i) = incr;
6661 i++;
6664 if (IS_EMPTY_STMT (pre_body))
6665 pre_body = NULL;
6667 if (code == CILK_FOR && !processing_template_decl)
6668 block = push_stmt_list ();
6670 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6671 body, pre_body);
6673 if (omp_for == NULL)
6675 if (block)
6676 pop_stmt_list (block);
6677 return NULL;
6680 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6682 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6683 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6685 if (TREE_CODE (incr) != MODIFY_EXPR)
6686 continue;
6688 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6689 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6690 && !processing_template_decl)
6692 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6693 if (TREE_SIDE_EFFECTS (t)
6694 && t != decl
6695 && (TREE_CODE (t) != NOP_EXPR
6696 || TREE_OPERAND (t, 0) != decl))
6697 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6698 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6700 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6701 if (TREE_SIDE_EFFECTS (t)
6702 && t != decl
6703 && (TREE_CODE (t) != NOP_EXPR
6704 || TREE_OPERAND (t, 0) != decl))
6705 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6706 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6709 if (orig_incr)
6710 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6712 OMP_FOR_CLAUSES (omp_for) = clauses;
6714 if (block)
6716 tree omp_par = make_node (OMP_PARALLEL);
6717 TREE_TYPE (omp_par) = void_type_node;
6718 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6719 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6720 TREE_SIDE_EFFECTS (bind) = 1;
6721 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6722 OMP_PARALLEL_BODY (omp_par) = bind;
6723 if (OMP_FOR_PRE_BODY (omp_for))
6725 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6726 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6728 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6729 decl = TREE_OPERAND (init, 0);
6730 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6731 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6732 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6733 clauses = OMP_FOR_CLAUSES (omp_for);
6734 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6735 for (pc = &clauses; *pc; )
6736 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6738 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6739 OMP_FOR_CLAUSES (omp_for) = *pc;
6740 *pc = OMP_CLAUSE_CHAIN (*pc);
6741 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6743 else
6745 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6746 pc = &OMP_CLAUSE_CHAIN (*pc);
6748 if (TREE_CODE (t) != INTEGER_CST)
6750 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6751 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6752 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6753 OMP_CLAUSE_CHAIN (c) = clauses;
6754 clauses = c;
6756 if (TREE_CODE (incr) == MODIFY_EXPR)
6758 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6759 if (TREE_CODE (t) != INTEGER_CST)
6761 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6762 = get_temp_regvar (TREE_TYPE (t), t);
6763 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6764 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6765 OMP_CLAUSE_CHAIN (c) = clauses;
6766 clauses = c;
6769 t = TREE_OPERAND (init, 1);
6770 if (TREE_CODE (t) != INTEGER_CST)
6772 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6773 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6774 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6775 OMP_CLAUSE_CHAIN (c) = clauses;
6776 clauses = c;
6778 if (orig_decl && orig_decl != decl)
6780 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6781 OMP_CLAUSE_DECL (c) = orig_decl;
6782 OMP_CLAUSE_CHAIN (c) = clauses;
6783 clauses = c;
6785 if (last)
6787 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6788 OMP_CLAUSE_DECL (c) = last;
6789 OMP_CLAUSE_CHAIN (c) = clauses;
6790 clauses = c;
6792 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6793 OMP_CLAUSE_DECL (c) = decl;
6794 OMP_CLAUSE_CHAIN (c) = clauses;
6795 clauses = c;
6796 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6797 OMP_CLAUSE_OPERAND (c, 0)
6798 = cilk_for_number_of_iterations (omp_for);
6799 OMP_CLAUSE_CHAIN (c) = clauses;
6800 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6801 add_stmt (omp_par);
6802 return omp_par;
6804 else if (code == CILK_FOR && processing_template_decl)
6806 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6807 if (orig_decl && orig_decl != decl)
6809 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6810 OMP_CLAUSE_DECL (c) = orig_decl;
6811 OMP_CLAUSE_CHAIN (c) = clauses;
6812 clauses = c;
6814 if (last)
6816 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6817 OMP_CLAUSE_DECL (c) = last;
6818 OMP_CLAUSE_CHAIN (c) = clauses;
6819 clauses = c;
6821 OMP_FOR_CLAUSES (omp_for) = clauses;
6823 return omp_for;
6826 void
6827 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6828 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6830 tree orig_lhs;
6831 tree orig_rhs;
6832 tree orig_v;
6833 tree orig_lhs1;
6834 tree orig_rhs1;
6835 bool dependent_p;
6836 tree stmt;
6838 orig_lhs = lhs;
6839 orig_rhs = rhs;
6840 orig_v = v;
6841 orig_lhs1 = lhs1;
6842 orig_rhs1 = rhs1;
6843 dependent_p = false;
6844 stmt = NULL_TREE;
6846 /* Even in a template, we can detect invalid uses of the atomic
6847 pragma if neither LHS nor RHS is type-dependent. */
6848 if (processing_template_decl)
6850 dependent_p = (type_dependent_expression_p (lhs)
6851 || (rhs && type_dependent_expression_p (rhs))
6852 || (v && type_dependent_expression_p (v))
6853 || (lhs1 && type_dependent_expression_p (lhs1))
6854 || (rhs1 && type_dependent_expression_p (rhs1)));
6855 if (!dependent_p)
6857 lhs = build_non_dependent_expr (lhs);
6858 if (rhs)
6859 rhs = build_non_dependent_expr (rhs);
6860 if (v)
6861 v = build_non_dependent_expr (v);
6862 if (lhs1)
6863 lhs1 = build_non_dependent_expr (lhs1);
6864 if (rhs1)
6865 rhs1 = build_non_dependent_expr (rhs1);
6868 if (!dependent_p)
6870 bool swapped = false;
6871 if (rhs1 && cp_tree_equal (lhs, rhs))
6873 std::swap (rhs, rhs1);
6874 swapped = !commutative_tree_code (opcode);
6876 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6878 if (code == OMP_ATOMIC)
6879 error ("%<#pragma omp atomic update%> uses two different "
6880 "expressions for memory");
6881 else
6882 error ("%<#pragma omp atomic capture%> uses two different "
6883 "expressions for memory");
6884 return;
6886 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6888 if (code == OMP_ATOMIC)
6889 error ("%<#pragma omp atomic update%> uses two different "
6890 "expressions for memory");
6891 else
6892 error ("%<#pragma omp atomic capture%> uses two different "
6893 "expressions for memory");
6894 return;
6896 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6897 v, lhs1, rhs1, swapped, seq_cst);
6898 if (stmt == error_mark_node)
6899 return;
6901 if (processing_template_decl)
6903 if (code == OMP_ATOMIC_READ)
6905 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6906 OMP_ATOMIC_READ, orig_lhs);
6907 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6908 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6910 else
6912 if (opcode == NOP_EXPR)
6913 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6914 else
6915 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6916 if (orig_rhs1)
6917 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6918 COMPOUND_EXPR, orig_rhs1, stmt);
6919 if (code != OMP_ATOMIC)
6921 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6922 code, orig_lhs1, stmt);
6923 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6924 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6927 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6928 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6930 finish_expr_stmt (stmt);
6933 void
6934 finish_omp_barrier (void)
6936 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6937 vec<tree, va_gc> *vec = make_tree_vector ();
6938 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6939 release_tree_vector (vec);
6940 finish_expr_stmt (stmt);
6943 void
6944 finish_omp_flush (void)
6946 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6947 vec<tree, va_gc> *vec = make_tree_vector ();
6948 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6949 release_tree_vector (vec);
6950 finish_expr_stmt (stmt);
6953 void
6954 finish_omp_taskwait (void)
6956 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6957 vec<tree, va_gc> *vec = make_tree_vector ();
6958 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6959 release_tree_vector (vec);
6960 finish_expr_stmt (stmt);
6963 void
6964 finish_omp_taskyield (void)
6966 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6967 vec<tree, va_gc> *vec = make_tree_vector ();
6968 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6969 release_tree_vector (vec);
6970 finish_expr_stmt (stmt);
6973 void
6974 finish_omp_cancel (tree clauses)
6976 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6977 int mask = 0;
6978 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6979 mask = 1;
6980 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6981 mask = 2;
6982 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6983 mask = 4;
6984 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6985 mask = 8;
6986 else
6988 error ("%<#pragma omp cancel must specify one of "
6989 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6990 return;
6992 vec<tree, va_gc> *vec = make_tree_vector ();
6993 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6994 if (ifc != NULL_TREE)
6996 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6997 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6998 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6999 build_zero_cst (type));
7001 else
7002 ifc = boolean_true_node;
7003 vec->quick_push (build_int_cst (integer_type_node, mask));
7004 vec->quick_push (ifc);
7005 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7006 release_tree_vector (vec);
7007 finish_expr_stmt (stmt);
7010 void
7011 finish_omp_cancellation_point (tree clauses)
7013 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7014 int mask = 0;
7015 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7016 mask = 1;
7017 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7018 mask = 2;
7019 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7020 mask = 4;
7021 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7022 mask = 8;
7023 else
7025 error ("%<#pragma omp cancellation point must specify one of "
7026 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7027 return;
7029 vec<tree, va_gc> *vec
7030 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7031 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7032 release_tree_vector (vec);
7033 finish_expr_stmt (stmt);
7036 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7037 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7038 should create an extra compound stmt. */
7040 tree
7041 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7043 tree r;
7045 if (pcompound)
7046 *pcompound = begin_compound_stmt (0);
7048 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7050 /* Only add the statement to the function if support enabled. */
7051 if (flag_tm)
7052 add_stmt (r);
7053 else
7054 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7055 ? G_("%<__transaction_relaxed%> without "
7056 "transactional memory support enabled")
7057 : G_("%<__transaction_atomic%> without "
7058 "transactional memory support enabled")));
7060 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7061 TREE_SIDE_EFFECTS (r) = 1;
7062 return r;
7065 /* End a __transaction_atomic or __transaction_relaxed statement.
7066 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7067 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7068 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7070 void
7071 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7073 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7074 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7075 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7076 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7078 /* noexcept specifications are not allowed for function transactions. */
7079 gcc_assert (!(noex && compound_stmt));
7080 if (noex)
7082 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7083 noex);
7084 /* This may not be true when the STATEMENT_LIST is empty. */
7085 if (EXPR_P (body))
7086 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7087 TREE_SIDE_EFFECTS (body) = 1;
7088 TRANSACTION_EXPR_BODY (stmt) = body;
7091 if (compound_stmt)
7092 finish_compound_stmt (compound_stmt);
7095 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7096 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7097 condition. */
7099 tree
7100 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7102 tree ret;
7103 if (noex)
7105 expr = build_must_not_throw_expr (expr, noex);
7106 if (EXPR_P (expr))
7107 SET_EXPR_LOCATION (expr, loc);
7108 TREE_SIDE_EFFECTS (expr) = 1;
7110 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7111 if (flags & TM_STMT_ATTR_RELAXED)
7112 TRANSACTION_EXPR_RELAXED (ret) = 1;
7113 TREE_SIDE_EFFECTS (ret) = 1;
7114 SET_EXPR_LOCATION (ret, loc);
7115 return ret;
7118 void
7119 init_cp_semantics (void)
7123 /* Build a STATIC_ASSERT for a static assertion with the condition
7124 CONDITION and the message text MESSAGE. LOCATION is the location
7125 of the static assertion in the source code. When MEMBER_P, this
7126 static assertion is a member of a class. */
7127 void
7128 finish_static_assert (tree condition, tree message, location_t location,
7129 bool member_p)
7131 if (message == NULL_TREE
7132 || message == error_mark_node
7133 || condition == NULL_TREE
7134 || condition == error_mark_node)
7135 return;
7137 if (check_for_bare_parameter_packs (condition))
7138 condition = error_mark_node;
7140 if (type_dependent_expression_p (condition)
7141 || value_dependent_expression_p (condition))
7143 /* We're in a template; build a STATIC_ASSERT and put it in
7144 the right place. */
7145 tree assertion;
7147 assertion = make_node (STATIC_ASSERT);
7148 STATIC_ASSERT_CONDITION (assertion) = condition;
7149 STATIC_ASSERT_MESSAGE (assertion) = message;
7150 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7152 if (member_p)
7153 maybe_add_class_template_decl_list (current_class_type,
7154 assertion,
7155 /*friend_p=*/0);
7156 else
7157 add_stmt (assertion);
7159 return;
7162 /* Fold the expression and convert it to a boolean value. */
7163 condition = instantiate_non_dependent_expr (condition);
7164 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7165 condition = maybe_constant_value (condition);
7167 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7168 /* Do nothing; the condition is satisfied. */
7170 else
7172 location_t saved_loc = input_location;
7174 input_location = location;
7175 if (TREE_CODE (condition) == INTEGER_CST
7176 && integer_zerop (condition))
7177 /* Report the error. */
7178 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7179 else if (condition && condition != error_mark_node)
7181 error ("non-constant condition for static assertion");
7182 if (require_potential_rvalue_constant_expression (condition))
7183 cxx_constant_value (condition);
7185 input_location = saved_loc;
7189 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7190 suitable for use as a type-specifier.
7192 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7193 id-expression or a class member access, FALSE when it was parsed as
7194 a full expression. */
7196 tree
7197 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7198 tsubst_flags_t complain)
7200 tree type = NULL_TREE;
7202 if (!expr || error_operand_p (expr))
7203 return error_mark_node;
7205 if (TYPE_P (expr)
7206 || TREE_CODE (expr) == TYPE_DECL
7207 || (TREE_CODE (expr) == BIT_NOT_EXPR
7208 && TYPE_P (TREE_OPERAND (expr, 0))))
7210 if (complain & tf_error)
7211 error ("argument to decltype must be an expression");
7212 return error_mark_node;
7215 /* Depending on the resolution of DR 1172, we may later need to distinguish
7216 instantiation-dependent but not type-dependent expressions so that, say,
7217 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7218 if (instantiation_dependent_expression_p (expr))
7220 type = cxx_make_type (DECLTYPE_TYPE);
7221 DECLTYPE_TYPE_EXPR (type) = expr;
7222 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7223 = id_expression_or_member_access_p;
7224 SET_TYPE_STRUCTURAL_EQUALITY (type);
7226 return type;
7229 /* The type denoted by decltype(e) is defined as follows: */
7231 expr = resolve_nondeduced_context (expr);
7233 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
7234 return error_mark_node;
7236 if (type_unknown_p (expr))
7238 if (complain & tf_error)
7239 error ("decltype cannot resolve address of overloaded function");
7240 return error_mark_node;
7243 /* To get the size of a static data member declared as an array of
7244 unknown bound, we need to instantiate it. */
7245 if (VAR_P (expr)
7246 && VAR_HAD_UNKNOWN_BOUND (expr)
7247 && DECL_TEMPLATE_INSTANTIATION (expr))
7248 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7250 if (id_expression_or_member_access_p)
7252 /* If e is an id-expression or a class member access (5.2.5
7253 [expr.ref]), decltype(e) is defined as the type of the entity
7254 named by e. If there is no such entity, or e names a set of
7255 overloaded functions, the program is ill-formed. */
7256 if (identifier_p (expr))
7257 expr = lookup_name (expr);
7259 if (INDIRECT_REF_P (expr))
7260 /* This can happen when the expression is, e.g., "a.b". Just
7261 look at the underlying operand. */
7262 expr = TREE_OPERAND (expr, 0);
7264 if (TREE_CODE (expr) == OFFSET_REF
7265 || TREE_CODE (expr) == MEMBER_REF
7266 || TREE_CODE (expr) == SCOPE_REF)
7267 /* We're only interested in the field itself. If it is a
7268 BASELINK, we will need to see through it in the next
7269 step. */
7270 expr = TREE_OPERAND (expr, 1);
7272 if (BASELINK_P (expr))
7273 /* See through BASELINK nodes to the underlying function. */
7274 expr = BASELINK_FUNCTIONS (expr);
7276 switch (TREE_CODE (expr))
7278 case FIELD_DECL:
7279 if (DECL_BIT_FIELD_TYPE (expr))
7281 type = DECL_BIT_FIELD_TYPE (expr);
7282 break;
7284 /* Fall through for fields that aren't bitfields. */
7286 case FUNCTION_DECL:
7287 case VAR_DECL:
7288 case CONST_DECL:
7289 case PARM_DECL:
7290 case RESULT_DECL:
7291 case TEMPLATE_PARM_INDEX:
7292 expr = mark_type_use (expr);
7293 type = TREE_TYPE (expr);
7294 break;
7296 case ERROR_MARK:
7297 type = error_mark_node;
7298 break;
7300 case COMPONENT_REF:
7301 case COMPOUND_EXPR:
7302 mark_type_use (expr);
7303 type = is_bitfield_expr_with_lowered_type (expr);
7304 if (!type)
7305 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7306 break;
7308 case BIT_FIELD_REF:
7309 gcc_unreachable ();
7311 case INTEGER_CST:
7312 case PTRMEM_CST:
7313 /* We can get here when the id-expression refers to an
7314 enumerator or non-type template parameter. */
7315 type = TREE_TYPE (expr);
7316 break;
7318 default:
7319 /* Handle instantiated template non-type arguments. */
7320 type = TREE_TYPE (expr);
7321 break;
7324 else
7326 /* Within a lambda-expression:
7328 Every occurrence of decltype((x)) where x is a possibly
7329 parenthesized id-expression that names an entity of
7330 automatic storage duration is treated as if x were
7331 transformed into an access to a corresponding data member
7332 of the closure type that would have been declared if x
7333 were a use of the denoted entity. */
7334 if (outer_automatic_var_p (expr)
7335 && current_function_decl
7336 && LAMBDA_FUNCTION_P (current_function_decl))
7337 type = capture_decltype (expr);
7338 else if (error_operand_p (expr))
7339 type = error_mark_node;
7340 else if (expr == current_class_ptr)
7341 /* If the expression is just "this", we want the
7342 cv-unqualified pointer for the "this" type. */
7343 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7344 else
7346 /* Otherwise, where T is the type of e, if e is an lvalue,
7347 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7348 cp_lvalue_kind clk = lvalue_kind (expr);
7349 type = unlowered_expr_type (expr);
7350 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7352 /* For vector types, pick a non-opaque variant. */
7353 if (TREE_CODE (type) == VECTOR_TYPE)
7354 type = strip_typedefs (type);
7356 if (clk != clk_none && !(clk & clk_class))
7357 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7361 return type;
7364 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7365 __has_nothrow_copy, depending on assign_p. */
7367 static bool
7368 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7370 tree fns;
7372 if (assign_p)
7374 int ix;
7375 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7376 if (ix < 0)
7377 return false;
7378 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7380 else if (TYPE_HAS_COPY_CTOR (type))
7382 /* If construction of the copy constructor was postponed, create
7383 it now. */
7384 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7385 lazily_declare_fn (sfk_copy_constructor, type);
7386 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7387 lazily_declare_fn (sfk_move_constructor, type);
7388 fns = CLASSTYPE_CONSTRUCTORS (type);
7390 else
7391 return false;
7393 for (; fns; fns = OVL_NEXT (fns))
7395 tree fn = OVL_CURRENT (fns);
7397 if (assign_p)
7399 if (copy_fn_p (fn) == 0)
7400 continue;
7402 else if (copy_fn_p (fn) <= 0)
7403 continue;
7405 maybe_instantiate_noexcept (fn);
7406 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7407 return false;
7410 return true;
7413 /* Actually evaluates the trait. */
7415 static bool
7416 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7418 enum tree_code type_code1;
7419 tree t;
7421 type_code1 = TREE_CODE (type1);
7423 switch (kind)
7425 case CPTK_HAS_NOTHROW_ASSIGN:
7426 type1 = strip_array_types (type1);
7427 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7428 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7429 || (CLASS_TYPE_P (type1)
7430 && classtype_has_nothrow_assign_or_copy_p (type1,
7431 true))));
7433 case CPTK_HAS_TRIVIAL_ASSIGN:
7434 /* ??? The standard seems to be missing the "or array of such a class
7435 type" wording for this trait. */
7436 type1 = strip_array_types (type1);
7437 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7438 && (trivial_type_p (type1)
7439 || (CLASS_TYPE_P (type1)
7440 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7442 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7443 type1 = strip_array_types (type1);
7444 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7445 || (CLASS_TYPE_P (type1)
7446 && (t = locate_ctor (type1))
7447 && (maybe_instantiate_noexcept (t),
7448 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7450 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7451 type1 = strip_array_types (type1);
7452 return (trivial_type_p (type1)
7453 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7455 case CPTK_HAS_NOTHROW_COPY:
7456 type1 = strip_array_types (type1);
7457 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7458 || (CLASS_TYPE_P (type1)
7459 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7461 case CPTK_HAS_TRIVIAL_COPY:
7462 /* ??? The standard seems to be missing the "or array of such a class
7463 type" wording for this trait. */
7464 type1 = strip_array_types (type1);
7465 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7466 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7468 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7469 type1 = strip_array_types (type1);
7470 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7471 || (CLASS_TYPE_P (type1)
7472 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7474 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7475 return type_has_virtual_destructor (type1);
7477 case CPTK_IS_ABSTRACT:
7478 return (ABSTRACT_CLASS_TYPE_P (type1));
7480 case CPTK_IS_BASE_OF:
7481 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7482 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7483 || DERIVED_FROM_P (type1, type2)));
7485 case CPTK_IS_CLASS:
7486 return (NON_UNION_CLASS_TYPE_P (type1));
7488 case CPTK_IS_EMPTY:
7489 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7491 case CPTK_IS_ENUM:
7492 return (type_code1 == ENUMERAL_TYPE);
7494 case CPTK_IS_FINAL:
7495 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7497 case CPTK_IS_LITERAL_TYPE:
7498 return (literal_type_p (type1));
7500 case CPTK_IS_POD:
7501 return (pod_type_p (type1));
7503 case CPTK_IS_POLYMORPHIC:
7504 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7506 case CPTK_IS_STD_LAYOUT:
7507 return (std_layout_type_p (type1));
7509 case CPTK_IS_TRIVIAL:
7510 return (trivial_type_p (type1));
7512 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7513 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7515 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7516 return is_trivially_xible (INIT_EXPR, type1, type2);
7518 case CPTK_IS_TRIVIALLY_COPYABLE:
7519 return (trivially_copyable_p (type1));
7521 case CPTK_IS_UNION:
7522 return (type_code1 == UNION_TYPE);
7524 default:
7525 gcc_unreachable ();
7526 return false;
7530 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7531 void, or a complete type, returns true, otherwise false. */
7533 static bool
7534 check_trait_type (tree type)
7536 if (type == NULL_TREE)
7537 return true;
7539 if (TREE_CODE (type) == TREE_LIST)
7540 return (check_trait_type (TREE_VALUE (type))
7541 && check_trait_type (TREE_CHAIN (type)));
7543 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7544 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7545 return true;
7547 if (VOID_TYPE_P (type))
7548 return true;
7550 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7553 /* Process a trait expression. */
7555 tree
7556 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7558 if (type1 == error_mark_node
7559 || type2 == error_mark_node)
7560 return error_mark_node;
7562 if (processing_template_decl)
7564 tree trait_expr = make_node (TRAIT_EXPR);
7565 TREE_TYPE (trait_expr) = boolean_type_node;
7566 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7567 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7568 TRAIT_EXPR_KIND (trait_expr) = kind;
7569 return trait_expr;
7572 switch (kind)
7574 case CPTK_HAS_NOTHROW_ASSIGN:
7575 case CPTK_HAS_TRIVIAL_ASSIGN:
7576 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7577 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7578 case CPTK_HAS_NOTHROW_COPY:
7579 case CPTK_HAS_TRIVIAL_COPY:
7580 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7581 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7582 case CPTK_IS_ABSTRACT:
7583 case CPTK_IS_EMPTY:
7584 case CPTK_IS_FINAL:
7585 case CPTK_IS_LITERAL_TYPE:
7586 case CPTK_IS_POD:
7587 case CPTK_IS_POLYMORPHIC:
7588 case CPTK_IS_STD_LAYOUT:
7589 case CPTK_IS_TRIVIAL:
7590 case CPTK_IS_TRIVIALLY_COPYABLE:
7591 if (!check_trait_type (type1))
7592 return error_mark_node;
7593 break;
7595 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7596 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7597 if (!check_trait_type (type1)
7598 || !check_trait_type (type2))
7599 return error_mark_node;
7600 break;
7602 case CPTK_IS_BASE_OF:
7603 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7604 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7605 && !complete_type_or_else (type2, NULL_TREE))
7606 /* We already issued an error. */
7607 return error_mark_node;
7608 break;
7610 case CPTK_IS_CLASS:
7611 case CPTK_IS_ENUM:
7612 case CPTK_IS_UNION:
7613 break;
7615 default:
7616 gcc_unreachable ();
7619 return (trait_expr_value (kind, type1, type2)
7620 ? boolean_true_node : boolean_false_node);
7623 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7624 which is ignored for C++. */
7626 void
7627 set_float_const_decimal64 (void)
7631 void
7632 clear_float_const_decimal64 (void)
7636 bool
7637 float_const_decimal64_p (void)
7639 return 0;
7643 /* Return true if T designates the implied `this' parameter. */
7645 bool
7646 is_this_parameter (tree t)
7648 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7649 return false;
7650 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7651 return true;
7654 /* Insert the deduced return type for an auto function. */
7656 void
7657 apply_deduced_return_type (tree fco, tree return_type)
7659 tree result;
7661 if (return_type == error_mark_node)
7662 return;
7664 if (LAMBDA_FUNCTION_P (fco))
7666 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7667 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7670 if (DECL_CONV_FN_P (fco))
7671 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7673 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7675 result = DECL_RESULT (fco);
7676 if (result == NULL_TREE)
7677 return;
7678 if (TREE_TYPE (result) == return_type)
7679 return;
7681 /* We already have a DECL_RESULT from start_preparsed_function.
7682 Now we need to redo the work it and allocate_struct_function
7683 did to reflect the new type. */
7684 gcc_assert (current_function_decl == fco);
7685 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7686 TYPE_MAIN_VARIANT (return_type));
7687 DECL_ARTIFICIAL (result) = 1;
7688 DECL_IGNORED_P (result) = 1;
7689 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7690 result);
7692 DECL_RESULT (fco) = result;
7694 if (!processing_template_decl)
7696 if (!VOID_TYPE_P (TREE_TYPE (result)))
7697 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7698 bool aggr = aggregate_value_p (result, fco);
7699 #ifdef PCC_STATIC_STRUCT_RETURN
7700 cfun->returns_pcc_struct = aggr;
7701 #endif
7702 cfun->returns_struct = aggr;
7707 /* DECL is a local variable or parameter from the surrounding scope of a
7708 lambda-expression. Returns the decltype for a use of the capture field
7709 for DECL even if it hasn't been captured yet. */
7711 static tree
7712 capture_decltype (tree decl)
7714 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7715 /* FIXME do lookup instead of list walk? */
7716 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7717 tree type;
7719 if (cap)
7720 type = TREE_TYPE (TREE_PURPOSE (cap));
7721 else
7722 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7724 case CPLD_NONE:
7725 error ("%qD is not captured", decl);
7726 return error_mark_node;
7728 case CPLD_COPY:
7729 type = TREE_TYPE (decl);
7730 if (TREE_CODE (type) == REFERENCE_TYPE
7731 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7732 type = TREE_TYPE (type);
7733 break;
7735 case CPLD_REFERENCE:
7736 type = TREE_TYPE (decl);
7737 if (TREE_CODE (type) != REFERENCE_TYPE)
7738 type = build_reference_type (TREE_TYPE (decl));
7739 break;
7741 default:
7742 gcc_unreachable ();
7745 if (TREE_CODE (type) != REFERENCE_TYPE)
7747 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7748 type = cp_build_qualified_type (type, (cp_type_quals (type)
7749 |TYPE_QUAL_CONST));
7750 type = build_reference_type (type);
7752 return type;
7755 #include "gt-cp-semantics.h"