Merge from trunk
[official-gcc.git] / gcc / cp / semantics.c
blobee999f077488763a3681149e00fbb4ecbfe16739
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2015 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "stmt.h"
41 #include "varasm.h"
42 #include "stor-layout.h"
43 #include "stringpool.h"
44 #include "cp-tree.h"
45 #include "c-family/c-common.h"
46 #include "c-family/c-objc.h"
47 #include "tree-inline.h"
48 #include "intl.h"
49 #include "toplev.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "diagnostic.h"
53 #include "hash-map.h"
54 #include "is-a.h"
55 #include "plugin-api.h"
56 #include "hard-reg-set.h"
57 #include "input.h"
58 #include "function.h"
59 #include "ipa-ref.h"
60 #include "cgraph.h"
61 #include "tree-iterator.h"
62 #include "target.h"
63 #include "hash-table.h"
64 #include "gimplify.h"
65 #include "bitmap.h"
66 #include "omp-low.h"
67 #include "builtins.h"
68 #include "convert.h"
69 #include "gomp-constants.h"
71 /* There routines provide a modular interface to perform many parsing
72 operations. They may therefore be used during actual parsing, or
73 during template instantiation, which may be regarded as a
74 degenerate form of parsing. */
76 static tree maybe_convert_cond (tree);
77 static tree finalize_nrv_r (tree *, int *, void *);
78 static tree capture_decltype (tree);
81 /* Deferred Access Checking Overview
82 ---------------------------------
84 Most C++ expressions and declarations require access checking
85 to be performed during parsing. However, in several cases,
86 this has to be treated differently.
88 For member declarations, access checking has to be deferred
89 until more information about the declaration is known. For
90 example:
92 class A {
93 typedef int X;
94 public:
95 X f();
98 A::X A::f();
99 A::X g();
101 When we are parsing the function return type `A::X', we don't
102 really know if this is allowed until we parse the function name.
104 Furthermore, some contexts require that access checking is
105 never performed at all. These include class heads, and template
106 instantiations.
108 Typical use of access checking functions is described here:
110 1. When we enter a context that requires certain access checking
111 mode, the function `push_deferring_access_checks' is called with
112 DEFERRING argument specifying the desired mode. Access checking
113 may be performed immediately (dk_no_deferred), deferred
114 (dk_deferred), or not performed (dk_no_check).
116 2. When a declaration such as a type, or a variable, is encountered,
117 the function `perform_or_defer_access_check' is called. It
118 maintains a vector of all deferred checks.
120 3. The global `current_class_type' or `current_function_decl' is then
121 setup by the parser. `enforce_access' relies on these information
122 to check access.
124 4. Upon exiting the context mentioned in step 1,
125 `perform_deferred_access_checks' is called to check all declaration
126 stored in the vector. `pop_deferring_access_checks' is then
127 called to restore the previous access checking mode.
129 In case of parsing error, we simply call `pop_deferring_access_checks'
130 without `perform_deferred_access_checks'. */
132 typedef struct GTY(()) deferred_access {
133 /* A vector representing name-lookups for which we have deferred
134 checking access controls. We cannot check the accessibility of
135 names used in a decl-specifier-seq until we know what is being
136 declared because code like:
138 class A {
139 class B {};
140 B* f();
143 A::B* A::f() { return 0; }
145 is valid, even though `A::B' is not generally accessible. */
146 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
148 /* The current mode of access checks. */
149 enum deferring_kind deferring_access_checks_kind;
151 } deferred_access;
153 /* Data for deferred access checking. */
154 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
155 static GTY(()) unsigned deferred_access_no_check;
157 /* Save the current deferred access states and start deferred
158 access checking iff DEFER_P is true. */
160 void
161 push_deferring_access_checks (deferring_kind deferring)
163 /* For context like template instantiation, access checking
164 disabling applies to all nested context. */
165 if (deferred_access_no_check || deferring == dk_no_check)
166 deferred_access_no_check++;
167 else
169 deferred_access e = {NULL, deferring};
170 vec_safe_push (deferred_access_stack, e);
174 /* Save the current deferred access states and start deferred access
175 checking, continuing the set of deferred checks in CHECKS. */
177 void
178 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
180 push_deferring_access_checks (dk_deferred);
181 if (!deferred_access_no_check)
182 deferred_access_stack->last().deferred_access_checks = checks;
185 /* Resume deferring access checks again after we stopped doing
186 this previously. */
188 void
189 resume_deferring_access_checks (void)
191 if (!deferred_access_no_check)
192 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
195 /* Stop deferring access checks. */
197 void
198 stop_deferring_access_checks (void)
200 if (!deferred_access_no_check)
201 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
204 /* Discard the current deferred access checks and restore the
205 previous states. */
207 void
208 pop_deferring_access_checks (void)
210 if (deferred_access_no_check)
211 deferred_access_no_check--;
212 else
213 deferred_access_stack->pop ();
216 /* Returns a TREE_LIST representing the deferred checks.
217 The TREE_PURPOSE of each node is the type through which the
218 access occurred; the TREE_VALUE is the declaration named.
221 vec<deferred_access_check, va_gc> *
222 get_deferred_access_checks (void)
224 if (deferred_access_no_check)
225 return NULL;
226 else
227 return (deferred_access_stack->last().deferred_access_checks);
230 /* Take current deferred checks and combine with the
231 previous states if we also defer checks previously.
232 Otherwise perform checks now. */
234 void
235 pop_to_parent_deferring_access_checks (void)
237 if (deferred_access_no_check)
238 deferred_access_no_check--;
239 else
241 vec<deferred_access_check, va_gc> *checks;
242 deferred_access *ptr;
244 checks = (deferred_access_stack->last ().deferred_access_checks);
246 deferred_access_stack->pop ();
247 ptr = &deferred_access_stack->last ();
248 if (ptr->deferring_access_checks_kind == dk_no_deferred)
250 /* Check access. */
251 perform_access_checks (checks, tf_warning_or_error);
253 else
255 /* Merge with parent. */
256 int i, j;
257 deferred_access_check *chk, *probe;
259 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
261 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
263 if (probe->binfo == chk->binfo &&
264 probe->decl == chk->decl &&
265 probe->diag_decl == chk->diag_decl)
266 goto found;
268 /* Insert into parent's checks. */
269 vec_safe_push (ptr->deferred_access_checks, *chk);
270 found:;
276 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
277 is the BINFO indicating the qualifying scope used to access the
278 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
279 or we aren't in SFINAE context or all the checks succeed return TRUE,
280 otherwise FALSE. */
282 bool
283 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
284 tsubst_flags_t complain)
286 int i;
287 deferred_access_check *chk;
288 location_t loc = input_location;
289 bool ok = true;
291 if (!checks)
292 return true;
294 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
296 input_location = chk->loc;
297 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
300 input_location = loc;
301 return (complain & tf_error) ? true : ok;
304 /* Perform the deferred access checks.
306 After performing the checks, we still have to keep the list
307 `deferred_access_stack->deferred_access_checks' since we may want
308 to check access for them again later in a different context.
309 For example:
311 class A {
312 typedef int X;
313 static X a;
315 A::X A::a, x; // No error for `A::a', error for `x'
317 We have to perform deferred access of `A::X', first with `A::a',
318 next with `x'. Return value like perform_access_checks above. */
320 bool
321 perform_deferred_access_checks (tsubst_flags_t complain)
323 return perform_access_checks (get_deferred_access_checks (), complain);
326 /* Defer checking the accessibility of DECL, when looked up in
327 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
328 Return value like perform_access_checks above. */
330 bool
331 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
332 tsubst_flags_t complain)
334 int i;
335 deferred_access *ptr;
336 deferred_access_check *chk;
339 /* Exit if we are in a context that no access checking is performed.
341 if (deferred_access_no_check)
342 return true;
344 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
346 ptr = &deferred_access_stack->last ();
348 /* If we are not supposed to defer access checks, just check now. */
349 if (ptr->deferring_access_checks_kind == dk_no_deferred)
351 bool ok = enforce_access (binfo, decl, diag_decl, complain);
352 return (complain & tf_error) ? true : ok;
355 /* See if we are already going to perform this check. */
356 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
358 if (chk->decl == decl && chk->binfo == binfo &&
359 chk->diag_decl == diag_decl)
361 return true;
364 /* If not, record the check. */
365 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
366 vec_safe_push (ptr->deferred_access_checks, new_access);
368 return true;
371 /* Returns nonzero if the current statement is a full expression,
372 i.e. temporaries created during that statement should be destroyed
373 at the end of the statement. */
376 stmts_are_full_exprs_p (void)
378 return current_stmt_tree ()->stmts_are_full_exprs_p;
381 /* T is a statement. Add it to the statement-tree. This is the C++
382 version. The C/ObjC frontends have a slightly different version of
383 this function. */
385 tree
386 add_stmt (tree t)
388 enum tree_code code = TREE_CODE (t);
390 if (EXPR_P (t) && code != LABEL_EXPR)
392 if (!EXPR_HAS_LOCATION (t))
393 SET_EXPR_LOCATION (t, input_location);
395 /* When we expand a statement-tree, we must know whether or not the
396 statements are full-expressions. We record that fact here. */
397 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
400 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
401 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
403 /* Add T to the statement-tree. Non-side-effect statements need to be
404 recorded during statement expressions. */
405 gcc_checking_assert (!stmt_list_stack->is_empty ());
406 append_to_statement_list_force (t, &cur_stmt_list);
408 return t;
411 /* Returns the stmt_tree to which statements are currently being added. */
413 stmt_tree
414 current_stmt_tree (void)
416 return (cfun
417 ? &cfun->language->base.x_stmt_tree
418 : &scope_chain->x_stmt_tree);
421 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
423 static tree
424 maybe_cleanup_point_expr (tree expr)
426 if (!processing_template_decl && stmts_are_full_exprs_p ())
427 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
428 return expr;
431 /* Like maybe_cleanup_point_expr except have the type of the new expression be
432 void so we don't need to create a temporary variable to hold the inner
433 expression. The reason why we do this is because the original type might be
434 an aggregate and we cannot create a temporary variable for that type. */
436 tree
437 maybe_cleanup_point_expr_void (tree expr)
439 if (!processing_template_decl && stmts_are_full_exprs_p ())
440 expr = fold_build_cleanup_point_expr (void_type_node, expr);
441 return expr;
446 /* Create a declaration statement for the declaration given by the DECL. */
448 void
449 add_decl_expr (tree decl)
451 tree r = build_stmt (input_location, DECL_EXPR, decl);
452 if (DECL_INITIAL (decl)
453 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
454 r = maybe_cleanup_point_expr_void (r);
455 add_stmt (r);
458 /* Finish a scope. */
460 tree
461 do_poplevel (tree stmt_list)
463 tree block = NULL;
465 if (stmts_are_full_exprs_p ())
466 block = poplevel (kept_level_p (), 1, 0);
468 stmt_list = pop_stmt_list (stmt_list);
470 if (!processing_template_decl)
472 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
473 /* ??? See c_end_compound_stmt re statement expressions. */
476 return stmt_list;
479 /* Begin a new scope. */
481 static tree
482 do_pushlevel (scope_kind sk)
484 tree ret = push_stmt_list ();
485 if (stmts_are_full_exprs_p ())
486 begin_scope (sk, NULL);
487 return ret;
490 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
491 when the current scope is exited. EH_ONLY is true when this is not
492 meant to apply to normal control flow transfer. */
494 void
495 push_cleanup (tree decl, tree cleanup, bool eh_only)
497 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
498 CLEANUP_EH_ONLY (stmt) = eh_only;
499 add_stmt (stmt);
500 CLEANUP_BODY (stmt) = push_stmt_list ();
503 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
504 the current loops, represented by 'NULL_TREE' if we've seen a possible
505 exit, and 'error_mark_node' if not. This is currently used only to
506 suppress the warning about a function with no return statements, and
507 therefore we don't bother noting returns as possible exits. We also
508 don't bother with gotos. */
510 static void
511 begin_maybe_infinite_loop (tree cond)
513 /* Only track this while parsing a function, not during instantiation. */
514 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
515 && !processing_template_decl))
516 return;
517 bool maybe_infinite = true;
518 if (cond)
520 cond = fold_non_dependent_expr (cond);
521 maybe_infinite = integer_nonzerop (cond);
523 vec_safe_push (cp_function_chain->infinite_loops,
524 maybe_infinite ? error_mark_node : NULL_TREE);
528 /* A break is a possible exit for the current loop. */
530 void
531 break_maybe_infinite_loop (void)
533 if (!cfun)
534 return;
535 cp_function_chain->infinite_loops->last() = NULL_TREE;
538 /* If we reach the end of the loop without seeing a possible exit, we have
539 an infinite loop. */
541 static void
542 end_maybe_infinite_loop (tree cond)
544 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
545 && !processing_template_decl))
546 return;
547 tree current = cp_function_chain->infinite_loops->pop();
548 if (current != NULL_TREE)
550 cond = fold_non_dependent_expr (cond);
551 if (integer_nonzerop (cond))
552 current_function_infinite_loop = 1;
557 /* Begin a conditional that might contain a declaration. When generating
558 normal code, we want the declaration to appear before the statement
559 containing the conditional. When generating template code, we want the
560 conditional to be rendered as the raw DECL_EXPR. */
562 static void
563 begin_cond (tree *cond_p)
565 if (processing_template_decl)
566 *cond_p = push_stmt_list ();
569 /* Finish such a conditional. */
571 static void
572 finish_cond (tree *cond_p, tree expr)
574 if (processing_template_decl)
576 tree cond = pop_stmt_list (*cond_p);
578 if (expr == NULL_TREE)
579 /* Empty condition in 'for'. */
580 gcc_assert (empty_expr_stmt_p (cond));
581 else if (check_for_bare_parameter_packs (expr))
582 expr = error_mark_node;
583 else if (!empty_expr_stmt_p (cond))
584 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
586 *cond_p = expr;
589 /* If *COND_P specifies a conditional with a declaration, transform the
590 loop such that
591 while (A x = 42) { }
592 for (; A x = 42;) { }
593 becomes
594 while (true) { A x = 42; if (!x) break; }
595 for (;;) { A x = 42; if (!x) break; }
596 The statement list for BODY will be empty if the conditional did
597 not declare anything. */
599 static void
600 simplify_loop_decl_cond (tree *cond_p, tree body)
602 tree cond, if_stmt;
604 if (!TREE_SIDE_EFFECTS (body))
605 return;
607 cond = *cond_p;
608 *cond_p = boolean_true_node;
610 if_stmt = begin_if_stmt ();
611 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
612 finish_if_stmt_cond (cond, if_stmt);
613 finish_break_stmt ();
614 finish_then_clause (if_stmt);
615 finish_if_stmt (if_stmt);
618 /* Finish a goto-statement. */
620 tree
621 finish_goto_stmt (tree destination)
623 if (identifier_p (destination))
624 destination = lookup_label (destination);
626 /* We warn about unused labels with -Wunused. That means we have to
627 mark the used labels as used. */
628 if (TREE_CODE (destination) == LABEL_DECL)
629 TREE_USED (destination) = 1;
630 else
632 if (check_no_cilk (destination,
633 "Cilk array notation cannot be used as a computed goto expression",
634 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
635 destination = error_mark_node;
636 destination = mark_rvalue_use (destination);
637 if (!processing_template_decl)
639 destination = cp_convert (ptr_type_node, destination,
640 tf_warning_or_error);
641 if (error_operand_p (destination))
642 return NULL_TREE;
643 destination
644 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
645 destination);
649 check_goto (destination);
651 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
654 /* COND is the condition-expression for an if, while, etc.,
655 statement. Convert it to a boolean value, if appropriate.
656 In addition, verify sequence points if -Wsequence-point is enabled. */
658 static tree
659 maybe_convert_cond (tree cond)
661 /* Empty conditions remain empty. */
662 if (!cond)
663 return NULL_TREE;
665 /* Wait until we instantiate templates before doing conversion. */
666 if (processing_template_decl)
667 return cond;
669 if (warn_sequence_point)
670 verify_sequence_points (cond);
672 /* Do the conversion. */
673 cond = convert_from_reference (cond);
675 if (TREE_CODE (cond) == MODIFY_EXPR
676 && !TREE_NO_WARNING (cond)
677 && warn_parentheses)
679 warning (OPT_Wparentheses,
680 "suggest parentheses around assignment used as truth value");
681 TREE_NO_WARNING (cond) = 1;
684 return condition_conversion (cond);
687 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
689 tree
690 finish_expr_stmt (tree expr)
692 tree r = NULL_TREE;
694 if (expr != NULL_TREE)
696 if (!processing_template_decl)
698 if (warn_sequence_point)
699 verify_sequence_points (expr);
700 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
702 else if (!type_dependent_expression_p (expr))
703 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
704 tf_warning_or_error);
706 if (check_for_bare_parameter_packs (expr))
707 expr = error_mark_node;
709 /* Simplification of inner statement expressions, compound exprs,
710 etc can result in us already having an EXPR_STMT. */
711 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
713 if (TREE_CODE (expr) != EXPR_STMT)
714 expr = build_stmt (input_location, EXPR_STMT, expr);
715 expr = maybe_cleanup_point_expr_void (expr);
718 r = add_stmt (expr);
721 return r;
725 /* Begin an if-statement. Returns a newly created IF_STMT if
726 appropriate. */
728 tree
729 begin_if_stmt (void)
731 tree r, scope;
732 scope = do_pushlevel (sk_cond);
733 r = build_stmt (input_location, IF_STMT, NULL_TREE,
734 NULL_TREE, NULL_TREE, scope);
735 begin_cond (&IF_COND (r));
736 return r;
739 /* Process the COND of an if-statement, which may be given by
740 IF_STMT. */
742 void
743 finish_if_stmt_cond (tree cond, tree if_stmt)
745 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
746 add_stmt (if_stmt);
747 THEN_CLAUSE (if_stmt) = push_stmt_list ();
750 /* Finish the then-clause of an if-statement, which may be given by
751 IF_STMT. */
753 tree
754 finish_then_clause (tree if_stmt)
756 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757 return if_stmt;
760 /* Begin the else-clause of an if-statement. */
762 void
763 begin_else_clause (tree if_stmt)
765 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
768 /* Finish the else-clause of an if-statement, which may be given by
769 IF_STMT. */
771 void
772 finish_else_clause (tree if_stmt)
774 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
777 /* Finish an if-statement. */
779 void
780 finish_if_stmt (tree if_stmt)
782 tree scope = IF_SCOPE (if_stmt);
783 IF_SCOPE (if_stmt) = NULL;
784 add_stmt (do_poplevel (scope));
787 /* Begin a while-statement. Returns a newly created WHILE_STMT if
788 appropriate. */
790 tree
791 begin_while_stmt (void)
793 tree r;
794 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795 add_stmt (r);
796 WHILE_BODY (r) = do_pushlevel (sk_block);
797 begin_cond (&WHILE_COND (r));
798 return r;
801 /* Process the COND of a while-statement, which may be given by
802 WHILE_STMT. */
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
807 if (check_no_cilk (cond,
808 "Cilk array notation cannot be used as a condition for while statement",
809 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
810 cond = error_mark_node;
811 cond = maybe_convert_cond (cond);
812 finish_cond (&WHILE_COND (while_stmt), cond);
813 begin_maybe_infinite_loop (cond);
814 if (ivdep && cond != error_mark_node)
815 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
816 TREE_TYPE (WHILE_COND (while_stmt)),
817 WHILE_COND (while_stmt),
818 build_int_cst (integer_type_node,
819 annot_expr_ivdep_kind));
820 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
823 /* Finish a while-statement, which may be given by WHILE_STMT. */
825 void
826 finish_while_stmt (tree while_stmt)
828 end_maybe_infinite_loop (boolean_true_node);
829 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
832 /* Begin a do-statement. Returns a newly created DO_STMT if
833 appropriate. */
835 tree
836 begin_do_stmt (void)
838 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
839 begin_maybe_infinite_loop (boolean_true_node);
840 add_stmt (r);
841 DO_BODY (r) = push_stmt_list ();
842 return r;
845 /* Finish the body of a do-statement, which may be given by DO_STMT. */
847 void
848 finish_do_body (tree do_stmt)
850 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
852 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
853 body = STATEMENT_LIST_TAIL (body)->stmt;
855 if (IS_EMPTY_STMT (body))
856 warning (OPT_Wempty_body,
857 "suggest explicit braces around empty body in %<do%> statement");
860 /* Finish a do-statement, which may be given by DO_STMT, and whose
861 COND is as indicated. */
863 void
864 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
866 if (check_no_cilk (cond,
867 "Cilk array notation cannot be used as a condition for a do-while statement",
868 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
869 cond = error_mark_node;
870 cond = maybe_convert_cond (cond);
871 end_maybe_infinite_loop (cond);
872 if (ivdep && cond != error_mark_node)
873 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
874 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
875 DO_COND (do_stmt) = cond;
878 /* Finish a return-statement. The EXPRESSION returned, if any, is as
879 indicated. */
881 tree
882 finish_return_stmt (tree expr)
884 tree r;
885 bool no_warning;
887 expr = check_return_expr (expr, &no_warning);
889 if (error_operand_p (expr)
890 || (flag_openmp && !check_omp_return ()))
892 /* Suppress -Wreturn-type for this function. */
893 if (warn_return_type)
894 TREE_NO_WARNING (current_function_decl) = true;
895 return error_mark_node;
898 if (!processing_template_decl)
900 if (warn_sequence_point)
901 verify_sequence_points (expr);
903 if (DECL_DESTRUCTOR_P (current_function_decl)
904 || (DECL_CONSTRUCTOR_P (current_function_decl)
905 && targetm.cxx.cdtor_returns_this ()))
907 /* Similarly, all destructors must run destructors for
908 base-classes before returning. So, all returns in a
909 destructor get sent to the DTOR_LABEL; finish_function emits
910 code to return a value there. */
911 return finish_goto_stmt (cdtor_label);
915 r = build_stmt (input_location, RETURN_EXPR, expr);
916 TREE_NO_WARNING (r) |= no_warning;
917 r = maybe_cleanup_point_expr_void (r);
918 r = add_stmt (r);
920 return r;
923 /* Begin the scope of a for-statement or a range-for-statement.
924 Both the returned trees are to be used in a call to
925 begin_for_stmt or begin_range_for_stmt. */
927 tree
928 begin_for_scope (tree *init)
930 tree scope = NULL_TREE;
931 if (flag_new_for_scope > 0)
932 scope = do_pushlevel (sk_for);
934 if (processing_template_decl)
935 *init = push_stmt_list ();
936 else
937 *init = NULL_TREE;
939 return scope;
942 /* Begin a for-statement. Returns a new FOR_STMT.
943 SCOPE and INIT should be the return of begin_for_scope,
944 or both NULL_TREE */
946 tree
947 begin_for_stmt (tree scope, tree init)
949 tree r;
951 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
952 NULL_TREE, NULL_TREE, NULL_TREE);
954 if (scope == NULL_TREE)
956 gcc_assert (!init || !(flag_new_for_scope > 0));
957 if (!init)
958 scope = begin_for_scope (&init);
960 FOR_INIT_STMT (r) = init;
961 FOR_SCOPE (r) = scope;
963 return r;
966 /* Finish the for-init-statement of a for-statement, which may be
967 given by FOR_STMT. */
969 void
970 finish_for_init_stmt (tree for_stmt)
972 if (processing_template_decl)
973 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
974 add_stmt (for_stmt);
975 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
976 begin_cond (&FOR_COND (for_stmt));
979 /* Finish the COND of a for-statement, which may be given by
980 FOR_STMT. */
982 void
983 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
985 if (check_no_cilk (cond,
986 "Cilk array notation cannot be used in a condition for a for-loop",
987 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
988 cond = error_mark_node;
989 cond = maybe_convert_cond (cond);
990 finish_cond (&FOR_COND (for_stmt), cond);
991 begin_maybe_infinite_loop (cond);
992 if (ivdep && cond != error_mark_node)
993 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
994 TREE_TYPE (FOR_COND (for_stmt)),
995 FOR_COND (for_stmt),
996 build_int_cst (integer_type_node,
997 annot_expr_ivdep_kind));
998 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1001 /* Finish the increment-EXPRESSION in a for-statement, which may be
1002 given by FOR_STMT. */
1004 void
1005 finish_for_expr (tree expr, tree for_stmt)
1007 if (!expr)
1008 return;
1009 /* If EXPR is an overloaded function, issue an error; there is no
1010 context available to use to perform overload resolution. */
1011 if (type_unknown_p (expr))
1013 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1014 expr = error_mark_node;
1016 if (!processing_template_decl)
1018 if (warn_sequence_point)
1019 verify_sequence_points (expr);
1020 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1021 tf_warning_or_error);
1023 else if (!type_dependent_expression_p (expr))
1024 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1025 tf_warning_or_error);
1026 expr = maybe_cleanup_point_expr_void (expr);
1027 if (check_for_bare_parameter_packs (expr))
1028 expr = error_mark_node;
1029 FOR_EXPR (for_stmt) = expr;
1032 /* Finish the body of a for-statement, which may be given by
1033 FOR_STMT. The increment-EXPR for the loop must be
1034 provided.
1035 It can also finish RANGE_FOR_STMT. */
1037 void
1038 finish_for_stmt (tree for_stmt)
1040 end_maybe_infinite_loop (boolean_true_node);
1042 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1043 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1044 else
1045 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1047 /* Pop the scope for the body of the loop. */
1048 if (flag_new_for_scope > 0)
1050 tree scope;
1051 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1052 ? &RANGE_FOR_SCOPE (for_stmt)
1053 : &FOR_SCOPE (for_stmt));
1054 scope = *scope_ptr;
1055 *scope_ptr = NULL;
1056 add_stmt (do_poplevel (scope));
1060 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1061 SCOPE and INIT should be the return of begin_for_scope,
1062 or both NULL_TREE .
1063 To finish it call finish_for_stmt(). */
1065 tree
1066 begin_range_for_stmt (tree scope, tree init)
1068 tree r;
1070 begin_maybe_infinite_loop (boolean_false_node);
1072 r = build_stmt (input_location, RANGE_FOR_STMT,
1073 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1075 if (scope == NULL_TREE)
1077 gcc_assert (!init || !(flag_new_for_scope > 0));
1078 if (!init)
1079 scope = begin_for_scope (&init);
1082 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1083 pop it now. */
1084 if (init)
1085 pop_stmt_list (init);
1086 RANGE_FOR_SCOPE (r) = scope;
1088 return r;
1091 /* Finish the head of a range-based for statement, which may
1092 be given by RANGE_FOR_STMT. DECL must be the declaration
1093 and EXPR must be the loop expression. */
1095 void
1096 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1098 RANGE_FOR_DECL (range_for_stmt) = decl;
1099 RANGE_FOR_EXPR (range_for_stmt) = expr;
1100 add_stmt (range_for_stmt);
1101 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1104 /* Finish a break-statement. */
1106 tree
1107 finish_break_stmt (void)
1109 /* In switch statements break is sometimes stylistically used after
1110 a return statement. This can lead to spurious warnings about
1111 control reaching the end of a non-void function when it is
1112 inlined. Note that we are calling block_may_fallthru with
1113 language specific tree nodes; this works because
1114 block_may_fallthru returns true when given something it does not
1115 understand. */
1116 if (!block_may_fallthru (cur_stmt_list))
1117 return void_node;
1118 return add_stmt (build_stmt (input_location, BREAK_STMT));
1121 /* Finish a continue-statement. */
1123 tree
1124 finish_continue_stmt (void)
1126 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1129 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1130 appropriate. */
1132 tree
1133 begin_switch_stmt (void)
1135 tree r, scope;
1137 scope = do_pushlevel (sk_cond);
1138 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1140 begin_cond (&SWITCH_STMT_COND (r));
1142 return r;
1145 /* Finish the cond of a switch-statement. */
1147 void
1148 finish_switch_cond (tree cond, tree switch_stmt)
1150 tree orig_type = NULL;
1152 if (check_no_cilk (cond,
1153 "Cilk array notation cannot be used as a condition for switch statement",
1154 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1155 cond = error_mark_node;
1157 if (!processing_template_decl)
1159 /* Convert the condition to an integer or enumeration type. */
1160 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1161 if (cond == NULL_TREE)
1163 error ("switch quantity not an integer");
1164 cond = error_mark_node;
1166 /* We want unlowered type here to handle enum bit-fields. */
1167 orig_type = unlowered_expr_type (cond);
1168 if (cond != error_mark_node)
1170 /* Warn if the condition has boolean value. */
1171 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1172 warning_at (input_location, OPT_Wswitch_bool,
1173 "switch condition has type bool");
1175 /* [stmt.switch]
1177 Integral promotions are performed. */
1178 cond = perform_integral_promotions (cond);
1179 cond = maybe_cleanup_point_expr (cond);
1182 if (check_for_bare_parameter_packs (cond))
1183 cond = error_mark_node;
1184 else if (!processing_template_decl && warn_sequence_point)
1185 verify_sequence_points (cond);
1187 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1188 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1189 add_stmt (switch_stmt);
1190 push_switch (switch_stmt);
1191 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1194 /* Finish the body of a switch-statement, which may be given by
1195 SWITCH_STMT. The COND to switch on is indicated. */
1197 void
1198 finish_switch_stmt (tree switch_stmt)
1200 tree scope;
1202 SWITCH_STMT_BODY (switch_stmt) =
1203 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1204 pop_switch ();
1206 scope = SWITCH_STMT_SCOPE (switch_stmt);
1207 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1208 add_stmt (do_poplevel (scope));
1211 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1212 appropriate. */
1214 tree
1215 begin_try_block (void)
1217 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1218 add_stmt (r);
1219 TRY_STMTS (r) = push_stmt_list ();
1220 return r;
1223 /* Likewise, for a function-try-block. The block returned in
1224 *COMPOUND_STMT is an artificial outer scope, containing the
1225 function-try-block. */
1227 tree
1228 begin_function_try_block (tree *compound_stmt)
1230 tree r;
1231 /* This outer scope does not exist in the C++ standard, but we need
1232 a place to put __FUNCTION__ and similar variables. */
1233 *compound_stmt = begin_compound_stmt (0);
1234 r = begin_try_block ();
1235 FN_TRY_BLOCK_P (r) = 1;
1236 return r;
1239 /* Finish a try-block, which may be given by TRY_BLOCK. */
1241 void
1242 finish_try_block (tree try_block)
1244 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1245 TRY_HANDLERS (try_block) = push_stmt_list ();
1248 /* Finish the body of a cleanup try-block, which may be given by
1249 TRY_BLOCK. */
1251 void
1252 finish_cleanup_try_block (tree try_block)
1254 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1257 /* Finish an implicitly generated try-block, with a cleanup is given
1258 by CLEANUP. */
1260 void
1261 finish_cleanup (tree cleanup, tree try_block)
1263 TRY_HANDLERS (try_block) = cleanup;
1264 CLEANUP_P (try_block) = 1;
1267 /* Likewise, for a function-try-block. */
1269 void
1270 finish_function_try_block (tree try_block)
1272 finish_try_block (try_block);
1273 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1274 the try block, but moving it inside. */
1275 in_function_try_handler = 1;
1278 /* Finish a handler-sequence for a try-block, which may be given by
1279 TRY_BLOCK. */
1281 void
1282 finish_handler_sequence (tree try_block)
1284 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1285 check_handlers (TRY_HANDLERS (try_block));
1288 /* Finish the handler-seq for a function-try-block, given by
1289 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1290 begin_function_try_block. */
1292 void
1293 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1295 in_function_try_handler = 0;
1296 finish_handler_sequence (try_block);
1297 finish_compound_stmt (compound_stmt);
1300 /* Begin a handler. Returns a HANDLER if appropriate. */
1302 tree
1303 begin_handler (void)
1305 tree r;
1307 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1308 add_stmt (r);
1310 /* Create a binding level for the eh_info and the exception object
1311 cleanup. */
1312 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1314 return r;
1317 /* Finish the handler-parameters for a handler, which may be given by
1318 HANDLER. DECL is the declaration for the catch parameter, or NULL
1319 if this is a `catch (...)' clause. */
1321 void
1322 finish_handler_parms (tree decl, tree handler)
1324 tree type = NULL_TREE;
1325 if (processing_template_decl)
1327 if (decl)
1329 decl = pushdecl (decl);
1330 decl = push_template_decl (decl);
1331 HANDLER_PARMS (handler) = decl;
1332 type = TREE_TYPE (decl);
1335 else
1336 type = expand_start_catch_block (decl);
1337 HANDLER_TYPE (handler) = type;
1340 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1341 the return value from the matching call to finish_handler_parms. */
1343 void
1344 finish_handler (tree handler)
1346 if (!processing_template_decl)
1347 expand_end_catch_block ();
1348 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1351 /* Begin a compound statement. FLAGS contains some bits that control the
1352 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1353 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1354 block of a function. If BCS_TRY_BLOCK is set, this is the block
1355 created on behalf of a TRY statement. Returns a token to be passed to
1356 finish_compound_stmt. */
1358 tree
1359 begin_compound_stmt (unsigned int flags)
1361 tree r;
1363 if (flags & BCS_NO_SCOPE)
1365 r = push_stmt_list ();
1366 STATEMENT_LIST_NO_SCOPE (r) = 1;
1368 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1369 But, if it's a statement-expression with a scopeless block, there's
1370 nothing to keep, and we don't want to accidentally keep a block
1371 *inside* the scopeless block. */
1372 keep_next_level (false);
1374 else
1375 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1377 /* When processing a template, we need to remember where the braces were,
1378 so that we can set up identical scopes when instantiating the template
1379 later. BIND_EXPR is a handy candidate for this.
1380 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1381 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1382 processing templates. */
1383 if (processing_template_decl)
1385 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1386 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1387 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1388 TREE_SIDE_EFFECTS (r) = 1;
1391 return r;
1394 /* Finish a compound-statement, which is given by STMT. */
1396 void
1397 finish_compound_stmt (tree stmt)
1399 if (TREE_CODE (stmt) == BIND_EXPR)
1401 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1402 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1403 discard the BIND_EXPR so it can be merged with the containing
1404 STATEMENT_LIST. */
1405 if (TREE_CODE (body) == STATEMENT_LIST
1406 && STATEMENT_LIST_HEAD (body) == NULL
1407 && !BIND_EXPR_BODY_BLOCK (stmt)
1408 && !BIND_EXPR_TRY_BLOCK (stmt))
1409 stmt = body;
1410 else
1411 BIND_EXPR_BODY (stmt) = body;
1413 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1414 stmt = pop_stmt_list (stmt);
1415 else
1417 /* Destroy any ObjC "super" receivers that may have been
1418 created. */
1419 objc_clear_super_receiver ();
1421 stmt = do_poplevel (stmt);
1424 /* ??? See c_end_compound_stmt wrt statement expressions. */
1425 add_stmt (stmt);
1428 /* Finish an asm-statement, whose components are a STRING, some
1429 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1430 LABELS. Also note whether the asm-statement should be
1431 considered volatile. */
1433 tree
1434 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1435 tree input_operands, tree clobbers, tree labels)
1437 tree r;
1438 tree t;
1439 int ninputs = list_length (input_operands);
1440 int noutputs = list_length (output_operands);
1442 if (!processing_template_decl)
1444 const char *constraint;
1445 const char **oconstraints;
1446 bool allows_mem, allows_reg, is_inout;
1447 tree operand;
1448 int i;
1450 oconstraints = XALLOCAVEC (const char *, noutputs);
1452 string = resolve_asm_operand_names (string, output_operands,
1453 input_operands, labels);
1455 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1457 operand = TREE_VALUE (t);
1459 /* ??? Really, this should not be here. Users should be using a
1460 proper lvalue, dammit. But there's a long history of using
1461 casts in the output operands. In cases like longlong.h, this
1462 becomes a primitive form of typechecking -- if the cast can be
1463 removed, then the output operand had a type of the proper width;
1464 otherwise we'll get an error. Gross, but ... */
1465 STRIP_NOPS (operand);
1467 operand = mark_lvalue_use (operand);
1469 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1470 operand = error_mark_node;
1472 if (operand != error_mark_node
1473 && (TREE_READONLY (operand)
1474 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1475 /* Functions are not modifiable, even though they are
1476 lvalues. */
1477 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1478 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1479 /* If it's an aggregate and any field is const, then it is
1480 effectively const. */
1481 || (CLASS_TYPE_P (TREE_TYPE (operand))
1482 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1483 cxx_readonly_error (operand, lv_asm);
1485 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1486 oconstraints[i] = constraint;
1488 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1489 &allows_mem, &allows_reg, &is_inout))
1491 /* If the operand is going to end up in memory,
1492 mark it addressable. */
1493 if (!allows_reg && !cxx_mark_addressable (operand))
1494 operand = error_mark_node;
1496 else
1497 operand = error_mark_node;
1499 TREE_VALUE (t) = operand;
1502 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1504 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1505 bool constraint_parsed
1506 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1507 oconstraints, &allows_mem, &allows_reg);
1508 /* If the operand is going to end up in memory, don't call
1509 decay_conversion. */
1510 if (constraint_parsed && !allows_reg && allows_mem)
1511 operand = mark_lvalue_use (TREE_VALUE (t));
1512 else
1513 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1515 /* If the type of the operand hasn't been determined (e.g.,
1516 because it involves an overloaded function), then issue
1517 an error message. There's no context available to
1518 resolve the overloading. */
1519 if (TREE_TYPE (operand) == unknown_type_node)
1521 error ("type of asm operand %qE could not be determined",
1522 TREE_VALUE (t));
1523 operand = error_mark_node;
1526 if (constraint_parsed)
1528 /* If the operand is going to end up in memory,
1529 mark it addressable. */
1530 if (!allows_reg && allows_mem)
1532 /* Strip the nops as we allow this case. FIXME, this really
1533 should be rejected or made deprecated. */
1534 STRIP_NOPS (operand);
1535 if (!cxx_mark_addressable (operand))
1536 operand = error_mark_node;
1538 else if (!allows_reg && !allows_mem)
1540 /* If constraint allows neither register nor memory,
1541 try harder to get a constant. */
1542 tree constop = maybe_constant_value (operand);
1543 if (TREE_CONSTANT (constop))
1544 operand = constop;
1547 else
1548 operand = error_mark_node;
1550 TREE_VALUE (t) = operand;
1554 r = build_stmt (input_location, ASM_EXPR, string,
1555 output_operands, input_operands,
1556 clobbers, labels);
1557 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1558 r = maybe_cleanup_point_expr_void (r);
1559 return add_stmt (r);
1562 /* Finish a label with the indicated NAME. Returns the new label. */
1564 tree
1565 finish_label_stmt (tree name)
1567 tree decl = define_label (input_location, name);
1569 if (decl == error_mark_node)
1570 return error_mark_node;
1572 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1574 return decl;
1577 /* Finish a series of declarations for local labels. G++ allows users
1578 to declare "local" labels, i.e., labels with scope. This extension
1579 is useful when writing code involving statement-expressions. */
1581 void
1582 finish_label_decl (tree name)
1584 if (!at_function_scope_p ())
1586 error ("__label__ declarations are only allowed in function scopes");
1587 return;
1590 add_decl_expr (declare_local_label (name));
1593 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1595 void
1596 finish_decl_cleanup (tree decl, tree cleanup)
1598 push_cleanup (decl, cleanup, false);
1601 /* If the current scope exits with an exception, run CLEANUP. */
1603 void
1604 finish_eh_cleanup (tree cleanup)
1606 push_cleanup (NULL, cleanup, true);
1609 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1610 order they were written by the user. Each node is as for
1611 emit_mem_initializers. */
1613 void
1614 finish_mem_initializers (tree mem_inits)
1616 /* Reorder the MEM_INITS so that they are in the order they appeared
1617 in the source program. */
1618 mem_inits = nreverse (mem_inits);
1620 if (processing_template_decl)
1622 tree mem;
1624 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1626 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1627 check for bare parameter packs in the TREE_VALUE, because
1628 any parameter packs in the TREE_VALUE have already been
1629 bound as part of the TREE_PURPOSE. See
1630 make_pack_expansion for more information. */
1631 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1632 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1633 TREE_VALUE (mem) = error_mark_node;
1636 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1637 CTOR_INITIALIZER, mem_inits));
1639 else
1640 emit_mem_initializers (mem_inits);
1643 /* Obfuscate EXPR if it looks like an id-expression or member access so
1644 that the call to finish_decltype in do_auto_deduction will give the
1645 right result. */
1647 tree
1648 force_paren_expr (tree expr)
1650 /* This is only needed for decltype(auto) in C++14. */
1651 if (cxx_dialect < cxx14)
1652 return expr;
1654 /* If we're in unevaluated context, we can't be deducing a
1655 return/initializer type, so we don't need to mess with this. */
1656 if (cp_unevaluated_operand)
1657 return expr;
1659 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1660 && TREE_CODE (expr) != SCOPE_REF)
1661 return expr;
1663 if (TREE_CODE (expr) == COMPONENT_REF)
1664 REF_PARENTHESIZED_P (expr) = true;
1665 else if (type_dependent_expression_p (expr))
1666 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1667 else
1669 cp_lvalue_kind kind = lvalue_kind (expr);
1670 if ((kind & ~clk_class) != clk_none)
1672 tree type = unlowered_expr_type (expr);
1673 bool rval = !!(kind & clk_rvalueref);
1674 type = cp_build_reference_type (type, rval);
1675 /* This inhibits warnings in, eg, cxx_mark_addressable
1676 (c++/60955). */
1677 warning_sentinel s (extra_warnings);
1678 expr = build_static_cast (type, expr, tf_error);
1679 if (expr != error_mark_node)
1680 REF_PARENTHESIZED_P (expr) = true;
1684 return expr;
1687 /* Finish a parenthesized expression EXPR. */
1689 tree
1690 finish_parenthesized_expr (tree expr)
1692 if (EXPR_P (expr))
1693 /* This inhibits warnings in c_common_truthvalue_conversion. */
1694 TREE_NO_WARNING (expr) = 1;
1696 if (TREE_CODE (expr) == OFFSET_REF
1697 || TREE_CODE (expr) == SCOPE_REF)
1698 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1699 enclosed in parentheses. */
1700 PTRMEM_OK_P (expr) = 0;
1702 if (TREE_CODE (expr) == STRING_CST)
1703 PAREN_STRING_LITERAL_P (expr) = 1;
1705 expr = force_paren_expr (expr);
1707 return expr;
1710 /* Finish a reference to a non-static data member (DECL) that is not
1711 preceded by `.' or `->'. */
1713 tree
1714 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1716 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1718 if (!object)
1720 tree scope = qualifying_scope;
1721 if (scope == NULL_TREE)
1722 scope = context_for_name_lookup (decl);
1723 object = maybe_dummy_object (scope, NULL);
1726 object = maybe_resolve_dummy (object, true);
1727 if (object == error_mark_node)
1728 return error_mark_node;
1730 /* DR 613/850: Can use non-static data members without an associated
1731 object in sizeof/decltype/alignof. */
1732 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1733 && (!processing_template_decl || !current_class_ref))
1735 if (current_function_decl
1736 && DECL_STATIC_FUNCTION_P (current_function_decl))
1737 error ("invalid use of member %qD in static member function", decl);
1738 else
1739 error ("invalid use of non-static data member %qD", decl);
1740 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1742 return error_mark_node;
1745 if (current_class_ptr)
1746 TREE_USED (current_class_ptr) = 1;
1747 if (processing_template_decl && !qualifying_scope)
1749 tree type = TREE_TYPE (decl);
1751 if (TREE_CODE (type) == REFERENCE_TYPE)
1752 /* Quals on the object don't matter. */;
1753 else if (PACK_EXPANSION_P (type))
1754 /* Don't bother trying to represent this. */
1755 type = NULL_TREE;
1756 else
1758 /* Set the cv qualifiers. */
1759 int quals = cp_type_quals (TREE_TYPE (object));
1761 if (DECL_MUTABLE_P (decl))
1762 quals &= ~TYPE_QUAL_CONST;
1764 quals |= cp_type_quals (TREE_TYPE (decl));
1765 type = cp_build_qualified_type (type, quals);
1768 return (convert_from_reference
1769 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1771 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1772 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1773 for now. */
1774 else if (processing_template_decl)
1775 return build_qualified_name (TREE_TYPE (decl),
1776 qualifying_scope,
1777 decl,
1778 /*template_p=*/false);
1779 else
1781 tree access_type = TREE_TYPE (object);
1783 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1784 decl, tf_warning_or_error);
1786 /* If the data member was named `C::M', convert `*this' to `C'
1787 first. */
1788 if (qualifying_scope)
1790 tree binfo = NULL_TREE;
1791 object = build_scoped_ref (object, qualifying_scope,
1792 &binfo);
1795 return build_class_member_access_expr (object, decl,
1796 /*access_path=*/NULL_TREE,
1797 /*preserve_reference=*/false,
1798 tf_warning_or_error);
1802 /* If we are currently parsing a template and we encountered a typedef
1803 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1804 adds the typedef to a list tied to the current template.
1805 At template instantiation time, that list is walked and access check
1806 performed for each typedef.
1807 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1809 void
1810 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1811 tree context,
1812 location_t location)
1814 tree template_info = NULL;
1815 tree cs = current_scope ();
1817 if (!is_typedef_decl (typedef_decl)
1818 || !context
1819 || !CLASS_TYPE_P (context)
1820 || !cs)
1821 return;
1823 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1824 template_info = get_template_info (cs);
1826 if (template_info
1827 && TI_TEMPLATE (template_info)
1828 && !currently_open_class (context))
1829 append_type_to_template_for_access_check (cs, typedef_decl,
1830 context, location);
1833 /* DECL was the declaration to which a qualified-id resolved. Issue
1834 an error message if it is not accessible. If OBJECT_TYPE is
1835 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1836 type of `*x', or `x', respectively. If the DECL was named as
1837 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1839 void
1840 check_accessibility_of_qualified_id (tree decl,
1841 tree object_type,
1842 tree nested_name_specifier)
1844 tree scope;
1845 tree qualifying_type = NULL_TREE;
1847 /* If we are parsing a template declaration and if decl is a typedef,
1848 add it to a list tied to the template.
1849 At template instantiation time, that list will be walked and
1850 access check performed. */
1851 add_typedef_to_current_template_for_access_check (decl,
1852 nested_name_specifier
1853 ? nested_name_specifier
1854 : DECL_CONTEXT (decl),
1855 input_location);
1857 /* If we're not checking, return immediately. */
1858 if (deferred_access_no_check)
1859 return;
1861 /* Determine the SCOPE of DECL. */
1862 scope = context_for_name_lookup (decl);
1863 /* If the SCOPE is not a type, then DECL is not a member. */
1864 if (!TYPE_P (scope))
1865 return;
1866 /* Compute the scope through which DECL is being accessed. */
1867 if (object_type
1868 /* OBJECT_TYPE might not be a class type; consider:
1870 class A { typedef int I; };
1871 I *p;
1872 p->A::I::~I();
1874 In this case, we will have "A::I" as the DECL, but "I" as the
1875 OBJECT_TYPE. */
1876 && CLASS_TYPE_P (object_type)
1877 && DERIVED_FROM_P (scope, object_type))
1878 /* If we are processing a `->' or `.' expression, use the type of the
1879 left-hand side. */
1880 qualifying_type = object_type;
1881 else if (nested_name_specifier)
1883 /* If the reference is to a non-static member of the
1884 current class, treat it as if it were referenced through
1885 `this'. */
1886 tree ct;
1887 if (DECL_NONSTATIC_MEMBER_P (decl)
1888 && current_class_ptr
1889 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1890 qualifying_type = ct;
1891 /* Otherwise, use the type indicated by the
1892 nested-name-specifier. */
1893 else
1894 qualifying_type = nested_name_specifier;
1896 else
1897 /* Otherwise, the name must be from the current class or one of
1898 its bases. */
1899 qualifying_type = currently_open_derived_class (scope);
1901 if (qualifying_type
1902 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1903 or similar in a default argument value. */
1904 && CLASS_TYPE_P (qualifying_type)
1905 && !dependent_type_p (qualifying_type))
1906 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1907 decl, tf_warning_or_error);
1910 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1911 class named to the left of the "::" operator. DONE is true if this
1912 expression is a complete postfix-expression; it is false if this
1913 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1914 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1915 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1916 is true iff this qualified name appears as a template argument. */
1918 tree
1919 finish_qualified_id_expr (tree qualifying_class,
1920 tree expr,
1921 bool done,
1922 bool address_p,
1923 bool template_p,
1924 bool template_arg_p,
1925 tsubst_flags_t complain)
1927 gcc_assert (TYPE_P (qualifying_class));
1929 if (error_operand_p (expr))
1930 return error_mark_node;
1932 if ((DECL_P (expr) || BASELINK_P (expr))
1933 && !mark_used (expr, complain))
1934 return error_mark_node;
1936 if (template_p)
1937 check_template_keyword (expr);
1939 /* If EXPR occurs as the operand of '&', use special handling that
1940 permits a pointer-to-member. */
1941 if (address_p && done)
1943 if (TREE_CODE (expr) == SCOPE_REF)
1944 expr = TREE_OPERAND (expr, 1);
1945 expr = build_offset_ref (qualifying_class, expr,
1946 /*address_p=*/true, complain);
1947 return expr;
1950 /* No need to check access within an enum. */
1951 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1952 return expr;
1954 /* Within the scope of a class, turn references to non-static
1955 members into expression of the form "this->...". */
1956 if (template_arg_p)
1957 /* But, within a template argument, we do not want make the
1958 transformation, as there is no "this" pointer. */
1960 else if (TREE_CODE (expr) == FIELD_DECL)
1962 push_deferring_access_checks (dk_no_check);
1963 expr = finish_non_static_data_member (expr, NULL_TREE,
1964 qualifying_class);
1965 pop_deferring_access_checks ();
1967 else if (BASELINK_P (expr) && !processing_template_decl)
1969 /* See if any of the functions are non-static members. */
1970 /* If so, the expression may be relative to 'this'. */
1971 if (!shared_member_p (expr)
1972 && current_class_ptr
1973 && DERIVED_FROM_P (qualifying_class,
1974 current_nonlambda_class_type ()))
1975 expr = (build_class_member_access_expr
1976 (maybe_dummy_object (qualifying_class, NULL),
1977 expr,
1978 BASELINK_ACCESS_BINFO (expr),
1979 /*preserve_reference=*/false,
1980 complain));
1981 else if (done)
1982 /* The expression is a qualified name whose address is not
1983 being taken. */
1984 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1985 complain);
1987 else if (BASELINK_P (expr))
1989 else
1991 /* In a template, return a SCOPE_REF for most qualified-ids
1992 so that we can check access at instantiation time. But if
1993 we're looking at a member of the current instantiation, we
1994 know we have access and building up the SCOPE_REF confuses
1995 non-type template argument handling. */
1996 if (processing_template_decl
1997 && !currently_open_class (qualifying_class))
1998 expr = build_qualified_name (TREE_TYPE (expr),
1999 qualifying_class, expr,
2000 template_p);
2002 expr = convert_from_reference (expr);
2005 return expr;
2008 /* Begin a statement-expression. The value returned must be passed to
2009 finish_stmt_expr. */
2011 tree
2012 begin_stmt_expr (void)
2014 return push_stmt_list ();
2017 /* Process the final expression of a statement expression. EXPR can be
2018 NULL, if the final expression is empty. Return a STATEMENT_LIST
2019 containing all the statements in the statement-expression, or
2020 ERROR_MARK_NODE if there was an error. */
2022 tree
2023 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2025 if (error_operand_p (expr))
2027 /* The type of the statement-expression is the type of the last
2028 expression. */
2029 TREE_TYPE (stmt_expr) = error_mark_node;
2030 return error_mark_node;
2033 /* If the last statement does not have "void" type, then the value
2034 of the last statement is the value of the entire expression. */
2035 if (expr)
2037 tree type = TREE_TYPE (expr);
2039 if (processing_template_decl)
2041 expr = build_stmt (input_location, EXPR_STMT, expr);
2042 expr = add_stmt (expr);
2043 /* Mark the last statement so that we can recognize it as such at
2044 template-instantiation time. */
2045 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2047 else if (VOID_TYPE_P (type))
2049 /* Just treat this like an ordinary statement. */
2050 expr = finish_expr_stmt (expr);
2052 else
2054 /* It actually has a value we need to deal with. First, force it
2055 to be an rvalue so that we won't need to build up a copy
2056 constructor call later when we try to assign it to something. */
2057 expr = force_rvalue (expr, tf_warning_or_error);
2058 if (error_operand_p (expr))
2059 return error_mark_node;
2061 /* Update for array-to-pointer decay. */
2062 type = TREE_TYPE (expr);
2064 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2065 normal statement, but don't convert to void or actually add
2066 the EXPR_STMT. */
2067 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2068 expr = maybe_cleanup_point_expr (expr);
2069 add_stmt (expr);
2072 /* The type of the statement-expression is the type of the last
2073 expression. */
2074 TREE_TYPE (stmt_expr) = type;
2077 return stmt_expr;
2080 /* Finish a statement-expression. EXPR should be the value returned
2081 by the previous begin_stmt_expr. Returns an expression
2082 representing the statement-expression. */
2084 tree
2085 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2087 tree type;
2088 tree result;
2090 if (error_operand_p (stmt_expr))
2092 pop_stmt_list (stmt_expr);
2093 return error_mark_node;
2096 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2098 type = TREE_TYPE (stmt_expr);
2099 result = pop_stmt_list (stmt_expr);
2100 TREE_TYPE (result) = type;
2102 if (processing_template_decl)
2104 result = build_min (STMT_EXPR, type, result);
2105 TREE_SIDE_EFFECTS (result) = 1;
2106 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2108 else if (CLASS_TYPE_P (type))
2110 /* Wrap the statement-expression in a TARGET_EXPR so that the
2111 temporary object created by the final expression is destroyed at
2112 the end of the full-expression containing the
2113 statement-expression. */
2114 result = force_target_expr (type, result, tf_warning_or_error);
2117 return result;
2120 /* Returns the expression which provides the value of STMT_EXPR. */
2122 tree
2123 stmt_expr_value_expr (tree stmt_expr)
2125 tree t = STMT_EXPR_STMT (stmt_expr);
2127 if (TREE_CODE (t) == BIND_EXPR)
2128 t = BIND_EXPR_BODY (t);
2130 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2131 t = STATEMENT_LIST_TAIL (t)->stmt;
2133 if (TREE_CODE (t) == EXPR_STMT)
2134 t = EXPR_STMT_EXPR (t);
2136 return t;
2139 /* Return TRUE iff EXPR_STMT is an empty list of
2140 expression statements. */
2142 bool
2143 empty_expr_stmt_p (tree expr_stmt)
2145 tree body = NULL_TREE;
2147 if (expr_stmt == void_node)
2148 return true;
2150 if (expr_stmt)
2152 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2153 body = EXPR_STMT_EXPR (expr_stmt);
2154 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2155 body = expr_stmt;
2158 if (body)
2160 if (TREE_CODE (body) == STATEMENT_LIST)
2161 return tsi_end_p (tsi_start (body));
2162 else
2163 return empty_expr_stmt_p (body);
2165 return false;
2168 /* Perform Koenig lookup. FN is the postfix-expression representing
2169 the function (or functions) to call; ARGS are the arguments to the
2170 call. Returns the functions to be considered by overload resolution. */
2172 tree
2173 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2174 tsubst_flags_t complain)
2176 tree identifier = NULL_TREE;
2177 tree functions = NULL_TREE;
2178 tree tmpl_args = NULL_TREE;
2179 bool template_id = false;
2181 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2183 /* Use a separate flag to handle null args. */
2184 template_id = true;
2185 tmpl_args = TREE_OPERAND (fn, 1);
2186 fn = TREE_OPERAND (fn, 0);
2189 /* Find the name of the overloaded function. */
2190 if (identifier_p (fn))
2191 identifier = fn;
2192 else if (is_overloaded_fn (fn))
2194 functions = fn;
2195 identifier = DECL_NAME (get_first_fn (functions));
2197 else if (DECL_P (fn))
2199 functions = fn;
2200 identifier = DECL_NAME (fn);
2203 /* A call to a namespace-scope function using an unqualified name.
2205 Do Koenig lookup -- unless any of the arguments are
2206 type-dependent. */
2207 if (!any_type_dependent_arguments_p (args)
2208 && !any_dependent_template_arguments_p (tmpl_args))
2210 fn = lookup_arg_dependent (identifier, functions, args);
2211 if (!fn)
2213 /* The unqualified name could not be resolved. */
2214 if (complain)
2215 fn = unqualified_fn_lookup_error (identifier);
2216 else
2217 fn = identifier;
2221 if (fn && template_id)
2222 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2224 return fn;
2227 /* Generate an expression for `FN (ARGS)'. This may change the
2228 contents of ARGS.
2230 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2231 as a virtual call, even if FN is virtual. (This flag is set when
2232 encountering an expression where the function name is explicitly
2233 qualified. For example a call to `X::f' never generates a virtual
2234 call.)
2236 Returns code for the call. */
2238 tree
2239 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2240 bool koenig_p, tsubst_flags_t complain)
2242 tree result;
2243 tree orig_fn;
2244 vec<tree, va_gc> *orig_args = NULL;
2246 if (fn == error_mark_node)
2247 return error_mark_node;
2249 gcc_assert (!TYPE_P (fn));
2251 orig_fn = fn;
2253 if (processing_template_decl)
2255 /* If the call expression is dependent, build a CALL_EXPR node
2256 with no type; type_dependent_expression_p recognizes
2257 expressions with no type as being dependent. */
2258 if (type_dependent_expression_p (fn)
2259 || any_type_dependent_arguments_p (*args)
2260 /* For a non-static member function that doesn't have an
2261 explicit object argument, we need to specifically
2262 test the type dependency of the "this" pointer because it
2263 is not included in *ARGS even though it is considered to
2264 be part of the list of arguments. Note that this is
2265 related to CWG issues 515 and 1005. */
2266 || (TREE_CODE (fn) != COMPONENT_REF
2267 && non_static_member_function_p (fn)
2268 && current_class_ref
2269 && type_dependent_expression_p (current_class_ref)))
2271 result = build_nt_call_vec (fn, *args);
2272 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2273 KOENIG_LOOKUP_P (result) = koenig_p;
2274 if (cfun)
2278 tree fndecl = OVL_CURRENT (fn);
2279 if (TREE_CODE (fndecl) != FUNCTION_DECL
2280 || !TREE_THIS_VOLATILE (fndecl))
2281 break;
2282 fn = OVL_NEXT (fn);
2284 while (fn);
2285 if (!fn)
2286 current_function_returns_abnormally = 1;
2288 return result;
2290 orig_args = make_tree_vector_copy (*args);
2291 if (!BASELINK_P (fn)
2292 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2293 && TREE_TYPE (fn) != unknown_type_node)
2294 fn = build_non_dependent_expr (fn);
2295 make_args_non_dependent (*args);
2298 if (TREE_CODE (fn) == COMPONENT_REF)
2300 tree member = TREE_OPERAND (fn, 1);
2301 if (BASELINK_P (member))
2303 tree object = TREE_OPERAND (fn, 0);
2304 return build_new_method_call (object, member,
2305 args, NULL_TREE,
2306 (disallow_virtual
2307 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2308 : LOOKUP_NORMAL),
2309 /*fn_p=*/NULL,
2310 complain);
2314 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2315 if (TREE_CODE (fn) == ADDR_EXPR
2316 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2317 fn = TREE_OPERAND (fn, 0);
2319 if (is_overloaded_fn (fn))
2320 fn = baselink_for_fns (fn);
2322 result = NULL_TREE;
2323 if (BASELINK_P (fn))
2325 tree object;
2327 /* A call to a member function. From [over.call.func]:
2329 If the keyword this is in scope and refers to the class of
2330 that member function, or a derived class thereof, then the
2331 function call is transformed into a qualified function call
2332 using (*this) as the postfix-expression to the left of the
2333 . operator.... [Otherwise] a contrived object of type T
2334 becomes the implied object argument.
2336 In this situation:
2338 struct A { void f(); };
2339 struct B : public A {};
2340 struct C : public A { void g() { B::f(); }};
2342 "the class of that member function" refers to `A'. But 11.2
2343 [class.access.base] says that we need to convert 'this' to B* as
2344 part of the access, so we pass 'B' to maybe_dummy_object. */
2346 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2347 NULL);
2349 if (processing_template_decl)
2351 if (type_dependent_expression_p (object))
2353 tree ret = build_nt_call_vec (orig_fn, orig_args);
2354 release_tree_vector (orig_args);
2355 return ret;
2357 object = build_non_dependent_expr (object);
2360 result = build_new_method_call (object, fn, args, NULL_TREE,
2361 (disallow_virtual
2362 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2363 : LOOKUP_NORMAL),
2364 /*fn_p=*/NULL,
2365 complain);
2367 else if (is_overloaded_fn (fn))
2369 /* If the function is an overloaded builtin, resolve it. */
2370 if (TREE_CODE (fn) == FUNCTION_DECL
2371 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2372 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2373 result = resolve_overloaded_builtin (input_location, fn, *args);
2375 if (!result)
2377 if (warn_sizeof_pointer_memaccess
2378 && !vec_safe_is_empty (*args)
2379 && !processing_template_decl)
2381 location_t sizeof_arg_loc[3];
2382 tree sizeof_arg[3];
2383 unsigned int i;
2384 for (i = 0; i < 3; i++)
2386 tree t;
2388 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2389 sizeof_arg[i] = NULL_TREE;
2390 if (i >= (*args)->length ())
2391 continue;
2392 t = (**args)[i];
2393 if (TREE_CODE (t) != SIZEOF_EXPR)
2394 continue;
2395 if (SIZEOF_EXPR_TYPE_P (t))
2396 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2397 else
2398 sizeof_arg[i] = TREE_OPERAND (t, 0);
2399 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2401 sizeof_pointer_memaccess_warning
2402 (sizeof_arg_loc, fn, *args,
2403 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2406 /* A call to a namespace-scope function. */
2407 result = build_new_function_call (fn, args, koenig_p, complain);
2410 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2412 if (!vec_safe_is_empty (*args))
2413 error ("arguments to destructor are not allowed");
2414 /* Mark the pseudo-destructor call as having side-effects so
2415 that we do not issue warnings about its use. */
2416 result = build1 (NOP_EXPR,
2417 void_type_node,
2418 TREE_OPERAND (fn, 0));
2419 TREE_SIDE_EFFECTS (result) = 1;
2421 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2422 /* If the "function" is really an object of class type, it might
2423 have an overloaded `operator ()'. */
2424 result = build_op_call (fn, args, complain);
2426 if (!result)
2427 /* A call where the function is unknown. */
2428 result = cp_build_function_call_vec (fn, args, complain);
2430 if (processing_template_decl && result != error_mark_node)
2432 if (INDIRECT_REF_P (result))
2433 result = TREE_OPERAND (result, 0);
2434 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2435 SET_EXPR_LOCATION (result, input_location);
2436 KOENIG_LOOKUP_P (result) = koenig_p;
2437 release_tree_vector (orig_args);
2438 result = convert_from_reference (result);
2441 if (koenig_p)
2443 /* Free garbage OVERLOADs from arg-dependent lookup. */
2444 tree next = NULL_TREE;
2445 for (fn = orig_fn;
2446 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2447 fn = next)
2449 if (processing_template_decl)
2450 /* In a template, we'll re-use them at instantiation time. */
2451 OVL_ARG_DEPENDENT (fn) = false;
2452 else
2454 next = OVL_CHAIN (fn);
2455 if (flag_concepts)
2456 remove_constraints (OVL_FUNCTION (fn));
2457 ggc_free (fn);
2462 return result;
2465 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2466 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2467 POSTDECREMENT_EXPR.) */
2469 tree
2470 finish_increment_expr (tree expr, enum tree_code code)
2472 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2475 /* Finish a use of `this'. Returns an expression for `this'. */
2477 tree
2478 finish_this_expr (void)
2480 tree result = NULL_TREE;
2482 if (current_class_ptr)
2484 tree type = TREE_TYPE (current_class_ref);
2486 /* In a lambda expression, 'this' refers to the captured 'this'. */
2487 if (LAMBDA_TYPE_P (type))
2488 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2489 else
2490 result = current_class_ptr;
2493 if (result)
2494 /* The keyword 'this' is a prvalue expression. */
2495 return rvalue (result);
2497 tree fn = current_nonlambda_function ();
2498 if (fn && DECL_STATIC_FUNCTION_P (fn))
2499 error ("%<this%> is unavailable for static member functions");
2500 else if (fn)
2501 error ("invalid use of %<this%> in non-member function");
2502 else
2503 error ("invalid use of %<this%> at top level");
2504 return error_mark_node;
2507 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2508 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2509 the TYPE for the type given. If SCOPE is non-NULL, the expression
2510 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2512 tree
2513 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2514 location_t loc)
2516 if (object == error_mark_node || destructor == error_mark_node)
2517 return error_mark_node;
2519 gcc_assert (TYPE_P (destructor));
2521 if (!processing_template_decl)
2523 if (scope == error_mark_node)
2525 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2526 return error_mark_node;
2528 if (is_auto (destructor))
2529 destructor = TREE_TYPE (object);
2530 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2532 error_at (loc,
2533 "qualified type %qT does not match destructor name ~%qT",
2534 scope, destructor);
2535 return error_mark_node;
2539 /* [expr.pseudo] says both:
2541 The type designated by the pseudo-destructor-name shall be
2542 the same as the object type.
2544 and:
2546 The cv-unqualified versions of the object type and of the
2547 type designated by the pseudo-destructor-name shall be the
2548 same type.
2550 We implement the more generous second sentence, since that is
2551 what most other compilers do. */
2552 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2553 destructor))
2555 error_at (loc, "%qE is not of type %qT", object, destructor);
2556 return error_mark_node;
2560 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2561 scope, destructor);
2564 /* Finish an expression of the form CODE EXPR. */
2566 tree
2567 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2568 tsubst_flags_t complain)
2570 tree result = build_x_unary_op (loc, code, expr, complain);
2571 if ((complain & tf_warning)
2572 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2573 overflow_warning (input_location, result);
2575 return result;
2578 /* Finish a compound-literal expression. TYPE is the type to which
2579 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2581 tree
2582 finish_compound_literal (tree type, tree compound_literal,
2583 tsubst_flags_t complain)
2585 if (type == error_mark_node)
2586 return error_mark_node;
2588 if (TREE_CODE (type) == REFERENCE_TYPE)
2590 compound_literal
2591 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2592 complain);
2593 return cp_build_c_cast (type, compound_literal, complain);
2596 if (!TYPE_OBJ_P (type))
2598 if (complain & tf_error)
2599 error ("compound literal of non-object type %qT", type);
2600 return error_mark_node;
2603 if (processing_template_decl)
2605 TREE_TYPE (compound_literal) = type;
2606 /* Mark the expression as a compound literal. */
2607 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2608 return compound_literal;
2611 type = complete_type (type);
2613 if (TYPE_NON_AGGREGATE_CLASS (type))
2615 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2616 everywhere that deals with function arguments would be a pain, so
2617 just wrap it in a TREE_LIST. The parser set a flag so we know
2618 that it came from T{} rather than T({}). */
2619 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2620 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2621 return build_functional_cast (type, compound_literal, complain);
2624 if (TREE_CODE (type) == ARRAY_TYPE
2625 && check_array_initializer (NULL_TREE, type, compound_literal))
2626 return error_mark_node;
2627 compound_literal = reshape_init (type, compound_literal, complain);
2628 if (SCALAR_TYPE_P (type)
2629 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2630 && !check_narrowing (type, compound_literal, complain))
2631 return error_mark_node;
2632 if (TREE_CODE (type) == ARRAY_TYPE
2633 && TYPE_DOMAIN (type) == NULL_TREE)
2635 cp_complete_array_type_or_error (&type, compound_literal,
2636 false, complain);
2637 if (type == error_mark_node)
2638 return error_mark_node;
2640 compound_literal = digest_init (type, compound_literal, complain);
2641 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2642 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2643 /* Put static/constant array temporaries in static variables, but always
2644 represent class temporaries with TARGET_EXPR so we elide copies. */
2645 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2646 && TREE_CODE (type) == ARRAY_TYPE
2647 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2648 && initializer_constant_valid_p (compound_literal, type))
2650 tree decl = create_temporary_var (type);
2651 DECL_INITIAL (decl) = compound_literal;
2652 TREE_STATIC (decl) = 1;
2653 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2655 /* 5.19 says that a constant expression can include an
2656 lvalue-rvalue conversion applied to "a glvalue of literal type
2657 that refers to a non-volatile temporary object initialized
2658 with a constant expression". Rather than try to communicate
2659 that this VAR_DECL is a temporary, just mark it constexpr. */
2660 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2661 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2662 TREE_CONSTANT (decl) = true;
2664 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2665 decl = pushdecl_top_level (decl);
2666 DECL_NAME (decl) = make_anon_name ();
2667 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2668 /* Make sure the destructor is callable. */
2669 tree clean = cxx_maybe_build_cleanup (decl, complain);
2670 if (clean == error_mark_node)
2671 return error_mark_node;
2672 return decl;
2674 else
2675 return get_target_expr_sfinae (compound_literal, complain);
2678 /* Return the declaration for the function-name variable indicated by
2679 ID. */
2681 tree
2682 finish_fname (tree id)
2684 tree decl;
2686 decl = fname_decl (input_location, C_RID_CODE (id), id);
2687 if (processing_template_decl && current_function_decl
2688 && decl != error_mark_node)
2689 decl = DECL_NAME (decl);
2690 return decl;
2693 /* Finish a translation unit. */
2695 void
2696 finish_translation_unit (void)
2698 /* In case there were missing closebraces,
2699 get us back to the global binding level. */
2700 pop_everything ();
2701 while (current_namespace != global_namespace)
2702 pop_namespace ();
2704 /* Do file scope __FUNCTION__ et al. */
2705 finish_fname_decls ();
2708 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2709 Returns the parameter. */
2711 tree
2712 finish_template_type_parm (tree aggr, tree identifier)
2714 if (aggr != class_type_node)
2716 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2717 aggr = class_type_node;
2720 return build_tree_list (aggr, identifier);
2723 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2724 Returns the parameter. */
2726 tree
2727 finish_template_template_parm (tree aggr, tree identifier)
2729 tree decl = build_lang_decl_loc (input_location,
2730 TYPE_DECL, identifier, NULL_TREE);
2732 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2733 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2734 DECL_TEMPLATE_RESULT (tmpl) = decl;
2735 DECL_ARTIFICIAL (decl) = 1;
2737 // Associate the constraints with the underlying declaration,
2738 // not the template.
2739 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2740 tree constr = build_constraints (reqs, NULL_TREE);
2741 set_constraints (decl, constr);
2743 end_template_decl ();
2745 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2747 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2748 /*is_primary=*/true, /*is_partial=*/false,
2749 /*is_friend=*/0);
2751 return finish_template_type_parm (aggr, tmpl);
2754 /* ARGUMENT is the default-argument value for a template template
2755 parameter. If ARGUMENT is invalid, issue error messages and return
2756 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2758 tree
2759 check_template_template_default_arg (tree argument)
2761 if (TREE_CODE (argument) != TEMPLATE_DECL
2762 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2763 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2765 if (TREE_CODE (argument) == TYPE_DECL)
2766 error ("invalid use of type %qT as a default value for a template "
2767 "template-parameter", TREE_TYPE (argument));
2768 else
2769 error ("invalid default argument for a template template parameter");
2770 return error_mark_node;
2773 return argument;
2776 /* Begin a class definition, as indicated by T. */
2778 tree
2779 begin_class_definition (tree t)
2781 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2782 return error_mark_node;
2784 if (processing_template_parmlist)
2786 error ("definition of %q#T inside template parameter list", t);
2787 return error_mark_node;
2790 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2791 are passed the same as decimal scalar types. */
2792 if (TREE_CODE (t) == RECORD_TYPE
2793 && !processing_template_decl)
2795 tree ns = TYPE_CONTEXT (t);
2796 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2797 && DECL_CONTEXT (ns) == std_node
2798 && DECL_NAME (ns)
2799 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2801 const char *n = TYPE_NAME_STRING (t);
2802 if ((strcmp (n, "decimal32") == 0)
2803 || (strcmp (n, "decimal64") == 0)
2804 || (strcmp (n, "decimal128") == 0))
2805 TYPE_TRANSPARENT_AGGR (t) = 1;
2809 /* A non-implicit typename comes from code like:
2811 template <typename T> struct A {
2812 template <typename U> struct A<T>::B ...
2814 This is erroneous. */
2815 else if (TREE_CODE (t) == TYPENAME_TYPE)
2817 error ("invalid definition of qualified type %qT", t);
2818 t = error_mark_node;
2821 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2823 t = make_class_type (RECORD_TYPE);
2824 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2827 if (TYPE_BEING_DEFINED (t))
2829 t = make_class_type (TREE_CODE (t));
2830 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2832 maybe_process_partial_specialization (t);
2833 pushclass (t);
2834 TYPE_BEING_DEFINED (t) = 1;
2835 class_binding_level->defining_class_p = 1;
2837 if (flag_pack_struct)
2839 tree v;
2840 TYPE_PACKED (t) = 1;
2841 /* Even though the type is being defined for the first time
2842 here, there might have been a forward declaration, so there
2843 might be cv-qualified variants of T. */
2844 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2845 TYPE_PACKED (v) = 1;
2847 /* Reset the interface data, at the earliest possible
2848 moment, as it might have been set via a class foo;
2849 before. */
2850 if (! TYPE_ANONYMOUS_P (t))
2852 struct c_fileinfo *finfo = \
2853 get_fileinfo (LOCATION_FILE (input_location));
2854 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2855 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2856 (t, finfo->interface_unknown);
2858 reset_specialization();
2860 /* Make a declaration for this class in its own scope. */
2861 build_self_reference ();
2863 return t;
2866 /* Finish the member declaration given by DECL. */
2868 void
2869 finish_member_declaration (tree decl)
2871 if (decl == error_mark_node || decl == NULL_TREE)
2872 return;
2874 if (decl == void_type_node)
2875 /* The COMPONENT was a friend, not a member, and so there's
2876 nothing for us to do. */
2877 return;
2879 /* We should see only one DECL at a time. */
2880 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2882 /* Set up access control for DECL. */
2883 TREE_PRIVATE (decl)
2884 = (current_access_specifier == access_private_node);
2885 TREE_PROTECTED (decl)
2886 = (current_access_specifier == access_protected_node);
2887 if (TREE_CODE (decl) == TEMPLATE_DECL)
2889 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2890 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2893 /* Mark the DECL as a member of the current class, unless it's
2894 a member of an enumeration. */
2895 if (TREE_CODE (decl) != CONST_DECL)
2896 DECL_CONTEXT (decl) = current_class_type;
2898 /* Check for bare parameter packs in the member variable declaration. */
2899 if (TREE_CODE (decl) == FIELD_DECL)
2901 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2902 TREE_TYPE (decl) = error_mark_node;
2903 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2904 DECL_ATTRIBUTES (decl) = NULL_TREE;
2907 /* [dcl.link]
2909 A C language linkage is ignored for the names of class members
2910 and the member function type of class member functions. */
2911 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2912 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2914 /* Put functions on the TYPE_METHODS list and everything else on the
2915 TYPE_FIELDS list. Note that these are built up in reverse order.
2916 We reverse them (to obtain declaration order) in finish_struct. */
2917 if (DECL_DECLARES_FUNCTION_P (decl))
2919 /* We also need to add this function to the
2920 CLASSTYPE_METHOD_VEC. */
2921 if (add_method (current_class_type, decl, NULL_TREE))
2923 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2924 TYPE_METHODS (current_class_type) = decl;
2926 maybe_add_class_template_decl_list (current_class_type, decl,
2927 /*friend_p=*/0);
2930 /* Enter the DECL into the scope of the class, if the class
2931 isn't a closure (whose fields are supposed to be unnamed). */
2932 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2933 || pushdecl_class_level (decl))
2935 if (TREE_CODE (decl) == USING_DECL)
2937 /* For now, ignore class-scope USING_DECLS, so that
2938 debugging backends do not see them. */
2939 DECL_IGNORED_P (decl) = 1;
2942 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2943 go at the beginning. The reason is that lookup_field_1
2944 searches the list in order, and we want a field name to
2945 override a type name so that the "struct stat hack" will
2946 work. In particular:
2948 struct S { enum E { }; int E } s;
2949 s.E = 3;
2951 is valid. In addition, the FIELD_DECLs must be maintained in
2952 declaration order so that class layout works as expected.
2953 However, we don't need that order until class layout, so we
2954 save a little time by putting FIELD_DECLs on in reverse order
2955 here, and then reversing them in finish_struct_1. (We could
2956 also keep a pointer to the correct insertion points in the
2957 list.) */
2959 if (TREE_CODE (decl) == TYPE_DECL)
2960 TYPE_FIELDS (current_class_type)
2961 = chainon (TYPE_FIELDS (current_class_type), decl);
2962 else
2964 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2965 TYPE_FIELDS (current_class_type) = decl;
2968 maybe_add_class_template_decl_list (current_class_type, decl,
2969 /*friend_p=*/0);
2972 if (pch_file)
2973 note_decl_for_pch (decl);
2976 /* DECL has been declared while we are building a PCH file. Perform
2977 actions that we might normally undertake lazily, but which can be
2978 performed now so that they do not have to be performed in
2979 translation units which include the PCH file. */
2981 void
2982 note_decl_for_pch (tree decl)
2984 gcc_assert (pch_file);
2986 /* There's a good chance that we'll have to mangle names at some
2987 point, even if only for emission in debugging information. */
2988 if (VAR_OR_FUNCTION_DECL_P (decl)
2989 && !processing_template_decl)
2990 mangle_decl (decl);
2993 /* Finish processing a complete template declaration. The PARMS are
2994 the template parameters. */
2996 void
2997 finish_template_decl (tree parms)
2999 if (parms)
3000 end_template_decl ();
3001 else
3002 end_specialization ();
3005 // Returns the template type of the class scope being entered. If we're
3006 // entering a constrained class scope. TYPE is the class template
3007 // scope being entered and we may need to match the intended type with
3008 // a constrained specialization. For example:
3010 // template<Object T>
3011 // struct S { void f(); }; #1
3013 // template<Object T>
3014 // void S<T>::f() { } #2
3016 // We check, in #2, that S<T> refers precisely to the type declared by
3017 // #1 (i.e., that the constraints match). Note that the following should
3018 // be an error since there is no specialization of S<T> that is
3019 // unconstrained, but this is not diagnosed here.
3021 // template<typename T>
3022 // void S<T>::f() { }
3024 // We cannot diagnose this problem here since this function also matches
3025 // qualified template names that are not part of a definition. For example:
3027 // template<Integral T, Floating_point U>
3028 // typename pair<T, U>::first_type void f(T, U);
3030 // Here, it is unlikely that there is a partial specialization of
3031 // pair constrained for for Integral and Floating_point arguments.
3033 // The general rule is: if a constrained specialization with matching
3034 // constraints is found return that type. Alos note that if TYPE is not a
3035 // class-type (e.g. a typename type), then no fixup is needed.
3036 static tree
3037 fixup_template_type (tree type)
3039 // Find the template parameter list at the a depth appropriate to
3040 // the scope we're trying to enter.
3041 tree parms = current_template_parms;
3042 int depth = template_class_depth (type);
3043 for (int n = processing_template_decl; n > depth && parms; --n)
3044 parms = TREE_CHAIN (parms);
3045 if (!parms)
3046 return type;
3047 tree cur_constr = TEMPLATE_PARMS_CONSTRAINTS (parms);
3049 // Search for a specialization whose type and constraints match.
3050 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3051 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3052 while (specs)
3054 tree spec_constr = get_constraints (TREE_VALUE (specs));
3056 // If the type and constraints match a specialization, then we
3057 // are entering that type. Note that the type comparison is
3058 // structural since constrained partial specialiations may
3059 // have different canonical types for the same type patterns.
3060 if (comptypes (type, TREE_TYPE (specs), COMPARE_STRUCTURAL)
3061 && equivalent_constraints (cur_constr, spec_constr))
3062 return TREE_TYPE (specs);
3063 specs = TREE_CHAIN (specs);
3066 // If no specialization matches, then must return the type
3067 // previously found.
3068 return type;
3072 /* Finish processing a template-id (which names a type) of the form
3073 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3074 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3075 the scope of template-id indicated. */
3077 tree
3078 finish_template_type (tree name, tree args, int entering_scope)
3080 tree type;
3082 type = lookup_template_class (name, args,
3083 NULL_TREE, NULL_TREE, entering_scope,
3084 tf_warning_or_error | tf_user);
3086 // If entering a scope of a template, correct the lookup to
3087 // account for constraints.
3088 if (flag_concepts
3089 && entering_scope
3090 && CLASS_TYPE_P (type)
3091 && CLASSTYPE_IS_TEMPLATE (type))
3092 type = fixup_template_type (type);
3094 if (type == error_mark_node)
3095 return type;
3096 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3097 return TYPE_STUB_DECL (type);
3098 else
3099 return TYPE_NAME (type);
3102 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3103 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3104 BASE_CLASS, or NULL_TREE if an error occurred. The
3105 ACCESS_SPECIFIER is one of
3106 access_{default,public,protected_private}_node. For a virtual base
3107 we set TREE_TYPE. */
3109 tree
3110 finish_base_specifier (tree base, tree access, bool virtual_p)
3112 tree result;
3114 if (base == error_mark_node)
3116 error ("invalid base-class specification");
3117 result = NULL_TREE;
3119 else if (! MAYBE_CLASS_TYPE_P (base))
3121 error ("%qT is not a class type", base);
3122 result = NULL_TREE;
3124 else
3126 if (cp_type_quals (base) != 0)
3128 /* DR 484: Can a base-specifier name a cv-qualified
3129 class type? */
3130 base = TYPE_MAIN_VARIANT (base);
3132 result = build_tree_list (access, base);
3133 if (virtual_p)
3134 TREE_TYPE (result) = integer_type_node;
3137 return result;
3140 /* If FNS is a member function, a set of member functions, or a
3141 template-id referring to one or more member functions, return a
3142 BASELINK for FNS, incorporating the current access context.
3143 Otherwise, return FNS unchanged. */
3145 tree
3146 baselink_for_fns (tree fns)
3148 tree scope;
3149 tree cl;
3151 if (BASELINK_P (fns)
3152 || error_operand_p (fns))
3153 return fns;
3155 scope = ovl_scope (fns);
3156 if (!CLASS_TYPE_P (scope))
3157 return fns;
3159 cl = currently_open_derived_class (scope);
3160 if (!cl)
3161 cl = scope;
3162 cl = TYPE_BINFO (cl);
3163 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3166 /* Returns true iff DECL is a variable from a function outside
3167 the current one. */
3169 static bool
3170 outer_var_p (tree decl)
3172 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3173 && DECL_FUNCTION_SCOPE_P (decl)
3174 && (DECL_CONTEXT (decl) != current_function_decl
3175 || parsing_nsdmi ()));
3178 /* As above, but also checks that DECL is automatic. */
3180 bool
3181 outer_automatic_var_p (tree decl)
3183 return (outer_var_p (decl)
3184 && !TREE_STATIC (decl));
3187 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3188 rewrite it for lambda capture. */
3190 tree
3191 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3193 if (cp_unevaluated_operand)
3194 /* It's not a use (3.2) if we're in an unevaluated context. */
3195 return decl;
3197 tree context = DECL_CONTEXT (decl);
3198 tree containing_function = current_function_decl;
3199 tree lambda_stack = NULL_TREE;
3200 tree lambda_expr = NULL_TREE;
3201 tree initializer = convert_from_reference (decl);
3203 /* Mark it as used now even if the use is ill-formed. */
3204 mark_used (decl);
3206 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3207 support for an approach in which a reference to a local
3208 [constant] automatic variable in a nested class or lambda body
3209 would enter the expression as an rvalue, which would reduce
3210 the complexity of the problem"
3212 FIXME update for final resolution of core issue 696. */
3213 if (decl_maybe_constant_var_p (decl))
3215 if (processing_template_decl)
3216 /* In a template, the constant value may not be in a usable
3217 form, so wait until instantiation time. */
3218 return decl;
3219 else if (decl_constant_var_p (decl))
3220 return scalar_constant_value (decl);
3223 if (parsing_nsdmi ())
3224 containing_function = NULL_TREE;
3225 else
3226 /* If we are in a lambda function, we can move out until we hit
3227 1. the context,
3228 2. a non-lambda function, or
3229 3. a non-default capturing lambda function. */
3230 while (context != containing_function
3231 && LAMBDA_FUNCTION_P (containing_function))
3233 tree closure = DECL_CONTEXT (containing_function);
3234 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3236 if (TYPE_CLASS_SCOPE_P (closure))
3237 /* A lambda in an NSDMI (c++/64496). */
3238 break;
3240 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3241 == CPLD_NONE)
3242 break;
3244 lambda_stack = tree_cons (NULL_TREE,
3245 lambda_expr,
3246 lambda_stack);
3248 containing_function
3249 = decl_function_context (containing_function);
3252 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3253 && DECL_ANON_UNION_VAR_P (decl))
3255 if (complain & tf_error)
3256 error ("cannot capture member %qD of anonymous union", decl);
3257 return error_mark_node;
3259 if (context == containing_function)
3261 decl = add_default_capture (lambda_stack,
3262 /*id=*/DECL_NAME (decl),
3263 initializer);
3265 else if (lambda_expr)
3267 if (complain & tf_error)
3269 error ("%qD is not captured", decl);
3270 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3271 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3272 == CPLD_NONE)
3273 inform (location_of (closure),
3274 "the lambda has no capture-default");
3275 else if (TYPE_CLASS_SCOPE_P (closure))
3276 inform (0, "lambda in local class %q+T cannot "
3277 "capture variables from the enclosing context",
3278 TYPE_CONTEXT (closure));
3279 inform (input_location, "%q+#D declared here", decl);
3281 return error_mark_node;
3283 else
3285 if (complain & tf_error)
3286 error (VAR_P (decl)
3287 ? G_("use of local variable with automatic storage from containing function")
3288 : G_("use of parameter from containing function"));
3289 inform (input_location, "%q+#D declared here", decl);
3290 return error_mark_node;
3292 return decl;
3295 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3296 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3297 if non-NULL, is the type or namespace used to explicitly qualify
3298 ID_EXPRESSION. DECL is the entity to which that name has been
3299 resolved.
3301 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3302 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3303 be set to true if this expression isn't permitted in a
3304 constant-expression, but it is otherwise not set by this function.
3305 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3306 constant-expression, but a non-constant expression is also
3307 permissible.
3309 DONE is true if this expression is a complete postfix-expression;
3310 it is false if this expression is followed by '->', '[', '(', etc.
3311 ADDRESS_P is true iff this expression is the operand of '&'.
3312 TEMPLATE_P is true iff the qualified-id was of the form
3313 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3314 appears as a template argument.
3316 If an error occurs, and it is the kind of error that might cause
3317 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3318 is the caller's responsibility to issue the message. *ERROR_MSG
3319 will be a string with static storage duration, so the caller need
3320 not "free" it.
3322 Return an expression for the entity, after issuing appropriate
3323 diagnostics. This function is also responsible for transforming a
3324 reference to a non-static member into a COMPONENT_REF that makes
3325 the use of "this" explicit.
3327 Upon return, *IDK will be filled in appropriately. */
3328 tree
3329 finish_id_expression (tree id_expression,
3330 tree decl,
3331 tree scope,
3332 cp_id_kind *idk,
3333 bool integral_constant_expression_p,
3334 bool allow_non_integral_constant_expression_p,
3335 bool *non_integral_constant_expression_p,
3336 bool template_p,
3337 bool done,
3338 bool address_p,
3339 bool template_arg_p,
3340 const char **error_msg,
3341 location_t location)
3343 decl = strip_using_decl (decl);
3345 /* Initialize the output parameters. */
3346 *idk = CP_ID_KIND_NONE;
3347 *error_msg = NULL;
3349 if (id_expression == error_mark_node)
3350 return error_mark_node;
3351 /* If we have a template-id, then no further lookup is
3352 required. If the template-id was for a template-class, we
3353 will sometimes have a TYPE_DECL at this point. */
3354 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3355 || TREE_CODE (decl) == TYPE_DECL)
3357 /* Look up the name. */
3358 else
3360 if (decl == error_mark_node)
3362 /* Name lookup failed. */
3363 if (scope
3364 && (!TYPE_P (scope)
3365 || (!dependent_type_p (scope)
3366 && !(identifier_p (id_expression)
3367 && IDENTIFIER_TYPENAME_P (id_expression)
3368 && dependent_type_p (TREE_TYPE (id_expression))))))
3370 /* If the qualifying type is non-dependent (and the name
3371 does not name a conversion operator to a dependent
3372 type), issue an error. */
3373 qualified_name_lookup_error (scope, id_expression, decl, location);
3374 return error_mark_node;
3376 else if (!scope)
3378 /* It may be resolved via Koenig lookup. */
3379 *idk = CP_ID_KIND_UNQUALIFIED;
3380 return id_expression;
3382 else
3383 decl = id_expression;
3385 /* If DECL is a variable that would be out of scope under
3386 ANSI/ISO rules, but in scope in the ARM, name lookup
3387 will succeed. Issue a diagnostic here. */
3388 else
3389 decl = check_for_out_of_scope_variable (decl);
3391 /* Remember that the name was used in the definition of
3392 the current class so that we can check later to see if
3393 the meaning would have been different after the class
3394 was entirely defined. */
3395 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3396 maybe_note_name_used_in_class (id_expression, decl);
3398 /* Disallow uses of local variables from containing functions, except
3399 within lambda-expressions. */
3400 if (outer_automatic_var_p (decl))
3402 decl = process_outer_var_ref (decl, tf_warning_or_error);
3403 if (decl == error_mark_node)
3404 return error_mark_node;
3407 /* Also disallow uses of function parameters outside the function
3408 body, except inside an unevaluated context (i.e. decltype). */
3409 if (TREE_CODE (decl) == PARM_DECL
3410 && DECL_CONTEXT (decl) == NULL_TREE
3411 && !cp_unevaluated_operand)
3413 *error_msg = "use of parameter outside function body";
3414 return error_mark_node;
3418 /* If we didn't find anything, or what we found was a type,
3419 then this wasn't really an id-expression. */
3420 if (TREE_CODE (decl) == TEMPLATE_DECL
3421 && !DECL_FUNCTION_TEMPLATE_P (decl))
3423 *error_msg = "missing template arguments";
3424 return error_mark_node;
3426 else if (TREE_CODE (decl) == TYPE_DECL
3427 || TREE_CODE (decl) == NAMESPACE_DECL)
3429 *error_msg = "expected primary-expression";
3430 return error_mark_node;
3433 /* If the name resolved to a template parameter, there is no
3434 need to look it up again later. */
3435 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3436 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3438 tree r;
3440 *idk = CP_ID_KIND_NONE;
3441 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3442 decl = TEMPLATE_PARM_DECL (decl);
3443 r = convert_from_reference (DECL_INITIAL (decl));
3445 if (integral_constant_expression_p
3446 && !dependent_type_p (TREE_TYPE (decl))
3447 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3449 if (!allow_non_integral_constant_expression_p)
3450 error ("template parameter %qD of type %qT is not allowed in "
3451 "an integral constant expression because it is not of "
3452 "integral or enumeration type", decl, TREE_TYPE (decl));
3453 *non_integral_constant_expression_p = true;
3455 return r;
3457 else
3459 bool dependent_p;
3461 /* If the declaration was explicitly qualified indicate
3462 that. The semantics of `A::f(3)' are different than
3463 `f(3)' if `f' is virtual. */
3464 *idk = (scope
3465 ? CP_ID_KIND_QUALIFIED
3466 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3467 ? CP_ID_KIND_TEMPLATE_ID
3468 : CP_ID_KIND_UNQUALIFIED));
3471 /* [temp.dep.expr]
3473 An id-expression is type-dependent if it contains an
3474 identifier that was declared with a dependent type.
3476 The standard is not very specific about an id-expression that
3477 names a set of overloaded functions. What if some of them
3478 have dependent types and some of them do not? Presumably,
3479 such a name should be treated as a dependent name. */
3480 /* Assume the name is not dependent. */
3481 dependent_p = false;
3482 if (!processing_template_decl)
3483 /* No names are dependent outside a template. */
3485 else if (TREE_CODE (decl) == CONST_DECL)
3486 /* We don't want to treat enumerators as dependent. */
3488 /* A template-id where the name of the template was not resolved
3489 is definitely dependent. */
3490 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3491 && (identifier_p (TREE_OPERAND (decl, 0))))
3492 dependent_p = true;
3493 /* For anything except an overloaded function, just check its
3494 type. */
3495 else if (!is_overloaded_fn (decl))
3496 dependent_p
3497 = dependent_type_p (TREE_TYPE (decl));
3498 /* For a set of overloaded functions, check each of the
3499 functions. */
3500 else
3502 tree fns = decl;
3504 if (BASELINK_P (fns))
3505 fns = BASELINK_FUNCTIONS (fns);
3507 /* For a template-id, check to see if the template
3508 arguments are dependent. */
3509 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3511 tree args = TREE_OPERAND (fns, 1);
3512 dependent_p = any_dependent_template_arguments_p (args);
3513 /* The functions are those referred to by the
3514 template-id. */
3515 fns = TREE_OPERAND (fns, 0);
3518 /* If there are no dependent template arguments, go through
3519 the overloaded functions. */
3520 while (fns && !dependent_p)
3522 tree fn = OVL_CURRENT (fns);
3524 /* Member functions of dependent classes are
3525 dependent. */
3526 if (TREE_CODE (fn) == FUNCTION_DECL
3527 && type_dependent_expression_p (fn))
3528 dependent_p = true;
3529 else if (TREE_CODE (fn) == TEMPLATE_DECL
3530 && dependent_template_p (fn))
3531 dependent_p = true;
3533 fns = OVL_NEXT (fns);
3537 /* If the name was dependent on a template parameter, we will
3538 resolve the name at instantiation time. */
3539 if (dependent_p)
3541 /* Create a SCOPE_REF for qualified names, if the scope is
3542 dependent. */
3543 if (scope)
3545 if (TYPE_P (scope))
3547 if (address_p && done)
3548 decl = finish_qualified_id_expr (scope, decl,
3549 done, address_p,
3550 template_p,
3551 template_arg_p,
3552 tf_warning_or_error);
3553 else
3555 tree type = NULL_TREE;
3556 if (DECL_P (decl) && !dependent_scope_p (scope))
3557 type = TREE_TYPE (decl);
3558 decl = build_qualified_name (type,
3559 scope,
3560 id_expression,
3561 template_p);
3564 if (TREE_TYPE (decl))
3565 decl = convert_from_reference (decl);
3566 return decl;
3568 /* A TEMPLATE_ID already contains all the information we
3569 need. */
3570 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3571 return id_expression;
3572 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3573 /* If we found a variable, then name lookup during the
3574 instantiation will always resolve to the same VAR_DECL
3575 (or an instantiation thereof). */
3576 if (VAR_P (decl)
3577 || TREE_CODE (decl) == PARM_DECL)
3579 mark_used (decl);
3580 return convert_from_reference (decl);
3582 /* The same is true for FIELD_DECL, but we also need to
3583 make sure that the syntax is correct. */
3584 else if (TREE_CODE (decl) == FIELD_DECL)
3586 /* Since SCOPE is NULL here, this is an unqualified name.
3587 Access checking has been performed during name lookup
3588 already. Turn off checking to avoid duplicate errors. */
3589 push_deferring_access_checks (dk_no_check);
3590 decl = finish_non_static_data_member
3591 (decl, NULL_TREE,
3592 /*qualifying_scope=*/NULL_TREE);
3593 pop_deferring_access_checks ();
3594 return decl;
3596 return id_expression;
3599 if (TREE_CODE (decl) == NAMESPACE_DECL)
3601 error ("use of namespace %qD as expression", decl);
3602 return error_mark_node;
3604 else if (DECL_CLASS_TEMPLATE_P (decl))
3606 error ("use of class template %qT as expression", decl);
3607 return error_mark_node;
3609 else if (TREE_CODE (decl) == TREE_LIST)
3611 /* Ambiguous reference to base members. */
3612 error ("request for member %qD is ambiguous in "
3613 "multiple inheritance lattice", id_expression);
3614 print_candidates (decl);
3615 return error_mark_node;
3618 /* Mark variable-like entities as used. Functions are similarly
3619 marked either below or after overload resolution. */
3620 if ((VAR_P (decl)
3621 || TREE_CODE (decl) == PARM_DECL
3622 || TREE_CODE (decl) == CONST_DECL
3623 || TREE_CODE (decl) == RESULT_DECL)
3624 && !mark_used (decl))
3625 return error_mark_node;
3627 /* Only certain kinds of names are allowed in constant
3628 expression. Template parameters have already
3629 been handled above. */
3630 if (! error_operand_p (decl)
3631 && integral_constant_expression_p
3632 && ! decl_constant_var_p (decl)
3633 && TREE_CODE (decl) != CONST_DECL
3634 && ! builtin_valid_in_constant_expr_p (decl))
3636 if (!allow_non_integral_constant_expression_p)
3638 error ("%qD cannot appear in a constant-expression", decl);
3639 return error_mark_node;
3641 *non_integral_constant_expression_p = true;
3644 tree wrap;
3645 if (VAR_P (decl)
3646 && !cp_unevaluated_operand
3647 && !processing_template_decl
3648 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3649 && DECL_THREAD_LOCAL_P (decl)
3650 && (wrap = get_tls_wrapper_fn (decl)))
3652 /* Replace an evaluated use of the thread_local variable with
3653 a call to its wrapper. */
3654 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3656 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3657 && variable_template_p (TREE_OPERAND (decl, 0)))
3659 decl = finish_template_variable (decl);
3660 mark_used (decl);
3662 else if (scope)
3664 decl = (adjust_result_of_qualified_name_lookup
3665 (decl, scope, current_nonlambda_class_type()));
3667 if (TREE_CODE (decl) == FUNCTION_DECL)
3668 mark_used (decl);
3670 if (TYPE_P (scope))
3671 decl = finish_qualified_id_expr (scope,
3672 decl,
3673 done,
3674 address_p,
3675 template_p,
3676 template_arg_p,
3677 tf_warning_or_error);
3678 else
3679 decl = convert_from_reference (decl);
3681 else if (TREE_CODE (decl) == FIELD_DECL)
3683 /* Since SCOPE is NULL here, this is an unqualified name.
3684 Access checking has been performed during name lookup
3685 already. Turn off checking to avoid duplicate errors. */
3686 push_deferring_access_checks (dk_no_check);
3687 decl = finish_non_static_data_member (decl, NULL_TREE,
3688 /*qualifying_scope=*/NULL_TREE);
3689 pop_deferring_access_checks ();
3691 else if (is_overloaded_fn (decl))
3693 tree first_fn;
3695 first_fn = get_first_fn (decl);
3696 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3697 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3699 if (!really_overloaded_fn (decl)
3700 && !mark_used (first_fn))
3701 return error_mark_node;
3703 if (!template_arg_p
3704 && TREE_CODE (first_fn) == FUNCTION_DECL
3705 && DECL_FUNCTION_MEMBER_P (first_fn)
3706 && !shared_member_p (decl))
3708 /* A set of member functions. */
3709 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3710 return finish_class_member_access_expr (decl, id_expression,
3711 /*template_p=*/false,
3712 tf_warning_or_error);
3715 decl = baselink_for_fns (decl);
3717 else
3719 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3720 && DECL_CLASS_SCOPE_P (decl))
3722 tree context = context_for_name_lookup (decl);
3723 if (context != current_class_type)
3725 tree path = currently_open_derived_class (context);
3726 perform_or_defer_access_check (TYPE_BINFO (path),
3727 decl, decl,
3728 tf_warning_or_error);
3732 decl = convert_from_reference (decl);
3736 /* Handle references (c++/56130). */
3737 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3738 if (TREE_DEPRECATED (t))
3739 warn_deprecated_use (t, NULL_TREE);
3741 return decl;
3744 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3745 use as a type-specifier. */
3747 tree
3748 finish_typeof (tree expr)
3750 tree type;
3752 if (type_dependent_expression_p (expr))
3754 type = cxx_make_type (TYPEOF_TYPE);
3755 TYPEOF_TYPE_EXPR (type) = expr;
3756 SET_TYPE_STRUCTURAL_EQUALITY (type);
3758 return type;
3761 expr = mark_type_use (expr);
3763 type = unlowered_expr_type (expr);
3765 if (!type || type == unknown_type_node)
3767 error ("type of %qE is unknown", expr);
3768 return error_mark_node;
3771 return type;
3774 /* Implement the __underlying_type keyword: Return the underlying
3775 type of TYPE, suitable for use as a type-specifier. */
3777 tree
3778 finish_underlying_type (tree type)
3780 tree underlying_type;
3782 if (processing_template_decl)
3784 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3785 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3786 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3788 return underlying_type;
3791 complete_type (type);
3793 if (TREE_CODE (type) != ENUMERAL_TYPE)
3795 error ("%qT is not an enumeration type", type);
3796 return error_mark_node;
3799 underlying_type = ENUM_UNDERLYING_TYPE (type);
3801 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3802 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3803 See finish_enum_value_list for details. */
3804 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3805 underlying_type
3806 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3807 TYPE_UNSIGNED (underlying_type));
3809 return underlying_type;
3812 /* Implement the __direct_bases keyword: Return the direct base classes
3813 of type */
3815 tree
3816 calculate_direct_bases (tree type)
3818 vec<tree, va_gc> *vector = make_tree_vector();
3819 tree bases_vec = NULL_TREE;
3820 vec<tree, va_gc> *base_binfos;
3821 tree binfo;
3822 unsigned i;
3824 complete_type (type);
3826 if (!NON_UNION_CLASS_TYPE_P (type))
3827 return make_tree_vec (0);
3829 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3831 /* Virtual bases are initialized first */
3832 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3834 if (BINFO_VIRTUAL_P (binfo))
3836 vec_safe_push (vector, binfo);
3840 /* Now non-virtuals */
3841 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3843 if (!BINFO_VIRTUAL_P (binfo))
3845 vec_safe_push (vector, binfo);
3850 bases_vec = make_tree_vec (vector->length ());
3852 for (i = 0; i < vector->length (); ++i)
3854 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3856 return bases_vec;
3859 /* Implement the __bases keyword: Return the base classes
3860 of type */
3862 /* Find morally non-virtual base classes by walking binfo hierarchy */
3863 /* Virtual base classes are handled separately in finish_bases */
3865 static tree
3866 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3868 /* Don't walk bases of virtual bases */
3869 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3872 static tree
3873 dfs_calculate_bases_post (tree binfo, void *data_)
3875 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3876 if (!BINFO_VIRTUAL_P (binfo))
3878 vec_safe_push (*data, BINFO_TYPE (binfo));
3880 return NULL_TREE;
3883 /* Calculates the morally non-virtual base classes of a class */
3884 static vec<tree, va_gc> *
3885 calculate_bases_helper (tree type)
3887 vec<tree, va_gc> *vector = make_tree_vector();
3889 /* Now add non-virtual base classes in order of construction */
3890 dfs_walk_all (TYPE_BINFO (type),
3891 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3892 return vector;
3895 tree
3896 calculate_bases (tree type)
3898 vec<tree, va_gc> *vector = make_tree_vector();
3899 tree bases_vec = NULL_TREE;
3900 unsigned i;
3901 vec<tree, va_gc> *vbases;
3902 vec<tree, va_gc> *nonvbases;
3903 tree binfo;
3905 complete_type (type);
3907 if (!NON_UNION_CLASS_TYPE_P (type))
3908 return make_tree_vec (0);
3910 /* First go through virtual base classes */
3911 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3912 vec_safe_iterate (vbases, i, &binfo); i++)
3914 vec<tree, va_gc> *vbase_bases;
3915 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3916 vec_safe_splice (vector, vbase_bases);
3917 release_tree_vector (vbase_bases);
3920 /* Now for the non-virtual bases */
3921 nonvbases = calculate_bases_helper (type);
3922 vec_safe_splice (vector, nonvbases);
3923 release_tree_vector (nonvbases);
3925 /* Last element is entire class, so don't copy */
3926 bases_vec = make_tree_vec (vector->length () - 1);
3928 for (i = 0; i < vector->length () - 1; ++i)
3930 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3932 release_tree_vector (vector);
3933 return bases_vec;
3936 tree
3937 finish_bases (tree type, bool direct)
3939 tree bases = NULL_TREE;
3941 if (!processing_template_decl)
3943 /* Parameter packs can only be used in templates */
3944 error ("Parameter pack __bases only valid in template declaration");
3945 return error_mark_node;
3948 bases = cxx_make_type (BASES);
3949 BASES_TYPE (bases) = type;
3950 BASES_DIRECT (bases) = direct;
3951 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3953 return bases;
3956 /* Perform C++-specific checks for __builtin_offsetof before calling
3957 fold_offsetof. */
3959 tree
3960 finish_offsetof (tree expr, location_t loc)
3962 /* If we're processing a template, we can't finish the semantics yet.
3963 Otherwise we can fold the entire expression now. */
3964 if (processing_template_decl)
3966 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3967 SET_EXPR_LOCATION (expr, loc);
3968 return expr;
3971 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3973 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3974 TREE_OPERAND (expr, 2));
3975 return error_mark_node;
3977 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3978 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3979 || TREE_TYPE (expr) == unknown_type_node)
3981 if (INDIRECT_REF_P (expr))
3982 error ("second operand of %<offsetof%> is neither a single "
3983 "identifier nor a sequence of member accesses and "
3984 "array references");
3985 else
3987 if (TREE_CODE (expr) == COMPONENT_REF
3988 || TREE_CODE (expr) == COMPOUND_EXPR)
3989 expr = TREE_OPERAND (expr, 1);
3990 error ("cannot apply %<offsetof%> to member function %qD", expr);
3992 return error_mark_node;
3994 if (REFERENCE_REF_P (expr))
3995 expr = TREE_OPERAND (expr, 0);
3996 if (TREE_CODE (expr) == COMPONENT_REF)
3998 tree object = TREE_OPERAND (expr, 0);
3999 if (!complete_type_or_else (TREE_TYPE (object), object))
4000 return error_mark_node;
4001 if (warn_invalid_offsetof
4002 && CLASS_TYPE_P (TREE_TYPE (object))
4003 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
4004 && cp_unevaluated_operand == 0)
4005 pedwarn (loc, OPT_Winvalid_offsetof,
4006 "offsetof within non-standard-layout type %qT is undefined",
4007 TREE_TYPE (object));
4009 return fold_offsetof (expr);
4012 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4013 function is broken out from the above for the benefit of the tree-ssa
4014 project. */
4016 void
4017 simplify_aggr_init_expr (tree *tp)
4019 tree aggr_init_expr = *tp;
4021 /* Form an appropriate CALL_EXPR. */
4022 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4023 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4024 tree type = TREE_TYPE (slot);
4026 tree call_expr;
4027 enum style_t { ctor, arg, pcc } style;
4029 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4030 style = ctor;
4031 #ifdef PCC_STATIC_STRUCT_RETURN
4032 else if (1)
4033 style = pcc;
4034 #endif
4035 else
4037 gcc_assert (TREE_ADDRESSABLE (type));
4038 style = arg;
4041 call_expr = build_call_array_loc (input_location,
4042 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4044 aggr_init_expr_nargs (aggr_init_expr),
4045 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4046 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4047 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
4049 if (style == ctor)
4051 /* Replace the first argument to the ctor with the address of the
4052 slot. */
4053 cxx_mark_addressable (slot);
4054 CALL_EXPR_ARG (call_expr, 0) =
4055 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4057 else if (style == arg)
4059 /* Just mark it addressable here, and leave the rest to
4060 expand_call{,_inline}. */
4061 cxx_mark_addressable (slot);
4062 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4063 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4065 else if (style == pcc)
4067 /* If we're using the non-reentrant PCC calling convention, then we
4068 need to copy the returned value out of the static buffer into the
4069 SLOT. */
4070 push_deferring_access_checks (dk_no_check);
4071 call_expr = build_aggr_init (slot, call_expr,
4072 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4073 tf_warning_or_error);
4074 pop_deferring_access_checks ();
4075 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4078 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4080 tree init = build_zero_init (type, NULL_TREE,
4081 /*static_storage_p=*/false);
4082 init = build2 (INIT_EXPR, void_type_node, slot, init);
4083 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4084 init, call_expr);
4087 *tp = call_expr;
4090 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4092 void
4093 emit_associated_thunks (tree fn)
4095 /* When we use vcall offsets, we emit thunks with the virtual
4096 functions to which they thunk. The whole point of vcall offsets
4097 is so that you can know statically the entire set of thunks that
4098 will ever be needed for a given virtual function, thereby
4099 enabling you to output all the thunks with the function itself. */
4100 if (DECL_VIRTUAL_P (fn)
4101 /* Do not emit thunks for extern template instantiations. */
4102 && ! DECL_REALLY_EXTERN (fn))
4104 tree thunk;
4106 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4108 if (!THUNK_ALIAS (thunk))
4110 use_thunk (thunk, /*emit_p=*/1);
4111 if (DECL_RESULT_THUNK_P (thunk))
4113 tree probe;
4115 for (probe = DECL_THUNKS (thunk);
4116 probe; probe = DECL_CHAIN (probe))
4117 use_thunk (probe, /*emit_p=*/1);
4120 else
4121 gcc_assert (!DECL_THUNKS (thunk));
4126 /* Generate RTL for FN. */
4128 bool
4129 expand_or_defer_fn_1 (tree fn)
4131 /* When the parser calls us after finishing the body of a template
4132 function, we don't really want to expand the body. */
4133 if (processing_template_decl)
4135 /* Normally, collection only occurs in rest_of_compilation. So,
4136 if we don't collect here, we never collect junk generated
4137 during the processing of templates until we hit a
4138 non-template function. It's not safe to do this inside a
4139 nested class, though, as the parser may have local state that
4140 is not a GC root. */
4141 if (!function_depth)
4142 ggc_collect ();
4143 return false;
4146 gcc_assert (DECL_SAVED_TREE (fn));
4148 /* We make a decision about linkage for these functions at the end
4149 of the compilation. Until that point, we do not want the back
4150 end to output them -- but we do want it to see the bodies of
4151 these functions so that it can inline them as appropriate. */
4152 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4154 if (DECL_INTERFACE_KNOWN (fn))
4155 /* We've already made a decision as to how this function will
4156 be handled. */;
4157 else if (!at_eof)
4158 tentative_decl_linkage (fn);
4159 else
4160 import_export_decl (fn);
4162 /* If the user wants us to keep all inline functions, then mark
4163 this function as needed so that finish_file will make sure to
4164 output it later. Similarly, all dllexport'd functions must
4165 be emitted; there may be callers in other DLLs. */
4166 if (DECL_DECLARED_INLINE_P (fn)
4167 && !DECL_REALLY_EXTERN (fn)
4168 && (flag_keep_inline_functions
4169 || (flag_keep_inline_dllexport
4170 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4172 mark_needed (fn);
4173 DECL_EXTERNAL (fn) = 0;
4177 /* If this is a constructor or destructor body, we have to clone
4178 it. */
4179 if (maybe_clone_body (fn))
4181 /* We don't want to process FN again, so pretend we've written
4182 it out, even though we haven't. */
4183 TREE_ASM_WRITTEN (fn) = 1;
4184 /* If this is an instantiation of a constexpr function, keep
4185 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4186 if (!is_instantiation_of_constexpr (fn))
4187 DECL_SAVED_TREE (fn) = NULL_TREE;
4188 return false;
4191 /* There's no reason to do any of the work here if we're only doing
4192 semantic analysis; this code just generates RTL. */
4193 if (flag_syntax_only)
4194 return false;
4196 return true;
4199 void
4200 expand_or_defer_fn (tree fn)
4202 if (expand_or_defer_fn_1 (fn))
4204 function_depth++;
4206 /* Expand or defer, at the whim of the compilation unit manager. */
4207 cgraph_node::finalize_function (fn, function_depth > 1);
4208 emit_associated_thunks (fn);
4210 function_depth--;
4214 struct nrv_data
4216 nrv_data () : visited (37) {}
4218 tree var;
4219 tree result;
4220 hash_table<pointer_hash <tree_node> > visited;
4223 /* Helper function for walk_tree, used by finalize_nrv below. */
4225 static tree
4226 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4228 struct nrv_data *dp = (struct nrv_data *)data;
4229 tree_node **slot;
4231 /* No need to walk into types. There wouldn't be any need to walk into
4232 non-statements, except that we have to consider STMT_EXPRs. */
4233 if (TYPE_P (*tp))
4234 *walk_subtrees = 0;
4235 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4236 but differs from using NULL_TREE in that it indicates that we care
4237 about the value of the RESULT_DECL. */
4238 else if (TREE_CODE (*tp) == RETURN_EXPR)
4239 TREE_OPERAND (*tp, 0) = dp->result;
4240 /* Change all cleanups for the NRV to only run when an exception is
4241 thrown. */
4242 else if (TREE_CODE (*tp) == CLEANUP_STMT
4243 && CLEANUP_DECL (*tp) == dp->var)
4244 CLEANUP_EH_ONLY (*tp) = 1;
4245 /* Replace the DECL_EXPR for the NRV with an initialization of the
4246 RESULT_DECL, if needed. */
4247 else if (TREE_CODE (*tp) == DECL_EXPR
4248 && DECL_EXPR_DECL (*tp) == dp->var)
4250 tree init;
4251 if (DECL_INITIAL (dp->var)
4252 && DECL_INITIAL (dp->var) != error_mark_node)
4253 init = build2 (INIT_EXPR, void_type_node, dp->result,
4254 DECL_INITIAL (dp->var));
4255 else
4256 init = build_empty_stmt (EXPR_LOCATION (*tp));
4257 DECL_INITIAL (dp->var) = NULL_TREE;
4258 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4259 *tp = init;
4261 /* And replace all uses of the NRV with the RESULT_DECL. */
4262 else if (*tp == dp->var)
4263 *tp = dp->result;
4265 /* Avoid walking into the same tree more than once. Unfortunately, we
4266 can't just use walk_tree_without duplicates because it would only call
4267 us for the first occurrence of dp->var in the function body. */
4268 slot = dp->visited.find_slot (*tp, INSERT);
4269 if (*slot)
4270 *walk_subtrees = 0;
4271 else
4272 *slot = *tp;
4274 /* Keep iterating. */
4275 return NULL_TREE;
4278 /* Called from finish_function to implement the named return value
4279 optimization by overriding all the RETURN_EXPRs and pertinent
4280 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4281 RESULT_DECL for the function. */
4283 void
4284 finalize_nrv (tree *tp, tree var, tree result)
4286 struct nrv_data data;
4288 /* Copy name from VAR to RESULT. */
4289 DECL_NAME (result) = DECL_NAME (var);
4290 /* Don't forget that we take its address. */
4291 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4292 /* Finally set DECL_VALUE_EXPR to avoid assigning
4293 a stack slot at -O0 for the original var and debug info
4294 uses RESULT location for VAR. */
4295 SET_DECL_VALUE_EXPR (var, result);
4296 DECL_HAS_VALUE_EXPR_P (var) = 1;
4298 data.var = var;
4299 data.result = result;
4300 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4303 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4305 bool
4306 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4307 bool need_copy_ctor, bool need_copy_assignment,
4308 bool need_dtor)
4310 int save_errorcount = errorcount;
4311 tree info, t;
4313 /* Always allocate 3 elements for simplicity. These are the
4314 function decls for the ctor, dtor, and assignment op.
4315 This layout is known to the three lang hooks,
4316 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4317 and cxx_omp_clause_assign_op. */
4318 info = make_tree_vec (3);
4319 CP_OMP_CLAUSE_INFO (c) = info;
4321 if (need_default_ctor || need_copy_ctor)
4323 if (need_default_ctor)
4324 t = get_default_ctor (type);
4325 else
4326 t = get_copy_ctor (type, tf_warning_or_error);
4328 if (t && !trivial_fn_p (t))
4329 TREE_VEC_ELT (info, 0) = t;
4332 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4333 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4335 if (need_copy_assignment)
4337 t = get_copy_assign (type);
4339 if (t && !trivial_fn_p (t))
4340 TREE_VEC_ELT (info, 2) = t;
4343 return errorcount != save_errorcount;
4346 /* Helper function for handle_omp_array_sections. Called recursively
4347 to handle multiple array-section-subscripts. C is the clause,
4348 T current expression (initially OMP_CLAUSE_DECL), which is either
4349 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4350 expression if specified, TREE_VALUE length expression if specified,
4351 TREE_CHAIN is what it has been specified after, or some decl.
4352 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4353 set to true if any of the array-section-subscript could have length
4354 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4355 first array-section-subscript which is known not to have length
4356 of one. Given say:
4357 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4358 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4359 all are or may have length of 1, array-section-subscript [:2] is the
4360 first one knonwn not to have length 1. For array-section-subscript
4361 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4362 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4363 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4364 case though, as some lengths could be zero. */
4366 static tree
4367 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4368 bool &maybe_zero_len, unsigned int &first_non_one)
4370 tree ret, low_bound, length, type;
4371 if (TREE_CODE (t) != TREE_LIST)
4373 if (error_operand_p (t))
4374 return error_mark_node;
4375 if (type_dependent_expression_p (t))
4376 return NULL_TREE;
4377 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4379 if (processing_template_decl)
4380 return NULL_TREE;
4381 if (DECL_P (t))
4382 error_at (OMP_CLAUSE_LOCATION (c),
4383 "%qD is not a variable in %qs clause", t,
4384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4385 else
4386 error_at (OMP_CLAUSE_LOCATION (c),
4387 "%qE is not a variable in %qs clause", t,
4388 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4389 return error_mark_node;
4391 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4392 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4394 error_at (OMP_CLAUSE_LOCATION (c),
4395 "%qD is threadprivate variable in %qs clause", t,
4396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4397 return error_mark_node;
4399 t = convert_from_reference (t);
4400 return t;
4403 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4404 maybe_zero_len, first_non_one);
4405 if (ret == error_mark_node || ret == NULL_TREE)
4406 return ret;
4408 type = TREE_TYPE (ret);
4409 low_bound = TREE_PURPOSE (t);
4410 length = TREE_VALUE (t);
4411 if ((low_bound && type_dependent_expression_p (low_bound))
4412 || (length && type_dependent_expression_p (length)))
4413 return NULL_TREE;
4415 if (low_bound == error_mark_node || length == error_mark_node)
4416 return error_mark_node;
4418 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4420 error_at (OMP_CLAUSE_LOCATION (c),
4421 "low bound %qE of array section does not have integral type",
4422 low_bound);
4423 return error_mark_node;
4425 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4427 error_at (OMP_CLAUSE_LOCATION (c),
4428 "length %qE of array section does not have integral type",
4429 length);
4430 return error_mark_node;
4432 if (low_bound)
4433 low_bound = mark_rvalue_use (low_bound);
4434 if (length)
4435 length = mark_rvalue_use (length);
4436 if (low_bound
4437 && TREE_CODE (low_bound) == INTEGER_CST
4438 && TYPE_PRECISION (TREE_TYPE (low_bound))
4439 > TYPE_PRECISION (sizetype))
4440 low_bound = fold_convert (sizetype, low_bound);
4441 if (length
4442 && TREE_CODE (length) == INTEGER_CST
4443 && TYPE_PRECISION (TREE_TYPE (length))
4444 > TYPE_PRECISION (sizetype))
4445 length = fold_convert (sizetype, length);
4446 if (low_bound == NULL_TREE)
4447 low_bound = integer_zero_node;
4449 if (length != NULL_TREE)
4451 if (!integer_nonzerop (length))
4452 maybe_zero_len = true;
4453 if (first_non_one == types.length ()
4454 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4455 first_non_one++;
4457 if (TREE_CODE (type) == ARRAY_TYPE)
4459 if (length == NULL_TREE
4460 && (TYPE_DOMAIN (type) == NULL_TREE
4461 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4463 error_at (OMP_CLAUSE_LOCATION (c),
4464 "for unknown bound array type length expression must "
4465 "be specified");
4466 return error_mark_node;
4468 if (TREE_CODE (low_bound) == INTEGER_CST
4469 && tree_int_cst_sgn (low_bound) == -1)
4471 error_at (OMP_CLAUSE_LOCATION (c),
4472 "negative low bound in array section in %qs clause",
4473 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4474 return error_mark_node;
4476 if (length != NULL_TREE
4477 && TREE_CODE (length) == INTEGER_CST
4478 && tree_int_cst_sgn (length) == -1)
4480 error_at (OMP_CLAUSE_LOCATION (c),
4481 "negative length in array section in %qs clause",
4482 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4483 return error_mark_node;
4485 if (TYPE_DOMAIN (type)
4486 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4487 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4488 == INTEGER_CST)
4490 tree size = size_binop (PLUS_EXPR,
4491 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4492 size_one_node);
4493 if (TREE_CODE (low_bound) == INTEGER_CST)
4495 if (tree_int_cst_lt (size, low_bound))
4497 error_at (OMP_CLAUSE_LOCATION (c),
4498 "low bound %qE above array section size "
4499 "in %qs clause", low_bound,
4500 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4501 return error_mark_node;
4503 if (tree_int_cst_equal (size, low_bound))
4504 maybe_zero_len = true;
4505 else if (length == NULL_TREE
4506 && first_non_one == types.length ()
4507 && tree_int_cst_equal
4508 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4509 low_bound))
4510 first_non_one++;
4512 else if (length == NULL_TREE)
4514 maybe_zero_len = true;
4515 if (first_non_one == types.length ())
4516 first_non_one++;
4518 if (length && TREE_CODE (length) == INTEGER_CST)
4520 if (tree_int_cst_lt (size, length))
4522 error_at (OMP_CLAUSE_LOCATION (c),
4523 "length %qE above array section size "
4524 "in %qs clause", length,
4525 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4526 return error_mark_node;
4528 if (TREE_CODE (low_bound) == INTEGER_CST)
4530 tree lbpluslen
4531 = size_binop (PLUS_EXPR,
4532 fold_convert (sizetype, low_bound),
4533 fold_convert (sizetype, length));
4534 if (TREE_CODE (lbpluslen) == INTEGER_CST
4535 && tree_int_cst_lt (size, lbpluslen))
4537 error_at (OMP_CLAUSE_LOCATION (c),
4538 "high bound %qE above array section size "
4539 "in %qs clause", lbpluslen,
4540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4541 return error_mark_node;
4546 else if (length == NULL_TREE)
4548 maybe_zero_len = true;
4549 if (first_non_one == types.length ())
4550 first_non_one++;
4553 /* For [lb:] we will need to evaluate lb more than once. */
4554 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4556 tree lb = cp_save_expr (low_bound);
4557 if (lb != low_bound)
4559 TREE_PURPOSE (t) = lb;
4560 low_bound = lb;
4564 else if (TREE_CODE (type) == POINTER_TYPE)
4566 if (length == NULL_TREE)
4568 error_at (OMP_CLAUSE_LOCATION (c),
4569 "for pointer type length expression must be specified");
4570 return error_mark_node;
4572 /* If there is a pointer type anywhere but in the very first
4573 array-section-subscript, the array section can't be contiguous. */
4574 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4575 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4577 error_at (OMP_CLAUSE_LOCATION (c),
4578 "array section is not contiguous in %qs clause",
4579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4580 return error_mark_node;
4583 else
4585 error_at (OMP_CLAUSE_LOCATION (c),
4586 "%qE does not have pointer or array type", ret);
4587 return error_mark_node;
4589 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4590 types.safe_push (TREE_TYPE (ret));
4591 /* We will need to evaluate lb more than once. */
4592 tree lb = cp_save_expr (low_bound);
4593 if (lb != low_bound)
4595 TREE_PURPOSE (t) = lb;
4596 low_bound = lb;
4598 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4599 return ret;
4602 /* Handle array sections for clause C. */
4604 static bool
4605 handle_omp_array_sections (tree c)
4607 bool maybe_zero_len = false;
4608 unsigned int first_non_one = 0;
4609 auto_vec<tree> types;
4610 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4611 maybe_zero_len, first_non_one);
4612 if (first == error_mark_node)
4613 return true;
4614 if (first == NULL_TREE)
4615 return false;
4616 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4618 tree t = OMP_CLAUSE_DECL (c);
4619 tree tem = NULL_TREE;
4620 if (processing_template_decl)
4621 return false;
4622 /* Need to evaluate side effects in the length expressions
4623 if any. */
4624 while (TREE_CODE (t) == TREE_LIST)
4626 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4628 if (tem == NULL_TREE)
4629 tem = TREE_VALUE (t);
4630 else
4631 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4632 TREE_VALUE (t), tem);
4634 t = TREE_CHAIN (t);
4636 if (tem)
4637 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4638 OMP_CLAUSE_DECL (c) = first;
4640 else
4642 unsigned int num = types.length (), i;
4643 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4644 tree condition = NULL_TREE;
4646 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4647 maybe_zero_len = true;
4648 if (processing_template_decl && maybe_zero_len)
4649 return false;
4651 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4652 t = TREE_CHAIN (t))
4654 tree low_bound = TREE_PURPOSE (t);
4655 tree length = TREE_VALUE (t);
4657 i--;
4658 if (low_bound
4659 && TREE_CODE (low_bound) == INTEGER_CST
4660 && TYPE_PRECISION (TREE_TYPE (low_bound))
4661 > TYPE_PRECISION (sizetype))
4662 low_bound = fold_convert (sizetype, low_bound);
4663 if (length
4664 && TREE_CODE (length) == INTEGER_CST
4665 && TYPE_PRECISION (TREE_TYPE (length))
4666 > TYPE_PRECISION (sizetype))
4667 length = fold_convert (sizetype, length);
4668 if (low_bound == NULL_TREE)
4669 low_bound = integer_zero_node;
4670 if (!maybe_zero_len && i > first_non_one)
4672 if (integer_nonzerop (low_bound))
4673 goto do_warn_noncontiguous;
4674 if (length != NULL_TREE
4675 && TREE_CODE (length) == INTEGER_CST
4676 && TYPE_DOMAIN (types[i])
4677 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4678 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4679 == INTEGER_CST)
4681 tree size;
4682 size = size_binop (PLUS_EXPR,
4683 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4684 size_one_node);
4685 if (!tree_int_cst_equal (length, size))
4687 do_warn_noncontiguous:
4688 error_at (OMP_CLAUSE_LOCATION (c),
4689 "array section is not contiguous in %qs "
4690 "clause",
4691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4692 return true;
4695 if (!processing_template_decl
4696 && length != NULL_TREE
4697 && TREE_SIDE_EFFECTS (length))
4699 if (side_effects == NULL_TREE)
4700 side_effects = length;
4701 else
4702 side_effects = build2 (COMPOUND_EXPR,
4703 TREE_TYPE (side_effects),
4704 length, side_effects);
4707 else if (processing_template_decl)
4708 continue;
4709 else
4711 tree l;
4713 if (i > first_non_one && length && integer_nonzerop (length))
4714 continue;
4715 if (length)
4716 l = fold_convert (sizetype, length);
4717 else
4719 l = size_binop (PLUS_EXPR,
4720 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4721 size_one_node);
4722 l = size_binop (MINUS_EXPR, l,
4723 fold_convert (sizetype, low_bound));
4725 if (i > first_non_one)
4727 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4728 size_zero_node);
4729 if (condition == NULL_TREE)
4730 condition = l;
4731 else
4732 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4733 l, condition);
4735 else if (size == NULL_TREE)
4737 size = size_in_bytes (TREE_TYPE (types[i]));
4738 size = size_binop (MULT_EXPR, size, l);
4739 if (condition)
4740 size = fold_build3 (COND_EXPR, sizetype, condition,
4741 size, size_zero_node);
4743 else
4744 size = size_binop (MULT_EXPR, size, l);
4747 if (!processing_template_decl)
4749 if (side_effects)
4750 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4751 OMP_CLAUSE_DECL (c) = first;
4752 OMP_CLAUSE_SIZE (c) = size;
4753 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4754 return false;
4755 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4756 OMP_CLAUSE_MAP);
4757 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4758 if (!cxx_mark_addressable (t))
4759 return false;
4760 OMP_CLAUSE_DECL (c2) = t;
4761 t = build_fold_addr_expr (first);
4762 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4763 ptrdiff_type_node, t);
4764 tree ptr = OMP_CLAUSE_DECL (c2);
4765 ptr = convert_from_reference (ptr);
4766 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4767 ptr = build_fold_addr_expr (ptr);
4768 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4769 ptrdiff_type_node, t,
4770 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4771 ptrdiff_type_node, ptr));
4772 OMP_CLAUSE_SIZE (c2) = t;
4773 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4774 OMP_CLAUSE_CHAIN (c) = c2;
4775 ptr = OMP_CLAUSE_DECL (c2);
4776 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4777 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4779 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4780 OMP_CLAUSE_MAP);
4781 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4782 OMP_CLAUSE_DECL (c3) = ptr;
4783 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4784 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4785 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4786 OMP_CLAUSE_CHAIN (c2) = c3;
4790 return false;
4793 /* Return identifier to look up for omp declare reduction. */
4795 tree
4796 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4798 const char *p = NULL;
4799 const char *m = NULL;
4800 switch (reduction_code)
4802 case PLUS_EXPR:
4803 case MULT_EXPR:
4804 case MINUS_EXPR:
4805 case BIT_AND_EXPR:
4806 case BIT_XOR_EXPR:
4807 case BIT_IOR_EXPR:
4808 case TRUTH_ANDIF_EXPR:
4809 case TRUTH_ORIF_EXPR:
4810 reduction_id = ansi_opname (reduction_code);
4811 break;
4812 case MIN_EXPR:
4813 p = "min";
4814 break;
4815 case MAX_EXPR:
4816 p = "max";
4817 break;
4818 default:
4819 break;
4822 if (p == NULL)
4824 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4825 return error_mark_node;
4826 p = IDENTIFIER_POINTER (reduction_id);
4829 if (type != NULL_TREE)
4830 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4832 const char prefix[] = "omp declare reduction ";
4833 size_t lenp = sizeof (prefix);
4834 if (strncmp (p, prefix, lenp - 1) == 0)
4835 lenp = 1;
4836 size_t len = strlen (p);
4837 size_t lenm = m ? strlen (m) + 1 : 0;
4838 char *name = XALLOCAVEC (char, lenp + len + lenm);
4839 if (lenp > 1)
4840 memcpy (name, prefix, lenp - 1);
4841 memcpy (name + lenp - 1, p, len + 1);
4842 if (m)
4844 name[lenp + len - 1] = '~';
4845 memcpy (name + lenp + len, m, lenm);
4847 return get_identifier (name);
4850 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4851 FUNCTION_DECL or NULL_TREE if not found. */
4853 static tree
4854 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4855 vec<tree> *ambiguousp)
4857 tree orig_id = id;
4858 tree baselink = NULL_TREE;
4859 if (identifier_p (id))
4861 cp_id_kind idk;
4862 bool nonint_cst_expression_p;
4863 const char *error_msg;
4864 id = omp_reduction_id (ERROR_MARK, id, type);
4865 tree decl = lookup_name (id);
4866 if (decl == NULL_TREE)
4867 decl = error_mark_node;
4868 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4869 &nonint_cst_expression_p, false, true, false,
4870 false, &error_msg, loc);
4871 if (idk == CP_ID_KIND_UNQUALIFIED
4872 && identifier_p (id))
4874 vec<tree, va_gc> *args = NULL;
4875 vec_safe_push (args, build_reference_type (type));
4876 id = perform_koenig_lookup (id, args, tf_none);
4879 else if (TREE_CODE (id) == SCOPE_REF)
4880 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4881 omp_reduction_id (ERROR_MARK,
4882 TREE_OPERAND (id, 1),
4883 type),
4884 false, false);
4885 tree fns = id;
4886 if (id && is_overloaded_fn (id))
4887 id = get_fns (id);
4888 for (; id; id = OVL_NEXT (id))
4890 tree fndecl = OVL_CURRENT (id);
4891 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4893 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4894 if (same_type_p (TREE_TYPE (argtype), type))
4895 break;
4898 if (id && BASELINK_P (fns))
4900 if (baselinkp)
4901 *baselinkp = fns;
4902 else
4903 baselink = fns;
4905 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4907 vec<tree> ambiguous = vNULL;
4908 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4909 unsigned int ix;
4910 if (ambiguousp == NULL)
4911 ambiguousp = &ambiguous;
4912 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4914 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4915 baselinkp ? baselinkp : &baselink,
4916 ambiguousp);
4917 if (id == NULL_TREE)
4918 continue;
4919 if (!ambiguousp->is_empty ())
4920 ambiguousp->safe_push (id);
4921 else if (ret != NULL_TREE)
4923 ambiguousp->safe_push (ret);
4924 ambiguousp->safe_push (id);
4925 ret = NULL_TREE;
4927 else
4928 ret = id;
4930 if (ambiguousp != &ambiguous)
4931 return ret;
4932 if (!ambiguous.is_empty ())
4934 const char *str = _("candidates are:");
4935 unsigned int idx;
4936 tree udr;
4937 error_at (loc, "user defined reduction lookup is ambiguous");
4938 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4940 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4941 if (idx == 0)
4942 str = get_spaces (str);
4944 ambiguous.release ();
4945 ret = error_mark_node;
4946 baselink = NULL_TREE;
4948 id = ret;
4950 if (id && baselink)
4951 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4952 id, id, tf_warning_or_error);
4953 return id;
4956 /* Helper function for cp_parser_omp_declare_reduction_exprs
4957 and tsubst_omp_udr.
4958 Remove CLEANUP_STMT for data (omp_priv variable).
4959 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4960 DECL_EXPR. */
4962 tree
4963 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4965 if (TYPE_P (*tp))
4966 *walk_subtrees = 0;
4967 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4968 *tp = CLEANUP_BODY (*tp);
4969 else if (TREE_CODE (*tp) == DECL_EXPR)
4971 tree decl = DECL_EXPR_DECL (*tp);
4972 if (!processing_template_decl
4973 && decl == (tree) data
4974 && DECL_INITIAL (decl)
4975 && DECL_INITIAL (decl) != error_mark_node)
4977 tree list = NULL_TREE;
4978 append_to_statement_list_force (*tp, &list);
4979 tree init_expr = build2 (INIT_EXPR, void_type_node,
4980 decl, DECL_INITIAL (decl));
4981 DECL_INITIAL (decl) = NULL_TREE;
4982 append_to_statement_list_force (init_expr, &list);
4983 *tp = list;
4986 return NULL_TREE;
4989 /* Data passed from cp_check_omp_declare_reduction to
4990 cp_check_omp_declare_reduction_r. */
4992 struct cp_check_omp_declare_reduction_data
4994 location_t loc;
4995 tree stmts[7];
4996 bool combiner_p;
4999 /* Helper function for cp_check_omp_declare_reduction, called via
5000 cp_walk_tree. */
5002 static tree
5003 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5005 struct cp_check_omp_declare_reduction_data *udr_data
5006 = (struct cp_check_omp_declare_reduction_data *) data;
5007 if (SSA_VAR_P (*tp)
5008 && !DECL_ARTIFICIAL (*tp)
5009 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5010 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5012 location_t loc = udr_data->loc;
5013 if (udr_data->combiner_p)
5014 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5015 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5016 *tp);
5017 else
5018 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5019 "to variable %qD which is not %<omp_priv%> nor "
5020 "%<omp_orig%>",
5021 *tp);
5022 return *tp;
5024 return NULL_TREE;
5027 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5029 void
5030 cp_check_omp_declare_reduction (tree udr)
5032 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5033 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5034 type = TREE_TYPE (type);
5035 int i;
5036 location_t loc = DECL_SOURCE_LOCATION (udr);
5038 if (type == error_mark_node)
5039 return;
5040 if (ARITHMETIC_TYPE_P (type))
5042 static enum tree_code predef_codes[]
5043 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5044 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5045 for (i = 0; i < 8; i++)
5047 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5048 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5049 const char *n2 = IDENTIFIER_POINTER (id);
5050 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5051 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5052 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5053 break;
5056 if (i == 8
5057 && TREE_CODE (type) != COMPLEX_EXPR)
5059 const char prefix_minmax[] = "omp declare reduction m";
5060 size_t prefix_size = sizeof (prefix_minmax) - 1;
5061 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5062 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5063 prefix_minmax, prefix_size) == 0
5064 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5065 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5066 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5067 i = 0;
5069 if (i < 8)
5071 error_at (loc, "predeclared arithmetic type %qT in "
5072 "%<#pragma omp declare reduction%>", type);
5073 return;
5076 else if (TREE_CODE (type) == FUNCTION_TYPE
5077 || TREE_CODE (type) == METHOD_TYPE
5078 || TREE_CODE (type) == ARRAY_TYPE)
5080 error_at (loc, "function or array type %qT in "
5081 "%<#pragma omp declare reduction%>", type);
5082 return;
5084 else if (TREE_CODE (type) == REFERENCE_TYPE)
5086 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5087 type);
5088 return;
5090 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5092 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5093 "%<#pragma omp declare reduction%>", type);
5094 return;
5097 tree body = DECL_SAVED_TREE (udr);
5098 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5099 return;
5101 tree_stmt_iterator tsi;
5102 struct cp_check_omp_declare_reduction_data data;
5103 memset (data.stmts, 0, sizeof data.stmts);
5104 for (i = 0, tsi = tsi_start (body);
5105 i < 7 && !tsi_end_p (tsi);
5106 i++, tsi_next (&tsi))
5107 data.stmts[i] = tsi_stmt (tsi);
5108 data.loc = loc;
5109 gcc_assert (tsi_end_p (tsi));
5110 if (i >= 3)
5112 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5113 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5114 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5115 return;
5116 data.combiner_p = true;
5117 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5118 &data, NULL))
5119 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5121 if (i >= 6)
5123 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5124 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5125 data.combiner_p = false;
5126 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5127 &data, NULL)
5128 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5129 cp_check_omp_declare_reduction_r, &data, NULL))
5130 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5131 if (i == 7)
5132 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5136 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5137 an inline call. But, remap
5138 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5139 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5141 static tree
5142 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5143 tree decl, tree placeholder)
5145 copy_body_data id;
5146 hash_map<tree, tree> decl_map;
5148 decl_map.put (omp_decl1, placeholder);
5149 decl_map.put (omp_decl2, decl);
5150 memset (&id, 0, sizeof (id));
5151 id.src_fn = DECL_CONTEXT (omp_decl1);
5152 id.dst_fn = current_function_decl;
5153 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5154 id.decl_map = &decl_map;
5156 id.copy_decl = copy_decl_no_change;
5157 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5158 id.transform_new_cfg = true;
5159 id.transform_return_to_modify = false;
5160 id.transform_lang_insert_block = NULL;
5161 id.eh_lp_nr = 0;
5162 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5163 return stmt;
5166 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5167 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5169 static tree
5170 find_omp_placeholder_r (tree *tp, int *, void *data)
5172 if (*tp == (tree) data)
5173 return *tp;
5174 return NULL_TREE;
5177 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5178 Return true if there is some error and the clause should be removed. */
5180 static bool
5181 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5183 tree t = OMP_CLAUSE_DECL (c);
5184 bool predefined = false;
5185 tree type = TREE_TYPE (t);
5186 if (TREE_CODE (type) == REFERENCE_TYPE)
5187 type = TREE_TYPE (type);
5188 if (type == error_mark_node)
5189 return true;
5190 else if (ARITHMETIC_TYPE_P (type))
5191 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5193 case PLUS_EXPR:
5194 case MULT_EXPR:
5195 case MINUS_EXPR:
5196 predefined = true;
5197 break;
5198 case MIN_EXPR:
5199 case MAX_EXPR:
5200 if (TREE_CODE (type) == COMPLEX_TYPE)
5201 break;
5202 predefined = true;
5203 break;
5204 case BIT_AND_EXPR:
5205 case BIT_IOR_EXPR:
5206 case BIT_XOR_EXPR:
5207 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5208 break;
5209 predefined = true;
5210 break;
5211 case TRUTH_ANDIF_EXPR:
5212 case TRUTH_ORIF_EXPR:
5213 if (FLOAT_TYPE_P (type))
5214 break;
5215 predefined = true;
5216 break;
5217 default:
5218 break;
5220 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5222 error ("%qE has invalid type for %<reduction%>", t);
5223 return true;
5225 else if (!processing_template_decl)
5227 t = require_complete_type (t);
5228 if (t == error_mark_node)
5229 return true;
5230 OMP_CLAUSE_DECL (c) = t;
5233 if (predefined)
5235 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5236 return false;
5238 else if (processing_template_decl)
5239 return false;
5241 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5243 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5244 if (TREE_CODE (type) == REFERENCE_TYPE)
5245 type = TREE_TYPE (type);
5246 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5247 if (id == NULL_TREE)
5248 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5249 NULL_TREE, NULL_TREE);
5250 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5251 if (id)
5253 if (id == error_mark_node)
5254 return true;
5255 id = OVL_CURRENT (id);
5256 mark_used (id);
5257 tree body = DECL_SAVED_TREE (id);
5258 if (!body)
5259 return true;
5260 if (TREE_CODE (body) == STATEMENT_LIST)
5262 tree_stmt_iterator tsi;
5263 tree placeholder = NULL_TREE;
5264 int i;
5265 tree stmts[7];
5266 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5267 atype = TREE_TYPE (atype);
5268 bool need_static_cast = !same_type_p (type, atype);
5269 memset (stmts, 0, sizeof stmts);
5270 for (i = 0, tsi = tsi_start (body);
5271 i < 7 && !tsi_end_p (tsi);
5272 i++, tsi_next (&tsi))
5273 stmts[i] = tsi_stmt (tsi);
5274 gcc_assert (tsi_end_p (tsi));
5276 if (i >= 3)
5278 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5279 && TREE_CODE (stmts[1]) == DECL_EXPR);
5280 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5281 DECL_ARTIFICIAL (placeholder) = 1;
5282 DECL_IGNORED_P (placeholder) = 1;
5283 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5284 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5285 cxx_mark_addressable (placeholder);
5286 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5287 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5288 != REFERENCE_TYPE)
5289 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5290 tree omp_out = placeholder;
5291 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5292 if (need_static_cast)
5294 tree rtype = build_reference_type (atype);
5295 omp_out = build_static_cast (rtype, omp_out,
5296 tf_warning_or_error);
5297 omp_in = build_static_cast (rtype, omp_in,
5298 tf_warning_or_error);
5299 if (omp_out == error_mark_node || omp_in == error_mark_node)
5300 return true;
5301 omp_out = convert_from_reference (omp_out);
5302 omp_in = convert_from_reference (omp_in);
5304 OMP_CLAUSE_REDUCTION_MERGE (c)
5305 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5306 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5308 if (i >= 6)
5310 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5311 && TREE_CODE (stmts[4]) == DECL_EXPR);
5312 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5313 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5314 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5315 cxx_mark_addressable (placeholder);
5316 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5317 tree omp_orig = placeholder;
5318 if (need_static_cast)
5320 if (i == 7)
5322 error_at (OMP_CLAUSE_LOCATION (c),
5323 "user defined reduction with constructor "
5324 "initializer for base class %qT", atype);
5325 return true;
5327 tree rtype = build_reference_type (atype);
5328 omp_priv = build_static_cast (rtype, omp_priv,
5329 tf_warning_or_error);
5330 omp_orig = build_static_cast (rtype, omp_orig,
5331 tf_warning_or_error);
5332 if (omp_priv == error_mark_node
5333 || omp_orig == error_mark_node)
5334 return true;
5335 omp_priv = convert_from_reference (omp_priv);
5336 omp_orig = convert_from_reference (omp_orig);
5338 if (i == 6)
5339 *need_default_ctor = true;
5340 OMP_CLAUSE_REDUCTION_INIT (c)
5341 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5342 DECL_EXPR_DECL (stmts[3]),
5343 omp_priv, omp_orig);
5344 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5345 find_omp_placeholder_r, placeholder, NULL))
5346 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5348 else if (i >= 3)
5350 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5351 *need_default_ctor = true;
5352 else
5354 tree init;
5355 tree v = convert_from_reference (t);
5356 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5357 init = build_constructor (TREE_TYPE (v), NULL);
5358 else
5359 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5360 OMP_CLAUSE_REDUCTION_INIT (c)
5361 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5366 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5367 *need_dtor = true;
5368 else
5370 error ("user defined reduction not found for %qD", t);
5371 return true;
5373 return false;
5376 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5377 Remove any elements from the list that are invalid. */
5379 tree
5380 finish_omp_clauses (tree clauses)
5382 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5383 bitmap_head aligned_head;
5384 tree c, t, *pc;
5385 bool branch_seen = false;
5386 bool copyprivate_seen = false;
5388 bitmap_obstack_initialize (NULL);
5389 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5390 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5391 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5392 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5394 for (pc = &clauses, c = clauses; c ; c = *pc)
5396 bool remove = false;
5398 switch (OMP_CLAUSE_CODE (c))
5400 case OMP_CLAUSE_SHARED:
5401 goto check_dup_generic;
5402 case OMP_CLAUSE_PRIVATE:
5403 goto check_dup_generic;
5404 case OMP_CLAUSE_REDUCTION:
5405 goto check_dup_generic;
5406 case OMP_CLAUSE_COPYPRIVATE:
5407 copyprivate_seen = true;
5408 goto check_dup_generic;
5409 case OMP_CLAUSE_COPYIN:
5410 goto check_dup_generic;
5411 case OMP_CLAUSE_LINEAR:
5412 t = OMP_CLAUSE_DECL (c);
5413 if (!type_dependent_expression_p (t)
5414 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5415 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5417 error ("linear clause applied to non-integral non-pointer "
5418 "variable with %qT type", TREE_TYPE (t));
5419 remove = true;
5420 break;
5422 t = OMP_CLAUSE_LINEAR_STEP (c);
5423 if (t == NULL_TREE)
5424 t = integer_one_node;
5425 if (t == error_mark_node)
5427 remove = true;
5428 break;
5430 else if (!type_dependent_expression_p (t)
5431 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5433 error ("linear step expression must be integral");
5434 remove = true;
5435 break;
5437 else
5439 t = mark_rvalue_use (t);
5440 if (!processing_template_decl)
5442 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5443 t = maybe_constant_value (t);
5444 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5445 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5446 == POINTER_TYPE)
5448 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5449 OMP_CLAUSE_DECL (c), t);
5450 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5451 MINUS_EXPR, sizetype, t,
5452 OMP_CLAUSE_DECL (c));
5453 if (t == error_mark_node)
5455 remove = true;
5456 break;
5459 else
5460 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5462 OMP_CLAUSE_LINEAR_STEP (c) = t;
5464 goto check_dup_generic;
5465 check_dup_generic:
5466 t = OMP_CLAUSE_DECL (c);
5467 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5469 if (processing_template_decl)
5470 break;
5471 if (DECL_P (t))
5472 error ("%qD is not a variable in clause %qs", t,
5473 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5474 else
5475 error ("%qE is not a variable in clause %qs", t,
5476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5477 remove = true;
5479 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5480 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5481 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5483 error ("%qD appears more than once in data clauses", t);
5484 remove = true;
5486 else
5487 bitmap_set_bit (&generic_head, DECL_UID (t));
5488 break;
5490 case OMP_CLAUSE_FIRSTPRIVATE:
5491 t = OMP_CLAUSE_DECL (c);
5492 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5494 if (processing_template_decl)
5495 break;
5496 if (DECL_P (t))
5497 error ("%qD is not a variable in clause %<firstprivate%>", t);
5498 else
5499 error ("%qE is not a variable in clause %<firstprivate%>", t);
5500 remove = true;
5502 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5503 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5505 error ("%qD appears more than once in data clauses", t);
5506 remove = true;
5508 else
5509 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5510 break;
5512 case OMP_CLAUSE_LASTPRIVATE:
5513 t = OMP_CLAUSE_DECL (c);
5514 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5516 if (processing_template_decl)
5517 break;
5518 if (DECL_P (t))
5519 error ("%qD is not a variable in clause %<lastprivate%>", t);
5520 else
5521 error ("%qE is not a variable in clause %<lastprivate%>", t);
5522 remove = true;
5524 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5525 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5527 error ("%qD appears more than once in data clauses", t);
5528 remove = true;
5530 else
5531 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5532 break;
5534 case OMP_CLAUSE_IF:
5535 t = OMP_CLAUSE_IF_EXPR (c);
5536 t = maybe_convert_cond (t);
5537 if (t == error_mark_node)
5538 remove = true;
5539 else if (!processing_template_decl)
5540 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5541 OMP_CLAUSE_IF_EXPR (c) = t;
5542 break;
5544 case OMP_CLAUSE_FINAL:
5545 t = OMP_CLAUSE_FINAL_EXPR (c);
5546 t = maybe_convert_cond (t);
5547 if (t == error_mark_node)
5548 remove = true;
5549 else if (!processing_template_decl)
5550 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5551 OMP_CLAUSE_FINAL_EXPR (c) = t;
5552 break;
5554 case OMP_CLAUSE_NUM_THREADS:
5555 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5556 if (t == error_mark_node)
5557 remove = true;
5558 else if (!type_dependent_expression_p (t)
5559 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5561 error ("num_threads expression must be integral");
5562 remove = true;
5564 else
5566 t = mark_rvalue_use (t);
5567 if (!processing_template_decl)
5568 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5569 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5571 break;
5573 case OMP_CLAUSE_SCHEDULE:
5574 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5575 if (t == NULL)
5577 else if (t == error_mark_node)
5578 remove = true;
5579 else if (!type_dependent_expression_p (t)
5580 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5581 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5582 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5584 error ("schedule chunk size expression must be integral");
5585 remove = true;
5587 else
5589 t = mark_rvalue_use (t);
5590 if (!processing_template_decl)
5592 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5593 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5595 t = convert_to_integer (long_integer_type_node, t);
5596 if (t == error_mark_node)
5598 remove = true;
5599 break;
5602 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5604 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5606 break;
5608 case OMP_CLAUSE_SIMDLEN:
5609 case OMP_CLAUSE_SAFELEN:
5610 t = OMP_CLAUSE_OPERAND (c, 0);
5611 if (t == error_mark_node)
5612 remove = true;
5613 else if (!type_dependent_expression_p (t)
5614 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5616 error ("%qs length expression must be integral",
5617 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5618 remove = true;
5620 else
5622 t = mark_rvalue_use (t);
5623 t = maybe_constant_value (t);
5624 if (!processing_template_decl)
5626 if (TREE_CODE (t) != INTEGER_CST
5627 || tree_int_cst_sgn (t) != 1)
5629 error ("%qs length expression must be positive constant"
5630 " integer expression",
5631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5632 remove = true;
5635 OMP_CLAUSE_OPERAND (c, 0) = t;
5637 break;
5639 case OMP_CLAUSE_NUM_TEAMS:
5640 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5641 if (t == error_mark_node)
5642 remove = true;
5643 else if (!type_dependent_expression_p (t)
5644 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5646 error ("%<num_teams%> expression must be integral");
5647 remove = true;
5649 else
5651 t = mark_rvalue_use (t);
5652 if (!processing_template_decl)
5653 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5654 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5656 break;
5658 case OMP_CLAUSE_ASYNC:
5659 t = OMP_CLAUSE_ASYNC_EXPR (c);
5660 if (t == error_mark_node)
5661 remove = true;
5662 else if (!type_dependent_expression_p (t)
5663 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5665 error ("%<async%> expression must be integral");
5666 remove = true;
5668 else
5670 t = mark_rvalue_use (t);
5671 if (!processing_template_decl)
5672 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5673 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5675 break;
5677 case OMP_CLAUSE_VECTOR_LENGTH:
5678 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5679 t = maybe_convert_cond (t);
5680 if (t == error_mark_node)
5681 remove = true;
5682 else if (!processing_template_decl)
5683 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5684 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5685 break;
5687 case OMP_CLAUSE_WAIT:
5688 t = OMP_CLAUSE_WAIT_EXPR (c);
5689 if (t == error_mark_node)
5690 remove = true;
5691 else if (!processing_template_decl)
5692 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5693 OMP_CLAUSE_WAIT_EXPR (c) = t;
5694 break;
5696 case OMP_CLAUSE_THREAD_LIMIT:
5697 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5698 if (t == error_mark_node)
5699 remove = true;
5700 else if (!type_dependent_expression_p (t)
5701 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5703 error ("%<thread_limit%> expression must be integral");
5704 remove = true;
5706 else
5708 t = mark_rvalue_use (t);
5709 if (!processing_template_decl)
5710 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5711 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5713 break;
5715 case OMP_CLAUSE_DEVICE:
5716 t = OMP_CLAUSE_DEVICE_ID (c);
5717 if (t == error_mark_node)
5718 remove = true;
5719 else if (!type_dependent_expression_p (t)
5720 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5722 error ("%<device%> id must be integral");
5723 remove = true;
5725 else
5727 t = mark_rvalue_use (t);
5728 if (!processing_template_decl)
5729 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5730 OMP_CLAUSE_DEVICE_ID (c) = t;
5732 break;
5734 case OMP_CLAUSE_DIST_SCHEDULE:
5735 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5736 if (t == NULL)
5738 else if (t == error_mark_node)
5739 remove = true;
5740 else if (!type_dependent_expression_p (t)
5741 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5743 error ("%<dist_schedule%> chunk size expression must be "
5744 "integral");
5745 remove = true;
5747 else
5749 t = mark_rvalue_use (t);
5750 if (!processing_template_decl)
5751 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5752 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5754 break;
5756 case OMP_CLAUSE_ALIGNED:
5757 t = OMP_CLAUSE_DECL (c);
5758 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5760 if (processing_template_decl)
5761 break;
5762 if (DECL_P (t))
5763 error ("%qD is not a variable in %<aligned%> clause", t);
5764 else
5765 error ("%qE is not a variable in %<aligned%> clause", t);
5766 remove = true;
5768 else if (!type_dependent_expression_p (t)
5769 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5770 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5771 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5772 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5773 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5774 != ARRAY_TYPE))))
5776 error_at (OMP_CLAUSE_LOCATION (c),
5777 "%qE in %<aligned%> clause is neither a pointer nor "
5778 "an array nor a reference to pointer or array", t);
5779 remove = true;
5781 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5783 error ("%qD appears more than once in %<aligned%> clauses", t);
5784 remove = true;
5786 else
5787 bitmap_set_bit (&aligned_head, DECL_UID (t));
5788 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5789 if (t == error_mark_node)
5790 remove = true;
5791 else if (t == NULL_TREE)
5792 break;
5793 else if (!type_dependent_expression_p (t)
5794 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5796 error ("%<aligned%> clause alignment expression must "
5797 "be integral");
5798 remove = true;
5800 else
5802 t = mark_rvalue_use (t);
5803 t = maybe_constant_value (t);
5804 if (!processing_template_decl)
5806 if (TREE_CODE (t) != INTEGER_CST
5807 || tree_int_cst_sgn (t) != 1)
5809 error ("%<aligned%> clause alignment expression must be "
5810 "positive constant integer expression");
5811 remove = true;
5814 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5816 break;
5818 case OMP_CLAUSE_DEPEND:
5819 t = OMP_CLAUSE_DECL (c);
5820 if (TREE_CODE (t) == TREE_LIST)
5822 if (handle_omp_array_sections (c))
5823 remove = true;
5824 break;
5826 if (t == error_mark_node)
5827 remove = true;
5828 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5830 if (processing_template_decl)
5831 break;
5832 if (DECL_P (t))
5833 error ("%qD is not a variable in %<depend%> clause", t);
5834 else
5835 error ("%qE is not a variable in %<depend%> clause", t);
5836 remove = true;
5838 else if (!processing_template_decl
5839 && !cxx_mark_addressable (t))
5840 remove = true;
5841 break;
5843 case OMP_CLAUSE_MAP:
5844 case OMP_CLAUSE_TO:
5845 case OMP_CLAUSE_FROM:
5846 case OMP_CLAUSE__CACHE_:
5847 t = OMP_CLAUSE_DECL (c);
5848 if (TREE_CODE (t) == TREE_LIST)
5850 if (handle_omp_array_sections (c))
5851 remove = true;
5852 else
5854 t = OMP_CLAUSE_DECL (c);
5855 if (TREE_CODE (t) != TREE_LIST
5856 && !type_dependent_expression_p (t)
5857 && !cp_omp_mappable_type (TREE_TYPE (t)))
5859 error_at (OMP_CLAUSE_LOCATION (c),
5860 "array section does not have mappable type "
5861 "in %qs clause",
5862 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5863 remove = true;
5866 break;
5868 if (t == error_mark_node)
5869 remove = true;
5870 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5872 if (processing_template_decl)
5873 break;
5874 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5875 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5876 break;
5877 if (DECL_P (t))
5878 error ("%qD is not a variable in %qs clause", t,
5879 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5880 else
5881 error ("%qE is not a variable in %qs clause", t,
5882 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5883 remove = true;
5885 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5887 error ("%qD is threadprivate variable in %qs clause", t,
5888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5889 remove = true;
5891 else if (!processing_template_decl
5892 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5893 && !cxx_mark_addressable (t))
5894 remove = true;
5895 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5896 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5897 && !type_dependent_expression_p (t)
5898 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5899 == REFERENCE_TYPE)
5900 ? TREE_TYPE (TREE_TYPE (t))
5901 : TREE_TYPE (t)))
5903 error_at (OMP_CLAUSE_LOCATION (c),
5904 "%qD does not have a mappable type in %qs clause", t,
5905 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5906 remove = true;
5908 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5910 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5911 error ("%qD appears more than once in motion clauses", t);
5912 else
5913 error ("%qD appears more than once in map clauses", t);
5914 remove = true;
5916 else
5917 bitmap_set_bit (&generic_head, DECL_UID (t));
5918 break;
5920 case OMP_CLAUSE_UNIFORM:
5921 t = OMP_CLAUSE_DECL (c);
5922 if (TREE_CODE (t) != PARM_DECL)
5924 if (processing_template_decl)
5925 break;
5926 if (DECL_P (t))
5927 error ("%qD is not an argument in %<uniform%> clause", t);
5928 else
5929 error ("%qE is not an argument in %<uniform%> clause", t);
5930 remove = true;
5931 break;
5933 goto check_dup_generic;
5935 case OMP_CLAUSE_NOWAIT:
5936 case OMP_CLAUSE_ORDERED:
5937 case OMP_CLAUSE_DEFAULT:
5938 case OMP_CLAUSE_UNTIED:
5939 case OMP_CLAUSE_COLLAPSE:
5940 case OMP_CLAUSE_MERGEABLE:
5941 case OMP_CLAUSE_PARALLEL:
5942 case OMP_CLAUSE_FOR:
5943 case OMP_CLAUSE_SECTIONS:
5944 case OMP_CLAUSE_TASKGROUP:
5945 case OMP_CLAUSE_PROC_BIND:
5946 case OMP_CLAUSE__CILK_FOR_COUNT_:
5947 break;
5949 case OMP_CLAUSE_INBRANCH:
5950 case OMP_CLAUSE_NOTINBRANCH:
5951 if (branch_seen)
5953 error ("%<inbranch%> clause is incompatible with "
5954 "%<notinbranch%>");
5955 remove = true;
5957 branch_seen = true;
5958 break;
5960 default:
5961 gcc_unreachable ();
5964 if (remove)
5965 *pc = OMP_CLAUSE_CHAIN (c);
5966 else
5967 pc = &OMP_CLAUSE_CHAIN (c);
5970 for (pc = &clauses, c = clauses; c ; c = *pc)
5972 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5973 bool remove = false;
5974 bool need_complete_non_reference = false;
5975 bool need_default_ctor = false;
5976 bool need_copy_ctor = false;
5977 bool need_copy_assignment = false;
5978 bool need_implicitly_determined = false;
5979 bool need_dtor = false;
5980 tree type, inner_type;
5982 switch (c_kind)
5984 case OMP_CLAUSE_SHARED:
5985 need_implicitly_determined = true;
5986 break;
5987 case OMP_CLAUSE_PRIVATE:
5988 need_complete_non_reference = true;
5989 need_default_ctor = true;
5990 need_dtor = true;
5991 need_implicitly_determined = true;
5992 break;
5993 case OMP_CLAUSE_FIRSTPRIVATE:
5994 need_complete_non_reference = true;
5995 need_copy_ctor = true;
5996 need_dtor = true;
5997 need_implicitly_determined = true;
5998 break;
5999 case OMP_CLAUSE_LASTPRIVATE:
6000 need_complete_non_reference = true;
6001 need_copy_assignment = true;
6002 need_implicitly_determined = true;
6003 break;
6004 case OMP_CLAUSE_REDUCTION:
6005 need_implicitly_determined = true;
6006 break;
6007 case OMP_CLAUSE_COPYPRIVATE:
6008 need_copy_assignment = true;
6009 break;
6010 case OMP_CLAUSE_COPYIN:
6011 need_copy_assignment = true;
6012 break;
6013 case OMP_CLAUSE_NOWAIT:
6014 if (copyprivate_seen)
6016 error_at (OMP_CLAUSE_LOCATION (c),
6017 "%<nowait%> clause must not be used together "
6018 "with %<copyprivate%>");
6019 *pc = OMP_CLAUSE_CHAIN (c);
6020 continue;
6022 /* FALLTHRU */
6023 default:
6024 pc = &OMP_CLAUSE_CHAIN (c);
6025 continue;
6028 t = OMP_CLAUSE_DECL (c);
6029 if (processing_template_decl
6030 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6032 pc = &OMP_CLAUSE_CHAIN (c);
6033 continue;
6036 switch (c_kind)
6038 case OMP_CLAUSE_LASTPRIVATE:
6039 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6041 need_default_ctor = true;
6042 need_dtor = true;
6044 break;
6046 case OMP_CLAUSE_REDUCTION:
6047 if (finish_omp_reduction_clause (c, &need_default_ctor,
6048 &need_dtor))
6049 remove = true;
6050 else
6051 t = OMP_CLAUSE_DECL (c);
6052 break;
6054 case OMP_CLAUSE_COPYIN:
6055 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
6057 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
6058 remove = true;
6060 break;
6062 default:
6063 break;
6066 if (need_complete_non_reference || need_copy_assignment)
6068 t = require_complete_type (t);
6069 if (t == error_mark_node)
6070 remove = true;
6071 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
6072 && need_complete_non_reference)
6074 error ("%qE has reference type for %qs", t,
6075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6076 remove = true;
6079 if (need_implicitly_determined)
6081 const char *share_name = NULL;
6083 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
6084 share_name = "threadprivate";
6085 else switch (cxx_omp_predetermined_sharing (t))
6087 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6088 break;
6089 case OMP_CLAUSE_DEFAULT_SHARED:
6090 /* const vars may be specified in firstprivate clause. */
6091 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6092 && cxx_omp_const_qual_no_mutable (t))
6093 break;
6094 share_name = "shared";
6095 break;
6096 case OMP_CLAUSE_DEFAULT_PRIVATE:
6097 share_name = "private";
6098 break;
6099 default:
6100 gcc_unreachable ();
6102 if (share_name)
6104 error ("%qE is predetermined %qs for %qs",
6105 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6106 remove = true;
6110 /* We're interested in the base element, not arrays. */
6111 inner_type = type = TREE_TYPE (t);
6112 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6113 inner_type = TREE_TYPE (inner_type);
6115 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6116 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6117 inner_type = TREE_TYPE (inner_type);
6119 /* Check for special function availability by building a call to one.
6120 Save the results, because later we won't be in the right context
6121 for making these queries. */
6122 if (CLASS_TYPE_P (inner_type)
6123 && COMPLETE_TYPE_P (inner_type)
6124 && (need_default_ctor || need_copy_ctor
6125 || need_copy_assignment || need_dtor)
6126 && !type_dependent_expression_p (t)
6127 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6128 need_copy_ctor, need_copy_assignment,
6129 need_dtor))
6130 remove = true;
6132 if (remove)
6133 *pc = OMP_CLAUSE_CHAIN (c);
6134 else
6135 pc = &OMP_CLAUSE_CHAIN (c);
6138 bitmap_obstack_release (NULL);
6139 return clauses;
6142 /* For all variables in the tree_list VARS, mark them as thread local. */
6144 void
6145 finish_omp_threadprivate (tree vars)
6147 tree t;
6149 /* Mark every variable in VARS to be assigned thread local storage. */
6150 for (t = vars; t; t = TREE_CHAIN (t))
6152 tree v = TREE_PURPOSE (t);
6154 if (error_operand_p (v))
6156 else if (!VAR_P (v))
6157 error ("%<threadprivate%> %qD is not file, namespace "
6158 "or block scope variable", v);
6159 /* If V had already been marked threadprivate, it doesn't matter
6160 whether it had been used prior to this point. */
6161 else if (TREE_USED (v)
6162 && (DECL_LANG_SPECIFIC (v) == NULL
6163 || !CP_DECL_THREADPRIVATE_P (v)))
6164 error ("%qE declared %<threadprivate%> after first use", v);
6165 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6166 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6167 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6168 error ("%<threadprivate%> %qE has incomplete type", v);
6169 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6170 && CP_DECL_CONTEXT (v) != current_class_type)
6171 error ("%<threadprivate%> %qE directive not "
6172 "in %qT definition", v, CP_DECL_CONTEXT (v));
6173 else
6175 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6176 if (DECL_LANG_SPECIFIC (v) == NULL)
6178 retrofit_lang_decl (v);
6180 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6181 after the allocation of the lang_decl structure. */
6182 if (DECL_DISCRIMINATOR_P (v))
6183 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6186 if (! DECL_THREAD_LOCAL_P (v))
6188 set_decl_tls_model (v, decl_default_tls_model (v));
6189 /* If rtl has been already set for this var, call
6190 make_decl_rtl once again, so that encode_section_info
6191 has a chance to look at the new decl flags. */
6192 if (DECL_RTL_SET_P (v))
6193 make_decl_rtl (v);
6195 CP_DECL_THREADPRIVATE_P (v) = 1;
6200 /* Build an OpenMP structured block. */
6202 tree
6203 begin_omp_structured_block (void)
6205 return do_pushlevel (sk_omp);
6208 tree
6209 finish_omp_structured_block (tree block)
6211 return do_poplevel (block);
6214 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6215 statement. LOC is the location of the OACC_DATA. */
6217 tree
6218 finish_oacc_data (tree clauses, tree block)
6220 tree stmt;
6222 block = finish_omp_structured_block (block);
6224 stmt = make_node (OACC_DATA);
6225 TREE_TYPE (stmt) = void_type_node;
6226 OACC_DATA_CLAUSES (stmt) = clauses;
6227 OACC_DATA_BODY (stmt) = block;
6229 return add_stmt (stmt);
6232 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6233 statement. LOC is the location of the OACC_KERNELS. */
6235 tree
6236 finish_oacc_kernels (tree clauses, tree block)
6238 tree stmt;
6240 block = finish_omp_structured_block (block);
6242 stmt = make_node (OACC_KERNELS);
6243 TREE_TYPE (stmt) = void_type_node;
6244 OACC_KERNELS_CLAUSES (stmt) = clauses;
6245 OACC_KERNELS_BODY (stmt) = block;
6247 return add_stmt (stmt);
6250 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6251 statement. LOC is the location of the OACC_PARALLEL. */
6253 tree
6254 finish_oacc_parallel (tree clauses, tree block)
6256 tree stmt;
6258 block = finish_omp_structured_block (block);
6260 stmt = make_node (OACC_PARALLEL);
6261 TREE_TYPE (stmt) = void_type_node;
6262 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6263 OACC_PARALLEL_BODY (stmt) = block;
6265 return add_stmt (stmt);
6268 /* Similarly, except force the retention of the BLOCK. */
6270 tree
6271 begin_omp_parallel (void)
6273 keep_next_level (true);
6274 return begin_omp_structured_block ();
6277 tree
6278 finish_omp_parallel (tree clauses, tree body)
6280 tree stmt;
6282 body = finish_omp_structured_block (body);
6284 stmt = make_node (OMP_PARALLEL);
6285 TREE_TYPE (stmt) = void_type_node;
6286 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6287 OMP_PARALLEL_BODY (stmt) = body;
6289 return add_stmt (stmt);
6292 tree
6293 begin_omp_task (void)
6295 keep_next_level (true);
6296 return begin_omp_structured_block ();
6299 tree
6300 finish_omp_task (tree clauses, tree body)
6302 tree stmt;
6304 body = finish_omp_structured_block (body);
6306 stmt = make_node (OMP_TASK);
6307 TREE_TYPE (stmt) = void_type_node;
6308 OMP_TASK_CLAUSES (stmt) = clauses;
6309 OMP_TASK_BODY (stmt) = body;
6311 return add_stmt (stmt);
6314 /* Helper function for finish_omp_for. Convert Ith random access iterator
6315 into integral iterator. Return FALSE if successful. */
6317 static bool
6318 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6319 tree condv, tree incrv, tree *body,
6320 tree *pre_body, tree clauses, tree *lastp)
6322 tree diff, iter_init, iter_incr = NULL, last;
6323 tree incr_var = NULL, orig_pre_body, orig_body, c;
6324 tree decl = TREE_VEC_ELT (declv, i);
6325 tree init = TREE_VEC_ELT (initv, i);
6326 tree cond = TREE_VEC_ELT (condv, i);
6327 tree incr = TREE_VEC_ELT (incrv, i);
6328 tree iter = decl;
6329 location_t elocus = locus;
6331 if (init && EXPR_HAS_LOCATION (init))
6332 elocus = EXPR_LOCATION (init);
6334 switch (TREE_CODE (cond))
6336 case GT_EXPR:
6337 case GE_EXPR:
6338 case LT_EXPR:
6339 case LE_EXPR:
6340 case NE_EXPR:
6341 if (TREE_OPERAND (cond, 1) == iter)
6342 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6343 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6344 if (TREE_OPERAND (cond, 0) != iter)
6345 cond = error_mark_node;
6346 else
6348 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6349 TREE_CODE (cond),
6350 iter, ERROR_MARK,
6351 TREE_OPERAND (cond, 1), ERROR_MARK,
6352 NULL, tf_warning_or_error);
6353 if (error_operand_p (tem))
6354 return true;
6356 break;
6357 default:
6358 cond = error_mark_node;
6359 break;
6361 if (cond == error_mark_node)
6363 error_at (elocus, "invalid controlling predicate");
6364 return true;
6366 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6367 ERROR_MARK, iter, ERROR_MARK, NULL,
6368 tf_warning_or_error);
6369 if (error_operand_p (diff))
6370 return true;
6371 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6373 error_at (elocus, "difference between %qE and %qD does not have integer type",
6374 TREE_OPERAND (cond, 1), iter);
6375 return true;
6378 switch (TREE_CODE (incr))
6380 case PREINCREMENT_EXPR:
6381 case PREDECREMENT_EXPR:
6382 case POSTINCREMENT_EXPR:
6383 case POSTDECREMENT_EXPR:
6384 if (TREE_OPERAND (incr, 0) != iter)
6386 incr = error_mark_node;
6387 break;
6389 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6390 TREE_CODE (incr), iter,
6391 tf_warning_or_error);
6392 if (error_operand_p (iter_incr))
6393 return true;
6394 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6395 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6396 incr = integer_one_node;
6397 else
6398 incr = integer_minus_one_node;
6399 break;
6400 case MODIFY_EXPR:
6401 if (TREE_OPERAND (incr, 0) != iter)
6402 incr = error_mark_node;
6403 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6404 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6406 tree rhs = TREE_OPERAND (incr, 1);
6407 if (TREE_OPERAND (rhs, 0) == iter)
6409 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6410 != INTEGER_TYPE)
6411 incr = error_mark_node;
6412 else
6414 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6415 iter, TREE_CODE (rhs),
6416 TREE_OPERAND (rhs, 1),
6417 tf_warning_or_error);
6418 if (error_operand_p (iter_incr))
6419 return true;
6420 incr = TREE_OPERAND (rhs, 1);
6421 incr = cp_convert (TREE_TYPE (diff), incr,
6422 tf_warning_or_error);
6423 if (TREE_CODE (rhs) == MINUS_EXPR)
6425 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6426 incr = fold_if_not_in_template (incr);
6428 if (TREE_CODE (incr) != INTEGER_CST
6429 && (TREE_CODE (incr) != NOP_EXPR
6430 || (TREE_CODE (TREE_OPERAND (incr, 0))
6431 != INTEGER_CST)))
6432 iter_incr = NULL;
6435 else if (TREE_OPERAND (rhs, 1) == iter)
6437 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6438 || TREE_CODE (rhs) != PLUS_EXPR)
6439 incr = error_mark_node;
6440 else
6442 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6443 PLUS_EXPR,
6444 TREE_OPERAND (rhs, 0),
6445 ERROR_MARK, iter,
6446 ERROR_MARK, NULL,
6447 tf_warning_or_error);
6448 if (error_operand_p (iter_incr))
6449 return true;
6450 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6451 iter, NOP_EXPR,
6452 iter_incr,
6453 tf_warning_or_error);
6454 if (error_operand_p (iter_incr))
6455 return true;
6456 incr = TREE_OPERAND (rhs, 0);
6457 iter_incr = NULL;
6460 else
6461 incr = error_mark_node;
6463 else
6464 incr = error_mark_node;
6465 break;
6466 default:
6467 incr = error_mark_node;
6468 break;
6471 if (incr == error_mark_node)
6473 error_at (elocus, "invalid increment expression");
6474 return true;
6477 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6478 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6479 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6480 && OMP_CLAUSE_DECL (c) == iter)
6481 break;
6483 decl = create_temporary_var (TREE_TYPE (diff));
6484 pushdecl (decl);
6485 add_decl_expr (decl);
6486 last = create_temporary_var (TREE_TYPE (diff));
6487 pushdecl (last);
6488 add_decl_expr (last);
6489 if (c && iter_incr == NULL)
6491 incr_var = create_temporary_var (TREE_TYPE (diff));
6492 pushdecl (incr_var);
6493 add_decl_expr (incr_var);
6495 gcc_assert (stmts_are_full_exprs_p ());
6497 orig_pre_body = *pre_body;
6498 *pre_body = push_stmt_list ();
6499 if (orig_pre_body)
6500 add_stmt (orig_pre_body);
6501 if (init != NULL)
6502 finish_expr_stmt (build_x_modify_expr (elocus,
6503 iter, NOP_EXPR, init,
6504 tf_warning_or_error));
6505 init = build_int_cst (TREE_TYPE (diff), 0);
6506 if (c && iter_incr == NULL)
6508 finish_expr_stmt (build_x_modify_expr (elocus,
6509 incr_var, NOP_EXPR,
6510 incr, tf_warning_or_error));
6511 incr = incr_var;
6512 iter_incr = build_x_modify_expr (elocus,
6513 iter, PLUS_EXPR, incr,
6514 tf_warning_or_error);
6516 finish_expr_stmt (build_x_modify_expr (elocus,
6517 last, NOP_EXPR, init,
6518 tf_warning_or_error));
6519 *pre_body = pop_stmt_list (*pre_body);
6521 cond = cp_build_binary_op (elocus,
6522 TREE_CODE (cond), decl, diff,
6523 tf_warning_or_error);
6524 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6525 elocus, incr, NULL_TREE);
6527 orig_body = *body;
6528 *body = push_stmt_list ();
6529 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6530 iter_init = build_x_modify_expr (elocus,
6531 iter, PLUS_EXPR, iter_init,
6532 tf_warning_or_error);
6533 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6534 finish_expr_stmt (iter_init);
6535 finish_expr_stmt (build_x_modify_expr (elocus,
6536 last, NOP_EXPR, decl,
6537 tf_warning_or_error));
6538 add_stmt (orig_body);
6539 *body = pop_stmt_list (*body);
6541 if (c)
6543 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6544 finish_expr_stmt (iter_incr);
6545 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6546 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6549 TREE_VEC_ELT (declv, i) = decl;
6550 TREE_VEC_ELT (initv, i) = init;
6551 TREE_VEC_ELT (condv, i) = cond;
6552 TREE_VEC_ELT (incrv, i) = incr;
6553 *lastp = last;
6555 return false;
6558 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6559 are directly for their associated operands in the statement. DECL
6560 and INIT are a combo; if DECL is NULL then INIT ought to be a
6561 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6562 optional statements that need to go before the loop into its
6563 sk_omp scope. */
6565 tree
6566 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6567 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6569 tree omp_for = NULL, orig_incr = NULL;
6570 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6571 tree last = NULL_TREE;
6572 location_t elocus;
6573 int i;
6575 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6576 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6577 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6578 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6580 decl = TREE_VEC_ELT (declv, i);
6581 init = TREE_VEC_ELT (initv, i);
6582 cond = TREE_VEC_ELT (condv, i);
6583 incr = TREE_VEC_ELT (incrv, i);
6584 elocus = locus;
6586 if (decl == NULL)
6588 if (init != NULL)
6589 switch (TREE_CODE (init))
6591 case MODIFY_EXPR:
6592 decl = TREE_OPERAND (init, 0);
6593 init = TREE_OPERAND (init, 1);
6594 break;
6595 case MODOP_EXPR:
6596 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6598 decl = TREE_OPERAND (init, 0);
6599 init = TREE_OPERAND (init, 2);
6601 break;
6602 default:
6603 break;
6606 if (decl == NULL)
6608 error_at (locus,
6609 "expected iteration declaration or initialization");
6610 return NULL;
6614 if (init && EXPR_HAS_LOCATION (init))
6615 elocus = EXPR_LOCATION (init);
6617 if (cond == NULL)
6619 error_at (elocus, "missing controlling predicate");
6620 return NULL;
6623 if (incr == NULL)
6625 error_at (elocus, "missing increment expression");
6626 return NULL;
6629 TREE_VEC_ELT (declv, i) = decl;
6630 TREE_VEC_ELT (initv, i) = init;
6633 if (dependent_omp_for_p (declv, initv, condv, incrv))
6635 tree stmt;
6637 stmt = make_node (code);
6639 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6641 /* This is really just a place-holder. We'll be decomposing this
6642 again and going through the cp_build_modify_expr path below when
6643 we instantiate the thing. */
6644 TREE_VEC_ELT (initv, i)
6645 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6646 TREE_VEC_ELT (initv, i));
6649 TREE_TYPE (stmt) = void_type_node;
6650 OMP_FOR_INIT (stmt) = initv;
6651 OMP_FOR_COND (stmt) = condv;
6652 OMP_FOR_INCR (stmt) = incrv;
6653 OMP_FOR_BODY (stmt) = body;
6654 OMP_FOR_PRE_BODY (stmt) = pre_body;
6655 OMP_FOR_CLAUSES (stmt) = clauses;
6657 SET_EXPR_LOCATION (stmt, locus);
6658 return add_stmt (stmt);
6661 if (processing_template_decl)
6662 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6664 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6666 decl = TREE_VEC_ELT (declv, i);
6667 init = TREE_VEC_ELT (initv, i);
6668 cond = TREE_VEC_ELT (condv, i);
6669 incr = TREE_VEC_ELT (incrv, i);
6670 if (orig_incr)
6671 TREE_VEC_ELT (orig_incr, i) = incr;
6672 elocus = locus;
6674 if (init && EXPR_HAS_LOCATION (init))
6675 elocus = EXPR_LOCATION (init);
6677 if (!DECL_P (decl))
6679 error_at (elocus, "expected iteration declaration or initialization");
6680 return NULL;
6683 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6685 if (orig_incr)
6686 TREE_VEC_ELT (orig_incr, i) = incr;
6687 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6688 TREE_CODE (TREE_OPERAND (incr, 1)),
6689 TREE_OPERAND (incr, 2),
6690 tf_warning_or_error);
6693 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6695 if (code == OMP_SIMD)
6697 error_at (elocus, "%<#pragma omp simd%> used with class "
6698 "iteration variable %qE", decl);
6699 return NULL;
6701 if (code == CILK_FOR && i == 0)
6702 orig_decl = decl;
6703 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6704 incrv, &body, &pre_body,
6705 clauses, &last))
6706 return NULL;
6707 continue;
6710 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6711 && !TYPE_PTR_P (TREE_TYPE (decl)))
6713 error_at (elocus, "invalid type for iteration variable %qE", decl);
6714 return NULL;
6717 if (!processing_template_decl)
6719 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6720 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6722 else
6723 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6724 if (cond
6725 && TREE_SIDE_EFFECTS (cond)
6726 && COMPARISON_CLASS_P (cond)
6727 && !processing_template_decl)
6729 tree t = TREE_OPERAND (cond, 0);
6730 if (TREE_SIDE_EFFECTS (t)
6731 && t != decl
6732 && (TREE_CODE (t) != NOP_EXPR
6733 || TREE_OPERAND (t, 0) != decl))
6734 TREE_OPERAND (cond, 0)
6735 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6737 t = TREE_OPERAND (cond, 1);
6738 if (TREE_SIDE_EFFECTS (t)
6739 && t != decl
6740 && (TREE_CODE (t) != NOP_EXPR
6741 || TREE_OPERAND (t, 0) != decl))
6742 TREE_OPERAND (cond, 1)
6743 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6745 if (decl == error_mark_node || init == error_mark_node)
6746 return NULL;
6748 TREE_VEC_ELT (declv, i) = decl;
6749 TREE_VEC_ELT (initv, i) = init;
6750 TREE_VEC_ELT (condv, i) = cond;
6751 TREE_VEC_ELT (incrv, i) = incr;
6752 i++;
6755 if (IS_EMPTY_STMT (pre_body))
6756 pre_body = NULL;
6758 if (code == CILK_FOR && !processing_template_decl)
6759 block = push_stmt_list ();
6761 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6762 body, pre_body);
6764 if (omp_for == NULL)
6766 if (block)
6767 pop_stmt_list (block);
6768 return NULL;
6771 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6773 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6774 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6776 if (TREE_CODE (incr) != MODIFY_EXPR)
6777 continue;
6779 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6780 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6781 && !processing_template_decl)
6783 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6784 if (TREE_SIDE_EFFECTS (t)
6785 && t != decl
6786 && (TREE_CODE (t) != NOP_EXPR
6787 || TREE_OPERAND (t, 0) != decl))
6788 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6789 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6791 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6792 if (TREE_SIDE_EFFECTS (t)
6793 && t != decl
6794 && (TREE_CODE (t) != NOP_EXPR
6795 || TREE_OPERAND (t, 0) != decl))
6796 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6797 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6800 if (orig_incr)
6801 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6803 OMP_FOR_CLAUSES (omp_for) = clauses;
6805 if (block)
6807 tree omp_par = make_node (OMP_PARALLEL);
6808 TREE_TYPE (omp_par) = void_type_node;
6809 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6810 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6811 TREE_SIDE_EFFECTS (bind) = 1;
6812 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6813 OMP_PARALLEL_BODY (omp_par) = bind;
6814 if (OMP_FOR_PRE_BODY (omp_for))
6816 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6817 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6819 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6820 decl = TREE_OPERAND (init, 0);
6821 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6822 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6823 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6824 clauses = OMP_FOR_CLAUSES (omp_for);
6825 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6826 for (pc = &clauses; *pc; )
6827 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6829 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6830 OMP_FOR_CLAUSES (omp_for) = *pc;
6831 *pc = OMP_CLAUSE_CHAIN (*pc);
6832 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6834 else
6836 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6837 pc = &OMP_CLAUSE_CHAIN (*pc);
6839 if (TREE_CODE (t) != INTEGER_CST)
6841 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6842 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6843 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6844 OMP_CLAUSE_CHAIN (c) = clauses;
6845 clauses = c;
6847 if (TREE_CODE (incr) == MODIFY_EXPR)
6849 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6850 if (TREE_CODE (t) != INTEGER_CST)
6852 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6853 = get_temp_regvar (TREE_TYPE (t), t);
6854 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6855 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6856 OMP_CLAUSE_CHAIN (c) = clauses;
6857 clauses = c;
6860 t = TREE_OPERAND (init, 1);
6861 if (TREE_CODE (t) != INTEGER_CST)
6863 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6864 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6865 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6866 OMP_CLAUSE_CHAIN (c) = clauses;
6867 clauses = c;
6869 if (orig_decl && orig_decl != decl)
6871 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6872 OMP_CLAUSE_DECL (c) = orig_decl;
6873 OMP_CLAUSE_CHAIN (c) = clauses;
6874 clauses = c;
6876 if (last)
6878 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6879 OMP_CLAUSE_DECL (c) = last;
6880 OMP_CLAUSE_CHAIN (c) = clauses;
6881 clauses = c;
6883 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6884 OMP_CLAUSE_DECL (c) = decl;
6885 OMP_CLAUSE_CHAIN (c) = clauses;
6886 clauses = c;
6887 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6888 OMP_CLAUSE_OPERAND (c, 0)
6889 = cilk_for_number_of_iterations (omp_for);
6890 OMP_CLAUSE_CHAIN (c) = clauses;
6891 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6892 add_stmt (omp_par);
6893 return omp_par;
6895 else if (code == CILK_FOR && processing_template_decl)
6897 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6898 if (orig_decl && orig_decl != decl)
6900 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6901 OMP_CLAUSE_DECL (c) = orig_decl;
6902 OMP_CLAUSE_CHAIN (c) = clauses;
6903 clauses = c;
6905 if (last)
6907 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6908 OMP_CLAUSE_DECL (c) = last;
6909 OMP_CLAUSE_CHAIN (c) = clauses;
6910 clauses = c;
6912 OMP_FOR_CLAUSES (omp_for) = clauses;
6914 return omp_for;
6917 void
6918 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6919 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6921 tree orig_lhs;
6922 tree orig_rhs;
6923 tree orig_v;
6924 tree orig_lhs1;
6925 tree orig_rhs1;
6926 bool dependent_p;
6927 tree stmt;
6929 orig_lhs = lhs;
6930 orig_rhs = rhs;
6931 orig_v = v;
6932 orig_lhs1 = lhs1;
6933 orig_rhs1 = rhs1;
6934 dependent_p = false;
6935 stmt = NULL_TREE;
6937 /* Even in a template, we can detect invalid uses of the atomic
6938 pragma if neither LHS nor RHS is type-dependent. */
6939 if (processing_template_decl)
6941 dependent_p = (type_dependent_expression_p (lhs)
6942 || (rhs && type_dependent_expression_p (rhs))
6943 || (v && type_dependent_expression_p (v))
6944 || (lhs1 && type_dependent_expression_p (lhs1))
6945 || (rhs1 && type_dependent_expression_p (rhs1)));
6946 if (!dependent_p)
6948 lhs = build_non_dependent_expr (lhs);
6949 if (rhs)
6950 rhs = build_non_dependent_expr (rhs);
6951 if (v)
6952 v = build_non_dependent_expr (v);
6953 if (lhs1)
6954 lhs1 = build_non_dependent_expr (lhs1);
6955 if (rhs1)
6956 rhs1 = build_non_dependent_expr (rhs1);
6959 if (!dependent_p)
6961 bool swapped = false;
6962 if (rhs1 && cp_tree_equal (lhs, rhs))
6964 tree tem = rhs;
6965 rhs = rhs1;
6966 rhs1 = tem;
6967 swapped = !commutative_tree_code (opcode);
6969 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6971 if (code == OMP_ATOMIC)
6972 error ("%<#pragma omp atomic update%> uses two different "
6973 "expressions for memory");
6974 else
6975 error ("%<#pragma omp atomic capture%> uses two different "
6976 "expressions for memory");
6977 return;
6979 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6981 if (code == OMP_ATOMIC)
6982 error ("%<#pragma omp atomic update%> uses two different "
6983 "expressions for memory");
6984 else
6985 error ("%<#pragma omp atomic capture%> uses two different "
6986 "expressions for memory");
6987 return;
6989 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6990 v, lhs1, rhs1, swapped, seq_cst);
6991 if (stmt == error_mark_node)
6992 return;
6994 if (processing_template_decl)
6996 if (code == OMP_ATOMIC_READ)
6998 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6999 OMP_ATOMIC_READ, orig_lhs);
7000 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7001 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
7003 else
7005 if (opcode == NOP_EXPR)
7006 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
7007 else
7008 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
7009 if (orig_rhs1)
7010 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
7011 COMPOUND_EXPR, orig_rhs1, stmt);
7012 if (code != OMP_ATOMIC)
7014 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
7015 code, orig_lhs1, stmt);
7016 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7017 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
7020 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
7021 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
7023 finish_expr_stmt (stmt);
7026 void
7027 finish_omp_barrier (void)
7029 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
7030 vec<tree, va_gc> *vec = make_tree_vector ();
7031 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7032 release_tree_vector (vec);
7033 finish_expr_stmt (stmt);
7036 void
7037 finish_omp_flush (void)
7039 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
7040 vec<tree, va_gc> *vec = make_tree_vector ();
7041 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7042 release_tree_vector (vec);
7043 finish_expr_stmt (stmt);
7046 void
7047 finish_omp_taskwait (void)
7049 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
7050 vec<tree, va_gc> *vec = make_tree_vector ();
7051 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7052 release_tree_vector (vec);
7053 finish_expr_stmt (stmt);
7056 void
7057 finish_omp_taskyield (void)
7059 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
7060 vec<tree, va_gc> *vec = make_tree_vector ();
7061 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7062 release_tree_vector (vec);
7063 finish_expr_stmt (stmt);
7066 void
7067 finish_omp_cancel (tree clauses)
7069 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
7070 int mask = 0;
7071 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7072 mask = 1;
7073 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7074 mask = 2;
7075 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7076 mask = 4;
7077 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7078 mask = 8;
7079 else
7081 error ("%<#pragma omp cancel must specify one of "
7082 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7083 return;
7085 vec<tree, va_gc> *vec = make_tree_vector ();
7086 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
7087 if (ifc != NULL_TREE)
7089 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
7090 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
7091 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
7092 build_zero_cst (type));
7094 else
7095 ifc = boolean_true_node;
7096 vec->quick_push (build_int_cst (integer_type_node, mask));
7097 vec->quick_push (ifc);
7098 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7099 release_tree_vector (vec);
7100 finish_expr_stmt (stmt);
7103 void
7104 finish_omp_cancellation_point (tree clauses)
7106 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7107 int mask = 0;
7108 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7109 mask = 1;
7110 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7111 mask = 2;
7112 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7113 mask = 4;
7114 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7115 mask = 8;
7116 else
7118 error ("%<#pragma omp cancellation point must specify one of "
7119 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7120 return;
7122 vec<tree, va_gc> *vec
7123 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7124 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7125 release_tree_vector (vec);
7126 finish_expr_stmt (stmt);
7129 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7130 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7131 should create an extra compound stmt. */
7133 tree
7134 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7136 tree r;
7138 if (pcompound)
7139 *pcompound = begin_compound_stmt (0);
7141 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7143 /* Only add the statement to the function if support enabled. */
7144 if (flag_tm)
7145 add_stmt (r);
7146 else
7147 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7148 ? G_("%<__transaction_relaxed%> without "
7149 "transactional memory support enabled")
7150 : G_("%<__transaction_atomic%> without "
7151 "transactional memory support enabled")));
7153 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7154 TREE_SIDE_EFFECTS (r) = 1;
7155 return r;
7158 /* End a __transaction_atomic or __transaction_relaxed statement.
7159 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7160 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7161 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7163 void
7164 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7166 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7167 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7168 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7169 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7171 /* noexcept specifications are not allowed for function transactions. */
7172 gcc_assert (!(noex && compound_stmt));
7173 if (noex)
7175 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7176 noex);
7177 /* This may not be true when the STATEMENT_LIST is empty. */
7178 if (EXPR_P (body))
7179 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7180 TREE_SIDE_EFFECTS (body) = 1;
7181 TRANSACTION_EXPR_BODY (stmt) = body;
7184 if (compound_stmt)
7185 finish_compound_stmt (compound_stmt);
7188 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7189 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7190 condition. */
7192 tree
7193 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7195 tree ret;
7196 if (noex)
7198 expr = build_must_not_throw_expr (expr, noex);
7199 if (EXPR_P (expr))
7200 SET_EXPR_LOCATION (expr, loc);
7201 TREE_SIDE_EFFECTS (expr) = 1;
7203 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7204 if (flags & TM_STMT_ATTR_RELAXED)
7205 TRANSACTION_EXPR_RELAXED (ret) = 1;
7206 TREE_SIDE_EFFECTS (ret) = 1;
7207 SET_EXPR_LOCATION (ret, loc);
7208 return ret;
7211 void
7212 init_cp_semantics (void)
7216 /* Build a STATIC_ASSERT for a static assertion with the condition
7217 CONDITION and the message text MESSAGE. LOCATION is the location
7218 of the static assertion in the source code. When MEMBER_P, this
7219 static assertion is a member of a class. */
7220 void
7221 finish_static_assert (tree condition, tree message, location_t location,
7222 bool member_p)
7224 if (message == NULL_TREE
7225 || message == error_mark_node
7226 || condition == NULL_TREE
7227 || condition == error_mark_node)
7228 return;
7230 if (check_for_bare_parameter_packs (condition))
7231 condition = error_mark_node;
7233 if (type_dependent_expression_p (condition)
7234 || value_dependent_expression_p (condition))
7236 /* We're in a template; build a STATIC_ASSERT and put it in
7237 the right place. */
7238 tree assertion;
7240 assertion = make_node (STATIC_ASSERT);
7241 STATIC_ASSERT_CONDITION (assertion) = condition;
7242 STATIC_ASSERT_MESSAGE (assertion) = message;
7243 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7245 if (member_p)
7246 maybe_add_class_template_decl_list (current_class_type,
7247 assertion,
7248 /*friend_p=*/0);
7249 else
7250 add_stmt (assertion);
7252 return;
7255 /* Fold the expression and convert it to a boolean value. */
7256 condition = instantiate_non_dependent_expr (condition);
7257 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7258 condition = maybe_constant_value (condition);
7260 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7261 /* Do nothing; the condition is satisfied. */
7263 else
7265 location_t saved_loc = input_location;
7267 input_location = location;
7268 if (TREE_CODE (condition) == INTEGER_CST
7269 && integer_zerop (condition))
7270 /* Report the error. */
7271 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7272 else if (condition && condition != error_mark_node)
7274 error ("non-constant condition for static assertion");
7275 if (require_potential_rvalue_constant_expression (condition))
7276 cxx_constant_value (condition);
7278 input_location = saved_loc;
7282 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7283 suitable for use as a type-specifier.
7285 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7286 id-expression or a class member access, FALSE when it was parsed as
7287 a full expression. */
7289 tree
7290 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7291 tsubst_flags_t complain)
7293 tree type = NULL_TREE;
7295 if (!expr || error_operand_p (expr))
7296 return error_mark_node;
7298 if (TYPE_P (expr)
7299 || TREE_CODE (expr) == TYPE_DECL
7300 || (TREE_CODE (expr) == BIT_NOT_EXPR
7301 && TYPE_P (TREE_OPERAND (expr, 0))))
7303 if (complain & tf_error)
7304 error ("argument to decltype must be an expression");
7305 return error_mark_node;
7308 /* Depending on the resolution of DR 1172, we may later need to distinguish
7309 instantiation-dependent but not type-dependent expressions so that, say,
7310 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7311 if (instantiation_dependent_expression_p (expr))
7313 type = cxx_make_type (DECLTYPE_TYPE);
7314 DECLTYPE_TYPE_EXPR (type) = expr;
7315 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7316 = id_expression_or_member_access_p;
7317 SET_TYPE_STRUCTURAL_EQUALITY (type);
7319 return type;
7323 /* The type denoted by decltype(e) is defined as follows: */
7325 expr = resolve_nondeduced_context (expr);
7327 if (invalid_nonstatic_memfn_p (expr, complain))
7328 return error_mark_node;
7330 if (type_unknown_p (expr))
7332 if (complain & tf_error)
7333 error ("decltype cannot resolve address of overloaded function");
7334 return error_mark_node;
7337 /* To get the size of a static data member declared as an array of
7338 unknown bound, we need to instantiate it. */
7339 if (VAR_P (expr)
7340 && VAR_HAD_UNKNOWN_BOUND (expr)
7341 && DECL_TEMPLATE_INSTANTIATION (expr))
7342 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7344 if (id_expression_or_member_access_p)
7346 /* If e is an id-expression or a class member access (5.2.5
7347 [expr.ref]), decltype(e) is defined as the type of the entity
7348 named by e. If there is no such entity, or e names a set of
7349 overloaded functions, the program is ill-formed. */
7350 if (identifier_p (expr))
7351 expr = lookup_name (expr);
7353 if (INDIRECT_REF_P (expr))
7354 /* This can happen when the expression is, e.g., "a.b". Just
7355 look at the underlying operand. */
7356 expr = TREE_OPERAND (expr, 0);
7358 if (TREE_CODE (expr) == OFFSET_REF
7359 || TREE_CODE (expr) == MEMBER_REF
7360 || TREE_CODE (expr) == SCOPE_REF)
7361 /* We're only interested in the field itself. If it is a
7362 BASELINK, we will need to see through it in the next
7363 step. */
7364 expr = TREE_OPERAND (expr, 1);
7366 if (BASELINK_P (expr))
7367 /* See through BASELINK nodes to the underlying function. */
7368 expr = BASELINK_FUNCTIONS (expr);
7370 switch (TREE_CODE (expr))
7372 case FIELD_DECL:
7373 if (DECL_BIT_FIELD_TYPE (expr))
7375 type = DECL_BIT_FIELD_TYPE (expr);
7376 break;
7378 /* Fall through for fields that aren't bitfields. */
7380 case FUNCTION_DECL:
7381 case VAR_DECL:
7382 case CONST_DECL:
7383 case PARM_DECL:
7384 case RESULT_DECL:
7385 case TEMPLATE_PARM_INDEX:
7386 expr = mark_type_use (expr);
7387 type = TREE_TYPE (expr);
7388 break;
7390 case ERROR_MARK:
7391 type = error_mark_node;
7392 break;
7394 case COMPONENT_REF:
7395 case COMPOUND_EXPR:
7396 mark_type_use (expr);
7397 type = is_bitfield_expr_with_lowered_type (expr);
7398 if (!type)
7399 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7400 break;
7402 case BIT_FIELD_REF:
7403 gcc_unreachable ();
7405 case INTEGER_CST:
7406 case PTRMEM_CST:
7407 /* We can get here when the id-expression refers to an
7408 enumerator or non-type template parameter. */
7409 type = TREE_TYPE (expr);
7410 break;
7412 default:
7413 /* Handle instantiated template non-type arguments. */
7414 type = TREE_TYPE (expr);
7415 break;
7418 else
7420 /* Within a lambda-expression:
7422 Every occurrence of decltype((x)) where x is a possibly
7423 parenthesized id-expression that names an entity of
7424 automatic storage duration is treated as if x were
7425 transformed into an access to a corresponding data member
7426 of the closure type that would have been declared if x
7427 were a use of the denoted entity. */
7428 if (outer_automatic_var_p (expr)
7429 && current_function_decl
7430 && LAMBDA_FUNCTION_P (current_function_decl))
7431 type = capture_decltype (expr);
7432 else if (error_operand_p (expr))
7433 type = error_mark_node;
7434 else if (expr == current_class_ptr)
7435 /* If the expression is just "this", we want the
7436 cv-unqualified pointer for the "this" type. */
7437 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7438 else
7440 /* Otherwise, where T is the type of e, if e is an lvalue,
7441 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7442 cp_lvalue_kind clk = lvalue_kind (expr);
7443 type = unlowered_expr_type (expr);
7444 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7446 /* For vector types, pick a non-opaque variant. */
7447 if (TREE_CODE (type) == VECTOR_TYPE)
7448 type = strip_typedefs (type);
7450 if (clk != clk_none && !(clk & clk_class))
7451 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7455 return type;
7458 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7459 __has_nothrow_copy, depending on assign_p. */
7461 static bool
7462 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7464 tree fns;
7466 if (assign_p)
7468 int ix;
7469 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7470 if (ix < 0)
7471 return false;
7472 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7474 else if (TYPE_HAS_COPY_CTOR (type))
7476 /* If construction of the copy constructor was postponed, create
7477 it now. */
7478 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7479 lazily_declare_fn (sfk_copy_constructor, type);
7480 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7481 lazily_declare_fn (sfk_move_constructor, type);
7482 fns = CLASSTYPE_CONSTRUCTORS (type);
7484 else
7485 return false;
7487 for (; fns; fns = OVL_NEXT (fns))
7489 tree fn = OVL_CURRENT (fns);
7491 if (assign_p)
7493 if (copy_fn_p (fn) == 0)
7494 continue;
7496 else if (copy_fn_p (fn) <= 0)
7497 continue;
7499 maybe_instantiate_noexcept (fn);
7500 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7501 return false;
7504 return true;
7507 // Returns true if K denotes a unary type trait.
7508 inline bool
7509 is_unary_trait (cp_trait_kind k)
7511 if (k == CPTK_IS_CONVERTIBLE_TO || k == CPTK_IS_BASE_OF)
7512 return false;
7513 return true;
7516 // Returns true if K denotes a binary type trait.
7517 bool
7518 is_binary_trait (cp_trait_kind k)
7520 return !is_unary_trait (k);
7523 // Returns a type for T that can be used as an xvalue. For function
7524 // types, this returns an rvalue reference to T. For all other types,
7525 // this simply returns T.
7526 tree
7527 xvalue_result_type (tree t)
7529 if (TREE_CODE (t) == FUNCTION_TYPE)
7530 return cp_build_reference_type(t, true);
7531 else
7532 return t;
7535 /* Actually evaluates the trait. */
7537 static bool
7538 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7540 enum tree_code type_code1;
7541 tree t;
7543 type_code1 = TREE_CODE (type1);
7545 switch (kind)
7547 case CPTK_HAS_NOTHROW_ASSIGN:
7548 type1 = strip_array_types (type1);
7549 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7550 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7551 || (CLASS_TYPE_P (type1)
7552 && classtype_has_nothrow_assign_or_copy_p (type1,
7553 true))));
7555 case CPTK_HAS_TRIVIAL_ASSIGN:
7556 /* ??? The standard seems to be missing the "or array of such a class
7557 type" wording for this trait. */
7558 type1 = strip_array_types (type1);
7559 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7560 && (trivial_type_p (type1)
7561 || (CLASS_TYPE_P (type1)
7562 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7564 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7565 type1 = strip_array_types (type1);
7566 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7567 || (CLASS_TYPE_P (type1)
7568 && (t = locate_ctor (type1))
7569 && (maybe_instantiate_noexcept (t),
7570 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7572 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7573 type1 = strip_array_types (type1);
7574 return (trivial_type_p (type1)
7575 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7577 case CPTK_HAS_NOTHROW_COPY:
7578 type1 = strip_array_types (type1);
7579 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7580 || (CLASS_TYPE_P (type1)
7581 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7583 case CPTK_HAS_TRIVIAL_COPY:
7584 /* ??? The standard seems to be missing the "or array of such a class
7585 type" wording for this trait. */
7586 type1 = strip_array_types (type1);
7587 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7588 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7590 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7591 type1 = strip_array_types (type1);
7592 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7593 || (CLASS_TYPE_P (type1)
7594 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7596 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7597 return type_has_virtual_destructor (type1);
7599 case CPTK_IS_ABSTRACT:
7600 return (ABSTRACT_CLASS_TYPE_P (type1));
7602 case CPTK_IS_BASE_OF:
7603 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7604 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7605 || DERIVED_FROM_P (type1, type2)));
7607 case CPTK_IS_CLASS:
7608 return (NON_UNION_CLASS_TYPE_P (type1));
7610 case CPTK_IS_CONVERTIBLE_TO:
7611 return can_convert (type2, xvalue_result_type (type1), tf_none);
7613 case CPTK_IS_EMPTY:
7614 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7616 case CPTK_IS_ENUM:
7617 return (type_code1 == ENUMERAL_TYPE);
7619 case CPTK_IS_FINAL:
7620 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7622 case CPTK_IS_LITERAL_TYPE:
7623 return (literal_type_p (type1));
7625 case CPTK_IS_POD:
7626 return (pod_type_p (type1));
7628 case CPTK_IS_POLYMORPHIC:
7629 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7631 case CPTK_IS_SAME_AS:
7632 return same_type_p (type1, type2);
7634 case CPTK_IS_STD_LAYOUT:
7635 return (std_layout_type_p (type1));
7637 case CPTK_IS_TRIVIAL:
7638 return (trivial_type_p (type1));
7640 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7641 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7643 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7644 return is_trivially_xible (INIT_EXPR, type1, type2);
7646 case CPTK_IS_TRIVIALLY_COPYABLE:
7647 return (trivially_copyable_p (type1));
7649 case CPTK_IS_UNION:
7650 return (type_code1 == UNION_TYPE);
7652 default:
7653 gcc_unreachable ();
7654 return false;
7658 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7659 void, or a complete type, returns true, otherwise false. */
7661 static bool
7662 check_trait_type (tree type)
7664 if (type == NULL_TREE)
7665 return true;
7667 if (TREE_CODE (type) == TREE_LIST)
7668 return (check_trait_type (TREE_VALUE (type))
7669 && check_trait_type (TREE_CHAIN (type)));
7671 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7672 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7673 return true;
7675 if (VOID_TYPE_P (type))
7676 return true;
7678 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7681 /* Process a trait expression. */
7683 tree
7684 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7686 if (type1 == error_mark_node
7687 || type2 == error_mark_node)
7688 return error_mark_node;
7690 if (processing_template_decl)
7692 tree trait_expr = make_node (TRAIT_EXPR);
7693 TREE_TYPE (trait_expr) = boolean_type_node;
7694 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7695 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7696 TRAIT_EXPR_KIND (trait_expr) = kind;
7697 return trait_expr;
7700 switch (kind)
7702 case CPTK_HAS_NOTHROW_ASSIGN:
7703 case CPTK_HAS_TRIVIAL_ASSIGN:
7704 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7705 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7706 case CPTK_HAS_NOTHROW_COPY:
7707 case CPTK_HAS_TRIVIAL_COPY:
7708 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7709 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7710 case CPTK_IS_ABSTRACT:
7711 case CPTK_IS_EMPTY:
7712 case CPTK_IS_FINAL:
7713 case CPTK_IS_LITERAL_TYPE:
7714 case CPTK_IS_POD:
7715 case CPTK_IS_POLYMORPHIC:
7716 case CPTK_IS_STD_LAYOUT:
7717 case CPTK_IS_TRIVIAL:
7718 case CPTK_IS_TRIVIALLY_COPYABLE:
7719 if (!check_trait_type (type1))
7720 return error_mark_node;
7721 break;
7723 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7724 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7725 if (!check_trait_type (type1)
7726 || !check_trait_type (type2))
7727 return error_mark_node;
7728 break;
7730 case CPTK_IS_BASE_OF:
7731 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7732 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7733 && !complete_type_or_else (type2, NULL_TREE))
7734 /* We already issued an error. */
7735 return error_mark_node;
7736 break;
7738 case CPTK_IS_CLASS:
7739 case CPTK_IS_ENUM:
7740 case CPTK_IS_UNION:
7741 case CPTK_IS_SAME_AS:
7742 break;
7744 case CPTK_IS_CONVERTIBLE_TO:
7745 if (!check_trait_type (type1))
7746 return error_mark_node;
7747 if (!check_trait_type (type2))
7748 return error_mark_node;
7749 break;
7751 default:
7752 gcc_unreachable ();
7755 return (trait_expr_value (kind, type1, type2)
7756 ? boolean_true_node : boolean_false_node);
7759 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7760 which is ignored for C++. */
7762 void
7763 set_float_const_decimal64 (void)
7767 void
7768 clear_float_const_decimal64 (void)
7772 bool
7773 float_const_decimal64_p (void)
7775 return 0;
7779 /* Return true if T designates the implied `this' parameter. */
7781 bool
7782 is_this_parameter (tree t)
7784 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7785 return false;
7786 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7787 return true;
7790 /* Insert the deduced return type for an auto function. */
7792 void
7793 apply_deduced_return_type (tree fco, tree return_type)
7795 tree result;
7797 if (return_type == error_mark_node)
7798 return;
7800 if (LAMBDA_FUNCTION_P (fco))
7802 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7803 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7806 if (DECL_CONV_FN_P (fco))
7807 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7809 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7811 result = DECL_RESULT (fco);
7812 if (result == NULL_TREE)
7813 return;
7814 if (TREE_TYPE (result) == return_type)
7815 return;
7817 /* We already have a DECL_RESULT from start_preparsed_function.
7818 Now we need to redo the work it and allocate_struct_function
7819 did to reflect the new type. */
7820 gcc_assert (current_function_decl == fco);
7821 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7822 TYPE_MAIN_VARIANT (return_type));
7823 DECL_ARTIFICIAL (result) = 1;
7824 DECL_IGNORED_P (result) = 1;
7825 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7826 result);
7828 DECL_RESULT (fco) = result;
7830 if (!processing_template_decl)
7832 if (!VOID_TYPE_P (TREE_TYPE (result)))
7833 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7834 bool aggr = aggregate_value_p (result, fco);
7835 #ifdef PCC_STATIC_STRUCT_RETURN
7836 cfun->returns_pcc_struct = aggr;
7837 #endif
7838 cfun->returns_struct = aggr;
7843 /* DECL is a local variable or parameter from the surrounding scope of a
7844 lambda-expression. Returns the decltype for a use of the capture field
7845 for DECL even if it hasn't been captured yet. */
7847 static tree
7848 capture_decltype (tree decl)
7850 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7851 /* FIXME do lookup instead of list walk? */
7852 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7853 tree type;
7855 if (cap)
7856 type = TREE_TYPE (TREE_PURPOSE (cap));
7857 else
7858 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7860 case CPLD_NONE:
7861 error ("%qD is not captured", decl);
7862 return error_mark_node;
7864 case CPLD_COPY:
7865 type = TREE_TYPE (decl);
7866 if (TREE_CODE (type) == REFERENCE_TYPE
7867 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7868 type = TREE_TYPE (type);
7869 break;
7871 case CPLD_REFERENCE:
7872 type = TREE_TYPE (decl);
7873 if (TREE_CODE (type) != REFERENCE_TYPE)
7874 type = build_reference_type (TREE_TYPE (decl));
7875 break;
7877 default:
7878 gcc_unreachable ();
7881 if (TREE_CODE (type) != REFERENCE_TYPE)
7883 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7884 type = cp_build_qualified_type (type, (cp_type_quals (type)
7885 |TYPE_QUAL_CONST));
7886 type = build_reference_type (type);
7888 return type;
7891 #include "gt-cp-semantics.h"