* gimplify.c (gimplify_scan_omp_clauses): Initialize sc
[official-gcc.git] / gcc / cp / semantics.c
blob287bc379782fd5707c0f25796b97f8da14024e5c
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2015 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "alias.h"
31 #include "tree.h"
32 #include "stmt.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "cp-tree.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "tree-inline.h"
40 #include "intl.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "timevar.h"
44 #include "diagnostic.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "cgraph.h"
48 #include "tree-iterator.h"
49 #include "target.h"
50 #include "gimplify.h"
51 #include "bitmap.h"
52 #include "omp-low.h"
53 #include "builtins.h"
54 #include "convert.h"
55 #include "gomp-constants.h"
57 /* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
60 degenerate form of parsing. */
62 static tree maybe_convert_cond (tree);
63 static tree finalize_nrv_r (tree *, int *, void *);
64 static tree capture_decltype (tree);
66 /* Used for OpenMP non-static data member privatization. */
68 static hash_map<tree, tree> *omp_private_member_map;
69 static vec<tree> omp_private_member_vec;
70 static bool omp_private_member_ignore_next;
73 /* Deferred Access Checking Overview
74 ---------------------------------
76 Most C++ expressions and declarations require access checking
77 to be performed during parsing. However, in several cases,
78 this has to be treated differently.
80 For member declarations, access checking has to be deferred
81 until more information about the declaration is known. For
82 example:
84 class A {
85 typedef int X;
86 public:
87 X f();
90 A::X A::f();
91 A::X g();
93 When we are parsing the function return type `A::X', we don't
94 really know if this is allowed until we parse the function name.
96 Furthermore, some contexts require that access checking is
97 never performed at all. These include class heads, and template
98 instantiations.
100 Typical use of access checking functions is described here:
102 1. When we enter a context that requires certain access checking
103 mode, the function `push_deferring_access_checks' is called with
104 DEFERRING argument specifying the desired mode. Access checking
105 may be performed immediately (dk_no_deferred), deferred
106 (dk_deferred), or not performed (dk_no_check).
108 2. When a declaration such as a type, or a variable, is encountered,
109 the function `perform_or_defer_access_check' is called. It
110 maintains a vector of all deferred checks.
112 3. The global `current_class_type' or `current_function_decl' is then
113 setup by the parser. `enforce_access' relies on these information
114 to check access.
116 4. Upon exiting the context mentioned in step 1,
117 `perform_deferred_access_checks' is called to check all declaration
118 stored in the vector. `pop_deferring_access_checks' is then
119 called to restore the previous access checking mode.
121 In case of parsing error, we simply call `pop_deferring_access_checks'
122 without `perform_deferred_access_checks'. */
124 typedef struct GTY(()) deferred_access {
125 /* A vector representing name-lookups for which we have deferred
126 checking access controls. We cannot check the accessibility of
127 names used in a decl-specifier-seq until we know what is being
128 declared because code like:
130 class A {
131 class B {};
132 B* f();
135 A::B* A::f() { return 0; }
137 is valid, even though `A::B' is not generally accessible. */
138 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
140 /* The current mode of access checks. */
141 enum deferring_kind deferring_access_checks_kind;
143 } deferred_access;
145 /* Data for deferred access checking. */
146 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
147 static GTY(()) unsigned deferred_access_no_check;
149 /* Save the current deferred access states and start deferred
150 access checking iff DEFER_P is true. */
152 void
153 push_deferring_access_checks (deferring_kind deferring)
155 /* For context like template instantiation, access checking
156 disabling applies to all nested context. */
157 if (deferred_access_no_check || deferring == dk_no_check)
158 deferred_access_no_check++;
159 else
161 deferred_access e = {NULL, deferring};
162 vec_safe_push (deferred_access_stack, e);
166 /* Save the current deferred access states and start deferred access
167 checking, continuing the set of deferred checks in CHECKS. */
169 void
170 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
172 push_deferring_access_checks (dk_deferred);
173 if (!deferred_access_no_check)
174 deferred_access_stack->last().deferred_access_checks = checks;
177 /* Resume deferring access checks again after we stopped doing
178 this previously. */
180 void
181 resume_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
187 /* Stop deferring access checks. */
189 void
190 stop_deferring_access_checks (void)
192 if (!deferred_access_no_check)
193 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
196 /* Discard the current deferred access checks and restore the
197 previous states. */
199 void
200 pop_deferring_access_checks (void)
202 if (deferred_access_no_check)
203 deferred_access_no_check--;
204 else
205 deferred_access_stack->pop ();
208 /* Returns a TREE_LIST representing the deferred checks.
209 The TREE_PURPOSE of each node is the type through which the
210 access occurred; the TREE_VALUE is the declaration named.
213 vec<deferred_access_check, va_gc> *
214 get_deferred_access_checks (void)
216 if (deferred_access_no_check)
217 return NULL;
218 else
219 return (deferred_access_stack->last().deferred_access_checks);
222 /* Take current deferred checks and combine with the
223 previous states if we also defer checks previously.
224 Otherwise perform checks now. */
226 void
227 pop_to_parent_deferring_access_checks (void)
229 if (deferred_access_no_check)
230 deferred_access_no_check--;
231 else
233 vec<deferred_access_check, va_gc> *checks;
234 deferred_access *ptr;
236 checks = (deferred_access_stack->last ().deferred_access_checks);
238 deferred_access_stack->pop ();
239 ptr = &deferred_access_stack->last ();
240 if (ptr->deferring_access_checks_kind == dk_no_deferred)
242 /* Check access. */
243 perform_access_checks (checks, tf_warning_or_error);
245 else
247 /* Merge with parent. */
248 int i, j;
249 deferred_access_check *chk, *probe;
251 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
253 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
255 if (probe->binfo == chk->binfo &&
256 probe->decl == chk->decl &&
257 probe->diag_decl == chk->diag_decl)
258 goto found;
260 /* Insert into parent's checks. */
261 vec_safe_push (ptr->deferred_access_checks, *chk);
262 found:;
268 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
269 is the BINFO indicating the qualifying scope used to access the
270 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
271 or we aren't in SFINAE context or all the checks succeed return TRUE,
272 otherwise FALSE. */
274 bool
275 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
276 tsubst_flags_t complain)
278 int i;
279 deferred_access_check *chk;
280 location_t loc = input_location;
281 bool ok = true;
283 if (!checks)
284 return true;
286 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
288 input_location = chk->loc;
289 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
292 input_location = loc;
293 return (complain & tf_error) ? true : ok;
296 /* Perform the deferred access checks.
298 After performing the checks, we still have to keep the list
299 `deferred_access_stack->deferred_access_checks' since we may want
300 to check access for them again later in a different context.
301 For example:
303 class A {
304 typedef int X;
305 static X a;
307 A::X A::a, x; // No error for `A::a', error for `x'
309 We have to perform deferred access of `A::X', first with `A::a',
310 next with `x'. Return value like perform_access_checks above. */
312 bool
313 perform_deferred_access_checks (tsubst_flags_t complain)
315 return perform_access_checks (get_deferred_access_checks (), complain);
318 /* Defer checking the accessibility of DECL, when looked up in
319 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
320 Return value like perform_access_checks above. */
322 bool
323 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
324 tsubst_flags_t complain)
326 int i;
327 deferred_access *ptr;
328 deferred_access_check *chk;
331 /* Exit if we are in a context that no access checking is performed.
333 if (deferred_access_no_check)
334 return true;
336 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
338 ptr = &deferred_access_stack->last ();
340 /* If we are not supposed to defer access checks, just check now. */
341 if (ptr->deferring_access_checks_kind == dk_no_deferred)
343 bool ok = enforce_access (binfo, decl, diag_decl, complain);
344 return (complain & tf_error) ? true : ok;
347 /* See if we are already going to perform this check. */
348 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
350 if (chk->decl == decl && chk->binfo == binfo &&
351 chk->diag_decl == diag_decl)
353 return true;
356 /* If not, record the check. */
357 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
358 vec_safe_push (ptr->deferred_access_checks, new_access);
360 return true;
363 /* Returns nonzero if the current statement is a full expression,
364 i.e. temporaries created during that statement should be destroyed
365 at the end of the statement. */
368 stmts_are_full_exprs_p (void)
370 return current_stmt_tree ()->stmts_are_full_exprs_p;
373 /* T is a statement. Add it to the statement-tree. This is the C++
374 version. The C/ObjC frontends have a slightly different version of
375 this function. */
377 tree
378 add_stmt (tree t)
380 enum tree_code code = TREE_CODE (t);
382 if (EXPR_P (t) && code != LABEL_EXPR)
384 if (!EXPR_HAS_LOCATION (t))
385 SET_EXPR_LOCATION (t, input_location);
387 /* When we expand a statement-tree, we must know whether or not the
388 statements are full-expressions. We record that fact here. */
389 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
392 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
393 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
395 /* Add T to the statement-tree. Non-side-effect statements need to be
396 recorded during statement expressions. */
397 gcc_checking_assert (!stmt_list_stack->is_empty ());
398 append_to_statement_list_force (t, &cur_stmt_list);
400 return t;
403 /* Returns the stmt_tree to which statements are currently being added. */
405 stmt_tree
406 current_stmt_tree (void)
408 return (cfun
409 ? &cfun->language->base.x_stmt_tree
410 : &scope_chain->x_stmt_tree);
413 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
415 static tree
416 maybe_cleanup_point_expr (tree expr)
418 if (!processing_template_decl && stmts_are_full_exprs_p ())
419 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
420 return expr;
423 /* Like maybe_cleanup_point_expr except have the type of the new expression be
424 void so we don't need to create a temporary variable to hold the inner
425 expression. The reason why we do this is because the original type might be
426 an aggregate and we cannot create a temporary variable for that type. */
428 tree
429 maybe_cleanup_point_expr_void (tree expr)
431 if (!processing_template_decl && stmts_are_full_exprs_p ())
432 expr = fold_build_cleanup_point_expr (void_type_node, expr);
433 return expr;
438 /* Create a declaration statement for the declaration given by the DECL. */
440 void
441 add_decl_expr (tree decl)
443 tree r = build_stmt (input_location, DECL_EXPR, decl);
444 if (DECL_INITIAL (decl)
445 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
446 r = maybe_cleanup_point_expr_void (r);
447 add_stmt (r);
450 /* Finish a scope. */
452 tree
453 do_poplevel (tree stmt_list)
455 tree block = NULL;
457 if (stmts_are_full_exprs_p ())
458 block = poplevel (kept_level_p (), 1, 0);
460 stmt_list = pop_stmt_list (stmt_list);
462 if (!processing_template_decl)
464 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
465 /* ??? See c_end_compound_stmt re statement expressions. */
468 return stmt_list;
471 /* Begin a new scope. */
473 static tree
474 do_pushlevel (scope_kind sk)
476 tree ret = push_stmt_list ();
477 if (stmts_are_full_exprs_p ())
478 begin_scope (sk, NULL);
479 return ret;
482 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
483 when the current scope is exited. EH_ONLY is true when this is not
484 meant to apply to normal control flow transfer. */
486 void
487 push_cleanup (tree decl, tree cleanup, bool eh_only)
489 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
490 CLEANUP_EH_ONLY (stmt) = eh_only;
491 add_stmt (stmt);
492 CLEANUP_BODY (stmt) = push_stmt_list ();
495 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
496 the current loops, represented by 'NULL_TREE' if we've seen a possible
497 exit, and 'error_mark_node' if not. This is currently used only to
498 suppress the warning about a function with no return statements, and
499 therefore we don't bother noting returns as possible exits. We also
500 don't bother with gotos. */
502 static void
503 begin_maybe_infinite_loop (tree cond)
505 /* Only track this while parsing a function, not during instantiation. */
506 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
507 && !processing_template_decl))
508 return;
509 bool maybe_infinite = true;
510 if (cond)
512 cond = fold_non_dependent_expr (cond);
513 maybe_infinite = integer_nonzerop (cond);
515 vec_safe_push (cp_function_chain->infinite_loops,
516 maybe_infinite ? error_mark_node : NULL_TREE);
520 /* A break is a possible exit for the current loop. */
522 void
523 break_maybe_infinite_loop (void)
525 if (!cfun)
526 return;
527 cp_function_chain->infinite_loops->last() = NULL_TREE;
530 /* If we reach the end of the loop without seeing a possible exit, we have
531 an infinite loop. */
533 static void
534 end_maybe_infinite_loop (tree cond)
536 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
537 && !processing_template_decl))
538 return;
539 tree current = cp_function_chain->infinite_loops->pop();
540 if (current != NULL_TREE)
542 cond = fold_non_dependent_expr (cond);
543 if (integer_nonzerop (cond))
544 current_function_infinite_loop = 1;
549 /* Begin a conditional that might contain a declaration. When generating
550 normal code, we want the declaration to appear before the statement
551 containing the conditional. When generating template code, we want the
552 conditional to be rendered as the raw DECL_EXPR. */
554 static void
555 begin_cond (tree *cond_p)
557 if (processing_template_decl)
558 *cond_p = push_stmt_list ();
561 /* Finish such a conditional. */
563 static void
564 finish_cond (tree *cond_p, tree expr)
566 if (processing_template_decl)
568 tree cond = pop_stmt_list (*cond_p);
570 if (expr == NULL_TREE)
571 /* Empty condition in 'for'. */
572 gcc_assert (empty_expr_stmt_p (cond));
573 else if (check_for_bare_parameter_packs (expr))
574 expr = error_mark_node;
575 else if (!empty_expr_stmt_p (cond))
576 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
578 *cond_p = expr;
581 /* If *COND_P specifies a conditional with a declaration, transform the
582 loop such that
583 while (A x = 42) { }
584 for (; A x = 42;) { }
585 becomes
586 while (true) { A x = 42; if (!x) break; }
587 for (;;) { A x = 42; if (!x) break; }
588 The statement list for BODY will be empty if the conditional did
589 not declare anything. */
591 static void
592 simplify_loop_decl_cond (tree *cond_p, tree body)
594 tree cond, if_stmt;
596 if (!TREE_SIDE_EFFECTS (body))
597 return;
599 cond = *cond_p;
600 *cond_p = boolean_true_node;
602 if_stmt = begin_if_stmt ();
603 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
604 finish_if_stmt_cond (cond, if_stmt);
605 finish_break_stmt ();
606 finish_then_clause (if_stmt);
607 finish_if_stmt (if_stmt);
610 /* Finish a goto-statement. */
612 tree
613 finish_goto_stmt (tree destination)
615 if (identifier_p (destination))
616 destination = lookup_label (destination);
618 /* We warn about unused labels with -Wunused. That means we have to
619 mark the used labels as used. */
620 if (TREE_CODE (destination) == LABEL_DECL)
621 TREE_USED (destination) = 1;
622 else
624 if (check_no_cilk (destination,
625 "Cilk array notation cannot be used as a computed goto expression",
626 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
627 destination = error_mark_node;
628 destination = mark_rvalue_use (destination);
629 if (!processing_template_decl)
631 destination = cp_convert (ptr_type_node, destination,
632 tf_warning_or_error);
633 if (error_operand_p (destination))
634 return NULL_TREE;
635 destination
636 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
637 destination);
641 check_goto (destination);
643 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
646 /* COND is the condition-expression for an if, while, etc.,
647 statement. Convert it to a boolean value, if appropriate.
648 In addition, verify sequence points if -Wsequence-point is enabled. */
650 static tree
651 maybe_convert_cond (tree cond)
653 /* Empty conditions remain empty. */
654 if (!cond)
655 return NULL_TREE;
657 /* Wait until we instantiate templates before doing conversion. */
658 if (processing_template_decl)
659 return cond;
661 if (warn_sequence_point)
662 verify_sequence_points (cond);
664 /* Do the conversion. */
665 cond = convert_from_reference (cond);
667 if (TREE_CODE (cond) == MODIFY_EXPR
668 && !TREE_NO_WARNING (cond)
669 && warn_parentheses)
671 warning (OPT_Wparentheses,
672 "suggest parentheses around assignment used as truth value");
673 TREE_NO_WARNING (cond) = 1;
676 return condition_conversion (cond);
679 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
681 tree
682 finish_expr_stmt (tree expr)
684 tree r = NULL_TREE;
686 if (expr != NULL_TREE)
688 if (!processing_template_decl)
690 if (warn_sequence_point)
691 verify_sequence_points (expr);
692 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
694 else if (!type_dependent_expression_p (expr))
695 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
696 tf_warning_or_error);
698 if (check_for_bare_parameter_packs (expr))
699 expr = error_mark_node;
701 /* Simplification of inner statement expressions, compound exprs,
702 etc can result in us already having an EXPR_STMT. */
703 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
705 if (TREE_CODE (expr) != EXPR_STMT)
706 expr = build_stmt (input_location, EXPR_STMT, expr);
707 expr = maybe_cleanup_point_expr_void (expr);
710 r = add_stmt (expr);
713 return r;
717 /* Begin an if-statement. Returns a newly created IF_STMT if
718 appropriate. */
720 tree
721 begin_if_stmt (void)
723 tree r, scope;
724 scope = do_pushlevel (sk_cond);
725 r = build_stmt (input_location, IF_STMT, NULL_TREE,
726 NULL_TREE, NULL_TREE, scope);
727 begin_cond (&IF_COND (r));
728 return r;
731 /* Process the COND of an if-statement, which may be given by
732 IF_STMT. */
734 void
735 finish_if_stmt_cond (tree cond, tree if_stmt)
737 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
738 add_stmt (if_stmt);
739 THEN_CLAUSE (if_stmt) = push_stmt_list ();
742 /* Finish the then-clause of an if-statement, which may be given by
743 IF_STMT. */
745 tree
746 finish_then_clause (tree if_stmt)
748 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
749 return if_stmt;
752 /* Begin the else-clause of an if-statement. */
754 void
755 begin_else_clause (tree if_stmt)
757 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
760 /* Finish the else-clause of an if-statement, which may be given by
761 IF_STMT. */
763 void
764 finish_else_clause (tree if_stmt)
766 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
769 /* Finish an if-statement. */
771 void
772 finish_if_stmt (tree if_stmt)
774 tree scope = IF_SCOPE (if_stmt);
775 IF_SCOPE (if_stmt) = NULL;
776 add_stmt (do_poplevel (scope));
779 /* Begin a while-statement. Returns a newly created WHILE_STMT if
780 appropriate. */
782 tree
783 begin_while_stmt (void)
785 tree r;
786 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
787 add_stmt (r);
788 WHILE_BODY (r) = do_pushlevel (sk_block);
789 begin_cond (&WHILE_COND (r));
790 return r;
793 /* Process the COND of a while-statement, which may be given by
794 WHILE_STMT. */
796 void
797 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
799 if (check_no_cilk (cond,
800 "Cilk array notation cannot be used as a condition for while statement",
801 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
802 cond = error_mark_node;
803 cond = maybe_convert_cond (cond);
804 finish_cond (&WHILE_COND (while_stmt), cond);
805 begin_maybe_infinite_loop (cond);
806 if (ivdep && cond != error_mark_node)
807 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
808 TREE_TYPE (WHILE_COND (while_stmt)),
809 WHILE_COND (while_stmt),
810 build_int_cst (integer_type_node,
811 annot_expr_ivdep_kind));
812 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
815 /* Finish a while-statement, which may be given by WHILE_STMT. */
817 void
818 finish_while_stmt (tree while_stmt)
820 end_maybe_infinite_loop (boolean_true_node);
821 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
824 /* Begin a do-statement. Returns a newly created DO_STMT if
825 appropriate. */
827 tree
828 begin_do_stmt (void)
830 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
831 begin_maybe_infinite_loop (boolean_true_node);
832 add_stmt (r);
833 DO_BODY (r) = push_stmt_list ();
834 return r;
837 /* Finish the body of a do-statement, which may be given by DO_STMT. */
839 void
840 finish_do_body (tree do_stmt)
842 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
844 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
845 body = STATEMENT_LIST_TAIL (body)->stmt;
847 if (IS_EMPTY_STMT (body))
848 warning (OPT_Wempty_body,
849 "suggest explicit braces around empty body in %<do%> statement");
852 /* Finish a do-statement, which may be given by DO_STMT, and whose
853 COND is as indicated. */
855 void
856 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
858 if (check_no_cilk (cond,
859 "Cilk array notation cannot be used as a condition for a do-while statement",
860 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
861 cond = error_mark_node;
862 cond = maybe_convert_cond (cond);
863 end_maybe_infinite_loop (cond);
864 if (ivdep && cond != error_mark_node)
865 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
866 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
867 DO_COND (do_stmt) = cond;
870 /* Finish a return-statement. The EXPRESSION returned, if any, is as
871 indicated. */
873 tree
874 finish_return_stmt (tree expr)
876 tree r;
877 bool no_warning;
879 expr = check_return_expr (expr, &no_warning);
881 if (error_operand_p (expr)
882 || (flag_openmp && !check_omp_return ()))
884 /* Suppress -Wreturn-type for this function. */
885 if (warn_return_type)
886 TREE_NO_WARNING (current_function_decl) = true;
887 return error_mark_node;
890 if (!processing_template_decl)
892 if (warn_sequence_point)
893 verify_sequence_points (expr);
895 if (DECL_DESTRUCTOR_P (current_function_decl)
896 || (DECL_CONSTRUCTOR_P (current_function_decl)
897 && targetm.cxx.cdtor_returns_this ()))
899 /* Similarly, all destructors must run destructors for
900 base-classes before returning. So, all returns in a
901 destructor get sent to the DTOR_LABEL; finish_function emits
902 code to return a value there. */
903 return finish_goto_stmt (cdtor_label);
907 r = build_stmt (input_location, RETURN_EXPR, expr);
908 TREE_NO_WARNING (r) |= no_warning;
909 r = maybe_cleanup_point_expr_void (r);
910 r = add_stmt (r);
912 return r;
915 /* Begin the scope of a for-statement or a range-for-statement.
916 Both the returned trees are to be used in a call to
917 begin_for_stmt or begin_range_for_stmt. */
919 tree
920 begin_for_scope (tree *init)
922 tree scope = NULL_TREE;
923 if (flag_new_for_scope > 0)
924 scope = do_pushlevel (sk_for);
926 if (processing_template_decl)
927 *init = push_stmt_list ();
928 else
929 *init = NULL_TREE;
931 return scope;
934 /* Begin a for-statement. Returns a new FOR_STMT.
935 SCOPE and INIT should be the return of begin_for_scope,
936 or both NULL_TREE */
938 tree
939 begin_for_stmt (tree scope, tree init)
941 tree r;
943 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
944 NULL_TREE, NULL_TREE, NULL_TREE);
946 if (scope == NULL_TREE)
948 gcc_assert (!init || !(flag_new_for_scope > 0));
949 if (!init)
950 scope = begin_for_scope (&init);
952 FOR_INIT_STMT (r) = init;
953 FOR_SCOPE (r) = scope;
955 return r;
958 /* Finish the for-init-statement of a for-statement, which may be
959 given by FOR_STMT. */
961 void
962 finish_for_init_stmt (tree for_stmt)
964 if (processing_template_decl)
965 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
966 add_stmt (for_stmt);
967 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
968 begin_cond (&FOR_COND (for_stmt));
971 /* Finish the COND of a for-statement, which may be given by
972 FOR_STMT. */
974 void
975 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
977 if (check_no_cilk (cond,
978 "Cilk array notation cannot be used in a condition for a for-loop",
979 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
980 cond = error_mark_node;
981 cond = maybe_convert_cond (cond);
982 finish_cond (&FOR_COND (for_stmt), cond);
983 begin_maybe_infinite_loop (cond);
984 if (ivdep && cond != error_mark_node)
985 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
986 TREE_TYPE (FOR_COND (for_stmt)),
987 FOR_COND (for_stmt),
988 build_int_cst (integer_type_node,
989 annot_expr_ivdep_kind));
990 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
993 /* Finish the increment-EXPRESSION in a for-statement, which may be
994 given by FOR_STMT. */
996 void
997 finish_for_expr (tree expr, tree for_stmt)
999 if (!expr)
1000 return;
1001 /* If EXPR is an overloaded function, issue an error; there is no
1002 context available to use to perform overload resolution. */
1003 if (type_unknown_p (expr))
1005 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1006 expr = error_mark_node;
1008 if (!processing_template_decl)
1010 if (warn_sequence_point)
1011 verify_sequence_points (expr);
1012 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1013 tf_warning_or_error);
1015 else if (!type_dependent_expression_p (expr))
1016 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1017 tf_warning_or_error);
1018 expr = maybe_cleanup_point_expr_void (expr);
1019 if (check_for_bare_parameter_packs (expr))
1020 expr = error_mark_node;
1021 FOR_EXPR (for_stmt) = expr;
1024 /* Finish the body of a for-statement, which may be given by
1025 FOR_STMT. The increment-EXPR for the loop must be
1026 provided.
1027 It can also finish RANGE_FOR_STMT. */
1029 void
1030 finish_for_stmt (tree for_stmt)
1032 end_maybe_infinite_loop (boolean_true_node);
1034 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1035 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1036 else
1037 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1039 /* Pop the scope for the body of the loop. */
1040 if (flag_new_for_scope > 0)
1042 tree scope;
1043 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1044 ? &RANGE_FOR_SCOPE (for_stmt)
1045 : &FOR_SCOPE (for_stmt));
1046 scope = *scope_ptr;
1047 *scope_ptr = NULL;
1048 add_stmt (do_poplevel (scope));
1052 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1053 SCOPE and INIT should be the return of begin_for_scope,
1054 or both NULL_TREE .
1055 To finish it call finish_for_stmt(). */
1057 tree
1058 begin_range_for_stmt (tree scope, tree init)
1060 tree r;
1062 begin_maybe_infinite_loop (boolean_false_node);
1064 r = build_stmt (input_location, RANGE_FOR_STMT,
1065 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1067 if (scope == NULL_TREE)
1069 gcc_assert (!init || !(flag_new_for_scope > 0));
1070 if (!init)
1071 scope = begin_for_scope (&init);
1074 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1075 pop it now. */
1076 if (init)
1077 pop_stmt_list (init);
1078 RANGE_FOR_SCOPE (r) = scope;
1080 return r;
1083 /* Finish the head of a range-based for statement, which may
1084 be given by RANGE_FOR_STMT. DECL must be the declaration
1085 and EXPR must be the loop expression. */
1087 void
1088 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1090 RANGE_FOR_DECL (range_for_stmt) = decl;
1091 RANGE_FOR_EXPR (range_for_stmt) = expr;
1092 add_stmt (range_for_stmt);
1093 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1096 /* Finish a break-statement. */
1098 tree
1099 finish_break_stmt (void)
1101 /* In switch statements break is sometimes stylistically used after
1102 a return statement. This can lead to spurious warnings about
1103 control reaching the end of a non-void function when it is
1104 inlined. Note that we are calling block_may_fallthru with
1105 language specific tree nodes; this works because
1106 block_may_fallthru returns true when given something it does not
1107 understand. */
1108 if (!block_may_fallthru (cur_stmt_list))
1109 return void_node;
1110 return add_stmt (build_stmt (input_location, BREAK_STMT));
1113 /* Finish a continue-statement. */
1115 tree
1116 finish_continue_stmt (void)
1118 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1121 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1122 appropriate. */
1124 tree
1125 begin_switch_stmt (void)
1127 tree r, scope;
1129 scope = do_pushlevel (sk_cond);
1130 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1132 begin_cond (&SWITCH_STMT_COND (r));
1134 return r;
1137 /* Finish the cond of a switch-statement. */
1139 void
1140 finish_switch_cond (tree cond, tree switch_stmt)
1142 tree orig_type = NULL;
1144 if (check_no_cilk (cond,
1145 "Cilk array notation cannot be used as a condition for switch statement",
1146 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1147 cond = error_mark_node;
1149 if (!processing_template_decl)
1151 /* Convert the condition to an integer or enumeration type. */
1152 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1153 if (cond == NULL_TREE)
1155 error ("switch quantity not an integer");
1156 cond = error_mark_node;
1158 /* We want unlowered type here to handle enum bit-fields. */
1159 orig_type = unlowered_expr_type (cond);
1160 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1161 orig_type = TREE_TYPE (cond);
1162 if (cond != error_mark_node)
1164 /* [stmt.switch]
1166 Integral promotions are performed. */
1167 cond = perform_integral_promotions (cond);
1168 cond = maybe_cleanup_point_expr (cond);
1171 if (check_for_bare_parameter_packs (cond))
1172 cond = error_mark_node;
1173 else if (!processing_template_decl && warn_sequence_point)
1174 verify_sequence_points (cond);
1176 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1177 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1178 add_stmt (switch_stmt);
1179 push_switch (switch_stmt);
1180 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1183 /* Finish the body of a switch-statement, which may be given by
1184 SWITCH_STMT. The COND to switch on is indicated. */
1186 void
1187 finish_switch_stmt (tree switch_stmt)
1189 tree scope;
1191 SWITCH_STMT_BODY (switch_stmt) =
1192 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1193 pop_switch ();
1195 scope = SWITCH_STMT_SCOPE (switch_stmt);
1196 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1197 add_stmt (do_poplevel (scope));
1200 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1201 appropriate. */
1203 tree
1204 begin_try_block (void)
1206 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1207 add_stmt (r);
1208 TRY_STMTS (r) = push_stmt_list ();
1209 return r;
1212 /* Likewise, for a function-try-block. The block returned in
1213 *COMPOUND_STMT is an artificial outer scope, containing the
1214 function-try-block. */
1216 tree
1217 begin_function_try_block (tree *compound_stmt)
1219 tree r;
1220 /* This outer scope does not exist in the C++ standard, but we need
1221 a place to put __FUNCTION__ and similar variables. */
1222 *compound_stmt = begin_compound_stmt (0);
1223 r = begin_try_block ();
1224 FN_TRY_BLOCK_P (r) = 1;
1225 return r;
1228 /* Finish a try-block, which may be given by TRY_BLOCK. */
1230 void
1231 finish_try_block (tree try_block)
1233 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1234 TRY_HANDLERS (try_block) = push_stmt_list ();
1237 /* Finish the body of a cleanup try-block, which may be given by
1238 TRY_BLOCK. */
1240 void
1241 finish_cleanup_try_block (tree try_block)
1243 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1246 /* Finish an implicitly generated try-block, with a cleanup is given
1247 by CLEANUP. */
1249 void
1250 finish_cleanup (tree cleanup, tree try_block)
1252 TRY_HANDLERS (try_block) = cleanup;
1253 CLEANUP_P (try_block) = 1;
1256 /* Likewise, for a function-try-block. */
1258 void
1259 finish_function_try_block (tree try_block)
1261 finish_try_block (try_block);
1262 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1263 the try block, but moving it inside. */
1264 in_function_try_handler = 1;
1267 /* Finish a handler-sequence for a try-block, which may be given by
1268 TRY_BLOCK. */
1270 void
1271 finish_handler_sequence (tree try_block)
1273 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1274 check_handlers (TRY_HANDLERS (try_block));
1277 /* Finish the handler-seq for a function-try-block, given by
1278 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1279 begin_function_try_block. */
1281 void
1282 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1284 in_function_try_handler = 0;
1285 finish_handler_sequence (try_block);
1286 finish_compound_stmt (compound_stmt);
1289 /* Begin a handler. Returns a HANDLER if appropriate. */
1291 tree
1292 begin_handler (void)
1294 tree r;
1296 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1297 add_stmt (r);
1299 /* Create a binding level for the eh_info and the exception object
1300 cleanup. */
1301 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1303 return r;
1306 /* Finish the handler-parameters for a handler, which may be given by
1307 HANDLER. DECL is the declaration for the catch parameter, or NULL
1308 if this is a `catch (...)' clause. */
1310 void
1311 finish_handler_parms (tree decl, tree handler)
1313 tree type = NULL_TREE;
1314 if (processing_template_decl)
1316 if (decl)
1318 decl = pushdecl (decl);
1319 decl = push_template_decl (decl);
1320 HANDLER_PARMS (handler) = decl;
1321 type = TREE_TYPE (decl);
1324 else
1325 type = expand_start_catch_block (decl);
1326 HANDLER_TYPE (handler) = type;
1329 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1330 the return value from the matching call to finish_handler_parms. */
1332 void
1333 finish_handler (tree handler)
1335 if (!processing_template_decl)
1336 expand_end_catch_block ();
1337 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1340 /* Begin a compound statement. FLAGS contains some bits that control the
1341 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1342 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1343 block of a function. If BCS_TRY_BLOCK is set, this is the block
1344 created on behalf of a TRY statement. Returns a token to be passed to
1345 finish_compound_stmt. */
1347 tree
1348 begin_compound_stmt (unsigned int flags)
1350 tree r;
1352 if (flags & BCS_NO_SCOPE)
1354 r = push_stmt_list ();
1355 STATEMENT_LIST_NO_SCOPE (r) = 1;
1357 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1358 But, if it's a statement-expression with a scopeless block, there's
1359 nothing to keep, and we don't want to accidentally keep a block
1360 *inside* the scopeless block. */
1361 keep_next_level (false);
1363 else
1364 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1366 /* When processing a template, we need to remember where the braces were,
1367 so that we can set up identical scopes when instantiating the template
1368 later. BIND_EXPR is a handy candidate for this.
1369 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1370 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1371 processing templates. */
1372 if (processing_template_decl)
1374 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1375 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1376 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1377 TREE_SIDE_EFFECTS (r) = 1;
1380 return r;
1383 /* Finish a compound-statement, which is given by STMT. */
1385 void
1386 finish_compound_stmt (tree stmt)
1388 if (TREE_CODE (stmt) == BIND_EXPR)
1390 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1391 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1392 discard the BIND_EXPR so it can be merged with the containing
1393 STATEMENT_LIST. */
1394 if (TREE_CODE (body) == STATEMENT_LIST
1395 && STATEMENT_LIST_HEAD (body) == NULL
1396 && !BIND_EXPR_BODY_BLOCK (stmt)
1397 && !BIND_EXPR_TRY_BLOCK (stmt))
1398 stmt = body;
1399 else
1400 BIND_EXPR_BODY (stmt) = body;
1402 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1403 stmt = pop_stmt_list (stmt);
1404 else
1406 /* Destroy any ObjC "super" receivers that may have been
1407 created. */
1408 objc_clear_super_receiver ();
1410 stmt = do_poplevel (stmt);
1413 /* ??? See c_end_compound_stmt wrt statement expressions. */
1414 add_stmt (stmt);
1417 /* Finish an asm-statement, whose components are a STRING, some
1418 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1419 LABELS. Also note whether the asm-statement should be
1420 considered volatile. */
1422 tree
1423 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1424 tree input_operands, tree clobbers, tree labels)
1426 tree r;
1427 tree t;
1428 int ninputs = list_length (input_operands);
1429 int noutputs = list_length (output_operands);
1431 if (!processing_template_decl)
1433 const char *constraint;
1434 const char **oconstraints;
1435 bool allows_mem, allows_reg, is_inout;
1436 tree operand;
1437 int i;
1439 oconstraints = XALLOCAVEC (const char *, noutputs);
1441 string = resolve_asm_operand_names (string, output_operands,
1442 input_operands, labels);
1444 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1446 operand = TREE_VALUE (t);
1448 /* ??? Really, this should not be here. Users should be using a
1449 proper lvalue, dammit. But there's a long history of using
1450 casts in the output operands. In cases like longlong.h, this
1451 becomes a primitive form of typechecking -- if the cast can be
1452 removed, then the output operand had a type of the proper width;
1453 otherwise we'll get an error. Gross, but ... */
1454 STRIP_NOPS (operand);
1456 operand = mark_lvalue_use (operand);
1458 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1459 operand = error_mark_node;
1461 if (operand != error_mark_node
1462 && (TREE_READONLY (operand)
1463 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1464 /* Functions are not modifiable, even though they are
1465 lvalues. */
1466 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1467 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1468 /* If it's an aggregate and any field is const, then it is
1469 effectively const. */
1470 || (CLASS_TYPE_P (TREE_TYPE (operand))
1471 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1472 cxx_readonly_error (operand, lv_asm);
1474 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1475 oconstraints[i] = constraint;
1477 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1478 &allows_mem, &allows_reg, &is_inout))
1480 /* If the operand is going to end up in memory,
1481 mark it addressable. */
1482 if (!allows_reg && !cxx_mark_addressable (operand))
1483 operand = error_mark_node;
1485 else
1486 operand = error_mark_node;
1488 TREE_VALUE (t) = operand;
1491 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1493 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1494 bool constraint_parsed
1495 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1496 oconstraints, &allows_mem, &allows_reg);
1497 /* If the operand is going to end up in memory, don't call
1498 decay_conversion. */
1499 if (constraint_parsed && !allows_reg && allows_mem)
1500 operand = mark_lvalue_use (TREE_VALUE (t));
1501 else
1502 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1504 /* If the type of the operand hasn't been determined (e.g.,
1505 because it involves an overloaded function), then issue
1506 an error message. There's no context available to
1507 resolve the overloading. */
1508 if (TREE_TYPE (operand) == unknown_type_node)
1510 error ("type of asm operand %qE could not be determined",
1511 TREE_VALUE (t));
1512 operand = error_mark_node;
1515 if (constraint_parsed)
1517 /* If the operand is going to end up in memory,
1518 mark it addressable. */
1519 if (!allows_reg && allows_mem)
1521 /* Strip the nops as we allow this case. FIXME, this really
1522 should be rejected or made deprecated. */
1523 STRIP_NOPS (operand);
1524 if (!cxx_mark_addressable (operand))
1525 operand = error_mark_node;
1527 else if (!allows_reg && !allows_mem)
1529 /* If constraint allows neither register nor memory,
1530 try harder to get a constant. */
1531 tree constop = maybe_constant_value (operand);
1532 if (TREE_CONSTANT (constop))
1533 operand = constop;
1536 else
1537 operand = error_mark_node;
1539 TREE_VALUE (t) = operand;
1543 r = build_stmt (input_location, ASM_EXPR, string,
1544 output_operands, input_operands,
1545 clobbers, labels);
1546 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1547 r = maybe_cleanup_point_expr_void (r);
1548 return add_stmt (r);
1551 /* Finish a label with the indicated NAME. Returns the new label. */
1553 tree
1554 finish_label_stmt (tree name)
1556 tree decl = define_label (input_location, name);
1558 if (decl == error_mark_node)
1559 return error_mark_node;
1561 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1563 return decl;
1566 /* Finish a series of declarations for local labels. G++ allows users
1567 to declare "local" labels, i.e., labels with scope. This extension
1568 is useful when writing code involving statement-expressions. */
1570 void
1571 finish_label_decl (tree name)
1573 if (!at_function_scope_p ())
1575 error ("__label__ declarations are only allowed in function scopes");
1576 return;
1579 add_decl_expr (declare_local_label (name));
1582 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1584 void
1585 finish_decl_cleanup (tree decl, tree cleanup)
1587 push_cleanup (decl, cleanup, false);
1590 /* If the current scope exits with an exception, run CLEANUP. */
1592 void
1593 finish_eh_cleanup (tree cleanup)
1595 push_cleanup (NULL, cleanup, true);
1598 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1599 order they were written by the user. Each node is as for
1600 emit_mem_initializers. */
1602 void
1603 finish_mem_initializers (tree mem_inits)
1605 /* Reorder the MEM_INITS so that they are in the order they appeared
1606 in the source program. */
1607 mem_inits = nreverse (mem_inits);
1609 if (processing_template_decl)
1611 tree mem;
1613 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1615 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1616 check for bare parameter packs in the TREE_VALUE, because
1617 any parameter packs in the TREE_VALUE have already been
1618 bound as part of the TREE_PURPOSE. See
1619 make_pack_expansion for more information. */
1620 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1621 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1622 TREE_VALUE (mem) = error_mark_node;
1625 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1626 CTOR_INITIALIZER, mem_inits));
1628 else
1629 emit_mem_initializers (mem_inits);
1632 /* Obfuscate EXPR if it looks like an id-expression or member access so
1633 that the call to finish_decltype in do_auto_deduction will give the
1634 right result. */
1636 tree
1637 force_paren_expr (tree expr)
1639 /* This is only needed for decltype(auto) in C++14. */
1640 if (cxx_dialect < cxx14)
1641 return expr;
1643 /* If we're in unevaluated context, we can't be deducing a
1644 return/initializer type, so we don't need to mess with this. */
1645 if (cp_unevaluated_operand)
1646 return expr;
1648 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1649 && TREE_CODE (expr) != SCOPE_REF)
1650 return expr;
1652 if (TREE_CODE (expr) == COMPONENT_REF)
1653 REF_PARENTHESIZED_P (expr) = true;
1654 else if (type_dependent_expression_p (expr))
1655 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1656 else
1658 cp_lvalue_kind kind = lvalue_kind (expr);
1659 if ((kind & ~clk_class) != clk_none)
1661 tree type = unlowered_expr_type (expr);
1662 bool rval = !!(kind & clk_rvalueref);
1663 type = cp_build_reference_type (type, rval);
1664 /* This inhibits warnings in, eg, cxx_mark_addressable
1665 (c++/60955). */
1666 warning_sentinel s (extra_warnings);
1667 expr = build_static_cast (type, expr, tf_error);
1668 if (expr != error_mark_node)
1669 REF_PARENTHESIZED_P (expr) = true;
1673 return expr;
1676 /* Finish a parenthesized expression EXPR. */
1678 tree
1679 finish_parenthesized_expr (tree expr)
1681 if (EXPR_P (expr))
1682 /* This inhibits warnings in c_common_truthvalue_conversion. */
1683 TREE_NO_WARNING (expr) = 1;
1685 if (TREE_CODE (expr) == OFFSET_REF
1686 || TREE_CODE (expr) == SCOPE_REF)
1687 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1688 enclosed in parentheses. */
1689 PTRMEM_OK_P (expr) = 0;
1691 if (TREE_CODE (expr) == STRING_CST)
1692 PAREN_STRING_LITERAL_P (expr) = 1;
1694 expr = force_paren_expr (expr);
1696 return expr;
1699 /* Finish a reference to a non-static data member (DECL) that is not
1700 preceded by `.' or `->'. */
1702 tree
1703 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1705 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1706 bool try_omp_private = !object && omp_private_member_map;
1707 tree ret;
1709 if (!object)
1711 tree scope = qualifying_scope;
1712 if (scope == NULL_TREE)
1713 scope = context_for_name_lookup (decl);
1714 object = maybe_dummy_object (scope, NULL);
1717 object = maybe_resolve_dummy (object, true);
1718 if (object == error_mark_node)
1719 return error_mark_node;
1721 /* DR 613/850: Can use non-static data members without an associated
1722 object in sizeof/decltype/alignof. */
1723 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1724 && (!processing_template_decl || !current_class_ref))
1726 if (current_function_decl
1727 && DECL_STATIC_FUNCTION_P (current_function_decl))
1728 error ("invalid use of member %qD in static member function", decl);
1729 else
1730 error ("invalid use of non-static data member %qD", decl);
1731 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1733 return error_mark_node;
1736 if (current_class_ptr)
1737 TREE_USED (current_class_ptr) = 1;
1738 if (processing_template_decl && !qualifying_scope)
1740 tree type = TREE_TYPE (decl);
1742 if (TREE_CODE (type) == REFERENCE_TYPE)
1743 /* Quals on the object don't matter. */;
1744 else if (PACK_EXPANSION_P (type))
1745 /* Don't bother trying to represent this. */
1746 type = NULL_TREE;
1747 else
1749 /* Set the cv qualifiers. */
1750 int quals = cp_type_quals (TREE_TYPE (object));
1752 if (DECL_MUTABLE_P (decl))
1753 quals &= ~TYPE_QUAL_CONST;
1755 quals |= cp_type_quals (TREE_TYPE (decl));
1756 type = cp_build_qualified_type (type, quals);
1759 ret = (convert_from_reference
1760 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1762 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1763 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1764 for now. */
1765 else if (processing_template_decl)
1766 ret = build_qualified_name (TREE_TYPE (decl),
1767 qualifying_scope,
1768 decl,
1769 /*template_p=*/false);
1770 else
1772 tree access_type = TREE_TYPE (object);
1774 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1775 decl, tf_warning_or_error);
1777 /* If the data member was named `C::M', convert `*this' to `C'
1778 first. */
1779 if (qualifying_scope)
1781 tree binfo = NULL_TREE;
1782 object = build_scoped_ref (object, qualifying_scope,
1783 &binfo);
1786 ret = build_class_member_access_expr (object, decl,
1787 /*access_path=*/NULL_TREE,
1788 /*preserve_reference=*/false,
1789 tf_warning_or_error);
1791 if (try_omp_private)
1793 tree *v = omp_private_member_map->get (decl);
1794 if (v)
1795 ret = convert_from_reference (*v);
1797 return ret;
1800 /* If we are currently parsing a template and we encountered a typedef
1801 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1802 adds the typedef to a list tied to the current template.
1803 At template instantiation time, that list is walked and access check
1804 performed for each typedef.
1805 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1807 void
1808 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1809 tree context,
1810 location_t location)
1812 tree template_info = NULL;
1813 tree cs = current_scope ();
1815 if (!is_typedef_decl (typedef_decl)
1816 || !context
1817 || !CLASS_TYPE_P (context)
1818 || !cs)
1819 return;
1821 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1822 template_info = get_template_info (cs);
1824 if (template_info
1825 && TI_TEMPLATE (template_info)
1826 && !currently_open_class (context))
1827 append_type_to_template_for_access_check (cs, typedef_decl,
1828 context, location);
1831 /* DECL was the declaration to which a qualified-id resolved. Issue
1832 an error message if it is not accessible. If OBJECT_TYPE is
1833 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1834 type of `*x', or `x', respectively. If the DECL was named as
1835 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1837 void
1838 check_accessibility_of_qualified_id (tree decl,
1839 tree object_type,
1840 tree nested_name_specifier)
1842 tree scope;
1843 tree qualifying_type = NULL_TREE;
1845 /* If we are parsing a template declaration and if decl is a typedef,
1846 add it to a list tied to the template.
1847 At template instantiation time, that list will be walked and
1848 access check performed. */
1849 add_typedef_to_current_template_for_access_check (decl,
1850 nested_name_specifier
1851 ? nested_name_specifier
1852 : DECL_CONTEXT (decl),
1853 input_location);
1855 /* If we're not checking, return immediately. */
1856 if (deferred_access_no_check)
1857 return;
1859 /* Determine the SCOPE of DECL. */
1860 scope = context_for_name_lookup (decl);
1861 /* If the SCOPE is not a type, then DECL is not a member. */
1862 if (!TYPE_P (scope))
1863 return;
1864 /* Compute the scope through which DECL is being accessed. */
1865 if (object_type
1866 /* OBJECT_TYPE might not be a class type; consider:
1868 class A { typedef int I; };
1869 I *p;
1870 p->A::I::~I();
1872 In this case, we will have "A::I" as the DECL, but "I" as the
1873 OBJECT_TYPE. */
1874 && CLASS_TYPE_P (object_type)
1875 && DERIVED_FROM_P (scope, object_type))
1876 /* If we are processing a `->' or `.' expression, use the type of the
1877 left-hand side. */
1878 qualifying_type = object_type;
1879 else if (nested_name_specifier)
1881 /* If the reference is to a non-static member of the
1882 current class, treat it as if it were referenced through
1883 `this'. */
1884 tree ct;
1885 if (DECL_NONSTATIC_MEMBER_P (decl)
1886 && current_class_ptr
1887 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1888 qualifying_type = ct;
1889 /* Otherwise, use the type indicated by the
1890 nested-name-specifier. */
1891 else
1892 qualifying_type = nested_name_specifier;
1894 else
1895 /* Otherwise, the name must be from the current class or one of
1896 its bases. */
1897 qualifying_type = currently_open_derived_class (scope);
1899 if (qualifying_type
1900 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1901 or similar in a default argument value. */
1902 && CLASS_TYPE_P (qualifying_type)
1903 && !dependent_type_p (qualifying_type))
1904 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1905 decl, tf_warning_or_error);
1908 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1909 class named to the left of the "::" operator. DONE is true if this
1910 expression is a complete postfix-expression; it is false if this
1911 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1912 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1913 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1914 is true iff this qualified name appears as a template argument. */
1916 tree
1917 finish_qualified_id_expr (tree qualifying_class,
1918 tree expr,
1919 bool done,
1920 bool address_p,
1921 bool template_p,
1922 bool template_arg_p,
1923 tsubst_flags_t complain)
1925 gcc_assert (TYPE_P (qualifying_class));
1927 if (error_operand_p (expr))
1928 return error_mark_node;
1930 if ((DECL_P (expr) || BASELINK_P (expr))
1931 && !mark_used (expr, complain))
1932 return error_mark_node;
1934 if (template_p)
1935 check_template_keyword (expr);
1937 /* If EXPR occurs as the operand of '&', use special handling that
1938 permits a pointer-to-member. */
1939 if (address_p && done)
1941 if (TREE_CODE (expr) == SCOPE_REF)
1942 expr = TREE_OPERAND (expr, 1);
1943 expr = build_offset_ref (qualifying_class, expr,
1944 /*address_p=*/true, complain);
1945 return expr;
1948 /* No need to check access within an enum. */
1949 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1950 return expr;
1952 /* Within the scope of a class, turn references to non-static
1953 members into expression of the form "this->...". */
1954 if (template_arg_p)
1955 /* But, within a template argument, we do not want make the
1956 transformation, as there is no "this" pointer. */
1958 else if (TREE_CODE (expr) == FIELD_DECL)
1960 push_deferring_access_checks (dk_no_check);
1961 expr = finish_non_static_data_member (expr, NULL_TREE,
1962 qualifying_class);
1963 pop_deferring_access_checks ();
1965 else if (BASELINK_P (expr) && !processing_template_decl)
1967 /* See if any of the functions are non-static members. */
1968 /* If so, the expression may be relative to 'this'. */
1969 if (!shared_member_p (expr)
1970 && current_class_ptr
1971 && DERIVED_FROM_P (qualifying_class,
1972 current_nonlambda_class_type ()))
1973 expr = (build_class_member_access_expr
1974 (maybe_dummy_object (qualifying_class, NULL),
1975 expr,
1976 BASELINK_ACCESS_BINFO (expr),
1977 /*preserve_reference=*/false,
1978 complain));
1979 else if (done)
1980 /* The expression is a qualified name whose address is not
1981 being taken. */
1982 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1983 complain);
1985 else if (BASELINK_P (expr))
1987 else
1989 /* In a template, return a SCOPE_REF for most qualified-ids
1990 so that we can check access at instantiation time. But if
1991 we're looking at a member of the current instantiation, we
1992 know we have access and building up the SCOPE_REF confuses
1993 non-type template argument handling. */
1994 if (processing_template_decl
1995 && !currently_open_class (qualifying_class))
1996 expr = build_qualified_name (TREE_TYPE (expr),
1997 qualifying_class, expr,
1998 template_p);
2000 expr = convert_from_reference (expr);
2003 return expr;
2006 /* Begin a statement-expression. The value returned must be passed to
2007 finish_stmt_expr. */
2009 tree
2010 begin_stmt_expr (void)
2012 return push_stmt_list ();
2015 /* Process the final expression of a statement expression. EXPR can be
2016 NULL, if the final expression is empty. Return a STATEMENT_LIST
2017 containing all the statements in the statement-expression, or
2018 ERROR_MARK_NODE if there was an error. */
2020 tree
2021 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2023 if (error_operand_p (expr))
2025 /* The type of the statement-expression is the type of the last
2026 expression. */
2027 TREE_TYPE (stmt_expr) = error_mark_node;
2028 return error_mark_node;
2031 /* If the last statement does not have "void" type, then the value
2032 of the last statement is the value of the entire expression. */
2033 if (expr)
2035 tree type = TREE_TYPE (expr);
2037 if (processing_template_decl)
2039 expr = build_stmt (input_location, EXPR_STMT, expr);
2040 expr = add_stmt (expr);
2041 /* Mark the last statement so that we can recognize it as such at
2042 template-instantiation time. */
2043 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2045 else if (VOID_TYPE_P (type))
2047 /* Just treat this like an ordinary statement. */
2048 expr = finish_expr_stmt (expr);
2050 else
2052 /* It actually has a value we need to deal with. First, force it
2053 to be an rvalue so that we won't need to build up a copy
2054 constructor call later when we try to assign it to something. */
2055 expr = force_rvalue (expr, tf_warning_or_error);
2056 if (error_operand_p (expr))
2057 return error_mark_node;
2059 /* Update for array-to-pointer decay. */
2060 type = TREE_TYPE (expr);
2062 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2063 normal statement, but don't convert to void or actually add
2064 the EXPR_STMT. */
2065 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2066 expr = maybe_cleanup_point_expr (expr);
2067 add_stmt (expr);
2070 /* The type of the statement-expression is the type of the last
2071 expression. */
2072 TREE_TYPE (stmt_expr) = type;
2075 return stmt_expr;
2078 /* Finish a statement-expression. EXPR should be the value returned
2079 by the previous begin_stmt_expr. Returns an expression
2080 representing the statement-expression. */
2082 tree
2083 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2085 tree type;
2086 tree result;
2088 if (error_operand_p (stmt_expr))
2090 pop_stmt_list (stmt_expr);
2091 return error_mark_node;
2094 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2096 type = TREE_TYPE (stmt_expr);
2097 result = pop_stmt_list (stmt_expr);
2098 TREE_TYPE (result) = type;
2100 if (processing_template_decl)
2102 result = build_min (STMT_EXPR, type, result);
2103 TREE_SIDE_EFFECTS (result) = 1;
2104 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2106 else if (CLASS_TYPE_P (type))
2108 /* Wrap the statement-expression in a TARGET_EXPR so that the
2109 temporary object created by the final expression is destroyed at
2110 the end of the full-expression containing the
2111 statement-expression. */
2112 result = force_target_expr (type, result, tf_warning_or_error);
2115 return result;
2118 /* Returns the expression which provides the value of STMT_EXPR. */
2120 tree
2121 stmt_expr_value_expr (tree stmt_expr)
2123 tree t = STMT_EXPR_STMT (stmt_expr);
2125 if (TREE_CODE (t) == BIND_EXPR)
2126 t = BIND_EXPR_BODY (t);
2128 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2129 t = STATEMENT_LIST_TAIL (t)->stmt;
2131 if (TREE_CODE (t) == EXPR_STMT)
2132 t = EXPR_STMT_EXPR (t);
2134 return t;
2137 /* Return TRUE iff EXPR_STMT is an empty list of
2138 expression statements. */
2140 bool
2141 empty_expr_stmt_p (tree expr_stmt)
2143 tree body = NULL_TREE;
2145 if (expr_stmt == void_node)
2146 return true;
2148 if (expr_stmt)
2150 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2151 body = EXPR_STMT_EXPR (expr_stmt);
2152 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2153 body = expr_stmt;
2156 if (body)
2158 if (TREE_CODE (body) == STATEMENT_LIST)
2159 return tsi_end_p (tsi_start (body));
2160 else
2161 return empty_expr_stmt_p (body);
2163 return false;
2166 /* Perform Koenig lookup. FN is the postfix-expression representing
2167 the function (or functions) to call; ARGS are the arguments to the
2168 call. Returns the functions to be considered by overload resolution. */
2170 tree
2171 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2172 tsubst_flags_t complain)
2174 tree identifier = NULL_TREE;
2175 tree functions = NULL_TREE;
2176 tree tmpl_args = NULL_TREE;
2177 bool template_id = false;
2179 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2181 /* Use a separate flag to handle null args. */
2182 template_id = true;
2183 tmpl_args = TREE_OPERAND (fn, 1);
2184 fn = TREE_OPERAND (fn, 0);
2187 /* Find the name of the overloaded function. */
2188 if (identifier_p (fn))
2189 identifier = fn;
2190 else if (is_overloaded_fn (fn))
2192 functions = fn;
2193 identifier = DECL_NAME (get_first_fn (functions));
2195 else if (DECL_P (fn))
2197 functions = fn;
2198 identifier = DECL_NAME (fn);
2201 /* A call to a namespace-scope function using an unqualified name.
2203 Do Koenig lookup -- unless any of the arguments are
2204 type-dependent. */
2205 if (!any_type_dependent_arguments_p (args)
2206 && !any_dependent_template_arguments_p (tmpl_args))
2208 fn = lookup_arg_dependent (identifier, functions, args);
2209 if (!fn)
2211 /* The unqualified name could not be resolved. */
2212 if (complain)
2213 fn = unqualified_fn_lookup_error (identifier);
2214 else
2215 fn = identifier;
2219 if (fn && template_id)
2220 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2222 return fn;
2225 /* Generate an expression for `FN (ARGS)'. This may change the
2226 contents of ARGS.
2228 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2229 as a virtual call, even if FN is virtual. (This flag is set when
2230 encountering an expression where the function name is explicitly
2231 qualified. For example a call to `X::f' never generates a virtual
2232 call.)
2234 Returns code for the call. */
2236 tree
2237 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2238 bool koenig_p, tsubst_flags_t complain)
2240 tree result;
2241 tree orig_fn;
2242 vec<tree, va_gc> *orig_args = NULL;
2244 if (fn == error_mark_node)
2245 return error_mark_node;
2247 gcc_assert (!TYPE_P (fn));
2249 orig_fn = fn;
2251 if (processing_template_decl)
2253 /* If the call expression is dependent, build a CALL_EXPR node
2254 with no type; type_dependent_expression_p recognizes
2255 expressions with no type as being dependent. */
2256 if (type_dependent_expression_p (fn)
2257 || any_type_dependent_arguments_p (*args)
2258 /* For a non-static member function that doesn't have an
2259 explicit object argument, we need to specifically
2260 test the type dependency of the "this" pointer because it
2261 is not included in *ARGS even though it is considered to
2262 be part of the list of arguments. Note that this is
2263 related to CWG issues 515 and 1005. */
2264 || (TREE_CODE (fn) != COMPONENT_REF
2265 && non_static_member_function_p (fn)
2266 && current_class_ref
2267 && type_dependent_expression_p (current_class_ref)))
2269 result = build_nt_call_vec (fn, *args);
2270 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2271 KOENIG_LOOKUP_P (result) = koenig_p;
2272 if (cfun)
2276 tree fndecl = OVL_CURRENT (fn);
2277 if (TREE_CODE (fndecl) != FUNCTION_DECL
2278 || !TREE_THIS_VOLATILE (fndecl))
2279 break;
2280 fn = OVL_NEXT (fn);
2282 while (fn);
2283 if (!fn)
2284 current_function_returns_abnormally = 1;
2286 return result;
2288 orig_args = make_tree_vector_copy (*args);
2289 if (!BASELINK_P (fn)
2290 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2291 && TREE_TYPE (fn) != unknown_type_node)
2292 fn = build_non_dependent_expr (fn);
2293 make_args_non_dependent (*args);
2296 if (TREE_CODE (fn) == COMPONENT_REF)
2298 tree member = TREE_OPERAND (fn, 1);
2299 if (BASELINK_P (member))
2301 tree object = TREE_OPERAND (fn, 0);
2302 return build_new_method_call (object, member,
2303 args, NULL_TREE,
2304 (disallow_virtual
2305 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2306 : LOOKUP_NORMAL),
2307 /*fn_p=*/NULL,
2308 complain);
2312 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2313 if (TREE_CODE (fn) == ADDR_EXPR
2314 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2315 fn = TREE_OPERAND (fn, 0);
2317 if (is_overloaded_fn (fn))
2318 fn = baselink_for_fns (fn);
2320 result = NULL_TREE;
2321 if (BASELINK_P (fn))
2323 tree object;
2325 /* A call to a member function. From [over.call.func]:
2327 If the keyword this is in scope and refers to the class of
2328 that member function, or a derived class thereof, then the
2329 function call is transformed into a qualified function call
2330 using (*this) as the postfix-expression to the left of the
2331 . operator.... [Otherwise] a contrived object of type T
2332 becomes the implied object argument.
2334 In this situation:
2336 struct A { void f(); };
2337 struct B : public A {};
2338 struct C : public A { void g() { B::f(); }};
2340 "the class of that member function" refers to `A'. But 11.2
2341 [class.access.base] says that we need to convert 'this' to B* as
2342 part of the access, so we pass 'B' to maybe_dummy_object. */
2344 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2345 NULL);
2347 if (processing_template_decl)
2349 if (type_dependent_expression_p (object))
2351 tree ret = build_nt_call_vec (orig_fn, orig_args);
2352 release_tree_vector (orig_args);
2353 return ret;
2355 object = build_non_dependent_expr (object);
2358 result = build_new_method_call (object, fn, args, NULL_TREE,
2359 (disallow_virtual
2360 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2361 : LOOKUP_NORMAL),
2362 /*fn_p=*/NULL,
2363 complain);
2365 else if (is_overloaded_fn (fn))
2367 /* If the function is an overloaded builtin, resolve it. */
2368 if (TREE_CODE (fn) == FUNCTION_DECL
2369 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2370 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2371 result = resolve_overloaded_builtin (input_location, fn, *args);
2373 if (!result)
2375 if (warn_sizeof_pointer_memaccess
2376 && (complain & tf_warning)
2377 && !vec_safe_is_empty (*args)
2378 && !processing_template_decl)
2380 location_t sizeof_arg_loc[3];
2381 tree sizeof_arg[3];
2382 unsigned int i;
2383 for (i = 0; i < 3; i++)
2385 tree t;
2387 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2388 sizeof_arg[i] = NULL_TREE;
2389 if (i >= (*args)->length ())
2390 continue;
2391 t = (**args)[i];
2392 if (TREE_CODE (t) != SIZEOF_EXPR)
2393 continue;
2394 if (SIZEOF_EXPR_TYPE_P (t))
2395 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2396 else
2397 sizeof_arg[i] = TREE_OPERAND (t, 0);
2398 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2400 sizeof_pointer_memaccess_warning
2401 (sizeof_arg_loc, fn, *args,
2402 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2405 /* A call to a namespace-scope function. */
2406 result = build_new_function_call (fn, args, koenig_p, complain);
2409 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2411 if (!vec_safe_is_empty (*args))
2412 error ("arguments to destructor are not allowed");
2413 /* Mark the pseudo-destructor call as having side-effects so
2414 that we do not issue warnings about its use. */
2415 result = build1 (NOP_EXPR,
2416 void_type_node,
2417 TREE_OPERAND (fn, 0));
2418 TREE_SIDE_EFFECTS (result) = 1;
2420 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2421 /* If the "function" is really an object of class type, it might
2422 have an overloaded `operator ()'. */
2423 result = build_op_call (fn, args, complain);
2425 if (!result)
2426 /* A call where the function is unknown. */
2427 result = cp_build_function_call_vec (fn, args, complain);
2429 if (processing_template_decl && result != error_mark_node)
2431 if (INDIRECT_REF_P (result))
2432 result = TREE_OPERAND (result, 0);
2433 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2434 SET_EXPR_LOCATION (result, input_location);
2435 KOENIG_LOOKUP_P (result) = koenig_p;
2436 release_tree_vector (orig_args);
2437 result = convert_from_reference (result);
2440 if (koenig_p)
2442 /* Free garbage OVERLOADs from arg-dependent lookup. */
2443 tree next = NULL_TREE;
2444 for (fn = orig_fn;
2445 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2446 fn = next)
2448 if (processing_template_decl)
2449 /* In a template, we'll re-use them at instantiation time. */
2450 OVL_ARG_DEPENDENT (fn) = false;
2451 else
2453 next = OVL_CHAIN (fn);
2454 ggc_free (fn);
2459 return result;
2462 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2463 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2464 POSTDECREMENT_EXPR.) */
2466 tree
2467 finish_increment_expr (tree expr, enum tree_code code)
2469 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2472 /* Finish a use of `this'. Returns an expression for `this'. */
2474 tree
2475 finish_this_expr (void)
2477 tree result = NULL_TREE;
2479 if (current_class_ptr)
2481 tree type = TREE_TYPE (current_class_ref);
2483 /* In a lambda expression, 'this' refers to the captured 'this'. */
2484 if (LAMBDA_TYPE_P (type))
2485 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2486 else
2487 result = current_class_ptr;
2490 if (result)
2491 /* The keyword 'this' is a prvalue expression. */
2492 return rvalue (result);
2494 tree fn = current_nonlambda_function ();
2495 if (fn && DECL_STATIC_FUNCTION_P (fn))
2496 error ("%<this%> is unavailable for static member functions");
2497 else if (fn)
2498 error ("invalid use of %<this%> in non-member function");
2499 else
2500 error ("invalid use of %<this%> at top level");
2501 return error_mark_node;
2504 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2505 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2506 the TYPE for the type given. If SCOPE is non-NULL, the expression
2507 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2509 tree
2510 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2511 location_t loc)
2513 if (object == error_mark_node || destructor == error_mark_node)
2514 return error_mark_node;
2516 gcc_assert (TYPE_P (destructor));
2518 if (!processing_template_decl)
2520 if (scope == error_mark_node)
2522 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2523 return error_mark_node;
2525 if (is_auto (destructor))
2526 destructor = TREE_TYPE (object);
2527 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2529 error_at (loc,
2530 "qualified type %qT does not match destructor name ~%qT",
2531 scope, destructor);
2532 return error_mark_node;
2536 /* [expr.pseudo] says both:
2538 The type designated by the pseudo-destructor-name shall be
2539 the same as the object type.
2541 and:
2543 The cv-unqualified versions of the object type and of the
2544 type designated by the pseudo-destructor-name shall be the
2545 same type.
2547 We implement the more generous second sentence, since that is
2548 what most other compilers do. */
2549 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2550 destructor))
2552 error_at (loc, "%qE is not of type %qT", object, destructor);
2553 return error_mark_node;
2557 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2558 scope, destructor);
2561 /* Finish an expression of the form CODE EXPR. */
2563 tree
2564 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2565 tsubst_flags_t complain)
2567 tree result = build_x_unary_op (loc, code, expr, complain);
2568 if ((complain & tf_warning)
2569 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2570 overflow_warning (input_location, result);
2572 return result;
2575 /* Finish a compound-literal expression. TYPE is the type to which
2576 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2578 tree
2579 finish_compound_literal (tree type, tree compound_literal,
2580 tsubst_flags_t complain)
2582 if (type == error_mark_node)
2583 return error_mark_node;
2585 if (TREE_CODE (type) == REFERENCE_TYPE)
2587 compound_literal
2588 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2589 complain);
2590 return cp_build_c_cast (type, compound_literal, complain);
2593 if (!TYPE_OBJ_P (type))
2595 if (complain & tf_error)
2596 error ("compound literal of non-object type %qT", type);
2597 return error_mark_node;
2600 if (processing_template_decl)
2602 TREE_TYPE (compound_literal) = type;
2603 /* Mark the expression as a compound literal. */
2604 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2605 return compound_literal;
2608 type = complete_type (type);
2610 if (TYPE_NON_AGGREGATE_CLASS (type))
2612 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2613 everywhere that deals with function arguments would be a pain, so
2614 just wrap it in a TREE_LIST. The parser set a flag so we know
2615 that it came from T{} rather than T({}). */
2616 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2617 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2618 return build_functional_cast (type, compound_literal, complain);
2621 if (TREE_CODE (type) == ARRAY_TYPE
2622 && check_array_initializer (NULL_TREE, type, compound_literal))
2623 return error_mark_node;
2624 compound_literal = reshape_init (type, compound_literal, complain);
2625 if (SCALAR_TYPE_P (type)
2626 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2627 && !check_narrowing (type, compound_literal, complain))
2628 return error_mark_node;
2629 if (TREE_CODE (type) == ARRAY_TYPE
2630 && TYPE_DOMAIN (type) == NULL_TREE)
2632 cp_complete_array_type_or_error (&type, compound_literal,
2633 false, complain);
2634 if (type == error_mark_node)
2635 return error_mark_node;
2637 compound_literal = digest_init (type, compound_literal, complain);
2638 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2639 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2640 /* Put static/constant array temporaries in static variables, but always
2641 represent class temporaries with TARGET_EXPR so we elide copies. */
2642 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2643 && TREE_CODE (type) == ARRAY_TYPE
2644 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2645 && initializer_constant_valid_p (compound_literal, type))
2647 tree decl = create_temporary_var (type);
2648 DECL_INITIAL (decl) = compound_literal;
2649 TREE_STATIC (decl) = 1;
2650 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2652 /* 5.19 says that a constant expression can include an
2653 lvalue-rvalue conversion applied to "a glvalue of literal type
2654 that refers to a non-volatile temporary object initialized
2655 with a constant expression". Rather than try to communicate
2656 that this VAR_DECL is a temporary, just mark it constexpr. */
2657 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2658 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2659 TREE_CONSTANT (decl) = true;
2661 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2662 decl = pushdecl_top_level (decl);
2663 DECL_NAME (decl) = make_anon_name ();
2664 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2665 /* Make sure the destructor is callable. */
2666 tree clean = cxx_maybe_build_cleanup (decl, complain);
2667 if (clean == error_mark_node)
2668 return error_mark_node;
2669 return decl;
2671 else
2672 return get_target_expr_sfinae (compound_literal, complain);
2675 /* Return the declaration for the function-name variable indicated by
2676 ID. */
2678 tree
2679 finish_fname (tree id)
2681 tree decl;
2683 decl = fname_decl (input_location, C_RID_CODE (id), id);
2684 if (processing_template_decl && current_function_decl
2685 && decl != error_mark_node)
2686 decl = DECL_NAME (decl);
2687 return decl;
2690 /* Finish a translation unit. */
2692 void
2693 finish_translation_unit (void)
2695 /* In case there were missing closebraces,
2696 get us back to the global binding level. */
2697 pop_everything ();
2698 while (current_namespace != global_namespace)
2699 pop_namespace ();
2701 /* Do file scope __FUNCTION__ et al. */
2702 finish_fname_decls ();
2705 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2706 Returns the parameter. */
2708 tree
2709 finish_template_type_parm (tree aggr, tree identifier)
2711 if (aggr != class_type_node)
2713 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2714 aggr = class_type_node;
2717 return build_tree_list (aggr, identifier);
2720 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2721 Returns the parameter. */
2723 tree
2724 finish_template_template_parm (tree aggr, tree identifier)
2726 tree decl = build_decl (input_location,
2727 TYPE_DECL, identifier, NULL_TREE);
2728 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2729 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2730 DECL_TEMPLATE_RESULT (tmpl) = decl;
2731 DECL_ARTIFICIAL (decl) = 1;
2732 end_template_decl ();
2734 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2736 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2737 /*is_primary=*/true, /*is_partial=*/false,
2738 /*is_friend=*/0);
2740 return finish_template_type_parm (aggr, tmpl);
2743 /* ARGUMENT is the default-argument value for a template template
2744 parameter. If ARGUMENT is invalid, issue error messages and return
2745 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2747 tree
2748 check_template_template_default_arg (tree argument)
2750 if (TREE_CODE (argument) != TEMPLATE_DECL
2751 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2752 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2754 if (TREE_CODE (argument) == TYPE_DECL)
2755 error ("invalid use of type %qT as a default value for a template "
2756 "template-parameter", TREE_TYPE (argument));
2757 else
2758 error ("invalid default argument for a template template parameter");
2759 return error_mark_node;
2762 return argument;
2765 /* Begin a class definition, as indicated by T. */
2767 tree
2768 begin_class_definition (tree t)
2770 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2771 return error_mark_node;
2773 if (processing_template_parmlist)
2775 error ("definition of %q#T inside template parameter list", t);
2776 return error_mark_node;
2779 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2780 are passed the same as decimal scalar types. */
2781 if (TREE_CODE (t) == RECORD_TYPE
2782 && !processing_template_decl)
2784 tree ns = TYPE_CONTEXT (t);
2785 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2786 && DECL_CONTEXT (ns) == std_node
2787 && DECL_NAME (ns)
2788 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2790 const char *n = TYPE_NAME_STRING (t);
2791 if ((strcmp (n, "decimal32") == 0)
2792 || (strcmp (n, "decimal64") == 0)
2793 || (strcmp (n, "decimal128") == 0))
2794 TYPE_TRANSPARENT_AGGR (t) = 1;
2798 /* A non-implicit typename comes from code like:
2800 template <typename T> struct A {
2801 template <typename U> struct A<T>::B ...
2803 This is erroneous. */
2804 else if (TREE_CODE (t) == TYPENAME_TYPE)
2806 error ("invalid definition of qualified type %qT", t);
2807 t = error_mark_node;
2810 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2812 t = make_class_type (RECORD_TYPE);
2813 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2816 if (TYPE_BEING_DEFINED (t))
2818 t = make_class_type (TREE_CODE (t));
2819 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2821 maybe_process_partial_specialization (t);
2822 pushclass (t);
2823 TYPE_BEING_DEFINED (t) = 1;
2824 class_binding_level->defining_class_p = 1;
2826 if (flag_pack_struct)
2828 tree v;
2829 TYPE_PACKED (t) = 1;
2830 /* Even though the type is being defined for the first time
2831 here, there might have been a forward declaration, so there
2832 might be cv-qualified variants of T. */
2833 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2834 TYPE_PACKED (v) = 1;
2836 /* Reset the interface data, at the earliest possible
2837 moment, as it might have been set via a class foo;
2838 before. */
2839 if (! TYPE_ANONYMOUS_P (t))
2841 struct c_fileinfo *finfo = \
2842 get_fileinfo (LOCATION_FILE (input_location));
2843 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2844 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2845 (t, finfo->interface_unknown);
2847 reset_specialization();
2849 /* Make a declaration for this class in its own scope. */
2850 build_self_reference ();
2852 return t;
2855 /* Finish the member declaration given by DECL. */
2857 void
2858 finish_member_declaration (tree decl)
2860 if (decl == error_mark_node || decl == NULL_TREE)
2861 return;
2863 if (decl == void_type_node)
2864 /* The COMPONENT was a friend, not a member, and so there's
2865 nothing for us to do. */
2866 return;
2868 /* We should see only one DECL at a time. */
2869 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2871 /* Set up access control for DECL. */
2872 TREE_PRIVATE (decl)
2873 = (current_access_specifier == access_private_node);
2874 TREE_PROTECTED (decl)
2875 = (current_access_specifier == access_protected_node);
2876 if (TREE_CODE (decl) == TEMPLATE_DECL)
2878 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2879 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2882 /* Mark the DECL as a member of the current class, unless it's
2883 a member of an enumeration. */
2884 if (TREE_CODE (decl) != CONST_DECL)
2885 DECL_CONTEXT (decl) = current_class_type;
2887 /* Check for bare parameter packs in the member variable declaration. */
2888 if (TREE_CODE (decl) == FIELD_DECL)
2890 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2891 TREE_TYPE (decl) = error_mark_node;
2892 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2893 DECL_ATTRIBUTES (decl) = NULL_TREE;
2896 /* [dcl.link]
2898 A C language linkage is ignored for the names of class members
2899 and the member function type of class member functions. */
2900 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2901 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2903 /* Put functions on the TYPE_METHODS list and everything else on the
2904 TYPE_FIELDS list. Note that these are built up in reverse order.
2905 We reverse them (to obtain declaration order) in finish_struct. */
2906 if (DECL_DECLARES_FUNCTION_P (decl))
2908 /* We also need to add this function to the
2909 CLASSTYPE_METHOD_VEC. */
2910 if (add_method (current_class_type, decl, NULL_TREE))
2912 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2913 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2914 TYPE_METHODS (current_class_type) = decl;
2916 maybe_add_class_template_decl_list (current_class_type, decl,
2917 /*friend_p=*/0);
2920 /* Enter the DECL into the scope of the class, if the class
2921 isn't a closure (whose fields are supposed to be unnamed). */
2922 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2923 || pushdecl_class_level (decl))
2925 if (TREE_CODE (decl) == USING_DECL)
2927 /* For now, ignore class-scope USING_DECLS, so that
2928 debugging backends do not see them. */
2929 DECL_IGNORED_P (decl) = 1;
2932 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2933 go at the beginning. The reason is that lookup_field_1
2934 searches the list in order, and we want a field name to
2935 override a type name so that the "struct stat hack" will
2936 work. In particular:
2938 struct S { enum E { }; int E } s;
2939 s.E = 3;
2941 is valid. In addition, the FIELD_DECLs must be maintained in
2942 declaration order so that class layout works as expected.
2943 However, we don't need that order until class layout, so we
2944 save a little time by putting FIELD_DECLs on in reverse order
2945 here, and then reversing them in finish_struct_1. (We could
2946 also keep a pointer to the correct insertion points in the
2947 list.) */
2949 if (TREE_CODE (decl) == TYPE_DECL)
2950 TYPE_FIELDS (current_class_type)
2951 = chainon (TYPE_FIELDS (current_class_type), decl);
2952 else
2954 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2955 TYPE_FIELDS (current_class_type) = decl;
2958 maybe_add_class_template_decl_list (current_class_type, decl,
2959 /*friend_p=*/0);
2962 if (pch_file)
2963 note_decl_for_pch (decl);
2966 /* DECL has been declared while we are building a PCH file. Perform
2967 actions that we might normally undertake lazily, but which can be
2968 performed now so that they do not have to be performed in
2969 translation units which include the PCH file. */
2971 void
2972 note_decl_for_pch (tree decl)
2974 gcc_assert (pch_file);
2976 /* There's a good chance that we'll have to mangle names at some
2977 point, even if only for emission in debugging information. */
2978 if (VAR_OR_FUNCTION_DECL_P (decl)
2979 && !processing_template_decl)
2980 mangle_decl (decl);
2983 /* Finish processing a complete template declaration. The PARMS are
2984 the template parameters. */
2986 void
2987 finish_template_decl (tree parms)
2989 if (parms)
2990 end_template_decl ();
2991 else
2992 end_specialization ();
2995 /* Finish processing a template-id (which names a type) of the form
2996 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2997 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2998 the scope of template-id indicated. */
3000 tree
3001 finish_template_type (tree name, tree args, int entering_scope)
3003 tree type;
3005 type = lookup_template_class (name, args,
3006 NULL_TREE, NULL_TREE, entering_scope,
3007 tf_warning_or_error | tf_user);
3008 if (type == error_mark_node)
3009 return type;
3010 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3011 return TYPE_STUB_DECL (type);
3012 else
3013 return TYPE_NAME (type);
3016 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3017 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3018 BASE_CLASS, or NULL_TREE if an error occurred. The
3019 ACCESS_SPECIFIER is one of
3020 access_{default,public,protected_private}_node. For a virtual base
3021 we set TREE_TYPE. */
3023 tree
3024 finish_base_specifier (tree base, tree access, bool virtual_p)
3026 tree result;
3028 if (base == error_mark_node)
3030 error ("invalid base-class specification");
3031 result = NULL_TREE;
3033 else if (! MAYBE_CLASS_TYPE_P (base))
3035 error ("%qT is not a class type", base);
3036 result = NULL_TREE;
3038 else
3040 if (cp_type_quals (base) != 0)
3042 /* DR 484: Can a base-specifier name a cv-qualified
3043 class type? */
3044 base = TYPE_MAIN_VARIANT (base);
3046 result = build_tree_list (access, base);
3047 if (virtual_p)
3048 TREE_TYPE (result) = integer_type_node;
3051 return result;
3054 /* If FNS is a member function, a set of member functions, or a
3055 template-id referring to one or more member functions, return a
3056 BASELINK for FNS, incorporating the current access context.
3057 Otherwise, return FNS unchanged. */
3059 tree
3060 baselink_for_fns (tree fns)
3062 tree scope;
3063 tree cl;
3065 if (BASELINK_P (fns)
3066 || error_operand_p (fns))
3067 return fns;
3069 scope = ovl_scope (fns);
3070 if (!CLASS_TYPE_P (scope))
3071 return fns;
3073 cl = currently_open_derived_class (scope);
3074 if (!cl)
3075 cl = scope;
3076 cl = TYPE_BINFO (cl);
3077 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3080 /* Returns true iff DECL is a variable from a function outside
3081 the current one. */
3083 static bool
3084 outer_var_p (tree decl)
3086 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3087 && DECL_FUNCTION_SCOPE_P (decl)
3088 && (DECL_CONTEXT (decl) != current_function_decl
3089 || parsing_nsdmi ()));
3092 /* As above, but also checks that DECL is automatic. */
3094 bool
3095 outer_automatic_var_p (tree decl)
3097 return (outer_var_p (decl)
3098 && !TREE_STATIC (decl));
3101 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3102 rewrite it for lambda capture. */
3104 tree
3105 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3107 if (cp_unevaluated_operand)
3108 /* It's not a use (3.2) if we're in an unevaluated context. */
3109 return decl;
3110 if (decl == error_mark_node)
3111 return decl;
3113 tree context = DECL_CONTEXT (decl);
3114 tree containing_function = current_function_decl;
3115 tree lambda_stack = NULL_TREE;
3116 tree lambda_expr = NULL_TREE;
3117 tree initializer = convert_from_reference (decl);
3119 /* Mark it as used now even if the use is ill-formed. */
3120 if (!mark_used (decl, complain) && !(complain & tf_error))
3121 return error_mark_node;
3123 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3124 support for an approach in which a reference to a local
3125 [constant] automatic variable in a nested class or lambda body
3126 would enter the expression as an rvalue, which would reduce
3127 the complexity of the problem"
3129 FIXME update for final resolution of core issue 696. */
3130 if (decl_maybe_constant_var_p (decl))
3132 if (processing_template_decl)
3133 /* In a template, the constant value may not be in a usable
3134 form, so wait until instantiation time. */
3135 return decl;
3136 else if (decl_constant_var_p (decl))
3138 tree t = maybe_constant_value (convert_from_reference (decl));
3139 if (TREE_CONSTANT (t))
3140 return t;
3144 if (parsing_nsdmi ())
3145 containing_function = NULL_TREE;
3146 else
3147 /* If we are in a lambda function, we can move out until we hit
3148 1. the context,
3149 2. a non-lambda function, or
3150 3. a non-default capturing lambda function. */
3151 while (context != containing_function
3152 && LAMBDA_FUNCTION_P (containing_function))
3154 tree closure = DECL_CONTEXT (containing_function);
3155 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3157 if (TYPE_CLASS_SCOPE_P (closure))
3158 /* A lambda in an NSDMI (c++/64496). */
3159 break;
3161 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3162 == CPLD_NONE)
3163 break;
3165 lambda_stack = tree_cons (NULL_TREE,
3166 lambda_expr,
3167 lambda_stack);
3169 containing_function
3170 = decl_function_context (containing_function);
3173 if (lambda_expr && VAR_P (decl)
3174 && DECL_ANON_UNION_VAR_P (decl))
3176 if (complain & tf_error)
3177 error ("cannot capture member %qD of anonymous union", decl);
3178 return error_mark_node;
3180 if (context == containing_function)
3182 decl = add_default_capture (lambda_stack,
3183 /*id=*/DECL_NAME (decl),
3184 initializer);
3186 else if (lambda_expr)
3188 if (complain & tf_error)
3190 error ("%qD is not captured", decl);
3191 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3192 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3193 == CPLD_NONE)
3194 inform (location_of (closure),
3195 "the lambda has no capture-default");
3196 else if (TYPE_CLASS_SCOPE_P (closure))
3197 inform (0, "lambda in local class %q+T cannot "
3198 "capture variables from the enclosing context",
3199 TYPE_CONTEXT (closure));
3200 inform (input_location, "%q+#D declared here", decl);
3202 return error_mark_node;
3204 else
3206 if (complain & tf_error)
3207 error (VAR_P (decl)
3208 ? G_("use of local variable with automatic storage from containing function")
3209 : G_("use of parameter from containing function"));
3210 inform (input_location, "%q+#D declared here", decl);
3211 return error_mark_node;
3213 return decl;
3216 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3217 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3218 if non-NULL, is the type or namespace used to explicitly qualify
3219 ID_EXPRESSION. DECL is the entity to which that name has been
3220 resolved.
3222 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3223 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3224 be set to true if this expression isn't permitted in a
3225 constant-expression, but it is otherwise not set by this function.
3226 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3227 constant-expression, but a non-constant expression is also
3228 permissible.
3230 DONE is true if this expression is a complete postfix-expression;
3231 it is false if this expression is followed by '->', '[', '(', etc.
3232 ADDRESS_P is true iff this expression is the operand of '&'.
3233 TEMPLATE_P is true iff the qualified-id was of the form
3234 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3235 appears as a template argument.
3237 If an error occurs, and it is the kind of error that might cause
3238 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3239 is the caller's responsibility to issue the message. *ERROR_MSG
3240 will be a string with static storage duration, so the caller need
3241 not "free" it.
3243 Return an expression for the entity, after issuing appropriate
3244 diagnostics. This function is also responsible for transforming a
3245 reference to a non-static member into a COMPONENT_REF that makes
3246 the use of "this" explicit.
3248 Upon return, *IDK will be filled in appropriately. */
3249 tree
3250 finish_id_expression (tree id_expression,
3251 tree decl,
3252 tree scope,
3253 cp_id_kind *idk,
3254 bool integral_constant_expression_p,
3255 bool allow_non_integral_constant_expression_p,
3256 bool *non_integral_constant_expression_p,
3257 bool template_p,
3258 bool done,
3259 bool address_p,
3260 bool template_arg_p,
3261 const char **error_msg,
3262 location_t location)
3264 decl = strip_using_decl (decl);
3266 /* Initialize the output parameters. */
3267 *idk = CP_ID_KIND_NONE;
3268 *error_msg = NULL;
3270 if (id_expression == error_mark_node)
3271 return error_mark_node;
3272 /* If we have a template-id, then no further lookup is
3273 required. If the template-id was for a template-class, we
3274 will sometimes have a TYPE_DECL at this point. */
3275 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3276 || TREE_CODE (decl) == TYPE_DECL)
3278 /* Look up the name. */
3279 else
3281 if (decl == error_mark_node)
3283 /* Name lookup failed. */
3284 if (scope
3285 && (!TYPE_P (scope)
3286 || (!dependent_type_p (scope)
3287 && !(identifier_p (id_expression)
3288 && IDENTIFIER_TYPENAME_P (id_expression)
3289 && dependent_type_p (TREE_TYPE (id_expression))))))
3291 /* If the qualifying type is non-dependent (and the name
3292 does not name a conversion operator to a dependent
3293 type), issue an error. */
3294 qualified_name_lookup_error (scope, id_expression, decl, location);
3295 return error_mark_node;
3297 else if (!scope)
3299 /* It may be resolved via Koenig lookup. */
3300 *idk = CP_ID_KIND_UNQUALIFIED;
3301 return id_expression;
3303 else
3304 decl = id_expression;
3306 /* If DECL is a variable that would be out of scope under
3307 ANSI/ISO rules, but in scope in the ARM, name lookup
3308 will succeed. Issue a diagnostic here. */
3309 else
3310 decl = check_for_out_of_scope_variable (decl);
3312 /* Remember that the name was used in the definition of
3313 the current class so that we can check later to see if
3314 the meaning would have been different after the class
3315 was entirely defined. */
3316 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3317 maybe_note_name_used_in_class (id_expression, decl);
3319 /* Disallow uses of local variables from containing functions, except
3320 within lambda-expressions. */
3321 if (outer_automatic_var_p (decl))
3323 decl = process_outer_var_ref (decl, tf_warning_or_error);
3324 if (decl == error_mark_node)
3325 return error_mark_node;
3328 /* Also disallow uses of function parameters outside the function
3329 body, except inside an unevaluated context (i.e. decltype). */
3330 if (TREE_CODE (decl) == PARM_DECL
3331 && DECL_CONTEXT (decl) == NULL_TREE
3332 && !cp_unevaluated_operand)
3334 *error_msg = "use of parameter outside function body";
3335 return error_mark_node;
3339 /* If we didn't find anything, or what we found was a type,
3340 then this wasn't really an id-expression. */
3341 if (TREE_CODE (decl) == TEMPLATE_DECL
3342 && !DECL_FUNCTION_TEMPLATE_P (decl))
3344 *error_msg = "missing template arguments";
3345 return error_mark_node;
3347 else if (TREE_CODE (decl) == TYPE_DECL
3348 || TREE_CODE (decl) == NAMESPACE_DECL)
3350 *error_msg = "expected primary-expression";
3351 return error_mark_node;
3354 /* If the name resolved to a template parameter, there is no
3355 need to look it up again later. */
3356 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3357 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3359 tree r;
3361 *idk = CP_ID_KIND_NONE;
3362 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3363 decl = TEMPLATE_PARM_DECL (decl);
3364 r = convert_from_reference (DECL_INITIAL (decl));
3366 if (integral_constant_expression_p
3367 && !dependent_type_p (TREE_TYPE (decl))
3368 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3370 if (!allow_non_integral_constant_expression_p)
3371 error ("template parameter %qD of type %qT is not allowed in "
3372 "an integral constant expression because it is not of "
3373 "integral or enumeration type", decl, TREE_TYPE (decl));
3374 *non_integral_constant_expression_p = true;
3376 return r;
3378 else
3380 bool dependent_p;
3382 /* If the declaration was explicitly qualified indicate
3383 that. The semantics of `A::f(3)' are different than
3384 `f(3)' if `f' is virtual. */
3385 *idk = (scope
3386 ? CP_ID_KIND_QUALIFIED
3387 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3388 ? CP_ID_KIND_TEMPLATE_ID
3389 : CP_ID_KIND_UNQUALIFIED));
3392 /* [temp.dep.expr]
3394 An id-expression is type-dependent if it contains an
3395 identifier that was declared with a dependent type.
3397 The standard is not very specific about an id-expression that
3398 names a set of overloaded functions. What if some of them
3399 have dependent types and some of them do not? Presumably,
3400 such a name should be treated as a dependent name. */
3401 /* Assume the name is not dependent. */
3402 dependent_p = false;
3403 if (!processing_template_decl)
3404 /* No names are dependent outside a template. */
3406 else if (TREE_CODE (decl) == CONST_DECL)
3407 /* We don't want to treat enumerators as dependent. */
3409 /* A template-id where the name of the template was not resolved
3410 is definitely dependent. */
3411 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3412 && (identifier_p (TREE_OPERAND (decl, 0))))
3413 dependent_p = true;
3414 /* For anything except an overloaded function, just check its
3415 type. */
3416 else if (!is_overloaded_fn (decl))
3417 dependent_p
3418 = dependent_type_p (TREE_TYPE (decl));
3419 /* For a set of overloaded functions, check each of the
3420 functions. */
3421 else
3423 tree fns = decl;
3425 if (BASELINK_P (fns))
3426 fns = BASELINK_FUNCTIONS (fns);
3428 /* For a template-id, check to see if the template
3429 arguments are dependent. */
3430 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3432 tree args = TREE_OPERAND (fns, 1);
3433 dependent_p = any_dependent_template_arguments_p (args);
3434 /* The functions are those referred to by the
3435 template-id. */
3436 fns = TREE_OPERAND (fns, 0);
3439 /* If there are no dependent template arguments, go through
3440 the overloaded functions. */
3441 while (fns && !dependent_p)
3443 tree fn = OVL_CURRENT (fns);
3445 /* Member functions of dependent classes are
3446 dependent. */
3447 if (TREE_CODE (fn) == FUNCTION_DECL
3448 && type_dependent_expression_p (fn))
3449 dependent_p = true;
3450 else if (TREE_CODE (fn) == TEMPLATE_DECL
3451 && dependent_template_p (fn))
3452 dependent_p = true;
3454 fns = OVL_NEXT (fns);
3458 /* If the name was dependent on a template parameter, we will
3459 resolve the name at instantiation time. */
3460 if (dependent_p)
3462 /* Create a SCOPE_REF for qualified names, if the scope is
3463 dependent. */
3464 if (scope)
3466 if (TYPE_P (scope))
3468 if (address_p && done)
3469 decl = finish_qualified_id_expr (scope, decl,
3470 done, address_p,
3471 template_p,
3472 template_arg_p,
3473 tf_warning_or_error);
3474 else
3476 tree type = NULL_TREE;
3477 if (DECL_P (decl) && !dependent_scope_p (scope))
3478 type = TREE_TYPE (decl);
3479 decl = build_qualified_name (type,
3480 scope,
3481 id_expression,
3482 template_p);
3485 if (TREE_TYPE (decl))
3486 decl = convert_from_reference (decl);
3487 return decl;
3489 /* A TEMPLATE_ID already contains all the information we
3490 need. */
3491 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3492 return id_expression;
3493 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3494 /* If we found a variable, then name lookup during the
3495 instantiation will always resolve to the same VAR_DECL
3496 (or an instantiation thereof). */
3497 if (VAR_P (decl)
3498 || TREE_CODE (decl) == PARM_DECL)
3500 mark_used (decl);
3501 return convert_from_reference (decl);
3503 /* The same is true for FIELD_DECL, but we also need to
3504 make sure that the syntax is correct. */
3505 else if (TREE_CODE (decl) == FIELD_DECL)
3507 /* Since SCOPE is NULL here, this is an unqualified name.
3508 Access checking has been performed during name lookup
3509 already. Turn off checking to avoid duplicate errors. */
3510 push_deferring_access_checks (dk_no_check);
3511 decl = finish_non_static_data_member
3512 (decl, NULL_TREE,
3513 /*qualifying_scope=*/NULL_TREE);
3514 pop_deferring_access_checks ();
3515 return decl;
3517 return id_expression;
3520 if (TREE_CODE (decl) == NAMESPACE_DECL)
3522 error ("use of namespace %qD as expression", decl);
3523 return error_mark_node;
3525 else if (DECL_CLASS_TEMPLATE_P (decl))
3527 error ("use of class template %qT as expression", decl);
3528 return error_mark_node;
3530 else if (TREE_CODE (decl) == TREE_LIST)
3532 /* Ambiguous reference to base members. */
3533 error ("request for member %qD is ambiguous in "
3534 "multiple inheritance lattice", id_expression);
3535 print_candidates (decl);
3536 return error_mark_node;
3539 /* Mark variable-like entities as used. Functions are similarly
3540 marked either below or after overload resolution. */
3541 if ((VAR_P (decl)
3542 || TREE_CODE (decl) == PARM_DECL
3543 || TREE_CODE (decl) == CONST_DECL
3544 || TREE_CODE (decl) == RESULT_DECL)
3545 && !mark_used (decl))
3546 return error_mark_node;
3548 /* Only certain kinds of names are allowed in constant
3549 expression. Template parameters have already
3550 been handled above. */
3551 if (! error_operand_p (decl)
3552 && integral_constant_expression_p
3553 && ! decl_constant_var_p (decl)
3554 && TREE_CODE (decl) != CONST_DECL
3555 && ! builtin_valid_in_constant_expr_p (decl))
3557 if (!allow_non_integral_constant_expression_p)
3559 error ("%qD cannot appear in a constant-expression", decl);
3560 return error_mark_node;
3562 *non_integral_constant_expression_p = true;
3565 tree wrap;
3566 if (VAR_P (decl)
3567 && !cp_unevaluated_operand
3568 && !processing_template_decl
3569 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3570 && CP_DECL_THREAD_LOCAL_P (decl)
3571 && (wrap = get_tls_wrapper_fn (decl)))
3573 /* Replace an evaluated use of the thread_local variable with
3574 a call to its wrapper. */
3575 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3577 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3578 && variable_template_p (TREE_OPERAND (decl, 0)))
3580 decl = finish_template_variable (decl);
3581 mark_used (decl);
3583 else if (scope)
3585 decl = (adjust_result_of_qualified_name_lookup
3586 (decl, scope, current_nonlambda_class_type()));
3588 if (TREE_CODE (decl) == FUNCTION_DECL)
3589 mark_used (decl);
3591 if (TYPE_P (scope))
3592 decl = finish_qualified_id_expr (scope,
3593 decl,
3594 done,
3595 address_p,
3596 template_p,
3597 template_arg_p,
3598 tf_warning_or_error);
3599 else
3600 decl = convert_from_reference (decl);
3602 else if (TREE_CODE (decl) == FIELD_DECL)
3604 /* Since SCOPE is NULL here, this is an unqualified name.
3605 Access checking has been performed during name lookup
3606 already. Turn off checking to avoid duplicate errors. */
3607 push_deferring_access_checks (dk_no_check);
3608 decl = finish_non_static_data_member (decl, NULL_TREE,
3609 /*qualifying_scope=*/NULL_TREE);
3610 pop_deferring_access_checks ();
3612 else if (is_overloaded_fn (decl))
3614 tree first_fn;
3616 first_fn = get_first_fn (decl);
3617 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3618 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3620 if (!really_overloaded_fn (decl)
3621 && !mark_used (first_fn))
3622 return error_mark_node;
3624 if (!template_arg_p
3625 && TREE_CODE (first_fn) == FUNCTION_DECL
3626 && DECL_FUNCTION_MEMBER_P (first_fn)
3627 && !shared_member_p (decl))
3629 /* A set of member functions. */
3630 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3631 return finish_class_member_access_expr (decl, id_expression,
3632 /*template_p=*/false,
3633 tf_warning_or_error);
3636 decl = baselink_for_fns (decl);
3638 else
3640 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3641 && DECL_CLASS_SCOPE_P (decl))
3643 tree context = context_for_name_lookup (decl);
3644 if (context != current_class_type)
3646 tree path = currently_open_derived_class (context);
3647 perform_or_defer_access_check (TYPE_BINFO (path),
3648 decl, decl,
3649 tf_warning_or_error);
3653 decl = convert_from_reference (decl);
3657 return decl;
3660 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3661 use as a type-specifier. */
3663 tree
3664 finish_typeof (tree expr)
3666 tree type;
3668 if (type_dependent_expression_p (expr))
3670 type = cxx_make_type (TYPEOF_TYPE);
3671 TYPEOF_TYPE_EXPR (type) = expr;
3672 SET_TYPE_STRUCTURAL_EQUALITY (type);
3674 return type;
3677 expr = mark_type_use (expr);
3679 type = unlowered_expr_type (expr);
3681 if (!type || type == unknown_type_node)
3683 error ("type of %qE is unknown", expr);
3684 return error_mark_node;
3687 return type;
3690 /* Implement the __underlying_type keyword: Return the underlying
3691 type of TYPE, suitable for use as a type-specifier. */
3693 tree
3694 finish_underlying_type (tree type)
3696 tree underlying_type;
3698 if (processing_template_decl)
3700 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3701 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3702 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3704 return underlying_type;
3707 complete_type (type);
3709 if (TREE_CODE (type) != ENUMERAL_TYPE)
3711 error ("%qT is not an enumeration type", type);
3712 return error_mark_node;
3715 underlying_type = ENUM_UNDERLYING_TYPE (type);
3717 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3718 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3719 See finish_enum_value_list for details. */
3720 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3721 underlying_type
3722 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3723 TYPE_UNSIGNED (underlying_type));
3725 return underlying_type;
3728 /* Implement the __direct_bases keyword: Return the direct base classes
3729 of type */
3731 tree
3732 calculate_direct_bases (tree type)
3734 vec<tree, va_gc> *vector = make_tree_vector();
3735 tree bases_vec = NULL_TREE;
3736 vec<tree, va_gc> *base_binfos;
3737 tree binfo;
3738 unsigned i;
3740 complete_type (type);
3742 if (!NON_UNION_CLASS_TYPE_P (type))
3743 return make_tree_vec (0);
3745 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3747 /* Virtual bases are initialized first */
3748 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3750 if (BINFO_VIRTUAL_P (binfo))
3752 vec_safe_push (vector, binfo);
3756 /* Now non-virtuals */
3757 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3759 if (!BINFO_VIRTUAL_P (binfo))
3761 vec_safe_push (vector, binfo);
3766 bases_vec = make_tree_vec (vector->length ());
3768 for (i = 0; i < vector->length (); ++i)
3770 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3772 return bases_vec;
3775 /* Implement the __bases keyword: Return the base classes
3776 of type */
3778 /* Find morally non-virtual base classes by walking binfo hierarchy */
3779 /* Virtual base classes are handled separately in finish_bases */
3781 static tree
3782 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3784 /* Don't walk bases of virtual bases */
3785 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3788 static tree
3789 dfs_calculate_bases_post (tree binfo, void *data_)
3791 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3792 if (!BINFO_VIRTUAL_P (binfo))
3794 vec_safe_push (*data, BINFO_TYPE (binfo));
3796 return NULL_TREE;
3799 /* Calculates the morally non-virtual base classes of a class */
3800 static vec<tree, va_gc> *
3801 calculate_bases_helper (tree type)
3803 vec<tree, va_gc> *vector = make_tree_vector();
3805 /* Now add non-virtual base classes in order of construction */
3806 dfs_walk_all (TYPE_BINFO (type),
3807 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3808 return vector;
3811 tree
3812 calculate_bases (tree type)
3814 vec<tree, va_gc> *vector = make_tree_vector();
3815 tree bases_vec = NULL_TREE;
3816 unsigned i;
3817 vec<tree, va_gc> *vbases;
3818 vec<tree, va_gc> *nonvbases;
3819 tree binfo;
3821 complete_type (type);
3823 if (!NON_UNION_CLASS_TYPE_P (type))
3824 return make_tree_vec (0);
3826 /* First go through virtual base classes */
3827 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3828 vec_safe_iterate (vbases, i, &binfo); i++)
3830 vec<tree, va_gc> *vbase_bases;
3831 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3832 vec_safe_splice (vector, vbase_bases);
3833 release_tree_vector (vbase_bases);
3836 /* Now for the non-virtual bases */
3837 nonvbases = calculate_bases_helper (type);
3838 vec_safe_splice (vector, nonvbases);
3839 release_tree_vector (nonvbases);
3841 /* Last element is entire class, so don't copy */
3842 bases_vec = make_tree_vec (vector->length () - 1);
3844 for (i = 0; i < vector->length () - 1; ++i)
3846 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3848 release_tree_vector (vector);
3849 return bases_vec;
3852 tree
3853 finish_bases (tree type, bool direct)
3855 tree bases = NULL_TREE;
3857 if (!processing_template_decl)
3859 /* Parameter packs can only be used in templates */
3860 error ("Parameter pack __bases only valid in template declaration");
3861 return error_mark_node;
3864 bases = cxx_make_type (BASES);
3865 BASES_TYPE (bases) = type;
3866 BASES_DIRECT (bases) = direct;
3867 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3869 return bases;
3872 /* Perform C++-specific checks for __builtin_offsetof before calling
3873 fold_offsetof. */
3875 tree
3876 finish_offsetof (tree expr, location_t loc)
3878 /* If we're processing a template, we can't finish the semantics yet.
3879 Otherwise we can fold the entire expression now. */
3880 if (processing_template_decl)
3882 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3883 SET_EXPR_LOCATION (expr, loc);
3884 return expr;
3887 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3889 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3890 TREE_OPERAND (expr, 2));
3891 return error_mark_node;
3893 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3894 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3895 || TREE_TYPE (expr) == unknown_type_node)
3897 if (INDIRECT_REF_P (expr))
3898 error ("second operand of %<offsetof%> is neither a single "
3899 "identifier nor a sequence of member accesses and "
3900 "array references");
3901 else
3903 if (TREE_CODE (expr) == COMPONENT_REF
3904 || TREE_CODE (expr) == COMPOUND_EXPR)
3905 expr = TREE_OPERAND (expr, 1);
3906 error ("cannot apply %<offsetof%> to member function %qD", expr);
3908 return error_mark_node;
3910 if (REFERENCE_REF_P (expr))
3911 expr = TREE_OPERAND (expr, 0);
3912 if (TREE_CODE (expr) == COMPONENT_REF)
3914 tree object = TREE_OPERAND (expr, 0);
3915 if (!complete_type_or_else (TREE_TYPE (object), object))
3916 return error_mark_node;
3917 if (warn_invalid_offsetof
3918 && CLASS_TYPE_P (TREE_TYPE (object))
3919 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3920 && cp_unevaluated_operand == 0)
3921 pedwarn (loc, OPT_Winvalid_offsetof,
3922 "offsetof within non-standard-layout type %qT is undefined",
3923 TREE_TYPE (object));
3925 return fold_offsetof (expr);
3928 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3929 function is broken out from the above for the benefit of the tree-ssa
3930 project. */
3932 void
3933 simplify_aggr_init_expr (tree *tp)
3935 tree aggr_init_expr = *tp;
3937 /* Form an appropriate CALL_EXPR. */
3938 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3939 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3940 tree type = TREE_TYPE (slot);
3942 tree call_expr;
3943 enum style_t { ctor, arg, pcc } style;
3945 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3946 style = ctor;
3947 #ifdef PCC_STATIC_STRUCT_RETURN
3948 else if (1)
3949 style = pcc;
3950 #endif
3951 else
3953 gcc_assert (TREE_ADDRESSABLE (type));
3954 style = arg;
3957 call_expr = build_call_array_loc (input_location,
3958 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3960 aggr_init_expr_nargs (aggr_init_expr),
3961 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3962 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3963 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3965 if (style == ctor)
3967 /* Replace the first argument to the ctor with the address of the
3968 slot. */
3969 cxx_mark_addressable (slot);
3970 CALL_EXPR_ARG (call_expr, 0) =
3971 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3973 else if (style == arg)
3975 /* Just mark it addressable here, and leave the rest to
3976 expand_call{,_inline}. */
3977 cxx_mark_addressable (slot);
3978 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3979 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3981 else if (style == pcc)
3983 /* If we're using the non-reentrant PCC calling convention, then we
3984 need to copy the returned value out of the static buffer into the
3985 SLOT. */
3986 push_deferring_access_checks (dk_no_check);
3987 call_expr = build_aggr_init (slot, call_expr,
3988 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3989 tf_warning_or_error);
3990 pop_deferring_access_checks ();
3991 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3994 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3996 tree init = build_zero_init (type, NULL_TREE,
3997 /*static_storage_p=*/false);
3998 init = build2 (INIT_EXPR, void_type_node, slot, init);
3999 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4000 init, call_expr);
4003 *tp = call_expr;
4006 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4008 void
4009 emit_associated_thunks (tree fn)
4011 /* When we use vcall offsets, we emit thunks with the virtual
4012 functions to which they thunk. The whole point of vcall offsets
4013 is so that you can know statically the entire set of thunks that
4014 will ever be needed for a given virtual function, thereby
4015 enabling you to output all the thunks with the function itself. */
4016 if (DECL_VIRTUAL_P (fn)
4017 /* Do not emit thunks for extern template instantiations. */
4018 && ! DECL_REALLY_EXTERN (fn))
4020 tree thunk;
4022 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4024 if (!THUNK_ALIAS (thunk))
4026 use_thunk (thunk, /*emit_p=*/1);
4027 if (DECL_RESULT_THUNK_P (thunk))
4029 tree probe;
4031 for (probe = DECL_THUNKS (thunk);
4032 probe; probe = DECL_CHAIN (probe))
4033 use_thunk (probe, /*emit_p=*/1);
4036 else
4037 gcc_assert (!DECL_THUNKS (thunk));
4042 /* Generate RTL for FN. */
4044 bool
4045 expand_or_defer_fn_1 (tree fn)
4047 /* When the parser calls us after finishing the body of a template
4048 function, we don't really want to expand the body. */
4049 if (processing_template_decl)
4051 /* Normally, collection only occurs in rest_of_compilation. So,
4052 if we don't collect here, we never collect junk generated
4053 during the processing of templates until we hit a
4054 non-template function. It's not safe to do this inside a
4055 nested class, though, as the parser may have local state that
4056 is not a GC root. */
4057 if (!function_depth)
4058 ggc_collect ();
4059 return false;
4062 gcc_assert (DECL_SAVED_TREE (fn));
4064 /* We make a decision about linkage for these functions at the end
4065 of the compilation. Until that point, we do not want the back
4066 end to output them -- but we do want it to see the bodies of
4067 these functions so that it can inline them as appropriate. */
4068 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4070 if (DECL_INTERFACE_KNOWN (fn))
4071 /* We've already made a decision as to how this function will
4072 be handled. */;
4073 else if (!at_eof)
4074 tentative_decl_linkage (fn);
4075 else
4076 import_export_decl (fn);
4078 /* If the user wants us to keep all inline functions, then mark
4079 this function as needed so that finish_file will make sure to
4080 output it later. Similarly, all dllexport'd functions must
4081 be emitted; there may be callers in other DLLs. */
4082 if (DECL_DECLARED_INLINE_P (fn)
4083 && !DECL_REALLY_EXTERN (fn)
4084 && (flag_keep_inline_functions
4085 || (flag_keep_inline_dllexport
4086 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4088 mark_needed (fn);
4089 DECL_EXTERNAL (fn) = 0;
4093 /* If this is a constructor or destructor body, we have to clone
4094 it. */
4095 if (maybe_clone_body (fn))
4097 /* We don't want to process FN again, so pretend we've written
4098 it out, even though we haven't. */
4099 TREE_ASM_WRITTEN (fn) = 1;
4100 /* If this is an instantiation of a constexpr function, keep
4101 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4102 if (!is_instantiation_of_constexpr (fn))
4103 DECL_SAVED_TREE (fn) = NULL_TREE;
4104 return false;
4107 /* There's no reason to do any of the work here if we're only doing
4108 semantic analysis; this code just generates RTL. */
4109 if (flag_syntax_only)
4110 return false;
4112 return true;
4115 void
4116 expand_or_defer_fn (tree fn)
4118 if (expand_or_defer_fn_1 (fn))
4120 function_depth++;
4122 /* Expand or defer, at the whim of the compilation unit manager. */
4123 cgraph_node::finalize_function (fn, function_depth > 1);
4124 emit_associated_thunks (fn);
4126 function_depth--;
4130 struct nrv_data
4132 nrv_data () : visited (37) {}
4134 tree var;
4135 tree result;
4136 hash_table<nofree_ptr_hash <tree_node> > visited;
4139 /* Helper function for walk_tree, used by finalize_nrv below. */
4141 static tree
4142 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4144 struct nrv_data *dp = (struct nrv_data *)data;
4145 tree_node **slot;
4147 /* No need to walk into types. There wouldn't be any need to walk into
4148 non-statements, except that we have to consider STMT_EXPRs. */
4149 if (TYPE_P (*tp))
4150 *walk_subtrees = 0;
4151 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4152 but differs from using NULL_TREE in that it indicates that we care
4153 about the value of the RESULT_DECL. */
4154 else if (TREE_CODE (*tp) == RETURN_EXPR)
4155 TREE_OPERAND (*tp, 0) = dp->result;
4156 /* Change all cleanups for the NRV to only run when an exception is
4157 thrown. */
4158 else if (TREE_CODE (*tp) == CLEANUP_STMT
4159 && CLEANUP_DECL (*tp) == dp->var)
4160 CLEANUP_EH_ONLY (*tp) = 1;
4161 /* Replace the DECL_EXPR for the NRV with an initialization of the
4162 RESULT_DECL, if needed. */
4163 else if (TREE_CODE (*tp) == DECL_EXPR
4164 && DECL_EXPR_DECL (*tp) == dp->var)
4166 tree init;
4167 if (DECL_INITIAL (dp->var)
4168 && DECL_INITIAL (dp->var) != error_mark_node)
4169 init = build2 (INIT_EXPR, void_type_node, dp->result,
4170 DECL_INITIAL (dp->var));
4171 else
4172 init = build_empty_stmt (EXPR_LOCATION (*tp));
4173 DECL_INITIAL (dp->var) = NULL_TREE;
4174 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4175 *tp = init;
4177 /* And replace all uses of the NRV with the RESULT_DECL. */
4178 else if (*tp == dp->var)
4179 *tp = dp->result;
4181 /* Avoid walking into the same tree more than once. Unfortunately, we
4182 can't just use walk_tree_without duplicates because it would only call
4183 us for the first occurrence of dp->var in the function body. */
4184 slot = dp->visited.find_slot (*tp, INSERT);
4185 if (*slot)
4186 *walk_subtrees = 0;
4187 else
4188 *slot = *tp;
4190 /* Keep iterating. */
4191 return NULL_TREE;
4194 /* Called from finish_function to implement the named return value
4195 optimization by overriding all the RETURN_EXPRs and pertinent
4196 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4197 RESULT_DECL for the function. */
4199 void
4200 finalize_nrv (tree *tp, tree var, tree result)
4202 struct nrv_data data;
4204 /* Copy name from VAR to RESULT. */
4205 DECL_NAME (result) = DECL_NAME (var);
4206 /* Don't forget that we take its address. */
4207 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4208 /* Finally set DECL_VALUE_EXPR to avoid assigning
4209 a stack slot at -O0 for the original var and debug info
4210 uses RESULT location for VAR. */
4211 SET_DECL_VALUE_EXPR (var, result);
4212 DECL_HAS_VALUE_EXPR_P (var) = 1;
4214 data.var = var;
4215 data.result = result;
4216 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4219 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4221 bool
4222 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4223 bool need_copy_ctor, bool need_copy_assignment,
4224 bool need_dtor)
4226 int save_errorcount = errorcount;
4227 tree info, t;
4229 /* Always allocate 3 elements for simplicity. These are the
4230 function decls for the ctor, dtor, and assignment op.
4231 This layout is known to the three lang hooks,
4232 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4233 and cxx_omp_clause_assign_op. */
4234 info = make_tree_vec (3);
4235 CP_OMP_CLAUSE_INFO (c) = info;
4237 if (need_default_ctor || need_copy_ctor)
4239 if (need_default_ctor)
4240 t = get_default_ctor (type);
4241 else
4242 t = get_copy_ctor (type, tf_warning_or_error);
4244 if (t && !trivial_fn_p (t))
4245 TREE_VEC_ELT (info, 0) = t;
4248 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4249 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4251 if (need_copy_assignment)
4253 t = get_copy_assign (type);
4255 if (t && !trivial_fn_p (t))
4256 TREE_VEC_ELT (info, 2) = t;
4259 return errorcount != save_errorcount;
4262 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4263 FIELD_DECL, otherwise return DECL itself. */
4265 static tree
4266 omp_clause_decl_field (tree decl)
4268 if (VAR_P (decl)
4269 && DECL_HAS_VALUE_EXPR_P (decl)
4270 && DECL_ARTIFICIAL (decl)
4271 && DECL_LANG_SPECIFIC (decl)
4272 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4274 tree f = DECL_VALUE_EXPR (decl);
4275 if (TREE_CODE (f) == INDIRECT_REF)
4276 f = TREE_OPERAND (f, 0);
4277 if (TREE_CODE (f) == COMPONENT_REF)
4279 f = TREE_OPERAND (f, 1);
4280 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4281 return f;
4284 return NULL_TREE;
4287 /* Adjust DECL if needed for printing using %qE. */
4289 static tree
4290 omp_clause_printable_decl (tree decl)
4292 tree t = omp_clause_decl_field (decl);
4293 if (t)
4294 return t;
4295 return decl;
4298 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4299 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4300 privatization. */
4302 static void
4303 omp_note_field_privatization (tree f, tree t)
4305 if (!omp_private_member_map)
4306 omp_private_member_map = new hash_map<tree, tree>;
4307 tree &v = omp_private_member_map->get_or_insert (f);
4308 if (v == NULL_TREE)
4310 v = t;
4311 omp_private_member_vec.safe_push (f);
4312 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4313 omp_private_member_vec.safe_push (integer_zero_node);
4317 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4318 dummy VAR_DECL. */
4320 tree
4321 omp_privatize_field (tree t)
4323 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4324 if (m == error_mark_node)
4325 return error_mark_node;
4326 if (!omp_private_member_map)
4327 omp_private_member_map = new hash_map<tree, tree>;
4328 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4330 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4331 m = TREE_OPERAND (m, 0);
4333 tree &v = omp_private_member_map->get_or_insert (t);
4334 if (v == NULL_TREE)
4336 v = create_temporary_var (TREE_TYPE (m));
4337 if (!DECL_LANG_SPECIFIC (v))
4338 retrofit_lang_decl (v);
4339 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4340 SET_DECL_VALUE_EXPR (v, m);
4341 DECL_HAS_VALUE_EXPR_P (v) = 1;
4342 omp_private_member_vec.safe_push (t);
4344 return v;
4347 /* Helper function for handle_omp_array_sections. Called recursively
4348 to handle multiple array-section-subscripts. C is the clause,
4349 T current expression (initially OMP_CLAUSE_DECL), which is either
4350 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4351 expression if specified, TREE_VALUE length expression if specified,
4352 TREE_CHAIN is what it has been specified after, or some decl.
4353 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4354 set to true if any of the array-section-subscript could have length
4355 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4356 first array-section-subscript which is known not to have length
4357 of one. Given say:
4358 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4359 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4360 all are or may have length of 1, array-section-subscript [:2] is the
4361 first one known not to have length 1. For array-section-subscript
4362 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4363 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4364 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4365 case though, as some lengths could be zero. */
4367 static tree
4368 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4369 bool &maybe_zero_len, unsigned int &first_non_one,
4370 bool is_omp)
4372 tree ret, low_bound, length, type;
4373 if (TREE_CODE (t) != TREE_LIST)
4375 if (error_operand_p (t))
4376 return error_mark_node;
4377 if (type_dependent_expression_p (t))
4378 return NULL_TREE;
4379 if (REFERENCE_REF_P (t)
4380 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4381 t = TREE_OPERAND (t, 0);
4382 ret = t;
4383 if (TREE_CODE (t) == COMPONENT_REF
4384 && is_omp
4385 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4386 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4387 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
4389 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4391 error_at (OMP_CLAUSE_LOCATION (c),
4392 "bit-field %qE in %qs clause",
4393 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4394 return error_mark_node;
4396 while (TREE_CODE (t) == COMPONENT_REF)
4398 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4400 error_at (OMP_CLAUSE_LOCATION (c),
4401 "%qE is a member of a union", t);
4402 return error_mark_node;
4404 t = TREE_OPERAND (t, 0);
4407 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4409 if (processing_template_decl)
4410 return NULL_TREE;
4411 if (DECL_P (t))
4412 error_at (OMP_CLAUSE_LOCATION (c),
4413 "%qD is not a variable in %qs clause", t,
4414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4415 else
4416 error_at (OMP_CLAUSE_LOCATION (c),
4417 "%qE is not a variable in %qs clause", t,
4418 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4419 return error_mark_node;
4421 else if (TREE_CODE (t) == PARM_DECL
4422 && DECL_ARTIFICIAL (t)
4423 && DECL_NAME (t) == this_identifier)
4425 error_at (OMP_CLAUSE_LOCATION (c),
4426 "%<this%> allowed in OpenMP only in %<declare simd%>"
4427 " clauses");
4428 return error_mark_node;
4430 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4431 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4433 error_at (OMP_CLAUSE_LOCATION (c),
4434 "%qD is threadprivate variable in %qs clause", t,
4435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4436 return error_mark_node;
4438 ret = convert_from_reference (ret);
4439 return ret;
4442 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4443 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4444 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t));
4445 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4446 maybe_zero_len, first_non_one, is_omp);
4447 if (ret == error_mark_node || ret == NULL_TREE)
4448 return ret;
4450 type = TREE_TYPE (ret);
4451 low_bound = TREE_PURPOSE (t);
4452 length = TREE_VALUE (t);
4453 if ((low_bound && type_dependent_expression_p (low_bound))
4454 || (length && type_dependent_expression_p (length)))
4455 return NULL_TREE;
4457 if (low_bound == error_mark_node || length == error_mark_node)
4458 return error_mark_node;
4460 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4462 error_at (OMP_CLAUSE_LOCATION (c),
4463 "low bound %qE of array section does not have integral type",
4464 low_bound);
4465 return error_mark_node;
4467 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4469 error_at (OMP_CLAUSE_LOCATION (c),
4470 "length %qE of array section does not have integral type",
4471 length);
4472 return error_mark_node;
4474 if (low_bound)
4475 low_bound = mark_rvalue_use (low_bound);
4476 if (length)
4477 length = mark_rvalue_use (length);
4478 if (low_bound
4479 && TREE_CODE (low_bound) == INTEGER_CST
4480 && TYPE_PRECISION (TREE_TYPE (low_bound))
4481 > TYPE_PRECISION (sizetype))
4482 low_bound = fold_convert (sizetype, low_bound);
4483 if (length
4484 && TREE_CODE (length) == INTEGER_CST
4485 && TYPE_PRECISION (TREE_TYPE (length))
4486 > TYPE_PRECISION (sizetype))
4487 length = fold_convert (sizetype, length);
4488 if (low_bound == NULL_TREE)
4489 low_bound = integer_zero_node;
4491 if (length != NULL_TREE)
4493 if (!integer_nonzerop (length))
4495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4496 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4498 if (integer_zerop (length))
4500 error_at (OMP_CLAUSE_LOCATION (c),
4501 "zero length array section in %qs clause",
4502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4503 return error_mark_node;
4506 else
4507 maybe_zero_len = true;
4509 if (first_non_one == types.length ()
4510 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4511 first_non_one++;
4513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4514 && !integer_zerop (low_bound))
4516 error_at (OMP_CLAUSE_LOCATION (c),
4517 "%<reduction%> array section has to be zero-based");
4518 return error_mark_node;
4520 if (TREE_CODE (type) == ARRAY_TYPE)
4522 if (length == NULL_TREE
4523 && (TYPE_DOMAIN (type) == NULL_TREE
4524 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4526 error_at (OMP_CLAUSE_LOCATION (c),
4527 "for unknown bound array type length expression must "
4528 "be specified");
4529 return error_mark_node;
4531 if (TREE_CODE (low_bound) == INTEGER_CST
4532 && tree_int_cst_sgn (low_bound) == -1)
4534 error_at (OMP_CLAUSE_LOCATION (c),
4535 "negative low bound in array section in %qs clause",
4536 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4537 return error_mark_node;
4539 if (length != NULL_TREE
4540 && TREE_CODE (length) == INTEGER_CST
4541 && tree_int_cst_sgn (length) == -1)
4543 error_at (OMP_CLAUSE_LOCATION (c),
4544 "negative length in array section in %qs clause",
4545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4546 return error_mark_node;
4548 if (TYPE_DOMAIN (type)
4549 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4550 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4551 == INTEGER_CST)
4553 tree size = size_binop (PLUS_EXPR,
4554 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4555 size_one_node);
4556 if (TREE_CODE (low_bound) == INTEGER_CST)
4558 if (tree_int_cst_lt (size, low_bound))
4560 error_at (OMP_CLAUSE_LOCATION (c),
4561 "low bound %qE above array section size "
4562 "in %qs clause", low_bound,
4563 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4564 return error_mark_node;
4566 if (tree_int_cst_equal (size, low_bound))
4568 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4569 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4571 error_at (OMP_CLAUSE_LOCATION (c),
4572 "zero length array section in %qs clause",
4573 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4574 return error_mark_node;
4576 maybe_zero_len = true;
4578 else if (length == NULL_TREE
4579 && first_non_one == types.length ()
4580 && tree_int_cst_equal
4581 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4582 low_bound))
4583 first_non_one++;
4585 else if (length == NULL_TREE)
4587 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4588 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4589 maybe_zero_len = true;
4590 if (first_non_one == types.length ())
4591 first_non_one++;
4593 if (length && TREE_CODE (length) == INTEGER_CST)
4595 if (tree_int_cst_lt (size, length))
4597 error_at (OMP_CLAUSE_LOCATION (c),
4598 "length %qE above array section size "
4599 "in %qs clause", length,
4600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4601 return error_mark_node;
4603 if (TREE_CODE (low_bound) == INTEGER_CST)
4605 tree lbpluslen
4606 = size_binop (PLUS_EXPR,
4607 fold_convert (sizetype, low_bound),
4608 fold_convert (sizetype, length));
4609 if (TREE_CODE (lbpluslen) == INTEGER_CST
4610 && tree_int_cst_lt (size, lbpluslen))
4612 error_at (OMP_CLAUSE_LOCATION (c),
4613 "high bound %qE above array section size "
4614 "in %qs clause", lbpluslen,
4615 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4616 return error_mark_node;
4621 else if (length == NULL_TREE)
4623 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4624 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4625 maybe_zero_len = true;
4626 if (first_non_one == types.length ())
4627 first_non_one++;
4630 /* For [lb:] we will need to evaluate lb more than once. */
4631 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4633 tree lb = cp_save_expr (low_bound);
4634 if (lb != low_bound)
4636 TREE_PURPOSE (t) = lb;
4637 low_bound = lb;
4641 else if (TREE_CODE (type) == POINTER_TYPE)
4643 if (length == NULL_TREE)
4645 error_at (OMP_CLAUSE_LOCATION (c),
4646 "for pointer type length expression must be specified");
4647 return error_mark_node;
4649 /* If there is a pointer type anywhere but in the very first
4650 array-section-subscript, the array section can't be contiguous. */
4651 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4652 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "array section is not contiguous in %qs clause",
4656 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4657 return error_mark_node;
4660 else
4662 error_at (OMP_CLAUSE_LOCATION (c),
4663 "%qE does not have pointer or array type", ret);
4664 return error_mark_node;
4666 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4667 types.safe_push (TREE_TYPE (ret));
4668 /* We will need to evaluate lb more than once. */
4669 tree lb = cp_save_expr (low_bound);
4670 if (lb != low_bound)
4672 TREE_PURPOSE (t) = lb;
4673 low_bound = lb;
4675 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4676 return ret;
4679 /* Handle array sections for clause C. */
4681 static bool
4682 handle_omp_array_sections (tree c, bool is_omp)
4684 bool maybe_zero_len = false;
4685 unsigned int first_non_one = 0;
4686 auto_vec<tree, 10> types;
4687 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4688 maybe_zero_len, first_non_one,
4689 is_omp);
4690 if (first == error_mark_node)
4691 return true;
4692 if (first == NULL_TREE)
4693 return false;
4694 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4696 tree t = OMP_CLAUSE_DECL (c);
4697 tree tem = NULL_TREE;
4698 if (processing_template_decl)
4699 return false;
4700 /* Need to evaluate side effects in the length expressions
4701 if any. */
4702 while (TREE_CODE (t) == TREE_LIST)
4704 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4706 if (tem == NULL_TREE)
4707 tem = TREE_VALUE (t);
4708 else
4709 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4710 TREE_VALUE (t), tem);
4712 t = TREE_CHAIN (t);
4714 if (tem)
4715 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4716 OMP_CLAUSE_DECL (c) = first;
4718 else
4720 unsigned int num = types.length (), i;
4721 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4722 tree condition = NULL_TREE;
4724 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4725 maybe_zero_len = true;
4726 if (processing_template_decl && maybe_zero_len)
4727 return false;
4729 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4730 t = TREE_CHAIN (t))
4732 tree low_bound = TREE_PURPOSE (t);
4733 tree length = TREE_VALUE (t);
4735 i--;
4736 if (low_bound
4737 && TREE_CODE (low_bound) == INTEGER_CST
4738 && TYPE_PRECISION (TREE_TYPE (low_bound))
4739 > TYPE_PRECISION (sizetype))
4740 low_bound = fold_convert (sizetype, low_bound);
4741 if (length
4742 && TREE_CODE (length) == INTEGER_CST
4743 && TYPE_PRECISION (TREE_TYPE (length))
4744 > TYPE_PRECISION (sizetype))
4745 length = fold_convert (sizetype, length);
4746 if (low_bound == NULL_TREE)
4747 low_bound = integer_zero_node;
4748 if (!maybe_zero_len && i > first_non_one)
4750 if (integer_nonzerop (low_bound))
4751 goto do_warn_noncontiguous;
4752 if (length != NULL_TREE
4753 && TREE_CODE (length) == INTEGER_CST
4754 && TYPE_DOMAIN (types[i])
4755 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4756 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4757 == INTEGER_CST)
4759 tree size;
4760 size = size_binop (PLUS_EXPR,
4761 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4762 size_one_node);
4763 if (!tree_int_cst_equal (length, size))
4765 do_warn_noncontiguous:
4766 error_at (OMP_CLAUSE_LOCATION (c),
4767 "array section is not contiguous in %qs "
4768 "clause",
4769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4770 return true;
4773 if (!processing_template_decl
4774 && length != NULL_TREE
4775 && TREE_SIDE_EFFECTS (length))
4777 if (side_effects == NULL_TREE)
4778 side_effects = length;
4779 else
4780 side_effects = build2 (COMPOUND_EXPR,
4781 TREE_TYPE (side_effects),
4782 length, side_effects);
4785 else if (processing_template_decl)
4786 continue;
4787 else
4789 tree l;
4791 if (i > first_non_one
4792 && ((length && integer_nonzerop (length))
4793 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4794 continue;
4795 if (length)
4796 l = fold_convert (sizetype, length);
4797 else
4799 l = size_binop (PLUS_EXPR,
4800 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4801 size_one_node);
4802 l = size_binop (MINUS_EXPR, l,
4803 fold_convert (sizetype, low_bound));
4805 if (i > first_non_one)
4807 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4808 size_zero_node);
4809 if (condition == NULL_TREE)
4810 condition = l;
4811 else
4812 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4813 l, condition);
4815 else if (size == NULL_TREE)
4817 size = size_in_bytes (TREE_TYPE (types[i]));
4818 tree eltype = TREE_TYPE (types[num - 1]);
4819 while (TREE_CODE (eltype) == ARRAY_TYPE)
4820 eltype = TREE_TYPE (eltype);
4821 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4822 size = size_binop (EXACT_DIV_EXPR, size,
4823 size_in_bytes (eltype));
4824 size = size_binop (MULT_EXPR, size, l);
4825 if (condition)
4826 size = fold_build3 (COND_EXPR, sizetype, condition,
4827 size, size_zero_node);
4829 else
4830 size = size_binop (MULT_EXPR, size, l);
4833 if (!processing_template_decl)
4835 if (side_effects)
4836 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4837 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4839 size = size_binop (MINUS_EXPR, size, size_one_node);
4840 tree index_type = build_index_type (size);
4841 tree eltype = TREE_TYPE (first);
4842 while (TREE_CODE (eltype) == ARRAY_TYPE)
4843 eltype = TREE_TYPE (eltype);
4844 tree type = build_array_type (eltype, index_type);
4845 tree ptype = build_pointer_type (eltype);
4846 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4847 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4848 t = convert_from_reference (t);
4849 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4850 t = build_fold_addr_expr (t);
4851 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4852 OMP_CLAUSE_DECL (c) = t;
4853 return false;
4855 OMP_CLAUSE_DECL (c) = first;
4856 OMP_CLAUSE_SIZE (c) = size;
4857 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4858 || (TREE_CODE (t) == COMPONENT_REF
4859 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4860 return false;
4861 if (is_omp)
4862 switch (OMP_CLAUSE_MAP_KIND (c))
4864 case GOMP_MAP_ALLOC:
4865 case GOMP_MAP_TO:
4866 case GOMP_MAP_FROM:
4867 case GOMP_MAP_TOFROM:
4868 case GOMP_MAP_ALWAYS_TO:
4869 case GOMP_MAP_ALWAYS_FROM:
4870 case GOMP_MAP_ALWAYS_TOFROM:
4871 case GOMP_MAP_RELEASE:
4872 case GOMP_MAP_DELETE:
4873 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
4874 break;
4875 default:
4876 break;
4878 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4879 OMP_CLAUSE_MAP);
4880 OMP_CLAUSE_SET_MAP_KIND (c2, is_omp ? GOMP_MAP_FIRSTPRIVATE_POINTER
4881 : GOMP_MAP_POINTER);
4882 if (!is_omp && !cxx_mark_addressable (t))
4883 return false;
4884 OMP_CLAUSE_DECL (c2) = t;
4885 t = build_fold_addr_expr (first);
4886 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4887 ptrdiff_type_node, t);
4888 tree ptr = OMP_CLAUSE_DECL (c2);
4889 ptr = convert_from_reference (ptr);
4890 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4891 ptr = build_fold_addr_expr (ptr);
4892 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4893 ptrdiff_type_node, t,
4894 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4895 ptrdiff_type_node, ptr));
4896 OMP_CLAUSE_SIZE (c2) = t;
4897 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4898 OMP_CLAUSE_CHAIN (c) = c2;
4899 ptr = OMP_CLAUSE_DECL (c2);
4900 if (!is_omp
4901 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4902 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4904 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4905 OMP_CLAUSE_MAP);
4906 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4907 OMP_CLAUSE_DECL (c3) = ptr;
4908 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4909 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4910 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4911 OMP_CLAUSE_CHAIN (c2) = c3;
4915 return false;
4918 /* Return identifier to look up for omp declare reduction. */
4920 tree
4921 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4923 const char *p = NULL;
4924 const char *m = NULL;
4925 switch (reduction_code)
4927 case PLUS_EXPR:
4928 case MULT_EXPR:
4929 case MINUS_EXPR:
4930 case BIT_AND_EXPR:
4931 case BIT_XOR_EXPR:
4932 case BIT_IOR_EXPR:
4933 case TRUTH_ANDIF_EXPR:
4934 case TRUTH_ORIF_EXPR:
4935 reduction_id = ansi_opname (reduction_code);
4936 break;
4937 case MIN_EXPR:
4938 p = "min";
4939 break;
4940 case MAX_EXPR:
4941 p = "max";
4942 break;
4943 default:
4944 break;
4947 if (p == NULL)
4949 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4950 return error_mark_node;
4951 p = IDENTIFIER_POINTER (reduction_id);
4954 if (type != NULL_TREE)
4955 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4957 const char prefix[] = "omp declare reduction ";
4958 size_t lenp = sizeof (prefix);
4959 if (strncmp (p, prefix, lenp - 1) == 0)
4960 lenp = 1;
4961 size_t len = strlen (p);
4962 size_t lenm = m ? strlen (m) + 1 : 0;
4963 char *name = XALLOCAVEC (char, lenp + len + lenm);
4964 if (lenp > 1)
4965 memcpy (name, prefix, lenp - 1);
4966 memcpy (name + lenp - 1, p, len + 1);
4967 if (m)
4969 name[lenp + len - 1] = '~';
4970 memcpy (name + lenp + len, m, lenm);
4972 return get_identifier (name);
4975 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4976 FUNCTION_DECL or NULL_TREE if not found. */
4978 static tree
4979 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4980 vec<tree> *ambiguousp)
4982 tree orig_id = id;
4983 tree baselink = NULL_TREE;
4984 if (identifier_p (id))
4986 cp_id_kind idk;
4987 bool nonint_cst_expression_p;
4988 const char *error_msg;
4989 id = omp_reduction_id (ERROR_MARK, id, type);
4990 tree decl = lookup_name (id);
4991 if (decl == NULL_TREE)
4992 decl = error_mark_node;
4993 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4994 &nonint_cst_expression_p, false, true, false,
4995 false, &error_msg, loc);
4996 if (idk == CP_ID_KIND_UNQUALIFIED
4997 && identifier_p (id))
4999 vec<tree, va_gc> *args = NULL;
5000 vec_safe_push (args, build_reference_type (type));
5001 id = perform_koenig_lookup (id, args, tf_none);
5004 else if (TREE_CODE (id) == SCOPE_REF)
5005 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5006 omp_reduction_id (ERROR_MARK,
5007 TREE_OPERAND (id, 1),
5008 type),
5009 false, false);
5010 tree fns = id;
5011 if (id && is_overloaded_fn (id))
5012 id = get_fns (id);
5013 for (; id; id = OVL_NEXT (id))
5015 tree fndecl = OVL_CURRENT (id);
5016 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5018 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5019 if (same_type_p (TREE_TYPE (argtype), type))
5020 break;
5023 if (id && BASELINK_P (fns))
5025 if (baselinkp)
5026 *baselinkp = fns;
5027 else
5028 baselink = fns;
5030 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5032 vec<tree> ambiguous = vNULL;
5033 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5034 unsigned int ix;
5035 if (ambiguousp == NULL)
5036 ambiguousp = &ambiguous;
5037 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5039 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5040 baselinkp ? baselinkp : &baselink,
5041 ambiguousp);
5042 if (id == NULL_TREE)
5043 continue;
5044 if (!ambiguousp->is_empty ())
5045 ambiguousp->safe_push (id);
5046 else if (ret != NULL_TREE)
5048 ambiguousp->safe_push (ret);
5049 ambiguousp->safe_push (id);
5050 ret = NULL_TREE;
5052 else
5053 ret = id;
5055 if (ambiguousp != &ambiguous)
5056 return ret;
5057 if (!ambiguous.is_empty ())
5059 const char *str = _("candidates are:");
5060 unsigned int idx;
5061 tree udr;
5062 error_at (loc, "user defined reduction lookup is ambiguous");
5063 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5065 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
5066 if (idx == 0)
5067 str = get_spaces (str);
5069 ambiguous.release ();
5070 ret = error_mark_node;
5071 baselink = NULL_TREE;
5073 id = ret;
5075 if (id && baselink)
5076 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5077 id, id, tf_warning_or_error);
5078 return id;
5081 /* Helper function for cp_parser_omp_declare_reduction_exprs
5082 and tsubst_omp_udr.
5083 Remove CLEANUP_STMT for data (omp_priv variable).
5084 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5085 DECL_EXPR. */
5087 tree
5088 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5090 if (TYPE_P (*tp))
5091 *walk_subtrees = 0;
5092 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5093 *tp = CLEANUP_BODY (*tp);
5094 else if (TREE_CODE (*tp) == DECL_EXPR)
5096 tree decl = DECL_EXPR_DECL (*tp);
5097 if (!processing_template_decl
5098 && decl == (tree) data
5099 && DECL_INITIAL (decl)
5100 && DECL_INITIAL (decl) != error_mark_node)
5102 tree list = NULL_TREE;
5103 append_to_statement_list_force (*tp, &list);
5104 tree init_expr = build2 (INIT_EXPR, void_type_node,
5105 decl, DECL_INITIAL (decl));
5106 DECL_INITIAL (decl) = NULL_TREE;
5107 append_to_statement_list_force (init_expr, &list);
5108 *tp = list;
5111 return NULL_TREE;
5114 /* Data passed from cp_check_omp_declare_reduction to
5115 cp_check_omp_declare_reduction_r. */
5117 struct cp_check_omp_declare_reduction_data
5119 location_t loc;
5120 tree stmts[7];
5121 bool combiner_p;
5124 /* Helper function for cp_check_omp_declare_reduction, called via
5125 cp_walk_tree. */
5127 static tree
5128 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5130 struct cp_check_omp_declare_reduction_data *udr_data
5131 = (struct cp_check_omp_declare_reduction_data *) data;
5132 if (SSA_VAR_P (*tp)
5133 && !DECL_ARTIFICIAL (*tp)
5134 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5135 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5137 location_t loc = udr_data->loc;
5138 if (udr_data->combiner_p)
5139 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5140 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5141 *tp);
5142 else
5143 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5144 "to variable %qD which is not %<omp_priv%> nor "
5145 "%<omp_orig%>",
5146 *tp);
5147 return *tp;
5149 return NULL_TREE;
5152 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5154 void
5155 cp_check_omp_declare_reduction (tree udr)
5157 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5158 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5159 type = TREE_TYPE (type);
5160 int i;
5161 location_t loc = DECL_SOURCE_LOCATION (udr);
5163 if (type == error_mark_node)
5164 return;
5165 if (ARITHMETIC_TYPE_P (type))
5167 static enum tree_code predef_codes[]
5168 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5169 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5170 for (i = 0; i < 8; i++)
5172 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5173 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5174 const char *n2 = IDENTIFIER_POINTER (id);
5175 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5176 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5177 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5178 break;
5181 if (i == 8
5182 && TREE_CODE (type) != COMPLEX_EXPR)
5184 const char prefix_minmax[] = "omp declare reduction m";
5185 size_t prefix_size = sizeof (prefix_minmax) - 1;
5186 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5187 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5188 prefix_minmax, prefix_size) == 0
5189 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5190 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5191 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5192 i = 0;
5194 if (i < 8)
5196 error_at (loc, "predeclared arithmetic type %qT in "
5197 "%<#pragma omp declare reduction%>", type);
5198 return;
5201 else if (TREE_CODE (type) == FUNCTION_TYPE
5202 || TREE_CODE (type) == METHOD_TYPE
5203 || TREE_CODE (type) == ARRAY_TYPE)
5205 error_at (loc, "function or array type %qT in "
5206 "%<#pragma omp declare reduction%>", type);
5207 return;
5209 else if (TREE_CODE (type) == REFERENCE_TYPE)
5211 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5212 type);
5213 return;
5215 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5217 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5218 "%<#pragma omp declare reduction%>", type);
5219 return;
5222 tree body = DECL_SAVED_TREE (udr);
5223 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5224 return;
5226 tree_stmt_iterator tsi;
5227 struct cp_check_omp_declare_reduction_data data;
5228 memset (data.stmts, 0, sizeof data.stmts);
5229 for (i = 0, tsi = tsi_start (body);
5230 i < 7 && !tsi_end_p (tsi);
5231 i++, tsi_next (&tsi))
5232 data.stmts[i] = tsi_stmt (tsi);
5233 data.loc = loc;
5234 gcc_assert (tsi_end_p (tsi));
5235 if (i >= 3)
5237 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5238 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5239 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5240 return;
5241 data.combiner_p = true;
5242 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5243 &data, NULL))
5244 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5246 if (i >= 6)
5248 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5249 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5250 data.combiner_p = false;
5251 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5252 &data, NULL)
5253 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5254 cp_check_omp_declare_reduction_r, &data, NULL))
5255 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5256 if (i == 7)
5257 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5261 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5262 an inline call. But, remap
5263 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5264 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5266 static tree
5267 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5268 tree decl, tree placeholder)
5270 copy_body_data id;
5271 hash_map<tree, tree> decl_map;
5273 decl_map.put (omp_decl1, placeholder);
5274 decl_map.put (omp_decl2, decl);
5275 memset (&id, 0, sizeof (id));
5276 id.src_fn = DECL_CONTEXT (omp_decl1);
5277 id.dst_fn = current_function_decl;
5278 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5279 id.decl_map = &decl_map;
5281 id.copy_decl = copy_decl_no_change;
5282 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5283 id.transform_new_cfg = true;
5284 id.transform_return_to_modify = false;
5285 id.transform_lang_insert_block = NULL;
5286 id.eh_lp_nr = 0;
5287 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5288 return stmt;
5291 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5292 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5294 static tree
5295 find_omp_placeholder_r (tree *tp, int *, void *data)
5297 if (*tp == (tree) data)
5298 return *tp;
5299 return NULL_TREE;
5302 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5303 Return true if there is some error and the clause should be removed. */
5305 static bool
5306 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5308 tree t = OMP_CLAUSE_DECL (c);
5309 bool predefined = false;
5310 if (TREE_CODE (t) == TREE_LIST)
5312 gcc_assert (processing_template_decl);
5313 return false;
5315 tree type = TREE_TYPE (t);
5316 if (TREE_CODE (t) == MEM_REF)
5317 type = TREE_TYPE (type);
5318 if (TREE_CODE (type) == REFERENCE_TYPE)
5319 type = TREE_TYPE (type);
5320 if (TREE_CODE (type) == ARRAY_TYPE)
5322 tree oatype = type;
5323 gcc_assert (TREE_CODE (t) != MEM_REF);
5324 while (TREE_CODE (type) == ARRAY_TYPE)
5325 type = TREE_TYPE (type);
5326 if (!processing_template_decl)
5328 t = require_complete_type (t);
5329 if (t == error_mark_node)
5330 return true;
5331 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5332 TYPE_SIZE_UNIT (type));
5333 if (integer_zerop (size))
5335 error ("%qE in %<reduction%> clause is a zero size array",
5336 omp_clause_printable_decl (t));
5337 return true;
5339 size = size_binop (MINUS_EXPR, size, size_one_node);
5340 tree index_type = build_index_type (size);
5341 tree atype = build_array_type (type, index_type);
5342 tree ptype = build_pointer_type (type);
5343 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5344 t = build_fold_addr_expr (t);
5345 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5346 OMP_CLAUSE_DECL (c) = t;
5349 if (type == error_mark_node)
5350 return true;
5351 else if (ARITHMETIC_TYPE_P (type))
5352 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5354 case PLUS_EXPR:
5355 case MULT_EXPR:
5356 case MINUS_EXPR:
5357 predefined = true;
5358 break;
5359 case MIN_EXPR:
5360 case MAX_EXPR:
5361 if (TREE_CODE (type) == COMPLEX_TYPE)
5362 break;
5363 predefined = true;
5364 break;
5365 case BIT_AND_EXPR:
5366 case BIT_IOR_EXPR:
5367 case BIT_XOR_EXPR:
5368 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5369 break;
5370 predefined = true;
5371 break;
5372 case TRUTH_ANDIF_EXPR:
5373 case TRUTH_ORIF_EXPR:
5374 if (FLOAT_TYPE_P (type))
5375 break;
5376 predefined = true;
5377 break;
5378 default:
5379 break;
5381 else if (TYPE_READONLY (type))
5383 error ("%qE has const type for %<reduction%>",
5384 omp_clause_printable_decl (t));
5385 return true;
5387 else if (!processing_template_decl)
5389 t = require_complete_type (t);
5390 if (t == error_mark_node)
5391 return true;
5392 OMP_CLAUSE_DECL (c) = t;
5395 if (predefined)
5397 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5398 return false;
5400 else if (processing_template_decl)
5401 return false;
5403 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5405 type = TYPE_MAIN_VARIANT (type);
5406 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5407 if (id == NULL_TREE)
5408 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5409 NULL_TREE, NULL_TREE);
5410 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5411 if (id)
5413 if (id == error_mark_node)
5414 return true;
5415 id = OVL_CURRENT (id);
5416 mark_used (id);
5417 tree body = DECL_SAVED_TREE (id);
5418 if (!body)
5419 return true;
5420 if (TREE_CODE (body) == STATEMENT_LIST)
5422 tree_stmt_iterator tsi;
5423 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5424 int i;
5425 tree stmts[7];
5426 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5427 atype = TREE_TYPE (atype);
5428 bool need_static_cast = !same_type_p (type, atype);
5429 memset (stmts, 0, sizeof stmts);
5430 for (i = 0, tsi = tsi_start (body);
5431 i < 7 && !tsi_end_p (tsi);
5432 i++, tsi_next (&tsi))
5433 stmts[i] = tsi_stmt (tsi);
5434 gcc_assert (tsi_end_p (tsi));
5436 if (i >= 3)
5438 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5439 && TREE_CODE (stmts[1]) == DECL_EXPR);
5440 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5441 DECL_ARTIFICIAL (placeholder) = 1;
5442 DECL_IGNORED_P (placeholder) = 1;
5443 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5444 if (TREE_CODE (t) == MEM_REF)
5446 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5447 type);
5448 DECL_ARTIFICIAL (decl_placeholder) = 1;
5449 DECL_IGNORED_P (decl_placeholder) = 1;
5450 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5452 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5453 cxx_mark_addressable (placeholder);
5454 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5455 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5456 != REFERENCE_TYPE)
5457 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5458 : OMP_CLAUSE_DECL (c));
5459 tree omp_out = placeholder;
5460 tree omp_in = decl_placeholder ? decl_placeholder
5461 : convert_from_reference (OMP_CLAUSE_DECL (c));
5462 if (need_static_cast)
5464 tree rtype = build_reference_type (atype);
5465 omp_out = build_static_cast (rtype, omp_out,
5466 tf_warning_or_error);
5467 omp_in = build_static_cast (rtype, omp_in,
5468 tf_warning_or_error);
5469 if (omp_out == error_mark_node || omp_in == error_mark_node)
5470 return true;
5471 omp_out = convert_from_reference (omp_out);
5472 omp_in = convert_from_reference (omp_in);
5474 OMP_CLAUSE_REDUCTION_MERGE (c)
5475 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5476 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5478 if (i >= 6)
5480 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5481 && TREE_CODE (stmts[4]) == DECL_EXPR);
5482 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5483 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5484 : OMP_CLAUSE_DECL (c));
5485 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5486 cxx_mark_addressable (placeholder);
5487 tree omp_priv = decl_placeholder ? decl_placeholder
5488 : convert_from_reference (OMP_CLAUSE_DECL (c));
5489 tree omp_orig = placeholder;
5490 if (need_static_cast)
5492 if (i == 7)
5494 error_at (OMP_CLAUSE_LOCATION (c),
5495 "user defined reduction with constructor "
5496 "initializer for base class %qT", atype);
5497 return true;
5499 tree rtype = build_reference_type (atype);
5500 omp_priv = build_static_cast (rtype, omp_priv,
5501 tf_warning_or_error);
5502 omp_orig = build_static_cast (rtype, omp_orig,
5503 tf_warning_or_error);
5504 if (omp_priv == error_mark_node
5505 || omp_orig == error_mark_node)
5506 return true;
5507 omp_priv = convert_from_reference (omp_priv);
5508 omp_orig = convert_from_reference (omp_orig);
5510 if (i == 6)
5511 *need_default_ctor = true;
5512 OMP_CLAUSE_REDUCTION_INIT (c)
5513 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5514 DECL_EXPR_DECL (stmts[3]),
5515 omp_priv, omp_orig);
5516 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5517 find_omp_placeholder_r, placeholder, NULL))
5518 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5520 else if (i >= 3)
5522 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5523 *need_default_ctor = true;
5524 else
5526 tree init;
5527 tree v = decl_placeholder ? decl_placeholder
5528 : convert_from_reference (t);
5529 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5530 init = build_constructor (TREE_TYPE (v), NULL);
5531 else
5532 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5533 OMP_CLAUSE_REDUCTION_INIT (c)
5534 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5539 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5540 *need_dtor = true;
5541 else
5543 error ("user defined reduction not found for %qE",
5544 omp_clause_printable_decl (t));
5545 return true;
5547 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5548 gcc_assert (TYPE_SIZE_UNIT (type)
5549 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5550 return false;
5553 /* Called from finish_struct_1. linear(this) or linear(this:step)
5554 clauses might not be finalized yet because the class has been incomplete
5555 when parsing #pragma omp declare simd methods. Fix those up now. */
5557 void
5558 finish_omp_declare_simd_methods (tree t)
5560 if (processing_template_decl)
5561 return;
5563 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5565 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5566 continue;
5567 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5568 if (!ods || !TREE_VALUE (ods))
5569 continue;
5570 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5571 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5572 && integer_zerop (OMP_CLAUSE_DECL (c))
5573 && OMP_CLAUSE_LINEAR_STEP (c)
5574 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5575 == POINTER_TYPE)
5577 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5578 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5579 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5580 sizetype, s, TYPE_SIZE_UNIT (t));
5581 OMP_CLAUSE_LINEAR_STEP (c) = s;
5586 /* Adjust sink depend clause to take into account pointer offsets.
5588 Return TRUE if there was a problem processing the offset, and the
5589 whole clause should be removed. */
5591 static bool
5592 cp_finish_omp_clause_depend_sink (tree sink_clause)
5594 tree t = OMP_CLAUSE_DECL (sink_clause);
5595 gcc_assert (TREE_CODE (t) == TREE_LIST);
5597 /* Make sure we don't adjust things twice for templates. */
5598 if (processing_template_decl)
5599 return false;
5601 for (; t; t = TREE_CHAIN (t))
5603 tree decl = TREE_VALUE (t);
5604 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5606 tree offset = TREE_PURPOSE (t);
5607 bool neg = wi::neg_p ((wide_int) offset);
5608 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5609 decl = mark_rvalue_use (decl);
5610 decl = convert_from_reference (decl);
5611 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5612 neg ? MINUS_EXPR : PLUS_EXPR,
5613 decl, offset);
5614 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5615 MINUS_EXPR, sizetype, t2,
5616 decl);
5617 if (t2 == error_mark_node)
5618 return true;
5619 TREE_PURPOSE (t) = t2;
5622 return false;
5625 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5626 Remove any elements from the list that are invalid. */
5628 tree
5629 finish_omp_clauses (tree clauses, bool allow_fields, bool declare_simd)
5631 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5632 bitmap_head aligned_head, map_head, map_field_head, generic_field_head;
5633 tree c, t, *pc;
5634 tree safelen = NULL_TREE;
5635 bool branch_seen = false;
5636 bool copyprivate_seen = false;
5638 bitmap_obstack_initialize (NULL);
5639 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5640 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5641 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5642 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5643 bitmap_initialize (&map_head, &bitmap_default_obstack);
5644 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5645 bitmap_initialize (&generic_field_head, &bitmap_default_obstack);
5647 for (pc = &clauses, c = clauses; c ; c = *pc)
5649 bool remove = false;
5650 bool field_ok = false;
5652 switch (OMP_CLAUSE_CODE (c))
5654 case OMP_CLAUSE_SHARED:
5655 goto check_dup_generic;
5656 case OMP_CLAUSE_PRIVATE:
5657 field_ok = allow_fields;
5658 goto check_dup_generic;
5659 case OMP_CLAUSE_REDUCTION:
5660 field_ok = allow_fields;
5661 t = OMP_CLAUSE_DECL (c);
5662 if (TREE_CODE (t) == TREE_LIST)
5664 if (handle_omp_array_sections (c, allow_fields))
5666 remove = true;
5667 break;
5669 if (TREE_CODE (t) == TREE_LIST)
5671 while (TREE_CODE (t) == TREE_LIST)
5672 t = TREE_CHAIN (t);
5674 else
5676 gcc_assert (TREE_CODE (t) == MEM_REF);
5677 t = TREE_OPERAND (t, 0);
5678 if (TREE_CODE (t) == ADDR_EXPR
5679 || TREE_CODE (t) == INDIRECT_REF)
5680 t = TREE_OPERAND (t, 0);
5682 tree n = omp_clause_decl_field (t);
5683 if (n)
5684 t = n;
5685 goto check_dup_generic_t;
5687 goto check_dup_generic;
5688 case OMP_CLAUSE_COPYPRIVATE:
5689 copyprivate_seen = true;
5690 field_ok = allow_fields;
5691 goto check_dup_generic;
5692 case OMP_CLAUSE_COPYIN:
5693 goto check_dup_generic;
5694 case OMP_CLAUSE_LINEAR:
5695 field_ok = allow_fields;
5696 t = OMP_CLAUSE_DECL (c);
5697 if (!type_dependent_expression_p (t))
5699 tree type = TREE_TYPE (t);
5700 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5701 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5702 && TREE_CODE (type) != REFERENCE_TYPE)
5704 error ("linear clause with %qs modifier applied to "
5705 "non-reference variable with %qT type",
5706 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5707 ? "ref" : "uval", TREE_TYPE (t));
5708 remove = true;
5709 break;
5711 if (TREE_CODE (type) == REFERENCE_TYPE)
5712 type = TREE_TYPE (type);
5713 if (!INTEGRAL_TYPE_P (type)
5714 && TREE_CODE (type) != POINTER_TYPE)
5716 error ("linear clause applied to non-integral non-pointer "
5717 "variable with %qT type", TREE_TYPE (t));
5718 remove = true;
5719 break;
5722 t = OMP_CLAUSE_LINEAR_STEP (c);
5723 if (t == NULL_TREE)
5724 t = integer_one_node;
5725 if (t == error_mark_node)
5727 remove = true;
5728 break;
5730 else if (!type_dependent_expression_p (t)
5731 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5733 error ("linear step expression must be integral");
5734 remove = true;
5735 break;
5737 else
5739 t = mark_rvalue_use (t);
5740 if (!processing_template_decl)
5742 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5743 t = maybe_constant_value (t);
5744 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5745 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5746 if (TREE_CODE (type) == REFERENCE_TYPE)
5747 type = TREE_TYPE (type);
5748 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5750 type = build_pointer_type (type);
5751 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5752 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5753 d, t);
5754 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5755 MINUS_EXPR, sizetype, t, d);
5756 if (t == error_mark_node)
5758 remove = true;
5759 break;
5762 else if (TREE_CODE (type) == POINTER_TYPE
5763 /* Can't multiply the step yet if *this
5764 is still incomplete type. */
5765 && (!declare_simd
5766 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5767 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5768 || DECL_NAME (OMP_CLAUSE_DECL (c))
5769 != this_identifier
5770 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5772 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5773 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5774 d, t);
5775 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5776 MINUS_EXPR, sizetype, t, d);
5777 if (t == error_mark_node)
5779 remove = true;
5780 break;
5783 else
5784 t = fold_convert (type, t);
5786 OMP_CLAUSE_LINEAR_STEP (c) = t;
5788 goto check_dup_generic;
5789 check_dup_generic:
5790 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5791 if (t)
5793 if (!remove)
5794 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5796 else
5797 t = OMP_CLAUSE_DECL (c);
5798 check_dup_generic_t:
5799 if (t == current_class_ptr
5800 && (!declare_simd
5801 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
5802 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
5804 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5805 " clauses");
5806 remove = true;
5807 break;
5809 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5810 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
5812 if (processing_template_decl)
5813 break;
5814 if (DECL_P (t))
5815 error ("%qD is not a variable in clause %qs", t,
5816 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5817 else
5818 error ("%qE is not a variable in clause %qs", t,
5819 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5820 remove = true;
5822 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5823 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5824 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5826 error ("%qD appears more than once in data clauses", t);
5827 remove = true;
5829 else
5830 bitmap_set_bit (&generic_head, DECL_UID (t));
5831 if (!field_ok)
5832 break;
5833 handle_field_decl:
5834 if (!remove
5835 && TREE_CODE (t) == FIELD_DECL
5836 && t == OMP_CLAUSE_DECL (c))
5838 OMP_CLAUSE_DECL (c) = omp_privatize_field (t);
5839 if (OMP_CLAUSE_DECL (c) == error_mark_node)
5840 remove = true;
5842 break;
5844 case OMP_CLAUSE_FIRSTPRIVATE:
5845 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5846 if (t)
5847 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5848 else
5849 t = OMP_CLAUSE_DECL (c);
5850 if (t == current_class_ptr)
5852 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5853 " clauses");
5854 remove = true;
5855 break;
5857 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5858 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
5860 if (processing_template_decl)
5861 break;
5862 if (DECL_P (t))
5863 error ("%qD is not a variable in clause %<firstprivate%>", t);
5864 else
5865 error ("%qE is not a variable in clause %<firstprivate%>", t);
5866 remove = true;
5868 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5869 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5871 error ("%qD appears more than once in data clauses", t);
5872 remove = true;
5874 else
5875 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5876 goto handle_field_decl;
5878 case OMP_CLAUSE_LASTPRIVATE:
5879 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5880 if (t)
5881 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5882 else
5883 t = OMP_CLAUSE_DECL (c);
5884 if (t == current_class_ptr)
5886 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5887 " clauses");
5888 remove = true;
5889 break;
5891 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5892 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
5894 if (processing_template_decl)
5895 break;
5896 if (DECL_P (t))
5897 error ("%qD is not a variable in clause %<lastprivate%>", t);
5898 else
5899 error ("%qE is not a variable in clause %<lastprivate%>", t);
5900 remove = true;
5902 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5903 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5905 error ("%qD appears more than once in data clauses", t);
5906 remove = true;
5908 else
5909 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5910 goto handle_field_decl;
5912 case OMP_CLAUSE_IF:
5913 t = OMP_CLAUSE_IF_EXPR (c);
5914 t = maybe_convert_cond (t);
5915 if (t == error_mark_node)
5916 remove = true;
5917 else if (!processing_template_decl)
5918 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5919 OMP_CLAUSE_IF_EXPR (c) = t;
5920 break;
5922 case OMP_CLAUSE_FINAL:
5923 t = OMP_CLAUSE_FINAL_EXPR (c);
5924 t = maybe_convert_cond (t);
5925 if (t == error_mark_node)
5926 remove = true;
5927 else if (!processing_template_decl)
5928 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5929 OMP_CLAUSE_FINAL_EXPR (c) = t;
5930 break;
5932 case OMP_CLAUSE_NUM_THREADS:
5933 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5934 if (t == error_mark_node)
5935 remove = true;
5936 else if (!type_dependent_expression_p (t)
5937 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5939 error ("num_threads expression must be integral");
5940 remove = true;
5942 else
5944 t = mark_rvalue_use (t);
5945 if (!processing_template_decl)
5947 t = maybe_constant_value (t);
5948 if (TREE_CODE (t) == INTEGER_CST
5949 && tree_int_cst_sgn (t) != 1)
5951 warning_at (OMP_CLAUSE_LOCATION (c), 0,
5952 "%<num_threads%> value must be positive");
5953 t = integer_one_node;
5955 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5957 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5959 break;
5961 case OMP_CLAUSE_SCHEDULE:
5962 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5963 if (t == NULL)
5965 else if (t == error_mark_node)
5966 remove = true;
5967 else if (!type_dependent_expression_p (t)
5968 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5969 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5970 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5972 error ("schedule chunk size expression must be integral");
5973 remove = true;
5975 else
5977 t = mark_rvalue_use (t);
5978 if (!processing_template_decl)
5980 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5981 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5983 t = convert_to_integer (long_integer_type_node, t);
5984 if (t == error_mark_node)
5986 remove = true;
5987 break;
5990 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5992 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5994 break;
5996 case OMP_CLAUSE_SIMDLEN:
5997 case OMP_CLAUSE_SAFELEN:
5998 t = OMP_CLAUSE_OPERAND (c, 0);
5999 if (t == error_mark_node)
6000 remove = true;
6001 else if (!type_dependent_expression_p (t)
6002 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6004 error ("%qs length expression must be integral",
6005 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6006 remove = true;
6008 else
6010 t = mark_rvalue_use (t);
6011 t = maybe_constant_value (t);
6012 if (!processing_template_decl)
6014 if (TREE_CODE (t) != INTEGER_CST
6015 || tree_int_cst_sgn (t) != 1)
6017 error ("%qs length expression must be positive constant"
6018 " integer expression",
6019 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6020 remove = true;
6023 OMP_CLAUSE_OPERAND (c, 0) = t;
6024 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6025 safelen = c;
6027 break;
6029 case OMP_CLAUSE_NUM_TEAMS:
6030 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
6031 if (t == error_mark_node)
6032 remove = true;
6033 else if (!type_dependent_expression_p (t)
6034 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6036 error ("%<num_teams%> expression must be integral");
6037 remove = true;
6039 else
6041 t = mark_rvalue_use (t);
6042 if (!processing_template_decl)
6044 t = maybe_constant_value (t);
6045 if (TREE_CODE (t) == INTEGER_CST
6046 && tree_int_cst_sgn (t) != 1)
6048 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6049 "%<num_teams%> value must be positive");
6050 t = integer_one_node;
6052 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6054 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
6056 break;
6058 case OMP_CLAUSE_ASYNC:
6059 t = OMP_CLAUSE_ASYNC_EXPR (c);
6060 if (t == error_mark_node)
6061 remove = true;
6062 else if (!type_dependent_expression_p (t)
6063 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6065 error ("%<async%> expression must be integral");
6066 remove = true;
6068 else
6070 t = mark_rvalue_use (t);
6071 if (!processing_template_decl)
6072 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6073 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6075 break;
6077 case OMP_CLAUSE_VECTOR_LENGTH:
6078 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
6079 t = maybe_convert_cond (t);
6080 if (t == error_mark_node)
6081 remove = true;
6082 else if (!processing_template_decl)
6083 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6084 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
6085 break;
6087 case OMP_CLAUSE_WAIT:
6088 t = OMP_CLAUSE_WAIT_EXPR (c);
6089 if (t == error_mark_node)
6090 remove = true;
6091 else if (!processing_template_decl)
6092 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6093 OMP_CLAUSE_WAIT_EXPR (c) = t;
6094 break;
6096 case OMP_CLAUSE_THREAD_LIMIT:
6097 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6098 if (t == error_mark_node)
6099 remove = true;
6100 else if (!type_dependent_expression_p (t)
6101 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6103 error ("%<thread_limit%> expression must be integral");
6104 remove = true;
6106 else
6108 t = mark_rvalue_use (t);
6109 if (!processing_template_decl)
6111 t = maybe_constant_value (t);
6112 if (TREE_CODE (t) == INTEGER_CST
6113 && tree_int_cst_sgn (t) != 1)
6115 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6116 "%<thread_limit%> value must be positive");
6117 t = integer_one_node;
6119 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6121 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6123 break;
6125 case OMP_CLAUSE_DEVICE:
6126 t = OMP_CLAUSE_DEVICE_ID (c);
6127 if (t == error_mark_node)
6128 remove = true;
6129 else if (!type_dependent_expression_p (t)
6130 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6132 error ("%<device%> id must be integral");
6133 remove = true;
6135 else
6137 t = mark_rvalue_use (t);
6138 if (!processing_template_decl)
6139 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6140 OMP_CLAUSE_DEVICE_ID (c) = t;
6142 break;
6144 case OMP_CLAUSE_DIST_SCHEDULE:
6145 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6146 if (t == NULL)
6148 else if (t == error_mark_node)
6149 remove = true;
6150 else if (!type_dependent_expression_p (t)
6151 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6153 error ("%<dist_schedule%> chunk size expression must be "
6154 "integral");
6155 remove = true;
6157 else
6159 t = mark_rvalue_use (t);
6160 if (!processing_template_decl)
6161 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6162 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6164 break;
6166 case OMP_CLAUSE_ALIGNED:
6167 t = OMP_CLAUSE_DECL (c);
6168 if (t == current_class_ptr && !declare_simd)
6170 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6171 " clauses");
6172 remove = true;
6173 break;
6175 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6177 if (processing_template_decl)
6178 break;
6179 if (DECL_P (t))
6180 error ("%qD is not a variable in %<aligned%> clause", t);
6181 else
6182 error ("%qE is not a variable in %<aligned%> clause", t);
6183 remove = true;
6185 else if (!type_dependent_expression_p (t)
6186 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6187 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6188 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6189 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6190 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6191 != ARRAY_TYPE))))
6193 error_at (OMP_CLAUSE_LOCATION (c),
6194 "%qE in %<aligned%> clause is neither a pointer nor "
6195 "an array nor a reference to pointer or array", t);
6196 remove = true;
6198 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6200 error ("%qD appears more than once in %<aligned%> clauses", t);
6201 remove = true;
6203 else
6204 bitmap_set_bit (&aligned_head, DECL_UID (t));
6205 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6206 if (t == error_mark_node)
6207 remove = true;
6208 else if (t == NULL_TREE)
6209 break;
6210 else if (!type_dependent_expression_p (t)
6211 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6213 error ("%<aligned%> clause alignment expression must "
6214 "be integral");
6215 remove = true;
6217 else
6219 t = mark_rvalue_use (t);
6220 t = maybe_constant_value (t);
6221 if (!processing_template_decl)
6223 if (TREE_CODE (t) != INTEGER_CST
6224 || tree_int_cst_sgn (t) != 1)
6226 error ("%<aligned%> clause alignment expression must be "
6227 "positive constant integer expression");
6228 remove = true;
6231 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6233 break;
6235 case OMP_CLAUSE_DEPEND:
6236 t = OMP_CLAUSE_DECL (c);
6237 if (t == NULL_TREE)
6239 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6240 == OMP_CLAUSE_DEPEND_SOURCE);
6241 break;
6243 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6245 if (cp_finish_omp_clause_depend_sink (c))
6246 remove = true;
6247 break;
6249 if (TREE_CODE (t) == TREE_LIST)
6251 if (handle_omp_array_sections (c, allow_fields))
6252 remove = true;
6253 break;
6255 if (t == error_mark_node)
6256 remove = true;
6257 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6259 if (processing_template_decl)
6260 break;
6261 if (DECL_P (t))
6262 error ("%qD is not a variable in %<depend%> clause", t);
6263 else
6264 error ("%qE is not a variable in %<depend%> clause", t);
6265 remove = true;
6267 else if (t == current_class_ptr)
6269 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6270 " clauses");
6271 remove = true;
6273 else if (!processing_template_decl
6274 && !cxx_mark_addressable (t))
6275 remove = true;
6276 break;
6278 case OMP_CLAUSE_MAP:
6279 case OMP_CLAUSE_TO:
6280 case OMP_CLAUSE_FROM:
6281 case OMP_CLAUSE__CACHE_:
6282 t = OMP_CLAUSE_DECL (c);
6283 if (TREE_CODE (t) == TREE_LIST)
6285 if (handle_omp_array_sections (c, allow_fields))
6286 remove = true;
6287 else
6289 t = OMP_CLAUSE_DECL (c);
6290 if (TREE_CODE (t) != TREE_LIST
6291 && !type_dependent_expression_p (t)
6292 && !cp_omp_mappable_type (TREE_TYPE (t)))
6294 error_at (OMP_CLAUSE_LOCATION (c),
6295 "array section does not have mappable type "
6296 "in %qs clause",
6297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6298 remove = true;
6300 while (TREE_CODE (t) == ARRAY_REF)
6301 t = TREE_OPERAND (t, 0);
6302 if (TREE_CODE (t) == COMPONENT_REF
6303 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6305 while (TREE_CODE (t) == COMPONENT_REF)
6306 t = TREE_OPERAND (t, 0);
6307 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6308 break;
6309 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6311 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6312 error ("%qD appears more than once in motion"
6313 " clauses", t);
6314 else
6315 error ("%qD appears more than once in map"
6316 " clauses", t);
6317 remove = true;
6319 else
6321 bitmap_set_bit (&map_head, DECL_UID (t));
6322 bitmap_set_bit (&map_field_head, DECL_UID (t));
6326 break;
6328 if (t == error_mark_node)
6330 remove = true;
6331 break;
6333 if (REFERENCE_REF_P (t)
6334 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6335 t = TREE_OPERAND (t, 0);
6336 if (TREE_CODE (t) == COMPONENT_REF
6337 && allow_fields
6338 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6340 if (type_dependent_expression_p (t))
6341 break;
6342 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6344 error_at (OMP_CLAUSE_LOCATION (c),
6345 "bit-field %qE in %qs clause",
6346 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6347 remove = true;
6349 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6351 error_at (OMP_CLAUSE_LOCATION (c),
6352 "%qE does not have a mappable type in %qs clause",
6353 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6354 remove = true;
6356 while (TREE_CODE (t) == COMPONENT_REF)
6358 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6359 == UNION_TYPE)
6361 error_at (OMP_CLAUSE_LOCATION (c),
6362 "%qE is a member of a union", t);
6363 remove = true;
6364 break;
6366 t = TREE_OPERAND (t, 0);
6368 if (remove)
6369 break;
6370 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6372 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6373 && (OMP_CLAUSE_MAP_KIND (c)
6374 == GOMP_MAP_FIRSTPRIVATE_POINTER))
6376 if (bitmap_bit_p (&generic_field_head, DECL_UID (t)))
6377 break;
6379 else if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6380 break;
6383 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6385 if (processing_template_decl)
6386 break;
6387 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6388 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
6389 break;
6390 if (DECL_P (t))
6391 error ("%qD is not a variable in %qs clause", t,
6392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6393 else
6394 error ("%qE is not a variable in %qs clause", t,
6395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6396 remove = true;
6398 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6400 error ("%qD is threadprivate variable in %qs clause", t,
6401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6402 remove = true;
6404 else if (t == current_class_ptr)
6406 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6407 " clauses");
6408 remove = true;
6409 break;
6411 else if (!processing_template_decl
6412 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6413 && !cxx_mark_addressable (t))
6414 remove = true;
6415 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6416 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6417 || (OMP_CLAUSE_MAP_KIND (c)
6418 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6419 && t == OMP_CLAUSE_DECL (c)
6420 && !type_dependent_expression_p (t)
6421 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6422 == REFERENCE_TYPE)
6423 ? TREE_TYPE (TREE_TYPE (t))
6424 : TREE_TYPE (t)))
6426 error_at (OMP_CLAUSE_LOCATION (c),
6427 "%qD does not have a mappable type in %qs clause", t,
6428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6429 remove = true;
6431 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6432 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6434 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6435 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6437 error ("%qD appears more than once in data clauses", t);
6438 remove = true;
6440 else
6442 bitmap_set_bit (&generic_head, DECL_UID (t));
6443 if (t != OMP_CLAUSE_DECL (c)
6444 && (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF
6445 || (REFERENCE_REF_P (OMP_CLAUSE_DECL (c))
6446 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c),
6448 == COMPONENT_REF))))
6449 bitmap_set_bit (&generic_field_head, DECL_UID (t));
6452 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6454 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6455 error ("%qD appears more than once in motion clauses", t);
6456 else
6457 error ("%qD appears more than once in map clauses", t);
6458 remove = true;
6460 else
6462 bitmap_set_bit (&map_head, DECL_UID (t));
6463 if (t != OMP_CLAUSE_DECL (c)
6464 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6465 bitmap_set_bit (&map_field_head, DECL_UID (t));
6467 break;
6469 case OMP_CLAUSE_TO_DECLARE:
6470 t = OMP_CLAUSE_DECL (c);
6471 if (TREE_CODE (t) == FUNCTION_DECL)
6472 break;
6473 /* FALLTHRU */
6474 case OMP_CLAUSE_LINK:
6475 t = OMP_CLAUSE_DECL (c);
6476 if (!VAR_P (t))
6478 error_at (OMP_CLAUSE_LOCATION (c),
6479 "%qE is not a variable in clause %qs", t,
6480 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6481 remove = true;
6483 else if (DECL_THREAD_LOCAL_P (t))
6485 error_at (OMP_CLAUSE_LOCATION (c),
6486 "%qD is threadprivate variable in %qs clause", t,
6487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6488 remove = true;
6490 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6492 error_at (OMP_CLAUSE_LOCATION (c),
6493 "%qD does not have a mappable type in %qs clause", t,
6494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6495 remove = true;
6497 break;
6499 case OMP_CLAUSE_UNIFORM:
6500 t = OMP_CLAUSE_DECL (c);
6501 if (TREE_CODE (t) != PARM_DECL)
6503 if (processing_template_decl)
6504 break;
6505 if (DECL_P (t))
6506 error ("%qD is not an argument in %<uniform%> clause", t);
6507 else
6508 error ("%qE is not an argument in %<uniform%> clause", t);
6509 remove = true;
6510 break;
6512 goto check_dup_generic;
6514 case OMP_CLAUSE_NUM_TASKS:
6515 t = OMP_CLAUSE_NUM_TASKS_EXPR (c);
6516 if (t == error_mark_node)
6517 remove = true;
6518 else if (!type_dependent_expression_p (t)
6519 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6521 error ("%<num_tasks%> expression must be integral");
6522 remove = true;
6524 else
6526 t = mark_rvalue_use (t);
6527 if (!processing_template_decl)
6529 t = maybe_constant_value (t);
6530 if (TREE_CODE (t) == INTEGER_CST
6531 && tree_int_cst_sgn (t) != 1)
6533 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6534 "%<num_tasks%> value must be positive");
6535 t = integer_one_node;
6537 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6539 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
6541 break;
6543 case OMP_CLAUSE_GRAINSIZE:
6544 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6545 if (t == error_mark_node)
6546 remove = true;
6547 else if (!type_dependent_expression_p (t)
6548 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6550 error ("%<grainsize%> expression must be integral");
6551 remove = true;
6553 else
6555 t = mark_rvalue_use (t);
6556 if (!processing_template_decl)
6558 t = maybe_constant_value (t);
6559 if (TREE_CODE (t) == INTEGER_CST
6560 && tree_int_cst_sgn (t) != 1)
6562 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6563 "%<grainsize%> value must be positive");
6564 t = integer_one_node;
6566 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6568 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6570 break;
6572 case OMP_CLAUSE_PRIORITY:
6573 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6574 if (t == error_mark_node)
6575 remove = true;
6576 else if (!type_dependent_expression_p (t)
6577 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6579 error ("%<priority%> expression must be integral");
6580 remove = true;
6582 else
6584 t = mark_rvalue_use (t);
6585 if (!processing_template_decl)
6587 t = maybe_constant_value (t);
6588 if (TREE_CODE (t) == INTEGER_CST
6589 && tree_int_cst_sgn (t) == -1)
6591 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6592 "%<priority%> value must be non-negative");
6593 t = integer_one_node;
6595 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6597 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6599 break;
6601 case OMP_CLAUSE_HINT:
6602 t = OMP_CLAUSE_HINT_EXPR (c);
6603 if (t == error_mark_node)
6604 remove = true;
6605 else if (!type_dependent_expression_p (t)
6606 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6608 error ("%<num_tasks%> expression must be integral");
6609 remove = true;
6611 else
6613 t = mark_rvalue_use (t);
6614 if (!processing_template_decl)
6616 t = maybe_constant_value (t);
6617 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6619 OMP_CLAUSE_HINT_EXPR (c) = t;
6621 break;
6623 case OMP_CLAUSE_IS_DEVICE_PTR:
6624 case OMP_CLAUSE_USE_DEVICE_PTR:
6625 field_ok = allow_fields;
6626 t = OMP_CLAUSE_DECL (c);
6627 if (!type_dependent_expression_p (t))
6629 tree type = TREE_TYPE (t);
6630 if (TREE_CODE (type) != POINTER_TYPE
6631 && TREE_CODE (type) != ARRAY_TYPE
6632 && (TREE_CODE (type) != REFERENCE_TYPE
6633 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6634 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
6636 error_at (OMP_CLAUSE_LOCATION (c),
6637 "%qs variable is neither a pointer, nor an array"
6638 "nor reference to pointer or array",
6639 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6640 remove = true;
6643 goto check_dup_generic;
6645 case OMP_CLAUSE_NOWAIT:
6646 case OMP_CLAUSE_ORDERED:
6647 case OMP_CLAUSE_DEFAULT:
6648 case OMP_CLAUSE_UNTIED:
6649 case OMP_CLAUSE_COLLAPSE:
6650 case OMP_CLAUSE_MERGEABLE:
6651 case OMP_CLAUSE_PARALLEL:
6652 case OMP_CLAUSE_FOR:
6653 case OMP_CLAUSE_SECTIONS:
6654 case OMP_CLAUSE_TASKGROUP:
6655 case OMP_CLAUSE_PROC_BIND:
6656 case OMP_CLAUSE_NOGROUP:
6657 case OMP_CLAUSE_THREADS:
6658 case OMP_CLAUSE_SIMD:
6659 case OMP_CLAUSE_DEFAULTMAP:
6660 case OMP_CLAUSE__CILK_FOR_COUNT_:
6661 break;
6663 case OMP_CLAUSE_INBRANCH:
6664 case OMP_CLAUSE_NOTINBRANCH:
6665 if (branch_seen)
6667 error ("%<inbranch%> clause is incompatible with "
6668 "%<notinbranch%>");
6669 remove = true;
6671 branch_seen = true;
6672 break;
6674 default:
6675 gcc_unreachable ();
6678 if (remove)
6679 *pc = OMP_CLAUSE_CHAIN (c);
6680 else
6681 pc = &OMP_CLAUSE_CHAIN (c);
6684 for (pc = &clauses, c = clauses; c ; c = *pc)
6686 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
6687 bool remove = false;
6688 bool need_complete_type = false;
6689 bool need_default_ctor = false;
6690 bool need_copy_ctor = false;
6691 bool need_copy_assignment = false;
6692 bool need_implicitly_determined = false;
6693 bool need_dtor = false;
6694 tree type, inner_type;
6696 switch (c_kind)
6698 case OMP_CLAUSE_SHARED:
6699 need_implicitly_determined = true;
6700 break;
6701 case OMP_CLAUSE_PRIVATE:
6702 need_complete_type = true;
6703 need_default_ctor = true;
6704 need_dtor = true;
6705 need_implicitly_determined = true;
6706 break;
6707 case OMP_CLAUSE_FIRSTPRIVATE:
6708 need_complete_type = true;
6709 need_copy_ctor = true;
6710 need_dtor = true;
6711 need_implicitly_determined = true;
6712 break;
6713 case OMP_CLAUSE_LASTPRIVATE:
6714 need_complete_type = true;
6715 need_copy_assignment = true;
6716 need_implicitly_determined = true;
6717 break;
6718 case OMP_CLAUSE_REDUCTION:
6719 need_implicitly_determined = true;
6720 break;
6721 case OMP_CLAUSE_LINEAR:
6722 if (!declare_simd)
6723 need_implicitly_determined = true;
6724 break;
6725 case OMP_CLAUSE_COPYPRIVATE:
6726 need_copy_assignment = true;
6727 break;
6728 case OMP_CLAUSE_COPYIN:
6729 need_copy_assignment = true;
6730 break;
6731 case OMP_CLAUSE_SIMDLEN:
6732 if (safelen
6733 && !processing_template_decl
6734 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
6735 OMP_CLAUSE_SIMDLEN_EXPR (c)))
6737 error_at (OMP_CLAUSE_LOCATION (c),
6738 "%<simdlen%> clause value is bigger than "
6739 "%<safelen%> clause value");
6740 OMP_CLAUSE_SIMDLEN_EXPR (c)
6741 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
6743 pc = &OMP_CLAUSE_CHAIN (c);
6744 continue;
6745 case OMP_CLAUSE_NOWAIT:
6746 if (copyprivate_seen)
6748 error_at (OMP_CLAUSE_LOCATION (c),
6749 "%<nowait%> clause must not be used together "
6750 "with %<copyprivate%>");
6751 *pc = OMP_CLAUSE_CHAIN (c);
6752 continue;
6754 /* FALLTHRU */
6755 default:
6756 pc = &OMP_CLAUSE_CHAIN (c);
6757 continue;
6760 t = OMP_CLAUSE_DECL (c);
6761 if (processing_template_decl
6762 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6764 pc = &OMP_CLAUSE_CHAIN (c);
6765 continue;
6768 switch (c_kind)
6770 case OMP_CLAUSE_LASTPRIVATE:
6771 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6773 need_default_ctor = true;
6774 need_dtor = true;
6776 break;
6778 case OMP_CLAUSE_REDUCTION:
6779 if (finish_omp_reduction_clause (c, &need_default_ctor,
6780 &need_dtor))
6781 remove = true;
6782 else
6783 t = OMP_CLAUSE_DECL (c);
6784 break;
6786 case OMP_CLAUSE_COPYIN:
6787 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
6789 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
6790 remove = true;
6792 break;
6794 default:
6795 break;
6798 if (need_complete_type || need_copy_assignment)
6800 t = require_complete_type (t);
6801 if (t == error_mark_node)
6802 remove = true;
6803 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
6804 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
6805 remove = true;
6807 if (need_implicitly_determined)
6809 const char *share_name = NULL;
6811 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6812 share_name = "threadprivate";
6813 else switch (cxx_omp_predetermined_sharing (t))
6815 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6816 break;
6817 case OMP_CLAUSE_DEFAULT_SHARED:
6818 /* const vars may be specified in firstprivate clause. */
6819 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6820 && cxx_omp_const_qual_no_mutable (t))
6821 break;
6822 share_name = "shared";
6823 break;
6824 case OMP_CLAUSE_DEFAULT_PRIVATE:
6825 share_name = "private";
6826 break;
6827 default:
6828 gcc_unreachable ();
6830 if (share_name)
6832 error ("%qE is predetermined %qs for %qs",
6833 omp_clause_printable_decl (t), share_name,
6834 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6835 remove = true;
6839 /* We're interested in the base element, not arrays. */
6840 inner_type = type = TREE_TYPE (t);
6841 if ((need_complete_type
6842 || need_copy_assignment
6843 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6844 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6845 inner_type = TREE_TYPE (inner_type);
6846 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6847 inner_type = TREE_TYPE (inner_type);
6849 /* Check for special function availability by building a call to one.
6850 Save the results, because later we won't be in the right context
6851 for making these queries. */
6852 if (CLASS_TYPE_P (inner_type)
6853 && COMPLETE_TYPE_P (inner_type)
6854 && (need_default_ctor || need_copy_ctor
6855 || need_copy_assignment || need_dtor)
6856 && !type_dependent_expression_p (t)
6857 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6858 need_copy_ctor, need_copy_assignment,
6859 need_dtor))
6860 remove = true;
6862 if (remove)
6863 *pc = OMP_CLAUSE_CHAIN (c);
6864 else
6865 pc = &OMP_CLAUSE_CHAIN (c);
6868 bitmap_obstack_release (NULL);
6869 return clauses;
6872 /* Start processing OpenMP clauses that can include any
6873 privatization clauses for non-static data members. */
6875 tree
6876 push_omp_privatization_clauses (bool ignore_next)
6878 if (omp_private_member_ignore_next)
6880 omp_private_member_ignore_next = ignore_next;
6881 return NULL_TREE;
6883 omp_private_member_ignore_next = ignore_next;
6884 if (omp_private_member_map)
6885 omp_private_member_vec.safe_push (error_mark_node);
6886 return push_stmt_list ();
6889 /* Revert remapping of any non-static data members since
6890 the last push_omp_privatization_clauses () call. */
6892 void
6893 pop_omp_privatization_clauses (tree stmt)
6895 if (stmt == NULL_TREE)
6896 return;
6897 stmt = pop_stmt_list (stmt);
6898 if (omp_private_member_map)
6900 while (!omp_private_member_vec.is_empty ())
6902 tree t = omp_private_member_vec.pop ();
6903 if (t == error_mark_node)
6905 add_stmt (stmt);
6906 return;
6908 bool no_decl_expr = t == integer_zero_node;
6909 if (no_decl_expr)
6910 t = omp_private_member_vec.pop ();
6911 tree *v = omp_private_member_map->get (t);
6912 gcc_assert (v);
6913 if (!no_decl_expr)
6914 add_decl_expr (*v);
6915 omp_private_member_map->remove (t);
6917 delete omp_private_member_map;
6918 omp_private_member_map = NULL;
6920 add_stmt (stmt);
6923 /* Remember OpenMP privatization clauses mapping and clear it.
6924 Used for lambdas. */
6926 void
6927 save_omp_privatization_clauses (vec<tree> &save)
6929 save = vNULL;
6930 if (omp_private_member_ignore_next)
6931 save.safe_push (integer_one_node);
6932 omp_private_member_ignore_next = false;
6933 if (!omp_private_member_map)
6934 return;
6936 while (!omp_private_member_vec.is_empty ())
6938 tree t = omp_private_member_vec.pop ();
6939 if (t == error_mark_node)
6941 save.safe_push (t);
6942 continue;
6944 tree n = t;
6945 if (t == integer_zero_node)
6946 t = omp_private_member_vec.pop ();
6947 tree *v = omp_private_member_map->get (t);
6948 gcc_assert (v);
6949 save.safe_push (*v);
6950 save.safe_push (t);
6951 if (n != t)
6952 save.safe_push (n);
6954 delete omp_private_member_map;
6955 omp_private_member_map = NULL;
6958 /* Restore OpenMP privatization clauses mapping saved by the
6959 above function. */
6961 void
6962 restore_omp_privatization_clauses (vec<tree> &save)
6964 gcc_assert (omp_private_member_vec.is_empty ());
6965 omp_private_member_ignore_next = false;
6966 if (save.is_empty ())
6967 return;
6968 if (save.length () == 1 && save[0] == integer_one_node)
6970 omp_private_member_ignore_next = true;
6971 save.release ();
6972 return;
6975 omp_private_member_map = new hash_map <tree, tree>;
6976 while (!save.is_empty ())
6978 tree t = save.pop ();
6979 tree n = t;
6980 if (t != error_mark_node)
6982 if (t == integer_one_node)
6984 omp_private_member_ignore_next = true;
6985 gcc_assert (save.is_empty ());
6986 break;
6988 if (t == integer_zero_node)
6989 t = save.pop ();
6990 tree &v = omp_private_member_map->get_or_insert (t);
6991 v = save.pop ();
6993 omp_private_member_vec.safe_push (t);
6994 if (n != t)
6995 omp_private_member_vec.safe_push (n);
6997 save.release ();
7000 /* For all variables in the tree_list VARS, mark them as thread local. */
7002 void
7003 finish_omp_threadprivate (tree vars)
7005 tree t;
7007 /* Mark every variable in VARS to be assigned thread local storage. */
7008 for (t = vars; t; t = TREE_CHAIN (t))
7010 tree v = TREE_PURPOSE (t);
7012 if (error_operand_p (v))
7014 else if (!VAR_P (v))
7015 error ("%<threadprivate%> %qD is not file, namespace "
7016 "or block scope variable", v);
7017 /* If V had already been marked threadprivate, it doesn't matter
7018 whether it had been used prior to this point. */
7019 else if (TREE_USED (v)
7020 && (DECL_LANG_SPECIFIC (v) == NULL
7021 || !CP_DECL_THREADPRIVATE_P (v)))
7022 error ("%qE declared %<threadprivate%> after first use", v);
7023 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7024 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7025 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7026 error ("%<threadprivate%> %qE has incomplete type", v);
7027 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7028 && CP_DECL_CONTEXT (v) != current_class_type)
7029 error ("%<threadprivate%> %qE directive not "
7030 "in %qT definition", v, CP_DECL_CONTEXT (v));
7031 else
7033 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7034 if (DECL_LANG_SPECIFIC (v) == NULL)
7036 retrofit_lang_decl (v);
7038 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7039 after the allocation of the lang_decl structure. */
7040 if (DECL_DISCRIMINATOR_P (v))
7041 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7044 if (! CP_DECL_THREAD_LOCAL_P (v))
7046 CP_DECL_THREAD_LOCAL_P (v) = true;
7047 set_decl_tls_model (v, decl_default_tls_model (v));
7048 /* If rtl has been already set for this var, call
7049 make_decl_rtl once again, so that encode_section_info
7050 has a chance to look at the new decl flags. */
7051 if (DECL_RTL_SET_P (v))
7052 make_decl_rtl (v);
7054 CP_DECL_THREADPRIVATE_P (v) = 1;
7059 /* Build an OpenMP structured block. */
7061 tree
7062 begin_omp_structured_block (void)
7064 return do_pushlevel (sk_omp);
7067 tree
7068 finish_omp_structured_block (tree block)
7070 return do_poplevel (block);
7073 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7074 statement. LOC is the location of the OACC_DATA. */
7076 tree
7077 finish_oacc_data (tree clauses, tree block)
7079 tree stmt;
7081 block = finish_omp_structured_block (block);
7083 stmt = make_node (OACC_DATA);
7084 TREE_TYPE (stmt) = void_type_node;
7085 OACC_DATA_CLAUSES (stmt) = clauses;
7086 OACC_DATA_BODY (stmt) = block;
7088 return add_stmt (stmt);
7091 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
7092 statement. LOC is the location of the OACC_KERNELS. */
7094 tree
7095 finish_oacc_kernels (tree clauses, tree block)
7097 tree stmt;
7099 block = finish_omp_structured_block (block);
7101 stmt = make_node (OACC_KERNELS);
7102 TREE_TYPE (stmt) = void_type_node;
7103 OACC_KERNELS_CLAUSES (stmt) = clauses;
7104 OACC_KERNELS_BODY (stmt) = block;
7106 return add_stmt (stmt);
7109 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
7110 statement. LOC is the location of the OACC_PARALLEL. */
7112 tree
7113 finish_oacc_parallel (tree clauses, tree block)
7115 tree stmt;
7117 block = finish_omp_structured_block (block);
7119 stmt = make_node (OACC_PARALLEL);
7120 TREE_TYPE (stmt) = void_type_node;
7121 OACC_PARALLEL_CLAUSES (stmt) = clauses;
7122 OACC_PARALLEL_BODY (stmt) = block;
7124 return add_stmt (stmt);
7127 /* Similarly, except force the retention of the BLOCK. */
7129 tree
7130 begin_omp_parallel (void)
7132 keep_next_level (true);
7133 return begin_omp_structured_block ();
7136 tree
7137 finish_omp_parallel (tree clauses, tree body)
7139 tree stmt;
7141 body = finish_omp_structured_block (body);
7143 stmt = make_node (OMP_PARALLEL);
7144 TREE_TYPE (stmt) = void_type_node;
7145 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7146 OMP_PARALLEL_BODY (stmt) = body;
7148 return add_stmt (stmt);
7151 tree
7152 begin_omp_task (void)
7154 keep_next_level (true);
7155 return begin_omp_structured_block ();
7158 tree
7159 finish_omp_task (tree clauses, tree body)
7161 tree stmt;
7163 body = finish_omp_structured_block (body);
7165 stmt = make_node (OMP_TASK);
7166 TREE_TYPE (stmt) = void_type_node;
7167 OMP_TASK_CLAUSES (stmt) = clauses;
7168 OMP_TASK_BODY (stmt) = body;
7170 return add_stmt (stmt);
7173 /* Helper function for finish_omp_for. Convert Ith random access iterator
7174 into integral iterator. Return FALSE if successful. */
7176 static bool
7177 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7178 tree declv, tree initv, tree condv, tree incrv,
7179 tree *body, tree *pre_body, tree &clauses,
7180 tree *lastp)
7182 tree diff, iter_init, iter_incr = NULL, last;
7183 tree incr_var = NULL, orig_pre_body, orig_body, c;
7184 tree decl = TREE_VEC_ELT (declv, i);
7185 tree init = TREE_VEC_ELT (initv, i);
7186 tree cond = TREE_VEC_ELT (condv, i);
7187 tree incr = TREE_VEC_ELT (incrv, i);
7188 tree iter = decl;
7189 location_t elocus = locus;
7191 if (init && EXPR_HAS_LOCATION (init))
7192 elocus = EXPR_LOCATION (init);
7194 switch (TREE_CODE (cond))
7196 case GT_EXPR:
7197 case GE_EXPR:
7198 case LT_EXPR:
7199 case LE_EXPR:
7200 case NE_EXPR:
7201 if (TREE_OPERAND (cond, 1) == iter)
7202 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7203 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7204 if (TREE_OPERAND (cond, 0) != iter)
7205 cond = error_mark_node;
7206 else
7208 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7209 TREE_CODE (cond),
7210 iter, ERROR_MARK,
7211 TREE_OPERAND (cond, 1), ERROR_MARK,
7212 NULL, tf_warning_or_error);
7213 if (error_operand_p (tem))
7214 return true;
7216 break;
7217 default:
7218 cond = error_mark_node;
7219 break;
7221 if (cond == error_mark_node)
7223 error_at (elocus, "invalid controlling predicate");
7224 return true;
7226 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7227 ERROR_MARK, iter, ERROR_MARK, NULL,
7228 tf_warning_or_error);
7229 if (error_operand_p (diff))
7230 return true;
7231 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7233 error_at (elocus, "difference between %qE and %qD does not have integer type",
7234 TREE_OPERAND (cond, 1), iter);
7235 return true;
7238 switch (TREE_CODE (incr))
7240 case PREINCREMENT_EXPR:
7241 case PREDECREMENT_EXPR:
7242 case POSTINCREMENT_EXPR:
7243 case POSTDECREMENT_EXPR:
7244 if (TREE_OPERAND (incr, 0) != iter)
7246 incr = error_mark_node;
7247 break;
7249 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7250 TREE_CODE (incr), iter,
7251 tf_warning_or_error);
7252 if (error_operand_p (iter_incr))
7253 return true;
7254 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7255 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7256 incr = integer_one_node;
7257 else
7258 incr = integer_minus_one_node;
7259 break;
7260 case MODIFY_EXPR:
7261 if (TREE_OPERAND (incr, 0) != iter)
7262 incr = error_mark_node;
7263 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7264 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7266 tree rhs = TREE_OPERAND (incr, 1);
7267 if (TREE_OPERAND (rhs, 0) == iter)
7269 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7270 != INTEGER_TYPE)
7271 incr = error_mark_node;
7272 else
7274 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7275 iter, TREE_CODE (rhs),
7276 TREE_OPERAND (rhs, 1),
7277 tf_warning_or_error);
7278 if (error_operand_p (iter_incr))
7279 return true;
7280 incr = TREE_OPERAND (rhs, 1);
7281 incr = cp_convert (TREE_TYPE (diff), incr,
7282 tf_warning_or_error);
7283 if (TREE_CODE (rhs) == MINUS_EXPR)
7285 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7286 incr = fold_if_not_in_template (incr);
7288 if (TREE_CODE (incr) != INTEGER_CST
7289 && (TREE_CODE (incr) != NOP_EXPR
7290 || (TREE_CODE (TREE_OPERAND (incr, 0))
7291 != INTEGER_CST)))
7292 iter_incr = NULL;
7295 else if (TREE_OPERAND (rhs, 1) == iter)
7297 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7298 || TREE_CODE (rhs) != PLUS_EXPR)
7299 incr = error_mark_node;
7300 else
7302 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7303 PLUS_EXPR,
7304 TREE_OPERAND (rhs, 0),
7305 ERROR_MARK, iter,
7306 ERROR_MARK, NULL,
7307 tf_warning_or_error);
7308 if (error_operand_p (iter_incr))
7309 return true;
7310 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7311 iter, NOP_EXPR,
7312 iter_incr,
7313 tf_warning_or_error);
7314 if (error_operand_p (iter_incr))
7315 return true;
7316 incr = TREE_OPERAND (rhs, 0);
7317 iter_incr = NULL;
7320 else
7321 incr = error_mark_node;
7323 else
7324 incr = error_mark_node;
7325 break;
7326 default:
7327 incr = error_mark_node;
7328 break;
7331 if (incr == error_mark_node)
7333 error_at (elocus, "invalid increment expression");
7334 return true;
7337 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7338 bool taskloop_iv_seen = false;
7339 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7340 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7341 && OMP_CLAUSE_DECL (c) == iter)
7343 if (code == OMP_TASKLOOP)
7345 taskloop_iv_seen = true;
7346 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7348 break;
7350 else if (code == OMP_TASKLOOP
7351 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7352 && OMP_CLAUSE_DECL (c) == iter)
7354 taskloop_iv_seen = true;
7355 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7358 decl = create_temporary_var (TREE_TYPE (diff));
7359 pushdecl (decl);
7360 add_decl_expr (decl);
7361 last = create_temporary_var (TREE_TYPE (diff));
7362 pushdecl (last);
7363 add_decl_expr (last);
7364 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST)
7366 incr_var = create_temporary_var (TREE_TYPE (diff));
7367 pushdecl (incr_var);
7368 add_decl_expr (incr_var);
7370 gcc_assert (stmts_are_full_exprs_p ());
7371 tree diffvar = NULL_TREE;
7372 if (code == OMP_TASKLOOP)
7374 if (!taskloop_iv_seen)
7376 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7377 OMP_CLAUSE_DECL (ivc) = iter;
7378 cxx_omp_finish_clause (ivc, NULL);
7379 OMP_CLAUSE_CHAIN (ivc) = clauses;
7380 clauses = ivc;
7382 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7383 OMP_CLAUSE_DECL (lvc) = last;
7384 OMP_CLAUSE_CHAIN (lvc) = clauses;
7385 clauses = lvc;
7386 diffvar = create_temporary_var (TREE_TYPE (diff));
7387 pushdecl (diffvar);
7388 add_decl_expr (diffvar);
7391 orig_pre_body = *pre_body;
7392 *pre_body = push_stmt_list ();
7393 if (orig_pre_body)
7394 add_stmt (orig_pre_body);
7395 if (init != NULL)
7396 finish_expr_stmt (build_x_modify_expr (elocus,
7397 iter, NOP_EXPR, init,
7398 tf_warning_or_error));
7399 init = build_int_cst (TREE_TYPE (diff), 0);
7400 if (c && iter_incr == NULL)
7402 if (incr_var)
7404 finish_expr_stmt (build_x_modify_expr (elocus,
7405 incr_var, NOP_EXPR,
7406 incr, tf_warning_or_error));
7407 incr = incr_var;
7409 iter_incr = build_x_modify_expr (elocus,
7410 iter, PLUS_EXPR, incr,
7411 tf_warning_or_error);
7413 finish_expr_stmt (build_x_modify_expr (elocus,
7414 last, NOP_EXPR, init,
7415 tf_warning_or_error));
7416 if (diffvar)
7418 finish_expr_stmt (build_x_modify_expr (elocus,
7419 diffvar, NOP_EXPR,
7420 diff, tf_warning_or_error));
7421 diff = diffvar;
7423 *pre_body = pop_stmt_list (*pre_body);
7425 cond = cp_build_binary_op (elocus,
7426 TREE_CODE (cond), decl, diff,
7427 tf_warning_or_error);
7428 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7429 elocus, incr, NULL_TREE);
7431 orig_body = *body;
7432 *body = push_stmt_list ();
7433 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7434 iter_init = build_x_modify_expr (elocus,
7435 iter, PLUS_EXPR, iter_init,
7436 tf_warning_or_error);
7437 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7438 finish_expr_stmt (iter_init);
7439 finish_expr_stmt (build_x_modify_expr (elocus,
7440 last, NOP_EXPR, decl,
7441 tf_warning_or_error));
7442 add_stmt (orig_body);
7443 *body = pop_stmt_list (*body);
7445 if (c)
7447 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7448 finish_expr_stmt (iter_incr);
7449 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7450 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7453 TREE_VEC_ELT (declv, i) = decl;
7454 TREE_VEC_ELT (initv, i) = init;
7455 TREE_VEC_ELT (condv, i) = cond;
7456 TREE_VEC_ELT (incrv, i) = incr;
7457 *lastp = last;
7459 return false;
7462 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7463 are directly for their associated operands in the statement. DECL
7464 and INIT are a combo; if DECL is NULL then INIT ought to be a
7465 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7466 optional statements that need to go before the loop into its
7467 sk_omp scope. */
7469 tree
7470 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7471 tree orig_declv, tree initv, tree condv, tree incrv,
7472 tree body, tree pre_body, tree clauses)
7474 tree omp_for = NULL, orig_incr = NULL;
7475 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7476 tree last = NULL_TREE;
7477 location_t elocus;
7478 int i;
7480 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7481 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7482 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7483 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7485 decl = TREE_VEC_ELT (declv, i);
7486 init = TREE_VEC_ELT (initv, i);
7487 cond = TREE_VEC_ELT (condv, i);
7488 incr = TREE_VEC_ELT (incrv, i);
7489 elocus = locus;
7491 if (decl == NULL)
7493 if (init != NULL)
7494 switch (TREE_CODE (init))
7496 case MODIFY_EXPR:
7497 decl = TREE_OPERAND (init, 0);
7498 init = TREE_OPERAND (init, 1);
7499 break;
7500 case MODOP_EXPR:
7501 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
7503 decl = TREE_OPERAND (init, 0);
7504 init = TREE_OPERAND (init, 2);
7506 break;
7507 default:
7508 break;
7511 if (decl == NULL)
7513 error_at (locus,
7514 "expected iteration declaration or initialization");
7515 return NULL;
7519 if (init && EXPR_HAS_LOCATION (init))
7520 elocus = EXPR_LOCATION (init);
7522 if (cond == NULL)
7524 error_at (elocus, "missing controlling predicate");
7525 return NULL;
7528 if (incr == NULL)
7530 error_at (elocus, "missing increment expression");
7531 return NULL;
7534 TREE_VEC_ELT (declv, i) = decl;
7535 TREE_VEC_ELT (initv, i) = init;
7538 if (dependent_omp_for_p (declv, initv, condv, incrv))
7540 tree stmt;
7542 stmt = make_node (code);
7544 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7546 /* This is really just a place-holder. We'll be decomposing this
7547 again and going through the cp_build_modify_expr path below when
7548 we instantiate the thing. */
7549 TREE_VEC_ELT (initv, i)
7550 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
7551 TREE_VEC_ELT (initv, i));
7554 TREE_TYPE (stmt) = void_type_node;
7555 OMP_FOR_INIT (stmt) = initv;
7556 OMP_FOR_COND (stmt) = condv;
7557 OMP_FOR_INCR (stmt) = incrv;
7558 OMP_FOR_BODY (stmt) = body;
7559 OMP_FOR_PRE_BODY (stmt) = pre_body;
7560 OMP_FOR_CLAUSES (stmt) = clauses;
7562 SET_EXPR_LOCATION (stmt, locus);
7563 return add_stmt (stmt);
7566 if (!orig_declv)
7567 orig_declv = copy_node (declv);
7569 if (processing_template_decl)
7570 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
7572 for (i = 0; i < TREE_VEC_LENGTH (declv); )
7574 decl = TREE_VEC_ELT (declv, i);
7575 init = TREE_VEC_ELT (initv, i);
7576 cond = TREE_VEC_ELT (condv, i);
7577 incr = TREE_VEC_ELT (incrv, i);
7578 if (orig_incr)
7579 TREE_VEC_ELT (orig_incr, i) = incr;
7580 elocus = locus;
7582 if (init && EXPR_HAS_LOCATION (init))
7583 elocus = EXPR_LOCATION (init);
7585 if (!DECL_P (decl))
7587 error_at (elocus, "expected iteration declaration or initialization");
7588 return NULL;
7591 if (incr && TREE_CODE (incr) == MODOP_EXPR)
7593 if (orig_incr)
7594 TREE_VEC_ELT (orig_incr, i) = incr;
7595 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
7596 TREE_CODE (TREE_OPERAND (incr, 1)),
7597 TREE_OPERAND (incr, 2),
7598 tf_warning_or_error);
7601 if (CLASS_TYPE_P (TREE_TYPE (decl)))
7603 if (code == OMP_SIMD)
7605 error_at (elocus, "%<#pragma omp simd%> used with class "
7606 "iteration variable %qE", decl);
7607 return NULL;
7609 if (code == CILK_FOR && i == 0)
7610 orig_decl = decl;
7611 if (handle_omp_for_class_iterator (i, locus, code, declv, initv,
7612 condv, incrv, &body, &pre_body,
7613 clauses, &last))
7614 return NULL;
7615 continue;
7618 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
7619 && !TYPE_PTR_P (TREE_TYPE (decl)))
7621 error_at (elocus, "invalid type for iteration variable %qE", decl);
7622 return NULL;
7625 if (!processing_template_decl)
7627 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
7628 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
7630 else
7631 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
7632 if (cond
7633 && TREE_SIDE_EFFECTS (cond)
7634 && COMPARISON_CLASS_P (cond)
7635 && !processing_template_decl)
7637 tree t = TREE_OPERAND (cond, 0);
7638 if (TREE_SIDE_EFFECTS (t)
7639 && t != decl
7640 && (TREE_CODE (t) != NOP_EXPR
7641 || TREE_OPERAND (t, 0) != decl))
7642 TREE_OPERAND (cond, 0)
7643 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7645 t = TREE_OPERAND (cond, 1);
7646 if (TREE_SIDE_EFFECTS (t)
7647 && t != decl
7648 && (TREE_CODE (t) != NOP_EXPR
7649 || TREE_OPERAND (t, 0) != decl))
7650 TREE_OPERAND (cond, 1)
7651 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7653 if (decl == error_mark_node || init == error_mark_node)
7654 return NULL;
7656 TREE_VEC_ELT (declv, i) = decl;
7657 TREE_VEC_ELT (initv, i) = init;
7658 TREE_VEC_ELT (condv, i) = cond;
7659 TREE_VEC_ELT (incrv, i) = incr;
7660 i++;
7663 if (IS_EMPTY_STMT (pre_body))
7664 pre_body = NULL;
7666 if (code == CILK_FOR && !processing_template_decl)
7667 block = push_stmt_list ();
7669 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
7670 incrv, body, pre_body);
7672 if (omp_for == NULL)
7674 if (block)
7675 pop_stmt_list (block);
7676 return NULL;
7679 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
7681 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
7682 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
7684 if (TREE_CODE (incr) != MODIFY_EXPR)
7685 continue;
7687 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
7688 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
7689 && !processing_template_decl)
7691 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
7692 if (TREE_SIDE_EFFECTS (t)
7693 && t != decl
7694 && (TREE_CODE (t) != NOP_EXPR
7695 || TREE_OPERAND (t, 0) != decl))
7696 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
7697 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7699 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7700 if (TREE_SIDE_EFFECTS (t)
7701 && t != decl
7702 && (TREE_CODE (t) != NOP_EXPR
7703 || TREE_OPERAND (t, 0) != decl))
7704 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
7705 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7708 if (orig_incr)
7709 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
7711 OMP_FOR_CLAUSES (omp_for) = clauses;
7713 /* For simd loops with non-static data member iterators, we could have added
7714 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
7715 step at this point, fill it in. */
7716 if (code == OMP_SIMD && !processing_template_decl
7717 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
7718 for (tree c = find_omp_clause (clauses, OMP_CLAUSE_LINEAR); c;
7719 c = find_omp_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
7720 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
7722 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
7723 gcc_assert (decl == OMP_CLAUSE_DECL (c));
7724 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
7725 tree step, stept;
7726 switch (TREE_CODE (incr))
7728 case PREINCREMENT_EXPR:
7729 case POSTINCREMENT_EXPR:
7730 /* c_omp_for_incr_canonicalize_ptr() should have been
7731 called to massage things appropriately. */
7732 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7733 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
7734 break;
7735 case PREDECREMENT_EXPR:
7736 case POSTDECREMENT_EXPR:
7737 /* c_omp_for_incr_canonicalize_ptr() should have been
7738 called to massage things appropriately. */
7739 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7740 OMP_CLAUSE_LINEAR_STEP (c)
7741 = build_int_cst (TREE_TYPE (decl), -1);
7742 break;
7743 case MODIFY_EXPR:
7744 gcc_assert (TREE_OPERAND (incr, 0) == decl);
7745 incr = TREE_OPERAND (incr, 1);
7746 switch (TREE_CODE (incr))
7748 case PLUS_EXPR:
7749 if (TREE_OPERAND (incr, 1) == decl)
7750 step = TREE_OPERAND (incr, 0);
7751 else
7752 step = TREE_OPERAND (incr, 1);
7753 break;
7754 case MINUS_EXPR:
7755 case POINTER_PLUS_EXPR:
7756 gcc_assert (TREE_OPERAND (incr, 0) == decl);
7757 step = TREE_OPERAND (incr, 1);
7758 break;
7759 default:
7760 gcc_unreachable ();
7762 stept = TREE_TYPE (decl);
7763 if (POINTER_TYPE_P (stept))
7764 stept = sizetype;
7765 step = fold_convert (stept, step);
7766 if (TREE_CODE (incr) == MINUS_EXPR)
7767 step = fold_build1 (NEGATE_EXPR, stept, step);
7768 OMP_CLAUSE_LINEAR_STEP (c) = step;
7769 break;
7770 default:
7771 gcc_unreachable ();
7775 if (block)
7777 tree omp_par = make_node (OMP_PARALLEL);
7778 TREE_TYPE (omp_par) = void_type_node;
7779 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
7780 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7781 TREE_SIDE_EFFECTS (bind) = 1;
7782 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
7783 OMP_PARALLEL_BODY (omp_par) = bind;
7784 if (OMP_FOR_PRE_BODY (omp_for))
7786 add_stmt (OMP_FOR_PRE_BODY (omp_for));
7787 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
7789 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
7790 decl = TREE_OPERAND (init, 0);
7791 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
7792 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
7793 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
7794 clauses = OMP_FOR_CLAUSES (omp_for);
7795 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
7796 for (pc = &clauses; *pc; )
7797 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
7799 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
7800 OMP_FOR_CLAUSES (omp_for) = *pc;
7801 *pc = OMP_CLAUSE_CHAIN (*pc);
7802 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
7804 else
7806 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
7807 pc = &OMP_CLAUSE_CHAIN (*pc);
7809 if (TREE_CODE (t) != INTEGER_CST)
7811 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
7812 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7813 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
7814 OMP_CLAUSE_CHAIN (c) = clauses;
7815 clauses = c;
7817 if (TREE_CODE (incr) == MODIFY_EXPR)
7819 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7820 if (TREE_CODE (t) != INTEGER_CST)
7822 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
7823 = get_temp_regvar (TREE_TYPE (t), t);
7824 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7825 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7826 OMP_CLAUSE_CHAIN (c) = clauses;
7827 clauses = c;
7830 t = TREE_OPERAND (init, 1);
7831 if (TREE_CODE (t) != INTEGER_CST)
7833 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
7834 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7835 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
7836 OMP_CLAUSE_CHAIN (c) = clauses;
7837 clauses = c;
7839 if (orig_decl && orig_decl != decl)
7841 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7842 OMP_CLAUSE_DECL (c) = orig_decl;
7843 OMP_CLAUSE_CHAIN (c) = clauses;
7844 clauses = c;
7846 if (last)
7848 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7849 OMP_CLAUSE_DECL (c) = last;
7850 OMP_CLAUSE_CHAIN (c) = clauses;
7851 clauses = c;
7853 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
7854 OMP_CLAUSE_DECL (c) = decl;
7855 OMP_CLAUSE_CHAIN (c) = clauses;
7856 clauses = c;
7857 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
7858 OMP_CLAUSE_OPERAND (c, 0)
7859 = cilk_for_number_of_iterations (omp_for);
7860 OMP_CLAUSE_CHAIN (c) = clauses;
7861 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, false);
7862 add_stmt (omp_par);
7863 return omp_par;
7865 else if (code == CILK_FOR && processing_template_decl)
7867 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
7868 if (orig_decl && orig_decl != decl)
7870 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7871 OMP_CLAUSE_DECL (c) = orig_decl;
7872 OMP_CLAUSE_CHAIN (c) = clauses;
7873 clauses = c;
7875 if (last)
7877 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7878 OMP_CLAUSE_DECL (c) = last;
7879 OMP_CLAUSE_CHAIN (c) = clauses;
7880 clauses = c;
7882 OMP_FOR_CLAUSES (omp_for) = clauses;
7884 return omp_for;
7887 void
7888 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
7889 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
7891 tree orig_lhs;
7892 tree orig_rhs;
7893 tree orig_v;
7894 tree orig_lhs1;
7895 tree orig_rhs1;
7896 bool dependent_p;
7897 tree stmt;
7899 orig_lhs = lhs;
7900 orig_rhs = rhs;
7901 orig_v = v;
7902 orig_lhs1 = lhs1;
7903 orig_rhs1 = rhs1;
7904 dependent_p = false;
7905 stmt = NULL_TREE;
7907 /* Even in a template, we can detect invalid uses of the atomic
7908 pragma if neither LHS nor RHS is type-dependent. */
7909 if (processing_template_decl)
7911 dependent_p = (type_dependent_expression_p (lhs)
7912 || (rhs && type_dependent_expression_p (rhs))
7913 || (v && type_dependent_expression_p (v))
7914 || (lhs1 && type_dependent_expression_p (lhs1))
7915 || (rhs1 && type_dependent_expression_p (rhs1)));
7916 if (!dependent_p)
7918 lhs = build_non_dependent_expr (lhs);
7919 if (rhs)
7920 rhs = build_non_dependent_expr (rhs);
7921 if (v)
7922 v = build_non_dependent_expr (v);
7923 if (lhs1)
7924 lhs1 = build_non_dependent_expr (lhs1);
7925 if (rhs1)
7926 rhs1 = build_non_dependent_expr (rhs1);
7929 if (!dependent_p)
7931 bool swapped = false;
7932 if (rhs1 && cp_tree_equal (lhs, rhs))
7934 std::swap (rhs, rhs1);
7935 swapped = !commutative_tree_code (opcode);
7937 if (rhs1 && !cp_tree_equal (lhs, rhs1))
7939 if (code == OMP_ATOMIC)
7940 error ("%<#pragma omp atomic update%> uses two different "
7941 "expressions for memory");
7942 else
7943 error ("%<#pragma omp atomic capture%> uses two different "
7944 "expressions for memory");
7945 return;
7947 if (lhs1 && !cp_tree_equal (lhs, lhs1))
7949 if (code == OMP_ATOMIC)
7950 error ("%<#pragma omp atomic update%> uses two different "
7951 "expressions for memory");
7952 else
7953 error ("%<#pragma omp atomic capture%> uses two different "
7954 "expressions for memory");
7955 return;
7957 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
7958 v, lhs1, rhs1, swapped, seq_cst);
7959 if (stmt == error_mark_node)
7960 return;
7962 if (processing_template_decl)
7964 if (code == OMP_ATOMIC_READ)
7966 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
7967 OMP_ATOMIC_READ, orig_lhs);
7968 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7969 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
7971 else
7973 if (opcode == NOP_EXPR)
7974 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
7975 else
7976 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
7977 if (orig_rhs1)
7978 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
7979 COMPOUND_EXPR, orig_rhs1, stmt);
7980 if (code != OMP_ATOMIC)
7982 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
7983 code, orig_lhs1, stmt);
7984 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7985 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
7988 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
7989 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7991 finish_expr_stmt (stmt);
7994 void
7995 finish_omp_barrier (void)
7997 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
7998 vec<tree, va_gc> *vec = make_tree_vector ();
7999 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8000 release_tree_vector (vec);
8001 finish_expr_stmt (stmt);
8004 void
8005 finish_omp_flush (void)
8007 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8008 vec<tree, va_gc> *vec = make_tree_vector ();
8009 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8010 release_tree_vector (vec);
8011 finish_expr_stmt (stmt);
8014 void
8015 finish_omp_taskwait (void)
8017 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8018 vec<tree, va_gc> *vec = make_tree_vector ();
8019 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8020 release_tree_vector (vec);
8021 finish_expr_stmt (stmt);
8024 void
8025 finish_omp_taskyield (void)
8027 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8028 vec<tree, va_gc> *vec = make_tree_vector ();
8029 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8030 release_tree_vector (vec);
8031 finish_expr_stmt (stmt);
8034 void
8035 finish_omp_cancel (tree clauses)
8037 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8038 int mask = 0;
8039 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8040 mask = 1;
8041 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8042 mask = 2;
8043 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8044 mask = 4;
8045 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8046 mask = 8;
8047 else
8049 error ("%<#pragma omp cancel must specify one of "
8050 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8051 return;
8053 vec<tree, va_gc> *vec = make_tree_vector ();
8054 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
8055 if (ifc != NULL_TREE)
8057 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8058 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8059 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8060 build_zero_cst (type));
8062 else
8063 ifc = boolean_true_node;
8064 vec->quick_push (build_int_cst (integer_type_node, mask));
8065 vec->quick_push (ifc);
8066 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8067 release_tree_vector (vec);
8068 finish_expr_stmt (stmt);
8071 void
8072 finish_omp_cancellation_point (tree clauses)
8074 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8075 int mask = 0;
8076 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8077 mask = 1;
8078 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8079 mask = 2;
8080 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8081 mask = 4;
8082 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8083 mask = 8;
8084 else
8086 error ("%<#pragma omp cancellation point must specify one of "
8087 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8088 return;
8090 vec<tree, va_gc> *vec
8091 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8092 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8093 release_tree_vector (vec);
8094 finish_expr_stmt (stmt);
8097 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8098 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8099 should create an extra compound stmt. */
8101 tree
8102 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8104 tree r;
8106 if (pcompound)
8107 *pcompound = begin_compound_stmt (0);
8109 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8111 /* Only add the statement to the function if support enabled. */
8112 if (flag_tm)
8113 add_stmt (r);
8114 else
8115 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8116 ? G_("%<__transaction_relaxed%> without "
8117 "transactional memory support enabled")
8118 : G_("%<__transaction_atomic%> without "
8119 "transactional memory support enabled")));
8121 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8122 TREE_SIDE_EFFECTS (r) = 1;
8123 return r;
8126 /* End a __transaction_atomic or __transaction_relaxed statement.
8127 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8128 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8129 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8131 void
8132 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8134 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8135 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8136 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8137 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8139 /* noexcept specifications are not allowed for function transactions. */
8140 gcc_assert (!(noex && compound_stmt));
8141 if (noex)
8143 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8144 noex);
8145 /* This may not be true when the STATEMENT_LIST is empty. */
8146 if (EXPR_P (body))
8147 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8148 TREE_SIDE_EFFECTS (body) = 1;
8149 TRANSACTION_EXPR_BODY (stmt) = body;
8152 if (compound_stmt)
8153 finish_compound_stmt (compound_stmt);
8156 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8157 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8158 condition. */
8160 tree
8161 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8163 tree ret;
8164 if (noex)
8166 expr = build_must_not_throw_expr (expr, noex);
8167 if (EXPR_P (expr))
8168 SET_EXPR_LOCATION (expr, loc);
8169 TREE_SIDE_EFFECTS (expr) = 1;
8171 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8172 if (flags & TM_STMT_ATTR_RELAXED)
8173 TRANSACTION_EXPR_RELAXED (ret) = 1;
8174 TREE_SIDE_EFFECTS (ret) = 1;
8175 SET_EXPR_LOCATION (ret, loc);
8176 return ret;
8179 void
8180 init_cp_semantics (void)
8184 /* Build a STATIC_ASSERT for a static assertion with the condition
8185 CONDITION and the message text MESSAGE. LOCATION is the location
8186 of the static assertion in the source code. When MEMBER_P, this
8187 static assertion is a member of a class. */
8188 void
8189 finish_static_assert (tree condition, tree message, location_t location,
8190 bool member_p)
8192 if (message == NULL_TREE
8193 || message == error_mark_node
8194 || condition == NULL_TREE
8195 || condition == error_mark_node)
8196 return;
8198 if (check_for_bare_parameter_packs (condition))
8199 condition = error_mark_node;
8201 if (type_dependent_expression_p (condition)
8202 || value_dependent_expression_p (condition))
8204 /* We're in a template; build a STATIC_ASSERT and put it in
8205 the right place. */
8206 tree assertion;
8208 assertion = make_node (STATIC_ASSERT);
8209 STATIC_ASSERT_CONDITION (assertion) = condition;
8210 STATIC_ASSERT_MESSAGE (assertion) = message;
8211 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8213 if (member_p)
8214 maybe_add_class_template_decl_list (current_class_type,
8215 assertion,
8216 /*friend_p=*/0);
8217 else
8218 add_stmt (assertion);
8220 return;
8223 /* Fold the expression and convert it to a boolean value. */
8224 condition = instantiate_non_dependent_expr (condition);
8225 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8226 condition = maybe_constant_value (condition);
8228 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8229 /* Do nothing; the condition is satisfied. */
8231 else
8233 location_t saved_loc = input_location;
8235 input_location = location;
8236 if (TREE_CODE (condition) == INTEGER_CST
8237 && integer_zerop (condition))
8239 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8240 (TREE_TYPE (TREE_TYPE (message))));
8241 int len = TREE_STRING_LENGTH (message) / sz - 1;
8242 /* Report the error. */
8243 if (len == 0)
8244 error ("static assertion failed");
8245 else
8246 error ("static assertion failed: %s",
8247 TREE_STRING_POINTER (message));
8249 else if (condition && condition != error_mark_node)
8251 error ("non-constant condition for static assertion");
8252 if (require_potential_rvalue_constant_expression (condition))
8253 cxx_constant_value (condition);
8255 input_location = saved_loc;
8259 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8260 suitable for use as a type-specifier.
8262 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8263 id-expression or a class member access, FALSE when it was parsed as
8264 a full expression. */
8266 tree
8267 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8268 tsubst_flags_t complain)
8270 tree type = NULL_TREE;
8272 if (!expr || error_operand_p (expr))
8273 return error_mark_node;
8275 if (TYPE_P (expr)
8276 || TREE_CODE (expr) == TYPE_DECL
8277 || (TREE_CODE (expr) == BIT_NOT_EXPR
8278 && TYPE_P (TREE_OPERAND (expr, 0))))
8280 if (complain & tf_error)
8281 error ("argument to decltype must be an expression");
8282 return error_mark_node;
8285 /* Depending on the resolution of DR 1172, we may later need to distinguish
8286 instantiation-dependent but not type-dependent expressions so that, say,
8287 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8288 if (instantiation_dependent_expression_p (expr))
8290 type = cxx_make_type (DECLTYPE_TYPE);
8291 DECLTYPE_TYPE_EXPR (type) = expr;
8292 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8293 = id_expression_or_member_access_p;
8294 SET_TYPE_STRUCTURAL_EQUALITY (type);
8296 return type;
8299 /* The type denoted by decltype(e) is defined as follows: */
8301 expr = resolve_nondeduced_context (expr);
8303 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8304 return error_mark_node;
8306 if (type_unknown_p (expr))
8308 if (complain & tf_error)
8309 error ("decltype cannot resolve address of overloaded function");
8310 return error_mark_node;
8313 /* To get the size of a static data member declared as an array of
8314 unknown bound, we need to instantiate it. */
8315 if (VAR_P (expr)
8316 && VAR_HAD_UNKNOWN_BOUND (expr)
8317 && DECL_TEMPLATE_INSTANTIATION (expr))
8318 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8320 if (id_expression_or_member_access_p)
8322 /* If e is an id-expression or a class member access (5.2.5
8323 [expr.ref]), decltype(e) is defined as the type of the entity
8324 named by e. If there is no such entity, or e names a set of
8325 overloaded functions, the program is ill-formed. */
8326 if (identifier_p (expr))
8327 expr = lookup_name (expr);
8329 if (INDIRECT_REF_P (expr))
8330 /* This can happen when the expression is, e.g., "a.b". Just
8331 look at the underlying operand. */
8332 expr = TREE_OPERAND (expr, 0);
8334 if (TREE_CODE (expr) == OFFSET_REF
8335 || TREE_CODE (expr) == MEMBER_REF
8336 || TREE_CODE (expr) == SCOPE_REF)
8337 /* We're only interested in the field itself. If it is a
8338 BASELINK, we will need to see through it in the next
8339 step. */
8340 expr = TREE_OPERAND (expr, 1);
8342 if (BASELINK_P (expr))
8343 /* See through BASELINK nodes to the underlying function. */
8344 expr = BASELINK_FUNCTIONS (expr);
8346 switch (TREE_CODE (expr))
8348 case FIELD_DECL:
8349 if (DECL_BIT_FIELD_TYPE (expr))
8351 type = DECL_BIT_FIELD_TYPE (expr);
8352 break;
8354 /* Fall through for fields that aren't bitfields. */
8356 case FUNCTION_DECL:
8357 case VAR_DECL:
8358 case CONST_DECL:
8359 case PARM_DECL:
8360 case RESULT_DECL:
8361 case TEMPLATE_PARM_INDEX:
8362 expr = mark_type_use (expr);
8363 type = TREE_TYPE (expr);
8364 break;
8366 case ERROR_MARK:
8367 type = error_mark_node;
8368 break;
8370 case COMPONENT_REF:
8371 case COMPOUND_EXPR:
8372 mark_type_use (expr);
8373 type = is_bitfield_expr_with_lowered_type (expr);
8374 if (!type)
8375 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8376 break;
8378 case BIT_FIELD_REF:
8379 gcc_unreachable ();
8381 case INTEGER_CST:
8382 case PTRMEM_CST:
8383 /* We can get here when the id-expression refers to an
8384 enumerator or non-type template parameter. */
8385 type = TREE_TYPE (expr);
8386 break;
8388 default:
8389 /* Handle instantiated template non-type arguments. */
8390 type = TREE_TYPE (expr);
8391 break;
8394 else
8396 /* Within a lambda-expression:
8398 Every occurrence of decltype((x)) where x is a possibly
8399 parenthesized id-expression that names an entity of
8400 automatic storage duration is treated as if x were
8401 transformed into an access to a corresponding data member
8402 of the closure type that would have been declared if x
8403 were a use of the denoted entity. */
8404 if (outer_automatic_var_p (expr)
8405 && current_function_decl
8406 && LAMBDA_FUNCTION_P (current_function_decl))
8407 type = capture_decltype (expr);
8408 else if (error_operand_p (expr))
8409 type = error_mark_node;
8410 else if (expr == current_class_ptr)
8411 /* If the expression is just "this", we want the
8412 cv-unqualified pointer for the "this" type. */
8413 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8414 else
8416 /* Otherwise, where T is the type of e, if e is an lvalue,
8417 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8418 cp_lvalue_kind clk = lvalue_kind (expr);
8419 type = unlowered_expr_type (expr);
8420 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8422 /* For vector types, pick a non-opaque variant. */
8423 if (VECTOR_TYPE_P (type))
8424 type = strip_typedefs (type);
8426 if (clk != clk_none && !(clk & clk_class))
8427 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8431 return type;
8434 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8435 __has_nothrow_copy, depending on assign_p. */
8437 static bool
8438 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8440 tree fns;
8442 if (assign_p)
8444 int ix;
8445 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
8446 if (ix < 0)
8447 return false;
8448 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
8450 else if (TYPE_HAS_COPY_CTOR (type))
8452 /* If construction of the copy constructor was postponed, create
8453 it now. */
8454 if (CLASSTYPE_LAZY_COPY_CTOR (type))
8455 lazily_declare_fn (sfk_copy_constructor, type);
8456 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
8457 lazily_declare_fn (sfk_move_constructor, type);
8458 fns = CLASSTYPE_CONSTRUCTORS (type);
8460 else
8461 return false;
8463 for (; fns; fns = OVL_NEXT (fns))
8465 tree fn = OVL_CURRENT (fns);
8467 if (assign_p)
8469 if (copy_fn_p (fn) == 0)
8470 continue;
8472 else if (copy_fn_p (fn) <= 0)
8473 continue;
8475 maybe_instantiate_noexcept (fn);
8476 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8477 return false;
8480 return true;
8483 /* Actually evaluates the trait. */
8485 static bool
8486 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8488 enum tree_code type_code1;
8489 tree t;
8491 type_code1 = TREE_CODE (type1);
8493 switch (kind)
8495 case CPTK_HAS_NOTHROW_ASSIGN:
8496 type1 = strip_array_types (type1);
8497 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8498 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8499 || (CLASS_TYPE_P (type1)
8500 && classtype_has_nothrow_assign_or_copy_p (type1,
8501 true))));
8503 case CPTK_HAS_TRIVIAL_ASSIGN:
8504 /* ??? The standard seems to be missing the "or array of such a class
8505 type" wording for this trait. */
8506 type1 = strip_array_types (type1);
8507 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8508 && (trivial_type_p (type1)
8509 || (CLASS_TYPE_P (type1)
8510 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8512 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8513 type1 = strip_array_types (type1);
8514 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8515 || (CLASS_TYPE_P (type1)
8516 && (t = locate_ctor (type1))
8517 && (maybe_instantiate_noexcept (t),
8518 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8520 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8521 type1 = strip_array_types (type1);
8522 return (trivial_type_p (type1)
8523 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8525 case CPTK_HAS_NOTHROW_COPY:
8526 type1 = strip_array_types (type1);
8527 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8528 || (CLASS_TYPE_P (type1)
8529 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8531 case CPTK_HAS_TRIVIAL_COPY:
8532 /* ??? The standard seems to be missing the "or array of such a class
8533 type" wording for this trait. */
8534 type1 = strip_array_types (type1);
8535 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8536 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8538 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8539 type1 = strip_array_types (type1);
8540 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8541 || (CLASS_TYPE_P (type1)
8542 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8544 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8545 return type_has_virtual_destructor (type1);
8547 case CPTK_IS_ABSTRACT:
8548 return (ABSTRACT_CLASS_TYPE_P (type1));
8550 case CPTK_IS_BASE_OF:
8551 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8552 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8553 || DERIVED_FROM_P (type1, type2)));
8555 case CPTK_IS_CLASS:
8556 return (NON_UNION_CLASS_TYPE_P (type1));
8558 case CPTK_IS_EMPTY:
8559 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
8561 case CPTK_IS_ENUM:
8562 return (type_code1 == ENUMERAL_TYPE);
8564 case CPTK_IS_FINAL:
8565 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
8567 case CPTK_IS_LITERAL_TYPE:
8568 return (literal_type_p (type1));
8570 case CPTK_IS_POD:
8571 return (pod_type_p (type1));
8573 case CPTK_IS_POLYMORPHIC:
8574 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
8576 case CPTK_IS_STD_LAYOUT:
8577 return (std_layout_type_p (type1));
8579 case CPTK_IS_TRIVIAL:
8580 return (trivial_type_p (type1));
8582 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8583 return is_trivially_xible (MODIFY_EXPR, type1, type2);
8585 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8586 return is_trivially_xible (INIT_EXPR, type1, type2);
8588 case CPTK_IS_TRIVIALLY_COPYABLE:
8589 return (trivially_copyable_p (type1));
8591 case CPTK_IS_UNION:
8592 return (type_code1 == UNION_TYPE);
8594 default:
8595 gcc_unreachable ();
8596 return false;
8600 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
8601 void, or a complete type, returns true, otherwise false. */
8603 static bool
8604 check_trait_type (tree type)
8606 if (type == NULL_TREE)
8607 return true;
8609 if (TREE_CODE (type) == TREE_LIST)
8610 return (check_trait_type (TREE_VALUE (type))
8611 && check_trait_type (TREE_CHAIN (type)));
8613 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8614 && COMPLETE_TYPE_P (TREE_TYPE (type)))
8615 return true;
8617 if (VOID_TYPE_P (type))
8618 return true;
8620 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
8623 /* Process a trait expression. */
8625 tree
8626 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
8628 if (type1 == error_mark_node
8629 || type2 == error_mark_node)
8630 return error_mark_node;
8632 if (processing_template_decl)
8634 tree trait_expr = make_node (TRAIT_EXPR);
8635 TREE_TYPE (trait_expr) = boolean_type_node;
8636 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
8637 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
8638 TRAIT_EXPR_KIND (trait_expr) = kind;
8639 return trait_expr;
8642 switch (kind)
8644 case CPTK_HAS_NOTHROW_ASSIGN:
8645 case CPTK_HAS_TRIVIAL_ASSIGN:
8646 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8647 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8648 case CPTK_HAS_NOTHROW_COPY:
8649 case CPTK_HAS_TRIVIAL_COPY:
8650 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8651 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8652 case CPTK_IS_ABSTRACT:
8653 case CPTK_IS_EMPTY:
8654 case CPTK_IS_FINAL:
8655 case CPTK_IS_LITERAL_TYPE:
8656 case CPTK_IS_POD:
8657 case CPTK_IS_POLYMORPHIC:
8658 case CPTK_IS_STD_LAYOUT:
8659 case CPTK_IS_TRIVIAL:
8660 case CPTK_IS_TRIVIALLY_COPYABLE:
8661 if (!check_trait_type (type1))
8662 return error_mark_node;
8663 break;
8665 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8666 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8667 if (!check_trait_type (type1)
8668 || !check_trait_type (type2))
8669 return error_mark_node;
8670 break;
8672 case CPTK_IS_BASE_OF:
8673 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8674 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
8675 && !complete_type_or_else (type2, NULL_TREE))
8676 /* We already issued an error. */
8677 return error_mark_node;
8678 break;
8680 case CPTK_IS_CLASS:
8681 case CPTK_IS_ENUM:
8682 case CPTK_IS_UNION:
8683 break;
8685 default:
8686 gcc_unreachable ();
8689 return (trait_expr_value (kind, type1, type2)
8690 ? boolean_true_node : boolean_false_node);
8693 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
8694 which is ignored for C++. */
8696 void
8697 set_float_const_decimal64 (void)
8701 void
8702 clear_float_const_decimal64 (void)
8706 bool
8707 float_const_decimal64_p (void)
8709 return 0;
8713 /* Return true if T designates the implied `this' parameter. */
8715 bool
8716 is_this_parameter (tree t)
8718 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8719 return false;
8720 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8721 return true;
8724 /* Insert the deduced return type for an auto function. */
8726 void
8727 apply_deduced_return_type (tree fco, tree return_type)
8729 tree result;
8731 if (return_type == error_mark_node)
8732 return;
8734 if (LAMBDA_FUNCTION_P (fco))
8736 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
8737 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
8740 if (DECL_CONV_FN_P (fco))
8741 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
8743 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
8745 result = DECL_RESULT (fco);
8746 if (result == NULL_TREE)
8747 return;
8748 if (TREE_TYPE (result) == return_type)
8749 return;
8751 /* We already have a DECL_RESULT from start_preparsed_function.
8752 Now we need to redo the work it and allocate_struct_function
8753 did to reflect the new type. */
8754 gcc_assert (current_function_decl == fco);
8755 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
8756 TYPE_MAIN_VARIANT (return_type));
8757 DECL_ARTIFICIAL (result) = 1;
8758 DECL_IGNORED_P (result) = 1;
8759 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
8760 result);
8762 DECL_RESULT (fco) = result;
8764 if (!processing_template_decl)
8766 if (!VOID_TYPE_P (TREE_TYPE (result)))
8767 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
8768 bool aggr = aggregate_value_p (result, fco);
8769 #ifdef PCC_STATIC_STRUCT_RETURN
8770 cfun->returns_pcc_struct = aggr;
8771 #endif
8772 cfun->returns_struct = aggr;
8777 /* DECL is a local variable or parameter from the surrounding scope of a
8778 lambda-expression. Returns the decltype for a use of the capture field
8779 for DECL even if it hasn't been captured yet. */
8781 static tree
8782 capture_decltype (tree decl)
8784 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
8785 /* FIXME do lookup instead of list walk? */
8786 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
8787 tree type;
8789 if (cap)
8790 type = TREE_TYPE (TREE_PURPOSE (cap));
8791 else
8792 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
8794 case CPLD_NONE:
8795 error ("%qD is not captured", decl);
8796 return error_mark_node;
8798 case CPLD_COPY:
8799 type = TREE_TYPE (decl);
8800 if (TREE_CODE (type) == REFERENCE_TYPE
8801 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
8802 type = TREE_TYPE (type);
8803 break;
8805 case CPLD_REFERENCE:
8806 type = TREE_TYPE (decl);
8807 if (TREE_CODE (type) != REFERENCE_TYPE)
8808 type = build_reference_type (TREE_TYPE (decl));
8809 break;
8811 default:
8812 gcc_unreachable ();
8815 if (TREE_CODE (type) != REFERENCE_TYPE)
8817 if (!LAMBDA_EXPR_MUTABLE_P (lam))
8818 type = cp_build_qualified_type (type, (cp_type_quals (type)
8819 |TYPE_QUAL_CONST));
8820 type = build_reference_type (type);
8822 return type;
8825 #include "gt-cp-semantics.h"