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