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