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