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