Daily bump.
[official-gcc.git] / gcc / cp / semantics.c
blobae409e6cd3e52fd17834aad487445824c4f04563
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-2016 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 "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-low.h"
42 #include "convert.h"
43 #include "gomp-constants.h"
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. */
50 static tree maybe_convert_cond (tree);
51 static tree finalize_nrv_r (tree *, int *, void *);
52 static tree capture_decltype (tree);
54 /* Used for OpenMP non-static data member privatization. */
56 static hash_map<tree, tree> *omp_private_member_map;
57 static vec<tree> omp_private_member_vec;
58 static bool omp_private_member_ignore_next;
61 /* Deferred Access Checking Overview
62 ---------------------------------
64 Most C++ expressions and declarations require access checking
65 to be performed during parsing. However, in several cases,
66 this has to be treated differently.
68 For member declarations, access checking has to be deferred
69 until more information about the declaration is known. For
70 example:
72 class A {
73 typedef int X;
74 public:
75 X f();
78 A::X A::f();
79 A::X g();
81 When we are parsing the function return type `A::X', we don't
82 really know if this is allowed until we parse the function name.
84 Furthermore, some contexts require that access checking is
85 never performed at all. These include class heads, and template
86 instantiations.
88 Typical use of access checking functions is described here:
90 1. When we enter a context that requires certain access checking
91 mode, the function `push_deferring_access_checks' is called with
92 DEFERRING argument specifying the desired mode. Access checking
93 may be performed immediately (dk_no_deferred), deferred
94 (dk_deferred), or not performed (dk_no_check).
96 2. When a declaration such as a type, or a variable, is encountered,
97 the function `perform_or_defer_access_check' is called. It
98 maintains a vector of all deferred checks.
100 3. The global `current_class_type' or `current_function_decl' is then
101 setup by the parser. `enforce_access' relies on these information
102 to check access.
104 4. Upon exiting the context mentioned in step 1,
105 `perform_deferred_access_checks' is called to check all declaration
106 stored in the vector. `pop_deferring_access_checks' is then
107 called to restore the previous access checking mode.
109 In case of parsing error, we simply call `pop_deferring_access_checks'
110 without `perform_deferred_access_checks'. */
112 struct GTY(()) deferred_access {
113 /* A vector representing name-lookups for which we have deferred
114 checking access controls. We cannot check the accessibility of
115 names used in a decl-specifier-seq until we know what is being
116 declared because code like:
118 class A {
119 class B {};
120 B* f();
123 A::B* A::f() { return 0; }
125 is valid, even though `A::B' is not generally accessible. */
126 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
128 /* The current mode of access checks. */
129 enum deferring_kind deferring_access_checks_kind;
133 /* Data for deferred access checking. */
134 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
135 static GTY(()) unsigned deferred_access_no_check;
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
140 void
141 push_deferring_access_checks (deferring_kind deferring)
143 /* For context like template instantiation, access checking
144 disabling applies to all nested context. */
145 if (deferred_access_no_check || deferring == dk_no_check)
146 deferred_access_no_check++;
147 else
149 deferred_access e = {NULL, deferring};
150 vec_safe_push (deferred_access_stack, e);
154 /* Save the current deferred access states and start deferred access
155 checking, continuing the set of deferred checks in CHECKS. */
157 void
158 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
160 push_deferring_access_checks (dk_deferred);
161 if (!deferred_access_no_check)
162 deferred_access_stack->last().deferred_access_checks = checks;
165 /* Resume deferring access checks again after we stopped doing
166 this previously. */
168 void
169 resume_deferring_access_checks (void)
171 if (!deferred_access_no_check)
172 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
175 /* Stop deferring access checks. */
177 void
178 stop_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
184 /* Discard the current deferred access checks and restore the
185 previous states. */
187 void
188 pop_deferring_access_checks (void)
190 if (deferred_access_no_check)
191 deferred_access_no_check--;
192 else
193 deferred_access_stack->pop ();
196 /* Returns a TREE_LIST representing the deferred checks.
197 The TREE_PURPOSE of each node is the type through which the
198 access occurred; the TREE_VALUE is the declaration named.
201 vec<deferred_access_check, va_gc> *
202 get_deferred_access_checks (void)
204 if (deferred_access_no_check)
205 return NULL;
206 else
207 return (deferred_access_stack->last().deferred_access_checks);
210 /* Take current deferred checks and combine with the
211 previous states if we also defer checks previously.
212 Otherwise perform checks now. */
214 void
215 pop_to_parent_deferring_access_checks (void)
217 if (deferred_access_no_check)
218 deferred_access_no_check--;
219 else
221 vec<deferred_access_check, va_gc> *checks;
222 deferred_access *ptr;
224 checks = (deferred_access_stack->last ().deferred_access_checks);
226 deferred_access_stack->pop ();
227 ptr = &deferred_access_stack->last ();
228 if (ptr->deferring_access_checks_kind == dk_no_deferred)
230 /* Check access. */
231 perform_access_checks (checks, tf_warning_or_error);
233 else
235 /* Merge with parent. */
236 int i, j;
237 deferred_access_check *chk, *probe;
239 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
241 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
243 if (probe->binfo == chk->binfo &&
244 probe->decl == chk->decl &&
245 probe->diag_decl == chk->diag_decl)
246 goto found;
248 /* Insert into parent's checks. */
249 vec_safe_push (ptr->deferred_access_checks, *chk);
250 found:;
256 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
257 is the BINFO indicating the qualifying scope used to access the
258 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
259 or we aren't in SFINAE context or all the checks succeed return TRUE,
260 otherwise FALSE. */
262 bool
263 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
264 tsubst_flags_t complain)
266 int i;
267 deferred_access_check *chk;
268 location_t loc = input_location;
269 bool ok = true;
271 if (!checks)
272 return true;
274 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
276 input_location = chk->loc;
277 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
280 input_location = loc;
281 return (complain & tf_error) ? true : ok;
284 /* Perform the deferred access checks.
286 After performing the checks, we still have to keep the list
287 `deferred_access_stack->deferred_access_checks' since we may want
288 to check access for them again later in a different context.
289 For example:
291 class A {
292 typedef int X;
293 static X a;
295 A::X A::a, x; // No error for `A::a', error for `x'
297 We have to perform deferred access of `A::X', first with `A::a',
298 next with `x'. Return value like perform_access_checks above. */
300 bool
301 perform_deferred_access_checks (tsubst_flags_t complain)
303 return perform_access_checks (get_deferred_access_checks (), complain);
306 /* Defer checking the accessibility of DECL, when looked up in
307 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
308 Return value like perform_access_checks above. */
310 bool
311 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
312 tsubst_flags_t complain)
314 int i;
315 deferred_access *ptr;
316 deferred_access_check *chk;
319 /* Exit if we are in a context that no access checking is performed.
321 if (deferred_access_no_check)
322 return true;
324 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
326 ptr = &deferred_access_stack->last ();
328 /* If we are not supposed to defer access checks, just check now. */
329 if (ptr->deferring_access_checks_kind == dk_no_deferred)
331 bool ok = enforce_access (binfo, decl, diag_decl, complain);
332 return (complain & tf_error) ? true : ok;
335 /* See if we are already going to perform this check. */
336 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
338 if (chk->decl == decl && chk->binfo == binfo &&
339 chk->diag_decl == diag_decl)
341 return true;
344 /* If not, record the check. */
345 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
346 vec_safe_push (ptr->deferred_access_checks, new_access);
348 return true;
351 /* Returns nonzero if the current statement is a full expression,
352 i.e. temporaries created during that statement should be destroyed
353 at the end of the statement. */
356 stmts_are_full_exprs_p (void)
358 return current_stmt_tree ()->stmts_are_full_exprs_p;
361 /* T is a statement. Add it to the statement-tree. This is the C++
362 version. The C/ObjC frontends have a slightly different version of
363 this function. */
365 tree
366 add_stmt (tree t)
368 enum tree_code code = TREE_CODE (t);
370 if (EXPR_P (t) && code != LABEL_EXPR)
372 if (!EXPR_HAS_LOCATION (t))
373 SET_EXPR_LOCATION (t, input_location);
375 /* When we expand a statement-tree, we must know whether or not the
376 statements are full-expressions. We record that fact here. */
377 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
380 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
381 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
383 /* Add T to the statement-tree. Non-side-effect statements need to be
384 recorded during statement expressions. */
385 gcc_checking_assert (!stmt_list_stack->is_empty ());
386 append_to_statement_list_force (t, &cur_stmt_list);
388 return t;
391 /* Returns the stmt_tree to which statements are currently being added. */
393 stmt_tree
394 current_stmt_tree (void)
396 return (cfun
397 ? &cfun->language->base.x_stmt_tree
398 : &scope_chain->x_stmt_tree);
401 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
403 static tree
404 maybe_cleanup_point_expr (tree expr)
406 if (!processing_template_decl && stmts_are_full_exprs_p ())
407 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
408 return expr;
411 /* Like maybe_cleanup_point_expr except have the type of the new expression be
412 void so we don't need to create a temporary variable to hold the inner
413 expression. The reason why we do this is because the original type might be
414 an aggregate and we cannot create a temporary variable for that type. */
416 tree
417 maybe_cleanup_point_expr_void (tree expr)
419 if (!processing_template_decl && stmts_are_full_exprs_p ())
420 expr = fold_build_cleanup_point_expr (void_type_node, expr);
421 return expr;
426 /* Create a declaration statement for the declaration given by the DECL. */
428 void
429 add_decl_expr (tree decl)
431 tree r = build_stmt (input_location, DECL_EXPR, decl);
432 if (DECL_INITIAL (decl)
433 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
434 r = maybe_cleanup_point_expr_void (r);
435 add_stmt (r);
438 /* Finish a scope. */
440 tree
441 do_poplevel (tree stmt_list)
443 tree block = NULL;
445 if (stmts_are_full_exprs_p ())
446 block = poplevel (kept_level_p (), 1, 0);
448 stmt_list = pop_stmt_list (stmt_list);
450 if (!processing_template_decl)
452 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
453 /* ??? See c_end_compound_stmt re statement expressions. */
456 return stmt_list;
459 /* Begin a new scope. */
461 static tree
462 do_pushlevel (scope_kind sk)
464 tree ret = push_stmt_list ();
465 if (stmts_are_full_exprs_p ())
466 begin_scope (sk, NULL);
467 return ret;
470 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
471 when the current scope is exited. EH_ONLY is true when this is not
472 meant to apply to normal control flow transfer. */
474 void
475 push_cleanup (tree decl, tree cleanup, bool eh_only)
477 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
478 CLEANUP_EH_ONLY (stmt) = eh_only;
479 add_stmt (stmt);
480 CLEANUP_BODY (stmt) = push_stmt_list ();
483 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
484 the current loops, represented by 'NULL_TREE' if we've seen a possible
485 exit, and 'error_mark_node' if not. This is currently used only to
486 suppress the warning about a function with no return statements, and
487 therefore we don't bother noting returns as possible exits. We also
488 don't bother with gotos. */
490 static void
491 begin_maybe_infinite_loop (tree cond)
493 /* Only track this while parsing a function, not during instantiation. */
494 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
495 && !processing_template_decl))
496 return;
497 bool maybe_infinite = true;
498 if (cond)
500 cond = fold_non_dependent_expr (cond);
501 maybe_infinite = integer_nonzerop (cond);
503 vec_safe_push (cp_function_chain->infinite_loops,
504 maybe_infinite ? error_mark_node : NULL_TREE);
508 /* A break is a possible exit for the current loop. */
510 void
511 break_maybe_infinite_loop (void)
513 if (!cfun)
514 return;
515 cp_function_chain->infinite_loops->last() = NULL_TREE;
518 /* If we reach the end of the loop without seeing a possible exit, we have
519 an infinite loop. */
521 static void
522 end_maybe_infinite_loop (tree cond)
524 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
525 && !processing_template_decl))
526 return;
527 tree current = cp_function_chain->infinite_loops->pop();
528 if (current != NULL_TREE)
530 cond = fold_non_dependent_expr (cond);
531 if (integer_nonzerop (cond))
532 current_function_infinite_loop = 1;
537 /* Begin a conditional that might contain a declaration. When generating
538 normal code, we want the declaration to appear before the statement
539 containing the conditional. When generating template code, we want the
540 conditional to be rendered as the raw DECL_EXPR. */
542 static void
543 begin_cond (tree *cond_p)
545 if (processing_template_decl)
546 *cond_p = push_stmt_list ();
549 /* Finish such a conditional. */
551 static void
552 finish_cond (tree *cond_p, tree expr)
554 if (processing_template_decl)
556 tree cond = pop_stmt_list (*cond_p);
558 if (expr == NULL_TREE)
559 /* Empty condition in 'for'. */
560 gcc_assert (empty_expr_stmt_p (cond));
561 else if (check_for_bare_parameter_packs (expr))
562 expr = error_mark_node;
563 else if (!empty_expr_stmt_p (cond))
564 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
566 *cond_p = expr;
569 /* If *COND_P specifies a conditional with a declaration, transform the
570 loop such that
571 while (A x = 42) { }
572 for (; A x = 42;) { }
573 becomes
574 while (true) { A x = 42; if (!x) break; }
575 for (;;) { A x = 42; if (!x) break; }
576 The statement list for BODY will be empty if the conditional did
577 not declare anything. */
579 static void
580 simplify_loop_decl_cond (tree *cond_p, tree body)
582 tree cond, if_stmt;
584 if (!TREE_SIDE_EFFECTS (body))
585 return;
587 cond = *cond_p;
588 *cond_p = boolean_true_node;
590 if_stmt = begin_if_stmt ();
591 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
592 finish_if_stmt_cond (cond, if_stmt);
593 finish_break_stmt ();
594 finish_then_clause (if_stmt);
595 finish_if_stmt (if_stmt);
598 /* Finish a goto-statement. */
600 tree
601 finish_goto_stmt (tree destination)
603 if (identifier_p (destination))
604 destination = lookup_label (destination);
606 /* We warn about unused labels with -Wunused. That means we have to
607 mark the used labels as used. */
608 if (TREE_CODE (destination) == LABEL_DECL)
609 TREE_USED (destination) = 1;
610 else
612 if (check_no_cilk (destination,
613 "Cilk array notation cannot be used as a computed goto expression",
614 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
615 destination = error_mark_node;
616 destination = mark_rvalue_use (destination);
617 if (!processing_template_decl)
619 destination = cp_convert (ptr_type_node, destination,
620 tf_warning_or_error);
621 if (error_operand_p (destination))
622 return NULL_TREE;
623 destination
624 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
625 destination);
629 check_goto (destination);
631 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
634 /* COND is the condition-expression for an if, while, etc.,
635 statement. Convert it to a boolean value, if appropriate.
636 In addition, verify sequence points if -Wsequence-point is enabled. */
638 static tree
639 maybe_convert_cond (tree cond)
641 /* Empty conditions remain empty. */
642 if (!cond)
643 return NULL_TREE;
645 /* Wait until we instantiate templates before doing conversion. */
646 if (processing_template_decl)
647 return cond;
649 if (warn_sequence_point)
650 verify_sequence_points (cond);
652 /* Do the conversion. */
653 cond = convert_from_reference (cond);
655 if (TREE_CODE (cond) == MODIFY_EXPR
656 && !TREE_NO_WARNING (cond)
657 && warn_parentheses)
659 warning (OPT_Wparentheses,
660 "suggest parentheses around assignment used as truth value");
661 TREE_NO_WARNING (cond) = 1;
664 return condition_conversion (cond);
667 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
669 tree
670 finish_expr_stmt (tree expr)
672 tree r = NULL_TREE;
674 if (expr != NULL_TREE)
676 /* If we ran into a problem, make sure we complained. */
677 gcc_assert (expr != error_mark_node || seen_error ());
679 if (!processing_template_decl)
681 if (warn_sequence_point)
682 verify_sequence_points (expr);
683 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
685 else if (!type_dependent_expression_p (expr))
686 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
687 tf_warning_or_error);
689 if (check_for_bare_parameter_packs (expr))
690 expr = error_mark_node;
692 /* Simplification of inner statement expressions, compound exprs,
693 etc can result in us already having an EXPR_STMT. */
694 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
696 if (TREE_CODE (expr) != EXPR_STMT)
697 expr = build_stmt (input_location, EXPR_STMT, expr);
698 expr = maybe_cleanup_point_expr_void (expr);
701 r = add_stmt (expr);
704 return r;
708 /* Begin an if-statement. Returns a newly created IF_STMT if
709 appropriate. */
711 tree
712 begin_if_stmt (void)
714 tree r, scope;
715 scope = do_pushlevel (sk_cond);
716 r = build_stmt (input_location, IF_STMT, NULL_TREE,
717 NULL_TREE, NULL_TREE, scope);
718 begin_cond (&IF_COND (r));
719 return r;
722 /* Process the COND of an if-statement, which may be given by
723 IF_STMT. */
725 void
726 finish_if_stmt_cond (tree cond, tree if_stmt)
728 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
729 add_stmt (if_stmt);
730 THEN_CLAUSE (if_stmt) = push_stmt_list ();
733 /* Finish the then-clause of an if-statement, which may be given by
734 IF_STMT. */
736 tree
737 finish_then_clause (tree if_stmt)
739 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
740 return if_stmt;
743 /* Begin the else-clause of an if-statement. */
745 void
746 begin_else_clause (tree if_stmt)
748 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
751 /* Finish the else-clause of an if-statement, which may be given by
752 IF_STMT. */
754 void
755 finish_else_clause (tree if_stmt)
757 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
760 /* Finish an if-statement. */
762 void
763 finish_if_stmt (tree if_stmt)
765 tree scope = IF_SCOPE (if_stmt);
766 IF_SCOPE (if_stmt) = NULL;
767 add_stmt (do_poplevel (scope));
770 /* Begin a while-statement. Returns a newly created WHILE_STMT if
771 appropriate. */
773 tree
774 begin_while_stmt (void)
776 tree r;
777 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
778 add_stmt (r);
779 WHILE_BODY (r) = do_pushlevel (sk_block);
780 begin_cond (&WHILE_COND (r));
781 return r;
784 /* Process the COND of a while-statement, which may be given by
785 WHILE_STMT. */
787 void
788 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
790 if (check_no_cilk (cond,
791 "Cilk array notation cannot be used as a condition for while statement",
792 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
793 cond = error_mark_node;
794 cond = maybe_convert_cond (cond);
795 finish_cond (&WHILE_COND (while_stmt), cond);
796 begin_maybe_infinite_loop (cond);
797 if (ivdep && cond != error_mark_node)
798 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
799 TREE_TYPE (WHILE_COND (while_stmt)),
800 WHILE_COND (while_stmt),
801 build_int_cst (integer_type_node,
802 annot_expr_ivdep_kind));
803 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
806 /* Finish a while-statement, which may be given by WHILE_STMT. */
808 void
809 finish_while_stmt (tree while_stmt)
811 end_maybe_infinite_loop (boolean_true_node);
812 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
815 /* Begin a do-statement. Returns a newly created DO_STMT if
816 appropriate. */
818 tree
819 begin_do_stmt (void)
821 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
822 begin_maybe_infinite_loop (boolean_true_node);
823 add_stmt (r);
824 DO_BODY (r) = push_stmt_list ();
825 return r;
828 /* Finish the body of a do-statement, which may be given by DO_STMT. */
830 void
831 finish_do_body (tree do_stmt)
833 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
835 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
836 body = STATEMENT_LIST_TAIL (body)->stmt;
838 if (IS_EMPTY_STMT (body))
839 warning (OPT_Wempty_body,
840 "suggest explicit braces around empty body in %<do%> statement");
843 /* Finish a do-statement, which may be given by DO_STMT, and whose
844 COND is as indicated. */
846 void
847 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
849 if (check_no_cilk (cond,
850 "Cilk array notation cannot be used as a condition for a do-while statement",
851 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
852 cond = error_mark_node;
853 cond = maybe_convert_cond (cond);
854 end_maybe_infinite_loop (cond);
855 if (ivdep && cond != error_mark_node)
856 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
857 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
858 DO_COND (do_stmt) = cond;
861 /* Finish a return-statement. The EXPRESSION returned, if any, is as
862 indicated. */
864 tree
865 finish_return_stmt (tree expr)
867 tree r;
868 bool no_warning;
870 expr = check_return_expr (expr, &no_warning);
872 if (error_operand_p (expr)
873 || (flag_openmp && !check_omp_return ()))
875 /* Suppress -Wreturn-type for this function. */
876 if (warn_return_type)
877 TREE_NO_WARNING (current_function_decl) = true;
878 return error_mark_node;
881 if (!processing_template_decl)
883 if (warn_sequence_point)
884 verify_sequence_points (expr);
886 if (DECL_DESTRUCTOR_P (current_function_decl)
887 || (DECL_CONSTRUCTOR_P (current_function_decl)
888 && targetm.cxx.cdtor_returns_this ()))
890 /* Similarly, all destructors must run destructors for
891 base-classes before returning. So, all returns in a
892 destructor get sent to the DTOR_LABEL; finish_function emits
893 code to return a value there. */
894 return finish_goto_stmt (cdtor_label);
898 r = build_stmt (input_location, RETURN_EXPR, expr);
899 TREE_NO_WARNING (r) |= no_warning;
900 r = maybe_cleanup_point_expr_void (r);
901 r = add_stmt (r);
903 return r;
906 /* Begin the scope of a for-statement or a range-for-statement.
907 Both the returned trees are to be used in a call to
908 begin_for_stmt or begin_range_for_stmt. */
910 tree
911 begin_for_scope (tree *init)
913 tree scope = NULL_TREE;
914 if (flag_new_for_scope > 0)
915 scope = do_pushlevel (sk_for);
917 if (processing_template_decl)
918 *init = push_stmt_list ();
919 else
920 *init = NULL_TREE;
922 return scope;
925 /* Begin a for-statement. Returns a new FOR_STMT.
926 SCOPE and INIT should be the return of begin_for_scope,
927 or both NULL_TREE */
929 tree
930 begin_for_stmt (tree scope, tree init)
932 tree r;
934 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
935 NULL_TREE, NULL_TREE, NULL_TREE);
937 if (scope == NULL_TREE)
939 gcc_assert (!init || !(flag_new_for_scope > 0));
940 if (!init)
941 scope = begin_for_scope (&init);
943 FOR_INIT_STMT (r) = init;
944 FOR_SCOPE (r) = scope;
946 return r;
949 /* Finish the for-init-statement of a for-statement, which may be
950 given by FOR_STMT. */
952 void
953 finish_for_init_stmt (tree for_stmt)
955 if (processing_template_decl)
956 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
957 add_stmt (for_stmt);
958 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
959 begin_cond (&FOR_COND (for_stmt));
962 /* Finish the COND of a for-statement, which may be given by
963 FOR_STMT. */
965 void
966 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
968 if (check_no_cilk (cond,
969 "Cilk array notation cannot be used in a condition for a for-loop",
970 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
971 cond = error_mark_node;
972 cond = maybe_convert_cond (cond);
973 finish_cond (&FOR_COND (for_stmt), cond);
974 begin_maybe_infinite_loop (cond);
975 if (ivdep && cond != error_mark_node)
976 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
977 TREE_TYPE (FOR_COND (for_stmt)),
978 FOR_COND (for_stmt),
979 build_int_cst (integer_type_node,
980 annot_expr_ivdep_kind));
981 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
984 /* Finish the increment-EXPRESSION in a for-statement, which may be
985 given by FOR_STMT. */
987 void
988 finish_for_expr (tree expr, tree for_stmt)
990 if (!expr)
991 return;
992 /* If EXPR is an overloaded function, issue an error; there is no
993 context available to use to perform overload resolution. */
994 if (type_unknown_p (expr))
996 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
997 expr = error_mark_node;
999 if (!processing_template_decl)
1001 if (warn_sequence_point)
1002 verify_sequence_points (expr);
1003 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1004 tf_warning_or_error);
1006 else if (!type_dependent_expression_p (expr))
1007 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1008 tf_warning_or_error);
1009 expr = maybe_cleanup_point_expr_void (expr);
1010 if (check_for_bare_parameter_packs (expr))
1011 expr = error_mark_node;
1012 FOR_EXPR (for_stmt) = expr;
1015 /* Finish the body of a for-statement, which may be given by
1016 FOR_STMT. The increment-EXPR for the loop must be
1017 provided.
1018 It can also finish RANGE_FOR_STMT. */
1020 void
1021 finish_for_stmt (tree for_stmt)
1023 end_maybe_infinite_loop (boolean_true_node);
1025 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1026 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1027 else
1028 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1030 /* Pop the scope for the body of the loop. */
1031 if (flag_new_for_scope > 0)
1033 tree scope;
1034 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1035 ? &RANGE_FOR_SCOPE (for_stmt)
1036 : &FOR_SCOPE (for_stmt));
1037 scope = *scope_ptr;
1038 *scope_ptr = NULL;
1039 add_stmt (do_poplevel (scope));
1043 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1044 SCOPE and INIT should be the return of begin_for_scope,
1045 or both NULL_TREE .
1046 To finish it call finish_for_stmt(). */
1048 tree
1049 begin_range_for_stmt (tree scope, tree init)
1051 tree r;
1053 begin_maybe_infinite_loop (boolean_false_node);
1055 r = build_stmt (input_location, RANGE_FOR_STMT,
1056 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1058 if (scope == NULL_TREE)
1060 gcc_assert (!init || !(flag_new_for_scope > 0));
1061 if (!init)
1062 scope = begin_for_scope (&init);
1065 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1066 pop it now. */
1067 if (init)
1068 pop_stmt_list (init);
1069 RANGE_FOR_SCOPE (r) = scope;
1071 return r;
1074 /* Finish the head of a range-based for statement, which may
1075 be given by RANGE_FOR_STMT. DECL must be the declaration
1076 and EXPR must be the loop expression. */
1078 void
1079 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1081 RANGE_FOR_DECL (range_for_stmt) = decl;
1082 RANGE_FOR_EXPR (range_for_stmt) = expr;
1083 add_stmt (range_for_stmt);
1084 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1087 /* Finish a break-statement. */
1089 tree
1090 finish_break_stmt (void)
1092 /* In switch statements break is sometimes stylistically used after
1093 a return statement. This can lead to spurious warnings about
1094 control reaching the end of a non-void function when it is
1095 inlined. Note that we are calling block_may_fallthru with
1096 language specific tree nodes; this works because
1097 block_may_fallthru returns true when given something it does not
1098 understand. */
1099 if (!block_may_fallthru (cur_stmt_list))
1100 return void_node;
1101 return add_stmt (build_stmt (input_location, BREAK_STMT));
1104 /* Finish a continue-statement. */
1106 tree
1107 finish_continue_stmt (void)
1109 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1112 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1113 appropriate. */
1115 tree
1116 begin_switch_stmt (void)
1118 tree r, scope;
1120 scope = do_pushlevel (sk_cond);
1121 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1123 begin_cond (&SWITCH_STMT_COND (r));
1125 return r;
1128 /* Finish the cond of a switch-statement. */
1130 void
1131 finish_switch_cond (tree cond, tree switch_stmt)
1133 tree orig_type = NULL;
1135 if (check_no_cilk (cond,
1136 "Cilk array notation cannot be used as a condition for switch statement",
1137 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1138 cond = error_mark_node;
1140 if (!processing_template_decl)
1142 /* Convert the condition to an integer or enumeration type. */
1143 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1144 if (cond == NULL_TREE)
1146 error ("switch quantity not an integer");
1147 cond = error_mark_node;
1149 /* We want unlowered type here to handle enum bit-fields. */
1150 orig_type = unlowered_expr_type (cond);
1151 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1152 orig_type = TREE_TYPE (cond);
1153 if (cond != error_mark_node)
1155 /* [stmt.switch]
1157 Integral promotions are performed. */
1158 cond = perform_integral_promotions (cond);
1159 cond = maybe_cleanup_point_expr (cond);
1162 if (check_for_bare_parameter_packs (cond))
1163 cond = error_mark_node;
1164 else if (!processing_template_decl && warn_sequence_point)
1165 verify_sequence_points (cond);
1167 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1168 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1169 add_stmt (switch_stmt);
1170 push_switch (switch_stmt);
1171 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1174 /* Finish the body of a switch-statement, which may be given by
1175 SWITCH_STMT. The COND to switch on is indicated. */
1177 void
1178 finish_switch_stmt (tree switch_stmt)
1180 tree scope;
1182 SWITCH_STMT_BODY (switch_stmt) =
1183 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1184 pop_switch ();
1186 scope = SWITCH_STMT_SCOPE (switch_stmt);
1187 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1188 add_stmt (do_poplevel (scope));
1191 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1192 appropriate. */
1194 tree
1195 begin_try_block (void)
1197 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1198 add_stmt (r);
1199 TRY_STMTS (r) = push_stmt_list ();
1200 return r;
1203 /* Likewise, for a function-try-block. The block returned in
1204 *COMPOUND_STMT is an artificial outer scope, containing the
1205 function-try-block. */
1207 tree
1208 begin_function_try_block (tree *compound_stmt)
1210 tree r;
1211 /* This outer scope does not exist in the C++ standard, but we need
1212 a place to put __FUNCTION__ and similar variables. */
1213 *compound_stmt = begin_compound_stmt (0);
1214 r = begin_try_block ();
1215 FN_TRY_BLOCK_P (r) = 1;
1216 return r;
1219 /* Finish a try-block, which may be given by TRY_BLOCK. */
1221 void
1222 finish_try_block (tree try_block)
1224 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1225 TRY_HANDLERS (try_block) = push_stmt_list ();
1228 /* Finish the body of a cleanup try-block, which may be given by
1229 TRY_BLOCK. */
1231 void
1232 finish_cleanup_try_block (tree try_block)
1234 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1237 /* Finish an implicitly generated try-block, with a cleanup is given
1238 by CLEANUP. */
1240 void
1241 finish_cleanup (tree cleanup, tree try_block)
1243 TRY_HANDLERS (try_block) = cleanup;
1244 CLEANUP_P (try_block) = 1;
1247 /* Likewise, for a function-try-block. */
1249 void
1250 finish_function_try_block (tree try_block)
1252 finish_try_block (try_block);
1253 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1254 the try block, but moving it inside. */
1255 in_function_try_handler = 1;
1258 /* Finish a handler-sequence for a try-block, which may be given by
1259 TRY_BLOCK. */
1261 void
1262 finish_handler_sequence (tree try_block)
1264 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1265 check_handlers (TRY_HANDLERS (try_block));
1268 /* Finish the handler-seq for a function-try-block, given by
1269 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1270 begin_function_try_block. */
1272 void
1273 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1275 in_function_try_handler = 0;
1276 finish_handler_sequence (try_block);
1277 finish_compound_stmt (compound_stmt);
1280 /* Begin a handler. Returns a HANDLER if appropriate. */
1282 tree
1283 begin_handler (void)
1285 tree r;
1287 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1288 add_stmt (r);
1290 /* Create a binding level for the eh_info and the exception object
1291 cleanup. */
1292 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1294 return r;
1297 /* Finish the handler-parameters for a handler, which may be given by
1298 HANDLER. DECL is the declaration for the catch parameter, or NULL
1299 if this is a `catch (...)' clause. */
1301 void
1302 finish_handler_parms (tree decl, tree handler)
1304 tree type = NULL_TREE;
1305 if (processing_template_decl)
1307 if (decl)
1309 decl = pushdecl (decl);
1310 decl = push_template_decl (decl);
1311 HANDLER_PARMS (handler) = decl;
1312 type = TREE_TYPE (decl);
1315 else
1316 type = expand_start_catch_block (decl);
1317 HANDLER_TYPE (handler) = type;
1320 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1321 the return value from the matching call to finish_handler_parms. */
1323 void
1324 finish_handler (tree handler)
1326 if (!processing_template_decl)
1327 expand_end_catch_block ();
1328 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1331 /* Begin a compound statement. FLAGS contains some bits that control the
1332 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1333 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1334 block of a function. If BCS_TRY_BLOCK is set, this is the block
1335 created on behalf of a TRY statement. Returns a token to be passed to
1336 finish_compound_stmt. */
1338 tree
1339 begin_compound_stmt (unsigned int flags)
1341 tree r;
1343 if (flags & BCS_NO_SCOPE)
1345 r = push_stmt_list ();
1346 STATEMENT_LIST_NO_SCOPE (r) = 1;
1348 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1349 But, if it's a statement-expression with a scopeless block, there's
1350 nothing to keep, and we don't want to accidentally keep a block
1351 *inside* the scopeless block. */
1352 keep_next_level (false);
1354 else
1356 scope_kind sk = sk_block;
1357 if (flags & BCS_TRY_BLOCK)
1358 sk = sk_try;
1359 else if (flags & BCS_TRANSACTION)
1360 sk = sk_transaction;
1361 r = do_pushlevel (sk);
1364 /* When processing a template, we need to remember where the braces were,
1365 so that we can set up identical scopes when instantiating the template
1366 later. BIND_EXPR is a handy candidate for this.
1367 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1368 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1369 processing templates. */
1370 if (processing_template_decl)
1372 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1373 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1374 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1375 TREE_SIDE_EFFECTS (r) = 1;
1378 return r;
1381 /* Finish a compound-statement, which is given by STMT. */
1383 void
1384 finish_compound_stmt (tree stmt)
1386 if (TREE_CODE (stmt) == BIND_EXPR)
1388 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1389 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1390 discard the BIND_EXPR so it can be merged with the containing
1391 STATEMENT_LIST. */
1392 if (TREE_CODE (body) == STATEMENT_LIST
1393 && STATEMENT_LIST_HEAD (body) == NULL
1394 && !BIND_EXPR_BODY_BLOCK (stmt)
1395 && !BIND_EXPR_TRY_BLOCK (stmt))
1396 stmt = body;
1397 else
1398 BIND_EXPR_BODY (stmt) = body;
1400 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1401 stmt = pop_stmt_list (stmt);
1402 else
1404 /* Destroy any ObjC "super" receivers that may have been
1405 created. */
1406 objc_clear_super_receiver ();
1408 stmt = do_poplevel (stmt);
1411 /* ??? See c_end_compound_stmt wrt statement expressions. */
1412 add_stmt (stmt);
1415 /* Finish an asm-statement, whose components are a STRING, some
1416 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1417 LABELS. Also note whether the asm-statement should be
1418 considered volatile. */
1420 tree
1421 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1422 tree input_operands, tree clobbers, tree labels)
1424 tree r;
1425 tree t;
1426 int ninputs = list_length (input_operands);
1427 int noutputs = list_length (output_operands);
1429 if (!processing_template_decl)
1431 const char *constraint;
1432 const char **oconstraints;
1433 bool allows_mem, allows_reg, is_inout;
1434 tree operand;
1435 int i;
1437 oconstraints = XALLOCAVEC (const char *, noutputs);
1439 string = resolve_asm_operand_names (string, output_operands,
1440 input_operands, labels);
1442 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1444 operand = TREE_VALUE (t);
1446 /* ??? Really, this should not be here. Users should be using a
1447 proper lvalue, dammit. But there's a long history of using
1448 casts in the output operands. In cases like longlong.h, this
1449 becomes a primitive form of typechecking -- if the cast can be
1450 removed, then the output operand had a type of the proper width;
1451 otherwise we'll get an error. Gross, but ... */
1452 STRIP_NOPS (operand);
1454 operand = mark_lvalue_use (operand);
1456 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1457 operand = error_mark_node;
1459 if (operand != error_mark_node
1460 && (TREE_READONLY (operand)
1461 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1462 /* Functions are not modifiable, even though they are
1463 lvalues. */
1464 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1465 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1466 /* If it's an aggregate and any field is const, then it is
1467 effectively const. */
1468 || (CLASS_TYPE_P (TREE_TYPE (operand))
1469 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1470 cxx_readonly_error (operand, lv_asm);
1472 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1473 oconstraints[i] = constraint;
1475 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1476 &allows_mem, &allows_reg, &is_inout))
1478 /* If the operand is going to end up in memory,
1479 mark it addressable. */
1480 if (!allows_reg && !cxx_mark_addressable (operand))
1481 operand = error_mark_node;
1483 else
1484 operand = error_mark_node;
1486 TREE_VALUE (t) = operand;
1489 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1491 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1492 bool constraint_parsed
1493 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1494 oconstraints, &allows_mem, &allows_reg);
1495 /* If the operand is going to end up in memory, don't call
1496 decay_conversion. */
1497 if (constraint_parsed && !allows_reg && allows_mem)
1498 operand = mark_lvalue_use (TREE_VALUE (t));
1499 else
1500 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1502 /* If the type of the operand hasn't been determined (e.g.,
1503 because it involves an overloaded function), then issue
1504 an error message. There's no context available to
1505 resolve the overloading. */
1506 if (TREE_TYPE (operand) == unknown_type_node)
1508 error ("type of asm operand %qE could not be determined",
1509 TREE_VALUE (t));
1510 operand = error_mark_node;
1513 if (constraint_parsed)
1515 /* If the operand is going to end up in memory,
1516 mark it addressable. */
1517 if (!allows_reg && allows_mem)
1519 /* Strip the nops as we allow this case. FIXME, this really
1520 should be rejected or made deprecated. */
1521 STRIP_NOPS (operand);
1522 if (!cxx_mark_addressable (operand))
1523 operand = error_mark_node;
1525 else if (!allows_reg && !allows_mem)
1527 /* If constraint allows neither register nor memory,
1528 try harder to get a constant. */
1529 tree constop = maybe_constant_value (operand);
1530 if (TREE_CONSTANT (constop))
1531 operand = constop;
1534 else
1535 operand = error_mark_node;
1537 TREE_VALUE (t) = operand;
1541 r = build_stmt (input_location, ASM_EXPR, string,
1542 output_operands, input_operands,
1543 clobbers, labels);
1544 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1545 r = maybe_cleanup_point_expr_void (r);
1546 return add_stmt (r);
1549 /* Finish a label with the indicated NAME. Returns the new label. */
1551 tree
1552 finish_label_stmt (tree name)
1554 tree decl = define_label (input_location, name);
1556 if (decl == error_mark_node)
1557 return error_mark_node;
1559 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1561 return decl;
1564 /* Finish a series of declarations for local labels. G++ allows users
1565 to declare "local" labels, i.e., labels with scope. This extension
1566 is useful when writing code involving statement-expressions. */
1568 void
1569 finish_label_decl (tree name)
1571 if (!at_function_scope_p ())
1573 error ("__label__ declarations are only allowed in function scopes");
1574 return;
1577 add_decl_expr (declare_local_label (name));
1580 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1582 void
1583 finish_decl_cleanup (tree decl, tree cleanup)
1585 push_cleanup (decl, cleanup, false);
1588 /* If the current scope exits with an exception, run CLEANUP. */
1590 void
1591 finish_eh_cleanup (tree cleanup)
1593 push_cleanup (NULL, cleanup, true);
1596 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1597 order they were written by the user. Each node is as for
1598 emit_mem_initializers. */
1600 void
1601 finish_mem_initializers (tree mem_inits)
1603 /* Reorder the MEM_INITS so that they are in the order they appeared
1604 in the source program. */
1605 mem_inits = nreverse (mem_inits);
1607 if (processing_template_decl)
1609 tree mem;
1611 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1613 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1614 check for bare parameter packs in the TREE_VALUE, because
1615 any parameter packs in the TREE_VALUE have already been
1616 bound as part of the TREE_PURPOSE. See
1617 make_pack_expansion for more information. */
1618 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1619 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1620 TREE_VALUE (mem) = error_mark_node;
1623 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1624 CTOR_INITIALIZER, mem_inits));
1626 else
1627 emit_mem_initializers (mem_inits);
1630 /* Obfuscate EXPR if it looks like an id-expression or member access so
1631 that the call to finish_decltype in do_auto_deduction will give the
1632 right result. */
1634 tree
1635 force_paren_expr (tree expr)
1637 /* This is only needed for decltype(auto) in C++14. */
1638 if (cxx_dialect < cxx14)
1639 return expr;
1641 /* If we're in unevaluated context, we can't be deducing a
1642 return/initializer type, so we don't need to mess with this. */
1643 if (cp_unevaluated_operand)
1644 return expr;
1646 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1647 && TREE_CODE (expr) != SCOPE_REF)
1648 return expr;
1650 if (TREE_CODE (expr) == COMPONENT_REF)
1651 REF_PARENTHESIZED_P (expr) = true;
1652 else if (type_dependent_expression_p (expr)
1653 /* When processing_template_decl, a SCOPE_REF may actually be
1654 referring to a non-static data member of the current class, in
1655 which case its TREE_TYPE may not be properly cv-qualified (the
1656 cv-qualifiers of the implicit *this object haven't yet been taken
1657 into account) so we have to delay building a static_cast until
1658 instantiation. */
1659 || (processing_template_decl
1660 && TREE_CODE (expr) == SCOPE_REF))
1661 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1662 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1663 /* We can't bind a hard register variable to a reference. */;
1664 else
1666 cp_lvalue_kind kind = lvalue_kind (expr);
1667 if ((kind & ~clk_class) != clk_none)
1669 tree type = unlowered_expr_type (expr);
1670 bool rval = !!(kind & clk_rvalueref);
1671 type = cp_build_reference_type (type, rval);
1672 /* This inhibits warnings in, eg, cxx_mark_addressable
1673 (c++/60955). */
1674 warning_sentinel s (extra_warnings);
1675 expr = build_static_cast (type, expr, tf_error);
1676 if (expr != error_mark_node)
1677 REF_PARENTHESIZED_P (expr) = true;
1681 return expr;
1684 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1685 obfuscation and return the underlying id-expression. Otherwise
1686 return T. */
1688 tree
1689 maybe_undo_parenthesized_ref (tree t)
1691 if (cxx_dialect >= cxx14
1692 && INDIRECT_REF_P (t)
1693 && REF_PARENTHESIZED_P (t))
1695 t = TREE_OPERAND (t, 0);
1696 while (TREE_CODE (t) == NON_LVALUE_EXPR
1697 || TREE_CODE (t) == NOP_EXPR)
1698 t = TREE_OPERAND (t, 0);
1700 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1701 || TREE_CODE (t) == STATIC_CAST_EXPR);
1702 t = TREE_OPERAND (t, 0);
1705 return t;
1708 /* Finish a parenthesized expression EXPR. */
1710 cp_expr
1711 finish_parenthesized_expr (cp_expr expr)
1713 if (EXPR_P (expr))
1714 /* This inhibits warnings in c_common_truthvalue_conversion. */
1715 TREE_NO_WARNING (expr) = 1;
1717 if (TREE_CODE (expr) == OFFSET_REF
1718 || TREE_CODE (expr) == SCOPE_REF)
1719 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1720 enclosed in parentheses. */
1721 PTRMEM_OK_P (expr) = 0;
1723 if (TREE_CODE (expr) == STRING_CST)
1724 PAREN_STRING_LITERAL_P (expr) = 1;
1726 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1728 return expr;
1731 /* Finish a reference to a non-static data member (DECL) that is not
1732 preceded by `.' or `->'. */
1734 tree
1735 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1737 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1738 bool try_omp_private = !object && omp_private_member_map;
1739 tree ret;
1741 if (!object)
1743 tree scope = qualifying_scope;
1744 if (scope == NULL_TREE)
1745 scope = context_for_name_lookup (decl);
1746 object = maybe_dummy_object (scope, NULL);
1749 object = maybe_resolve_dummy (object, true);
1750 if (object == error_mark_node)
1751 return error_mark_node;
1753 /* DR 613/850: Can use non-static data members without an associated
1754 object in sizeof/decltype/alignof. */
1755 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1756 && (!processing_template_decl || !current_class_ref))
1758 if (current_function_decl
1759 && DECL_STATIC_FUNCTION_P (current_function_decl))
1760 error ("invalid use of member %qD in static member function", decl);
1761 else
1762 error ("invalid use of non-static data member %qD", decl);
1763 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1765 return error_mark_node;
1768 if (current_class_ptr)
1769 TREE_USED (current_class_ptr) = 1;
1770 if (processing_template_decl && !qualifying_scope)
1772 tree type = TREE_TYPE (decl);
1774 if (TREE_CODE (type) == REFERENCE_TYPE)
1775 /* Quals on the object don't matter. */;
1776 else if (PACK_EXPANSION_P (type))
1777 /* Don't bother trying to represent this. */
1778 type = NULL_TREE;
1779 else
1781 /* Set the cv qualifiers. */
1782 int quals = cp_type_quals (TREE_TYPE (object));
1784 if (DECL_MUTABLE_P (decl))
1785 quals &= ~TYPE_QUAL_CONST;
1787 quals |= cp_type_quals (TREE_TYPE (decl));
1788 type = cp_build_qualified_type (type, quals);
1791 ret = (convert_from_reference
1792 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1794 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1795 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1796 for now. */
1797 else if (processing_template_decl)
1798 ret = build_qualified_name (TREE_TYPE (decl),
1799 qualifying_scope,
1800 decl,
1801 /*template_p=*/false);
1802 else
1804 tree access_type = TREE_TYPE (object);
1806 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1807 decl, tf_warning_or_error);
1809 /* If the data member was named `C::M', convert `*this' to `C'
1810 first. */
1811 if (qualifying_scope)
1813 tree binfo = NULL_TREE;
1814 object = build_scoped_ref (object, qualifying_scope,
1815 &binfo);
1818 ret = build_class_member_access_expr (object, decl,
1819 /*access_path=*/NULL_TREE,
1820 /*preserve_reference=*/false,
1821 tf_warning_or_error);
1823 if (try_omp_private)
1825 tree *v = omp_private_member_map->get (decl);
1826 if (v)
1827 ret = convert_from_reference (*v);
1829 return ret;
1832 /* If we are currently parsing a template and we encountered a typedef
1833 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1834 adds the typedef to a list tied to the current template.
1835 At template instantiation time, that list is walked and access check
1836 performed for each typedef.
1837 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1839 void
1840 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1841 tree context,
1842 location_t location)
1844 tree template_info = NULL;
1845 tree cs = current_scope ();
1847 if (!is_typedef_decl (typedef_decl)
1848 || !context
1849 || !CLASS_TYPE_P (context)
1850 || !cs)
1851 return;
1853 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1854 template_info = get_template_info (cs);
1856 if (template_info
1857 && TI_TEMPLATE (template_info)
1858 && !currently_open_class (context))
1859 append_type_to_template_for_access_check (cs, typedef_decl,
1860 context, location);
1863 /* DECL was the declaration to which a qualified-id resolved. Issue
1864 an error message if it is not accessible. If OBJECT_TYPE is
1865 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1866 type of `*x', or `x', respectively. If the DECL was named as
1867 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1869 void
1870 check_accessibility_of_qualified_id (tree decl,
1871 tree object_type,
1872 tree nested_name_specifier)
1874 tree scope;
1875 tree qualifying_type = NULL_TREE;
1877 /* If we are parsing a template declaration and if decl is a typedef,
1878 add it to a list tied to the template.
1879 At template instantiation time, that list will be walked and
1880 access check performed. */
1881 add_typedef_to_current_template_for_access_check (decl,
1882 nested_name_specifier
1883 ? nested_name_specifier
1884 : DECL_CONTEXT (decl),
1885 input_location);
1887 /* If we're not checking, return immediately. */
1888 if (deferred_access_no_check)
1889 return;
1891 /* Determine the SCOPE of DECL. */
1892 scope = context_for_name_lookup (decl);
1893 /* If the SCOPE is not a type, then DECL is not a member. */
1894 if (!TYPE_P (scope))
1895 return;
1896 /* Compute the scope through which DECL is being accessed. */
1897 if (object_type
1898 /* OBJECT_TYPE might not be a class type; consider:
1900 class A { typedef int I; };
1901 I *p;
1902 p->A::I::~I();
1904 In this case, we will have "A::I" as the DECL, but "I" as the
1905 OBJECT_TYPE. */
1906 && CLASS_TYPE_P (object_type)
1907 && DERIVED_FROM_P (scope, object_type))
1908 /* If we are processing a `->' or `.' expression, use the type of the
1909 left-hand side. */
1910 qualifying_type = object_type;
1911 else if (nested_name_specifier)
1913 /* If the reference is to a non-static member of the
1914 current class, treat it as if it were referenced through
1915 `this'. */
1916 tree ct;
1917 if (DECL_NONSTATIC_MEMBER_P (decl)
1918 && current_class_ptr
1919 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1920 qualifying_type = ct;
1921 /* Otherwise, use the type indicated by the
1922 nested-name-specifier. */
1923 else
1924 qualifying_type = nested_name_specifier;
1926 else
1927 /* Otherwise, the name must be from the current class or one of
1928 its bases. */
1929 qualifying_type = currently_open_derived_class (scope);
1931 if (qualifying_type
1932 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1933 or similar in a default argument value. */
1934 && CLASS_TYPE_P (qualifying_type)
1935 && !dependent_type_p (qualifying_type))
1936 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1937 decl, tf_warning_or_error);
1940 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1941 class named to the left of the "::" operator. DONE is true if this
1942 expression is a complete postfix-expression; it is false if this
1943 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1944 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1945 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1946 is true iff this qualified name appears as a template argument. */
1948 tree
1949 finish_qualified_id_expr (tree qualifying_class,
1950 tree expr,
1951 bool done,
1952 bool address_p,
1953 bool template_p,
1954 bool template_arg_p,
1955 tsubst_flags_t complain)
1957 gcc_assert (TYPE_P (qualifying_class));
1959 if (error_operand_p (expr))
1960 return error_mark_node;
1962 if ((DECL_P (expr) || BASELINK_P (expr))
1963 && !mark_used (expr, complain))
1964 return error_mark_node;
1966 if (template_p)
1968 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
1969 /* cp_parser_lookup_name thought we were looking for a type,
1970 but we're actually looking for a declaration. */
1971 expr = build_qualified_name (/*type*/NULL_TREE,
1972 TYPE_CONTEXT (expr),
1973 TYPE_IDENTIFIER (expr),
1974 /*template_p*/true);
1975 else
1976 check_template_keyword (expr);
1979 /* If EXPR occurs as the operand of '&', use special handling that
1980 permits a pointer-to-member. */
1981 if (address_p && done)
1983 if (TREE_CODE (expr) == SCOPE_REF)
1984 expr = TREE_OPERAND (expr, 1);
1985 expr = build_offset_ref (qualifying_class, expr,
1986 /*address_p=*/true, complain);
1987 return expr;
1990 /* No need to check access within an enum. */
1991 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1992 return expr;
1994 /* Within the scope of a class, turn references to non-static
1995 members into expression of the form "this->...". */
1996 if (template_arg_p)
1997 /* But, within a template argument, we do not want make the
1998 transformation, as there is no "this" pointer. */
2000 else if (TREE_CODE (expr) == FIELD_DECL)
2002 push_deferring_access_checks (dk_no_check);
2003 expr = finish_non_static_data_member (expr, NULL_TREE,
2004 qualifying_class);
2005 pop_deferring_access_checks ();
2007 else if (BASELINK_P (expr) && !processing_template_decl)
2009 /* See if any of the functions are non-static members. */
2010 /* If so, the expression may be relative to 'this'. */
2011 if (!shared_member_p (expr)
2012 && current_class_ptr
2013 && DERIVED_FROM_P (qualifying_class,
2014 current_nonlambda_class_type ()))
2015 expr = (build_class_member_access_expr
2016 (maybe_dummy_object (qualifying_class, NULL),
2017 expr,
2018 BASELINK_ACCESS_BINFO (expr),
2019 /*preserve_reference=*/false,
2020 complain));
2021 else if (done)
2022 /* The expression is a qualified name whose address is not
2023 being taken. */
2024 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2025 complain);
2027 else if (BASELINK_P (expr))
2029 else
2031 /* In a template, return a SCOPE_REF for most qualified-ids
2032 so that we can check access at instantiation time. But if
2033 we're looking at a member of the current instantiation, we
2034 know we have access and building up the SCOPE_REF confuses
2035 non-type template argument handling. */
2036 if (processing_template_decl
2037 && !currently_open_class (qualifying_class))
2038 expr = build_qualified_name (TREE_TYPE (expr),
2039 qualifying_class, expr,
2040 template_p);
2042 expr = convert_from_reference (expr);
2045 return expr;
2048 /* Begin a statement-expression. The value returned must be passed to
2049 finish_stmt_expr. */
2051 tree
2052 begin_stmt_expr (void)
2054 return push_stmt_list ();
2057 /* Process the final expression of a statement expression. EXPR can be
2058 NULL, if the final expression is empty. Return a STATEMENT_LIST
2059 containing all the statements in the statement-expression, or
2060 ERROR_MARK_NODE if there was an error. */
2062 tree
2063 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2065 if (error_operand_p (expr))
2067 /* The type of the statement-expression is the type of the last
2068 expression. */
2069 TREE_TYPE (stmt_expr) = error_mark_node;
2070 return error_mark_node;
2073 /* If the last statement does not have "void" type, then the value
2074 of the last statement is the value of the entire expression. */
2075 if (expr)
2077 tree type = TREE_TYPE (expr);
2079 if (processing_template_decl)
2081 expr = build_stmt (input_location, EXPR_STMT, expr);
2082 expr = add_stmt (expr);
2083 /* Mark the last statement so that we can recognize it as such at
2084 template-instantiation time. */
2085 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2087 else if (VOID_TYPE_P (type))
2089 /* Just treat this like an ordinary statement. */
2090 expr = finish_expr_stmt (expr);
2092 else
2094 /* It actually has a value we need to deal with. First, force it
2095 to be an rvalue so that we won't need to build up a copy
2096 constructor call later when we try to assign it to something. */
2097 expr = force_rvalue (expr, tf_warning_or_error);
2098 if (error_operand_p (expr))
2099 return error_mark_node;
2101 /* Update for array-to-pointer decay. */
2102 type = TREE_TYPE (expr);
2104 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2105 normal statement, but don't convert to void or actually add
2106 the EXPR_STMT. */
2107 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2108 expr = maybe_cleanup_point_expr (expr);
2109 add_stmt (expr);
2112 /* The type of the statement-expression is the type of the last
2113 expression. */
2114 TREE_TYPE (stmt_expr) = type;
2117 return stmt_expr;
2120 /* Finish a statement-expression. EXPR should be the value returned
2121 by the previous begin_stmt_expr. Returns an expression
2122 representing the statement-expression. */
2124 tree
2125 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2127 tree type;
2128 tree result;
2130 if (error_operand_p (stmt_expr))
2132 pop_stmt_list (stmt_expr);
2133 return error_mark_node;
2136 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2138 type = TREE_TYPE (stmt_expr);
2139 result = pop_stmt_list (stmt_expr);
2140 TREE_TYPE (result) = type;
2142 if (processing_template_decl)
2144 result = build_min (STMT_EXPR, type, result);
2145 TREE_SIDE_EFFECTS (result) = 1;
2146 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2148 else if (CLASS_TYPE_P (type))
2150 /* Wrap the statement-expression in a TARGET_EXPR so that the
2151 temporary object created by the final expression is destroyed at
2152 the end of the full-expression containing the
2153 statement-expression. */
2154 result = force_target_expr (type, result, tf_warning_or_error);
2157 return result;
2160 /* Returns the expression which provides the value of STMT_EXPR. */
2162 tree
2163 stmt_expr_value_expr (tree stmt_expr)
2165 tree t = STMT_EXPR_STMT (stmt_expr);
2167 if (TREE_CODE (t) == BIND_EXPR)
2168 t = BIND_EXPR_BODY (t);
2170 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2171 t = STATEMENT_LIST_TAIL (t)->stmt;
2173 if (TREE_CODE (t) == EXPR_STMT)
2174 t = EXPR_STMT_EXPR (t);
2176 return t;
2179 /* Return TRUE iff EXPR_STMT is an empty list of
2180 expression statements. */
2182 bool
2183 empty_expr_stmt_p (tree expr_stmt)
2185 tree body = NULL_TREE;
2187 if (expr_stmt == void_node)
2188 return true;
2190 if (expr_stmt)
2192 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2193 body = EXPR_STMT_EXPR (expr_stmt);
2194 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2195 body = expr_stmt;
2198 if (body)
2200 if (TREE_CODE (body) == STATEMENT_LIST)
2201 return tsi_end_p (tsi_start (body));
2202 else
2203 return empty_expr_stmt_p (body);
2205 return false;
2208 /* Perform Koenig lookup. FN is the postfix-expression representing
2209 the function (or functions) to call; ARGS are the arguments to the
2210 call. Returns the functions to be considered by overload resolution. */
2212 cp_expr
2213 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2214 tsubst_flags_t complain)
2216 tree identifier = NULL_TREE;
2217 tree functions = NULL_TREE;
2218 tree tmpl_args = NULL_TREE;
2219 bool template_id = false;
2221 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2223 /* Use a separate flag to handle null args. */
2224 template_id = true;
2225 tmpl_args = TREE_OPERAND (fn, 1);
2226 fn = TREE_OPERAND (fn, 0);
2229 /* Find the name of the overloaded function. */
2230 if (identifier_p (fn))
2231 identifier = fn;
2232 else if (is_overloaded_fn (fn))
2234 functions = fn;
2235 identifier = DECL_NAME (get_first_fn (functions));
2237 else if (DECL_P (fn))
2239 functions = fn;
2240 identifier = DECL_NAME (fn);
2243 /* A call to a namespace-scope function using an unqualified name.
2245 Do Koenig lookup -- unless any of the arguments are
2246 type-dependent. */
2247 if (!any_type_dependent_arguments_p (args)
2248 && !any_dependent_template_arguments_p (tmpl_args))
2250 fn = lookup_arg_dependent (identifier, functions, args);
2251 if (!fn)
2253 /* The unqualified name could not be resolved. */
2254 if (complain)
2255 fn = unqualified_fn_lookup_error (identifier);
2256 else
2257 fn = identifier;
2261 if (fn && template_id)
2262 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2264 return fn;
2267 /* Generate an expression for `FN (ARGS)'. This may change the
2268 contents of ARGS.
2270 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2271 as a virtual call, even if FN is virtual. (This flag is set when
2272 encountering an expression where the function name is explicitly
2273 qualified. For example a call to `X::f' never generates a virtual
2274 call.)
2276 Returns code for the call. */
2278 tree
2279 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2280 bool koenig_p, tsubst_flags_t complain)
2282 tree result;
2283 tree orig_fn;
2284 vec<tree, va_gc> *orig_args = NULL;
2286 if (fn == error_mark_node)
2287 return error_mark_node;
2289 gcc_assert (!TYPE_P (fn));
2291 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2292 it so that we can tell this is a call to a known function. */
2293 fn = maybe_undo_parenthesized_ref (fn);
2295 orig_fn = fn;
2297 if (processing_template_decl)
2299 /* If the call expression is dependent, build a CALL_EXPR node
2300 with no type; type_dependent_expression_p recognizes
2301 expressions with no type as being dependent. */
2302 if (type_dependent_expression_p (fn)
2303 || any_type_dependent_arguments_p (*args)
2304 /* For a non-static member function that doesn't have an
2305 explicit object argument, we need to specifically
2306 test the type dependency of the "this" pointer because it
2307 is not included in *ARGS even though it is considered to
2308 be part of the list of arguments. Note that this is
2309 related to CWG issues 515 and 1005. */
2310 || (TREE_CODE (fn) != COMPONENT_REF
2311 && non_static_member_function_p (fn)
2312 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn))
2313 && current_class_ref
2314 && type_dependent_expression_p (current_class_ref)))
2316 result = build_nt_call_vec (fn, *args);
2317 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2318 KOENIG_LOOKUP_P (result) = koenig_p;
2319 if (cfun)
2323 tree fndecl = OVL_CURRENT (fn);
2324 if (TREE_CODE (fndecl) != FUNCTION_DECL
2325 || !TREE_THIS_VOLATILE (fndecl))
2326 break;
2327 fn = OVL_NEXT (fn);
2329 while (fn);
2330 if (!fn)
2331 current_function_returns_abnormally = 1;
2333 return result;
2335 orig_args = make_tree_vector_copy (*args);
2336 if (!BASELINK_P (fn)
2337 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2338 && TREE_TYPE (fn) != unknown_type_node)
2339 fn = build_non_dependent_expr (fn);
2340 make_args_non_dependent (*args);
2343 if (TREE_CODE (fn) == COMPONENT_REF)
2345 tree member = TREE_OPERAND (fn, 1);
2346 if (BASELINK_P (member))
2348 tree object = TREE_OPERAND (fn, 0);
2349 return build_new_method_call (object, member,
2350 args, NULL_TREE,
2351 (disallow_virtual
2352 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2353 : LOOKUP_NORMAL),
2354 /*fn_p=*/NULL,
2355 complain);
2359 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2360 if (TREE_CODE (fn) == ADDR_EXPR
2361 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2362 fn = TREE_OPERAND (fn, 0);
2364 if (is_overloaded_fn (fn))
2365 fn = baselink_for_fns (fn);
2367 result = NULL_TREE;
2368 if (BASELINK_P (fn))
2370 tree object;
2372 /* A call to a member function. From [over.call.func]:
2374 If the keyword this is in scope and refers to the class of
2375 that member function, or a derived class thereof, then the
2376 function call is transformed into a qualified function call
2377 using (*this) as the postfix-expression to the left of the
2378 . operator.... [Otherwise] a contrived object of type T
2379 becomes the implied object argument.
2381 In this situation:
2383 struct A { void f(); };
2384 struct B : public A {};
2385 struct C : public A { void g() { B::f(); }};
2387 "the class of that member function" refers to `A'. But 11.2
2388 [class.access.base] says that we need to convert 'this' to B* as
2389 part of the access, so we pass 'B' to maybe_dummy_object. */
2391 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2393 /* A constructor call always uses a dummy object. (This constructor
2394 call which has the form A::A () is actually invalid and we are
2395 going to reject it later in build_new_method_call.) */
2396 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2398 else
2399 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2400 NULL);
2402 if (processing_template_decl)
2404 if (type_dependent_expression_p (object))
2406 tree ret = build_nt_call_vec (orig_fn, orig_args);
2407 release_tree_vector (orig_args);
2408 return ret;
2410 object = build_non_dependent_expr (object);
2413 result = build_new_method_call (object, fn, args, NULL_TREE,
2414 (disallow_virtual
2415 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2416 : LOOKUP_NORMAL),
2417 /*fn_p=*/NULL,
2418 complain);
2420 else if (is_overloaded_fn (fn))
2422 /* If the function is an overloaded builtin, resolve it. */
2423 if (TREE_CODE (fn) == FUNCTION_DECL
2424 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2425 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2426 result = resolve_overloaded_builtin (input_location, fn, *args);
2428 if (!result)
2430 if (warn_sizeof_pointer_memaccess
2431 && (complain & tf_warning)
2432 && !vec_safe_is_empty (*args)
2433 && !processing_template_decl)
2435 location_t sizeof_arg_loc[3];
2436 tree sizeof_arg[3];
2437 unsigned int i;
2438 for (i = 0; i < 3; i++)
2440 tree t;
2442 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2443 sizeof_arg[i] = NULL_TREE;
2444 if (i >= (*args)->length ())
2445 continue;
2446 t = (**args)[i];
2447 if (TREE_CODE (t) != SIZEOF_EXPR)
2448 continue;
2449 if (SIZEOF_EXPR_TYPE_P (t))
2450 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2451 else
2452 sizeof_arg[i] = TREE_OPERAND (t, 0);
2453 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2455 sizeof_pointer_memaccess_warning
2456 (sizeof_arg_loc, fn, *args,
2457 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2460 /* A call to a namespace-scope function. */
2461 result = build_new_function_call (fn, args, koenig_p, complain);
2464 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2466 if (!vec_safe_is_empty (*args))
2467 error ("arguments to destructor are not allowed");
2468 /* Mark the pseudo-destructor call as having side-effects so
2469 that we do not issue warnings about its use. */
2470 result = build1 (NOP_EXPR,
2471 void_type_node,
2472 TREE_OPERAND (fn, 0));
2473 TREE_SIDE_EFFECTS (result) = 1;
2475 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2476 /* If the "function" is really an object of class type, it might
2477 have an overloaded `operator ()'. */
2478 result = build_op_call (fn, args, complain);
2480 if (!result)
2481 /* A call where the function is unknown. */
2482 result = cp_build_function_call_vec (fn, args, complain);
2484 if (processing_template_decl && result != error_mark_node)
2486 if (INDIRECT_REF_P (result))
2487 result = TREE_OPERAND (result, 0);
2488 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2489 SET_EXPR_LOCATION (result, input_location);
2490 KOENIG_LOOKUP_P (result) = koenig_p;
2491 release_tree_vector (orig_args);
2492 result = convert_from_reference (result);
2495 if (koenig_p)
2497 /* Free garbage OVERLOADs from arg-dependent lookup. */
2498 tree next = NULL_TREE;
2499 for (fn = orig_fn;
2500 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2501 fn = next)
2503 if (processing_template_decl)
2504 /* In a template, we'll re-use them at instantiation time. */
2505 OVL_ARG_DEPENDENT (fn) = false;
2506 else
2508 next = OVL_CHAIN (fn);
2509 ggc_free (fn);
2514 return result;
2517 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2518 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2519 POSTDECREMENT_EXPR.) */
2521 cp_expr
2522 finish_increment_expr (cp_expr expr, enum tree_code code)
2524 /* input_location holds the location of the trailing operator token.
2525 Build a location of the form:
2526 expr++
2527 ~~~~^~
2528 with the caret at the operator token, ranging from the start
2529 of EXPR to the end of the operator token. */
2530 location_t combined_loc = make_location (input_location,
2531 expr.get_start (),
2532 get_finish (input_location));
2533 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2534 tf_warning_or_error);
2535 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2536 result.set_location (combined_loc);
2537 return result;
2540 /* Finish a use of `this'. Returns an expression for `this'. */
2542 tree
2543 finish_this_expr (void)
2545 tree result = NULL_TREE;
2547 if (current_class_ptr)
2549 tree type = TREE_TYPE (current_class_ref);
2551 /* In a lambda expression, 'this' refers to the captured 'this'. */
2552 if (LAMBDA_TYPE_P (type))
2553 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2554 else
2555 result = current_class_ptr;
2558 if (result)
2559 /* The keyword 'this' is a prvalue expression. */
2560 return rvalue (result);
2562 tree fn = current_nonlambda_function ();
2563 if (fn && DECL_STATIC_FUNCTION_P (fn))
2564 error ("%<this%> is unavailable for static member functions");
2565 else if (fn)
2566 error ("invalid use of %<this%> in non-member function");
2567 else
2568 error ("invalid use of %<this%> at top level");
2569 return error_mark_node;
2572 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2573 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2574 the TYPE for the type given. If SCOPE is non-NULL, the expression
2575 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2577 tree
2578 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2579 location_t loc)
2581 if (object == error_mark_node || destructor == error_mark_node)
2582 return error_mark_node;
2584 gcc_assert (TYPE_P (destructor));
2586 if (!processing_template_decl)
2588 if (scope == error_mark_node)
2590 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2591 return error_mark_node;
2593 if (is_auto (destructor))
2594 destructor = TREE_TYPE (object);
2595 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2597 error_at (loc,
2598 "qualified type %qT does not match destructor name ~%qT",
2599 scope, destructor);
2600 return error_mark_node;
2604 /* [expr.pseudo] says both:
2606 The type designated by the pseudo-destructor-name shall be
2607 the same as the object type.
2609 and:
2611 The cv-unqualified versions of the object type and of the
2612 type designated by the pseudo-destructor-name shall be the
2613 same type.
2615 We implement the more generous second sentence, since that is
2616 what most other compilers do. */
2617 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2618 destructor))
2620 error_at (loc, "%qE is not of type %qT", object, destructor);
2621 return error_mark_node;
2625 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2626 scope, destructor);
2629 /* Finish an expression of the form CODE EXPR. */
2631 cp_expr
2632 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2633 tsubst_flags_t complain)
2635 /* Build a location of the form:
2636 ++expr
2637 ^~~~~~
2638 with the caret at the operator token, ranging from the start
2639 of the operator token to the end of EXPR. */
2640 location_t combined_loc = make_location (op_loc,
2641 op_loc, expr.get_finish ());
2642 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2643 /* TODO: build_x_unary_op doesn't always honor the location. */
2644 result.set_location (combined_loc);
2646 tree result_ovl, expr_ovl;
2648 if (!(complain & tf_warning))
2649 return result;
2651 result_ovl = result;
2652 expr_ovl = expr;
2654 if (!processing_template_decl)
2655 expr_ovl = cp_fully_fold (expr_ovl);
2657 if (!CONSTANT_CLASS_P (expr_ovl)
2658 || TREE_OVERFLOW_P (expr_ovl))
2659 return result;
2661 if (!processing_template_decl)
2662 result_ovl = cp_fully_fold (result_ovl);
2664 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2665 overflow_warning (combined_loc, result_ovl);
2667 return result;
2670 /* Finish a compound-literal expression. TYPE is the type to which
2671 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2673 tree
2674 finish_compound_literal (tree type, tree compound_literal,
2675 tsubst_flags_t complain)
2677 if (type == error_mark_node)
2678 return error_mark_node;
2680 if (TREE_CODE (type) == REFERENCE_TYPE)
2682 compound_literal
2683 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2684 complain);
2685 return cp_build_c_cast (type, compound_literal, complain);
2688 if (!TYPE_OBJ_P (type))
2690 if (complain & tf_error)
2691 error ("compound literal of non-object type %qT", type);
2692 return error_mark_node;
2695 if (processing_template_decl)
2697 TREE_TYPE (compound_literal) = type;
2698 /* Mark the expression as a compound literal. */
2699 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2700 return compound_literal;
2703 type = complete_type (type);
2705 if (TYPE_NON_AGGREGATE_CLASS (type))
2707 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2708 everywhere that deals with function arguments would be a pain, so
2709 just wrap it in a TREE_LIST. The parser set a flag so we know
2710 that it came from T{} rather than T({}). */
2711 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2712 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2713 return build_functional_cast (type, compound_literal, complain);
2716 if (TREE_CODE (type) == ARRAY_TYPE
2717 && check_array_initializer (NULL_TREE, type, compound_literal))
2718 return error_mark_node;
2719 compound_literal = reshape_init (type, compound_literal, complain);
2720 if (SCALAR_TYPE_P (type)
2721 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2722 && !check_narrowing (type, compound_literal, complain))
2723 return error_mark_node;
2724 if (TREE_CODE (type) == ARRAY_TYPE
2725 && TYPE_DOMAIN (type) == NULL_TREE)
2727 cp_complete_array_type_or_error (&type, compound_literal,
2728 false, complain);
2729 if (type == error_mark_node)
2730 return error_mark_node;
2732 compound_literal = digest_init (type, compound_literal, complain);
2733 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2734 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2735 /* Put static/constant array temporaries in static variables, but always
2736 represent class temporaries with TARGET_EXPR so we elide copies. */
2737 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2738 && TREE_CODE (type) == ARRAY_TYPE
2739 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2740 && initializer_constant_valid_p (compound_literal, type))
2742 tree decl = create_temporary_var (type);
2743 DECL_INITIAL (decl) = compound_literal;
2744 TREE_STATIC (decl) = 1;
2745 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2747 /* 5.19 says that a constant expression can include an
2748 lvalue-rvalue conversion applied to "a glvalue of literal type
2749 that refers to a non-volatile temporary object initialized
2750 with a constant expression". Rather than try to communicate
2751 that this VAR_DECL is a temporary, just mark it constexpr. */
2752 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2753 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2754 TREE_CONSTANT (decl) = true;
2756 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2757 decl = pushdecl_top_level (decl);
2758 DECL_NAME (decl) = make_anon_name ();
2759 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2760 /* Make sure the destructor is callable. */
2761 tree clean = cxx_maybe_build_cleanup (decl, complain);
2762 if (clean == error_mark_node)
2763 return error_mark_node;
2764 return decl;
2766 else
2767 return get_target_expr_sfinae (compound_literal, complain);
2770 /* Return the declaration for the function-name variable indicated by
2771 ID. */
2773 tree
2774 finish_fname (tree id)
2776 tree decl;
2778 decl = fname_decl (input_location, C_RID_CODE (id), id);
2779 if (processing_template_decl && current_function_decl
2780 && decl != error_mark_node)
2781 decl = DECL_NAME (decl);
2782 return decl;
2785 /* Finish a translation unit. */
2787 void
2788 finish_translation_unit (void)
2790 /* In case there were missing closebraces,
2791 get us back to the global binding level. */
2792 pop_everything ();
2793 while (current_namespace != global_namespace)
2794 pop_namespace ();
2796 /* Do file scope __FUNCTION__ et al. */
2797 finish_fname_decls ();
2800 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2801 Returns the parameter. */
2803 tree
2804 finish_template_type_parm (tree aggr, tree identifier)
2806 if (aggr != class_type_node)
2808 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2809 aggr = class_type_node;
2812 return build_tree_list (aggr, identifier);
2815 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2816 Returns the parameter. */
2818 tree
2819 finish_template_template_parm (tree aggr, tree identifier)
2821 tree decl = build_decl (input_location,
2822 TYPE_DECL, identifier, NULL_TREE);
2824 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2825 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2826 DECL_TEMPLATE_RESULT (tmpl) = decl;
2827 DECL_ARTIFICIAL (decl) = 1;
2829 // Associate the constraints with the underlying declaration,
2830 // not the template.
2831 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2832 tree constr = build_constraints (reqs, NULL_TREE);
2833 set_constraints (decl, constr);
2835 end_template_decl ();
2837 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2839 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2840 /*is_primary=*/true, /*is_partial=*/false,
2841 /*is_friend=*/0);
2843 return finish_template_type_parm (aggr, tmpl);
2846 /* ARGUMENT is the default-argument value for a template template
2847 parameter. If ARGUMENT is invalid, issue error messages and return
2848 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2850 tree
2851 check_template_template_default_arg (tree argument)
2853 if (TREE_CODE (argument) != TEMPLATE_DECL
2854 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2855 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2857 if (TREE_CODE (argument) == TYPE_DECL)
2858 error ("invalid use of type %qT as a default value for a template "
2859 "template-parameter", TREE_TYPE (argument));
2860 else
2861 error ("invalid default argument for a template template parameter");
2862 return error_mark_node;
2865 return argument;
2868 /* Begin a class definition, as indicated by T. */
2870 tree
2871 begin_class_definition (tree t)
2873 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2874 return error_mark_node;
2876 if (processing_template_parmlist)
2878 error ("definition of %q#T inside template parameter list", t);
2879 return error_mark_node;
2882 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2883 are passed the same as decimal scalar types. */
2884 if (TREE_CODE (t) == RECORD_TYPE
2885 && !processing_template_decl)
2887 tree ns = TYPE_CONTEXT (t);
2888 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2889 && DECL_CONTEXT (ns) == std_node
2890 && DECL_NAME (ns)
2891 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2893 const char *n = TYPE_NAME_STRING (t);
2894 if ((strcmp (n, "decimal32") == 0)
2895 || (strcmp (n, "decimal64") == 0)
2896 || (strcmp (n, "decimal128") == 0))
2897 TYPE_TRANSPARENT_AGGR (t) = 1;
2901 /* A non-implicit typename comes from code like:
2903 template <typename T> struct A {
2904 template <typename U> struct A<T>::B ...
2906 This is erroneous. */
2907 else if (TREE_CODE (t) == TYPENAME_TYPE)
2909 error ("invalid definition of qualified type %qT", t);
2910 t = error_mark_node;
2913 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2915 t = make_class_type (RECORD_TYPE);
2916 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2919 if (TYPE_BEING_DEFINED (t))
2921 t = make_class_type (TREE_CODE (t));
2922 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2924 maybe_process_partial_specialization (t);
2925 pushclass (t);
2926 TYPE_BEING_DEFINED (t) = 1;
2927 class_binding_level->defining_class_p = 1;
2929 if (flag_pack_struct)
2931 tree v;
2932 TYPE_PACKED (t) = 1;
2933 /* Even though the type is being defined for the first time
2934 here, there might have been a forward declaration, so there
2935 might be cv-qualified variants of T. */
2936 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2937 TYPE_PACKED (v) = 1;
2939 /* Reset the interface data, at the earliest possible
2940 moment, as it might have been set via a class foo;
2941 before. */
2942 if (! TYPE_ANONYMOUS_P (t))
2944 struct c_fileinfo *finfo = \
2945 get_fileinfo (LOCATION_FILE (input_location));
2946 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2947 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2948 (t, finfo->interface_unknown);
2950 reset_specialization();
2952 /* Make a declaration for this class in its own scope. */
2953 build_self_reference ();
2955 return t;
2958 /* Finish the member declaration given by DECL. */
2960 void
2961 finish_member_declaration (tree decl)
2963 if (decl == error_mark_node || decl == NULL_TREE)
2964 return;
2966 if (decl == void_type_node)
2967 /* The COMPONENT was a friend, not a member, and so there's
2968 nothing for us to do. */
2969 return;
2971 /* We should see only one DECL at a time. */
2972 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2974 /* Set up access control for DECL. */
2975 TREE_PRIVATE (decl)
2976 = (current_access_specifier == access_private_node);
2977 TREE_PROTECTED (decl)
2978 = (current_access_specifier == access_protected_node);
2979 if (TREE_CODE (decl) == TEMPLATE_DECL)
2981 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2982 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2985 /* Mark the DECL as a member of the current class, unless it's
2986 a member of an enumeration. */
2987 if (TREE_CODE (decl) != CONST_DECL)
2988 DECL_CONTEXT (decl) = current_class_type;
2990 /* Check for bare parameter packs in the member variable declaration. */
2991 if (TREE_CODE (decl) == FIELD_DECL)
2993 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2994 TREE_TYPE (decl) = error_mark_node;
2995 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2996 DECL_ATTRIBUTES (decl) = NULL_TREE;
2999 /* [dcl.link]
3001 A C language linkage is ignored for the names of class members
3002 and the member function type of class member functions. */
3003 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
3004 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3006 /* Put functions on the TYPE_METHODS list and everything else on the
3007 TYPE_FIELDS list. Note that these are built up in reverse order.
3008 We reverse them (to obtain declaration order) in finish_struct. */
3009 if (DECL_DECLARES_FUNCTION_P (decl))
3011 /* We also need to add this function to the
3012 CLASSTYPE_METHOD_VEC. */
3013 if (add_method (current_class_type, decl, NULL_TREE))
3015 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
3016 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
3017 TYPE_METHODS (current_class_type) = decl;
3019 maybe_add_class_template_decl_list (current_class_type, decl,
3020 /*friend_p=*/0);
3023 /* Enter the DECL into the scope of the class, if the class
3024 isn't a closure (whose fields are supposed to be unnamed). */
3025 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3026 || pushdecl_class_level (decl))
3028 if (TREE_CODE (decl) == USING_DECL)
3030 /* For now, ignore class-scope USING_DECLS, so that
3031 debugging backends do not see them. */
3032 DECL_IGNORED_P (decl) = 1;
3035 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3036 go at the beginning. The reason is that lookup_field_1
3037 searches the list in order, and we want a field name to
3038 override a type name so that the "struct stat hack" will
3039 work. In particular:
3041 struct S { enum E { }; int E } s;
3042 s.E = 3;
3044 is valid. In addition, the FIELD_DECLs must be maintained in
3045 declaration order so that class layout works as expected.
3046 However, we don't need that order until class layout, so we
3047 save a little time by putting FIELD_DECLs on in reverse order
3048 here, and then reversing them in finish_struct_1. (We could
3049 also keep a pointer to the correct insertion points in the
3050 list.) */
3052 if (TREE_CODE (decl) == TYPE_DECL)
3053 TYPE_FIELDS (current_class_type)
3054 = chainon (TYPE_FIELDS (current_class_type), decl);
3055 else
3057 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3058 TYPE_FIELDS (current_class_type) = decl;
3061 maybe_add_class_template_decl_list (current_class_type, decl,
3062 /*friend_p=*/0);
3066 /* Finish processing a complete template declaration. The PARMS are
3067 the template parameters. */
3069 void
3070 finish_template_decl (tree parms)
3072 if (parms)
3073 end_template_decl ();
3074 else
3075 end_specialization ();
3078 // Returns the template type of the class scope being entered. If we're
3079 // entering a constrained class scope. TYPE is the class template
3080 // scope being entered and we may need to match the intended type with
3081 // a constrained specialization. For example:
3083 // template<Object T>
3084 // struct S { void f(); }; #1
3086 // template<Object T>
3087 // void S<T>::f() { } #2
3089 // We check, in #2, that S<T> refers precisely to the type declared by
3090 // #1 (i.e., that the constraints match). Note that the following should
3091 // be an error since there is no specialization of S<T> that is
3092 // unconstrained, but this is not diagnosed here.
3094 // template<typename T>
3095 // void S<T>::f() { }
3097 // We cannot diagnose this problem here since this function also matches
3098 // qualified template names that are not part of a definition. For example:
3100 // template<Integral T, Floating_point U>
3101 // typename pair<T, U>::first_type void f(T, U);
3103 // Here, it is unlikely that there is a partial specialization of
3104 // pair constrained for for Integral and Floating_point arguments.
3106 // The general rule is: if a constrained specialization with matching
3107 // constraints is found return that type. Also note that if TYPE is not a
3108 // class-type (e.g. a typename type), then no fixup is needed.
3110 static tree
3111 fixup_template_type (tree type)
3113 // Find the template parameter list at the a depth appropriate to
3114 // the scope we're trying to enter.
3115 tree parms = current_template_parms;
3116 int depth = template_class_depth (type);
3117 for (int n = processing_template_decl; n > depth && parms; --n)
3118 parms = TREE_CHAIN (parms);
3119 if (!parms)
3120 return type;
3121 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3122 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3124 // Search for a specialization whose type and constraints match.
3125 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3126 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3127 while (specs)
3129 tree spec_constr = get_constraints (TREE_VALUE (specs));
3131 // If the type and constraints match a specialization, then we
3132 // are entering that type.
3133 if (same_type_p (type, TREE_TYPE (specs))
3134 && equivalent_constraints (cur_constr, spec_constr))
3135 return TREE_TYPE (specs);
3136 specs = TREE_CHAIN (specs);
3139 // If no specialization matches, then must return the type
3140 // previously found.
3141 return type;
3144 /* Finish processing a template-id (which names a type) of the form
3145 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3146 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3147 the scope of template-id indicated. */
3149 tree
3150 finish_template_type (tree name, tree args, int entering_scope)
3152 tree type;
3154 type = lookup_template_class (name, args,
3155 NULL_TREE, NULL_TREE, entering_scope,
3156 tf_warning_or_error | tf_user);
3158 /* If we might be entering the scope of a partial specialization,
3159 find the one with the right constraints. */
3160 if (flag_concepts
3161 && entering_scope
3162 && CLASS_TYPE_P (type)
3163 && dependent_type_p (type)
3164 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3165 type = fixup_template_type (type);
3167 if (type == error_mark_node)
3168 return type;
3169 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3170 return TYPE_STUB_DECL (type);
3171 else
3172 return TYPE_NAME (type);
3175 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3176 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3177 BASE_CLASS, or NULL_TREE if an error occurred. The
3178 ACCESS_SPECIFIER is one of
3179 access_{default,public,protected_private}_node. For a virtual base
3180 we set TREE_TYPE. */
3182 tree
3183 finish_base_specifier (tree base, tree access, bool virtual_p)
3185 tree result;
3187 if (base == error_mark_node)
3189 error ("invalid base-class specification");
3190 result = NULL_TREE;
3192 else if (! MAYBE_CLASS_TYPE_P (base))
3194 error ("%qT is not a class type", base);
3195 result = NULL_TREE;
3197 else
3199 if (cp_type_quals (base) != 0)
3201 /* DR 484: Can a base-specifier name a cv-qualified
3202 class type? */
3203 base = TYPE_MAIN_VARIANT (base);
3205 result = build_tree_list (access, base);
3206 if (virtual_p)
3207 TREE_TYPE (result) = integer_type_node;
3210 return result;
3213 /* If FNS is a member function, a set of member functions, or a
3214 template-id referring to one or more member functions, return a
3215 BASELINK for FNS, incorporating the current access context.
3216 Otherwise, return FNS unchanged. */
3218 tree
3219 baselink_for_fns (tree fns)
3221 tree scope;
3222 tree cl;
3224 if (BASELINK_P (fns)
3225 || error_operand_p (fns))
3226 return fns;
3228 scope = ovl_scope (fns);
3229 if (!CLASS_TYPE_P (scope))
3230 return fns;
3232 cl = currently_open_derived_class (scope);
3233 if (!cl)
3234 cl = scope;
3235 cl = TYPE_BINFO (cl);
3236 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3239 /* Returns true iff DECL is a variable from a function outside
3240 the current one. */
3242 static bool
3243 outer_var_p (tree decl)
3245 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3246 && DECL_FUNCTION_SCOPE_P (decl)
3247 && (DECL_CONTEXT (decl) != current_function_decl
3248 || parsing_nsdmi ()));
3251 /* As above, but also checks that DECL is automatic. */
3253 bool
3254 outer_automatic_var_p (tree decl)
3256 return (outer_var_p (decl)
3257 && !TREE_STATIC (decl));
3260 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3261 rewrite it for lambda capture. */
3263 tree
3264 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3266 if (cp_unevaluated_operand)
3267 /* It's not a use (3.2) if we're in an unevaluated context. */
3268 return decl;
3269 if (decl == error_mark_node)
3270 return decl;
3272 tree context = DECL_CONTEXT (decl);
3273 tree containing_function = current_function_decl;
3274 tree lambda_stack = NULL_TREE;
3275 tree lambda_expr = NULL_TREE;
3276 tree initializer = convert_from_reference (decl);
3278 /* Mark it as used now even if the use is ill-formed. */
3279 if (!mark_used (decl, complain))
3280 return error_mark_node;
3282 bool saw_generic_lambda = false;
3283 if (parsing_nsdmi ())
3284 containing_function = NULL_TREE;
3285 else
3286 /* If we are in a lambda function, we can move out until we hit
3287 1. the context,
3288 2. a non-lambda function, or
3289 3. a non-default capturing lambda function. */
3290 while (context != containing_function
3291 && LAMBDA_FUNCTION_P (containing_function))
3293 tree closure = DECL_CONTEXT (containing_function);
3294 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3296 if (generic_lambda_fn_p (containing_function))
3297 saw_generic_lambda = true;
3299 if (TYPE_CLASS_SCOPE_P (closure))
3300 /* A lambda in an NSDMI (c++/64496). */
3301 break;
3303 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3304 == CPLD_NONE)
3305 break;
3307 lambda_stack = tree_cons (NULL_TREE,
3308 lambda_expr,
3309 lambda_stack);
3311 containing_function
3312 = decl_function_context (containing_function);
3315 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3316 support for an approach in which a reference to a local
3317 [constant] automatic variable in a nested class or lambda body
3318 would enter the expression as an rvalue, which would reduce
3319 the complexity of the problem"
3321 FIXME update for final resolution of core issue 696. */
3322 if (decl_maybe_constant_var_p (decl))
3324 if (processing_template_decl && !saw_generic_lambda)
3325 /* In a non-generic lambda within a template, wait until instantiation
3326 time to decide whether to capture. For a generic lambda, we can't
3327 wait until we instantiate the op() because the closure class is
3328 already defined at that point. FIXME to get the semantics exactly
3329 right we need to partially-instantiate the lambda body so the only
3330 dependencies left are on the generic parameters themselves. This
3331 probably means moving away from our current model of lambdas in
3332 templates (instantiating the closure type) to one based on creating
3333 the closure type when instantiating the lambda context. That is
3334 probably also the way to handle lambdas within pack expansions. */
3335 return decl;
3336 else if (decl_constant_var_p (decl))
3338 tree t = maybe_constant_value (convert_from_reference (decl));
3339 if (TREE_CONSTANT (t))
3340 return t;
3344 if (lambda_expr && VAR_P (decl)
3345 && DECL_ANON_UNION_VAR_P (decl))
3347 if (complain & tf_error)
3348 error ("cannot capture member %qD of anonymous union", decl);
3349 return error_mark_node;
3351 if (context == containing_function)
3353 decl = add_default_capture (lambda_stack,
3354 /*id=*/DECL_NAME (decl),
3355 initializer);
3357 else if (lambda_expr)
3359 if (complain & tf_error)
3361 error ("%qD is not captured", decl);
3362 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3363 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3364 == CPLD_NONE)
3365 inform (location_of (closure),
3366 "the lambda has no capture-default");
3367 else if (TYPE_CLASS_SCOPE_P (closure))
3368 inform (0, "lambda in local class %q+T cannot "
3369 "capture variables from the enclosing context",
3370 TYPE_CONTEXT (closure));
3371 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3373 return error_mark_node;
3375 else
3377 if (complain & tf_error)
3378 error (VAR_P (decl)
3379 ? G_("use of local variable with automatic storage from containing function")
3380 : G_("use of parameter from containing function"));
3381 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3382 return error_mark_node;
3384 return decl;
3387 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3388 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3389 if non-NULL, is the type or namespace used to explicitly qualify
3390 ID_EXPRESSION. DECL is the entity to which that name has been
3391 resolved.
3393 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3394 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3395 be set to true if this expression isn't permitted in a
3396 constant-expression, but it is otherwise not set by this function.
3397 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3398 constant-expression, but a non-constant expression is also
3399 permissible.
3401 DONE is true if this expression is a complete postfix-expression;
3402 it is false if this expression is followed by '->', '[', '(', etc.
3403 ADDRESS_P is true iff this expression is the operand of '&'.
3404 TEMPLATE_P is true iff the qualified-id was of the form
3405 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3406 appears as a template argument.
3408 If an error occurs, and it is the kind of error that might cause
3409 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3410 is the caller's responsibility to issue the message. *ERROR_MSG
3411 will be a string with static storage duration, so the caller need
3412 not "free" it.
3414 Return an expression for the entity, after issuing appropriate
3415 diagnostics. This function is also responsible for transforming a
3416 reference to a non-static member into a COMPONENT_REF that makes
3417 the use of "this" explicit.
3419 Upon return, *IDK will be filled in appropriately. */
3420 cp_expr
3421 finish_id_expression (tree id_expression,
3422 tree decl,
3423 tree scope,
3424 cp_id_kind *idk,
3425 bool integral_constant_expression_p,
3426 bool allow_non_integral_constant_expression_p,
3427 bool *non_integral_constant_expression_p,
3428 bool template_p,
3429 bool done,
3430 bool address_p,
3431 bool template_arg_p,
3432 const char **error_msg,
3433 location_t location)
3435 decl = strip_using_decl (decl);
3437 /* Initialize the output parameters. */
3438 *idk = CP_ID_KIND_NONE;
3439 *error_msg = NULL;
3441 if (id_expression == error_mark_node)
3442 return error_mark_node;
3443 /* If we have a template-id, then no further lookup is
3444 required. If the template-id was for a template-class, we
3445 will sometimes have a TYPE_DECL at this point. */
3446 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3447 || TREE_CODE (decl) == TYPE_DECL)
3449 /* Look up the name. */
3450 else
3452 if (decl == error_mark_node)
3454 /* Name lookup failed. */
3455 if (scope
3456 && (!TYPE_P (scope)
3457 || (!dependent_type_p (scope)
3458 && !(identifier_p (id_expression)
3459 && IDENTIFIER_TYPENAME_P (id_expression)
3460 && dependent_type_p (TREE_TYPE (id_expression))))))
3462 /* If the qualifying type is non-dependent (and the name
3463 does not name a conversion operator to a dependent
3464 type), issue an error. */
3465 qualified_name_lookup_error (scope, id_expression, decl, location);
3466 return error_mark_node;
3468 else if (!scope)
3470 /* It may be resolved via Koenig lookup. */
3471 *idk = CP_ID_KIND_UNQUALIFIED;
3472 return id_expression;
3474 else
3475 decl = id_expression;
3477 /* If DECL is a variable that would be out of scope under
3478 ANSI/ISO rules, but in scope in the ARM, name lookup
3479 will succeed. Issue a diagnostic here. */
3480 else
3481 decl = check_for_out_of_scope_variable (decl);
3483 /* Remember that the name was used in the definition of
3484 the current class so that we can check later to see if
3485 the meaning would have been different after the class
3486 was entirely defined. */
3487 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3488 maybe_note_name_used_in_class (id_expression, decl);
3490 /* A use in unevaluated operand might not be instantiated appropriately
3491 if tsubst_copy builds a dummy parm, or if we never instantiate a
3492 generic lambda, so mark it now. */
3493 if (processing_template_decl && cp_unevaluated_operand)
3494 mark_type_use (decl);
3496 /* Disallow uses of local variables from containing functions, except
3497 within lambda-expressions. */
3498 if (outer_automatic_var_p (decl))
3500 decl = process_outer_var_ref (decl, tf_warning_or_error);
3501 if (decl == error_mark_node)
3502 return error_mark_node;
3505 /* Also disallow uses of function parameters outside the function
3506 body, except inside an unevaluated context (i.e. decltype). */
3507 if (TREE_CODE (decl) == PARM_DECL
3508 && DECL_CONTEXT (decl) == NULL_TREE
3509 && !cp_unevaluated_operand)
3511 *error_msg = "use of parameter outside function body";
3512 return error_mark_node;
3516 /* If we didn't find anything, or what we found was a type,
3517 then this wasn't really an id-expression. */
3518 if (TREE_CODE (decl) == TEMPLATE_DECL
3519 && !DECL_FUNCTION_TEMPLATE_P (decl))
3521 *error_msg = "missing template arguments";
3522 return error_mark_node;
3524 else if (TREE_CODE (decl) == TYPE_DECL
3525 || TREE_CODE (decl) == NAMESPACE_DECL)
3527 *error_msg = "expected primary-expression";
3528 return error_mark_node;
3531 /* If the name resolved to a template parameter, there is no
3532 need to look it up again later. */
3533 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3534 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3536 tree r;
3538 *idk = CP_ID_KIND_NONE;
3539 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3540 decl = TEMPLATE_PARM_DECL (decl);
3541 r = convert_from_reference (DECL_INITIAL (decl));
3543 if (integral_constant_expression_p
3544 && !dependent_type_p (TREE_TYPE (decl))
3545 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3547 if (!allow_non_integral_constant_expression_p)
3548 error ("template parameter %qD of type %qT is not allowed in "
3549 "an integral constant expression because it is not of "
3550 "integral or enumeration type", decl, TREE_TYPE (decl));
3551 *non_integral_constant_expression_p = true;
3553 return r;
3555 else
3557 bool dependent_p = type_dependent_expression_p (decl);
3559 /* If the declaration was explicitly qualified indicate
3560 that. The semantics of `A::f(3)' are different than
3561 `f(3)' if `f' is virtual. */
3562 *idk = (scope
3563 ? CP_ID_KIND_QUALIFIED
3564 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3565 ? CP_ID_KIND_TEMPLATE_ID
3566 : (dependent_p
3567 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3568 : CP_ID_KIND_UNQUALIFIED)));
3570 /* If the name was dependent on a template parameter, we will
3571 resolve the name at instantiation time. */
3572 if (dependent_p)
3574 /* If we found a variable, then name lookup during the
3575 instantiation will always resolve to the same VAR_DECL
3576 (or an instantiation thereof). */
3577 if (VAR_P (decl)
3578 || TREE_CODE (decl) == CONST_DECL
3579 || TREE_CODE (decl) == PARM_DECL)
3581 mark_used (decl);
3582 return convert_from_reference (decl);
3585 /* Create a SCOPE_REF for qualified names, if the scope is
3586 dependent. */
3587 if (scope)
3589 if (TYPE_P (scope))
3591 if (address_p && done)
3592 decl = finish_qualified_id_expr (scope, decl,
3593 done, address_p,
3594 template_p,
3595 template_arg_p,
3596 tf_warning_or_error);
3597 else
3599 tree type = NULL_TREE;
3600 if (DECL_P (decl) && !dependent_scope_p (scope))
3601 type = TREE_TYPE (decl);
3602 decl = build_qualified_name (type,
3603 scope,
3604 id_expression,
3605 template_p);
3608 if (TREE_TYPE (decl))
3609 decl = convert_from_reference (decl);
3610 return decl;
3612 /* A TEMPLATE_ID already contains all the information we
3613 need. */
3614 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3615 return id_expression;
3616 /* The same is true for FIELD_DECL, but we also need to
3617 make sure that the syntax is correct. */
3618 else if (TREE_CODE (decl) == FIELD_DECL)
3620 /* Since SCOPE is NULL here, this is an unqualified name.
3621 Access checking has been performed during name lookup
3622 already. Turn off checking to avoid duplicate errors. */
3623 push_deferring_access_checks (dk_no_check);
3624 decl = finish_non_static_data_member
3625 (decl, NULL_TREE,
3626 /*qualifying_scope=*/NULL_TREE);
3627 pop_deferring_access_checks ();
3628 return decl;
3630 return id_expression;
3633 if (TREE_CODE (decl) == NAMESPACE_DECL)
3635 error ("use of namespace %qD as expression", decl);
3636 return error_mark_node;
3638 else if (DECL_CLASS_TEMPLATE_P (decl))
3640 error ("use of class template %qT as expression", decl);
3641 return error_mark_node;
3643 else if (TREE_CODE (decl) == TREE_LIST)
3645 /* Ambiguous reference to base members. */
3646 error ("request for member %qD is ambiguous in "
3647 "multiple inheritance lattice", id_expression);
3648 print_candidates (decl);
3649 return error_mark_node;
3652 /* Mark variable-like entities as used. Functions are similarly
3653 marked either below or after overload resolution. */
3654 if ((VAR_P (decl)
3655 || TREE_CODE (decl) == PARM_DECL
3656 || TREE_CODE (decl) == CONST_DECL
3657 || TREE_CODE (decl) == RESULT_DECL)
3658 && !mark_used (decl))
3659 return error_mark_node;
3661 /* Only certain kinds of names are allowed in constant
3662 expression. Template parameters have already
3663 been handled above. */
3664 if (! error_operand_p (decl)
3665 && integral_constant_expression_p
3666 && ! decl_constant_var_p (decl)
3667 && TREE_CODE (decl) != CONST_DECL
3668 && ! builtin_valid_in_constant_expr_p (decl))
3670 if (!allow_non_integral_constant_expression_p)
3672 error ("%qD cannot appear in a constant-expression", decl);
3673 return error_mark_node;
3675 *non_integral_constant_expression_p = true;
3678 tree wrap;
3679 if (VAR_P (decl)
3680 && !cp_unevaluated_operand
3681 && !processing_template_decl
3682 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3683 && CP_DECL_THREAD_LOCAL_P (decl)
3684 && (wrap = get_tls_wrapper_fn (decl)))
3686 /* Replace an evaluated use of the thread_local variable with
3687 a call to its wrapper. */
3688 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3690 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3691 && variable_template_p (TREE_OPERAND (decl, 0)))
3693 decl = finish_template_variable (decl);
3694 mark_used (decl);
3695 decl = convert_from_reference (decl);
3697 else if (scope)
3699 decl = (adjust_result_of_qualified_name_lookup
3700 (decl, scope, current_nonlambda_class_type()));
3702 if (TREE_CODE (decl) == FUNCTION_DECL)
3703 mark_used (decl);
3705 if (TYPE_P (scope))
3706 decl = finish_qualified_id_expr (scope,
3707 decl,
3708 done,
3709 address_p,
3710 template_p,
3711 template_arg_p,
3712 tf_warning_or_error);
3713 else
3714 decl = convert_from_reference (decl);
3716 else if (TREE_CODE (decl) == FIELD_DECL)
3718 /* Since SCOPE is NULL here, this is an unqualified name.
3719 Access checking has been performed during name lookup
3720 already. Turn off checking to avoid duplicate errors. */
3721 push_deferring_access_checks (dk_no_check);
3722 decl = finish_non_static_data_member (decl, NULL_TREE,
3723 /*qualifying_scope=*/NULL_TREE);
3724 pop_deferring_access_checks ();
3726 else if (is_overloaded_fn (decl))
3728 tree first_fn;
3730 first_fn = get_first_fn (decl);
3731 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3732 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3734 if (!really_overloaded_fn (decl)
3735 && !mark_used (first_fn))
3736 return error_mark_node;
3738 if (!template_arg_p
3739 && TREE_CODE (first_fn) == FUNCTION_DECL
3740 && DECL_FUNCTION_MEMBER_P (first_fn)
3741 && !shared_member_p (decl))
3743 /* A set of member functions. */
3744 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3745 return finish_class_member_access_expr (decl, id_expression,
3746 /*template_p=*/false,
3747 tf_warning_or_error);
3750 decl = baselink_for_fns (decl);
3752 else
3754 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3755 && DECL_CLASS_SCOPE_P (decl))
3757 tree context = context_for_name_lookup (decl);
3758 if (context != current_class_type)
3760 tree path = currently_open_derived_class (context);
3761 perform_or_defer_access_check (TYPE_BINFO (path),
3762 decl, decl,
3763 tf_warning_or_error);
3767 decl = convert_from_reference (decl);
3771 return cp_expr (decl, location);
3774 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3775 use as a type-specifier. */
3777 tree
3778 finish_typeof (tree expr)
3780 tree type;
3782 if (type_dependent_expression_p (expr))
3784 type = cxx_make_type (TYPEOF_TYPE);
3785 TYPEOF_TYPE_EXPR (type) = expr;
3786 SET_TYPE_STRUCTURAL_EQUALITY (type);
3788 return type;
3791 expr = mark_type_use (expr);
3793 type = unlowered_expr_type (expr);
3795 if (!type || type == unknown_type_node)
3797 error ("type of %qE is unknown", expr);
3798 return error_mark_node;
3801 return type;
3804 /* Implement the __underlying_type keyword: Return the underlying
3805 type of TYPE, suitable for use as a type-specifier. */
3807 tree
3808 finish_underlying_type (tree type)
3810 tree underlying_type;
3812 if (processing_template_decl)
3814 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3815 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3816 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3818 return underlying_type;
3821 complete_type (type);
3823 if (TREE_CODE (type) != ENUMERAL_TYPE)
3825 error ("%qT is not an enumeration type", type);
3826 return error_mark_node;
3829 underlying_type = ENUM_UNDERLYING_TYPE (type);
3831 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3832 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3833 See finish_enum_value_list for details. */
3834 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3835 underlying_type
3836 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3837 TYPE_UNSIGNED (underlying_type));
3839 return underlying_type;
3842 /* Implement the __direct_bases keyword: Return the direct base classes
3843 of type */
3845 tree
3846 calculate_direct_bases (tree type)
3848 vec<tree, va_gc> *vector = make_tree_vector();
3849 tree bases_vec = NULL_TREE;
3850 vec<tree, va_gc> *base_binfos;
3851 tree binfo;
3852 unsigned i;
3854 complete_type (type);
3856 if (!NON_UNION_CLASS_TYPE_P (type))
3857 return make_tree_vec (0);
3859 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3861 /* Virtual bases are initialized first */
3862 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3864 if (BINFO_VIRTUAL_P (binfo))
3866 vec_safe_push (vector, binfo);
3870 /* Now non-virtuals */
3871 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3873 if (!BINFO_VIRTUAL_P (binfo))
3875 vec_safe_push (vector, binfo);
3880 bases_vec = make_tree_vec (vector->length ());
3882 for (i = 0; i < vector->length (); ++i)
3884 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3886 return bases_vec;
3889 /* Implement the __bases keyword: Return the base classes
3890 of type */
3892 /* Find morally non-virtual base classes by walking binfo hierarchy */
3893 /* Virtual base classes are handled separately in finish_bases */
3895 static tree
3896 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3898 /* Don't walk bases of virtual bases */
3899 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3902 static tree
3903 dfs_calculate_bases_post (tree binfo, void *data_)
3905 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3906 if (!BINFO_VIRTUAL_P (binfo))
3908 vec_safe_push (*data, BINFO_TYPE (binfo));
3910 return NULL_TREE;
3913 /* Calculates the morally non-virtual base classes of a class */
3914 static vec<tree, va_gc> *
3915 calculate_bases_helper (tree type)
3917 vec<tree, va_gc> *vector = make_tree_vector();
3919 /* Now add non-virtual base classes in order of construction */
3920 if (TYPE_BINFO (type))
3921 dfs_walk_all (TYPE_BINFO (type),
3922 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3923 return vector;
3926 tree
3927 calculate_bases (tree type)
3929 vec<tree, va_gc> *vector = make_tree_vector();
3930 tree bases_vec = NULL_TREE;
3931 unsigned i;
3932 vec<tree, va_gc> *vbases;
3933 vec<tree, va_gc> *nonvbases;
3934 tree binfo;
3936 complete_type (type);
3938 if (!NON_UNION_CLASS_TYPE_P (type))
3939 return make_tree_vec (0);
3941 /* First go through virtual base classes */
3942 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3943 vec_safe_iterate (vbases, i, &binfo); i++)
3945 vec<tree, va_gc> *vbase_bases;
3946 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3947 vec_safe_splice (vector, vbase_bases);
3948 release_tree_vector (vbase_bases);
3951 /* Now for the non-virtual bases */
3952 nonvbases = calculate_bases_helper (type);
3953 vec_safe_splice (vector, nonvbases);
3954 release_tree_vector (nonvbases);
3956 /* Note that during error recovery vector->length can even be zero. */
3957 if (vector->length () > 1)
3959 /* Last element is entire class, so don't copy */
3960 bases_vec = make_tree_vec (vector->length() - 1);
3962 for (i = 0; i < vector->length () - 1; ++i)
3963 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3965 else
3966 bases_vec = make_tree_vec (0);
3968 release_tree_vector (vector);
3969 return bases_vec;
3972 tree
3973 finish_bases (tree type, bool direct)
3975 tree bases = NULL_TREE;
3977 if (!processing_template_decl)
3979 /* Parameter packs can only be used in templates */
3980 error ("Parameter pack __bases only valid in template declaration");
3981 return error_mark_node;
3984 bases = cxx_make_type (BASES);
3985 BASES_TYPE (bases) = type;
3986 BASES_DIRECT (bases) = direct;
3987 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3989 return bases;
3992 /* Perform C++-specific checks for __builtin_offsetof before calling
3993 fold_offsetof. */
3995 tree
3996 finish_offsetof (tree expr, location_t loc)
3998 /* If we're processing a template, we can't finish the semantics yet.
3999 Otherwise we can fold the entire expression now. */
4000 if (processing_template_decl)
4002 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
4003 SET_EXPR_LOCATION (expr, loc);
4004 return expr;
4007 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4009 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4010 TREE_OPERAND (expr, 2));
4011 return error_mark_node;
4013 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4014 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4015 || TREE_TYPE (expr) == unknown_type_node)
4017 if (INDIRECT_REF_P (expr))
4018 error ("second operand of %<offsetof%> is neither a single "
4019 "identifier nor a sequence of member accesses and "
4020 "array references");
4021 else
4023 if (TREE_CODE (expr) == COMPONENT_REF
4024 || TREE_CODE (expr) == COMPOUND_EXPR)
4025 expr = TREE_OPERAND (expr, 1);
4026 error ("cannot apply %<offsetof%> to member function %qD", expr);
4028 return error_mark_node;
4030 if (REFERENCE_REF_P (expr))
4031 expr = TREE_OPERAND (expr, 0);
4032 if (TREE_CODE (expr) == COMPONENT_REF)
4034 tree object = TREE_OPERAND (expr, 0);
4035 if (!complete_type_or_else (TREE_TYPE (object), object))
4036 return error_mark_node;
4037 if (warn_invalid_offsetof
4038 && CLASS_TYPE_P (TREE_TYPE (object))
4039 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
4040 && cp_unevaluated_operand == 0)
4041 pedwarn (loc, OPT_Winvalid_offsetof,
4042 "offsetof within non-standard-layout type %qT is undefined",
4043 TREE_TYPE (object));
4045 return fold_offsetof (expr);
4048 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4049 function is broken out from the above for the benefit of the tree-ssa
4050 project. */
4052 void
4053 simplify_aggr_init_expr (tree *tp)
4055 tree aggr_init_expr = *tp;
4057 /* Form an appropriate CALL_EXPR. */
4058 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4059 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4060 tree type = TREE_TYPE (slot);
4062 tree call_expr;
4063 enum style_t { ctor, arg, pcc } style;
4065 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4066 style = ctor;
4067 #ifdef PCC_STATIC_STRUCT_RETURN
4068 else if (1)
4069 style = pcc;
4070 #endif
4071 else
4073 gcc_assert (TREE_ADDRESSABLE (type));
4074 style = arg;
4077 call_expr = build_call_array_loc (input_location,
4078 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4080 aggr_init_expr_nargs (aggr_init_expr),
4081 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4082 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4083 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
4084 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4086 if (style == ctor)
4088 /* Replace the first argument to the ctor with the address of the
4089 slot. */
4090 cxx_mark_addressable (slot);
4091 CALL_EXPR_ARG (call_expr, 0) =
4092 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4094 else if (style == arg)
4096 /* Just mark it addressable here, and leave the rest to
4097 expand_call{,_inline}. */
4098 cxx_mark_addressable (slot);
4099 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4100 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4102 else if (style == pcc)
4104 /* If we're using the non-reentrant PCC calling convention, then we
4105 need to copy the returned value out of the static buffer into the
4106 SLOT. */
4107 push_deferring_access_checks (dk_no_check);
4108 call_expr = build_aggr_init (slot, call_expr,
4109 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4110 tf_warning_or_error);
4111 pop_deferring_access_checks ();
4112 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4115 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4117 tree init = build_zero_init (type, NULL_TREE,
4118 /*static_storage_p=*/false);
4119 init = build2 (INIT_EXPR, void_type_node, slot, init);
4120 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4121 init, call_expr);
4124 *tp = call_expr;
4127 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4129 void
4130 emit_associated_thunks (tree fn)
4132 /* When we use vcall offsets, we emit thunks with the virtual
4133 functions to which they thunk. The whole point of vcall offsets
4134 is so that you can know statically the entire set of thunks that
4135 will ever be needed for a given virtual function, thereby
4136 enabling you to output all the thunks with the function itself. */
4137 if (DECL_VIRTUAL_P (fn)
4138 /* Do not emit thunks for extern template instantiations. */
4139 && ! DECL_REALLY_EXTERN (fn))
4141 tree thunk;
4143 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4145 if (!THUNK_ALIAS (thunk))
4147 use_thunk (thunk, /*emit_p=*/1);
4148 if (DECL_RESULT_THUNK_P (thunk))
4150 tree probe;
4152 for (probe = DECL_THUNKS (thunk);
4153 probe; probe = DECL_CHAIN (probe))
4154 use_thunk (probe, /*emit_p=*/1);
4157 else
4158 gcc_assert (!DECL_THUNKS (thunk));
4163 /* Generate RTL for FN. */
4165 bool
4166 expand_or_defer_fn_1 (tree fn)
4168 /* When the parser calls us after finishing the body of a template
4169 function, we don't really want to expand the body. */
4170 if (processing_template_decl)
4172 /* Normally, collection only occurs in rest_of_compilation. So,
4173 if we don't collect here, we never collect junk generated
4174 during the processing of templates until we hit a
4175 non-template function. It's not safe to do this inside a
4176 nested class, though, as the parser may have local state that
4177 is not a GC root. */
4178 if (!function_depth)
4179 ggc_collect ();
4180 return false;
4183 gcc_assert (DECL_SAVED_TREE (fn));
4185 /* We make a decision about linkage for these functions at the end
4186 of the compilation. Until that point, we do not want the back
4187 end to output them -- but we do want it to see the bodies of
4188 these functions so that it can inline them as appropriate. */
4189 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4191 if (DECL_INTERFACE_KNOWN (fn))
4192 /* We've already made a decision as to how this function will
4193 be handled. */;
4194 else if (!at_eof)
4195 tentative_decl_linkage (fn);
4196 else
4197 import_export_decl (fn);
4199 /* If the user wants us to keep all inline functions, then mark
4200 this function as needed so that finish_file will make sure to
4201 output it later. Similarly, all dllexport'd functions must
4202 be emitted; there may be callers in other DLLs. */
4203 if (DECL_DECLARED_INLINE_P (fn)
4204 && !DECL_REALLY_EXTERN (fn)
4205 && (flag_keep_inline_functions
4206 || (flag_keep_inline_dllexport
4207 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4209 mark_needed (fn);
4210 DECL_EXTERNAL (fn) = 0;
4214 /* If this is a constructor or destructor body, we have to clone
4215 it. */
4216 if (maybe_clone_body (fn))
4218 /* We don't want to process FN again, so pretend we've written
4219 it out, even though we haven't. */
4220 TREE_ASM_WRITTEN (fn) = 1;
4221 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4222 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4223 DECL_SAVED_TREE (fn) = NULL_TREE;
4224 return false;
4227 /* There's no reason to do any of the work here if we're only doing
4228 semantic analysis; this code just generates RTL. */
4229 if (flag_syntax_only)
4230 return false;
4232 return true;
4235 void
4236 expand_or_defer_fn (tree fn)
4238 if (expand_or_defer_fn_1 (fn))
4240 function_depth++;
4242 /* Expand or defer, at the whim of the compilation unit manager. */
4243 cgraph_node::finalize_function (fn, function_depth > 1);
4244 emit_associated_thunks (fn);
4246 function_depth--;
4250 struct nrv_data
4252 nrv_data () : visited (37) {}
4254 tree var;
4255 tree result;
4256 hash_table<nofree_ptr_hash <tree_node> > visited;
4259 /* Helper function for walk_tree, used by finalize_nrv below. */
4261 static tree
4262 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4264 struct nrv_data *dp = (struct nrv_data *)data;
4265 tree_node **slot;
4267 /* No need to walk into types. There wouldn't be any need to walk into
4268 non-statements, except that we have to consider STMT_EXPRs. */
4269 if (TYPE_P (*tp))
4270 *walk_subtrees = 0;
4271 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4272 but differs from using NULL_TREE in that it indicates that we care
4273 about the value of the RESULT_DECL. */
4274 else if (TREE_CODE (*tp) == RETURN_EXPR)
4275 TREE_OPERAND (*tp, 0) = dp->result;
4276 /* Change all cleanups for the NRV to only run when an exception is
4277 thrown. */
4278 else if (TREE_CODE (*tp) == CLEANUP_STMT
4279 && CLEANUP_DECL (*tp) == dp->var)
4280 CLEANUP_EH_ONLY (*tp) = 1;
4281 /* Replace the DECL_EXPR for the NRV with an initialization of the
4282 RESULT_DECL, if needed. */
4283 else if (TREE_CODE (*tp) == DECL_EXPR
4284 && DECL_EXPR_DECL (*tp) == dp->var)
4286 tree init;
4287 if (DECL_INITIAL (dp->var)
4288 && DECL_INITIAL (dp->var) != error_mark_node)
4289 init = build2 (INIT_EXPR, void_type_node, dp->result,
4290 DECL_INITIAL (dp->var));
4291 else
4292 init = build_empty_stmt (EXPR_LOCATION (*tp));
4293 DECL_INITIAL (dp->var) = NULL_TREE;
4294 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4295 *tp = init;
4297 /* And replace all uses of the NRV with the RESULT_DECL. */
4298 else if (*tp == dp->var)
4299 *tp = dp->result;
4301 /* Avoid walking into the same tree more than once. Unfortunately, we
4302 can't just use walk_tree_without duplicates because it would only call
4303 us for the first occurrence of dp->var in the function body. */
4304 slot = dp->visited.find_slot (*tp, INSERT);
4305 if (*slot)
4306 *walk_subtrees = 0;
4307 else
4308 *slot = *tp;
4310 /* Keep iterating. */
4311 return NULL_TREE;
4314 /* Called from finish_function to implement the named return value
4315 optimization by overriding all the RETURN_EXPRs and pertinent
4316 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4317 RESULT_DECL for the function. */
4319 void
4320 finalize_nrv (tree *tp, tree var, tree result)
4322 struct nrv_data data;
4324 /* Copy name from VAR to RESULT. */
4325 DECL_NAME (result) = DECL_NAME (var);
4326 /* Don't forget that we take its address. */
4327 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4328 /* Finally set DECL_VALUE_EXPR to avoid assigning
4329 a stack slot at -O0 for the original var and debug info
4330 uses RESULT location for VAR. */
4331 SET_DECL_VALUE_EXPR (var, result);
4332 DECL_HAS_VALUE_EXPR_P (var) = 1;
4334 data.var = var;
4335 data.result = result;
4336 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4339 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4341 bool
4342 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4343 bool need_copy_ctor, bool need_copy_assignment,
4344 bool need_dtor)
4346 int save_errorcount = errorcount;
4347 tree info, t;
4349 /* Always allocate 3 elements for simplicity. These are the
4350 function decls for the ctor, dtor, and assignment op.
4351 This layout is known to the three lang hooks,
4352 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4353 and cxx_omp_clause_assign_op. */
4354 info = make_tree_vec (3);
4355 CP_OMP_CLAUSE_INFO (c) = info;
4357 if (need_default_ctor || need_copy_ctor)
4359 if (need_default_ctor)
4360 t = get_default_ctor (type);
4361 else
4362 t = get_copy_ctor (type, tf_warning_or_error);
4364 if (t && !trivial_fn_p (t))
4365 TREE_VEC_ELT (info, 0) = t;
4368 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4369 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4371 if (need_copy_assignment)
4373 t = get_copy_assign (type);
4375 if (t && !trivial_fn_p (t))
4376 TREE_VEC_ELT (info, 2) = t;
4379 return errorcount != save_errorcount;
4382 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4383 FIELD_DECL, otherwise return DECL itself. */
4385 static tree
4386 omp_clause_decl_field (tree decl)
4388 if (VAR_P (decl)
4389 && DECL_HAS_VALUE_EXPR_P (decl)
4390 && DECL_ARTIFICIAL (decl)
4391 && DECL_LANG_SPECIFIC (decl)
4392 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4394 tree f = DECL_VALUE_EXPR (decl);
4395 if (TREE_CODE (f) == INDIRECT_REF)
4396 f = TREE_OPERAND (f, 0);
4397 if (TREE_CODE (f) == COMPONENT_REF)
4399 f = TREE_OPERAND (f, 1);
4400 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4401 return f;
4404 return NULL_TREE;
4407 /* Adjust DECL if needed for printing using %qE. */
4409 static tree
4410 omp_clause_printable_decl (tree decl)
4412 tree t = omp_clause_decl_field (decl);
4413 if (t)
4414 return t;
4415 return decl;
4418 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4419 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4420 privatization. */
4422 static void
4423 omp_note_field_privatization (tree f, tree t)
4425 if (!omp_private_member_map)
4426 omp_private_member_map = new hash_map<tree, tree>;
4427 tree &v = omp_private_member_map->get_or_insert (f);
4428 if (v == NULL_TREE)
4430 v = t;
4431 omp_private_member_vec.safe_push (f);
4432 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4433 omp_private_member_vec.safe_push (integer_zero_node);
4437 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4438 dummy VAR_DECL. */
4440 tree
4441 omp_privatize_field (tree t, bool shared)
4443 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4444 if (m == error_mark_node)
4445 return error_mark_node;
4446 if (!omp_private_member_map && !shared)
4447 omp_private_member_map = new hash_map<tree, tree>;
4448 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4450 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4451 m = TREE_OPERAND (m, 0);
4453 tree vb = NULL_TREE;
4454 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4455 if (v == NULL_TREE)
4457 v = create_temporary_var (TREE_TYPE (m));
4458 if (!DECL_LANG_SPECIFIC (v))
4459 retrofit_lang_decl (v);
4460 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4461 SET_DECL_VALUE_EXPR (v, m);
4462 DECL_HAS_VALUE_EXPR_P (v) = 1;
4463 if (!shared)
4464 omp_private_member_vec.safe_push (t);
4466 return v;
4469 /* Helper function for handle_omp_array_sections. Called recursively
4470 to handle multiple array-section-subscripts. C is the clause,
4471 T current expression (initially OMP_CLAUSE_DECL), which is either
4472 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4473 expression if specified, TREE_VALUE length expression if specified,
4474 TREE_CHAIN is what it has been specified after, or some decl.
4475 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4476 set to true if any of the array-section-subscript could have length
4477 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4478 first array-section-subscript which is known not to have length
4479 of one. Given say:
4480 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4481 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4482 all are or may have length of 1, array-section-subscript [:2] is the
4483 first one known not to have length 1. For array-section-subscript
4484 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4485 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4486 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4487 case though, as some lengths could be zero. */
4489 static tree
4490 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4491 bool &maybe_zero_len, unsigned int &first_non_one,
4492 bool is_omp)
4494 tree ret, low_bound, length, type;
4495 if (TREE_CODE (t) != TREE_LIST)
4497 if (error_operand_p (t))
4498 return error_mark_node;
4499 if (REFERENCE_REF_P (t)
4500 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4501 t = TREE_OPERAND (t, 0);
4502 ret = t;
4503 if (TREE_CODE (t) == COMPONENT_REF
4504 && is_omp
4505 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4507 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4508 && !type_dependent_expression_p (t))
4510 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4512 error_at (OMP_CLAUSE_LOCATION (c),
4513 "bit-field %qE in %qs clause",
4514 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4515 return error_mark_node;
4517 while (TREE_CODE (t) == COMPONENT_REF)
4519 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4521 error_at (OMP_CLAUSE_LOCATION (c),
4522 "%qE is a member of a union", t);
4523 return error_mark_node;
4525 t = TREE_OPERAND (t, 0);
4528 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4530 if (processing_template_decl)
4531 return NULL_TREE;
4532 if (DECL_P (t))
4533 error_at (OMP_CLAUSE_LOCATION (c),
4534 "%qD is not a variable in %qs clause", t,
4535 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4536 else
4537 error_at (OMP_CLAUSE_LOCATION (c),
4538 "%qE is not a variable in %qs clause", t,
4539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4540 return error_mark_node;
4542 else if (TREE_CODE (t) == PARM_DECL
4543 && DECL_ARTIFICIAL (t)
4544 && DECL_NAME (t) == this_identifier)
4546 error_at (OMP_CLAUSE_LOCATION (c),
4547 "%<this%> allowed in OpenMP only in %<declare simd%>"
4548 " clauses");
4549 return error_mark_node;
4551 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4552 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4554 error_at (OMP_CLAUSE_LOCATION (c),
4555 "%qD is threadprivate variable in %qs clause", t,
4556 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4557 return error_mark_node;
4559 if (type_dependent_expression_p (ret))
4560 return NULL_TREE;
4561 ret = convert_from_reference (ret);
4562 return ret;
4565 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4566 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4567 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4568 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4569 maybe_zero_len, first_non_one, is_omp);
4570 if (ret == error_mark_node || ret == NULL_TREE)
4571 return ret;
4573 type = TREE_TYPE (ret);
4574 low_bound = TREE_PURPOSE (t);
4575 length = TREE_VALUE (t);
4576 if ((low_bound && type_dependent_expression_p (low_bound))
4577 || (length && type_dependent_expression_p (length)))
4578 return NULL_TREE;
4580 if (low_bound == error_mark_node || length == error_mark_node)
4581 return error_mark_node;
4583 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4585 error_at (OMP_CLAUSE_LOCATION (c),
4586 "low bound %qE of array section does not have integral type",
4587 low_bound);
4588 return error_mark_node;
4590 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4592 error_at (OMP_CLAUSE_LOCATION (c),
4593 "length %qE of array section does not have integral type",
4594 length);
4595 return error_mark_node;
4597 if (low_bound)
4598 low_bound = mark_rvalue_use (low_bound);
4599 if (length)
4600 length = mark_rvalue_use (length);
4601 /* We need to reduce to real constant-values for checks below. */
4602 if (length)
4603 length = fold_simple (length);
4604 if (low_bound)
4605 low_bound = fold_simple (low_bound);
4606 if (low_bound
4607 && TREE_CODE (low_bound) == INTEGER_CST
4608 && TYPE_PRECISION (TREE_TYPE (low_bound))
4609 > TYPE_PRECISION (sizetype))
4610 low_bound = fold_convert (sizetype, low_bound);
4611 if (length
4612 && TREE_CODE (length) == INTEGER_CST
4613 && TYPE_PRECISION (TREE_TYPE (length))
4614 > TYPE_PRECISION (sizetype))
4615 length = fold_convert (sizetype, length);
4616 if (low_bound == NULL_TREE)
4617 low_bound = integer_zero_node;
4619 if (length != NULL_TREE)
4621 if (!integer_nonzerop (length))
4623 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4624 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4626 if (integer_zerop (length))
4628 error_at (OMP_CLAUSE_LOCATION (c),
4629 "zero length array section in %qs clause",
4630 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4631 return error_mark_node;
4634 else
4635 maybe_zero_len = true;
4637 if (first_non_one == types.length ()
4638 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4639 first_non_one++;
4641 if (TREE_CODE (type) == ARRAY_TYPE)
4643 if (length == NULL_TREE
4644 && (TYPE_DOMAIN (type) == NULL_TREE
4645 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4647 error_at (OMP_CLAUSE_LOCATION (c),
4648 "for unknown bound array type length expression must "
4649 "be specified");
4650 return error_mark_node;
4652 if (TREE_CODE (low_bound) == INTEGER_CST
4653 && tree_int_cst_sgn (low_bound) == -1)
4655 error_at (OMP_CLAUSE_LOCATION (c),
4656 "negative low bound in array section in %qs clause",
4657 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4658 return error_mark_node;
4660 if (length != NULL_TREE
4661 && TREE_CODE (length) == INTEGER_CST
4662 && tree_int_cst_sgn (length) == -1)
4664 error_at (OMP_CLAUSE_LOCATION (c),
4665 "negative length in array section in %qs clause",
4666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4667 return error_mark_node;
4669 if (TYPE_DOMAIN (type)
4670 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4671 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4672 == INTEGER_CST)
4674 tree size = size_binop (PLUS_EXPR,
4675 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4676 size_one_node);
4677 if (TREE_CODE (low_bound) == INTEGER_CST)
4679 if (tree_int_cst_lt (size, low_bound))
4681 error_at (OMP_CLAUSE_LOCATION (c),
4682 "low bound %qE above array section size "
4683 "in %qs clause", low_bound,
4684 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4685 return error_mark_node;
4687 if (tree_int_cst_equal (size, low_bound))
4689 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4690 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4692 error_at (OMP_CLAUSE_LOCATION (c),
4693 "zero length array section in %qs clause",
4694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4695 return error_mark_node;
4697 maybe_zero_len = true;
4699 else if (length == NULL_TREE
4700 && first_non_one == types.length ()
4701 && tree_int_cst_equal
4702 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4703 low_bound))
4704 first_non_one++;
4706 else if (length == NULL_TREE)
4708 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4709 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4710 maybe_zero_len = true;
4711 if (first_non_one == types.length ())
4712 first_non_one++;
4714 if (length && TREE_CODE (length) == INTEGER_CST)
4716 if (tree_int_cst_lt (size, length))
4718 error_at (OMP_CLAUSE_LOCATION (c),
4719 "length %qE above array section size "
4720 "in %qs clause", length,
4721 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4722 return error_mark_node;
4724 if (TREE_CODE (low_bound) == INTEGER_CST)
4726 tree lbpluslen
4727 = size_binop (PLUS_EXPR,
4728 fold_convert (sizetype, low_bound),
4729 fold_convert (sizetype, length));
4730 if (TREE_CODE (lbpluslen) == INTEGER_CST
4731 && tree_int_cst_lt (size, lbpluslen))
4733 error_at (OMP_CLAUSE_LOCATION (c),
4734 "high bound %qE above array section size "
4735 "in %qs clause", lbpluslen,
4736 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4737 return error_mark_node;
4742 else if (length == NULL_TREE)
4744 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4745 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4746 maybe_zero_len = true;
4747 if (first_non_one == types.length ())
4748 first_non_one++;
4751 /* For [lb:] we will need to evaluate lb more than once. */
4752 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4754 tree lb = cp_save_expr (low_bound);
4755 if (lb != low_bound)
4757 TREE_PURPOSE (t) = lb;
4758 low_bound = lb;
4762 else if (TREE_CODE (type) == POINTER_TYPE)
4764 if (length == NULL_TREE)
4766 error_at (OMP_CLAUSE_LOCATION (c),
4767 "for pointer type length expression must be specified");
4768 return error_mark_node;
4770 if (length != NULL_TREE
4771 && TREE_CODE (length) == INTEGER_CST
4772 && tree_int_cst_sgn (length) == -1)
4774 error_at (OMP_CLAUSE_LOCATION (c),
4775 "negative length in array section in %qs clause",
4776 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4777 return error_mark_node;
4779 /* If there is a pointer type anywhere but in the very first
4780 array-section-subscript, the array section can't be contiguous. */
4781 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4782 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4784 error_at (OMP_CLAUSE_LOCATION (c),
4785 "array section is not contiguous in %qs clause",
4786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4787 return error_mark_node;
4790 else
4792 error_at (OMP_CLAUSE_LOCATION (c),
4793 "%qE does not have pointer or array type", ret);
4794 return error_mark_node;
4796 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4797 types.safe_push (TREE_TYPE (ret));
4798 /* We will need to evaluate lb more than once. */
4799 tree lb = cp_save_expr (low_bound);
4800 if (lb != low_bound)
4802 TREE_PURPOSE (t) = lb;
4803 low_bound = lb;
4805 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4806 return ret;
4809 /* Handle array sections for clause C. */
4811 static bool
4812 handle_omp_array_sections (tree c, bool is_omp)
4814 bool maybe_zero_len = false;
4815 unsigned int first_non_one = 0;
4816 auto_vec<tree, 10> types;
4817 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4818 maybe_zero_len, first_non_one,
4819 is_omp);
4820 if (first == error_mark_node)
4821 return true;
4822 if (first == NULL_TREE)
4823 return false;
4824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4826 tree t = OMP_CLAUSE_DECL (c);
4827 tree tem = NULL_TREE;
4828 if (processing_template_decl)
4829 return false;
4830 /* Need to evaluate side effects in the length expressions
4831 if any. */
4832 while (TREE_CODE (t) == TREE_LIST)
4834 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4836 if (tem == NULL_TREE)
4837 tem = TREE_VALUE (t);
4838 else
4839 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4840 TREE_VALUE (t), tem);
4842 t = TREE_CHAIN (t);
4844 if (tem)
4845 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4846 OMP_CLAUSE_DECL (c) = first;
4848 else
4850 unsigned int num = types.length (), i;
4851 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4852 tree condition = NULL_TREE;
4854 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4855 maybe_zero_len = true;
4856 if (processing_template_decl && maybe_zero_len)
4857 return false;
4859 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4860 t = TREE_CHAIN (t))
4862 tree low_bound = TREE_PURPOSE (t);
4863 tree length = TREE_VALUE (t);
4865 i--;
4866 if (low_bound
4867 && TREE_CODE (low_bound) == INTEGER_CST
4868 && TYPE_PRECISION (TREE_TYPE (low_bound))
4869 > TYPE_PRECISION (sizetype))
4870 low_bound = fold_convert (sizetype, low_bound);
4871 if (length
4872 && TREE_CODE (length) == INTEGER_CST
4873 && TYPE_PRECISION (TREE_TYPE (length))
4874 > TYPE_PRECISION (sizetype))
4875 length = fold_convert (sizetype, length);
4876 if (low_bound == NULL_TREE)
4877 low_bound = integer_zero_node;
4878 if (!maybe_zero_len && i > first_non_one)
4880 if (integer_nonzerop (low_bound))
4881 goto do_warn_noncontiguous;
4882 if (length != NULL_TREE
4883 && TREE_CODE (length) == INTEGER_CST
4884 && TYPE_DOMAIN (types[i])
4885 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4886 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4887 == INTEGER_CST)
4889 tree size;
4890 size = size_binop (PLUS_EXPR,
4891 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4892 size_one_node);
4893 if (!tree_int_cst_equal (length, size))
4895 do_warn_noncontiguous:
4896 error_at (OMP_CLAUSE_LOCATION (c),
4897 "array section is not contiguous in %qs "
4898 "clause",
4899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4900 return true;
4903 if (!processing_template_decl
4904 && length != NULL_TREE
4905 && TREE_SIDE_EFFECTS (length))
4907 if (side_effects == NULL_TREE)
4908 side_effects = length;
4909 else
4910 side_effects = build2 (COMPOUND_EXPR,
4911 TREE_TYPE (side_effects),
4912 length, side_effects);
4915 else if (processing_template_decl)
4916 continue;
4917 else
4919 tree l;
4921 if (i > first_non_one
4922 && ((length && integer_nonzerop (length))
4923 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4924 continue;
4925 if (length)
4926 l = fold_convert (sizetype, length);
4927 else
4929 l = size_binop (PLUS_EXPR,
4930 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4931 size_one_node);
4932 l = size_binop (MINUS_EXPR, l,
4933 fold_convert (sizetype, low_bound));
4935 if (i > first_non_one)
4937 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4938 size_zero_node);
4939 if (condition == NULL_TREE)
4940 condition = l;
4941 else
4942 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4943 l, condition);
4945 else if (size == NULL_TREE)
4947 size = size_in_bytes (TREE_TYPE (types[i]));
4948 tree eltype = TREE_TYPE (types[num - 1]);
4949 while (TREE_CODE (eltype) == ARRAY_TYPE)
4950 eltype = TREE_TYPE (eltype);
4951 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4952 size = size_binop (EXACT_DIV_EXPR, size,
4953 size_in_bytes (eltype));
4954 size = size_binop (MULT_EXPR, size, l);
4955 if (condition)
4956 size = fold_build3 (COND_EXPR, sizetype, condition,
4957 size, size_zero_node);
4959 else
4960 size = size_binop (MULT_EXPR, size, l);
4963 if (!processing_template_decl)
4965 if (side_effects)
4966 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4967 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4969 size = size_binop (MINUS_EXPR, size, size_one_node);
4970 tree index_type = build_index_type (size);
4971 tree eltype = TREE_TYPE (first);
4972 while (TREE_CODE (eltype) == ARRAY_TYPE)
4973 eltype = TREE_TYPE (eltype);
4974 tree type = build_array_type (eltype, index_type);
4975 tree ptype = build_pointer_type (eltype);
4976 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4977 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4978 t = convert_from_reference (t);
4979 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4980 t = build_fold_addr_expr (t);
4981 tree t2 = build_fold_addr_expr (first);
4982 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4983 ptrdiff_type_node, t2);
4984 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4985 ptrdiff_type_node, t2,
4986 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4987 ptrdiff_type_node, t));
4988 if (tree_fits_shwi_p (t2))
4989 t = build2 (MEM_REF, type, t,
4990 build_int_cst (ptype, tree_to_shwi (t2)));
4991 else
4993 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4994 sizetype, t2);
4995 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4996 TREE_TYPE (t), t, t2);
4997 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4999 OMP_CLAUSE_DECL (c) = t;
5000 return false;
5002 OMP_CLAUSE_DECL (c) = first;
5003 OMP_CLAUSE_SIZE (c) = size;
5004 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5005 || (TREE_CODE (t) == COMPONENT_REF
5006 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5007 return false;
5008 if (is_omp)
5009 switch (OMP_CLAUSE_MAP_KIND (c))
5011 case GOMP_MAP_ALLOC:
5012 case GOMP_MAP_TO:
5013 case GOMP_MAP_FROM:
5014 case GOMP_MAP_TOFROM:
5015 case GOMP_MAP_ALWAYS_TO:
5016 case GOMP_MAP_ALWAYS_FROM:
5017 case GOMP_MAP_ALWAYS_TOFROM:
5018 case GOMP_MAP_RELEASE:
5019 case GOMP_MAP_DELETE:
5020 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5021 break;
5022 default:
5023 break;
5025 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5026 OMP_CLAUSE_MAP);
5027 if (!is_omp)
5028 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5029 else if (TREE_CODE (t) == COMPONENT_REF)
5030 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5031 else if (REFERENCE_REF_P (t)
5032 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5034 t = TREE_OPERAND (t, 0);
5035 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5037 else
5038 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5039 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5040 && !cxx_mark_addressable (t))
5041 return false;
5042 OMP_CLAUSE_DECL (c2) = t;
5043 t = build_fold_addr_expr (first);
5044 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5045 ptrdiff_type_node, t);
5046 tree ptr = OMP_CLAUSE_DECL (c2);
5047 ptr = convert_from_reference (ptr);
5048 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5049 ptr = build_fold_addr_expr (ptr);
5050 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5051 ptrdiff_type_node, t,
5052 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5053 ptrdiff_type_node, ptr));
5054 OMP_CLAUSE_SIZE (c2) = t;
5055 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5056 OMP_CLAUSE_CHAIN (c) = c2;
5057 ptr = OMP_CLAUSE_DECL (c2);
5058 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5059 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5060 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5062 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5063 OMP_CLAUSE_MAP);
5064 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5065 OMP_CLAUSE_DECL (c3) = ptr;
5066 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5067 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5068 else
5069 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5070 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5071 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5072 OMP_CLAUSE_CHAIN (c2) = c3;
5076 return false;
5079 /* Return identifier to look up for omp declare reduction. */
5081 tree
5082 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5084 const char *p = NULL;
5085 const char *m = NULL;
5086 switch (reduction_code)
5088 case PLUS_EXPR:
5089 case MULT_EXPR:
5090 case MINUS_EXPR:
5091 case BIT_AND_EXPR:
5092 case BIT_XOR_EXPR:
5093 case BIT_IOR_EXPR:
5094 case TRUTH_ANDIF_EXPR:
5095 case TRUTH_ORIF_EXPR:
5096 reduction_id = ansi_opname (reduction_code);
5097 break;
5098 case MIN_EXPR:
5099 p = "min";
5100 break;
5101 case MAX_EXPR:
5102 p = "max";
5103 break;
5104 default:
5105 break;
5108 if (p == NULL)
5110 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5111 return error_mark_node;
5112 p = IDENTIFIER_POINTER (reduction_id);
5115 if (type != NULL_TREE)
5116 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5118 const char prefix[] = "omp declare reduction ";
5119 size_t lenp = sizeof (prefix);
5120 if (strncmp (p, prefix, lenp - 1) == 0)
5121 lenp = 1;
5122 size_t len = strlen (p);
5123 size_t lenm = m ? strlen (m) + 1 : 0;
5124 char *name = XALLOCAVEC (char, lenp + len + lenm);
5125 if (lenp > 1)
5126 memcpy (name, prefix, lenp - 1);
5127 memcpy (name + lenp - 1, p, len + 1);
5128 if (m)
5130 name[lenp + len - 1] = '~';
5131 memcpy (name + lenp + len, m, lenm);
5133 return get_identifier (name);
5136 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5137 FUNCTION_DECL or NULL_TREE if not found. */
5139 static tree
5140 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5141 vec<tree> *ambiguousp)
5143 tree orig_id = id;
5144 tree baselink = NULL_TREE;
5145 if (identifier_p (id))
5147 cp_id_kind idk;
5148 bool nonint_cst_expression_p;
5149 const char *error_msg;
5150 id = omp_reduction_id (ERROR_MARK, id, type);
5151 tree decl = lookup_name (id);
5152 if (decl == NULL_TREE)
5153 decl = error_mark_node;
5154 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5155 &nonint_cst_expression_p, false, true, false,
5156 false, &error_msg, loc);
5157 if (idk == CP_ID_KIND_UNQUALIFIED
5158 && identifier_p (id))
5160 vec<tree, va_gc> *args = NULL;
5161 vec_safe_push (args, build_reference_type (type));
5162 id = perform_koenig_lookup (id, args, tf_none);
5165 else if (TREE_CODE (id) == SCOPE_REF)
5166 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5167 omp_reduction_id (ERROR_MARK,
5168 TREE_OPERAND (id, 1),
5169 type),
5170 false, false);
5171 tree fns = id;
5172 if (id && is_overloaded_fn (id))
5173 id = get_fns (id);
5174 for (; id; id = OVL_NEXT (id))
5176 tree fndecl = OVL_CURRENT (id);
5177 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5179 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5180 if (same_type_p (TREE_TYPE (argtype), type))
5181 break;
5184 if (id && BASELINK_P (fns))
5186 if (baselinkp)
5187 *baselinkp = fns;
5188 else
5189 baselink = fns;
5191 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5193 vec<tree> ambiguous = vNULL;
5194 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5195 unsigned int ix;
5196 if (ambiguousp == NULL)
5197 ambiguousp = &ambiguous;
5198 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5200 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5201 baselinkp ? baselinkp : &baselink,
5202 ambiguousp);
5203 if (id == NULL_TREE)
5204 continue;
5205 if (!ambiguousp->is_empty ())
5206 ambiguousp->safe_push (id);
5207 else if (ret != NULL_TREE)
5209 ambiguousp->safe_push (ret);
5210 ambiguousp->safe_push (id);
5211 ret = NULL_TREE;
5213 else
5214 ret = id;
5216 if (ambiguousp != &ambiguous)
5217 return ret;
5218 if (!ambiguous.is_empty ())
5220 const char *str = _("candidates are:");
5221 unsigned int idx;
5222 tree udr;
5223 error_at (loc, "user defined reduction lookup is ambiguous");
5224 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5226 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
5227 if (idx == 0)
5228 str = get_spaces (str);
5230 ambiguous.release ();
5231 ret = error_mark_node;
5232 baselink = NULL_TREE;
5234 id = ret;
5236 if (id && baselink)
5237 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5238 id, id, tf_warning_or_error);
5239 return id;
5242 /* Helper function for cp_parser_omp_declare_reduction_exprs
5243 and tsubst_omp_udr.
5244 Remove CLEANUP_STMT for data (omp_priv variable).
5245 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5246 DECL_EXPR. */
5248 tree
5249 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5251 if (TYPE_P (*tp))
5252 *walk_subtrees = 0;
5253 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5254 *tp = CLEANUP_BODY (*tp);
5255 else if (TREE_CODE (*tp) == DECL_EXPR)
5257 tree decl = DECL_EXPR_DECL (*tp);
5258 if (!processing_template_decl
5259 && decl == (tree) data
5260 && DECL_INITIAL (decl)
5261 && DECL_INITIAL (decl) != error_mark_node)
5263 tree list = NULL_TREE;
5264 append_to_statement_list_force (*tp, &list);
5265 tree init_expr = build2 (INIT_EXPR, void_type_node,
5266 decl, DECL_INITIAL (decl));
5267 DECL_INITIAL (decl) = NULL_TREE;
5268 append_to_statement_list_force (init_expr, &list);
5269 *tp = list;
5272 return NULL_TREE;
5275 /* Data passed from cp_check_omp_declare_reduction to
5276 cp_check_omp_declare_reduction_r. */
5278 struct cp_check_omp_declare_reduction_data
5280 location_t loc;
5281 tree stmts[7];
5282 bool combiner_p;
5285 /* Helper function for cp_check_omp_declare_reduction, called via
5286 cp_walk_tree. */
5288 static tree
5289 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5291 struct cp_check_omp_declare_reduction_data *udr_data
5292 = (struct cp_check_omp_declare_reduction_data *) data;
5293 if (SSA_VAR_P (*tp)
5294 && !DECL_ARTIFICIAL (*tp)
5295 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5296 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5298 location_t loc = udr_data->loc;
5299 if (udr_data->combiner_p)
5300 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5301 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5302 *tp);
5303 else
5304 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5305 "to variable %qD which is not %<omp_priv%> nor "
5306 "%<omp_orig%>",
5307 *tp);
5308 return *tp;
5310 return NULL_TREE;
5313 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5315 void
5316 cp_check_omp_declare_reduction (tree udr)
5318 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5319 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5320 type = TREE_TYPE (type);
5321 int i;
5322 location_t loc = DECL_SOURCE_LOCATION (udr);
5324 if (type == error_mark_node)
5325 return;
5326 if (ARITHMETIC_TYPE_P (type))
5328 static enum tree_code predef_codes[]
5329 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5330 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5331 for (i = 0; i < 8; i++)
5333 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5334 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5335 const char *n2 = IDENTIFIER_POINTER (id);
5336 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5337 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5338 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5339 break;
5342 if (i == 8
5343 && TREE_CODE (type) != COMPLEX_EXPR)
5345 const char prefix_minmax[] = "omp declare reduction m";
5346 size_t prefix_size = sizeof (prefix_minmax) - 1;
5347 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5348 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5349 prefix_minmax, prefix_size) == 0
5350 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5351 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5352 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5353 i = 0;
5355 if (i < 8)
5357 error_at (loc, "predeclared arithmetic type %qT in "
5358 "%<#pragma omp declare reduction%>", type);
5359 return;
5362 else if (TREE_CODE (type) == FUNCTION_TYPE
5363 || TREE_CODE (type) == METHOD_TYPE
5364 || TREE_CODE (type) == ARRAY_TYPE)
5366 error_at (loc, "function or array type %qT in "
5367 "%<#pragma omp declare reduction%>", type);
5368 return;
5370 else if (TREE_CODE (type) == REFERENCE_TYPE)
5372 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5373 type);
5374 return;
5376 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5378 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5379 "%<#pragma omp declare reduction%>", type);
5380 return;
5383 tree body = DECL_SAVED_TREE (udr);
5384 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5385 return;
5387 tree_stmt_iterator tsi;
5388 struct cp_check_omp_declare_reduction_data data;
5389 memset (data.stmts, 0, sizeof data.stmts);
5390 for (i = 0, tsi = tsi_start (body);
5391 i < 7 && !tsi_end_p (tsi);
5392 i++, tsi_next (&tsi))
5393 data.stmts[i] = tsi_stmt (tsi);
5394 data.loc = loc;
5395 gcc_assert (tsi_end_p (tsi));
5396 if (i >= 3)
5398 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5399 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5400 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5401 return;
5402 data.combiner_p = true;
5403 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5404 &data, NULL))
5405 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5407 if (i >= 6)
5409 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5410 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5411 data.combiner_p = false;
5412 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5413 &data, NULL)
5414 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5415 cp_check_omp_declare_reduction_r, &data, NULL))
5416 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5417 if (i == 7)
5418 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5422 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5423 an inline call. But, remap
5424 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5425 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5427 static tree
5428 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5429 tree decl, tree placeholder)
5431 copy_body_data id;
5432 hash_map<tree, tree> decl_map;
5434 decl_map.put (omp_decl1, placeholder);
5435 decl_map.put (omp_decl2, decl);
5436 memset (&id, 0, sizeof (id));
5437 id.src_fn = DECL_CONTEXT (omp_decl1);
5438 id.dst_fn = current_function_decl;
5439 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5440 id.decl_map = &decl_map;
5442 id.copy_decl = copy_decl_no_change;
5443 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5444 id.transform_new_cfg = true;
5445 id.transform_return_to_modify = false;
5446 id.transform_lang_insert_block = NULL;
5447 id.eh_lp_nr = 0;
5448 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5449 return stmt;
5452 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5453 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5455 static tree
5456 find_omp_placeholder_r (tree *tp, int *, void *data)
5458 if (*tp == (tree) data)
5459 return *tp;
5460 return NULL_TREE;
5463 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5464 Return true if there is some error and the clause should be removed. */
5466 static bool
5467 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5469 tree t = OMP_CLAUSE_DECL (c);
5470 bool predefined = false;
5471 if (TREE_CODE (t) == TREE_LIST)
5473 gcc_assert (processing_template_decl);
5474 return false;
5476 tree type = TREE_TYPE (t);
5477 if (TREE_CODE (t) == MEM_REF)
5478 type = TREE_TYPE (type);
5479 if (TREE_CODE (type) == REFERENCE_TYPE)
5480 type = TREE_TYPE (type);
5481 if (TREE_CODE (type) == ARRAY_TYPE)
5483 tree oatype = type;
5484 gcc_assert (TREE_CODE (t) != MEM_REF);
5485 while (TREE_CODE (type) == ARRAY_TYPE)
5486 type = TREE_TYPE (type);
5487 if (!processing_template_decl)
5489 t = require_complete_type (t);
5490 if (t == error_mark_node)
5491 return true;
5492 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5493 TYPE_SIZE_UNIT (type));
5494 if (integer_zerop (size))
5496 error ("%qE in %<reduction%> clause is a zero size array",
5497 omp_clause_printable_decl (t));
5498 return true;
5500 size = size_binop (MINUS_EXPR, size, size_one_node);
5501 tree index_type = build_index_type (size);
5502 tree atype = build_array_type (type, index_type);
5503 tree ptype = build_pointer_type (type);
5504 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5505 t = build_fold_addr_expr (t);
5506 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5507 OMP_CLAUSE_DECL (c) = t;
5510 if (type == error_mark_node)
5511 return true;
5512 else if (ARITHMETIC_TYPE_P (type))
5513 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5515 case PLUS_EXPR:
5516 case MULT_EXPR:
5517 case MINUS_EXPR:
5518 predefined = true;
5519 break;
5520 case MIN_EXPR:
5521 case MAX_EXPR:
5522 if (TREE_CODE (type) == COMPLEX_TYPE)
5523 break;
5524 predefined = true;
5525 break;
5526 case BIT_AND_EXPR:
5527 case BIT_IOR_EXPR:
5528 case BIT_XOR_EXPR:
5529 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5530 break;
5531 predefined = true;
5532 break;
5533 case TRUTH_ANDIF_EXPR:
5534 case TRUTH_ORIF_EXPR:
5535 if (FLOAT_TYPE_P (type))
5536 break;
5537 predefined = true;
5538 break;
5539 default:
5540 break;
5542 else if (TYPE_READONLY (type))
5544 error ("%qE has const type for %<reduction%>",
5545 omp_clause_printable_decl (t));
5546 return true;
5548 else if (!processing_template_decl)
5550 t = require_complete_type (t);
5551 if (t == error_mark_node)
5552 return true;
5553 OMP_CLAUSE_DECL (c) = t;
5556 if (predefined)
5558 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5559 return false;
5561 else if (processing_template_decl)
5562 return false;
5564 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5566 type = TYPE_MAIN_VARIANT (type);
5567 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5568 if (id == NULL_TREE)
5569 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5570 NULL_TREE, NULL_TREE);
5571 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5572 if (id)
5574 if (id == error_mark_node)
5575 return true;
5576 id = OVL_CURRENT (id);
5577 mark_used (id);
5578 tree body = DECL_SAVED_TREE (id);
5579 if (!body)
5580 return true;
5581 if (TREE_CODE (body) == STATEMENT_LIST)
5583 tree_stmt_iterator tsi;
5584 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5585 int i;
5586 tree stmts[7];
5587 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5588 atype = TREE_TYPE (atype);
5589 bool need_static_cast = !same_type_p (type, atype);
5590 memset (stmts, 0, sizeof stmts);
5591 for (i = 0, tsi = tsi_start (body);
5592 i < 7 && !tsi_end_p (tsi);
5593 i++, tsi_next (&tsi))
5594 stmts[i] = tsi_stmt (tsi);
5595 gcc_assert (tsi_end_p (tsi));
5597 if (i >= 3)
5599 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5600 && TREE_CODE (stmts[1]) == DECL_EXPR);
5601 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5602 DECL_ARTIFICIAL (placeholder) = 1;
5603 DECL_IGNORED_P (placeholder) = 1;
5604 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5605 if (TREE_CODE (t) == MEM_REF)
5607 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5608 type);
5609 DECL_ARTIFICIAL (decl_placeholder) = 1;
5610 DECL_IGNORED_P (decl_placeholder) = 1;
5611 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5613 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5614 cxx_mark_addressable (placeholder);
5615 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5616 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5617 != REFERENCE_TYPE)
5618 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5619 : OMP_CLAUSE_DECL (c));
5620 tree omp_out = placeholder;
5621 tree omp_in = decl_placeholder ? decl_placeholder
5622 : convert_from_reference (OMP_CLAUSE_DECL (c));
5623 if (need_static_cast)
5625 tree rtype = build_reference_type (atype);
5626 omp_out = build_static_cast (rtype, omp_out,
5627 tf_warning_or_error);
5628 omp_in = build_static_cast (rtype, omp_in,
5629 tf_warning_or_error);
5630 if (omp_out == error_mark_node || omp_in == error_mark_node)
5631 return true;
5632 omp_out = convert_from_reference (omp_out);
5633 omp_in = convert_from_reference (omp_in);
5635 OMP_CLAUSE_REDUCTION_MERGE (c)
5636 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5637 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5639 if (i >= 6)
5641 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5642 && TREE_CODE (stmts[4]) == DECL_EXPR);
5643 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5644 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5645 : OMP_CLAUSE_DECL (c));
5646 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5647 cxx_mark_addressable (placeholder);
5648 tree omp_priv = decl_placeholder ? decl_placeholder
5649 : convert_from_reference (OMP_CLAUSE_DECL (c));
5650 tree omp_orig = placeholder;
5651 if (need_static_cast)
5653 if (i == 7)
5655 error_at (OMP_CLAUSE_LOCATION (c),
5656 "user defined reduction with constructor "
5657 "initializer for base class %qT", atype);
5658 return true;
5660 tree rtype = build_reference_type (atype);
5661 omp_priv = build_static_cast (rtype, omp_priv,
5662 tf_warning_or_error);
5663 omp_orig = build_static_cast (rtype, omp_orig,
5664 tf_warning_or_error);
5665 if (omp_priv == error_mark_node
5666 || omp_orig == error_mark_node)
5667 return true;
5668 omp_priv = convert_from_reference (omp_priv);
5669 omp_orig = convert_from_reference (omp_orig);
5671 if (i == 6)
5672 *need_default_ctor = true;
5673 OMP_CLAUSE_REDUCTION_INIT (c)
5674 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5675 DECL_EXPR_DECL (stmts[3]),
5676 omp_priv, omp_orig);
5677 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5678 find_omp_placeholder_r, placeholder, NULL))
5679 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5681 else if (i >= 3)
5683 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5684 *need_default_ctor = true;
5685 else
5687 tree init;
5688 tree v = decl_placeholder ? decl_placeholder
5689 : convert_from_reference (t);
5690 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5691 init = build_constructor (TREE_TYPE (v), NULL);
5692 else
5693 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5694 OMP_CLAUSE_REDUCTION_INIT (c)
5695 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5700 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5701 *need_dtor = true;
5702 else
5704 error ("user defined reduction not found for %qE",
5705 omp_clause_printable_decl (t));
5706 return true;
5708 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5709 gcc_assert (TYPE_SIZE_UNIT (type)
5710 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5711 return false;
5714 /* Called from finish_struct_1. linear(this) or linear(this:step)
5715 clauses might not be finalized yet because the class has been incomplete
5716 when parsing #pragma omp declare simd methods. Fix those up now. */
5718 void
5719 finish_omp_declare_simd_methods (tree t)
5721 if (processing_template_decl)
5722 return;
5724 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5726 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5727 continue;
5728 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5729 if (!ods || !TREE_VALUE (ods))
5730 continue;
5731 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5733 && integer_zerop (OMP_CLAUSE_DECL (c))
5734 && OMP_CLAUSE_LINEAR_STEP (c)
5735 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5736 == POINTER_TYPE)
5738 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5739 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5740 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5741 sizetype, s, TYPE_SIZE_UNIT (t));
5742 OMP_CLAUSE_LINEAR_STEP (c) = s;
5747 /* Adjust sink depend clause to take into account pointer offsets.
5749 Return TRUE if there was a problem processing the offset, and the
5750 whole clause should be removed. */
5752 static bool
5753 cp_finish_omp_clause_depend_sink (tree sink_clause)
5755 tree t = OMP_CLAUSE_DECL (sink_clause);
5756 gcc_assert (TREE_CODE (t) == TREE_LIST);
5758 /* Make sure we don't adjust things twice for templates. */
5759 if (processing_template_decl)
5760 return false;
5762 for (; t; t = TREE_CHAIN (t))
5764 tree decl = TREE_VALUE (t);
5765 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5767 tree offset = TREE_PURPOSE (t);
5768 bool neg = wi::neg_p ((wide_int) offset);
5769 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5770 decl = mark_rvalue_use (decl);
5771 decl = convert_from_reference (decl);
5772 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5773 neg ? MINUS_EXPR : PLUS_EXPR,
5774 decl, offset);
5775 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5776 MINUS_EXPR, sizetype,
5777 fold_convert (sizetype, t2),
5778 fold_convert (sizetype, decl));
5779 if (t2 == error_mark_node)
5780 return true;
5781 TREE_PURPOSE (t) = t2;
5784 return false;
5787 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5788 Remove any elements from the list that are invalid. */
5790 tree
5791 finish_omp_clauses (tree clauses, bool allow_fields, bool declare_simd)
5793 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5794 bitmap_head aligned_head, map_head, map_field_head;
5795 tree c, t, *pc;
5796 tree safelen = NULL_TREE;
5797 bool branch_seen = false;
5798 bool copyprivate_seen = false;
5799 bool ordered_seen = false;
5801 bitmap_obstack_initialize (NULL);
5802 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5803 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5804 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5805 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5806 bitmap_initialize (&map_head, &bitmap_default_obstack);
5807 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5809 for (pc = &clauses, c = clauses; c ; c = *pc)
5811 bool remove = false;
5812 bool field_ok = false;
5814 switch (OMP_CLAUSE_CODE (c))
5816 case OMP_CLAUSE_SHARED:
5817 field_ok = allow_fields;
5818 goto check_dup_generic;
5819 case OMP_CLAUSE_PRIVATE:
5820 field_ok = allow_fields;
5821 goto check_dup_generic;
5822 case OMP_CLAUSE_REDUCTION:
5823 field_ok = allow_fields;
5824 t = OMP_CLAUSE_DECL (c);
5825 if (TREE_CODE (t) == TREE_LIST)
5827 if (handle_omp_array_sections (c, allow_fields))
5829 remove = true;
5830 break;
5832 if (TREE_CODE (t) == TREE_LIST)
5834 while (TREE_CODE (t) == TREE_LIST)
5835 t = TREE_CHAIN (t);
5837 else
5839 gcc_assert (TREE_CODE (t) == MEM_REF);
5840 t = TREE_OPERAND (t, 0);
5841 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5842 t = TREE_OPERAND (t, 0);
5843 if (TREE_CODE (t) == ADDR_EXPR
5844 || TREE_CODE (t) == INDIRECT_REF)
5845 t = TREE_OPERAND (t, 0);
5847 tree n = omp_clause_decl_field (t);
5848 if (n)
5849 t = n;
5850 goto check_dup_generic_t;
5852 goto check_dup_generic;
5853 case OMP_CLAUSE_COPYPRIVATE:
5854 copyprivate_seen = true;
5855 field_ok = allow_fields;
5856 goto check_dup_generic;
5857 case OMP_CLAUSE_COPYIN:
5858 goto check_dup_generic;
5859 case OMP_CLAUSE_LINEAR:
5860 field_ok = allow_fields;
5861 t = OMP_CLAUSE_DECL (c);
5862 if (!declare_simd
5863 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5865 error_at (OMP_CLAUSE_LOCATION (c),
5866 "modifier should not be specified in %<linear%> "
5867 "clause on %<simd%> or %<for%> constructs");
5868 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5870 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5871 && !type_dependent_expression_p (t))
5873 tree type = TREE_TYPE (t);
5874 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5875 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5876 && TREE_CODE (type) != REFERENCE_TYPE)
5878 error ("linear clause with %qs modifier applied to "
5879 "non-reference variable with %qT type",
5880 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5881 ? "ref" : "uval", TREE_TYPE (t));
5882 remove = true;
5883 break;
5885 if (TREE_CODE (type) == REFERENCE_TYPE)
5886 type = TREE_TYPE (type);
5887 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF
5888 && !INTEGRAL_TYPE_P (type)
5889 && TREE_CODE (type) != POINTER_TYPE)
5891 error ("linear clause applied to non-integral non-pointer "
5892 "variable with %qT type", TREE_TYPE (t));
5893 remove = true;
5894 break;
5897 t = OMP_CLAUSE_LINEAR_STEP (c);
5898 if (t == NULL_TREE)
5899 t = integer_one_node;
5900 if (t == error_mark_node)
5902 remove = true;
5903 break;
5905 else if (!type_dependent_expression_p (t)
5906 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5907 && (!declare_simd
5908 || TREE_CODE (t) != PARM_DECL
5909 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5910 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5912 error ("linear step expression must be integral");
5913 remove = true;
5914 break;
5916 else
5918 t = mark_rvalue_use (t);
5919 if (declare_simd && TREE_CODE (t) == PARM_DECL)
5921 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5922 goto check_dup_generic;
5924 if (!processing_template_decl
5925 && (VAR_P (OMP_CLAUSE_DECL (c))
5926 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5928 if (declare_simd)
5930 t = maybe_constant_value (t);
5931 if (TREE_CODE (t) != INTEGER_CST)
5933 error_at (OMP_CLAUSE_LOCATION (c),
5934 "%<linear%> clause step %qE is neither "
5935 "constant nor a parameter", t);
5936 remove = true;
5937 break;
5940 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5941 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5942 if (TREE_CODE (type) == REFERENCE_TYPE)
5943 type = TREE_TYPE (type);
5944 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5946 type = build_pointer_type (type);
5947 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5948 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5949 d, t);
5950 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5951 MINUS_EXPR, sizetype,
5952 fold_convert (sizetype, t),
5953 fold_convert (sizetype, d));
5954 if (t == error_mark_node)
5956 remove = true;
5957 break;
5960 else if (TREE_CODE (type) == POINTER_TYPE
5961 /* Can't multiply the step yet if *this
5962 is still incomplete type. */
5963 && (!declare_simd
5964 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5965 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5966 || DECL_NAME (OMP_CLAUSE_DECL (c))
5967 != this_identifier
5968 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5970 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5971 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5972 d, t);
5973 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5974 MINUS_EXPR, sizetype,
5975 fold_convert (sizetype, t),
5976 fold_convert (sizetype, d));
5977 if (t == error_mark_node)
5979 remove = true;
5980 break;
5983 else
5984 t = fold_convert (type, t);
5986 OMP_CLAUSE_LINEAR_STEP (c) = t;
5988 goto check_dup_generic;
5989 check_dup_generic:
5990 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5991 if (t)
5993 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
5994 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5996 else
5997 t = OMP_CLAUSE_DECL (c);
5998 check_dup_generic_t:
5999 if (t == current_class_ptr
6000 && (!declare_simd
6001 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6002 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6004 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6005 " clauses");
6006 remove = true;
6007 break;
6009 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6010 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6012 if (processing_template_decl)
6013 break;
6014 if (DECL_P (t))
6015 error ("%qD is not a variable in clause %qs", t,
6016 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6017 else
6018 error ("%qE is not a variable in clause %qs", t,
6019 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6020 remove = true;
6022 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6023 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6024 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6026 error ("%qD appears more than once in data clauses", t);
6027 remove = true;
6029 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6030 && bitmap_bit_p (&map_head, DECL_UID (t)))
6032 error ("%qD appears both in data and map clauses", t);
6033 remove = true;
6035 else
6036 bitmap_set_bit (&generic_head, DECL_UID (t));
6037 if (!field_ok)
6038 break;
6039 handle_field_decl:
6040 if (!remove
6041 && TREE_CODE (t) == FIELD_DECL
6042 && t == OMP_CLAUSE_DECL (c))
6044 OMP_CLAUSE_DECL (c)
6045 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6046 == OMP_CLAUSE_SHARED));
6047 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6048 remove = true;
6050 break;
6052 case OMP_CLAUSE_FIRSTPRIVATE:
6053 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6054 if (t)
6055 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6056 else
6057 t = OMP_CLAUSE_DECL (c);
6058 if (t == current_class_ptr)
6060 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6061 " clauses");
6062 remove = true;
6063 break;
6065 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6066 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
6068 if (processing_template_decl)
6069 break;
6070 if (DECL_P (t))
6071 error ("%qD is not a variable in clause %<firstprivate%>", t);
6072 else
6073 error ("%qE is not a variable in clause %<firstprivate%>", t);
6074 remove = true;
6076 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6077 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6079 error ("%qD appears more than once in data clauses", t);
6080 remove = true;
6082 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6084 error ("%qD appears both in data and map clauses", t);
6085 remove = true;
6087 else
6088 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6089 goto handle_field_decl;
6091 case OMP_CLAUSE_LASTPRIVATE:
6092 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6093 if (t)
6094 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6095 else
6096 t = OMP_CLAUSE_DECL (c);
6097 if (t == current_class_ptr)
6099 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6100 " clauses");
6101 remove = true;
6102 break;
6104 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6105 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
6107 if (processing_template_decl)
6108 break;
6109 if (DECL_P (t))
6110 error ("%qD is not a variable in clause %<lastprivate%>", t);
6111 else
6112 error ("%qE is not a variable in clause %<lastprivate%>", t);
6113 remove = true;
6115 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6116 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6118 error ("%qD appears more than once in data clauses", t);
6119 remove = true;
6121 else
6122 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6123 goto handle_field_decl;
6125 case OMP_CLAUSE_IF:
6126 t = OMP_CLAUSE_IF_EXPR (c);
6127 t = maybe_convert_cond (t);
6128 if (t == error_mark_node)
6129 remove = true;
6130 else if (!processing_template_decl)
6131 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6132 OMP_CLAUSE_IF_EXPR (c) = t;
6133 break;
6135 case OMP_CLAUSE_FINAL:
6136 t = OMP_CLAUSE_FINAL_EXPR (c);
6137 t = maybe_convert_cond (t);
6138 if (t == error_mark_node)
6139 remove = true;
6140 else if (!processing_template_decl)
6141 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6142 OMP_CLAUSE_FINAL_EXPR (c) = t;
6143 break;
6145 case OMP_CLAUSE_GANG:
6146 /* Operand 1 is the gang static: argument. */
6147 t = OMP_CLAUSE_OPERAND (c, 1);
6148 if (t != NULL_TREE)
6150 if (t == error_mark_node)
6151 remove = true;
6152 else if (!type_dependent_expression_p (t)
6153 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6155 error ("%<gang%> static expression must be integral");
6156 remove = true;
6158 else
6160 t = mark_rvalue_use (t);
6161 if (!processing_template_decl)
6163 t = maybe_constant_value (t);
6164 if (TREE_CODE (t) == INTEGER_CST
6165 && tree_int_cst_sgn (t) != 1
6166 && t != integer_minus_one_node)
6168 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6169 "%<gang%> static value must be"
6170 "positive");
6171 t = integer_one_node;
6174 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6176 OMP_CLAUSE_OPERAND (c, 1) = t;
6178 /* Check operand 0, the num argument. */
6180 case OMP_CLAUSE_WORKER:
6181 case OMP_CLAUSE_VECTOR:
6182 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6183 break;
6185 case OMP_CLAUSE_NUM_TASKS:
6186 case OMP_CLAUSE_NUM_TEAMS:
6187 case OMP_CLAUSE_NUM_THREADS:
6188 case OMP_CLAUSE_NUM_GANGS:
6189 case OMP_CLAUSE_NUM_WORKERS:
6190 case OMP_CLAUSE_VECTOR_LENGTH:
6191 t = OMP_CLAUSE_OPERAND (c, 0);
6192 if (t == error_mark_node)
6193 remove = true;
6194 else if (!type_dependent_expression_p (t)
6195 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6197 switch (OMP_CLAUSE_CODE (c))
6199 case OMP_CLAUSE_GANG:
6200 error_at (OMP_CLAUSE_LOCATION (c),
6201 "%<gang%> num expression must be integral"); break;
6202 case OMP_CLAUSE_VECTOR:
6203 error_at (OMP_CLAUSE_LOCATION (c),
6204 "%<vector%> length expression must be integral");
6205 break;
6206 case OMP_CLAUSE_WORKER:
6207 error_at (OMP_CLAUSE_LOCATION (c),
6208 "%<worker%> num expression must be integral");
6209 break;
6210 default:
6211 error_at (OMP_CLAUSE_LOCATION (c),
6212 "%qs expression must be integral",
6213 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6215 remove = true;
6217 else
6219 t = mark_rvalue_use (t);
6220 if (!processing_template_decl)
6222 t = maybe_constant_value (t);
6223 if (TREE_CODE (t) == INTEGER_CST
6224 && tree_int_cst_sgn (t) != 1)
6226 switch (OMP_CLAUSE_CODE (c))
6228 case OMP_CLAUSE_GANG:
6229 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6230 "%<gang%> num value must be positive");
6231 break;
6232 case OMP_CLAUSE_VECTOR:
6233 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6234 "%<vector%> length value must be"
6235 "positive");
6236 break;
6237 case OMP_CLAUSE_WORKER:
6238 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6239 "%<worker%> num value must be"
6240 "positive");
6241 break;
6242 default:
6243 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6244 "%qs value must be positive",
6245 omp_clause_code_name
6246 [OMP_CLAUSE_CODE (c)]);
6248 t = integer_one_node;
6250 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6252 OMP_CLAUSE_OPERAND (c, 0) = t;
6254 break;
6256 case OMP_CLAUSE_SCHEDULE:
6257 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6259 const char *p = NULL;
6260 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6262 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6263 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6264 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6265 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6266 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6267 default: gcc_unreachable ();
6269 if (p)
6271 error_at (OMP_CLAUSE_LOCATION (c),
6272 "%<nonmonotonic%> modifier specified for %qs "
6273 "schedule kind", p);
6274 OMP_CLAUSE_SCHEDULE_KIND (c)
6275 = (enum omp_clause_schedule_kind)
6276 (OMP_CLAUSE_SCHEDULE_KIND (c)
6277 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6281 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6282 if (t == NULL)
6284 else if (t == error_mark_node)
6285 remove = true;
6286 else if (!type_dependent_expression_p (t)
6287 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6288 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6289 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6291 error ("schedule chunk size expression must be integral");
6292 remove = true;
6294 else
6296 t = mark_rvalue_use (t);
6297 if (!processing_template_decl)
6299 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6300 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6302 t = convert_to_integer (long_integer_type_node, t);
6303 if (t == error_mark_node)
6305 remove = true;
6306 break;
6309 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6311 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6313 break;
6315 case OMP_CLAUSE_SIMDLEN:
6316 case OMP_CLAUSE_SAFELEN:
6317 t = OMP_CLAUSE_OPERAND (c, 0);
6318 if (t == error_mark_node)
6319 remove = true;
6320 else if (!type_dependent_expression_p (t)
6321 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6323 error ("%qs length expression must be integral",
6324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6325 remove = true;
6327 else
6329 t = mark_rvalue_use (t);
6330 t = maybe_constant_value (t);
6331 if (!processing_template_decl)
6333 if (TREE_CODE (t) != INTEGER_CST
6334 || tree_int_cst_sgn (t) != 1)
6336 error ("%qs length expression must be positive constant"
6337 " integer expression",
6338 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6339 remove = true;
6342 OMP_CLAUSE_OPERAND (c, 0) = t;
6343 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6344 safelen = c;
6346 break;
6348 case OMP_CLAUSE_ASYNC:
6349 t = OMP_CLAUSE_ASYNC_EXPR (c);
6350 if (t == error_mark_node)
6351 remove = true;
6352 else if (!type_dependent_expression_p (t)
6353 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6355 error ("%<async%> expression must be integral");
6356 remove = true;
6358 else
6360 t = mark_rvalue_use (t);
6361 if (!processing_template_decl)
6362 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6363 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6365 break;
6367 case OMP_CLAUSE_WAIT:
6368 t = OMP_CLAUSE_WAIT_EXPR (c);
6369 if (t == error_mark_node)
6370 remove = true;
6371 else if (!processing_template_decl)
6372 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6373 OMP_CLAUSE_WAIT_EXPR (c) = t;
6374 break;
6376 case OMP_CLAUSE_THREAD_LIMIT:
6377 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6378 if (t == error_mark_node)
6379 remove = true;
6380 else if (!type_dependent_expression_p (t)
6381 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6383 error ("%<thread_limit%> expression must be integral");
6384 remove = true;
6386 else
6388 t = mark_rvalue_use (t);
6389 if (!processing_template_decl)
6391 t = maybe_constant_value (t);
6392 if (TREE_CODE (t) == INTEGER_CST
6393 && tree_int_cst_sgn (t) != 1)
6395 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6396 "%<thread_limit%> value must be positive");
6397 t = integer_one_node;
6399 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6401 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6403 break;
6405 case OMP_CLAUSE_DEVICE:
6406 t = OMP_CLAUSE_DEVICE_ID (c);
6407 if (t == error_mark_node)
6408 remove = true;
6409 else if (!type_dependent_expression_p (t)
6410 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6412 error ("%<device%> id must be integral");
6413 remove = true;
6415 else
6417 t = mark_rvalue_use (t);
6418 if (!processing_template_decl)
6419 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6420 OMP_CLAUSE_DEVICE_ID (c) = t;
6422 break;
6424 case OMP_CLAUSE_DIST_SCHEDULE:
6425 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6426 if (t == NULL)
6428 else if (t == error_mark_node)
6429 remove = true;
6430 else if (!type_dependent_expression_p (t)
6431 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6433 error ("%<dist_schedule%> chunk size expression must be "
6434 "integral");
6435 remove = true;
6437 else
6439 t = mark_rvalue_use (t);
6440 if (!processing_template_decl)
6441 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6442 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6444 break;
6446 case OMP_CLAUSE_ALIGNED:
6447 t = OMP_CLAUSE_DECL (c);
6448 if (t == current_class_ptr && !declare_simd)
6450 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6451 " clauses");
6452 remove = true;
6453 break;
6455 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6457 if (processing_template_decl)
6458 break;
6459 if (DECL_P (t))
6460 error ("%qD is not a variable in %<aligned%> clause", t);
6461 else
6462 error ("%qE is not a variable in %<aligned%> clause", t);
6463 remove = true;
6465 else if (!type_dependent_expression_p (t)
6466 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6467 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6468 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6469 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6470 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6471 != ARRAY_TYPE))))
6473 error_at (OMP_CLAUSE_LOCATION (c),
6474 "%qE in %<aligned%> clause is neither a pointer nor "
6475 "an array nor a reference to pointer or array", t);
6476 remove = true;
6478 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6480 error ("%qD appears more than once in %<aligned%> clauses", t);
6481 remove = true;
6483 else
6484 bitmap_set_bit (&aligned_head, DECL_UID (t));
6485 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6486 if (t == error_mark_node)
6487 remove = true;
6488 else if (t == NULL_TREE)
6489 break;
6490 else if (!type_dependent_expression_p (t)
6491 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6493 error ("%<aligned%> clause alignment expression must "
6494 "be integral");
6495 remove = true;
6497 else
6499 t = mark_rvalue_use (t);
6500 t = maybe_constant_value (t);
6501 if (!processing_template_decl)
6503 if (TREE_CODE (t) != INTEGER_CST
6504 || tree_int_cst_sgn (t) != 1)
6506 error ("%<aligned%> clause alignment expression must be "
6507 "positive constant integer expression");
6508 remove = true;
6511 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6513 break;
6515 case OMP_CLAUSE_DEPEND:
6516 t = OMP_CLAUSE_DECL (c);
6517 if (t == NULL_TREE)
6519 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6520 == OMP_CLAUSE_DEPEND_SOURCE);
6521 break;
6523 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6525 if (cp_finish_omp_clause_depend_sink (c))
6526 remove = true;
6527 break;
6529 if (TREE_CODE (t) == TREE_LIST)
6531 if (handle_omp_array_sections (c, allow_fields))
6532 remove = true;
6533 break;
6535 if (t == error_mark_node)
6536 remove = true;
6537 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6539 if (processing_template_decl)
6540 break;
6541 if (DECL_P (t))
6542 error ("%qD is not a variable in %<depend%> clause", t);
6543 else
6544 error ("%qE is not a variable in %<depend%> clause", t);
6545 remove = true;
6547 else if (t == current_class_ptr)
6549 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6550 " clauses");
6551 remove = true;
6553 else if (!processing_template_decl
6554 && !cxx_mark_addressable (t))
6555 remove = true;
6556 break;
6558 case OMP_CLAUSE_MAP:
6559 case OMP_CLAUSE_TO:
6560 case OMP_CLAUSE_FROM:
6561 case OMP_CLAUSE__CACHE_:
6562 t = OMP_CLAUSE_DECL (c);
6563 if (TREE_CODE (t) == TREE_LIST)
6565 if (handle_omp_array_sections (c, allow_fields))
6566 remove = true;
6567 else
6569 t = OMP_CLAUSE_DECL (c);
6570 if (TREE_CODE (t) != TREE_LIST
6571 && !type_dependent_expression_p (t)
6572 && !cp_omp_mappable_type (TREE_TYPE (t)))
6574 error_at (OMP_CLAUSE_LOCATION (c),
6575 "array section does not have mappable type "
6576 "in %qs clause",
6577 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6578 remove = true;
6580 while (TREE_CODE (t) == ARRAY_REF)
6581 t = TREE_OPERAND (t, 0);
6582 if (TREE_CODE (t) == COMPONENT_REF
6583 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6585 while (TREE_CODE (t) == COMPONENT_REF)
6586 t = TREE_OPERAND (t, 0);
6587 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6588 break;
6589 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6591 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6592 error ("%qD appears more than once in motion"
6593 " clauses", t);
6594 else
6595 error ("%qD appears more than once in map"
6596 " clauses", t);
6597 remove = true;
6599 else
6601 bitmap_set_bit (&map_head, DECL_UID (t));
6602 bitmap_set_bit (&map_field_head, DECL_UID (t));
6606 break;
6608 if (t == error_mark_node)
6610 remove = true;
6611 break;
6613 if (REFERENCE_REF_P (t)
6614 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6616 t = TREE_OPERAND (t, 0);
6617 OMP_CLAUSE_DECL (c) = t;
6619 if (TREE_CODE (t) == COMPONENT_REF
6620 && allow_fields
6621 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6623 if (type_dependent_expression_p (t))
6624 break;
6625 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6627 error_at (OMP_CLAUSE_LOCATION (c),
6628 "bit-field %qE in %qs clause",
6629 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6630 remove = true;
6632 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6634 error_at (OMP_CLAUSE_LOCATION (c),
6635 "%qE does not have a mappable type in %qs clause",
6636 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6637 remove = true;
6639 while (TREE_CODE (t) == COMPONENT_REF)
6641 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6642 == UNION_TYPE)
6644 error_at (OMP_CLAUSE_LOCATION (c),
6645 "%qE is a member of a union", t);
6646 remove = true;
6647 break;
6649 t = TREE_OPERAND (t, 0);
6651 if (remove)
6652 break;
6653 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6655 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6656 goto handle_map_references;
6659 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6661 if (processing_template_decl)
6662 break;
6663 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6664 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6665 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6666 break;
6667 if (DECL_P (t))
6668 error ("%qD is not a variable in %qs clause", t,
6669 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6670 else
6671 error ("%qE is not a variable in %qs clause", t,
6672 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6673 remove = true;
6675 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6677 error ("%qD is threadprivate variable in %qs clause", t,
6678 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6679 remove = true;
6681 else if (t == current_class_ptr)
6683 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6684 " clauses");
6685 remove = true;
6686 break;
6688 else if (!processing_template_decl
6689 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6690 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6691 || (OMP_CLAUSE_MAP_KIND (c)
6692 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6693 && !cxx_mark_addressable (t))
6694 remove = true;
6695 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6696 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6697 || (OMP_CLAUSE_MAP_KIND (c)
6698 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6699 && t == OMP_CLAUSE_DECL (c)
6700 && !type_dependent_expression_p (t)
6701 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6702 == REFERENCE_TYPE)
6703 ? TREE_TYPE (TREE_TYPE (t))
6704 : TREE_TYPE (t)))
6706 error_at (OMP_CLAUSE_LOCATION (c),
6707 "%qD does not have a mappable type in %qs clause", t,
6708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6709 remove = true;
6711 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6712 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6713 && !type_dependent_expression_p (t)
6714 && !POINTER_TYPE_P (TREE_TYPE (t)))
6716 error ("%qD is not a pointer variable", t);
6717 remove = true;
6719 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6720 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6722 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6723 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6725 error ("%qD appears more than once in data clauses", t);
6726 remove = true;
6728 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6730 error ("%qD appears both in data and map clauses", t);
6731 remove = true;
6733 else
6734 bitmap_set_bit (&generic_head, DECL_UID (t));
6736 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6738 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6739 error ("%qD appears more than once in motion clauses", t);
6740 else
6741 error ("%qD appears more than once in map clauses", t);
6742 remove = true;
6744 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6745 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6747 error ("%qD appears both in data and map clauses", t);
6748 remove = true;
6750 else
6752 bitmap_set_bit (&map_head, DECL_UID (t));
6753 if (t != OMP_CLAUSE_DECL (c)
6754 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6755 bitmap_set_bit (&map_field_head, DECL_UID (t));
6757 handle_map_references:
6758 if (!remove
6759 && !processing_template_decl
6760 && allow_fields
6761 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6763 t = OMP_CLAUSE_DECL (c);
6764 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6766 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6767 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6768 OMP_CLAUSE_SIZE (c)
6769 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6771 else if (OMP_CLAUSE_MAP_KIND (c)
6772 != GOMP_MAP_FIRSTPRIVATE_POINTER
6773 && (OMP_CLAUSE_MAP_KIND (c)
6774 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6775 && (OMP_CLAUSE_MAP_KIND (c)
6776 != GOMP_MAP_ALWAYS_POINTER))
6778 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6779 OMP_CLAUSE_MAP);
6780 if (TREE_CODE (t) == COMPONENT_REF)
6781 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6782 else
6783 OMP_CLAUSE_SET_MAP_KIND (c2,
6784 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6785 OMP_CLAUSE_DECL (c2) = t;
6786 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6787 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6788 OMP_CLAUSE_CHAIN (c) = c2;
6789 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6790 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6791 OMP_CLAUSE_SIZE (c)
6792 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6793 c = c2;
6796 break;
6798 case OMP_CLAUSE_TO_DECLARE:
6799 case OMP_CLAUSE_LINK:
6800 t = OMP_CLAUSE_DECL (c);
6801 if (TREE_CODE (t) == FUNCTION_DECL
6802 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6804 else if (!VAR_P (t))
6806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6808 if (TREE_CODE (t) == OVERLOAD && OVL_CHAIN (t))
6809 error_at (OMP_CLAUSE_LOCATION (c),
6810 "overloaded function name %qE in clause %qs", t,
6811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6812 else if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6813 error_at (OMP_CLAUSE_LOCATION (c),
6814 "template %qE in clause %qs", t,
6815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6816 else
6817 error_at (OMP_CLAUSE_LOCATION (c),
6818 "%qE is neither a variable nor a function name "
6819 "in clause %qs", t,
6820 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6822 else
6823 error_at (OMP_CLAUSE_LOCATION (c),
6824 "%qE is not a variable in clause %qs", t,
6825 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6826 remove = true;
6828 else if (DECL_THREAD_LOCAL_P (t))
6830 error_at (OMP_CLAUSE_LOCATION (c),
6831 "%qD is threadprivate variable in %qs clause", t,
6832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6833 remove = true;
6835 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6837 error_at (OMP_CLAUSE_LOCATION (c),
6838 "%qD does not have a mappable type in %qs clause", t,
6839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6840 remove = true;
6842 if (remove)
6843 break;
6844 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6846 error_at (OMP_CLAUSE_LOCATION (c),
6847 "%qE appears more than once on the same "
6848 "%<declare target%> directive", t);
6849 remove = true;
6851 else
6852 bitmap_set_bit (&generic_head, DECL_UID (t));
6853 break;
6855 case OMP_CLAUSE_UNIFORM:
6856 t = OMP_CLAUSE_DECL (c);
6857 if (TREE_CODE (t) != PARM_DECL)
6859 if (processing_template_decl)
6860 break;
6861 if (DECL_P (t))
6862 error ("%qD is not an argument in %<uniform%> clause", t);
6863 else
6864 error ("%qE is not an argument in %<uniform%> clause", t);
6865 remove = true;
6866 break;
6868 /* map_head bitmap is used as uniform_head if declare_simd. */
6869 bitmap_set_bit (&map_head, DECL_UID (t));
6870 goto check_dup_generic;
6872 case OMP_CLAUSE_GRAINSIZE:
6873 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6874 if (t == error_mark_node)
6875 remove = true;
6876 else if (!type_dependent_expression_p (t)
6877 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6879 error ("%<grainsize%> expression must be integral");
6880 remove = true;
6882 else
6884 t = mark_rvalue_use (t);
6885 if (!processing_template_decl)
6887 t = maybe_constant_value (t);
6888 if (TREE_CODE (t) == INTEGER_CST
6889 && tree_int_cst_sgn (t) != 1)
6891 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6892 "%<grainsize%> value must be positive");
6893 t = integer_one_node;
6895 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6897 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6899 break;
6901 case OMP_CLAUSE_PRIORITY:
6902 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6903 if (t == error_mark_node)
6904 remove = true;
6905 else if (!type_dependent_expression_p (t)
6906 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6908 error ("%<priority%> expression must be integral");
6909 remove = true;
6911 else
6913 t = mark_rvalue_use (t);
6914 if (!processing_template_decl)
6916 t = maybe_constant_value (t);
6917 if (TREE_CODE (t) == INTEGER_CST
6918 && tree_int_cst_sgn (t) == -1)
6920 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6921 "%<priority%> value must be non-negative");
6922 t = integer_one_node;
6924 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6926 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6928 break;
6930 case OMP_CLAUSE_HINT:
6931 t = OMP_CLAUSE_HINT_EXPR (c);
6932 if (t == error_mark_node)
6933 remove = true;
6934 else if (!type_dependent_expression_p (t)
6935 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6937 error ("%<num_tasks%> expression must be integral");
6938 remove = true;
6940 else
6942 t = mark_rvalue_use (t);
6943 if (!processing_template_decl)
6945 t = maybe_constant_value (t);
6946 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6948 OMP_CLAUSE_HINT_EXPR (c) = t;
6950 break;
6952 case OMP_CLAUSE_IS_DEVICE_PTR:
6953 case OMP_CLAUSE_USE_DEVICE_PTR:
6954 field_ok = allow_fields;
6955 t = OMP_CLAUSE_DECL (c);
6956 if (!type_dependent_expression_p (t))
6958 tree type = TREE_TYPE (t);
6959 if (TREE_CODE (type) != POINTER_TYPE
6960 && TREE_CODE (type) != ARRAY_TYPE
6961 && (TREE_CODE (type) != REFERENCE_TYPE
6962 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6963 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
6965 error_at (OMP_CLAUSE_LOCATION (c),
6966 "%qs variable is neither a pointer, nor an array"
6967 "nor reference to pointer or array",
6968 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6969 remove = true;
6972 goto check_dup_generic;
6974 case OMP_CLAUSE_NOWAIT:
6975 case OMP_CLAUSE_DEFAULT:
6976 case OMP_CLAUSE_UNTIED:
6977 case OMP_CLAUSE_COLLAPSE:
6978 case OMP_CLAUSE_MERGEABLE:
6979 case OMP_CLAUSE_PARALLEL:
6980 case OMP_CLAUSE_FOR:
6981 case OMP_CLAUSE_SECTIONS:
6982 case OMP_CLAUSE_TASKGROUP:
6983 case OMP_CLAUSE_PROC_BIND:
6984 case OMP_CLAUSE_NOGROUP:
6985 case OMP_CLAUSE_THREADS:
6986 case OMP_CLAUSE_SIMD:
6987 case OMP_CLAUSE_DEFAULTMAP:
6988 case OMP_CLAUSE__CILK_FOR_COUNT_:
6989 case OMP_CLAUSE_AUTO:
6990 case OMP_CLAUSE_INDEPENDENT:
6991 case OMP_CLAUSE_SEQ:
6992 break;
6994 case OMP_CLAUSE_TILE:
6995 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
6996 list = TREE_CHAIN (list))
6998 t = TREE_VALUE (list);
7000 if (t == error_mark_node)
7001 remove = true;
7002 else if (!type_dependent_expression_p (t)
7003 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7005 error ("%<tile%> value must be integral");
7006 remove = true;
7008 else
7010 t = mark_rvalue_use (t);
7011 if (!processing_template_decl)
7013 t = maybe_constant_value (t);
7014 if (TREE_CODE (t) == INTEGER_CST
7015 && tree_int_cst_sgn (t) != 1
7016 && t != integer_minus_one_node)
7018 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7019 "%<tile%> value must be positive");
7020 t = integer_one_node;
7023 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7026 /* Update list item. */
7027 TREE_VALUE (list) = t;
7029 break;
7031 case OMP_CLAUSE_ORDERED:
7032 ordered_seen = true;
7033 break;
7035 case OMP_CLAUSE_INBRANCH:
7036 case OMP_CLAUSE_NOTINBRANCH:
7037 if (branch_seen)
7039 error ("%<inbranch%> clause is incompatible with "
7040 "%<notinbranch%>");
7041 remove = true;
7043 branch_seen = true;
7044 break;
7046 default:
7047 gcc_unreachable ();
7050 if (remove)
7051 *pc = OMP_CLAUSE_CHAIN (c);
7052 else
7053 pc = &OMP_CLAUSE_CHAIN (c);
7056 for (pc = &clauses, c = clauses; c ; c = *pc)
7058 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7059 bool remove = false;
7060 bool need_complete_type = false;
7061 bool need_default_ctor = false;
7062 bool need_copy_ctor = false;
7063 bool need_copy_assignment = false;
7064 bool need_implicitly_determined = false;
7065 bool need_dtor = false;
7066 tree type, inner_type;
7068 switch (c_kind)
7070 case OMP_CLAUSE_SHARED:
7071 need_implicitly_determined = true;
7072 break;
7073 case OMP_CLAUSE_PRIVATE:
7074 need_complete_type = true;
7075 need_default_ctor = true;
7076 need_dtor = true;
7077 need_implicitly_determined = true;
7078 break;
7079 case OMP_CLAUSE_FIRSTPRIVATE:
7080 need_complete_type = true;
7081 need_copy_ctor = true;
7082 need_dtor = true;
7083 need_implicitly_determined = true;
7084 break;
7085 case OMP_CLAUSE_LASTPRIVATE:
7086 need_complete_type = true;
7087 need_copy_assignment = true;
7088 need_implicitly_determined = true;
7089 break;
7090 case OMP_CLAUSE_REDUCTION:
7091 need_implicitly_determined = true;
7092 break;
7093 case OMP_CLAUSE_LINEAR:
7094 if (!declare_simd)
7095 need_implicitly_determined = true;
7096 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7097 && !bitmap_bit_p (&map_head,
7098 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7100 error_at (OMP_CLAUSE_LOCATION (c),
7101 "%<linear%> clause step is a parameter %qD not "
7102 "specified in %<uniform%> clause",
7103 OMP_CLAUSE_LINEAR_STEP (c));
7104 *pc = OMP_CLAUSE_CHAIN (c);
7105 continue;
7107 break;
7108 case OMP_CLAUSE_COPYPRIVATE:
7109 need_copy_assignment = true;
7110 break;
7111 case OMP_CLAUSE_COPYIN:
7112 need_copy_assignment = true;
7113 break;
7114 case OMP_CLAUSE_SIMDLEN:
7115 if (safelen
7116 && !processing_template_decl
7117 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7118 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7120 error_at (OMP_CLAUSE_LOCATION (c),
7121 "%<simdlen%> clause value is bigger than "
7122 "%<safelen%> clause value");
7123 OMP_CLAUSE_SIMDLEN_EXPR (c)
7124 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7126 pc = &OMP_CLAUSE_CHAIN (c);
7127 continue;
7128 case OMP_CLAUSE_SCHEDULE:
7129 if (ordered_seen
7130 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7131 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7133 error_at (OMP_CLAUSE_LOCATION (c),
7134 "%<nonmonotonic%> schedule modifier specified "
7135 "together with %<ordered%> clause");
7136 OMP_CLAUSE_SCHEDULE_KIND (c)
7137 = (enum omp_clause_schedule_kind)
7138 (OMP_CLAUSE_SCHEDULE_KIND (c)
7139 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7141 pc = &OMP_CLAUSE_CHAIN (c);
7142 continue;
7143 case OMP_CLAUSE_NOWAIT:
7144 if (copyprivate_seen)
7146 error_at (OMP_CLAUSE_LOCATION (c),
7147 "%<nowait%> clause must not be used together "
7148 "with %<copyprivate%>");
7149 *pc = OMP_CLAUSE_CHAIN (c);
7150 continue;
7152 /* FALLTHRU */
7153 default:
7154 pc = &OMP_CLAUSE_CHAIN (c);
7155 continue;
7158 t = OMP_CLAUSE_DECL (c);
7159 if (processing_template_decl
7160 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7162 pc = &OMP_CLAUSE_CHAIN (c);
7163 continue;
7166 switch (c_kind)
7168 case OMP_CLAUSE_LASTPRIVATE:
7169 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7171 need_default_ctor = true;
7172 need_dtor = true;
7174 break;
7176 case OMP_CLAUSE_REDUCTION:
7177 if (finish_omp_reduction_clause (c, &need_default_ctor,
7178 &need_dtor))
7179 remove = true;
7180 else
7181 t = OMP_CLAUSE_DECL (c);
7182 break;
7184 case OMP_CLAUSE_COPYIN:
7185 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7187 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7188 remove = true;
7190 break;
7192 default:
7193 break;
7196 if (need_complete_type || need_copy_assignment)
7198 t = require_complete_type (t);
7199 if (t == error_mark_node)
7200 remove = true;
7201 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7202 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7203 remove = true;
7205 if (need_implicitly_determined)
7207 const char *share_name = NULL;
7209 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7210 share_name = "threadprivate";
7211 else switch (cxx_omp_predetermined_sharing (t))
7213 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7214 break;
7215 case OMP_CLAUSE_DEFAULT_SHARED:
7216 /* const vars may be specified in firstprivate clause. */
7217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7218 && cxx_omp_const_qual_no_mutable (t))
7219 break;
7220 share_name = "shared";
7221 break;
7222 case OMP_CLAUSE_DEFAULT_PRIVATE:
7223 share_name = "private";
7224 break;
7225 default:
7226 gcc_unreachable ();
7228 if (share_name)
7230 error ("%qE is predetermined %qs for %qs",
7231 omp_clause_printable_decl (t), share_name,
7232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7233 remove = true;
7237 /* We're interested in the base element, not arrays. */
7238 inner_type = type = TREE_TYPE (t);
7239 if ((need_complete_type
7240 || need_copy_assignment
7241 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7242 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7243 inner_type = TREE_TYPE (inner_type);
7244 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7245 inner_type = TREE_TYPE (inner_type);
7247 /* Check for special function availability by building a call to one.
7248 Save the results, because later we won't be in the right context
7249 for making these queries. */
7250 if (CLASS_TYPE_P (inner_type)
7251 && COMPLETE_TYPE_P (inner_type)
7252 && (need_default_ctor || need_copy_ctor
7253 || need_copy_assignment || need_dtor)
7254 && !type_dependent_expression_p (t)
7255 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7256 need_copy_ctor, need_copy_assignment,
7257 need_dtor))
7258 remove = true;
7260 if (!remove
7261 && c_kind == OMP_CLAUSE_SHARED
7262 && processing_template_decl)
7264 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7265 if (t)
7266 OMP_CLAUSE_DECL (c) = t;
7269 if (remove)
7270 *pc = OMP_CLAUSE_CHAIN (c);
7271 else
7272 pc = &OMP_CLAUSE_CHAIN (c);
7275 bitmap_obstack_release (NULL);
7276 return clauses;
7279 /* Start processing OpenMP clauses that can include any
7280 privatization clauses for non-static data members. */
7282 tree
7283 push_omp_privatization_clauses (bool ignore_next)
7285 if (omp_private_member_ignore_next)
7287 omp_private_member_ignore_next = ignore_next;
7288 return NULL_TREE;
7290 omp_private_member_ignore_next = ignore_next;
7291 if (omp_private_member_map)
7292 omp_private_member_vec.safe_push (error_mark_node);
7293 return push_stmt_list ();
7296 /* Revert remapping of any non-static data members since
7297 the last push_omp_privatization_clauses () call. */
7299 void
7300 pop_omp_privatization_clauses (tree stmt)
7302 if (stmt == NULL_TREE)
7303 return;
7304 stmt = pop_stmt_list (stmt);
7305 if (omp_private_member_map)
7307 while (!omp_private_member_vec.is_empty ())
7309 tree t = omp_private_member_vec.pop ();
7310 if (t == error_mark_node)
7312 add_stmt (stmt);
7313 return;
7315 bool no_decl_expr = t == integer_zero_node;
7316 if (no_decl_expr)
7317 t = omp_private_member_vec.pop ();
7318 tree *v = omp_private_member_map->get (t);
7319 gcc_assert (v);
7320 if (!no_decl_expr)
7321 add_decl_expr (*v);
7322 omp_private_member_map->remove (t);
7324 delete omp_private_member_map;
7325 omp_private_member_map = NULL;
7327 add_stmt (stmt);
7330 /* Remember OpenMP privatization clauses mapping and clear it.
7331 Used for lambdas. */
7333 void
7334 save_omp_privatization_clauses (vec<tree> &save)
7336 save = vNULL;
7337 if (omp_private_member_ignore_next)
7338 save.safe_push (integer_one_node);
7339 omp_private_member_ignore_next = false;
7340 if (!omp_private_member_map)
7341 return;
7343 while (!omp_private_member_vec.is_empty ())
7345 tree t = omp_private_member_vec.pop ();
7346 if (t == error_mark_node)
7348 save.safe_push (t);
7349 continue;
7351 tree n = t;
7352 if (t == integer_zero_node)
7353 t = omp_private_member_vec.pop ();
7354 tree *v = omp_private_member_map->get (t);
7355 gcc_assert (v);
7356 save.safe_push (*v);
7357 save.safe_push (t);
7358 if (n != t)
7359 save.safe_push (n);
7361 delete omp_private_member_map;
7362 omp_private_member_map = NULL;
7365 /* Restore OpenMP privatization clauses mapping saved by the
7366 above function. */
7368 void
7369 restore_omp_privatization_clauses (vec<tree> &save)
7371 gcc_assert (omp_private_member_vec.is_empty ());
7372 omp_private_member_ignore_next = false;
7373 if (save.is_empty ())
7374 return;
7375 if (save.length () == 1 && save[0] == integer_one_node)
7377 omp_private_member_ignore_next = true;
7378 save.release ();
7379 return;
7382 omp_private_member_map = new hash_map <tree, tree>;
7383 while (!save.is_empty ())
7385 tree t = save.pop ();
7386 tree n = t;
7387 if (t != error_mark_node)
7389 if (t == integer_one_node)
7391 omp_private_member_ignore_next = true;
7392 gcc_assert (save.is_empty ());
7393 break;
7395 if (t == integer_zero_node)
7396 t = save.pop ();
7397 tree &v = omp_private_member_map->get_or_insert (t);
7398 v = save.pop ();
7400 omp_private_member_vec.safe_push (t);
7401 if (n != t)
7402 omp_private_member_vec.safe_push (n);
7404 save.release ();
7407 /* For all variables in the tree_list VARS, mark them as thread local. */
7409 void
7410 finish_omp_threadprivate (tree vars)
7412 tree t;
7414 /* Mark every variable in VARS to be assigned thread local storage. */
7415 for (t = vars; t; t = TREE_CHAIN (t))
7417 tree v = TREE_PURPOSE (t);
7419 if (error_operand_p (v))
7421 else if (!VAR_P (v))
7422 error ("%<threadprivate%> %qD is not file, namespace "
7423 "or block scope variable", v);
7424 /* If V had already been marked threadprivate, it doesn't matter
7425 whether it had been used prior to this point. */
7426 else if (TREE_USED (v)
7427 && (DECL_LANG_SPECIFIC (v) == NULL
7428 || !CP_DECL_THREADPRIVATE_P (v)))
7429 error ("%qE declared %<threadprivate%> after first use", v);
7430 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7431 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7432 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7433 error ("%<threadprivate%> %qE has incomplete type", v);
7434 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7435 && CP_DECL_CONTEXT (v) != current_class_type)
7436 error ("%<threadprivate%> %qE directive not "
7437 "in %qT definition", v, CP_DECL_CONTEXT (v));
7438 else
7440 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7441 if (DECL_LANG_SPECIFIC (v) == NULL)
7443 retrofit_lang_decl (v);
7445 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7446 after the allocation of the lang_decl structure. */
7447 if (DECL_DISCRIMINATOR_P (v))
7448 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7451 if (! CP_DECL_THREAD_LOCAL_P (v))
7453 CP_DECL_THREAD_LOCAL_P (v) = true;
7454 set_decl_tls_model (v, decl_default_tls_model (v));
7455 /* If rtl has been already set for this var, call
7456 make_decl_rtl once again, so that encode_section_info
7457 has a chance to look at the new decl flags. */
7458 if (DECL_RTL_SET_P (v))
7459 make_decl_rtl (v);
7461 CP_DECL_THREADPRIVATE_P (v) = 1;
7466 /* Build an OpenMP structured block. */
7468 tree
7469 begin_omp_structured_block (void)
7471 return do_pushlevel (sk_omp);
7474 tree
7475 finish_omp_structured_block (tree block)
7477 return do_poplevel (block);
7480 /* Similarly, except force the retention of the BLOCK. */
7482 tree
7483 begin_omp_parallel (void)
7485 keep_next_level (true);
7486 return begin_omp_structured_block ();
7489 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7490 statement. */
7492 tree
7493 finish_oacc_data (tree clauses, tree block)
7495 tree stmt;
7497 block = finish_omp_structured_block (block);
7499 stmt = make_node (OACC_DATA);
7500 TREE_TYPE (stmt) = void_type_node;
7501 OACC_DATA_CLAUSES (stmt) = clauses;
7502 OACC_DATA_BODY (stmt) = block;
7504 return add_stmt (stmt);
7507 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7508 statement. */
7510 tree
7511 finish_oacc_host_data (tree clauses, tree block)
7513 tree stmt;
7515 block = finish_omp_structured_block (block);
7517 stmt = make_node (OACC_HOST_DATA);
7518 TREE_TYPE (stmt) = void_type_node;
7519 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7520 OACC_HOST_DATA_BODY (stmt) = block;
7522 return add_stmt (stmt);
7525 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7526 statement. */
7528 tree
7529 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7531 body = finish_omp_structured_block (body);
7533 tree stmt = make_node (code);
7534 TREE_TYPE (stmt) = void_type_node;
7535 OMP_BODY (stmt) = body;
7536 OMP_CLAUSES (stmt) = clauses;
7538 return add_stmt (stmt);
7541 tree
7542 finish_omp_parallel (tree clauses, tree body)
7544 tree stmt;
7546 body = finish_omp_structured_block (body);
7548 stmt = make_node (OMP_PARALLEL);
7549 TREE_TYPE (stmt) = void_type_node;
7550 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7551 OMP_PARALLEL_BODY (stmt) = body;
7553 return add_stmt (stmt);
7556 tree
7557 begin_omp_task (void)
7559 keep_next_level (true);
7560 return begin_omp_structured_block ();
7563 tree
7564 finish_omp_task (tree clauses, tree body)
7566 tree stmt;
7568 body = finish_omp_structured_block (body);
7570 stmt = make_node (OMP_TASK);
7571 TREE_TYPE (stmt) = void_type_node;
7572 OMP_TASK_CLAUSES (stmt) = clauses;
7573 OMP_TASK_BODY (stmt) = body;
7575 return add_stmt (stmt);
7578 /* Helper function for finish_omp_for. Convert Ith random access iterator
7579 into integral iterator. Return FALSE if successful. */
7581 static bool
7582 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7583 tree declv, tree orig_declv, tree initv,
7584 tree condv, tree incrv, tree *body,
7585 tree *pre_body, tree &clauses, tree *lastp,
7586 int collapse, int ordered)
7588 tree diff, iter_init, iter_incr = NULL, last;
7589 tree incr_var = NULL, orig_pre_body, orig_body, c;
7590 tree decl = TREE_VEC_ELT (declv, i);
7591 tree init = TREE_VEC_ELT (initv, i);
7592 tree cond = TREE_VEC_ELT (condv, i);
7593 tree incr = TREE_VEC_ELT (incrv, i);
7594 tree iter = decl;
7595 location_t elocus = locus;
7597 if (init && EXPR_HAS_LOCATION (init))
7598 elocus = EXPR_LOCATION (init);
7600 cond = cp_fully_fold (cond);
7601 switch (TREE_CODE (cond))
7603 case GT_EXPR:
7604 case GE_EXPR:
7605 case LT_EXPR:
7606 case LE_EXPR:
7607 case NE_EXPR:
7608 if (TREE_OPERAND (cond, 1) == iter)
7609 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7610 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7611 if (TREE_OPERAND (cond, 0) != iter)
7612 cond = error_mark_node;
7613 else
7615 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7616 TREE_CODE (cond),
7617 iter, ERROR_MARK,
7618 TREE_OPERAND (cond, 1), ERROR_MARK,
7619 NULL, tf_warning_or_error);
7620 if (error_operand_p (tem))
7621 return true;
7623 break;
7624 default:
7625 cond = error_mark_node;
7626 break;
7628 if (cond == error_mark_node)
7630 error_at (elocus, "invalid controlling predicate");
7631 return true;
7633 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7634 ERROR_MARK, iter, ERROR_MARK, NULL,
7635 tf_warning_or_error);
7636 diff = cp_fully_fold (diff);
7637 if (error_operand_p (diff))
7638 return true;
7639 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7641 error_at (elocus, "difference between %qE and %qD does not have integer type",
7642 TREE_OPERAND (cond, 1), iter);
7643 return true;
7645 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7646 TREE_VEC_ELT (declv, i), NULL_TREE,
7647 cond, cp_walk_subtrees))
7648 return true;
7650 switch (TREE_CODE (incr))
7652 case PREINCREMENT_EXPR:
7653 case PREDECREMENT_EXPR:
7654 case POSTINCREMENT_EXPR:
7655 case POSTDECREMENT_EXPR:
7656 if (TREE_OPERAND (incr, 0) != iter)
7658 incr = error_mark_node;
7659 break;
7661 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7662 TREE_CODE (incr), iter,
7663 tf_warning_or_error);
7664 if (error_operand_p (iter_incr))
7665 return true;
7666 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7667 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7668 incr = integer_one_node;
7669 else
7670 incr = integer_minus_one_node;
7671 break;
7672 case MODIFY_EXPR:
7673 if (TREE_OPERAND (incr, 0) != iter)
7674 incr = error_mark_node;
7675 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7676 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7678 tree rhs = TREE_OPERAND (incr, 1);
7679 if (TREE_OPERAND (rhs, 0) == iter)
7681 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7682 != INTEGER_TYPE)
7683 incr = error_mark_node;
7684 else
7686 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7687 iter, TREE_CODE (rhs),
7688 TREE_OPERAND (rhs, 1),
7689 tf_warning_or_error);
7690 if (error_operand_p (iter_incr))
7691 return true;
7692 incr = TREE_OPERAND (rhs, 1);
7693 incr = cp_convert (TREE_TYPE (diff), incr,
7694 tf_warning_or_error);
7695 if (TREE_CODE (rhs) == MINUS_EXPR)
7697 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7698 incr = fold_simple (incr);
7700 if (TREE_CODE (incr) != INTEGER_CST
7701 && (TREE_CODE (incr) != NOP_EXPR
7702 || (TREE_CODE (TREE_OPERAND (incr, 0))
7703 != INTEGER_CST)))
7704 iter_incr = NULL;
7707 else if (TREE_OPERAND (rhs, 1) == iter)
7709 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7710 || TREE_CODE (rhs) != PLUS_EXPR)
7711 incr = error_mark_node;
7712 else
7714 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7715 PLUS_EXPR,
7716 TREE_OPERAND (rhs, 0),
7717 ERROR_MARK, iter,
7718 ERROR_MARK, NULL,
7719 tf_warning_or_error);
7720 if (error_operand_p (iter_incr))
7721 return true;
7722 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7723 iter, NOP_EXPR,
7724 iter_incr,
7725 tf_warning_or_error);
7726 if (error_operand_p (iter_incr))
7727 return true;
7728 incr = TREE_OPERAND (rhs, 0);
7729 iter_incr = NULL;
7732 else
7733 incr = error_mark_node;
7735 else
7736 incr = error_mark_node;
7737 break;
7738 default:
7739 incr = error_mark_node;
7740 break;
7743 if (incr == error_mark_node)
7745 error_at (elocus, "invalid increment expression");
7746 return true;
7749 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7750 bool taskloop_iv_seen = false;
7751 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7752 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7753 && OMP_CLAUSE_DECL (c) == iter)
7755 if (code == OMP_TASKLOOP)
7757 taskloop_iv_seen = true;
7758 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7760 break;
7762 else if (code == OMP_TASKLOOP
7763 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7764 && OMP_CLAUSE_DECL (c) == iter)
7766 taskloop_iv_seen = true;
7767 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7770 decl = create_temporary_var (TREE_TYPE (diff));
7771 pushdecl (decl);
7772 add_decl_expr (decl);
7773 last = create_temporary_var (TREE_TYPE (diff));
7774 pushdecl (last);
7775 add_decl_expr (last);
7776 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7777 && (!ordered || (i < collapse && collapse > 1)))
7779 incr_var = create_temporary_var (TREE_TYPE (diff));
7780 pushdecl (incr_var);
7781 add_decl_expr (incr_var);
7783 gcc_assert (stmts_are_full_exprs_p ());
7784 tree diffvar = NULL_TREE;
7785 if (code == OMP_TASKLOOP)
7787 if (!taskloop_iv_seen)
7789 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7790 OMP_CLAUSE_DECL (ivc) = iter;
7791 cxx_omp_finish_clause (ivc, NULL);
7792 OMP_CLAUSE_CHAIN (ivc) = clauses;
7793 clauses = ivc;
7795 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7796 OMP_CLAUSE_DECL (lvc) = last;
7797 OMP_CLAUSE_CHAIN (lvc) = clauses;
7798 clauses = lvc;
7799 diffvar = create_temporary_var (TREE_TYPE (diff));
7800 pushdecl (diffvar);
7801 add_decl_expr (diffvar);
7804 orig_pre_body = *pre_body;
7805 *pre_body = push_stmt_list ();
7806 if (orig_pre_body)
7807 add_stmt (orig_pre_body);
7808 if (init != NULL)
7809 finish_expr_stmt (build_x_modify_expr (elocus,
7810 iter, NOP_EXPR, init,
7811 tf_warning_or_error));
7812 init = build_int_cst (TREE_TYPE (diff), 0);
7813 if (c && iter_incr == NULL
7814 && (!ordered || (i < collapse && collapse > 1)))
7816 if (incr_var)
7818 finish_expr_stmt (build_x_modify_expr (elocus,
7819 incr_var, NOP_EXPR,
7820 incr, tf_warning_or_error));
7821 incr = incr_var;
7823 iter_incr = build_x_modify_expr (elocus,
7824 iter, PLUS_EXPR, incr,
7825 tf_warning_or_error);
7827 if (c && ordered && i < collapse && collapse > 1)
7828 iter_incr = incr;
7829 finish_expr_stmt (build_x_modify_expr (elocus,
7830 last, NOP_EXPR, init,
7831 tf_warning_or_error));
7832 if (diffvar)
7834 finish_expr_stmt (build_x_modify_expr (elocus,
7835 diffvar, NOP_EXPR,
7836 diff, tf_warning_or_error));
7837 diff = diffvar;
7839 *pre_body = pop_stmt_list (*pre_body);
7841 cond = cp_build_binary_op (elocus,
7842 TREE_CODE (cond), decl, diff,
7843 tf_warning_or_error);
7844 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7845 elocus, incr, NULL_TREE);
7847 orig_body = *body;
7848 *body = push_stmt_list ();
7849 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7850 iter_init = build_x_modify_expr (elocus,
7851 iter, PLUS_EXPR, iter_init,
7852 tf_warning_or_error);
7853 if (iter_init != error_mark_node)
7854 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7855 finish_expr_stmt (iter_init);
7856 finish_expr_stmt (build_x_modify_expr (elocus,
7857 last, NOP_EXPR, decl,
7858 tf_warning_or_error));
7859 add_stmt (orig_body);
7860 *body = pop_stmt_list (*body);
7862 if (c)
7864 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7865 if (!ordered)
7866 finish_expr_stmt (iter_incr);
7867 else
7869 iter_init = decl;
7870 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7871 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7872 iter_init, iter_incr);
7873 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7874 iter_init = build_x_modify_expr (elocus,
7875 iter, PLUS_EXPR, iter_init,
7876 tf_warning_or_error);
7877 if (iter_init != error_mark_node)
7878 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7879 finish_expr_stmt (iter_init);
7881 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7882 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7885 TREE_VEC_ELT (declv, i) = decl;
7886 TREE_VEC_ELT (initv, i) = init;
7887 TREE_VEC_ELT (condv, i) = cond;
7888 TREE_VEC_ELT (incrv, i) = incr;
7889 *lastp = last;
7891 return false;
7894 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7895 are directly for their associated operands in the statement. DECL
7896 and INIT are a combo; if DECL is NULL then INIT ought to be a
7897 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7898 optional statements that need to go before the loop into its
7899 sk_omp scope. */
7901 tree
7902 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7903 tree orig_declv, tree initv, tree condv, tree incrv,
7904 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7906 tree omp_for = NULL, orig_incr = NULL;
7907 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7908 tree last = NULL_TREE;
7909 location_t elocus;
7910 int i;
7911 int collapse = 1;
7912 int ordered = 0;
7914 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7915 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7916 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7917 if (TREE_VEC_LENGTH (declv) > 1)
7919 tree c = find_omp_clause (clauses, OMP_CLAUSE_COLLAPSE);
7920 if (c)
7921 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7922 if (collapse != TREE_VEC_LENGTH (declv))
7923 ordered = TREE_VEC_LENGTH (declv);
7925 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7927 decl = TREE_VEC_ELT (declv, i);
7928 init = TREE_VEC_ELT (initv, i);
7929 cond = TREE_VEC_ELT (condv, i);
7930 incr = TREE_VEC_ELT (incrv, i);
7931 elocus = locus;
7933 if (decl == NULL)
7935 if (init != NULL)
7936 switch (TREE_CODE (init))
7938 case MODIFY_EXPR:
7939 decl = TREE_OPERAND (init, 0);
7940 init = TREE_OPERAND (init, 1);
7941 break;
7942 case MODOP_EXPR:
7943 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
7945 decl = TREE_OPERAND (init, 0);
7946 init = TREE_OPERAND (init, 2);
7948 break;
7949 default:
7950 break;
7953 if (decl == NULL)
7955 error_at (locus,
7956 "expected iteration declaration or initialization");
7957 return NULL;
7961 if (init && EXPR_HAS_LOCATION (init))
7962 elocus = EXPR_LOCATION (init);
7964 if (cond == NULL)
7966 error_at (elocus, "missing controlling predicate");
7967 return NULL;
7970 if (incr == NULL)
7972 error_at (elocus, "missing increment expression");
7973 return NULL;
7976 TREE_VEC_ELT (declv, i) = decl;
7977 TREE_VEC_ELT (initv, i) = init;
7980 if (orig_inits)
7982 bool fail = false;
7983 tree orig_init;
7984 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
7985 if (orig_init
7986 && !c_omp_check_loop_iv_exprs (locus, declv,
7987 TREE_VEC_ELT (declv, i), orig_init,
7988 NULL_TREE, cp_walk_subtrees))
7989 fail = true;
7990 if (fail)
7991 return NULL;
7994 if (dependent_omp_for_p (declv, initv, condv, incrv))
7996 tree stmt;
7998 stmt = make_node (code);
8000 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8002 /* This is really just a place-holder. We'll be decomposing this
8003 again and going through the cp_build_modify_expr path below when
8004 we instantiate the thing. */
8005 TREE_VEC_ELT (initv, i)
8006 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8007 TREE_VEC_ELT (initv, i));
8010 TREE_TYPE (stmt) = void_type_node;
8011 OMP_FOR_INIT (stmt) = initv;
8012 OMP_FOR_COND (stmt) = condv;
8013 OMP_FOR_INCR (stmt) = incrv;
8014 OMP_FOR_BODY (stmt) = body;
8015 OMP_FOR_PRE_BODY (stmt) = pre_body;
8016 OMP_FOR_CLAUSES (stmt) = clauses;
8018 SET_EXPR_LOCATION (stmt, locus);
8019 return add_stmt (stmt);
8022 if (!orig_declv)
8023 orig_declv = copy_node (declv);
8025 if (processing_template_decl)
8026 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8028 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8030 decl = TREE_VEC_ELT (declv, i);
8031 init = TREE_VEC_ELT (initv, i);
8032 cond = TREE_VEC_ELT (condv, i);
8033 incr = TREE_VEC_ELT (incrv, i);
8034 if (orig_incr)
8035 TREE_VEC_ELT (orig_incr, i) = incr;
8036 elocus = locus;
8038 if (init && EXPR_HAS_LOCATION (init))
8039 elocus = EXPR_LOCATION (init);
8041 if (!DECL_P (decl))
8043 error_at (elocus, "expected iteration declaration or initialization");
8044 return NULL;
8047 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8049 if (orig_incr)
8050 TREE_VEC_ELT (orig_incr, i) = incr;
8051 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
8052 TREE_CODE (TREE_OPERAND (incr, 1)),
8053 TREE_OPERAND (incr, 2),
8054 tf_warning_or_error);
8057 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8059 if (code == OMP_SIMD)
8061 error_at (elocus, "%<#pragma omp simd%> used with class "
8062 "iteration variable %qE", decl);
8063 return NULL;
8065 if (code == CILK_FOR && i == 0)
8066 orig_decl = decl;
8067 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8068 initv, condv, incrv, &body,
8069 &pre_body, clauses, &last,
8070 collapse, ordered))
8071 return NULL;
8072 continue;
8075 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8076 && !TYPE_PTR_P (TREE_TYPE (decl)))
8078 error_at (elocus, "invalid type for iteration variable %qE", decl);
8079 return NULL;
8082 if (!processing_template_decl)
8084 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8085 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
8087 else
8088 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8089 if (cond
8090 && TREE_SIDE_EFFECTS (cond)
8091 && COMPARISON_CLASS_P (cond)
8092 && !processing_template_decl)
8094 tree t = TREE_OPERAND (cond, 0);
8095 if (TREE_SIDE_EFFECTS (t)
8096 && t != decl
8097 && (TREE_CODE (t) != NOP_EXPR
8098 || TREE_OPERAND (t, 0) != decl))
8099 TREE_OPERAND (cond, 0)
8100 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8102 t = TREE_OPERAND (cond, 1);
8103 if (TREE_SIDE_EFFECTS (t)
8104 && t != decl
8105 && (TREE_CODE (t) != NOP_EXPR
8106 || TREE_OPERAND (t, 0) != decl))
8107 TREE_OPERAND (cond, 1)
8108 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8110 if (decl == error_mark_node || init == error_mark_node)
8111 return NULL;
8113 TREE_VEC_ELT (declv, i) = decl;
8114 TREE_VEC_ELT (initv, i) = init;
8115 TREE_VEC_ELT (condv, i) = cond;
8116 TREE_VEC_ELT (incrv, i) = incr;
8117 i++;
8120 if (IS_EMPTY_STMT (pre_body))
8121 pre_body = NULL;
8123 if (code == CILK_FOR && !processing_template_decl)
8124 block = push_stmt_list ();
8126 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8127 incrv, body, pre_body);
8129 /* Check for iterators appearing in lb, b or incr expressions. */
8130 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8131 omp_for = NULL_TREE;
8133 if (omp_for == NULL)
8135 if (block)
8136 pop_stmt_list (block);
8137 return NULL;
8140 add_stmt (omp_for);
8142 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8144 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8145 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8147 if (TREE_CODE (incr) != MODIFY_EXPR)
8148 continue;
8150 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8151 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8152 && !processing_template_decl)
8154 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8155 if (TREE_SIDE_EFFECTS (t)
8156 && t != decl
8157 && (TREE_CODE (t) != NOP_EXPR
8158 || TREE_OPERAND (t, 0) != decl))
8159 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8160 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8162 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8163 if (TREE_SIDE_EFFECTS (t)
8164 && t != decl
8165 && (TREE_CODE (t) != NOP_EXPR
8166 || TREE_OPERAND (t, 0) != decl))
8167 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8168 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8171 if (orig_incr)
8172 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8174 OMP_FOR_CLAUSES (omp_for) = clauses;
8176 /* For simd loops with non-static data member iterators, we could have added
8177 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8178 step at this point, fill it in. */
8179 if (code == OMP_SIMD && !processing_template_decl
8180 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8181 for (tree c = find_omp_clause (clauses, OMP_CLAUSE_LINEAR); c;
8182 c = find_omp_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8183 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8185 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8186 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8187 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8188 tree step, stept;
8189 switch (TREE_CODE (incr))
8191 case PREINCREMENT_EXPR:
8192 case POSTINCREMENT_EXPR:
8193 /* c_omp_for_incr_canonicalize_ptr() should have been
8194 called to massage things appropriately. */
8195 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8196 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8197 break;
8198 case PREDECREMENT_EXPR:
8199 case POSTDECREMENT_EXPR:
8200 /* c_omp_for_incr_canonicalize_ptr() should have been
8201 called to massage things appropriately. */
8202 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8203 OMP_CLAUSE_LINEAR_STEP (c)
8204 = build_int_cst (TREE_TYPE (decl), -1);
8205 break;
8206 case MODIFY_EXPR:
8207 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8208 incr = TREE_OPERAND (incr, 1);
8209 switch (TREE_CODE (incr))
8211 case PLUS_EXPR:
8212 if (TREE_OPERAND (incr, 1) == decl)
8213 step = TREE_OPERAND (incr, 0);
8214 else
8215 step = TREE_OPERAND (incr, 1);
8216 break;
8217 case MINUS_EXPR:
8218 case POINTER_PLUS_EXPR:
8219 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8220 step = TREE_OPERAND (incr, 1);
8221 break;
8222 default:
8223 gcc_unreachable ();
8225 stept = TREE_TYPE (decl);
8226 if (POINTER_TYPE_P (stept))
8227 stept = sizetype;
8228 step = fold_convert (stept, step);
8229 if (TREE_CODE (incr) == MINUS_EXPR)
8230 step = fold_build1 (NEGATE_EXPR, stept, step);
8231 OMP_CLAUSE_LINEAR_STEP (c) = step;
8232 break;
8233 default:
8234 gcc_unreachable ();
8238 if (block)
8240 tree omp_par = make_node (OMP_PARALLEL);
8241 TREE_TYPE (omp_par) = void_type_node;
8242 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8243 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8244 TREE_SIDE_EFFECTS (bind) = 1;
8245 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8246 OMP_PARALLEL_BODY (omp_par) = bind;
8247 if (OMP_FOR_PRE_BODY (omp_for))
8249 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8250 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8252 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8253 decl = TREE_OPERAND (init, 0);
8254 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8255 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8256 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8257 clauses = OMP_FOR_CLAUSES (omp_for);
8258 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8259 for (pc = &clauses; *pc; )
8260 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8262 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8263 OMP_FOR_CLAUSES (omp_for) = *pc;
8264 *pc = OMP_CLAUSE_CHAIN (*pc);
8265 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8267 else
8269 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8270 pc = &OMP_CLAUSE_CHAIN (*pc);
8272 if (TREE_CODE (t) != INTEGER_CST)
8274 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8275 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8276 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8277 OMP_CLAUSE_CHAIN (c) = clauses;
8278 clauses = c;
8280 if (TREE_CODE (incr) == MODIFY_EXPR)
8282 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8283 if (TREE_CODE (t) != INTEGER_CST)
8285 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8286 = get_temp_regvar (TREE_TYPE (t), t);
8287 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8288 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8289 OMP_CLAUSE_CHAIN (c) = clauses;
8290 clauses = c;
8293 t = TREE_OPERAND (init, 1);
8294 if (TREE_CODE (t) != INTEGER_CST)
8296 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8297 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8298 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8299 OMP_CLAUSE_CHAIN (c) = clauses;
8300 clauses = c;
8302 if (orig_decl && orig_decl != decl)
8304 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8305 OMP_CLAUSE_DECL (c) = orig_decl;
8306 OMP_CLAUSE_CHAIN (c) = clauses;
8307 clauses = c;
8309 if (last)
8311 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8312 OMP_CLAUSE_DECL (c) = last;
8313 OMP_CLAUSE_CHAIN (c) = clauses;
8314 clauses = c;
8316 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8317 OMP_CLAUSE_DECL (c) = decl;
8318 OMP_CLAUSE_CHAIN (c) = clauses;
8319 clauses = c;
8320 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8321 OMP_CLAUSE_OPERAND (c, 0)
8322 = cilk_for_number_of_iterations (omp_for);
8323 OMP_CLAUSE_CHAIN (c) = clauses;
8324 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, false);
8325 add_stmt (omp_par);
8326 return omp_par;
8328 else if (code == CILK_FOR && processing_template_decl)
8330 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8331 if (orig_decl && orig_decl != decl)
8333 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8334 OMP_CLAUSE_DECL (c) = orig_decl;
8335 OMP_CLAUSE_CHAIN (c) = clauses;
8336 clauses = c;
8338 if (last)
8340 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8341 OMP_CLAUSE_DECL (c) = last;
8342 OMP_CLAUSE_CHAIN (c) = clauses;
8343 clauses = c;
8345 OMP_FOR_CLAUSES (omp_for) = clauses;
8347 return omp_for;
8350 void
8351 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8352 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8354 tree orig_lhs;
8355 tree orig_rhs;
8356 tree orig_v;
8357 tree orig_lhs1;
8358 tree orig_rhs1;
8359 bool dependent_p;
8360 tree stmt;
8362 orig_lhs = lhs;
8363 orig_rhs = rhs;
8364 orig_v = v;
8365 orig_lhs1 = lhs1;
8366 orig_rhs1 = rhs1;
8367 dependent_p = false;
8368 stmt = NULL_TREE;
8370 /* Even in a template, we can detect invalid uses of the atomic
8371 pragma if neither LHS nor RHS is type-dependent. */
8372 if (processing_template_decl)
8374 dependent_p = (type_dependent_expression_p (lhs)
8375 || (rhs && type_dependent_expression_p (rhs))
8376 || (v && type_dependent_expression_p (v))
8377 || (lhs1 && type_dependent_expression_p (lhs1))
8378 || (rhs1 && type_dependent_expression_p (rhs1)));
8379 if (!dependent_p)
8381 lhs = build_non_dependent_expr (lhs);
8382 if (rhs)
8383 rhs = build_non_dependent_expr (rhs);
8384 if (v)
8385 v = build_non_dependent_expr (v);
8386 if (lhs1)
8387 lhs1 = build_non_dependent_expr (lhs1);
8388 if (rhs1)
8389 rhs1 = build_non_dependent_expr (rhs1);
8392 if (!dependent_p)
8394 bool swapped = false;
8395 if (rhs1 && cp_tree_equal (lhs, rhs))
8397 std::swap (rhs, rhs1);
8398 swapped = !commutative_tree_code (opcode);
8400 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8402 if (code == OMP_ATOMIC)
8403 error ("%<#pragma omp atomic update%> uses two different "
8404 "expressions for memory");
8405 else
8406 error ("%<#pragma omp atomic capture%> uses two different "
8407 "expressions for memory");
8408 return;
8410 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8412 if (code == OMP_ATOMIC)
8413 error ("%<#pragma omp atomic update%> uses two different "
8414 "expressions for memory");
8415 else
8416 error ("%<#pragma omp atomic capture%> uses two different "
8417 "expressions for memory");
8418 return;
8420 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8421 v, lhs1, rhs1, swapped, seq_cst,
8422 processing_template_decl != 0);
8423 if (stmt == error_mark_node)
8424 return;
8426 if (processing_template_decl)
8428 if (code == OMP_ATOMIC_READ)
8430 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8431 OMP_ATOMIC_READ, orig_lhs);
8432 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8433 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8435 else
8437 if (opcode == NOP_EXPR)
8438 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8439 else
8440 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8441 if (orig_rhs1)
8442 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8443 COMPOUND_EXPR, orig_rhs1, stmt);
8444 if (code != OMP_ATOMIC)
8446 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8447 code, orig_lhs1, stmt);
8448 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8449 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8452 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8453 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8455 finish_expr_stmt (stmt);
8458 void
8459 finish_omp_barrier (void)
8461 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8462 vec<tree, va_gc> *vec = make_tree_vector ();
8463 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8464 release_tree_vector (vec);
8465 finish_expr_stmt (stmt);
8468 void
8469 finish_omp_flush (void)
8471 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8472 vec<tree, va_gc> *vec = make_tree_vector ();
8473 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8474 release_tree_vector (vec);
8475 finish_expr_stmt (stmt);
8478 void
8479 finish_omp_taskwait (void)
8481 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8482 vec<tree, va_gc> *vec = make_tree_vector ();
8483 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8484 release_tree_vector (vec);
8485 finish_expr_stmt (stmt);
8488 void
8489 finish_omp_taskyield (void)
8491 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8492 vec<tree, va_gc> *vec = make_tree_vector ();
8493 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8494 release_tree_vector (vec);
8495 finish_expr_stmt (stmt);
8498 void
8499 finish_omp_cancel (tree clauses)
8501 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8502 int mask = 0;
8503 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8504 mask = 1;
8505 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8506 mask = 2;
8507 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8508 mask = 4;
8509 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8510 mask = 8;
8511 else
8513 error ("%<#pragma omp cancel must specify one of "
8514 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8515 return;
8517 vec<tree, va_gc> *vec = make_tree_vector ();
8518 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
8519 if (ifc != NULL_TREE)
8521 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8522 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8523 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8524 build_zero_cst (type));
8526 else
8527 ifc = boolean_true_node;
8528 vec->quick_push (build_int_cst (integer_type_node, mask));
8529 vec->quick_push (ifc);
8530 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8531 release_tree_vector (vec);
8532 finish_expr_stmt (stmt);
8535 void
8536 finish_omp_cancellation_point (tree clauses)
8538 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8539 int mask = 0;
8540 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8541 mask = 1;
8542 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8543 mask = 2;
8544 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8545 mask = 4;
8546 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8547 mask = 8;
8548 else
8550 error ("%<#pragma omp cancellation point must specify one of "
8551 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8552 return;
8554 vec<tree, va_gc> *vec
8555 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8556 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8557 release_tree_vector (vec);
8558 finish_expr_stmt (stmt);
8561 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8562 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8563 should create an extra compound stmt. */
8565 tree
8566 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8568 tree r;
8570 if (pcompound)
8571 *pcompound = begin_compound_stmt (0);
8573 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8575 /* Only add the statement to the function if support enabled. */
8576 if (flag_tm)
8577 add_stmt (r);
8578 else
8579 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8580 ? G_("%<__transaction_relaxed%> without "
8581 "transactional memory support enabled")
8582 : G_("%<__transaction_atomic%> without "
8583 "transactional memory support enabled")));
8585 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8586 TREE_SIDE_EFFECTS (r) = 1;
8587 return r;
8590 /* End a __transaction_atomic or __transaction_relaxed statement.
8591 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8592 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8593 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8595 void
8596 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8598 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8599 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8600 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8601 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8603 /* noexcept specifications are not allowed for function transactions. */
8604 gcc_assert (!(noex && compound_stmt));
8605 if (noex)
8607 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8608 noex);
8609 protected_set_expr_location
8610 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8611 TREE_SIDE_EFFECTS (body) = 1;
8612 TRANSACTION_EXPR_BODY (stmt) = body;
8615 if (compound_stmt)
8616 finish_compound_stmt (compound_stmt);
8619 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8620 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8621 condition. */
8623 tree
8624 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8626 tree ret;
8627 if (noex)
8629 expr = build_must_not_throw_expr (expr, noex);
8630 protected_set_expr_location (expr, loc);
8631 TREE_SIDE_EFFECTS (expr) = 1;
8633 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8634 if (flags & TM_STMT_ATTR_RELAXED)
8635 TRANSACTION_EXPR_RELAXED (ret) = 1;
8636 TREE_SIDE_EFFECTS (ret) = 1;
8637 SET_EXPR_LOCATION (ret, loc);
8638 return ret;
8641 void
8642 init_cp_semantics (void)
8646 /* Build a STATIC_ASSERT for a static assertion with the condition
8647 CONDITION and the message text MESSAGE. LOCATION is the location
8648 of the static assertion in the source code. When MEMBER_P, this
8649 static assertion is a member of a class. */
8650 void
8651 finish_static_assert (tree condition, tree message, location_t location,
8652 bool member_p)
8654 if (message == NULL_TREE
8655 || message == error_mark_node
8656 || condition == NULL_TREE
8657 || condition == error_mark_node)
8658 return;
8660 if (check_for_bare_parameter_packs (condition))
8661 condition = error_mark_node;
8663 if (type_dependent_expression_p (condition)
8664 || value_dependent_expression_p (condition))
8666 /* We're in a template; build a STATIC_ASSERT and put it in
8667 the right place. */
8668 tree assertion;
8670 assertion = make_node (STATIC_ASSERT);
8671 STATIC_ASSERT_CONDITION (assertion) = condition;
8672 STATIC_ASSERT_MESSAGE (assertion) = message;
8673 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8675 if (member_p)
8676 maybe_add_class_template_decl_list (current_class_type,
8677 assertion,
8678 /*friend_p=*/0);
8679 else
8680 add_stmt (assertion);
8682 return;
8685 /* Fold the expression and convert it to a boolean value. */
8686 condition = instantiate_non_dependent_expr (condition);
8687 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8688 condition = maybe_constant_value (condition);
8690 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8691 /* Do nothing; the condition is satisfied. */
8693 else
8695 location_t saved_loc = input_location;
8697 input_location = location;
8698 if (TREE_CODE (condition) == INTEGER_CST
8699 && integer_zerop (condition))
8701 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8702 (TREE_TYPE (TREE_TYPE (message))));
8703 int len = TREE_STRING_LENGTH (message) / sz - 1;
8704 /* Report the error. */
8705 if (len == 0)
8706 error ("static assertion failed");
8707 else
8708 error ("static assertion failed: %s",
8709 TREE_STRING_POINTER (message));
8711 else if (condition && condition != error_mark_node)
8713 error ("non-constant condition for static assertion");
8714 if (require_potential_rvalue_constant_expression (condition))
8715 cxx_constant_value (condition);
8717 input_location = saved_loc;
8721 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8722 suitable for use as a type-specifier.
8724 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8725 id-expression or a class member access, FALSE when it was parsed as
8726 a full expression. */
8728 tree
8729 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8730 tsubst_flags_t complain)
8732 tree type = NULL_TREE;
8734 if (!expr || error_operand_p (expr))
8735 return error_mark_node;
8737 if (TYPE_P (expr)
8738 || TREE_CODE (expr) == TYPE_DECL
8739 || (TREE_CODE (expr) == BIT_NOT_EXPR
8740 && TYPE_P (TREE_OPERAND (expr, 0))))
8742 if (complain & tf_error)
8743 error ("argument to decltype must be an expression");
8744 return error_mark_node;
8747 /* Depending on the resolution of DR 1172, we may later need to distinguish
8748 instantiation-dependent but not type-dependent expressions so that, say,
8749 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8750 if (instantiation_dependent_uneval_expression_p (expr))
8752 type = cxx_make_type (DECLTYPE_TYPE);
8753 DECLTYPE_TYPE_EXPR (type) = expr;
8754 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8755 = id_expression_or_member_access_p;
8756 SET_TYPE_STRUCTURAL_EQUALITY (type);
8758 return type;
8761 /* The type denoted by decltype(e) is defined as follows: */
8763 expr = resolve_nondeduced_context (expr, complain);
8765 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8766 return error_mark_node;
8768 if (type_unknown_p (expr))
8770 if (complain & tf_error)
8771 error ("decltype cannot resolve address of overloaded function");
8772 return error_mark_node;
8775 /* To get the size of a static data member declared as an array of
8776 unknown bound, we need to instantiate it. */
8777 if (VAR_P (expr)
8778 && VAR_HAD_UNKNOWN_BOUND (expr)
8779 && DECL_TEMPLATE_INSTANTIATION (expr))
8780 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8782 if (id_expression_or_member_access_p)
8784 /* If e is an id-expression or a class member access (5.2.5
8785 [expr.ref]), decltype(e) is defined as the type of the entity
8786 named by e. If there is no such entity, or e names a set of
8787 overloaded functions, the program is ill-formed. */
8788 if (identifier_p (expr))
8789 expr = lookup_name (expr);
8791 if (INDIRECT_REF_P (expr))
8792 /* This can happen when the expression is, e.g., "a.b". Just
8793 look at the underlying operand. */
8794 expr = TREE_OPERAND (expr, 0);
8796 if (TREE_CODE (expr) == OFFSET_REF
8797 || TREE_CODE (expr) == MEMBER_REF
8798 || TREE_CODE (expr) == SCOPE_REF)
8799 /* We're only interested in the field itself. If it is a
8800 BASELINK, we will need to see through it in the next
8801 step. */
8802 expr = TREE_OPERAND (expr, 1);
8804 if (BASELINK_P (expr))
8805 /* See through BASELINK nodes to the underlying function. */
8806 expr = BASELINK_FUNCTIONS (expr);
8808 switch (TREE_CODE (expr))
8810 case FIELD_DECL:
8811 if (DECL_BIT_FIELD_TYPE (expr))
8813 type = DECL_BIT_FIELD_TYPE (expr);
8814 break;
8816 /* Fall through for fields that aren't bitfields. */
8818 case FUNCTION_DECL:
8819 case VAR_DECL:
8820 case CONST_DECL:
8821 case PARM_DECL:
8822 case RESULT_DECL:
8823 case TEMPLATE_PARM_INDEX:
8824 expr = mark_type_use (expr);
8825 type = TREE_TYPE (expr);
8826 break;
8828 case ERROR_MARK:
8829 type = error_mark_node;
8830 break;
8832 case COMPONENT_REF:
8833 case COMPOUND_EXPR:
8834 mark_type_use (expr);
8835 type = is_bitfield_expr_with_lowered_type (expr);
8836 if (!type)
8837 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8838 break;
8840 case BIT_FIELD_REF:
8841 gcc_unreachable ();
8843 case INTEGER_CST:
8844 case PTRMEM_CST:
8845 /* We can get here when the id-expression refers to an
8846 enumerator or non-type template parameter. */
8847 type = TREE_TYPE (expr);
8848 break;
8850 default:
8851 /* Handle instantiated template non-type arguments. */
8852 type = TREE_TYPE (expr);
8853 break;
8856 else
8858 /* Within a lambda-expression:
8860 Every occurrence of decltype((x)) where x is a possibly
8861 parenthesized id-expression that names an entity of
8862 automatic storage duration is treated as if x were
8863 transformed into an access to a corresponding data member
8864 of the closure type that would have been declared if x
8865 were a use of the denoted entity. */
8866 if (outer_automatic_var_p (expr)
8867 && current_function_decl
8868 && LAMBDA_FUNCTION_P (current_function_decl))
8869 type = capture_decltype (expr);
8870 else if (error_operand_p (expr))
8871 type = error_mark_node;
8872 else if (expr == current_class_ptr)
8873 /* If the expression is just "this", we want the
8874 cv-unqualified pointer for the "this" type. */
8875 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8876 else
8878 /* Otherwise, where T is the type of e, if e is an lvalue,
8879 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8880 cp_lvalue_kind clk = lvalue_kind (expr);
8881 type = unlowered_expr_type (expr);
8882 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8884 /* For vector types, pick a non-opaque variant. */
8885 if (VECTOR_TYPE_P (type))
8886 type = strip_typedefs (type);
8888 if (clk != clk_none && !(clk & clk_class))
8889 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8893 return type;
8896 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8897 __has_nothrow_copy, depending on assign_p. */
8899 static bool
8900 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8902 tree fns;
8904 if (assign_p)
8906 int ix;
8907 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
8908 if (ix < 0)
8909 return false;
8910 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
8912 else if (TYPE_HAS_COPY_CTOR (type))
8914 /* If construction of the copy constructor was postponed, create
8915 it now. */
8916 if (CLASSTYPE_LAZY_COPY_CTOR (type))
8917 lazily_declare_fn (sfk_copy_constructor, type);
8918 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
8919 lazily_declare_fn (sfk_move_constructor, type);
8920 fns = CLASSTYPE_CONSTRUCTORS (type);
8922 else
8923 return false;
8925 for (; fns; fns = OVL_NEXT (fns))
8927 tree fn = OVL_CURRENT (fns);
8929 if (assign_p)
8931 if (copy_fn_p (fn) == 0)
8932 continue;
8934 else if (copy_fn_p (fn) <= 0)
8935 continue;
8937 maybe_instantiate_noexcept (fn);
8938 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8939 return false;
8942 return true;
8945 /* Actually evaluates the trait. */
8947 static bool
8948 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8950 enum tree_code type_code1;
8951 tree t;
8953 type_code1 = TREE_CODE (type1);
8955 switch (kind)
8957 case CPTK_HAS_NOTHROW_ASSIGN:
8958 type1 = strip_array_types (type1);
8959 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8960 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8961 || (CLASS_TYPE_P (type1)
8962 && classtype_has_nothrow_assign_or_copy_p (type1,
8963 true))));
8965 case CPTK_HAS_TRIVIAL_ASSIGN:
8966 /* ??? The standard seems to be missing the "or array of such a class
8967 type" wording for this trait. */
8968 type1 = strip_array_types (type1);
8969 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8970 && (trivial_type_p (type1)
8971 || (CLASS_TYPE_P (type1)
8972 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8974 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8975 type1 = strip_array_types (type1);
8976 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8977 || (CLASS_TYPE_P (type1)
8978 && (t = locate_ctor (type1))
8979 && (maybe_instantiate_noexcept (t),
8980 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8982 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8983 type1 = strip_array_types (type1);
8984 return (trivial_type_p (type1)
8985 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8987 case CPTK_HAS_NOTHROW_COPY:
8988 type1 = strip_array_types (type1);
8989 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8990 || (CLASS_TYPE_P (type1)
8991 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8993 case CPTK_HAS_TRIVIAL_COPY:
8994 /* ??? The standard seems to be missing the "or array of such a class
8995 type" wording for this trait. */
8996 type1 = strip_array_types (type1);
8997 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8998 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9000 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9001 type1 = strip_array_types (type1);
9002 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9003 || (CLASS_TYPE_P (type1)
9004 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9006 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9007 return type_has_virtual_destructor (type1);
9009 case CPTK_IS_ABSTRACT:
9010 return (ABSTRACT_CLASS_TYPE_P (type1));
9012 case CPTK_IS_BASE_OF:
9013 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9014 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9015 || DERIVED_FROM_P (type1, type2)));
9017 case CPTK_IS_CLASS:
9018 return (NON_UNION_CLASS_TYPE_P (type1));
9020 case CPTK_IS_EMPTY:
9021 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
9023 case CPTK_IS_ENUM:
9024 return (type_code1 == ENUMERAL_TYPE);
9026 case CPTK_IS_FINAL:
9027 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
9029 case CPTK_IS_LITERAL_TYPE:
9030 return (literal_type_p (type1));
9032 case CPTK_IS_POD:
9033 return (pod_type_p (type1));
9035 case CPTK_IS_POLYMORPHIC:
9036 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
9038 case CPTK_IS_SAME_AS:
9039 return same_type_p (type1, type2);
9041 case CPTK_IS_STD_LAYOUT:
9042 return (std_layout_type_p (type1));
9044 case CPTK_IS_TRIVIAL:
9045 return (trivial_type_p (type1));
9047 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9048 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9050 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9051 return is_trivially_xible (INIT_EXPR, type1, type2);
9053 case CPTK_IS_TRIVIALLY_COPYABLE:
9054 return (trivially_copyable_p (type1));
9056 case CPTK_IS_UNION:
9057 return (type_code1 == UNION_TYPE);
9059 default:
9060 gcc_unreachable ();
9061 return false;
9065 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9066 void, or a complete type, returns true, otherwise false. */
9068 static bool
9069 check_trait_type (tree type)
9071 if (type == NULL_TREE)
9072 return true;
9074 if (TREE_CODE (type) == TREE_LIST)
9075 return (check_trait_type (TREE_VALUE (type))
9076 && check_trait_type (TREE_CHAIN (type)));
9078 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9079 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9080 return true;
9082 if (VOID_TYPE_P (type))
9083 return true;
9085 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9088 /* Process a trait expression. */
9090 tree
9091 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9093 if (type1 == error_mark_node
9094 || type2 == error_mark_node)
9095 return error_mark_node;
9097 if (processing_template_decl)
9099 tree trait_expr = make_node (TRAIT_EXPR);
9100 TREE_TYPE (trait_expr) = boolean_type_node;
9101 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9102 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9103 TRAIT_EXPR_KIND (trait_expr) = kind;
9104 return trait_expr;
9107 switch (kind)
9109 case CPTK_HAS_NOTHROW_ASSIGN:
9110 case CPTK_HAS_TRIVIAL_ASSIGN:
9111 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9112 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9113 case CPTK_HAS_NOTHROW_COPY:
9114 case CPTK_HAS_TRIVIAL_COPY:
9115 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9116 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9117 case CPTK_IS_ABSTRACT:
9118 case CPTK_IS_EMPTY:
9119 case CPTK_IS_FINAL:
9120 case CPTK_IS_LITERAL_TYPE:
9121 case CPTK_IS_POD:
9122 case CPTK_IS_POLYMORPHIC:
9123 case CPTK_IS_STD_LAYOUT:
9124 case CPTK_IS_TRIVIAL:
9125 case CPTK_IS_TRIVIALLY_COPYABLE:
9126 if (!check_trait_type (type1))
9127 return error_mark_node;
9128 break;
9130 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9131 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9132 if (!check_trait_type (type1)
9133 || !check_trait_type (type2))
9134 return error_mark_node;
9135 break;
9137 case CPTK_IS_BASE_OF:
9138 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9139 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9140 && !complete_type_or_else (type2, NULL_TREE))
9141 /* We already issued an error. */
9142 return error_mark_node;
9143 break;
9145 case CPTK_IS_CLASS:
9146 case CPTK_IS_ENUM:
9147 case CPTK_IS_UNION:
9148 case CPTK_IS_SAME_AS:
9149 break;
9151 default:
9152 gcc_unreachable ();
9155 return (trait_expr_value (kind, type1, type2)
9156 ? boolean_true_node : boolean_false_node);
9159 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9160 which is ignored for C++. */
9162 void
9163 set_float_const_decimal64 (void)
9167 void
9168 clear_float_const_decimal64 (void)
9172 bool
9173 float_const_decimal64_p (void)
9175 return 0;
9179 /* Return true if T designates the implied `this' parameter. */
9181 bool
9182 is_this_parameter (tree t)
9184 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9185 return false;
9186 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
9187 return true;
9190 /* Insert the deduced return type for an auto function. */
9192 void
9193 apply_deduced_return_type (tree fco, tree return_type)
9195 tree result;
9197 if (return_type == error_mark_node)
9198 return;
9200 if (LAMBDA_FUNCTION_P (fco))
9202 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
9203 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
9206 if (DECL_CONV_FN_P (fco))
9207 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
9209 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9211 result = DECL_RESULT (fco);
9212 if (result == NULL_TREE)
9213 return;
9214 if (TREE_TYPE (result) == return_type)
9215 return;
9217 /* We already have a DECL_RESULT from start_preparsed_function.
9218 Now we need to redo the work it and allocate_struct_function
9219 did to reflect the new type. */
9220 gcc_assert (current_function_decl == fco);
9221 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9222 TYPE_MAIN_VARIANT (return_type));
9223 DECL_ARTIFICIAL (result) = 1;
9224 DECL_IGNORED_P (result) = 1;
9225 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9226 result);
9228 DECL_RESULT (fco) = result;
9230 if (!processing_template_decl)
9232 if (!VOID_TYPE_P (TREE_TYPE (result)))
9233 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
9234 bool aggr = aggregate_value_p (result, fco);
9235 #ifdef PCC_STATIC_STRUCT_RETURN
9236 cfun->returns_pcc_struct = aggr;
9237 #endif
9238 cfun->returns_struct = aggr;
9243 /* DECL is a local variable or parameter from the surrounding scope of a
9244 lambda-expression. Returns the decltype for a use of the capture field
9245 for DECL even if it hasn't been captured yet. */
9247 static tree
9248 capture_decltype (tree decl)
9250 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9251 /* FIXME do lookup instead of list walk? */
9252 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9253 tree type;
9255 if (cap)
9256 type = TREE_TYPE (TREE_PURPOSE (cap));
9257 else
9258 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9260 case CPLD_NONE:
9261 error ("%qD is not captured", decl);
9262 return error_mark_node;
9264 case CPLD_COPY:
9265 type = TREE_TYPE (decl);
9266 if (TREE_CODE (type) == REFERENCE_TYPE
9267 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9268 type = TREE_TYPE (type);
9269 break;
9271 case CPLD_REFERENCE:
9272 type = TREE_TYPE (decl);
9273 if (TREE_CODE (type) != REFERENCE_TYPE)
9274 type = build_reference_type (TREE_TYPE (decl));
9275 break;
9277 default:
9278 gcc_unreachable ();
9281 if (TREE_CODE (type) != REFERENCE_TYPE)
9283 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9284 type = cp_build_qualified_type (type, (cp_type_quals (type)
9285 |TYPE_QUAL_CONST));
9286 type = build_reference_type (type);
9288 return type;
9291 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9292 this is a right unary fold. Otherwise it is a left unary fold. */
9294 static tree
9295 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9297 // Build a pack expansion (assuming expr has pack type).
9298 if (!uses_parameter_packs (expr))
9300 error_at (location_of (expr), "operand of fold expression has no "
9301 "unexpanded parameter packs");
9302 return error_mark_node;
9304 tree pack = make_pack_expansion (expr);
9306 // Build the fold expression.
9307 tree code = build_int_cstu (integer_type_node, abs (op));
9308 tree fold = build_min (dir, unknown_type_node, code, pack);
9309 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9310 return fold;
9313 tree
9314 finish_left_unary_fold_expr (tree expr, int op)
9316 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9319 tree
9320 finish_right_unary_fold_expr (tree expr, int op)
9322 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9325 /* Build a binary fold expression over EXPR1 and EXPR2. The
9326 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9327 has an unexpanded parameter pack). */
9329 tree
9330 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9332 pack = make_pack_expansion (pack);
9333 tree code = build_int_cstu (integer_type_node, abs (op));
9334 tree fold = build_min (dir, unknown_type_node, code, pack, init);
9335 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9336 return fold;
9339 tree
9340 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9342 // Determine which expr has an unexpanded parameter pack and
9343 // set the pack and initial term.
9344 bool pack1 = uses_parameter_packs (expr1);
9345 bool pack2 = uses_parameter_packs (expr2);
9346 if (pack1 && !pack2)
9347 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9348 else if (pack2 && !pack1)
9349 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9350 else
9352 if (pack1)
9353 error ("both arguments in binary fold have unexpanded parameter packs");
9354 else
9355 error ("no unexpanded parameter packs in binary fold");
9357 return error_mark_node;
9360 #include "gt-cp-semantics.h"