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