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