Merge trunk version 216723 into gupc branch.
[official-gcc.git] / gcc / cp / semantics.c
blob26e66f512cf4bb6e01e606f5bb8660326ebbb081
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "hash-table.h"
48 #include "gimplify.h"
49 #include "bitmap.h"
50 #include "omp-low.h"
51 #include "builtins.h"
52 #include "convert.h"
54 /* There routines provide a modular interface to perform many parsing
55 operations. They may therefore be used during actual parsing, or
56 during template instantiation, which may be regarded as a
57 degenerate form of parsing. */
59 static tree maybe_convert_cond (tree);
60 static tree finalize_nrv_r (tree *, int *, void *);
61 static tree capture_decltype (tree);
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 typedef struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
134 } deferred_access;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above. */
313 bool
314 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
315 tsubst_flags_t complain)
317 int i;
318 deferred_access *ptr;
319 deferred_access_check *chk;
322 /* Exit if we are in a context that no access checking is performed.
324 if (deferred_access_no_check)
325 return true;
327 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
329 ptr = &deferred_access_stack->last ();
331 /* If we are not supposed to defer access checks, just check now. */
332 if (ptr->deferring_access_checks_kind == dk_no_deferred)
334 bool ok = enforce_access (binfo, decl, diag_decl, complain);
335 return (complain & tf_error) ? true : ok;
338 /* See if we are already going to perform this check. */
339 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
341 if (chk->decl == decl && chk->binfo == binfo &&
342 chk->diag_decl == diag_decl)
344 return true;
347 /* If not, record the check. */
348 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
349 vec_safe_push (ptr->deferred_access_checks, new_access);
351 return true;
354 /* Returns nonzero if the current statement is a full expression,
355 i.e. temporaries created during that statement should be destroyed
356 at the end of the statement. */
359 stmts_are_full_exprs_p (void)
361 return current_stmt_tree ()->stmts_are_full_exprs_p;
364 /* T is a statement. Add it to the statement-tree. This is the C++
365 version. The C/ObjC frontends have a slightly different version of
366 this function. */
368 tree
369 add_stmt (tree t)
371 enum tree_code code = TREE_CODE (t);
373 if (EXPR_P (t) && code != LABEL_EXPR)
375 if (!EXPR_HAS_LOCATION (t))
376 SET_EXPR_LOCATION (t, input_location);
378 /* When we expand a statement-tree, we must know whether or not the
379 statements are full-expressions. We record that fact here. */
380 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
383 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
384 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
386 /* Add T to the statement-tree. Non-side-effect statements need to be
387 recorded during statement expressions. */
388 gcc_checking_assert (!stmt_list_stack->is_empty ());
389 append_to_statement_list_force (t, &cur_stmt_list);
391 return t;
394 /* Returns the stmt_tree to which statements are currently being added. */
396 stmt_tree
397 current_stmt_tree (void)
399 return (cfun
400 ? &cfun->language->base.x_stmt_tree
401 : &scope_chain->x_stmt_tree);
404 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
406 static tree
407 maybe_cleanup_point_expr (tree expr)
409 if (!processing_template_decl && stmts_are_full_exprs_p ())
410 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
411 return expr;
414 /* Like maybe_cleanup_point_expr except have the type of the new expression be
415 void so we don't need to create a temporary variable to hold the inner
416 expression. The reason why we do this is because the original type might be
417 an aggregate and we cannot create a temporary variable for that type. */
419 tree
420 maybe_cleanup_point_expr_void (tree expr)
422 if (!processing_template_decl && stmts_are_full_exprs_p ())
423 expr = fold_build_cleanup_point_expr (void_type_node, expr);
424 return expr;
429 /* Create a declaration statement for the declaration given by the DECL. */
431 void
432 add_decl_expr (tree decl)
434 tree r = build_stmt (input_location, DECL_EXPR, decl);
435 if (DECL_INITIAL (decl)
436 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
437 r = maybe_cleanup_point_expr_void (r);
438 add_stmt (r);
441 /* Finish a scope. */
443 tree
444 do_poplevel (tree stmt_list)
446 tree block = NULL;
448 if (stmts_are_full_exprs_p ())
449 block = poplevel (kept_level_p (), 1, 0);
451 stmt_list = pop_stmt_list (stmt_list);
453 if (!processing_template_decl)
455 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
456 /* ??? See c_end_compound_stmt re statement expressions. */
459 return stmt_list;
462 /* Begin a new scope. */
464 static tree
465 do_pushlevel (scope_kind sk)
467 tree ret = push_stmt_list ();
468 if (stmts_are_full_exprs_p ())
469 begin_scope (sk, NULL);
470 return ret;
473 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
474 when the current scope is exited. EH_ONLY is true when this is not
475 meant to apply to normal control flow transfer. */
477 void
478 push_cleanup (tree decl, tree cleanup, bool eh_only)
480 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
481 CLEANUP_EH_ONLY (stmt) = eh_only;
482 add_stmt (stmt);
483 CLEANUP_BODY (stmt) = push_stmt_list ();
486 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
487 the current loops, represented by 'NULL_TREE' if we've seen a possible
488 exit, and 'error_mark_node' if not. This is currently used only to
489 suppress the warning about a function with no return statements, and
490 therefore we don't bother noting returns as possible exits. We also
491 don't bother with gotos. */
493 static void
494 begin_maybe_infinite_loop (tree cond)
496 /* Only track this while parsing a function, not during instantiation. */
497 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
498 && !processing_template_decl))
499 return;
500 bool maybe_infinite = true;
501 if (cond)
503 cond = fold_non_dependent_expr_sfinae (cond, tf_none);
504 cond = maybe_constant_value (cond);
505 maybe_infinite = integer_nonzerop (cond);
507 vec_safe_push (cp_function_chain->infinite_loops,
508 maybe_infinite ? error_mark_node : NULL_TREE);
512 /* A break is a possible exit for the current loop. */
514 void
515 break_maybe_infinite_loop (void)
517 if (!cfun)
518 return;
519 cp_function_chain->infinite_loops->last() = NULL_TREE;
522 /* If we reach the end of the loop without seeing a possible exit, we have
523 an infinite loop. */
525 static void
526 end_maybe_infinite_loop (tree cond)
528 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
529 && !processing_template_decl))
530 return;
531 tree current = cp_function_chain->infinite_loops->pop();
532 if (current != NULL_TREE)
534 cond = fold_non_dependent_expr (cond);
535 cond = maybe_constant_value (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
635 /* COND is the condition-expression for an if, while, etc.,
636 statement. Convert it to a boolean value, if appropriate.
637 In addition, verify sequence points if -Wsequence-point is enabled. */
639 static tree
640 maybe_convert_cond (tree cond)
642 /* Empty conditions remain empty. */
643 if (!cond)
644 return NULL_TREE;
646 /* Wait until we instantiate templates before doing conversion. */
647 if (processing_template_decl)
648 return cond;
650 if (warn_sequence_point)
651 verify_sequence_points (cond);
653 /* Do the conversion. */
654 cond = convert_from_reference (cond);
656 if (TREE_CODE (cond) == MODIFY_EXPR
657 && !TREE_NO_WARNING (cond)
658 && warn_parentheses)
660 warning (OPT_Wparentheses,
661 "suggest parentheses around assignment used as truth value");
662 TREE_NO_WARNING (cond) = 1;
665 return condition_conversion (cond);
668 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
670 tree
671 finish_expr_stmt (tree expr)
673 tree r = NULL_TREE;
675 if (expr != NULL_TREE)
677 if (!processing_template_decl)
679 if (warn_sequence_point)
680 verify_sequence_points (expr);
681 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
683 else if (!type_dependent_expression_p (expr))
684 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
685 tf_warning_or_error);
687 if (check_for_bare_parameter_packs (expr))
688 expr = error_mark_node;
690 /* Simplification of inner statement expressions, compound exprs,
691 etc can result in us already having an EXPR_STMT. */
692 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
694 if (TREE_CODE (expr) != EXPR_STMT)
695 expr = build_stmt (input_location, EXPR_STMT, expr);
696 expr = maybe_cleanup_point_expr_void (expr);
699 r = add_stmt (expr);
702 return r;
706 /* Begin an if-statement. Returns a newly created IF_STMT if
707 appropriate. */
709 tree
710 begin_if_stmt (void)
712 tree r, scope;
713 scope = do_pushlevel (sk_cond);
714 r = build_stmt (input_location, IF_STMT, NULL_TREE,
715 NULL_TREE, NULL_TREE, scope);
716 begin_cond (&IF_COND (r));
717 return r;
720 /* Process the COND of an if-statement, which may be given by
721 IF_STMT. */
723 void
724 finish_if_stmt_cond (tree cond, tree if_stmt)
726 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
727 add_stmt (if_stmt);
728 THEN_CLAUSE (if_stmt) = push_stmt_list ();
731 /* Finish the then-clause of an if-statement, which may be given by
732 IF_STMT. */
734 tree
735 finish_then_clause (tree if_stmt)
737 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
738 return if_stmt;
741 /* Begin the else-clause of an if-statement. */
743 void
744 begin_else_clause (tree if_stmt)
746 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
749 /* Finish the else-clause of an if-statement, which may be given by
750 IF_STMT. */
752 void
753 finish_else_clause (tree if_stmt)
755 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
758 /* Finish an if-statement. */
760 void
761 finish_if_stmt (tree if_stmt)
763 tree scope = IF_SCOPE (if_stmt);
764 IF_SCOPE (if_stmt) = NULL;
765 add_stmt (do_poplevel (scope));
768 /* Begin a while-statement. Returns a newly created WHILE_STMT if
769 appropriate. */
771 tree
772 begin_while_stmt (void)
774 tree r;
775 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
776 add_stmt (r);
777 WHILE_BODY (r) = do_pushlevel (sk_block);
778 begin_cond (&WHILE_COND (r));
779 return r;
782 /* Process the COND of a while-statement, which may be given by
783 WHILE_STMT. */
785 void
786 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
788 cond = maybe_convert_cond (cond);
789 finish_cond (&WHILE_COND (while_stmt), cond);
790 begin_maybe_infinite_loop (cond);
791 if (ivdep && cond != error_mark_node)
792 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
793 TREE_TYPE (WHILE_COND (while_stmt)),
794 WHILE_COND (while_stmt),
795 build_int_cst (integer_type_node,
796 annot_expr_ivdep_kind));
797 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
800 /* Finish a while-statement, which may be given by WHILE_STMT. */
802 void
803 finish_while_stmt (tree while_stmt)
805 end_maybe_infinite_loop (boolean_true_node);
806 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
809 /* Begin a do-statement. Returns a newly created DO_STMT if
810 appropriate. */
812 tree
813 begin_do_stmt (void)
815 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
816 begin_maybe_infinite_loop (boolean_true_node);
817 add_stmt (r);
818 DO_BODY (r) = push_stmt_list ();
819 return r;
822 /* Finish the body of a do-statement, which may be given by DO_STMT. */
824 void
825 finish_do_body (tree do_stmt)
827 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
829 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
830 body = STATEMENT_LIST_TAIL (body)->stmt;
832 if (IS_EMPTY_STMT (body))
833 warning (OPT_Wempty_body,
834 "suggest explicit braces around empty body in %<do%> statement");
837 /* Finish a do-statement, which may be given by DO_STMT, and whose
838 COND is as indicated. */
840 void
841 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
843 cond = maybe_convert_cond (cond);
844 end_maybe_infinite_loop (cond);
845 if (ivdep && cond != error_mark_node)
846 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
847 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
848 DO_COND (do_stmt) = cond;
851 /* Finish a return-statement. The EXPRESSION returned, if any, is as
852 indicated. */
854 tree
855 finish_return_stmt (tree expr)
857 tree r;
858 bool no_warning;
860 expr = check_return_expr (expr, &no_warning);
862 if (error_operand_p (expr)
863 || (flag_openmp && !check_omp_return ()))
864 return error_mark_node;
865 if (!processing_template_decl)
867 if (warn_sequence_point)
868 verify_sequence_points (expr);
870 if (DECL_DESTRUCTOR_P (current_function_decl)
871 || (DECL_CONSTRUCTOR_P (current_function_decl)
872 && targetm.cxx.cdtor_returns_this ()))
874 /* Similarly, all destructors must run destructors for
875 base-classes before returning. So, all returns in a
876 destructor get sent to the DTOR_LABEL; finish_function emits
877 code to return a value there. */
878 return finish_goto_stmt (cdtor_label);
882 r = build_stmt (input_location, RETURN_EXPR, expr);
883 TREE_NO_WARNING (r) |= no_warning;
884 r = maybe_cleanup_point_expr_void (r);
885 r = add_stmt (r);
887 return r;
890 /* Begin the scope of a for-statement or a range-for-statement.
891 Both the returned trees are to be used in a call to
892 begin_for_stmt or begin_range_for_stmt. */
894 tree
895 begin_for_scope (tree *init)
897 tree scope = NULL_TREE;
898 if (flag_new_for_scope > 0)
899 scope = do_pushlevel (sk_for);
901 if (processing_template_decl)
902 *init = push_stmt_list ();
903 else
904 *init = NULL_TREE;
906 return scope;
909 /* Begin a for-statement. Returns a new FOR_STMT.
910 SCOPE and INIT should be the return of begin_for_scope,
911 or both NULL_TREE */
913 tree
914 begin_for_stmt (tree scope, tree init)
916 tree r;
918 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
919 NULL_TREE, NULL_TREE, NULL_TREE);
921 if (scope == NULL_TREE)
923 gcc_assert (!init || !(flag_new_for_scope > 0));
924 if (!init)
925 scope = begin_for_scope (&init);
927 FOR_INIT_STMT (r) = init;
928 FOR_SCOPE (r) = scope;
930 return r;
933 /* Finish the for-init-statement of a for-statement, which may be
934 given by FOR_STMT. */
936 void
937 finish_for_init_stmt (tree for_stmt)
939 if (processing_template_decl)
940 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
941 add_stmt (for_stmt);
942 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
943 begin_cond (&FOR_COND (for_stmt));
946 /* Finish the COND of a for-statement, which may be given by
947 FOR_STMT. */
949 void
950 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
952 cond = maybe_convert_cond (cond);
953 finish_cond (&FOR_COND (for_stmt), cond);
954 begin_maybe_infinite_loop (cond);
955 if (ivdep && cond != error_mark_node)
956 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
957 TREE_TYPE (FOR_COND (for_stmt)),
958 FOR_COND (for_stmt),
959 build_int_cst (integer_type_node,
960 annot_expr_ivdep_kind));
961 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
964 /* Finish the increment-EXPRESSION in a for-statement, which may be
965 given by FOR_STMT. */
967 void
968 finish_for_expr (tree expr, tree for_stmt)
970 if (!expr)
971 return;
972 /* If EXPR is an overloaded function, issue an error; there is no
973 context available to use to perform overload resolution. */
974 if (type_unknown_p (expr))
976 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
977 expr = error_mark_node;
979 if (!processing_template_decl)
981 if (warn_sequence_point)
982 verify_sequence_points (expr);
983 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
984 tf_warning_or_error);
986 else if (!type_dependent_expression_p (expr))
987 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
988 tf_warning_or_error);
989 expr = maybe_cleanup_point_expr_void (expr);
990 if (check_for_bare_parameter_packs (expr))
991 expr = error_mark_node;
992 FOR_EXPR (for_stmt) = expr;
995 /* Finish the body of a for-statement, which may be given by
996 FOR_STMT. The increment-EXPR for the loop must be
997 provided.
998 It can also finish RANGE_FOR_STMT. */
1000 void
1001 finish_for_stmt (tree for_stmt)
1003 end_maybe_infinite_loop (boolean_true_node);
1005 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1006 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1007 else
1008 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1010 /* Pop the scope for the body of the loop. */
1011 if (flag_new_for_scope > 0)
1013 tree scope;
1014 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1015 ? &RANGE_FOR_SCOPE (for_stmt)
1016 : &FOR_SCOPE (for_stmt));
1017 scope = *scope_ptr;
1018 *scope_ptr = NULL;
1019 add_stmt (do_poplevel (scope));
1023 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1024 SCOPE and INIT should be the return of begin_for_scope,
1025 or both NULL_TREE .
1026 To finish it call finish_for_stmt(). */
1028 tree
1029 begin_range_for_stmt (tree scope, tree init)
1031 tree r;
1033 begin_maybe_infinite_loop (boolean_false_node);
1035 r = build_stmt (input_location, RANGE_FOR_STMT,
1036 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1038 if (scope == NULL_TREE)
1040 gcc_assert (!init || !(flag_new_for_scope > 0));
1041 if (!init)
1042 scope = begin_for_scope (&init);
1045 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1046 pop it now. */
1047 if (init)
1048 pop_stmt_list (init);
1049 RANGE_FOR_SCOPE (r) = scope;
1051 return r;
1054 /* Finish the head of a range-based for statement, which may
1055 be given by RANGE_FOR_STMT. DECL must be the declaration
1056 and EXPR must be the loop expression. */
1058 void
1059 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1061 RANGE_FOR_DECL (range_for_stmt) = decl;
1062 RANGE_FOR_EXPR (range_for_stmt) = expr;
1063 add_stmt (range_for_stmt);
1064 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1067 /* Finish a break-statement. */
1069 tree
1070 finish_break_stmt (void)
1072 /* In switch statements break is sometimes stylistically used after
1073 a return statement. This can lead to spurious warnings about
1074 control reaching the end of a non-void function when it is
1075 inlined. Note that we are calling block_may_fallthru with
1076 language specific tree nodes; this works because
1077 block_may_fallthru returns true when given something it does not
1078 understand. */
1079 if (!block_may_fallthru (cur_stmt_list))
1080 return void_node;
1081 return add_stmt (build_stmt (input_location, BREAK_STMT));
1084 /* Finish a continue-statement. */
1086 tree
1087 finish_continue_stmt (void)
1089 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1092 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1093 appropriate. */
1095 tree
1096 begin_switch_stmt (void)
1098 tree r, scope;
1100 scope = do_pushlevel (sk_cond);
1101 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1103 begin_cond (&SWITCH_STMT_COND (r));
1105 return r;
1108 /* Finish the cond of a switch-statement. */
1110 void
1111 finish_switch_cond (tree cond, tree switch_stmt)
1113 tree orig_type = NULL;
1114 if (!processing_template_decl)
1116 /* Convert the condition to an integer or enumeration type. */
1117 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1118 if (cond == NULL_TREE)
1120 error ("switch quantity not an integer");
1121 cond = error_mark_node;
1123 /* We want unlowered type here to handle enum bit-fields. */
1124 orig_type = unlowered_expr_type (cond);
1125 if (cond != error_mark_node)
1127 /* Warn if the condition has boolean value. */
1128 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1129 warning_at (input_location, OPT_Wswitch_bool,
1130 "switch condition has type bool");
1132 /* [stmt.switch]
1134 Integral promotions are performed. */
1135 cond = perform_integral_promotions (cond);
1136 cond = maybe_cleanup_point_expr (cond);
1139 if (check_for_bare_parameter_packs (cond))
1140 cond = error_mark_node;
1141 else if (!processing_template_decl && warn_sequence_point)
1142 verify_sequence_points (cond);
1144 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1145 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1146 add_stmt (switch_stmt);
1147 push_switch (switch_stmt);
1148 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1151 /* Finish the body of a switch-statement, which may be given by
1152 SWITCH_STMT. The COND to switch on is indicated. */
1154 void
1155 finish_switch_stmt (tree switch_stmt)
1157 tree scope;
1159 SWITCH_STMT_BODY (switch_stmt) =
1160 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1161 pop_switch ();
1163 scope = SWITCH_STMT_SCOPE (switch_stmt);
1164 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1165 add_stmt (do_poplevel (scope));
1168 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1169 appropriate. */
1171 tree
1172 begin_try_block (void)
1174 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1175 add_stmt (r);
1176 TRY_STMTS (r) = push_stmt_list ();
1177 return r;
1180 /* Likewise, for a function-try-block. The block returned in
1181 *COMPOUND_STMT is an artificial outer scope, containing the
1182 function-try-block. */
1184 tree
1185 begin_function_try_block (tree *compound_stmt)
1187 tree r;
1188 /* This outer scope does not exist in the C++ standard, but we need
1189 a place to put __FUNCTION__ and similar variables. */
1190 *compound_stmt = begin_compound_stmt (0);
1191 r = begin_try_block ();
1192 FN_TRY_BLOCK_P (r) = 1;
1193 return r;
1196 /* Finish a try-block, which may be given by TRY_BLOCK. */
1198 void
1199 finish_try_block (tree try_block)
1201 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1202 TRY_HANDLERS (try_block) = push_stmt_list ();
1205 /* Finish the body of a cleanup try-block, which may be given by
1206 TRY_BLOCK. */
1208 void
1209 finish_cleanup_try_block (tree try_block)
1211 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1214 /* Finish an implicitly generated try-block, with a cleanup is given
1215 by CLEANUP. */
1217 void
1218 finish_cleanup (tree cleanup, tree try_block)
1220 TRY_HANDLERS (try_block) = cleanup;
1221 CLEANUP_P (try_block) = 1;
1224 /* Likewise, for a function-try-block. */
1226 void
1227 finish_function_try_block (tree try_block)
1229 finish_try_block (try_block);
1230 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1231 the try block, but moving it inside. */
1232 in_function_try_handler = 1;
1235 /* Finish a handler-sequence for a try-block, which may be given by
1236 TRY_BLOCK. */
1238 void
1239 finish_handler_sequence (tree try_block)
1241 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1242 check_handlers (TRY_HANDLERS (try_block));
1245 /* Finish the handler-seq for a function-try-block, given by
1246 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1247 begin_function_try_block. */
1249 void
1250 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1252 in_function_try_handler = 0;
1253 finish_handler_sequence (try_block);
1254 finish_compound_stmt (compound_stmt);
1257 /* Begin a handler. Returns a HANDLER if appropriate. */
1259 tree
1260 begin_handler (void)
1262 tree r;
1264 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1265 add_stmt (r);
1267 /* Create a binding level for the eh_info and the exception object
1268 cleanup. */
1269 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1271 return r;
1274 /* Finish the handler-parameters for a handler, which may be given by
1275 HANDLER. DECL is the declaration for the catch parameter, or NULL
1276 if this is a `catch (...)' clause. */
1278 void
1279 finish_handler_parms (tree decl, tree handler)
1281 tree type = NULL_TREE;
1282 if (processing_template_decl)
1284 if (decl)
1286 decl = pushdecl (decl);
1287 decl = push_template_decl (decl);
1288 HANDLER_PARMS (handler) = decl;
1289 type = TREE_TYPE (decl);
1292 else
1293 type = expand_start_catch_block (decl);
1294 HANDLER_TYPE (handler) = type;
1297 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1298 the return value from the matching call to finish_handler_parms. */
1300 void
1301 finish_handler (tree handler)
1303 if (!processing_template_decl)
1304 expand_end_catch_block ();
1305 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1308 /* Begin a compound statement. FLAGS contains some bits that control the
1309 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1310 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1311 block of a function. If BCS_TRY_BLOCK is set, this is the block
1312 created on behalf of a TRY statement. Returns a token to be passed to
1313 finish_compound_stmt. */
1315 tree
1316 begin_compound_stmt (unsigned int flags)
1318 tree r;
1320 if (flags & BCS_NO_SCOPE)
1322 r = push_stmt_list ();
1323 STATEMENT_LIST_NO_SCOPE (r) = 1;
1325 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1326 But, if it's a statement-expression with a scopeless block, there's
1327 nothing to keep, and we don't want to accidentally keep a block
1328 *inside* the scopeless block. */
1329 keep_next_level (false);
1331 else
1332 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1334 /* When processing a template, we need to remember where the braces were,
1335 so that we can set up identical scopes when instantiating the template
1336 later. BIND_EXPR is a handy candidate for this.
1337 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1338 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1339 processing templates. */
1340 if (processing_template_decl)
1342 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1343 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1344 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1345 TREE_SIDE_EFFECTS (r) = 1;
1348 return r;
1351 /* Finish a compound-statement, which is given by STMT. */
1353 void
1354 finish_compound_stmt (tree stmt)
1356 if (TREE_CODE (stmt) == BIND_EXPR)
1358 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1359 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1360 discard the BIND_EXPR so it can be merged with the containing
1361 STATEMENT_LIST. */
1362 if (TREE_CODE (body) == STATEMENT_LIST
1363 && STATEMENT_LIST_HEAD (body) == NULL
1364 && !BIND_EXPR_BODY_BLOCK (stmt)
1365 && !BIND_EXPR_TRY_BLOCK (stmt))
1366 stmt = body;
1367 else
1368 BIND_EXPR_BODY (stmt) = body;
1370 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1371 stmt = pop_stmt_list (stmt);
1372 else
1374 /* Destroy any ObjC "super" receivers that may have been
1375 created. */
1376 objc_clear_super_receiver ();
1378 stmt = do_poplevel (stmt);
1381 /* ??? See c_end_compound_stmt wrt statement expressions. */
1382 add_stmt (stmt);
1385 /* Finish an asm-statement, whose components are a STRING, some
1386 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1387 LABELS. Also note whether the asm-statement should be
1388 considered volatile. */
1390 tree
1391 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1392 tree input_operands, tree clobbers, tree labels)
1394 tree r;
1395 tree t;
1396 int ninputs = list_length (input_operands);
1397 int noutputs = list_length (output_operands);
1399 if (!processing_template_decl)
1401 const char *constraint;
1402 const char **oconstraints;
1403 bool allows_mem, allows_reg, is_inout;
1404 tree operand;
1405 int i;
1407 oconstraints = XALLOCAVEC (const char *, noutputs);
1409 string = resolve_asm_operand_names (string, output_operands,
1410 input_operands, labels);
1412 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1414 operand = TREE_VALUE (t);
1416 /* ??? Really, this should not be here. Users should be using a
1417 proper lvalue, dammit. But there's a long history of using
1418 casts in the output operands. In cases like longlong.h, this
1419 becomes a primitive form of typechecking -- if the cast can be
1420 removed, then the output operand had a type of the proper width;
1421 otherwise we'll get an error. Gross, but ... */
1422 STRIP_NOPS (operand);
1424 operand = mark_lvalue_use (operand);
1426 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1427 operand = error_mark_node;
1429 if (operand != error_mark_node
1430 && (TREE_READONLY (operand)
1431 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1432 /* Functions are not modifiable, even though they are
1433 lvalues. */
1434 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1435 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1436 /* If it's an aggregate and any field is const, then it is
1437 effectively const. */
1438 || (CLASS_TYPE_P (TREE_TYPE (operand))
1439 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1440 cxx_readonly_error (operand, lv_asm);
1442 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1443 oconstraints[i] = constraint;
1445 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1446 &allows_mem, &allows_reg, &is_inout))
1448 /* If the operand is going to end up in memory,
1449 mark it addressable. */
1450 if (!allows_reg && !cxx_mark_addressable (operand))
1451 operand = error_mark_node;
1453 else
1454 operand = error_mark_node;
1456 TREE_VALUE (t) = operand;
1459 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1461 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1462 bool constraint_parsed
1463 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1464 oconstraints, &allows_mem, &allows_reg);
1465 /* If the operand is going to end up in memory, don't call
1466 decay_conversion. */
1467 if (constraint_parsed && !allows_reg && allows_mem)
1468 operand = mark_lvalue_use (TREE_VALUE (t));
1469 else
1470 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1472 /* If the type of the operand hasn't been determined (e.g.,
1473 because it involves an overloaded function), then issue
1474 an error message. There's no context available to
1475 resolve the overloading. */
1476 if (TREE_TYPE (operand) == unknown_type_node)
1478 error ("type of asm operand %qE could not be determined",
1479 TREE_VALUE (t));
1480 operand = error_mark_node;
1483 if (constraint_parsed)
1485 /* If the operand is going to end up in memory,
1486 mark it addressable. */
1487 if (!allows_reg && allows_mem)
1489 /* Strip the nops as we allow this case. FIXME, this really
1490 should be rejected or made deprecated. */
1491 STRIP_NOPS (operand);
1492 if (!cxx_mark_addressable (operand))
1493 operand = error_mark_node;
1495 else if (!allows_reg && !allows_mem)
1497 /* If constraint allows neither register nor memory,
1498 try harder to get a constant. */
1499 tree constop = maybe_constant_value (operand);
1500 if (TREE_CONSTANT (constop))
1501 operand = constop;
1504 else
1505 operand = error_mark_node;
1507 TREE_VALUE (t) = operand;
1511 r = build_stmt (input_location, ASM_EXPR, string,
1512 output_operands, input_operands,
1513 clobbers, labels);
1514 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1515 r = maybe_cleanup_point_expr_void (r);
1516 return add_stmt (r);
1519 /* Finish a label with the indicated NAME. Returns the new label. */
1521 tree
1522 finish_label_stmt (tree name)
1524 tree decl = define_label (input_location, name);
1526 if (decl == error_mark_node)
1527 return error_mark_node;
1529 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1531 return decl;
1534 /* Finish a series of declarations for local labels. G++ allows users
1535 to declare "local" labels, i.e., labels with scope. This extension
1536 is useful when writing code involving statement-expressions. */
1538 void
1539 finish_label_decl (tree name)
1541 if (!at_function_scope_p ())
1543 error ("__label__ declarations are only allowed in function scopes");
1544 return;
1547 add_decl_expr (declare_local_label (name));
1550 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1552 void
1553 finish_decl_cleanup (tree decl, tree cleanup)
1555 push_cleanup (decl, cleanup, false);
1558 /* If the current scope exits with an exception, run CLEANUP. */
1560 void
1561 finish_eh_cleanup (tree cleanup)
1563 push_cleanup (NULL, cleanup, true);
1566 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1567 order they were written by the user. Each node is as for
1568 emit_mem_initializers. */
1570 void
1571 finish_mem_initializers (tree mem_inits)
1573 /* Reorder the MEM_INITS so that they are in the order they appeared
1574 in the source program. */
1575 mem_inits = nreverse (mem_inits);
1577 if (processing_template_decl)
1579 tree mem;
1581 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1583 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1584 check for bare parameter packs in the TREE_VALUE, because
1585 any parameter packs in the TREE_VALUE have already been
1586 bound as part of the TREE_PURPOSE. See
1587 make_pack_expansion for more information. */
1588 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1589 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1590 TREE_VALUE (mem) = error_mark_node;
1593 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1594 CTOR_INITIALIZER, mem_inits));
1596 else
1597 emit_mem_initializers (mem_inits);
1600 /* Obfuscate EXPR if it looks like an id-expression or member access so
1601 that the call to finish_decltype in do_auto_deduction will give the
1602 right result. */
1604 tree
1605 force_paren_expr (tree expr)
1607 /* This is only needed for decltype(auto) in C++14. */
1608 if (cxx_dialect < cxx14)
1609 return expr;
1611 /* If we're in unevaluated context, we can't be deducing a
1612 return/initializer type, so we don't need to mess with this. */
1613 if (cp_unevaluated_operand)
1614 return expr;
1616 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1617 && TREE_CODE (expr) != SCOPE_REF)
1618 return expr;
1620 if (TREE_CODE (expr) == COMPONENT_REF)
1621 REF_PARENTHESIZED_P (expr) = true;
1622 else if (type_dependent_expression_p (expr))
1623 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1624 else
1626 cp_lvalue_kind kind = lvalue_kind (expr);
1627 if ((kind & ~clk_class) != clk_none)
1629 tree type = unlowered_expr_type (expr);
1630 bool rval = !!(kind & clk_rvalueref);
1631 type = cp_build_reference_type (type, rval);
1632 expr = build_static_cast (type, expr, tf_error);
1633 if (expr != error_mark_node)
1634 REF_PARENTHESIZED_P (expr) = true;
1638 return expr;
1641 /* Finish a parenthesized expression EXPR. */
1643 tree
1644 finish_parenthesized_expr (tree expr)
1646 if (EXPR_P (expr))
1647 /* This inhibits warnings in c_common_truthvalue_conversion. */
1648 TREE_NO_WARNING (expr) = 1;
1650 if (TREE_CODE (expr) == OFFSET_REF
1651 || TREE_CODE (expr) == SCOPE_REF)
1652 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1653 enclosed in parentheses. */
1654 PTRMEM_OK_P (expr) = 0;
1656 if (TREE_CODE (expr) == STRING_CST)
1657 PAREN_STRING_LITERAL_P (expr) = 1;
1659 expr = force_paren_expr (expr);
1661 return expr;
1664 /* Finish a reference to a non-static data member (DECL) that is not
1665 preceded by `.' or `->'. */
1667 tree
1668 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1670 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1672 if (!object)
1674 tree scope = qualifying_scope;
1675 if (scope == NULL_TREE)
1676 scope = context_for_name_lookup (decl);
1677 object = maybe_dummy_object (scope, NULL);
1680 object = maybe_resolve_dummy (object, true);
1681 if (object == error_mark_node)
1682 return error_mark_node;
1684 /* DR 613/850: Can use non-static data members without an associated
1685 object in sizeof/decltype/alignof. */
1686 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1687 && (!processing_template_decl || !current_class_ref))
1689 if (current_function_decl
1690 && DECL_STATIC_FUNCTION_P (current_function_decl))
1691 error ("invalid use of member %qD in static member function", decl);
1692 else
1693 error ("invalid use of non-static data member %qD", decl);
1694 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1696 return error_mark_node;
1699 if (current_class_ptr)
1700 TREE_USED (current_class_ptr) = 1;
1701 if (processing_template_decl && !qualifying_scope)
1703 tree type = TREE_TYPE (decl);
1705 if (TREE_CODE (type) == REFERENCE_TYPE)
1706 /* Quals on the object don't matter. */;
1707 else if (PACK_EXPANSION_P (type))
1708 /* Don't bother trying to represent this. */
1709 type = NULL_TREE;
1710 else
1712 /* Set the cv qualifiers. */
1713 int quals = cp_type_quals (TREE_TYPE (object));
1715 if (DECL_MUTABLE_P (decl))
1716 quals &= ~TYPE_QUAL_CONST;
1718 quals |= cp_type_quals (TREE_TYPE (decl));
1719 type = cp_build_qualified_type (type, quals);
1722 return (convert_from_reference
1723 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1725 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1726 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1727 for now. */
1728 else if (processing_template_decl)
1729 return build_qualified_name (TREE_TYPE (decl),
1730 qualifying_scope,
1731 decl,
1732 /*template_p=*/false);
1733 else
1735 tree access_type = TREE_TYPE (object);
1737 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1738 decl, tf_warning_or_error);
1740 /* If the data member was named `C::M', convert `*this' to `C'
1741 first. */
1742 if (qualifying_scope)
1744 tree binfo = NULL_TREE;
1745 object = build_scoped_ref (object, qualifying_scope,
1746 &binfo);
1749 return build_class_member_access_expr (object, decl,
1750 /*access_path=*/NULL_TREE,
1751 /*preserve_reference=*/false,
1752 tf_warning_or_error);
1756 /* If we are currently parsing a template and we encountered a typedef
1757 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1758 adds the typedef to a list tied to the current template.
1759 At template instantiation time, that list is walked and access check
1760 performed for each typedef.
1761 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1763 void
1764 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1765 tree context,
1766 location_t location)
1768 tree template_info = NULL;
1769 tree cs = current_scope ();
1771 if (!is_typedef_decl (typedef_decl)
1772 || !context
1773 || !CLASS_TYPE_P (context)
1774 || !cs)
1775 return;
1777 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1778 template_info = get_template_info (cs);
1780 if (template_info
1781 && TI_TEMPLATE (template_info)
1782 && !currently_open_class (context))
1783 append_type_to_template_for_access_check (cs, typedef_decl,
1784 context, location);
1787 /* DECL was the declaration to which a qualified-id resolved. Issue
1788 an error message if it is not accessible. If OBJECT_TYPE is
1789 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1790 type of `*x', or `x', respectively. If the DECL was named as
1791 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1793 void
1794 check_accessibility_of_qualified_id (tree decl,
1795 tree object_type,
1796 tree nested_name_specifier)
1798 tree scope;
1799 tree qualifying_type = NULL_TREE;
1801 /* If we are parsing a template declaration and if decl is a typedef,
1802 add it to a list tied to the template.
1803 At template instantiation time, that list will be walked and
1804 access check performed. */
1805 add_typedef_to_current_template_for_access_check (decl,
1806 nested_name_specifier
1807 ? nested_name_specifier
1808 : DECL_CONTEXT (decl),
1809 input_location);
1811 /* If we're not checking, return immediately. */
1812 if (deferred_access_no_check)
1813 return;
1815 /* Determine the SCOPE of DECL. */
1816 scope = context_for_name_lookup (decl);
1817 /* If the SCOPE is not a type, then DECL is not a member. */
1818 if (!TYPE_P (scope))
1819 return;
1820 /* Compute the scope through which DECL is being accessed. */
1821 if (object_type
1822 /* OBJECT_TYPE might not be a class type; consider:
1824 class A { typedef int I; };
1825 I *p;
1826 p->A::I::~I();
1828 In this case, we will have "A::I" as the DECL, but "I" as the
1829 OBJECT_TYPE. */
1830 && CLASS_TYPE_P (object_type)
1831 && DERIVED_FROM_P (scope, object_type))
1832 /* If we are processing a `->' or `.' expression, use the type of the
1833 left-hand side. */
1834 qualifying_type = object_type;
1835 else if (nested_name_specifier)
1837 /* If the reference is to a non-static member of the
1838 current class, treat it as if it were referenced through
1839 `this'. */
1840 tree ct;
1841 if (DECL_NONSTATIC_MEMBER_P (decl)
1842 && current_class_ptr
1843 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1844 qualifying_type = ct;
1845 /* Otherwise, use the type indicated by the
1846 nested-name-specifier. */
1847 else
1848 qualifying_type = nested_name_specifier;
1850 else
1851 /* Otherwise, the name must be from the current class or one of
1852 its bases. */
1853 qualifying_type = currently_open_derived_class (scope);
1855 if (qualifying_type
1856 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1857 or similar in a default argument value. */
1858 && CLASS_TYPE_P (qualifying_type)
1859 && !dependent_type_p (qualifying_type))
1860 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1861 decl, tf_warning_or_error);
1864 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1865 class named to the left of the "::" operator. DONE is true if this
1866 expression is a complete postfix-expression; it is false if this
1867 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1868 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1869 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1870 is true iff this qualified name appears as a template argument. */
1872 tree
1873 finish_qualified_id_expr (tree qualifying_class,
1874 tree expr,
1875 bool done,
1876 bool address_p,
1877 bool template_p,
1878 bool template_arg_p,
1879 tsubst_flags_t complain)
1881 gcc_assert (TYPE_P (qualifying_class));
1883 if (error_operand_p (expr))
1884 return error_mark_node;
1886 if ((DECL_P (expr) || BASELINK_P (expr))
1887 && !mark_used (expr, complain))
1888 return error_mark_node;
1890 if (template_p)
1891 check_template_keyword (expr);
1893 /* If EXPR occurs as the operand of '&', use special handling that
1894 permits a pointer-to-member. */
1895 if (address_p && done)
1897 if (TREE_CODE (expr) == SCOPE_REF)
1898 expr = TREE_OPERAND (expr, 1);
1899 expr = build_offset_ref (qualifying_class, expr,
1900 /*address_p=*/true, complain);
1901 return expr;
1904 /* No need to check access within an enum. */
1905 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1906 return expr;
1908 /* Within the scope of a class, turn references to non-static
1909 members into expression of the form "this->...". */
1910 if (template_arg_p)
1911 /* But, within a template argument, we do not want make the
1912 transformation, as there is no "this" pointer. */
1914 else if (TREE_CODE (expr) == FIELD_DECL)
1916 push_deferring_access_checks (dk_no_check);
1917 expr = finish_non_static_data_member (expr, NULL_TREE,
1918 qualifying_class);
1919 pop_deferring_access_checks ();
1921 else if (BASELINK_P (expr) && !processing_template_decl)
1923 /* See if any of the functions are non-static members. */
1924 /* If so, the expression may be relative to 'this'. */
1925 if (!shared_member_p (expr)
1926 && current_class_ptr
1927 && DERIVED_FROM_P (qualifying_class,
1928 current_nonlambda_class_type ()))
1929 expr = (build_class_member_access_expr
1930 (maybe_dummy_object (qualifying_class, NULL),
1931 expr,
1932 BASELINK_ACCESS_BINFO (expr),
1933 /*preserve_reference=*/false,
1934 complain));
1935 else if (done)
1936 /* The expression is a qualified name whose address is not
1937 being taken. */
1938 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1939 complain);
1941 else if (BASELINK_P (expr))
1943 else
1945 /* In a template, return a SCOPE_REF for most qualified-ids
1946 so that we can check access at instantiation time. But if
1947 we're looking at a member of the current instantiation, we
1948 know we have access and building up the SCOPE_REF confuses
1949 non-type template argument handling. */
1950 if (processing_template_decl
1951 && !currently_open_class (qualifying_class))
1952 expr = build_qualified_name (TREE_TYPE (expr),
1953 qualifying_class, expr,
1954 template_p);
1956 expr = convert_from_reference (expr);
1959 return expr;
1962 /* Begin a statement-expression. The value returned must be passed to
1963 finish_stmt_expr. */
1965 tree
1966 begin_stmt_expr (void)
1968 return push_stmt_list ();
1971 /* Process the final expression of a statement expression. EXPR can be
1972 NULL, if the final expression is empty. Return a STATEMENT_LIST
1973 containing all the statements in the statement-expression, or
1974 ERROR_MARK_NODE if there was an error. */
1976 tree
1977 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1979 if (error_operand_p (expr))
1981 /* The type of the statement-expression is the type of the last
1982 expression. */
1983 TREE_TYPE (stmt_expr) = error_mark_node;
1984 return error_mark_node;
1987 /* If the last statement does not have "void" type, then the value
1988 of the last statement is the value of the entire expression. */
1989 if (expr)
1991 tree type = TREE_TYPE (expr);
1993 if (processing_template_decl)
1995 expr = build_stmt (input_location, EXPR_STMT, expr);
1996 expr = add_stmt (expr);
1997 /* Mark the last statement so that we can recognize it as such at
1998 template-instantiation time. */
1999 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2001 else if (VOID_TYPE_P (type))
2003 /* Just treat this like an ordinary statement. */
2004 expr = finish_expr_stmt (expr);
2006 else
2008 /* It actually has a value we need to deal with. First, force it
2009 to be an rvalue so that we won't need to build up a copy
2010 constructor call later when we try to assign it to something. */
2011 expr = force_rvalue (expr, tf_warning_or_error);
2012 if (error_operand_p (expr))
2013 return error_mark_node;
2015 /* Update for array-to-pointer decay. */
2016 type = TREE_TYPE (expr);
2018 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2019 normal statement, but don't convert to void or actually add
2020 the EXPR_STMT. */
2021 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2022 expr = maybe_cleanup_point_expr (expr);
2023 add_stmt (expr);
2026 /* The type of the statement-expression is the type of the last
2027 expression. */
2028 TREE_TYPE (stmt_expr) = type;
2031 return stmt_expr;
2034 /* Finish a statement-expression. EXPR should be the value returned
2035 by the previous begin_stmt_expr. Returns an expression
2036 representing the statement-expression. */
2038 tree
2039 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2041 tree type;
2042 tree result;
2044 if (error_operand_p (stmt_expr))
2046 pop_stmt_list (stmt_expr);
2047 return error_mark_node;
2050 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2052 type = TREE_TYPE (stmt_expr);
2053 result = pop_stmt_list (stmt_expr);
2054 TREE_TYPE (result) = type;
2056 if (processing_template_decl)
2058 result = build_min (STMT_EXPR, type, result);
2059 TREE_SIDE_EFFECTS (result) = 1;
2060 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2062 else if (CLASS_TYPE_P (type))
2064 /* Wrap the statement-expression in a TARGET_EXPR so that the
2065 temporary object created by the final expression is destroyed at
2066 the end of the full-expression containing the
2067 statement-expression. */
2068 result = force_target_expr (type, result, tf_warning_or_error);
2071 return result;
2074 /* Returns the expression which provides the value of STMT_EXPR. */
2076 tree
2077 stmt_expr_value_expr (tree stmt_expr)
2079 tree t = STMT_EXPR_STMT (stmt_expr);
2081 if (TREE_CODE (t) == BIND_EXPR)
2082 t = BIND_EXPR_BODY (t);
2084 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2085 t = STATEMENT_LIST_TAIL (t)->stmt;
2087 if (TREE_CODE (t) == EXPR_STMT)
2088 t = EXPR_STMT_EXPR (t);
2090 return t;
2093 /* Return TRUE iff EXPR_STMT is an empty list of
2094 expression statements. */
2096 bool
2097 empty_expr_stmt_p (tree expr_stmt)
2099 tree body = NULL_TREE;
2101 if (expr_stmt == void_node)
2102 return true;
2104 if (expr_stmt)
2106 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2107 body = EXPR_STMT_EXPR (expr_stmt);
2108 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2109 body = expr_stmt;
2112 if (body)
2114 if (TREE_CODE (body) == STATEMENT_LIST)
2115 return tsi_end_p (tsi_start (body));
2116 else
2117 return empty_expr_stmt_p (body);
2119 return false;
2122 /* Perform Koenig lookup. FN is the postfix-expression representing
2123 the function (or functions) to call; ARGS are the arguments to the
2124 call. Returns the functions to be considered by overload resolution. */
2126 tree
2127 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2128 tsubst_flags_t complain)
2130 tree identifier = NULL_TREE;
2131 tree functions = NULL_TREE;
2132 tree tmpl_args = NULL_TREE;
2133 bool template_id = false;
2135 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2137 /* Use a separate flag to handle null args. */
2138 template_id = true;
2139 tmpl_args = TREE_OPERAND (fn, 1);
2140 fn = TREE_OPERAND (fn, 0);
2143 /* Find the name of the overloaded function. */
2144 if (identifier_p (fn))
2145 identifier = fn;
2146 else if (is_overloaded_fn (fn))
2148 functions = fn;
2149 identifier = DECL_NAME (get_first_fn (functions));
2151 else if (DECL_P (fn))
2153 functions = fn;
2154 identifier = DECL_NAME (fn);
2157 /* A call to a namespace-scope function using an unqualified name.
2159 Do Koenig lookup -- unless any of the arguments are
2160 type-dependent. */
2161 if (!any_type_dependent_arguments_p (args)
2162 && !any_dependent_template_arguments_p (tmpl_args))
2164 fn = lookup_arg_dependent (identifier, functions, args);
2165 if (!fn)
2167 /* The unqualified name could not be resolved. */
2168 if (complain)
2169 fn = unqualified_fn_lookup_error (identifier);
2170 else
2171 fn = identifier;
2175 if (fn && template_id)
2176 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2178 return fn;
2181 /* Generate an expression for `FN (ARGS)'. This may change the
2182 contents of ARGS.
2184 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2185 as a virtual call, even if FN is virtual. (This flag is set when
2186 encountering an expression where the function name is explicitly
2187 qualified. For example a call to `X::f' never generates a virtual
2188 call.)
2190 Returns code for the call. */
2192 tree
2193 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2194 bool koenig_p, tsubst_flags_t complain)
2196 tree result;
2197 tree orig_fn;
2198 vec<tree, va_gc> *orig_args = NULL;
2200 if (fn == error_mark_node)
2201 return error_mark_node;
2203 gcc_assert (!TYPE_P (fn));
2205 orig_fn = fn;
2207 if (processing_template_decl)
2209 /* If the call expression is dependent, build a CALL_EXPR node
2210 with no type; type_dependent_expression_p recognizes
2211 expressions with no type as being dependent. */
2212 if (type_dependent_expression_p (fn)
2213 || any_type_dependent_arguments_p (*args)
2214 /* For a non-static member function that doesn't have an
2215 explicit object argument, we need to specifically
2216 test the type dependency of the "this" pointer because it
2217 is not included in *ARGS even though it is considered to
2218 be part of the list of arguments. Note that this is
2219 related to CWG issues 515 and 1005. */
2220 || (TREE_CODE (fn) != COMPONENT_REF
2221 && non_static_member_function_p (fn)
2222 && current_class_ref
2223 && type_dependent_expression_p (current_class_ref)))
2225 result = build_nt_call_vec (fn, *args);
2226 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2227 KOENIG_LOOKUP_P (result) = koenig_p;
2228 if (cfun)
2232 tree fndecl = OVL_CURRENT (fn);
2233 if (TREE_CODE (fndecl) != FUNCTION_DECL
2234 || !TREE_THIS_VOLATILE (fndecl))
2235 break;
2236 fn = OVL_NEXT (fn);
2238 while (fn);
2239 if (!fn)
2240 current_function_returns_abnormally = 1;
2242 return result;
2244 orig_args = make_tree_vector_copy (*args);
2245 if (!BASELINK_P (fn)
2246 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2247 && TREE_TYPE (fn) != unknown_type_node)
2248 fn = build_non_dependent_expr (fn);
2249 make_args_non_dependent (*args);
2252 if (TREE_CODE (fn) == COMPONENT_REF)
2254 tree member = TREE_OPERAND (fn, 1);
2255 if (BASELINK_P (member))
2257 tree object = TREE_OPERAND (fn, 0);
2258 return build_new_method_call (object, member,
2259 args, NULL_TREE,
2260 (disallow_virtual
2261 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2262 : LOOKUP_NORMAL),
2263 /*fn_p=*/NULL,
2264 complain);
2268 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2269 if (TREE_CODE (fn) == ADDR_EXPR
2270 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2271 fn = TREE_OPERAND (fn, 0);
2273 if (is_overloaded_fn (fn))
2274 fn = baselink_for_fns (fn);
2276 result = NULL_TREE;
2277 if (BASELINK_P (fn))
2279 tree object;
2281 /* A call to a member function. From [over.call.func]:
2283 If the keyword this is in scope and refers to the class of
2284 that member function, or a derived class thereof, then the
2285 function call is transformed into a qualified function call
2286 using (*this) as the postfix-expression to the left of the
2287 . operator.... [Otherwise] a contrived object of type T
2288 becomes the implied object argument.
2290 In this situation:
2292 struct A { void f(); };
2293 struct B : public A {};
2294 struct C : public A { void g() { B::f(); }};
2296 "the class of that member function" refers to `A'. But 11.2
2297 [class.access.base] says that we need to convert 'this' to B* as
2298 part of the access, so we pass 'B' to maybe_dummy_object. */
2300 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2301 NULL);
2303 if (processing_template_decl)
2305 if (type_dependent_expression_p (object))
2307 tree ret = build_nt_call_vec (orig_fn, orig_args);
2308 release_tree_vector (orig_args);
2309 return ret;
2311 object = build_non_dependent_expr (object);
2314 result = build_new_method_call (object, fn, args, NULL_TREE,
2315 (disallow_virtual
2316 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2317 : LOOKUP_NORMAL),
2318 /*fn_p=*/NULL,
2319 complain);
2321 else if (is_overloaded_fn (fn))
2323 /* If the function is an overloaded builtin, resolve it. */
2324 if (TREE_CODE (fn) == FUNCTION_DECL
2325 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2326 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2327 result = resolve_overloaded_builtin (input_location, fn, *args);
2329 if (!result)
2331 if (warn_sizeof_pointer_memaccess
2332 && !vec_safe_is_empty (*args)
2333 && !processing_template_decl)
2335 location_t sizeof_arg_loc[3];
2336 tree sizeof_arg[3];
2337 unsigned int i;
2338 for (i = 0; i < 3; i++)
2340 tree t;
2342 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2343 sizeof_arg[i] = NULL_TREE;
2344 if (i >= (*args)->length ())
2345 continue;
2346 t = (**args)[i];
2347 if (TREE_CODE (t) != SIZEOF_EXPR)
2348 continue;
2349 if (SIZEOF_EXPR_TYPE_P (t))
2350 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2351 else
2352 sizeof_arg[i] = TREE_OPERAND (t, 0);
2353 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2355 sizeof_pointer_memaccess_warning
2356 (sizeof_arg_loc, fn, *args,
2357 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2360 /* A call to a namespace-scope function. */
2361 result = build_new_function_call (fn, args, koenig_p, complain);
2364 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2366 if (!vec_safe_is_empty (*args))
2367 error ("arguments to destructor are not allowed");
2368 /* Mark the pseudo-destructor call as having side-effects so
2369 that we do not issue warnings about its use. */
2370 result = build1 (NOP_EXPR,
2371 void_type_node,
2372 TREE_OPERAND (fn, 0));
2373 TREE_SIDE_EFFECTS (result) = 1;
2375 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2376 /* If the "function" is really an object of class type, it might
2377 have an overloaded `operator ()'. */
2378 result = build_op_call (fn, args, complain);
2380 if (!result)
2381 /* A call where the function is unknown. */
2382 result = cp_build_function_call_vec (fn, args, complain);
2384 if (processing_template_decl && result != error_mark_node)
2386 if (INDIRECT_REF_P (result))
2387 result = TREE_OPERAND (result, 0);
2388 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2389 SET_EXPR_LOCATION (result, input_location);
2390 KOENIG_LOOKUP_P (result) = koenig_p;
2391 release_tree_vector (orig_args);
2392 result = convert_from_reference (result);
2395 if (koenig_p)
2397 /* Free garbage OVERLOADs from arg-dependent lookup. */
2398 tree next = NULL_TREE;
2399 for (fn = orig_fn;
2400 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2401 fn = next)
2403 if (processing_template_decl)
2404 /* In a template, we'll re-use them at instantiation time. */
2405 OVL_ARG_DEPENDENT (fn) = false;
2406 else
2408 next = OVL_CHAIN (fn);
2409 ggc_free (fn);
2414 return result;
2417 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
2419 tree
2420 finish_template_variable (tree var)
2422 return instantiate_template (TREE_OPERAND (var, 0), TREE_OPERAND (var, 1),
2423 tf_error);
2426 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2427 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2428 POSTDECREMENT_EXPR.) */
2430 tree
2431 finish_increment_expr (tree expr, enum tree_code code)
2433 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2436 /* Finish a use of `this'. Returns an expression for `this'. */
2438 tree
2439 finish_this_expr (void)
2441 tree result = NULL_TREE;
2443 if (current_class_ptr)
2445 tree type = TREE_TYPE (current_class_ref);
2447 /* In a lambda expression, 'this' refers to the captured 'this'. */
2448 if (LAMBDA_TYPE_P (type))
2449 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2450 else
2451 result = current_class_ptr;
2454 if (result)
2455 /* The keyword 'this' is a prvalue expression. */
2456 return rvalue (result);
2458 tree fn = current_nonlambda_function ();
2459 if (fn && DECL_STATIC_FUNCTION_P (fn))
2460 error ("%<this%> is unavailable for static member functions");
2461 else if (fn)
2462 error ("invalid use of %<this%> in non-member function");
2463 else
2464 error ("invalid use of %<this%> at top level");
2465 return error_mark_node;
2468 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2469 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2470 the TYPE for the type given. If SCOPE is non-NULL, the expression
2471 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2473 tree
2474 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2475 location_t loc)
2477 if (object == error_mark_node || destructor == error_mark_node)
2478 return error_mark_node;
2480 gcc_assert (TYPE_P (destructor));
2482 if (!processing_template_decl)
2484 if (scope == error_mark_node)
2486 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2487 return error_mark_node;
2489 if (is_auto (destructor))
2490 destructor = TREE_TYPE (object);
2491 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2493 error_at (loc,
2494 "qualified type %qT does not match destructor name ~%qT",
2495 scope, destructor);
2496 return error_mark_node;
2500 /* [expr.pseudo] says both:
2502 The type designated by the pseudo-destructor-name shall be
2503 the same as the object type.
2505 and:
2507 The cv-unqualified versions of the object type and of the
2508 type designated by the pseudo-destructor-name shall be the
2509 same type.
2511 We implement the more generous second sentence, since that is
2512 what most other compilers do. */
2513 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2514 destructor))
2516 error_at (loc, "%qE is not of type %qT", object, destructor);
2517 return error_mark_node;
2521 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2522 scope, destructor);
2525 /* Finish an expression of the form CODE EXPR. */
2527 tree
2528 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2529 tsubst_flags_t complain)
2531 tree result = build_x_unary_op (loc, code, expr, complain);
2532 if ((complain & tf_warning)
2533 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2534 overflow_warning (input_location, result);
2536 return result;
2539 /* Finish a compound-literal expression. TYPE is the type to which
2540 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2542 tree
2543 finish_compound_literal (tree type, tree compound_literal,
2544 tsubst_flags_t complain)
2546 if (type == error_mark_node)
2547 return error_mark_node;
2549 if (TREE_CODE (type) == REFERENCE_TYPE)
2551 compound_literal
2552 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2553 complain);
2554 return cp_build_c_cast (type, compound_literal, complain);
2557 if (!TYPE_OBJ_P (type))
2559 if (complain & tf_error)
2560 error ("compound literal of non-object type %qT", type);
2561 return error_mark_node;
2564 if (processing_template_decl)
2566 TREE_TYPE (compound_literal) = type;
2567 /* Mark the expression as a compound literal. */
2568 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2569 return compound_literal;
2572 type = complete_type (type);
2574 if (TYPE_NON_AGGREGATE_CLASS (type))
2576 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2577 everywhere that deals with function arguments would be a pain, so
2578 just wrap it in a TREE_LIST. The parser set a flag so we know
2579 that it came from T{} rather than T({}). */
2580 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2581 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2582 return build_functional_cast (type, compound_literal, complain);
2585 if (TREE_CODE (type) == ARRAY_TYPE
2586 && check_array_initializer (NULL_TREE, type, compound_literal))
2587 return error_mark_node;
2588 compound_literal = reshape_init (type, compound_literal, complain);
2589 if (SCALAR_TYPE_P (type)
2590 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2591 && !check_narrowing (type, compound_literal, complain))
2592 return error_mark_node;
2593 if (TREE_CODE (type) == ARRAY_TYPE
2594 && TYPE_DOMAIN (type) == NULL_TREE)
2596 cp_complete_array_type_or_error (&type, compound_literal,
2597 false, complain);
2598 if (type == error_mark_node)
2599 return error_mark_node;
2601 compound_literal = digest_init (type, compound_literal, complain);
2602 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2603 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2604 /* Put static/constant array temporaries in static variables, but always
2605 represent class temporaries with TARGET_EXPR so we elide copies. */
2606 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2607 && TREE_CODE (type) == ARRAY_TYPE
2608 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2609 && initializer_constant_valid_p (compound_literal, type))
2611 tree decl = create_temporary_var (type);
2612 DECL_INITIAL (decl) = compound_literal;
2613 TREE_STATIC (decl) = 1;
2614 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2616 /* 5.19 says that a constant expression can include an
2617 lvalue-rvalue conversion applied to "a glvalue of literal type
2618 that refers to a non-volatile temporary object initialized
2619 with a constant expression". Rather than try to communicate
2620 that this VAR_DECL is a temporary, just mark it constexpr. */
2621 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2622 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2623 TREE_CONSTANT (decl) = true;
2625 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2626 decl = pushdecl_top_level (decl);
2627 DECL_NAME (decl) = make_anon_name ();
2628 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2629 /* Make sure the destructor is callable. */
2630 tree clean = cxx_maybe_build_cleanup (decl, complain);
2631 if (clean == error_mark_node)
2632 return error_mark_node;
2633 return decl;
2635 else
2636 return get_target_expr_sfinae (compound_literal, complain);
2639 /* Return the declaration for the function-name variable indicated by
2640 ID. */
2642 tree
2643 finish_fname (tree id)
2645 tree decl;
2647 decl = fname_decl (input_location, C_RID_CODE (id), id);
2648 if (processing_template_decl && current_function_decl
2649 && decl != error_mark_node)
2650 decl = DECL_NAME (decl);
2651 return decl;
2654 /* Finish a translation unit. */
2656 void
2657 finish_translation_unit (void)
2659 /* In case there were missing closebraces,
2660 get us back to the global binding level. */
2661 pop_everything ();
2662 while (current_namespace != global_namespace)
2663 pop_namespace ();
2665 /* Do file scope __FUNCTION__ et al. */
2666 finish_fname_decls ();
2669 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2670 Returns the parameter. */
2672 tree
2673 finish_template_type_parm (tree aggr, tree identifier)
2675 if (aggr != class_type_node)
2677 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2678 aggr = class_type_node;
2681 return build_tree_list (aggr, identifier);
2684 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2685 Returns the parameter. */
2687 tree
2688 finish_template_template_parm (tree aggr, tree identifier)
2690 tree decl = build_decl (input_location,
2691 TYPE_DECL, identifier, NULL_TREE);
2692 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2693 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2694 DECL_TEMPLATE_RESULT (tmpl) = decl;
2695 DECL_ARTIFICIAL (decl) = 1;
2696 end_template_decl ();
2698 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2700 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2701 /*is_primary=*/true, /*is_partial=*/false,
2702 /*is_friend=*/0);
2704 return finish_template_type_parm (aggr, tmpl);
2707 /* ARGUMENT is the default-argument value for a template template
2708 parameter. If ARGUMENT is invalid, issue error messages and return
2709 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2711 tree
2712 check_template_template_default_arg (tree argument)
2714 if (TREE_CODE (argument) != TEMPLATE_DECL
2715 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2716 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2718 if (TREE_CODE (argument) == TYPE_DECL)
2719 error ("invalid use of type %qT as a default value for a template "
2720 "template-parameter", TREE_TYPE (argument));
2721 else
2722 error ("invalid default argument for a template template parameter");
2723 return error_mark_node;
2726 return argument;
2729 /* Begin a class definition, as indicated by T. */
2731 tree
2732 begin_class_definition (tree t)
2734 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2735 return error_mark_node;
2737 if (processing_template_parmlist)
2739 error ("definition of %q#T inside template parameter list", t);
2740 return error_mark_node;
2743 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2744 are passed the same as decimal scalar types. */
2745 if (TREE_CODE (t) == RECORD_TYPE
2746 && !processing_template_decl)
2748 tree ns = TYPE_CONTEXT (t);
2749 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2750 && DECL_CONTEXT (ns) == std_node
2751 && DECL_NAME (ns)
2752 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2754 const char *n = TYPE_NAME_STRING (t);
2755 if ((strcmp (n, "decimal32") == 0)
2756 || (strcmp (n, "decimal64") == 0)
2757 || (strcmp (n, "decimal128") == 0))
2758 TYPE_TRANSPARENT_AGGR (t) = 1;
2762 /* A non-implicit typename comes from code like:
2764 template <typename T> struct A {
2765 template <typename U> struct A<T>::B ...
2767 This is erroneous. */
2768 else if (TREE_CODE (t) == TYPENAME_TYPE)
2770 error ("invalid definition of qualified type %qT", t);
2771 t = error_mark_node;
2774 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2776 t = make_class_type (RECORD_TYPE);
2777 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2780 if (TYPE_BEING_DEFINED (t))
2782 t = make_class_type (TREE_CODE (t));
2783 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2785 maybe_process_partial_specialization (t);
2786 pushclass (t);
2787 TYPE_BEING_DEFINED (t) = 1;
2788 class_binding_level->defining_class_p = 1;
2790 if (flag_pack_struct)
2792 tree v;
2793 TYPE_PACKED (t) = 1;
2794 /* Even though the type is being defined for the first time
2795 here, there might have been a forward declaration, so there
2796 might be cv-qualified variants of T. */
2797 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2798 TYPE_PACKED (v) = 1;
2800 /* Reset the interface data, at the earliest possible
2801 moment, as it might have been set via a class foo;
2802 before. */
2803 if (! TYPE_ANONYMOUS_P (t))
2805 struct c_fileinfo *finfo = \
2806 get_fileinfo (LOCATION_FILE (input_location));
2807 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2808 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2809 (t, finfo->interface_unknown);
2811 reset_specialization();
2813 /* Make a declaration for this class in its own scope. */
2814 build_self_reference ();
2816 return t;
2819 /* Finish the member declaration given by DECL. */
2821 void
2822 finish_member_declaration (tree decl)
2824 if (decl == error_mark_node || decl == NULL_TREE)
2825 return;
2827 if (decl == void_type_node)
2828 /* The COMPONENT was a friend, not a member, and so there's
2829 nothing for us to do. */
2830 return;
2832 /* We should see only one DECL at a time. */
2833 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2835 /* Set up access control for DECL. */
2836 TREE_PRIVATE (decl)
2837 = (current_access_specifier == access_private_node);
2838 TREE_PROTECTED (decl)
2839 = (current_access_specifier == access_protected_node);
2840 if (TREE_CODE (decl) == TEMPLATE_DECL)
2842 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2843 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2846 /* Mark the DECL as a member of the current class, unless it's
2847 a member of an enumeration. */
2848 if (TREE_CODE (decl) != CONST_DECL)
2849 DECL_CONTEXT (decl) = current_class_type;
2851 /* Check for bare parameter packs in the member variable declaration. */
2852 if (TREE_CODE (decl) == FIELD_DECL)
2854 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2855 TREE_TYPE (decl) = error_mark_node;
2856 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2857 DECL_ATTRIBUTES (decl) = NULL_TREE;
2860 /* [dcl.link]
2862 A C language linkage is ignored for the names of class members
2863 and the member function type of class member functions. */
2864 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2865 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2867 /* Put functions on the TYPE_METHODS list and everything else on the
2868 TYPE_FIELDS list. Note that these are built up in reverse order.
2869 We reverse them (to obtain declaration order) in finish_struct. */
2870 if (DECL_DECLARES_FUNCTION_P (decl))
2872 /* We also need to add this function to the
2873 CLASSTYPE_METHOD_VEC. */
2874 if (add_method (current_class_type, decl, NULL_TREE))
2876 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2877 TYPE_METHODS (current_class_type) = decl;
2879 maybe_add_class_template_decl_list (current_class_type, decl,
2880 /*friend_p=*/0);
2883 /* Enter the DECL into the scope of the class, if the class
2884 isn't a closure (whose fields are supposed to be unnamed). */
2885 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2886 || pushdecl_class_level (decl))
2888 if (TREE_CODE (decl) == USING_DECL)
2890 /* For now, ignore class-scope USING_DECLS, so that
2891 debugging backends do not see them. */
2892 DECL_IGNORED_P (decl) = 1;
2895 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2896 go at the beginning. The reason is that lookup_field_1
2897 searches the list in order, and we want a field name to
2898 override a type name so that the "struct stat hack" will
2899 work. In particular:
2901 struct S { enum E { }; int E } s;
2902 s.E = 3;
2904 is valid. In addition, the FIELD_DECLs must be maintained in
2905 declaration order so that class layout works as expected.
2906 However, we don't need that order until class layout, so we
2907 save a little time by putting FIELD_DECLs on in reverse order
2908 here, and then reversing them in finish_struct_1. (We could
2909 also keep a pointer to the correct insertion points in the
2910 list.) */
2912 if (TREE_CODE (decl) == TYPE_DECL)
2913 TYPE_FIELDS (current_class_type)
2914 = chainon (TYPE_FIELDS (current_class_type), decl);
2915 else
2917 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2918 TYPE_FIELDS (current_class_type) = decl;
2921 maybe_add_class_template_decl_list (current_class_type, decl,
2922 /*friend_p=*/0);
2925 if (pch_file)
2926 note_decl_for_pch (decl);
2929 /* DECL has been declared while we are building a PCH file. Perform
2930 actions that we might normally undertake lazily, but which can be
2931 performed now so that they do not have to be performed in
2932 translation units which include the PCH file. */
2934 void
2935 note_decl_for_pch (tree decl)
2937 gcc_assert (pch_file);
2939 /* There's a good chance that we'll have to mangle names at some
2940 point, even if only for emission in debugging information. */
2941 if (VAR_OR_FUNCTION_DECL_P (decl)
2942 && !processing_template_decl)
2943 mangle_decl (decl);
2946 /* Finish processing a complete template declaration. The PARMS are
2947 the template parameters. */
2949 void
2950 finish_template_decl (tree parms)
2952 if (parms)
2953 end_template_decl ();
2954 else
2955 end_specialization ();
2958 /* Finish processing a template-id (which names a type) of the form
2959 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2960 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2961 the scope of template-id indicated. */
2963 tree
2964 finish_template_type (tree name, tree args, int entering_scope)
2966 tree type;
2968 type = lookup_template_class (name, args,
2969 NULL_TREE, NULL_TREE, entering_scope,
2970 tf_warning_or_error | tf_user);
2971 if (type == error_mark_node)
2972 return type;
2973 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2974 return TYPE_STUB_DECL (type);
2975 else
2976 return TYPE_NAME (type);
2979 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2980 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2981 BASE_CLASS, or NULL_TREE if an error occurred. The
2982 ACCESS_SPECIFIER is one of
2983 access_{default,public,protected_private}_node. For a virtual base
2984 we set TREE_TYPE. */
2986 tree
2987 finish_base_specifier (tree base, tree access, bool virtual_p)
2989 tree result;
2991 if (base == error_mark_node)
2993 error ("invalid base-class specification");
2994 result = NULL_TREE;
2996 else if (! MAYBE_CLASS_TYPE_P (base))
2998 error ("%qT is not a class type", base);
2999 result = NULL_TREE;
3001 else
3003 if (cp_type_quals (base) != 0)
3005 /* DR 484: Can a base-specifier name a cv-qualified
3006 class type? */
3007 base = TYPE_MAIN_VARIANT (base);
3009 result = build_tree_list (access, base);
3010 if (virtual_p)
3011 TREE_TYPE (result) = integer_type_node;
3014 return result;
3017 /* If FNS is a member function, a set of member functions, or a
3018 template-id referring to one or more member functions, return a
3019 BASELINK for FNS, incorporating the current access context.
3020 Otherwise, return FNS unchanged. */
3022 tree
3023 baselink_for_fns (tree fns)
3025 tree scope;
3026 tree cl;
3028 if (BASELINK_P (fns)
3029 || error_operand_p (fns))
3030 return fns;
3032 scope = ovl_scope (fns);
3033 if (!CLASS_TYPE_P (scope))
3034 return fns;
3036 cl = currently_open_derived_class (scope);
3037 if (!cl)
3038 cl = scope;
3039 cl = TYPE_BINFO (cl);
3040 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3043 /* Returns true iff DECL is a variable from a function outside
3044 the current one. */
3046 static bool
3047 outer_var_p (tree decl)
3049 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3050 && DECL_FUNCTION_SCOPE_P (decl)
3051 && (DECL_CONTEXT (decl) != current_function_decl
3052 || parsing_nsdmi ()));
3055 /* As above, but also checks that DECL is automatic. */
3057 bool
3058 outer_automatic_var_p (tree decl)
3060 return (outer_var_p (decl)
3061 && !TREE_STATIC (decl));
3064 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3065 rewrite it for lambda capture. */
3067 tree
3068 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3070 if (cp_unevaluated_operand)
3071 /* It's not a use (3.2) if we're in an unevaluated context. */
3072 return decl;
3074 tree context = DECL_CONTEXT (decl);
3075 tree containing_function = current_function_decl;
3076 tree lambda_stack = NULL_TREE;
3077 tree lambda_expr = NULL_TREE;
3078 tree initializer = convert_from_reference (decl);
3080 /* Mark it as used now even if the use is ill-formed. */
3081 mark_used (decl);
3083 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3084 support for an approach in which a reference to a local
3085 [constant] automatic variable in a nested class or lambda body
3086 would enter the expression as an rvalue, which would reduce
3087 the complexity of the problem"
3089 FIXME update for final resolution of core issue 696. */
3090 if (decl_maybe_constant_var_p (decl))
3092 if (processing_template_decl)
3093 /* In a template, the constant value may not be in a usable
3094 form, so wait until instantiation time. */
3095 return decl;
3096 else if (decl_constant_var_p (decl))
3097 return integral_constant_value (decl);
3100 if (parsing_nsdmi ())
3101 containing_function = NULL_TREE;
3102 else
3103 /* If we are in a lambda function, we can move out until we hit
3104 1. the context,
3105 2. a non-lambda function, or
3106 3. a non-default capturing lambda function. */
3107 while (context != containing_function
3108 && LAMBDA_FUNCTION_P (containing_function))
3110 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3111 (DECL_CONTEXT (containing_function));
3113 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3114 == CPLD_NONE)
3115 break;
3117 lambda_stack = tree_cons (NULL_TREE,
3118 lambda_expr,
3119 lambda_stack);
3121 containing_function
3122 = decl_function_context (containing_function);
3125 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3126 && DECL_ANON_UNION_VAR_P (decl))
3128 if (complain & tf_error)
3129 error ("cannot capture member %qD of anonymous union", decl);
3130 return error_mark_node;
3132 if (context == containing_function)
3134 decl = add_default_capture (lambda_stack,
3135 /*id=*/DECL_NAME (decl),
3136 initializer);
3138 else if (lambda_expr)
3140 if (complain & tf_error)
3141 error ("%qD is not captured", decl);
3142 return error_mark_node;
3144 else
3146 if (complain & tf_error)
3147 error (VAR_P (decl)
3148 ? G_("use of local variable with automatic storage from containing function")
3149 : G_("use of parameter from containing function"));
3150 inform (input_location, "%q+#D declared here", decl);
3151 return error_mark_node;
3153 return decl;
3156 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3157 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3158 if non-NULL, is the type or namespace used to explicitly qualify
3159 ID_EXPRESSION. DECL is the entity to which that name has been
3160 resolved.
3162 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3163 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3164 be set to true if this expression isn't permitted in a
3165 constant-expression, but it is otherwise not set by this function.
3166 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3167 constant-expression, but a non-constant expression is also
3168 permissible.
3170 DONE is true if this expression is a complete postfix-expression;
3171 it is false if this expression is followed by '->', '[', '(', etc.
3172 ADDRESS_P is true iff this expression is the operand of '&'.
3173 TEMPLATE_P is true iff the qualified-id was of the form
3174 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3175 appears as a template argument.
3177 If an error occurs, and it is the kind of error that might cause
3178 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3179 is the caller's responsibility to issue the message. *ERROR_MSG
3180 will be a string with static storage duration, so the caller need
3181 not "free" it.
3183 Return an expression for the entity, after issuing appropriate
3184 diagnostics. This function is also responsible for transforming a
3185 reference to a non-static member into a COMPONENT_REF that makes
3186 the use of "this" explicit.
3188 Upon return, *IDK will be filled in appropriately. */
3189 tree
3190 finish_id_expression (tree id_expression,
3191 tree decl,
3192 tree scope,
3193 cp_id_kind *idk,
3194 bool integral_constant_expression_p,
3195 bool allow_non_integral_constant_expression_p,
3196 bool *non_integral_constant_expression_p,
3197 bool template_p,
3198 bool done,
3199 bool address_p,
3200 bool template_arg_p,
3201 const char **error_msg,
3202 location_t location)
3204 decl = strip_using_decl (decl);
3206 /* Initialize the output parameters. */
3207 *idk = CP_ID_KIND_NONE;
3208 *error_msg = NULL;
3210 if (id_expression == error_mark_node)
3211 return error_mark_node;
3212 /* If we have a template-id, then no further lookup is
3213 required. If the template-id was for a template-class, we
3214 will sometimes have a TYPE_DECL at this point. */
3215 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3216 || TREE_CODE (decl) == TYPE_DECL)
3218 /* Look up the name. */
3219 else
3221 if (decl == error_mark_node)
3223 /* Name lookup failed. */
3224 if (scope
3225 && (!TYPE_P (scope)
3226 || (!dependent_type_p (scope)
3227 && !(identifier_p (id_expression)
3228 && IDENTIFIER_TYPENAME_P (id_expression)
3229 && dependent_type_p (TREE_TYPE (id_expression))))))
3231 /* If the qualifying type is non-dependent (and the name
3232 does not name a conversion operator to a dependent
3233 type), issue an error. */
3234 qualified_name_lookup_error (scope, id_expression, decl, location);
3235 return error_mark_node;
3237 else if (!scope)
3239 /* It may be resolved via Koenig lookup. */
3240 *idk = CP_ID_KIND_UNQUALIFIED;
3241 return id_expression;
3243 else
3244 decl = id_expression;
3246 /* If DECL is a variable that would be out of scope under
3247 ANSI/ISO rules, but in scope in the ARM, name lookup
3248 will succeed. Issue a diagnostic here. */
3249 else
3250 decl = check_for_out_of_scope_variable (decl);
3252 /* Remember that the name was used in the definition of
3253 the current class so that we can check later to see if
3254 the meaning would have been different after the class
3255 was entirely defined. */
3256 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3257 maybe_note_name_used_in_class (id_expression, decl);
3259 /* Disallow uses of local variables from containing functions, except
3260 within lambda-expressions. */
3261 if (outer_automatic_var_p (decl))
3263 decl = process_outer_var_ref (decl, tf_warning_or_error);
3264 if (decl == error_mark_node)
3265 return error_mark_node;
3268 /* Also disallow uses of function parameters outside the function
3269 body, except inside an unevaluated context (i.e. decltype). */
3270 if (TREE_CODE (decl) == PARM_DECL
3271 && DECL_CONTEXT (decl) == NULL_TREE
3272 && !cp_unevaluated_operand)
3274 *error_msg = "use of parameter outside function body";
3275 return error_mark_node;
3279 /* If we didn't find anything, or what we found was a type,
3280 then this wasn't really an id-expression. */
3281 if (TREE_CODE (decl) == TEMPLATE_DECL
3282 && !DECL_FUNCTION_TEMPLATE_P (decl))
3284 *error_msg = "missing template arguments";
3285 return error_mark_node;
3287 else if (TREE_CODE (decl) == TYPE_DECL
3288 || TREE_CODE (decl) == NAMESPACE_DECL)
3290 *error_msg = "expected primary-expression";
3291 return error_mark_node;
3294 /* If the name resolved to a template parameter, there is no
3295 need to look it up again later. */
3296 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3297 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3299 tree r;
3301 *idk = CP_ID_KIND_NONE;
3302 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3303 decl = TEMPLATE_PARM_DECL (decl);
3304 r = convert_from_reference (DECL_INITIAL (decl));
3306 if (integral_constant_expression_p
3307 && !dependent_type_p (TREE_TYPE (decl))
3308 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3310 if (!allow_non_integral_constant_expression_p)
3311 error ("template parameter %qD of type %qT is not allowed in "
3312 "an integral constant expression because it is not of "
3313 "integral or enumeration type", decl, TREE_TYPE (decl));
3314 *non_integral_constant_expression_p = true;
3316 return r;
3318 else
3320 bool dependent_p;
3322 /* If the declaration was explicitly qualified indicate
3323 that. The semantics of `A::f(3)' are different than
3324 `f(3)' if `f' is virtual. */
3325 *idk = (scope
3326 ? CP_ID_KIND_QUALIFIED
3327 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3328 ? CP_ID_KIND_TEMPLATE_ID
3329 : CP_ID_KIND_UNQUALIFIED));
3332 /* [temp.dep.expr]
3334 An id-expression is type-dependent if it contains an
3335 identifier that was declared with a dependent type.
3337 The standard is not very specific about an id-expression that
3338 names a set of overloaded functions. What if some of them
3339 have dependent types and some of them do not? Presumably,
3340 such a name should be treated as a dependent name. */
3341 /* Assume the name is not dependent. */
3342 dependent_p = false;
3343 if (!processing_template_decl)
3344 /* No names are dependent outside a template. */
3346 else if (TREE_CODE (decl) == CONST_DECL)
3347 /* We don't want to treat enumerators as dependent. */
3349 /* A template-id where the name of the template was not resolved
3350 is definitely dependent. */
3351 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3352 && (identifier_p (TREE_OPERAND (decl, 0))))
3353 dependent_p = true;
3354 /* For anything except an overloaded function, just check its
3355 type. */
3356 else if (!is_overloaded_fn (decl))
3357 dependent_p
3358 = dependent_type_p (TREE_TYPE (decl));
3359 /* For a set of overloaded functions, check each of the
3360 functions. */
3361 else
3363 tree fns = decl;
3365 if (BASELINK_P (fns))
3366 fns = BASELINK_FUNCTIONS (fns);
3368 /* For a template-id, check to see if the template
3369 arguments are dependent. */
3370 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3372 tree args = TREE_OPERAND (fns, 1);
3373 dependent_p = any_dependent_template_arguments_p (args);
3374 /* The functions are those referred to by the
3375 template-id. */
3376 fns = TREE_OPERAND (fns, 0);
3379 /* If there are no dependent template arguments, go through
3380 the overloaded functions. */
3381 while (fns && !dependent_p)
3383 tree fn = OVL_CURRENT (fns);
3385 /* Member functions of dependent classes are
3386 dependent. */
3387 if (TREE_CODE (fn) == FUNCTION_DECL
3388 && type_dependent_expression_p (fn))
3389 dependent_p = true;
3390 else if (TREE_CODE (fn) == TEMPLATE_DECL
3391 && dependent_template_p (fn))
3392 dependent_p = true;
3394 fns = OVL_NEXT (fns);
3398 /* If the name was dependent on a template parameter, we will
3399 resolve the name at instantiation time. */
3400 if (dependent_p)
3402 /* Create a SCOPE_REF for qualified names, if the scope is
3403 dependent. */
3404 if (scope)
3406 if (TYPE_P (scope))
3408 if (address_p && done)
3409 decl = finish_qualified_id_expr (scope, decl,
3410 done, address_p,
3411 template_p,
3412 template_arg_p,
3413 tf_warning_or_error);
3414 else
3416 tree type = NULL_TREE;
3417 if (DECL_P (decl) && !dependent_scope_p (scope))
3418 type = TREE_TYPE (decl);
3419 decl = build_qualified_name (type,
3420 scope,
3421 id_expression,
3422 template_p);
3425 if (TREE_TYPE (decl))
3426 decl = convert_from_reference (decl);
3427 return decl;
3429 /* A TEMPLATE_ID already contains all the information we
3430 need. */
3431 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3432 return id_expression;
3433 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3434 /* If we found a variable, then name lookup during the
3435 instantiation will always resolve to the same VAR_DECL
3436 (or an instantiation thereof). */
3437 if (VAR_P (decl)
3438 || TREE_CODE (decl) == PARM_DECL)
3440 mark_used (decl);
3441 return convert_from_reference (decl);
3443 /* The same is true for FIELD_DECL, but we also need to
3444 make sure that the syntax is correct. */
3445 else if (TREE_CODE (decl) == FIELD_DECL)
3447 /* Since SCOPE is NULL here, this is an unqualified name.
3448 Access checking has been performed during name lookup
3449 already. Turn off checking to avoid duplicate errors. */
3450 push_deferring_access_checks (dk_no_check);
3451 decl = finish_non_static_data_member
3452 (decl, NULL_TREE,
3453 /*qualifying_scope=*/NULL_TREE);
3454 pop_deferring_access_checks ();
3455 return decl;
3457 return id_expression;
3460 if (TREE_CODE (decl) == NAMESPACE_DECL)
3462 error ("use of namespace %qD as expression", decl);
3463 return error_mark_node;
3465 else if (DECL_CLASS_TEMPLATE_P (decl))
3467 error ("use of class template %qT as expression", decl);
3468 return error_mark_node;
3470 else if (TREE_CODE (decl) == TREE_LIST)
3472 /* Ambiguous reference to base members. */
3473 error ("request for member %qD is ambiguous in "
3474 "multiple inheritance lattice", id_expression);
3475 print_candidates (decl);
3476 return error_mark_node;
3479 /* Mark variable-like entities as used. Functions are similarly
3480 marked either below or after overload resolution. */
3481 if ((VAR_P (decl)
3482 || TREE_CODE (decl) == PARM_DECL
3483 || TREE_CODE (decl) == CONST_DECL
3484 || TREE_CODE (decl) == RESULT_DECL)
3485 && !mark_used (decl))
3486 return error_mark_node;
3488 /* Only certain kinds of names are allowed in constant
3489 expression. Template parameters have already
3490 been handled above. */
3491 if (! error_operand_p (decl)
3492 && integral_constant_expression_p
3493 && ! decl_constant_var_p (decl)
3494 && TREE_CODE (decl) != CONST_DECL
3495 && ! builtin_valid_in_constant_expr_p (decl))
3497 if (!allow_non_integral_constant_expression_p)
3499 error ("%qD cannot appear in a constant-expression", decl);
3500 return error_mark_node;
3502 *non_integral_constant_expression_p = true;
3505 tree wrap;
3506 if (VAR_P (decl)
3507 && !cp_unevaluated_operand
3508 && !processing_template_decl
3509 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3510 && DECL_THREAD_LOCAL_P (decl)
3511 && (wrap = get_tls_wrapper_fn (decl)))
3513 /* Replace an evaluated use of the thread_local variable with
3514 a call to its wrapper. */
3515 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3517 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3518 && variable_template_p (TREE_OPERAND (decl, 0)))
3520 decl = finish_template_variable (decl);
3521 mark_used (decl);
3523 else if (scope)
3525 decl = (adjust_result_of_qualified_name_lookup
3526 (decl, scope, current_nonlambda_class_type()));
3528 if (TREE_CODE (decl) == FUNCTION_DECL)
3529 mark_used (decl);
3531 if (TYPE_P (scope))
3532 decl = finish_qualified_id_expr (scope,
3533 decl,
3534 done,
3535 address_p,
3536 template_p,
3537 template_arg_p,
3538 tf_warning_or_error);
3539 else
3540 decl = convert_from_reference (decl);
3542 else if (TREE_CODE (decl) == FIELD_DECL)
3544 /* Since SCOPE is NULL here, this is an unqualified name.
3545 Access checking has been performed during name lookup
3546 already. Turn off checking to avoid duplicate errors. */
3547 push_deferring_access_checks (dk_no_check);
3548 decl = finish_non_static_data_member (decl, NULL_TREE,
3549 /*qualifying_scope=*/NULL_TREE);
3550 pop_deferring_access_checks ();
3552 else if (is_overloaded_fn (decl))
3554 tree first_fn;
3556 first_fn = get_first_fn (decl);
3557 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3558 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3560 if (!really_overloaded_fn (decl)
3561 && !mark_used (first_fn))
3562 return error_mark_node;
3564 if (!template_arg_p
3565 && TREE_CODE (first_fn) == FUNCTION_DECL
3566 && DECL_FUNCTION_MEMBER_P (first_fn)
3567 && !shared_member_p (decl))
3569 /* A set of member functions. */
3570 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3571 return finish_class_member_access_expr (decl, id_expression,
3572 /*template_p=*/false,
3573 tf_warning_or_error);
3576 decl = baselink_for_fns (decl);
3578 else
3580 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3581 && DECL_CLASS_SCOPE_P (decl))
3583 tree context = context_for_name_lookup (decl);
3584 if (context != current_class_type)
3586 tree path = currently_open_derived_class (context);
3587 perform_or_defer_access_check (TYPE_BINFO (path),
3588 decl, decl,
3589 tf_warning_or_error);
3593 decl = convert_from_reference (decl);
3597 /* Handle references (c++/56130). */
3598 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3599 if (TREE_DEPRECATED (t))
3600 warn_deprecated_use (t, NULL_TREE);
3602 return decl;
3605 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3606 use as a type-specifier. */
3608 tree
3609 finish_typeof (tree expr)
3611 tree type;
3613 if (type_dependent_expression_p (expr))
3615 type = cxx_make_type (TYPEOF_TYPE);
3616 TYPEOF_TYPE_EXPR (type) = expr;
3617 SET_TYPE_STRUCTURAL_EQUALITY (type);
3619 return type;
3622 expr = mark_type_use (expr);
3624 type = unlowered_expr_type (expr);
3626 if (!type || type == unknown_type_node)
3628 error ("type of %qE is unknown", expr);
3629 return error_mark_node;
3632 return type;
3635 /* Implement the __underlying_type keyword: Return the underlying
3636 type of TYPE, suitable for use as a type-specifier. */
3638 tree
3639 finish_underlying_type (tree type)
3641 tree underlying_type;
3643 if (processing_template_decl)
3645 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3646 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3647 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3649 return underlying_type;
3652 complete_type (type);
3654 if (TREE_CODE (type) != ENUMERAL_TYPE)
3656 error ("%qT is not an enumeration type", type);
3657 return error_mark_node;
3660 underlying_type = ENUM_UNDERLYING_TYPE (type);
3662 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3663 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3664 See finish_enum_value_list for details. */
3665 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3666 underlying_type
3667 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3668 TYPE_UNSIGNED (underlying_type));
3670 return underlying_type;
3673 /* Implement the __direct_bases keyword: Return the direct base classes
3674 of type */
3676 tree
3677 calculate_direct_bases (tree type)
3679 vec<tree, va_gc> *vector = make_tree_vector();
3680 tree bases_vec = NULL_TREE;
3681 vec<tree, va_gc> *base_binfos;
3682 tree binfo;
3683 unsigned i;
3685 complete_type (type);
3687 if (!NON_UNION_CLASS_TYPE_P (type))
3688 return make_tree_vec (0);
3690 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3692 /* Virtual bases are initialized first */
3693 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3695 if (BINFO_VIRTUAL_P (binfo))
3697 vec_safe_push (vector, binfo);
3701 /* Now non-virtuals */
3702 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3704 if (!BINFO_VIRTUAL_P (binfo))
3706 vec_safe_push (vector, binfo);
3711 bases_vec = make_tree_vec (vector->length ());
3713 for (i = 0; i < vector->length (); ++i)
3715 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3717 return bases_vec;
3720 /* Implement the __bases keyword: Return the base classes
3721 of type */
3723 /* Find morally non-virtual base classes by walking binfo hierarchy */
3724 /* Virtual base classes are handled separately in finish_bases */
3726 static tree
3727 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3729 /* Don't walk bases of virtual bases */
3730 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3733 static tree
3734 dfs_calculate_bases_post (tree binfo, void *data_)
3736 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3737 if (!BINFO_VIRTUAL_P (binfo))
3739 vec_safe_push (*data, BINFO_TYPE (binfo));
3741 return NULL_TREE;
3744 /* Calculates the morally non-virtual base classes of a class */
3745 static vec<tree, va_gc> *
3746 calculate_bases_helper (tree type)
3748 vec<tree, va_gc> *vector = make_tree_vector();
3750 /* Now add non-virtual base classes in order of construction */
3751 dfs_walk_all (TYPE_BINFO (type),
3752 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3753 return vector;
3756 tree
3757 calculate_bases (tree type)
3759 vec<tree, va_gc> *vector = make_tree_vector();
3760 tree bases_vec = NULL_TREE;
3761 unsigned i;
3762 vec<tree, va_gc> *vbases;
3763 vec<tree, va_gc> *nonvbases;
3764 tree binfo;
3766 complete_type (type);
3768 if (!NON_UNION_CLASS_TYPE_P (type))
3769 return make_tree_vec (0);
3771 /* First go through virtual base classes */
3772 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3773 vec_safe_iterate (vbases, i, &binfo); i++)
3775 vec<tree, va_gc> *vbase_bases;
3776 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3777 vec_safe_splice (vector, vbase_bases);
3778 release_tree_vector (vbase_bases);
3781 /* Now for the non-virtual bases */
3782 nonvbases = calculate_bases_helper (type);
3783 vec_safe_splice (vector, nonvbases);
3784 release_tree_vector (nonvbases);
3786 /* Last element is entire class, so don't copy */
3787 bases_vec = make_tree_vec (vector->length () - 1);
3789 for (i = 0; i < vector->length () - 1; ++i)
3791 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3793 release_tree_vector (vector);
3794 return bases_vec;
3797 tree
3798 finish_bases (tree type, bool direct)
3800 tree bases = NULL_TREE;
3802 if (!processing_template_decl)
3804 /* Parameter packs can only be used in templates */
3805 error ("Parameter pack __bases only valid in template declaration");
3806 return error_mark_node;
3809 bases = cxx_make_type (BASES);
3810 BASES_TYPE (bases) = type;
3811 BASES_DIRECT (bases) = direct;
3812 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3814 return bases;
3817 /* Perform C++-specific checks for __builtin_offsetof before calling
3818 fold_offsetof. */
3820 tree
3821 finish_offsetof (tree expr, location_t loc)
3823 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3825 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3826 TREE_OPERAND (expr, 2));
3827 return error_mark_node;
3829 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3830 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3831 || TREE_TYPE (expr) == unknown_type_node)
3833 if (INDIRECT_REF_P (expr))
3834 error ("second operand of %<offsetof%> is neither a single "
3835 "identifier nor a sequence of member accesses and "
3836 "array references");
3837 else
3839 if (TREE_CODE (expr) == COMPONENT_REF
3840 || TREE_CODE (expr) == COMPOUND_EXPR)
3841 expr = TREE_OPERAND (expr, 1);
3842 error ("cannot apply %<offsetof%> to member function %qD", expr);
3844 return error_mark_node;
3846 if (REFERENCE_REF_P (expr))
3847 expr = TREE_OPERAND (expr, 0);
3848 if (TREE_CODE (expr) == COMPONENT_REF)
3850 tree object = TREE_OPERAND (expr, 0);
3851 if (!complete_type_or_else (TREE_TYPE (object), object))
3852 return error_mark_node;
3853 if (warn_invalid_offsetof
3854 && CLASS_TYPE_P (TREE_TYPE (object))
3855 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3856 && cp_unevaluated_operand == 0)
3857 pedwarn (loc, OPT_Winvalid_offsetof,
3858 "offsetof within non-standard-layout type %qT is undefined",
3859 TREE_TYPE (object));
3861 return fold_offsetof (expr);
3864 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3865 function is broken out from the above for the benefit of the tree-ssa
3866 project. */
3868 void
3869 simplify_aggr_init_expr (tree *tp)
3871 tree aggr_init_expr = *tp;
3873 /* Form an appropriate CALL_EXPR. */
3874 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3875 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3876 tree type = TREE_TYPE (slot);
3878 tree call_expr;
3879 enum style_t { ctor, arg, pcc } style;
3881 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3882 style = ctor;
3883 #ifdef PCC_STATIC_STRUCT_RETURN
3884 else if (1)
3885 style = pcc;
3886 #endif
3887 else
3889 gcc_assert (TREE_ADDRESSABLE (type));
3890 style = arg;
3893 call_expr = build_call_array_loc (input_location,
3894 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3896 aggr_init_expr_nargs (aggr_init_expr),
3897 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3898 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3899 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3901 if (style == ctor)
3903 /* Replace the first argument to the ctor with the address of the
3904 slot. */
3905 cxx_mark_addressable (slot);
3906 CALL_EXPR_ARG (call_expr, 0) =
3907 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3909 else if (style == arg)
3911 /* Just mark it addressable here, and leave the rest to
3912 expand_call{,_inline}. */
3913 cxx_mark_addressable (slot);
3914 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3915 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3917 else if (style == pcc)
3919 /* If we're using the non-reentrant PCC calling convention, then we
3920 need to copy the returned value out of the static buffer into the
3921 SLOT. */
3922 push_deferring_access_checks (dk_no_check);
3923 call_expr = build_aggr_init (slot, call_expr,
3924 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3925 tf_warning_or_error);
3926 pop_deferring_access_checks ();
3927 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3930 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3932 tree init = build_zero_init (type, NULL_TREE,
3933 /*static_storage_p=*/false);
3934 init = build2 (INIT_EXPR, void_type_node, slot, init);
3935 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3936 init, call_expr);
3939 *tp = call_expr;
3942 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3944 void
3945 emit_associated_thunks (tree fn)
3947 /* When we use vcall offsets, we emit thunks with the virtual
3948 functions to which they thunk. The whole point of vcall offsets
3949 is so that you can know statically the entire set of thunks that
3950 will ever be needed for a given virtual function, thereby
3951 enabling you to output all the thunks with the function itself. */
3952 if (DECL_VIRTUAL_P (fn)
3953 /* Do not emit thunks for extern template instantiations. */
3954 && ! DECL_REALLY_EXTERN (fn))
3956 tree thunk;
3958 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3960 if (!THUNK_ALIAS (thunk))
3962 use_thunk (thunk, /*emit_p=*/1);
3963 if (DECL_RESULT_THUNK_P (thunk))
3965 tree probe;
3967 for (probe = DECL_THUNKS (thunk);
3968 probe; probe = DECL_CHAIN (probe))
3969 use_thunk (probe, /*emit_p=*/1);
3972 else
3973 gcc_assert (!DECL_THUNKS (thunk));
3978 /* Generate RTL for FN. */
3980 bool
3981 expand_or_defer_fn_1 (tree fn)
3983 /* When the parser calls us after finishing the body of a template
3984 function, we don't really want to expand the body. */
3985 if (processing_template_decl)
3987 /* Normally, collection only occurs in rest_of_compilation. So,
3988 if we don't collect here, we never collect junk generated
3989 during the processing of templates until we hit a
3990 non-template function. It's not safe to do this inside a
3991 nested class, though, as the parser may have local state that
3992 is not a GC root. */
3993 if (!function_depth)
3994 ggc_collect ();
3995 return false;
3998 gcc_assert (DECL_SAVED_TREE (fn));
4000 /* We make a decision about linkage for these functions at the end
4001 of the compilation. Until that point, we do not want the back
4002 end to output them -- but we do want it to see the bodies of
4003 these functions so that it can inline them as appropriate. */
4004 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4006 if (DECL_INTERFACE_KNOWN (fn))
4007 /* We've already made a decision as to how this function will
4008 be handled. */;
4009 else if (!at_eof)
4010 tentative_decl_linkage (fn);
4011 else
4012 import_export_decl (fn);
4014 /* If the user wants us to keep all inline functions, then mark
4015 this function as needed so that finish_file will make sure to
4016 output it later. Similarly, all dllexport'd functions must
4017 be emitted; there may be callers in other DLLs. */
4018 if (DECL_DECLARED_INLINE_P (fn)
4019 && !DECL_REALLY_EXTERN (fn)
4020 && (flag_keep_inline_functions
4021 || (flag_keep_inline_dllexport
4022 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4024 mark_needed (fn);
4025 DECL_EXTERNAL (fn) = 0;
4029 /* If this is a constructor or destructor body, we have to clone
4030 it. */
4031 if (maybe_clone_body (fn))
4033 /* We don't want to process FN again, so pretend we've written
4034 it out, even though we haven't. */
4035 TREE_ASM_WRITTEN (fn) = 1;
4036 /* If this is an instantiation of a constexpr function, keep
4037 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4038 if (!is_instantiation_of_constexpr (fn))
4039 DECL_SAVED_TREE (fn) = NULL_TREE;
4040 return false;
4043 /* There's no reason to do any of the work here if we're only doing
4044 semantic analysis; this code just generates RTL. */
4045 if (flag_syntax_only)
4046 return false;
4048 return true;
4051 void
4052 expand_or_defer_fn (tree fn)
4054 if (expand_or_defer_fn_1 (fn))
4056 function_depth++;
4058 /* Expand or defer, at the whim of the compilation unit manager. */
4059 cgraph_node::finalize_function (fn, function_depth > 1);
4060 emit_associated_thunks (fn);
4062 function_depth--;
4066 struct nrv_data
4068 nrv_data () : visited (37) {}
4070 tree var;
4071 tree result;
4072 hash_table<pointer_hash <tree_node> > visited;
4075 /* Helper function for walk_tree, used by finalize_nrv below. */
4077 static tree
4078 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4080 struct nrv_data *dp = (struct nrv_data *)data;
4081 tree_node **slot;
4083 /* No need to walk into types. There wouldn't be any need to walk into
4084 non-statements, except that we have to consider STMT_EXPRs. */
4085 if (TYPE_P (*tp))
4086 *walk_subtrees = 0;
4087 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4088 but differs from using NULL_TREE in that it indicates that we care
4089 about the value of the RESULT_DECL. */
4090 else if (TREE_CODE (*tp) == RETURN_EXPR)
4091 TREE_OPERAND (*tp, 0) = dp->result;
4092 /* Change all cleanups for the NRV to only run when an exception is
4093 thrown. */
4094 else if (TREE_CODE (*tp) == CLEANUP_STMT
4095 && CLEANUP_DECL (*tp) == dp->var)
4096 CLEANUP_EH_ONLY (*tp) = 1;
4097 /* Replace the DECL_EXPR for the NRV with an initialization of the
4098 RESULT_DECL, if needed. */
4099 else if (TREE_CODE (*tp) == DECL_EXPR
4100 && DECL_EXPR_DECL (*tp) == dp->var)
4102 tree init;
4103 if (DECL_INITIAL (dp->var)
4104 && DECL_INITIAL (dp->var) != error_mark_node)
4105 init = build2 (INIT_EXPR, void_type_node, dp->result,
4106 DECL_INITIAL (dp->var));
4107 else
4108 init = build_empty_stmt (EXPR_LOCATION (*tp));
4109 DECL_INITIAL (dp->var) = NULL_TREE;
4110 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4111 *tp = init;
4113 /* And replace all uses of the NRV with the RESULT_DECL. */
4114 else if (*tp == dp->var)
4115 *tp = dp->result;
4117 /* Avoid walking into the same tree more than once. Unfortunately, we
4118 can't just use walk_tree_without duplicates because it would only call
4119 us for the first occurrence of dp->var in the function body. */
4120 slot = dp->visited.find_slot (*tp, INSERT);
4121 if (*slot)
4122 *walk_subtrees = 0;
4123 else
4124 *slot = *tp;
4126 /* Keep iterating. */
4127 return NULL_TREE;
4130 /* Called from finish_function to implement the named return value
4131 optimization by overriding all the RETURN_EXPRs and pertinent
4132 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4133 RESULT_DECL for the function. */
4135 void
4136 finalize_nrv (tree *tp, tree var, tree result)
4138 struct nrv_data data;
4140 /* Copy name from VAR to RESULT. */
4141 DECL_NAME (result) = DECL_NAME (var);
4142 /* Don't forget that we take its address. */
4143 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4144 /* Finally set DECL_VALUE_EXPR to avoid assigning
4145 a stack slot at -O0 for the original var and debug info
4146 uses RESULT location for VAR. */
4147 SET_DECL_VALUE_EXPR (var, result);
4148 DECL_HAS_VALUE_EXPR_P (var) = 1;
4150 data.var = var;
4151 data.result = result;
4152 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4155 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4157 bool
4158 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4159 bool need_copy_ctor, bool need_copy_assignment,
4160 bool need_dtor)
4162 int save_errorcount = errorcount;
4163 tree info, t;
4165 /* Always allocate 3 elements for simplicity. These are the
4166 function decls for the ctor, dtor, and assignment op.
4167 This layout is known to the three lang hooks,
4168 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4169 and cxx_omp_clause_assign_op. */
4170 info = make_tree_vec (3);
4171 CP_OMP_CLAUSE_INFO (c) = info;
4173 if (need_default_ctor || need_copy_ctor)
4175 if (need_default_ctor)
4176 t = get_default_ctor (type);
4177 else
4178 t = get_copy_ctor (type, tf_warning_or_error);
4180 if (t && !trivial_fn_p (t))
4181 TREE_VEC_ELT (info, 0) = t;
4184 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4185 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4187 if (need_copy_assignment)
4189 t = get_copy_assign (type);
4191 if (t && !trivial_fn_p (t))
4192 TREE_VEC_ELT (info, 2) = t;
4195 return errorcount != save_errorcount;
4198 /* Helper function for handle_omp_array_sections. Called recursively
4199 to handle multiple array-section-subscripts. C is the clause,
4200 T current expression (initially OMP_CLAUSE_DECL), which is either
4201 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4202 expression if specified, TREE_VALUE length expression if specified,
4203 TREE_CHAIN is what it has been specified after, or some decl.
4204 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4205 set to true if any of the array-section-subscript could have length
4206 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4207 first array-section-subscript which is known not to have length
4208 of one. Given say:
4209 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4210 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4211 all are or may have length of 1, array-section-subscript [:2] is the
4212 first one knonwn not to have length 1. For array-section-subscript
4213 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4214 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4215 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4216 case though, as some lengths could be zero. */
4218 static tree
4219 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4220 bool &maybe_zero_len, unsigned int &first_non_one)
4222 tree ret, low_bound, length, type;
4223 if (TREE_CODE (t) != TREE_LIST)
4225 if (error_operand_p (t))
4226 return error_mark_node;
4227 if (type_dependent_expression_p (t))
4228 return NULL_TREE;
4229 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4231 if (processing_template_decl)
4232 return NULL_TREE;
4233 if (DECL_P (t))
4234 error_at (OMP_CLAUSE_LOCATION (c),
4235 "%qD is not a variable in %qs clause", t,
4236 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4237 else
4238 error_at (OMP_CLAUSE_LOCATION (c),
4239 "%qE is not a variable in %qs clause", t,
4240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4241 return error_mark_node;
4243 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4244 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4246 error_at (OMP_CLAUSE_LOCATION (c),
4247 "%qD is threadprivate variable in %qs clause", t,
4248 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4249 return error_mark_node;
4251 t = convert_from_reference (t);
4252 return t;
4255 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4256 maybe_zero_len, first_non_one);
4257 if (ret == error_mark_node || ret == NULL_TREE)
4258 return ret;
4260 type = TREE_TYPE (ret);
4261 low_bound = TREE_PURPOSE (t);
4262 length = TREE_VALUE (t);
4263 if ((low_bound && type_dependent_expression_p (low_bound))
4264 || (length && type_dependent_expression_p (length)))
4265 return NULL_TREE;
4267 if (low_bound == error_mark_node || length == error_mark_node)
4268 return error_mark_node;
4270 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4272 error_at (OMP_CLAUSE_LOCATION (c),
4273 "low bound %qE of array section does not have integral type",
4274 low_bound);
4275 return error_mark_node;
4277 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4279 error_at (OMP_CLAUSE_LOCATION (c),
4280 "length %qE of array section does not have integral type",
4281 length);
4282 return error_mark_node;
4284 if (low_bound)
4285 low_bound = mark_rvalue_use (low_bound);
4286 if (length)
4287 length = mark_rvalue_use (length);
4288 if (low_bound
4289 && TREE_CODE (low_bound) == INTEGER_CST
4290 && TYPE_PRECISION (TREE_TYPE (low_bound))
4291 > TYPE_PRECISION (sizetype))
4292 low_bound = fold_convert (sizetype, low_bound);
4293 if (length
4294 && TREE_CODE (length) == INTEGER_CST
4295 && TYPE_PRECISION (TREE_TYPE (length))
4296 > TYPE_PRECISION (sizetype))
4297 length = fold_convert (sizetype, length);
4298 if (low_bound == NULL_TREE)
4299 low_bound = integer_zero_node;
4301 if (length != NULL_TREE)
4303 if (!integer_nonzerop (length))
4304 maybe_zero_len = true;
4305 if (first_non_one == types.length ()
4306 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4307 first_non_one++;
4309 if (TREE_CODE (type) == ARRAY_TYPE)
4311 if (length == NULL_TREE
4312 && (TYPE_DOMAIN (type) == NULL_TREE
4313 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4315 error_at (OMP_CLAUSE_LOCATION (c),
4316 "for unknown bound array type length expression must "
4317 "be specified");
4318 return error_mark_node;
4320 if (TREE_CODE (low_bound) == INTEGER_CST
4321 && tree_int_cst_sgn (low_bound) == -1)
4323 error_at (OMP_CLAUSE_LOCATION (c),
4324 "negative low bound in array section in %qs clause",
4325 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4326 return error_mark_node;
4328 if (length != NULL_TREE
4329 && TREE_CODE (length) == INTEGER_CST
4330 && tree_int_cst_sgn (length) == -1)
4332 error_at (OMP_CLAUSE_LOCATION (c),
4333 "negative length in array section in %qs clause",
4334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4335 return error_mark_node;
4337 if (TYPE_DOMAIN (type)
4338 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4339 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4340 == INTEGER_CST)
4342 tree size = size_binop (PLUS_EXPR,
4343 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4344 size_one_node);
4345 if (TREE_CODE (low_bound) == INTEGER_CST)
4347 if (tree_int_cst_lt (size, low_bound))
4349 error_at (OMP_CLAUSE_LOCATION (c),
4350 "low bound %qE above array section size "
4351 "in %qs clause", low_bound,
4352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4353 return error_mark_node;
4355 if (tree_int_cst_equal (size, low_bound))
4356 maybe_zero_len = true;
4357 else if (length == NULL_TREE
4358 && first_non_one == types.length ()
4359 && tree_int_cst_equal
4360 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4361 low_bound))
4362 first_non_one++;
4364 else if (length == NULL_TREE)
4366 maybe_zero_len = true;
4367 if (first_non_one == types.length ())
4368 first_non_one++;
4370 if (length && TREE_CODE (length) == INTEGER_CST)
4372 if (tree_int_cst_lt (size, length))
4374 error_at (OMP_CLAUSE_LOCATION (c),
4375 "length %qE above array section size "
4376 "in %qs clause", length,
4377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4378 return error_mark_node;
4380 if (TREE_CODE (low_bound) == INTEGER_CST)
4382 tree lbpluslen
4383 = size_binop (PLUS_EXPR,
4384 fold_convert (sizetype, low_bound),
4385 fold_convert (sizetype, length));
4386 if (TREE_CODE (lbpluslen) == INTEGER_CST
4387 && tree_int_cst_lt (size, lbpluslen))
4389 error_at (OMP_CLAUSE_LOCATION (c),
4390 "high bound %qE above array section size "
4391 "in %qs clause", lbpluslen,
4392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4393 return error_mark_node;
4398 else if (length == NULL_TREE)
4400 maybe_zero_len = true;
4401 if (first_non_one == types.length ())
4402 first_non_one++;
4405 /* For [lb:] we will need to evaluate lb more than once. */
4406 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4408 tree lb = cp_save_expr (low_bound);
4409 if (lb != low_bound)
4411 TREE_PURPOSE (t) = lb;
4412 low_bound = lb;
4416 else if (TREE_CODE (type) == POINTER_TYPE)
4418 if (length == NULL_TREE)
4420 error_at (OMP_CLAUSE_LOCATION (c),
4421 "for pointer type length expression must be specified");
4422 return error_mark_node;
4424 /* If there is a pointer type anywhere but in the very first
4425 array-section-subscript, the array section can't be contiguous. */
4426 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4427 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4429 error_at (OMP_CLAUSE_LOCATION (c),
4430 "array section is not contiguous in %qs clause",
4431 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4432 return error_mark_node;
4435 else
4437 error_at (OMP_CLAUSE_LOCATION (c),
4438 "%qE does not have pointer or array type", ret);
4439 return error_mark_node;
4441 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4442 types.safe_push (TREE_TYPE (ret));
4443 /* We will need to evaluate lb more than once. */
4444 tree lb = cp_save_expr (low_bound);
4445 if (lb != low_bound)
4447 TREE_PURPOSE (t) = lb;
4448 low_bound = lb;
4450 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4451 return ret;
4454 /* Handle array sections for clause C. */
4456 static bool
4457 handle_omp_array_sections (tree c)
4459 bool maybe_zero_len = false;
4460 unsigned int first_non_one = 0;
4461 auto_vec<tree> types;
4462 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4463 maybe_zero_len, first_non_one);
4464 if (first == error_mark_node)
4465 return true;
4466 if (first == NULL_TREE)
4467 return false;
4468 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4470 tree t = OMP_CLAUSE_DECL (c);
4471 tree tem = NULL_TREE;
4472 if (processing_template_decl)
4473 return false;
4474 /* Need to evaluate side effects in the length expressions
4475 if any. */
4476 while (TREE_CODE (t) == TREE_LIST)
4478 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4480 if (tem == NULL_TREE)
4481 tem = TREE_VALUE (t);
4482 else
4483 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4484 TREE_VALUE (t), tem);
4486 t = TREE_CHAIN (t);
4488 if (tem)
4489 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4490 OMP_CLAUSE_DECL (c) = first;
4492 else
4494 unsigned int num = types.length (), i;
4495 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4496 tree condition = NULL_TREE;
4498 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4499 maybe_zero_len = true;
4500 if (processing_template_decl && maybe_zero_len)
4501 return false;
4503 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4504 t = TREE_CHAIN (t))
4506 tree low_bound = TREE_PURPOSE (t);
4507 tree length = TREE_VALUE (t);
4509 i--;
4510 if (low_bound
4511 && TREE_CODE (low_bound) == INTEGER_CST
4512 && TYPE_PRECISION (TREE_TYPE (low_bound))
4513 > TYPE_PRECISION (sizetype))
4514 low_bound = fold_convert (sizetype, low_bound);
4515 if (length
4516 && TREE_CODE (length) == INTEGER_CST
4517 && TYPE_PRECISION (TREE_TYPE (length))
4518 > TYPE_PRECISION (sizetype))
4519 length = fold_convert (sizetype, length);
4520 if (low_bound == NULL_TREE)
4521 low_bound = integer_zero_node;
4522 if (!maybe_zero_len && i > first_non_one)
4524 if (integer_nonzerop (low_bound))
4525 goto do_warn_noncontiguous;
4526 if (length != NULL_TREE
4527 && TREE_CODE (length) == INTEGER_CST
4528 && TYPE_DOMAIN (types[i])
4529 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4530 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4531 == INTEGER_CST)
4533 tree size;
4534 size = size_binop (PLUS_EXPR,
4535 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4536 size_one_node);
4537 if (!tree_int_cst_equal (length, size))
4539 do_warn_noncontiguous:
4540 error_at (OMP_CLAUSE_LOCATION (c),
4541 "array section is not contiguous in %qs "
4542 "clause",
4543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4544 return true;
4547 if (!processing_template_decl
4548 && length != NULL_TREE
4549 && TREE_SIDE_EFFECTS (length))
4551 if (side_effects == NULL_TREE)
4552 side_effects = length;
4553 else
4554 side_effects = build2 (COMPOUND_EXPR,
4555 TREE_TYPE (side_effects),
4556 length, side_effects);
4559 else if (processing_template_decl)
4560 continue;
4561 else
4563 tree l;
4565 if (i > first_non_one && length && integer_nonzerop (length))
4566 continue;
4567 if (length)
4568 l = fold_convert (sizetype, length);
4569 else
4571 l = size_binop (PLUS_EXPR,
4572 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4573 size_one_node);
4574 l = size_binop (MINUS_EXPR, l,
4575 fold_convert (sizetype, low_bound));
4577 if (i > first_non_one)
4579 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4580 size_zero_node);
4581 if (condition == NULL_TREE)
4582 condition = l;
4583 else
4584 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4585 l, condition);
4587 else if (size == NULL_TREE)
4589 size = size_in_bytes (TREE_TYPE (types[i]));
4590 size = size_binop (MULT_EXPR, size, l);
4591 if (condition)
4592 size = fold_build3 (COND_EXPR, sizetype, condition,
4593 size, size_zero_node);
4595 else
4596 size = size_binop (MULT_EXPR, size, l);
4599 if (!processing_template_decl)
4601 if (side_effects)
4602 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4603 OMP_CLAUSE_DECL (c) = first;
4604 OMP_CLAUSE_SIZE (c) = size;
4605 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4606 return false;
4607 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4608 OMP_CLAUSE_MAP);
4609 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4610 if (!cxx_mark_addressable (t))
4611 return false;
4612 OMP_CLAUSE_DECL (c2) = t;
4613 t = build_fold_addr_expr (first);
4614 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4615 ptrdiff_type_node, t);
4616 tree ptr = OMP_CLAUSE_DECL (c2);
4617 ptr = convert_from_reference (ptr);
4618 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4619 ptr = build_fold_addr_expr (ptr);
4620 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4621 ptrdiff_type_node, t,
4622 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4623 ptrdiff_type_node, ptr));
4624 OMP_CLAUSE_SIZE (c2) = t;
4625 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4626 OMP_CLAUSE_CHAIN (c) = c2;
4627 ptr = OMP_CLAUSE_DECL (c2);
4628 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4629 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4631 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4632 OMP_CLAUSE_MAP);
4633 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4634 OMP_CLAUSE_DECL (c3) = ptr;
4635 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4636 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4637 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4638 OMP_CLAUSE_CHAIN (c2) = c3;
4642 return false;
4645 /* Return identifier to look up for omp declare reduction. */
4647 tree
4648 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4650 const char *p = NULL;
4651 const char *m = NULL;
4652 switch (reduction_code)
4654 case PLUS_EXPR:
4655 case MULT_EXPR:
4656 case MINUS_EXPR:
4657 case BIT_AND_EXPR:
4658 case BIT_XOR_EXPR:
4659 case BIT_IOR_EXPR:
4660 case TRUTH_ANDIF_EXPR:
4661 case TRUTH_ORIF_EXPR:
4662 reduction_id = ansi_opname (reduction_code);
4663 break;
4664 case MIN_EXPR:
4665 p = "min";
4666 break;
4667 case MAX_EXPR:
4668 p = "max";
4669 break;
4670 default:
4671 break;
4674 if (p == NULL)
4676 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4677 return error_mark_node;
4678 p = IDENTIFIER_POINTER (reduction_id);
4681 if (type != NULL_TREE)
4682 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4684 const char prefix[] = "omp declare reduction ";
4685 size_t lenp = sizeof (prefix);
4686 if (strncmp (p, prefix, lenp - 1) == 0)
4687 lenp = 1;
4688 size_t len = strlen (p);
4689 size_t lenm = m ? strlen (m) + 1 : 0;
4690 char *name = XALLOCAVEC (char, lenp + len + lenm);
4691 if (lenp > 1)
4692 memcpy (name, prefix, lenp - 1);
4693 memcpy (name + lenp - 1, p, len + 1);
4694 if (m)
4696 name[lenp + len - 1] = '~';
4697 memcpy (name + lenp + len, m, lenm);
4699 return get_identifier (name);
4702 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4703 FUNCTION_DECL or NULL_TREE if not found. */
4705 static tree
4706 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4707 vec<tree> *ambiguousp)
4709 tree orig_id = id;
4710 tree baselink = NULL_TREE;
4711 if (identifier_p (id))
4713 cp_id_kind idk;
4714 bool nonint_cst_expression_p;
4715 const char *error_msg;
4716 id = omp_reduction_id (ERROR_MARK, id, type);
4717 tree decl = lookup_name (id);
4718 if (decl == NULL_TREE)
4719 decl = error_mark_node;
4720 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4721 &nonint_cst_expression_p, false, true, false,
4722 false, &error_msg, loc);
4723 if (idk == CP_ID_KIND_UNQUALIFIED
4724 && identifier_p (id))
4726 vec<tree, va_gc> *args = NULL;
4727 vec_safe_push (args, build_reference_type (type));
4728 id = perform_koenig_lookup (id, args, tf_none);
4731 else if (TREE_CODE (id) == SCOPE_REF)
4732 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4733 omp_reduction_id (ERROR_MARK,
4734 TREE_OPERAND (id, 1),
4735 type),
4736 false, false);
4737 tree fns = id;
4738 if (id && is_overloaded_fn (id))
4739 id = get_fns (id);
4740 for (; id; id = OVL_NEXT (id))
4742 tree fndecl = OVL_CURRENT (id);
4743 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4745 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4746 if (same_type_p (TREE_TYPE (argtype), type))
4747 break;
4750 if (id && BASELINK_P (fns))
4752 if (baselinkp)
4753 *baselinkp = fns;
4754 else
4755 baselink = fns;
4757 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4759 vec<tree> ambiguous = vNULL;
4760 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4761 unsigned int ix;
4762 if (ambiguousp == NULL)
4763 ambiguousp = &ambiguous;
4764 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4766 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4767 baselinkp ? baselinkp : &baselink,
4768 ambiguousp);
4769 if (id == NULL_TREE)
4770 continue;
4771 if (!ambiguousp->is_empty ())
4772 ambiguousp->safe_push (id);
4773 else if (ret != NULL_TREE)
4775 ambiguousp->safe_push (ret);
4776 ambiguousp->safe_push (id);
4777 ret = NULL_TREE;
4779 else
4780 ret = id;
4782 if (ambiguousp != &ambiguous)
4783 return ret;
4784 if (!ambiguous.is_empty ())
4786 const char *str = _("candidates are:");
4787 unsigned int idx;
4788 tree udr;
4789 error_at (loc, "user defined reduction lookup is ambiguous");
4790 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4792 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4793 if (idx == 0)
4794 str = get_spaces (str);
4796 ambiguous.release ();
4797 ret = error_mark_node;
4798 baselink = NULL_TREE;
4800 id = ret;
4802 if (id && baselink)
4803 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4804 id, id, tf_warning_or_error);
4805 return id;
4808 /* Helper function for cp_parser_omp_declare_reduction_exprs
4809 and tsubst_omp_udr.
4810 Remove CLEANUP_STMT for data (omp_priv variable).
4811 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4812 DECL_EXPR. */
4814 tree
4815 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4817 if (TYPE_P (*tp))
4818 *walk_subtrees = 0;
4819 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4820 *tp = CLEANUP_BODY (*tp);
4821 else if (TREE_CODE (*tp) == DECL_EXPR)
4823 tree decl = DECL_EXPR_DECL (*tp);
4824 if (!processing_template_decl
4825 && decl == (tree) data
4826 && DECL_INITIAL (decl)
4827 && DECL_INITIAL (decl) != error_mark_node)
4829 tree list = NULL_TREE;
4830 append_to_statement_list_force (*tp, &list);
4831 tree init_expr = build2 (INIT_EXPR, void_type_node,
4832 decl, DECL_INITIAL (decl));
4833 DECL_INITIAL (decl) = NULL_TREE;
4834 append_to_statement_list_force (init_expr, &list);
4835 *tp = list;
4838 return NULL_TREE;
4841 /* Data passed from cp_check_omp_declare_reduction to
4842 cp_check_omp_declare_reduction_r. */
4844 struct cp_check_omp_declare_reduction_data
4846 location_t loc;
4847 tree stmts[7];
4848 bool combiner_p;
4851 /* Helper function for cp_check_omp_declare_reduction, called via
4852 cp_walk_tree. */
4854 static tree
4855 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4857 struct cp_check_omp_declare_reduction_data *udr_data
4858 = (struct cp_check_omp_declare_reduction_data *) data;
4859 if (SSA_VAR_P (*tp)
4860 && !DECL_ARTIFICIAL (*tp)
4861 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4862 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4864 location_t loc = udr_data->loc;
4865 if (udr_data->combiner_p)
4866 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4867 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4868 *tp);
4869 else
4870 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4871 "to variable %qD which is not %<omp_priv%> nor "
4872 "%<omp_orig%>",
4873 *tp);
4874 return *tp;
4876 return NULL_TREE;
4879 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4881 void
4882 cp_check_omp_declare_reduction (tree udr)
4884 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4885 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4886 type = TREE_TYPE (type);
4887 int i;
4888 location_t loc = DECL_SOURCE_LOCATION (udr);
4890 if (type == error_mark_node)
4891 return;
4892 if (ARITHMETIC_TYPE_P (type))
4894 static enum tree_code predef_codes[]
4895 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4896 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4897 for (i = 0; i < 8; i++)
4899 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4900 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4901 const char *n2 = IDENTIFIER_POINTER (id);
4902 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4903 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4904 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4905 break;
4908 if (i == 8
4909 && TREE_CODE (type) != COMPLEX_EXPR)
4911 const char prefix_minmax[] = "omp declare reduction m";
4912 size_t prefix_size = sizeof (prefix_minmax) - 1;
4913 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4914 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4915 prefix_minmax, prefix_size) == 0
4916 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4917 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4918 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4919 i = 0;
4921 if (i < 8)
4923 error_at (loc, "predeclared arithmetic type %qT in "
4924 "%<#pragma omp declare reduction%>", type);
4925 return;
4928 else if (TREE_CODE (type) == FUNCTION_TYPE
4929 || TREE_CODE (type) == METHOD_TYPE
4930 || TREE_CODE (type) == ARRAY_TYPE)
4932 error_at (loc, "function or array type %qT in "
4933 "%<#pragma omp declare reduction%>", type);
4934 return;
4936 else if (TREE_CODE (type) == REFERENCE_TYPE)
4938 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4939 type);
4940 return;
4942 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4944 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4945 "%<#pragma omp declare reduction%>", type);
4946 return;
4949 tree body = DECL_SAVED_TREE (udr);
4950 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4951 return;
4953 tree_stmt_iterator tsi;
4954 struct cp_check_omp_declare_reduction_data data;
4955 memset (data.stmts, 0, sizeof data.stmts);
4956 for (i = 0, tsi = tsi_start (body);
4957 i < 7 && !tsi_end_p (tsi);
4958 i++, tsi_next (&tsi))
4959 data.stmts[i] = tsi_stmt (tsi);
4960 data.loc = loc;
4961 gcc_assert (tsi_end_p (tsi));
4962 if (i >= 3)
4964 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4965 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4966 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4967 return;
4968 data.combiner_p = true;
4969 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4970 &data, NULL))
4971 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4973 if (i >= 6)
4975 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4976 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4977 data.combiner_p = false;
4978 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4979 &data, NULL)
4980 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4981 cp_check_omp_declare_reduction_r, &data, NULL))
4982 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4983 if (i == 7)
4984 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4988 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4989 an inline call. But, remap
4990 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4991 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4993 static tree
4994 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4995 tree decl, tree placeholder)
4997 copy_body_data id;
4998 hash_map<tree, tree> decl_map;
5000 decl_map.put (omp_decl1, placeholder);
5001 decl_map.put (omp_decl2, decl);
5002 memset (&id, 0, sizeof (id));
5003 id.src_fn = DECL_CONTEXT (omp_decl1);
5004 id.dst_fn = current_function_decl;
5005 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5006 id.decl_map = &decl_map;
5008 id.copy_decl = copy_decl_no_change;
5009 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5010 id.transform_new_cfg = true;
5011 id.transform_return_to_modify = false;
5012 id.transform_lang_insert_block = NULL;
5013 id.eh_lp_nr = 0;
5014 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5015 return stmt;
5018 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5019 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5021 static tree
5022 find_omp_placeholder_r (tree *tp, int *, void *data)
5024 if (*tp == (tree) data)
5025 return *tp;
5026 return NULL_TREE;
5029 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5030 Return true if there is some error and the clause should be removed. */
5032 static bool
5033 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5035 tree t = OMP_CLAUSE_DECL (c);
5036 bool predefined = false;
5037 tree type = TREE_TYPE (t);
5038 if (TREE_CODE (type) == REFERENCE_TYPE)
5039 type = TREE_TYPE (type);
5040 if (type == error_mark_node)
5041 return true;
5042 else if (ARITHMETIC_TYPE_P (type))
5043 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5045 case PLUS_EXPR:
5046 case MULT_EXPR:
5047 case MINUS_EXPR:
5048 predefined = true;
5049 break;
5050 case MIN_EXPR:
5051 case MAX_EXPR:
5052 if (TREE_CODE (type) == COMPLEX_TYPE)
5053 break;
5054 predefined = true;
5055 break;
5056 case BIT_AND_EXPR:
5057 case BIT_IOR_EXPR:
5058 case BIT_XOR_EXPR:
5059 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5060 break;
5061 predefined = true;
5062 break;
5063 case TRUTH_ANDIF_EXPR:
5064 case TRUTH_ORIF_EXPR:
5065 if (FLOAT_TYPE_P (type))
5066 break;
5067 predefined = true;
5068 break;
5069 default:
5070 break;
5072 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5074 error ("%qE has invalid type for %<reduction%>", t);
5075 return true;
5077 else if (!processing_template_decl)
5079 t = require_complete_type (t);
5080 if (t == error_mark_node)
5081 return true;
5082 OMP_CLAUSE_DECL (c) = t;
5085 if (predefined)
5087 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5088 return false;
5090 else if (processing_template_decl)
5091 return false;
5093 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5095 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5096 if (TREE_CODE (type) == REFERENCE_TYPE)
5097 type = TREE_TYPE (type);
5098 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5099 if (id == NULL_TREE)
5100 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5101 NULL_TREE, NULL_TREE);
5102 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5103 if (id)
5105 if (id == error_mark_node)
5106 return true;
5107 id = OVL_CURRENT (id);
5108 mark_used (id);
5109 tree body = DECL_SAVED_TREE (id);
5110 if (TREE_CODE (body) == STATEMENT_LIST)
5112 tree_stmt_iterator tsi;
5113 tree placeholder = NULL_TREE;
5114 int i;
5115 tree stmts[7];
5116 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5117 atype = TREE_TYPE (atype);
5118 bool need_static_cast = !same_type_p (type, atype);
5119 memset (stmts, 0, sizeof stmts);
5120 for (i = 0, tsi = tsi_start (body);
5121 i < 7 && !tsi_end_p (tsi);
5122 i++, tsi_next (&tsi))
5123 stmts[i] = tsi_stmt (tsi);
5124 gcc_assert (tsi_end_p (tsi));
5126 if (i >= 3)
5128 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5129 && TREE_CODE (stmts[1]) == DECL_EXPR);
5130 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5131 DECL_ARTIFICIAL (placeholder) = 1;
5132 DECL_IGNORED_P (placeholder) = 1;
5133 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5134 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5135 cxx_mark_addressable (placeholder);
5136 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5137 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5138 != REFERENCE_TYPE)
5139 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5140 tree omp_out = placeholder;
5141 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5142 if (need_static_cast)
5144 tree rtype = build_reference_type (atype);
5145 omp_out = build_static_cast (rtype, omp_out,
5146 tf_warning_or_error);
5147 omp_in = build_static_cast (rtype, omp_in,
5148 tf_warning_or_error);
5149 if (omp_out == error_mark_node || omp_in == error_mark_node)
5150 return true;
5151 omp_out = convert_from_reference (omp_out);
5152 omp_in = convert_from_reference (omp_in);
5154 OMP_CLAUSE_REDUCTION_MERGE (c)
5155 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5156 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5158 if (i >= 6)
5160 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5161 && TREE_CODE (stmts[4]) == DECL_EXPR);
5162 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5163 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5164 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5165 cxx_mark_addressable (placeholder);
5166 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5167 tree omp_orig = placeholder;
5168 if (need_static_cast)
5170 if (i == 7)
5172 error_at (OMP_CLAUSE_LOCATION (c),
5173 "user defined reduction with constructor "
5174 "initializer for base class %qT", atype);
5175 return true;
5177 tree rtype = build_reference_type (atype);
5178 omp_priv = build_static_cast (rtype, omp_priv,
5179 tf_warning_or_error);
5180 omp_orig = build_static_cast (rtype, omp_orig,
5181 tf_warning_or_error);
5182 if (omp_priv == error_mark_node
5183 || omp_orig == error_mark_node)
5184 return true;
5185 omp_priv = convert_from_reference (omp_priv);
5186 omp_orig = convert_from_reference (omp_orig);
5188 if (i == 6)
5189 *need_default_ctor = true;
5190 OMP_CLAUSE_REDUCTION_INIT (c)
5191 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5192 DECL_EXPR_DECL (stmts[3]),
5193 omp_priv, omp_orig);
5194 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5195 find_omp_placeholder_r, placeholder, NULL))
5196 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5198 else if (i >= 3)
5200 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5201 *need_default_ctor = true;
5202 else
5204 tree init;
5205 tree v = convert_from_reference (t);
5206 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5207 init = build_constructor (TREE_TYPE (v), NULL);
5208 else
5209 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5210 OMP_CLAUSE_REDUCTION_INIT (c)
5211 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5216 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5217 *need_dtor = true;
5218 else
5220 error ("user defined reduction not found for %qD", t);
5221 return true;
5223 return false;
5226 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5227 Remove any elements from the list that are invalid. */
5229 tree
5230 finish_omp_clauses (tree clauses)
5232 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5233 bitmap_head aligned_head;
5234 tree c, t, *pc;
5235 bool branch_seen = false;
5236 bool copyprivate_seen = false;
5238 bitmap_obstack_initialize (NULL);
5239 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5240 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5241 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5242 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5244 for (pc = &clauses, c = clauses; c ; c = *pc)
5246 bool remove = false;
5248 switch (OMP_CLAUSE_CODE (c))
5250 case OMP_CLAUSE_SHARED:
5251 goto check_dup_generic;
5252 case OMP_CLAUSE_PRIVATE:
5253 goto check_dup_generic;
5254 case OMP_CLAUSE_REDUCTION:
5255 goto check_dup_generic;
5256 case OMP_CLAUSE_COPYPRIVATE:
5257 copyprivate_seen = true;
5258 goto check_dup_generic;
5259 case OMP_CLAUSE_COPYIN:
5260 goto check_dup_generic;
5261 case OMP_CLAUSE_LINEAR:
5262 t = OMP_CLAUSE_DECL (c);
5263 if (!type_dependent_expression_p (t)
5264 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5265 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5267 error ("linear clause applied to non-integral non-pointer "
5268 "variable with %qT type", TREE_TYPE (t));
5269 remove = true;
5270 break;
5272 t = OMP_CLAUSE_LINEAR_STEP (c);
5273 if (t == NULL_TREE)
5274 t = integer_one_node;
5275 if (t == error_mark_node)
5277 remove = true;
5278 break;
5280 else if (!type_dependent_expression_p (t)
5281 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5283 error ("linear step expression must be integral");
5284 remove = true;
5285 break;
5287 else
5289 t = mark_rvalue_use (t);
5290 if (!processing_template_decl)
5292 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5293 t = maybe_constant_value (t);
5294 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5295 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5296 == POINTER_TYPE)
5298 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5299 OMP_CLAUSE_DECL (c), t);
5300 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5301 MINUS_EXPR, sizetype, t,
5302 OMP_CLAUSE_DECL (c));
5303 if (t == error_mark_node)
5305 remove = true;
5306 break;
5309 else
5310 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5312 OMP_CLAUSE_LINEAR_STEP (c) = t;
5314 goto check_dup_generic;
5315 check_dup_generic:
5316 t = OMP_CLAUSE_DECL (c);
5317 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5319 if (processing_template_decl)
5320 break;
5321 if (DECL_P (t))
5322 error ("%qD is not a variable in clause %qs", t,
5323 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5324 else
5325 error ("%qE is not a variable in clause %qs", t,
5326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5327 remove = true;
5329 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5330 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5331 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5333 error ("%qD appears more than once in data clauses", t);
5334 remove = true;
5336 else
5337 bitmap_set_bit (&generic_head, DECL_UID (t));
5338 break;
5340 case OMP_CLAUSE_FIRSTPRIVATE:
5341 t = OMP_CLAUSE_DECL (c);
5342 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5344 if (processing_template_decl)
5345 break;
5346 if (DECL_P (t))
5347 error ("%qD is not a variable in clause %<firstprivate%>", t);
5348 else
5349 error ("%qE is not a variable in clause %<firstprivate%>", t);
5350 remove = true;
5352 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5353 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5355 error ("%qD appears more than once in data clauses", t);
5356 remove = true;
5358 else
5359 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5360 break;
5362 case OMP_CLAUSE_LASTPRIVATE:
5363 t = OMP_CLAUSE_DECL (c);
5364 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5366 if (processing_template_decl)
5367 break;
5368 if (DECL_P (t))
5369 error ("%qD is not a variable in clause %<lastprivate%>", t);
5370 else
5371 error ("%qE is not a variable in clause %<lastprivate%>", t);
5372 remove = true;
5374 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5375 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5377 error ("%qD appears more than once in data clauses", t);
5378 remove = true;
5380 else
5381 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5382 break;
5384 case OMP_CLAUSE_IF:
5385 t = OMP_CLAUSE_IF_EXPR (c);
5386 t = maybe_convert_cond (t);
5387 if (t == error_mark_node)
5388 remove = true;
5389 else if (!processing_template_decl)
5390 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5391 OMP_CLAUSE_IF_EXPR (c) = t;
5392 break;
5394 case OMP_CLAUSE_FINAL:
5395 t = OMP_CLAUSE_FINAL_EXPR (c);
5396 t = maybe_convert_cond (t);
5397 if (t == error_mark_node)
5398 remove = true;
5399 else if (!processing_template_decl)
5400 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5401 OMP_CLAUSE_FINAL_EXPR (c) = t;
5402 break;
5404 case OMP_CLAUSE_NUM_THREADS:
5405 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5406 if (t == error_mark_node)
5407 remove = true;
5408 else if (!type_dependent_expression_p (t)
5409 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5411 error ("num_threads expression must be integral");
5412 remove = true;
5414 else
5416 t = mark_rvalue_use (t);
5417 if (!processing_template_decl)
5418 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5419 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5421 break;
5423 case OMP_CLAUSE_SCHEDULE:
5424 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5425 if (t == NULL)
5427 else if (t == error_mark_node)
5428 remove = true;
5429 else if (!type_dependent_expression_p (t)
5430 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5431 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5432 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5434 error ("schedule chunk size expression must be integral");
5435 remove = true;
5437 else
5439 t = mark_rvalue_use (t);
5440 if (!processing_template_decl)
5442 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5443 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5445 t = convert_to_integer (long_integer_type_node, t);
5446 if (t == error_mark_node)
5448 remove = true;
5449 break;
5452 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5454 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5456 break;
5458 case OMP_CLAUSE_SIMDLEN:
5459 case OMP_CLAUSE_SAFELEN:
5460 t = OMP_CLAUSE_OPERAND (c, 0);
5461 if (t == error_mark_node)
5462 remove = true;
5463 else if (!type_dependent_expression_p (t)
5464 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5466 error ("%qs length expression must be integral",
5467 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5468 remove = true;
5470 else
5472 t = mark_rvalue_use (t);
5473 t = maybe_constant_value (t);
5474 if (!processing_template_decl)
5476 if (TREE_CODE (t) != INTEGER_CST
5477 || tree_int_cst_sgn (t) != 1)
5479 error ("%qs length expression must be positive constant"
5480 " integer expression",
5481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5482 remove = true;
5485 OMP_CLAUSE_OPERAND (c, 0) = t;
5487 break;
5489 case OMP_CLAUSE_NUM_TEAMS:
5490 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5491 if (t == error_mark_node)
5492 remove = true;
5493 else if (!type_dependent_expression_p (t)
5494 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5496 error ("%<num_teams%> expression must be integral");
5497 remove = true;
5499 else
5501 t = mark_rvalue_use (t);
5502 if (!processing_template_decl)
5503 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5504 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5506 break;
5508 case OMP_CLAUSE_THREAD_LIMIT:
5509 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5510 if (t == error_mark_node)
5511 remove = true;
5512 else if (!type_dependent_expression_p (t)
5513 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5515 error ("%<thread_limit%> expression must be integral");
5516 remove = true;
5518 else
5520 t = mark_rvalue_use (t);
5521 if (!processing_template_decl)
5522 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5523 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5525 break;
5527 case OMP_CLAUSE_DEVICE:
5528 t = OMP_CLAUSE_DEVICE_ID (c);
5529 if (t == error_mark_node)
5530 remove = true;
5531 else if (!type_dependent_expression_p (t)
5532 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5534 error ("%<device%> id must be integral");
5535 remove = true;
5537 else
5539 t = mark_rvalue_use (t);
5540 if (!processing_template_decl)
5541 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5542 OMP_CLAUSE_DEVICE_ID (c) = t;
5544 break;
5546 case OMP_CLAUSE_DIST_SCHEDULE:
5547 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5548 if (t == NULL)
5550 else 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 ("%<dist_schedule%> chunk size expression must be "
5556 "integral");
5557 remove = true;
5559 else
5561 t = mark_rvalue_use (t);
5562 if (!processing_template_decl)
5563 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5564 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5566 break;
5568 case OMP_CLAUSE_ALIGNED:
5569 t = OMP_CLAUSE_DECL (c);
5570 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5572 if (processing_template_decl)
5573 break;
5574 if (DECL_P (t))
5575 error ("%qD is not a variable in %<aligned%> clause", t);
5576 else
5577 error ("%qE is not a variable in %<aligned%> clause", t);
5578 remove = true;
5580 else if (!type_dependent_expression_p (t)
5581 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5582 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5583 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5584 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5585 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5586 != ARRAY_TYPE))))
5588 error_at (OMP_CLAUSE_LOCATION (c),
5589 "%qE in %<aligned%> clause is neither a pointer nor "
5590 "an array nor a reference to pointer or array", t);
5591 remove = true;
5593 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5595 error ("%qD appears more than once in %<aligned%> clauses", t);
5596 remove = true;
5598 else
5599 bitmap_set_bit (&aligned_head, DECL_UID (t));
5600 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5601 if (t == error_mark_node)
5602 remove = true;
5603 else if (t == NULL_TREE)
5604 break;
5605 else if (!type_dependent_expression_p (t)
5606 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5608 error ("%<aligned%> clause alignment expression must "
5609 "be integral");
5610 remove = true;
5612 else
5614 t = mark_rvalue_use (t);
5615 t = maybe_constant_value (t);
5616 if (!processing_template_decl)
5618 if (TREE_CODE (t) != INTEGER_CST
5619 || tree_int_cst_sgn (t) != 1)
5621 error ("%<aligned%> clause alignment expression must be "
5622 "positive constant integer expression");
5623 remove = true;
5626 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5628 break;
5630 case OMP_CLAUSE_DEPEND:
5631 t = OMP_CLAUSE_DECL (c);
5632 if (TREE_CODE (t) == TREE_LIST)
5634 if (handle_omp_array_sections (c))
5635 remove = true;
5636 break;
5638 if (t == error_mark_node)
5639 remove = true;
5640 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5642 if (processing_template_decl)
5643 break;
5644 if (DECL_P (t))
5645 error ("%qD is not a variable in %<depend%> clause", t);
5646 else
5647 error ("%qE is not a variable in %<depend%> clause", t);
5648 remove = true;
5650 else if (!processing_template_decl
5651 && !cxx_mark_addressable (t))
5652 remove = true;
5653 break;
5655 case OMP_CLAUSE_MAP:
5656 case OMP_CLAUSE_TO:
5657 case OMP_CLAUSE_FROM:
5658 t = OMP_CLAUSE_DECL (c);
5659 if (TREE_CODE (t) == TREE_LIST)
5661 if (handle_omp_array_sections (c))
5662 remove = true;
5663 else
5665 t = OMP_CLAUSE_DECL (c);
5666 if (TREE_CODE (t) != TREE_LIST
5667 && !type_dependent_expression_p (t)
5668 && !cp_omp_mappable_type (TREE_TYPE (t)))
5670 error_at (OMP_CLAUSE_LOCATION (c),
5671 "array section does not have mappable type "
5672 "in %qs clause",
5673 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5674 remove = true;
5677 break;
5679 if (t == error_mark_node)
5680 remove = true;
5681 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5683 if (processing_template_decl)
5684 break;
5685 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5686 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5687 break;
5688 if (DECL_P (t))
5689 error ("%qD is not a variable in %qs clause", t,
5690 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5691 else
5692 error ("%qE is not a variable in %qs clause", t,
5693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5694 remove = true;
5696 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5698 error ("%qD is threadprivate variable in %qs clause", t,
5699 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5700 remove = true;
5702 else if (!processing_template_decl
5703 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5704 && !cxx_mark_addressable (t))
5705 remove = true;
5706 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5707 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5708 && !type_dependent_expression_p (t)
5709 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5710 == REFERENCE_TYPE)
5711 ? TREE_TYPE (TREE_TYPE (t))
5712 : TREE_TYPE (t)))
5714 error_at (OMP_CLAUSE_LOCATION (c),
5715 "%qD does not have a mappable type in %qs clause", t,
5716 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5717 remove = true;
5719 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5721 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5722 error ("%qD appears more than once in motion clauses", t);
5723 else
5724 error ("%qD appears more than once in map clauses", t);
5725 remove = true;
5727 else
5728 bitmap_set_bit (&generic_head, DECL_UID (t));
5729 break;
5731 case OMP_CLAUSE_UNIFORM:
5732 t = OMP_CLAUSE_DECL (c);
5733 if (TREE_CODE (t) != PARM_DECL)
5735 if (processing_template_decl)
5736 break;
5737 if (DECL_P (t))
5738 error ("%qD is not an argument in %<uniform%> clause", t);
5739 else
5740 error ("%qE is not an argument in %<uniform%> clause", t);
5741 remove = true;
5742 break;
5744 goto check_dup_generic;
5746 case OMP_CLAUSE_NOWAIT:
5747 case OMP_CLAUSE_ORDERED:
5748 case OMP_CLAUSE_DEFAULT:
5749 case OMP_CLAUSE_UNTIED:
5750 case OMP_CLAUSE_COLLAPSE:
5751 case OMP_CLAUSE_MERGEABLE:
5752 case OMP_CLAUSE_PARALLEL:
5753 case OMP_CLAUSE_FOR:
5754 case OMP_CLAUSE_SECTIONS:
5755 case OMP_CLAUSE_TASKGROUP:
5756 case OMP_CLAUSE_PROC_BIND:
5757 case OMP_CLAUSE__CILK_FOR_COUNT_:
5758 break;
5760 case OMP_CLAUSE_INBRANCH:
5761 case OMP_CLAUSE_NOTINBRANCH:
5762 if (branch_seen)
5764 error ("%<inbranch%> clause is incompatible with "
5765 "%<notinbranch%>");
5766 remove = true;
5768 branch_seen = true;
5769 break;
5771 default:
5772 gcc_unreachable ();
5775 if (remove)
5776 *pc = OMP_CLAUSE_CHAIN (c);
5777 else
5778 pc = &OMP_CLAUSE_CHAIN (c);
5781 for (pc = &clauses, c = clauses; c ; c = *pc)
5783 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5784 bool remove = false;
5785 bool need_complete_non_reference = false;
5786 bool need_default_ctor = false;
5787 bool need_copy_ctor = false;
5788 bool need_copy_assignment = false;
5789 bool need_implicitly_determined = false;
5790 bool need_dtor = false;
5791 tree type, inner_type;
5793 switch (c_kind)
5795 case OMP_CLAUSE_SHARED:
5796 need_implicitly_determined = true;
5797 break;
5798 case OMP_CLAUSE_PRIVATE:
5799 need_complete_non_reference = true;
5800 need_default_ctor = true;
5801 need_dtor = true;
5802 need_implicitly_determined = true;
5803 break;
5804 case OMP_CLAUSE_FIRSTPRIVATE:
5805 need_complete_non_reference = true;
5806 need_copy_ctor = true;
5807 need_dtor = true;
5808 need_implicitly_determined = true;
5809 break;
5810 case OMP_CLAUSE_LASTPRIVATE:
5811 need_complete_non_reference = true;
5812 need_copy_assignment = true;
5813 need_implicitly_determined = true;
5814 break;
5815 case OMP_CLAUSE_REDUCTION:
5816 need_implicitly_determined = true;
5817 break;
5818 case OMP_CLAUSE_COPYPRIVATE:
5819 need_copy_assignment = true;
5820 break;
5821 case OMP_CLAUSE_COPYIN:
5822 need_copy_assignment = true;
5823 break;
5824 case OMP_CLAUSE_NOWAIT:
5825 if (copyprivate_seen)
5827 error_at (OMP_CLAUSE_LOCATION (c),
5828 "%<nowait%> clause must not be used together "
5829 "with %<copyprivate%>");
5830 *pc = OMP_CLAUSE_CHAIN (c);
5831 continue;
5833 /* FALLTHRU */
5834 default:
5835 pc = &OMP_CLAUSE_CHAIN (c);
5836 continue;
5839 t = OMP_CLAUSE_DECL (c);
5840 if (processing_template_decl
5841 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5843 pc = &OMP_CLAUSE_CHAIN (c);
5844 continue;
5847 switch (c_kind)
5849 case OMP_CLAUSE_LASTPRIVATE:
5850 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5852 need_default_ctor = true;
5853 need_dtor = true;
5855 break;
5857 case OMP_CLAUSE_REDUCTION:
5858 if (finish_omp_reduction_clause (c, &need_default_ctor,
5859 &need_dtor))
5860 remove = true;
5861 else
5862 t = OMP_CLAUSE_DECL (c);
5863 break;
5865 case OMP_CLAUSE_COPYIN:
5866 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5868 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5869 remove = true;
5871 break;
5873 default:
5874 break;
5877 if (need_complete_non_reference || need_copy_assignment)
5879 t = require_complete_type (t);
5880 if (t == error_mark_node)
5881 remove = true;
5882 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5883 && need_complete_non_reference)
5885 error ("%qE has reference type for %qs", t,
5886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5887 remove = true;
5890 if (need_implicitly_determined)
5892 const char *share_name = NULL;
5894 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5895 share_name = "threadprivate";
5896 else switch (cxx_omp_predetermined_sharing (t))
5898 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5899 break;
5900 case OMP_CLAUSE_DEFAULT_SHARED:
5901 /* const vars may be specified in firstprivate clause. */
5902 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5903 && cxx_omp_const_qual_no_mutable (t))
5904 break;
5905 share_name = "shared";
5906 break;
5907 case OMP_CLAUSE_DEFAULT_PRIVATE:
5908 share_name = "private";
5909 break;
5910 default:
5911 gcc_unreachable ();
5913 if (share_name)
5915 error ("%qE is predetermined %qs for %qs",
5916 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5917 remove = true;
5921 /* We're interested in the base element, not arrays. */
5922 inner_type = type = TREE_TYPE (t);
5923 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5924 inner_type = TREE_TYPE (inner_type);
5926 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5927 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5928 inner_type = TREE_TYPE (inner_type);
5930 /* Check for special function availability by building a call to one.
5931 Save the results, because later we won't be in the right context
5932 for making these queries. */
5933 if (CLASS_TYPE_P (inner_type)
5934 && COMPLETE_TYPE_P (inner_type)
5935 && (need_default_ctor || need_copy_ctor
5936 || need_copy_assignment || need_dtor)
5937 && !type_dependent_expression_p (t)
5938 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5939 need_copy_ctor, need_copy_assignment,
5940 need_dtor))
5941 remove = true;
5943 if (remove)
5944 *pc = OMP_CLAUSE_CHAIN (c);
5945 else
5946 pc = &OMP_CLAUSE_CHAIN (c);
5949 bitmap_obstack_release (NULL);
5950 return clauses;
5953 /* For all variables in the tree_list VARS, mark them as thread local. */
5955 void
5956 finish_omp_threadprivate (tree vars)
5958 tree t;
5960 /* Mark every variable in VARS to be assigned thread local storage. */
5961 for (t = vars; t; t = TREE_CHAIN (t))
5963 tree v = TREE_PURPOSE (t);
5965 if (error_operand_p (v))
5967 else if (!VAR_P (v))
5968 error ("%<threadprivate%> %qD is not file, namespace "
5969 "or block scope variable", v);
5970 /* If V had already been marked threadprivate, it doesn't matter
5971 whether it had been used prior to this point. */
5972 else if (TREE_USED (v)
5973 && (DECL_LANG_SPECIFIC (v) == NULL
5974 || !CP_DECL_THREADPRIVATE_P (v)))
5975 error ("%qE declared %<threadprivate%> after first use", v);
5976 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5977 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5978 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5979 error ("%<threadprivate%> %qE has incomplete type", v);
5980 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5981 && CP_DECL_CONTEXT (v) != current_class_type)
5982 error ("%<threadprivate%> %qE directive not "
5983 "in %qT definition", v, CP_DECL_CONTEXT (v));
5984 else
5986 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5987 if (DECL_LANG_SPECIFIC (v) == NULL)
5989 retrofit_lang_decl (v);
5991 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5992 after the allocation of the lang_decl structure. */
5993 if (DECL_DISCRIMINATOR_P (v))
5994 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5997 if (! DECL_THREAD_LOCAL_P (v))
5999 set_decl_tls_model (v, decl_default_tls_model (v));
6000 /* If rtl has been already set for this var, call
6001 make_decl_rtl once again, so that encode_section_info
6002 has a chance to look at the new decl flags. */
6003 if (DECL_RTL_SET_P (v))
6004 make_decl_rtl (v);
6006 CP_DECL_THREADPRIVATE_P (v) = 1;
6011 /* Build an OpenMP structured block. */
6013 tree
6014 begin_omp_structured_block (void)
6016 return do_pushlevel (sk_omp);
6019 tree
6020 finish_omp_structured_block (tree block)
6022 return do_poplevel (block);
6025 /* Similarly, except force the retention of the BLOCK. */
6027 tree
6028 begin_omp_parallel (void)
6030 keep_next_level (true);
6031 return begin_omp_structured_block ();
6034 tree
6035 finish_omp_parallel (tree clauses, tree body)
6037 tree stmt;
6039 body = finish_omp_structured_block (body);
6041 stmt = make_node (OMP_PARALLEL);
6042 TREE_TYPE (stmt) = void_type_node;
6043 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6044 OMP_PARALLEL_BODY (stmt) = body;
6046 return add_stmt (stmt);
6049 tree
6050 begin_omp_task (void)
6052 keep_next_level (true);
6053 return begin_omp_structured_block ();
6056 tree
6057 finish_omp_task (tree clauses, tree body)
6059 tree stmt;
6061 body = finish_omp_structured_block (body);
6063 stmt = make_node (OMP_TASK);
6064 TREE_TYPE (stmt) = void_type_node;
6065 OMP_TASK_CLAUSES (stmt) = clauses;
6066 OMP_TASK_BODY (stmt) = body;
6068 return add_stmt (stmt);
6071 /* Helper function for finish_omp_for. Convert Ith random access iterator
6072 into integral iterator. Return FALSE if successful. */
6074 static bool
6075 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6076 tree condv, tree incrv, tree *body,
6077 tree *pre_body, tree clauses, tree *lastp)
6079 tree diff, iter_init, iter_incr = NULL, last;
6080 tree incr_var = NULL, orig_pre_body, orig_body, c;
6081 tree decl = TREE_VEC_ELT (declv, i);
6082 tree init = TREE_VEC_ELT (initv, i);
6083 tree cond = TREE_VEC_ELT (condv, i);
6084 tree incr = TREE_VEC_ELT (incrv, i);
6085 tree iter = decl;
6086 location_t elocus = locus;
6088 if (init && EXPR_HAS_LOCATION (init))
6089 elocus = EXPR_LOCATION (init);
6091 switch (TREE_CODE (cond))
6093 case GT_EXPR:
6094 case GE_EXPR:
6095 case LT_EXPR:
6096 case LE_EXPR:
6097 case NE_EXPR:
6098 if (TREE_OPERAND (cond, 1) == iter)
6099 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6100 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6101 if (TREE_OPERAND (cond, 0) != iter)
6102 cond = error_mark_node;
6103 else
6105 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6106 TREE_CODE (cond),
6107 iter, ERROR_MARK,
6108 TREE_OPERAND (cond, 1), ERROR_MARK,
6109 NULL, tf_warning_or_error);
6110 if (error_operand_p (tem))
6111 return true;
6113 break;
6114 default:
6115 cond = error_mark_node;
6116 break;
6118 if (cond == error_mark_node)
6120 error_at (elocus, "invalid controlling predicate");
6121 return true;
6123 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6124 ERROR_MARK, iter, ERROR_MARK, NULL,
6125 tf_warning_or_error);
6126 if (error_operand_p (diff))
6127 return true;
6128 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6130 error_at (elocus, "difference between %qE and %qD does not have integer type",
6131 TREE_OPERAND (cond, 1), iter);
6132 return true;
6135 switch (TREE_CODE (incr))
6137 case PREINCREMENT_EXPR:
6138 case PREDECREMENT_EXPR:
6139 case POSTINCREMENT_EXPR:
6140 case POSTDECREMENT_EXPR:
6141 if (TREE_OPERAND (incr, 0) != iter)
6143 incr = error_mark_node;
6144 break;
6146 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6147 TREE_CODE (incr), iter,
6148 tf_warning_or_error);
6149 if (error_operand_p (iter_incr))
6150 return true;
6151 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6152 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6153 incr = integer_one_node;
6154 else
6155 incr = integer_minus_one_node;
6156 break;
6157 case MODIFY_EXPR:
6158 if (TREE_OPERAND (incr, 0) != iter)
6159 incr = error_mark_node;
6160 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6161 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6163 tree rhs = TREE_OPERAND (incr, 1);
6164 if (TREE_OPERAND (rhs, 0) == iter)
6166 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6167 != INTEGER_TYPE)
6168 incr = error_mark_node;
6169 else
6171 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6172 iter, TREE_CODE (rhs),
6173 TREE_OPERAND (rhs, 1),
6174 tf_warning_or_error);
6175 if (error_operand_p (iter_incr))
6176 return true;
6177 incr = TREE_OPERAND (rhs, 1);
6178 incr = cp_convert (TREE_TYPE (diff), incr,
6179 tf_warning_or_error);
6180 if (TREE_CODE (rhs) == MINUS_EXPR)
6182 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6183 incr = fold_if_not_in_template (incr);
6185 if (TREE_CODE (incr) != INTEGER_CST
6186 && (TREE_CODE (incr) != NOP_EXPR
6187 || (TREE_CODE (TREE_OPERAND (incr, 0))
6188 != INTEGER_CST)))
6189 iter_incr = NULL;
6192 else if (TREE_OPERAND (rhs, 1) == iter)
6194 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6195 || TREE_CODE (rhs) != PLUS_EXPR)
6196 incr = error_mark_node;
6197 else
6199 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6200 PLUS_EXPR,
6201 TREE_OPERAND (rhs, 0),
6202 ERROR_MARK, iter,
6203 ERROR_MARK, NULL,
6204 tf_warning_or_error);
6205 if (error_operand_p (iter_incr))
6206 return true;
6207 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6208 iter, NOP_EXPR,
6209 iter_incr,
6210 tf_warning_or_error);
6211 if (error_operand_p (iter_incr))
6212 return true;
6213 incr = TREE_OPERAND (rhs, 0);
6214 iter_incr = NULL;
6217 else
6218 incr = error_mark_node;
6220 else
6221 incr = error_mark_node;
6222 break;
6223 default:
6224 incr = error_mark_node;
6225 break;
6228 if (incr == error_mark_node)
6230 error_at (elocus, "invalid increment expression");
6231 return true;
6234 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6235 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6236 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6237 && OMP_CLAUSE_DECL (c) == iter)
6238 break;
6240 decl = create_temporary_var (TREE_TYPE (diff));
6241 pushdecl (decl);
6242 add_decl_expr (decl);
6243 last = create_temporary_var (TREE_TYPE (diff));
6244 pushdecl (last);
6245 add_decl_expr (last);
6246 if (c && iter_incr == NULL)
6248 incr_var = create_temporary_var (TREE_TYPE (diff));
6249 pushdecl (incr_var);
6250 add_decl_expr (incr_var);
6252 gcc_assert (stmts_are_full_exprs_p ());
6254 orig_pre_body = *pre_body;
6255 *pre_body = push_stmt_list ();
6256 if (orig_pre_body)
6257 add_stmt (orig_pre_body);
6258 if (init != NULL)
6259 finish_expr_stmt (build_x_modify_expr (elocus,
6260 iter, NOP_EXPR, init,
6261 tf_warning_or_error));
6262 init = build_int_cst (TREE_TYPE (diff), 0);
6263 if (c && iter_incr == NULL)
6265 finish_expr_stmt (build_x_modify_expr (elocus,
6266 incr_var, NOP_EXPR,
6267 incr, tf_warning_or_error));
6268 incr = incr_var;
6269 iter_incr = build_x_modify_expr (elocus,
6270 iter, PLUS_EXPR, incr,
6271 tf_warning_or_error);
6273 finish_expr_stmt (build_x_modify_expr (elocus,
6274 last, NOP_EXPR, init,
6275 tf_warning_or_error));
6276 *pre_body = pop_stmt_list (*pre_body);
6278 cond = cp_build_binary_op (elocus,
6279 TREE_CODE (cond), decl, diff,
6280 tf_warning_or_error);
6281 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6282 elocus, incr, NULL_TREE);
6284 orig_body = *body;
6285 *body = push_stmt_list ();
6286 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6287 iter_init = build_x_modify_expr (elocus,
6288 iter, PLUS_EXPR, iter_init,
6289 tf_warning_or_error);
6290 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6291 finish_expr_stmt (iter_init);
6292 finish_expr_stmt (build_x_modify_expr (elocus,
6293 last, NOP_EXPR, decl,
6294 tf_warning_or_error));
6295 add_stmt (orig_body);
6296 *body = pop_stmt_list (*body);
6298 if (c)
6300 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6301 finish_expr_stmt (iter_incr);
6302 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6303 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6306 TREE_VEC_ELT (declv, i) = decl;
6307 TREE_VEC_ELT (initv, i) = init;
6308 TREE_VEC_ELT (condv, i) = cond;
6309 TREE_VEC_ELT (incrv, i) = incr;
6310 *lastp = last;
6312 return false;
6315 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6316 are directly for their associated operands in the statement. DECL
6317 and INIT are a combo; if DECL is NULL then INIT ought to be a
6318 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6319 optional statements that need to go before the loop into its
6320 sk_omp scope. */
6322 tree
6323 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6324 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6326 tree omp_for = NULL, orig_incr = NULL;
6327 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6328 tree last = NULL_TREE;
6329 location_t elocus;
6330 int i;
6332 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6333 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6334 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6335 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6337 decl = TREE_VEC_ELT (declv, i);
6338 init = TREE_VEC_ELT (initv, i);
6339 cond = TREE_VEC_ELT (condv, i);
6340 incr = TREE_VEC_ELT (incrv, i);
6341 elocus = locus;
6343 if (decl == NULL)
6345 if (init != NULL)
6346 switch (TREE_CODE (init))
6348 case MODIFY_EXPR:
6349 decl = TREE_OPERAND (init, 0);
6350 init = TREE_OPERAND (init, 1);
6351 break;
6352 case MODOP_EXPR:
6353 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6355 decl = TREE_OPERAND (init, 0);
6356 init = TREE_OPERAND (init, 2);
6358 break;
6359 default:
6360 break;
6363 if (decl == NULL)
6365 error_at (locus,
6366 "expected iteration declaration or initialization");
6367 return NULL;
6371 if (init && EXPR_HAS_LOCATION (init))
6372 elocus = EXPR_LOCATION (init);
6374 if (cond == NULL)
6376 error_at (elocus, "missing controlling predicate");
6377 return NULL;
6380 if (incr == NULL)
6382 error_at (elocus, "missing increment expression");
6383 return NULL;
6386 TREE_VEC_ELT (declv, i) = decl;
6387 TREE_VEC_ELT (initv, i) = init;
6390 if (dependent_omp_for_p (declv, initv, condv, incrv))
6392 tree stmt;
6394 stmt = make_node (code);
6396 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6398 /* This is really just a place-holder. We'll be decomposing this
6399 again and going through the cp_build_modify_expr path below when
6400 we instantiate the thing. */
6401 TREE_VEC_ELT (initv, i)
6402 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6403 TREE_VEC_ELT (initv, i));
6406 TREE_TYPE (stmt) = void_type_node;
6407 OMP_FOR_INIT (stmt) = initv;
6408 OMP_FOR_COND (stmt) = condv;
6409 OMP_FOR_INCR (stmt) = incrv;
6410 OMP_FOR_BODY (stmt) = body;
6411 OMP_FOR_PRE_BODY (stmt) = pre_body;
6412 OMP_FOR_CLAUSES (stmt) = clauses;
6414 SET_EXPR_LOCATION (stmt, locus);
6415 return add_stmt (stmt);
6418 if (processing_template_decl)
6419 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6421 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6423 decl = TREE_VEC_ELT (declv, i);
6424 init = TREE_VEC_ELT (initv, i);
6425 cond = TREE_VEC_ELT (condv, i);
6426 incr = TREE_VEC_ELT (incrv, i);
6427 if (orig_incr)
6428 TREE_VEC_ELT (orig_incr, i) = incr;
6429 elocus = locus;
6431 if (init && EXPR_HAS_LOCATION (init))
6432 elocus = EXPR_LOCATION (init);
6434 if (!DECL_P (decl))
6436 error_at (elocus, "expected iteration declaration or initialization");
6437 return NULL;
6440 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6442 if (orig_incr)
6443 TREE_VEC_ELT (orig_incr, i) = incr;
6444 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6445 TREE_CODE (TREE_OPERAND (incr, 1)),
6446 TREE_OPERAND (incr, 2),
6447 tf_warning_or_error);
6450 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6452 if (code == OMP_SIMD)
6454 error_at (elocus, "%<#pragma omp simd%> used with class "
6455 "iteration variable %qE", decl);
6456 return NULL;
6458 if (code == CILK_FOR && i == 0)
6459 orig_decl = decl;
6460 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6461 incrv, &body, &pre_body,
6462 clauses, &last))
6463 return NULL;
6464 continue;
6467 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6468 && !TYPE_PTR_P (TREE_TYPE (decl)))
6470 error_at (elocus, "invalid type for iteration variable %qE", decl);
6471 return NULL;
6474 if (!processing_template_decl)
6476 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6477 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6479 else
6480 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6481 if (cond
6482 && TREE_SIDE_EFFECTS (cond)
6483 && COMPARISON_CLASS_P (cond)
6484 && !processing_template_decl)
6486 tree t = TREE_OPERAND (cond, 0);
6487 if (TREE_SIDE_EFFECTS (t)
6488 && t != decl
6489 && (TREE_CODE (t) != NOP_EXPR
6490 || TREE_OPERAND (t, 0) != decl))
6491 TREE_OPERAND (cond, 0)
6492 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6494 t = TREE_OPERAND (cond, 1);
6495 if (TREE_SIDE_EFFECTS (t)
6496 && t != decl
6497 && (TREE_CODE (t) != NOP_EXPR
6498 || TREE_OPERAND (t, 0) != decl))
6499 TREE_OPERAND (cond, 1)
6500 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6502 if (decl == error_mark_node || init == error_mark_node)
6503 return NULL;
6505 TREE_VEC_ELT (declv, i) = decl;
6506 TREE_VEC_ELT (initv, i) = init;
6507 TREE_VEC_ELT (condv, i) = cond;
6508 TREE_VEC_ELT (incrv, i) = incr;
6509 i++;
6512 if (IS_EMPTY_STMT (pre_body))
6513 pre_body = NULL;
6515 if (code == CILK_FOR && !processing_template_decl)
6516 block = push_stmt_list ();
6518 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6519 body, pre_body);
6521 if (omp_for == NULL)
6523 if (block)
6524 pop_stmt_list (block);
6525 return NULL;
6528 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6530 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6531 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6533 if (TREE_CODE (incr) != MODIFY_EXPR)
6534 continue;
6536 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6537 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6538 && !processing_template_decl)
6540 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6541 if (TREE_SIDE_EFFECTS (t)
6542 && t != decl
6543 && (TREE_CODE (t) != NOP_EXPR
6544 || TREE_OPERAND (t, 0) != decl))
6545 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6546 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6548 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6549 if (TREE_SIDE_EFFECTS (t)
6550 && t != decl
6551 && (TREE_CODE (t) != NOP_EXPR
6552 || TREE_OPERAND (t, 0) != decl))
6553 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6554 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6557 if (orig_incr)
6558 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6560 OMP_FOR_CLAUSES (omp_for) = clauses;
6562 if (block)
6564 tree omp_par = make_node (OMP_PARALLEL);
6565 TREE_TYPE (omp_par) = void_type_node;
6566 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6567 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6568 TREE_SIDE_EFFECTS (bind) = 1;
6569 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6570 OMP_PARALLEL_BODY (omp_par) = bind;
6571 if (OMP_FOR_PRE_BODY (omp_for))
6573 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6574 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6576 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6577 decl = TREE_OPERAND (init, 0);
6578 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6579 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6580 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6581 clauses = OMP_FOR_CLAUSES (omp_for);
6582 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6583 for (pc = &clauses; *pc; )
6584 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6586 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6587 OMP_FOR_CLAUSES (omp_for) = *pc;
6588 *pc = OMP_CLAUSE_CHAIN (*pc);
6589 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6591 else
6593 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6594 pc = &OMP_CLAUSE_CHAIN (*pc);
6596 if (TREE_CODE (t) != INTEGER_CST)
6598 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6599 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6600 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6601 OMP_CLAUSE_CHAIN (c) = clauses;
6602 clauses = c;
6604 if (TREE_CODE (incr) == MODIFY_EXPR)
6606 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6607 if (TREE_CODE (t) != INTEGER_CST)
6609 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6610 = get_temp_regvar (TREE_TYPE (t), t);
6611 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6612 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6613 OMP_CLAUSE_CHAIN (c) = clauses;
6614 clauses = c;
6617 t = TREE_OPERAND (init, 1);
6618 if (TREE_CODE (t) != INTEGER_CST)
6620 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6621 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6622 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6623 OMP_CLAUSE_CHAIN (c) = clauses;
6624 clauses = c;
6626 if (orig_decl && orig_decl != decl)
6628 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6629 OMP_CLAUSE_DECL (c) = orig_decl;
6630 OMP_CLAUSE_CHAIN (c) = clauses;
6631 clauses = c;
6633 if (last)
6635 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6636 OMP_CLAUSE_DECL (c) = last;
6637 OMP_CLAUSE_CHAIN (c) = clauses;
6638 clauses = c;
6640 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6641 OMP_CLAUSE_DECL (c) = decl;
6642 OMP_CLAUSE_CHAIN (c) = clauses;
6643 clauses = c;
6644 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6645 OMP_CLAUSE_OPERAND (c, 0)
6646 = cilk_for_number_of_iterations (omp_for);
6647 OMP_CLAUSE_CHAIN (c) = clauses;
6648 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6649 add_stmt (omp_par);
6650 return omp_par;
6652 else if (code == CILK_FOR && processing_template_decl)
6654 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6655 if (orig_decl && orig_decl != decl)
6657 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6658 OMP_CLAUSE_DECL (c) = orig_decl;
6659 OMP_CLAUSE_CHAIN (c) = clauses;
6660 clauses = c;
6662 if (last)
6664 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6665 OMP_CLAUSE_DECL (c) = last;
6666 OMP_CLAUSE_CHAIN (c) = clauses;
6667 clauses = c;
6669 OMP_FOR_CLAUSES (omp_for) = clauses;
6671 return omp_for;
6674 void
6675 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6676 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6678 tree orig_lhs;
6679 tree orig_rhs;
6680 tree orig_v;
6681 tree orig_lhs1;
6682 tree orig_rhs1;
6683 bool dependent_p;
6684 tree stmt;
6686 orig_lhs = lhs;
6687 orig_rhs = rhs;
6688 orig_v = v;
6689 orig_lhs1 = lhs1;
6690 orig_rhs1 = rhs1;
6691 dependent_p = false;
6692 stmt = NULL_TREE;
6694 /* Even in a template, we can detect invalid uses of the atomic
6695 pragma if neither LHS nor RHS is type-dependent. */
6696 if (processing_template_decl)
6698 dependent_p = (type_dependent_expression_p (lhs)
6699 || (rhs && type_dependent_expression_p (rhs))
6700 || (v && type_dependent_expression_p (v))
6701 || (lhs1 && type_dependent_expression_p (lhs1))
6702 || (rhs1 && type_dependent_expression_p (rhs1)));
6703 if (!dependent_p)
6705 lhs = build_non_dependent_expr (lhs);
6706 if (rhs)
6707 rhs = build_non_dependent_expr (rhs);
6708 if (v)
6709 v = build_non_dependent_expr (v);
6710 if (lhs1)
6711 lhs1 = build_non_dependent_expr (lhs1);
6712 if (rhs1)
6713 rhs1 = build_non_dependent_expr (rhs1);
6716 if (!dependent_p)
6718 bool swapped = false;
6719 if (rhs1 && cp_tree_equal (lhs, rhs))
6721 tree tem = rhs;
6722 rhs = rhs1;
6723 rhs1 = tem;
6724 swapped = !commutative_tree_code (opcode);
6726 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6728 if (code == OMP_ATOMIC)
6729 error ("%<#pragma omp atomic update%> uses two different "
6730 "expressions for memory");
6731 else
6732 error ("%<#pragma omp atomic capture%> uses two different "
6733 "expressions for memory");
6734 return;
6736 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6738 if (code == OMP_ATOMIC)
6739 error ("%<#pragma omp atomic update%> uses two different "
6740 "expressions for memory");
6741 else
6742 error ("%<#pragma omp atomic capture%> uses two different "
6743 "expressions for memory");
6744 return;
6746 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6747 v, lhs1, rhs1, swapped, seq_cst);
6748 if (stmt == error_mark_node)
6749 return;
6751 if (processing_template_decl)
6753 if (code == OMP_ATOMIC_READ)
6755 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6756 OMP_ATOMIC_READ, orig_lhs);
6757 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6758 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6760 else
6762 if (opcode == NOP_EXPR)
6763 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6764 else
6765 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6766 if (orig_rhs1)
6767 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6768 COMPOUND_EXPR, orig_rhs1, stmt);
6769 if (code != OMP_ATOMIC)
6771 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6772 code, orig_lhs1, stmt);
6773 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6774 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6777 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6778 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6780 finish_expr_stmt (stmt);
6783 void
6784 finish_omp_barrier (void)
6786 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6787 vec<tree, va_gc> *vec = make_tree_vector ();
6788 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6789 release_tree_vector (vec);
6790 finish_expr_stmt (stmt);
6793 void
6794 finish_omp_flush (void)
6796 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6797 vec<tree, va_gc> *vec = make_tree_vector ();
6798 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6799 release_tree_vector (vec);
6800 finish_expr_stmt (stmt);
6803 void
6804 finish_omp_taskwait (void)
6806 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6807 vec<tree, va_gc> *vec = make_tree_vector ();
6808 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6809 release_tree_vector (vec);
6810 finish_expr_stmt (stmt);
6813 void
6814 finish_omp_taskyield (void)
6816 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6817 vec<tree, va_gc> *vec = make_tree_vector ();
6818 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6819 release_tree_vector (vec);
6820 finish_expr_stmt (stmt);
6823 void
6824 finish_omp_cancel (tree clauses)
6826 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6827 int mask = 0;
6828 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6829 mask = 1;
6830 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6831 mask = 2;
6832 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6833 mask = 4;
6834 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6835 mask = 8;
6836 else
6838 error ("%<#pragma omp cancel must specify one of "
6839 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6840 return;
6842 vec<tree, va_gc> *vec = make_tree_vector ();
6843 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6844 if (ifc != NULL_TREE)
6846 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6847 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6848 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6849 build_zero_cst (type));
6851 else
6852 ifc = boolean_true_node;
6853 vec->quick_push (build_int_cst (integer_type_node, mask));
6854 vec->quick_push (ifc);
6855 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6856 release_tree_vector (vec);
6857 finish_expr_stmt (stmt);
6860 void
6861 finish_omp_cancellation_point (tree clauses)
6863 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6864 int mask = 0;
6865 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6866 mask = 1;
6867 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6868 mask = 2;
6869 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6870 mask = 4;
6871 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6872 mask = 8;
6873 else
6875 error ("%<#pragma omp cancellation point must specify one of "
6876 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6877 return;
6879 vec<tree, va_gc> *vec
6880 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6881 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6882 release_tree_vector (vec);
6883 finish_expr_stmt (stmt);
6886 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6887 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6888 should create an extra compound stmt. */
6890 tree
6891 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6893 tree r;
6895 if (pcompound)
6896 *pcompound = begin_compound_stmt (0);
6898 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6900 /* Only add the statement to the function if support enabled. */
6901 if (flag_tm)
6902 add_stmt (r);
6903 else
6904 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6905 ? G_("%<__transaction_relaxed%> without "
6906 "transactional memory support enabled")
6907 : G_("%<__transaction_atomic%> without "
6908 "transactional memory support enabled")));
6910 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6911 TREE_SIDE_EFFECTS (r) = 1;
6912 return r;
6915 /* End a __transaction_atomic or __transaction_relaxed statement.
6916 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6917 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6918 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6920 void
6921 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6923 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6924 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6925 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6926 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6928 /* noexcept specifications are not allowed for function transactions. */
6929 gcc_assert (!(noex && compound_stmt));
6930 if (noex)
6932 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6933 noex);
6934 /* This may not be true when the STATEMENT_LIST is empty. */
6935 if (EXPR_P (body))
6936 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6937 TREE_SIDE_EFFECTS (body) = 1;
6938 TRANSACTION_EXPR_BODY (stmt) = body;
6941 if (compound_stmt)
6942 finish_compound_stmt (compound_stmt);
6945 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6946 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6947 condition. */
6949 tree
6950 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6952 tree ret;
6953 if (noex)
6955 expr = build_must_not_throw_expr (expr, noex);
6956 if (EXPR_P (expr))
6957 SET_EXPR_LOCATION (expr, loc);
6958 TREE_SIDE_EFFECTS (expr) = 1;
6960 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6961 if (flags & TM_STMT_ATTR_RELAXED)
6962 TRANSACTION_EXPR_RELAXED (ret) = 1;
6963 TREE_SIDE_EFFECTS (ret) = 1;
6964 SET_EXPR_LOCATION (ret, loc);
6965 return ret;
6968 void
6969 init_cp_semantics (void)
6973 /* Build a STATIC_ASSERT for a static assertion with the condition
6974 CONDITION and the message text MESSAGE. LOCATION is the location
6975 of the static assertion in the source code. When MEMBER_P, this
6976 static assertion is a member of a class. */
6977 void
6978 finish_static_assert (tree condition, tree message, location_t location,
6979 bool member_p)
6981 if (message == NULL_TREE
6982 || message == error_mark_node
6983 || condition == NULL_TREE
6984 || condition == error_mark_node)
6985 return;
6987 if (check_for_bare_parameter_packs (condition))
6988 condition = error_mark_node;
6990 if (type_dependent_expression_p (condition)
6991 || value_dependent_expression_p (condition))
6993 /* We're in a template; build a STATIC_ASSERT and put it in
6994 the right place. */
6995 tree assertion;
6997 assertion = make_node (STATIC_ASSERT);
6998 STATIC_ASSERT_CONDITION (assertion) = condition;
6999 STATIC_ASSERT_MESSAGE (assertion) = message;
7000 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7002 if (member_p)
7003 maybe_add_class_template_decl_list (current_class_type,
7004 assertion,
7005 /*friend_p=*/0);
7006 else
7007 add_stmt (assertion);
7009 return;
7012 /* Fold the expression and convert it to a boolean value. */
7013 condition = fold_non_dependent_expr (condition);
7014 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7015 condition = maybe_constant_value (condition);
7017 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7018 /* Do nothing; the condition is satisfied. */
7020 else
7022 location_t saved_loc = input_location;
7024 input_location = location;
7025 if (TREE_CODE (condition) == INTEGER_CST
7026 && integer_zerop (condition))
7027 /* Report the error. */
7028 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7029 else if (condition && condition != error_mark_node)
7031 error ("non-constant condition for static assertion");
7032 if (require_potential_rvalue_constant_expression (condition))
7033 cxx_constant_value (condition);
7035 input_location = saved_loc;
7039 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7040 suitable for use as a type-specifier.
7042 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7043 id-expression or a class member access, FALSE when it was parsed as
7044 a full expression. */
7046 tree
7047 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7048 tsubst_flags_t complain)
7050 tree type = NULL_TREE;
7052 if (!expr || error_operand_p (expr))
7053 return error_mark_node;
7055 if (TYPE_P (expr)
7056 || TREE_CODE (expr) == TYPE_DECL
7057 || (TREE_CODE (expr) == BIT_NOT_EXPR
7058 && TYPE_P (TREE_OPERAND (expr, 0))))
7060 if (complain & tf_error)
7061 error ("argument to decltype must be an expression");
7062 return error_mark_node;
7065 /* Depending on the resolution of DR 1172, we may later need to distinguish
7066 instantiation-dependent but not type-dependent expressions so that, say,
7067 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7068 if (instantiation_dependent_expression_p (expr))
7070 type = cxx_make_type (DECLTYPE_TYPE);
7071 DECLTYPE_TYPE_EXPR (type) = expr;
7072 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7073 = id_expression_or_member_access_p;
7074 SET_TYPE_STRUCTURAL_EQUALITY (type);
7076 return type;
7079 /* The type denoted by decltype(e) is defined as follows: */
7081 expr = resolve_nondeduced_context (expr);
7083 if (invalid_nonstatic_memfn_p (expr, complain))
7084 return error_mark_node;
7086 if (type_unknown_p (expr))
7088 if (complain & tf_error)
7089 error ("decltype cannot resolve address of overloaded function");
7090 return error_mark_node;
7093 /* To get the size of a static data member declared as an array of
7094 unknown bound, we need to instantiate it. */
7095 if (VAR_P (expr)
7096 && VAR_HAD_UNKNOWN_BOUND (expr)
7097 && DECL_TEMPLATE_INSTANTIATION (expr))
7098 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7100 if (id_expression_or_member_access_p)
7102 /* If e is an id-expression or a class member access (5.2.5
7103 [expr.ref]), decltype(e) is defined as the type of the entity
7104 named by e. If there is no such entity, or e names a set of
7105 overloaded functions, the program is ill-formed. */
7106 if (identifier_p (expr))
7107 expr = lookup_name (expr);
7109 if (INDIRECT_REF_P (expr))
7110 /* This can happen when the expression is, e.g., "a.b". Just
7111 look at the underlying operand. */
7112 expr = TREE_OPERAND (expr, 0);
7114 if (TREE_CODE (expr) == OFFSET_REF
7115 || TREE_CODE (expr) == MEMBER_REF
7116 || TREE_CODE (expr) == SCOPE_REF)
7117 /* We're only interested in the field itself. If it is a
7118 BASELINK, we will need to see through it in the next
7119 step. */
7120 expr = TREE_OPERAND (expr, 1);
7122 if (BASELINK_P (expr))
7123 /* See through BASELINK nodes to the underlying function. */
7124 expr = BASELINK_FUNCTIONS (expr);
7126 switch (TREE_CODE (expr))
7128 case FIELD_DECL:
7129 if (DECL_BIT_FIELD_TYPE (expr))
7131 type = DECL_BIT_FIELD_TYPE (expr);
7132 break;
7134 /* Fall through for fields that aren't bitfields. */
7136 case FUNCTION_DECL:
7137 case VAR_DECL:
7138 case CONST_DECL:
7139 case PARM_DECL:
7140 case RESULT_DECL:
7141 case TEMPLATE_PARM_INDEX:
7142 expr = mark_type_use (expr);
7143 type = TREE_TYPE (expr);
7144 break;
7146 case ERROR_MARK:
7147 type = error_mark_node;
7148 break;
7150 case COMPONENT_REF:
7151 case COMPOUND_EXPR:
7152 mark_type_use (expr);
7153 type = is_bitfield_expr_with_lowered_type (expr);
7154 if (!type)
7155 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7156 break;
7158 case BIT_FIELD_REF:
7159 gcc_unreachable ();
7161 case INTEGER_CST:
7162 case PTRMEM_CST:
7163 /* We can get here when the id-expression refers to an
7164 enumerator or non-type template parameter. */
7165 type = TREE_TYPE (expr);
7166 break;
7168 default:
7169 /* Handle instantiated template non-type arguments. */
7170 type = TREE_TYPE (expr);
7171 break;
7174 else
7176 /* Within a lambda-expression:
7178 Every occurrence of decltype((x)) where x is a possibly
7179 parenthesized id-expression that names an entity of
7180 automatic storage duration is treated as if x were
7181 transformed into an access to a corresponding data member
7182 of the closure type that would have been declared if x
7183 were a use of the denoted entity. */
7184 if (outer_automatic_var_p (expr)
7185 && current_function_decl
7186 && LAMBDA_FUNCTION_P (current_function_decl))
7187 type = capture_decltype (expr);
7188 else if (error_operand_p (expr))
7189 type = error_mark_node;
7190 else if (expr == current_class_ptr)
7191 /* If the expression is just "this", we want the
7192 cv-unqualified pointer for the "this" type. */
7193 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7194 else
7196 /* Otherwise, where T is the type of e, if e is an lvalue,
7197 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7198 cp_lvalue_kind clk = lvalue_kind (expr);
7199 type = unlowered_expr_type (expr);
7200 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7202 /* For vector types, pick a non-opaque variant. */
7203 if (TREE_CODE (type) == VECTOR_TYPE)
7204 type = strip_typedefs (type);
7206 if (clk != clk_none && !(clk & clk_class))
7207 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7211 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
7212 && (flag_iso || warn_vla > 0))
7214 if (complain & tf_warning_or_error)
7215 pedwarn (input_location, OPT_Wvla,
7216 "taking decltype of array of runtime bound");
7217 else
7218 return error_mark_node;
7221 return type;
7224 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7225 __has_nothrow_copy, depending on assign_p. */
7227 static bool
7228 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7230 tree fns;
7232 if (assign_p)
7234 int ix;
7235 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7236 if (ix < 0)
7237 return false;
7238 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7240 else if (TYPE_HAS_COPY_CTOR (type))
7242 /* If construction of the copy constructor was postponed, create
7243 it now. */
7244 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7245 lazily_declare_fn (sfk_copy_constructor, type);
7246 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7247 lazily_declare_fn (sfk_move_constructor, type);
7248 fns = CLASSTYPE_CONSTRUCTORS (type);
7250 else
7251 return false;
7253 for (; fns; fns = OVL_NEXT (fns))
7255 tree fn = OVL_CURRENT (fns);
7257 if (assign_p)
7259 if (copy_fn_p (fn) == 0)
7260 continue;
7262 else if (copy_fn_p (fn) <= 0)
7263 continue;
7265 maybe_instantiate_noexcept (fn);
7266 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7267 return false;
7270 return true;
7273 /* Actually evaluates the trait. */
7275 static bool
7276 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7278 enum tree_code type_code1;
7279 tree t;
7281 type_code1 = TREE_CODE (type1);
7283 switch (kind)
7285 case CPTK_HAS_NOTHROW_ASSIGN:
7286 type1 = strip_array_types (type1);
7287 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7288 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7289 || (CLASS_TYPE_P (type1)
7290 && classtype_has_nothrow_assign_or_copy_p (type1,
7291 true))));
7293 case CPTK_HAS_TRIVIAL_ASSIGN:
7294 /* ??? The standard seems to be missing the "or array of such a class
7295 type" wording for this trait. */
7296 type1 = strip_array_types (type1);
7297 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7298 && (trivial_type_p (type1)
7299 || (CLASS_TYPE_P (type1)
7300 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7302 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7303 type1 = strip_array_types (type1);
7304 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7305 || (CLASS_TYPE_P (type1)
7306 && (t = locate_ctor (type1))
7307 && (maybe_instantiate_noexcept (t),
7308 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7310 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7311 type1 = strip_array_types (type1);
7312 return (trivial_type_p (type1)
7313 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7315 case CPTK_HAS_NOTHROW_COPY:
7316 type1 = strip_array_types (type1);
7317 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7318 || (CLASS_TYPE_P (type1)
7319 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7321 case CPTK_HAS_TRIVIAL_COPY:
7322 /* ??? The standard seems to be missing the "or array of such a class
7323 type" wording for this trait. */
7324 type1 = strip_array_types (type1);
7325 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7326 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7328 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7329 type1 = strip_array_types (type1);
7330 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7331 || (CLASS_TYPE_P (type1)
7332 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7334 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7335 return type_has_virtual_destructor (type1);
7337 case CPTK_IS_ABSTRACT:
7338 return (ABSTRACT_CLASS_TYPE_P (type1));
7340 case CPTK_IS_BASE_OF:
7341 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7342 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7343 || DERIVED_FROM_P (type1, type2)));
7345 case CPTK_IS_CLASS:
7346 return (NON_UNION_CLASS_TYPE_P (type1));
7348 case CPTK_IS_EMPTY:
7349 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7351 case CPTK_IS_ENUM:
7352 return (type_code1 == ENUMERAL_TYPE);
7354 case CPTK_IS_FINAL:
7355 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7357 case CPTK_IS_LITERAL_TYPE:
7358 return (literal_type_p (type1));
7360 case CPTK_IS_POD:
7361 return (pod_type_p (type1));
7363 case CPTK_IS_POLYMORPHIC:
7364 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7366 case CPTK_IS_STD_LAYOUT:
7367 return (std_layout_type_p (type1));
7369 case CPTK_IS_TRIVIAL:
7370 return (trivial_type_p (type1));
7372 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7373 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7375 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7376 return is_trivially_xible (INIT_EXPR, type1, type2);
7378 case CPTK_IS_TRIVIALLY_COPYABLE:
7379 return (trivially_copyable_p (type1));
7381 case CPTK_IS_UNION:
7382 return (type_code1 == UNION_TYPE);
7384 default:
7385 gcc_unreachable ();
7386 return false;
7390 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7391 void, or a complete type, returns true, otherwise false. */
7393 static bool
7394 check_trait_type (tree type)
7396 if (type == NULL_TREE)
7397 return true;
7399 if (TREE_CODE (type) == TREE_LIST)
7400 return (check_trait_type (TREE_VALUE (type))
7401 && check_trait_type (TREE_CHAIN (type)));
7403 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7404 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7405 return true;
7407 if (VOID_TYPE_P (type))
7408 return true;
7410 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7413 /* Process a trait expression. */
7415 tree
7416 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7418 if (type1 == error_mark_node
7419 || type2 == error_mark_node)
7420 return error_mark_node;
7422 if (processing_template_decl)
7424 tree trait_expr = make_node (TRAIT_EXPR);
7425 TREE_TYPE (trait_expr) = boolean_type_node;
7426 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7427 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7428 TRAIT_EXPR_KIND (trait_expr) = kind;
7429 return trait_expr;
7432 switch (kind)
7434 case CPTK_HAS_NOTHROW_ASSIGN:
7435 case CPTK_HAS_TRIVIAL_ASSIGN:
7436 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7437 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7438 case CPTK_HAS_NOTHROW_COPY:
7439 case CPTK_HAS_TRIVIAL_COPY:
7440 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7441 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7442 case CPTK_IS_ABSTRACT:
7443 case CPTK_IS_EMPTY:
7444 case CPTK_IS_FINAL:
7445 case CPTK_IS_LITERAL_TYPE:
7446 case CPTK_IS_POD:
7447 case CPTK_IS_POLYMORPHIC:
7448 case CPTK_IS_STD_LAYOUT:
7449 case CPTK_IS_TRIVIAL:
7450 case CPTK_IS_TRIVIALLY_COPYABLE:
7451 if (!check_trait_type (type1))
7452 return error_mark_node;
7453 break;
7455 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7456 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7457 if (!check_trait_type (type1)
7458 || !check_trait_type (type2))
7459 return error_mark_node;
7460 break;
7462 case CPTK_IS_BASE_OF:
7463 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7464 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7465 && !complete_type_or_else (type2, NULL_TREE))
7466 /* We already issued an error. */
7467 return error_mark_node;
7468 break;
7470 case CPTK_IS_CLASS:
7471 case CPTK_IS_ENUM:
7472 case CPTK_IS_UNION:
7473 break;
7475 default:
7476 gcc_unreachable ();
7479 return (trait_expr_value (kind, type1, type2)
7480 ? boolean_true_node : boolean_false_node);
7483 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7484 which is ignored for C++. */
7486 void
7487 set_float_const_decimal64 (void)
7491 void
7492 clear_float_const_decimal64 (void)
7496 bool
7497 float_const_decimal64_p (void)
7499 return 0;
7503 /* Return true if T designates the implied `this' parameter. */
7505 bool
7506 is_this_parameter (tree t)
7508 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7509 return false;
7510 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7511 return true;
7514 /* Insert the deduced return type for an auto function. */
7516 void
7517 apply_deduced_return_type (tree fco, tree return_type)
7519 tree result;
7521 if (return_type == error_mark_node)
7522 return;
7524 if (LAMBDA_FUNCTION_P (fco))
7526 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7527 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7530 if (DECL_CONV_FN_P (fco))
7531 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7533 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7535 result = DECL_RESULT (fco);
7536 if (result == NULL_TREE)
7537 return;
7538 if (TREE_TYPE (result) == return_type)
7539 return;
7541 /* We already have a DECL_RESULT from start_preparsed_function.
7542 Now we need to redo the work it and allocate_struct_function
7543 did to reflect the new type. */
7544 gcc_assert (current_function_decl == fco);
7545 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7546 TYPE_MAIN_VARIANT (return_type));
7547 DECL_ARTIFICIAL (result) = 1;
7548 DECL_IGNORED_P (result) = 1;
7549 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7550 result);
7552 DECL_RESULT (fco) = result;
7554 if (!processing_template_decl)
7556 if (!VOID_TYPE_P (TREE_TYPE (result)))
7557 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7558 bool aggr = aggregate_value_p (result, fco);
7559 #ifdef PCC_STATIC_STRUCT_RETURN
7560 cfun->returns_pcc_struct = aggr;
7561 #endif
7562 cfun->returns_struct = aggr;
7567 /* DECL is a local variable or parameter from the surrounding scope of a
7568 lambda-expression. Returns the decltype for a use of the capture field
7569 for DECL even if it hasn't been captured yet. */
7571 static tree
7572 capture_decltype (tree decl)
7574 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7575 /* FIXME do lookup instead of list walk? */
7576 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7577 tree type;
7579 if (cap)
7580 type = TREE_TYPE (TREE_PURPOSE (cap));
7581 else
7582 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7584 case CPLD_NONE:
7585 error ("%qD is not captured", decl);
7586 return error_mark_node;
7588 case CPLD_COPY:
7589 type = TREE_TYPE (decl);
7590 if (TREE_CODE (type) == REFERENCE_TYPE
7591 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7592 type = TREE_TYPE (type);
7593 break;
7595 case CPLD_REFERENCE:
7596 type = TREE_TYPE (decl);
7597 if (TREE_CODE (type) != REFERENCE_TYPE)
7598 type = build_reference_type (TREE_TYPE (decl));
7599 break;
7601 default:
7602 gcc_unreachable ();
7605 if (TREE_CODE (type) != REFERENCE_TYPE)
7607 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7608 type = cp_build_qualified_type (type, (cp_type_quals (type)
7609 |TYPE_QUAL_CONST));
7610 type = build_reference_type (type);
7612 return type;
7615 #include "gt-cp-semantics.h"