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