]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/semantics.c
PR c++/86969 - ICE with constexpr if and recursive generic lambdas.
[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 ((type_dependent_expression_p (expr)
2100 || !shared_member_p (expr))
2101 && current_class_ptr
2102 && DERIVED_FROM_P (qualifying_class,
2103 current_nonlambda_class_type ()))
2104 expr = (build_class_member_access_expr
2105 (maybe_dummy_object (qualifying_class, NULL),
2106 expr,
2107 BASELINK_ACCESS_BINFO (expr),
2108 /*preserve_reference=*/false,
2109 complain));
2110 else if (done)
2111 /* The expression is a qualified name whose address is not
2112 being taken. */
2113 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2114 complain);
2115 }
2116 else
2117 {
2118 /* In a template, return a SCOPE_REF for most qualified-ids
2119 so that we can check access at instantiation time. But if
2120 we're looking at a member of the current instantiation, we
2121 know we have access and building up the SCOPE_REF confuses
2122 non-type template argument handling. */
2123 if (processing_template_decl
2124 && (!currently_open_class (qualifying_class)
2125 || TREE_CODE (expr) == IDENTIFIER_NODE
2126 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2127 || TREE_CODE (expr) == BIT_NOT_EXPR))
2128 expr = build_qualified_name (TREE_TYPE (expr),
2129 qualifying_class, expr,
2130 template_p);
2131
2132 expr = convert_from_reference (expr);
2133 }
2134
2135 return expr;
2136 }
2137
2138 /* Begin a statement-expression. The value returned must be passed to
2139 finish_stmt_expr. */
2140
2141 tree
2142 begin_stmt_expr (void)
2143 {
2144 return push_stmt_list ();
2145 }
2146
2147 /* Process the final expression of a statement expression. EXPR can be
2148 NULL, if the final expression is empty. Return a STATEMENT_LIST
2149 containing all the statements in the statement-expression, or
2150 ERROR_MARK_NODE if there was an error. */
2151
2152 tree
2153 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2154 {
2155 if (error_operand_p (expr))
2156 {
2157 /* The type of the statement-expression is the type of the last
2158 expression. */
2159 TREE_TYPE (stmt_expr) = error_mark_node;
2160 return error_mark_node;
2161 }
2162
2163 /* If the last statement does not have "void" type, then the value
2164 of the last statement is the value of the entire expression. */
2165 if (expr)
2166 {
2167 tree type = TREE_TYPE (expr);
2168
2169 if (type && type_unknown_p (type))
2170 {
2171 error ("a statement expression is an insufficient context"
2172 " for overload resolution");
2173 TREE_TYPE (stmt_expr) = error_mark_node;
2174 return error_mark_node;
2175 }
2176 else if (processing_template_decl)
2177 {
2178 expr = build_stmt (input_location, EXPR_STMT, expr);
2179 expr = add_stmt (expr);
2180 /* Mark the last statement so that we can recognize it as such at
2181 template-instantiation time. */
2182 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2183 }
2184 else if (VOID_TYPE_P (type))
2185 {
2186 /* Just treat this like an ordinary statement. */
2187 expr = finish_expr_stmt (expr);
2188 }
2189 else
2190 {
2191 /* It actually has a value we need to deal with. First, force it
2192 to be an rvalue so that we won't need to build up a copy
2193 constructor call later when we try to assign it to something. */
2194 expr = force_rvalue (expr, tf_warning_or_error);
2195 if (error_operand_p (expr))
2196 return error_mark_node;
2197
2198 /* Update for array-to-pointer decay. */
2199 type = TREE_TYPE (expr);
2200
2201 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2202 normal statement, but don't convert to void or actually add
2203 the EXPR_STMT. */
2204 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2205 expr = maybe_cleanup_point_expr (expr);
2206 add_stmt (expr);
2207 }
2208
2209 /* The type of the statement-expression is the type of the last
2210 expression. */
2211 TREE_TYPE (stmt_expr) = type;
2212 }
2213
2214 return stmt_expr;
2215 }
2216
2217 /* Finish a statement-expression. EXPR should be the value returned
2218 by the previous begin_stmt_expr. Returns an expression
2219 representing the statement-expression. */
2220
2221 tree
2222 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2223 {
2224 tree type;
2225 tree result;
2226
2227 if (error_operand_p (stmt_expr))
2228 {
2229 pop_stmt_list (stmt_expr);
2230 return error_mark_node;
2231 }
2232
2233 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2234
2235 type = TREE_TYPE (stmt_expr);
2236 result = pop_stmt_list (stmt_expr);
2237 TREE_TYPE (result) = type;
2238
2239 if (processing_template_decl)
2240 {
2241 result = build_min (STMT_EXPR, type, result);
2242 TREE_SIDE_EFFECTS (result) = 1;
2243 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2244 }
2245 else if (CLASS_TYPE_P (type))
2246 {
2247 /* Wrap the statement-expression in a TARGET_EXPR so that the
2248 temporary object created by the final expression is destroyed at
2249 the end of the full-expression containing the
2250 statement-expression. */
2251 result = force_target_expr (type, result, tf_warning_or_error);
2252 }
2253
2254 return result;
2255 }
2256
2257 /* Returns the expression which provides the value of STMT_EXPR. */
2258
2259 tree
2260 stmt_expr_value_expr (tree stmt_expr)
2261 {
2262 tree t = STMT_EXPR_STMT (stmt_expr);
2263
2264 if (TREE_CODE (t) == BIND_EXPR)
2265 t = BIND_EXPR_BODY (t);
2266
2267 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2268 t = STATEMENT_LIST_TAIL (t)->stmt;
2269
2270 if (TREE_CODE (t) == EXPR_STMT)
2271 t = EXPR_STMT_EXPR (t);
2272
2273 return t;
2274 }
2275
2276 /* Return TRUE iff EXPR_STMT is an empty list of
2277 expression statements. */
2278
2279 bool
2280 empty_expr_stmt_p (tree expr_stmt)
2281 {
2282 tree body = NULL_TREE;
2283
2284 if (expr_stmt == void_node)
2285 return true;
2286
2287 if (expr_stmt)
2288 {
2289 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2290 body = EXPR_STMT_EXPR (expr_stmt);
2291 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2292 body = expr_stmt;
2293 }
2294
2295 if (body)
2296 {
2297 if (TREE_CODE (body) == STATEMENT_LIST)
2298 return tsi_end_p (tsi_start (body));
2299 else
2300 return empty_expr_stmt_p (body);
2301 }
2302 return false;
2303 }
2304
2305 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2306 the function (or functions) to call; ARGS are the arguments to the
2307 call. Returns the functions to be considered by overload resolution. */
2308
2309 cp_expr
2310 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2311 tsubst_flags_t complain)
2312 {
2313 tree identifier = NULL_TREE;
2314 tree functions = NULL_TREE;
2315 tree tmpl_args = NULL_TREE;
2316 bool template_id = false;
2317 location_t loc = fn_expr.get_location ();
2318 tree fn = fn_expr.get_value ();
2319
2320 STRIP_ANY_LOCATION_WRAPPER (fn);
2321
2322 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2323 {
2324 /* Use a separate flag to handle null args. */
2325 template_id = true;
2326 tmpl_args = TREE_OPERAND (fn, 1);
2327 fn = TREE_OPERAND (fn, 0);
2328 }
2329
2330 /* Find the name of the overloaded function. */
2331 if (identifier_p (fn))
2332 identifier = fn;
2333 else
2334 {
2335 functions = fn;
2336 identifier = OVL_NAME (functions);
2337 }
2338
2339 /* A call to a namespace-scope function using an unqualified name.
2340
2341 Do Koenig lookup -- unless any of the arguments are
2342 type-dependent. */
2343 if (!any_type_dependent_arguments_p (args)
2344 && !any_dependent_template_arguments_p (tmpl_args))
2345 {
2346 fn = lookup_arg_dependent (identifier, functions, args);
2347 if (!fn)
2348 {
2349 /* The unqualified name could not be resolved. */
2350 if (complain & tf_error)
2351 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2352 else
2353 fn = identifier;
2354 }
2355 }
2356
2357 if (fn && template_id && fn != error_mark_node)
2358 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2359
2360 return cp_expr (fn, loc);
2361 }
2362
2363 /* Generate an expression for `FN (ARGS)'. This may change the
2364 contents of ARGS.
2365
2366 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2367 as a virtual call, even if FN is virtual. (This flag is set when
2368 encountering an expression where the function name is explicitly
2369 qualified. For example a call to `X::f' never generates a virtual
2370 call.)
2371
2372 Returns code for the call. */
2373
2374 tree
2375 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2376 bool koenig_p, tsubst_flags_t complain)
2377 {
2378 tree result;
2379 tree orig_fn;
2380 vec<tree, va_gc> *orig_args = *args;
2381
2382 if (fn == error_mark_node)
2383 return error_mark_node;
2384
2385 gcc_assert (!TYPE_P (fn));
2386
2387 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2388 it so that we can tell this is a call to a known function. */
2389 fn = maybe_undo_parenthesized_ref (fn);
2390
2391 STRIP_ANY_LOCATION_WRAPPER (fn);
2392
2393 orig_fn = fn;
2394
2395 if (processing_template_decl)
2396 {
2397 /* If FN is a local extern declaration or set thereof, look them up
2398 again at instantiation time. */
2399 if (is_overloaded_fn (fn))
2400 {
2401 tree ifn = get_first_fn (fn);
2402 if (TREE_CODE (ifn) == FUNCTION_DECL
2403 && DECL_LOCAL_FUNCTION_P (ifn))
2404 orig_fn = DECL_NAME (ifn);
2405 }
2406
2407 /* If the call expression is dependent, build a CALL_EXPR node
2408 with no type; type_dependent_expression_p recognizes
2409 expressions with no type as being dependent. */
2410 if (type_dependent_expression_p (fn)
2411 || any_type_dependent_arguments_p (*args))
2412 {
2413 result = build_min_nt_call_vec (orig_fn, *args);
2414 SET_EXPR_LOCATION (result, cp_expr_loc_or_loc (fn, input_location));
2415 KOENIG_LOOKUP_P (result) = koenig_p;
2416 if (is_overloaded_fn (fn))
2417 fn = get_fns (fn);
2418
2419 if (cfun)
2420 {
2421 bool abnormal = true;
2422 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2423 {
2424 tree fndecl = *iter;
2425 if (TREE_CODE (fndecl) != FUNCTION_DECL
2426 || !TREE_THIS_VOLATILE (fndecl))
2427 abnormal = false;
2428 }
2429 /* FIXME: Stop warning about falling off end of non-void
2430 function. But this is wrong. Even if we only see
2431 no-return fns at this point, we could select a
2432 future-defined return fn during instantiation. Or
2433 vice-versa. */
2434 if (abnormal)
2435 current_function_returns_abnormally = 1;
2436 }
2437 return result;
2438 }
2439 orig_args = make_tree_vector_copy (*args);
2440 if (!BASELINK_P (fn)
2441 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2442 && TREE_TYPE (fn) != unknown_type_node)
2443 fn = build_non_dependent_expr (fn);
2444 make_args_non_dependent (*args);
2445 }
2446
2447 if (TREE_CODE (fn) == COMPONENT_REF)
2448 {
2449 tree member = TREE_OPERAND (fn, 1);
2450 if (BASELINK_P (member))
2451 {
2452 tree object = TREE_OPERAND (fn, 0);
2453 return build_new_method_call (object, member,
2454 args, NULL_TREE,
2455 (disallow_virtual
2456 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2457 : LOOKUP_NORMAL),
2458 /*fn_p=*/NULL,
2459 complain);
2460 }
2461 }
2462
2463 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2464 if (TREE_CODE (fn) == ADDR_EXPR
2465 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2466 fn = TREE_OPERAND (fn, 0);
2467
2468 if (is_overloaded_fn (fn))
2469 fn = baselink_for_fns (fn);
2470
2471 result = NULL_TREE;
2472 if (BASELINK_P (fn))
2473 {
2474 tree object;
2475
2476 /* A call to a member function. From [over.call.func]:
2477
2478 If the keyword this is in scope and refers to the class of
2479 that member function, or a derived class thereof, then the
2480 function call is transformed into a qualified function call
2481 using (*this) as the postfix-expression to the left of the
2482 . operator.... [Otherwise] a contrived object of type T
2483 becomes the implied object argument.
2484
2485 In this situation:
2486
2487 struct A { void f(); };
2488 struct B : public A {};
2489 struct C : public A { void g() { B::f(); }};
2490
2491 "the class of that member function" refers to `A'. But 11.2
2492 [class.access.base] says that we need to convert 'this' to B* as
2493 part of the access, so we pass 'B' to maybe_dummy_object. */
2494
2495 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2496 {
2497 /* A constructor call always uses a dummy object. (This constructor
2498 call which has the form A::A () is actually invalid and we are
2499 going to reject it later in build_new_method_call.) */
2500 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2501 }
2502 else
2503 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2504 NULL);
2505
2506 result = build_new_method_call (object, fn, args, NULL_TREE,
2507 (disallow_virtual
2508 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2509 : LOOKUP_NORMAL),
2510 /*fn_p=*/NULL,
2511 complain);
2512 }
2513 else if (is_overloaded_fn (fn))
2514 {
2515 /* If the function is an overloaded builtin, resolve it. */
2516 if (TREE_CODE (fn) == FUNCTION_DECL
2517 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2518 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2519 result = resolve_overloaded_builtin (input_location, fn, *args);
2520
2521 if (!result)
2522 {
2523 if (warn_sizeof_pointer_memaccess
2524 && (complain & tf_warning)
2525 && !vec_safe_is_empty (*args)
2526 && !processing_template_decl)
2527 {
2528 location_t sizeof_arg_loc[3];
2529 tree sizeof_arg[3];
2530 unsigned int i;
2531 for (i = 0; i < 3; i++)
2532 {
2533 tree t;
2534
2535 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2536 sizeof_arg[i] = NULL_TREE;
2537 if (i >= (*args)->length ())
2538 continue;
2539 t = (**args)[i];
2540 if (TREE_CODE (t) != SIZEOF_EXPR)
2541 continue;
2542 if (SIZEOF_EXPR_TYPE_P (t))
2543 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2544 else
2545 sizeof_arg[i] = TREE_OPERAND (t, 0);
2546 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2547 }
2548 sizeof_pointer_memaccess_warning
2549 (sizeof_arg_loc, fn, *args,
2550 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2551 }
2552
2553 if ((complain & tf_warning)
2554 && TREE_CODE (fn) == FUNCTION_DECL
2555 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2556 && vec_safe_length (*args) == 3
2557 && !any_type_dependent_arguments_p (*args))
2558 {
2559 tree arg0 = (*orig_args)[0];
2560 tree arg1 = (*orig_args)[1];
2561 tree arg2 = (*orig_args)[2];
2562 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2563 | (literal_integer_zerop (arg2) << 2));
2564 arg2 = instantiate_non_dependent_expr (arg2);
2565 warn_for_memset (input_location, arg0, arg2, literal_mask);
2566 }
2567
2568 /* A call to a namespace-scope function. */
2569 result = build_new_function_call (fn, args, complain);
2570 }
2571 }
2572 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2573 {
2574 if (!vec_safe_is_empty (*args))
2575 error ("arguments to destructor are not allowed");
2576 /* Mark the pseudo-destructor call as having side-effects so
2577 that we do not issue warnings about its use. */
2578 result = build1 (NOP_EXPR,
2579 void_type_node,
2580 TREE_OPERAND (fn, 0));
2581 TREE_SIDE_EFFECTS (result) = 1;
2582 }
2583 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2584 /* If the "function" is really an object of class type, it might
2585 have an overloaded `operator ()'. */
2586 result = build_op_call (fn, args, complain);
2587
2588 if (!result)
2589 /* A call where the function is unknown. */
2590 result = cp_build_function_call_vec (fn, args, complain);
2591
2592 if (processing_template_decl && result != error_mark_node)
2593 {
2594 if (INDIRECT_REF_P (result))
2595 result = TREE_OPERAND (result, 0);
2596 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2597 SET_EXPR_LOCATION (result, input_location);
2598 KOENIG_LOOKUP_P (result) = koenig_p;
2599 release_tree_vector (orig_args);
2600 result = convert_from_reference (result);
2601 }
2602
2603 return result;
2604 }
2605
2606 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2607 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2608 POSTDECREMENT_EXPR.) */
2609
2610 cp_expr
2611 finish_increment_expr (cp_expr expr, enum tree_code code)
2612 {
2613 /* input_location holds the location of the trailing operator token.
2614 Build a location of the form:
2615 expr++
2616 ~~~~^~
2617 with the caret at the operator token, ranging from the start
2618 of EXPR to the end of the operator token. */
2619 location_t combined_loc = make_location (input_location,
2620 expr.get_start (),
2621 get_finish (input_location));
2622 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2623 tf_warning_or_error);
2624 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2625 result.set_location (combined_loc);
2626 return result;
2627 }
2628
2629 /* Finish a use of `this'. Returns an expression for `this'. */
2630
2631 tree
2632 finish_this_expr (void)
2633 {
2634 tree result = NULL_TREE;
2635
2636 if (current_class_ptr)
2637 {
2638 tree type = TREE_TYPE (current_class_ref);
2639
2640 /* In a lambda expression, 'this' refers to the captured 'this'. */
2641 if (LAMBDA_TYPE_P (type))
2642 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2643 else
2644 result = current_class_ptr;
2645 }
2646
2647 if (result)
2648 /* The keyword 'this' is a prvalue expression. */
2649 return rvalue (result);
2650
2651 tree fn = current_nonlambda_function ();
2652 if (fn && DECL_STATIC_FUNCTION_P (fn))
2653 error ("%<this%> is unavailable for static member functions");
2654 else if (fn)
2655 error ("invalid use of %<this%> in non-member function");
2656 else
2657 error ("invalid use of %<this%> at top level");
2658 return error_mark_node;
2659 }
2660
2661 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2662 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2663 the TYPE for the type given. If SCOPE is non-NULL, the expression
2664 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2665
2666 tree
2667 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2668 location_t loc)
2669 {
2670 if (object == error_mark_node || destructor == error_mark_node)
2671 return error_mark_node;
2672
2673 gcc_assert (TYPE_P (destructor));
2674
2675 if (!processing_template_decl)
2676 {
2677 if (scope == error_mark_node)
2678 {
2679 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2680 return error_mark_node;
2681 }
2682 if (is_auto (destructor))
2683 destructor = TREE_TYPE (object);
2684 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2685 {
2686 error_at (loc,
2687 "qualified type %qT does not match destructor name ~%qT",
2688 scope, destructor);
2689 return error_mark_node;
2690 }
2691
2692
2693 /* [expr.pseudo] says both:
2694
2695 The type designated by the pseudo-destructor-name shall be
2696 the same as the object type.
2697
2698 and:
2699
2700 The cv-unqualified versions of the object type and of the
2701 type designated by the pseudo-destructor-name shall be the
2702 same type.
2703
2704 We implement the more generous second sentence, since that is
2705 what most other compilers do. */
2706 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2707 destructor))
2708 {
2709 error_at (loc, "%qE is not of type %qT", object, destructor);
2710 return error_mark_node;
2711 }
2712 }
2713
2714 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2715 scope, destructor);
2716 }
2717
2718 /* Finish an expression of the form CODE EXPR. */
2719
2720 cp_expr
2721 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2722 tsubst_flags_t complain)
2723 {
2724 /* Build a location of the form:
2725 ++expr
2726 ^~~~~~
2727 with the caret at the operator token, ranging from the start
2728 of the operator token to the end of EXPR. */
2729 location_t combined_loc = make_location (op_loc,
2730 op_loc, expr.get_finish ());
2731 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2732 /* TODO: build_x_unary_op doesn't always honor the location. */
2733 result.set_location (combined_loc);
2734
2735 if (result == error_mark_node)
2736 return result;
2737
2738 if (!(complain & tf_warning))
2739 return result;
2740
2741 tree result_ovl = result;
2742 tree expr_ovl = expr;
2743
2744 if (!processing_template_decl)
2745 expr_ovl = cp_fully_fold (expr_ovl);
2746
2747 if (!CONSTANT_CLASS_P (expr_ovl)
2748 || TREE_OVERFLOW_P (expr_ovl))
2749 return result;
2750
2751 if (!processing_template_decl)
2752 result_ovl = cp_fully_fold (result_ovl);
2753
2754 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2755 overflow_warning (combined_loc, result_ovl);
2756
2757 return result;
2758 }
2759
2760 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2761 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2762 is being cast. */
2763
2764 tree
2765 finish_compound_literal (tree type, tree compound_literal,
2766 tsubst_flags_t complain,
2767 fcl_t fcl_context)
2768 {
2769 if (type == error_mark_node)
2770 return error_mark_node;
2771
2772 if (TYPE_REF_P (type))
2773 {
2774 compound_literal
2775 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2776 complain, fcl_context);
2777 /* The prvalue is then used to direct-initialize the reference. */
2778 tree r = (perform_implicit_conversion_flags
2779 (type, compound_literal, complain, LOOKUP_NORMAL));
2780 return convert_from_reference (r);
2781 }
2782
2783 if (!TYPE_OBJ_P (type))
2784 {
2785 if (complain & tf_error)
2786 error ("compound literal of non-object type %qT", type);
2787 return error_mark_node;
2788 }
2789
2790 if (tree anode = type_uses_auto (type))
2791 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2792 {
2793 type = do_auto_deduction (type, compound_literal, anode, complain,
2794 adc_variable_type);
2795 if (type == error_mark_node)
2796 return error_mark_node;
2797 }
2798
2799 /* Used to hold a copy of the compound literal in a template. */
2800 tree orig_cl = NULL_TREE;
2801
2802 if (processing_template_decl)
2803 {
2804 const bool dependent_p
2805 = (instantiation_dependent_expression_p (compound_literal)
2806 || dependent_type_p (type));
2807 if (dependent_p)
2808 /* We're about to return, no need to copy. */
2809 orig_cl = compound_literal;
2810 else
2811 /* We're going to need a copy. */
2812 orig_cl = unshare_constructor (compound_literal);
2813 TREE_TYPE (orig_cl) = type;
2814 /* Mark the expression as a compound literal. */
2815 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
2816 /* And as instantiation-dependent. */
2817 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
2818 if (fcl_context == fcl_c99)
2819 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
2820 /* If the compound literal is dependent, we're done for now. */
2821 if (dependent_p)
2822 return orig_cl;
2823 /* Otherwise, do go on to e.g. check narrowing. */
2824 }
2825
2826 type = complete_type (type);
2827
2828 if (TYPE_NON_AGGREGATE_CLASS (type))
2829 {
2830 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2831 everywhere that deals with function arguments would be a pain, so
2832 just wrap it in a TREE_LIST. The parser set a flag so we know
2833 that it came from T{} rather than T({}). */
2834 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2835 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2836 return build_functional_cast (type, compound_literal, complain);
2837 }
2838
2839 if (TREE_CODE (type) == ARRAY_TYPE
2840 && check_array_initializer (NULL_TREE, type, compound_literal))
2841 return error_mark_node;
2842 compound_literal = reshape_init (type, compound_literal, complain);
2843 if (SCALAR_TYPE_P (type)
2844 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
2845 {
2846 tree t = instantiate_non_dependent_expr_sfinae (compound_literal,
2847 complain);
2848 if (!check_narrowing (type, t, complain))
2849 return error_mark_node;
2850 }
2851 if (TREE_CODE (type) == ARRAY_TYPE
2852 && TYPE_DOMAIN (type) == NULL_TREE)
2853 {
2854 cp_complete_array_type_or_error (&type, compound_literal,
2855 false, complain);
2856 if (type == error_mark_node)
2857 return error_mark_node;
2858 }
2859 compound_literal = digest_init_flags (type, compound_literal,
2860 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
2861 complain);
2862 /* If we're in a template, return the original compound literal. */
2863 if (orig_cl)
2864 {
2865 if (!VECTOR_TYPE_P (type))
2866 return get_target_expr_sfinae (orig_cl, complain);
2867 else
2868 return orig_cl;
2869 }
2870
2871 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2872 {
2873 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2874 if (fcl_context == fcl_c99)
2875 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2876 }
2877
2878 /* Put static/constant array temporaries in static variables. */
2879 /* FIXME all C99 compound literals should be variables rather than C++
2880 temporaries, unless they are used as an aggregate initializer. */
2881 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2882 && fcl_context == fcl_c99
2883 && TREE_CODE (type) == ARRAY_TYPE
2884 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2885 && initializer_constant_valid_p (compound_literal, type))
2886 {
2887 tree decl = create_temporary_var (type);
2888 DECL_INITIAL (decl) = compound_literal;
2889 TREE_STATIC (decl) = 1;
2890 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2891 {
2892 /* 5.19 says that a constant expression can include an
2893 lvalue-rvalue conversion applied to "a glvalue of literal type
2894 that refers to a non-volatile temporary object initialized
2895 with a constant expression". Rather than try to communicate
2896 that this VAR_DECL is a temporary, just mark it constexpr. */
2897 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2898 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2899 TREE_CONSTANT (decl) = true;
2900 }
2901 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2902 decl = pushdecl_top_level (decl);
2903 DECL_NAME (decl) = make_anon_name ();
2904 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2905 /* Make sure the destructor is callable. */
2906 tree clean = cxx_maybe_build_cleanup (decl, complain);
2907 if (clean == error_mark_node)
2908 return error_mark_node;
2909 return decl;
2910 }
2911
2912 /* Represent other compound literals with TARGET_EXPR so we produce
2913 an lvalue, but can elide copies. */
2914 if (!VECTOR_TYPE_P (type))
2915 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2916
2917 return compound_literal;
2918 }
2919
2920 /* Return the declaration for the function-name variable indicated by
2921 ID. */
2922
2923 tree
2924 finish_fname (tree id)
2925 {
2926 tree decl;
2927
2928 decl = fname_decl (input_location, C_RID_CODE (id), id);
2929 if (processing_template_decl && current_function_decl
2930 && decl != error_mark_node)
2931 decl = DECL_NAME (decl);
2932 return decl;
2933 }
2934
2935 /* Finish a translation unit. */
2936
2937 void
2938 finish_translation_unit (void)
2939 {
2940 /* In case there were missing closebraces,
2941 get us back to the global binding level. */
2942 pop_everything ();
2943 while (current_namespace != global_namespace)
2944 pop_namespace ();
2945
2946 /* Do file scope __FUNCTION__ et al. */
2947 finish_fname_decls ();
2948 }
2949
2950 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2951 Returns the parameter. */
2952
2953 tree
2954 finish_template_type_parm (tree aggr, tree identifier)
2955 {
2956 if (aggr != class_type_node)
2957 {
2958 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2959 aggr = class_type_node;
2960 }
2961
2962 return build_tree_list (aggr, identifier);
2963 }
2964
2965 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2966 Returns the parameter. */
2967
2968 tree
2969 finish_template_template_parm (tree aggr, tree identifier)
2970 {
2971 tree decl = build_decl (input_location,
2972 TYPE_DECL, identifier, NULL_TREE);
2973
2974 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2975 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2976 DECL_TEMPLATE_RESULT (tmpl) = decl;
2977 DECL_ARTIFICIAL (decl) = 1;
2978
2979 // Associate the constraints with the underlying declaration,
2980 // not the template.
2981 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2982 tree constr = build_constraints (reqs, NULL_TREE);
2983 set_constraints (decl, constr);
2984
2985 end_template_decl ();
2986
2987 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2988
2989 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2990 /*is_primary=*/true, /*is_partial=*/false,
2991 /*is_friend=*/0);
2992
2993 return finish_template_type_parm (aggr, tmpl);
2994 }
2995
2996 /* ARGUMENT is the default-argument value for a template template
2997 parameter. If ARGUMENT is invalid, issue error messages and return
2998 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2999
3000 tree
3001 check_template_template_default_arg (tree argument)
3002 {
3003 if (TREE_CODE (argument) != TEMPLATE_DECL
3004 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3005 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3006 {
3007 if (TREE_CODE (argument) == TYPE_DECL)
3008 error ("invalid use of type %qT as a default value for a template "
3009 "template-parameter", TREE_TYPE (argument));
3010 else
3011 error ("invalid default argument for a template template parameter");
3012 return error_mark_node;
3013 }
3014
3015 return argument;
3016 }
3017
3018 /* Begin a class definition, as indicated by T. */
3019
3020 tree
3021 begin_class_definition (tree t)
3022 {
3023 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3024 return error_mark_node;
3025
3026 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3027 are passed the same as decimal scalar types. */
3028 if (TREE_CODE (t) == RECORD_TYPE
3029 && !processing_template_decl)
3030 {
3031 tree ns = TYPE_CONTEXT (t);
3032 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3033 && DECL_CONTEXT (ns) == std_node
3034 && DECL_NAME (ns)
3035 && id_equal (DECL_NAME (ns), "decimal"))
3036 {
3037 const char *n = TYPE_NAME_STRING (t);
3038 if ((strcmp (n, "decimal32") == 0)
3039 || (strcmp (n, "decimal64") == 0)
3040 || (strcmp (n, "decimal128") == 0))
3041 TYPE_TRANSPARENT_AGGR (t) = 1;
3042 }
3043 }
3044
3045 /* A non-implicit typename comes from code like:
3046
3047 template <typename T> struct A {
3048 template <typename U> struct A<T>::B ...
3049
3050 This is erroneous. */
3051 else if (TREE_CODE (t) == TYPENAME_TYPE)
3052 {
3053 error ("invalid definition of qualified type %qT", t);
3054 t = error_mark_node;
3055 }
3056
3057 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3058 {
3059 t = make_class_type (RECORD_TYPE);
3060 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3061 }
3062
3063 if (TYPE_BEING_DEFINED (t))
3064 {
3065 t = make_class_type (TREE_CODE (t));
3066 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3067 }
3068 maybe_process_partial_specialization (t);
3069 pushclass (t);
3070 TYPE_BEING_DEFINED (t) = 1;
3071 class_binding_level->defining_class_p = 1;
3072
3073 if (flag_pack_struct)
3074 {
3075 tree v;
3076 TYPE_PACKED (t) = 1;
3077 /* Even though the type is being defined for the first time
3078 here, there might have been a forward declaration, so there
3079 might be cv-qualified variants of T. */
3080 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3081 TYPE_PACKED (v) = 1;
3082 }
3083 /* Reset the interface data, at the earliest possible
3084 moment, as it might have been set via a class foo;
3085 before. */
3086 if (! TYPE_UNNAMED_P (t))
3087 {
3088 struct c_fileinfo *finfo = \
3089 get_fileinfo (LOCATION_FILE (input_location));
3090 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3091 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3092 (t, finfo->interface_unknown);
3093 }
3094 reset_specialization();
3095
3096 /* Make a declaration for this class in its own scope. */
3097 build_self_reference ();
3098
3099 return t;
3100 }
3101
3102 /* Finish the member declaration given by DECL. */
3103
3104 void
3105 finish_member_declaration (tree decl)
3106 {
3107 if (decl == error_mark_node || decl == NULL_TREE)
3108 return;
3109
3110 if (decl == void_type_node)
3111 /* The COMPONENT was a friend, not a member, and so there's
3112 nothing for us to do. */
3113 return;
3114
3115 /* We should see only one DECL at a time. */
3116 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3117
3118 /* Don't add decls after definition. */
3119 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3120 /* We can add lambda types when late parsing default
3121 arguments. */
3122 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3123
3124 /* Set up access control for DECL. */
3125 TREE_PRIVATE (decl)
3126 = (current_access_specifier == access_private_node);
3127 TREE_PROTECTED (decl)
3128 = (current_access_specifier == access_protected_node);
3129 if (TREE_CODE (decl) == TEMPLATE_DECL)
3130 {
3131 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3132 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3133 }
3134
3135 /* Mark the DECL as a member of the current class, unless it's
3136 a member of an enumeration. */
3137 if (TREE_CODE (decl) != CONST_DECL)
3138 DECL_CONTEXT (decl) = current_class_type;
3139
3140 if (TREE_CODE (decl) == USING_DECL)
3141 /* For now, ignore class-scope USING_DECLS, so that debugging
3142 backends do not see them. */
3143 DECL_IGNORED_P (decl) = 1;
3144
3145 /* Check for bare parameter packs in the non-static data member
3146 declaration. */
3147 if (TREE_CODE (decl) == FIELD_DECL)
3148 {
3149 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3150 TREE_TYPE (decl) = error_mark_node;
3151 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3152 DECL_ATTRIBUTES (decl) = NULL_TREE;
3153 }
3154
3155 /* [dcl.link]
3156
3157 A C language linkage is ignored for the names of class members
3158 and the member function type of class member functions. */
3159 if (DECL_LANG_SPECIFIC (decl))
3160 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3161
3162 bool add = false;
3163
3164 /* Functions and non-functions are added differently. */
3165 if (DECL_DECLARES_FUNCTION_P (decl))
3166 add = add_method (current_class_type, decl, false);
3167 /* Enter the DECL into the scope of the class, if the class
3168 isn't a closure (whose fields are supposed to be unnamed). */
3169 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3170 || pushdecl_class_level (decl))
3171 add = true;
3172
3173 if (add)
3174 {
3175 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3176 go at the beginning. The reason is that
3177 legacy_nonfn_member_lookup searches the list in order, and we
3178 want a field name to override a type name so that the "struct
3179 stat hack" will work. In particular:
3180
3181 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3182
3183 is valid. */
3184
3185 if (TREE_CODE (decl) == TYPE_DECL)
3186 TYPE_FIELDS (current_class_type)
3187 = chainon (TYPE_FIELDS (current_class_type), decl);
3188 else
3189 {
3190 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3191 TYPE_FIELDS (current_class_type) = decl;
3192 }
3193
3194 maybe_add_class_template_decl_list (current_class_type, decl,
3195 /*friend_p=*/0);
3196 }
3197 }
3198
3199 /* Finish processing a complete template declaration. The PARMS are
3200 the template parameters. */
3201
3202 void
3203 finish_template_decl (tree parms)
3204 {
3205 if (parms)
3206 end_template_decl ();
3207 else
3208 end_specialization ();
3209 }
3210
3211 // Returns the template type of the class scope being entered. If we're
3212 // entering a constrained class scope. TYPE is the class template
3213 // scope being entered and we may need to match the intended type with
3214 // a constrained specialization. For example:
3215 //
3216 // template<Object T>
3217 // struct S { void f(); }; #1
3218 //
3219 // template<Object T>
3220 // void S<T>::f() { } #2
3221 //
3222 // We check, in #2, that S<T> refers precisely to the type declared by
3223 // #1 (i.e., that the constraints match). Note that the following should
3224 // be an error since there is no specialization of S<T> that is
3225 // unconstrained, but this is not diagnosed here.
3226 //
3227 // template<typename T>
3228 // void S<T>::f() { }
3229 //
3230 // We cannot diagnose this problem here since this function also matches
3231 // qualified template names that are not part of a definition. For example:
3232 //
3233 // template<Integral T, Floating_point U>
3234 // typename pair<T, U>::first_type void f(T, U);
3235 //
3236 // Here, it is unlikely that there is a partial specialization of
3237 // pair constrained for for Integral and Floating_point arguments.
3238 //
3239 // The general rule is: if a constrained specialization with matching
3240 // constraints is found return that type. Also note that if TYPE is not a
3241 // class-type (e.g. a typename type), then no fixup is needed.
3242
3243 static tree
3244 fixup_template_type (tree type)
3245 {
3246 // Find the template parameter list at the a depth appropriate to
3247 // the scope we're trying to enter.
3248 tree parms = current_template_parms;
3249 int depth = template_class_depth (type);
3250 for (int n = processing_template_decl; n > depth && parms; --n)
3251 parms = TREE_CHAIN (parms);
3252 if (!parms)
3253 return type;
3254 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3255 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3256
3257 // Search for a specialization whose type and constraints match.
3258 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3259 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3260 while (specs)
3261 {
3262 tree spec_constr = get_constraints (TREE_VALUE (specs));
3263
3264 // If the type and constraints match a specialization, then we
3265 // are entering that type.
3266 if (same_type_p (type, TREE_TYPE (specs))
3267 && equivalent_constraints (cur_constr, spec_constr))
3268 return TREE_TYPE (specs);
3269 specs = TREE_CHAIN (specs);
3270 }
3271
3272 // If no specialization matches, then must return the type
3273 // previously found.
3274 return type;
3275 }
3276
3277 /* Finish processing a template-id (which names a type) of the form
3278 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3279 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3280 the scope of template-id indicated. */
3281
3282 tree
3283 finish_template_type (tree name, tree args, int entering_scope)
3284 {
3285 tree type;
3286
3287 type = lookup_template_class (name, args,
3288 NULL_TREE, NULL_TREE, entering_scope,
3289 tf_warning_or_error | tf_user);
3290
3291 /* If we might be entering the scope of a partial specialization,
3292 find the one with the right constraints. */
3293 if (flag_concepts
3294 && entering_scope
3295 && CLASS_TYPE_P (type)
3296 && CLASSTYPE_TEMPLATE_INFO (type)
3297 && dependent_type_p (type)
3298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3299 type = fixup_template_type (type);
3300
3301 if (type == error_mark_node)
3302 return type;
3303 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3304 return TYPE_STUB_DECL (type);
3305 else
3306 return TYPE_NAME (type);
3307 }
3308
3309 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3310 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3311 BASE_CLASS, or NULL_TREE if an error occurred. The
3312 ACCESS_SPECIFIER is one of
3313 access_{default,public,protected_private}_node. For a virtual base
3314 we set TREE_TYPE. */
3315
3316 tree
3317 finish_base_specifier (tree base, tree access, bool virtual_p)
3318 {
3319 tree result;
3320
3321 if (base == error_mark_node)
3322 {
3323 error ("invalid base-class specification");
3324 result = NULL_TREE;
3325 }
3326 else if (! MAYBE_CLASS_TYPE_P (base))
3327 {
3328 error ("%qT is not a class type", base);
3329 result = NULL_TREE;
3330 }
3331 else
3332 {
3333 if (cp_type_quals (base) != 0)
3334 {
3335 /* DR 484: Can a base-specifier name a cv-qualified
3336 class type? */
3337 base = TYPE_MAIN_VARIANT (base);
3338 }
3339 result = build_tree_list (access, base);
3340 if (virtual_p)
3341 TREE_TYPE (result) = integer_type_node;
3342 }
3343
3344 return result;
3345 }
3346
3347 /* If FNS is a member function, a set of member functions, or a
3348 template-id referring to one or more member functions, return a
3349 BASELINK for FNS, incorporating the current access context.
3350 Otherwise, return FNS unchanged. */
3351
3352 tree
3353 baselink_for_fns (tree fns)
3354 {
3355 tree scope;
3356 tree cl;
3357
3358 if (BASELINK_P (fns)
3359 || error_operand_p (fns))
3360 return fns;
3361
3362 scope = ovl_scope (fns);
3363 if (!CLASS_TYPE_P (scope))
3364 return fns;
3365
3366 cl = currently_open_derived_class (scope);
3367 if (!cl)
3368 cl = scope;
3369 cl = TYPE_BINFO (cl);
3370 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3371 }
3372
3373 /* Returns true iff DECL is a variable from a function outside
3374 the current one. */
3375
3376 static bool
3377 outer_var_p (tree decl)
3378 {
3379 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3380 && DECL_FUNCTION_SCOPE_P (decl)
3381 /* Don't get confused by temporaries. */
3382 && DECL_NAME (decl)
3383 && (DECL_CONTEXT (decl) != current_function_decl
3384 || parsing_nsdmi ()));
3385 }
3386
3387 /* As above, but also checks that DECL is automatic. */
3388
3389 bool
3390 outer_automatic_var_p (tree decl)
3391 {
3392 return (outer_var_p (decl)
3393 && !TREE_STATIC (decl));
3394 }
3395
3396 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3397 rewrite it for lambda capture.
3398
3399 If ODR_USE is true, we're being called from mark_use, and we complain about
3400 use of constant variables. If ODR_USE is false, we're being called for the
3401 id-expression, and we do lambda capture. */
3402
3403 tree
3404 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3405 {
3406 if (cp_unevaluated_operand)
3407 /* It's not a use (3.2) if we're in an unevaluated context. */
3408 return decl;
3409 if (decl == error_mark_node)
3410 return decl;
3411
3412 tree context = DECL_CONTEXT (decl);
3413 tree containing_function = current_function_decl;
3414 tree lambda_stack = NULL_TREE;
3415 tree lambda_expr = NULL_TREE;
3416 tree initializer = convert_from_reference (decl);
3417
3418 /* Mark it as used now even if the use is ill-formed. */
3419 if (!mark_used (decl, complain))
3420 return error_mark_node;
3421
3422 if (parsing_nsdmi ())
3423 containing_function = NULL_TREE;
3424
3425 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3426 {
3427 /* Check whether we've already built a proxy. */
3428 tree var = decl;
3429 while (is_normal_capture_proxy (var))
3430 var = DECL_CAPTURED_VARIABLE (var);
3431 tree d = retrieve_local_specialization (var);
3432
3433 if (d && d != decl && is_capture_proxy (d))
3434 {
3435 if (DECL_CONTEXT (d) == containing_function)
3436 /* We already have an inner proxy. */
3437 return d;
3438 else
3439 /* We need to capture an outer proxy. */
3440 return process_outer_var_ref (d, complain, odr_use);
3441 }
3442 }
3443
3444 /* If we are in a lambda function, we can move out until we hit
3445 1. the context,
3446 2. a non-lambda function, or
3447 3. a non-default capturing lambda function. */
3448 while (context != containing_function
3449 /* containing_function can be null with invalid generic lambdas. */
3450 && containing_function
3451 && LAMBDA_FUNCTION_P (containing_function))
3452 {
3453 tree closure = DECL_CONTEXT (containing_function);
3454 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3455
3456 if (TYPE_CLASS_SCOPE_P (closure))
3457 /* A lambda in an NSDMI (c++/64496). */
3458 break;
3459
3460 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3461 == CPLD_NONE)
3462 break;
3463
3464 lambda_stack = tree_cons (NULL_TREE,
3465 lambda_expr,
3466 lambda_stack);
3467
3468 containing_function
3469 = decl_function_context (containing_function);
3470 }
3471
3472 /* In a lambda within a template, wait until instantiation time to implicitly
3473 capture a parameter pack. We want to wait because we don't know if we're
3474 capturing the whole pack or a single element, and it's OK to wait because
3475 find_parameter_packs_r walks into the lambda body. */
3476 if (context == containing_function
3477 && DECL_PACK_P (decl))
3478 return decl;
3479
3480 if (lambda_expr && VAR_P (decl)
3481 && DECL_ANON_UNION_VAR_P (decl))
3482 {
3483 if (complain & tf_error)
3484 error ("cannot capture member %qD of anonymous union", decl);
3485 return error_mark_node;
3486 }
3487 /* Do lambda capture when processing the id-expression, not when
3488 odr-using a variable. */
3489 if (!odr_use && context == containing_function)
3490 {
3491 decl = add_default_capture (lambda_stack,
3492 /*id=*/DECL_NAME (decl),
3493 initializer);
3494 }
3495 /* Only an odr-use of an outer automatic variable causes an
3496 error, and a constant variable can decay to a prvalue
3497 constant without odr-use. So don't complain yet. */
3498 else if (!odr_use && decl_constant_var_p (decl))
3499 return decl;
3500 else if (lambda_expr)
3501 {
3502 if (complain & tf_error)
3503 {
3504 error ("%qD is not captured", decl);
3505 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3506 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3507 == CPLD_NONE)
3508 inform (location_of (closure),
3509 "the lambda has no capture-default");
3510 else if (TYPE_CLASS_SCOPE_P (closure))
3511 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3512 "capture variables from the enclosing context",
3513 TYPE_CONTEXT (closure));
3514 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3515 }
3516 return error_mark_node;
3517 }
3518 else
3519 {
3520 if (complain & tf_error)
3521 {
3522 error (VAR_P (decl)
3523 ? G_("use of local variable with automatic storage from "
3524 "containing function")
3525 : G_("use of parameter from containing function"));
3526 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3527 }
3528 return error_mark_node;
3529 }
3530 return decl;
3531 }
3532
3533 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3534 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3535 if non-NULL, is the type or namespace used to explicitly qualify
3536 ID_EXPRESSION. DECL is the entity to which that name has been
3537 resolved.
3538
3539 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3540 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3541 be set to true if this expression isn't permitted in a
3542 constant-expression, but it is otherwise not set by this function.
3543 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3544 constant-expression, but a non-constant expression is also
3545 permissible.
3546
3547 DONE is true if this expression is a complete postfix-expression;
3548 it is false if this expression is followed by '->', '[', '(', etc.
3549 ADDRESS_P is true iff this expression is the operand of '&'.
3550 TEMPLATE_P is true iff the qualified-id was of the form
3551 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3552 appears as a template argument.
3553
3554 If an error occurs, and it is the kind of error that might cause
3555 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3556 is the caller's responsibility to issue the message. *ERROR_MSG
3557 will be a string with static storage duration, so the caller need
3558 not "free" it.
3559
3560 Return an expression for the entity, after issuing appropriate
3561 diagnostics. This function is also responsible for transforming a
3562 reference to a non-static member into a COMPONENT_REF that makes
3563 the use of "this" explicit.
3564
3565 Upon return, *IDK will be filled in appropriately. */
3566 static cp_expr
3567 finish_id_expression_1 (tree id_expression,
3568 tree decl,
3569 tree scope,
3570 cp_id_kind *idk,
3571 bool integral_constant_expression_p,
3572 bool allow_non_integral_constant_expression_p,
3573 bool *non_integral_constant_expression_p,
3574 bool template_p,
3575 bool done,
3576 bool address_p,
3577 bool template_arg_p,
3578 const char **error_msg,
3579 location_t location)
3580 {
3581 decl = strip_using_decl (decl);
3582
3583 /* Initialize the output parameters. */
3584 *idk = CP_ID_KIND_NONE;
3585 *error_msg = NULL;
3586
3587 if (id_expression == error_mark_node)
3588 return error_mark_node;
3589 /* If we have a template-id, then no further lookup is
3590 required. If the template-id was for a template-class, we
3591 will sometimes have a TYPE_DECL at this point. */
3592 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3593 || TREE_CODE (decl) == TYPE_DECL)
3594 ;
3595 /* Look up the name. */
3596 else
3597 {
3598 if (decl == error_mark_node)
3599 {
3600 /* Name lookup failed. */
3601 if (scope
3602 && (!TYPE_P (scope)
3603 || (!dependent_type_p (scope)
3604 && !(identifier_p (id_expression)
3605 && IDENTIFIER_CONV_OP_P (id_expression)
3606 && dependent_type_p (TREE_TYPE (id_expression))))))
3607 {
3608 /* If the qualifying type is non-dependent (and the name
3609 does not name a conversion operator to a dependent
3610 type), issue an error. */
3611 qualified_name_lookup_error (scope, id_expression, decl, location);
3612 return error_mark_node;
3613 }
3614 else if (!scope)
3615 {
3616 /* It may be resolved via Koenig lookup. */
3617 *idk = CP_ID_KIND_UNQUALIFIED;
3618 return id_expression;
3619 }
3620 else
3621 decl = id_expression;
3622 }
3623
3624 /* Remember that the name was used in the definition of
3625 the current class so that we can check later to see if
3626 the meaning would have been different after the class
3627 was entirely defined. */
3628 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3629 maybe_note_name_used_in_class (id_expression, decl);
3630
3631 /* A use in unevaluated operand might not be instantiated appropriately
3632 if tsubst_copy builds a dummy parm, or if we never instantiate a
3633 generic lambda, so mark it now. */
3634 if (processing_template_decl && cp_unevaluated_operand)
3635 mark_type_use (decl);
3636
3637 /* Disallow uses of local variables from containing functions, except
3638 within lambda-expressions. */
3639 if (outer_automatic_var_p (decl))
3640 {
3641 decl = process_outer_var_ref (decl, tf_warning_or_error);
3642 if (decl == error_mark_node)
3643 return error_mark_node;
3644 }
3645
3646 /* Also disallow uses of function parameters outside the function
3647 body, except inside an unevaluated context (i.e. decltype). */
3648 if (TREE_CODE (decl) == PARM_DECL
3649 && DECL_CONTEXT (decl) == NULL_TREE
3650 && !cp_unevaluated_operand)
3651 {
3652 *error_msg = G_("use of parameter outside function body");
3653 return error_mark_node;
3654 }
3655 }
3656
3657 /* If we didn't find anything, or what we found was a type,
3658 then this wasn't really an id-expression. */
3659 if (TREE_CODE (decl) == TEMPLATE_DECL
3660 && !DECL_FUNCTION_TEMPLATE_P (decl))
3661 {
3662 *error_msg = G_("missing template arguments");
3663 return error_mark_node;
3664 }
3665 else if (TREE_CODE (decl) == TYPE_DECL
3666 || TREE_CODE (decl) == NAMESPACE_DECL)
3667 {
3668 *error_msg = G_("expected primary-expression");
3669 return error_mark_node;
3670 }
3671
3672 /* If the name resolved to a template parameter, there is no
3673 need to look it up again later. */
3674 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3675 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3676 {
3677 tree r;
3678
3679 *idk = CP_ID_KIND_NONE;
3680 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3681 decl = TEMPLATE_PARM_DECL (decl);
3682 r = DECL_INITIAL (decl);
3683 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
3684 {
3685 /* If the entity is a template parameter object for a template
3686 parameter of type T, the type of the expression is const T. */
3687 tree ctype = TREE_TYPE (r);
3688 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
3689 | TYPE_QUAL_CONST));
3690 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
3691 }
3692 r = convert_from_reference (r);
3693 if (integral_constant_expression_p
3694 && !dependent_type_p (TREE_TYPE (decl))
3695 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3696 {
3697 if (!allow_non_integral_constant_expression_p)
3698 error ("template parameter %qD of type %qT is not allowed in "
3699 "an integral constant expression because it is not of "
3700 "integral or enumeration type", decl, TREE_TYPE (decl));
3701 *non_integral_constant_expression_p = true;
3702 }
3703 return r;
3704 }
3705 else
3706 {
3707 bool dependent_p = type_dependent_expression_p (decl);
3708
3709 /* If the declaration was explicitly qualified indicate
3710 that. The semantics of `A::f(3)' are different than
3711 `f(3)' if `f' is virtual. */
3712 *idk = (scope
3713 ? CP_ID_KIND_QUALIFIED
3714 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3715 ? CP_ID_KIND_TEMPLATE_ID
3716 : (dependent_p
3717 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3718 : CP_ID_KIND_UNQUALIFIED)));
3719
3720 if (dependent_p
3721 && DECL_P (decl)
3722 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3723 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3724 wrong, so just return the identifier. */
3725 return id_expression;
3726
3727 if (TREE_CODE (decl) == NAMESPACE_DECL)
3728 {
3729 error ("use of namespace %qD as expression", decl);
3730 return error_mark_node;
3731 }
3732 else if (DECL_CLASS_TEMPLATE_P (decl))
3733 {
3734 error ("use of class template %qT as expression", decl);
3735 return error_mark_node;
3736 }
3737 else if (TREE_CODE (decl) == TREE_LIST)
3738 {
3739 /* Ambiguous reference to base members. */
3740 error ("request for member %qD is ambiguous in "
3741 "multiple inheritance lattice", id_expression);
3742 print_candidates (decl);
3743 return error_mark_node;
3744 }
3745
3746 /* Mark variable-like entities as used. Functions are similarly
3747 marked either below or after overload resolution. */
3748 if ((VAR_P (decl)
3749 || TREE_CODE (decl) == PARM_DECL
3750 || TREE_CODE (decl) == CONST_DECL
3751 || TREE_CODE (decl) == RESULT_DECL)
3752 && !mark_used (decl))
3753 return error_mark_node;
3754
3755 /* Only certain kinds of names are allowed in constant
3756 expression. Template parameters have already
3757 been handled above. */
3758 if (! error_operand_p (decl)
3759 && !dependent_p
3760 && integral_constant_expression_p
3761 && ! decl_constant_var_p (decl)
3762 && TREE_CODE (decl) != CONST_DECL
3763 && ! builtin_valid_in_constant_expr_p (decl))
3764 {
3765 if (!allow_non_integral_constant_expression_p)
3766 {
3767 error ("%qD cannot appear in a constant-expression", decl);
3768 return error_mark_node;
3769 }
3770 *non_integral_constant_expression_p = true;
3771 }
3772
3773 tree wrap;
3774 if (VAR_P (decl)
3775 && !cp_unevaluated_operand
3776 && !processing_template_decl
3777 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3778 && CP_DECL_THREAD_LOCAL_P (decl)
3779 && (wrap = get_tls_wrapper_fn (decl)))
3780 {
3781 /* Replace an evaluated use of the thread_local variable with
3782 a call to its wrapper. */
3783 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3784 }
3785 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3786 && !dependent_p
3787 && variable_template_p (TREE_OPERAND (decl, 0)))
3788 {
3789 decl = finish_template_variable (decl);
3790 mark_used (decl);
3791 decl = convert_from_reference (decl);
3792 }
3793 else if (scope)
3794 {
3795 if (TREE_CODE (decl) == SCOPE_REF)
3796 {
3797 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3798 decl = TREE_OPERAND (decl, 1);
3799 }
3800
3801 decl = (adjust_result_of_qualified_name_lookup
3802 (decl, scope, current_nonlambda_class_type()));
3803
3804 if (TREE_CODE (decl) == FUNCTION_DECL)
3805 mark_used (decl);
3806
3807 if (TYPE_P (scope))
3808 decl = finish_qualified_id_expr (scope,
3809 decl,
3810 done,
3811 address_p,
3812 template_p,
3813 template_arg_p,
3814 tf_warning_or_error);
3815 else
3816 decl = convert_from_reference (decl);
3817 }
3818 else if (TREE_CODE (decl) == FIELD_DECL)
3819 {
3820 /* Since SCOPE is NULL here, this is an unqualified name.
3821 Access checking has been performed during name lookup
3822 already. Turn off checking to avoid duplicate errors. */
3823 push_deferring_access_checks (dk_no_check);
3824 decl = finish_non_static_data_member (decl, NULL_TREE,
3825 /*qualifying_scope=*/NULL_TREE);
3826 pop_deferring_access_checks ();
3827 }
3828 else if (is_overloaded_fn (decl))
3829 {
3830 tree first_fn = get_first_fn (decl);
3831
3832 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3833 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3834
3835 /* [basic.def.odr]: "A function whose name appears as a
3836 potentially-evaluated expression is odr-used if it is the unique
3837 lookup result".
3838
3839 But only mark it if it's a complete postfix-expression; in a call,
3840 ADL might select a different function, and we'll call mark_used in
3841 build_over_call. */
3842 if (done
3843 && !really_overloaded_fn (decl)
3844 && !mark_used (first_fn))
3845 return error_mark_node;
3846
3847 if (!template_arg_p
3848 && (TREE_CODE (first_fn) == USING_DECL
3849 || (TREE_CODE (first_fn) == FUNCTION_DECL
3850 && DECL_FUNCTION_MEMBER_P (first_fn)
3851 && !shared_member_p (decl))))
3852 {
3853 /* A set of member functions. */
3854 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3855 return finish_class_member_access_expr (decl, id_expression,
3856 /*template_p=*/false,
3857 tf_warning_or_error);
3858 }
3859
3860 decl = baselink_for_fns (decl);
3861 }
3862 else
3863 {
3864 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3865 && DECL_CLASS_SCOPE_P (decl))
3866 {
3867 tree context = context_for_name_lookup (decl);
3868 if (context != current_class_type)
3869 {
3870 tree path = currently_open_derived_class (context);
3871 perform_or_defer_access_check (TYPE_BINFO (path),
3872 decl, decl,
3873 tf_warning_or_error);
3874 }
3875 }
3876
3877 decl = convert_from_reference (decl);
3878 }
3879 }
3880
3881 return cp_expr (decl, location);
3882 }
3883
3884 /* As per finish_id_expression_1, but adding a wrapper node
3885 around the result if needed to express LOCATION. */
3886
3887 cp_expr
3888 finish_id_expression (tree id_expression,
3889 tree decl,
3890 tree scope,
3891 cp_id_kind *idk,
3892 bool integral_constant_expression_p,
3893 bool allow_non_integral_constant_expression_p,
3894 bool *non_integral_constant_expression_p,
3895 bool template_p,
3896 bool done,
3897 bool address_p,
3898 bool template_arg_p,
3899 const char **error_msg,
3900 location_t location)
3901 {
3902 cp_expr result
3903 = finish_id_expression_1 (id_expression, decl, scope, idk,
3904 integral_constant_expression_p,
3905 allow_non_integral_constant_expression_p,
3906 non_integral_constant_expression_p,
3907 template_p, done, address_p, template_arg_p,
3908 error_msg, location);
3909 return result.maybe_add_location_wrapper ();
3910 }
3911
3912 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3913 use as a type-specifier. */
3914
3915 tree
3916 finish_typeof (tree expr)
3917 {
3918 tree type;
3919
3920 if (type_dependent_expression_p (expr))
3921 {
3922 type = cxx_make_type (TYPEOF_TYPE);
3923 TYPEOF_TYPE_EXPR (type) = expr;
3924 SET_TYPE_STRUCTURAL_EQUALITY (type);
3925
3926 return type;
3927 }
3928
3929 expr = mark_type_use (expr);
3930
3931 type = unlowered_expr_type (expr);
3932
3933 if (!type || type == unknown_type_node)
3934 {
3935 error ("type of %qE is unknown", expr);
3936 return error_mark_node;
3937 }
3938
3939 return type;
3940 }
3941
3942 /* Implement the __underlying_type keyword: Return the underlying
3943 type of TYPE, suitable for use as a type-specifier. */
3944
3945 tree
3946 finish_underlying_type (tree type)
3947 {
3948 tree underlying_type;
3949
3950 if (processing_template_decl)
3951 {
3952 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3953 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3954 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3955
3956 return underlying_type;
3957 }
3958
3959 if (!complete_type_or_else (type, NULL_TREE))
3960 return error_mark_node;
3961
3962 if (TREE_CODE (type) != ENUMERAL_TYPE)
3963 {
3964 error ("%qT is not an enumeration type", type);
3965 return error_mark_node;
3966 }
3967
3968 underlying_type = ENUM_UNDERLYING_TYPE (type);
3969
3970 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3971 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3972 See finish_enum_value_list for details. */
3973 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3974 underlying_type
3975 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3976 TYPE_UNSIGNED (underlying_type));
3977
3978 return underlying_type;
3979 }
3980
3981 /* Implement the __direct_bases keyword: Return the direct base classes
3982 of type. */
3983
3984 tree
3985 calculate_direct_bases (tree type, tsubst_flags_t complain)
3986 {
3987 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3988 || !NON_UNION_CLASS_TYPE_P (type))
3989 return make_tree_vec (0);
3990
3991 vec<tree, va_gc> *vector = make_tree_vector ();
3992 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3993 tree binfo;
3994 unsigned i;
3995
3996 /* Virtual bases are initialized first */
3997 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3998 if (BINFO_VIRTUAL_P (binfo))
3999 vec_safe_push (vector, binfo);
4000
4001 /* Now non-virtuals */
4002 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4003 if (!BINFO_VIRTUAL_P (binfo))
4004 vec_safe_push (vector, binfo);
4005
4006 tree bases_vec = make_tree_vec (vector->length ());
4007
4008 for (i = 0; i < vector->length (); ++i)
4009 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4010
4011 release_tree_vector (vector);
4012 return bases_vec;
4013 }
4014
4015 /* Implement the __bases keyword: Return the base classes
4016 of type */
4017
4018 /* Find morally non-virtual base classes by walking binfo hierarchy */
4019 /* Virtual base classes are handled separately in finish_bases */
4020
4021 static tree
4022 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4023 {
4024 /* Don't walk bases of virtual bases */
4025 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4026 }
4027
4028 static tree
4029 dfs_calculate_bases_post (tree binfo, void *data_)
4030 {
4031 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4032 if (!BINFO_VIRTUAL_P (binfo))
4033 vec_safe_push (*data, BINFO_TYPE (binfo));
4034 return NULL_TREE;
4035 }
4036
4037 /* Calculates the morally non-virtual base classes of a class */
4038 static vec<tree, va_gc> *
4039 calculate_bases_helper (tree type)
4040 {
4041 vec<tree, va_gc> *vector = make_tree_vector ();
4042
4043 /* Now add non-virtual base classes in order of construction */
4044 if (TYPE_BINFO (type))
4045 dfs_walk_all (TYPE_BINFO (type),
4046 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4047 return vector;
4048 }
4049
4050 tree
4051 calculate_bases (tree type, tsubst_flags_t complain)
4052 {
4053 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4054 || !NON_UNION_CLASS_TYPE_P (type))
4055 return make_tree_vec (0);
4056
4057 vec<tree, va_gc> *vector = make_tree_vector ();
4058 tree bases_vec = NULL_TREE;
4059 unsigned i;
4060 vec<tree, va_gc> *vbases;
4061 vec<tree, va_gc> *nonvbases;
4062 tree binfo;
4063
4064 /* First go through virtual base classes */
4065 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4066 vec_safe_iterate (vbases, i, &binfo); i++)
4067 {
4068 vec<tree, va_gc> *vbase_bases
4069 = calculate_bases_helper (BINFO_TYPE (binfo));
4070 vec_safe_splice (vector, vbase_bases);
4071 release_tree_vector (vbase_bases);
4072 }
4073
4074 /* Now for the non-virtual bases */
4075 nonvbases = calculate_bases_helper (type);
4076 vec_safe_splice (vector, nonvbases);
4077 release_tree_vector (nonvbases);
4078
4079 /* Note that during error recovery vector->length can even be zero. */
4080 if (vector->length () > 1)
4081 {
4082 /* Last element is entire class, so don't copy */
4083 bases_vec = make_tree_vec (vector->length () - 1);
4084
4085 for (i = 0; i < vector->length () - 1; ++i)
4086 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4087 }
4088 else
4089 bases_vec = make_tree_vec (0);
4090
4091 release_tree_vector (vector);
4092 return bases_vec;
4093 }
4094
4095 tree
4096 finish_bases (tree type, bool direct)
4097 {
4098 tree bases = NULL_TREE;
4099
4100 if (!processing_template_decl)
4101 {
4102 /* Parameter packs can only be used in templates */
4103 error ("Parameter pack __bases only valid in template declaration");
4104 return error_mark_node;
4105 }
4106
4107 bases = cxx_make_type (BASES);
4108 BASES_TYPE (bases) = type;
4109 BASES_DIRECT (bases) = direct;
4110 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4111
4112 return bases;
4113 }
4114
4115 /* Perform C++-specific checks for __builtin_offsetof before calling
4116 fold_offsetof. */
4117
4118 tree
4119 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4120 {
4121 /* If we're processing a template, we can't finish the semantics yet.
4122 Otherwise we can fold the entire expression now. */
4123 if (processing_template_decl)
4124 {
4125 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4126 SET_EXPR_LOCATION (expr, loc);
4127 return expr;
4128 }
4129
4130 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4131 {
4132 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4133 TREE_OPERAND (expr, 2));
4134 return error_mark_node;
4135 }
4136 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4137 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4138 || TREE_TYPE (expr) == unknown_type_node)
4139 {
4140 while (TREE_CODE (expr) == COMPONENT_REF
4141 || TREE_CODE (expr) == COMPOUND_EXPR)
4142 expr = TREE_OPERAND (expr, 1);
4143
4144 if (DECL_P (expr))
4145 {
4146 error ("cannot apply %<offsetof%> to member function %qD", expr);
4147 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4148 }
4149 else
4150 error ("cannot apply %<offsetof%> to member function");
4151 return error_mark_node;
4152 }
4153 if (TREE_CODE (expr) == CONST_DECL)
4154 {
4155 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4156 return error_mark_node;
4157 }
4158 if (REFERENCE_REF_P (expr))
4159 expr = TREE_OPERAND (expr, 0);
4160 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4161 return error_mark_node;
4162 if (warn_invalid_offsetof
4163 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4164 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4165 && cp_unevaluated_operand == 0)
4166 warning_at (loc, OPT_Winvalid_offsetof, "offsetof within "
4167 "non-standard-layout type %qT is conditionally-supported",
4168 TREE_TYPE (TREE_TYPE (object_ptr)));
4169 return fold_offsetof (expr);
4170 }
4171
4172 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4173 function is broken out from the above for the benefit of the tree-ssa
4174 project. */
4175
4176 void
4177 simplify_aggr_init_expr (tree *tp)
4178 {
4179 tree aggr_init_expr = *tp;
4180
4181 /* Form an appropriate CALL_EXPR. */
4182 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4183 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4184 tree type = TREE_TYPE (slot);
4185
4186 tree call_expr;
4187 enum style_t { ctor, arg, pcc } style;
4188
4189 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4190 style = ctor;
4191 #ifdef PCC_STATIC_STRUCT_RETURN
4192 else if (1)
4193 style = pcc;
4194 #endif
4195 else
4196 {
4197 gcc_assert (TREE_ADDRESSABLE (type));
4198 style = arg;
4199 }
4200
4201 call_expr = build_call_array_loc (input_location,
4202 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4203 fn,
4204 aggr_init_expr_nargs (aggr_init_expr),
4205 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4206 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4207 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4208 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4209 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4210 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4211 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4212
4213 if (style == ctor)
4214 {
4215 /* Replace the first argument to the ctor with the address of the
4216 slot. */
4217 cxx_mark_addressable (slot);
4218 CALL_EXPR_ARG (call_expr, 0) =
4219 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4220 }
4221 else if (style == arg)
4222 {
4223 /* Just mark it addressable here, and leave the rest to
4224 expand_call{,_inline}. */
4225 cxx_mark_addressable (slot);
4226 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4227 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4228 }
4229 else if (style == pcc)
4230 {
4231 /* If we're using the non-reentrant PCC calling convention, then we
4232 need to copy the returned value out of the static buffer into the
4233 SLOT. */
4234 push_deferring_access_checks (dk_no_check);
4235 call_expr = build_aggr_init (slot, call_expr,
4236 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4237 tf_warning_or_error);
4238 pop_deferring_access_checks ();
4239 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4240 }
4241
4242 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4243 {
4244 tree init = build_zero_init (type, NULL_TREE,
4245 /*static_storage_p=*/false);
4246 init = build2 (INIT_EXPR, void_type_node, slot, init);
4247 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4248 init, call_expr);
4249 }
4250
4251 *tp = call_expr;
4252 }
4253
4254 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4255
4256 void
4257 emit_associated_thunks (tree fn)
4258 {
4259 /* When we use vcall offsets, we emit thunks with the virtual
4260 functions to which they thunk. The whole point of vcall offsets
4261 is so that you can know statically the entire set of thunks that
4262 will ever be needed for a given virtual function, thereby
4263 enabling you to output all the thunks with the function itself. */
4264 if (DECL_VIRTUAL_P (fn)
4265 /* Do not emit thunks for extern template instantiations. */
4266 && ! DECL_REALLY_EXTERN (fn))
4267 {
4268 tree thunk;
4269
4270 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4271 {
4272 if (!THUNK_ALIAS (thunk))
4273 {
4274 use_thunk (thunk, /*emit_p=*/1);
4275 if (DECL_RESULT_THUNK_P (thunk))
4276 {
4277 tree probe;
4278
4279 for (probe = DECL_THUNKS (thunk);
4280 probe; probe = DECL_CHAIN (probe))
4281 use_thunk (probe, /*emit_p=*/1);
4282 }
4283 }
4284 else
4285 gcc_assert (!DECL_THUNKS (thunk));
4286 }
4287 }
4288 }
4289
4290 /* Generate RTL for FN. */
4291
4292 bool
4293 expand_or_defer_fn_1 (tree fn)
4294 {
4295 /* When the parser calls us after finishing the body of a template
4296 function, we don't really want to expand the body. */
4297 if (processing_template_decl)
4298 {
4299 /* Normally, collection only occurs in rest_of_compilation. So,
4300 if we don't collect here, we never collect junk generated
4301 during the processing of templates until we hit a
4302 non-template function. It's not safe to do this inside a
4303 nested class, though, as the parser may have local state that
4304 is not a GC root. */
4305 if (!function_depth)
4306 ggc_collect ();
4307 return false;
4308 }
4309
4310 gcc_assert (DECL_SAVED_TREE (fn));
4311
4312 /* We make a decision about linkage for these functions at the end
4313 of the compilation. Until that point, we do not want the back
4314 end to output them -- but we do want it to see the bodies of
4315 these functions so that it can inline them as appropriate. */
4316 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4317 {
4318 if (DECL_INTERFACE_KNOWN (fn))
4319 /* We've already made a decision as to how this function will
4320 be handled. */;
4321 else if (!at_eof)
4322 tentative_decl_linkage (fn);
4323 else
4324 import_export_decl (fn);
4325
4326 /* If the user wants us to keep all inline functions, then mark
4327 this function as needed so that finish_file will make sure to
4328 output it later. Similarly, all dllexport'd functions must
4329 be emitted; there may be callers in other DLLs. */
4330 if (DECL_DECLARED_INLINE_P (fn)
4331 && !DECL_REALLY_EXTERN (fn)
4332 && (flag_keep_inline_functions
4333 || (flag_keep_inline_dllexport
4334 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4335 {
4336 mark_needed (fn);
4337 DECL_EXTERNAL (fn) = 0;
4338 }
4339 }
4340
4341 /* If this is a constructor or destructor body, we have to clone
4342 it. */
4343 if (maybe_clone_body (fn))
4344 {
4345 /* We don't want to process FN again, so pretend we've written
4346 it out, even though we haven't. */
4347 TREE_ASM_WRITTEN (fn) = 1;
4348 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4349 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4350 DECL_SAVED_TREE (fn) = NULL_TREE;
4351 return false;
4352 }
4353
4354 /* There's no reason to do any of the work here if we're only doing
4355 semantic analysis; this code just generates RTL. */
4356 if (flag_syntax_only)
4357 {
4358 /* Pretend that this function has been written out so that we don't try
4359 to expand it again. */
4360 TREE_ASM_WRITTEN (fn) = 1;
4361 return false;
4362 }
4363
4364 return true;
4365 }
4366
4367 void
4368 expand_or_defer_fn (tree fn)
4369 {
4370 if (expand_or_defer_fn_1 (fn))
4371 {
4372 function_depth++;
4373
4374 /* Expand or defer, at the whim of the compilation unit manager. */
4375 cgraph_node::finalize_function (fn, function_depth > 1);
4376 emit_associated_thunks (fn);
4377
4378 function_depth--;
4379 }
4380 }
4381
4382 struct nrv_data
4383 {
4384 nrv_data () : visited (37) {}
4385
4386 tree var;
4387 tree result;
4388 hash_table<nofree_ptr_hash <tree_node> > visited;
4389 };
4390
4391 /* Helper function for walk_tree, used by finalize_nrv below. */
4392
4393 static tree
4394 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4395 {
4396 struct nrv_data *dp = (struct nrv_data *)data;
4397 tree_node **slot;
4398
4399 /* No need to walk into types. There wouldn't be any need to walk into
4400 non-statements, except that we have to consider STMT_EXPRs. */
4401 if (TYPE_P (*tp))
4402 *walk_subtrees = 0;
4403 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4404 but differs from using NULL_TREE in that it indicates that we care
4405 about the value of the RESULT_DECL. */
4406 else if (TREE_CODE (*tp) == RETURN_EXPR)
4407 TREE_OPERAND (*tp, 0) = dp->result;
4408 /* Change all cleanups for the NRV to only run when an exception is
4409 thrown. */
4410 else if (TREE_CODE (*tp) == CLEANUP_STMT
4411 && CLEANUP_DECL (*tp) == dp->var)
4412 CLEANUP_EH_ONLY (*tp) = 1;
4413 /* Replace the DECL_EXPR for the NRV with an initialization of the
4414 RESULT_DECL, if needed. */
4415 else if (TREE_CODE (*tp) == DECL_EXPR
4416 && DECL_EXPR_DECL (*tp) == dp->var)
4417 {
4418 tree init;
4419 if (DECL_INITIAL (dp->var)
4420 && DECL_INITIAL (dp->var) != error_mark_node)
4421 init = build2 (INIT_EXPR, void_type_node, dp->result,
4422 DECL_INITIAL (dp->var));
4423 else
4424 init = build_empty_stmt (EXPR_LOCATION (*tp));
4425 DECL_INITIAL (dp->var) = NULL_TREE;
4426 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4427 *tp = init;
4428 }
4429 /* And replace all uses of the NRV with the RESULT_DECL. */
4430 else if (*tp == dp->var)
4431 *tp = dp->result;
4432
4433 /* Avoid walking into the same tree more than once. Unfortunately, we
4434 can't just use walk_tree_without duplicates because it would only call
4435 us for the first occurrence of dp->var in the function body. */
4436 slot = dp->visited.find_slot (*tp, INSERT);
4437 if (*slot)
4438 *walk_subtrees = 0;
4439 else
4440 *slot = *tp;
4441
4442 /* Keep iterating. */
4443 return NULL_TREE;
4444 }
4445
4446 /* Called from finish_function to implement the named return value
4447 optimization by overriding all the RETURN_EXPRs and pertinent
4448 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4449 RESULT_DECL for the function. */
4450
4451 void
4452 finalize_nrv (tree *tp, tree var, tree result)
4453 {
4454 struct nrv_data data;
4455
4456 /* Copy name from VAR to RESULT. */
4457 DECL_NAME (result) = DECL_NAME (var);
4458 /* Don't forget that we take its address. */
4459 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4460 /* Finally set DECL_VALUE_EXPR to avoid assigning
4461 a stack slot at -O0 for the original var and debug info
4462 uses RESULT location for VAR. */
4463 SET_DECL_VALUE_EXPR (var, result);
4464 DECL_HAS_VALUE_EXPR_P (var) = 1;
4465
4466 data.var = var;
4467 data.result = result;
4468 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4469 }
4470 \f
4471 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4472
4473 bool
4474 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4475 bool need_copy_ctor, bool need_copy_assignment,
4476 bool need_dtor)
4477 {
4478 int save_errorcount = errorcount;
4479 tree info, t;
4480
4481 /* Always allocate 3 elements for simplicity. These are the
4482 function decls for the ctor, dtor, and assignment op.
4483 This layout is known to the three lang hooks,
4484 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4485 and cxx_omp_clause_assign_op. */
4486 info = make_tree_vec (3);
4487 CP_OMP_CLAUSE_INFO (c) = info;
4488
4489 if (need_default_ctor || need_copy_ctor)
4490 {
4491 if (need_default_ctor)
4492 t = get_default_ctor (type);
4493 else
4494 t = get_copy_ctor (type, tf_warning_or_error);
4495
4496 if (t && !trivial_fn_p (t))
4497 TREE_VEC_ELT (info, 0) = t;
4498 }
4499
4500 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4501 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4502
4503 if (need_copy_assignment)
4504 {
4505 t = get_copy_assign (type);
4506
4507 if (t && !trivial_fn_p (t))
4508 TREE_VEC_ELT (info, 2) = t;
4509 }
4510
4511 return errorcount != save_errorcount;
4512 }
4513
4514 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4515 FIELD_DECL, otherwise return DECL itself. */
4516
4517 static tree
4518 omp_clause_decl_field (tree decl)
4519 {
4520 if (VAR_P (decl)
4521 && DECL_HAS_VALUE_EXPR_P (decl)
4522 && DECL_ARTIFICIAL (decl)
4523 && DECL_LANG_SPECIFIC (decl)
4524 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4525 {
4526 tree f = DECL_VALUE_EXPR (decl);
4527 if (INDIRECT_REF_P (f))
4528 f = TREE_OPERAND (f, 0);
4529 if (TREE_CODE (f) == COMPONENT_REF)
4530 {
4531 f = TREE_OPERAND (f, 1);
4532 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4533 return f;
4534 }
4535 }
4536 return NULL_TREE;
4537 }
4538
4539 /* Adjust DECL if needed for printing using %qE. */
4540
4541 static tree
4542 omp_clause_printable_decl (tree decl)
4543 {
4544 tree t = omp_clause_decl_field (decl);
4545 if (t)
4546 return t;
4547 return decl;
4548 }
4549
4550 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4551 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4552 privatization. */
4553
4554 static void
4555 omp_note_field_privatization (tree f, tree t)
4556 {
4557 if (!omp_private_member_map)
4558 omp_private_member_map = new hash_map<tree, tree>;
4559 tree &v = omp_private_member_map->get_or_insert (f);
4560 if (v == NULL_TREE)
4561 {
4562 v = t;
4563 omp_private_member_vec.safe_push (f);
4564 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4565 omp_private_member_vec.safe_push (integer_zero_node);
4566 }
4567 }
4568
4569 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4570 dummy VAR_DECL. */
4571
4572 tree
4573 omp_privatize_field (tree t, bool shared)
4574 {
4575 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4576 if (m == error_mark_node)
4577 return error_mark_node;
4578 if (!omp_private_member_map && !shared)
4579 omp_private_member_map = new hash_map<tree, tree>;
4580 if (TYPE_REF_P (TREE_TYPE (t)))
4581 {
4582 gcc_assert (INDIRECT_REF_P (m));
4583 m = TREE_OPERAND (m, 0);
4584 }
4585 tree vb = NULL_TREE;
4586 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4587 if (v == NULL_TREE)
4588 {
4589 v = create_temporary_var (TREE_TYPE (m));
4590 retrofit_lang_decl (v);
4591 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4592 SET_DECL_VALUE_EXPR (v, m);
4593 DECL_HAS_VALUE_EXPR_P (v) = 1;
4594 if (!shared)
4595 omp_private_member_vec.safe_push (t);
4596 }
4597 return v;
4598 }
4599
4600 /* Helper function for handle_omp_array_sections. Called recursively
4601 to handle multiple array-section-subscripts. C is the clause,
4602 T current expression (initially OMP_CLAUSE_DECL), which is either
4603 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4604 expression if specified, TREE_VALUE length expression if specified,
4605 TREE_CHAIN is what it has been specified after, or some decl.
4606 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4607 set to true if any of the array-section-subscript could have length
4608 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4609 first array-section-subscript which is known not to have length
4610 of one. Given say:
4611 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4612 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4613 all are or may have length of 1, array-section-subscript [:2] is the
4614 first one known not to have length 1. For array-section-subscript
4615 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4616 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4617 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4618 case though, as some lengths could be zero. */
4619
4620 static tree
4621 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4622 bool &maybe_zero_len, unsigned int &first_non_one,
4623 enum c_omp_region_type ort)
4624 {
4625 tree ret, low_bound, length, type;
4626 if (TREE_CODE (t) != TREE_LIST)
4627 {
4628 if (error_operand_p (t))
4629 return error_mark_node;
4630 if (REFERENCE_REF_P (t)
4631 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4632 t = TREE_OPERAND (t, 0);
4633 ret = t;
4634 if (TREE_CODE (t) == COMPONENT_REF
4635 && ort == C_ORT_OMP
4636 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4637 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4638 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4639 && !type_dependent_expression_p (t))
4640 {
4641 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4642 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4643 {
4644 error_at (OMP_CLAUSE_LOCATION (c),
4645 "bit-field %qE in %qs clause",
4646 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4647 return error_mark_node;
4648 }
4649 while (TREE_CODE (t) == COMPONENT_REF)
4650 {
4651 if (TREE_TYPE (TREE_OPERAND (t, 0))
4652 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4653 {
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "%qE is a member of a union", t);
4656 return error_mark_node;
4657 }
4658 t = TREE_OPERAND (t, 0);
4659 }
4660 if (REFERENCE_REF_P (t))
4661 t = TREE_OPERAND (t, 0);
4662 }
4663 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4664 {
4665 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4666 return NULL_TREE;
4667 if (DECL_P (t))
4668 error_at (OMP_CLAUSE_LOCATION (c),
4669 "%qD is not a variable in %qs clause", t,
4670 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4671 else
4672 error_at (OMP_CLAUSE_LOCATION (c),
4673 "%qE is not a variable in %qs clause", t,
4674 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4675 return error_mark_node;
4676 }
4677 else if (ort == C_ORT_OMP
4678 && TREE_CODE (t) == PARM_DECL
4679 && DECL_ARTIFICIAL (t)
4680 && DECL_NAME (t) == this_identifier)
4681 {
4682 error_at (OMP_CLAUSE_LOCATION (c),
4683 "%<this%> allowed in OpenMP only in %<declare simd%>"
4684 " clauses");
4685 return error_mark_node;
4686 }
4687 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4688 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4689 {
4690 error_at (OMP_CLAUSE_LOCATION (c),
4691 "%qD is threadprivate variable in %qs clause", t,
4692 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4693 return error_mark_node;
4694 }
4695 if (type_dependent_expression_p (ret))
4696 return NULL_TREE;
4697 ret = convert_from_reference (ret);
4698 return ret;
4699 }
4700
4701 if (ort == C_ORT_OMP
4702 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4703 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4704 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4705 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4706 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4707 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4708 maybe_zero_len, first_non_one, ort);
4709 if (ret == error_mark_node || ret == NULL_TREE)
4710 return ret;
4711
4712 type = TREE_TYPE (ret);
4713 low_bound = TREE_PURPOSE (t);
4714 length = TREE_VALUE (t);
4715 if ((low_bound && type_dependent_expression_p (low_bound))
4716 || (length && type_dependent_expression_p (length)))
4717 return NULL_TREE;
4718
4719 if (low_bound == error_mark_node || length == error_mark_node)
4720 return error_mark_node;
4721
4722 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4723 {
4724 error_at (OMP_CLAUSE_LOCATION (c),
4725 "low bound %qE of array section does not have integral type",
4726 low_bound);
4727 return error_mark_node;
4728 }
4729 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4730 {
4731 error_at (OMP_CLAUSE_LOCATION (c),
4732 "length %qE of array section does not have integral type",
4733 length);
4734 return error_mark_node;
4735 }
4736 if (low_bound)
4737 low_bound = mark_rvalue_use (low_bound);
4738 if (length)
4739 length = mark_rvalue_use (length);
4740 /* We need to reduce to real constant-values for checks below. */
4741 if (length)
4742 length = fold_simple (length);
4743 if (low_bound)
4744 low_bound = fold_simple (low_bound);
4745 if (low_bound
4746 && TREE_CODE (low_bound) == INTEGER_CST
4747 && TYPE_PRECISION (TREE_TYPE (low_bound))
4748 > TYPE_PRECISION (sizetype))
4749 low_bound = fold_convert (sizetype, low_bound);
4750 if (length
4751 && TREE_CODE (length) == INTEGER_CST
4752 && TYPE_PRECISION (TREE_TYPE (length))
4753 > TYPE_PRECISION (sizetype))
4754 length = fold_convert (sizetype, length);
4755 if (low_bound == NULL_TREE)
4756 low_bound = integer_zero_node;
4757
4758 if (length != NULL_TREE)
4759 {
4760 if (!integer_nonzerop (length))
4761 {
4762 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4763 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4764 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4765 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4766 {
4767 if (integer_zerop (length))
4768 {
4769 error_at (OMP_CLAUSE_LOCATION (c),
4770 "zero length array section in %qs clause",
4771 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4772 return error_mark_node;
4773 }
4774 }
4775 else
4776 maybe_zero_len = true;
4777 }
4778 if (first_non_one == types.length ()
4779 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4780 first_non_one++;
4781 }
4782 if (TREE_CODE (type) == ARRAY_TYPE)
4783 {
4784 if (length == NULL_TREE
4785 && (TYPE_DOMAIN (type) == NULL_TREE
4786 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4787 {
4788 error_at (OMP_CLAUSE_LOCATION (c),
4789 "for unknown bound array type length expression must "
4790 "be specified");
4791 return error_mark_node;
4792 }
4793 if (TREE_CODE (low_bound) == INTEGER_CST
4794 && tree_int_cst_sgn (low_bound) == -1)
4795 {
4796 error_at (OMP_CLAUSE_LOCATION (c),
4797 "negative low bound in array section in %qs clause",
4798 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4799 return error_mark_node;
4800 }
4801 if (length != NULL_TREE
4802 && TREE_CODE (length) == INTEGER_CST
4803 && tree_int_cst_sgn (length) == -1)
4804 {
4805 error_at (OMP_CLAUSE_LOCATION (c),
4806 "negative length in array section in %qs clause",
4807 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4808 return error_mark_node;
4809 }
4810 if (TYPE_DOMAIN (type)
4811 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4812 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4813 == INTEGER_CST)
4814 {
4815 tree size
4816 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4817 size = size_binop (PLUS_EXPR, size, size_one_node);
4818 if (TREE_CODE (low_bound) == INTEGER_CST)
4819 {
4820 if (tree_int_cst_lt (size, low_bound))
4821 {
4822 error_at (OMP_CLAUSE_LOCATION (c),
4823 "low bound %qE above array section size "
4824 "in %qs clause", low_bound,
4825 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4826 return error_mark_node;
4827 }
4828 if (tree_int_cst_equal (size, low_bound))
4829 {
4830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4831 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4832 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4833 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4834 {
4835 error_at (OMP_CLAUSE_LOCATION (c),
4836 "zero length array section in %qs clause",
4837 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4838 return error_mark_node;
4839 }
4840 maybe_zero_len = true;
4841 }
4842 else if (length == NULL_TREE
4843 && first_non_one == types.length ()
4844 && tree_int_cst_equal
4845 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4846 low_bound))
4847 first_non_one++;
4848 }
4849 else if (length == NULL_TREE)
4850 {
4851 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4852 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4853 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4854 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4855 maybe_zero_len = true;
4856 if (first_non_one == types.length ())
4857 first_non_one++;
4858 }
4859 if (length && TREE_CODE (length) == INTEGER_CST)
4860 {
4861 if (tree_int_cst_lt (size, length))
4862 {
4863 error_at (OMP_CLAUSE_LOCATION (c),
4864 "length %qE above array section size "
4865 "in %qs clause", length,
4866 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4867 return error_mark_node;
4868 }
4869 if (TREE_CODE (low_bound) == INTEGER_CST)
4870 {
4871 tree lbpluslen
4872 = size_binop (PLUS_EXPR,
4873 fold_convert (sizetype, low_bound),
4874 fold_convert (sizetype, length));
4875 if (TREE_CODE (lbpluslen) == INTEGER_CST
4876 && tree_int_cst_lt (size, lbpluslen))
4877 {
4878 error_at (OMP_CLAUSE_LOCATION (c),
4879 "high bound %qE above array section size "
4880 "in %qs clause", lbpluslen,
4881 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4882 return error_mark_node;
4883 }
4884 }
4885 }
4886 }
4887 else if (length == NULL_TREE)
4888 {
4889 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4890 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4891 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4892 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4893 maybe_zero_len = true;
4894 if (first_non_one == types.length ())
4895 first_non_one++;
4896 }
4897
4898 /* For [lb:] we will need to evaluate lb more than once. */
4899 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4900 {
4901 tree lb = cp_save_expr (low_bound);
4902 if (lb != low_bound)
4903 {
4904 TREE_PURPOSE (t) = lb;
4905 low_bound = lb;
4906 }
4907 }
4908 }
4909 else if (TYPE_PTR_P (type))
4910 {
4911 if (length == NULL_TREE)
4912 {
4913 error_at (OMP_CLAUSE_LOCATION (c),
4914 "for pointer type length expression must be specified");
4915 return error_mark_node;
4916 }
4917 if (length != NULL_TREE
4918 && TREE_CODE (length) == INTEGER_CST
4919 && tree_int_cst_sgn (length) == -1)
4920 {
4921 error_at (OMP_CLAUSE_LOCATION (c),
4922 "negative length in array section in %qs clause",
4923 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4924 return error_mark_node;
4925 }
4926 /* If there is a pointer type anywhere but in the very first
4927 array-section-subscript, the array section can't be contiguous. */
4928 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4929 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4930 {
4931 error_at (OMP_CLAUSE_LOCATION (c),
4932 "array section is not contiguous in %qs clause",
4933 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4934 return error_mark_node;
4935 }
4936 }
4937 else
4938 {
4939 error_at (OMP_CLAUSE_LOCATION (c),
4940 "%qE does not have pointer or array type", ret);
4941 return error_mark_node;
4942 }
4943 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4944 types.safe_push (TREE_TYPE (ret));
4945 /* We will need to evaluate lb more than once. */
4946 tree lb = cp_save_expr (low_bound);
4947 if (lb != low_bound)
4948 {
4949 TREE_PURPOSE (t) = lb;
4950 low_bound = lb;
4951 }
4952 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4953 return ret;
4954 }
4955
4956 /* Handle array sections for clause C. */
4957
4958 static bool
4959 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4960 {
4961 bool maybe_zero_len = false;
4962 unsigned int first_non_one = 0;
4963 auto_vec<tree, 10> types;
4964 tree *tp = &OMP_CLAUSE_DECL (c);
4965 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4966 && TREE_CODE (*tp) == TREE_LIST
4967 && TREE_PURPOSE (*tp)
4968 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
4969 tp = &TREE_VALUE (*tp);
4970 tree first = handle_omp_array_sections_1 (c, *tp, types,
4971 maybe_zero_len, first_non_one,
4972 ort);
4973 if (first == error_mark_node)
4974 return true;
4975 if (first == NULL_TREE)
4976 return false;
4977 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4978 {
4979 tree t = *tp;
4980 tree tem = NULL_TREE;
4981 if (processing_template_decl)
4982 return false;
4983 /* Need to evaluate side effects in the length expressions
4984 if any. */
4985 while (TREE_CODE (t) == TREE_LIST)
4986 {
4987 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4988 {
4989 if (tem == NULL_TREE)
4990 tem = TREE_VALUE (t);
4991 else
4992 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4993 TREE_VALUE (t), tem);
4994 }
4995 t = TREE_CHAIN (t);
4996 }
4997 if (tem)
4998 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4999 *tp = first;
5000 }
5001 else
5002 {
5003 unsigned int num = types.length (), i;
5004 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5005 tree condition = NULL_TREE;
5006
5007 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5008 maybe_zero_len = true;
5009 if (processing_template_decl && maybe_zero_len)
5010 return false;
5011
5012 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5013 t = TREE_CHAIN (t))
5014 {
5015 tree low_bound = TREE_PURPOSE (t);
5016 tree length = TREE_VALUE (t);
5017
5018 i--;
5019 if (low_bound
5020 && TREE_CODE (low_bound) == INTEGER_CST
5021 && TYPE_PRECISION (TREE_TYPE (low_bound))
5022 > TYPE_PRECISION (sizetype))
5023 low_bound = fold_convert (sizetype, low_bound);
5024 if (length
5025 && TREE_CODE (length) == INTEGER_CST
5026 && TYPE_PRECISION (TREE_TYPE (length))
5027 > TYPE_PRECISION (sizetype))
5028 length = fold_convert (sizetype, length);
5029 if (low_bound == NULL_TREE)
5030 low_bound = integer_zero_node;
5031 if (!maybe_zero_len && i > first_non_one)
5032 {
5033 if (integer_nonzerop (low_bound))
5034 goto do_warn_noncontiguous;
5035 if (length != NULL_TREE
5036 && TREE_CODE (length) == INTEGER_CST
5037 && TYPE_DOMAIN (types[i])
5038 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5039 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5040 == INTEGER_CST)
5041 {
5042 tree size;
5043 size = size_binop (PLUS_EXPR,
5044 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5045 size_one_node);
5046 if (!tree_int_cst_equal (length, size))
5047 {
5048 do_warn_noncontiguous:
5049 error_at (OMP_CLAUSE_LOCATION (c),
5050 "array section is not contiguous in %qs "
5051 "clause",
5052 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5053 return true;
5054 }
5055 }
5056 if (!processing_template_decl
5057 && length != NULL_TREE
5058 && TREE_SIDE_EFFECTS (length))
5059 {
5060 if (side_effects == NULL_TREE)
5061 side_effects = length;
5062 else
5063 side_effects = build2 (COMPOUND_EXPR,
5064 TREE_TYPE (side_effects),
5065 length, side_effects);
5066 }
5067 }
5068 else if (processing_template_decl)
5069 continue;
5070 else
5071 {
5072 tree l;
5073
5074 if (i > first_non_one
5075 && ((length && integer_nonzerop (length))
5076 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5077 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5078 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5079 continue;
5080 if (length)
5081 l = fold_convert (sizetype, length);
5082 else
5083 {
5084 l = size_binop (PLUS_EXPR,
5085 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5086 size_one_node);
5087 l = size_binop (MINUS_EXPR, l,
5088 fold_convert (sizetype, low_bound));
5089 }
5090 if (i > first_non_one)
5091 {
5092 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5093 size_zero_node);
5094 if (condition == NULL_TREE)
5095 condition = l;
5096 else
5097 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5098 l, condition);
5099 }
5100 else if (size == NULL_TREE)
5101 {
5102 size = size_in_bytes (TREE_TYPE (types[i]));
5103 tree eltype = TREE_TYPE (types[num - 1]);
5104 while (TREE_CODE (eltype) == ARRAY_TYPE)
5105 eltype = TREE_TYPE (eltype);
5106 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5107 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5108 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5109 size = size_binop (EXACT_DIV_EXPR, size,
5110 size_in_bytes (eltype));
5111 size = size_binop (MULT_EXPR, size, l);
5112 if (condition)
5113 size = fold_build3 (COND_EXPR, sizetype, condition,
5114 size, size_zero_node);
5115 }
5116 else
5117 size = size_binop (MULT_EXPR, size, l);
5118 }
5119 }
5120 if (!processing_template_decl)
5121 {
5122 if (side_effects)
5123 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5124 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5125 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5126 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5127 {
5128 size = size_binop (MINUS_EXPR, size, size_one_node);
5129 size = save_expr (size);
5130 tree index_type = build_index_type (size);
5131 tree eltype = TREE_TYPE (first);
5132 while (TREE_CODE (eltype) == ARRAY_TYPE)
5133 eltype = TREE_TYPE (eltype);
5134 tree type = build_array_type (eltype, index_type);
5135 tree ptype = build_pointer_type (eltype);
5136 if (TYPE_REF_P (TREE_TYPE (t))
5137 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5138 t = convert_from_reference (t);
5139 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5140 t = build_fold_addr_expr (t);
5141 tree t2 = build_fold_addr_expr (first);
5142 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5143 ptrdiff_type_node, t2);
5144 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5145 ptrdiff_type_node, t2,
5146 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5147 ptrdiff_type_node, t));
5148 if (tree_fits_shwi_p (t2))
5149 t = build2 (MEM_REF, type, t,
5150 build_int_cst (ptype, tree_to_shwi (t2)));
5151 else
5152 {
5153 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5154 sizetype, t2);
5155 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5156 TREE_TYPE (t), t, t2);
5157 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5158 }
5159 OMP_CLAUSE_DECL (c) = t;
5160 return false;
5161 }
5162 OMP_CLAUSE_DECL (c) = first;
5163 OMP_CLAUSE_SIZE (c) = size;
5164 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5165 || (TREE_CODE (t) == COMPONENT_REF
5166 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5167 return false;
5168 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5169 switch (OMP_CLAUSE_MAP_KIND (c))
5170 {
5171 case GOMP_MAP_ALLOC:
5172 case GOMP_MAP_TO:
5173 case GOMP_MAP_FROM:
5174 case GOMP_MAP_TOFROM:
5175 case GOMP_MAP_ALWAYS_TO:
5176 case GOMP_MAP_ALWAYS_FROM:
5177 case GOMP_MAP_ALWAYS_TOFROM:
5178 case GOMP_MAP_RELEASE:
5179 case GOMP_MAP_DELETE:
5180 case GOMP_MAP_FORCE_TO:
5181 case GOMP_MAP_FORCE_FROM:
5182 case GOMP_MAP_FORCE_TOFROM:
5183 case GOMP_MAP_FORCE_PRESENT:
5184 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5185 break;
5186 default:
5187 break;
5188 }
5189 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5190 OMP_CLAUSE_MAP);
5191 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5192 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5193 else if (TREE_CODE (t) == COMPONENT_REF)
5194 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5195 else if (REFERENCE_REF_P (t)
5196 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5197 {
5198 t = TREE_OPERAND (t, 0);
5199 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5200 }
5201 else
5202 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5203 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5204 && !cxx_mark_addressable (t))
5205 return false;
5206 OMP_CLAUSE_DECL (c2) = t;
5207 t = build_fold_addr_expr (first);
5208 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5209 ptrdiff_type_node, t);
5210 tree ptr = OMP_CLAUSE_DECL (c2);
5211 ptr = convert_from_reference (ptr);
5212 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5213 ptr = build_fold_addr_expr (ptr);
5214 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5215 ptrdiff_type_node, t,
5216 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5217 ptrdiff_type_node, ptr));
5218 OMP_CLAUSE_SIZE (c2) = t;
5219 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5220 OMP_CLAUSE_CHAIN (c) = c2;
5221 ptr = OMP_CLAUSE_DECL (c2);
5222 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5223 && TYPE_REF_P (TREE_TYPE (ptr))
5224 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5225 {
5226 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5227 OMP_CLAUSE_MAP);
5228 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5229 OMP_CLAUSE_DECL (c3) = ptr;
5230 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5231 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5232 else
5233 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5234 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5235 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5236 OMP_CLAUSE_CHAIN (c2) = c3;
5237 }
5238 }
5239 }
5240 return false;
5241 }
5242
5243 /* Return identifier to look up for omp declare reduction. */
5244
5245 tree
5246 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5247 {
5248 const char *p = NULL;
5249 const char *m = NULL;
5250 switch (reduction_code)
5251 {
5252 case PLUS_EXPR:
5253 case MULT_EXPR:
5254 case MINUS_EXPR:
5255 case BIT_AND_EXPR:
5256 case BIT_XOR_EXPR:
5257 case BIT_IOR_EXPR:
5258 case TRUTH_ANDIF_EXPR:
5259 case TRUTH_ORIF_EXPR:
5260 reduction_id = ovl_op_identifier (false, reduction_code);
5261 break;
5262 case MIN_EXPR:
5263 p = "min";
5264 break;
5265 case MAX_EXPR:
5266 p = "max";
5267 break;
5268 default:
5269 break;
5270 }
5271
5272 if (p == NULL)
5273 {
5274 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5275 return error_mark_node;
5276 p = IDENTIFIER_POINTER (reduction_id);
5277 }
5278
5279 if (type != NULL_TREE)
5280 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5281
5282 const char prefix[] = "omp declare reduction ";
5283 size_t lenp = sizeof (prefix);
5284 if (strncmp (p, prefix, lenp - 1) == 0)
5285 lenp = 1;
5286 size_t len = strlen (p);
5287 size_t lenm = m ? strlen (m) + 1 : 0;
5288 char *name = XALLOCAVEC (char, lenp + len + lenm);
5289 if (lenp > 1)
5290 memcpy (name, prefix, lenp - 1);
5291 memcpy (name + lenp - 1, p, len + 1);
5292 if (m)
5293 {
5294 name[lenp + len - 1] = '~';
5295 memcpy (name + lenp + len, m, lenm);
5296 }
5297 return get_identifier (name);
5298 }
5299
5300 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5301 FUNCTION_DECL or NULL_TREE if not found. */
5302
5303 static tree
5304 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5305 vec<tree> *ambiguousp)
5306 {
5307 tree orig_id = id;
5308 tree baselink = NULL_TREE;
5309 if (identifier_p (id))
5310 {
5311 cp_id_kind idk;
5312 bool nonint_cst_expression_p;
5313 const char *error_msg;
5314 id = omp_reduction_id (ERROR_MARK, id, type);
5315 tree decl = lookup_name (id);
5316 if (decl == NULL_TREE)
5317 decl = error_mark_node;
5318 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5319 &nonint_cst_expression_p, false, true, false,
5320 false, &error_msg, loc);
5321 if (idk == CP_ID_KIND_UNQUALIFIED
5322 && identifier_p (id))
5323 {
5324 vec<tree, va_gc> *args = NULL;
5325 vec_safe_push (args, build_reference_type (type));
5326 id = perform_koenig_lookup (id, args, tf_none);
5327 }
5328 }
5329 else if (TREE_CODE (id) == SCOPE_REF)
5330 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5331 omp_reduction_id (ERROR_MARK,
5332 TREE_OPERAND (id, 1),
5333 type),
5334 false, false);
5335 tree fns = id;
5336 id = NULL_TREE;
5337 if (fns && is_overloaded_fn (fns))
5338 {
5339 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5340 {
5341 tree fndecl = *iter;
5342 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5343 {
5344 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5345 if (same_type_p (TREE_TYPE (argtype), type))
5346 {
5347 id = fndecl;
5348 break;
5349 }
5350 }
5351 }
5352
5353 if (id && BASELINK_P (fns))
5354 {
5355 if (baselinkp)
5356 *baselinkp = fns;
5357 else
5358 baselink = fns;
5359 }
5360 }
5361
5362 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5363 {
5364 vec<tree> ambiguous = vNULL;
5365 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5366 unsigned int ix;
5367 if (ambiguousp == NULL)
5368 ambiguousp = &ambiguous;
5369 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5370 {
5371 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5372 baselinkp ? baselinkp : &baselink,
5373 ambiguousp);
5374 if (id == NULL_TREE)
5375 continue;
5376 if (!ambiguousp->is_empty ())
5377 ambiguousp->safe_push (id);
5378 else if (ret != NULL_TREE)
5379 {
5380 ambiguousp->safe_push (ret);
5381 ambiguousp->safe_push (id);
5382 ret = NULL_TREE;
5383 }
5384 else
5385 ret = id;
5386 }
5387 if (ambiguousp != &ambiguous)
5388 return ret;
5389 if (!ambiguous.is_empty ())
5390 {
5391 const char *str = _("candidates are:");
5392 unsigned int idx;
5393 tree udr;
5394 error_at (loc, "user defined reduction lookup is ambiguous");
5395 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5396 {
5397 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5398 if (idx == 0)
5399 str = get_spaces (str);
5400 }
5401 ambiguous.release ();
5402 ret = error_mark_node;
5403 baselink = NULL_TREE;
5404 }
5405 id = ret;
5406 }
5407 if (id && baselink)
5408 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5409 id, id, tf_warning_or_error);
5410 return id;
5411 }
5412
5413 /* Helper function for cp_parser_omp_declare_reduction_exprs
5414 and tsubst_omp_udr.
5415 Remove CLEANUP_STMT for data (omp_priv variable).
5416 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5417 DECL_EXPR. */
5418
5419 tree
5420 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5421 {
5422 if (TYPE_P (*tp))
5423 *walk_subtrees = 0;
5424 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5425 *tp = CLEANUP_BODY (*tp);
5426 else if (TREE_CODE (*tp) == DECL_EXPR)
5427 {
5428 tree decl = DECL_EXPR_DECL (*tp);
5429 if (!processing_template_decl
5430 && decl == (tree) data
5431 && DECL_INITIAL (decl)
5432 && DECL_INITIAL (decl) != error_mark_node)
5433 {
5434 tree list = NULL_TREE;
5435 append_to_statement_list_force (*tp, &list);
5436 tree init_expr = build2 (INIT_EXPR, void_type_node,
5437 decl, DECL_INITIAL (decl));
5438 DECL_INITIAL (decl) = NULL_TREE;
5439 append_to_statement_list_force (init_expr, &list);
5440 *tp = list;
5441 }
5442 }
5443 return NULL_TREE;
5444 }
5445
5446 /* Data passed from cp_check_omp_declare_reduction to
5447 cp_check_omp_declare_reduction_r. */
5448
5449 struct cp_check_omp_declare_reduction_data
5450 {
5451 location_t loc;
5452 tree stmts[7];
5453 bool combiner_p;
5454 };
5455
5456 /* Helper function for cp_check_omp_declare_reduction, called via
5457 cp_walk_tree. */
5458
5459 static tree
5460 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5461 {
5462 struct cp_check_omp_declare_reduction_data *udr_data
5463 = (struct cp_check_omp_declare_reduction_data *) data;
5464 if (SSA_VAR_P (*tp)
5465 && !DECL_ARTIFICIAL (*tp)
5466 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5467 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5468 {
5469 location_t loc = udr_data->loc;
5470 if (udr_data->combiner_p)
5471 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5472 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5473 *tp);
5474 else
5475 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5476 "to variable %qD which is not %<omp_priv%> nor "
5477 "%<omp_orig%>",
5478 *tp);
5479 return *tp;
5480 }
5481 return NULL_TREE;
5482 }
5483
5484 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5485
5486 void
5487 cp_check_omp_declare_reduction (tree udr)
5488 {
5489 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5490 gcc_assert (TYPE_REF_P (type));
5491 type = TREE_TYPE (type);
5492 int i;
5493 location_t loc = DECL_SOURCE_LOCATION (udr);
5494
5495 if (type == error_mark_node)
5496 return;
5497 if (ARITHMETIC_TYPE_P (type))
5498 {
5499 static enum tree_code predef_codes[]
5500 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5501 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5502 for (i = 0; i < 8; i++)
5503 {
5504 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5505 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5506 const char *n2 = IDENTIFIER_POINTER (id);
5507 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5508 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5509 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5510 break;
5511 }
5512
5513 if (i == 8
5514 && TREE_CODE (type) != COMPLEX_EXPR)
5515 {
5516 const char prefix_minmax[] = "omp declare reduction m";
5517 size_t prefix_size = sizeof (prefix_minmax) - 1;
5518 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5519 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5520 prefix_minmax, prefix_size) == 0
5521 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5522 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5523 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5524 i = 0;
5525 }
5526 if (i < 8)
5527 {
5528 error_at (loc, "predeclared arithmetic type %qT in "
5529 "%<#pragma omp declare reduction%>", type);
5530 return;
5531 }
5532 }
5533 else if (TREE_CODE (type) == FUNCTION_TYPE
5534 || TREE_CODE (type) == METHOD_TYPE
5535 || TREE_CODE (type) == ARRAY_TYPE)
5536 {
5537 error_at (loc, "function or array type %qT in "
5538 "%<#pragma omp declare reduction%>", type);
5539 return;
5540 }
5541 else if (TYPE_REF_P (type))
5542 {
5543 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5544 type);
5545 return;
5546 }
5547 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5548 {
5549 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5550 "%<#pragma omp declare reduction%>", type);
5551 return;
5552 }
5553
5554 tree body = DECL_SAVED_TREE (udr);
5555 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5556 return;
5557
5558 tree_stmt_iterator tsi;
5559 struct cp_check_omp_declare_reduction_data data;
5560 memset (data.stmts, 0, sizeof data.stmts);
5561 for (i = 0, tsi = tsi_start (body);
5562 i < 7 && !tsi_end_p (tsi);
5563 i++, tsi_next (&tsi))
5564 data.stmts[i] = tsi_stmt (tsi);
5565 data.loc = loc;
5566 gcc_assert (tsi_end_p (tsi));
5567 if (i >= 3)
5568 {
5569 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5570 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5571 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5572 return;
5573 data.combiner_p = true;
5574 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5575 &data, NULL))
5576 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5577 }
5578 if (i >= 6)
5579 {
5580 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5581 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5582 data.combiner_p = false;
5583 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5584 &data, NULL)
5585 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5586 cp_check_omp_declare_reduction_r, &data, NULL))
5587 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5588 if (i == 7)
5589 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5590 }
5591 }
5592
5593 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5594 an inline call. But, remap
5595 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5596 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5597
5598 static tree
5599 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5600 tree decl, tree placeholder)
5601 {
5602 copy_body_data id;
5603 hash_map<tree, tree> decl_map;
5604
5605 decl_map.put (omp_decl1, placeholder);
5606 decl_map.put (omp_decl2, decl);
5607 memset (&id, 0, sizeof (id));
5608 id.src_fn = DECL_CONTEXT (omp_decl1);
5609 id.dst_fn = current_function_decl;
5610 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5611 id.decl_map = &decl_map;
5612
5613 id.copy_decl = copy_decl_no_change;
5614 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5615 id.transform_new_cfg = true;
5616 id.transform_return_to_modify = false;
5617 id.transform_lang_insert_block = NULL;
5618 id.eh_lp_nr = 0;
5619 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5620 return stmt;
5621 }
5622
5623 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5624 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5625
5626 static tree
5627 find_omp_placeholder_r (tree *tp, int *, void *data)
5628 {
5629 if (*tp == (tree) data)
5630 return *tp;
5631 return NULL_TREE;
5632 }
5633
5634 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5635 Return true if there is some error and the clause should be removed. */
5636
5637 static bool
5638 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5639 {
5640 tree t = OMP_CLAUSE_DECL (c);
5641 bool predefined = false;
5642 if (TREE_CODE (t) == TREE_LIST)
5643 {
5644 gcc_assert (processing_template_decl);
5645 return false;
5646 }
5647 tree type = TREE_TYPE (t);
5648 if (TREE_CODE (t) == MEM_REF)
5649 type = TREE_TYPE (type);
5650 if (TYPE_REF_P (type))
5651 type = TREE_TYPE (type);
5652 if (TREE_CODE (type) == ARRAY_TYPE)
5653 {
5654 tree oatype = type;
5655 gcc_assert (TREE_CODE (t) != MEM_REF);
5656 while (TREE_CODE (type) == ARRAY_TYPE)
5657 type = TREE_TYPE (type);
5658 if (!processing_template_decl)
5659 {
5660 t = require_complete_type (t);
5661 if (t == error_mark_node)
5662 return true;
5663 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5664 TYPE_SIZE_UNIT (type));
5665 if (integer_zerop (size))
5666 {
5667 error_at (OMP_CLAUSE_LOCATION (c),
5668 "%qE in %<reduction%> clause is a zero size array",
5669 omp_clause_printable_decl (t));
5670 return true;
5671 }
5672 size = size_binop (MINUS_EXPR, size, size_one_node);
5673 size = save_expr (size);
5674 tree index_type = build_index_type (size);
5675 tree atype = build_array_type (type, index_type);
5676 tree ptype = build_pointer_type (type);
5677 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5678 t = build_fold_addr_expr (t);
5679 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5680 OMP_CLAUSE_DECL (c) = t;
5681 }
5682 }
5683 if (type == error_mark_node)
5684 return true;
5685 else if (ARITHMETIC_TYPE_P (type))
5686 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5687 {
5688 case PLUS_EXPR:
5689 case MULT_EXPR:
5690 case MINUS_EXPR:
5691 predefined = true;
5692 break;
5693 case MIN_EXPR:
5694 case MAX_EXPR:
5695 if (TREE_CODE (type) == COMPLEX_TYPE)
5696 break;
5697 predefined = true;
5698 break;
5699 case BIT_AND_EXPR:
5700 case BIT_IOR_EXPR:
5701 case BIT_XOR_EXPR:
5702 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5703 break;
5704 predefined = true;
5705 break;
5706 case TRUTH_ANDIF_EXPR:
5707 case TRUTH_ORIF_EXPR:
5708 if (FLOAT_TYPE_P (type))
5709 break;
5710 predefined = true;
5711 break;
5712 default:
5713 break;
5714 }
5715 else if (TYPE_READONLY (type))
5716 {
5717 error_at (OMP_CLAUSE_LOCATION (c),
5718 "%qE has const type for %<reduction%>",
5719 omp_clause_printable_decl (t));
5720 return true;
5721 }
5722 else if (!processing_template_decl)
5723 {
5724 t = require_complete_type (t);
5725 if (t == error_mark_node)
5726 return true;
5727 OMP_CLAUSE_DECL (c) = t;
5728 }
5729
5730 if (predefined)
5731 {
5732 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5733 return false;
5734 }
5735 else if (processing_template_decl)
5736 {
5737 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
5738 return true;
5739 return false;
5740 }
5741
5742 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5743
5744 type = TYPE_MAIN_VARIANT (type);
5745 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5746 if (id == NULL_TREE)
5747 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5748 NULL_TREE, NULL_TREE);
5749 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5750 if (id)
5751 {
5752 if (id == error_mark_node)
5753 return true;
5754 mark_used (id);
5755 tree body = DECL_SAVED_TREE (id);
5756 if (!body)
5757 return true;
5758 if (TREE_CODE (body) == STATEMENT_LIST)
5759 {
5760 tree_stmt_iterator tsi;
5761 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5762 int i;
5763 tree stmts[7];
5764 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5765 atype = TREE_TYPE (atype);
5766 bool need_static_cast = !same_type_p (type, atype);
5767 memset (stmts, 0, sizeof stmts);
5768 for (i = 0, tsi = tsi_start (body);
5769 i < 7 && !tsi_end_p (tsi);
5770 i++, tsi_next (&tsi))
5771 stmts[i] = tsi_stmt (tsi);
5772 gcc_assert (tsi_end_p (tsi));
5773
5774 if (i >= 3)
5775 {
5776 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5777 && TREE_CODE (stmts[1]) == DECL_EXPR);
5778 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5779 DECL_ARTIFICIAL (placeholder) = 1;
5780 DECL_IGNORED_P (placeholder) = 1;
5781 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5782 if (TREE_CODE (t) == MEM_REF)
5783 {
5784 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5785 type);
5786 DECL_ARTIFICIAL (decl_placeholder) = 1;
5787 DECL_IGNORED_P (decl_placeholder) = 1;
5788 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5789 }
5790 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5791 cxx_mark_addressable (placeholder);
5792 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5793 && (decl_placeholder
5794 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5795 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5796 : OMP_CLAUSE_DECL (c));
5797 tree omp_out = placeholder;
5798 tree omp_in = decl_placeholder ? decl_placeholder
5799 : convert_from_reference (OMP_CLAUSE_DECL (c));
5800 if (need_static_cast)
5801 {
5802 tree rtype = build_reference_type (atype);
5803 omp_out = build_static_cast (rtype, omp_out,
5804 tf_warning_or_error);
5805 omp_in = build_static_cast (rtype, omp_in,
5806 tf_warning_or_error);
5807 if (omp_out == error_mark_node || omp_in == error_mark_node)
5808 return true;
5809 omp_out = convert_from_reference (omp_out);
5810 omp_in = convert_from_reference (omp_in);
5811 }
5812 OMP_CLAUSE_REDUCTION_MERGE (c)
5813 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5814 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5815 }
5816 if (i >= 6)
5817 {
5818 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5819 && TREE_CODE (stmts[4]) == DECL_EXPR);
5820 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
5821 && (decl_placeholder
5822 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5823 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5824 : OMP_CLAUSE_DECL (c));
5825 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5826 cxx_mark_addressable (placeholder);
5827 tree omp_priv = decl_placeholder ? decl_placeholder
5828 : convert_from_reference (OMP_CLAUSE_DECL (c));
5829 tree omp_orig = placeholder;
5830 if (need_static_cast)
5831 {
5832 if (i == 7)
5833 {
5834 error_at (OMP_CLAUSE_LOCATION (c),
5835 "user defined reduction with constructor "
5836 "initializer for base class %qT", atype);
5837 return true;
5838 }
5839 tree rtype = build_reference_type (atype);
5840 omp_priv = build_static_cast (rtype, omp_priv,
5841 tf_warning_or_error);
5842 omp_orig = build_static_cast (rtype, omp_orig,
5843 tf_warning_or_error);
5844 if (omp_priv == error_mark_node
5845 || omp_orig == error_mark_node)
5846 return true;
5847 omp_priv = convert_from_reference (omp_priv);
5848 omp_orig = convert_from_reference (omp_orig);
5849 }
5850 if (i == 6)
5851 *need_default_ctor = true;
5852 OMP_CLAUSE_REDUCTION_INIT (c)
5853 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5854 DECL_EXPR_DECL (stmts[3]),
5855 omp_priv, omp_orig);
5856 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5857 find_omp_placeholder_r, placeholder, NULL))
5858 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5859 }
5860 else if (i >= 3)
5861 {
5862 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5863 *need_default_ctor = true;
5864 else
5865 {
5866 tree init;
5867 tree v = decl_placeholder ? decl_placeholder
5868 : convert_from_reference (t);
5869 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5870 init = build_constructor (TREE_TYPE (v), NULL);
5871 else
5872 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5873 OMP_CLAUSE_REDUCTION_INIT (c)
5874 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5875 }
5876 }
5877 }
5878 }
5879 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5880 *need_dtor = true;
5881 else
5882 {
5883 error_at (OMP_CLAUSE_LOCATION (c),
5884 "user defined reduction not found for %qE",
5885 omp_clause_printable_decl (t));
5886 return true;
5887 }
5888 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5889 gcc_assert (TYPE_SIZE_UNIT (type)
5890 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5891 return false;
5892 }
5893
5894 /* Called from finish_struct_1. linear(this) or linear(this:step)
5895 clauses might not be finalized yet because the class has been incomplete
5896 when parsing #pragma omp declare simd methods. Fix those up now. */
5897
5898 void
5899 finish_omp_declare_simd_methods (tree t)
5900 {
5901 if (processing_template_decl)
5902 return;
5903
5904 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5905 {
5906 if (TREE_CODE (x) == USING_DECL
5907 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
5908 continue;
5909 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5910 if (!ods || !TREE_VALUE (ods))
5911 continue;
5912 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5913 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5914 && integer_zerop (OMP_CLAUSE_DECL (c))
5915 && OMP_CLAUSE_LINEAR_STEP (c)
5916 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
5917 {
5918 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5919 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5920 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5921 sizetype, s, TYPE_SIZE_UNIT (t));
5922 OMP_CLAUSE_LINEAR_STEP (c) = s;
5923 }
5924 }
5925 }
5926
5927 /* Adjust sink depend clause to take into account pointer offsets.
5928
5929 Return TRUE if there was a problem processing the offset, and the
5930 whole clause should be removed. */
5931
5932 static bool
5933 cp_finish_omp_clause_depend_sink (tree sink_clause)
5934 {
5935 tree t = OMP_CLAUSE_DECL (sink_clause);
5936 gcc_assert (TREE_CODE (t) == TREE_LIST);
5937
5938 /* Make sure we don't adjust things twice for templates. */
5939 if (processing_template_decl)
5940 return false;
5941
5942 for (; t; t = TREE_CHAIN (t))
5943 {
5944 tree decl = TREE_VALUE (t);
5945 if (TYPE_PTR_P (TREE_TYPE (decl)))
5946 {
5947 tree offset = TREE_PURPOSE (t);
5948 bool neg = wi::neg_p (wi::to_wide (offset));
5949 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5950 decl = mark_rvalue_use (decl);
5951 decl = convert_from_reference (decl);
5952 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5953 neg ? MINUS_EXPR : PLUS_EXPR,
5954 decl, offset);
5955 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5956 MINUS_EXPR, sizetype,
5957 fold_convert (sizetype, t2),
5958 fold_convert (sizetype, decl));
5959 if (t2 == error_mark_node)
5960 return true;
5961 TREE_PURPOSE (t) = t2;
5962 }
5963 }
5964 return false;
5965 }
5966
5967 /* Finish OpenMP iterators ITER. Return true if they are errorneous
5968 and clauses containing them should be removed. */
5969
5970 static bool
5971 cp_omp_finish_iterators (tree iter)
5972 {
5973 bool ret = false;
5974 for (tree it = iter; it; it = TREE_CHAIN (it))
5975 {
5976 tree var = TREE_VEC_ELT (it, 0);
5977 tree begin = TREE_VEC_ELT (it, 1);
5978 tree end = TREE_VEC_ELT (it, 2);
5979 tree step = TREE_VEC_ELT (it, 3);
5980 tree orig_step;
5981 tree type = TREE_TYPE (var);
5982 location_t loc = DECL_SOURCE_LOCATION (var);
5983 if (type == error_mark_node)
5984 {
5985 ret = true;
5986 continue;
5987 }
5988 if (type_dependent_expression_p (var))
5989 continue;
5990 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
5991 {
5992 error_at (loc, "iterator %qD has neither integral nor pointer type",
5993 var);
5994 ret = true;
5995 continue;
5996 }
5997 else if (TYPE_READONLY (type))
5998 {
5999 error_at (loc, "iterator %qD has const qualified type", var);
6000 ret = true;
6001 continue;
6002 }
6003 if (type_dependent_expression_p (begin)
6004 || type_dependent_expression_p (end)
6005 || type_dependent_expression_p (step))
6006 continue;
6007 else if (error_operand_p (step))
6008 {
6009 ret = true;
6010 continue;
6011 }
6012 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6013 {
6014 error_at (EXPR_LOC_OR_LOC (step, loc),
6015 "iterator step with non-integral type");
6016 ret = true;
6017 continue;
6018 }
6019
6020 begin = mark_rvalue_use (begin);
6021 end = mark_rvalue_use (end);
6022 step = mark_rvalue_use (step);
6023 begin = cp_build_c_cast (type, begin, tf_warning_or_error);
6024 end = cp_build_c_cast (type, end, tf_warning_or_error);
6025 orig_step = step;
6026 if (!processing_template_decl)
6027 step = orig_step = save_expr (step);
6028 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6029 step = cp_build_c_cast (stype, step, tf_warning_or_error);
6030 if (POINTER_TYPE_P (type) && !processing_template_decl)
6031 {
6032 begin = save_expr (begin);
6033 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6034 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6035 fold_convert (sizetype, step),
6036 fold_convert (sizetype, begin));
6037 step = fold_convert (ssizetype, step);
6038 }
6039 if (!processing_template_decl)
6040 {
6041 begin = maybe_constant_value (begin);
6042 end = maybe_constant_value (end);
6043 step = maybe_constant_value (step);
6044 orig_step = maybe_constant_value (orig_step);
6045 }
6046 if (integer_zerop (step))
6047 {
6048 error_at (loc, "iterator %qD has zero step", var);
6049 ret = true;
6050 continue;
6051 }
6052
6053 if (begin == error_mark_node
6054 || end == error_mark_node
6055 || step == error_mark_node
6056 || orig_step == error_mark_node)
6057 {
6058 ret = true;
6059 continue;
6060 }
6061
6062 if (!processing_template_decl)
6063 {
6064 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6065 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6066 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6067 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6068 orig_step);
6069 }
6070 hash_set<tree> pset;
6071 tree it2;
6072 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6073 {
6074 tree var2 = TREE_VEC_ELT (it2, 0);
6075 tree begin2 = TREE_VEC_ELT (it2, 1);
6076 tree end2 = TREE_VEC_ELT (it2, 2);
6077 tree step2 = TREE_VEC_ELT (it2, 3);
6078 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6079 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6080 {
6081 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6082 "begin expression refers to outer iterator %qD", var);
6083 break;
6084 }
6085 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6086 {
6087 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6088 "end expression refers to outer iterator %qD", var);
6089 break;
6090 }
6091 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6092 {
6093 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6094 "step expression refers to outer iterator %qD", var);
6095 break;
6096 }
6097 }
6098 if (it2)
6099 {
6100 ret = true;
6101 continue;
6102 }
6103 TREE_VEC_ELT (it, 1) = begin;
6104 TREE_VEC_ELT (it, 2) = end;
6105 if (processing_template_decl)
6106 TREE_VEC_ELT (it, 3) = orig_step;
6107 else
6108 {
6109 TREE_VEC_ELT (it, 3) = step;
6110 TREE_VEC_ELT (it, 4) = orig_step;
6111 }
6112 }
6113 return ret;
6114 }
6115
6116 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6117 Remove any elements from the list that are invalid. */
6118
6119 tree
6120 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6121 {
6122 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6123 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
6124 tree c, t, *pc;
6125 tree safelen = NULL_TREE;
6126 bool branch_seen = false;
6127 bool copyprivate_seen = false;
6128 bool ordered_seen = false;
6129 bool oacc_async = false;
6130 tree last_iterators = NULL_TREE;
6131 bool last_iterators_remove = false;
6132 bool reduction_seen = false;
6133
6134 bitmap_obstack_initialize (NULL);
6135 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6136 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6137 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6138 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6139 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6140 bitmap_initialize (&map_head, &bitmap_default_obstack);
6141 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6142 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
6143 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6144
6145 if (ort & C_ORT_ACC)
6146 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6147 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6148 {
6149 oacc_async = true;
6150 break;
6151 }
6152
6153 for (pc = &clauses, c = clauses; c ; c = *pc)
6154 {
6155 bool remove = false;
6156 bool field_ok = false;
6157
6158 switch (OMP_CLAUSE_CODE (c))
6159 {
6160 case OMP_CLAUSE_SHARED:
6161 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6162 goto check_dup_generic;
6163 case OMP_CLAUSE_PRIVATE:
6164 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6165 goto check_dup_generic;
6166 case OMP_CLAUSE_REDUCTION:
6167 reduction_seen = true;
6168 /* FALLTHRU */
6169 case OMP_CLAUSE_IN_REDUCTION:
6170 case OMP_CLAUSE_TASK_REDUCTION:
6171 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6172 t = OMP_CLAUSE_DECL (c);
6173 if (TREE_CODE (t) == TREE_LIST)
6174 {
6175 if (handle_omp_array_sections (c, ort))
6176 {
6177 remove = true;
6178 break;
6179 }
6180 if (TREE_CODE (t) == TREE_LIST)
6181 {
6182 while (TREE_CODE (t) == TREE_LIST)
6183 t = TREE_CHAIN (t);
6184 }
6185 else
6186 {
6187 gcc_assert (TREE_CODE (t) == MEM_REF);
6188 t = TREE_OPERAND (t, 0);
6189 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6190 t = TREE_OPERAND (t, 0);
6191 if (TREE_CODE (t) == ADDR_EXPR
6192 || INDIRECT_REF_P (t))
6193 t = TREE_OPERAND (t, 0);
6194 }
6195 tree n = omp_clause_decl_field (t);
6196 if (n)
6197 t = n;
6198 goto check_dup_generic_t;
6199 }
6200 if (oacc_async)
6201 cxx_mark_addressable (t);
6202 goto check_dup_generic;
6203 case OMP_CLAUSE_COPYPRIVATE:
6204 copyprivate_seen = true;
6205 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6206 goto check_dup_generic;
6207 case OMP_CLAUSE_COPYIN:
6208 goto check_dup_generic;
6209 case OMP_CLAUSE_LINEAR:
6210 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6211 t = OMP_CLAUSE_DECL (c);
6212 if (ort != C_ORT_OMP_DECLARE_SIMD
6213 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6214 {
6215 error_at (OMP_CLAUSE_LOCATION (c),
6216 "modifier should not be specified in %<linear%> "
6217 "clause on %<simd%> or %<for%> constructs");
6218 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6219 }
6220 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6221 && !type_dependent_expression_p (t))
6222 {
6223 tree type = TREE_TYPE (t);
6224 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6225 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6226 && !TYPE_REF_P (type))
6227 {
6228 error_at (OMP_CLAUSE_LOCATION (c),
6229 "linear clause with %qs modifier applied to "
6230 "non-reference variable with %qT type",
6231 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6232 ? "ref" : "uval", TREE_TYPE (t));
6233 remove = true;
6234 break;
6235 }
6236 if (TYPE_REF_P (type))
6237 type = TREE_TYPE (type);
6238 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6239 {
6240 if (!INTEGRAL_TYPE_P (type)
6241 && !TYPE_PTR_P (type))
6242 {
6243 error_at (OMP_CLAUSE_LOCATION (c),
6244 "linear clause applied to non-integral "
6245 "non-pointer variable with %qT type",
6246 TREE_TYPE (t));
6247 remove = true;
6248 break;
6249 }
6250 }
6251 }
6252 t = OMP_CLAUSE_LINEAR_STEP (c);
6253 if (t == NULL_TREE)
6254 t = integer_one_node;
6255 if (t == error_mark_node)
6256 {
6257 remove = true;
6258 break;
6259 }
6260 else if (!type_dependent_expression_p (t)
6261 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6262 && (ort != C_ORT_OMP_DECLARE_SIMD
6263 || TREE_CODE (t) != PARM_DECL
6264 || !TYPE_REF_P (TREE_TYPE (t))
6265 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6266 {
6267 error_at (OMP_CLAUSE_LOCATION (c),
6268 "linear step expression must be integral");
6269 remove = true;
6270 break;
6271 }
6272 else
6273 {
6274 t = mark_rvalue_use (t);
6275 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6276 {
6277 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6278 goto check_dup_generic;
6279 }
6280 if (!processing_template_decl
6281 && (VAR_P (OMP_CLAUSE_DECL (c))
6282 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6283 {
6284 if (ort == C_ORT_OMP_DECLARE_SIMD)
6285 {
6286 t = maybe_constant_value (t);
6287 if (TREE_CODE (t) != INTEGER_CST)
6288 {
6289 error_at (OMP_CLAUSE_LOCATION (c),
6290 "%<linear%> clause step %qE is neither "
6291 "constant nor a parameter", t);
6292 remove = true;
6293 break;
6294 }
6295 }
6296 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6297 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6298 if (TYPE_REF_P (type))
6299 type = TREE_TYPE (type);
6300 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6301 {
6302 type = build_pointer_type (type);
6303 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6304 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6305 d, t);
6306 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6307 MINUS_EXPR, sizetype,
6308 fold_convert (sizetype, t),
6309 fold_convert (sizetype, d));
6310 if (t == error_mark_node)
6311 {
6312 remove = true;
6313 break;
6314 }
6315 }
6316 else if (TYPE_PTR_P (type)
6317 /* Can't multiply the step yet if *this
6318 is still incomplete type. */
6319 && (ort != C_ORT_OMP_DECLARE_SIMD
6320 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6321 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6322 || DECL_NAME (OMP_CLAUSE_DECL (c))
6323 != this_identifier
6324 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6325 {
6326 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6327 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6328 d, t);
6329 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6330 MINUS_EXPR, sizetype,
6331 fold_convert (sizetype, t),
6332 fold_convert (sizetype, d));
6333 if (t == error_mark_node)
6334 {
6335 remove = true;
6336 break;
6337 }
6338 }
6339 else
6340 t = fold_convert (type, t);
6341 }
6342 OMP_CLAUSE_LINEAR_STEP (c) = t;
6343 }
6344 goto check_dup_generic;
6345 check_dup_generic:
6346 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6347 if (t)
6348 {
6349 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6350 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6351 }
6352 else
6353 t = OMP_CLAUSE_DECL (c);
6354 check_dup_generic_t:
6355 if (t == current_class_ptr
6356 && (ort != C_ORT_OMP_DECLARE_SIMD
6357 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6358 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6359 {
6360 error_at (OMP_CLAUSE_LOCATION (c),
6361 "%<this%> allowed in OpenMP only in %<declare simd%>"
6362 " clauses");
6363 remove = true;
6364 break;
6365 }
6366 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6367 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6368 {
6369 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6370 break;
6371 if (DECL_P (t))
6372 error_at (OMP_CLAUSE_LOCATION (c),
6373 "%qD is not a variable in clause %qs", t,
6374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6375 else
6376 error_at (OMP_CLAUSE_LOCATION (c),
6377 "%qE is not a variable in clause %qs", t,
6378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6379 remove = true;
6380 }
6381 else if (ort == C_ORT_ACC
6382 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6383 {
6384 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6385 {
6386 error_at (OMP_CLAUSE_LOCATION (c),
6387 "%qD appears more than once in reduction clauses",
6388 t);
6389 remove = true;
6390 }
6391 else
6392 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6393 }
6394 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6395 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6396 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6397 {
6398 error_at (OMP_CLAUSE_LOCATION (c),
6399 "%qD appears more than once in data clauses", t);
6400 remove = true;
6401 }
6402 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6403 && bitmap_bit_p (&map_head, DECL_UID (t)))
6404 {
6405 if (ort == C_ORT_ACC)
6406 error_at (OMP_CLAUSE_LOCATION (c),
6407 "%qD appears more than once in data clauses", t);
6408 else
6409 error_at (OMP_CLAUSE_LOCATION (c),
6410 "%qD appears both in data and map clauses", t);
6411 remove = true;
6412 }
6413 else
6414 bitmap_set_bit (&generic_head, DECL_UID (t));
6415 if (!field_ok)
6416 break;
6417 handle_field_decl:
6418 if (!remove
6419 && TREE_CODE (t) == FIELD_DECL
6420 && t == OMP_CLAUSE_DECL (c)
6421 && ort != C_ORT_ACC)
6422 {
6423 OMP_CLAUSE_DECL (c)
6424 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6425 == OMP_CLAUSE_SHARED));
6426 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6427 remove = true;
6428 }
6429 break;
6430
6431 case OMP_CLAUSE_FIRSTPRIVATE:
6432 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6433 if (t)
6434 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6435 else
6436 t = OMP_CLAUSE_DECL (c);
6437 if (ort != C_ORT_ACC && t == current_class_ptr)
6438 {
6439 error_at (OMP_CLAUSE_LOCATION (c),
6440 "%<this%> allowed in OpenMP only in %<declare simd%>"
6441 " clauses");
6442 remove = true;
6443 break;
6444 }
6445 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6446 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6447 || TREE_CODE (t) != FIELD_DECL))
6448 {
6449 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6450 break;
6451 if (DECL_P (t))
6452 error_at (OMP_CLAUSE_LOCATION (c),
6453 "%qD is not a variable in clause %<firstprivate%>",
6454 t);
6455 else
6456 error_at (OMP_CLAUSE_LOCATION (c),
6457 "%qE is not a variable in clause %<firstprivate%>",
6458 t);
6459 remove = true;
6460 }
6461 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6462 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6463 {
6464 error_at (OMP_CLAUSE_LOCATION (c),
6465 "%qD appears more than once in data clauses", t);
6466 remove = true;
6467 }
6468 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6469 {
6470 if (ort == C_ORT_ACC)
6471 error_at (OMP_CLAUSE_LOCATION (c),
6472 "%qD appears more than once in data clauses", t);
6473 else
6474 error_at (OMP_CLAUSE_LOCATION (c),
6475 "%qD appears both in data and map clauses", t);
6476 remove = true;
6477 }
6478 else
6479 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6480 goto handle_field_decl;
6481
6482 case OMP_CLAUSE_LASTPRIVATE:
6483 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6484 if (t)
6485 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6486 else
6487 t = OMP_CLAUSE_DECL (c);
6488 if (t == current_class_ptr)
6489 {
6490 error_at (OMP_CLAUSE_LOCATION (c),
6491 "%<this%> allowed in OpenMP only in %<declare simd%>"
6492 " clauses");
6493 remove = true;
6494 break;
6495 }
6496 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6497 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6498 || TREE_CODE (t) != FIELD_DECL))
6499 {
6500 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6501 break;
6502 if (DECL_P (t))
6503 error_at (OMP_CLAUSE_LOCATION (c),
6504 "%qD is not a variable in clause %<lastprivate%>",
6505 t);
6506 else
6507 error_at (OMP_CLAUSE_LOCATION (c),
6508 "%qE is not a variable in clause %<lastprivate%>",
6509 t);
6510 remove = true;
6511 }
6512 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6513 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6514 {
6515 error_at (OMP_CLAUSE_LOCATION (c),
6516 "%qD appears more than once in data clauses", t);
6517 remove = true;
6518 }
6519 else
6520 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6521 goto handle_field_decl;
6522
6523 case OMP_CLAUSE_IF:
6524 t = OMP_CLAUSE_IF_EXPR (c);
6525 t = maybe_convert_cond (t);
6526 if (t == error_mark_node)
6527 remove = true;
6528 else if (!processing_template_decl)
6529 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6530 OMP_CLAUSE_IF_EXPR (c) = t;
6531 break;
6532
6533 case OMP_CLAUSE_FINAL:
6534 t = OMP_CLAUSE_FINAL_EXPR (c);
6535 t = maybe_convert_cond (t);
6536 if (t == error_mark_node)
6537 remove = true;
6538 else if (!processing_template_decl)
6539 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6540 OMP_CLAUSE_FINAL_EXPR (c) = t;
6541 break;
6542
6543 case OMP_CLAUSE_GANG:
6544 /* Operand 1 is the gang static: argument. */
6545 t = OMP_CLAUSE_OPERAND (c, 1);
6546 if (t != NULL_TREE)
6547 {
6548 if (t == error_mark_node)
6549 remove = true;
6550 else if (!type_dependent_expression_p (t)
6551 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6552 {
6553 error_at (OMP_CLAUSE_LOCATION (c),
6554 "%<gang%> static expression must be integral");
6555 remove = true;
6556 }
6557 else
6558 {
6559 t = mark_rvalue_use (t);
6560 if (!processing_template_decl)
6561 {
6562 t = maybe_constant_value (t);
6563 if (TREE_CODE (t) == INTEGER_CST
6564 && tree_int_cst_sgn (t) != 1
6565 && t != integer_minus_one_node)
6566 {
6567 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6568 "%<gang%> static value must be "
6569 "positive");
6570 t = integer_one_node;
6571 }
6572 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6573 }
6574 }
6575 OMP_CLAUSE_OPERAND (c, 1) = t;
6576 }
6577 /* Check operand 0, the num argument. */
6578 /* FALLTHRU */
6579
6580 case OMP_CLAUSE_WORKER:
6581 case OMP_CLAUSE_VECTOR:
6582 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6583 break;
6584 /* FALLTHRU */
6585
6586 case OMP_CLAUSE_NUM_TASKS:
6587 case OMP_CLAUSE_NUM_TEAMS:
6588 case OMP_CLAUSE_NUM_THREADS:
6589 case OMP_CLAUSE_NUM_GANGS:
6590 case OMP_CLAUSE_NUM_WORKERS:
6591 case OMP_CLAUSE_VECTOR_LENGTH:
6592 t = OMP_CLAUSE_OPERAND (c, 0);
6593 if (t == error_mark_node)
6594 remove = true;
6595 else if (!type_dependent_expression_p (t)
6596 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6597 {
6598 switch (OMP_CLAUSE_CODE (c))
6599 {
6600 case OMP_CLAUSE_GANG:
6601 error_at (OMP_CLAUSE_LOCATION (c),
6602 "%<gang%> num expression must be integral"); break;
6603 case OMP_CLAUSE_VECTOR:
6604 error_at (OMP_CLAUSE_LOCATION (c),
6605 "%<vector%> length expression must be integral");
6606 break;
6607 case OMP_CLAUSE_WORKER:
6608 error_at (OMP_CLAUSE_LOCATION (c),
6609 "%<worker%> num expression must be integral");
6610 break;
6611 default:
6612 error_at (OMP_CLAUSE_LOCATION (c),
6613 "%qs expression must be integral",
6614 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6615 }
6616 remove = true;
6617 }
6618 else
6619 {
6620 t = mark_rvalue_use (t);
6621 if (!processing_template_decl)
6622 {
6623 t = maybe_constant_value (t);
6624 if (TREE_CODE (t) == INTEGER_CST
6625 && tree_int_cst_sgn (t) != 1)
6626 {
6627 switch (OMP_CLAUSE_CODE (c))
6628 {
6629 case OMP_CLAUSE_GANG:
6630 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6631 "%<gang%> num value must be positive");
6632 break;
6633 case OMP_CLAUSE_VECTOR:
6634 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6635 "%<vector%> length value must be "
6636 "positive");
6637 break;
6638 case OMP_CLAUSE_WORKER:
6639 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6640 "%<worker%> num value must be "
6641 "positive");
6642 break;
6643 default:
6644 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6645 "%qs value must be positive",
6646 omp_clause_code_name
6647 [OMP_CLAUSE_CODE (c)]);
6648 }
6649 t = integer_one_node;
6650 }
6651 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6652 }
6653 OMP_CLAUSE_OPERAND (c, 0) = t;
6654 }
6655 break;
6656
6657 case OMP_CLAUSE_SCHEDULE:
6658 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6659 if (t == NULL)
6660 ;
6661 else if (t == error_mark_node)
6662 remove = true;
6663 else if (!type_dependent_expression_p (t)
6664 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6665 {
6666 error_at (OMP_CLAUSE_LOCATION (c),
6667 "schedule chunk size expression must be integral");
6668 remove = true;
6669 }
6670 else
6671 {
6672 t = mark_rvalue_use (t);
6673 if (!processing_template_decl)
6674 {
6675 t = maybe_constant_value (t);
6676 if (TREE_CODE (t) == INTEGER_CST
6677 && tree_int_cst_sgn (t) != 1)
6678 {
6679 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6680 "chunk size value must be positive");
6681 t = integer_one_node;
6682 }
6683 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6684 }
6685 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6686 }
6687 break;
6688
6689 case OMP_CLAUSE_SIMDLEN:
6690 case OMP_CLAUSE_SAFELEN:
6691 t = OMP_CLAUSE_OPERAND (c, 0);
6692 if (t == error_mark_node)
6693 remove = true;
6694 else if (!type_dependent_expression_p (t)
6695 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6696 {
6697 error_at (OMP_CLAUSE_LOCATION (c),
6698 "%qs length expression must be integral",
6699 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6700 remove = true;
6701 }
6702 else
6703 {
6704 t = mark_rvalue_use (t);
6705 if (!processing_template_decl)
6706 {
6707 t = maybe_constant_value (t);
6708 if (TREE_CODE (t) != INTEGER_CST
6709 || tree_int_cst_sgn (t) != 1)
6710 {
6711 error_at (OMP_CLAUSE_LOCATION (c),
6712 "%qs length expression must be positive "
6713 "constant integer expression",
6714 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6715 remove = true;
6716 }
6717 }
6718 OMP_CLAUSE_OPERAND (c, 0) = t;
6719 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6720 safelen = c;
6721 }
6722 break;
6723
6724 case OMP_CLAUSE_ASYNC:
6725 t = OMP_CLAUSE_ASYNC_EXPR (c);
6726 if (t == error_mark_node)
6727 remove = true;
6728 else if (!type_dependent_expression_p (t)
6729 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6730 {
6731 error_at (OMP_CLAUSE_LOCATION (c),
6732 "%<async%> expression must be integral");
6733 remove = true;
6734 }
6735 else
6736 {
6737 t = mark_rvalue_use (t);
6738 if (!processing_template_decl)
6739 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6740 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6741 }
6742 break;
6743
6744 case OMP_CLAUSE_WAIT:
6745 t = OMP_CLAUSE_WAIT_EXPR (c);
6746 if (t == error_mark_node)
6747 remove = true;
6748 else if (!processing_template_decl)
6749 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6750 OMP_CLAUSE_WAIT_EXPR (c) = t;
6751 break;
6752
6753 case OMP_CLAUSE_THREAD_LIMIT:
6754 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6755 if (t == error_mark_node)
6756 remove = true;
6757 else if (!type_dependent_expression_p (t)
6758 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6759 {
6760 error_at (OMP_CLAUSE_LOCATION (c),
6761 "%<thread_limit%> expression must be integral");
6762 remove = true;
6763 }
6764 else
6765 {
6766 t = mark_rvalue_use (t);
6767 if (!processing_template_decl)
6768 {
6769 t = maybe_constant_value (t);
6770 if (TREE_CODE (t) == INTEGER_CST
6771 && tree_int_cst_sgn (t) != 1)
6772 {
6773 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6774 "%<thread_limit%> value must be positive");
6775 t = integer_one_node;
6776 }
6777 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6778 }
6779 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6780 }
6781 break;
6782
6783 case OMP_CLAUSE_DEVICE:
6784 t = OMP_CLAUSE_DEVICE_ID (c);
6785 if (t == error_mark_node)
6786 remove = true;
6787 else if (!type_dependent_expression_p (t)
6788 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6789 {
6790 error_at (OMP_CLAUSE_LOCATION (c),
6791 "%<device%> id must be integral");
6792 remove = true;
6793 }
6794 else
6795 {
6796 t = mark_rvalue_use (t);
6797 if (!processing_template_decl)
6798 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6799 OMP_CLAUSE_DEVICE_ID (c) = t;
6800 }
6801 break;
6802
6803 case OMP_CLAUSE_DIST_SCHEDULE:
6804 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6805 if (t == NULL)
6806 ;
6807 else if (t == error_mark_node)
6808 remove = true;
6809 else if (!type_dependent_expression_p (t)
6810 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6811 {
6812 error_at (OMP_CLAUSE_LOCATION (c),
6813 "%<dist_schedule%> chunk size expression must be "
6814 "integral");
6815 remove = true;
6816 }
6817 else
6818 {
6819 t = mark_rvalue_use (t);
6820 if (!processing_template_decl)
6821 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6822 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6823 }
6824 break;
6825
6826 case OMP_CLAUSE_ALIGNED:
6827 t = OMP_CLAUSE_DECL (c);
6828 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6829 {
6830 error_at (OMP_CLAUSE_LOCATION (c),
6831 "%<this%> allowed in OpenMP only in %<declare simd%>"
6832 " clauses");
6833 remove = true;
6834 break;
6835 }
6836 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6837 {
6838 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6839 break;
6840 if (DECL_P (t))
6841 error_at (OMP_CLAUSE_LOCATION (c),
6842 "%qD is not a variable in %<aligned%> clause", t);
6843 else
6844 error_at (OMP_CLAUSE_LOCATION (c),
6845 "%qE is not a variable in %<aligned%> clause", t);
6846 remove = true;
6847 }
6848 else if (!type_dependent_expression_p (t)
6849 && !TYPE_PTR_P (TREE_TYPE (t))
6850 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6851 && (!TYPE_REF_P (TREE_TYPE (t))
6852 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6853 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6854 != ARRAY_TYPE))))
6855 {
6856 error_at (OMP_CLAUSE_LOCATION (c),
6857 "%qE in %<aligned%> clause is neither a pointer nor "
6858 "an array nor a reference to pointer or array", t);
6859 remove = true;
6860 }
6861 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6862 {
6863 error_at (OMP_CLAUSE_LOCATION (c),
6864 "%qD appears more than once in %<aligned%> clauses",
6865 t);
6866 remove = true;
6867 }
6868 else
6869 bitmap_set_bit (&aligned_head, DECL_UID (t));
6870 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6871 if (t == error_mark_node)
6872 remove = true;
6873 else if (t == NULL_TREE)
6874 break;
6875 else if (!type_dependent_expression_p (t)
6876 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6877 {
6878 error_at (OMP_CLAUSE_LOCATION (c),
6879 "%<aligned%> clause alignment expression must "
6880 "be integral");
6881 remove = true;
6882 }
6883 else
6884 {
6885 t = mark_rvalue_use (t);
6886 if (!processing_template_decl)
6887 {
6888 t = maybe_constant_value (t);
6889 if (TREE_CODE (t) != INTEGER_CST
6890 || tree_int_cst_sgn (t) != 1)
6891 {
6892 error_at (OMP_CLAUSE_LOCATION (c),
6893 "%<aligned%> clause alignment expression must "
6894 "be positive constant integer expression");
6895 remove = true;
6896 }
6897 else
6898 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6899 }
6900 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6901 }
6902 break;
6903
6904 case OMP_CLAUSE_NONTEMPORAL:
6905 t = OMP_CLAUSE_DECL (c);
6906 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6907 {
6908 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6909 break;
6910 if (DECL_P (t))
6911 error_at (OMP_CLAUSE_LOCATION (c),
6912 "%qD is not a variable in %<nontemporal%> clause",
6913 t);
6914 else
6915 error_at (OMP_CLAUSE_LOCATION (c),
6916 "%qE is not a variable in %<nontemporal%> clause",
6917 t);
6918 remove = true;
6919 }
6920 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6921 {
6922 error_at (OMP_CLAUSE_LOCATION (c),
6923 "%qD appears more than once in %<nontemporal%> "
6924 "clauses", t);
6925 remove = true;
6926 }
6927 else
6928 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6929 break;
6930
6931 case OMP_CLAUSE_DEPEND:
6932 t = OMP_CLAUSE_DECL (c);
6933 if (t == NULL_TREE)
6934 {
6935 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6936 == OMP_CLAUSE_DEPEND_SOURCE);
6937 break;
6938 }
6939 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6940 {
6941 if (cp_finish_omp_clause_depend_sink (c))
6942 remove = true;
6943 break;
6944 }
6945 if (TREE_CODE (t) == TREE_LIST
6946 && TREE_PURPOSE (t)
6947 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
6948 {
6949 if (TREE_PURPOSE (t) != last_iterators)
6950 last_iterators_remove
6951 = cp_omp_finish_iterators (TREE_PURPOSE (t));
6952 last_iterators = TREE_PURPOSE (t);
6953 t = TREE_VALUE (t);
6954 if (last_iterators_remove)
6955 t = error_mark_node;
6956 }
6957 else
6958 last_iterators = NULL_TREE;
6959
6960 if (TREE_CODE (t) == TREE_LIST)
6961 {
6962 if (handle_omp_array_sections (c, ort))
6963 remove = true;
6964 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6965 {
6966 error_at (OMP_CLAUSE_LOCATION (c),
6967 "%<depend%> clause with %<depobj%> dependence "
6968 "type on array section");
6969 remove = true;
6970 }
6971 break;
6972 }
6973 if (t == error_mark_node)
6974 remove = true;
6975 else if (t == current_class_ptr)
6976 {
6977 error_at (OMP_CLAUSE_LOCATION (c),
6978 "%<this%> allowed in OpenMP only in %<declare simd%>"
6979 " clauses");
6980 remove = true;
6981 }
6982 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6983 break;
6984 else if (!lvalue_p (t))
6985 {
6986 if (DECL_P (t))
6987 error_at (OMP_CLAUSE_LOCATION (c),
6988 "%qD is not lvalue expression nor array section "
6989 "in %<depend%> clause", t);
6990 else
6991 error_at (OMP_CLAUSE_LOCATION (c),
6992 "%qE is not lvalue expression nor array section "
6993 "in %<depend%> clause", t);
6994 remove = true;
6995 }
6996 else if (TREE_CODE (t) == COMPONENT_REF
6997 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6998 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6999 {
7000 error_at (OMP_CLAUSE_LOCATION (c),
7001 "bit-field %qE in %qs clause", t, "depend");
7002 remove = true;
7003 }
7004 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7005 {
7006 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7007 ? TREE_TYPE (TREE_TYPE (t))
7008 : TREE_TYPE (t)))
7009 {
7010 error_at (OMP_CLAUSE_LOCATION (c),
7011 "%qE does not have %<omp_depend_t%> type in "
7012 "%<depend%> clause with %<depobj%> dependence "
7013 "type", t);
7014 remove = true;
7015 }
7016 }
7017 else if (c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7018 ? TREE_TYPE (TREE_TYPE (t))
7019 : TREE_TYPE (t)))
7020 {
7021 error_at (OMP_CLAUSE_LOCATION (c),
7022 "%qE should not have %<omp_depend_t%> type in "
7023 "%<depend%> clause with dependence type other than "
7024 "%<depobj%>", t);
7025 remove = true;
7026 }
7027 if (!remove)
7028 {
7029 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
7030 if (addr == error_mark_node)
7031 remove = true;
7032 else
7033 {
7034 t = cp_build_indirect_ref (addr, RO_UNARY_STAR,
7035 tf_warning_or_error);
7036 if (t == error_mark_node)
7037 remove = true;
7038 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
7039 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7040 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7041 == TREE_VEC))
7042 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7043 else
7044 OMP_CLAUSE_DECL (c) = t;
7045 }
7046 }
7047 break;
7048
7049 case OMP_CLAUSE_MAP:
7050 case OMP_CLAUSE_TO:
7051 case OMP_CLAUSE_FROM:
7052 case OMP_CLAUSE__CACHE_:
7053 t = OMP_CLAUSE_DECL (c);
7054 if (TREE_CODE (t) == TREE_LIST)
7055 {
7056 if (handle_omp_array_sections (c, ort))
7057 remove = true;
7058 else
7059 {
7060 t = OMP_CLAUSE_DECL (c);
7061 if (TREE_CODE (t) != TREE_LIST
7062 && !type_dependent_expression_p (t)
7063 && !cp_omp_mappable_type (TREE_TYPE (t)))
7064 {
7065 error_at (OMP_CLAUSE_LOCATION (c),
7066 "array section does not have mappable type "
7067 "in %qs clause",
7068 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7069 remove = true;
7070 }
7071 while (TREE_CODE (t) == ARRAY_REF)
7072 t = TREE_OPERAND (t, 0);
7073 if (TREE_CODE (t) == COMPONENT_REF
7074 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7075 {
7076 while (TREE_CODE (t) == COMPONENT_REF)
7077 t = TREE_OPERAND (t, 0);
7078 if (REFERENCE_REF_P (t))
7079 t = TREE_OPERAND (t, 0);
7080 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7081 break;
7082 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7083 {
7084 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7085 error_at (OMP_CLAUSE_LOCATION (c),
7086 "%qD appears more than once in motion"
7087 " clauses", t);
7088 else if (ort == C_ORT_ACC)
7089 error_at (OMP_CLAUSE_LOCATION (c),
7090 "%qD appears more than once in data"
7091 " clauses", t);
7092 else
7093 error_at (OMP_CLAUSE_LOCATION (c),
7094 "%qD appears more than once in map"
7095 " clauses", t);
7096 remove = true;
7097 }
7098 else
7099 {
7100 bitmap_set_bit (&map_head, DECL_UID (t));
7101 bitmap_set_bit (&map_field_head, DECL_UID (t));
7102 }
7103 }
7104 }
7105 break;
7106 }
7107 if (t == error_mark_node)
7108 {
7109 remove = true;
7110 break;
7111 }
7112 if (REFERENCE_REF_P (t)
7113 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7114 {
7115 t = TREE_OPERAND (t, 0);
7116 OMP_CLAUSE_DECL (c) = t;
7117 }
7118 if (TREE_CODE (t) == COMPONENT_REF
7119 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
7120 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7121 {
7122 if (type_dependent_expression_p (t))
7123 break;
7124 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7125 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7126 {
7127 error_at (OMP_CLAUSE_LOCATION (c),
7128 "bit-field %qE in %qs clause",
7129 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7130 remove = true;
7131 }
7132 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7133 {
7134 error_at (OMP_CLAUSE_LOCATION (c),
7135 "%qE does not have a mappable type in %qs clause",
7136 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7137 remove = true;
7138 }
7139 while (TREE_CODE (t) == COMPONENT_REF)
7140 {
7141 if (TREE_TYPE (TREE_OPERAND (t, 0))
7142 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7143 == UNION_TYPE))
7144 {
7145 error_at (OMP_CLAUSE_LOCATION (c),
7146 "%qE is a member of a union", t);
7147 remove = true;
7148 break;
7149 }
7150 t = TREE_OPERAND (t, 0);
7151 }
7152 if (remove)
7153 break;
7154 if (REFERENCE_REF_P (t))
7155 t = TREE_OPERAND (t, 0);
7156 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7157 {
7158 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7159 goto handle_map_references;
7160 }
7161 }
7162 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7163 {
7164 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7165 break;
7166 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7167 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7168 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
7169 break;
7170 if (DECL_P (t))
7171 error_at (OMP_CLAUSE_LOCATION (c),
7172 "%qD is not a variable in %qs clause", t,
7173 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7174 else
7175 error_at (OMP_CLAUSE_LOCATION (c),
7176 "%qE is not a variable in %qs clause", t,
7177 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7178 remove = true;
7179 }
7180 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7181 {
7182 error_at (OMP_CLAUSE_LOCATION (c),
7183 "%qD is threadprivate variable in %qs clause", t,
7184 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7185 remove = true;
7186 }
7187 else if (ort != C_ORT_ACC && t == current_class_ptr)
7188 {
7189 error_at (OMP_CLAUSE_LOCATION (c),
7190 "%<this%> allowed in OpenMP only in %<declare simd%>"
7191 " clauses");
7192 remove = true;
7193 break;
7194 }
7195 else if (!processing_template_decl
7196 && !TYPE_REF_P (TREE_TYPE (t))
7197 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7198 || (OMP_CLAUSE_MAP_KIND (c)
7199 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7200 && !cxx_mark_addressable (t))
7201 remove = true;
7202 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7203 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7204 || (OMP_CLAUSE_MAP_KIND (c)
7205 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7206 && t == OMP_CLAUSE_DECL (c)
7207 && !type_dependent_expression_p (t)
7208 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7209 ? TREE_TYPE (TREE_TYPE (t))
7210 : TREE_TYPE (t)))
7211 {
7212 error_at (OMP_CLAUSE_LOCATION (c),
7213 "%qD does not have a mappable type in %qs clause", t,
7214 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7215 remove = true;
7216 }
7217 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7218 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7219 && !type_dependent_expression_p (t)
7220 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7221 {
7222 error_at (OMP_CLAUSE_LOCATION (c),
7223 "%qD is not a pointer variable", t);
7224 remove = true;
7225 }
7226 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7227 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7228 {
7229 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7230 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7231 {
7232 error_at (OMP_CLAUSE_LOCATION (c),
7233 "%qD appears more than once in data clauses", t);
7234 remove = true;
7235 }
7236 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7237 {
7238 if (ort == C_ORT_ACC)
7239 error_at (OMP_CLAUSE_LOCATION (c),
7240 "%qD appears more than once in data clauses", t);
7241 else
7242 error_at (OMP_CLAUSE_LOCATION (c),
7243 "%qD appears both in data and map clauses", t);
7244 remove = true;
7245 }
7246 else
7247 bitmap_set_bit (&generic_head, DECL_UID (t));
7248 }
7249 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7250 {
7251 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7252 error_at (OMP_CLAUSE_LOCATION (c),
7253 "%qD appears more than once in motion clauses", t);
7254 if (ort == C_ORT_ACC)
7255 error_at (OMP_CLAUSE_LOCATION (c),
7256 "%qD appears more than once in data clauses", t);
7257 else
7258 error_at (OMP_CLAUSE_LOCATION (c),
7259 "%qD appears more than once in map clauses", t);
7260 remove = true;
7261 }
7262 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7263 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7264 {
7265 if (ort == C_ORT_ACC)
7266 error_at (OMP_CLAUSE_LOCATION (c),
7267 "%qD appears more than once in data clauses", t);
7268 else
7269 error_at (OMP_CLAUSE_LOCATION (c),
7270 "%qD appears both in data and map clauses", t);
7271 remove = true;
7272 }
7273 else
7274 {
7275 bitmap_set_bit (&map_head, DECL_UID (t));
7276 if (t != OMP_CLAUSE_DECL (c)
7277 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7278 bitmap_set_bit (&map_field_head, DECL_UID (t));
7279 }
7280 handle_map_references:
7281 if (!remove
7282 && !processing_template_decl
7283 && ort != C_ORT_DECLARE_SIMD
7284 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
7285 {
7286 t = OMP_CLAUSE_DECL (c);
7287 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7288 {
7289 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7290 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7291 OMP_CLAUSE_SIZE (c)
7292 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7293 }
7294 else if (OMP_CLAUSE_MAP_KIND (c)
7295 != GOMP_MAP_FIRSTPRIVATE_POINTER
7296 && (OMP_CLAUSE_MAP_KIND (c)
7297 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
7298 && (OMP_CLAUSE_MAP_KIND (c)
7299 != GOMP_MAP_ALWAYS_POINTER))
7300 {
7301 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7302 OMP_CLAUSE_MAP);
7303 if (TREE_CODE (t) == COMPONENT_REF)
7304 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
7305 else
7306 OMP_CLAUSE_SET_MAP_KIND (c2,
7307 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7308 OMP_CLAUSE_DECL (c2) = t;
7309 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7310 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
7311 OMP_CLAUSE_CHAIN (c) = c2;
7312 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7313 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7314 OMP_CLAUSE_SIZE (c)
7315 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7316 c = c2;
7317 }
7318 }
7319 break;
7320
7321 case OMP_CLAUSE_TO_DECLARE:
7322 case OMP_CLAUSE_LINK:
7323 t = OMP_CLAUSE_DECL (c);
7324 if (TREE_CODE (t) == FUNCTION_DECL
7325 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7326 ;
7327 else if (!VAR_P (t))
7328 {
7329 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7330 {
7331 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
7332 error_at (OMP_CLAUSE_LOCATION (c),
7333 "template %qE in clause %qs", t,
7334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7335 else if (really_overloaded_fn (t))
7336 error_at (OMP_CLAUSE_LOCATION (c),
7337 "overloaded function name %qE in clause %qs", t,
7338 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7339 else
7340 error_at (OMP_CLAUSE_LOCATION (c),
7341 "%qE is neither a variable nor a function name "
7342 "in clause %qs", t,
7343 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7344 }
7345 else
7346 error_at (OMP_CLAUSE_LOCATION (c),
7347 "%qE is not a variable in clause %qs", t,
7348 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7349 remove = true;
7350 }
7351 else if (DECL_THREAD_LOCAL_P (t))
7352 {
7353 error_at (OMP_CLAUSE_LOCATION (c),
7354 "%qD is threadprivate variable in %qs clause", t,
7355 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7356 remove = true;
7357 }
7358 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7359 {
7360 error_at (OMP_CLAUSE_LOCATION (c),
7361 "%qD does not have a mappable type in %qs clause", t,
7362 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7363 remove = true;
7364 }
7365 if (remove)
7366 break;
7367 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
7368 {
7369 error_at (OMP_CLAUSE_LOCATION (c),
7370 "%qE appears more than once on the same "
7371 "%<declare target%> directive", t);
7372 remove = true;
7373 }
7374 else
7375 bitmap_set_bit (&generic_head, DECL_UID (t));
7376 break;
7377
7378 case OMP_CLAUSE_UNIFORM:
7379 t = OMP_CLAUSE_DECL (c);
7380 if (TREE_CODE (t) != PARM_DECL)
7381 {
7382 if (processing_template_decl)
7383 break;
7384 if (DECL_P (t))
7385 error_at (OMP_CLAUSE_LOCATION (c),
7386 "%qD is not an argument in %<uniform%> clause", t);
7387 else
7388 error_at (OMP_CLAUSE_LOCATION (c),
7389 "%qE is not an argument in %<uniform%> clause", t);
7390 remove = true;
7391 break;
7392 }
7393 /* map_head bitmap is used as uniform_head if declare_simd. */
7394 bitmap_set_bit (&map_head, DECL_UID (t));
7395 goto check_dup_generic;
7396
7397 case OMP_CLAUSE_GRAINSIZE:
7398 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7399 if (t == error_mark_node)
7400 remove = true;
7401 else if (!type_dependent_expression_p (t)
7402 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7403 {
7404 error_at (OMP_CLAUSE_LOCATION (c),
7405 "%<grainsize%> expression must be integral");
7406 remove = true;
7407 }
7408 else
7409 {
7410 t = mark_rvalue_use (t);
7411 if (!processing_template_decl)
7412 {
7413 t = maybe_constant_value (t);
7414 if (TREE_CODE (t) == INTEGER_CST
7415 && tree_int_cst_sgn (t) != 1)
7416 {
7417 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7418 "%<grainsize%> value must be positive");
7419 t = integer_one_node;
7420 }
7421 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7422 }
7423 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7424 }
7425 break;
7426
7427 case OMP_CLAUSE_PRIORITY:
7428 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7429 if (t == error_mark_node)
7430 remove = true;
7431 else if (!type_dependent_expression_p (t)
7432 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7433 {
7434 error_at (OMP_CLAUSE_LOCATION (c),
7435 "%<priority%> expression must be integral");
7436 remove = true;
7437 }
7438 else
7439 {
7440 t = mark_rvalue_use (t);
7441 if (!processing_template_decl)
7442 {
7443 t = maybe_constant_value (t);
7444 if (TREE_CODE (t) == INTEGER_CST
7445 && tree_int_cst_sgn (t) == -1)
7446 {
7447 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7448 "%<priority%> value must be non-negative");
7449 t = integer_one_node;
7450 }
7451 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7452 }
7453 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7454 }
7455 break;
7456
7457 case OMP_CLAUSE_HINT:
7458 t = OMP_CLAUSE_HINT_EXPR (c);
7459 if (t == error_mark_node)
7460 remove = true;
7461 else if (!type_dependent_expression_p (t)
7462 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7463 {
7464 error_at (OMP_CLAUSE_LOCATION (c),
7465 "%<hint%> expression must be integral");
7466 remove = true;
7467 }
7468 else
7469 {
7470 t = mark_rvalue_use (t);
7471 if (!processing_template_decl)
7472 {
7473 t = maybe_constant_value (t);
7474 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7475 if (TREE_CODE (t) != INTEGER_CST)
7476 {
7477 error_at (OMP_CLAUSE_LOCATION (c),
7478 "%<hint%> expression must be constant integer "
7479 "expression");
7480 remove = true;
7481 }
7482 }
7483 OMP_CLAUSE_HINT_EXPR (c) = t;
7484 }
7485 break;
7486
7487 case OMP_CLAUSE_IS_DEVICE_PTR:
7488 case OMP_CLAUSE_USE_DEVICE_PTR:
7489 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7490 t = OMP_CLAUSE_DECL (c);
7491 if (!type_dependent_expression_p (t))
7492 {
7493 tree type = TREE_TYPE (t);
7494 if (!TYPE_PTR_P (type)
7495 && TREE_CODE (type) != ARRAY_TYPE
7496 && (!TYPE_REF_P (type)
7497 || (!TYPE_PTR_P (TREE_TYPE (type))
7498 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7499 {
7500 error_at (OMP_CLAUSE_LOCATION (c),
7501 "%qs variable is neither a pointer, nor an array "
7502 "nor reference to pointer or array",
7503 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7504 remove = true;
7505 }
7506 }
7507 goto check_dup_generic;
7508
7509 case OMP_CLAUSE_NOWAIT:
7510 case OMP_CLAUSE_DEFAULT:
7511 case OMP_CLAUSE_UNTIED:
7512 case OMP_CLAUSE_COLLAPSE:
7513 case OMP_CLAUSE_MERGEABLE:
7514 case OMP_CLAUSE_PARALLEL:
7515 case OMP_CLAUSE_FOR:
7516 case OMP_CLAUSE_SECTIONS:
7517 case OMP_CLAUSE_TASKGROUP:
7518 case OMP_CLAUSE_PROC_BIND:
7519 case OMP_CLAUSE_NOGROUP:
7520 case OMP_CLAUSE_THREADS:
7521 case OMP_CLAUSE_SIMD:
7522 case OMP_CLAUSE_DEFAULTMAP:
7523 case OMP_CLAUSE_AUTO:
7524 case OMP_CLAUSE_INDEPENDENT:
7525 case OMP_CLAUSE_SEQ:
7526 case OMP_CLAUSE_IF_PRESENT:
7527 case OMP_CLAUSE_FINALIZE:
7528 break;
7529
7530 case OMP_CLAUSE_TILE:
7531 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7532 list = TREE_CHAIN (list))
7533 {
7534 t = TREE_VALUE (list);
7535
7536 if (t == error_mark_node)
7537 remove = true;
7538 else if (!type_dependent_expression_p (t)
7539 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7540 {
7541 error_at (OMP_CLAUSE_LOCATION (c),
7542 "%<tile%> argument needs integral type");
7543 remove = true;
7544 }
7545 else
7546 {
7547 t = mark_rvalue_use (t);
7548 if (!processing_template_decl)
7549 {
7550 /* Zero is used to indicate '*', we permit you
7551 to get there via an ICE of value zero. */
7552 t = maybe_constant_value (t);
7553 if (!tree_fits_shwi_p (t)
7554 || tree_to_shwi (t) < 0)
7555 {
7556 error_at (OMP_CLAUSE_LOCATION (c),
7557 "%<tile%> argument needs positive "
7558 "integral constant");
7559 remove = true;
7560 }
7561 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7562 }
7563 }
7564
7565 /* Update list item. */
7566 TREE_VALUE (list) = t;
7567 }
7568 break;
7569
7570 case OMP_CLAUSE_ORDERED:
7571 ordered_seen = true;
7572 break;
7573
7574 case OMP_CLAUSE_INBRANCH:
7575 case OMP_CLAUSE_NOTINBRANCH:
7576 if (branch_seen)
7577 {
7578 error_at (OMP_CLAUSE_LOCATION (c),
7579 "%<inbranch%> clause is incompatible with "
7580 "%<notinbranch%>");
7581 remove = true;
7582 }
7583 branch_seen = true;
7584 break;
7585
7586 default:
7587 gcc_unreachable ();
7588 }
7589
7590 if (remove)
7591 *pc = OMP_CLAUSE_CHAIN (c);
7592 else
7593 pc = &OMP_CLAUSE_CHAIN (c);
7594 }
7595
7596 for (pc = &clauses, c = clauses; c ; c = *pc)
7597 {
7598 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7599 bool remove = false;
7600 bool need_complete_type = false;
7601 bool need_default_ctor = false;
7602 bool need_copy_ctor = false;
7603 bool need_copy_assignment = false;
7604 bool need_implicitly_determined = false;
7605 bool need_dtor = false;
7606 tree type, inner_type;
7607
7608 switch (c_kind)
7609 {
7610 case OMP_CLAUSE_SHARED:
7611 need_implicitly_determined = true;
7612 break;
7613 case OMP_CLAUSE_PRIVATE:
7614 need_complete_type = true;
7615 need_default_ctor = true;
7616 need_dtor = true;
7617 need_implicitly_determined = true;
7618 break;
7619 case OMP_CLAUSE_FIRSTPRIVATE:
7620 need_complete_type = true;
7621 need_copy_ctor = true;
7622 need_dtor = true;
7623 need_implicitly_determined = true;
7624 break;
7625 case OMP_CLAUSE_LASTPRIVATE:
7626 need_complete_type = true;
7627 need_copy_assignment = true;
7628 need_implicitly_determined = true;
7629 break;
7630 case OMP_CLAUSE_REDUCTION:
7631 case OMP_CLAUSE_IN_REDUCTION:
7632 case OMP_CLAUSE_TASK_REDUCTION:
7633 need_implicitly_determined = true;
7634 break;
7635 case OMP_CLAUSE_LINEAR:
7636 if (ort != C_ORT_OMP_DECLARE_SIMD)
7637 need_implicitly_determined = true;
7638 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7639 && !bitmap_bit_p (&map_head,
7640 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7641 {
7642 error_at (OMP_CLAUSE_LOCATION (c),
7643 "%<linear%> clause step is a parameter %qD not "
7644 "specified in %<uniform%> clause",
7645 OMP_CLAUSE_LINEAR_STEP (c));
7646 *pc = OMP_CLAUSE_CHAIN (c);
7647 continue;
7648 }
7649 break;
7650 case OMP_CLAUSE_COPYPRIVATE:
7651 need_copy_assignment = true;
7652 break;
7653 case OMP_CLAUSE_COPYIN:
7654 need_copy_assignment = true;
7655 break;
7656 case OMP_CLAUSE_SIMDLEN:
7657 if (safelen
7658 && !processing_template_decl
7659 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7660 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7661 {
7662 error_at (OMP_CLAUSE_LOCATION (c),
7663 "%<simdlen%> clause value is bigger than "
7664 "%<safelen%> clause value");
7665 OMP_CLAUSE_SIMDLEN_EXPR (c)
7666 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7667 }
7668 pc = &OMP_CLAUSE_CHAIN (c);
7669 continue;
7670 case OMP_CLAUSE_SCHEDULE:
7671 if (ordered_seen
7672 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7673 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7674 {
7675 error_at (OMP_CLAUSE_LOCATION (c),
7676 "%<nonmonotonic%> schedule modifier specified "
7677 "together with %<ordered%> clause");
7678 OMP_CLAUSE_SCHEDULE_KIND (c)
7679 = (enum omp_clause_schedule_kind)
7680 (OMP_CLAUSE_SCHEDULE_KIND (c)
7681 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7682 }
7683 pc = &OMP_CLAUSE_CHAIN (c);
7684 continue;
7685 case OMP_CLAUSE_NOGROUP:
7686 if (reduction_seen)
7687 {
7688 error_at (OMP_CLAUSE_LOCATION (c),
7689 "%<nogroup%> clause must not be used together with "
7690 "%<reduction%> clause");
7691 *pc = OMP_CLAUSE_CHAIN (c);
7692 continue;
7693 }
7694 pc = &OMP_CLAUSE_CHAIN (c);
7695 continue;
7696 case OMP_CLAUSE_NOWAIT:
7697 if (copyprivate_seen)
7698 {
7699 error_at (OMP_CLAUSE_LOCATION (c),
7700 "%<nowait%> clause must not be used together "
7701 "with %<copyprivate%>");
7702 *pc = OMP_CLAUSE_CHAIN (c);
7703 continue;
7704 }
7705 /* FALLTHRU */
7706 default:
7707 pc = &OMP_CLAUSE_CHAIN (c);
7708 continue;
7709 }
7710
7711 t = OMP_CLAUSE_DECL (c);
7712 if (processing_template_decl
7713 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7714 {
7715 pc = &OMP_CLAUSE_CHAIN (c);
7716 continue;
7717 }
7718
7719 switch (c_kind)
7720 {
7721 case OMP_CLAUSE_LASTPRIVATE:
7722 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7723 {
7724 need_default_ctor = true;
7725 need_dtor = true;
7726 }
7727 break;
7728
7729 case OMP_CLAUSE_REDUCTION:
7730 case OMP_CLAUSE_IN_REDUCTION:
7731 case OMP_CLAUSE_TASK_REDUCTION:
7732 if (finish_omp_reduction_clause (c, &need_default_ctor,
7733 &need_dtor))
7734 remove = true;
7735 else
7736 t = OMP_CLAUSE_DECL (c);
7737 break;
7738
7739 case OMP_CLAUSE_COPYIN:
7740 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7741 {
7742 error_at (OMP_CLAUSE_LOCATION (c),
7743 "%qE must be %<threadprivate%> for %<copyin%>", t);
7744 remove = true;
7745 }
7746 break;
7747
7748 default:
7749 break;
7750 }
7751
7752 if (need_complete_type || need_copy_assignment)
7753 {
7754 t = require_complete_type (t);
7755 if (t == error_mark_node)
7756 remove = true;
7757 else if (TYPE_REF_P (TREE_TYPE (t))
7758 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7759 remove = true;
7760 }
7761 if (need_implicitly_determined)
7762 {
7763 const char *share_name = NULL;
7764
7765 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7766 share_name = "threadprivate";
7767 else switch (cxx_omp_predetermined_sharing_1 (t))
7768 {
7769 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7770 break;
7771 case OMP_CLAUSE_DEFAULT_SHARED:
7772 if (VAR_P (t)
7773 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7774 && TREE_STATIC (t)
7775 && cxx_omp_const_qual_no_mutable (t))
7776 {
7777 tree ctx = CP_DECL_CONTEXT (t);
7778 /* const qualified static data members without mutable
7779 member may be specified in firstprivate clause. */
7780 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
7781 break;
7782 }
7783 share_name = "shared";
7784 break;
7785 case OMP_CLAUSE_DEFAULT_PRIVATE:
7786 share_name = "private";
7787 break;
7788 default:
7789 gcc_unreachable ();
7790 }
7791 if (share_name)
7792 {
7793 error_at (OMP_CLAUSE_LOCATION (c),
7794 "%qE is predetermined %qs for %qs",
7795 omp_clause_printable_decl (t), share_name,
7796 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7797 remove = true;
7798 }
7799 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
7800 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
7801 && cxx_omp_const_qual_no_mutable (t))
7802 {
7803 error_at (OMP_CLAUSE_LOCATION (c),
7804 "%<const%> qualified %qE without %<mutable%> member "
7805 "may appear only in %<shared%> or %<firstprivate%> "
7806 "clauses", omp_clause_printable_decl (t));
7807 remove = true;
7808 }
7809 }
7810
7811 /* We're interested in the base element, not arrays. */
7812 inner_type = type = TREE_TYPE (t);
7813 if ((need_complete_type
7814 || need_copy_assignment
7815 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7816 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7817 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
7818 && TYPE_REF_P (inner_type))
7819 inner_type = TREE_TYPE (inner_type);
7820 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7821 inner_type = TREE_TYPE (inner_type);
7822
7823 /* Check for special function availability by building a call to one.
7824 Save the results, because later we won't be in the right context
7825 for making these queries. */
7826 if (CLASS_TYPE_P (inner_type)
7827 && COMPLETE_TYPE_P (inner_type)
7828 && (need_default_ctor || need_copy_ctor
7829 || need_copy_assignment || need_dtor)
7830 && !type_dependent_expression_p (t)
7831 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7832 need_copy_ctor, need_copy_assignment,
7833 need_dtor))
7834 remove = true;
7835
7836 if (!remove
7837 && c_kind == OMP_CLAUSE_SHARED
7838 && processing_template_decl)
7839 {
7840 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7841 if (t)
7842 OMP_CLAUSE_DECL (c) = t;
7843 }
7844
7845 if (remove)
7846 *pc = OMP_CLAUSE_CHAIN (c);
7847 else
7848 pc = &OMP_CLAUSE_CHAIN (c);
7849 }
7850
7851 bitmap_obstack_release (NULL);
7852 return clauses;
7853 }
7854
7855 /* Start processing OpenMP clauses that can include any
7856 privatization clauses for non-static data members. */
7857
7858 tree
7859 push_omp_privatization_clauses (bool ignore_next)
7860 {
7861 if (omp_private_member_ignore_next)
7862 {
7863 omp_private_member_ignore_next = ignore_next;
7864 return NULL_TREE;
7865 }
7866 omp_private_member_ignore_next = ignore_next;
7867 if (omp_private_member_map)
7868 omp_private_member_vec.safe_push (error_mark_node);
7869 return push_stmt_list ();
7870 }
7871
7872 /* Revert remapping of any non-static data members since
7873 the last push_omp_privatization_clauses () call. */
7874
7875 void
7876 pop_omp_privatization_clauses (tree stmt)
7877 {
7878 if (stmt == NULL_TREE)
7879 return;
7880 stmt = pop_stmt_list (stmt);
7881 if (omp_private_member_map)
7882 {
7883 while (!omp_private_member_vec.is_empty ())
7884 {
7885 tree t = omp_private_member_vec.pop ();
7886 if (t == error_mark_node)
7887 {
7888 add_stmt (stmt);
7889 return;
7890 }
7891 bool no_decl_expr = t == integer_zero_node;
7892 if (no_decl_expr)
7893 t = omp_private_member_vec.pop ();
7894 tree *v = omp_private_member_map->get (t);
7895 gcc_assert (v);
7896 if (!no_decl_expr)
7897 add_decl_expr (*v);
7898 omp_private_member_map->remove (t);
7899 }
7900 delete omp_private_member_map;
7901 omp_private_member_map = NULL;
7902 }
7903 add_stmt (stmt);
7904 }
7905
7906 /* Remember OpenMP privatization clauses mapping and clear it.
7907 Used for lambdas. */
7908
7909 void
7910 save_omp_privatization_clauses (vec<tree> &save)
7911 {
7912 save = vNULL;
7913 if (omp_private_member_ignore_next)
7914 save.safe_push (integer_one_node);
7915 omp_private_member_ignore_next = false;
7916 if (!omp_private_member_map)
7917 return;
7918
7919 while (!omp_private_member_vec.is_empty ())
7920 {
7921 tree t = omp_private_member_vec.pop ();
7922 if (t == error_mark_node)
7923 {
7924 save.safe_push (t);
7925 continue;
7926 }
7927 tree n = t;
7928 if (t == integer_zero_node)
7929 t = omp_private_member_vec.pop ();
7930 tree *v = omp_private_member_map->get (t);
7931 gcc_assert (v);
7932 save.safe_push (*v);
7933 save.safe_push (t);
7934 if (n != t)
7935 save.safe_push (n);
7936 }
7937 delete omp_private_member_map;
7938 omp_private_member_map = NULL;
7939 }
7940
7941 /* Restore OpenMP privatization clauses mapping saved by the
7942 above function. */
7943
7944 void
7945 restore_omp_privatization_clauses (vec<tree> &save)
7946 {
7947 gcc_assert (omp_private_member_vec.is_empty ());
7948 omp_private_member_ignore_next = false;
7949 if (save.is_empty ())
7950 return;
7951 if (save.length () == 1 && save[0] == integer_one_node)
7952 {
7953 omp_private_member_ignore_next = true;
7954 save.release ();
7955 return;
7956 }
7957
7958 omp_private_member_map = new hash_map <tree, tree>;
7959 while (!save.is_empty ())
7960 {
7961 tree t = save.pop ();
7962 tree n = t;
7963 if (t != error_mark_node)
7964 {
7965 if (t == integer_one_node)
7966 {
7967 omp_private_member_ignore_next = true;
7968 gcc_assert (save.is_empty ());
7969 break;
7970 }
7971 if (t == integer_zero_node)
7972 t = save.pop ();
7973 tree &v = omp_private_member_map->get_or_insert (t);
7974 v = save.pop ();
7975 }
7976 omp_private_member_vec.safe_push (t);
7977 if (n != t)
7978 omp_private_member_vec.safe_push (n);
7979 }
7980 save.release ();
7981 }
7982
7983 /* For all variables in the tree_list VARS, mark them as thread local. */
7984
7985 void
7986 finish_omp_threadprivate (tree vars)
7987 {
7988 tree t;
7989
7990 /* Mark every variable in VARS to be assigned thread local storage. */
7991 for (t = vars; t; t = TREE_CHAIN (t))
7992 {
7993 tree v = TREE_PURPOSE (t);
7994
7995 if (error_operand_p (v))
7996 ;
7997 else if (!VAR_P (v))
7998 error ("%<threadprivate%> %qD is not file, namespace "
7999 "or block scope variable", v);
8000 /* If V had already been marked threadprivate, it doesn't matter
8001 whether it had been used prior to this point. */
8002 else if (TREE_USED (v)
8003 && (DECL_LANG_SPECIFIC (v) == NULL
8004 || !CP_DECL_THREADPRIVATE_P (v)))
8005 error ("%qE declared %<threadprivate%> after first use", v);
8006 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8007 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8008 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
8009 error ("%<threadprivate%> %qE has incomplete type", v);
8010 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
8011 && CP_DECL_CONTEXT (v) != current_class_type)
8012 error ("%<threadprivate%> %qE directive not "
8013 "in %qT definition", v, CP_DECL_CONTEXT (v));
8014 else
8015 {
8016 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
8017 if (DECL_LANG_SPECIFIC (v) == NULL)
8018 retrofit_lang_decl (v);
8019
8020 if (! CP_DECL_THREAD_LOCAL_P (v))
8021 {
8022 CP_DECL_THREAD_LOCAL_P (v) = true;
8023 set_decl_tls_model (v, decl_default_tls_model (v));
8024 /* If rtl has been already set for this var, call
8025 make_decl_rtl once again, so that encode_section_info
8026 has a chance to look at the new decl flags. */
8027 if (DECL_RTL_SET_P (v))
8028 make_decl_rtl (v);
8029 }
8030 CP_DECL_THREADPRIVATE_P (v) = 1;
8031 }
8032 }
8033 }
8034
8035 /* Build an OpenMP structured block. */
8036
8037 tree
8038 begin_omp_structured_block (void)
8039 {
8040 return do_pushlevel (sk_omp);
8041 }
8042
8043 tree
8044 finish_omp_structured_block (tree block)
8045 {
8046 return do_poplevel (block);
8047 }
8048
8049 /* Similarly, except force the retention of the BLOCK. */
8050
8051 tree
8052 begin_omp_parallel (void)
8053 {
8054 keep_next_level (true);
8055 return begin_omp_structured_block ();
8056 }
8057
8058 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
8059 statement. */
8060
8061 tree
8062 finish_oacc_data (tree clauses, tree block)
8063 {
8064 tree stmt;
8065
8066 block = finish_omp_structured_block (block);
8067
8068 stmt = make_node (OACC_DATA);
8069 TREE_TYPE (stmt) = void_type_node;
8070 OACC_DATA_CLAUSES (stmt) = clauses;
8071 OACC_DATA_BODY (stmt) = block;
8072
8073 return add_stmt (stmt);
8074 }
8075
8076 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8077 statement. */
8078
8079 tree
8080 finish_oacc_host_data (tree clauses, tree block)
8081 {
8082 tree stmt;
8083
8084 block = finish_omp_structured_block (block);
8085
8086 stmt = make_node (OACC_HOST_DATA);
8087 TREE_TYPE (stmt) = void_type_node;
8088 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
8089 OACC_HOST_DATA_BODY (stmt) = block;
8090
8091 return add_stmt (stmt);
8092 }
8093
8094 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
8095 statement. */
8096
8097 tree
8098 finish_omp_construct (enum tree_code code, tree body, tree clauses)
8099 {
8100 body = finish_omp_structured_block (body);
8101
8102 tree stmt = make_node (code);
8103 TREE_TYPE (stmt) = void_type_node;
8104 OMP_BODY (stmt) = body;
8105 OMP_CLAUSES (stmt) = clauses;
8106
8107 return add_stmt (stmt);
8108 }
8109
8110 tree
8111 finish_omp_parallel (tree clauses, tree body)
8112 {
8113 tree stmt;
8114
8115 body = finish_omp_structured_block (body);
8116
8117 stmt = make_node (OMP_PARALLEL);
8118 TREE_TYPE (stmt) = void_type_node;
8119 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8120 OMP_PARALLEL_BODY (stmt) = body;
8121
8122 return add_stmt (stmt);
8123 }
8124
8125 tree
8126 begin_omp_task (void)
8127 {
8128 keep_next_level (true);
8129 return begin_omp_structured_block ();
8130 }
8131
8132 tree
8133 finish_omp_task (tree clauses, tree body)
8134 {
8135 tree stmt;
8136
8137 body = finish_omp_structured_block (body);
8138
8139 stmt = make_node (OMP_TASK);
8140 TREE_TYPE (stmt) = void_type_node;
8141 OMP_TASK_CLAUSES (stmt) = clauses;
8142 OMP_TASK_BODY (stmt) = body;
8143
8144 return add_stmt (stmt);
8145 }
8146
8147 /* Helper function for finish_omp_for. Convert Ith random access iterator
8148 into integral iterator. Return FALSE if successful. */
8149
8150 static bool
8151 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
8152 tree declv, tree orig_declv, tree initv,
8153 tree condv, tree incrv, tree *body,
8154 tree *pre_body, tree &clauses,
8155 int collapse, int ordered)
8156 {
8157 tree diff, iter_init, iter_incr = NULL, last;
8158 tree incr_var = NULL, orig_pre_body, orig_body, c;
8159 tree decl = TREE_VEC_ELT (declv, i);
8160 tree init = TREE_VEC_ELT (initv, i);
8161 tree cond = TREE_VEC_ELT (condv, i);
8162 tree incr = TREE_VEC_ELT (incrv, i);
8163 tree iter = decl;
8164 location_t elocus = locus;
8165
8166 if (init && EXPR_HAS_LOCATION (init))
8167 elocus = EXPR_LOCATION (init);
8168
8169 cond = cp_fully_fold (cond);
8170 switch (TREE_CODE (cond))
8171 {
8172 case GT_EXPR:
8173 case GE_EXPR:
8174 case LT_EXPR:
8175 case LE_EXPR:
8176 case NE_EXPR:
8177 if (TREE_OPERAND (cond, 1) == iter)
8178 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
8179 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
8180 if (TREE_OPERAND (cond, 0) != iter)
8181 cond = error_mark_node;
8182 else
8183 {
8184 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
8185 TREE_CODE (cond),
8186 iter, ERROR_MARK,
8187 TREE_OPERAND (cond, 1), ERROR_MARK,
8188 NULL, tf_warning_or_error);
8189 if (error_operand_p (tem))
8190 return true;
8191 }
8192 break;
8193 default:
8194 cond = error_mark_node;
8195 break;
8196 }
8197 if (cond == error_mark_node)
8198 {
8199 error_at (elocus, "invalid controlling predicate");
8200 return true;
8201 }
8202 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
8203 ERROR_MARK, iter, ERROR_MARK, NULL,
8204 tf_warning_or_error);
8205 diff = cp_fully_fold (diff);
8206 if (error_operand_p (diff))
8207 return true;
8208 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
8209 {
8210 error_at (elocus, "difference between %qE and %qD does not have integer type",
8211 TREE_OPERAND (cond, 1), iter);
8212 return true;
8213 }
8214 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
8215 TREE_VEC_ELT (declv, i), NULL_TREE,
8216 cond, cp_walk_subtrees))
8217 return true;
8218
8219 switch (TREE_CODE (incr))
8220 {
8221 case PREINCREMENT_EXPR:
8222 case PREDECREMENT_EXPR:
8223 case POSTINCREMENT_EXPR:
8224 case POSTDECREMENT_EXPR:
8225 if (TREE_OPERAND (incr, 0) != iter)
8226 {
8227 incr = error_mark_node;
8228 break;
8229 }
8230 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
8231 TREE_CODE (incr), iter,
8232 tf_warning_or_error);
8233 if (error_operand_p (iter_incr))
8234 return true;
8235 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
8236 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
8237 incr = integer_one_node;
8238 else
8239 incr = integer_minus_one_node;
8240 break;
8241 case MODIFY_EXPR:
8242 if (TREE_OPERAND (incr, 0) != iter)
8243 incr = error_mark_node;
8244 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
8245 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
8246 {
8247 tree rhs = TREE_OPERAND (incr, 1);
8248 if (TREE_OPERAND (rhs, 0) == iter)
8249 {
8250 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
8251 != INTEGER_TYPE)
8252 incr = error_mark_node;
8253 else
8254 {
8255 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8256 iter, TREE_CODE (rhs),
8257 TREE_OPERAND (rhs, 1),
8258 tf_warning_or_error);
8259 if (error_operand_p (iter_incr))
8260 return true;
8261 incr = TREE_OPERAND (rhs, 1);
8262 incr = cp_convert (TREE_TYPE (diff), incr,
8263 tf_warning_or_error);
8264 if (TREE_CODE (rhs) == MINUS_EXPR)
8265 {
8266 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
8267 incr = fold_simple (incr);
8268 }
8269 if (TREE_CODE (incr) != INTEGER_CST
8270 && (TREE_CODE (incr) != NOP_EXPR
8271 || (TREE_CODE (TREE_OPERAND (incr, 0))
8272 != INTEGER_CST)))
8273 iter_incr = NULL;
8274 }
8275 }
8276 else if (TREE_OPERAND (rhs, 1) == iter)
8277 {
8278 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
8279 || TREE_CODE (rhs) != PLUS_EXPR)
8280 incr = error_mark_node;
8281 else
8282 {
8283 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
8284 PLUS_EXPR,
8285 TREE_OPERAND (rhs, 0),
8286 ERROR_MARK, iter,
8287 ERROR_MARK, NULL,
8288 tf_warning_or_error);
8289 if (error_operand_p (iter_incr))
8290 return true;
8291 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8292 iter, NOP_EXPR,
8293 iter_incr,
8294 tf_warning_or_error);
8295 if (error_operand_p (iter_incr))
8296 return true;
8297 incr = TREE_OPERAND (rhs, 0);
8298 iter_incr = NULL;
8299 }
8300 }
8301 else
8302 incr = error_mark_node;
8303 }
8304 else
8305 incr = error_mark_node;
8306 break;
8307 default:
8308 incr = error_mark_node;
8309 break;
8310 }
8311
8312 if (incr == error_mark_node)
8313 {
8314 error_at (elocus, "invalid increment expression");
8315 return true;
8316 }
8317
8318 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
8319 incr = cp_fully_fold (incr);
8320 bool taskloop_iv_seen = false;
8321 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8322 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8323 && OMP_CLAUSE_DECL (c) == iter)
8324 {
8325 if (code == OMP_TASKLOOP)
8326 {
8327 taskloop_iv_seen = true;
8328 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
8329 }
8330 break;
8331 }
8332 else if (code == OMP_TASKLOOP
8333 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8334 && OMP_CLAUSE_DECL (c) == iter)
8335 {
8336 taskloop_iv_seen = true;
8337 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
8338 }
8339
8340 decl = create_temporary_var (TREE_TYPE (diff));
8341 pushdecl (decl);
8342 add_decl_expr (decl);
8343 last = create_temporary_var (TREE_TYPE (diff));
8344 pushdecl (last);
8345 add_decl_expr (last);
8346 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
8347 && (!ordered || (i < collapse && collapse > 1)))
8348 {
8349 incr_var = create_temporary_var (TREE_TYPE (diff));
8350 pushdecl (incr_var);
8351 add_decl_expr (incr_var);
8352 }
8353 gcc_assert (stmts_are_full_exprs_p ());
8354 tree diffvar = NULL_TREE;
8355 if (code == OMP_TASKLOOP)
8356 {
8357 if (!taskloop_iv_seen)
8358 {
8359 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8360 OMP_CLAUSE_DECL (ivc) = iter;
8361 cxx_omp_finish_clause (ivc, NULL);
8362 OMP_CLAUSE_CHAIN (ivc) = clauses;
8363 clauses = ivc;
8364 }
8365 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8366 OMP_CLAUSE_DECL (lvc) = last;
8367 OMP_CLAUSE_CHAIN (lvc) = clauses;
8368 clauses = lvc;
8369 diffvar = create_temporary_var (TREE_TYPE (diff));
8370 pushdecl (diffvar);
8371 add_decl_expr (diffvar);
8372 }
8373
8374 orig_pre_body = *pre_body;
8375 *pre_body = push_stmt_list ();
8376 if (orig_pre_body)
8377 add_stmt (orig_pre_body);
8378 if (init != NULL)
8379 finish_expr_stmt (build_x_modify_expr (elocus,
8380 iter, NOP_EXPR, init,
8381 tf_warning_or_error));
8382 init = build_int_cst (TREE_TYPE (diff), 0);
8383 if (c && iter_incr == NULL
8384 && (!ordered || (i < collapse && collapse > 1)))
8385 {
8386 if (incr_var)
8387 {
8388 finish_expr_stmt (build_x_modify_expr (elocus,
8389 incr_var, NOP_EXPR,
8390 incr, tf_warning_or_error));
8391 incr = incr_var;
8392 }
8393 iter_incr = build_x_modify_expr (elocus,
8394 iter, PLUS_EXPR, incr,
8395 tf_warning_or_error);
8396 }
8397 if (c && ordered && i < collapse && collapse > 1)
8398 iter_incr = incr;
8399 finish_expr_stmt (build_x_modify_expr (elocus,
8400 last, NOP_EXPR, init,
8401 tf_warning_or_error));
8402 if (diffvar)
8403 {
8404 finish_expr_stmt (build_x_modify_expr (elocus,
8405 diffvar, NOP_EXPR,
8406 diff, tf_warning_or_error));
8407 diff = diffvar;
8408 }
8409 *pre_body = pop_stmt_list (*pre_body);
8410
8411 cond = cp_build_binary_op (elocus,
8412 TREE_CODE (cond), decl, diff,
8413 tf_warning_or_error);
8414 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
8415 elocus, incr, NULL_TREE);
8416
8417 orig_body = *body;
8418 *body = push_stmt_list ();
8419 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
8420 iter_init = build_x_modify_expr (elocus,
8421 iter, PLUS_EXPR, iter_init,
8422 tf_warning_or_error);
8423 if (iter_init != error_mark_node)
8424 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8425 finish_expr_stmt (iter_init);
8426 finish_expr_stmt (build_x_modify_expr (elocus,
8427 last, NOP_EXPR, decl,
8428 tf_warning_or_error));
8429 add_stmt (orig_body);
8430 *body = pop_stmt_list (*body);
8431
8432 if (c)
8433 {
8434 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
8435 if (!ordered)
8436 finish_expr_stmt (iter_incr);
8437 else
8438 {
8439 iter_init = decl;
8440 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
8441 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
8442 iter_init, iter_incr);
8443 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
8444 iter_init = build_x_modify_expr (elocus,
8445 iter, PLUS_EXPR, iter_init,
8446 tf_warning_or_error);
8447 if (iter_init != error_mark_node)
8448 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8449 finish_expr_stmt (iter_init);
8450 }
8451 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8452 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8453 }
8454
8455 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
8456 {
8457 tree t = TREE_VEC_ELT (orig_declv, i);
8458 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8459 && TREE_VALUE (t) == NULL_TREE
8460 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
8461 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
8462 TREE_VALUE (t) = last;
8463 }
8464 else
8465 TREE_VEC_ELT (orig_declv, i)
8466 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
8467 TREE_VEC_ELT (declv, i) = decl;
8468 TREE_VEC_ELT (initv, i) = init;
8469 TREE_VEC_ELT (condv, i) = cond;
8470 TREE_VEC_ELT (incrv, i) = incr;
8471
8472 return false;
8473 }
8474
8475 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8476 are directly for their associated operands in the statement. DECL
8477 and INIT are a combo; if DECL is NULL then INIT ought to be a
8478 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8479 optional statements that need to go before the loop into its
8480 sk_omp scope. */
8481
8482 tree
8483 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8484 tree orig_declv, tree initv, tree condv, tree incrv,
8485 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8486 {
8487 tree omp_for = NULL, orig_incr = NULL;
8488 tree decl = NULL, init, cond, incr;
8489 location_t elocus;
8490 int i;
8491 int collapse = 1;
8492 int ordered = 0;
8493
8494 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8495 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8496 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8497 if (TREE_VEC_LENGTH (declv) > 1)
8498 {
8499 tree c;
8500
8501 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8502 if (c)
8503 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8504 else
8505 {
8506 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8507 if (c)
8508 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8509 if (collapse != TREE_VEC_LENGTH (declv))
8510 ordered = TREE_VEC_LENGTH (declv);
8511 }
8512 }
8513 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8514 {
8515 decl = TREE_VEC_ELT (declv, i);
8516 init = TREE_VEC_ELT (initv, i);
8517 cond = TREE_VEC_ELT (condv, i);
8518 incr = TREE_VEC_ELT (incrv, i);
8519 elocus = locus;
8520
8521 if (decl == NULL)
8522 {
8523 if (init != NULL)
8524 switch (TREE_CODE (init))
8525 {
8526 case MODIFY_EXPR:
8527 decl = TREE_OPERAND (init, 0);
8528 init = TREE_OPERAND (init, 1);
8529 break;
8530 case MODOP_EXPR:
8531 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8532 {
8533 decl = TREE_OPERAND (init, 0);
8534 init = TREE_OPERAND (init, 2);
8535 }
8536 break;
8537 default:
8538 break;
8539 }
8540
8541 if (decl == NULL)
8542 {
8543 error_at (locus,
8544 "expected iteration declaration or initialization");
8545 return NULL;
8546 }
8547 }
8548
8549 if (init && EXPR_HAS_LOCATION (init))
8550 elocus = EXPR_LOCATION (init);
8551
8552 if (cond == global_namespace)
8553 continue;
8554
8555 if (cond == NULL)
8556 {
8557 error_at (elocus, "missing controlling predicate");
8558 return NULL;
8559 }
8560
8561 if (incr == NULL)
8562 {
8563 error_at (elocus, "missing increment expression");
8564 return NULL;
8565 }
8566
8567 TREE_VEC_ELT (declv, i) = decl;
8568 TREE_VEC_ELT (initv, i) = init;
8569 }
8570
8571 if (orig_inits)
8572 {
8573 bool fail = false;
8574 tree orig_init;
8575 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8576 if (orig_init
8577 && !c_omp_check_loop_iv_exprs (locus, orig_declv
8578 ? orig_declv : declv,
8579 TREE_VEC_ELT (declv, i), orig_init,
8580 NULL_TREE, cp_walk_subtrees))
8581 fail = true;
8582 if (fail)
8583 return NULL;
8584 }
8585
8586 if (dependent_omp_for_p (declv, initv, condv, incrv))
8587 {
8588 tree stmt;
8589
8590 stmt = make_node (code);
8591
8592 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8593 {
8594 /* This is really just a place-holder. We'll be decomposing this
8595 again and going through the cp_build_modify_expr path below when
8596 we instantiate the thing. */
8597 TREE_VEC_ELT (initv, i)
8598 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8599 TREE_VEC_ELT (initv, i));
8600 }
8601
8602 TREE_TYPE (stmt) = void_type_node;
8603 OMP_FOR_INIT (stmt) = initv;
8604 OMP_FOR_COND (stmt) = condv;
8605 OMP_FOR_INCR (stmt) = incrv;
8606 OMP_FOR_BODY (stmt) = body;
8607 OMP_FOR_PRE_BODY (stmt) = pre_body;
8608 OMP_FOR_CLAUSES (stmt) = clauses;
8609
8610 SET_EXPR_LOCATION (stmt, locus);
8611 return add_stmt (stmt);
8612 }
8613
8614 if (!orig_declv)
8615 orig_declv = copy_node (declv);
8616
8617 if (processing_template_decl)
8618 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8619
8620 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8621 {
8622 decl = TREE_VEC_ELT (declv, i);
8623 init = TREE_VEC_ELT (initv, i);
8624 cond = TREE_VEC_ELT (condv, i);
8625 incr = TREE_VEC_ELT (incrv, i);
8626 if (orig_incr)
8627 TREE_VEC_ELT (orig_incr, i) = incr;
8628 elocus = locus;
8629
8630 if (init && EXPR_HAS_LOCATION (init))
8631 elocus = EXPR_LOCATION (init);
8632
8633 if (!DECL_P (decl))
8634 {
8635 error_at (elocus, "expected iteration declaration or initialization");
8636 return NULL;
8637 }
8638
8639 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8640 {
8641 if (orig_incr)
8642 TREE_VEC_ELT (orig_incr, i) = incr;
8643 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8644 TREE_CODE (TREE_OPERAND (incr, 1)),
8645 TREE_OPERAND (incr, 2),
8646 tf_warning_or_error);
8647 }
8648
8649 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8650 {
8651 if (code == OMP_SIMD)
8652 {
8653 error_at (elocus, "%<#pragma omp simd%> used with class "
8654 "iteration variable %qE", decl);
8655 return NULL;
8656 }
8657 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8658 initv, condv, incrv, &body,
8659 &pre_body, clauses,
8660 collapse, ordered))
8661 return NULL;
8662 continue;
8663 }
8664
8665 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8666 && !TYPE_PTR_P (TREE_TYPE (decl)))
8667 {
8668 error_at (elocus, "invalid type for iteration variable %qE", decl);
8669 return NULL;
8670 }
8671
8672 if (!processing_template_decl)
8673 {
8674 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8675 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8676 tf_warning_or_error);
8677 }
8678 else
8679 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8680 if (cond
8681 && TREE_SIDE_EFFECTS (cond)
8682 && COMPARISON_CLASS_P (cond)
8683 && !processing_template_decl)
8684 {
8685 tree t = TREE_OPERAND (cond, 0);
8686 if (TREE_SIDE_EFFECTS (t)
8687 && t != decl
8688 && (TREE_CODE (t) != NOP_EXPR
8689 || TREE_OPERAND (t, 0) != decl))
8690 TREE_OPERAND (cond, 0)
8691 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8692
8693 t = TREE_OPERAND (cond, 1);
8694 if (TREE_SIDE_EFFECTS (t)
8695 && t != decl
8696 && (TREE_CODE (t) != NOP_EXPR
8697 || TREE_OPERAND (t, 0) != decl))
8698 TREE_OPERAND (cond, 1)
8699 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8700 }
8701 if (decl == error_mark_node || init == error_mark_node)
8702 return NULL;
8703
8704 TREE_VEC_ELT (declv, i) = decl;
8705 TREE_VEC_ELT (initv, i) = init;
8706 TREE_VEC_ELT (condv, i) = cond;
8707 TREE_VEC_ELT (incrv, i) = incr;
8708 i++;
8709 }
8710
8711 if (pre_body && IS_EMPTY_STMT (pre_body))
8712 pre_body = NULL;
8713
8714 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8715 incrv, body, pre_body,
8716 !processing_template_decl);
8717
8718 /* Check for iterators appearing in lb, b or incr expressions. */
8719 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8720 omp_for = NULL_TREE;
8721
8722 if (omp_for == NULL)
8723 {
8724 return NULL;
8725 }
8726
8727 add_stmt (omp_for);
8728
8729 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8730 {
8731 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8732 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8733
8734 if (TREE_CODE (incr) != MODIFY_EXPR)
8735 continue;
8736
8737 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8738 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8739 && !processing_template_decl)
8740 {
8741 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8742 if (TREE_SIDE_EFFECTS (t)
8743 && t != decl
8744 && (TREE_CODE (t) != NOP_EXPR
8745 || TREE_OPERAND (t, 0) != decl))
8746 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8747 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8748
8749 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8750 if (TREE_SIDE_EFFECTS (t)
8751 && t != decl
8752 && (TREE_CODE (t) != NOP_EXPR
8753 || TREE_OPERAND (t, 0) != decl))
8754 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8755 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8756 }
8757
8758 if (orig_incr)
8759 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8760 }
8761 OMP_FOR_CLAUSES (omp_for) = clauses;
8762
8763 /* For simd loops with non-static data member iterators, we could have added
8764 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8765 step at this point, fill it in. */
8766 if (code == OMP_SIMD && !processing_template_decl
8767 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8768 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8769 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8770 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8771 {
8772 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8773 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8774 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8775 tree step, stept;
8776 switch (TREE_CODE (incr))
8777 {
8778 case PREINCREMENT_EXPR:
8779 case POSTINCREMENT_EXPR:
8780 /* c_omp_for_incr_canonicalize_ptr() should have been
8781 called to massage things appropriately. */
8782 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8783 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8784 break;
8785 case PREDECREMENT_EXPR:
8786 case POSTDECREMENT_EXPR:
8787 /* c_omp_for_incr_canonicalize_ptr() should have been
8788 called to massage things appropriately. */
8789 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8790 OMP_CLAUSE_LINEAR_STEP (c)
8791 = build_int_cst (TREE_TYPE (decl), -1);
8792 break;
8793 case MODIFY_EXPR:
8794 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8795 incr = TREE_OPERAND (incr, 1);
8796 switch (TREE_CODE (incr))
8797 {
8798 case PLUS_EXPR:
8799 if (TREE_OPERAND (incr, 1) == decl)
8800 step = TREE_OPERAND (incr, 0);
8801 else
8802 step = TREE_OPERAND (incr, 1);
8803 break;
8804 case MINUS_EXPR:
8805 case POINTER_PLUS_EXPR:
8806 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8807 step = TREE_OPERAND (incr, 1);
8808 break;
8809 default:
8810 gcc_unreachable ();
8811 }
8812 stept = TREE_TYPE (decl);
8813 if (INDIRECT_TYPE_P (stept))
8814 stept = sizetype;
8815 step = fold_convert (stept, step);
8816 if (TREE_CODE (incr) == MINUS_EXPR)
8817 step = fold_build1 (NEGATE_EXPR, stept, step);
8818 OMP_CLAUSE_LINEAR_STEP (c) = step;
8819 break;
8820 default:
8821 gcc_unreachable ();
8822 }
8823 }
8824
8825 return omp_for;
8826 }
8827
8828 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
8829 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
8830
8831 tree
8832 finish_omp_for_block (tree bind, tree omp_for)
8833 {
8834 if (omp_for == NULL_TREE
8835 || !OMP_FOR_ORIG_DECLS (omp_for)
8836 || bind == NULL_TREE
8837 || TREE_CODE (bind) != BIND_EXPR)
8838 return bind;
8839 tree b = NULL_TREE;
8840 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
8841 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
8842 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
8843 {
8844 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
8845 gcc_assert (BIND_EXPR_BLOCK (bind)
8846 && (BIND_EXPR_VARS (bind)
8847 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
8848 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
8849 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
8850 {
8851 if (*p == TREE_VEC_ELT (v, j))
8852 {
8853 tree var = *p;
8854 *p = DECL_CHAIN (*p);
8855 if (b == NULL_TREE)
8856 {
8857 b = make_node (BLOCK);
8858 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
8859 OMP_FOR_BODY (omp_for), b);
8860 TREE_SIDE_EFFECTS (b) = 1;
8861 OMP_FOR_BODY (omp_for) = b;
8862 }
8863 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
8864 BIND_EXPR_VARS (b) = var;
8865 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
8866 }
8867 }
8868 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
8869 }
8870 return bind;
8871 }
8872
8873 void
8874 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
8875 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
8876 tree clauses, enum omp_memory_order mo)
8877 {
8878 tree orig_lhs;
8879 tree orig_rhs;
8880 tree orig_v;
8881 tree orig_lhs1;
8882 tree orig_rhs1;
8883 bool dependent_p;
8884 tree stmt;
8885
8886 orig_lhs = lhs;
8887 orig_rhs = rhs;
8888 orig_v = v;
8889 orig_lhs1 = lhs1;
8890 orig_rhs1 = rhs1;
8891 dependent_p = false;
8892 stmt = NULL_TREE;
8893
8894 /* Even in a template, we can detect invalid uses of the atomic
8895 pragma if neither LHS nor RHS is type-dependent. */
8896 if (processing_template_decl)
8897 {
8898 dependent_p = (type_dependent_expression_p (lhs)
8899 || (rhs && type_dependent_expression_p (rhs))
8900 || (v && type_dependent_expression_p (v))
8901 || (lhs1 && type_dependent_expression_p (lhs1))
8902 || (rhs1 && type_dependent_expression_p (rhs1)));
8903 if (clauses)
8904 {
8905 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
8906 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
8907 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
8908 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
8909 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
8910 dependent_p = true;
8911 }
8912 if (!dependent_p)
8913 {
8914 lhs = build_non_dependent_expr (lhs);
8915 if (rhs)
8916 rhs = build_non_dependent_expr (rhs);
8917 if (v)
8918 v = build_non_dependent_expr (v);
8919 if (lhs1)
8920 lhs1 = build_non_dependent_expr (lhs1);
8921 if (rhs1)
8922 rhs1 = build_non_dependent_expr (rhs1);
8923 }
8924 }
8925 if (!dependent_p)
8926 {
8927 bool swapped = false;
8928 if (rhs1 && cp_tree_equal (lhs, rhs))
8929 {
8930 std::swap (rhs, rhs1);
8931 swapped = !commutative_tree_code (opcode);
8932 }
8933 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8934 {
8935 if (code == OMP_ATOMIC)
8936 error ("%<#pragma omp atomic update%> uses two different "
8937 "expressions for memory");
8938 else
8939 error ("%<#pragma omp atomic capture%> uses two different "
8940 "expressions for memory");
8941 return;
8942 }
8943 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8944 {
8945 if (code == OMP_ATOMIC)
8946 error ("%<#pragma omp atomic update%> uses two different "
8947 "expressions for memory");
8948 else
8949 error ("%<#pragma omp atomic capture%> uses two different "
8950 "expressions for memory");
8951 return;
8952 }
8953 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
8954 v, lhs1, rhs1, swapped, mo,
8955 processing_template_decl != 0);
8956 if (stmt == error_mark_node)
8957 return;
8958 }
8959 if (processing_template_decl)
8960 {
8961 if (code == OMP_ATOMIC_READ)
8962 {
8963 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
8964 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8965 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8966 }
8967 else
8968 {
8969 if (opcode == NOP_EXPR)
8970 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8971 else
8972 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8973 if (orig_rhs1)
8974 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8975 COMPOUND_EXPR, orig_rhs1, stmt);
8976 if (code != OMP_ATOMIC)
8977 {
8978 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
8979 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8980 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8981 }
8982 }
8983 stmt = build2 (OMP_ATOMIC, void_type_node,
8984 clauses ? clauses : integer_zero_node, stmt);
8985 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8986 SET_EXPR_LOCATION (stmt, loc);
8987 }
8988 finish_expr_stmt (stmt);
8989 }
8990
8991 void
8992 finish_omp_barrier (void)
8993 {
8994 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8995 vec<tree, va_gc> *vec = make_tree_vector ();
8996 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8997 release_tree_vector (vec);
8998 finish_expr_stmt (stmt);
8999 }
9000
9001 void
9002 finish_omp_depobj (location_t loc, tree depobj,
9003 enum omp_clause_depend_kind kind, tree clause)
9004 {
9005 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
9006 {
9007 if (!lvalue_p (depobj))
9008 {
9009 error_at (EXPR_LOC_OR_LOC (depobj, loc),
9010 "%<depobj%> expression is not lvalue expression");
9011 depobj = error_mark_node;
9012 }
9013 }
9014
9015 if (processing_template_decl)
9016 {
9017 if (clause == NULL_TREE)
9018 clause = build_int_cst (integer_type_node, kind);
9019 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
9020 return;
9021 }
9022
9023 if (!error_operand_p (depobj))
9024 {
9025 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
9026 if (addr == error_mark_node)
9027 depobj = error_mark_node;
9028 else
9029 depobj = cp_build_indirect_ref (addr, RO_UNARY_STAR,
9030 tf_warning_or_error);
9031 }
9032
9033 c_finish_omp_depobj (loc, depobj, kind, clause);
9034 }
9035
9036 void
9037 finish_omp_flush (int mo)
9038 {
9039 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
9040 vec<tree, va_gc> *vec = make_tree_vector ();
9041 if (mo != MEMMODEL_LAST)
9042 {
9043 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
9044 vec->quick_push (build_int_cst (integer_type_node, mo));
9045 }
9046 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9047 release_tree_vector (vec);
9048 finish_expr_stmt (stmt);
9049 }
9050
9051 void
9052 finish_omp_taskwait (void)
9053 {
9054 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
9055 vec<tree, va_gc> *vec = make_tree_vector ();
9056 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9057 release_tree_vector (vec);
9058 finish_expr_stmt (stmt);
9059 }
9060
9061 void
9062 finish_omp_taskyield (void)
9063 {
9064 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
9065 vec<tree, va_gc> *vec = make_tree_vector ();
9066 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9067 release_tree_vector (vec);
9068 finish_expr_stmt (stmt);
9069 }
9070
9071 void
9072 finish_omp_cancel (tree clauses)
9073 {
9074 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
9075 int mask = 0;
9076 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9077 mask = 1;
9078 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9079 mask = 2;
9080 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9081 mask = 4;
9082 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9083 mask = 8;
9084 else
9085 {
9086 error ("%<#pragma omp cancel%> must specify one of "
9087 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9088 return;
9089 }
9090 vec<tree, va_gc> *vec = make_tree_vector ();
9091 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
9092 if (ifc != NULL_TREE)
9093 {
9094 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
9095 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
9096 error_at (OMP_CLAUSE_LOCATION (ifc),
9097 "expected %<cancel%> %<if%> clause modifier");
9098 else
9099 {
9100 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
9101 if (ifc2 != NULL_TREE)
9102 {
9103 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
9104 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
9105 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
9106 error_at (OMP_CLAUSE_LOCATION (ifc2),
9107 "expected %<cancel%> %<if%> clause modifier");
9108 }
9109 }
9110
9111 if (!processing_template_decl)
9112 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
9113 else
9114 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
9115 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
9116 integer_zero_node, ERROR_MARK,
9117 NULL, tf_warning_or_error);
9118 }
9119 else
9120 ifc = boolean_true_node;
9121 vec->quick_push (build_int_cst (integer_type_node, mask));
9122 vec->quick_push (ifc);
9123 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9124 release_tree_vector (vec);
9125 finish_expr_stmt (stmt);
9126 }
9127
9128 void
9129 finish_omp_cancellation_point (tree clauses)
9130 {
9131 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
9132 int mask = 0;
9133 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9134 mask = 1;
9135 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9136 mask = 2;
9137 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9138 mask = 4;
9139 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9140 mask = 8;
9141 else
9142 {
9143 error ("%<#pragma omp cancellation point%> must specify one of "
9144 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9145 return;
9146 }
9147 vec<tree, va_gc> *vec
9148 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
9149 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9150 release_tree_vector (vec);
9151 finish_expr_stmt (stmt);
9152 }
9153 \f
9154 /* Begin a __transaction_atomic or __transaction_relaxed statement.
9155 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
9156 should create an extra compound stmt. */
9157
9158 tree
9159 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
9160 {
9161 tree r;
9162
9163 if (pcompound)
9164 *pcompound = begin_compound_stmt (0);
9165
9166 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
9167
9168 /* Only add the statement to the function if support enabled. */
9169 if (flag_tm)
9170 add_stmt (r);
9171 else
9172 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
9173 ? G_("%<__transaction_relaxed%> without "
9174 "transactional memory support enabled")
9175 : G_("%<__transaction_atomic%> without "
9176 "transactional memory support enabled")));
9177
9178 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
9179 TREE_SIDE_EFFECTS (r) = 1;
9180 return r;
9181 }
9182
9183 /* End a __transaction_atomic or __transaction_relaxed statement.
9184 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
9185 and we should end the compound. If NOEX is non-NULL, we wrap the body in
9186 a MUST_NOT_THROW_EXPR with NOEX as condition. */
9187
9188 void
9189 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
9190 {
9191 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
9192 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
9193 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
9194 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
9195
9196 /* noexcept specifications are not allowed for function transactions. */
9197 gcc_assert (!(noex && compound_stmt));
9198 if (noex)
9199 {
9200 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
9201 noex);
9202 protected_set_expr_location
9203 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
9204 TREE_SIDE_EFFECTS (body) = 1;
9205 TRANSACTION_EXPR_BODY (stmt) = body;
9206 }
9207
9208 if (compound_stmt)
9209 finish_compound_stmt (compound_stmt);
9210 }
9211
9212 /* Build a __transaction_atomic or __transaction_relaxed expression. If
9213 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
9214 condition. */
9215
9216 tree
9217 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
9218 {
9219 tree ret;
9220 if (noex)
9221 {
9222 expr = build_must_not_throw_expr (expr, noex);
9223 protected_set_expr_location (expr, loc);
9224 TREE_SIDE_EFFECTS (expr) = 1;
9225 }
9226 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
9227 if (flags & TM_STMT_ATTR_RELAXED)
9228 TRANSACTION_EXPR_RELAXED (ret) = 1;
9229 TREE_SIDE_EFFECTS (ret) = 1;
9230 SET_EXPR_LOCATION (ret, loc);
9231 return ret;
9232 }
9233 \f
9234 void
9235 init_cp_semantics (void)
9236 {
9237 }
9238 \f
9239 /* Build a STATIC_ASSERT for a static assertion with the condition
9240 CONDITION and the message text MESSAGE. LOCATION is the location
9241 of the static assertion in the source code. When MEMBER_P, this
9242 static assertion is a member of a class. */
9243 void
9244 finish_static_assert (tree condition, tree message, location_t location,
9245 bool member_p)
9246 {
9247 tsubst_flags_t complain = tf_warning_or_error;
9248
9249 if (message == NULL_TREE
9250 || message == error_mark_node
9251 || condition == NULL_TREE
9252 || condition == error_mark_node)
9253 return;
9254
9255 if (check_for_bare_parameter_packs (condition))
9256 condition = error_mark_node;
9257
9258 if (instantiation_dependent_expression_p (condition))
9259 {
9260 /* We're in a template; build a STATIC_ASSERT and put it in
9261 the right place. */
9262 tree assertion;
9263
9264 assertion = make_node (STATIC_ASSERT);
9265 STATIC_ASSERT_CONDITION (assertion) = condition;
9266 STATIC_ASSERT_MESSAGE (assertion) = message;
9267 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
9268
9269 if (member_p)
9270 maybe_add_class_template_decl_list (current_class_type,
9271 assertion,
9272 /*friend_p=*/0);
9273 else
9274 add_stmt (assertion);
9275
9276 return;
9277 }
9278
9279 /* Fold the expression and convert it to a boolean value. */
9280 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
9281 complain, LOOKUP_NORMAL);
9282 condition = fold_non_dependent_expr (condition, complain,
9283 /*manifestly_const_eval=*/true);
9284
9285 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
9286 /* Do nothing; the condition is satisfied. */
9287 ;
9288 else
9289 {
9290 location_t saved_loc = input_location;
9291
9292 input_location = location;
9293 if (TREE_CODE (condition) == INTEGER_CST
9294 && integer_zerop (condition))
9295 {
9296 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
9297 (TREE_TYPE (TREE_TYPE (message))));
9298 int len = TREE_STRING_LENGTH (message) / sz - 1;
9299 /* Report the error. */
9300 if (len == 0)
9301 error ("static assertion failed");
9302 else
9303 error ("static assertion failed: %s",
9304 TREE_STRING_POINTER (message));
9305 }
9306 else if (condition && condition != error_mark_node)
9307 {
9308 error ("non-constant condition for static assertion");
9309 if (require_rvalue_constant_expression (condition))
9310 cxx_constant_value (condition);
9311 }
9312 input_location = saved_loc;
9313 }
9314 }
9315 \f
9316 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
9317 suitable for use as a type-specifier.
9318
9319 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
9320 id-expression or a class member access, FALSE when it was parsed as
9321 a full expression. */
9322
9323 tree
9324 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
9325 tsubst_flags_t complain)
9326 {
9327 tree type = NULL_TREE;
9328
9329 if (!expr || error_operand_p (expr))
9330 return error_mark_node;
9331
9332 if (TYPE_P (expr)
9333 || TREE_CODE (expr) == TYPE_DECL
9334 || (TREE_CODE (expr) == BIT_NOT_EXPR
9335 && TYPE_P (TREE_OPERAND (expr, 0))))
9336 {
9337 if (complain & tf_error)
9338 error ("argument to decltype must be an expression");
9339 return error_mark_node;
9340 }
9341
9342 /* Depending on the resolution of DR 1172, we may later need to distinguish
9343 instantiation-dependent but not type-dependent expressions so that, say,
9344 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
9345 if (instantiation_dependent_uneval_expression_p (expr))
9346 {
9347 type = cxx_make_type (DECLTYPE_TYPE);
9348 DECLTYPE_TYPE_EXPR (type) = expr;
9349 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
9350 = id_expression_or_member_access_p;
9351 SET_TYPE_STRUCTURAL_EQUALITY (type);
9352
9353 return type;
9354 }
9355
9356 /* The type denoted by decltype(e) is defined as follows: */
9357
9358 expr = resolve_nondeduced_context (expr, complain);
9359
9360 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
9361 return error_mark_node;
9362
9363 if (type_unknown_p (expr))
9364 {
9365 if (complain & tf_error)
9366 error ("decltype cannot resolve address of overloaded function");
9367 return error_mark_node;
9368 }
9369
9370 /* To get the size of a static data member declared as an array of
9371 unknown bound, we need to instantiate it. */
9372 if (VAR_P (expr)
9373 && VAR_HAD_UNKNOWN_BOUND (expr)
9374 && DECL_TEMPLATE_INSTANTIATION (expr))
9375 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
9376
9377 if (id_expression_or_member_access_p)
9378 {
9379 /* If e is an id-expression or a class member access (5.2.5
9380 [expr.ref]), decltype(e) is defined as the type of the entity
9381 named by e. If there is no such entity, or e names a set of
9382 overloaded functions, the program is ill-formed. */
9383 if (identifier_p (expr))
9384 expr = lookup_name (expr);
9385
9386 if (INDIRECT_REF_P (expr)
9387 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
9388 /* This can happen when the expression is, e.g., "a.b". Just
9389 look at the underlying operand. */
9390 expr = TREE_OPERAND (expr, 0);
9391
9392 if (TREE_CODE (expr) == OFFSET_REF
9393 || TREE_CODE (expr) == MEMBER_REF
9394 || TREE_CODE (expr) == SCOPE_REF)
9395 /* We're only interested in the field itself. If it is a
9396 BASELINK, we will need to see through it in the next
9397 step. */
9398 expr = TREE_OPERAND (expr, 1);
9399
9400 if (BASELINK_P (expr))
9401 /* See through BASELINK nodes to the underlying function. */
9402 expr = BASELINK_FUNCTIONS (expr);
9403
9404 /* decltype of a decomposition name drops references in the tuple case
9405 (unlike decltype of a normal variable) and keeps cv-qualifiers from
9406 the containing object in the other cases (unlike decltype of a member
9407 access expression). */
9408 if (DECL_DECOMPOSITION_P (expr))
9409 {
9410 if (DECL_HAS_VALUE_EXPR_P (expr))
9411 /* Expr is an array or struct subobject proxy, handle
9412 bit-fields properly. */
9413 return unlowered_expr_type (expr);
9414 else
9415 /* Expr is a reference variable for the tuple case. */
9416 return lookup_decomp_type (expr);
9417 }
9418
9419 switch (TREE_CODE (expr))
9420 {
9421 case FIELD_DECL:
9422 if (DECL_BIT_FIELD_TYPE (expr))
9423 {
9424 type = DECL_BIT_FIELD_TYPE (expr);
9425 break;
9426 }
9427 /* Fall through for fields that aren't bitfields. */
9428 gcc_fallthrough ();
9429
9430 case FUNCTION_DECL:
9431 case VAR_DECL:
9432 case CONST_DECL:
9433 case PARM_DECL:
9434 case RESULT_DECL:
9435 case TEMPLATE_PARM_INDEX:
9436 expr = mark_type_use (expr);
9437 type = TREE_TYPE (expr);
9438 break;
9439
9440 case ERROR_MARK:
9441 type = error_mark_node;
9442 break;
9443
9444 case COMPONENT_REF:
9445 case COMPOUND_EXPR:
9446 mark_type_use (expr);
9447 type = is_bitfield_expr_with_lowered_type (expr);
9448 if (!type)
9449 type = TREE_TYPE (TREE_OPERAND (expr, 1));
9450 break;
9451
9452 case BIT_FIELD_REF:
9453 gcc_unreachable ();
9454
9455 case INTEGER_CST:
9456 case PTRMEM_CST:
9457 /* We can get here when the id-expression refers to an
9458 enumerator or non-type template parameter. */
9459 type = TREE_TYPE (expr);
9460 break;
9461
9462 default:
9463 /* Handle instantiated template non-type arguments. */
9464 type = TREE_TYPE (expr);
9465 break;
9466 }
9467 }
9468 else
9469 {
9470 /* Within a lambda-expression:
9471
9472 Every occurrence of decltype((x)) where x is a possibly
9473 parenthesized id-expression that names an entity of
9474 automatic storage duration is treated as if x were
9475 transformed into an access to a corresponding data member
9476 of the closure type that would have been declared if x
9477 were a use of the denoted entity. */
9478 if (outer_automatic_var_p (expr)
9479 && current_function_decl
9480 && LAMBDA_FUNCTION_P (current_function_decl))
9481 type = capture_decltype (expr);
9482 else if (error_operand_p (expr))
9483 type = error_mark_node;
9484 else if (expr == current_class_ptr)
9485 /* If the expression is just "this", we want the
9486 cv-unqualified pointer for the "this" type. */
9487 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9488 else
9489 {
9490 /* Otherwise, where T is the type of e, if e is an lvalue,
9491 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9492 cp_lvalue_kind clk = lvalue_kind (expr);
9493 type = unlowered_expr_type (expr);
9494 gcc_assert (!TYPE_REF_P (type));
9495
9496 /* For vector types, pick a non-opaque variant. */
9497 if (VECTOR_TYPE_P (type))
9498 type = strip_typedefs (type);
9499
9500 if (clk != clk_none && !(clk & clk_class))
9501 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9502 }
9503 }
9504
9505 return type;
9506 }
9507
9508 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9509 __has_nothrow_copy, depending on assign_p. Returns true iff all
9510 the copy {ctor,assign} fns are nothrow. */
9511
9512 static bool
9513 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9514 {
9515 tree fns = NULL_TREE;
9516
9517 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9518 fns = get_class_binding (type, assign_p ? assign_op_identifier
9519 : ctor_identifier);
9520
9521 bool saw_copy = false;
9522 for (ovl_iterator iter (fns); iter; ++iter)
9523 {
9524 tree fn = *iter;
9525
9526 if (copy_fn_p (fn) > 0)
9527 {
9528 saw_copy = true;
9529 maybe_instantiate_noexcept (fn);
9530 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9531 return false;
9532 }
9533 }
9534
9535 return saw_copy;
9536 }
9537
9538 /* Actually evaluates the trait. */
9539
9540 static bool
9541 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9542 {
9543 enum tree_code type_code1;
9544 tree t;
9545
9546 type_code1 = TREE_CODE (type1);
9547
9548 switch (kind)
9549 {
9550 case CPTK_HAS_NOTHROW_ASSIGN:
9551 type1 = strip_array_types (type1);
9552 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9553 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9554 || (CLASS_TYPE_P (type1)
9555 && classtype_has_nothrow_assign_or_copy_p (type1,
9556 true))));
9557
9558 case CPTK_HAS_TRIVIAL_ASSIGN:
9559 /* ??? The standard seems to be missing the "or array of such a class
9560 type" wording for this trait. */
9561 type1 = strip_array_types (type1);
9562 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9563 && (trivial_type_p (type1)
9564 || (CLASS_TYPE_P (type1)
9565 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9566
9567 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9568 type1 = strip_array_types (type1);
9569 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9570 || (CLASS_TYPE_P (type1)
9571 && (t = locate_ctor (type1))
9572 && (maybe_instantiate_noexcept (t),
9573 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9574
9575 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9576 type1 = strip_array_types (type1);
9577 return (trivial_type_p (type1)
9578 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9579
9580 case CPTK_HAS_NOTHROW_COPY:
9581 type1 = strip_array_types (type1);
9582 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9583 || (CLASS_TYPE_P (type1)
9584 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9585
9586 case CPTK_HAS_TRIVIAL_COPY:
9587 /* ??? The standard seems to be missing the "or array of such a class
9588 type" wording for this trait. */
9589 type1 = strip_array_types (type1);
9590 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9591 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9592
9593 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9594 type1 = strip_array_types (type1);
9595 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9596 || (CLASS_TYPE_P (type1)
9597 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9598
9599 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9600 return type_has_virtual_destructor (type1);
9601
9602 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9603 return type_has_unique_obj_representations (type1);
9604
9605 case CPTK_IS_ABSTRACT:
9606 return ABSTRACT_CLASS_TYPE_P (type1);
9607
9608 case CPTK_IS_AGGREGATE:
9609 return CP_AGGREGATE_TYPE_P (type1);
9610
9611 case CPTK_IS_BASE_OF:
9612 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9613 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9614 || DERIVED_FROM_P (type1, type2)));
9615
9616 case CPTK_IS_CLASS:
9617 return NON_UNION_CLASS_TYPE_P (type1);
9618
9619 case CPTK_IS_EMPTY:
9620 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9621
9622 case CPTK_IS_ENUM:
9623 return type_code1 == ENUMERAL_TYPE;
9624
9625 case CPTK_IS_FINAL:
9626 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9627
9628 case CPTK_IS_LITERAL_TYPE:
9629 return literal_type_p (type1);
9630
9631 case CPTK_IS_POD:
9632 return pod_type_p (type1);
9633
9634 case CPTK_IS_POLYMORPHIC:
9635 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9636
9637 case CPTK_IS_SAME_AS:
9638 return same_type_p (type1, type2);
9639
9640 case CPTK_IS_STD_LAYOUT:
9641 return std_layout_type_p (type1);
9642
9643 case CPTK_IS_TRIVIAL:
9644 return trivial_type_p (type1);
9645
9646 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9647 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9648
9649 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9650 return is_trivially_xible (INIT_EXPR, type1, type2);
9651
9652 case CPTK_IS_TRIVIALLY_COPYABLE:
9653 return trivially_copyable_p (type1);
9654
9655 case CPTK_IS_UNION:
9656 return type_code1 == UNION_TYPE;
9657
9658 case CPTK_IS_ASSIGNABLE:
9659 return is_xible (MODIFY_EXPR, type1, type2);
9660
9661 case CPTK_IS_CONSTRUCTIBLE:
9662 return is_xible (INIT_EXPR, type1, type2);
9663
9664 default:
9665 gcc_unreachable ();
9666 return false;
9667 }
9668 }
9669
9670 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9671 void, or a complete type, returns true, otherwise false. */
9672
9673 static bool
9674 check_trait_type (tree type)
9675 {
9676 if (type == NULL_TREE)
9677 return true;
9678
9679 if (TREE_CODE (type) == TREE_LIST)
9680 return (check_trait_type (TREE_VALUE (type))
9681 && check_trait_type (TREE_CHAIN (type)));
9682
9683 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9684 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9685 return true;
9686
9687 if (VOID_TYPE_P (type))
9688 return true;
9689
9690 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9691 }
9692
9693 /* Process a trait expression. */
9694
9695 tree
9696 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9697 {
9698 if (type1 == error_mark_node
9699 || type2 == error_mark_node)
9700 return error_mark_node;
9701
9702 if (processing_template_decl)
9703 {
9704 tree trait_expr = make_node (TRAIT_EXPR);
9705 TREE_TYPE (trait_expr) = boolean_type_node;
9706 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9707 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9708 TRAIT_EXPR_KIND (trait_expr) = kind;
9709 return trait_expr;
9710 }
9711
9712 switch (kind)
9713 {
9714 case CPTK_HAS_NOTHROW_ASSIGN:
9715 case CPTK_HAS_TRIVIAL_ASSIGN:
9716 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9717 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9718 case CPTK_HAS_NOTHROW_COPY:
9719 case CPTK_HAS_TRIVIAL_COPY:
9720 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9721 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9722 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9723 case CPTK_IS_ABSTRACT:
9724 case CPTK_IS_AGGREGATE:
9725 case CPTK_IS_EMPTY:
9726 case CPTK_IS_FINAL:
9727 case CPTK_IS_LITERAL_TYPE:
9728 case CPTK_IS_POD:
9729 case CPTK_IS_POLYMORPHIC:
9730 case CPTK_IS_STD_LAYOUT:
9731 case CPTK_IS_TRIVIAL:
9732 case CPTK_IS_TRIVIALLY_COPYABLE:
9733 if (!check_trait_type (type1))
9734 return error_mark_node;
9735 break;
9736
9737 case CPTK_IS_ASSIGNABLE:
9738 case CPTK_IS_CONSTRUCTIBLE:
9739 break;
9740
9741 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9742 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9743 if (!check_trait_type (type1)
9744 || !check_trait_type (type2))
9745 return error_mark_node;
9746 break;
9747
9748 case CPTK_IS_BASE_OF:
9749 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9750 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9751 && !complete_type_or_else (type2, NULL_TREE))
9752 /* We already issued an error. */
9753 return error_mark_node;
9754 break;
9755
9756 case CPTK_IS_CLASS:
9757 case CPTK_IS_ENUM:
9758 case CPTK_IS_UNION:
9759 case CPTK_IS_SAME_AS:
9760 break;
9761
9762 default:
9763 gcc_unreachable ();
9764 }
9765
9766 return (trait_expr_value (kind, type1, type2)
9767 ? boolean_true_node : boolean_false_node);
9768 }
9769
9770 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9771 which is ignored for C++. */
9772
9773 void
9774 set_float_const_decimal64 (void)
9775 {
9776 }
9777
9778 void
9779 clear_float_const_decimal64 (void)
9780 {
9781 }
9782
9783 bool
9784 float_const_decimal64_p (void)
9785 {
9786 return 0;
9787 }
9788
9789 \f
9790 /* Return true if T designates the implied `this' parameter. */
9791
9792 bool
9793 is_this_parameter (tree t)
9794 {
9795 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9796 return false;
9797 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9798 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9799 return true;
9800 }
9801
9802 /* Insert the deduced return type for an auto function. */
9803
9804 void
9805 apply_deduced_return_type (tree fco, tree return_type)
9806 {
9807 tree result;
9808
9809 if (return_type == error_mark_node)
9810 return;
9811
9812 if (DECL_CONV_FN_P (fco))
9813 DECL_NAME (fco) = make_conv_op_name (return_type);
9814
9815 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9816
9817 result = DECL_RESULT (fco);
9818 if (result == NULL_TREE)
9819 return;
9820 if (TREE_TYPE (result) == return_type)
9821 return;
9822
9823 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9824 && !complete_type_or_else (return_type, NULL_TREE))
9825 return;
9826
9827 /* We already have a DECL_RESULT from start_preparsed_function.
9828 Now we need to redo the work it and allocate_struct_function
9829 did to reflect the new type. */
9830 gcc_assert (current_function_decl == fco);
9831 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9832 TYPE_MAIN_VARIANT (return_type));
9833 DECL_ARTIFICIAL (result) = 1;
9834 DECL_IGNORED_P (result) = 1;
9835 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9836 result);
9837
9838 DECL_RESULT (fco) = result;
9839
9840 if (!processing_template_decl)
9841 {
9842 bool aggr = aggregate_value_p (result, fco);
9843 #ifdef PCC_STATIC_STRUCT_RETURN
9844 cfun->returns_pcc_struct = aggr;
9845 #endif
9846 cfun->returns_struct = aggr;
9847 }
9848
9849 }
9850
9851 /* DECL is a local variable or parameter from the surrounding scope of a
9852 lambda-expression. Returns the decltype for a use of the capture field
9853 for DECL even if it hasn't been captured yet. */
9854
9855 static tree
9856 capture_decltype (tree decl)
9857 {
9858 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9859 tree cap = lookup_name_real (DECL_NAME (decl), /*type*/0, /*nonclass*/1,
9860 /*block_p=*/true, /*ns*/0, LOOKUP_HIDDEN);
9861 tree type;
9862
9863 if (cap && is_capture_proxy (cap))
9864 type = TREE_TYPE (cap);
9865 else
9866 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9867 {
9868 case CPLD_NONE:
9869 error ("%qD is not captured", decl);
9870 return error_mark_node;
9871
9872 case CPLD_COPY:
9873 type = TREE_TYPE (decl);
9874 if (TYPE_REF_P (type)
9875 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9876 type = TREE_TYPE (type);
9877 break;
9878
9879 case CPLD_REFERENCE:
9880 type = TREE_TYPE (decl);
9881 if (!TYPE_REF_P (type))
9882 type = build_reference_type (TREE_TYPE (decl));
9883 break;
9884
9885 default:
9886 gcc_unreachable ();
9887 }
9888
9889 if (!TYPE_REF_P (type))
9890 {
9891 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9892 type = cp_build_qualified_type (type, (cp_type_quals (type)
9893 |TYPE_QUAL_CONST));
9894 type = build_reference_type (type);
9895 }
9896 return type;
9897 }
9898
9899 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9900 this is a right unary fold. Otherwise it is a left unary fold. */
9901
9902 static tree
9903 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9904 {
9905 // Build a pack expansion (assuming expr has pack type).
9906 if (!uses_parameter_packs (expr))
9907 {
9908 error_at (location_of (expr), "operand of fold expression has no "
9909 "unexpanded parameter packs");
9910 return error_mark_node;
9911 }
9912 tree pack = make_pack_expansion (expr);
9913
9914 // Build the fold expression.
9915 tree code = build_int_cstu (integer_type_node, abs (op));
9916 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9917 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9918 return fold;
9919 }
9920
9921 tree
9922 finish_left_unary_fold_expr (tree expr, int op)
9923 {
9924 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9925 }
9926
9927 tree
9928 finish_right_unary_fold_expr (tree expr, int op)
9929 {
9930 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9931 }
9932
9933 /* Build a binary fold expression over EXPR1 and EXPR2. The
9934 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9935 has an unexpanded parameter pack). */
9936
9937 tree
9938 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9939 {
9940 pack = make_pack_expansion (pack);
9941 tree code = build_int_cstu (integer_type_node, abs (op));
9942 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9943 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9944 return fold;
9945 }
9946
9947 tree
9948 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9949 {
9950 // Determine which expr has an unexpanded parameter pack and
9951 // set the pack and initial term.
9952 bool pack1 = uses_parameter_packs (expr1);
9953 bool pack2 = uses_parameter_packs (expr2);
9954 if (pack1 && !pack2)
9955 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9956 else if (pack2 && !pack1)
9957 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9958 else
9959 {
9960 if (pack1)
9961 error ("both arguments in binary fold have unexpanded parameter packs");
9962 else
9963 error ("no unexpanded parameter packs in binary fold");
9964 }
9965 return error_mark_node;
9966 }
9967
9968 /* Finish __builtin_launder (arg). */
9969
9970 tree
9971 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9972 {
9973 tree orig_arg = arg;
9974 if (!type_dependent_expression_p (arg))
9975 arg = decay_conversion (arg, complain);
9976 if (error_operand_p (arg))
9977 return error_mark_node;
9978 if (!type_dependent_expression_p (arg)
9979 && !TYPE_PTR_P (TREE_TYPE (arg)))
9980 {
9981 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9982 return error_mark_node;
9983 }
9984 if (processing_template_decl)
9985 arg = orig_arg;
9986 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9987 TREE_TYPE (arg), 1, arg);
9988 }
9989
9990 /* Finish __builtin_convertvector (arg, type). */
9991
9992 tree
9993 cp_build_vec_convert (tree arg, location_t loc, tree type,
9994 tsubst_flags_t complain)
9995 {
9996 if (error_operand_p (type))
9997 return error_mark_node;
9998 if (error_operand_p (arg))
9999 return error_mark_node;
10000
10001 tree ret = NULL_TREE;
10002 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
10003 ret = c_build_vec_convert (cp_expr_loc_or_loc (arg, input_location), arg,
10004 loc, type, (complain & tf_error) != 0);
10005
10006 if (!processing_template_decl)
10007 return ret;
10008
10009 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
10010 }
10011
10012 #include "gt-cp-semantics.h"
This page took 0.515428 seconds and 6 git commands to generate.