re PR c++/88976 (ICE in fold_convert_loc, at fold-const.c:2552)
[gcc.git] / gcc / cp / semantics.c
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.
5
6 Copyright (C) 1998-2019 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
9
10 This file is part of GCC.
11
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.
16
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.
21
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/>. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
47 #include "memmodel.h"
48
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. */
53
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
57
58 /* Used for OpenMP non-static data member privatization. */
59
60 static hash_map<tree, tree> *omp_private_member_map;
61 static vec<tree> omp_private_member_vec;
62 static bool omp_private_member_ignore_next;
63
64
65 /* Deferred Access Checking Overview
66 ---------------------------------
67
68 Most C++ expressions and declarations require access checking
69 to be performed during parsing. However, in several cases,
70 this has to be treated differently.
71
72 For member declarations, access checking has to be deferred
73 until more information about the declaration is known. For
74 example:
75
76 class A {
77 typedef int X;
78 public:
79 X f();
80 };
81
82 A::X A::f();
83 A::X g();
84
85 When we are parsing the function return type `A::X', we don't
86 really know if this is allowed until we parse the function name.
87
88 Furthermore, some contexts require that access checking is
89 never performed at all. These include class heads, and template
90 instantiations.
91
92 Typical use of access checking functions is described here:
93
94 1. When we enter a context that requires certain access checking
95 mode, the function `push_deferring_access_checks' is called with
96 DEFERRING argument specifying the desired mode. Access checking
97 may be performed immediately (dk_no_deferred), deferred
98 (dk_deferred), or not performed (dk_no_check).
99
100 2. When a declaration such as a type, or a variable, is encountered,
101 the function `perform_or_defer_access_check' is called. It
102 maintains a vector of all deferred checks.
103
104 3. The global `current_class_type' or `current_function_decl' is then
105 setup by the parser. `enforce_access' relies on these information
106 to check access.
107
108 4. Upon exiting the context mentioned in step 1,
109 `perform_deferred_access_checks' is called to check all declaration
110 stored in the vector. `pop_deferring_access_checks' is then
111 called to restore the previous access checking mode.
112
113 In case of parsing error, we simply call `pop_deferring_access_checks'
114 without `perform_deferred_access_checks'. */
115
116 struct GTY(()) deferred_access {
117 /* A vector representing name-lookups for which we have deferred
118 checking access controls. We cannot check the accessibility of
119 names used in a decl-specifier-seq until we know what is being
120 declared because code like:
121
122 class A {
123 class B {};
124 B* f();
125 }
126
127 A::B* A::f() { return 0; }
128
129 is valid, even though `A::B' is not generally accessible. */
130 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131
132 /* The current mode of access checks. */
133 enum deferring_kind deferring_access_checks_kind;
134
135 };
136
137 /* Data for deferred access checking. */
138 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
139 static GTY(()) unsigned deferred_access_no_check;
140
141 /* Save the current deferred access states and start deferred
142 access checking iff DEFER_P is true. */
143
144 void
145 push_deferring_access_checks (deferring_kind deferring)
146 {
147 /* For context like template instantiation, access checking
148 disabling applies to all nested context. */
149 if (deferred_access_no_check || deferring == dk_no_check)
150 deferred_access_no_check++;
151 else
152 {
153 deferred_access e = {NULL, deferring};
154 vec_safe_push (deferred_access_stack, e);
155 }
156 }
157
158 /* Save the current deferred access states and start deferred access
159 checking, continuing the set of deferred checks in CHECKS. */
160
161 void
162 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 {
164 push_deferring_access_checks (dk_deferred);
165 if (!deferred_access_no_check)
166 deferred_access_stack->last().deferred_access_checks = checks;
167 }
168
169 /* Resume deferring access checks again after we stopped doing
170 this previously. */
171
172 void
173 resume_deferring_access_checks (void)
174 {
175 if (!deferred_access_no_check)
176 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
177 }
178
179 /* Stop deferring access checks. */
180
181 void
182 stop_deferring_access_checks (void)
183 {
184 if (!deferred_access_no_check)
185 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
186 }
187
188 /* Discard the current deferred access checks and restore the
189 previous states. */
190
191 void
192 pop_deferring_access_checks (void)
193 {
194 if (deferred_access_no_check)
195 deferred_access_no_check--;
196 else
197 deferred_access_stack->pop ();
198 }
199
200 /* Returns a TREE_LIST representing the deferred checks.
201 The TREE_PURPOSE of each node is the type through which the
202 access occurred; the TREE_VALUE is the declaration named.
203 */
204
205 vec<deferred_access_check, va_gc> *
206 get_deferred_access_checks (void)
207 {
208 if (deferred_access_no_check)
209 return NULL;
210 else
211 return (deferred_access_stack->last().deferred_access_checks);
212 }
213
214 /* Take current deferred checks and combine with the
215 previous states if we also defer checks previously.
216 Otherwise perform checks now. */
217
218 void
219 pop_to_parent_deferring_access_checks (void)
220 {
221 if (deferred_access_no_check)
222 deferred_access_no_check--;
223 else
224 {
225 vec<deferred_access_check, va_gc> *checks;
226 deferred_access *ptr;
227
228 checks = (deferred_access_stack->last ().deferred_access_checks);
229
230 deferred_access_stack->pop ();
231 ptr = &deferred_access_stack->last ();
232 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 {
234 /* Check access. */
235 perform_access_checks (checks, tf_warning_or_error);
236 }
237 else
238 {
239 /* Merge with parent. */
240 int i, j;
241 deferred_access_check *chk, *probe;
242
243 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 {
245 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 {
247 if (probe->binfo == chk->binfo &&
248 probe->decl == chk->decl &&
249 probe->diag_decl == chk->diag_decl)
250 goto found;
251 }
252 /* Insert into parent's checks. */
253 vec_safe_push (ptr->deferred_access_checks, *chk);
254 found:;
255 }
256 }
257 }
258 }
259
260 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
261 is the BINFO indicating the qualifying scope used to access the
262 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
263 or we aren't in SFINAE context or all the checks succeed return TRUE,
264 otherwise FALSE. */
265
266 bool
267 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
268 tsubst_flags_t complain)
269 {
270 int i;
271 deferred_access_check *chk;
272 location_t loc = input_location;
273 bool ok = true;
274
275 if (!checks)
276 return true;
277
278 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 {
280 input_location = chk->loc;
281 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
282 }
283
284 input_location = loc;
285 return (complain & tf_error) ? true : ok;
286 }
287
288 /* Perform the deferred access checks.
289
290 After performing the checks, we still have to keep the list
291 `deferred_access_stack->deferred_access_checks' since we may want
292 to check access for them again later in a different context.
293 For example:
294
295 class A {
296 typedef int X;
297 static X a;
298 };
299 A::X A::a, x; // No error for `A::a', error for `x'
300
301 We have to perform deferred access of `A::X', first with `A::a',
302 next with `x'. Return value like perform_access_checks above. */
303
304 bool
305 perform_deferred_access_checks (tsubst_flags_t complain)
306 {
307 return perform_access_checks (get_deferred_access_checks (), complain);
308 }
309
310 /* Defer checking the accessibility of DECL, when looked up in
311 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
312 Return value like perform_access_checks above.
313 If non-NULL, report failures to AFI. */
314
315 bool
316 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
317 tsubst_flags_t complain,
318 access_failure_info *afi)
319 {
320 int i;
321 deferred_access *ptr;
322 deferred_access_check *chk;
323
324
325 /* Exit if we are in a context that no access checking is performed.
326 */
327 if (deferred_access_no_check)
328 return true;
329
330 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331
332 ptr = &deferred_access_stack->last ();
333
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 {
337 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
338 return (complain & tf_error) ? true : ok;
339 }
340
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 {
344 if (chk->decl == decl && chk->binfo == binfo &&
345 chk->diag_decl == diag_decl)
346 {
347 return true;
348 }
349 }
350 /* If not, record the check. */
351 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
352 vec_safe_push (ptr->deferred_access_checks, new_access);
353
354 return true;
355 }
356
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
360
361 int
362 stmts_are_full_exprs_p (void)
363 {
364 return current_stmt_tree ()->stmts_are_full_exprs_p;
365 }
366
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
369 this function. */
370
371 tree
372 add_stmt (tree t)
373 {
374 enum tree_code code = TREE_CODE (t);
375
376 if (EXPR_P (t) && code != LABEL_EXPR)
377 {
378 if (!EXPR_HAS_LOCATION (t))
379 SET_EXPR_LOCATION (t, input_location);
380
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
384 }
385
386 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
387 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
393
394 return t;
395 }
396
397 /* Returns the stmt_tree to which statements are currently being added. */
398
399 stmt_tree
400 current_stmt_tree (void)
401 {
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
405 }
406
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408
409 static tree
410 maybe_cleanup_point_expr (tree expr)
411 {
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
415 }
416
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
421
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
424 {
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
428 }
429
430
431
432 /* Create a declaration statement for the declaration given by the DECL. */
433
434 void
435 add_decl_expr (tree decl)
436 {
437 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
442 }
443
444 /* Finish a scope. */
445
446 tree
447 do_poplevel (tree stmt_list)
448 {
449 tree block = NULL;
450
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
453
454 stmt_list = pop_stmt_list (stmt_list);
455
456 if (!processing_template_decl)
457 {
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
460 }
461
462 return stmt_list;
463 }
464
465 /* Begin a new scope. */
466
467 static tree
468 do_pushlevel (scope_kind sk)
469 {
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
474 }
475
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
479
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 {
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
487 }
488
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
495
496 static void
497 begin_maybe_infinite_loop (tree cond)
498 {
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
505 {
506 cond = fold_non_dependent_expr (cond);
507 maybe_infinite = integer_nonzerop (cond);
508 }
509 vec_safe_push (cp_function_chain->infinite_loops,
510 maybe_infinite ? error_mark_node : NULL_TREE);
511
512 }
513
514 /* A break is a possible exit for the current loop. */
515
516 void
517 break_maybe_infinite_loop (void)
518 {
519 if (!cfun)
520 return;
521 cp_function_chain->infinite_loops->last() = NULL_TREE;
522 }
523
524 /* If we reach the end of the loop without seeing a possible exit, we have
525 an infinite loop. */
526
527 static void
528 end_maybe_infinite_loop (tree cond)
529 {
530 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
531 && !processing_template_decl))
532 return;
533 tree current = cp_function_chain->infinite_loops->pop();
534 if (current != NULL_TREE)
535 {
536 cond = fold_non_dependent_expr (cond);
537 if (integer_nonzerop (cond))
538 current_function_infinite_loop = 1;
539 }
540 }
541
542
543 /* Begin a conditional that might contain a declaration. When generating
544 normal code, we want the declaration to appear before the statement
545 containing the conditional. When generating template code, we want the
546 conditional to be rendered as the raw DECL_EXPR. */
547
548 static void
549 begin_cond (tree *cond_p)
550 {
551 if (processing_template_decl)
552 *cond_p = push_stmt_list ();
553 }
554
555 /* Finish such a conditional. */
556
557 static void
558 finish_cond (tree *cond_p, tree expr)
559 {
560 if (processing_template_decl)
561 {
562 tree cond = pop_stmt_list (*cond_p);
563
564 if (expr == NULL_TREE)
565 /* Empty condition in 'for'. */
566 gcc_assert (empty_expr_stmt_p (cond));
567 else if (check_for_bare_parameter_packs (expr))
568 expr = error_mark_node;
569 else if (!empty_expr_stmt_p (cond))
570 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 }
572 *cond_p = expr;
573 }
574
575 /* If *COND_P specifies a conditional with a declaration, transform the
576 loop such that
577 while (A x = 42) { }
578 for (; A x = 42;) { }
579 becomes
580 while (true) { A x = 42; if (!x) break; }
581 for (;;) { A x = 42; if (!x) break; }
582 The statement list for BODY will be empty if the conditional did
583 not declare anything. */
584
585 static void
586 simplify_loop_decl_cond (tree *cond_p, tree body)
587 {
588 tree cond, if_stmt;
589
590 if (!TREE_SIDE_EFFECTS (body))
591 return;
592
593 cond = *cond_p;
594 *cond_p = boolean_true_node;
595
596 if_stmt = begin_if_stmt ();
597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
598 finish_if_stmt_cond (cond, if_stmt);
599 finish_break_stmt ();
600 finish_then_clause (if_stmt);
601 finish_if_stmt (if_stmt);
602 }
603
604 /* Finish a goto-statement. */
605
606 tree
607 finish_goto_stmt (tree destination)
608 {
609 if (identifier_p (destination))
610 destination = lookup_label (destination);
611
612 /* We warn about unused labels with -Wunused. That means we have to
613 mark the used labels as used. */
614 if (TREE_CODE (destination) == LABEL_DECL)
615 TREE_USED (destination) = 1;
616 else
617 {
618 destination = mark_rvalue_use (destination);
619 if (!processing_template_decl)
620 {
621 destination = cp_convert (ptr_type_node, destination,
622 tf_warning_or_error);
623 if (error_operand_p (destination))
624 return NULL_TREE;
625 destination
626 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
627 destination);
628 }
629 }
630
631 check_goto (destination);
632
633 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
634 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
635 }
636
637 /* COND is the condition-expression for an if, while, etc.,
638 statement. Convert it to a boolean value, if appropriate.
639 In addition, verify sequence points if -Wsequence-point is enabled. */
640
641 static tree
642 maybe_convert_cond (tree cond)
643 {
644 /* Empty conditions remain empty. */
645 if (!cond)
646 return NULL_TREE;
647
648 /* Wait until we instantiate templates before doing conversion. */
649 if (processing_template_decl)
650 return cond;
651
652 if (warn_sequence_point)
653 verify_sequence_points (cond);
654
655 /* Do the conversion. */
656 cond = convert_from_reference (cond);
657
658 if (TREE_CODE (cond) == MODIFY_EXPR
659 && !TREE_NO_WARNING (cond)
660 && warn_parentheses)
661 {
662 warning_at (cp_expr_loc_or_loc (cond, input_location), OPT_Wparentheses,
663 "suggest parentheses around assignment used as truth value");
664 TREE_NO_WARNING (cond) = 1;
665 }
666
667 return condition_conversion (cond);
668 }
669
670 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671
672 tree
673 finish_expr_stmt (tree expr)
674 {
675 tree r = NULL_TREE;
676 location_t loc = EXPR_LOCATION (expr);
677
678 if (expr != NULL_TREE)
679 {
680 /* If we ran into a problem, make sure we complained. */
681 gcc_assert (expr != error_mark_node || seen_error ());
682
683 if (!processing_template_decl)
684 {
685 if (warn_sequence_point)
686 verify_sequence_points (expr);
687 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 }
689 else if (!type_dependent_expression_p (expr))
690 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
691 tf_warning_or_error);
692
693 if (check_for_bare_parameter_packs (expr))
694 expr = error_mark_node;
695
696 /* Simplification of inner statement expressions, compound exprs,
697 etc can result in us already having an EXPR_STMT. */
698 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 {
700 if (TREE_CODE (expr) != EXPR_STMT)
701 expr = build_stmt (loc, EXPR_STMT, expr);
702 expr = maybe_cleanup_point_expr_void (expr);
703 }
704
705 r = add_stmt (expr);
706 }
707
708 return r;
709 }
710
711
712 /* Begin an if-statement. Returns a newly created IF_STMT if
713 appropriate. */
714
715 tree
716 begin_if_stmt (void)
717 {
718 tree r, scope;
719 scope = do_pushlevel (sk_cond);
720 r = build_stmt (input_location, IF_STMT, NULL_TREE,
721 NULL_TREE, NULL_TREE, scope);
722 current_binding_level->this_entity = r;
723 begin_cond (&IF_COND (r));
724 return r;
725 }
726
727 /* Process the COND of an if-statement, which may be given by
728 IF_STMT. */
729
730 tree
731 finish_if_stmt_cond (tree cond, tree if_stmt)
732 {
733 cond = maybe_convert_cond (cond);
734 if (IF_STMT_CONSTEXPR_P (if_stmt)
735 && !type_dependent_expression_p (cond)
736 && require_constant_expression (cond)
737 && !instantiation_dependent_expression_p (cond)
738 /* Wait until instantiation time, since only then COND has been
739 converted to bool. */
740 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
741 {
742 cond = instantiate_non_dependent_expr (cond);
743 cond = cxx_constant_value (cond, NULL_TREE);
744 }
745 finish_cond (&IF_COND (if_stmt), cond);
746 add_stmt (if_stmt);
747 THEN_CLAUSE (if_stmt) = push_stmt_list ();
748 return cond;
749 }
750
751 /* Finish the then-clause of an if-statement, which may be given by
752 IF_STMT. */
753
754 tree
755 finish_then_clause (tree if_stmt)
756 {
757 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
758 return if_stmt;
759 }
760
761 /* Begin the else-clause of an if-statement. */
762
763 void
764 begin_else_clause (tree if_stmt)
765 {
766 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
767 }
768
769 /* Finish the else-clause of an if-statement, which may be given by
770 IF_STMT. */
771
772 void
773 finish_else_clause (tree if_stmt)
774 {
775 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
776 }
777
778 /* Finish an if-statement. */
779
780 void
781 finish_if_stmt (tree if_stmt)
782 {
783 tree scope = IF_SCOPE (if_stmt);
784 IF_SCOPE (if_stmt) = NULL;
785 add_stmt (do_poplevel (scope));
786 }
787
788 /* Begin a while-statement. Returns a newly created WHILE_STMT if
789 appropriate. */
790
791 tree
792 begin_while_stmt (void)
793 {
794 tree r;
795 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
796 add_stmt (r);
797 WHILE_BODY (r) = do_pushlevel (sk_block);
798 begin_cond (&WHILE_COND (r));
799 return r;
800 }
801
802 /* Process the COND of a while-statement, which may be given by
803 WHILE_STMT. */
804
805 void
806 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
807 unsigned short unroll)
808 {
809 cond = maybe_convert_cond (cond);
810 finish_cond (&WHILE_COND (while_stmt), cond);
811 begin_maybe_infinite_loop (cond);
812 if (ivdep && cond != error_mark_node)
813 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
814 TREE_TYPE (WHILE_COND (while_stmt)),
815 WHILE_COND (while_stmt),
816 build_int_cst (integer_type_node,
817 annot_expr_ivdep_kind),
818 integer_zero_node);
819 if (unroll && cond != error_mark_node)
820 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
821 TREE_TYPE (WHILE_COND (while_stmt)),
822 WHILE_COND (while_stmt),
823 build_int_cst (integer_type_node,
824 annot_expr_unroll_kind),
825 build_int_cst (integer_type_node,
826 unroll));
827 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
828 }
829
830 /* Finish a while-statement, which may be given by WHILE_STMT. */
831
832 void
833 finish_while_stmt (tree while_stmt)
834 {
835 end_maybe_infinite_loop (boolean_true_node);
836 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
837 }
838
839 /* Begin a do-statement. Returns a newly created DO_STMT if
840 appropriate. */
841
842 tree
843 begin_do_stmt (void)
844 {
845 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
846 begin_maybe_infinite_loop (boolean_true_node);
847 add_stmt (r);
848 DO_BODY (r) = push_stmt_list ();
849 return r;
850 }
851
852 /* Finish the body of a do-statement, which may be given by DO_STMT. */
853
854 void
855 finish_do_body (tree do_stmt)
856 {
857 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
858
859 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
860 body = STATEMENT_LIST_TAIL (body)->stmt;
861
862 if (IS_EMPTY_STMT (body))
863 warning (OPT_Wempty_body,
864 "suggest explicit braces around empty body in %<do%> statement");
865 }
866
867 /* Finish a do-statement, which may be given by DO_STMT, and whose
868 COND is as indicated. */
869
870 void
871 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
872 {
873 cond = maybe_convert_cond (cond);
874 end_maybe_infinite_loop (cond);
875 if (ivdep && cond != error_mark_node)
876 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
877 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
878 integer_zero_node);
879 if (unroll && cond != error_mark_node)
880 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
881 build_int_cst (integer_type_node, annot_expr_unroll_kind),
882 build_int_cst (integer_type_node, unroll));
883 DO_COND (do_stmt) = cond;
884 }
885
886 /* Finish a return-statement. The EXPRESSION returned, if any, is as
887 indicated. */
888
889 tree
890 finish_return_stmt (tree expr)
891 {
892 tree r;
893 bool no_warning;
894
895 expr = check_return_expr (expr, &no_warning);
896
897 if (error_operand_p (expr)
898 || (flag_openmp && !check_omp_return ()))
899 {
900 /* Suppress -Wreturn-type for this function. */
901 if (warn_return_type)
902 TREE_NO_WARNING (current_function_decl) = true;
903 return error_mark_node;
904 }
905
906 if (!processing_template_decl)
907 {
908 if (warn_sequence_point)
909 verify_sequence_points (expr);
910
911 if (DECL_DESTRUCTOR_P (current_function_decl)
912 || (DECL_CONSTRUCTOR_P (current_function_decl)
913 && targetm.cxx.cdtor_returns_this ()))
914 {
915 /* Similarly, all destructors must run destructors for
916 base-classes before returning. So, all returns in a
917 destructor get sent to the DTOR_LABEL; finish_function emits
918 code to return a value there. */
919 return finish_goto_stmt (cdtor_label);
920 }
921 }
922
923 r = build_stmt (input_location, RETURN_EXPR, expr);
924 TREE_NO_WARNING (r) |= no_warning;
925 r = maybe_cleanup_point_expr_void (r);
926 r = add_stmt (r);
927
928 return r;
929 }
930
931 /* Begin the scope of a for-statement or a range-for-statement.
932 Both the returned trees are to be used in a call to
933 begin_for_stmt or begin_range_for_stmt. */
934
935 tree
936 begin_for_scope (tree *init)
937 {
938 tree scope = do_pushlevel (sk_for);
939
940 if (processing_template_decl)
941 *init = push_stmt_list ();
942 else
943 *init = NULL_TREE;
944
945 return scope;
946 }
947
948 /* Begin a for-statement. Returns a new FOR_STMT.
949 SCOPE and INIT should be the return of begin_for_scope,
950 or both NULL_TREE */
951
952 tree
953 begin_for_stmt (tree scope, tree init)
954 {
955 tree r;
956
957 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
958 NULL_TREE, NULL_TREE, NULL_TREE);
959
960 if (scope == NULL_TREE)
961 {
962 gcc_assert (!init);
963 scope = begin_for_scope (&init);
964 }
965
966 FOR_INIT_STMT (r) = init;
967 FOR_SCOPE (r) = scope;
968
969 return r;
970 }
971
972 /* Finish the init-statement of a for-statement, which may be
973 given by FOR_STMT. */
974
975 void
976 finish_init_stmt (tree for_stmt)
977 {
978 if (processing_template_decl)
979 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
980 add_stmt (for_stmt);
981 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
982 begin_cond (&FOR_COND (for_stmt));
983 }
984
985 /* Finish the COND of a for-statement, which may be given by
986 FOR_STMT. */
987
988 void
989 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
990 {
991 cond = maybe_convert_cond (cond);
992 finish_cond (&FOR_COND (for_stmt), cond);
993 begin_maybe_infinite_loop (cond);
994 if (ivdep && cond != error_mark_node)
995 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
996 TREE_TYPE (FOR_COND (for_stmt)),
997 FOR_COND (for_stmt),
998 build_int_cst (integer_type_node,
999 annot_expr_ivdep_kind),
1000 integer_zero_node);
1001 if (unroll && cond != error_mark_node)
1002 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1003 TREE_TYPE (FOR_COND (for_stmt)),
1004 FOR_COND (for_stmt),
1005 build_int_cst (integer_type_node,
1006 annot_expr_unroll_kind),
1007 build_int_cst (integer_type_node,
1008 unroll));
1009 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1010 }
1011
1012 /* Finish the increment-EXPRESSION in a for-statement, which may be
1013 given by FOR_STMT. */
1014
1015 void
1016 finish_for_expr (tree expr, tree for_stmt)
1017 {
1018 if (!expr)
1019 return;
1020 /* If EXPR is an overloaded function, issue an error; there is no
1021 context available to use to perform overload resolution. */
1022 if (type_unknown_p (expr))
1023 {
1024 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1025 expr = error_mark_node;
1026 }
1027 if (!processing_template_decl)
1028 {
1029 if (warn_sequence_point)
1030 verify_sequence_points (expr);
1031 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1032 tf_warning_or_error);
1033 }
1034 else if (!type_dependent_expression_p (expr))
1035 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1036 tf_warning_or_error);
1037 expr = maybe_cleanup_point_expr_void (expr);
1038 if (check_for_bare_parameter_packs (expr))
1039 expr = error_mark_node;
1040 FOR_EXPR (for_stmt) = expr;
1041 }
1042
1043 /* Finish the body of a for-statement, which may be given by
1044 FOR_STMT. The increment-EXPR for the loop must be
1045 provided.
1046 It can also finish RANGE_FOR_STMT. */
1047
1048 void
1049 finish_for_stmt (tree for_stmt)
1050 {
1051 end_maybe_infinite_loop (boolean_true_node);
1052
1053 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1054 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1055 else
1056 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1057
1058 /* Pop the scope for the body of the loop. */
1059 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1060 ? &RANGE_FOR_SCOPE (for_stmt)
1061 : &FOR_SCOPE (for_stmt));
1062 tree scope = *scope_ptr;
1063 *scope_ptr = NULL;
1064
1065 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1066 decl names to make those unaccessible by code in the body.
1067 Change it to ones with underscore instead of space, so that it can
1068 be inspected in the debugger. */
1069 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1070 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1071 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1072 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1073 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1074 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1075 for (int i = 0; i < 3; i++)
1076 {
1077 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1078 if (IDENTIFIER_BINDING (id)
1079 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1080 {
1081 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1082 gcc_assert (VAR_P (range_for_decl[i])
1083 && DECL_ARTIFICIAL (range_for_decl[i]));
1084 }
1085 }
1086
1087 add_stmt (do_poplevel (scope));
1088
1089 for (int i = 0; i < 3; i++)
1090 if (range_for_decl[i])
1091 DECL_NAME (range_for_decl[i])
1092 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1093 }
1094
1095 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1096 SCOPE and INIT should be the return of begin_for_scope,
1097 or both NULL_TREE .
1098 To finish it call finish_for_stmt(). */
1099
1100 tree
1101 begin_range_for_stmt (tree scope, tree init)
1102 {
1103 begin_maybe_infinite_loop (boolean_false_node);
1104
1105 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1106 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1107
1108 if (scope == NULL_TREE)
1109 {
1110 gcc_assert (!init);
1111 scope = begin_for_scope (&init);
1112 }
1113
1114 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1115 RANGE_FOR_INIT_STMT (r) = init;
1116 RANGE_FOR_SCOPE (r) = scope;
1117
1118 return r;
1119 }
1120
1121 /* Finish the head of a range-based for statement, which may
1122 be given by RANGE_FOR_STMT. DECL must be the declaration
1123 and EXPR must be the loop expression. */
1124
1125 void
1126 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1127 {
1128 if (processing_template_decl)
1129 RANGE_FOR_INIT_STMT (range_for_stmt)
1130 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1131 RANGE_FOR_DECL (range_for_stmt) = decl;
1132 RANGE_FOR_EXPR (range_for_stmt) = expr;
1133 add_stmt (range_for_stmt);
1134 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1135 }
1136
1137 /* Finish a break-statement. */
1138
1139 tree
1140 finish_break_stmt (void)
1141 {
1142 /* In switch statements break is sometimes stylistically used after
1143 a return statement. This can lead to spurious warnings about
1144 control reaching the end of a non-void function when it is
1145 inlined. Note that we are calling block_may_fallthru with
1146 language specific tree nodes; this works because
1147 block_may_fallthru returns true when given something it does not
1148 understand. */
1149 if (!block_may_fallthru (cur_stmt_list))
1150 return void_node;
1151 note_break_stmt ();
1152 return add_stmt (build_stmt (input_location, BREAK_STMT));
1153 }
1154
1155 /* Finish a continue-statement. */
1156
1157 tree
1158 finish_continue_stmt (void)
1159 {
1160 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1161 }
1162
1163 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1164 appropriate. */
1165
1166 tree
1167 begin_switch_stmt (void)
1168 {
1169 tree r, scope;
1170
1171 scope = do_pushlevel (sk_cond);
1172 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1173
1174 begin_cond (&SWITCH_STMT_COND (r));
1175
1176 return r;
1177 }
1178
1179 /* Finish the cond of a switch-statement. */
1180
1181 void
1182 finish_switch_cond (tree cond, tree switch_stmt)
1183 {
1184 tree orig_type = NULL;
1185
1186 if (!processing_template_decl)
1187 {
1188 /* Convert the condition to an integer or enumeration type. */
1189 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1190 if (cond == NULL_TREE)
1191 {
1192 error ("switch quantity not an integer");
1193 cond = error_mark_node;
1194 }
1195 /* We want unlowered type here to handle enum bit-fields. */
1196 orig_type = unlowered_expr_type (cond);
1197 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1198 orig_type = TREE_TYPE (cond);
1199 if (cond != error_mark_node)
1200 {
1201 /* [stmt.switch]
1202
1203 Integral promotions are performed. */
1204 cond = perform_integral_promotions (cond);
1205 cond = maybe_cleanup_point_expr (cond);
1206 }
1207 }
1208 if (check_for_bare_parameter_packs (cond))
1209 cond = error_mark_node;
1210 else if (!processing_template_decl && warn_sequence_point)
1211 verify_sequence_points (cond);
1212
1213 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1214 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1215 add_stmt (switch_stmt);
1216 push_switch (switch_stmt);
1217 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1218 }
1219
1220 /* Finish the body of a switch-statement, which may be given by
1221 SWITCH_STMT. The COND to switch on is indicated. */
1222
1223 void
1224 finish_switch_stmt (tree switch_stmt)
1225 {
1226 tree scope;
1227
1228 SWITCH_STMT_BODY (switch_stmt) =
1229 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1230 pop_switch ();
1231
1232 scope = SWITCH_STMT_SCOPE (switch_stmt);
1233 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1234 add_stmt (do_poplevel (scope));
1235 }
1236
1237 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1238 appropriate. */
1239
1240 tree
1241 begin_try_block (void)
1242 {
1243 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1244 add_stmt (r);
1245 TRY_STMTS (r) = push_stmt_list ();
1246 return r;
1247 }
1248
1249 /* Likewise, for a function-try-block. The block returned in
1250 *COMPOUND_STMT is an artificial outer scope, containing the
1251 function-try-block. */
1252
1253 tree
1254 begin_function_try_block (tree *compound_stmt)
1255 {
1256 tree r;
1257 /* This outer scope does not exist in the C++ standard, but we need
1258 a place to put __FUNCTION__ and similar variables. */
1259 *compound_stmt = begin_compound_stmt (0);
1260 r = begin_try_block ();
1261 FN_TRY_BLOCK_P (r) = 1;
1262 return r;
1263 }
1264
1265 /* Finish a try-block, which may be given by TRY_BLOCK. */
1266
1267 void
1268 finish_try_block (tree try_block)
1269 {
1270 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1271 TRY_HANDLERS (try_block) = push_stmt_list ();
1272 }
1273
1274 /* Finish the body of a cleanup try-block, which may be given by
1275 TRY_BLOCK. */
1276
1277 void
1278 finish_cleanup_try_block (tree try_block)
1279 {
1280 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1281 }
1282
1283 /* Finish an implicitly generated try-block, with a cleanup is given
1284 by CLEANUP. */
1285
1286 void
1287 finish_cleanup (tree cleanup, tree try_block)
1288 {
1289 TRY_HANDLERS (try_block) = cleanup;
1290 CLEANUP_P (try_block) = 1;
1291 }
1292
1293 /* Likewise, for a function-try-block. */
1294
1295 void
1296 finish_function_try_block (tree try_block)
1297 {
1298 finish_try_block (try_block);
1299 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1300 the try block, but moving it inside. */
1301 in_function_try_handler = 1;
1302 }
1303
1304 /* Finish a handler-sequence for a try-block, which may be given by
1305 TRY_BLOCK. */
1306
1307 void
1308 finish_handler_sequence (tree try_block)
1309 {
1310 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1311 check_handlers (TRY_HANDLERS (try_block));
1312 }
1313
1314 /* Finish the handler-seq for a function-try-block, given by
1315 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1316 begin_function_try_block. */
1317
1318 void
1319 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1320 {
1321 in_function_try_handler = 0;
1322 finish_handler_sequence (try_block);
1323 finish_compound_stmt (compound_stmt);
1324 }
1325
1326 /* Begin a handler. Returns a HANDLER if appropriate. */
1327
1328 tree
1329 begin_handler (void)
1330 {
1331 tree r;
1332
1333 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1334 add_stmt (r);
1335
1336 /* Create a binding level for the eh_info and the exception object
1337 cleanup. */
1338 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1339
1340 return r;
1341 }
1342
1343 /* Finish the handler-parameters for a handler, which may be given by
1344 HANDLER. DECL is the declaration for the catch parameter, or NULL
1345 if this is a `catch (...)' clause. */
1346
1347 void
1348 finish_handler_parms (tree decl, tree handler)
1349 {
1350 tree type = NULL_TREE;
1351 if (processing_template_decl)
1352 {
1353 if (decl)
1354 {
1355 decl = pushdecl (decl);
1356 decl = push_template_decl (decl);
1357 HANDLER_PARMS (handler) = decl;
1358 type = TREE_TYPE (decl);
1359 }
1360 }
1361 else
1362 {
1363 type = expand_start_catch_block (decl);
1364 if (warn_catch_value
1365 && type != NULL_TREE
1366 && type != error_mark_node
1367 && !TYPE_REF_P (TREE_TYPE (decl)))
1368 {
1369 tree orig_type = TREE_TYPE (decl);
1370 if (CLASS_TYPE_P (orig_type))
1371 {
1372 if (TYPE_POLYMORPHIC_P (orig_type))
1373 warning (OPT_Wcatch_value_,
1374 "catching polymorphic type %q#T by value", orig_type);
1375 else if (warn_catch_value > 1)
1376 warning (OPT_Wcatch_value_,
1377 "catching type %q#T by value", orig_type);
1378 }
1379 else if (warn_catch_value > 2)
1380 warning (OPT_Wcatch_value_,
1381 "catching non-reference type %q#T", orig_type);
1382 }
1383 }
1384 HANDLER_TYPE (handler) = type;
1385 }
1386
1387 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1388 the return value from the matching call to finish_handler_parms. */
1389
1390 void
1391 finish_handler (tree handler)
1392 {
1393 if (!processing_template_decl)
1394 expand_end_catch_block ();
1395 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1396 }
1397
1398 /* Begin a compound statement. FLAGS contains some bits that control the
1399 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1400 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1401 block of a function. If BCS_TRY_BLOCK is set, this is the block
1402 created on behalf of a TRY statement. Returns a token to be passed to
1403 finish_compound_stmt. */
1404
1405 tree
1406 begin_compound_stmt (unsigned int flags)
1407 {
1408 tree r;
1409
1410 if (flags & BCS_NO_SCOPE)
1411 {
1412 r = push_stmt_list ();
1413 STATEMENT_LIST_NO_SCOPE (r) = 1;
1414
1415 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1416 But, if it's a statement-expression with a scopeless block, there's
1417 nothing to keep, and we don't want to accidentally keep a block
1418 *inside* the scopeless block. */
1419 keep_next_level (false);
1420 }
1421 else
1422 {
1423 scope_kind sk = sk_block;
1424 if (flags & BCS_TRY_BLOCK)
1425 sk = sk_try;
1426 else if (flags & BCS_TRANSACTION)
1427 sk = sk_transaction;
1428 r = do_pushlevel (sk);
1429 }
1430
1431 /* When processing a template, we need to remember where the braces were,
1432 so that we can set up identical scopes when instantiating the template
1433 later. BIND_EXPR is a handy candidate for this.
1434 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1435 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1436 processing templates. */
1437 if (processing_template_decl)
1438 {
1439 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1440 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1441 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1442 TREE_SIDE_EFFECTS (r) = 1;
1443 }
1444
1445 return r;
1446 }
1447
1448 /* Finish a compound-statement, which is given by STMT. */
1449
1450 void
1451 finish_compound_stmt (tree stmt)
1452 {
1453 if (TREE_CODE (stmt) == BIND_EXPR)
1454 {
1455 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1456 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1457 discard the BIND_EXPR so it can be merged with the containing
1458 STATEMENT_LIST. */
1459 if (TREE_CODE (body) == STATEMENT_LIST
1460 && STATEMENT_LIST_HEAD (body) == NULL
1461 && !BIND_EXPR_BODY_BLOCK (stmt)
1462 && !BIND_EXPR_TRY_BLOCK (stmt))
1463 stmt = body;
1464 else
1465 BIND_EXPR_BODY (stmt) = body;
1466 }
1467 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1468 stmt = pop_stmt_list (stmt);
1469 else
1470 {
1471 /* Destroy any ObjC "super" receivers that may have been
1472 created. */
1473 objc_clear_super_receiver ();
1474
1475 stmt = do_poplevel (stmt);
1476 }
1477
1478 /* ??? See c_end_compound_stmt wrt statement expressions. */
1479 add_stmt (stmt);
1480 }
1481
1482 /* Finish an asm-statement, whose components are a STRING, some
1483 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1484 LABELS. Also note whether the asm-statement should be
1485 considered volatile, and whether it is asm inline. */
1486
1487 tree
1488 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1489 tree input_operands, tree clobbers, tree labels, bool inline_p)
1490 {
1491 tree r;
1492 tree t;
1493 int ninputs = list_length (input_operands);
1494 int noutputs = list_length (output_operands);
1495
1496 if (!processing_template_decl)
1497 {
1498 const char *constraint;
1499 const char **oconstraints;
1500 bool allows_mem, allows_reg, is_inout;
1501 tree operand;
1502 int i;
1503
1504 oconstraints = XALLOCAVEC (const char *, noutputs);
1505
1506 string = resolve_asm_operand_names (string, output_operands,
1507 input_operands, labels);
1508
1509 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1510 {
1511 operand = TREE_VALUE (t);
1512
1513 /* ??? Really, this should not be here. Users should be using a
1514 proper lvalue, dammit. But there's a long history of using
1515 casts in the output operands. In cases like longlong.h, this
1516 becomes a primitive form of typechecking -- if the cast can be
1517 removed, then the output operand had a type of the proper width;
1518 otherwise we'll get an error. Gross, but ... */
1519 STRIP_NOPS (operand);
1520
1521 operand = mark_lvalue_use (operand);
1522
1523 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1524 operand = error_mark_node;
1525
1526 if (operand != error_mark_node
1527 && (TREE_READONLY (operand)
1528 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1529 /* Functions are not modifiable, even though they are
1530 lvalues. */
1531 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1532 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1533 /* If it's an aggregate and any field is const, then it is
1534 effectively const. */
1535 || (CLASS_TYPE_P (TREE_TYPE (operand))
1536 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1537 cxx_readonly_error (input_location, operand, lv_asm);
1538
1539 tree *op = &operand;
1540 while (TREE_CODE (*op) == COMPOUND_EXPR)
1541 op = &TREE_OPERAND (*op, 1);
1542 switch (TREE_CODE (*op))
1543 {
1544 case PREINCREMENT_EXPR:
1545 case PREDECREMENT_EXPR:
1546 case MODIFY_EXPR:
1547 *op = genericize_compound_lvalue (*op);
1548 op = &TREE_OPERAND (*op, 1);
1549 break;
1550 default:
1551 break;
1552 }
1553
1554 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1555 oconstraints[i] = constraint;
1556
1557 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1558 &allows_mem, &allows_reg, &is_inout))
1559 {
1560 /* If the operand is going to end up in memory,
1561 mark it addressable. */
1562 if (!allows_reg && !cxx_mark_addressable (*op))
1563 operand = error_mark_node;
1564 }
1565 else
1566 operand = error_mark_node;
1567
1568 TREE_VALUE (t) = operand;
1569 }
1570
1571 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1572 {
1573 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1574 bool constraint_parsed
1575 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1576 oconstraints, &allows_mem, &allows_reg);
1577 /* If the operand is going to end up in memory, don't call
1578 decay_conversion. */
1579 if (constraint_parsed && !allows_reg && allows_mem)
1580 operand = mark_lvalue_use (TREE_VALUE (t));
1581 else
1582 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1583
1584 /* If the type of the operand hasn't been determined (e.g.,
1585 because it involves an overloaded function), then issue
1586 an error message. There's no context available to
1587 resolve the overloading. */
1588 if (TREE_TYPE (operand) == unknown_type_node)
1589 {
1590 error ("type of asm operand %qE could not be determined",
1591 TREE_VALUE (t));
1592 operand = error_mark_node;
1593 }
1594
1595 if (constraint_parsed)
1596 {
1597 /* If the operand is going to end up in memory,
1598 mark it addressable. */
1599 if (!allows_reg && allows_mem)
1600 {
1601 /* Strip the nops as we allow this case. FIXME, this really
1602 should be rejected or made deprecated. */
1603 STRIP_NOPS (operand);
1604
1605 tree *op = &operand;
1606 while (TREE_CODE (*op) == COMPOUND_EXPR)
1607 op = &TREE_OPERAND (*op, 1);
1608 switch (TREE_CODE (*op))
1609 {
1610 case PREINCREMENT_EXPR:
1611 case PREDECREMENT_EXPR:
1612 case MODIFY_EXPR:
1613 *op = genericize_compound_lvalue (*op);
1614 op = &TREE_OPERAND (*op, 1);
1615 break;
1616 default:
1617 break;
1618 }
1619
1620 if (!cxx_mark_addressable (*op))
1621 operand = error_mark_node;
1622 }
1623 else if (!allows_reg && !allows_mem)
1624 {
1625 /* If constraint allows neither register nor memory,
1626 try harder to get a constant. */
1627 tree constop = maybe_constant_value (operand);
1628 if (TREE_CONSTANT (constop))
1629 operand = constop;
1630 }
1631 }
1632 else
1633 operand = error_mark_node;
1634
1635 TREE_VALUE (t) = operand;
1636 }
1637 }
1638
1639 r = build_stmt (input_location, ASM_EXPR, string,
1640 output_operands, input_operands,
1641 clobbers, labels);
1642 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1643 ASM_INLINE_P (r) = inline_p;
1644 r = maybe_cleanup_point_expr_void (r);
1645 return add_stmt (r);
1646 }
1647
1648 /* Finish a label with the indicated NAME. Returns the new label. */
1649
1650 tree
1651 finish_label_stmt (tree name)
1652 {
1653 tree decl = define_label (input_location, name);
1654
1655 if (decl == error_mark_node)
1656 return error_mark_node;
1657
1658 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1659
1660 return decl;
1661 }
1662
1663 /* Finish a series of declarations for local labels. G++ allows users
1664 to declare "local" labels, i.e., labels with scope. This extension
1665 is useful when writing code involving statement-expressions. */
1666
1667 void
1668 finish_label_decl (tree name)
1669 {
1670 if (!at_function_scope_p ())
1671 {
1672 error ("__label__ declarations are only allowed in function scopes");
1673 return;
1674 }
1675
1676 add_decl_expr (declare_local_label (name));
1677 }
1678
1679 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1680
1681 void
1682 finish_decl_cleanup (tree decl, tree cleanup)
1683 {
1684 push_cleanup (decl, cleanup, false);
1685 }
1686
1687 /* If the current scope exits with an exception, run CLEANUP. */
1688
1689 void
1690 finish_eh_cleanup (tree cleanup)
1691 {
1692 push_cleanup (NULL, cleanup, true);
1693 }
1694
1695 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1696 order they were written by the user. Each node is as for
1697 emit_mem_initializers. */
1698
1699 void
1700 finish_mem_initializers (tree mem_inits)
1701 {
1702 /* Reorder the MEM_INITS so that they are in the order they appeared
1703 in the source program. */
1704 mem_inits = nreverse (mem_inits);
1705
1706 if (processing_template_decl)
1707 {
1708 tree mem;
1709
1710 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1711 {
1712 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1713 check for bare parameter packs in the TREE_VALUE, because
1714 any parameter packs in the TREE_VALUE have already been
1715 bound as part of the TREE_PURPOSE. See
1716 make_pack_expansion for more information. */
1717 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1718 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1719 TREE_VALUE (mem) = error_mark_node;
1720 }
1721
1722 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1723 CTOR_INITIALIZER, mem_inits));
1724 }
1725 else
1726 emit_mem_initializers (mem_inits);
1727 }
1728
1729 /* Obfuscate EXPR if it looks like an id-expression or member access so
1730 that the call to finish_decltype in do_auto_deduction will give the
1731 right result. */
1732
1733 tree
1734 force_paren_expr (tree expr)
1735 {
1736 /* This is only needed for decltype(auto) in C++14. */
1737 if (cxx_dialect < cxx14)
1738 return expr;
1739
1740 /* If we're in unevaluated context, we can't be deducing a
1741 return/initializer type, so we don't need to mess with this. */
1742 if (cp_unevaluated_operand)
1743 return expr;
1744
1745 if (!DECL_P (tree_strip_any_location_wrapper (expr))
1746 && TREE_CODE (expr) != COMPONENT_REF
1747 && TREE_CODE (expr) != SCOPE_REF)
1748 return expr;
1749
1750 if (TREE_CODE (expr) == COMPONENT_REF
1751 || TREE_CODE (expr) == SCOPE_REF)
1752 REF_PARENTHESIZED_P (expr) = true;
1753 else if (processing_template_decl)
1754 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1755 else
1756 {
1757 expr = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), expr);
1758 REF_PARENTHESIZED_P (expr) = true;
1759 }
1760
1761 return expr;
1762 }
1763
1764 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1765 obfuscation and return the underlying id-expression. Otherwise
1766 return T. */
1767
1768 tree
1769 maybe_undo_parenthesized_ref (tree t)
1770 {
1771 if (cxx_dialect < cxx14)
1772 return t;
1773
1774 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1775 {
1776 t = TREE_OPERAND (t, 0);
1777 while (TREE_CODE (t) == NON_LVALUE_EXPR
1778 || TREE_CODE (t) == NOP_EXPR)
1779 t = TREE_OPERAND (t, 0);
1780
1781 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1782 || TREE_CODE (t) == STATIC_CAST_EXPR);
1783 t = TREE_OPERAND (t, 0);
1784 }
1785 else if (TREE_CODE (t) == PAREN_EXPR)
1786 t = TREE_OPERAND (t, 0);
1787 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR
1788 && REF_PARENTHESIZED_P (t))
1789 t = TREE_OPERAND (t, 0);
1790
1791 return t;
1792 }
1793
1794 /* Finish a parenthesized expression EXPR. */
1795
1796 cp_expr
1797 finish_parenthesized_expr (cp_expr expr)
1798 {
1799 if (EXPR_P (expr))
1800 /* This inhibits warnings in c_common_truthvalue_conversion. */
1801 TREE_NO_WARNING (expr) = 1;
1802
1803 if (TREE_CODE (expr) == OFFSET_REF
1804 || TREE_CODE (expr) == SCOPE_REF)
1805 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1806 enclosed in parentheses. */
1807 PTRMEM_OK_P (expr) = 0;
1808
1809 tree stripped_expr = tree_strip_any_location_wrapper (expr);
1810 if (TREE_CODE (stripped_expr) == STRING_CST)
1811 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
1812
1813 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1814
1815 return expr;
1816 }
1817
1818 /* Finish a reference to a non-static data member (DECL) that is not
1819 preceded by `.' or `->'. */
1820
1821 tree
1822 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1823 {
1824 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1825 bool try_omp_private = !object && omp_private_member_map;
1826 tree ret;
1827
1828 if (!object)
1829 {
1830 tree scope = qualifying_scope;
1831 if (scope == NULL_TREE)
1832 scope = context_for_name_lookup (decl);
1833 object = maybe_dummy_object (scope, NULL);
1834 }
1835
1836 object = maybe_resolve_dummy (object, true);
1837 if (object == error_mark_node)
1838 return error_mark_node;
1839
1840 /* DR 613/850: Can use non-static data members without an associated
1841 object in sizeof/decltype/alignof. */
1842 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1843 && (!processing_template_decl || !current_class_ref))
1844 {
1845 if (current_function_decl
1846 && DECL_STATIC_FUNCTION_P (current_function_decl))
1847 error ("invalid use of member %qD in static member function", decl);
1848 else
1849 error ("invalid use of non-static data member %qD", decl);
1850 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1851
1852 return error_mark_node;
1853 }
1854
1855 if (current_class_ptr)
1856 TREE_USED (current_class_ptr) = 1;
1857 if (processing_template_decl && !qualifying_scope)
1858 {
1859 tree type = TREE_TYPE (decl);
1860
1861 if (TYPE_REF_P (type))
1862 /* Quals on the object don't matter. */;
1863 else if (PACK_EXPANSION_P (type))
1864 /* Don't bother trying to represent this. */
1865 type = NULL_TREE;
1866 else
1867 {
1868 /* Set the cv qualifiers. */
1869 int quals = cp_type_quals (TREE_TYPE (object));
1870
1871 if (DECL_MUTABLE_P (decl))
1872 quals &= ~TYPE_QUAL_CONST;
1873
1874 quals |= cp_type_quals (TREE_TYPE (decl));
1875 type = cp_build_qualified_type (type, quals);
1876 }
1877
1878 ret = (convert_from_reference
1879 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1880 }
1881 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1882 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1883 for now. */
1884 else if (processing_template_decl)
1885 ret = build_qualified_name (TREE_TYPE (decl),
1886 qualifying_scope,
1887 decl,
1888 /*template_p=*/false);
1889 else
1890 {
1891 tree access_type = TREE_TYPE (object);
1892
1893 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1894 decl, tf_warning_or_error);
1895
1896 /* If the data member was named `C::M', convert `*this' to `C'
1897 first. */
1898 if (qualifying_scope)
1899 {
1900 tree binfo = NULL_TREE;
1901 object = build_scoped_ref (object, qualifying_scope,
1902 &binfo);
1903 }
1904
1905 ret = build_class_member_access_expr (object, decl,
1906 /*access_path=*/NULL_TREE,
1907 /*preserve_reference=*/false,
1908 tf_warning_or_error);
1909 }
1910 if (try_omp_private)
1911 {
1912 tree *v = omp_private_member_map->get (decl);
1913 if (v)
1914 ret = convert_from_reference (*v);
1915 }
1916 return ret;
1917 }
1918
1919 /* If we are currently parsing a template and we encountered a typedef
1920 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1921 adds the typedef to a list tied to the current template.
1922 At template instantiation time, that list is walked and access check
1923 performed for each typedef.
1924 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1925
1926 void
1927 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1928 tree context,
1929 location_t location)
1930 {
1931 tree template_info = NULL;
1932 tree cs = current_scope ();
1933
1934 if (!is_typedef_decl (typedef_decl)
1935 || !context
1936 || !CLASS_TYPE_P (context)
1937 || !cs)
1938 return;
1939
1940 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1941 template_info = get_template_info (cs);
1942
1943 if (template_info
1944 && TI_TEMPLATE (template_info)
1945 && !currently_open_class (context))
1946 append_type_to_template_for_access_check (cs, typedef_decl,
1947 context, location);
1948 }
1949
1950 /* DECL was the declaration to which a qualified-id resolved. Issue
1951 an error message if it is not accessible. If OBJECT_TYPE is
1952 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1953 type of `*x', or `x', respectively. If the DECL was named as
1954 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1955
1956 void
1957 check_accessibility_of_qualified_id (tree decl,
1958 tree object_type,
1959 tree nested_name_specifier)
1960 {
1961 tree scope;
1962 tree qualifying_type = NULL_TREE;
1963
1964 /* If we are parsing a template declaration and if decl is a typedef,
1965 add it to a list tied to the template.
1966 At template instantiation time, that list will be walked and
1967 access check performed. */
1968 add_typedef_to_current_template_for_access_check (decl,
1969 nested_name_specifier
1970 ? nested_name_specifier
1971 : DECL_CONTEXT (decl),
1972 input_location);
1973
1974 /* If we're not checking, return immediately. */
1975 if (deferred_access_no_check)
1976 return;
1977
1978 /* Determine the SCOPE of DECL. */
1979 scope = context_for_name_lookup (decl);
1980 /* If the SCOPE is not a type, then DECL is not a member. */
1981 if (!TYPE_P (scope))
1982 return;
1983 /* Compute the scope through which DECL is being accessed. */
1984 if (object_type
1985 /* OBJECT_TYPE might not be a class type; consider:
1986
1987 class A { typedef int I; };
1988 I *p;
1989 p->A::I::~I();
1990
1991 In this case, we will have "A::I" as the DECL, but "I" as the
1992 OBJECT_TYPE. */
1993 && CLASS_TYPE_P (object_type)
1994 && DERIVED_FROM_P (scope, object_type))
1995 /* If we are processing a `->' or `.' expression, use the type of the
1996 left-hand side. */
1997 qualifying_type = object_type;
1998 else if (nested_name_specifier)
1999 {
2000 /* If the reference is to a non-static member of the
2001 current class, treat it as if it were referenced through
2002 `this'. */
2003 tree ct;
2004 if (DECL_NONSTATIC_MEMBER_P (decl)
2005 && current_class_ptr
2006 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
2007 qualifying_type = ct;
2008 /* Otherwise, use the type indicated by the
2009 nested-name-specifier. */
2010 else
2011 qualifying_type = nested_name_specifier;
2012 }
2013 else
2014 /* Otherwise, the name must be from the current class or one of
2015 its bases. */
2016 qualifying_type = currently_open_derived_class (scope);
2017
2018 if (qualifying_type
2019 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2020 or similar in a default argument value. */
2021 && CLASS_TYPE_P (qualifying_type)
2022 && !dependent_type_p (qualifying_type))
2023 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2024 decl, tf_warning_or_error);
2025 }
2026
2027 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2028 class named to the left of the "::" operator. DONE is true if this
2029 expression is a complete postfix-expression; it is false if this
2030 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2031 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2032 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2033 is true iff this qualified name appears as a template argument. */
2034
2035 tree
2036 finish_qualified_id_expr (tree qualifying_class,
2037 tree expr,
2038 bool done,
2039 bool address_p,
2040 bool template_p,
2041 bool template_arg_p,
2042 tsubst_flags_t complain)
2043 {
2044 gcc_assert (TYPE_P (qualifying_class));
2045
2046 if (error_operand_p (expr))
2047 return error_mark_node;
2048
2049 if ((DECL_P (expr) || BASELINK_P (expr))
2050 && !mark_used (expr, complain))
2051 return error_mark_node;
2052
2053 if (template_p)
2054 {
2055 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2056 {
2057 /* cp_parser_lookup_name thought we were looking for a type,
2058 but we're actually looking for a declaration. */
2059 qualifying_class = TYPE_CONTEXT (expr);
2060 expr = TYPE_IDENTIFIER (expr);
2061 }
2062 else
2063 check_template_keyword (expr);
2064 }
2065
2066 /* If EXPR occurs as the operand of '&', use special handling that
2067 permits a pointer-to-member. */
2068 if (address_p && done)
2069 {
2070 if (TREE_CODE (expr) == SCOPE_REF)
2071 expr = TREE_OPERAND (expr, 1);
2072 expr = build_offset_ref (qualifying_class, expr,
2073 /*address_p=*/true, complain);
2074 return expr;
2075 }
2076
2077 /* No need to check access within an enum. */
2078 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2079 && TREE_CODE (expr) != IDENTIFIER_NODE)
2080 return expr;
2081
2082 /* Within the scope of a class, turn references to non-static
2083 members into expression of the form "this->...". */
2084 if (template_arg_p)
2085 /* But, within a template argument, we do not want make the
2086 transformation, as there is no "this" pointer. */
2087 ;
2088 else if (TREE_CODE (expr) == FIELD_DECL)
2089 {
2090 push_deferring_access_checks (dk_no_check);
2091 expr = finish_non_static_data_member (expr, NULL_TREE,
2092 qualifying_class);
2093 pop_deferring_access_checks ();
2094 }
2095 else if (BASELINK_P (expr))
2096 {
2097 /* See if any of the functions are non-static members. */
2098 /* If so, the expression may be relative to 'this'. */
2099 if (!shared_member_p (expr)
2100 && current_class_ptr
2101 && DERIVED_FROM_P (qualifying_class,
2102 current_nonlambda_class_type ()))
2103 expr = (build_class_member_access_expr
2104 (maybe_dummy_object (qualifying_class, NULL),
2105 expr,
2106 BASELINK_ACCESS_BINFO (expr),
2107 /*preserve_reference=*/false,
2108 complain));
2109 else if (done)
2110 /* The expression is a qualified name whose address is not
2111 being taken. */
2112 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2113 complain);
2114 }
2115 else
2116 {
2117 /* In a template, return a SCOPE_REF for most qualified-ids
2118 so that we can check access at instantiation time. But if
2119 we're looking at a member of the current instantiation, we
2120 know we have access and building up the SCOPE_REF confuses
2121 non-type template argument handling. */
2122 if (processing_template_decl
2123 && (!currently_open_class (qualifying_class)
2124 || TREE_CODE (expr) == IDENTIFIER_NODE
2125 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2126 || TREE_CODE (expr) == BIT_NOT_EXPR))
2127 expr = build_qualified_name (TREE_TYPE (expr),
2128 qualifying_class, expr,
2129 template_p);
2130
2131 expr = convert_from_reference (expr);
2132 }
2133
2134 return expr;
2135 }
2136
2137 /* Begin a statement-expression. The value returned must be passed to
2138 finish_stmt_expr. */
2139
2140 tree
2141 begin_stmt_expr (void)
2142 {
2143 return push_stmt_list ();
2144 }
2145
2146 /* Process the final expression of a statement expression. EXPR can be
2147 NULL, if the final expression is empty. Return a STATEMENT_LIST
2148 containing all the statements in the statement-expression, or
2149 ERROR_MARK_NODE if there was an error. */
2150
2151 tree
2152 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2153 {
2154 if (error_operand_p (expr))
2155 {
2156 /* The type of the statement-expression is the type of the last
2157 expression. */
2158 TREE_TYPE (stmt_expr) = error_mark_node;
2159 return error_mark_node;
2160 }
2161
2162 /* If the last statement does not have "void" type, then the value
2163 of the last statement is the value of the entire expression. */
2164 if (expr)
2165 {
2166 tree type = TREE_TYPE (expr);
2167
2168 if (type && type_unknown_p (type))
2169 {
2170 error ("a statement expression is an insufficient context"
2171 " for overload resolution");
2172 TREE_TYPE (stmt_expr) = error_mark_node;
2173 return error_mark_node;
2174 }
2175 else if (processing_template_decl)
2176 {
2177 expr = build_stmt (input_location, EXPR_STMT, expr);
2178 expr = add_stmt (expr);
2179 /* Mark the last statement so that we can recognize it as such at
2180 template-instantiation time. */
2181 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2182 }
2183 else if (VOID_TYPE_P (type))
2184 {
2185 /* Just treat this like an ordinary statement. */
2186 expr = finish_expr_stmt (expr);
2187 }
2188 else
2189 {
2190 /* It actually has a value we need to deal with. First, force it
2191 to be an rvalue so that we won't need to build up a copy
2192 constructor call later when we try to assign it to something. */
2193 expr = force_rvalue (expr, tf_warning_or_error);
2194 if (error_operand_p (expr))
2195 return error_mark_node;
2196
2197 /* Update for array-to-pointer decay. */
2198 type = TREE_TYPE (expr);
2199
2200 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2201 normal statement, but don't convert to void or actually add
2202 the EXPR_STMT. */
2203 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2204 expr = maybe_cleanup_point_expr (expr);
2205 add_stmt (expr);
2206 }
2207
2208 /* The type of the statement-expression is the type of the last
2209 expression. */
2210 TREE_TYPE (stmt_expr) = type;
2211 }
2212
2213 return stmt_expr;
2214 }
2215
2216 /* Finish a statement-expression. EXPR should be the value returned
2217 by the previous begin_stmt_expr. Returns an expression
2218 representing the statement-expression. */
2219
2220 tree
2221 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2222 {
2223 tree type;
2224 tree result;
2225
2226 if (error_operand_p (stmt_expr))
2227 {
2228 pop_stmt_list (stmt_expr);
2229 return error_mark_node;
2230 }
2231
2232 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2233
2234 type = TREE_TYPE (stmt_expr);
2235 result = pop_stmt_list (stmt_expr);
2236 TREE_TYPE (result) = type;
2237
2238 if (processing_template_decl)
2239 {
2240 result = build_min (STMT_EXPR, type, result);
2241 TREE_SIDE_EFFECTS (result) = 1;
2242 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2243 }
2244 else if (CLASS_TYPE_P (type))
2245 {
2246 /* Wrap the statement-expression in a TARGET_EXPR so that the
2247 temporary object created by the final expression is destroyed at
2248 the end of the full-expression containing the
2249 statement-expression. */
2250 result = force_target_expr (type, result, tf_warning_or_error);
2251 }
2252
2253 return result;
2254 }
2255
2256 /* Returns the expression which provides the value of STMT_EXPR. */
2257
2258 tree
2259 stmt_expr_value_expr (tree stmt_expr)
2260 {
2261 tree t = STMT_EXPR_STMT (stmt_expr);
2262
2263 if (TREE_CODE (t) == BIND_EXPR)
2264 t = BIND_EXPR_BODY (t);
2265
2266 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2267 t = STATEMENT_LIST_TAIL (t)->stmt;
2268
2269 if (TREE_CODE (t) == EXPR_STMT)
2270 t = EXPR_STMT_EXPR (t);
2271
2272 return t;
2273 }
2274
2275 /* Return TRUE iff EXPR_STMT is an empty list of
2276 expression statements. */
2277
2278 bool
2279 empty_expr_stmt_p (tree expr_stmt)
2280 {
2281 tree body = NULL_TREE;
2282
2283 if (expr_stmt == void_node)
2284 return true;
2285
2286 if (expr_stmt)
2287 {
2288 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2289 body = EXPR_STMT_EXPR (expr_stmt);
2290 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2291 body = expr_stmt;
2292 }
2293
2294 if (body)
2295 {
2296 if (TREE_CODE (body) == STATEMENT_LIST)
2297 return tsi_end_p (tsi_start (body));
2298 else
2299 return empty_expr_stmt_p (body);
2300 }
2301 return false;
2302 }
2303
2304 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2305 the function (or functions) to call; ARGS are the arguments to the
2306 call. Returns the functions to be considered by overload resolution. */
2307
2308 cp_expr
2309 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2310 tsubst_flags_t complain)
2311 {
2312 tree identifier = NULL_TREE;
2313 tree functions = NULL_TREE;
2314 tree tmpl_args = NULL_TREE;
2315 bool template_id = false;
2316 location_t loc = fn_expr.get_location ();
2317 tree fn = fn_expr.get_value ();
2318
2319 STRIP_ANY_LOCATION_WRAPPER (fn);
2320
2321 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2322 {
2323 /* Use a separate flag to handle null args. */
2324 template_id = true;
2325 tmpl_args = TREE_OPERAND (fn, 1);
2326 fn = TREE_OPERAND (fn, 0);
2327 }
2328
2329 /* Find the name of the overloaded function. */
2330 if (identifier_p (fn))
2331 identifier = fn;
2332 else
2333 {
2334 functions = fn;
2335 identifier = OVL_NAME (functions);
2336 }
2337
2338 /* A call to a namespace-scope function using an unqualified name.
2339
2340 Do Koenig lookup -- unless any of the arguments are
2341 type-dependent. */
2342 if (!any_type_dependent_arguments_p (args)
2343 && !any_dependent_template_arguments_p (tmpl_args))
2344 {
2345 fn = lookup_arg_dependent (identifier, functions, args);
2346 if (!fn)
2347 {
2348 /* The unqualified name could not be resolved. */
2349 if (complain & tf_error)
2350 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2351 else
2352 fn = identifier;
2353 }
2354 }
2355
2356 if (fn && template_id && fn != error_mark_node)
2357 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2358
2359 return cp_expr (fn, loc);
2360 }
2361
2362 /* Generate an expression for `FN (ARGS)'. This may change the
2363 contents of ARGS.
2364
2365 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2366 as a virtual call, even if FN is virtual. (This flag is set when
2367 encountering an expression where the function name is explicitly
2368 qualified. For example a call to `X::f' never generates a virtual
2369 call.)
2370
2371 Returns code for the call. */
2372
2373 tree
2374 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2375 bool koenig_p, tsubst_flags_t complain)
2376 {
2377 tree result;
2378 tree orig_fn;
2379 vec<tree, va_gc> *orig_args = *args;
2380
2381 if (fn == error_mark_node)
2382 return error_mark_node;
2383
2384 gcc_assert (!TYPE_P (fn));
2385
2386 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2387 it so that we can tell this is a call to a known function. */
2388 fn = maybe_undo_parenthesized_ref (fn);
2389
2390 STRIP_ANY_LOCATION_WRAPPER (fn);
2391
2392 orig_fn = fn;
2393
2394 if (processing_template_decl)
2395 {
2396 /* If FN is a local extern declaration or set thereof, look them up
2397 again at instantiation time. */
2398 if (is_overloaded_fn (fn))
2399 {
2400 tree ifn = get_first_fn (fn);
2401 if (TREE_CODE (ifn) == FUNCTION_DECL
2402 && DECL_LOCAL_FUNCTION_P (ifn))
2403 orig_fn = DECL_NAME (ifn);
2404 }
2405
2406 /* If the call expression is dependent, build a CALL_EXPR node
2407 with no type; type_dependent_expression_p recognizes
2408 expressions with no type as being dependent. */
2409 if (type_dependent_expression_p (fn)
2410 || any_type_dependent_arguments_p (*args))
2411 {
2412 result = build_min_nt_call_vec (orig_fn, *args);
2413 SET_EXPR_LOCATION (result, cp_expr_loc_or_loc (fn, input_location));
2414 KOENIG_LOOKUP_P (result) = koenig_p;
2415 if (is_overloaded_fn (fn))
2416 fn = get_fns (fn);
2417
2418 if (cfun)
2419 {
2420 bool abnormal = true;
2421 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2422 {
2423 tree fndecl = *iter;
2424 if (TREE_CODE (fndecl) != FUNCTION_DECL
2425 || !TREE_THIS_VOLATILE (fndecl))
2426 abnormal = false;
2427 }
2428 /* FIXME: Stop warning about falling off end of non-void
2429 function. But this is wrong. Even if we only see
2430 no-return fns at this point, we could select a
2431 future-defined return fn during instantiation. Or
2432 vice-versa. */
2433 if (abnormal)
2434 current_function_returns_abnormally = 1;
2435 }
2436 return result;
2437 }
2438 orig_args = make_tree_vector_copy (*args);
2439 if (!BASELINK_P (fn)
2440 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2441 && TREE_TYPE (fn) != unknown_type_node)
2442 fn = build_non_dependent_expr (fn);
2443 make_args_non_dependent (*args);
2444 }
2445
2446 if (TREE_CODE (fn) == COMPONENT_REF)
2447 {
2448 tree member = TREE_OPERAND (fn, 1);
2449 if (BASELINK_P (member))
2450 {
2451 tree object = TREE_OPERAND (fn, 0);
2452 return build_new_method_call (object, member,
2453 args, NULL_TREE,
2454 (disallow_virtual
2455 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2456 : LOOKUP_NORMAL),
2457 /*fn_p=*/NULL,
2458 complain);
2459 }
2460 }
2461
2462 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2463 if (TREE_CODE (fn) == ADDR_EXPR
2464 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2465 fn = TREE_OPERAND (fn, 0);
2466
2467 if (is_overloaded_fn (fn))
2468 fn = baselink_for_fns (fn);
2469
2470 result = NULL_TREE;
2471 if (BASELINK_P (fn))
2472 {
2473 tree object;
2474
2475 /* A call to a member function. From [over.call.func]:
2476
2477 If the keyword this is in scope and refers to the class of
2478 that member function, or a derived class thereof, then the
2479 function call is transformed into a qualified function call
2480 using (*this) as the postfix-expression to the left of the
2481 . operator.... [Otherwise] a contrived object of type T
2482 becomes the implied object argument.
2483
2484 In this situation:
2485
2486 struct A { void f(); };
2487 struct B : public A {};
2488 struct C : public A { void g() { B::f(); }};
2489
2490 "the class of that member function" refers to `A'. But 11.2
2491 [class.access.base] says that we need to convert 'this' to B* as
2492 part of the access, so we pass 'B' to maybe_dummy_object. */
2493
2494 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2495 {
2496 /* A constructor call always uses a dummy object. (This constructor
2497 call which has the form A::A () is actually invalid and we are
2498 going to reject it later in build_new_method_call.) */
2499 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2500 }
2501 else
2502 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2503 NULL);
2504
2505 result = build_new_method_call (object, fn, args, NULL_TREE,
2506 (disallow_virtual
2507 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2508 : LOOKUP_NORMAL),
2509 /*fn_p=*/NULL,
2510 complain);
2511 }
2512 else if (is_overloaded_fn (fn))
2513 {
2514 /* If the function is an overloaded builtin, resolve it. */
2515 if (TREE_CODE (fn) == FUNCTION_DECL
2516 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2517 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2518 result = resolve_overloaded_builtin (input_location, fn, *args);
2519
2520 if (!result)
2521 {
2522 if (warn_sizeof_pointer_memaccess
2523 && (complain & tf_warning)
2524 && !vec_safe_is_empty (*args)
2525 && !processing_template_decl)
2526 {
2527 location_t sizeof_arg_loc[3];
2528 tree sizeof_arg[3];
2529 unsigned int i;
2530 for (i = 0; i < 3; i++)
2531 {
2532 tree t;
2533
2534 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2535 sizeof_arg[i] = NULL_TREE;
2536 if (i >= (*args)->length ())
2537 continue;
2538 t = (**args)[i];
2539 if (TREE_CODE (t) != SIZEOF_EXPR)
2540 continue;
2541 if (SIZEOF_EXPR_TYPE_P (t))
2542 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2543 else
2544 sizeof_arg[i] = TREE_OPERAND (t, 0);
2545 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2546 }
2547 sizeof_pointer_memaccess_warning
2548 (sizeof_arg_loc, fn, *args,
2549 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2550 }
2551
2552 if ((complain & tf_warning)
2553 && TREE_CODE (fn) == FUNCTION_DECL
2554 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2555 && vec_safe_length (*args) == 3
2556 && !any_type_dependent_arguments_p (*args))
2557 {
2558 tree arg0 = (*orig_args)[0];
2559 tree arg1 = (*orig_args)[1];
2560 tree arg2 = (*orig_args)[2];
2561 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2562 | (literal_integer_zerop (arg2) << 2));
2563 arg2 = instantiate_non_dependent_expr (arg2);
2564 warn_for_memset (input_location, arg0, arg2, literal_mask);
2565 }
2566
2567 /* A call to a namespace-scope function. */
2568 result = build_new_function_call (fn, args, complain);
2569 }
2570 }
2571 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2572 {
2573 if (!vec_safe_is_empty (*args))
2574 error ("arguments to destructor are not allowed");
2575 /* Mark the pseudo-destructor call as having side-effects so
2576 that we do not issue warnings about its use. */
2577 result = build1 (NOP_EXPR,
2578 void_type_node,
2579 TREE_OPERAND (fn, 0));
2580 TREE_SIDE_EFFECTS (result) = 1;
2581 }
2582 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2583 /* If the "function" is really an object of class type, it might
2584 have an overloaded `operator ()'. */
2585 result = build_op_call (fn, args, complain);
2586
2587 if (!result)
2588 /* A call where the function is unknown. */
2589 result = cp_build_function_call_vec (fn, args, complain);
2590
2591 if (processing_template_decl && result != error_mark_node)
2592 {
2593 if (INDIRECT_REF_P (result))
2594 result = TREE_OPERAND (result, 0);
2595 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2596 SET_EXPR_LOCATION (result, input_location);
2597 KOENIG_LOOKUP_P (result) = koenig_p;
2598 release_tree_vector (orig_args);
2599 result = convert_from_reference (result);
2600 }
2601
2602 return result;
2603 }
2604
2605 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2606 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2607 POSTDECREMENT_EXPR.) */
2608
2609 cp_expr
2610 finish_increment_expr (cp_expr expr, enum tree_code code)
2611 {
2612 /* input_location holds the location of the trailing operator token.
2613 Build a location of the form:
2614 expr++
2615 ~~~~^~
2616 with the caret at the operator token, ranging from the start
2617 of EXPR to the end of the operator token. */
2618 location_t combined_loc = make_location (input_location,
2619 expr.get_start (),
2620 get_finish (input_location));
2621 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2622 tf_warning_or_error);
2623 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2624 result.set_location (combined_loc);
2625 return result;
2626 }
2627
2628 /* Finish a use of `this'. Returns an expression for `this'. */
2629
2630 tree
2631 finish_this_expr (void)
2632 {
2633 tree result = NULL_TREE;
2634
2635 if (current_class_ptr)
2636 {
2637 tree type = TREE_TYPE (current_class_ref);
2638
2639 /* In a lambda expression, 'this' refers to the captured 'this'. */
2640 if (LAMBDA_TYPE_P (type))
2641 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2642 else
2643 result = current_class_ptr;
2644 }
2645
2646 if (result)
2647 /* The keyword 'this' is a prvalue expression. */
2648 return rvalue (result);
2649
2650 tree fn = current_nonlambda_function ();
2651 if (fn && DECL_STATIC_FUNCTION_P (fn))
2652 error ("%<this%> is unavailable for static member functions");
2653 else if (fn)
2654 error ("invalid use of %<this%> in non-member function");
2655 else
2656 error ("invalid use of %<this%> at top level");
2657 return error_mark_node;
2658 }
2659
2660 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2661 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2662 the TYPE for the type given. If SCOPE is non-NULL, the expression
2663 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2664
2665 tree
2666 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2667 location_t loc)
2668 {
2669 if (object == error_mark_node || destructor == error_mark_node)
2670 return error_mark_node;
2671
2672 gcc_assert (TYPE_P (destructor));
2673
2674 if (!processing_template_decl)
2675 {
2676 if (scope == error_mark_node)
2677 {
2678 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2679 return error_mark_node;
2680 }
2681 if (is_auto (destructor))
2682 destructor = TREE_TYPE (object);
2683 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2684 {
2685 error_at (loc,
2686 "qualified type %qT does not match destructor name ~%qT",
2687 scope, destructor);
2688 return error_mark_node;
2689 }
2690
2691
2692 /* [expr.pseudo] says both:
2693
2694 The type designated by the pseudo-destructor-name shall be
2695 the same as the object type.
2696
2697 and:
2698
2699 The cv-unqualified versions of the object type and of the
2700 type designated by the pseudo-destructor-name shall be the
2701 same type.
2702
2703 We implement the more generous second sentence, since that is
2704 what most other compilers do. */
2705 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2706 destructor))
2707 {
2708 error_at (loc, "%qE is not of type %qT", object, destructor);
2709 return error_mark_node;
2710 }
2711 }
2712
2713 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2714 scope, destructor);
2715 }
2716
2717 /* Finish an expression of the form CODE EXPR. */
2718
2719 cp_expr
2720 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2721 tsubst_flags_t complain)
2722 {
2723 /* Build a location of the form:
2724 ++expr
2725 ^~~~~~
2726 with the caret at the operator token, ranging from the start
2727 of the operator token to the end of EXPR. */
2728 location_t combined_loc = make_location (op_loc,
2729 op_loc, expr.get_finish ());
2730 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2731 /* TODO: build_x_unary_op doesn't always honor the location. */
2732 result.set_location (combined_loc);
2733
2734 if (result == error_mark_node)
2735 return result;
2736
2737 if (!(complain & tf_warning))
2738 return result;
2739
2740 tree result_ovl = result;
2741 tree expr_ovl = expr;
2742
2743 if (!processing_template_decl)
2744 expr_ovl = cp_fully_fold (expr_ovl);
2745
2746 if (!CONSTANT_CLASS_P (expr_ovl)
2747 || TREE_OVERFLOW_P (expr_ovl))
2748 return result;
2749
2750 if (!processing_template_decl)
2751 result_ovl = cp_fully_fold (result_ovl);
2752
2753 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2754 overflow_warning (combined_loc, result_ovl);
2755
2756 return result;
2757 }
2758
2759 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2760 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2761 is being cast. */
2762
2763 tree
2764 finish_compound_literal (tree type, tree compound_literal,
2765 tsubst_flags_t complain,
2766 fcl_t fcl_context)
2767 {
2768 if (type == error_mark_node)
2769 return error_mark_node;
2770
2771 if (TYPE_REF_P (type))
2772 {
2773 compound_literal
2774 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2775 complain, fcl_context);
2776 /* The prvalue is then used to direct-initialize the reference. */
2777 tree r = (perform_implicit_conversion_flags
2778 (type, compound_literal, complain, LOOKUP_NORMAL));
2779 return convert_from_reference (r);
2780 }
2781
2782 if (!TYPE_OBJ_P (type))
2783 {
2784 if (complain & tf_error)
2785 error ("compound literal of non-object type %qT", type);
2786 return error_mark_node;
2787 }
2788
2789 if (tree anode = type_uses_auto (type))
2790 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2791 {
2792 type = do_auto_deduction (type, compound_literal, anode, complain,
2793 adc_variable_type);
2794 if (type == error_mark_node)
2795 return error_mark_node;
2796 }
2797
2798 if (processing_template_decl)
2799 {
2800 TREE_TYPE (compound_literal) = type;
2801 /* Mark the expression as a compound literal. */
2802 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2803 if (fcl_context == fcl_c99)
2804 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2805 return compound_literal;
2806 }
2807
2808 type = complete_type (type);
2809
2810 if (TYPE_NON_AGGREGATE_CLASS (type))
2811 {
2812 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2813 everywhere that deals with function arguments would be a pain, so
2814 just wrap it in a TREE_LIST. The parser set a flag so we know
2815 that it came from T{} rather than T({}). */
2816 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2817 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2818 return build_functional_cast (type, compound_literal, complain);
2819 }
2820
2821 if (TREE_CODE (type) == ARRAY_TYPE
2822 && check_array_initializer (NULL_TREE, type, compound_literal))
2823 return error_mark_node;
2824 compound_literal = reshape_init (type, compound_literal, complain);
2825 if (SCALAR_TYPE_P (type)
2826 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2827 && !check_narrowing (type, compound_literal, complain))
2828 return error_mark_node;
2829 if (TREE_CODE (type) == ARRAY_TYPE
2830 && TYPE_DOMAIN (type) == NULL_TREE)
2831 {
2832 cp_complete_array_type_or_error (&type, compound_literal,
2833 false, complain);
2834 if (type == error_mark_node)
2835 return error_mark_node;
2836 }
2837 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2838 complain);
2839 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2840 {
2841 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2842 if (fcl_context == fcl_c99)
2843 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2844 }
2845
2846 /* Put static/constant array temporaries in static variables. */
2847 /* FIXME all C99 compound literals should be variables rather than C++
2848 temporaries, unless they are used as an aggregate initializer. */
2849 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2850 && fcl_context == fcl_c99
2851 && TREE_CODE (type) == ARRAY_TYPE
2852 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2853 && initializer_constant_valid_p (compound_literal, type))
2854 {
2855 tree decl = create_temporary_var (type);
2856 DECL_INITIAL (decl) = compound_literal;
2857 TREE_STATIC (decl) = 1;
2858 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2859 {
2860 /* 5.19 says that a constant expression can include an
2861 lvalue-rvalue conversion applied to "a glvalue of literal type
2862 that refers to a non-volatile temporary object initialized
2863 with a constant expression". Rather than try to communicate
2864 that this VAR_DECL is a temporary, just mark it constexpr. */
2865 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2866 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2867 TREE_CONSTANT (decl) = true;
2868 }
2869 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2870 decl = pushdecl_top_level (decl);
2871 DECL_NAME (decl) = make_anon_name ();
2872 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2873 /* Make sure the destructor is callable. */
2874 tree clean = cxx_maybe_build_cleanup (decl, complain);
2875 if (clean == error_mark_node)
2876 return error_mark_node;
2877 return decl;
2878 }
2879
2880 /* Represent other compound literals with TARGET_EXPR so we produce
2881 an lvalue, but can elide copies. */
2882 if (!VECTOR_TYPE_P (type))
2883 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2884
2885 return compound_literal;
2886 }
2887
2888 /* Return the declaration for the function-name variable indicated by
2889 ID. */
2890
2891 tree
2892 finish_fname (tree id)
2893 {
2894 tree decl;
2895
2896 decl = fname_decl (input_location, C_RID_CODE (id), id);
2897 if (processing_template_decl && current_function_decl
2898 && decl != error_mark_node)
2899 decl = DECL_NAME (decl);
2900 return decl;
2901 }
2902
2903 /* Finish a translation unit. */
2904
2905 void
2906 finish_translation_unit (void)
2907 {
2908 /* In case there were missing closebraces,
2909 get us back to the global binding level. */
2910 pop_everything ();
2911 while (current_namespace != global_namespace)
2912 pop_namespace ();
2913
2914 /* Do file scope __FUNCTION__ et al. */
2915 finish_fname_decls ();
2916 }
2917
2918 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2919 Returns the parameter. */
2920
2921 tree
2922 finish_template_type_parm (tree aggr, tree identifier)
2923 {
2924 if (aggr != class_type_node)
2925 {
2926 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2927 aggr = class_type_node;
2928 }
2929
2930 return build_tree_list (aggr, identifier);
2931 }
2932
2933 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2934 Returns the parameter. */
2935
2936 tree
2937 finish_template_template_parm (tree aggr, tree identifier)
2938 {
2939 tree decl = build_decl (input_location,
2940 TYPE_DECL, identifier, NULL_TREE);
2941
2942 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2943 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2944 DECL_TEMPLATE_RESULT (tmpl) = decl;
2945 DECL_ARTIFICIAL (decl) = 1;
2946
2947 // Associate the constraints with the underlying declaration,
2948 // not the template.
2949 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2950 tree constr = build_constraints (reqs, NULL_TREE);
2951 set_constraints (decl, constr);
2952
2953 end_template_decl ();
2954
2955 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2956
2957 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2958 /*is_primary=*/true, /*is_partial=*/false,
2959 /*is_friend=*/0);
2960
2961 return finish_template_type_parm (aggr, tmpl);
2962 }
2963
2964 /* ARGUMENT is the default-argument value for a template template
2965 parameter. If ARGUMENT is invalid, issue error messages and return
2966 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2967
2968 tree
2969 check_template_template_default_arg (tree argument)
2970 {
2971 if (TREE_CODE (argument) != TEMPLATE_DECL
2972 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2973 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2974 {
2975 if (TREE_CODE (argument) == TYPE_DECL)
2976 error ("invalid use of type %qT as a default value for a template "
2977 "template-parameter", TREE_TYPE (argument));
2978 else
2979 error ("invalid default argument for a template template parameter");
2980 return error_mark_node;
2981 }
2982
2983 return argument;
2984 }
2985
2986 /* Begin a class definition, as indicated by T. */
2987
2988 tree
2989 begin_class_definition (tree t)
2990 {
2991 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2992 return error_mark_node;
2993
2994 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2995 are passed the same as decimal scalar types. */
2996 if (TREE_CODE (t) == RECORD_TYPE
2997 && !processing_template_decl)
2998 {
2999 tree ns = TYPE_CONTEXT (t);
3000 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3001 && DECL_CONTEXT (ns) == std_node
3002 && DECL_NAME (ns)
3003 && id_equal (DECL_NAME (ns), "decimal"))
3004 {
3005 const char *n = TYPE_NAME_STRING (t);
3006 if ((strcmp (n, "decimal32") == 0)
3007 || (strcmp (n, "decimal64") == 0)
3008 || (strcmp (n, "decimal128") == 0))
3009 TYPE_TRANSPARENT_AGGR (t) = 1;
3010 }
3011 }
3012
3013 /* A non-implicit typename comes from code like:
3014
3015 template <typename T> struct A {
3016 template <typename U> struct A<T>::B ...
3017
3018 This is erroneous. */
3019 else if (TREE_CODE (t) == TYPENAME_TYPE)
3020 {
3021 error ("invalid definition of qualified type %qT", t);
3022 t = error_mark_node;
3023 }
3024
3025 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3026 {
3027 t = make_class_type (RECORD_TYPE);
3028 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3029 }
3030
3031 if (TYPE_BEING_DEFINED (t))
3032 {
3033 t = make_class_type (TREE_CODE (t));
3034 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3035 }
3036 maybe_process_partial_specialization (t);
3037 pushclass (t);
3038 TYPE_BEING_DEFINED (t) = 1;
3039 class_binding_level->defining_class_p = 1;
3040
3041 if (flag_pack_struct)
3042 {
3043 tree v;
3044 TYPE_PACKED (t) = 1;
3045 /* Even though the type is being defined for the first time
3046 here, there might have been a forward declaration, so there
3047 might be cv-qualified variants of T. */
3048 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3049 TYPE_PACKED (v) = 1;
3050 }
3051 /* Reset the interface data, at the earliest possible
3052 moment, as it might have been set via a class foo;
3053 before. */
3054 if (! TYPE_UNNAMED_P (t))
3055 {
3056 struct c_fileinfo *finfo = \
3057 get_fileinfo (LOCATION_FILE (input_location));
3058 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3059 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3060 (t, finfo->interface_unknown);
3061 }
3062 reset_specialization();
3063
3064 /* Make a declaration for this class in its own scope. */
3065 build_self_reference ();
3066
3067 return t;
3068 }
3069
3070 /* Finish the member declaration given by DECL. */
3071
3072 void
3073 finish_member_declaration (tree decl)
3074 {
3075 if (decl == error_mark_node || decl == NULL_TREE)
3076 return;
3077
3078 if (decl == void_type_node)
3079 /* The COMPONENT was a friend, not a member, and so there's
3080 nothing for us to do. */
3081 return;
3082
3083 /* We should see only one DECL at a time. */
3084 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3085
3086 /* Don't add decls after definition. */
3087 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3088 /* We can add lambda types when late parsing default
3089 arguments. */
3090 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3091
3092 /* Set up access control for DECL. */
3093 TREE_PRIVATE (decl)
3094 = (current_access_specifier == access_private_node);
3095 TREE_PROTECTED (decl)
3096 = (current_access_specifier == access_protected_node);
3097 if (TREE_CODE (decl) == TEMPLATE_DECL)
3098 {
3099 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3100 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3101 }
3102
3103 /* Mark the DECL as a member of the current class, unless it's
3104 a member of an enumeration. */
3105 if (TREE_CODE (decl) != CONST_DECL)
3106 DECL_CONTEXT (decl) = current_class_type;
3107
3108 if (TREE_CODE (decl) == USING_DECL)
3109 /* For now, ignore class-scope USING_DECLS, so that debugging
3110 backends do not see them. */
3111 DECL_IGNORED_P (decl) = 1;
3112
3113 /* Check for bare parameter packs in the non-static data member
3114 declaration. */
3115 if (TREE_CODE (decl) == FIELD_DECL)
3116 {
3117 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3118 TREE_TYPE (decl) = error_mark_node;
3119 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3120 DECL_ATTRIBUTES (decl) = NULL_TREE;
3121 }
3122
3123 /* [dcl.link]
3124
3125 A C language linkage is ignored for the names of class members
3126 and the member function type of class member functions. */
3127 if (DECL_LANG_SPECIFIC (decl))
3128 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3129
3130 bool add = false;
3131
3132 /* Functions and non-functions are added differently. */
3133 if (DECL_DECLARES_FUNCTION_P (decl))
3134 add = add_method (current_class_type, decl, false);
3135 /* Enter the DECL into the scope of the class, if the class
3136 isn't a closure (whose fields are supposed to be unnamed). */
3137 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3138 || pushdecl_class_level (decl))
3139 add = true;
3140
3141 if (add)
3142 {
3143 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3144 go at the beginning. The reason is that
3145 legacy_nonfn_member_lookup searches the list in order, and we
3146 want a field name to override a type name so that the "struct
3147 stat hack" will work. In particular:
3148
3149 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3150
3151 is valid. */
3152
3153 if (TREE_CODE (decl) == TYPE_DECL)
3154 TYPE_FIELDS (current_class_type)
3155 = chainon (TYPE_FIELDS (current_class_type), decl);
3156 else
3157 {
3158 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3159 TYPE_FIELDS (current_class_type) = decl;
3160 }
3161
3162 maybe_add_class_template_decl_list (current_class_type, decl,
3163 /*friend_p=*/0);
3164 }
3165 }
3166
3167 /* Finish processing a complete template declaration. The PARMS are
3168 the template parameters. */
3169
3170 void
3171 finish_template_decl (tree parms)
3172 {
3173 if (parms)
3174 end_template_decl ();
3175 else
3176 end_specialization ();
3177 }
3178
3179 // Returns the template type of the class scope being entered. If we're
3180 // entering a constrained class scope. TYPE is the class template
3181 // scope being entered and we may need to match the intended type with
3182 // a constrained specialization. For example:
3183 //
3184 // template<Object T>
3185 // struct S { void f(); }; #1
3186 //
3187 // template<Object T>
3188 // void S<T>::f() { } #2
3189 //
3190 // We check, in #2, that S<T> refers precisely to the type declared by
3191 // #1 (i.e., that the constraints match). Note that the following should
3192 // be an error since there is no specialization of S<T> that is
3193 // unconstrained, but this is not diagnosed here.
3194 //
3195 // template<typename T>
3196 // void S<T>::f() { }
3197 //
3198 // We cannot diagnose this problem here since this function also matches
3199 // qualified template names that are not part of a definition. For example:
3200 //
3201 // template<Integral T, Floating_point U>
3202 // typename pair<T, U>::first_type void f(T, U);
3203 //
3204 // Here, it is unlikely that there is a partial specialization of
3205 // pair constrained for for Integral and Floating_point arguments.
3206 //
3207 // The general rule is: if a constrained specialization with matching
3208 // constraints is found return that type. Also note that if TYPE is not a
3209 // class-type (e.g. a typename type), then no fixup is needed.
3210
3211 static tree
3212 fixup_template_type (tree type)
3213 {
3214 // Find the template parameter list at the a depth appropriate to
3215 // the scope we're trying to enter.
3216 tree parms = current_template_parms;
3217 int depth = template_class_depth (type);
3218 for (int n = processing_template_decl; n > depth && parms; --n)
3219 parms = TREE_CHAIN (parms);
3220 if (!parms)
3221 return type;
3222 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3223 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3224
3225 // Search for a specialization whose type and constraints match.
3226 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3227 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3228 while (specs)
3229 {
3230 tree spec_constr = get_constraints (TREE_VALUE (specs));
3231
3232 // If the type and constraints match a specialization, then we
3233 // are entering that type.
3234 if (same_type_p (type, TREE_TYPE (specs))
3235 && equivalent_constraints (cur_constr, spec_constr))
3236 return TREE_TYPE (specs);
3237 specs = TREE_CHAIN (specs);
3238 }
3239
3240 // If no specialization matches, then must return the type
3241 // previously found.
3242 return type;
3243 }
3244
3245 /* Finish processing a template-id (which names a type) of the form
3246 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3247 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3248 the scope of template-id indicated. */
3249
3250 tree
3251 finish_template_type (tree name, tree args, int entering_scope)
3252 {
3253 tree type;
3254
3255 type = lookup_template_class (name, args,
3256 NULL_TREE, NULL_TREE, entering_scope,
3257 tf_warning_or_error | tf_user);
3258
3259 /* If we might be entering the scope of a partial specialization,
3260 find the one with the right constraints. */
3261 if (flag_concepts
3262 && entering_scope
3263 && CLASS_TYPE_P (type)
3264 && CLASSTYPE_TEMPLATE_INFO (type)
3265 && dependent_type_p (type)
3266 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3267 type = fixup_template_type (type);
3268
3269 if (type == error_mark_node)
3270 return type;
3271 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3272 return TYPE_STUB_DECL (type);
3273 else
3274 return TYPE_NAME (type);
3275 }
3276
3277 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3278 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3279 BASE_CLASS, or NULL_TREE if an error occurred. The
3280 ACCESS_SPECIFIER is one of
3281 access_{default,public,protected_private}_node. For a virtual base
3282 we set TREE_TYPE. */
3283
3284 tree
3285 finish_base_specifier (tree base, tree access, bool virtual_p)
3286 {
3287 tree result;
3288
3289 if (base == error_mark_node)
3290 {
3291 error ("invalid base-class specification");
3292 result = NULL_TREE;
3293 }
3294 else if (! MAYBE_CLASS_TYPE_P (base))
3295 {
3296 error ("%qT is not a class type", base);
3297 result = NULL_TREE;
3298 }
3299 else
3300 {
3301 if (cp_type_quals (base) != 0)
3302 {
3303 /* DR 484: Can a base-specifier name a cv-qualified
3304 class type? */
3305 base = TYPE_MAIN_VARIANT (base);
3306 }
3307 result = build_tree_list (access, base);
3308 if (virtual_p)
3309 TREE_TYPE (result) = integer_type_node;
3310 }
3311
3312 return result;
3313 }
3314
3315 /* If FNS is a member function, a set of member functions, or a
3316 template-id referring to one or more member functions, return a
3317 BASELINK for FNS, incorporating the current access context.
3318 Otherwise, return FNS unchanged. */
3319
3320 tree
3321 baselink_for_fns (tree fns)
3322 {
3323 tree scope;
3324 tree cl;
3325
3326 if (BASELINK_P (fns)
3327 || error_operand_p (fns))
3328 return fns;
3329
3330 scope = ovl_scope (fns);
3331 if (!CLASS_TYPE_P (scope))
3332 return fns;
3333
3334 cl = currently_open_derived_class (scope);
3335 if (!cl)
3336 cl = scope;
3337 cl = TYPE_BINFO (cl);
3338 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3339 }
3340
3341 /* Returns true iff DECL is a variable from a function outside
3342 the current one. */
3343
3344 static bool
3345 outer_var_p (tree decl)
3346 {
3347 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3348 && DECL_FUNCTION_SCOPE_P (decl)
3349 /* Don't get confused by temporaries. */
3350 && DECL_NAME (decl)
3351 && (DECL_CONTEXT (decl) != current_function_decl
3352 || parsing_nsdmi ()));
3353 }
3354
3355 /* As above, but also checks that DECL is automatic. */
3356
3357 bool
3358 outer_automatic_var_p (tree decl)
3359 {
3360 return (outer_var_p (decl)
3361 && !TREE_STATIC (decl));
3362 }
3363
3364 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3365 rewrite it for lambda capture.
3366
3367 If ODR_USE is true, we're being called from mark_use, and we complain about
3368 use of constant variables. If ODR_USE is false, we're being called for the
3369 id-expression, and we do lambda capture. */
3370
3371 tree
3372 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3373 {
3374 if (cp_unevaluated_operand)
3375 /* It's not a use (3.2) if we're in an unevaluated context. */
3376 return decl;
3377 if (decl == error_mark_node)
3378 return decl;
3379
3380 tree context = DECL_CONTEXT (decl);
3381 tree containing_function = current_function_decl;
3382 tree lambda_stack = NULL_TREE;
3383 tree lambda_expr = NULL_TREE;
3384 tree initializer = convert_from_reference (decl);
3385
3386 /* Mark it as used now even if the use is ill-formed. */
3387 if (!mark_used (decl, complain))
3388 return error_mark_node;
3389
3390 if (parsing_nsdmi ())
3391 containing_function = NULL_TREE;
3392
3393 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3394 {
3395 /* Check whether we've already built a proxy. */
3396 tree var = decl;
3397 while (is_normal_capture_proxy (var))
3398 var = DECL_CAPTURED_VARIABLE (var);
3399 tree d = retrieve_local_specialization (var);
3400
3401 if (d && d != decl && is_capture_proxy (d))
3402 {
3403 if (DECL_CONTEXT (d) == containing_function)
3404 /* We already have an inner proxy. */
3405 return d;
3406 else
3407 /* We need to capture an outer proxy. */
3408 return process_outer_var_ref (d, complain, odr_use);
3409 }
3410 }
3411
3412 /* If we are in a lambda function, we can move out until we hit
3413 1. the context,
3414 2. a non-lambda function, or
3415 3. a non-default capturing lambda function. */
3416 while (context != containing_function
3417 /* containing_function can be null with invalid generic lambdas. */
3418 && containing_function
3419 && LAMBDA_FUNCTION_P (containing_function))
3420 {
3421 tree closure = DECL_CONTEXT (containing_function);
3422 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3423
3424 if (TYPE_CLASS_SCOPE_P (closure))
3425 /* A lambda in an NSDMI (c++/64496). */
3426 break;
3427
3428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3429 == CPLD_NONE)
3430 break;
3431
3432 lambda_stack = tree_cons (NULL_TREE,
3433 lambda_expr,
3434 lambda_stack);
3435
3436 containing_function
3437 = decl_function_context (containing_function);
3438 }
3439
3440 /* In a lambda within a template, wait until instantiation
3441 time to implicitly capture a dependent type. */
3442 if (context == containing_function
3443 && dependent_type_p (TREE_TYPE (decl)))
3444 return decl;
3445
3446 if (lambda_expr && VAR_P (decl)
3447 && DECL_ANON_UNION_VAR_P (decl))
3448 {
3449 if (complain & tf_error)
3450 error ("cannot capture member %qD of anonymous union", decl);
3451 return error_mark_node;
3452 }
3453 /* Do lambda capture when processing the id-expression, not when
3454 odr-using a variable. */
3455 if (!odr_use && context == containing_function)
3456 {
3457 decl = add_default_capture (lambda_stack,
3458 /*id=*/DECL_NAME (decl),
3459 initializer);
3460 }
3461 /* Only an odr-use of an outer automatic variable causes an
3462 error, and a constant variable can decay to a prvalue
3463 constant without odr-use. So don't complain yet. */
3464 else if (!odr_use && decl_constant_var_p (decl))
3465 return decl;
3466 else if (lambda_expr)
3467 {
3468 if (complain & tf_error)
3469 {
3470 error ("%qD is not captured", decl);
3471 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3472 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3473 == CPLD_NONE)
3474 inform (location_of (closure),
3475 "the lambda has no capture-default");
3476 else if (TYPE_CLASS_SCOPE_P (closure))
3477 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3478 "capture variables from the enclosing context",
3479 TYPE_CONTEXT (closure));
3480 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3481 }
3482 return error_mark_node;
3483 }
3484 else
3485 {
3486 if (complain & tf_error)
3487 {
3488 error (VAR_P (decl)
3489 ? G_("use of local variable with automatic storage from "
3490 "containing function")
3491 : G_("use of parameter from containing function"));
3492 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3493 }
3494 return error_mark_node;
3495 }
3496 return decl;
3497 }
3498
3499 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3500 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3501 if non-NULL, is the type or namespace used to explicitly qualify
3502 ID_EXPRESSION. DECL is the entity to which that name has been
3503 resolved.
3504
3505 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3506 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3507 be set to true if this expression isn't permitted in a
3508 constant-expression, but it is otherwise not set by this function.
3509 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3510 constant-expression, but a non-constant expression is also
3511 permissible.
3512
3513 DONE is true if this expression is a complete postfix-expression;
3514 it is false if this expression is followed by '->', '[', '(', etc.
3515 ADDRESS_P is true iff this expression is the operand of '&'.
3516 TEMPLATE_P is true iff the qualified-id was of the form
3517 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3518 appears as a template argument.
3519
3520 If an error occurs, and it is the kind of error that might cause
3521 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3522 is the caller's responsibility to issue the message. *ERROR_MSG
3523 will be a string with static storage duration, so the caller need
3524 not "free" it.
3525
3526 Return an expression for the entity, after issuing appropriate
3527 diagnostics. This function is also responsible for transforming a
3528 reference to a non-static member into a COMPONENT_REF that makes
3529 the use of "this" explicit.
3530
3531 Upon return, *IDK will be filled in appropriately. */
3532 static cp_expr
3533 finish_id_expression_1 (tree id_expression,
3534 tree decl,
3535 tree scope,
3536 cp_id_kind *idk,
3537 bool integral_constant_expression_p,
3538 bool allow_non_integral_constant_expression_p,
3539 bool *non_integral_constant_expression_p,
3540 bool template_p,
3541 bool done,
3542 bool address_p,
3543 bool template_arg_p,
3544 const char **error_msg,
3545 location_t location)
3546 {
3547 decl = strip_using_decl (decl);
3548
3549 /* Initialize the output parameters. */
3550 *idk = CP_ID_KIND_NONE;
3551 *error_msg = NULL;
3552
3553 if (id_expression == error_mark_node)
3554 return error_mark_node;
3555 /* If we have a template-id, then no further lookup is
3556 required. If the template-id was for a template-class, we
3557 will sometimes have a TYPE_DECL at this point. */
3558 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3559 || TREE_CODE (decl) == TYPE_DECL)
3560 ;
3561 /* Look up the name. */
3562 else
3563 {
3564 if (decl == error_mark_node)
3565 {
3566 /* Name lookup failed. */
3567 if (scope
3568 && (!TYPE_P (scope)
3569 || (!dependent_type_p (scope)
3570 && !(identifier_p (id_expression)
3571 && IDENTIFIER_CONV_OP_P (id_expression)
3572 && dependent_type_p (TREE_TYPE (id_expression))))))
3573 {
3574 /* If the qualifying type is non-dependent (and the name
3575 does not name a conversion operator to a dependent
3576 type), issue an error. */
3577 qualified_name_lookup_error (scope, id_expression, decl, location);
3578 return error_mark_node;
3579 }
3580 else if (!scope)
3581 {
3582 /* It may be resolved via Koenig lookup. */
3583 *idk = CP_ID_KIND_UNQUALIFIED;
3584 return id_expression;
3585 }
3586 else
3587 decl = id_expression;
3588 }
3589
3590 /* Remember that the name was used in the definition of
3591 the current class so that we can check later to see if
3592 the meaning would have been different after the class
3593 was entirely defined. */
3594 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3595 maybe_note_name_used_in_class (id_expression, decl);
3596
3597 /* A use in unevaluated operand might not be instantiated appropriately
3598 if tsubst_copy builds a dummy parm, or if we never instantiate a
3599 generic lambda, so mark it now. */
3600 if (processing_template_decl && cp_unevaluated_operand)
3601 mark_type_use (decl);
3602
3603 /* Disallow uses of local variables from containing functions, except
3604 within lambda-expressions. */
3605 if (outer_automatic_var_p (decl))
3606 {
3607 decl = process_outer_var_ref (decl, tf_warning_or_error);
3608 if (decl == error_mark_node)
3609 return error_mark_node;
3610 }
3611
3612 /* Also disallow uses of function parameters outside the function
3613 body, except inside an unevaluated context (i.e. decltype). */
3614 if (TREE_CODE (decl) == PARM_DECL
3615 && DECL_CONTEXT (decl) == NULL_TREE
3616 && !cp_unevaluated_operand)
3617 {
3618 *error_msg = G_("use of parameter outside function body");
3619 return error_mark_node;
3620 }
3621 }
3622
3623 /* If we didn't find anything, or what we found was a type,
3624 then this wasn't really an id-expression. */
3625 if (TREE_CODE (decl) == TEMPLATE_DECL
3626 && !DECL_FUNCTION_TEMPLATE_P (decl))
3627 {
3628 *error_msg = G_("missing template arguments");
3629 return error_mark_node;
3630 }
3631 else if (TREE_CODE (decl) == TYPE_DECL
3632 || TREE_CODE (decl) == NAMESPACE_DECL)
3633 {
3634 *error_msg = G_("expected primary-expression");
3635 return error_mark_node;
3636 }
3637
3638 /* If the name resolved to a template parameter, there is no
3639 need to look it up again later. */
3640 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3641 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3642 {
3643 tree r;
3644
3645 *idk = CP_ID_KIND_NONE;
3646 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3647 decl = TEMPLATE_PARM_DECL (decl);
3648 r = DECL_INITIAL (decl);
3649 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
3650 {
3651 /* If the entity is a template parameter object for a template
3652 parameter of type T, the type of the expression is const T. */
3653 tree ctype = TREE_TYPE (r);
3654 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
3655 | TYPE_QUAL_CONST));
3656 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
3657 }
3658 r = convert_from_reference (r);
3659 if (integral_constant_expression_p
3660 && !dependent_type_p (TREE_TYPE (decl))
3661 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3662 {
3663 if (!allow_non_integral_constant_expression_p)
3664 error ("template parameter %qD of type %qT is not allowed in "
3665 "an integral constant expression because it is not of "
3666 "integral or enumeration type", decl, TREE_TYPE (decl));
3667 *non_integral_constant_expression_p = true;
3668 }
3669 return r;
3670 }
3671 else
3672 {
3673 bool dependent_p = type_dependent_expression_p (decl);
3674
3675 /* If the declaration was explicitly qualified indicate
3676 that. The semantics of `A::f(3)' are different than
3677 `f(3)' if `f' is virtual. */
3678 *idk = (scope
3679 ? CP_ID_KIND_QUALIFIED
3680 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3681 ? CP_ID_KIND_TEMPLATE_ID
3682 : (dependent_p
3683 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3684 : CP_ID_KIND_UNQUALIFIED)));
3685
3686 if (dependent_p
3687 && DECL_P (decl)
3688 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3689 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3690 wrong, so just return the identifier. */
3691 return id_expression;
3692
3693 if (TREE_CODE (decl) == NAMESPACE_DECL)
3694 {
3695 error ("use of namespace %qD as expression", decl);
3696 return error_mark_node;
3697 }
3698 else if (DECL_CLASS_TEMPLATE_P (decl))
3699 {
3700 error ("use of class template %qT as expression", decl);
3701 return error_mark_node;
3702 }
3703 else if (TREE_CODE (decl) == TREE_LIST)
3704 {
3705 /* Ambiguous reference to base members. */
3706 error ("request for member %qD is ambiguous in "
3707 "multiple inheritance lattice", id_expression);
3708 print_candidates (decl);
3709 return error_mark_node;
3710 }
3711
3712 /* Mark variable-like entities as used. Functions are similarly
3713 marked either below or after overload resolution. */
3714 if ((VAR_P (decl)
3715 || TREE_CODE (decl) == PARM_DECL
3716 || TREE_CODE (decl) == CONST_DECL
3717 || TREE_CODE (decl) == RESULT_DECL)
3718 && !mark_used (decl))
3719 return error_mark_node;
3720
3721 /* Only certain kinds of names are allowed in constant
3722 expression. Template parameters have already
3723 been handled above. */
3724 if (! error_operand_p (decl)
3725 && !dependent_p
3726 && integral_constant_expression_p
3727 && ! decl_constant_var_p (decl)
3728 && TREE_CODE (decl) != CONST_DECL
3729 && ! builtin_valid_in_constant_expr_p (decl))
3730 {
3731 if (!allow_non_integral_constant_expression_p)
3732 {
3733 error ("%qD cannot appear in a constant-expression", decl);
3734 return error_mark_node;
3735 }
3736 *non_integral_constant_expression_p = true;
3737 }
3738
3739 tree wrap;
3740 if (VAR_P (decl)
3741 && !cp_unevaluated_operand
3742 && !processing_template_decl
3743 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3744 && CP_DECL_THREAD_LOCAL_P (decl)
3745 && (wrap = get_tls_wrapper_fn (decl)))
3746 {
3747 /* Replace an evaluated use of the thread_local variable with
3748 a call to its wrapper. */
3749 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3750 }
3751 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3752 && !dependent_p
3753 && variable_template_p (TREE_OPERAND (decl, 0)))
3754 {
3755 decl = finish_template_variable (decl);
3756 mark_used (decl);
3757 decl = convert_from_reference (decl);
3758 }
3759 else if (scope)
3760 {
3761 if (TREE_CODE (decl) == SCOPE_REF)
3762 {
3763 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3764 decl = TREE_OPERAND (decl, 1);
3765 }
3766
3767 decl = (adjust_result_of_qualified_name_lookup
3768 (decl, scope, current_nonlambda_class_type()));
3769
3770 if (TREE_CODE (decl) == FUNCTION_DECL)
3771 mark_used (decl);
3772
3773 if (TYPE_P (scope))
3774 decl = finish_qualified_id_expr (scope,
3775 decl,
3776 done,
3777 address_p,
3778 template_p,
3779 template_arg_p,
3780 tf_warning_or_error);
3781 else
3782 decl = convert_from_reference (decl);
3783 }
3784 else if (TREE_CODE (decl) == FIELD_DECL)
3785 {
3786 /* Since SCOPE is NULL here, this is an unqualified name.
3787 Access checking has been performed during name lookup
3788 already. Turn off checking to avoid duplicate errors. */
3789 push_deferring_access_checks (dk_no_check);
3790 decl = finish_non_static_data_member (decl, NULL_TREE,
3791 /*qualifying_scope=*/NULL_TREE);
3792 pop_deferring_access_checks ();
3793 }
3794 else if (is_overloaded_fn (decl))
3795 {
3796 tree first_fn = get_first_fn (decl);
3797
3798 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3799 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3800
3801 /* [basic.def.odr]: "A function whose name appears as a
3802 potentially-evaluated expression is odr-used if it is the unique
3803 lookup result".
3804
3805 But only mark it if it's a complete postfix-expression; in a call,
3806 ADL might select a different function, and we'll call mark_used in
3807 build_over_call. */
3808 if (done
3809 && !really_overloaded_fn (decl)
3810 && !mark_used (first_fn))
3811 return error_mark_node;
3812
3813 if (!template_arg_p
3814 && (TREE_CODE (first_fn) == USING_DECL
3815 || (TREE_CODE (first_fn) == FUNCTION_DECL
3816 && DECL_FUNCTION_MEMBER_P (first_fn)
3817 && !shared_member_p (decl))))
3818 {
3819 /* A set of member functions. */
3820 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3821 return finish_class_member_access_expr (decl, id_expression,
3822 /*template_p=*/false,
3823 tf_warning_or_error);
3824 }
3825
3826 decl = baselink_for_fns (decl);
3827 }
3828 else
3829 {
3830 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3831 && DECL_CLASS_SCOPE_P (decl))
3832 {
3833 tree context = context_for_name_lookup (decl);
3834 if (context != current_class_type)
3835 {
3836 tree path = currently_open_derived_class (context);
3837 perform_or_defer_access_check (TYPE_BINFO (path),
3838 decl, decl,
3839 tf_warning_or_error);
3840 }
3841 }
3842
3843 decl = convert_from_reference (decl);
3844 }
3845 }
3846
3847 return cp_expr (decl, location);
3848 }
3849
3850 /* As per finish_id_expression_1, but adding a wrapper node
3851 around the result if needed to express LOCATION. */
3852
3853 cp_expr
3854 finish_id_expression (tree id_expression,
3855 tree decl,
3856 tree scope,
3857 cp_id_kind *idk,
3858 bool integral_constant_expression_p,
3859 bool allow_non_integral_constant_expression_p,
3860 bool *non_integral_constant_expression_p,
3861 bool template_p,
3862 bool done,
3863 bool address_p,
3864 bool template_arg_p,
3865 const char **error_msg,
3866 location_t location)
3867 {
3868 cp_expr result
3869 = finish_id_expression_1 (id_expression, decl, scope, idk,
3870 integral_constant_expression_p,
3871 allow_non_integral_constant_expression_p,
3872 non_integral_constant_expression_p,
3873 template_p, done, address_p, template_arg_p,
3874 error_msg, location);
3875 return result.maybe_add_location_wrapper ();
3876 }
3877
3878 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3879 use as a type-specifier. */
3880
3881 tree
3882 finish_typeof (tree expr)
3883 {
3884 tree type;
3885
3886 if (type_dependent_expression_p (expr))
3887 {
3888 type = cxx_make_type (TYPEOF_TYPE);
3889 TYPEOF_TYPE_EXPR (type) = expr;
3890 SET_TYPE_STRUCTURAL_EQUALITY (type);
3891
3892 return type;
3893 }
3894
3895 expr = mark_type_use (expr);
3896
3897 type = unlowered_expr_type (expr);
3898
3899 if (!type || type == unknown_type_node)
3900 {
3901 error ("type of %qE is unknown", expr);
3902 return error_mark_node;
3903 }
3904
3905 return type;
3906 }
3907
3908 /* Implement the __underlying_type keyword: Return the underlying
3909 type of TYPE, suitable for use as a type-specifier. */
3910
3911 tree
3912 finish_underlying_type (tree type)
3913 {
3914 tree underlying_type;
3915
3916 if (processing_template_decl)
3917 {
3918 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3919 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3920 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3921
3922 return underlying_type;
3923 }
3924
3925 if (!complete_type_or_else (type, NULL_TREE))
3926 return error_mark_node;
3927
3928 if (TREE_CODE (type) != ENUMERAL_TYPE)
3929 {
3930 error ("%qT is not an enumeration type", type);
3931 return error_mark_node;
3932 }
3933
3934 underlying_type = ENUM_UNDERLYING_TYPE (type);
3935
3936 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3937 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3938 See finish_enum_value_list for details. */
3939 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3940 underlying_type
3941 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3942 TYPE_UNSIGNED (underlying_type));
3943
3944 return underlying_type;
3945 }
3946
3947 /* Implement the __direct_bases keyword: Return the direct base classes
3948 of type. */
3949
3950 tree
3951 calculate_direct_bases (tree type, tsubst_flags_t complain)
3952 {
3953 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3954 || !NON_UNION_CLASS_TYPE_P (type))
3955 return make_tree_vec (0);
3956
3957 vec<tree, va_gc> *vector = make_tree_vector ();
3958 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3959 tree binfo;
3960 unsigned i;
3961
3962 /* Virtual bases are initialized first */
3963 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3964 if (BINFO_VIRTUAL_P (binfo))
3965 vec_safe_push (vector, binfo);
3966
3967 /* Now non-virtuals */
3968 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3969 if (!BINFO_VIRTUAL_P (binfo))
3970 vec_safe_push (vector, binfo);
3971
3972 tree bases_vec = make_tree_vec (vector->length ());
3973
3974 for (i = 0; i < vector->length (); ++i)
3975 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3976
3977 release_tree_vector (vector);
3978 return bases_vec;
3979 }
3980
3981 /* Implement the __bases keyword: Return the base classes
3982 of type */
3983
3984 /* Find morally non-virtual base classes by walking binfo hierarchy */
3985 /* Virtual base classes are handled separately in finish_bases */
3986
3987 static tree
3988 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3989 {
3990 /* Don't walk bases of virtual bases */
3991 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3992 }
3993
3994 static tree
3995 dfs_calculate_bases_post (tree binfo, void *data_)
3996 {
3997 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3998 if (!BINFO_VIRTUAL_P (binfo))
3999 vec_safe_push (*data, BINFO_TYPE (binfo));
4000 return NULL_TREE;
4001 }
4002
4003 /* Calculates the morally non-virtual base classes of a class */
4004 static vec<tree, va_gc> *
4005 calculate_bases_helper (tree type)
4006 {
4007 vec<tree, va_gc> *vector = make_tree_vector ();
4008
4009 /* Now add non-virtual base classes in order of construction */
4010 if (TYPE_BINFO (type))
4011 dfs_walk_all (TYPE_BINFO (type),
4012 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4013 return vector;
4014 }
4015
4016 tree
4017 calculate_bases (tree type, tsubst_flags_t complain)
4018 {
4019 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4020 || !NON_UNION_CLASS_TYPE_P (type))
4021 return make_tree_vec (0);
4022
4023 vec<tree, va_gc> *vector = make_tree_vector ();
4024 tree bases_vec = NULL_TREE;
4025 unsigned i;
4026 vec<tree, va_gc> *vbases;
4027 vec<tree, va_gc> *nonvbases;
4028 tree binfo;
4029
4030 /* First go through virtual base classes */
4031 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4032 vec_safe_iterate (vbases, i, &binfo); i++)
4033 {
4034 vec<tree, va_gc> *vbase_bases
4035 = calculate_bases_helper (BINFO_TYPE (binfo));
4036 vec_safe_splice (vector, vbase_bases);
4037 release_tree_vector (vbase_bases);
4038 }
4039
4040 /* Now for the non-virtual bases */
4041 nonvbases = calculate_bases_helper (type);
4042 vec_safe_splice (vector, nonvbases);
4043 release_tree_vector (nonvbases);
4044
4045 /* Note that during error recovery vector->length can even be zero. */
4046 if (vector->length () > 1)
4047 {
4048 /* Last element is entire class, so don't copy */
4049 bases_vec = make_tree_vec (vector->length () - 1);
4050
4051 for (i = 0; i < vector->length () - 1; ++i)
4052 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4053 }
4054 else
4055 bases_vec = make_tree_vec (0);
4056
4057 release_tree_vector (vector);
4058 return bases_vec;
4059 }
4060
4061 tree
4062 finish_bases (tree type, bool direct)
4063 {
4064 tree bases = NULL_TREE;
4065
4066 if (!processing_template_decl)
4067 {
4068 /* Parameter packs can only be used in templates */
4069 error ("Parameter pack __bases only valid in template declaration");
4070 return error_mark_node;
4071 }
4072
4073 bases = cxx_make_type (BASES);
4074 BASES_TYPE (bases) = type;
4075 BASES_DIRECT (bases) = direct;
4076 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4077
4078 return bases;
4079 }
4080
4081 /* Perform C++-specific checks for __builtin_offsetof before calling
4082 fold_offsetof. */
4083
4084 tree
4085 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4086 {
4087 /* If we're processing a template, we can't finish the semantics yet.
4088 Otherwise we can fold the entire expression now. */
4089 if (processing_template_decl)
4090 {
4091 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4092 SET_EXPR_LOCATION (expr, loc);
4093 return expr;
4094 }
4095
4096 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4097 {
4098 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4099 TREE_OPERAND (expr, 2));
4100 return error_mark_node;
4101 }
4102 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4103 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4104 || TREE_TYPE (expr) == unknown_type_node)
4105 {
4106 while (TREE_CODE (expr) == COMPONENT_REF
4107 || TREE_CODE (expr) == COMPOUND_EXPR)
4108 expr = TREE_OPERAND (expr, 1);
4109
4110 if (DECL_P (expr))
4111 {
4112 error ("cannot apply %<offsetof%> to member function %qD", expr);
4113 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4114 }
4115 else
4116 error ("cannot apply %<offsetof%> to member function");
4117 return error_mark_node;
4118 }
4119 if (TREE_CODE (expr) == CONST_DECL)
4120 {
4121 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4122 return error_mark_node;
4123 }
4124 if (REFERENCE_REF_P (expr))
4125 expr = TREE_OPERAND (expr, 0);
4126 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4127 return error_mark_node;
4128 if (warn_invalid_offsetof
4129 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4130 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4131 && cp_unevaluated_operand == 0)
4132 warning_at (loc, OPT_Winvalid_offsetof, "offsetof within "
4133 "non-standard-layout type %qT is conditionally-supported",
4134 TREE_TYPE (TREE_TYPE (object_ptr)));
4135 return fold_offsetof (expr);
4136 }
4137
4138 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4139 function is broken out from the above for the benefit of the tree-ssa
4140 project. */
4141
4142 void
4143 simplify_aggr_init_expr (tree *tp)
4144 {
4145 tree aggr_init_expr = *tp;
4146
4147 /* Form an appropriate CALL_EXPR. */
4148 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4149 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4150 tree type = TREE_TYPE (slot);
4151
4152 tree call_expr;
4153 enum style_t { ctor, arg, pcc } style;
4154
4155 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4156 style = ctor;
4157 #ifdef PCC_STATIC_STRUCT_RETURN
4158 else if (1)
4159 style = pcc;
4160 #endif
4161 else
4162 {
4163 gcc_assert (TREE_ADDRESSABLE (type));
4164 style = arg;
4165 }
4166
4167 call_expr = build_call_array_loc (input_location,
4168 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4169 fn,
4170 aggr_init_expr_nargs (aggr_init_expr),
4171 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4172 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4173 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4174 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4175 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4176 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4177 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4178
4179 if (style == ctor)
4180 {
4181 /* Replace the first argument to the ctor with the address of the
4182 slot. */
4183 cxx_mark_addressable (slot);
4184 CALL_EXPR_ARG (call_expr, 0) =
4185 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4186 }
4187 else if (style == arg)
4188 {
4189 /* Just mark it addressable here, and leave the rest to
4190 expand_call{,_inline}. */
4191 cxx_mark_addressable (slot);
4192 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4193 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4194 }
4195 else if (style == pcc)
4196 {
4197 /* If we're using the non-reentrant PCC calling convention, then we
4198 need to copy the returned value out of the static buffer into the
4199 SLOT. */
4200 push_deferring_access_checks (dk_no_check);
4201 call_expr = build_aggr_init (slot, call_expr,
4202 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4203 tf_warning_or_error);
4204 pop_deferring_access_checks ();
4205 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4206 }
4207
4208 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4209 {
4210 tree init = build_zero_init (type, NULL_TREE,
4211 /*static_storage_p=*/false);
4212 init = build2 (INIT_EXPR, void_type_node, slot, init);
4213 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4214 init, call_expr);
4215 }
4216
4217 *tp = call_expr;
4218 }
4219
4220 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4221
4222 void
4223 emit_associated_thunks (tree fn)
4224 {
4225 /* When we use vcall offsets, we emit thunks with the virtual
4226 functions to which they thunk. The whole point of vcall offsets
4227 is so that you can know statically the entire set of thunks that
4228 will ever be needed for a given virtual function, thereby
4229 enabling you to output all the thunks with the function itself. */
4230 if (DECL_VIRTUAL_P (fn)
4231 /* Do not emit thunks for extern template instantiations. */
4232 && ! DECL_REALLY_EXTERN (fn))
4233 {
4234 tree thunk;
4235
4236 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4237 {
4238 if (!THUNK_ALIAS (thunk))
4239 {
4240 use_thunk (thunk, /*emit_p=*/1);
4241 if (DECL_RESULT_THUNK_P (thunk))
4242 {
4243 tree probe;
4244
4245 for (probe = DECL_THUNKS (thunk);
4246 probe; probe = DECL_CHAIN (probe))
4247 use_thunk (probe, /*emit_p=*/1);
4248 }
4249 }
4250 else
4251 gcc_assert (!DECL_THUNKS (thunk));
4252 }
4253 }
4254 }
4255
4256 /* Generate RTL for FN. */
4257
4258 bool
4259 expand_or_defer_fn_1 (tree fn)
4260 {
4261 /* When the parser calls us after finishing the body of a template
4262 function, we don't really want to expand the body. */
4263 if (processing_template_decl)
4264 {
4265 /* Normally, collection only occurs in rest_of_compilation. So,
4266 if we don't collect here, we never collect junk generated
4267 during the processing of templates until we hit a
4268 non-template function. It's not safe to do this inside a
4269 nested class, though, as the parser may have local state that
4270 is not a GC root. */
4271 if (!function_depth)
4272 ggc_collect ();
4273 return false;
4274 }
4275
4276 gcc_assert (DECL_SAVED_TREE (fn));
4277
4278 /* We make a decision about linkage for these functions at the end
4279 of the compilation. Until that point, we do not want the back
4280 end to output them -- but we do want it to see the bodies of
4281 these functions so that it can inline them as appropriate. */
4282 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4283 {
4284 if (DECL_INTERFACE_KNOWN (fn))
4285 /* We've already made a decision as to how this function will
4286 be handled. */;
4287 else if (!at_eof)
4288 tentative_decl_linkage (fn);
4289 else
4290 import_export_decl (fn);
4291
4292 /* If the user wants us to keep all inline functions, then mark
4293 this function as needed so that finish_file will make sure to
4294 output it later. Similarly, all dllexport'd functions must
4295 be emitted; there may be callers in other DLLs. */
4296 if (DECL_DECLARED_INLINE_P (fn)
4297 && !DECL_REALLY_EXTERN (fn)
4298 && (flag_keep_inline_functions
4299 || (flag_keep_inline_dllexport
4300 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4301 {
4302 mark_needed (fn);
4303 DECL_EXTERNAL (fn) = 0;
4304 }
4305 }
4306
4307 /* If this is a constructor or destructor body, we have to clone
4308 it. */
4309 if (maybe_clone_body (fn))
4310 {
4311 /* We don't want to process FN again, so pretend we've written
4312 it out, even though we haven't. */
4313 TREE_ASM_WRITTEN (fn) = 1;
4314 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4315 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4316 DECL_SAVED_TREE (fn) = NULL_TREE;
4317 return false;
4318 }
4319
4320 /* There's no reason to do any of the work here if we're only doing
4321 semantic analysis; this code just generates RTL. */
4322 if (flag_syntax_only)
4323 return false;
4324
4325 return true;
4326 }
4327
4328 void
4329 expand_or_defer_fn (tree fn)
4330 {
4331 if (expand_or_defer_fn_1 (fn))
4332 {
4333 function_depth++;
4334
4335 /* Expand or defer, at the whim of the compilation unit manager. */
4336 cgraph_node::finalize_function (fn, function_depth > 1);
4337 emit_associated_thunks (fn);
4338
4339 function_depth--;
4340 }
4341 }
4342
4343 struct nrv_data
4344 {
4345 nrv_data () : visited (37) {}
4346
4347 tree var;
4348 tree result;
4349 hash_table<nofree_ptr_hash <tree_node> > visited;
4350 };
4351
4352 /* Helper function for walk_tree, used by finalize_nrv below. */
4353
4354 static tree
4355 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4356 {
4357 struct nrv_data *dp = (struct nrv_data *)data;
4358 tree_node **slot;
4359
4360 /* No need to walk into types. There wouldn't be any need to walk into
4361 non-statements, except that we have to consider STMT_EXPRs. */
4362 if (TYPE_P (*tp))
4363 *walk_subtrees = 0;
4364 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4365 but differs from using NULL_TREE in that it indicates that we care
4366 about the value of the RESULT_DECL. */
4367 else if (TREE_CODE (*tp) == RETURN_EXPR)
4368 TREE_OPERAND (*tp, 0) = dp->result;
4369 /* Change all cleanups for the NRV to only run when an exception is
4370 thrown. */
4371 else if (TREE_CODE (*tp) == CLEANUP_STMT
4372 && CLEANUP_DECL (*tp) == dp->var)
4373 CLEANUP_EH_ONLY (*tp) = 1;
4374 /* Replace the DECL_EXPR for the NRV with an initialization of the
4375 RESULT_DECL, if needed. */
4376 else if (TREE_CODE (*tp) == DECL_EXPR
4377 && DECL_EXPR_DECL (*tp) == dp->var)
4378 {
4379 tree init;
4380 if (DECL_INITIAL (dp->var)
4381 && DECL_INITIAL (dp->var) != error_mark_node)
4382 init = build2 (INIT_EXPR, void_type_node, dp->result,
4383 DECL_INITIAL (dp->var));
4384 else
4385 init = build_empty_stmt (EXPR_LOCATION (*tp));
4386 DECL_INITIAL (dp->var) = NULL_TREE;
4387 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4388 *tp = init;
4389 }
4390 /* And replace all uses of the NRV with the RESULT_DECL. */
4391 else if (*tp == dp->var)
4392 *tp = dp->result;
4393
4394 /* Avoid walking into the same tree more than once. Unfortunately, we
4395 can't just use walk_tree_without duplicates because it would only call
4396 us for the first occurrence of dp->var in the function body. */
4397 slot = dp->visited.find_slot (*tp, INSERT);
4398 if (*slot)
4399 *walk_subtrees = 0;
4400 else
4401 *slot = *tp;
4402
4403 /* Keep iterating. */
4404 return NULL_TREE;
4405 }
4406
4407 /* Called from finish_function to implement the named return value
4408 optimization by overriding all the RETURN_EXPRs and pertinent
4409 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4410 RESULT_DECL for the function. */
4411
4412 void
4413 finalize_nrv (tree *tp, tree var, tree result)
4414 {
4415 struct nrv_data data;
4416
4417 /* Copy name from VAR to RESULT. */
4418 DECL_NAME (result) = DECL_NAME (var);
4419 /* Don't forget that we take its address. */
4420 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4421 /* Finally set DECL_VALUE_EXPR to avoid assigning
4422 a stack slot at -O0 for the original var and debug info
4423 uses RESULT location for VAR. */
4424 SET_DECL_VALUE_EXPR (var, result);
4425 DECL_HAS_VALUE_EXPR_P (var) = 1;
4426
4427 data.var = var;
4428 data.result = result;
4429 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4430 }
4431 \f
4432 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4433
4434 bool
4435 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4436 bool need_copy_ctor, bool need_copy_assignment,
4437 bool need_dtor)
4438 {
4439 int save_errorcount = errorcount;
4440 tree info, t;
4441
4442 /* Always allocate 3 elements for simplicity. These are the
4443 function decls for the ctor, dtor, and assignment op.
4444 This layout is known to the three lang hooks,
4445 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4446 and cxx_omp_clause_assign_op. */
4447 info = make_tree_vec (3);
4448 CP_OMP_CLAUSE_INFO (c) = info;
4449
4450 if (need_default_ctor || need_copy_ctor)
4451 {
4452 if (need_default_ctor)
4453 t = get_default_ctor (type);
4454 else
4455 t = get_copy_ctor (type, tf_warning_or_error);
4456
4457 if (t && !trivial_fn_p (t))
4458 TREE_VEC_ELT (info, 0) = t;
4459 }
4460
4461 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4462 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4463
4464 if (need_copy_assignment)
4465 {
4466 t = get_copy_assign (type);
4467
4468 if (t && !trivial_fn_p (t))
4469 TREE_VEC_ELT (info, 2) = t;
4470 }
4471
4472 return errorcount != save_errorcount;
4473 }
4474
4475 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4476 FIELD_DECL, otherwise return DECL itself. */
4477
4478 static tree
4479 omp_clause_decl_field (tree decl)
4480 {
4481 if (VAR_P (decl)
4482 && DECL_HAS_VALUE_EXPR_P (decl)
4483 && DECL_ARTIFICIAL (decl)
4484 && DECL_LANG_SPECIFIC (decl)
4485 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4486 {
4487 tree f = DECL_VALUE_EXPR (decl);
4488 if (INDIRECT_REF_P (f))
4489 f = TREE_OPERAND (f, 0);
4490 if (TREE_CODE (f) == COMPONENT_REF)
4491 {
4492 f = TREE_OPERAND (f, 1);
4493 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4494 return f;
4495 }
4496 }
4497 return NULL_TREE;
4498 }
4499
4500 /* Adjust DECL if needed for printing using %qE. */
4501
4502 static tree
4503 omp_clause_printable_decl (tree decl)
4504 {
4505 tree t = omp_clause_decl_field (decl);
4506 if (t)
4507 return t;
4508 return decl;
4509 }
4510
4511 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4512 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4513 privatization. */
4514
4515 static void
4516 omp_note_field_privatization (tree f, tree t)
4517 {
4518 if (!omp_private_member_map)
4519 omp_private_member_map = new hash_map<tree, tree>;
4520 tree &v = omp_private_member_map->get_or_insert (f);
4521 if (v == NULL_TREE)
4522 {
4523 v = t;
4524 omp_private_member_vec.safe_push (f);
4525 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4526 omp_private_member_vec.safe_push (integer_zero_node);
4527 }
4528 }
4529
4530 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4531 dummy VAR_DECL. */
4532
4533 tree
4534 omp_privatize_field (tree t, bool shared)
4535 {
4536 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4537 if (m == error_mark_node)
4538 return error_mark_node;
4539 if (!omp_private_member_map && !shared)
4540 omp_private_member_map = new hash_map<tree, tree>;
4541 if (TYPE_REF_P (TREE_TYPE (t)))
4542 {
4543 gcc_assert (INDIRECT_REF_P (m));
4544 m = TREE_OPERAND (m, 0);
4545 }
4546 tree vb = NULL_TREE;
4547 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4548 if (v == NULL_TREE)
4549 {
4550 v = create_temporary_var (TREE_TYPE (m));
4551 retrofit_lang_decl (v);
4552 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4553 SET_DECL_VALUE_EXPR (v, m);
4554 DECL_HAS_VALUE_EXPR_P (v) = 1;
4555 if (!shared)
4556 omp_private_member_vec.safe_push (t);
4557 }
4558 return v;
4559 }
4560
4561 /* Helper function for handle_omp_array_sections. Called recursively
4562 to handle multiple array-section-subscripts. C is the clause,
4563 T current expression (initially OMP_CLAUSE_DECL), which is either
4564 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4565 expression if specified, TREE_VALUE length expression if specified,
4566 TREE_CHAIN is what it has been specified after, or some decl.
4567 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4568 set to true if any of the array-section-subscript could have length
4569 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4570 first array-section-subscript which is known not to have length
4571 of one. Given say:
4572 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4573 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4574 all are or may have length of 1, array-section-subscript [:2] is the
4575 first one known not to have length 1. For array-section-subscript
4576 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4577 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4578 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4579 case though, as some lengths could be zero. */
4580
4581 static tree
4582 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4583 bool &maybe_zero_len, unsigned int &first_non_one,
4584 enum c_omp_region_type ort)
4585 {
4586 tree ret, low_bound, length, type;
4587 if (TREE_CODE (t) != TREE_LIST)
4588 {
4589 if (error_operand_p (t))
4590 return error_mark_node;
4591 if (REFERENCE_REF_P (t)
4592 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4593 t = TREE_OPERAND (t, 0);
4594 ret = t;
4595 if (TREE_CODE (t) == COMPONENT_REF
4596 && ort == C_ORT_OMP
4597 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4598 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4599 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4600 && !type_dependent_expression_p (t))
4601 {
4602 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4603 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4604 {
4605 error_at (OMP_CLAUSE_LOCATION (c),
4606 "bit-field %qE in %qs clause",
4607 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4608 return error_mark_node;
4609 }
4610 while (TREE_CODE (t) == COMPONENT_REF)
4611 {
4612 if (TREE_TYPE (TREE_OPERAND (t, 0))
4613 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4614 {
4615 error_at (OMP_CLAUSE_LOCATION (c),
4616 "%qE is a member of a union", t);
4617 return error_mark_node;
4618 }
4619 t = TREE_OPERAND (t, 0);
4620 }
4621 if (REFERENCE_REF_P (t))
4622 t = TREE_OPERAND (t, 0);
4623 }
4624 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4625 {
4626 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4627 return NULL_TREE;
4628 if (DECL_P (t))
4629 error_at (OMP_CLAUSE_LOCATION (c),
4630 "%qD is not a variable in %qs clause", t,
4631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4632 else
4633 error_at (OMP_CLAUSE_LOCATION (c),
4634 "%qE is not a variable in %qs clause", t,
4635 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4636 return error_mark_node;
4637 }
4638 else if (ort == C_ORT_OMP
4639 && TREE_CODE (t) == PARM_DECL
4640 && DECL_ARTIFICIAL (t)
4641 && DECL_NAME (t) == this_identifier)
4642 {
4643 error_at (OMP_CLAUSE_LOCATION (c),
4644 "%<this%> allowed in OpenMP only in %<declare simd%>"
4645 " clauses");
4646 return error_mark_node;
4647 }
4648 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4649 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4650 {
4651 error_at (OMP_CLAUSE_LOCATION (c),
4652 "%qD is threadprivate variable in %qs clause", t,
4653 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4654 return error_mark_node;
4655 }
4656 if (type_dependent_expression_p (ret))
4657 return NULL_TREE;
4658 ret = convert_from_reference (ret);
4659 return ret;
4660 }
4661
4662 if (ort == C_ORT_OMP
4663 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4664 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4665 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4666 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4667 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4668 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4669 maybe_zero_len, first_non_one, ort);
4670 if (ret == error_mark_node || ret == NULL_TREE)
4671 return ret;
4672
4673 type = TREE_TYPE (ret);
4674 low_bound = TREE_PURPOSE (t);
4675 length = TREE_VALUE (t);
4676 if ((low_bound && type_dependent_expression_p (low_bound))
4677 || (length && type_dependent_expression_p (length)))
4678 return NULL_TREE;
4679
4680 if (low_bound == error_mark_node || length == error_mark_node)
4681 return error_mark_node;
4682
4683 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4684 {
4685 error_at (OMP_CLAUSE_LOCATION (c),
4686 "low bound %qE of array section does not have integral type",
4687 low_bound);
4688 return error_mark_node;
4689 }
4690 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4691 {
4692 error_at (OMP_CLAUSE_LOCATION (c),
4693 "length %qE of array section does not have integral type",
4694 length);
4695 return error_mark_node;
4696 }
4697 if (low_bound)
4698 low_bound = mark_rvalue_use (low_bound);
4699 if (length)
4700 length = mark_rvalue_use (length);
4701 /* We need to reduce to real constant-values for checks below. */
4702 if (length)
4703 length = fold_simple (length);
4704 if (low_bound)
4705 low_bound = fold_simple (low_bound);
4706 if (low_bound
4707 && TREE_CODE (low_bound) == INTEGER_CST
4708 && TYPE_PRECISION (TREE_TYPE (low_bound))
4709 > TYPE_PRECISION (sizetype))
4710 low_bound = fold_convert (sizetype, low_bound);
4711 if (length
4712 && TREE_CODE (length) == INTEGER_CST
4713 && TYPE_PRECISION (TREE_TYPE (length))
4714 > TYPE_PRECISION (sizetype))
4715 length = fold_convert (sizetype, length);
4716 if (low_bound == NULL_TREE)
4717 low_bound = integer_zero_node;
4718
4719 if (length != NULL_TREE)
4720 {
4721 if (!integer_nonzerop (length))
4722 {
4723 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4724 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4725 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4727 {
4728 if (integer_zerop (length))
4729 {
4730 error_at (OMP_CLAUSE_LOCATION (c),
4731 "zero length array section in %qs clause",
4732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4733 return error_mark_node;
4734 }
4735 }
4736 else
4737 maybe_zero_len = true;
4738 }
4739 if (first_non_one == types.length ()
4740 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4741 first_non_one++;
4742 }
4743 if (TREE_CODE (type) == ARRAY_TYPE)
4744 {
4745 if (length == NULL_TREE
4746 && (TYPE_DOMAIN (type) == NULL_TREE
4747 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4748 {
4749 error_at (OMP_CLAUSE_LOCATION (c),
4750 "for unknown bound array type length expression must "
4751 "be specified");
4752 return error_mark_node;
4753 }
4754 if (TREE_CODE (low_bound) == INTEGER_CST
4755 && tree_int_cst_sgn (low_bound) == -1)
4756 {
4757 error_at (OMP_CLAUSE_LOCATION (c),
4758 "negative low bound in array section in %qs clause",
4759 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4760 return error_mark_node;
4761 }
4762 if (length != NULL_TREE
4763 && TREE_CODE (length) == INTEGER_CST
4764 && tree_int_cst_sgn (length) == -1)
4765 {
4766 error_at (OMP_CLAUSE_LOCATION (c),
4767 "negative length in array section in %qs clause",
4768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4769 return error_mark_node;
4770 }
4771 if (TYPE_DOMAIN (type)
4772 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4773 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4774 == INTEGER_CST)
4775 {
4776 tree size
4777 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4778 size = size_binop (PLUS_EXPR, size, size_one_node);
4779 if (TREE_CODE (low_bound) == INTEGER_CST)
4780 {
4781 if (tree_int_cst_lt (size, low_bound))
4782 {
4783 error_at (OMP_CLAUSE_LOCATION (c),
4784 "low bound %qE above array section size "
4785 "in %qs clause", low_bound,
4786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4787 return error_mark_node;
4788 }
4789 if (tree_int_cst_equal (size, low_bound))
4790 {
4791 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4792 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4793 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4794 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4795 {
4796 error_at (OMP_CLAUSE_LOCATION (c),
4797 "zero length array section in %qs clause",
4798 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4799 return error_mark_node;
4800 }
4801 maybe_zero_len = true;
4802 }
4803 else if (length == NULL_TREE
4804 && first_non_one == types.length ()
4805 && tree_int_cst_equal
4806 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4807 low_bound))
4808 first_non_one++;
4809 }
4810 else if (length == NULL_TREE)
4811 {
4812 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4813 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4814 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4815 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4816 maybe_zero_len = true;
4817 if (first_non_one == types.length ())
4818 first_non_one++;
4819 }
4820 if (length && TREE_CODE (length) == INTEGER_CST)
4821 {
4822 if (tree_int_cst_lt (size, length))
4823 {
4824 error_at (OMP_CLAUSE_LOCATION (c),
4825 "length %qE above array section size "
4826 "in %qs clause", length,
4827 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4828 return error_mark_node;
4829 }
4830 if (TREE_CODE (low_bound) == INTEGER_CST)
4831 {
4832 tree lbpluslen
4833 = size_binop (PLUS_EXPR,
4834 fold_convert (sizetype, low_bound),
4835 fold_convert (sizetype, length));
4836 if (TREE_CODE (lbpluslen) == INTEGER_CST
4837 && tree_int_cst_lt (size, lbpluslen))
4838 {
4839 error_at (OMP_CLAUSE_LOCATION (c),
4840 "high bound %qE above array section size "
4841 "in %qs clause", lbpluslen,
4842 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4843 return error_mark_node;
4844 }
4845 }
4846 }
4847 }
4848 else if (length == NULL_TREE)
4849 {
4850 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4851 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4852 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4853 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4854 maybe_zero_len = true;
4855 if (first_non_one == types.length ())
4856 first_non_one++;
4857 }
4858
4859 /* For [lb:] we will need to evaluate lb more than once. */
4860 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4861 {
4862 tree lb = cp_save_expr (low_bound);
4863 if (lb != low_bound)
4864 {
4865 TREE_PURPOSE (t) = lb;
4866 low_bound = lb;
4867 }
4868 }
4869 }
4870 else if (TYPE_PTR_P (type))
4871 {
4872 if (length == NULL_TREE)
4873 {
4874 error_at (OMP_CLAUSE_LOCATION (c),
4875 "for pointer type length expression must be specified");
4876 return error_mark_node;
4877 }
4878 if (length != NULL_TREE
4879 && TREE_CODE (length) == INTEGER_CST
4880 && tree_int_cst_sgn (length) == -1)
4881 {
4882 error_at (OMP_CLAUSE_LOCATION (c),
4883 "negative length in array section in %qs clause",
4884 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4885 return error_mark_node;
4886 }
4887 /* If there is a pointer type anywhere but in the very first
4888 array-section-subscript, the array section can't be contiguous. */
4889 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4890 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4891 {
4892 error_at (OMP_CLAUSE_LOCATION (c),
4893 "array section is not contiguous in %qs clause",
4894 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4895 return error_mark_node;
4896 }
4897 }
4898 else
4899 {
4900 error_at (OMP_CLAUSE_LOCATION (c),
4901 "%qE does not have pointer or array type", ret);
4902 return error_mark_node;
4903 }
4904 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4905 types.safe_push (TREE_TYPE (ret));
4906 /* We will need to evaluate lb more than once. */
4907 tree lb = cp_save_expr (low_bound);
4908 if (lb != low_bound)
4909 {
4910 TREE_PURPOSE (t) = lb;
4911 low_bound = lb;
4912 }
4913 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4914 return ret;
4915 }
4916
4917 /* Handle array sections for clause C. */
4918
4919 static bool
4920 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4921 {
4922 bool maybe_zero_len = false;
4923 unsigned int first_non_one = 0;
4924 auto_vec<tree, 10> types;
4925 tree *tp = &OMP_CLAUSE_DECL (c);
4926 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4927 && TREE_CODE (*tp) == TREE_LIST
4928 && TREE_PURPOSE (*tp)
4929 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
4930 tp = &TREE_VALUE (*tp);
4931 tree first = handle_omp_array_sections_1 (c, *tp, types,
4932 maybe_zero_len, first_non_one,
4933 ort);
4934 if (first == error_mark_node)
4935 return true;
4936 if (first == NULL_TREE)
4937 return false;
4938 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4939 {
4940 tree t = *tp;
4941 tree tem = NULL_TREE;
4942 if (processing_template_decl)
4943 return false;
4944 /* Need to evaluate side effects in the length expressions
4945 if any. */
4946 while (TREE_CODE (t) == TREE_LIST)
4947 {
4948 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4949 {
4950 if (tem == NULL_TREE)
4951 tem = TREE_VALUE (t);
4952 else
4953 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4954 TREE_VALUE (t), tem);
4955 }
4956 t = TREE_CHAIN (t);
4957 }
4958 if (tem)
4959 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4960 *tp = first;
4961 }
4962 else
4963 {
4964 unsigned int num = types.length (), i;
4965 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4966 tree condition = NULL_TREE;
4967
4968 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4969 maybe_zero_len = true;
4970 if (processing_template_decl && maybe_zero_len)
4971 return false;
4972
4973 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4974 t = TREE_CHAIN (t))
4975 {
4976 tree low_bound = TREE_PURPOSE (t);
4977 tree length = TREE_VALUE (t);
4978
4979 i--;
4980 if (low_bound
4981 && TREE_CODE (low_bound) == INTEGER_CST
4982 && TYPE_PRECISION (TREE_TYPE (low_bound))
4983 > TYPE_PRECISION (sizetype))
4984 low_bound = fold_convert (sizetype, low_bound);
4985 if (length
4986 && TREE_CODE (length) == INTEGER_CST
4987 && TYPE_PRECISION (TREE_TYPE (length))
4988 > TYPE_PRECISION (sizetype))
4989 length = fold_convert (sizetype, length);
4990 if (low_bound == NULL_TREE)
4991 low_bound = integer_zero_node;
4992 if (!maybe_zero_len && i > first_non_one)
4993 {
4994 if (integer_nonzerop (low_bound))
4995 goto do_warn_noncontiguous;
4996 if (length != NULL_TREE
4997 && TREE_CODE (length) == INTEGER_CST
4998 && TYPE_DOMAIN (types[i])
4999 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5000 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5001 == INTEGER_CST)
5002 {
5003 tree size;
5004 size = size_binop (PLUS_EXPR,
5005 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5006 size_one_node);
5007 if (!tree_int_cst_equal (length, size))
5008 {
5009 do_warn_noncontiguous:
5010 error_at (OMP_CLAUSE_LOCATION (c),
5011 "array section is not contiguous in %qs "
5012 "clause",
5013 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5014 return true;
5015 }
5016 }
5017 if (!processing_template_decl
5018 && length != NULL_TREE
5019 && TREE_SIDE_EFFECTS (length))
5020 {
5021 if (side_effects == NULL_TREE)
5022 side_effects = length;
5023 else
5024 side_effects = build2 (COMPOUND_EXPR,
5025 TREE_TYPE (side_effects),
5026 length, side_effects);
5027 }
5028 }
5029 else if (processing_template_decl)
5030 continue;
5031 else
5032 {
5033 tree l;
5034
5035 if (i > first_non_one
5036 && ((length && integer_nonzerop (length))
5037 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5038 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5039 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5040 continue;
5041 if (length)
5042 l = fold_convert (sizetype, length);
5043 else
5044 {
5045 l = size_binop (PLUS_EXPR,
5046 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5047 size_one_node);
5048 l = size_binop (MINUS_EXPR, l,
5049 fold_convert (sizetype, low_bound));
5050 }
5051 if (i > first_non_one)
5052 {
5053 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5054 size_zero_node);
5055 if (condition == NULL_TREE)
5056 condition = l;
5057 else
5058 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5059 l, condition);
5060 }
5061 else if (size == NULL_TREE)
5062 {
5063 size = size_in_bytes (TREE_TYPE (types[i]));
5064 tree eltype = TREE_TYPE (types[num - 1]);
5065 while (TREE_CODE (eltype) == ARRAY_TYPE)
5066 eltype = TREE_TYPE (eltype);
5067 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5068 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5069 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5070 size = size_binop (EXACT_DIV_EXPR, size,
5071 size_in_bytes (eltype));
5072 size = size_binop (MULT_EXPR, size, l);
5073 if (condition)
5074 size = fold_build3 (COND_EXPR, sizetype, condition,
5075 size, size_zero_node);
5076 }
5077 else
5078 size = size_binop (MULT_EXPR, size, l);
5079 }
5080 }
5081 if (!processing_template_decl)
5082 {
5083 if (side_effects)
5084 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5085 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5086 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5087 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5088 {
5089 size = size_binop (MINUS_EXPR, size, size_one_node);
5090 size = save_expr (size);
5091 tree index_type = build_index_type (size);
5092 tree eltype = TREE_TYPE (first);
5093 while (TREE_CODE (eltype) == ARRAY_TYPE)
5094 eltype = TREE_TYPE (eltype);
5095 tree type = build_array_type (eltype, index_type);
5096 tree ptype = build_pointer_type (eltype);
5097 if (TYPE_REF_P (TREE_TYPE (t))
5098 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5099 t = convert_from_reference (t);
5100 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5101 t = build_fold_addr_expr (t);
5102 tree t2 = build_fold_addr_expr (first);
5103 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5104 ptrdiff_type_node, t2);
5105 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5106 ptrdiff_type_node, t2,
5107 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5108 ptrdiff_type_node, t));
5109 if (tree_fits_shwi_p (t2))
5110 t = build2 (MEM_REF, type, t,
5111 build_int_cst (ptype, tree_to_shwi (t2)));
5112 else
5113 {
5114 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5115 sizetype, t2);
5116 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5117 TREE_TYPE (t), t, t2);
5118 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5119 }
5120 OMP_CLAUSE_DECL (c) = t;
5121 return false;
5122 }
5123 OMP_CLAUSE_DECL (c) = first;
5124 OMP_CLAUSE_SIZE (c) = size;
5125 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5126 || (TREE_CODE (t) == COMPONENT_REF
5127 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5128 return false;
5129 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5130 switch (OMP_CLAUSE_MAP_KIND (c))
5131 {
5132 case GOMP_MAP_ALLOC:
5133 case GOMP_MAP_TO:
5134 case GOMP_MAP_FROM:
5135 case GOMP_MAP_TOFROM:
5136 case GOMP_MAP_ALWAYS_TO:
5137 case GOMP_MAP_ALWAYS_FROM:
5138 case GOMP_MAP_ALWAYS_TOFROM:
5139 case GOMP_MAP_RELEASE:
5140 case GOMP_MAP_DELETE:
5141 case GOMP_MAP_FORCE_TO:
5142 case GOMP_MAP_FORCE_FROM:
5143 case GOMP_MAP_FORCE_TOFROM:
5144 case GOMP_MAP_FORCE_PRESENT:
5145 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5146 break;
5147 default:
5148 break;
5149 }
5150 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5151 OMP_CLAUSE_MAP);
5152 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5153 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5154 else if (TREE_CODE (t) == COMPONENT_REF)
5155 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5156 else if (REFERENCE_REF_P (t)
5157 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5158 {
5159 t = TREE_OPERAND (t, 0);
5160 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5161 }
5162 else
5163 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5164 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5165 && !cxx_mark_addressable (t))
5166 return false;
5167 OMP_CLAUSE_DECL (c2) = t;
5168 t = build_fold_addr_expr (first);
5169 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5170 ptrdiff_type_node, t);
5171 tree ptr = OMP_CLAUSE_DECL (c2);
5172 ptr = convert_from_reference (ptr);
5173 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5174 ptr = build_fold_addr_expr (ptr);
5175 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5176 ptrdiff_type_node, t,
5177 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5178 ptrdiff_type_node, ptr));
5179 OMP_CLAUSE_SIZE (c2) = t;
5180 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5181 OMP_CLAUSE_CHAIN (c) = c2;
5182 ptr = OMP_CLAUSE_DECL (c2);
5183 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5184 && TYPE_REF_P (TREE_TYPE (ptr))
5185 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5186 {
5187 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5188 OMP_CLAUSE_MAP);
5189 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5190 OMP_CLAUSE_DECL (c3) = ptr;
5191 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5192 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5193 else
5194 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5195 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5196 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5197 OMP_CLAUSE_CHAIN (c2) = c3;
5198 }
5199 }
5200 }
5201 return false;
5202 }
5203
5204 /* Return identifier to look up for omp declare reduction. */
5205
5206 tree
5207 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5208 {
5209 const char *p = NULL;
5210 const char *m = NULL;
5211 switch (reduction_code)
5212 {
5213 case PLUS_EXPR:
5214 case MULT_EXPR:
5215 case MINUS_EXPR:
5216 case BIT_AND_EXPR:
5217 case BIT_XOR_EXPR:
5218 case BIT_IOR_EXPR:
5219 case TRUTH_ANDIF_EXPR:
5220 case TRUTH_ORIF_EXPR:
5221 reduction_id = ovl_op_identifier (false, reduction_code);
5222 break;
5223 case MIN_EXPR:
5224 p = "min";
5225 break;
5226 case MAX_EXPR:
5227 p = "max";
5228 break;
5229 default:
5230 break;
5231 }
5232
5233 if (p == NULL)
5234 {
5235 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5236 return error_mark_node;
5237 p = IDENTIFIER_POINTER (reduction_id);
5238 }
5239
5240 if (type != NULL_TREE)
5241 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5242
5243 const char prefix[] = "omp declare reduction ";
5244 size_t lenp = sizeof (prefix);
5245 if (strncmp (p, prefix, lenp - 1) == 0)
5246 lenp = 1;
5247 size_t len = strlen (p);
5248 size_t lenm = m ? strlen (m) + 1 : 0;
5249 char *name = XALLOCAVEC (char, lenp + len + lenm);
5250 if (lenp > 1)
5251 memcpy (name, prefix, lenp - 1);
5252 memcpy (name + lenp - 1, p, len + 1);
5253 if (m)
5254 {
5255 name[lenp + len - 1] = '~';
5256 memcpy (name + lenp + len, m, lenm);
5257 }
5258 return get_identifier (name);
5259 }
5260
5261 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5262 FUNCTION_DECL or NULL_TREE if not found. */
5263
5264 static tree
5265 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5266 vec<tree> *ambiguousp)
5267 {
5268 tree orig_id = id;
5269 tree baselink = NULL_TREE;
5270 if (identifier_p (id))
5271 {
5272 cp_id_kind idk;
5273 bool nonint_cst_expression_p;
5274 const char *error_msg;
5275 id = omp_reduction_id (ERROR_MARK, id, type);
5276 tree decl = lookup_name (id);
5277 if (decl == NULL_TREE)
5278 decl = error_mark_node;
5279 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5280 &nonint_cst_expression_p, false, true, false,
5281 false, &error_msg, loc);
5282 if (idk == CP_ID_KIND_UNQUALIFIED
5283 && identifier_p (id))
5284 {
5285 vec<tree, va_gc> *args = NULL;
5286 vec_safe_push (args, build_reference_type (type));
5287 id = perform_koenig_lookup (id, args, tf_none);
5288 }
5289 }
5290 else if (TREE_CODE (id) == SCOPE_REF)
5291 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5292 omp_reduction_id (ERROR_MARK,
5293 TREE_OPERAND (id, 1),
5294 type),
5295 false, false);
5296 tree fns = id;
5297 id = NULL_TREE;
5298 if (fns && is_overloaded_fn (fns))
5299 {
5300 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5301 {
5302 tree fndecl = *iter;
5303 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5304 {
5305 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5306 if (same_type_p (TREE_TYPE (argtype), type))
5307 {
5308 id = fndecl;
5309 break;
5310 }
5311 }
5312 }
5313
5314 if (id && BASELINK_P (fns))
5315 {
5316 if (baselinkp)
5317 *baselinkp = fns;
5318 else
5319 baselink = fns;
5320 }
5321 }
5322
5323 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5324 {
5325 vec<tree> ambiguous = vNULL;
5326 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5327 unsigned int ix;
5328 if (ambiguousp == NULL)
5329 ambiguousp = &ambiguous;
5330 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5331 {
5332 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5333 baselinkp ? baselinkp : &baselink,
5334 ambiguousp);
5335 if (id == NULL_TREE)
5336 continue;
5337 if (!ambiguousp->is_empty ())
5338 ambiguousp->safe_push (id);
5339 else if (ret != NULL_TREE)
5340 {
5341 ambiguousp->safe_push (ret);
5342 ambiguousp->safe_push (id);
5343 ret = NULL_TREE;
5344 }
5345 else
5346 ret = id;
5347 }
5348 if (ambiguousp != &ambiguous)
5349 return ret;
5350 if (!ambiguous.is_empty ())
5351 {
5352 const char *str = _("candidates are:");
5353 unsigned int idx;
5354 tree udr;
5355 error_at (loc, "user defined reduction lookup is ambiguous");
5356 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5357 {
5358 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5359 if (idx == 0)
5360 str = get_spaces (str);
5361 }
5362 ambiguous.release ();
5363 ret = error_mark_node;
5364 baselink = NULL_TREE;
5365 }
5366 id = ret;
5367 }
5368 if (id && baselink)
5369 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5370 id, id, tf_warning_or_error);
5371 return id;
5372 }
5373
5374 /* Helper function for cp_parser_omp_declare_reduction_exprs
5375 and tsubst_omp_udr.
5376 Remove CLEANUP_STMT for data (omp_priv variable).
5377 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5378 DECL_EXPR. */
5379
5380 tree
5381 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5382 {
5383 if (TYPE_P (*tp))
5384 *walk_subtrees = 0;
5385 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5386 *tp = CLEANUP_BODY (*tp);
5387 else if (TREE_CODE (*tp) == DECL_EXPR)
5388 {
5389 tree decl = DECL_EXPR_DECL (*tp);
5390 if (!processing_template_decl
5391 && decl == (tree) data
5392 && DECL_INITIAL (decl)
5393 && DECL_INITIAL (decl) != error_mark_node)
5394 {
5395 tree list = NULL_TREE;
5396 append_to_statement_list_force (*tp, &list);
5397 tree init_expr = build2 (INIT_EXPR, void_type_node,
5398 decl, DECL_INITIAL (decl));
5399 DECL_INITIAL (decl) = NULL_TREE;
5400 append_to_statement_list_force (init_expr, &list);
5401 *tp = list;
5402 }
5403 }
5404 return NULL_TREE;
5405 }
5406
5407 /* Data passed from cp_check_omp_declare_reduction to
5408 cp_check_omp_declare_reduction_r. */
5409
5410 struct cp_check_omp_declare_reduction_data
5411 {
5412 location_t loc;
5413 tree stmts[7];
5414 bool combiner_p;
5415 };
5416
5417 /* Helper function for cp_check_omp_declare_reduction, called via
5418 cp_walk_tree. */
5419
5420 static tree
5421 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5422 {
5423 struct cp_check_omp_declare_reduction_data *udr_data
5424 = (struct cp_check_omp_declare_reduction_data *) data;
5425 if (SSA_VAR_P (*tp)
5426 && !DECL_ARTIFICIAL (*tp)
5427 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5428 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5429 {
5430 location_t loc = udr_data->loc;
5431 if (udr_data->combiner_p)
5432 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5433 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5434 *tp);
5435 else
5436 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5437 "to variable %qD which is not %<omp_priv%> nor "
5438 "%<omp_orig%>",
5439 *tp);
5440 return *tp;
5441 }
5442 return NULL_TREE;
5443 }
5444
5445 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5446
5447 void
5448 cp_check_omp_declare_reduction (tree udr)
5449 {
5450 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5451 gcc_assert (TYPE_REF_P (type));
5452 type = TREE_TYPE (type);
5453 int i;
5454 location_t loc = DECL_SOURCE_LOCATION (udr);
5455
5456 if (type == error_mark_node)
5457 return;
5458 if (ARITHMETIC_TYPE_P (type))
5459 {
5460 static enum tree_code predef_codes[]
5461 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5462 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5463 for (i = 0; i < 8; i++)
5464 {
5465 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5466 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5467 const char *n2 = IDENTIFIER_POINTER (id);
5468 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5469 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5470 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5471 break;
5472 }
5473
5474 if (i == 8
5475 && TREE_CODE (type) != COMPLEX_EXPR)
5476 {
5477 const char prefix_minmax[] = "omp declare reduction m";
5478 size_t prefix_size = sizeof (prefix_minmax) - 1;
5479 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5480 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5481 prefix_minmax, prefix_size) == 0
5482 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5483 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5484 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5485 i = 0;
5486 }
5487 if (i < 8)
5488 {
5489 error_at (loc, "predeclared arithmetic type %qT in "
5490 "%<#pragma omp declare reduction%>", type);
5491 return;
5492 }
5493 }
5494 else if (TREE_CODE (type) == FUNCTION_TYPE
5495 || TREE_CODE (type) == METHOD_TYPE
5496 || TREE_CODE (type) == ARRAY_TYPE)
5497 {
5498 error_at (loc, "function or array type %qT in "
5499 "%<#pragma omp declare reduction%>", type);
5500 return;
5501 }
5502 else if (TYPE_REF_P (type))
5503 {
5504 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5505 type);
5506 return;
5507 }
5508 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5509 {
5510 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5511 "%<#pragma omp declare reduction%>", type);
5512 return;
5513 }
5514
5515 tree body = DECL_SAVED_TREE (udr);
5516 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5517 return;
5518
5519 tree_stmt_iterator tsi;
5520 struct cp_check_omp_declare_reduction_data data;
5521 memset (data.stmts, 0, sizeof data.stmts);
5522 for (i = 0, tsi = tsi_start (body);
5523 i < 7 && !tsi_end_p (tsi);
5524 i++, tsi_next (&tsi))
5525 data.stmts[i] = tsi_stmt (tsi);
5526 data.loc = loc;
5527 gcc_assert (tsi_end_p (tsi));
5528 if (i >= 3)
5529 {
5530 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5531 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5532 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5533 return;
5534 data.combiner_p = true;
5535 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5536 &data, NULL))
5537 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5538 }
5539 if (i >= 6)
5540 {
5541 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5542 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5543 data.combiner_p = false;
5544 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5545 &data, NULL)
5546 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5547 cp_check_omp_declare_reduction_r, &data, NULL))
5548 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5549 if (i == 7)
5550 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5551 }
5552 }
5553
5554 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5555 an inline call. But, remap
5556 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5557 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5558
5559 static tree
5560 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5561 tree decl, tree placeholder)
5562 {
5563 copy_body_data id;
5564 hash_map<tree, tree> decl_map;
5565
5566 decl_map.put (omp_decl1, placeholder);
5567 decl_map.put (omp_decl2, decl);
5568 memset (&id, 0, sizeof (id));
5569 id.src_fn = DECL_CONTEXT (omp_decl1);
5570 id.dst_fn = current_function_decl;
5571 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5572 id.decl_map = &decl_map;
5573
5574 id.copy_decl = copy_decl_no_change;
5575 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5576 id.transform_new_cfg = true;
5577 id.transform_return_to_modify = false;
5578 id.transform_lang_insert_block = NULL;
5579 id.eh_lp_nr = 0;
5580 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5581 return stmt;
5582 }
5583
5584 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5585 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5586
5587 static tree
5588 find_omp_placeholder_r (tree *tp, int *, void *data)
5589 {
5590 if (*tp == (tree) data)
5591 return *tp;
5592 return NULL_TREE;
5593 }
5594
5595 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5596 Return true if there is some error and the clause should be removed. */
5597
5598 static bool
5599 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5600 {
5601 tree t = OMP_CLAUSE_DECL (c);
5602 bool predefined = false;
5603 if (TREE_CODE (t) == TREE_LIST)
5604 {
5605 gcc_assert (processing_template_decl);
5606 return false;
5607 }
5608 tree type = TREE_TYPE (t);
5609 if (TREE_CODE (t) == MEM_REF)
5610 type = TREE_TYPE (type);
5611 if (TYPE_REF_P (type))
5612 type = TREE_TYPE (type);
5613 if (TREE_CODE (type) == ARRAY_TYPE)
5614 {
5615 tree oatype = type;
5616 gcc_assert (TREE_CODE (t) != MEM_REF);
5617 while (TREE_CODE (type) == ARRAY_TYPE)
5618 type = TREE_TYPE (type);
5619 if (!processing_template_decl)
5620 {
5621 t = require_complete_type (t);
5622 if (t == error_mark_node)
5623 return true;
5624 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5625 TYPE_SIZE_UNIT (type));
5626 if (integer_zerop (size))
5627 {
5628 error_at (OMP_CLAUSE_LOCATION (c),
5629 "%qE in %<reduction%> clause is a zero size array",
5630 omp_clause_printable_decl (t));
5631 return true;
5632 }
5633 size = size_binop (MINUS_EXPR, size, size_one_node);
5634 size = save_expr (size);
5635 tree index_type = build_index_type (size);
5636 tree atype = build_array_type (type, index_type);
5637 tree ptype = build_pointer_type (type);
5638 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5639 t = build_fold_addr_expr (t);
5640 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5641 OMP_CLAUSE_DECL (c) = t;
5642 }
5643 }
5644 if (type == error_mark_node)
5645 return true;
5646 else if (ARITHMETIC_TYPE_P (type))
5647 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5648 {
5649 case PLUS_EXPR:
5650 case MULT_EXPR:
5651 case MINUS_EXPR:
5652 predefined = true;
5653 break;
5654 case MIN_EXPR:
5655 case MAX_EXPR:
5656 if (TREE_CODE (type) == COMPLEX_TYPE)
5657 break;
5658 predefined = true;
5659 break;
5660 case BIT_AND_EXPR:
5661 case BIT_IOR_EXPR:
5662 case BIT_XOR_EXPR:
5663 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5664 break;
5665 predefined = true;
5666 break;
5667 case TRUTH_ANDIF_EXPR:
5668 case TRUTH_ORIF_EXPR:
5669 if (FLOAT_TYPE_P (type))
5670 break;
5671 predefined = true;
5672 break;
5673 default:
5674 break;
5675 }
5676 else if (TYPE_READONLY (type))
5677 {
5678 error_at (OMP_CLAUSE_LOCATION (c),
5679 "%qE has const type for %<reduction%>",
5680 omp_clause_printable_decl (t));
5681 return true;
5682 }
5683 else if (!processing_template_decl)
5684 {
5685 t = require_complete_type (t);
5686 if (t == error_mark_node)
5687 return true;
5688 OMP_CLAUSE_DECL (c) = t;
5689 }
5690
5691 if (predefined)
5692 {
5693 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5694 return false;
5695 }
5696 else if (processing_template_decl)
5697 {
5698 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
5699 return true;
5700 return false;
5701 }
5702
5703 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5704
5705 type = TYPE_MAIN_VARIANT (type);
5706 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5707 if (id == NULL_TREE)
5708 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5709 NULL_TREE, NULL_TREE);
5710 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5711 if (id)
5712 {
5713 if (id == error_mark_node)
5714 return true;
5715 mark_used (id);
5716 tree body = DECL_SAVED_TREE (id);
5717 if (!body)
5718 return true;
5719 if (TREE_CODE (body) == STATEMENT_LIST)
5720 {
5721 tree_stmt_iterator tsi;
5722 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5723 int i;
5724 tree stmts[7];
5725 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5726 atype = TREE_TYPE (atype);
5727 bool need_static_cast = !same_type_p (type, atype);
5728 memset (stmts, 0, sizeof stmts);
5729 for (i = 0, tsi = tsi_start (body);
5730 i < 7 && !tsi_end_p (tsi);
5731 i++, tsi_next (&tsi))
5732 stmts[i] = tsi_stmt (tsi);
5733 gcc_assert (tsi_end_p (tsi));
5734
5735 if (i >= 3)
5736 {
5737 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5738 && TREE_CODE (stmts[1]) == DECL_EXPR);
5739 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5740 DECL_ARTIFICIAL (placeholder) = 1;
5741 DECL_IGNORED_P (placeholder) = 1;
5742 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5743 if (TREE_CODE (t) == MEM_REF)
5744 {
5745 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5746 type);
5747 DECL_ARTIFICIAL (decl_placeholder) = 1;
5748 DECL_IGNORED_P (decl_placeholder) = 1;
5749 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5750 }
5751 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5752 cxx_mark_addressable (placeholder);
5753 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5754 && (decl_placeholder
5755 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5756 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5757 : OMP_CLAUSE_DECL (c));
5758 tree omp_out = placeholder;
5759 tree omp_in = decl_placeholder ? decl_placeholder
5760 : convert_from_reference (OMP_CLAUSE_DECL (c));
5761 if (need_static_cast)
5762 {
5763 tree rtype = build_reference_type (atype);
5764 omp_out = build_static_cast (rtype, omp_out,
5765 tf_warning_or_error);
5766 omp_in = build_static_cast (rtype, omp_in,
5767 tf_warning_or_error);
5768 if (omp_out == error_mark_node || omp_in == error_mark_node)
5769 return true;
5770 omp_out = convert_from_reference (omp_out);
5771 omp_in = convert_from_reference (omp_in);
5772 }
5773 OMP_CLAUSE_REDUCTION_MERGE (c)
5774 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5775 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5776 }
5777 if (i >= 6)
5778 {
5779 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5780 && TREE_CODE (stmts[4]) == DECL_EXPR);
5781 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
5782 && (decl_placeholder
5783 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5784 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5785 : OMP_CLAUSE_DECL (c));
5786 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5787 cxx_mark_addressable (placeholder);
5788 tree omp_priv = decl_placeholder ? decl_placeholder
5789 : convert_from_reference (OMP_CLAUSE_DECL (c));
5790 tree omp_orig = placeholder;
5791 if (need_static_cast)
5792 {
5793 if (i == 7)
5794 {
5795 error_at (OMP_CLAUSE_LOCATION (c),
5796 "user defined reduction with constructor "
5797 "initializer for base class %qT", atype);
5798 return true;
5799 }
5800 tree rtype = build_reference_type (atype);
5801 omp_priv = build_static_cast (rtype, omp_priv,
5802 tf_warning_or_error);
5803 omp_orig = build_static_cast (rtype, omp_orig,
5804 tf_warning_or_error);
5805 if (omp_priv == error_mark_node
5806 || omp_orig == error_mark_node)
5807 return true;
5808 omp_priv = convert_from_reference (omp_priv);
5809 omp_orig = convert_from_reference (omp_orig);
5810 }
5811 if (i == 6)
5812 *need_default_ctor = true;
5813 OMP_CLAUSE_REDUCTION_INIT (c)
5814 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5815 DECL_EXPR_DECL (stmts[3]),
5816 omp_priv, omp_orig);
5817 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5818 find_omp_placeholder_r, placeholder, NULL))
5819 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5820 }
5821 else if (i >= 3)
5822 {
5823 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5824 *need_default_ctor = true;
5825 else
5826 {
5827 tree init;
5828 tree v = decl_placeholder ? decl_placeholder
5829 : convert_from_reference (t);
5830 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5831 init = build_constructor (TREE_TYPE (v), NULL);
5832 else
5833 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5834 OMP_CLAUSE_REDUCTION_INIT (c)
5835 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5836 }
5837 }
5838 }
5839 }
5840 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5841 *need_dtor = true;
5842 else
5843 {
5844 error_at (OMP_CLAUSE_LOCATION (c),
5845 "user defined reduction not found for %qE",
5846 omp_clause_printable_decl (t));
5847 return true;
5848 }
5849 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5850 gcc_assert (TYPE_SIZE_UNIT (type)
5851 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5852 return false;
5853 }
5854
5855 /* Called from finish_struct_1. linear(this) or linear(this:step)
5856 clauses might not be finalized yet because the class has been incomplete
5857 when parsing #pragma omp declare simd methods. Fix those up now. */
5858
5859 void
5860 finish_omp_declare_simd_methods (tree t)
5861 {
5862 if (processing_template_decl)
5863 return;
5864
5865 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5866 {
5867 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5868 continue;
5869 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5870 if (!ods || !TREE_VALUE (ods))
5871 continue;
5872 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5873 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5874 && integer_zerop (OMP_CLAUSE_DECL (c))
5875 && OMP_CLAUSE_LINEAR_STEP (c)
5876 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
5877 {
5878 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5879 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5880 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5881 sizetype, s, TYPE_SIZE_UNIT (t));
5882 OMP_CLAUSE_LINEAR_STEP (c) = s;
5883 }
5884 }
5885 }
5886
5887 /* Adjust sink depend clause to take into account pointer offsets.
5888
5889 Return TRUE if there was a problem processing the offset, and the
5890 whole clause should be removed. */
5891
5892 static bool
5893 cp_finish_omp_clause_depend_sink (tree sink_clause)
5894 {
5895 tree t = OMP_CLAUSE_DECL (sink_clause);
5896 gcc_assert (TREE_CODE (t) == TREE_LIST);
5897
5898 /* Make sure we don't adjust things twice for templates. */
5899 if (processing_template_decl)
5900 return false;
5901
5902 for (; t; t = TREE_CHAIN (t))
5903 {
5904 tree decl = TREE_VALUE (t);
5905 if (TYPE_PTR_P (TREE_TYPE (decl)))
5906 {
5907 tree offset = TREE_PURPOSE (t);
5908 bool neg = wi::neg_p (wi::to_wide (offset));
5909 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5910 decl = mark_rvalue_use (decl);
5911 decl = convert_from_reference (decl);
5912 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5913 neg ? MINUS_EXPR : PLUS_EXPR,
5914 decl, offset);
5915 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5916 MINUS_EXPR, sizetype,
5917 fold_convert (sizetype, t2),
5918 fold_convert (sizetype, decl));
5919 if (t2 == error_mark_node)
5920 return true;
5921 TREE_PURPOSE (t) = t2;
5922 }
5923 }
5924 return false;
5925 }
5926
5927 /* Finish OpenMP iterators ITER. Return true if they are errorneous
5928 and clauses containing them should be removed. */
5929
5930 static bool
5931 cp_omp_finish_iterators (tree iter)
5932 {
5933 bool ret = false;
5934 for (tree it = iter; it; it = TREE_CHAIN (it))
5935 {
5936 tree var = TREE_VEC_ELT (it, 0);
5937 tree begin = TREE_VEC_ELT (it, 1);
5938 tree end = TREE_VEC_ELT (it, 2);
5939 tree step = TREE_VEC_ELT (it, 3);
5940 tree orig_step;
5941 tree type = TREE_TYPE (var);
5942 location_t loc = DECL_SOURCE_LOCATION (var);
5943 if (type == error_mark_node)
5944 {
5945 ret = true;
5946 continue;
5947 }
5948 if (type_dependent_expression_p (var))
5949 continue;
5950 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
5951 {
5952 error_at (loc, "iterator %qD has neither integral nor pointer type",
5953 var);
5954 ret = true;
5955 continue;
5956 }
5957 else if (TYPE_READONLY (type))
5958 {
5959 error_at (loc, "iterator %qD has const qualified type", var);
5960 ret = true;
5961 continue;
5962 }
5963 if (type_dependent_expression_p (begin)
5964 || type_dependent_expression_p (end)
5965 || type_dependent_expression_p (step))
5966 continue;
5967 else if (error_operand_p (step))
5968 {
5969 ret = true;
5970 continue;
5971 }
5972 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
5973 {
5974 error_at (EXPR_LOC_OR_LOC (step, loc),
5975 "iterator step with non-integral type");
5976 ret = true;
5977 continue;
5978 }
5979
5980 begin = mark_rvalue_use (begin);
5981 end = mark_rvalue_use (end);
5982 step = mark_rvalue_use (step);
5983 begin = cp_build_c_cast (type, begin, tf_warning_or_error);
5984 end = cp_build_c_cast (type, end, tf_warning_or_error);
5985 orig_step = step;
5986 if (!processing_template_decl)
5987 step = orig_step = save_expr (step);
5988 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
5989 step = cp_build_c_cast (stype, step, tf_warning_or_error);
5990 if (POINTER_TYPE_P (type) && !processing_template_decl)
5991 {
5992 begin = save_expr (begin);
5993 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
5994 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
5995 fold_convert (sizetype, step),
5996 fold_convert (sizetype, begin));
5997 step = fold_convert (ssizetype, step);
5998 }
5999 if (!processing_template_decl)
6000 {
6001 begin = maybe_constant_value (begin);
6002 end = maybe_constant_value (end);
6003 step = maybe_constant_value (step);
6004 orig_step = maybe_constant_value (orig_step);
6005 }
6006 if (integer_zerop (step))
6007 {
6008 error_at (loc, "iterator %qD has zero step", var);
6009 ret = true;
6010 continue;
6011 }
6012
6013 if (begin == error_mark_node
6014 || end == error_mark_node
6015 || step == error_mark_node
6016 || orig_step == error_mark_node)
6017 {
6018 ret = true;
6019 continue;
6020 }
6021
6022 if (!processing_template_decl)
6023 {
6024 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6025 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6026 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6027 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6028 orig_step);
6029 }
6030 hash_set<tree> pset;
6031 tree it2;
6032 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6033 {
6034 tree var2 = TREE_VEC_ELT (it2, 0);
6035 tree begin2 = TREE_VEC_ELT (it2, 1);
6036 tree end2 = TREE_VEC_ELT (it2, 2);
6037 tree step2 = TREE_VEC_ELT (it2, 3);
6038 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6039 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6040 {
6041 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6042 "begin expression refers to outer iterator %qD", var);
6043 break;
6044 }
6045 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6046 {
6047 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6048 "end expression refers to outer iterator %qD", var);
6049 break;
6050 }
6051 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6052 {
6053 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6054 "step expression refers to outer iterator %qD", var);
6055 break;
6056 }
6057 }
6058 if (it2)
6059 {
6060 ret = true;
6061 continue;
6062 }
6063 TREE_VEC_ELT (it, 1) = begin;
6064 TREE_VEC_ELT (it, 2) = end;
6065 if (processing_template_decl)
6066 TREE_VEC_ELT (it, 3) = orig_step;
6067 else
6068 {
6069 TREE_VEC_ELT (it, 3) = step;
6070 TREE_VEC_ELT (it, 4) = orig_step;
6071 }
6072 }
6073 return ret;
6074 }
6075
6076 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6077 Remove any elements from the list that are invalid. */
6078
6079 tree
6080 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6081 {
6082 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6083 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
6084 tree c, t, *pc;
6085 tree safelen = NULL_TREE;
6086 bool branch_seen = false;
6087 bool copyprivate_seen = false;
6088 bool ordered_seen = false;
6089 bool oacc_async = false;
6090 tree last_iterators = NULL_TREE;
6091 bool last_iterators_remove = false;
6092 bool reduction_seen = false;
6093
6094 bitmap_obstack_initialize (NULL);
6095 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6096 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6097 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6098 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6099 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6100 bitmap_initialize (&map_head, &bitmap_default_obstack);
6101 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6102 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
6103 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6104
6105 if (ort & C_ORT_ACC)
6106 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6107 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6108 {
6109 oacc_async = true;
6110 break;
6111 }
6112
6113 for (pc = &clauses, c = clauses; c ; c = *pc)
6114 {
6115 bool remove = false;
6116 bool field_ok = false;
6117
6118 switch (OMP_CLAUSE_CODE (c))
6119 {
6120 case OMP_CLAUSE_SHARED:
6121 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6122 goto check_dup_generic;
6123 case OMP_CLAUSE_PRIVATE:
6124 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6125 goto check_dup_generic;
6126 case OMP_CLAUSE_REDUCTION:
6127 reduction_seen = true;
6128 /* FALLTHRU */
6129 case OMP_CLAUSE_IN_REDUCTION:
6130 case OMP_CLAUSE_TASK_REDUCTION:
6131 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6132 t = OMP_CLAUSE_DECL (c);
6133 if (TREE_CODE (t) == TREE_LIST)
6134 {
6135 if (handle_omp_array_sections (c, ort))
6136 {
6137 remove = true;
6138 break;
6139 }
6140 if (TREE_CODE (t) == TREE_LIST)
6141 {
6142 while (TREE_CODE (t) == TREE_LIST)
6143 t = TREE_CHAIN (t);
6144 }
6145 else
6146 {
6147 gcc_assert (TREE_CODE (t) == MEM_REF);
6148 t = TREE_OPERAND (t, 0);
6149 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6150 t = TREE_OPERAND (t, 0);
6151 if (TREE_CODE (t) == ADDR_EXPR
6152 || INDIRECT_REF_P (t))
6153 t = TREE_OPERAND (t, 0);
6154 }
6155 tree n = omp_clause_decl_field (t);
6156 if (n)
6157 t = n;
6158 goto check_dup_generic_t;
6159 }
6160 if (oacc_async)
6161 cxx_mark_addressable (t);
6162 goto check_dup_generic;
6163 case OMP_CLAUSE_COPYPRIVATE:
6164 copyprivate_seen = true;
6165 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6166 goto check_dup_generic;
6167 case OMP_CLAUSE_COPYIN:
6168 goto check_dup_generic;
6169 case OMP_CLAUSE_LINEAR:
6170 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6171 t = OMP_CLAUSE_DECL (c);
6172 if (ort != C_ORT_OMP_DECLARE_SIMD
6173 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6174 {
6175 error_at (OMP_CLAUSE_LOCATION (c),
6176 "modifier should not be specified in %<linear%> "
6177 "clause on %<simd%> or %<for%> constructs");
6178 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6179 }
6180 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6181 && !type_dependent_expression_p (t))
6182 {
6183 tree type = TREE_TYPE (t);
6184 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6185 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6186 && !TYPE_REF_P (type))
6187 {
6188 error_at (OMP_CLAUSE_LOCATION (c),
6189 "linear clause with %qs modifier applied to "
6190 "non-reference variable with %qT type",
6191 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6192 ? "ref" : "uval", TREE_TYPE (t));
6193 remove = true;
6194 break;
6195 }
6196 if (TYPE_REF_P (type))
6197 type = TREE_TYPE (type);
6198 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6199 {
6200 if (!INTEGRAL_TYPE_P (type)
6201 && !TYPE_PTR_P (type))
6202 {
6203 error_at (OMP_CLAUSE_LOCATION (c),
6204 "linear clause applied to non-integral "
6205 "non-pointer variable with %qT type",
6206 TREE_TYPE (t));
6207 remove = true;
6208 break;
6209 }
6210 }
6211 }
6212 t = OMP_CLAUSE_LINEAR_STEP (c);
6213 if (t == NULL_TREE)
6214 t = integer_one_node;
6215 if (t == error_mark_node)
6216 {
6217 remove = true;
6218 break;
6219 }
6220 else if (!type_dependent_expression_p (t)
6221 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6222 && (ort != C_ORT_OMP_DECLARE_SIMD
6223 || TREE_CODE (t) != PARM_DECL
6224 || !TYPE_REF_P (TREE_TYPE (t))
6225 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6226 {
6227 error_at (OMP_CLAUSE_LOCATION (c),
6228 "linear step expression must be integral");
6229 remove = true;
6230 break;
6231 }
6232 else
6233 {
6234 t = mark_rvalue_use (t);
6235 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6236 {
6237 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6238 goto check_dup_generic;
6239 }
6240 if (!processing_template_decl
6241 && (VAR_P (OMP_CLAUSE_DECL (c))
6242 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6243 {
6244 if (ort == C_ORT_OMP_DECLARE_SIMD)
6245 {
6246 t = maybe_constant_value (t);
6247 if (TREE_CODE (t) != INTEGER_CST)
6248 {
6249 error_at (OMP_CLAUSE_LOCATION (c),
6250 "%<linear%> clause step %qE is neither "
6251 "constant nor a parameter", t);
6252 remove = true;
6253 break;
6254 }
6255 }
6256 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6257 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6258 if (TYPE_REF_P (type))
6259 type = TREE_TYPE (type);
6260 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6261 {
6262 type = build_pointer_type (type);
6263 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6264 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6265 d, t);
6266 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6267 MINUS_EXPR, sizetype,
6268 fold_convert (sizetype, t),
6269 fold_convert (sizetype, d));
6270 if (t == error_mark_node)
6271 {
6272 remove = true;
6273 break;
6274 }
6275 }
6276 else if (TYPE_PTR_P (type)
6277 /* Can't multiply the step yet if *this
6278 is still incomplete type. */
6279 && (ort != C_ORT_OMP_DECLARE_SIMD
6280 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6281 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6282 || DECL_NAME (OMP_CLAUSE_DECL (c))
6283 != this_identifier
6284 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6285 {
6286 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6287 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6288 d, t);
6289 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6290 MINUS_EXPR, sizetype,
6291 fold_convert (sizetype, t),
6292 fold_convert (sizetype, d));
6293 if (t == error_mark_node)
6294 {
6295 remove = true;
6296 break;
6297 }
6298 }
6299 else
6300 t = fold_convert (type, t);
6301 }
6302 OMP_CLAUSE_LINEAR_STEP (c) = t;
6303 }
6304 goto check_dup_generic;
6305 check_dup_generic:
6306 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6307 if (t)
6308 {
6309 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6310 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6311 }
6312 else
6313 t = OMP_CLAUSE_DECL (c);
6314 check_dup_generic_t:
6315 if (t == current_class_ptr
6316 && (ort != C_ORT_OMP_DECLARE_SIMD
6317 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6318 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6319 {
6320 error_at (OMP_CLAUSE_LOCATION (c),
6321 "%<this%> allowed in OpenMP only in %<declare simd%>"
6322 " clauses");
6323 remove = true;
6324 break;
6325 }
6326 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6327 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6328 {
6329 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6330 break;
6331 if (DECL_P (t))
6332 error_at (OMP_CLAUSE_LOCATION (c),
6333 "%qD is not a variable in clause %qs", t,
6334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6335 else
6336 error_at (OMP_CLAUSE_LOCATION (c),
6337 "%qE is not a variable in clause %qs", t,
6338 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6339 remove = true;
6340 }
6341 else if (ort == C_ORT_ACC
6342 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6343 {
6344 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6345 {
6346 error_at (OMP_CLAUSE_LOCATION (c),
6347 "%qD appears more than once in reduction clauses",
6348 t);
6349 remove = true;
6350 }
6351 else
6352 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6353 }
6354 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6355 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6356 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6357 {
6358 error_at (OMP_CLAUSE_LOCATION (c),
6359 "%qD appears more than once in data clauses", t);
6360 remove = true;
6361 }
6362 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6363 && bitmap_bit_p (&map_head, DECL_UID (t)))
6364 {
6365 if (ort == C_ORT_ACC)
6366 error_at (OMP_CLAUSE_LOCATION (c),
6367 "%qD appears more than once in data clauses", t);
6368 else
6369 error_at (OMP_CLAUSE_LOCATION (c),
6370 "%qD appears both in data and map clauses", t);
6371 remove = true;
6372 }
6373 else
6374 bitmap_set_bit (&generic_head, DECL_UID (t));
6375 if (!field_ok)
6376 break;
6377 handle_field_decl:
6378 if (!remove
6379 && TREE_CODE (t) == FIELD_DECL
6380 && t == OMP_CLAUSE_DECL (c)
6381 && ort != C_ORT_ACC)
6382 {
6383 OMP_CLAUSE_DECL (c)
6384 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6385 == OMP_CLAUSE_SHARED));
6386 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6387 remove = true;
6388 }
6389 break;
6390
6391 case OMP_CLAUSE_FIRSTPRIVATE:
6392 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6393 if (t)
6394 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6395 else
6396 t = OMP_CLAUSE_DECL (c);
6397 if (ort != C_ORT_ACC && t == current_class_ptr)
6398 {
6399 error_at (OMP_CLAUSE_LOCATION (c),
6400 "%<this%> allowed in OpenMP only in %<declare simd%>"
6401 " clauses");
6402 remove = true;
6403 break;
6404 }
6405 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6406 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6407 || TREE_CODE (t) != FIELD_DECL))
6408 {
6409 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6410 break;
6411 if (DECL_P (t))
6412 error_at (OMP_CLAUSE_LOCATION (c),
6413 "%qD is not a variable in clause %<firstprivate%>",
6414 t);
6415 else
6416 error_at (OMP_CLAUSE_LOCATION (c),
6417 "%qE is not a variable in clause %<firstprivate%>",
6418 t);
6419 remove = true;
6420 }
6421 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6422 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6423 {
6424 error_at (OMP_CLAUSE_LOCATION (c),
6425 "%qD appears more than once in data clauses", t);
6426 remove = true;
6427 }
6428 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6429 {
6430 if (ort == C_ORT_ACC)
6431 error_at (OMP_CLAUSE_LOCATION (c),
6432 "%qD appears more than once in data clauses", t);
6433 else
6434 error_at (OMP_CLAUSE_LOCATION (c),
6435 "%qD appears both in data and map clauses", t);
6436 remove = true;
6437 }
6438 else
6439 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6440 goto handle_field_decl;
6441
6442 case OMP_CLAUSE_LASTPRIVATE:
6443 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6444 if (t)
6445 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6446 else
6447 t = OMP_CLAUSE_DECL (c);
6448 if (t == current_class_ptr)
6449 {
6450 error_at (OMP_CLAUSE_LOCATION (c),
6451 "%<this%> allowed in OpenMP only in %<declare simd%>"
6452 " clauses");
6453 remove = true;
6454 break;
6455 }
6456 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6457 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6458 || TREE_CODE (t) != FIELD_DECL))
6459 {
6460 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6461 break;
6462 if (DECL_P (t))
6463 error_at (OMP_CLAUSE_LOCATION (c),
6464 "%qD is not a variable in clause %<lastprivate%>",
6465 t);
6466 else
6467 error_at (OMP_CLAUSE_LOCATION (c),
6468 "%qE is not a variable in clause %<lastprivate%>",
6469 t);
6470 remove = true;
6471 }
6472 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6473 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6474 {
6475 error_at (OMP_CLAUSE_LOCATION (c),
6476 "%qD appears more than once in data clauses", t);
6477 remove = true;
6478 }
6479 else
6480 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6481 goto handle_field_decl;
6482
6483 case OMP_CLAUSE_IF:
6484 t = OMP_CLAUSE_IF_EXPR (c);
6485 t = maybe_convert_cond (t);
6486 if (t == error_mark_node)
6487 remove = true;
6488 else if (!processing_template_decl)
6489 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6490 OMP_CLAUSE_IF_EXPR (c) = t;
6491 break;
6492
6493 case OMP_CLAUSE_FINAL:
6494 t = OMP_CLAUSE_FINAL_EXPR (c);
6495 t = maybe_convert_cond (t);
6496 if (t == error_mark_node)
6497 remove = true;
6498 else if (!processing_template_decl)
6499 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6500 OMP_CLAUSE_FINAL_EXPR (c) = t;
6501 break;
6502
6503 case OMP_CLAUSE_GANG:
6504 /* Operand 1 is the gang static: argument. */
6505 t = OMP_CLAUSE_OPERAND (c, 1);
6506 if (t != NULL_TREE)
6507 {
6508 if (t == error_mark_node)
6509 remove = true;
6510 else if (!type_dependent_expression_p (t)
6511 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6512 {
6513 error_at (OMP_CLAUSE_LOCATION (c),
6514 "%<gang%> static expression must be integral");
6515 remove = true;
6516 }
6517 else
6518 {
6519 t = mark_rvalue_use (t);
6520 if (!processing_template_decl)
6521 {
6522 t = maybe_constant_value (t);
6523 if (TREE_CODE (t) == INTEGER_CST
6524 && tree_int_cst_sgn (t) != 1
6525 && t != integer_minus_one_node)
6526 {
6527 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6528 "%<gang%> static value must be "
6529 "positive");
6530 t = integer_one_node;
6531 }
6532 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6533 }
6534 }
6535 OMP_CLAUSE_OPERAND (c, 1) = t;
6536 }
6537 /* Check operand 0, the num argument. */
6538 /* FALLTHRU */
6539
6540 case OMP_CLAUSE_WORKER:
6541 case OMP_CLAUSE_VECTOR:
6542 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6543 break;
6544 /* FALLTHRU */
6545
6546 case OMP_CLAUSE_NUM_TASKS:
6547 case OMP_CLAUSE_NUM_TEAMS:
6548 case OMP_CLAUSE_NUM_THREADS:
6549 case OMP_CLAUSE_NUM_GANGS:
6550 case OMP_CLAUSE_NUM_WORKERS:
6551 case OMP_CLAUSE_VECTOR_LENGTH:
6552 t = OMP_CLAUSE_OPERAND (c, 0);
6553 if (t == error_mark_node)
6554 remove = true;
6555 else if (!type_dependent_expression_p (t)
6556 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6557 {
6558 switch (OMP_CLAUSE_CODE (c))
6559 {
6560 case OMP_CLAUSE_GANG:
6561 error_at (OMP_CLAUSE_LOCATION (c),
6562 "%<gang%> num expression must be integral"); break;
6563 case OMP_CLAUSE_VECTOR:
6564 error_at (OMP_CLAUSE_LOCATION (c),
6565 "%<vector%> length expression must be integral");
6566 break;
6567 case OMP_CLAUSE_WORKER:
6568 error_at (OMP_CLAUSE_LOCATION (c),
6569 "%<worker%> num expression must be integral");
6570 break;
6571 default:
6572 error_at (OMP_CLAUSE_LOCATION (c),
6573 "%qs expression must be integral",
6574 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6575 }
6576 remove = true;
6577 }
6578 else
6579 {
6580 t = mark_rvalue_use (t);
6581 if (!processing_template_decl)
6582 {
6583 t = maybe_constant_value (t);
6584 if (TREE_CODE (t) == INTEGER_CST
6585 && tree_int_cst_sgn (t) != 1)
6586 {
6587 switch (OMP_CLAUSE_CODE (c))
6588 {
6589 case OMP_CLAUSE_GANG:
6590 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6591 "%<gang%> num value must be positive");
6592 break;
6593 case OMP_CLAUSE_VECTOR:
6594 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6595 "%<vector%> length value must be "
6596 "positive");
6597 break;
6598 case OMP_CLAUSE_WORKER:
6599 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6600 "%<worker%> num value must be "
6601 "positive");
6602 break;
6603 default:
6604 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6605 "%qs value must be positive",
6606 omp_clause_code_name
6607 [OMP_CLAUSE_CODE (c)]);
6608 }
6609 t = integer_one_node;
6610 }
6611 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6612 }
6613 OMP_CLAUSE_OPERAND (c, 0) = t;
6614 }
6615 break;
6616
6617 case OMP_CLAUSE_SCHEDULE:
6618 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6619 if (t == NULL)
6620 ;
6621 else if (t == error_mark_node)
6622 remove = true;
6623 else if (!type_dependent_expression_p (t)
6624 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6625 {
6626 error_at (OMP_CLAUSE_LOCATION (c),
6627 "schedule chunk size expression must be integral");
6628 remove = true;
6629 }
6630 else
6631 {
6632 t = mark_rvalue_use (t);
6633 if (!processing_template_decl)
6634 {
6635 t = maybe_constant_value (t);
6636 if (TREE_CODE (t) == INTEGER_CST
6637 && tree_int_cst_sgn (t) != 1)
6638 {
6639 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6640 "chunk size value must be positive");
6641 t = integer_one_node;
6642 }
6643 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6644 }
6645 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6646 }
6647 break;
6648
6649 case OMP_CLAUSE_SIMDLEN:
6650 case OMP_CLAUSE_SAFELEN:
6651 t = OMP_CLAUSE_OPERAND (c, 0);
6652 if (t == error_mark_node)
6653 remove = true;
6654 else if (!type_dependent_expression_p (t)
6655 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6656 {
6657 error_at (OMP_CLAUSE_LOCATION (c),
6658 "%qs length expression must be integral",
6659 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6660 remove = true;
6661 }
6662 else
6663 {
6664 t = mark_rvalue_use (t);
6665 if (!processing_template_decl)
6666 {
6667 t = maybe_constant_value (t);
6668 if (TREE_CODE (t) != INTEGER_CST
6669 || tree_int_cst_sgn (t) != 1)
6670 {
6671 error_at (OMP_CLAUSE_LOCATION (c),
6672 "%qs length expression must be positive "
6673 "constant integer expression",
6674 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6675 remove = true;
6676 }
6677 }
6678 OMP_CLAUSE_OPERAND (c, 0) = t;
6679 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6680 safelen = c;
6681 }
6682 break;
6683
6684 case OMP_CLAUSE_ASYNC:
6685 t = OMP_CLAUSE_ASYNC_EXPR (c);
6686 if (t == error_mark_node)
6687 remove = true;
6688 else if (!type_dependent_expression_p (t)
6689 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6690 {
6691 error_at (OMP_CLAUSE_LOCATION (c),
6692 "%<async%> expression must be integral");
6693 remove = true;
6694 }
6695 else
6696 {
6697 t = mark_rvalue_use (t);
6698 if (!processing_template_decl)
6699 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6700 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6701 }
6702 break;
6703
6704 case OMP_CLAUSE_WAIT:
6705 t = OMP_CLAUSE_WAIT_EXPR (c);
6706 if (t == error_mark_node)
6707 remove = true;
6708 else if (!processing_template_decl)
6709 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6710 OMP_CLAUSE_WAIT_EXPR (c) = t;
6711 break;
6712
6713 case OMP_CLAUSE_THREAD_LIMIT:
6714 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6715 if (t == error_mark_node)
6716 remove = true;
6717 else if (!type_dependent_expression_p (t)
6718 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6719 {
6720 error_at (OMP_CLAUSE_LOCATION (c),
6721 "%<thread_limit%> expression must be integral");
6722 remove = true;
6723 }
6724 else
6725 {
6726 t = mark_rvalue_use (t);
6727 if (!processing_template_decl)
6728 {
6729 t = maybe_constant_value (t);
6730 if (TREE_CODE (t) == INTEGER_CST
6731 && tree_int_cst_sgn (t) != 1)
6732 {
6733 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6734 "%<thread_limit%> value must be positive");
6735 t = integer_one_node;
6736 }
6737 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6738 }
6739 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6740 }
6741 break;
6742
6743 case OMP_CLAUSE_DEVICE:
6744 t = OMP_CLAUSE_DEVICE_ID (c);
6745 if (t == error_mark_node)
6746 remove = true;
6747 else if (!type_dependent_expression_p (t)
6748 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6749 {
6750 error_at (OMP_CLAUSE_LOCATION (c),
6751 "%<device%> id must be integral");
6752 remove = true;
6753 }
6754 else
6755 {
6756 t = mark_rvalue_use (t);
6757 if (!processing_template_decl)
6758 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6759 OMP_CLAUSE_DEVICE_ID (c) = t;
6760 }
6761 break;
6762
6763 case OMP_CLAUSE_DIST_SCHEDULE:
6764 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6765 if (t == NULL)
6766 ;
6767 else if (t == error_mark_node)
6768 remove = true;
6769 else if (!type_dependent_expression_p (t)
6770 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6771 {
6772 error_at (OMP_CLAUSE_LOCATION (c),
6773 "%<dist_schedule%> chunk size expression must be "
6774 "integral");
6775 remove = true;
6776 }
6777 else
6778 {
6779 t = mark_rvalue_use (t);
6780 if (!processing_template_decl)
6781 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6782 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6783 }
6784 break;
6785
6786 case OMP_CLAUSE_ALIGNED:
6787 t = OMP_CLAUSE_DECL (c);
6788 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6789 {
6790 error_at (OMP_CLAUSE_LOCATION (c),
6791 "%<this%> allowed in OpenMP only in %<declare simd%>"
6792 " clauses");
6793 remove = true;
6794 break;
6795 }
6796 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6797 {
6798 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6799 break;
6800 if (DECL_P (t))
6801 error_at (OMP_CLAUSE_LOCATION (c),
6802 "%qD is not a variable in %<aligned%> clause", t);
6803 else
6804 error_at (OMP_CLAUSE_LOCATION (c),
6805 "%qE is not a variable in %<aligned%> clause", t);
6806 remove = true;
6807 }
6808 else if (!type_dependent_expression_p (t)
6809 && !TYPE_PTR_P (TREE_TYPE (t))
6810 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6811 && (!TYPE_REF_P (TREE_TYPE (t))
6812 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6813 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6814 != ARRAY_TYPE))))
6815 {
6816 error_at (OMP_CLAUSE_LOCATION (c),
6817 "%qE in %<aligned%> clause is neither a pointer nor "
6818 "an array nor a reference to pointer or array", t);
6819 remove = true;
6820 }
6821 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6822 {
6823 error_at (OMP_CLAUSE_LOCATION (c),
6824 "%qD appears more than once in %<aligned%> clauses",
6825 t);
6826 remove = true;
6827 }
6828 else
6829 bitmap_set_bit (&aligned_head, DECL_UID (t));
6830 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6831 if (t == error_mark_node)
6832 remove = true;
6833 else if (t == NULL_TREE)
6834 break;
6835 else if (!type_dependent_expression_p (t)
6836 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6837 {
6838 error_at (OMP_CLAUSE_LOCATION (c),
6839 "%<aligned%> clause alignment expression must "
6840 "be integral");
6841 remove = true;
6842 }
6843 else
6844 {
6845 t = mark_rvalue_use (t);
6846 if (!processing_template_decl)
6847 {
6848 t = maybe_constant_value (t);
6849 if (TREE_CODE (t) != INTEGER_CST
6850 || tree_int_cst_sgn (t) != 1)
6851 {
6852 error_at (OMP_CLAUSE_LOCATION (c),
6853 "%<aligned%> clause alignment expression must "
6854 "be positive constant integer expression");
6855 remove = true;
6856 }
6857 else
6858 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6859 }
6860 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6861 }
6862 break;
6863
6864 case OMP_CLAUSE_NONTEMPORAL:
6865 t = OMP_CLAUSE_DECL (c);
6866 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6867 {
6868 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6869 break;
6870 if (DECL_P (t))
6871 error_at (OMP_CLAUSE_LOCATION (c),
6872 "%qD is not a variable in %<nontemporal%> clause",
6873 t);
6874 else
6875 error_at (OMP_CLAUSE_LOCATION (c),
6876 "%qE is not a variable in %<nontemporal%> clause",
6877 t);
6878 remove = true;
6879 }
6880 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6881 {
6882 error_at (OMP_CLAUSE_LOCATION (c),
6883 "%qD appears more than once in %<nontemporal%> "
6884 "clauses", t);
6885 remove = true;
6886 }
6887 else
6888 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6889 break;
6890
6891 case OMP_CLAUSE_DEPEND:
6892 t = OMP_CLAUSE_DECL (c);
6893 if (t == NULL_TREE)
6894 {
6895 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6896 == OMP_CLAUSE_DEPEND_SOURCE);
6897 break;
6898 }
6899 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6900 {
6901 if (cp_finish_omp_clause_depend_sink (c))
6902 remove = true;
6903 break;
6904 }
6905 if (TREE_CODE (t) == TREE_LIST
6906 && TREE_PURPOSE (t)
6907 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
6908 {
6909 if (TREE_PURPOSE (t) != last_iterators)
6910 last_iterators_remove
6911 = cp_omp_finish_iterators (TREE_PURPOSE (t));
6912 last_iterators = TREE_PURPOSE (t);
6913 t = TREE_VALUE (t);
6914 if (last_iterators_remove)
6915 t = error_mark_node;
6916 }
6917 else
6918 last_iterators = NULL_TREE;
6919
6920 if (TREE_CODE (t) == TREE_LIST)
6921 {
6922 if (handle_omp_array_sections (c, ort))
6923 remove = true;
6924 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6925 {
6926 error_at (OMP_CLAUSE_LOCATION (c),
6927 "%<depend%> clause with %<depobj%> dependence "
6928 "type on array section");
6929 remove = true;
6930 }
6931 break;
6932 }
6933 if (t == error_mark_node)
6934 remove = true;
6935 else if (t == current_class_ptr)
6936 {
6937 error_at (OMP_CLAUSE_LOCATION (c),
6938 "%<this%> allowed in OpenMP only in %<declare simd%>"
6939 " clauses");
6940 remove = true;
6941 }
6942 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6943 break;
6944 else if (!lvalue_p (t))
6945 {
6946 if (DECL_P (t))
6947 error_at (OMP_CLAUSE_LOCATION (c),
6948 "%qD is not lvalue expression nor array section "
6949 "in %<depend%> clause", t);
6950 else
6951 error_at (OMP_CLAUSE_LOCATION (c),
6952 "%qE is not lvalue expression nor array section "
6953 "in %<depend%> clause", t);
6954 remove = true;
6955 }
6956 else if (TREE_CODE (t) == COMPONENT_REF
6957 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6958 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6959 {
6960 error_at (OMP_CLAUSE_LOCATION (c),
6961 "bit-field %qE in %qs clause", t, "depend");
6962 remove = true;
6963 }
6964 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6965 {
6966 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
6967 ? TREE_TYPE (TREE_TYPE (t))
6968 : TREE_TYPE (t)))
6969 {
6970 error_at (OMP_CLAUSE_LOCATION (c),
6971 "%qE does not have %<omp_depend_t%> type in "
6972 "%<depend%> clause with %<depobj%> dependence "
6973 "type", t);
6974 remove = true;
6975 }
6976 }
6977 else if (c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
6978 ? TREE_TYPE (TREE_TYPE (t))
6979 : TREE_TYPE (t)))
6980 {
6981 error_at (OMP_CLAUSE_LOCATION (c),
6982 "%qE should not have %<omp_depend_t%> type in "
6983 "%<depend%> clause with dependence type other than "
6984 "%<depobj%>", t);
6985 remove = true;
6986 }
6987 if (!remove)
6988 {
6989 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
6990 if (addr == error_mark_node)
6991 remove = true;
6992 else
6993 {
6994 t = cp_build_indirect_ref (addr, RO_UNARY_STAR,
6995 tf_warning_or_error);
6996 if (t == error_mark_node)
6997 remove = true;
6998 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
6999 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7000 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7001 == TREE_VEC))
7002 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7003 else
7004 OMP_CLAUSE_DECL (c) = t;
7005 }
7006 }
7007 break;
7008
7009 case OMP_CLAUSE_MAP:
7010 case OMP_CLAUSE_TO:
7011 case OMP_CLAUSE_FROM:
7012 case OMP_CLAUSE__CACHE_:
7013 t = OMP_CLAUSE_DECL (c);
7014 if (TREE_CODE (t) == TREE_LIST)
7015 {
7016 if (handle_omp_array_sections (c, ort))
7017 remove = true;
7018 else
7019 {
7020 t = OMP_CLAUSE_DECL (c);
7021 if (TREE_CODE (t) != TREE_LIST
7022 && !type_dependent_expression_p (t)
7023 && !cp_omp_mappable_type (TREE_TYPE (t)))
7024 {
7025 error_at (OMP_CLAUSE_LOCATION (c),
7026 "array section does not have mappable type "
7027 "in %qs clause",
7028 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7029 remove = true;
7030 }
7031 while (TREE_CODE (t) == ARRAY_REF)
7032 t = TREE_OPERAND (t, 0);
7033 if (TREE_CODE (t) == COMPONENT_REF
7034 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7035 {
7036 while (TREE_CODE (t) == COMPONENT_REF)
7037 t = TREE_OPERAND (t, 0);
7038 if (REFERENCE_REF_P (t))
7039 t = TREE_OPERAND (t, 0);
7040 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7041 break;
7042 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7043 {
7044 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7045 error_at (OMP_CLAUSE_LOCATION (c),
7046 "%qD appears more than once in motion"
7047 " clauses", t);
7048 else if (ort == C_ORT_ACC)
7049 error_at (OMP_CLAUSE_LOCATION (c),
7050 "%qD appears more than once in data"
7051 " clauses", t);
7052 else
7053 error_at (OMP_CLAUSE_LOCATION (c),
7054 "%qD appears more than once in map"
7055 " clauses", t);
7056 remove = true;
7057 }
7058 else
7059 {
7060 bitmap_set_bit (&map_head, DECL_UID (t));
7061 bitmap_set_bit (&map_field_head, DECL_UID (t));
7062 }
7063 }
7064 }
7065 break;
7066 }
7067 if (t == error_mark_node)
7068 {
7069 remove = true;
7070 break;
7071 }
7072 if (REFERENCE_REF_P (t)
7073 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7074 {
7075 t = TREE_OPERAND (t, 0);
7076 OMP_CLAUSE_DECL (c) = t;
7077 }
7078 if (TREE_CODE (t) == COMPONENT_REF
7079 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
7080 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7081 {
7082 if (type_dependent_expression_p (t))
7083 break;
7084 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7085 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7086 {
7087 error_at (OMP_CLAUSE_LOCATION (c),
7088 "bit-field %qE in %qs clause",
7089 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7090 remove = true;
7091 }
7092 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7093 {
7094 error_at (OMP_CLAUSE_LOCATION (c),
7095 "%qE does not have a mappable type in %qs clause",
7096 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7097 remove = true;
7098 }
7099 while (TREE_CODE (t) == COMPONENT_REF)
7100 {
7101 if (TREE_TYPE (TREE_OPERAND (t, 0))
7102 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7103 == UNION_TYPE))
7104 {
7105 error_at (OMP_CLAUSE_LOCATION (c),
7106 "%qE is a member of a union", t);
7107 remove = true;
7108 break;
7109 }
7110 t = TREE_OPERAND (t, 0);
7111 }
7112 if (remove)
7113 break;
7114 if (REFERENCE_REF_P (t))
7115 t = TREE_OPERAND (t, 0);
7116 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7117 {
7118 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7119 goto handle_map_references;
7120 }
7121 }
7122 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7123 {
7124 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7125 break;
7126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7127 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7128 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
7129 break;
7130 if (DECL_P (t))
7131 error_at (OMP_CLAUSE_LOCATION (c),
7132 "%qD is not a variable in %qs clause", t,
7133 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7134 else
7135 error_at (OMP_CLAUSE_LOCATION (c),
7136 "%qE is not a variable in %qs clause", t,
7137 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7138 remove = true;
7139 }
7140 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7141 {
7142 error_at (OMP_CLAUSE_LOCATION (c),
7143 "%qD is threadprivate variable in %qs clause", t,
7144 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7145 remove = true;
7146 }
7147 else if (ort != C_ORT_ACC && t == current_class_ptr)
7148 {
7149 error_at (OMP_CLAUSE_LOCATION (c),
7150 "%<this%> allowed in OpenMP only in %<declare simd%>"
7151 " clauses");
7152 remove = true;
7153 break;
7154 }
7155 else if (!processing_template_decl
7156 && !TYPE_REF_P (TREE_TYPE (t))
7157 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7158 || (OMP_CLAUSE_MAP_KIND (c)
7159 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7160 && !cxx_mark_addressable (t))
7161 remove = true;
7162 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7163 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7164 || (OMP_CLAUSE_MAP_KIND (c)
7165 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7166 && t == OMP_CLAUSE_DECL (c)
7167 && !type_dependent_expression_p (t)
7168 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7169 ? TREE_TYPE (TREE_TYPE (t))
7170 : TREE_TYPE (t)))
7171 {
7172 error_at (OMP_CLAUSE_LOCATION (c),
7173 "%qD does not have a mappable type in %qs clause", t,
7174 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7175 remove = true;
7176 }
7177 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7178 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7179 && !type_dependent_expression_p (t)
7180 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7181 {
7182 error_at (OMP_CLAUSE_LOCATION (c),
7183 "%qD is not a pointer variable", t);
7184 remove = true;
7185 }
7186 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7187 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7188 {
7189 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7190 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7191 {
7192 error_at (OMP_CLAUSE_LOCATION (c),
7193 "%qD appears more than once in data clauses", t);
7194 remove = true;
7195 }
7196 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7197 {
7198 if (ort == C_ORT_ACC)
7199 error_at (OMP_CLAUSE_LOCATION (c),
7200 "%qD appears more than once in data clauses", t);
7201 else
7202 error_at (OMP_CLAUSE_LOCATION (c),
7203 "%qD appears both in data and map clauses", t);
7204 remove = true;
7205 }
7206 else
7207 bitmap_set_bit (&generic_head, DECL_UID (t));
7208 }
7209 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7210 {
7211 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7212 error_at (OMP_CLAUSE_LOCATION (c),
7213 "%qD appears more than once in motion clauses", t);
7214 if (ort == C_ORT_ACC)
7215 error_at (OMP_CLAUSE_LOCATION (c),
7216 "%qD appears more than once in data clauses", t);
7217 else
7218 error_at (OMP_CLAUSE_LOCATION (c),
7219 "%qD appears more than once in map clauses", t);
7220 remove = true;
7221 }
7222 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7223 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7224 {
7225 if (ort == C_ORT_ACC)
7226 error_at (OMP_CLAUSE_LOCATION (c),
7227 "%qD appears more than once in data clauses", t);
7228 else
7229 error_at (OMP_CLAUSE_LOCATION (c),
7230 "%qD appears both in data and map clauses", t);
7231 remove = true;
7232 }
7233 else
7234 {
7235 bitmap_set_bit (&map_head, DECL_UID (t));
7236 if (t != OMP_CLAUSE_DECL (c)
7237 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7238 bitmap_set_bit (&map_field_head, DECL_UID (t));
7239 }
7240 handle_map_references:
7241 if (!remove
7242 && !processing_template_decl
7243 && ort != C_ORT_DECLARE_SIMD
7244 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
7245 {
7246 t = OMP_CLAUSE_DECL (c);
7247 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7248 {
7249 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7250 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7251 OMP_CLAUSE_SIZE (c)
7252 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7253 }
7254 else if (OMP_CLAUSE_MAP_KIND (c)
7255 != GOMP_MAP_FIRSTPRIVATE_POINTER
7256 && (OMP_CLAUSE_MAP_KIND (c)
7257 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
7258 && (OMP_CLAUSE_MAP_KIND (c)
7259 != GOMP_MAP_ALWAYS_POINTER))
7260 {
7261 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7262 OMP_CLAUSE_MAP);
7263 if (TREE_CODE (t) == COMPONENT_REF)
7264 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
7265 else
7266 OMP_CLAUSE_SET_MAP_KIND (c2,
7267 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7268 OMP_CLAUSE_DECL (c2) = t;
7269 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7270 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
7271 OMP_CLAUSE_CHAIN (c) = c2;
7272 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7273 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7274 OMP_CLAUSE_SIZE (c)
7275 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7276 c = c2;
7277 }
7278 }
7279 break;
7280
7281 case OMP_CLAUSE_TO_DECLARE:
7282 case OMP_CLAUSE_LINK:
7283 t = OMP_CLAUSE_DECL (c);
7284 if (TREE_CODE (t) == FUNCTION_DECL
7285 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7286 ;
7287 else if (!VAR_P (t))
7288 {
7289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7290 {
7291 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
7292 error_at (OMP_CLAUSE_LOCATION (c),
7293 "template %qE in clause %qs", t,
7294 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7295 else if (really_overloaded_fn (t))
7296 error_at (OMP_CLAUSE_LOCATION (c),
7297 "overloaded function name %qE in clause %qs", t,
7298 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7299 else
7300 error_at (OMP_CLAUSE_LOCATION (c),
7301 "%qE is neither a variable nor a function name "
7302 "in clause %qs", t,
7303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7304 }
7305 else
7306 error_at (OMP_CLAUSE_LOCATION (c),
7307 "%qE is not a variable in clause %qs", t,
7308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7309 remove = true;
7310 }
7311 else if (DECL_THREAD_LOCAL_P (t))
7312 {
7313 error_at (OMP_CLAUSE_LOCATION (c),
7314 "%qD is threadprivate variable in %qs clause", t,
7315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7316 remove = true;
7317 }
7318 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7319 {
7320 error_at (OMP_CLAUSE_LOCATION (c),
7321 "%qD does not have a mappable type in %qs clause", t,
7322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7323 remove = true;
7324 }
7325 if (remove)
7326 break;
7327 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
7328 {
7329 error_at (OMP_CLAUSE_LOCATION (c),
7330 "%qE appears more than once on the same "
7331 "%<declare target%> directive", t);
7332 remove = true;
7333 }
7334 else
7335 bitmap_set_bit (&generic_head, DECL_UID (t));
7336 break;
7337
7338 case OMP_CLAUSE_UNIFORM:
7339 t = OMP_CLAUSE_DECL (c);
7340 if (TREE_CODE (t) != PARM_DECL)
7341 {
7342 if (processing_template_decl)
7343 break;
7344 if (DECL_P (t))
7345 error_at (OMP_CLAUSE_LOCATION (c),
7346 "%qD is not an argument in %<uniform%> clause", t);
7347 else
7348 error_at (OMP_CLAUSE_LOCATION (c),
7349 "%qE is not an argument in %<uniform%> clause", t);
7350 remove = true;
7351 break;
7352 }
7353 /* map_head bitmap is used as uniform_head if declare_simd. */
7354 bitmap_set_bit (&map_head, DECL_UID (t));
7355 goto check_dup_generic;
7356
7357 case OMP_CLAUSE_GRAINSIZE:
7358 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7359 if (t == error_mark_node)
7360 remove = true;
7361 else if (!type_dependent_expression_p (t)
7362 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7363 {
7364 error_at (OMP_CLAUSE_LOCATION (c),
7365 "%<grainsize%> expression must be integral");
7366 remove = true;
7367 }
7368 else
7369 {
7370 t = mark_rvalue_use (t);
7371 if (!processing_template_decl)
7372 {
7373 t = maybe_constant_value (t);
7374 if (TREE_CODE (t) == INTEGER_CST
7375 && tree_int_cst_sgn (t) != 1)
7376 {
7377 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7378 "%<grainsize%> value must be positive");
7379 t = integer_one_node;
7380 }
7381 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7382 }
7383 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7384 }
7385 break;
7386
7387 case OMP_CLAUSE_PRIORITY:
7388 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7389 if (t == error_mark_node)
7390 remove = true;
7391 else if (!type_dependent_expression_p (t)
7392 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7393 {
7394 error_at (OMP_CLAUSE_LOCATION (c),
7395 "%<priority%> expression must be integral");
7396 remove = true;
7397 }
7398 else
7399 {
7400 t = mark_rvalue_use (t);
7401 if (!processing_template_decl)
7402 {
7403 t = maybe_constant_value (t);
7404 if (TREE_CODE (t) == INTEGER_CST
7405 && tree_int_cst_sgn (t) == -1)
7406 {
7407 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7408 "%<priority%> value must be non-negative");
7409 t = integer_one_node;
7410 }
7411 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7412 }
7413 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7414 }
7415 break;
7416
7417 case OMP_CLAUSE_HINT:
7418 t = OMP_CLAUSE_HINT_EXPR (c);
7419 if (t == error_mark_node)
7420 remove = true;
7421 else if (!type_dependent_expression_p (t)
7422 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7423 {
7424 error_at (OMP_CLAUSE_LOCATION (c),
7425 "%<hint%> expression must be integral");
7426 remove = true;
7427 }
7428 else
7429 {
7430 t = mark_rvalue_use (t);
7431 if (!processing_template_decl)
7432 {
7433 t = maybe_constant_value (t);
7434 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7435 if (TREE_CODE (t) != INTEGER_CST)
7436 {
7437 error_at (OMP_CLAUSE_LOCATION (c),
7438 "%<hint%> expression must be constant integer "
7439 "expression");
7440 remove = true;
7441 }
7442 }
7443 OMP_CLAUSE_HINT_EXPR (c) = t;
7444 }
7445 break;
7446
7447 case OMP_CLAUSE_IS_DEVICE_PTR:
7448 case OMP_CLAUSE_USE_DEVICE_PTR:
7449 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7450 t = OMP_CLAUSE_DECL (c);
7451 if (!type_dependent_expression_p (t))
7452 {
7453 tree type = TREE_TYPE (t);
7454 if (!TYPE_PTR_P (type)
7455 && TREE_CODE (type) != ARRAY_TYPE
7456 && (!TYPE_REF_P (type)
7457 || (!TYPE_PTR_P (TREE_TYPE (type))
7458 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7459 {
7460 error_at (OMP_CLAUSE_LOCATION (c),
7461 "%qs variable is neither a pointer, nor an array "
7462 "nor reference to pointer or array",
7463 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7464 remove = true;
7465 }
7466 }
7467 goto check_dup_generic;
7468
7469 case OMP_CLAUSE_NOWAIT:
7470 case OMP_CLAUSE_DEFAULT:
7471 case OMP_CLAUSE_UNTIED:
7472 case OMP_CLAUSE_COLLAPSE:
7473 case OMP_CLAUSE_MERGEABLE:
7474 case OMP_CLAUSE_PARALLEL:
7475 case OMP_CLAUSE_FOR:
7476 case OMP_CLAUSE_SECTIONS:
7477 case OMP_CLAUSE_TASKGROUP:
7478 case OMP_CLAUSE_PROC_BIND:
7479 case OMP_CLAUSE_NOGROUP:
7480 case OMP_CLAUSE_THREADS:
7481 case OMP_CLAUSE_SIMD:
7482 case OMP_CLAUSE_DEFAULTMAP:
7483 case OMP_CLAUSE_AUTO:
7484 case OMP_CLAUSE_INDEPENDENT:
7485 case OMP_CLAUSE_SEQ:
7486 case OMP_CLAUSE_IF_PRESENT:
7487 case OMP_CLAUSE_FINALIZE:
7488 break;
7489
7490 case OMP_CLAUSE_TILE:
7491 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7492 list = TREE_CHAIN (list))
7493 {
7494 t = TREE_VALUE (list);
7495
7496 if (t == error_mark_node)
7497 remove = true;
7498 else if (!type_dependent_expression_p (t)
7499 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7500 {
7501 error_at (OMP_CLAUSE_LOCATION (c),
7502 "%<tile%> argument needs integral type");
7503 remove = true;
7504 }
7505 else
7506 {
7507 t = mark_rvalue_use (t);
7508 if (!processing_template_decl)
7509 {
7510 /* Zero is used to indicate '*', we permit you
7511 to get there via an ICE of value zero. */
7512 t = maybe_constant_value (t);
7513 if (!tree_fits_shwi_p (t)
7514 || tree_to_shwi (t) < 0)
7515 {
7516 error_at (OMP_CLAUSE_LOCATION (c),
7517 "%<tile%> argument needs positive "
7518 "integral constant");
7519 remove = true;
7520 }
7521 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7522 }
7523 }
7524
7525 /* Update list item. */
7526 TREE_VALUE (list) = t;
7527 }
7528 break;
7529
7530 case OMP_CLAUSE_ORDERED:
7531 ordered_seen = true;
7532 break;
7533
7534 case OMP_CLAUSE_INBRANCH:
7535 case OMP_CLAUSE_NOTINBRANCH:
7536 if (branch_seen)
7537 {
7538 error_at (OMP_CLAUSE_LOCATION (c),
7539 "%<inbranch%> clause is incompatible with "
7540 "%<notinbranch%>");
7541 remove = true;
7542 }
7543 branch_seen = true;
7544 break;
7545
7546 default:
7547 gcc_unreachable ();
7548 }
7549
7550 if (remove)
7551 *pc = OMP_CLAUSE_CHAIN (c);
7552 else
7553 pc = &OMP_CLAUSE_CHAIN (c);
7554 }
7555
7556 for (pc = &clauses, c = clauses; c ; c = *pc)
7557 {
7558 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7559 bool remove = false;
7560 bool need_complete_type = false;
7561 bool need_default_ctor = false;
7562 bool need_copy_ctor = false;
7563 bool need_copy_assignment = false;
7564 bool need_implicitly_determined = false;
7565 bool need_dtor = false;
7566 tree type, inner_type;
7567
7568 switch (c_kind)
7569 {
7570 case OMP_CLAUSE_SHARED:
7571 need_implicitly_determined = true;
7572 break;
7573 case OMP_CLAUSE_PRIVATE:
7574 need_complete_type = true;
7575 need_default_ctor = true;
7576 need_dtor = true;
7577 need_implicitly_determined = true;
7578 break;
7579 case OMP_CLAUSE_FIRSTPRIVATE:
7580 need_complete_type = true;
7581 need_copy_ctor = true;
7582 need_dtor = true;
7583 need_implicitly_determined = true;
7584 break;
7585 case OMP_CLAUSE_LASTPRIVATE:
7586 need_complete_type = true;
7587 need_copy_assignment = true;
7588 need_implicitly_determined = true;
7589 break;
7590 case OMP_CLAUSE_REDUCTION:
7591 case OMP_CLAUSE_IN_REDUCTION:
7592 case OMP_CLAUSE_TASK_REDUCTION:
7593 need_implicitly_determined = true;
7594 break;
7595 case OMP_CLAUSE_LINEAR:
7596 if (ort != C_ORT_OMP_DECLARE_SIMD)
7597 need_implicitly_determined = true;
7598 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7599 && !bitmap_bit_p (&map_head,
7600 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7601 {
7602 error_at (OMP_CLAUSE_LOCATION (c),
7603 "%<linear%> clause step is a parameter %qD not "
7604 "specified in %<uniform%> clause",
7605 OMP_CLAUSE_LINEAR_STEP (c));
7606 *pc = OMP_CLAUSE_CHAIN (c);
7607 continue;
7608 }
7609 break;
7610 case OMP_CLAUSE_COPYPRIVATE:
7611 need_copy_assignment = true;
7612 break;
7613 case OMP_CLAUSE_COPYIN:
7614 need_copy_assignment = true;
7615 break;
7616 case OMP_CLAUSE_SIMDLEN:
7617 if (safelen
7618 && !processing_template_decl
7619 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7620 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7621 {
7622 error_at (OMP_CLAUSE_LOCATION (c),
7623 "%<simdlen%> clause value is bigger than "
7624 "%<safelen%> clause value");
7625 OMP_CLAUSE_SIMDLEN_EXPR (c)
7626 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7627 }
7628 pc = &OMP_CLAUSE_CHAIN (c);
7629 continue;
7630 case OMP_CLAUSE_SCHEDULE:
7631 if (ordered_seen
7632 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7633 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7634 {
7635 error_at (OMP_CLAUSE_LOCATION (c),
7636 "%<nonmonotonic%> schedule modifier specified "
7637 "together with %<ordered%> clause");
7638 OMP_CLAUSE_SCHEDULE_KIND (c)
7639 = (enum omp_clause_schedule_kind)
7640 (OMP_CLAUSE_SCHEDULE_KIND (c)
7641 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7642 }
7643 pc = &OMP_CLAUSE_CHAIN (c);
7644 continue;
7645 case OMP_CLAUSE_NOGROUP:
7646 if (reduction_seen)
7647 {
7648 error_at (OMP_CLAUSE_LOCATION (c),
7649 "%<nogroup%> clause must not be used together with "
7650 "%<reduction%> clause");
7651 *pc = OMP_CLAUSE_CHAIN (c);
7652 continue;
7653 }
7654 pc = &OMP_CLAUSE_CHAIN (c);
7655 continue;
7656 case OMP_CLAUSE_NOWAIT:
7657 if (copyprivate_seen)
7658 {
7659 error_at (OMP_CLAUSE_LOCATION (c),
7660 "%<nowait%> clause must not be used together "
7661 "with %<copyprivate%>");
7662 *pc = OMP_CLAUSE_CHAIN (c);
7663 continue;
7664 }
7665 /* FALLTHRU */
7666 default:
7667 pc = &OMP_CLAUSE_CHAIN (c);
7668 continue;
7669 }
7670
7671 t = OMP_CLAUSE_DECL (c);
7672 if (processing_template_decl
7673 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7674 {
7675 pc = &OMP_CLAUSE_CHAIN (c);
7676 continue;
7677 }
7678
7679 switch (c_kind)
7680 {
7681 case OMP_CLAUSE_LASTPRIVATE:
7682 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7683 {
7684 need_default_ctor = true;
7685 need_dtor = true;
7686 }
7687 break;
7688
7689 case OMP_CLAUSE_REDUCTION:
7690 case OMP_CLAUSE_IN_REDUCTION:
7691 case OMP_CLAUSE_TASK_REDUCTION:
7692 if (finish_omp_reduction_clause (c, &need_default_ctor,
7693 &need_dtor))
7694 remove = true;
7695 else
7696 t = OMP_CLAUSE_DECL (c);
7697 break;
7698
7699 case OMP_CLAUSE_COPYIN:
7700 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7701 {
7702 error_at (OMP_CLAUSE_LOCATION (c),
7703 "%qE must be %<threadprivate%> for %<copyin%>", t);
7704 remove = true;
7705 }
7706 break;
7707
7708 default:
7709 break;
7710 }
7711
7712 if (need_complete_type || need_copy_assignment)
7713 {
7714 t = require_complete_type (t);
7715 if (t == error_mark_node)
7716 remove = true;
7717 else if (TYPE_REF_P (TREE_TYPE (t))
7718 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7719 remove = true;
7720 }
7721 if (need_implicitly_determined)
7722 {
7723 const char *share_name = NULL;
7724
7725 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7726 share_name = "threadprivate";
7727 else switch (cxx_omp_predetermined_sharing_1 (t))
7728 {
7729 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7730 break;
7731 case OMP_CLAUSE_DEFAULT_SHARED:
7732 if (VAR_P (t)
7733 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7734 && TREE_STATIC (t)
7735 && cxx_omp_const_qual_no_mutable (t))
7736 {
7737 tree ctx = CP_DECL_CONTEXT (t);
7738 /* const qualified static data members without mutable
7739 member may be specified in firstprivate clause. */
7740 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
7741 break;
7742 }
7743 share_name = "shared";
7744 break;
7745 case OMP_CLAUSE_DEFAULT_PRIVATE:
7746 share_name = "private";
7747 break;
7748 default:
7749 gcc_unreachable ();
7750 }
7751 if (share_name)
7752 {
7753 error_at (OMP_CLAUSE_LOCATION (c),
7754 "%qE is predetermined %qs for %qs",
7755 omp_clause_printable_decl (t), share_name,
7756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7757 remove = true;
7758 }
7759 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
7760 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
7761 && cxx_omp_const_qual_no_mutable (t))
7762 {
7763 error_at (OMP_CLAUSE_LOCATION (c),
7764 "%<const%> qualified %qE without %<mutable%> member "
7765 "may appear only in %<shared%> or %<firstprivate%> "
7766 "clauses", omp_clause_printable_decl (t));
7767 remove = true;
7768 }
7769 }
7770
7771 /* We're interested in the base element, not arrays. */
7772 inner_type = type = TREE_TYPE (t);
7773 if ((need_complete_type
7774 || need_copy_assignment
7775 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7776 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7777 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
7778 && TYPE_REF_P (inner_type))
7779 inner_type = TREE_TYPE (inner_type);
7780 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7781 inner_type = TREE_TYPE (inner_type);
7782
7783 /* Check for special function availability by building a call to one.
7784 Save the results, because later we won't be in the right context
7785 for making these queries. */
7786 if (CLASS_TYPE_P (inner_type)
7787 && COMPLETE_TYPE_P (inner_type)
7788 && (need_default_ctor || need_copy_ctor
7789 || need_copy_assignment || need_dtor)
7790 && !type_dependent_expression_p (t)
7791 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7792 need_copy_ctor, need_copy_assignment,
7793 need_dtor))
7794 remove = true;
7795
7796 if (!remove
7797 && c_kind == OMP_CLAUSE_SHARED
7798 && processing_template_decl)
7799 {
7800 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7801 if (t)
7802 OMP_CLAUSE_DECL (c) = t;
7803 }
7804
7805 if (remove)
7806 *pc = OMP_CLAUSE_CHAIN (c);
7807 else
7808 pc = &OMP_CLAUSE_CHAIN (c);
7809 }
7810
7811 bitmap_obstack_release (NULL);
7812 return clauses;
7813 }
7814
7815 /* Start processing OpenMP clauses that can include any
7816 privatization clauses for non-static data members. */
7817
7818 tree
7819 push_omp_privatization_clauses (bool ignore_next)
7820 {
7821 if (omp_private_member_ignore_next)
7822 {
7823 omp_private_member_ignore_next = ignore_next;
7824 return NULL_TREE;
7825 }
7826 omp_private_member_ignore_next = ignore_next;
7827 if (omp_private_member_map)
7828 omp_private_member_vec.safe_push (error_mark_node);
7829 return push_stmt_list ();
7830 }
7831
7832 /* Revert remapping of any non-static data members since
7833 the last push_omp_privatization_clauses () call. */
7834
7835 void
7836 pop_omp_privatization_clauses (tree stmt)
7837 {
7838 if (stmt == NULL_TREE)
7839 return;
7840 stmt = pop_stmt_list (stmt);
7841 if (omp_private_member_map)
7842 {
7843 while (!omp_private_member_vec.is_empty ())
7844 {
7845 tree t = omp_private_member_vec.pop ();
7846 if (t == error_mark_node)
7847 {
7848 add_stmt (stmt);
7849 return;
7850 }
7851 bool no_decl_expr = t == integer_zero_node;
7852 if (no_decl_expr)
7853 t = omp_private_member_vec.pop ();
7854 tree *v = omp_private_member_map->get (t);
7855 gcc_assert (v);
7856 if (!no_decl_expr)
7857 add_decl_expr (*v);
7858 omp_private_member_map->remove (t);
7859 }
7860 delete omp_private_member_map;
7861 omp_private_member_map = NULL;
7862 }
7863 add_stmt (stmt);
7864 }
7865
7866 /* Remember OpenMP privatization clauses mapping and clear it.
7867 Used for lambdas. */
7868
7869 void
7870 save_omp_privatization_clauses (vec<tree> &save)
7871 {
7872 save = vNULL;
7873 if (omp_private_member_ignore_next)
7874 save.safe_push (integer_one_node);
7875 omp_private_member_ignore_next = false;
7876 if (!omp_private_member_map)
7877 return;
7878
7879 while (!omp_private_member_vec.is_empty ())
7880 {
7881 tree t = omp_private_member_vec.pop ();
7882 if (t == error_mark_node)
7883 {
7884 save.safe_push (t);
7885 continue;
7886 }
7887 tree n = t;
7888 if (t == integer_zero_node)
7889 t = omp_private_member_vec.pop ();
7890 tree *v = omp_private_member_map->get (t);
7891 gcc_assert (v);
7892 save.safe_push (*v);
7893 save.safe_push (t);
7894 if (n != t)
7895 save.safe_push (n);
7896 }
7897 delete omp_private_member_map;
7898 omp_private_member_map = NULL;
7899 }
7900
7901 /* Restore OpenMP privatization clauses mapping saved by the
7902 above function. */
7903
7904 void
7905 restore_omp_privatization_clauses (vec<tree> &save)
7906 {
7907 gcc_assert (omp_private_member_vec.is_empty ());
7908 omp_private_member_ignore_next = false;
7909 if (save.is_empty ())
7910 return;
7911 if (save.length () == 1 && save[0] == integer_one_node)
7912 {
7913 omp_private_member_ignore_next = true;
7914 save.release ();
7915 return;
7916 }
7917
7918 omp_private_member_map = new hash_map <tree, tree>;
7919 while (!save.is_empty ())
7920 {
7921 tree t = save.pop ();
7922 tree n = t;
7923 if (t != error_mark_node)
7924 {
7925 if (t == integer_one_node)
7926 {
7927 omp_private_member_ignore_next = true;
7928 gcc_assert (save.is_empty ());
7929 break;
7930 }
7931 if (t == integer_zero_node)
7932 t = save.pop ();
7933 tree &v = omp_private_member_map->get_or_insert (t);
7934 v = save.pop ();
7935 }
7936 omp_private_member_vec.safe_push (t);
7937 if (n != t)
7938 omp_private_member_vec.safe_push (n);
7939 }
7940 save.release ();
7941 }
7942
7943 /* For all variables in the tree_list VARS, mark them as thread local. */
7944
7945 void
7946 finish_omp_threadprivate (tree vars)
7947 {
7948 tree t;
7949
7950 /* Mark every variable in VARS to be assigned thread local storage. */
7951 for (t = vars; t; t = TREE_CHAIN (t))
7952 {
7953 tree v = TREE_PURPOSE (t);
7954
7955 if (error_operand_p (v))
7956 ;
7957 else if (!VAR_P (v))
7958 error ("%<threadprivate%> %qD is not file, namespace "
7959 "or block scope variable", v);
7960 /* If V had already been marked threadprivate, it doesn't matter
7961 whether it had been used prior to this point. */
7962 else if (TREE_USED (v)
7963 && (DECL_LANG_SPECIFIC (v) == NULL
7964 || !CP_DECL_THREADPRIVATE_P (v)))
7965 error ("%qE declared %<threadprivate%> after first use", v);
7966 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7967 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7968 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7969 error ("%<threadprivate%> %qE has incomplete type", v);
7970 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7971 && CP_DECL_CONTEXT (v) != current_class_type)
7972 error ("%<threadprivate%> %qE directive not "
7973 "in %qT definition", v, CP_DECL_CONTEXT (v));
7974 else
7975 {
7976 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7977 if (DECL_LANG_SPECIFIC (v) == NULL)
7978 retrofit_lang_decl (v);
7979
7980 if (! CP_DECL_THREAD_LOCAL_P (v))
7981 {
7982 CP_DECL_THREAD_LOCAL_P (v) = true;
7983 set_decl_tls_model (v, decl_default_tls_model (v));
7984 /* If rtl has been already set for this var, call
7985 make_decl_rtl once again, so that encode_section_info
7986 has a chance to look at the new decl flags. */
7987 if (DECL_RTL_SET_P (v))
7988 make_decl_rtl (v);
7989 }
7990 CP_DECL_THREADPRIVATE_P (v) = 1;
7991 }
7992 }
7993 }
7994
7995 /* Build an OpenMP structured block. */
7996
7997 tree
7998 begin_omp_structured_block (void)
7999 {
8000 return do_pushlevel (sk_omp);
8001 }
8002
8003 tree
8004 finish_omp_structured_block (tree block)
8005 {
8006 return do_poplevel (block);
8007 }
8008
8009 /* Similarly, except force the retention of the BLOCK. */
8010
8011 tree
8012 begin_omp_parallel (void)
8013 {
8014 keep_next_level (true);
8015 return begin_omp_structured_block ();
8016 }
8017
8018 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
8019 statement. */
8020
8021 tree
8022 finish_oacc_data (tree clauses, tree block)
8023 {
8024 tree stmt;
8025
8026 block = finish_omp_structured_block (block);
8027
8028 stmt = make_node (OACC_DATA);
8029 TREE_TYPE (stmt) = void_type_node;
8030 OACC_DATA_CLAUSES (stmt) = clauses;
8031 OACC_DATA_BODY (stmt) = block;
8032
8033 return add_stmt (stmt);
8034 }
8035
8036 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8037 statement. */
8038
8039 tree
8040 finish_oacc_host_data (tree clauses, tree block)
8041 {
8042 tree stmt;
8043
8044 block = finish_omp_structured_block (block);
8045
8046 stmt = make_node (OACC_HOST_DATA);
8047 TREE_TYPE (stmt) = void_type_node;
8048 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
8049 OACC_HOST_DATA_BODY (stmt) = block;
8050
8051 return add_stmt (stmt);
8052 }
8053
8054 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
8055 statement. */
8056
8057 tree
8058 finish_omp_construct (enum tree_code code, tree body, tree clauses)
8059 {
8060 body = finish_omp_structured_block (body);
8061
8062 tree stmt = make_node (code);
8063 TREE_TYPE (stmt) = void_type_node;
8064 OMP_BODY (stmt) = body;
8065 OMP_CLAUSES (stmt) = clauses;
8066
8067 return add_stmt (stmt);
8068 }
8069
8070 tree
8071 finish_omp_parallel (tree clauses, tree body)
8072 {
8073 tree stmt;
8074
8075 body = finish_omp_structured_block (body);
8076
8077 stmt = make_node (OMP_PARALLEL);
8078 TREE_TYPE (stmt) = void_type_node;
8079 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8080 OMP_PARALLEL_BODY (stmt) = body;
8081
8082 return add_stmt (stmt);
8083 }
8084
8085 tree
8086 begin_omp_task (void)
8087 {
8088 keep_next_level (true);
8089 return begin_omp_structured_block ();
8090 }
8091
8092 tree
8093 finish_omp_task (tree clauses, tree body)
8094 {
8095 tree stmt;
8096
8097 body = finish_omp_structured_block (body);
8098
8099 stmt = make_node (OMP_TASK);
8100 TREE_TYPE (stmt) = void_type_node;
8101 OMP_TASK_CLAUSES (stmt) = clauses;
8102 OMP_TASK_BODY (stmt) = body;
8103
8104 return add_stmt (stmt);
8105 }
8106
8107 /* Helper function for finish_omp_for. Convert Ith random access iterator
8108 into integral iterator. Return FALSE if successful. */
8109
8110 static bool
8111 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
8112 tree declv, tree orig_declv, tree initv,
8113 tree condv, tree incrv, tree *body,
8114 tree *pre_body, tree &clauses,
8115 int collapse, int ordered)
8116 {
8117 tree diff, iter_init, iter_incr = NULL, last;
8118 tree incr_var = NULL, orig_pre_body, orig_body, c;
8119 tree decl = TREE_VEC_ELT (declv, i);
8120 tree init = TREE_VEC_ELT (initv, i);
8121 tree cond = TREE_VEC_ELT (condv, i);
8122 tree incr = TREE_VEC_ELT (incrv, i);
8123 tree iter = decl;
8124 location_t elocus = locus;
8125
8126 if (init && EXPR_HAS_LOCATION (init))
8127 elocus = EXPR_LOCATION (init);
8128
8129 cond = cp_fully_fold (cond);
8130 switch (TREE_CODE (cond))
8131 {
8132 case GT_EXPR:
8133 case GE_EXPR:
8134 case LT_EXPR:
8135 case LE_EXPR:
8136 case NE_EXPR:
8137 if (TREE_OPERAND (cond, 1) == iter)
8138 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
8139 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
8140 if (TREE_OPERAND (cond, 0) != iter)
8141 cond = error_mark_node;
8142 else
8143 {
8144 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
8145 TREE_CODE (cond),
8146 iter, ERROR_MARK,
8147 TREE_OPERAND (cond, 1), ERROR_MARK,
8148 NULL, tf_warning_or_error);
8149 if (error_operand_p (tem))
8150 return true;
8151 }
8152 break;
8153 default:
8154 cond = error_mark_node;
8155 break;
8156 }
8157 if (cond == error_mark_node)
8158 {
8159 error_at (elocus, "invalid controlling predicate");
8160 return true;
8161 }
8162 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
8163 ERROR_MARK, iter, ERROR_MARK, NULL,
8164 tf_warning_or_error);
8165 diff = cp_fully_fold (diff);
8166 if (error_operand_p (diff))
8167 return true;
8168 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
8169 {
8170 error_at (elocus, "difference between %qE and %qD does not have integer type",
8171 TREE_OPERAND (cond, 1), iter);
8172 return true;
8173 }
8174 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
8175 TREE_VEC_ELT (declv, i), NULL_TREE,
8176 cond, cp_walk_subtrees))
8177 return true;
8178
8179 switch (TREE_CODE (incr))
8180 {
8181 case PREINCREMENT_EXPR:
8182 case PREDECREMENT_EXPR:
8183 case POSTINCREMENT_EXPR:
8184 case POSTDECREMENT_EXPR:
8185 if (TREE_OPERAND (incr, 0) != iter)
8186 {
8187 incr = error_mark_node;
8188 break;
8189 }
8190 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
8191 TREE_CODE (incr), iter,
8192 tf_warning_or_error);
8193 if (error_operand_p (iter_incr))
8194 return true;
8195 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
8196 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
8197 incr = integer_one_node;
8198 else
8199 incr = integer_minus_one_node;
8200 break;
8201 case MODIFY_EXPR:
8202 if (TREE_OPERAND (incr, 0) != iter)
8203 incr = error_mark_node;
8204 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
8205 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
8206 {
8207 tree rhs = TREE_OPERAND (incr, 1);
8208 if (TREE_OPERAND (rhs, 0) == iter)
8209 {
8210 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
8211 != INTEGER_TYPE)
8212 incr = error_mark_node;
8213 else
8214 {
8215 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8216 iter, TREE_CODE (rhs),
8217 TREE_OPERAND (rhs, 1),
8218 tf_warning_or_error);
8219 if (error_operand_p (iter_incr))
8220 return true;
8221 incr = TREE_OPERAND (rhs, 1);
8222 incr = cp_convert (TREE_TYPE (diff), incr,
8223 tf_warning_or_error);
8224 if (TREE_CODE (rhs) == MINUS_EXPR)
8225 {
8226 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
8227 incr = fold_simple (incr);
8228 }
8229 if (TREE_CODE (incr) != INTEGER_CST
8230 && (TREE_CODE (incr) != NOP_EXPR
8231 || (TREE_CODE (TREE_OPERAND (incr, 0))
8232 != INTEGER_CST)))
8233 iter_incr = NULL;
8234 }
8235 }
8236 else if (TREE_OPERAND (rhs, 1) == iter)
8237 {
8238 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
8239 || TREE_CODE (rhs) != PLUS_EXPR)
8240 incr = error_mark_node;
8241 else
8242 {
8243 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
8244 PLUS_EXPR,
8245 TREE_OPERAND (rhs, 0),
8246 ERROR_MARK, iter,
8247 ERROR_MARK, NULL,
8248 tf_warning_or_error);
8249 if (error_operand_p (iter_incr))
8250 return true;
8251 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8252 iter, NOP_EXPR,
8253 iter_incr,
8254 tf_warning_or_error);
8255 if (error_operand_p (iter_incr))
8256 return true;
8257 incr = TREE_OPERAND (rhs, 0);
8258 iter_incr = NULL;
8259 }
8260 }
8261 else
8262 incr = error_mark_node;
8263 }
8264 else
8265 incr = error_mark_node;
8266 break;
8267 default:
8268 incr = error_mark_node;
8269 break;
8270 }
8271
8272 if (incr == error_mark_node)
8273 {
8274 error_at (elocus, "invalid increment expression");
8275 return true;
8276 }
8277
8278 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
8279 incr = cp_fully_fold (incr);
8280 bool taskloop_iv_seen = false;
8281 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8282 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8283 && OMP_CLAUSE_DECL (c) == iter)
8284 {
8285 if (code == OMP_TASKLOOP)
8286 {
8287 taskloop_iv_seen = true;
8288 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
8289 }
8290 break;
8291 }
8292 else if (code == OMP_TASKLOOP
8293 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8294 && OMP_CLAUSE_DECL (c) == iter)
8295 {
8296 taskloop_iv_seen = true;
8297 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
8298 }
8299
8300 decl = create_temporary_var (TREE_TYPE (diff));
8301 pushdecl (decl);
8302 add_decl_expr (decl);
8303 last = create_temporary_var (TREE_TYPE (diff));
8304 pushdecl (last);
8305 add_decl_expr (last);
8306 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
8307 && (!ordered || (i < collapse && collapse > 1)))
8308 {
8309 incr_var = create_temporary_var (TREE_TYPE (diff));
8310 pushdecl (incr_var);
8311 add_decl_expr (incr_var);
8312 }
8313 gcc_assert (stmts_are_full_exprs_p ());
8314 tree diffvar = NULL_TREE;
8315 if (code == OMP_TASKLOOP)
8316 {
8317 if (!taskloop_iv_seen)
8318 {
8319 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8320 OMP_CLAUSE_DECL (ivc) = iter;
8321 cxx_omp_finish_clause (ivc, NULL);
8322 OMP_CLAUSE_CHAIN (ivc) = clauses;
8323 clauses = ivc;
8324 }
8325 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8326 OMP_CLAUSE_DECL (lvc) = last;
8327 OMP_CLAUSE_CHAIN (lvc) = clauses;
8328 clauses = lvc;
8329 diffvar = create_temporary_var (TREE_TYPE (diff));
8330 pushdecl (diffvar);
8331 add_decl_expr (diffvar);
8332 }
8333
8334 orig_pre_body = *pre_body;
8335 *pre_body = push_stmt_list ();
8336 if (orig_pre_body)
8337 add_stmt (orig_pre_body);
8338 if (init != NULL)
8339 finish_expr_stmt (build_x_modify_expr (elocus,
8340 iter, NOP_EXPR, init,
8341 tf_warning_or_error));
8342 init = build_int_cst (TREE_TYPE (diff), 0);
8343 if (c && iter_incr == NULL
8344 && (!ordered || (i < collapse && collapse > 1)))
8345 {
8346 if (incr_var)
8347 {
8348 finish_expr_stmt (build_x_modify_expr (elocus,
8349 incr_var, NOP_EXPR,
8350 incr, tf_warning_or_error));
8351 incr = incr_var;
8352 }
8353 iter_incr = build_x_modify_expr (elocus,
8354 iter, PLUS_EXPR, incr,
8355 tf_warning_or_error);
8356 }
8357 if (c && ordered && i < collapse && collapse > 1)
8358 iter_incr = incr;
8359 finish_expr_stmt (build_x_modify_expr (elocus,
8360 last, NOP_EXPR, init,
8361 tf_warning_or_error));
8362 if (diffvar)
8363 {
8364 finish_expr_stmt (build_x_modify_expr (elocus,
8365 diffvar, NOP_EXPR,
8366 diff, tf_warning_or_error));
8367 diff = diffvar;
8368 }
8369 *pre_body = pop_stmt_list (*pre_body);
8370
8371 cond = cp_build_binary_op (elocus,
8372 TREE_CODE (cond), decl, diff,
8373 tf_warning_or_error);
8374 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
8375 elocus, incr, NULL_TREE);
8376
8377 orig_body = *body;
8378 *body = push_stmt_list ();
8379 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
8380 iter_init = build_x_modify_expr (elocus,
8381 iter, PLUS_EXPR, iter_init,
8382 tf_warning_or_error);
8383 if (iter_init != error_mark_node)
8384 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8385 finish_expr_stmt (iter_init);
8386 finish_expr_stmt (build_x_modify_expr (elocus,
8387 last, NOP_EXPR, decl,
8388 tf_warning_or_error));
8389 add_stmt (orig_body);
8390 *body = pop_stmt_list (*body);
8391
8392 if (c)
8393 {
8394 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
8395 if (!ordered)
8396 finish_expr_stmt (iter_incr);
8397 else
8398 {
8399 iter_init = decl;
8400 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
8401 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
8402 iter_init, iter_incr);
8403 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
8404 iter_init = build_x_modify_expr (elocus,
8405 iter, PLUS_EXPR, iter_init,
8406 tf_warning_or_error);
8407 if (iter_init != error_mark_node)
8408 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8409 finish_expr_stmt (iter_init);
8410 }
8411 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8412 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8413 }
8414
8415 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
8416 {
8417 tree t = TREE_VEC_ELT (orig_declv, i);
8418 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8419 && TREE_VALUE (t) == NULL_TREE
8420 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
8421 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
8422 TREE_VALUE (t) = last;
8423 }
8424 else
8425 TREE_VEC_ELT (orig_declv, i)
8426 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
8427 TREE_VEC_ELT (declv, i) = decl;
8428 TREE_VEC_ELT (initv, i) = init;
8429 TREE_VEC_ELT (condv, i) = cond;
8430 TREE_VEC_ELT (incrv, i) = incr;
8431
8432 return false;
8433 }
8434
8435 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8436 are directly for their associated operands in the statement. DECL
8437 and INIT are a combo; if DECL is NULL then INIT ought to be a
8438 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8439 optional statements that need to go before the loop into its
8440 sk_omp scope. */
8441
8442 tree
8443 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8444 tree orig_declv, tree initv, tree condv, tree incrv,
8445 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8446 {
8447 tree omp_for = NULL, orig_incr = NULL;
8448 tree decl = NULL, init, cond, incr;
8449 location_t elocus;
8450 int i;
8451 int collapse = 1;
8452 int ordered = 0;
8453
8454 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8455 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8456 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8457 if (TREE_VEC_LENGTH (declv) > 1)
8458 {
8459 tree c;
8460
8461 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8462 if (c)
8463 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8464 else
8465 {
8466 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8467 if (c)
8468 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8469 if (collapse != TREE_VEC_LENGTH (declv))
8470 ordered = TREE_VEC_LENGTH (declv);
8471 }
8472 }
8473 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8474 {
8475 decl = TREE_VEC_ELT (declv, i);
8476 init = TREE_VEC_ELT (initv, i);
8477 cond = TREE_VEC_ELT (condv, i);
8478 incr = TREE_VEC_ELT (incrv, i);
8479 elocus = locus;
8480
8481 if (decl == NULL)
8482 {
8483 if (init != NULL)
8484 switch (TREE_CODE (init))
8485 {
8486 case MODIFY_EXPR:
8487 decl = TREE_OPERAND (init, 0);
8488 init = TREE_OPERAND (init, 1);
8489 break;
8490 case MODOP_EXPR:
8491 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8492 {
8493 decl = TREE_OPERAND (init, 0);
8494 init = TREE_OPERAND (init, 2);
8495 }
8496 break;
8497 default:
8498 break;
8499 }
8500
8501 if (decl == NULL)
8502 {
8503 error_at (locus,
8504 "expected iteration declaration or initialization");
8505 return NULL;
8506 }
8507 }
8508
8509 if (init && EXPR_HAS_LOCATION (init))
8510 elocus = EXPR_LOCATION (init);
8511
8512 if (cond == global_namespace)
8513 continue;
8514
8515 if (cond == NULL)
8516 {
8517 error_at (elocus, "missing controlling predicate");
8518 return NULL;
8519 }
8520
8521 if (incr == NULL)
8522 {
8523 error_at (elocus, "missing increment expression");
8524 return NULL;
8525 }
8526
8527 TREE_VEC_ELT (declv, i) = decl;
8528 TREE_VEC_ELT (initv, i) = init;
8529 }
8530
8531 if (orig_inits)
8532 {
8533 bool fail = false;
8534 tree orig_init;
8535 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8536 if (orig_init
8537 && !c_omp_check_loop_iv_exprs (locus, orig_declv
8538 ? orig_declv : declv,
8539 TREE_VEC_ELT (declv, i), orig_init,
8540 NULL_TREE, cp_walk_subtrees))
8541 fail = true;
8542 if (fail)
8543 return NULL;
8544 }
8545
8546 if (dependent_omp_for_p (declv, initv, condv, incrv))
8547 {
8548 tree stmt;
8549
8550 stmt = make_node (code);
8551
8552 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8553 {
8554 /* This is really just a place-holder. We'll be decomposing this
8555 again and going through the cp_build_modify_expr path below when
8556 we instantiate the thing. */
8557 TREE_VEC_ELT (initv, i)
8558 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8559 TREE_VEC_ELT (initv, i));
8560 }
8561
8562 TREE_TYPE (stmt) = void_type_node;
8563 OMP_FOR_INIT (stmt) = initv;
8564 OMP_FOR_COND (stmt) = condv;
8565 OMP_FOR_INCR (stmt) = incrv;
8566 OMP_FOR_BODY (stmt) = body;
8567 OMP_FOR_PRE_BODY (stmt) = pre_body;
8568 OMP_FOR_CLAUSES (stmt) = clauses;
8569
8570 SET_EXPR_LOCATION (stmt, locus);
8571 return add_stmt (stmt);
8572 }
8573
8574 if (!orig_declv)
8575 orig_declv = copy_node (declv);
8576
8577 if (processing_template_decl)
8578 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8579
8580 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8581 {
8582 decl = TREE_VEC_ELT (declv, i);
8583 init = TREE_VEC_ELT (initv, i);
8584 cond = TREE_VEC_ELT (condv, i);
8585 incr = TREE_VEC_ELT (incrv, i);
8586 if (orig_incr)
8587 TREE_VEC_ELT (orig_incr, i) = incr;
8588 elocus = locus;
8589
8590 if (init && EXPR_HAS_LOCATION (init))
8591 elocus = EXPR_LOCATION (init);
8592
8593 if (!DECL_P (decl))
8594 {
8595 error_at (elocus, "expected iteration declaration or initialization");
8596 return NULL;
8597 }
8598
8599 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8600 {
8601 if (orig_incr)
8602 TREE_VEC_ELT (orig_incr, i) = incr;
8603 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8604 TREE_CODE (TREE_OPERAND (incr, 1)),
8605 TREE_OPERAND (incr, 2),
8606 tf_warning_or_error);
8607 }
8608
8609 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8610 {
8611 if (code == OMP_SIMD)
8612 {
8613 error_at (elocus, "%<#pragma omp simd%> used with class "
8614 "iteration variable %qE", decl);
8615 return NULL;
8616 }
8617 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8618 initv, condv, incrv, &body,
8619 &pre_body, clauses,
8620 collapse, ordered))
8621 return NULL;
8622 continue;
8623 }
8624
8625 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8626 && !TYPE_PTR_P (TREE_TYPE (decl)))
8627 {
8628 error_at (elocus, "invalid type for iteration variable %qE", decl);
8629 return NULL;
8630 }
8631
8632 if (!processing_template_decl)
8633 {
8634 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8635 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8636 tf_warning_or_error);
8637 }
8638 else
8639 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8640 if (cond
8641 && TREE_SIDE_EFFECTS (cond)
8642 && COMPARISON_CLASS_P (cond)
8643 && !processing_template_decl)
8644 {
8645 tree t = TREE_OPERAND (cond, 0);
8646 if (TREE_SIDE_EFFECTS (t)
8647 && t != decl
8648 && (TREE_CODE (t) != NOP_EXPR
8649 || TREE_OPERAND (t, 0) != decl))
8650 TREE_OPERAND (cond, 0)
8651 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8652
8653 t = TREE_OPERAND (cond, 1);
8654 if (TREE_SIDE_EFFECTS (t)
8655 && t != decl
8656 && (TREE_CODE (t) != NOP_EXPR
8657 || TREE_OPERAND (t, 0) != decl))
8658 TREE_OPERAND (cond, 1)
8659 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8660 }
8661 if (decl == error_mark_node || init == error_mark_node)
8662 return NULL;
8663
8664 TREE_VEC_ELT (declv, i) = decl;
8665 TREE_VEC_ELT (initv, i) = init;
8666 TREE_VEC_ELT (condv, i) = cond;
8667 TREE_VEC_ELT (incrv, i) = incr;
8668 i++;
8669 }
8670
8671 if (pre_body && IS_EMPTY_STMT (pre_body))
8672 pre_body = NULL;
8673
8674 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8675 incrv, body, pre_body,
8676 !processing_template_decl);
8677
8678 /* Check for iterators appearing in lb, b or incr expressions. */
8679 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8680 omp_for = NULL_TREE;
8681
8682 if (omp_for == NULL)
8683 {
8684 return NULL;
8685 }
8686
8687 add_stmt (omp_for);
8688
8689 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8690 {
8691 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8692 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8693
8694 if (TREE_CODE (incr) != MODIFY_EXPR)
8695 continue;
8696
8697 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8698 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8699 && !processing_template_decl)
8700 {
8701 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8702 if (TREE_SIDE_EFFECTS (t)
8703 && t != decl
8704 && (TREE_CODE (t) != NOP_EXPR
8705 || TREE_OPERAND (t, 0) != decl))
8706 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8707 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8708
8709 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8710 if (TREE_SIDE_EFFECTS (t)
8711 && t != decl
8712 && (TREE_CODE (t) != NOP_EXPR
8713 || TREE_OPERAND (t, 0) != decl))
8714 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8715 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8716 }
8717
8718 if (orig_incr)
8719 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8720 }
8721 OMP_FOR_CLAUSES (omp_for) = clauses;
8722
8723 /* For simd loops with non-static data member iterators, we could have added
8724 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8725 step at this point, fill it in. */
8726 if (code == OMP_SIMD && !processing_template_decl
8727 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8728 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8729 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8730 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8731 {
8732 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8733 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8734 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8735 tree step, stept;
8736 switch (TREE_CODE (incr))
8737 {
8738 case PREINCREMENT_EXPR:
8739 case POSTINCREMENT_EXPR:
8740 /* c_omp_for_incr_canonicalize_ptr() should have been
8741 called to massage things appropriately. */
8742 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8743 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8744 break;
8745 case PREDECREMENT_EXPR:
8746 case POSTDECREMENT_EXPR:
8747 /* c_omp_for_incr_canonicalize_ptr() should have been
8748 called to massage things appropriately. */
8749 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8750 OMP_CLAUSE_LINEAR_STEP (c)
8751 = build_int_cst (TREE_TYPE (decl), -1);
8752 break;
8753 case MODIFY_EXPR:
8754 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8755 incr = TREE_OPERAND (incr, 1);
8756 switch (TREE_CODE (incr))
8757 {
8758 case PLUS_EXPR:
8759 if (TREE_OPERAND (incr, 1) == decl)
8760 step = TREE_OPERAND (incr, 0);
8761 else
8762 step = TREE_OPERAND (incr, 1);
8763 break;
8764 case MINUS_EXPR:
8765 case POINTER_PLUS_EXPR:
8766 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8767 step = TREE_OPERAND (incr, 1);
8768 break;
8769 default:
8770 gcc_unreachable ();
8771 }
8772 stept = TREE_TYPE (decl);
8773 if (INDIRECT_TYPE_P (stept))
8774 stept = sizetype;
8775 step = fold_convert (stept, step);
8776 if (TREE_CODE (incr) == MINUS_EXPR)
8777 step = fold_build1 (NEGATE_EXPR, stept, step);
8778 OMP_CLAUSE_LINEAR_STEP (c) = step;
8779 break;
8780 default:
8781 gcc_unreachable ();
8782 }
8783 }
8784
8785 return omp_for;
8786 }
8787
8788 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
8789 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
8790
8791 tree
8792 finish_omp_for_block (tree bind, tree omp_for)
8793 {
8794 if (omp_for == NULL_TREE
8795 || !OMP_FOR_ORIG_DECLS (omp_for)
8796 || bind == NULL_TREE
8797 || TREE_CODE (bind) != BIND_EXPR)
8798 return bind;
8799 tree b = NULL_TREE;
8800 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
8801 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
8802 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
8803 {
8804 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
8805 gcc_assert (BIND_EXPR_BLOCK (bind)
8806 && (BIND_EXPR_VARS (bind)
8807 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
8808 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
8809 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
8810 {
8811 if (*p == TREE_VEC_ELT (v, j))
8812 {
8813 tree var = *p;
8814 *p = DECL_CHAIN (*p);
8815 if (b == NULL_TREE)
8816 {
8817 b = make_node (BLOCK);
8818 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
8819 OMP_FOR_BODY (omp_for), b);
8820 TREE_SIDE_EFFECTS (b) = 1;
8821 OMP_FOR_BODY (omp_for) = b;
8822 }
8823 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
8824 BIND_EXPR_VARS (b) = var;
8825 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
8826 }
8827 }
8828 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
8829 }
8830 return bind;
8831 }
8832
8833 void
8834 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
8835 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
8836 tree clauses, enum omp_memory_order mo)
8837 {
8838 tree orig_lhs;
8839 tree orig_rhs;
8840 tree orig_v;
8841 tree orig_lhs1;
8842 tree orig_rhs1;
8843 bool dependent_p;
8844 tree stmt;
8845
8846 orig_lhs = lhs;
8847 orig_rhs = rhs;
8848 orig_v = v;
8849 orig_lhs1 = lhs1;
8850 orig_rhs1 = rhs1;
8851 dependent_p = false;
8852 stmt = NULL_TREE;
8853
8854 /* Even in a template, we can detect invalid uses of the atomic
8855 pragma if neither LHS nor RHS is type-dependent. */
8856 if (processing_template_decl)
8857 {
8858 dependent_p = (type_dependent_expression_p (lhs)
8859 || (rhs && type_dependent_expression_p (rhs))
8860 || (v && type_dependent_expression_p (v))
8861 || (lhs1 && type_dependent_expression_p (lhs1))
8862 || (rhs1 && type_dependent_expression_p (rhs1)));
8863 if (clauses)
8864 {
8865 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
8866 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
8867 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
8868 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
8869 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
8870 dependent_p = true;
8871 }
8872 if (!dependent_p)
8873 {
8874 lhs = build_non_dependent_expr (lhs);
8875 if (rhs)
8876 rhs = build_non_dependent_expr (rhs);
8877 if (v)
8878 v = build_non_dependent_expr (v);
8879 if (lhs1)
8880 lhs1 = build_non_dependent_expr (lhs1);
8881 if (rhs1)
8882 rhs1 = build_non_dependent_expr (rhs1);
8883 }
8884 }
8885 if (!dependent_p)
8886 {
8887 bool swapped = false;
8888 if (rhs1 && cp_tree_equal (lhs, rhs))
8889 {
8890 std::swap (rhs, rhs1);
8891 swapped = !commutative_tree_code (opcode);
8892 }
8893 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8894 {
8895 if (code == OMP_ATOMIC)
8896 error ("%<#pragma omp atomic update%> uses two different "
8897 "expressions for memory");
8898 else
8899 error ("%<#pragma omp atomic capture%> uses two different "
8900 "expressions for memory");
8901 return;
8902 }
8903 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8904 {
8905 if (code == OMP_ATOMIC)
8906 error ("%<#pragma omp atomic update%> uses two different "
8907 "expressions for memory");
8908 else
8909 error ("%<#pragma omp atomic capture%> uses two different "
8910 "expressions for memory");
8911 return;
8912 }
8913 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
8914 v, lhs1, rhs1, swapped, mo,
8915 processing_template_decl != 0);
8916 if (stmt == error_mark_node)
8917 return;
8918 }
8919 if (processing_template_decl)
8920 {
8921 if (code == OMP_ATOMIC_READ)
8922 {
8923 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
8924 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8925 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8926 }
8927 else
8928 {
8929 if (opcode == NOP_EXPR)
8930 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8931 else
8932 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8933 if (orig_rhs1)
8934 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8935 COMPOUND_EXPR, orig_rhs1, stmt);
8936 if (code != OMP_ATOMIC)
8937 {
8938 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
8939 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8940 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8941 }
8942 }
8943 stmt = build2 (OMP_ATOMIC, void_type_node,
8944 clauses ? clauses : integer_zero_node, stmt);
8945 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8946 SET_EXPR_LOCATION (stmt, loc);
8947 }
8948 finish_expr_stmt (stmt);
8949 }
8950
8951 void
8952 finish_omp_barrier (void)
8953 {
8954 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8955 vec<tree, va_gc> *vec = make_tree_vector ();
8956 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8957 release_tree_vector (vec);
8958 finish_expr_stmt (stmt);
8959 }
8960
8961 void
8962 finish_omp_depobj (location_t loc, tree depobj,
8963 enum omp_clause_depend_kind kind, tree clause)
8964 {
8965 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
8966 {
8967 if (!lvalue_p (depobj))
8968 {
8969 error_at (EXPR_LOC_OR_LOC (depobj, loc),
8970 "%<depobj%> expression is not lvalue expression");
8971 depobj = error_mark_node;
8972 }
8973 }
8974
8975 if (processing_template_decl)
8976 {
8977 if (clause == NULL_TREE)
8978 clause = build_int_cst (integer_type_node, kind);
8979 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
8980 return;
8981 }
8982
8983 if (!error_operand_p (depobj))
8984 {
8985 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
8986 if (addr == error_mark_node)
8987 depobj = error_mark_node;
8988 else
8989 depobj = cp_build_indirect_ref (addr, RO_UNARY_STAR,
8990 tf_warning_or_error);
8991 }
8992
8993 c_finish_omp_depobj (loc, depobj, kind, clause);
8994 }
8995
8996 void
8997 finish_omp_flush (int mo)
8998 {
8999 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
9000 vec<tree, va_gc> *vec = make_tree_vector ();
9001 if (mo != MEMMODEL_LAST)
9002 {
9003 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
9004 vec->quick_push (build_int_cst (integer_type_node, mo));
9005 }
9006 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9007 release_tree_vector (vec);
9008 finish_expr_stmt (stmt);
9009 }
9010
9011 void
9012 finish_omp_taskwait (void)
9013 {
9014 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
9015 vec<tree, va_gc> *vec = make_tree_vector ();
9016 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9017 release_tree_vector (vec);
9018 finish_expr_stmt (stmt);
9019 }
9020
9021 void
9022 finish_omp_taskyield (void)
9023 {
9024 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
9025 vec<tree, va_gc> *vec = make_tree_vector ();
9026 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9027 release_tree_vector (vec);
9028 finish_expr_stmt (stmt);
9029 }
9030
9031 void
9032 finish_omp_cancel (tree clauses)
9033 {
9034 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
9035 int mask = 0;
9036 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9037 mask = 1;
9038 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9039 mask = 2;
9040 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9041 mask = 4;
9042 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9043 mask = 8;
9044 else
9045 {
9046 error ("%<#pragma omp cancel%> must specify one of "
9047 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9048 return;
9049 }
9050 vec<tree, va_gc> *vec = make_tree_vector ();
9051 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
9052 if (ifc != NULL_TREE)
9053 {
9054 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
9055 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
9056 error_at (OMP_CLAUSE_LOCATION (ifc),
9057 "expected %<cancel%> %<if%> clause modifier");
9058 else
9059 {
9060 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
9061 if (ifc2 != NULL_TREE)
9062 {
9063 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
9064 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
9065 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
9066 error_at (OMP_CLAUSE_LOCATION (ifc2),
9067 "expected %<cancel%> %<if%> clause modifier");
9068 }
9069 }
9070
9071 if (!processing_template_decl)
9072 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
9073 else
9074 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
9075 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
9076 integer_zero_node, ERROR_MARK,
9077 NULL, tf_warning_or_error);
9078 }
9079 else
9080 ifc = boolean_true_node;
9081 vec->quick_push (build_int_cst (integer_type_node, mask));
9082 vec->quick_push (ifc);
9083 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9084 release_tree_vector (vec);
9085 finish_expr_stmt (stmt);
9086 }
9087
9088 void
9089 finish_omp_cancellation_point (tree clauses)
9090 {
9091 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
9092 int mask = 0;
9093 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9094 mask = 1;
9095 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9096 mask = 2;
9097 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9098 mask = 4;
9099 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9100 mask = 8;
9101 else
9102 {
9103 error ("%<#pragma omp cancellation point%> must specify one of "
9104 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9105 return;
9106 }
9107 vec<tree, va_gc> *vec
9108 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
9109 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9110 release_tree_vector (vec);
9111 finish_expr_stmt (stmt);
9112 }
9113 \f
9114 /* Begin a __transaction_atomic or __transaction_relaxed statement.
9115 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
9116 should create an extra compound stmt. */
9117
9118 tree
9119 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
9120 {
9121 tree r;
9122
9123 if (pcompound)
9124 *pcompound = begin_compound_stmt (0);
9125
9126 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
9127
9128 /* Only add the statement to the function if support enabled. */
9129 if (flag_tm)
9130 add_stmt (r);
9131 else
9132 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
9133 ? G_("%<__transaction_relaxed%> without "
9134 "transactional memory support enabled")
9135 : G_("%<__transaction_atomic%> without "
9136 "transactional memory support enabled")));
9137
9138 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
9139 TREE_SIDE_EFFECTS (r) = 1;
9140 return r;
9141 }
9142
9143 /* End a __transaction_atomic or __transaction_relaxed statement.
9144 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
9145 and we should end the compound. If NOEX is non-NULL, we wrap the body in
9146 a MUST_NOT_THROW_EXPR with NOEX as condition. */
9147
9148 void
9149 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
9150 {
9151 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
9152 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
9153 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
9154 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
9155
9156 /* noexcept specifications are not allowed for function transactions. */
9157 gcc_assert (!(noex && compound_stmt));
9158 if (noex)
9159 {
9160 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
9161 noex);
9162 protected_set_expr_location
9163 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
9164 TREE_SIDE_EFFECTS (body) = 1;
9165 TRANSACTION_EXPR_BODY (stmt) = body;
9166 }
9167
9168 if (compound_stmt)
9169 finish_compound_stmt (compound_stmt);
9170 }
9171
9172 /* Build a __transaction_atomic or __transaction_relaxed expression. If
9173 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
9174 condition. */
9175
9176 tree
9177 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
9178 {
9179 tree ret;
9180 if (noex)
9181 {
9182 expr = build_must_not_throw_expr (expr, noex);
9183 protected_set_expr_location (expr, loc);
9184 TREE_SIDE_EFFECTS (expr) = 1;
9185 }
9186 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
9187 if (flags & TM_STMT_ATTR_RELAXED)
9188 TRANSACTION_EXPR_RELAXED (ret) = 1;
9189 TREE_SIDE_EFFECTS (ret) = 1;
9190 SET_EXPR_LOCATION (ret, loc);
9191 return ret;
9192 }
9193 \f
9194 void
9195 init_cp_semantics (void)
9196 {
9197 }
9198 \f
9199 /* Build a STATIC_ASSERT for a static assertion with the condition
9200 CONDITION and the message text MESSAGE. LOCATION is the location
9201 of the static assertion in the source code. When MEMBER_P, this
9202 static assertion is a member of a class. */
9203 void
9204 finish_static_assert (tree condition, tree message, location_t location,
9205 bool member_p)
9206 {
9207 tsubst_flags_t complain = tf_warning_or_error;
9208
9209 if (message == NULL_TREE
9210 || message == error_mark_node
9211 || condition == NULL_TREE
9212 || condition == error_mark_node)
9213 return;
9214
9215 if (check_for_bare_parameter_packs (condition))
9216 condition = error_mark_node;
9217
9218 if (instantiation_dependent_expression_p (condition))
9219 {
9220 /* We're in a template; build a STATIC_ASSERT and put it in
9221 the right place. */
9222 tree assertion;
9223
9224 assertion = make_node (STATIC_ASSERT);
9225 STATIC_ASSERT_CONDITION (assertion) = condition;
9226 STATIC_ASSERT_MESSAGE (assertion) = message;
9227 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
9228
9229 if (member_p)
9230 maybe_add_class_template_decl_list (current_class_type,
9231 assertion,
9232 /*friend_p=*/0);
9233 else
9234 add_stmt (assertion);
9235
9236 return;
9237 }
9238
9239 /* Fold the expression and convert it to a boolean value. */
9240 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
9241 complain, LOOKUP_NORMAL);
9242 condition = fold_non_dependent_expr (condition, complain,
9243 /*manifestly_const_eval=*/true);
9244
9245 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
9246 /* Do nothing; the condition is satisfied. */
9247 ;
9248 else
9249 {
9250 location_t saved_loc = input_location;
9251
9252 input_location = location;
9253 if (TREE_CODE (condition) == INTEGER_CST
9254 && integer_zerop (condition))
9255 {
9256 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
9257 (TREE_TYPE (TREE_TYPE (message))));
9258 int len = TREE_STRING_LENGTH (message) / sz - 1;
9259 /* Report the error. */
9260 if (len == 0)
9261 error ("static assertion failed");
9262 else
9263 error ("static assertion failed: %s",
9264 TREE_STRING_POINTER (message));
9265 }
9266 else if (condition && condition != error_mark_node)
9267 {
9268 error ("non-constant condition for static assertion");
9269 if (require_rvalue_constant_expression (condition))
9270 cxx_constant_value (condition);
9271 }
9272 input_location = saved_loc;
9273 }
9274 }
9275 \f
9276 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
9277 suitable for use as a type-specifier.
9278
9279 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
9280 id-expression or a class member access, FALSE when it was parsed as
9281 a full expression. */
9282
9283 tree
9284 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
9285 tsubst_flags_t complain)
9286 {
9287 tree type = NULL_TREE;
9288
9289 if (!expr || error_operand_p (expr))
9290 return error_mark_node;
9291
9292 if (TYPE_P (expr)
9293 || TREE_CODE (expr) == TYPE_DECL
9294 || (TREE_CODE (expr) == BIT_NOT_EXPR
9295 && TYPE_P (TREE_OPERAND (expr, 0))))
9296 {
9297 if (complain & tf_error)
9298 error ("argument to decltype must be an expression");
9299 return error_mark_node;
9300 }
9301
9302 /* Depending on the resolution of DR 1172, we may later need to distinguish
9303 instantiation-dependent but not type-dependent expressions so that, say,
9304 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
9305 if (instantiation_dependent_uneval_expression_p (expr))
9306 {
9307 type = cxx_make_type (DECLTYPE_TYPE);
9308 DECLTYPE_TYPE_EXPR (type) = expr;
9309 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
9310 = id_expression_or_member_access_p;
9311 SET_TYPE_STRUCTURAL_EQUALITY (type);
9312
9313 return type;
9314 }
9315
9316 /* The type denoted by decltype(e) is defined as follows: */
9317
9318 expr = resolve_nondeduced_context (expr, complain);
9319
9320 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
9321 return error_mark_node;
9322
9323 if (type_unknown_p (expr))
9324 {
9325 if (complain & tf_error)
9326 error ("decltype cannot resolve address of overloaded function");
9327 return error_mark_node;
9328 }
9329
9330 /* To get the size of a static data member declared as an array of
9331 unknown bound, we need to instantiate it. */
9332 if (VAR_P (expr)
9333 && VAR_HAD_UNKNOWN_BOUND (expr)
9334 && DECL_TEMPLATE_INSTANTIATION (expr))
9335 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
9336
9337 if (id_expression_or_member_access_p)
9338 {
9339 /* If e is an id-expression or a class member access (5.2.5
9340 [expr.ref]), decltype(e) is defined as the type of the entity
9341 named by e. If there is no such entity, or e names a set of
9342 overloaded functions, the program is ill-formed. */
9343 if (identifier_p (expr))
9344 expr = lookup_name (expr);
9345
9346 if (INDIRECT_REF_P (expr)
9347 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
9348 /* This can happen when the expression is, e.g., "a.b". Just
9349 look at the underlying operand. */
9350 expr = TREE_OPERAND (expr, 0);
9351
9352 if (TREE_CODE (expr) == OFFSET_REF
9353 || TREE_CODE (expr) == MEMBER_REF
9354 || TREE_CODE (expr) == SCOPE_REF)
9355 /* We're only interested in the field itself. If it is a
9356 BASELINK, we will need to see through it in the next
9357 step. */
9358 expr = TREE_OPERAND (expr, 1);
9359
9360 if (BASELINK_P (expr))
9361 /* See through BASELINK nodes to the underlying function. */
9362 expr = BASELINK_FUNCTIONS (expr);
9363
9364 /* decltype of a decomposition name drops references in the tuple case
9365 (unlike decltype of a normal variable) and keeps cv-qualifiers from
9366 the containing object in the other cases (unlike decltype of a member
9367 access expression). */
9368 if (DECL_DECOMPOSITION_P (expr))
9369 {
9370 if (DECL_HAS_VALUE_EXPR_P (expr))
9371 /* Expr is an array or struct subobject proxy, handle
9372 bit-fields properly. */
9373 return unlowered_expr_type (expr);
9374 else
9375 /* Expr is a reference variable for the tuple case. */
9376 return lookup_decomp_type (expr);
9377 }
9378
9379 switch (TREE_CODE (expr))
9380 {
9381 case FIELD_DECL:
9382 if (DECL_BIT_FIELD_TYPE (expr))
9383 {
9384 type = DECL_BIT_FIELD_TYPE (expr);
9385 break;
9386 }
9387 /* Fall through for fields that aren't bitfields. */
9388 gcc_fallthrough ();
9389
9390 case FUNCTION_DECL:
9391 case VAR_DECL:
9392 case CONST_DECL:
9393 case PARM_DECL:
9394 case RESULT_DECL:
9395 case TEMPLATE_PARM_INDEX:
9396 expr = mark_type_use (expr);
9397 type = TREE_TYPE (expr);
9398 break;
9399
9400 case ERROR_MARK:
9401 type = error_mark_node;
9402 break;
9403
9404 case COMPONENT_REF:
9405 case COMPOUND_EXPR:
9406 mark_type_use (expr);
9407 type = is_bitfield_expr_with_lowered_type (expr);
9408 if (!type)
9409 type = TREE_TYPE (TREE_OPERAND (expr, 1));
9410 break;
9411
9412 case BIT_FIELD_REF:
9413 gcc_unreachable ();
9414
9415 case INTEGER_CST:
9416 case PTRMEM_CST:
9417 /* We can get here when the id-expression refers to an
9418 enumerator or non-type template parameter. */
9419 type = TREE_TYPE (expr);
9420 break;
9421
9422 default:
9423 /* Handle instantiated template non-type arguments. */
9424 type = TREE_TYPE (expr);
9425 break;
9426 }
9427 }
9428 else
9429 {
9430 /* Within a lambda-expression:
9431
9432 Every occurrence of decltype((x)) where x is a possibly
9433 parenthesized id-expression that names an entity of
9434 automatic storage duration is treated as if x were
9435 transformed into an access to a corresponding data member
9436 of the closure type that would have been declared if x
9437 were a use of the denoted entity. */
9438 if (outer_automatic_var_p (expr)
9439 && current_function_decl
9440 && LAMBDA_FUNCTION_P (current_function_decl))
9441 type = capture_decltype (expr);
9442 else if (error_operand_p (expr))
9443 type = error_mark_node;
9444 else if (expr == current_class_ptr)
9445 /* If the expression is just "this", we want the
9446 cv-unqualified pointer for the "this" type. */
9447 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9448 else
9449 {
9450 /* Otherwise, where T is the type of e, if e is an lvalue,
9451 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9452 cp_lvalue_kind clk = lvalue_kind (expr);
9453 type = unlowered_expr_type (expr);
9454 gcc_assert (!TYPE_REF_P (type));
9455
9456 /* For vector types, pick a non-opaque variant. */
9457 if (VECTOR_TYPE_P (type))
9458 type = strip_typedefs (type);
9459
9460 if (clk != clk_none && !(clk & clk_class))
9461 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9462 }
9463 }
9464
9465 return type;
9466 }
9467
9468 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9469 __has_nothrow_copy, depending on assign_p. Returns true iff all
9470 the copy {ctor,assign} fns are nothrow. */
9471
9472 static bool
9473 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9474 {
9475 tree fns = NULL_TREE;
9476
9477 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9478 fns = get_class_binding (type, assign_p ? assign_op_identifier
9479 : ctor_identifier);
9480
9481 bool saw_copy = false;
9482 for (ovl_iterator iter (fns); iter; ++iter)
9483 {
9484 tree fn = *iter;
9485
9486 if (copy_fn_p (fn) > 0)
9487 {
9488 saw_copy = true;
9489 maybe_instantiate_noexcept (fn);
9490 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9491 return false;
9492 }
9493 }
9494
9495 return saw_copy;
9496 }
9497
9498 /* Actually evaluates the trait. */
9499
9500 static bool
9501 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9502 {
9503 enum tree_code type_code1;
9504 tree t;
9505
9506 type_code1 = TREE_CODE (type1);
9507
9508 switch (kind)
9509 {
9510 case CPTK_HAS_NOTHROW_ASSIGN:
9511 type1 = strip_array_types (type1);
9512 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9513 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9514 || (CLASS_TYPE_P (type1)
9515 && classtype_has_nothrow_assign_or_copy_p (type1,
9516 true))));
9517
9518 case CPTK_HAS_TRIVIAL_ASSIGN:
9519 /* ??? The standard seems to be missing the "or array of such a class
9520 type" wording for this trait. */
9521 type1 = strip_array_types (type1);
9522 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9523 && (trivial_type_p (type1)
9524 || (CLASS_TYPE_P (type1)
9525 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9526
9527 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9528 type1 = strip_array_types (type1);
9529 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9530 || (CLASS_TYPE_P (type1)
9531 && (t = locate_ctor (type1))
9532 && (maybe_instantiate_noexcept (t),
9533 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9534
9535 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9536 type1 = strip_array_types (type1);
9537 return (trivial_type_p (type1)
9538 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9539
9540 case CPTK_HAS_NOTHROW_COPY:
9541 type1 = strip_array_types (type1);
9542 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9543 || (CLASS_TYPE_P (type1)
9544 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9545
9546 case CPTK_HAS_TRIVIAL_COPY:
9547 /* ??? The standard seems to be missing the "or array of such a class
9548 type" wording for this trait. */
9549 type1 = strip_array_types (type1);
9550 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9551 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9552
9553 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9554 type1 = strip_array_types (type1);
9555 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9556 || (CLASS_TYPE_P (type1)
9557 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9558
9559 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9560 return type_has_virtual_destructor (type1);
9561
9562 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9563 return type_has_unique_obj_representations (type1);
9564
9565 case CPTK_IS_ABSTRACT:
9566 return ABSTRACT_CLASS_TYPE_P (type1);
9567
9568 case CPTK_IS_AGGREGATE:
9569 return CP_AGGREGATE_TYPE_P (type1);
9570
9571 case CPTK_IS_BASE_OF:
9572 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9573 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9574 || DERIVED_FROM_P (type1, type2)));
9575
9576 case CPTK_IS_CLASS:
9577 return NON_UNION_CLASS_TYPE_P (type1);
9578
9579 case CPTK_IS_EMPTY:
9580 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9581
9582 case CPTK_IS_ENUM:
9583 return type_code1 == ENUMERAL_TYPE;
9584
9585 case CPTK_IS_FINAL:
9586 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9587
9588 case CPTK_IS_LITERAL_TYPE:
9589 return literal_type_p (type1);
9590
9591 case CPTK_IS_POD:
9592 return pod_type_p (type1);
9593
9594 case CPTK_IS_POLYMORPHIC:
9595 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9596
9597 case CPTK_IS_SAME_AS:
9598 return same_type_p (type1, type2);
9599
9600 case CPTK_IS_STD_LAYOUT:
9601 return std_layout_type_p (type1);
9602
9603 case CPTK_IS_TRIVIAL:
9604 return trivial_type_p (type1);
9605
9606 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9607 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9608
9609 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9610 return is_trivially_xible (INIT_EXPR, type1, type2);
9611
9612 case CPTK_IS_TRIVIALLY_COPYABLE:
9613 return trivially_copyable_p (type1);
9614
9615 case CPTK_IS_UNION:
9616 return type_code1 == UNION_TYPE;
9617
9618 case CPTK_IS_ASSIGNABLE:
9619 return is_xible (MODIFY_EXPR, type1, type2);
9620
9621 case CPTK_IS_CONSTRUCTIBLE:
9622 return is_xible (INIT_EXPR, type1, type2);
9623
9624 default:
9625 gcc_unreachable ();
9626 return false;
9627 }
9628 }
9629
9630 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9631 void, or a complete type, returns true, otherwise false. */
9632
9633 static bool
9634 check_trait_type (tree type)
9635 {
9636 if (type == NULL_TREE)
9637 return true;
9638
9639 if (TREE_CODE (type) == TREE_LIST)
9640 return (check_trait_type (TREE_VALUE (type))
9641 && check_trait_type (TREE_CHAIN (type)));
9642
9643 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9644 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9645 return true;
9646
9647 if (VOID_TYPE_P (type))
9648 return true;
9649
9650 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9651 }
9652
9653 /* Process a trait expression. */
9654
9655 tree
9656 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9657 {
9658 if (type1 == error_mark_node
9659 || type2 == error_mark_node)
9660 return error_mark_node;
9661
9662 if (processing_template_decl)
9663 {
9664 tree trait_expr = make_node (TRAIT_EXPR);
9665 TREE_TYPE (trait_expr) = boolean_type_node;
9666 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9667 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9668 TRAIT_EXPR_KIND (trait_expr) = kind;
9669 return trait_expr;
9670 }
9671
9672 switch (kind)
9673 {
9674 case CPTK_HAS_NOTHROW_ASSIGN:
9675 case CPTK_HAS_TRIVIAL_ASSIGN:
9676 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9677 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9678 case CPTK_HAS_NOTHROW_COPY:
9679 case CPTK_HAS_TRIVIAL_COPY:
9680 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9681 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9682 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9683 case CPTK_IS_ABSTRACT:
9684 case CPTK_IS_AGGREGATE:
9685 case CPTK_IS_EMPTY:
9686 case CPTK_IS_FINAL:
9687 case CPTK_IS_LITERAL_TYPE:
9688 case CPTK_IS_POD:
9689 case CPTK_IS_POLYMORPHIC:
9690 case CPTK_IS_STD_LAYOUT:
9691 case CPTK_IS_TRIVIAL:
9692 case CPTK_IS_TRIVIALLY_COPYABLE:
9693 if (!check_trait_type (type1))
9694 return error_mark_node;
9695 break;
9696
9697 case CPTK_IS_ASSIGNABLE:
9698 case CPTK_IS_CONSTRUCTIBLE:
9699 break;
9700
9701 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9702 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9703 if (!check_trait_type (type1)
9704 || !check_trait_type (type2))
9705 return error_mark_node;
9706 break;
9707
9708 case CPTK_IS_BASE_OF:
9709 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9710 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9711 && !complete_type_or_else (type2, NULL_TREE))
9712 /* We already issued an error. */
9713 return error_mark_node;
9714 break;
9715
9716 case CPTK_IS_CLASS:
9717 case CPTK_IS_ENUM:
9718 case CPTK_IS_UNION:
9719 case CPTK_IS_SAME_AS:
9720 break;
9721
9722 default:
9723 gcc_unreachable ();
9724 }
9725
9726 return (trait_expr_value (kind, type1, type2)
9727 ? boolean_true_node : boolean_false_node);
9728 }
9729
9730 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9731 which is ignored for C++. */
9732
9733 void
9734 set_float_const_decimal64 (void)
9735 {
9736 }
9737
9738 void
9739 clear_float_const_decimal64 (void)
9740 {
9741 }
9742
9743 bool
9744 float_const_decimal64_p (void)
9745 {
9746 return 0;
9747 }
9748
9749 \f
9750 /* Return true if T designates the implied `this' parameter. */
9751
9752 bool
9753 is_this_parameter (tree t)
9754 {
9755 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9756 return false;
9757 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9758 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9759 return true;
9760 }
9761
9762 /* Insert the deduced return type for an auto function. */
9763
9764 void
9765 apply_deduced_return_type (tree fco, tree return_type)
9766 {
9767 tree result;
9768
9769 if (return_type == error_mark_node)
9770 return;
9771
9772 if (DECL_CONV_FN_P (fco))
9773 DECL_NAME (fco) = make_conv_op_name (return_type);
9774
9775 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9776
9777 result = DECL_RESULT (fco);
9778 if (result == NULL_TREE)
9779 return;
9780 if (TREE_TYPE (result) == return_type)
9781 return;
9782
9783 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9784 && !complete_type_or_else (return_type, NULL_TREE))
9785 return;
9786
9787 /* We already have a DECL_RESULT from start_preparsed_function.
9788 Now we need to redo the work it and allocate_struct_function
9789 did to reflect the new type. */
9790 gcc_assert (current_function_decl == fco);
9791 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9792 TYPE_MAIN_VARIANT (return_type));
9793 DECL_ARTIFICIAL (result) = 1;
9794 DECL_IGNORED_P (result) = 1;
9795 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9796 result);
9797
9798 DECL_RESULT (fco) = result;
9799
9800 if (!processing_template_decl)
9801 {
9802 bool aggr = aggregate_value_p (result, fco);
9803 #ifdef PCC_STATIC_STRUCT_RETURN
9804 cfun->returns_pcc_struct = aggr;
9805 #endif
9806 cfun->returns_struct = aggr;
9807 }
9808
9809 }
9810
9811 /* DECL is a local variable or parameter from the surrounding scope of a
9812 lambda-expression. Returns the decltype for a use of the capture field
9813 for DECL even if it hasn't been captured yet. */
9814
9815 static tree
9816 capture_decltype (tree decl)
9817 {
9818 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9819 tree cap = lookup_name_real (DECL_NAME (decl), /*type*/0, /*nonclass*/1,
9820 /*block_p=*/true, /*ns*/0, LOOKUP_HIDDEN);
9821 tree type;
9822
9823 if (cap && is_capture_proxy (cap))
9824 type = TREE_TYPE (cap);
9825 else
9826 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9827 {
9828 case CPLD_NONE:
9829 error ("%qD is not captured", decl);
9830 return error_mark_node;
9831
9832 case CPLD_COPY:
9833 type = TREE_TYPE (decl);
9834 if (TYPE_REF_P (type)
9835 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9836 type = TREE_TYPE (type);
9837 break;
9838
9839 case CPLD_REFERENCE:
9840 type = TREE_TYPE (decl);
9841 if (!TYPE_REF_P (type))
9842 type = build_reference_type (TREE_TYPE (decl));
9843 break;
9844
9845 default:
9846 gcc_unreachable ();
9847 }
9848
9849 if (!TYPE_REF_P (type))
9850 {
9851 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9852 type = cp_build_qualified_type (type, (cp_type_quals (type)
9853 |TYPE_QUAL_CONST));
9854 type = build_reference_type (type);
9855 }
9856 return type;
9857 }
9858
9859 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9860 this is a right unary fold. Otherwise it is a left unary fold. */
9861
9862 static tree
9863 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9864 {
9865 // Build a pack expansion (assuming expr has pack type).
9866 if (!uses_parameter_packs (expr))
9867 {
9868 error_at (location_of (expr), "operand of fold expression has no "
9869 "unexpanded parameter packs");
9870 return error_mark_node;
9871 }
9872 tree pack = make_pack_expansion (expr);
9873
9874 // Build the fold expression.
9875 tree code = build_int_cstu (integer_type_node, abs (op));
9876 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9877 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9878 return fold;
9879 }
9880
9881 tree
9882 finish_left_unary_fold_expr (tree expr, int op)
9883 {
9884 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9885 }
9886
9887 tree
9888 finish_right_unary_fold_expr (tree expr, int op)
9889 {
9890 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9891 }
9892
9893 /* Build a binary fold expression over EXPR1 and EXPR2. The
9894 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9895 has an unexpanded parameter pack). */
9896
9897 tree
9898 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9899 {
9900 pack = make_pack_expansion (pack);
9901 tree code = build_int_cstu (integer_type_node, abs (op));
9902 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9903 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9904 return fold;
9905 }
9906
9907 tree
9908 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9909 {
9910 // Determine which expr has an unexpanded parameter pack and
9911 // set the pack and initial term.
9912 bool pack1 = uses_parameter_packs (expr1);
9913 bool pack2 = uses_parameter_packs (expr2);
9914 if (pack1 && !pack2)
9915 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9916 else if (pack2 && !pack1)
9917 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9918 else
9919 {
9920 if (pack1)
9921 error ("both arguments in binary fold have unexpanded parameter packs");
9922 else
9923 error ("no unexpanded parameter packs in binary fold");
9924 }
9925 return error_mark_node;
9926 }
9927
9928 /* Finish __builtin_launder (arg). */
9929
9930 tree
9931 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9932 {
9933 tree orig_arg = arg;
9934 if (!type_dependent_expression_p (arg))
9935 arg = decay_conversion (arg, complain);
9936 if (error_operand_p (arg))
9937 return error_mark_node;
9938 if (!type_dependent_expression_p (arg)
9939 && !TYPE_PTR_P (TREE_TYPE (arg)))
9940 {
9941 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9942 return error_mark_node;
9943 }
9944 if (processing_template_decl)
9945 arg = orig_arg;
9946 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9947 TREE_TYPE (arg), 1, arg);
9948 }
9949
9950 /* Finish __builtin_convertvector (arg, type). */
9951
9952 tree
9953 cp_build_vec_convert (tree arg, location_t loc, tree type,
9954 tsubst_flags_t complain)
9955 {
9956 if (error_operand_p (type))
9957 return error_mark_node;
9958 if (error_operand_p (arg))
9959 return error_mark_node;
9960
9961 tree ret = NULL_TREE;
9962 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
9963 ret = c_build_vec_convert (cp_expr_loc_or_loc (arg, input_location), arg,
9964 loc, type, (complain & tf_error) != 0);
9965
9966 if (!processing_template_decl)
9967 return ret;
9968
9969 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
9970 }
9971
9972 #include "gt-cp-semantics.h"