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