]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/semantics.c
Implement N4514, C++ Extensions for Transactional Memory.
[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
5624e564 6 Copyright (C) 1998-2015 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
ZW
28#include "coretypes.h"
29#include "tm.h"
40e23961 30#include "alias.h"
ad321293 31#include "tree.h"
d8a2d370
DN
32#include "stmt.h"
33#include "varasm.h"
34#include "stor-layout.h"
35#include "stringpool.h"
ad321293 36#include "cp-tree.h"
39dabefd 37#include "c-family/c-common.h"
61d3ce20 38#include "c-family/c-objc.h"
25af8512 39#include "tree-inline.h"
9b6ab3c0 40#include "intl.h"
12027a89 41#include "toplev.h"
84df082b 42#include "flags.h"
ea11ca7e 43#include "timevar.h"
6de9cd9a 44#include "diagnostic.h"
c582198b 45#include "hard-reg-set.h"
c582198b 46#include "function.h"
8cd2462c 47#include "cgraph.h"
325c3691 48#include "tree-iterator.h"
44d10c10 49#include "target.h"
45b0be94 50#include "gimplify.h"
7a8cba34 51#include "bitmap.h"
0645c1a2 52#include "omp-low.h"
9b2b7279 53#include "builtins.h"
9a771876 54#include "convert.h"
41dbbb37 55#include "gomp-constants.h"
ad321293
MM
56
57/* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
0b4d5576 60 degenerate form of parsing. */
ad321293 61
3a978d72 62static tree maybe_convert_cond (tree);
6de9cd9a 63static tree finalize_nrv_r (tree *, int *, void *);
d5f4eddd 64static tree capture_decltype (tree);
4985cde3 65
558475f0 66
8d241e0b
KL
67/* Deferred Access Checking Overview
68 ---------------------------------
69
70 Most C++ expressions and declarations require access checking
71 to be performed during parsing. However, in several cases,
72 this has to be treated differently.
73
74 For member declarations, access checking has to be deferred
75 until more information about the declaration is known. For
76 example:
77
78 class A {
0cbd7506 79 typedef int X;
8d241e0b 80 public:
0cbd7506 81 X f();
8d241e0b
KL
82 };
83
84 A::X A::f();
85 A::X g();
86
87 When we are parsing the function return type `A::X', we don't
88 really know if this is allowed until we parse the function name.
89
90 Furthermore, some contexts require that access checking is
91 never performed at all. These include class heads, and template
92 instantiations.
93
94 Typical use of access checking functions is described here:
c8094d83 95
8d241e0b
KL
96 1. When we enter a context that requires certain access checking
97 mode, the function `push_deferring_access_checks' is called with
98 DEFERRING argument specifying the desired mode. Access checking
99 may be performed immediately (dk_no_deferred), deferred
100 (dk_deferred), or not performed (dk_no_check).
101
102 2. When a declaration such as a type, or a variable, is encountered,
103 the function `perform_or_defer_access_check' is called. It
9771b263 104 maintains a vector of all deferred checks.
8d241e0b
KL
105
106 3. The global `current_class_type' or `current_function_decl' is then
107 setup by the parser. `enforce_access' relies on these information
108 to check access.
109
110 4. Upon exiting the context mentioned in step 1,
111 `perform_deferred_access_checks' is called to check all declaration
9771b263 112 stored in the vector. `pop_deferring_access_checks' is then
8d241e0b
KL
113 called to restore the previous access checking mode.
114
115 In case of parsing error, we simply call `pop_deferring_access_checks'
116 without `perform_deferred_access_checks'. */
117
a79683d5 118struct GTY(()) deferred_access {
9771b263 119 /* A vector representing name-lookups for which we have deferred
3e1f1ba5
NS
120 checking access controls. We cannot check the accessibility of
121 names used in a decl-specifier-seq until we know what is being
122 declared because code like:
123
c8094d83 124 class A {
0cbd7506
MS
125 class B {};
126 B* f();
3e1f1ba5
NS
127 }
128
129 A::B* A::f() { return 0; }
130
d6b418fa 131 is valid, even though `A::B' is not generally accessible. */
9771b263 132 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
c8094d83 133
3e1f1ba5
NS
134 /* The current mode of access checks. */
135 enum deferring_kind deferring_access_checks_kind;
c8094d83 136
a79683d5 137};
3e1f1ba5 138
cf22909c 139/* Data for deferred access checking. */
9771b263 140static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
3e1f1ba5 141static GTY(()) unsigned deferred_access_no_check;
cf22909c
KL
142
143/* Save the current deferred access states and start deferred
144 access checking iff DEFER_P is true. */
145
572c2b17
AP
146void
147push_deferring_access_checks (deferring_kind deferring)
cf22909c 148{
78757caa
KL
149 /* For context like template instantiation, access checking
150 disabling applies to all nested context. */
3e1f1ba5
NS
151 if (deferred_access_no_check || deferring == dk_no_check)
152 deferred_access_no_check++;
cf22909c 153 else
3e1f1ba5 154 {
f32682ca 155 deferred_access e = {NULL, deferring};
9771b263 156 vec_safe_push (deferred_access_stack, e);
3e1f1ba5 157 }
cf22909c
KL
158}
159
0ef08a81
JM
160/* Save the current deferred access states and start deferred access
161 checking, continuing the set of deferred checks in CHECKS. */
162
163void
164reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
165{
166 push_deferring_access_checks (dk_deferred);
167 if (!deferred_access_no_check)
168 deferred_access_stack->last().deferred_access_checks = checks;
169}
170
cf22909c
KL
171/* Resume deferring access checks again after we stopped doing
172 this previously. */
173
572c2b17
AP
174void
175resume_deferring_access_checks (void)
cf22909c 176{
3e1f1ba5 177 if (!deferred_access_no_check)
9771b263 178 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
cf22909c
KL
179}
180
181/* Stop deferring access checks. */
182
572c2b17
AP
183void
184stop_deferring_access_checks (void)
cf22909c 185{
3e1f1ba5 186 if (!deferred_access_no_check)
9771b263 187 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
cf22909c
KL
188}
189
190/* Discard the current deferred access checks and restore the
191 previous states. */
192
572c2b17
AP
193void
194pop_deferring_access_checks (void)
cf22909c 195{
3e1f1ba5
NS
196 if (deferred_access_no_check)
197 deferred_access_no_check--;
198 else
9771b263 199 deferred_access_stack->pop ();
cf22909c
KL
200}
201
c8094d83
MS
202/* Returns a TREE_LIST representing the deferred checks.
203 The TREE_PURPOSE of each node is the type through which the
cf22909c
KL
204 access occurred; the TREE_VALUE is the declaration named.
205 */
206
9771b263 207vec<deferred_access_check, va_gc> *
572c2b17 208get_deferred_access_checks (void)
cf22909c 209{
3e1f1ba5
NS
210 if (deferred_access_no_check)
211 return NULL;
212 else
9771b263 213 return (deferred_access_stack->last().deferred_access_checks);
cf22909c
KL
214}
215
216/* Take current deferred checks and combine with the
217 previous states if we also defer checks previously.
218 Otherwise perform checks now. */
219
572c2b17
AP
220void
221pop_to_parent_deferring_access_checks (void)
cf22909c 222{
3e1f1ba5
NS
223 if (deferred_access_no_check)
224 deferred_access_no_check--;
225 else
226 {
9771b263 227 vec<deferred_access_check, va_gc> *checks;
3e1f1ba5
NS
228 deferred_access *ptr;
229
9771b263 230 checks = (deferred_access_stack->last ().deferred_access_checks);
3e1f1ba5 231
9771b263
DN
232 deferred_access_stack->pop ();
233 ptr = &deferred_access_stack->last ();
3e1f1ba5
NS
234 if (ptr->deferring_access_checks_kind == dk_no_deferred)
235 {
236 /* Check access. */
0e69fdf0 237 perform_access_checks (checks, tf_warning_or_error);
3e1f1ba5
NS
238 }
239 else
240 {
241 /* Merge with parent. */
d6b418fa
SM
242 int i, j;
243 deferred_access_check *chk, *probe;
c8094d83 244
9771b263 245 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
3e1f1ba5 246 {
9771b263 247 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
d6b418fa
SM
248 {
249 if (probe->binfo == chk->binfo &&
250 probe->decl == chk->decl &&
251 probe->diag_decl == chk->diag_decl)
252 goto found;
253 }
3e1f1ba5 254 /* Insert into parent's checks. */
9771b263 255 vec_safe_push (ptr->deferred_access_checks, *chk);
3e1f1ba5
NS
256 found:;
257 }
258 }
259 }
cf22909c
KL
260}
261
6b648482
MM
262/* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
263 is the BINFO indicating the qualifying scope used to access the
0e69fdf0
PC
264 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
265 or we aren't in SFINAE context or all the checks succeed return TRUE,
266 otherwise FALSE. */
6b648482 267
0e69fdf0 268bool
9771b263 269perform_access_checks (vec<deferred_access_check, va_gc> *checks,
0e69fdf0 270 tsubst_flags_t complain)
6b648482 271{
d6b418fa
SM
272 int i;
273 deferred_access_check *chk;
b3ff651a 274 location_t loc = input_location;
0e69fdf0 275 bool ok = true;
d6b418fa
SM
276
277 if (!checks)
0e69fdf0 278 return true;
d6b418fa 279
9771b263 280 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
b3ff651a
JM
281 {
282 input_location = chk->loc;
0e69fdf0 283 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
b3ff651a
JM
284 }
285
286 input_location = loc;
0e69fdf0 287 return (complain & tf_error) ? true : ok;
6b648482
MM
288}
289
25903d03
KL
290/* Perform the deferred access checks.
291
292 After performing the checks, we still have to keep the list
293 `deferred_access_stack->deferred_access_checks' since we may want
294 to check access for them again later in a different context.
295 For example:
296
297 class A {
298 typedef int X;
299 static X a;
300 };
301 A::X A::a, x; // No error for `A::a', error for `x'
302
303 We have to perform deferred access of `A::X', first with `A::a',
0e69fdf0 304 next with `x'. Return value like perform_access_checks above. */
cf22909c 305
0e69fdf0
PC
306bool
307perform_deferred_access_checks (tsubst_flags_t complain)
cf22909c 308{
0e69fdf0 309 return perform_access_checks (get_deferred_access_checks (), complain);
cf22909c
KL
310}
311
312/* Defer checking the accessibility of DECL, when looked up in
0e69fdf0
PC
313 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
314 Return value like perform_access_checks above. */
cf22909c 315
0e69fdf0
PC
316bool
317perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
318 tsubst_flags_t complain)
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 {
0e69fdf0
PC
337 bool ok = enforce_access (binfo, decl, diag_decl, complain);
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{
c2255bc4 437 tree r = build_stmt (input_location, 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 ();
5ade1ed2 597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, 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 {
14f68c39
AK
618 if (check_no_cilk (destination,
619 "Cilk array notation cannot be used as a computed goto expression",
620 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
621 destination = error_mark_node;
84628aa8 622 destination = mark_rvalue_use (destination);
fc2b8477 623 if (!processing_template_decl)
f5651df1 624 {
4b978f96
PC
625 destination = cp_convert (ptr_type_node, destination,
626 tf_warning_or_error);
f5651df1
JJ
627 if (error_operand_p (destination))
628 return NULL_TREE;
53406315
JJ
629 destination
630 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
631 destination);
f5651df1 632 }
fc2b8477 633 }
c8094d83 634
35b1567d
BC
635 check_goto (destination);
636
c2255bc4 637 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
35b1567d
BC
638}
639
ed5511d9 640/* COND is the condition-expression for an if, while, etc.,
d4033c81
MLI
641 statement. Convert it to a boolean value, if appropriate.
642 In addition, verify sequence points if -Wsequence-point is enabled. */
ed5511d9 643
8ce33230 644static tree
3a978d72 645maybe_convert_cond (tree cond)
ed5511d9
MM
646{
647 /* Empty conditions remain empty. */
648 if (!cond)
649 return NULL_TREE;
650
651 /* Wait until we instantiate templates before doing conversion. */
652 if (processing_template_decl)
653 return cond;
654
d4033c81
MLI
655 if (warn_sequence_point)
656 verify_sequence_points (cond);
657
ed5511d9
MM
658 /* Do the conversion. */
659 cond = convert_from_reference (cond);
fbc8d2d3
ILT
660
661 if (TREE_CODE (cond) == MODIFY_EXPR
662 && !TREE_NO_WARNING (cond)
663 && warn_parentheses)
664 {
665 warning (OPT_Wparentheses,
666 "suggest parentheses around assignment used as truth value");
667 TREE_NO_WARNING (cond) = 1;
668 }
669
ed5511d9
MM
670 return condition_conversion (cond);
671}
672
9bfadf57 673/* Finish an expression-statement, whose EXPRESSION is as indicated. */
a7e4cfa0 674
3e4d04a1 675tree
3a978d72 676finish_expr_stmt (tree expr)
ad321293 677{
3e4d04a1
RH
678 tree r = NULL_TREE;
679
ce4a0391 680 if (expr != NULL_TREE)
ad321293 681 {
a5bcc582 682 if (!processing_template_decl)
3a5b9284
RH
683 {
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
ebeb2c24 686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
3a5b9284 687 }
47d4c811 688 else if (!type_dependent_expression_p (expr))
ebeb2c24 689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
5ade1ed2 690 tf_warning_or_error);
325c3691 691
7b3e2d46 692 if (check_for_bare_parameter_packs (expr))
4439d02f 693 expr = error_mark_node;
5d80a306 694
325c3691 695 /* Simplification of inner statement expressions, compound exprs,
ca5b80f3 696 etc can result in us already having an EXPR_STMT. */
543a0daa
RH
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
698 {
699 if (TREE_CODE (expr) != EXPR_STMT)
c2255bc4 700 expr = build_stmt (input_location, EXPR_STMT, expr);
0ad28dde 701 expr = maybe_cleanup_point_expr_void (expr);
543a0daa
RH
702 }
703
325c3691 704 r = add_stmt (expr);
35b1567d 705 }
364460b6 706
3e4d04a1 707 return r;
35b1567d
BC
708}
709
35b1567d 710
ad321293
MM
711/* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
713
714tree
3a978d72 715begin_if_stmt (void)
ad321293 716{
325c3691 717 tree r, scope;
1eb2a14d 718 scope = do_pushlevel (sk_cond);
545f261b
NF
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
caf2523d 721 begin_cond (&IF_COND (r));
ad321293
MM
722 return r;
723}
724
725/* Process the COND of an if-statement, which may be given by
726 IF_STMT. */
727
c8094d83 728void
3a978d72 729finish_if_stmt_cond (tree cond, tree if_stmt)
ad321293 730{
caf2523d
RH
731 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
732 add_stmt (if_stmt);
325c3691 733 THEN_CLAUSE (if_stmt) = push_stmt_list ();
ad321293
MM
734}
735
736/* Finish the then-clause of an if-statement, which may be given by
737 IF_STMT. */
738
739tree
3a978d72 740finish_then_clause (tree if_stmt)
ad321293 741{
325c3691 742 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
35b1567d 743 return if_stmt;
ad321293
MM
744}
745
746/* Begin the else-clause of an if-statement. */
747
325c3691
RH
748void
749begin_else_clause (tree if_stmt)
ad321293 750{
325c3691 751 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
ad321293
MM
752}
753
754/* Finish the else-clause of an if-statement, which may be given by
755 IF_STMT. */
756
757void
3a978d72 758finish_else_clause (tree if_stmt)
ad321293 759{
325c3691 760 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
ad321293
MM
761}
762
dfbb4f34 763/* Finish an if-statement. */
ad321293 764
c8094d83 765void
325c3691 766finish_if_stmt (tree if_stmt)
ad321293 767{
545f261b
NF
768 tree scope = IF_SCOPE (if_stmt);
769 IF_SCOPE (if_stmt) = NULL;
325c3691 770 add_stmt (do_poplevel (scope));
35b1567d
BC
771}
772
ad321293
MM
773/* Begin a while-statement. Returns a newly created WHILE_STMT if
774 appropriate. */
775
776tree
3a978d72 777begin_while_stmt (void)
ad321293
MM
778{
779 tree r;
c2255bc4 780 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
ae499cce 781 add_stmt (r);
325c3691 782 WHILE_BODY (r) = do_pushlevel (sk_block);
caf2523d 783 begin_cond (&WHILE_COND (r));
ad321293
MM
784 return r;
785}
786
27d26ee7 787/* Process the COND of a while-statement, which may be given by
ad321293
MM
788 WHILE_STMT. */
789
c8094d83 790void
c5028d80 791finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
ad321293 792{
14f68c39
AK
793 if (check_no_cilk (cond,
794 "Cilk array notation cannot be used as a condition for while statement",
795 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
796 cond = error_mark_node;
20f18c3c
JM
797 cond = maybe_convert_cond (cond);
798 finish_cond (&WHILE_COND (while_stmt), cond);
799 begin_maybe_infinite_loop (cond);
c5028d80
TB
800 if (ivdep && cond != error_mark_node)
801 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
802 TREE_TYPE (WHILE_COND (while_stmt)),
803 WHILE_COND (while_stmt),
804 build_int_cst (integer_type_node,
805 annot_expr_ivdep_kind));
caf2523d 806 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
ad321293
MM
807}
808
809/* Finish a while-statement, which may be given by WHILE_STMT. */
810
c8094d83 811void
3a978d72 812finish_while_stmt (tree while_stmt)
ad321293 813{
20f18c3c 814 end_maybe_infinite_loop (boolean_true_node);
325c3691 815 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
ad321293
MM
816}
817
818/* Begin a do-statement. Returns a newly created DO_STMT if
819 appropriate. */
820
821tree
3a978d72 822begin_do_stmt (void)
ad321293 823{
c2255bc4 824 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
20f18c3c 825 begin_maybe_infinite_loop (boolean_true_node);
ae499cce 826 add_stmt (r);
325c3691 827 DO_BODY (r) = push_stmt_list ();
35b1567d 828 return r;
ad321293
MM
829}
830
831/* Finish the body of a do-statement, which may be given by DO_STMT. */
832
833void
3a978d72 834finish_do_body (tree do_stmt)
ad321293 835{
62e00e94
DM
836 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
837
838 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
839 body = STATEMENT_LIST_TAIL (body)->stmt;
840
841 if (IS_EMPTY_STMT (body))
842 warning (OPT_Wempty_body,
843 "suggest explicit braces around empty body in %<do%> statement");
ad321293
MM
844}
845
846/* Finish a do-statement, which may be given by DO_STMT, and whose
847 COND is as indicated. */
848
849void
c5028d80 850finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
ad321293 851{
14f68c39
AK
852 if (check_no_cilk (cond,
853 "Cilk array notation cannot be used as a condition for a do-while statement",
854 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
855 cond = error_mark_node;
ed5511d9 856 cond = maybe_convert_cond (cond);
20f18c3c 857 end_maybe_infinite_loop (cond);
c5028d80
TB
858 if (ivdep && cond != error_mark_node)
859 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
860 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
35b1567d 861 DO_COND (do_stmt) = cond;
35b1567d 862}
ed5511d9 863
ad321293
MM
864/* Finish a return-statement. The EXPRESSION returned, if any, is as
865 indicated. */
866
3e4d04a1 867tree
3a978d72 868finish_return_stmt (tree expr)
ad321293 869{
3e4d04a1 870 tree r;
0c9b182b 871 bool no_warning;
3e4d04a1 872
0c9b182b 873 expr = check_return_expr (expr, &no_warning);
1799e5d5 874
830ce0a2
MP
875 if (error_operand_p (expr)
876 || (flag_openmp && !check_omp_return ()))
3dbb8427
JM
877 {
878 /* Suppress -Wreturn-type for this function. */
879 if (warn_return_type)
880 TREE_NO_WARNING (current_function_decl) = true;
881 return error_mark_node;
882 }
883
35b1567d 884 if (!processing_template_decl)
efee38a9 885 {
d4033c81
MLI
886 if (warn_sequence_point)
887 verify_sequence_points (expr);
888
44d10c10 889 if (DECL_DESTRUCTOR_P (current_function_decl)
c8094d83 890 || (DECL_CONSTRUCTOR_P (current_function_decl)
44d10c10 891 && targetm.cxx.cdtor_returns_this ()))
efee38a9
MM
892 {
893 /* Similarly, all destructors must run destructors for
894 base-classes before returning. So, all returns in a
dfbb4f34 895 destructor get sent to the DTOR_LABEL; finish_function emits
efee38a9 896 code to return a value there. */
44d10c10 897 return finish_goto_stmt (cdtor_label);
efee38a9
MM
898 }
899 }
543a0daa 900
c2255bc4 901 r = build_stmt (input_location, RETURN_EXPR, expr);
0c9b182b 902 TREE_NO_WARNING (r) |= no_warning;
0ad28dde 903 r = maybe_cleanup_point_expr_void (r);
543a0daa 904 r = add_stmt (r);
3e4d04a1
RH
905
906 return r;
35b1567d 907}
efee38a9 908
3f43ac31
RRC
909/* Begin the scope of a for-statement or a range-for-statement.
910 Both the returned trees are to be used in a call to
911 begin_for_stmt or begin_range_for_stmt. */
ad321293
MM
912
913tree
3f43ac31
RRC
914begin_for_scope (tree *init)
915{
916 tree scope = NULL_TREE;
917 if (flag_new_for_scope > 0)
918 scope = do_pushlevel (sk_for);
919
920 if (processing_template_decl)
921 *init = push_stmt_list ();
922 else
923 *init = NULL_TREE;
924
925 return scope;
926}
927
928/* Begin a for-statement. Returns a new FOR_STMT.
929 SCOPE and INIT should be the return of begin_for_scope,
930 or both NULL_TREE */
931
932tree
933begin_for_stmt (tree scope, tree init)
ad321293
MM
934{
935 tree r;
936
c2255bc4 937 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
40e71fc7 938 NULL_TREE, NULL_TREE, NULL_TREE);
325c3691 939
3f43ac31
RRC
940 if (scope == NULL_TREE)
941 {
95cc031f
JJ
942 gcc_assert (!init || !(flag_new_for_scope > 0));
943 if (!init)
944 scope = begin_for_scope (&init);
3f43ac31
RRC
945 }
946 FOR_INIT_STMT (r) = init;
40e71fc7 947 FOR_SCOPE (r) = scope;
894ca2c9 948
ad321293
MM
949 return r;
950}
951
952/* Finish the for-init-statement of a for-statement, which may be
953 given by FOR_STMT. */
954
955void
3a978d72 956finish_for_init_stmt (tree for_stmt)
ad321293 957{
894ca2c9
RH
958 if (processing_template_decl)
959 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
325c3691
RH
960 add_stmt (for_stmt);
961 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
caf2523d 962 begin_cond (&FOR_COND (for_stmt));
ad321293
MM
963}
964
965/* Finish the COND of a for-statement, which may be given by
966 FOR_STMT. */
967
968void
c5028d80 969finish_for_cond (tree cond, tree for_stmt, bool ivdep)
ad321293 970{
14f68c39
AK
971 if (check_no_cilk (cond,
972 "Cilk array notation cannot be used in a condition for a for-loop",
973 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
974 cond = error_mark_node;
20f18c3c
JM
975 cond = maybe_convert_cond (cond);
976 finish_cond (&FOR_COND (for_stmt), cond);
977 begin_maybe_infinite_loop (cond);
c5028d80
TB
978 if (ivdep && cond != error_mark_node)
979 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
980 TREE_TYPE (FOR_COND (for_stmt)),
981 FOR_COND (for_stmt),
982 build_int_cst (integer_type_node,
983 annot_expr_ivdep_kind));
caf2523d 984 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
ad321293
MM
985}
986
987/* Finish the increment-EXPRESSION in a for-statement, which may be
988 given by FOR_STMT. */
989
990void
3a978d72 991finish_for_expr (tree expr, tree for_stmt)
ad321293 992{
543a0daa
RH
993 if (!expr)
994 return;
6f69173e
MM
995 /* If EXPR is an overloaded function, issue an error; there is no
996 context available to use to perform overload resolution. */
543a0daa 997 if (type_unknown_p (expr))
6f69173e
MM
998 {
999 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1000 expr = error_mark_node;
1001 }
bcd46a7c
AP
1002 if (!processing_template_decl)
1003 {
1004 if (warn_sequence_point)
0cbd7506 1005 verify_sequence_points (expr);
ebeb2c24 1006 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
5ade1ed2 1007 tf_warning_or_error);
bcd46a7c
AP
1008 }
1009 else if (!type_dependent_expression_p (expr))
ebeb2c24 1010 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
5ade1ed2 1011 tf_warning_or_error);
0ad28dde 1012 expr = maybe_cleanup_point_expr_void (expr);
7b3e2d46 1013 if (check_for_bare_parameter_packs (expr))
4439d02f 1014 expr = error_mark_node;
35b1567d 1015 FOR_EXPR (for_stmt) = expr;
ad321293
MM
1016}
1017
1018/* Finish the body of a for-statement, which may be given by
1019 FOR_STMT. The increment-EXPR for the loop must be
f9132eb7
RRC
1020 provided.
1021 It can also finish RANGE_FOR_STMT. */
ad321293
MM
1022
1023void
3a978d72 1024finish_for_stmt (tree for_stmt)
ad321293 1025{
20f18c3c
JM
1026 end_maybe_infinite_loop (boolean_true_node);
1027
f9132eb7 1028 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
a8733ebf 1029 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
f9132eb7 1030 else
a8733ebf 1031 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
325c3691 1032
ad321293 1033 /* Pop the scope for the body of the loop. */
a8733ebf 1034 if (flag_new_for_scope > 0)
325c3691 1035 {
40e71fc7
NF
1036 tree scope;
1037 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1038 ? &RANGE_FOR_SCOPE (for_stmt)
1039 : &FOR_SCOPE (for_stmt));
1040 scope = *scope_ptr;
1041 *scope_ptr = NULL;
325c3691
RH
1042 add_stmt (do_poplevel (scope));
1043 }
ad321293
MM
1044}
1045
f9132eb7 1046/* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
3f43ac31
RRC
1047 SCOPE and INIT should be the return of begin_for_scope,
1048 or both NULL_TREE .
f9132eb7
RRC
1049 To finish it call finish_for_stmt(). */
1050
1051tree
3f43ac31 1052begin_range_for_stmt (tree scope, tree init)
f9132eb7
RRC
1053{
1054 tree r;
a8733ebf 1055
20f18c3c
JM
1056 begin_maybe_infinite_loop (boolean_false_node);
1057
f9132eb7 1058 r = build_stmt (input_location, RANGE_FOR_STMT,
40e71fc7 1059 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
a8733ebf 1060
3f43ac31
RRC
1061 if (scope == NULL_TREE)
1062 {
95cc031f
JJ
1063 gcc_assert (!init || !(flag_new_for_scope > 0));
1064 if (!init)
1065 scope = begin_for_scope (&init);
3f43ac31
RRC
1066 }
1067
1068 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1069 pop it now. */
1070 if (init)
1071 pop_stmt_list (init);
40e71fc7 1072 RANGE_FOR_SCOPE (r) = scope;
f9132eb7
RRC
1073
1074 return r;
1075}
1076
1077/* Finish the head of a range-based for statement, which may
1078 be given by RANGE_FOR_STMT. DECL must be the declaration
1079 and EXPR must be the loop expression. */
1080
1081void
1082finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1083{
1084 RANGE_FOR_DECL (range_for_stmt) = decl;
1085 RANGE_FOR_EXPR (range_for_stmt) = expr;
1086 add_stmt (range_for_stmt);
1087 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1088}
1089
ad321293
MM
1090/* Finish a break-statement. */
1091
3e4d04a1 1092tree
3a978d72 1093finish_break_stmt (void)
ad321293 1094{
04771457
JM
1095 /* In switch statements break is sometimes stylistically used after
1096 a return statement. This can lead to spurious warnings about
1097 control reaching the end of a non-void function when it is
1098 inlined. Note that we are calling block_may_fallthru with
1099 language specific tree nodes; this works because
1100 block_may_fallthru returns true when given something it does not
1101 understand. */
1102 if (!block_may_fallthru (cur_stmt_list))
632f2871 1103 return void_node;
c2255bc4 1104 return add_stmt (build_stmt (input_location, BREAK_STMT));
35b1567d
BC
1105}
1106
ad321293
MM
1107/* Finish a continue-statement. */
1108
3e4d04a1 1109tree
3a978d72 1110finish_continue_stmt (void)
ad321293 1111{
c2255bc4 1112 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
ad321293
MM
1113}
1114
35b1567d
BC
1115/* Begin a switch-statement. Returns a new SWITCH_STMT if
1116 appropriate. */
1117
1118tree
3a978d72 1119begin_switch_stmt (void)
35b1567d 1120{
325c3691
RH
1121 tree r, scope;
1122
1eb2a14d 1123 scope = do_pushlevel (sk_cond);
1f18dbc6
NF
1124 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1125
ebaae582 1126 begin_cond (&SWITCH_STMT_COND (r));
325c3691 1127
527f0080 1128 return r;
ad321293
MM
1129}
1130
527f0080 1131/* Finish the cond of a switch-statement. */
ad321293 1132
527f0080 1133void
3a978d72 1134finish_switch_cond (tree cond, tree switch_stmt)
ad321293 1135{
6f9fdf4d 1136 tree orig_type = NULL;
14f68c39
AK
1137
1138 if (check_no_cilk (cond,
1139 "Cilk array notation cannot be used as a condition for switch statement",
1140 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1141 cond = error_mark_node;
1142
35b1567d 1143 if (!processing_template_decl)
373eb3b3 1144 {
35b1567d 1145 /* Convert the condition to an integer or enumeration type. */
b746c5dc 1146 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
35b1567d 1147 if (cond == NULL_TREE)
373eb3b3 1148 {
35b1567d
BC
1149 error ("switch quantity not an integer");
1150 cond = error_mark_node;
1151 }
083e891e
MP
1152 /* We want unlowered type here to handle enum bit-fields. */
1153 orig_type = unlowered_expr_type (cond);
5964a3a6
MP
1154 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1155 orig_type = TREE_TYPE (cond);
35b1567d
BC
1156 if (cond != error_mark_node)
1157 {
0a72704b
MM
1158 /* [stmt.switch]
1159
1160 Integral promotions are performed. */
1161 cond = perform_integral_promotions (cond);
543a0daa 1162 cond = maybe_cleanup_point_expr (cond);
373eb3b3 1163 }
ad321293 1164 }
7b3e2d46 1165 if (check_for_bare_parameter_packs (cond))
4439d02f 1166 cond = error_mark_node;
d4033c81
MLI
1167 else if (!processing_template_decl && warn_sequence_point)
1168 verify_sequence_points (cond);
1169
ebaae582
SB
1170 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1171 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
caf2523d 1172 add_stmt (switch_stmt);
56cb9733 1173 push_switch (switch_stmt);
ebaae582 1174 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
ad321293
MM
1175}
1176
1177/* Finish the body of a switch-statement, which may be given by
1178 SWITCH_STMT. The COND to switch on is indicated. */
1179
1180void
3a978d72 1181finish_switch_stmt (tree switch_stmt)
ad321293 1182{
325c3691
RH
1183 tree scope;
1184
ebaae582
SB
1185 SWITCH_STMT_BODY (switch_stmt) =
1186 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
c8094d83 1187 pop_switch ();
325c3691 1188
1f18dbc6
NF
1189 scope = SWITCH_STMT_SCOPE (switch_stmt);
1190 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
325c3691 1191 add_stmt (do_poplevel (scope));
ad321293
MM
1192}
1193
ad321293
MM
1194/* Begin a try-block. Returns a newly-created TRY_BLOCK if
1195 appropriate. */
1196
1197tree
3a978d72 1198begin_try_block (void)
ad321293 1199{
c2255bc4 1200 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
ae499cce 1201 add_stmt (r);
325c3691 1202 TRY_STMTS (r) = push_stmt_list ();
35b1567d 1203 return r;
ad321293
MM
1204}
1205
eaf6fb90
MM
1206/* Likewise, for a function-try-block. The block returned in
1207 *COMPOUND_STMT is an artificial outer scope, containing the
1208 function-try-block. */
0dde4175
JM
1209
1210tree
eaf6fb90 1211begin_function_try_block (tree *compound_stmt)
0dde4175 1212{
eaf6fb90
MM
1213 tree r;
1214 /* This outer scope does not exist in the C++ standard, but we need
1215 a place to put __FUNCTION__ and similar variables. */
1216 *compound_stmt = begin_compound_stmt (0);
1217 r = begin_try_block ();
35b1567d 1218 FN_TRY_BLOCK_P (r) = 1;
35b1567d 1219 return r;
0dde4175
JM
1220}
1221
ad321293
MM
1222/* Finish a try-block, which may be given by TRY_BLOCK. */
1223
1224void
3a978d72 1225finish_try_block (tree try_block)
ad321293 1226{
325c3691
RH
1227 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1228 TRY_HANDLERS (try_block) = push_stmt_list ();
ad321293
MM
1229}
1230
efa8eda3
MM
1231/* Finish the body of a cleanup try-block, which may be given by
1232 TRY_BLOCK. */
1233
62409b39 1234void
3a978d72 1235finish_cleanup_try_block (tree try_block)
62409b39 1236{
325c3691 1237 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
62409b39
MM
1238}
1239
f1dedc31
MM
1240/* Finish an implicitly generated try-block, with a cleanup is given
1241 by CLEANUP. */
1242
1243void
3a978d72 1244finish_cleanup (tree cleanup, tree try_block)
f1dedc31 1245{
35b1567d
BC
1246 TRY_HANDLERS (try_block) = cleanup;
1247 CLEANUP_P (try_block) = 1;
f1dedc31
MM
1248}
1249
0dde4175
JM
1250/* Likewise, for a function-try-block. */
1251
1252void
3a978d72 1253finish_function_try_block (tree try_block)
0dde4175 1254{
325c3691
RH
1255 finish_try_block (try_block);
1256 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1257 the try block, but moving it inside. */
b35d4555 1258 in_function_try_handler = 1;
0dde4175
JM
1259}
1260
ad321293
MM
1261/* Finish a handler-sequence for a try-block, which may be given by
1262 TRY_BLOCK. */
1263
1264void
3a978d72 1265finish_handler_sequence (tree try_block)
ad321293 1266{
325c3691 1267 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
35b1567d 1268 check_handlers (TRY_HANDLERS (try_block));
ad321293
MM
1269}
1270
eaf6fb90
MM
1271/* Finish the handler-seq for a function-try-block, given by
1272 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1273 begin_function_try_block. */
0dde4175
JM
1274
1275void
eaf6fb90 1276finish_function_handler_sequence (tree try_block, tree compound_stmt)
0dde4175 1277{
b35d4555 1278 in_function_try_handler = 0;
325c3691 1279 finish_handler_sequence (try_block);
eaf6fb90 1280 finish_compound_stmt (compound_stmt);
35b1567d
BC
1281}
1282
ad321293
MM
1283/* Begin a handler. Returns a HANDLER if appropriate. */
1284
1285tree
3a978d72 1286begin_handler (void)
ad321293
MM
1287{
1288 tree r;
325c3691 1289
c2255bc4 1290 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
ae499cce 1291 add_stmt (r);
325c3691 1292
1a6025b4
JM
1293 /* Create a binding level for the eh_info and the exception object
1294 cleanup. */
325c3691
RH
1295 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1296
ad321293
MM
1297 return r;
1298}
1299
1300/* Finish the handler-parameters for a handler, which may be given by
b35d4555
MM
1301 HANDLER. DECL is the declaration for the catch parameter, or NULL
1302 if this is a `catch (...)' clause. */
ad321293 1303
1a6025b4 1304void
3a978d72 1305finish_handler_parms (tree decl, tree handler)
b35d4555 1306{
1a6025b4 1307 tree type = NULL_TREE;
b35d4555
MM
1308 if (processing_template_decl)
1309 {
1310 if (decl)
1311 {
1312 decl = pushdecl (decl);
1313 decl = push_template_decl (decl);
325c3691 1314 HANDLER_PARMS (handler) = decl;
1a6025b4 1315 type = TREE_TYPE (decl);
b35d4555
MM
1316 }
1317 }
35b1567d 1318 else
1a6025b4 1319 type = expand_start_catch_block (decl);
1a6025b4 1320 HANDLER_TYPE (handler) = type;
35b1567d
BC
1321}
1322
1323/* Finish a handler, which may be given by HANDLER. The BLOCKs are
1324 the return value from the matching call to finish_handler_parms. */
1325
1326void
3a978d72 1327finish_handler (tree handler)
35b1567d
BC
1328{
1329 if (!processing_template_decl)
1a6025b4 1330 expand_end_catch_block ();
325c3691 1331 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
35b1567d
BC
1332}
1333
5882f0f3 1334/* Begin a compound statement. FLAGS contains some bits that control the
5995ebfb 1335 behavior and context. If BCS_NO_SCOPE is set, the compound statement
5882f0f3 1336 does not define a scope. If BCS_FN_BODY is set, this is the outermost
c8094d83 1337 block of a function. If BCS_TRY_BLOCK is set, this is the block
5882f0f3
RH
1338 created on behalf of a TRY statement. Returns a token to be passed to
1339 finish_compound_stmt. */
ad321293
MM
1340
1341tree
325c3691 1342begin_compound_stmt (unsigned int flags)
ad321293 1343{
325c3691 1344 tree r;
558475f0 1345
325c3691
RH
1346 if (flags & BCS_NO_SCOPE)
1347 {
1348 r = push_stmt_list ();
1349 STATEMENT_LIST_NO_SCOPE (r) = 1;
1350
1351 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1352 But, if it's a statement-expression with a scopeless block, there's
1353 nothing to keep, and we don't want to accidentally keep a block
c8094d83 1354 *inside* the scopeless block. */
325c3691
RH
1355 keep_next_level (false);
1356 }
f1dedc31 1357 else
b8fd7909
JM
1358 {
1359 scope_kind sk = sk_block;
1360 if (flags & BCS_TRY_BLOCK)
1361 sk = sk_try;
1362 else if (flags & BCS_TRANSACTION)
1363 sk = sk_transaction;
1364 r = do_pushlevel (sk);
1365 }
325c3691 1366
5882f0f3
RH
1367 /* When processing a template, we need to remember where the braces were,
1368 so that we can set up identical scopes when instantiating the template
1369 later. BIND_EXPR is a handy candidate for this.
1370 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1371 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1372 processing templates. */
1373 if (processing_template_decl)
325c3691 1374 {
f293ce4b 1375 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
5882f0f3
RH
1376 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1377 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
325c3691
RH
1378 TREE_SIDE_EFFECTS (r) = 1;
1379 }
ad321293
MM
1380
1381 return r;
1382}
1383
5882f0f3 1384/* Finish a compound-statement, which is given by STMT. */
ad321293 1385
325c3691
RH
1386void
1387finish_compound_stmt (tree stmt)
ad321293 1388{
5882f0f3 1389 if (TREE_CODE (stmt) == BIND_EXPR)
e02927a1
JM
1390 {
1391 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1392 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1393 discard the BIND_EXPR so it can be merged with the containing
1394 STATEMENT_LIST. */
1395 if (TREE_CODE (body) == STATEMENT_LIST
1396 && STATEMENT_LIST_HEAD (body) == NULL
1397 && !BIND_EXPR_BODY_BLOCK (stmt)
1398 && !BIND_EXPR_TRY_BLOCK (stmt))
1399 stmt = body;
1400 else
1401 BIND_EXPR_BODY (stmt) = body;
1402 }
325c3691
RH
1403 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1404 stmt = pop_stmt_list (stmt);
7a3397c7 1405 else
5f070bc7
ZL
1406 {
1407 /* Destroy any ObjC "super" receivers that may have been
1408 created. */
1409 objc_clear_super_receiver ();
1410
1411 stmt = do_poplevel (stmt);
1412 }
ad321293 1413
325c3691
RH
1414 /* ??? See c_end_compound_stmt wrt statement expressions. */
1415 add_stmt (stmt);
ad321293
MM
1416}
1417
6de9cd9a 1418/* Finish an asm-statement, whose components are a STRING, some
1c384bf1
RH
1419 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1420 LABELS. Also note whether the asm-statement should be
1421 considered volatile. */
7dc5bd62 1422
3e4d04a1 1423tree
6de9cd9a 1424finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1c384bf1 1425 tree input_operands, tree clobbers, tree labels)
35b1567d
BC
1426{
1427 tree r;
abfc8a36 1428 tree t;
5544530a
PB
1429 int ninputs = list_length (input_operands);
1430 int noutputs = list_length (output_operands);
abfc8a36 1431
abfc8a36 1432 if (!processing_template_decl)
40b18c0a 1433 {
74f0c611
RH
1434 const char *constraint;
1435 const char **oconstraints;
1436 bool allows_mem, allows_reg, is_inout;
1437 tree operand;
40b18c0a 1438 int i;
40b18c0a 1439
86b8fed1 1440 oconstraints = XALLOCAVEC (const char *, noutputs);
74f0c611
RH
1441
1442 string = resolve_asm_operand_names (string, output_operands,
1c384bf1 1443 input_operands, labels);
74f0c611
RH
1444
1445 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
40b18c0a 1446 {
74f0c611
RH
1447 operand = TREE_VALUE (t);
1448
1449 /* ??? Really, this should not be here. Users should be using a
1450 proper lvalue, dammit. But there's a long history of using
1451 casts in the output operands. In cases like longlong.h, this
1452 becomes a primitive form of typechecking -- if the cast can be
1453 removed, then the output operand had a type of the proper width;
1454 otherwise we'll get an error. Gross, but ... */
1455 STRIP_NOPS (operand);
1456
03a904b5
JJ
1457 operand = mark_lvalue_use (operand);
1458
5ade1ed2 1459 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
74f0c611
RH
1460 operand = error_mark_node;
1461
3db45ab5 1462 if (operand != error_mark_node
5544530a
PB
1463 && (TREE_READONLY (operand)
1464 || CP_TYPE_CONST_P (TREE_TYPE (operand))
3db45ab5
MS
1465 /* Functions are not modifiable, even though they are
1466 lvalues. */
1467 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1468 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1469 /* If it's an aggregate and any field is const, then it is
1470 effectively const. */
1471 || (CLASS_TYPE_P (TREE_TYPE (operand))
1472 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
4816c593 1473 cxx_readonly_error (operand, lv_asm);
5544530a 1474
74f0c611
RH
1475 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1476 oconstraints[i] = constraint;
1477
1478 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1479 &allows_mem, &allows_reg, &is_inout))
1480 {
1481 /* If the operand is going to end up in memory,
1482 mark it addressable. */
1483 if (!allows_reg && !cxx_mark_addressable (operand))
1484 operand = error_mark_node;
1485 }
1486 else
1487 operand = error_mark_node;
1488
1489 TREE_VALUE (t) = operand;
1490 }
1491
1492 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1493 {
1494 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
0ab19cbc
JJ
1495 bool constraint_parsed
1496 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1497 oconstraints, &allows_mem, &allows_reg);
1498 /* If the operand is going to end up in memory, don't call
1499 decay_conversion. */
1500 if (constraint_parsed && !allows_reg && allows_mem)
1501 operand = mark_lvalue_use (TREE_VALUE (t));
1502 else
1503 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
74f0c611 1504
40b18c0a
MM
1505 /* If the type of the operand hasn't been determined (e.g.,
1506 because it involves an overloaded function), then issue
1507 an error message. There's no context available to
1508 resolve the overloading. */
74f0c611 1509 if (TREE_TYPE (operand) == unknown_type_node)
40b18c0a 1510 {
c8094d83 1511 error ("type of asm operand %qE could not be determined",
0cbd7506 1512 TREE_VALUE (t));
74f0c611 1513 operand = error_mark_node;
40b18c0a 1514 }
40b18c0a 1515
0ab19cbc 1516 if (constraint_parsed)
40b18c0a 1517 {
74f0c611
RH
1518 /* If the operand is going to end up in memory,
1519 mark it addressable. */
b4c33883
AP
1520 if (!allows_reg && allows_mem)
1521 {
1522 /* Strip the nops as we allow this case. FIXME, this really
1523 should be rejected or made deprecated. */
1524 STRIP_NOPS (operand);
1525 if (!cxx_mark_addressable (operand))
1526 operand = error_mark_node;
1527 }
6760071f
JJ
1528 else if (!allows_reg && !allows_mem)
1529 {
1530 /* If constraint allows neither register nor memory,
1531 try harder to get a constant. */
1532 tree constop = maybe_constant_value (operand);
1533 if (TREE_CONSTANT (constop))
1534 operand = constop;
1535 }
40b18c0a 1536 }
74f0c611
RH
1537 else
1538 operand = error_mark_node;
40b18c0a 1539
74f0c611 1540 TREE_VALUE (t) = operand;
40b18c0a
MM
1541 }
1542 }
abfc8a36 1543
c2255bc4 1544 r = build_stmt (input_location, ASM_EXPR, string,
0dfdeca6 1545 output_operands, input_operands,
1c384bf1 1546 clobbers, labels);
5544530a 1547 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
0ad28dde 1548 r = maybe_cleanup_point_expr_void (r);
3e4d04a1 1549 return add_stmt (r);
ad321293 1550}
b4c4a9ec 1551
5bca4e80 1552/* Finish a label with the indicated NAME. Returns the new label. */
f01b0acb 1553
a723baf1 1554tree
3a978d72 1555finish_label_stmt (tree name)
f01b0acb 1556{
5b030314 1557 tree decl = define_label (input_location, name);
417fa55b 1558
5bca4e80 1559 if (decl == error_mark_node)
417fa55b
LM
1560 return error_mark_node;
1561
c2255bc4 1562 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
5bca4e80
ILT
1563
1564 return decl;
f01b0acb
MM
1565}
1566
acef433b
MM
1567/* Finish a series of declarations for local labels. G++ allows users
1568 to declare "local" labels, i.e., labels with scope. This extension
1569 is useful when writing code involving statement-expressions. */
1570
1571void
3a978d72 1572finish_label_decl (tree name)
acef433b 1573{
a6d76a95
PC
1574 if (!at_function_scope_p ())
1575 {
1576 error ("__label__ declarations are only allowed in function scopes");
1577 return;
1578 }
1579
1580 add_decl_expr (declare_local_label (name));
acef433b
MM
1581}
1582
659e5a7a 1583/* When DECL goes out of scope, make sure that CLEANUP is executed. */
f1dedc31 1584
c8094d83 1585void
3a978d72 1586finish_decl_cleanup (tree decl, tree cleanup)
f1dedc31 1587{
325c3691 1588 push_cleanup (decl, cleanup, false);
35b1567d
BC
1589}
1590
659e5a7a 1591/* If the current scope exits with an exception, run CLEANUP. */
24bef158 1592
659e5a7a 1593void
3a978d72 1594finish_eh_cleanup (tree cleanup)
24bef158 1595{
325c3691 1596 push_cleanup (NULL, cleanup, true);
35b1567d
BC
1597}
1598
2282d28d
MM
1599/* The MEM_INITS is a list of mem-initializers, in reverse of the
1600 order they were written by the user. Each node is as for
1601 emit_mem_initializers. */
bf3428d0
MM
1602
1603void
2282d28d 1604finish_mem_initializers (tree mem_inits)
bf3428d0 1605{
2282d28d
MM
1606 /* Reorder the MEM_INITS so that they are in the order they appeared
1607 in the source program. */
1608 mem_inits = nreverse (mem_inits);
bf3428d0 1609
a0de9d20 1610 if (processing_template_decl)
5d80a306
DG
1611 {
1612 tree mem;
1613
1614 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1615 {
1616 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1617 check for bare parameter packs in the TREE_VALUE, because
1618 any parameter packs in the TREE_VALUE have already been
1619 bound as part of the TREE_PURPOSE. See
1620 make_pack_expansion for more information. */
4439d02f 1621 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
7b3e2d46 1622 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
4439d02f 1623 TREE_VALUE (mem) = error_mark_node;
5d80a306
DG
1624 }
1625
f330f599
PC
1626 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1627 CTOR_INITIALIZER, mem_inits));
5d80a306 1628 }
cdd2559c 1629 else
2282d28d 1630 emit_mem_initializers (mem_inits);
558475f0
MM
1631}
1632
10c6dc8e
JM
1633/* Obfuscate EXPR if it looks like an id-expression or member access so
1634 that the call to finish_decltype in do_auto_deduction will give the
1635 right result. */
1636
1637tree
1638force_paren_expr (tree expr)
1639{
1640 /* This is only needed for decltype(auto) in C++14. */
e4276ba5 1641 if (cxx_dialect < cxx14)
10c6dc8e
JM
1642 return expr;
1643
0e4cf887
JM
1644 /* If we're in unevaluated context, we can't be deducing a
1645 return/initializer type, so we don't need to mess with this. */
1646 if (cp_unevaluated_operand)
1647 return expr;
1648
10c6dc8e
JM
1649 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1650 && TREE_CODE (expr) != SCOPE_REF)
1651 return expr;
1652
0e4cf887
JM
1653 if (TREE_CODE (expr) == COMPONENT_REF)
1654 REF_PARENTHESIZED_P (expr) = true;
1655 else if (type_dependent_expression_p (expr))
10c6dc8e
JM
1656 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1657 else
1658 {
1659 cp_lvalue_kind kind = lvalue_kind (expr);
1660 if ((kind & ~clk_class) != clk_none)
1661 {
1662 tree type = unlowered_expr_type (expr);
1663 bool rval = !!(kind & clk_rvalueref);
1664 type = cp_build_reference_type (type, rval);
6e4992ca
PC
1665 /* This inhibits warnings in, eg, cxx_mark_addressable
1666 (c++/60955). */
1667 warning_sentinel s (extra_warnings);
0e4cf887 1668 expr = build_static_cast (type, expr, tf_error);
96e780c0
JM
1669 if (expr != error_mark_node)
1670 REF_PARENTHESIZED_P (expr) = true;
10c6dc8e
JM
1671 }
1672 }
1673
1674 return expr;
1675}
1676
b4c4a9ec
MM
1677/* Finish a parenthesized expression EXPR. */
1678
1679tree
3a978d72 1680finish_parenthesized_expr (tree expr)
b4c4a9ec 1681{
6615c446 1682 if (EXPR_P (expr))
78ef5b89 1683 /* This inhibits warnings in c_common_truthvalue_conversion. */
31ec7d2f 1684 TREE_NO_WARNING (expr) = 1;
b4c4a9ec 1685
d816a3ba
JM
1686 if (TREE_CODE (expr) == OFFSET_REF
1687 || TREE_CODE (expr) == SCOPE_REF)
19420d00
NS
1688 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1689 enclosed in parentheses. */
1690 PTRMEM_OK_P (expr) = 0;
c8094d83 1691
7a8380ae
NS
1692 if (TREE_CODE (expr) == STRING_CST)
1693 PAREN_STRING_LITERAL_P (expr) = 1;
c8094d83 1694
10c6dc8e
JM
1695 expr = force_paren_expr (expr);
1696
b4c4a9ec
MM
1697 return expr;
1698}
1699
a723baf1
MM
1700/* Finish a reference to a non-static data member (DECL) that is not
1701 preceded by `.' or `->'. */
1702
1703tree
a3f10e50 1704finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
a723baf1 1705{
50bc768d 1706 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
a723baf1 1707
2defb926 1708 if (!object)
51fc2d02 1709 {
51fc2d02
JM
1710 tree scope = qualifying_scope;
1711 if (scope == NULL_TREE)
1712 scope = context_for_name_lookup (decl);
1713 object = maybe_dummy_object (scope, NULL);
1714 }
1715
0b360a07 1716 object = maybe_resolve_dummy (object, true);
1496e7d6
PC
1717 if (object == error_mark_node)
1718 return error_mark_node;
1719
e2254932 1720 /* DR 613/850: Can use non-static data members without an associated
2defb926
JM
1721 object in sizeof/decltype/alignof. */
1722 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1723 && (!processing_template_decl || !current_class_ref))
a723baf1 1724 {
c8094d83 1725 if (current_function_decl
a723baf1 1726 && DECL_STATIC_FUNCTION_P (current_function_decl))
6863c41a 1727 error ("invalid use of member %qD in static member function", decl);
a723baf1 1728 else
6863c41a
JM
1729 error ("invalid use of non-static data member %qD", decl);
1730 inform (DECL_SOURCE_LOCATION (decl), "declared here");
a723baf1
MM
1731
1732 return error_mark_node;
1733 }
d5f4eddd 1734
51fc2d02
JM
1735 if (current_class_ptr)
1736 TREE_USED (current_class_ptr) = 1;
58e1d54c 1737 if (processing_template_decl && !qualifying_scope)
a723baf1 1738 {
a3f10e50 1739 tree type = TREE_TYPE (decl);
a723baf1 1740
a3f10e50 1741 if (TREE_CODE (type) == REFERENCE_TYPE)
2516ccfe 1742 /* Quals on the object don't matter. */;
2993d08a
JM
1743 else if (PACK_EXPANSION_P (type))
1744 /* Don't bother trying to represent this. */
1745 type = NULL_TREE;
a3f10e50
NS
1746 else
1747 {
f4f206f4 1748 /* Set the cv qualifiers. */
bab5167f 1749 int quals = cp_type_quals (TREE_TYPE (object));
c8094d83 1750
a3f10e50
NS
1751 if (DECL_MUTABLE_P (decl))
1752 quals &= ~TYPE_QUAL_CONST;
9e95d15f 1753
a3f10e50
NS
1754 quals |= cp_type_quals (TREE_TYPE (decl));
1755 type = cp_build_qualified_type (type, quals);
1756 }
c8094d83 1757
2516ccfe
JM
1758 return (convert_from_reference
1759 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
a3f10e50 1760 }
ab11c13f
JM
1761 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1762 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1763 for now. */
1764 else if (processing_template_decl)
1765 return build_qualified_name (TREE_TYPE (decl),
1766 qualifying_scope,
c5c8755a 1767 decl,
ab11c13f 1768 /*template_p=*/false);
a3f10e50
NS
1769 else
1770 {
1771 tree access_type = TREE_TYPE (object);
9f01ded6 1772
02022f3a 1773 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
0e69fdf0 1774 decl, tf_warning_or_error);
a723baf1
MM
1775
1776 /* If the data member was named `C::M', convert `*this' to `C'
1777 first. */
1778 if (qualifying_scope)
1779 {
1780 tree binfo = NULL_TREE;
1781 object = build_scoped_ref (object, qualifying_scope,
1782 &binfo);
1783 }
1784
1785 return build_class_member_access_expr (object, decl,
1786 /*access_path=*/NULL_TREE,
5ade1ed2
DG
1787 /*preserve_reference=*/false,
1788 tf_warning_or_error);
a723baf1
MM
1789 }
1790}
1791
aa373032
DS
1792/* If we are currently parsing a template and we encountered a typedef
1793 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1794 adds the typedef to a list tied to the current template.
b52259e1 1795 At template instantiation time, that list is walked and access check
aa373032
DS
1796 performed for each typedef.
1797 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1798
1799void
1800add_typedef_to_current_template_for_access_check (tree typedef_decl,
1801 tree context,
1802 location_t location)
1803{
1804 tree template_info = NULL;
1805 tree cs = current_scope ();
1806
1807 if (!is_typedef_decl (typedef_decl)
1808 || !context
1809 || !CLASS_TYPE_P (context)
1810 || !cs)
1811 return;
1812
1813 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1814 template_info = get_template_info (cs);
1815
1816 if (template_info
1817 && TI_TEMPLATE (template_info)
1818 && !currently_open_class (context))
1819 append_type_to_template_for_access_check (cs, typedef_decl,
1820 context, location);
1821}
1822
ee76b931
MM
1823/* DECL was the declaration to which a qualified-id resolved. Issue
1824 an error message if it is not accessible. If OBJECT_TYPE is
1825 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1826 type of `*x', or `x', respectively. If the DECL was named as
1827 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1828
1829void
c8094d83
MS
1830check_accessibility_of_qualified_id (tree decl,
1831 tree object_type,
ee76b931
MM
1832 tree nested_name_specifier)
1833{
1834 tree scope;
1835 tree qualifying_type = NULL_TREE;
95b4aca6 1836
d0940d56
DS
1837 /* If we are parsing a template declaration and if decl is a typedef,
1838 add it to a list tied to the template.
1839 At template instantiation time, that list will be walked and
1840 access check performed. */
aa373032
DS
1841 add_typedef_to_current_template_for_access_check (decl,
1842 nested_name_specifier
1843 ? nested_name_specifier
1844 : DECL_CONTEXT (decl),
1845 input_location);
d0940d56 1846
77880ae4 1847 /* If we're not checking, return immediately. */
95b4aca6
NS
1848 if (deferred_access_no_check)
1849 return;
c8094d83 1850
ee76b931
MM
1851 /* Determine the SCOPE of DECL. */
1852 scope = context_for_name_lookup (decl);
1853 /* If the SCOPE is not a type, then DECL is not a member. */
1854 if (!TYPE_P (scope))
1855 return;
1856 /* Compute the scope through which DECL is being accessed. */
c8094d83 1857 if (object_type
ee76b931
MM
1858 /* OBJECT_TYPE might not be a class type; consider:
1859
1860 class A { typedef int I; };
1861 I *p;
1862 p->A::I::~I();
1863
0cbd7506 1864 In this case, we will have "A::I" as the DECL, but "I" as the
ee76b931
MM
1865 OBJECT_TYPE. */
1866 && CLASS_TYPE_P (object_type)
1867 && DERIVED_FROM_P (scope, object_type))
1868 /* If we are processing a `->' or `.' expression, use the type of the
1869 left-hand side. */
1870 qualifying_type = object_type;
1871 else if (nested_name_specifier)
1872 {
1873 /* If the reference is to a non-static member of the
1874 current class, treat it as if it were referenced through
1875 `this'. */
adb50dfb 1876 tree ct;
ee76b931
MM
1877 if (DECL_NONSTATIC_MEMBER_P (decl)
1878 && current_class_ptr
adb50dfb
VV
1879 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1880 qualifying_type = ct;
ee76b931
MM
1881 /* Otherwise, use the type indicated by the
1882 nested-name-specifier. */
1883 else
1884 qualifying_type = nested_name_specifier;
1885 }
1886 else
1887 /* Otherwise, the name must be from the current class or one of
1888 its bases. */
1889 qualifying_type = currently_open_derived_class (scope);
1890
fc429748
MM
1891 if (qualifying_type
1892 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1893 or similar in a default argument value. */
1894 && CLASS_TYPE_P (qualifying_type)
1895 && !dependent_type_p (qualifying_type))
02022f3a 1896 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
0e69fdf0 1897 decl, tf_warning_or_error);
ee76b931
MM
1898}
1899
1900/* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1901 class named to the left of the "::" operator. DONE is true if this
1902 expression is a complete postfix-expression; it is false if this
1903 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
02ed62dd
MM
1904 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1905 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1906 is true iff this qualified name appears as a template argument. */
ee76b931
MM
1907
1908tree
3db45ab5
MS
1909finish_qualified_id_expr (tree qualifying_class,
1910 tree expr,
02ed62dd 1911 bool done,
3db45ab5 1912 bool address_p,
02ed62dd 1913 bool template_p,
a378996b
PC
1914 bool template_arg_p,
1915 tsubst_flags_t complain)
ee76b931 1916{
d4f0f205
MM
1917 gcc_assert (TYPE_P (qualifying_class));
1918
5e08432e
MM
1919 if (error_operand_p (expr))
1920 return error_mark_node;
1921
2e649151
PC
1922 if ((DECL_P (expr) || BASELINK_P (expr))
1923 && !mark_used (expr, complain))
1924 return error_mark_node;
d4f0f205 1925
02ed62dd
MM
1926 if (template_p)
1927 check_template_keyword (expr);
1928
ee76b931
MM
1929 /* If EXPR occurs as the operand of '&', use special handling that
1930 permits a pointer-to-member. */
1931 if (address_p && done)
1932 {
1933 if (TREE_CODE (expr) == SCOPE_REF)
1934 expr = TREE_OPERAND (expr, 1);
c8094d83 1935 expr = build_offset_ref (qualifying_class, expr,
a378996b 1936 /*address_p=*/true, complain);
ee76b931
MM
1937 return expr;
1938 }
1939
d348f172
JM
1940 /* No need to check access within an enum. */
1941 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1942 return expr;
1943
02ed62dd
MM
1944 /* Within the scope of a class, turn references to non-static
1945 members into expression of the form "this->...". */
1946 if (template_arg_p)
1947 /* But, within a template argument, we do not want make the
1948 transformation, as there is no "this" pointer. */
1949 ;
1950 else if (TREE_CODE (expr) == FIELD_DECL)
43854f72
SB
1951 {
1952 push_deferring_access_checks (dk_no_check);
2defb926 1953 expr = finish_non_static_data_member (expr, NULL_TREE,
43854f72
SB
1954 qualifying_class);
1955 pop_deferring_access_checks ();
1956 }
ee76b931
MM
1957 else if (BASELINK_P (expr) && !processing_template_decl)
1958 {
ee76b931 1959 /* See if any of the functions are non-static members. */
7e361ae6 1960 /* If so, the expression may be relative to 'this'. */
294e855f 1961 if (!shared_member_p (expr)
0ef811d7
JM
1962 && current_class_ptr
1963 && DERIVED_FROM_P (qualifying_class,
1964 current_nonlambda_class_type ()))
c8094d83 1965 expr = (build_class_member_access_expr
0ef811d7 1966 (maybe_dummy_object (qualifying_class, NULL),
ee76b931
MM
1967 expr,
1968 BASELINK_ACCESS_BINFO (expr),
5ade1ed2 1969 /*preserve_reference=*/false,
a378996b 1970 complain));
ee76b931 1971 else if (done)
a5ac359a
MM
1972 /* The expression is a qualified name whose address is not
1973 being taken. */
a378996b
PC
1974 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1975 complain);
ee76b931 1976 }
e467c9d2
JM
1977 else if (BASELINK_P (expr))
1978 ;
1979 else
1980 {
e467c9d2
JM
1981 /* In a template, return a SCOPE_REF for most qualified-ids
1982 so that we can check access at instantiation time. But if
1983 we're looking at a member of the current instantiation, we
1984 know we have access and building up the SCOPE_REF confuses
1985 non-type template argument handling. */
1986 if (processing_template_decl
1987 && !currently_open_class (qualifying_class))
1988 expr = build_qualified_name (TREE_TYPE (expr),
1989 qualifying_class, expr,
1990 template_p);
1e8671f7
JM
1991
1992 expr = convert_from_reference (expr);
e467c9d2 1993 }
ee76b931
MM
1994
1995 return expr;
1996}
1997
b69b1501
MM
1998/* Begin a statement-expression. The value returned must be passed to
1999 finish_stmt_expr. */
b4c4a9ec 2000
c8094d83 2001tree
3a978d72 2002begin_stmt_expr (void)
b4c4a9ec 2003{
325c3691 2004 return push_stmt_list ();
35b1567d
BC
2005}
2006
a5bcc582 2007/* Process the final expression of a statement expression. EXPR can be
85a56c9d
MM
2008 NULL, if the final expression is empty. Return a STATEMENT_LIST
2009 containing all the statements in the statement-expression, or
2010 ERROR_MARK_NODE if there was an error. */
a5bcc582
NS
2011
2012tree
325c3691 2013finish_stmt_expr_expr (tree expr, tree stmt_expr)
a5bcc582 2014{
72e4661a 2015 if (error_operand_p (expr))
76b9a2a1
AP
2016 {
2017 /* The type of the statement-expression is the type of the last
2018 expression. */
2019 TREE_TYPE (stmt_expr) = error_mark_node;
2020 return error_mark_node;
2021 }
c8094d83 2022
85a56c9d 2023 /* If the last statement does not have "void" type, then the value
3db45ab5 2024 of the last statement is the value of the entire expression. */
a5bcc582
NS
2025 if (expr)
2026 {
c6c7698d
JM
2027 tree type = TREE_TYPE (expr);
2028
2029 if (processing_template_decl)
2030 {
c2255bc4 2031 expr = build_stmt (input_location, EXPR_STMT, expr);
c6c7698d
JM
2032 expr = add_stmt (expr);
2033 /* Mark the last statement so that we can recognize it as such at
2034 template-instantiation time. */
2035 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2036 }
2037 else if (VOID_TYPE_P (type))
a5bcc582 2038 {
c6c7698d
JM
2039 /* Just treat this like an ordinary statement. */
2040 expr = finish_expr_stmt (expr);
2041 }
2042 else
2043 {
2044 /* It actually has a value we need to deal with. First, force it
2045 to be an rvalue so that we won't need to build up a copy
2046 constructor call later when we try to assign it to something. */
574cfaa4 2047 expr = force_rvalue (expr, tf_warning_or_error);
85a56c9d
MM
2048 if (error_operand_p (expr))
2049 return error_mark_node;
c6c7698d
JM
2050
2051 /* Update for array-to-pointer decay. */
2692eb7d 2052 type = TREE_TYPE (expr);
c6c7698d
JM
2053
2054 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2055 normal statement, but don't convert to void or actually add
2056 the EXPR_STMT. */
2057 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2058 expr = maybe_cleanup_point_expr (expr);
2059 add_stmt (expr);
85a56c9d 2060 }
c6c7698d 2061
85a56c9d
MM
2062 /* The type of the statement-expression is the type of the last
2063 expression. */
2064 TREE_TYPE (stmt_expr) = type;
a5bcc582 2065 }
c8094d83 2066
85a56c9d 2067 return stmt_expr;
a5bcc582
NS
2068}
2069
303b7406
NS
2070/* Finish a statement-expression. EXPR should be the value returned
2071 by the previous begin_stmt_expr. Returns an expression
2072 representing the statement-expression. */
b4c4a9ec 2073
c8094d83 2074tree
325c3691 2075finish_stmt_expr (tree stmt_expr, bool has_no_scope)
b4c4a9ec 2076{
85a56c9d
MM
2077 tree type;
2078 tree result;
325c3691 2079
85a56c9d 2080 if (error_operand_p (stmt_expr))
d7b5fa31
JJ
2081 {
2082 pop_stmt_list (stmt_expr);
2083 return error_mark_node;
2084 }
325c3691 2085
85a56c9d 2086 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
325c3691 2087
85a56c9d
MM
2088 type = TREE_TYPE (stmt_expr);
2089 result = pop_stmt_list (stmt_expr);
c6c7698d 2090 TREE_TYPE (result) = type;
6f80451c 2091
a5bcc582 2092 if (processing_template_decl)
325c3691
RH
2093 {
2094 result = build_min (STMT_EXPR, type, result);
2095 TREE_SIDE_EFFECTS (result) = 1;
2096 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2097 }
c6c7698d 2098 else if (CLASS_TYPE_P (type))
a5bcc582 2099 {
c6c7698d
JM
2100 /* Wrap the statement-expression in a TARGET_EXPR so that the
2101 temporary object created by the final expression is destroyed at
2102 the end of the full-expression containing the
2103 statement-expression. */
574cfaa4 2104 result = force_target_expr (type, result, tf_warning_or_error);
a5bcc582 2105 }
325c3691 2106
b4c4a9ec
MM
2107 return result;
2108}
2109
c2acde1e
JM
2110/* Returns the expression which provides the value of STMT_EXPR. */
2111
2112tree
2113stmt_expr_value_expr (tree stmt_expr)
2114{
2115 tree t = STMT_EXPR_STMT (stmt_expr);
2116
2117 if (TREE_CODE (t) == BIND_EXPR)
2118 t = BIND_EXPR_BODY (t);
2119
9de0e916 2120 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
c2acde1e
JM
2121 t = STATEMENT_LIST_TAIL (t)->stmt;
2122
2123 if (TREE_CODE (t) == EXPR_STMT)
2124 t = EXPR_STMT_EXPR (t);
2125
2126 return t;
2127}
2128
9af66ed1
DS
2129/* Return TRUE iff EXPR_STMT is an empty list of
2130 expression statements. */
2131
2132bool
2133empty_expr_stmt_p (tree expr_stmt)
2134{
2135 tree body = NULL_TREE;
2136
632f2871 2137 if (expr_stmt == void_node)
489df541
DS
2138 return true;
2139
9af66ed1
DS
2140 if (expr_stmt)
2141 {
2142 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2143 body = EXPR_STMT_EXPR (expr_stmt);
2144 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2145 body = expr_stmt;
2146 }
2147
489df541
DS
2148 if (body)
2149 {
2150 if (TREE_CODE (body) == STATEMENT_LIST)
2151 return tsi_end_p (tsi_start (body));
2152 else
2153 return empty_expr_stmt_p (body);
2154 }
9af66ed1
DS
2155 return false;
2156}
2157
b3445994 2158/* Perform Koenig lookup. FN is the postfix-expression representing
fa531100 2159 the function (or functions) to call; ARGS are the arguments to the
cdc23b1b 2160 call. Returns the functions to be considered by overload resolution. */
b3445994
MM
2161
2162tree
cdc23b1b 2163perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
94df301f 2164 tsubst_flags_t complain)
b3445994
MM
2165{
2166 tree identifier = NULL_TREE;
2167 tree functions = NULL_TREE;
d095e03c 2168 tree tmpl_args = NULL_TREE;
4e6a9725 2169 bool template_id = false;
d095e03c
JM
2170
2171 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2172 {
4e6a9725
JM
2173 /* Use a separate flag to handle null args. */
2174 template_id = true;
d095e03c
JM
2175 tmpl_args = TREE_OPERAND (fn, 1);
2176 fn = TREE_OPERAND (fn, 0);
2177 }
b3445994
MM
2178
2179 /* Find the name of the overloaded function. */
9dc6f476 2180 if (identifier_p (fn))
b3445994
MM
2181 identifier = fn;
2182 else if (is_overloaded_fn (fn))
2183 {
2184 functions = fn;
2185 identifier = DECL_NAME (get_first_fn (functions));
2186 }
2187 else if (DECL_P (fn))
2188 {
2189 functions = fn;
2190 identifier = DECL_NAME (fn);
2191 }
2192
2193 /* A call to a namespace-scope function using an unqualified name.
2194
2195 Do Koenig lookup -- unless any of the arguments are
2196 type-dependent. */
d095e03c
JM
2197 if (!any_type_dependent_arguments_p (args)
2198 && !any_dependent_template_arguments_p (tmpl_args))
b3445994 2199 {
cdc23b1b 2200 fn = lookup_arg_dependent (identifier, functions, args);
b3445994 2201 if (!fn)
94df301f
JM
2202 {
2203 /* The unqualified name could not be resolved. */
2204 if (complain)
2205 fn = unqualified_fn_lookup_error (identifier);
2206 else
2207 fn = identifier;
2208 }
b3445994 2209 }
b3445994 2210
4e6a9725
JM
2211 if (fn && template_id)
2212 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
d095e03c 2213
b3445994
MM
2214 return fn;
2215}
2216
c166b898
ILT
2217/* Generate an expression for `FN (ARGS)'. This may change the
2218 contents of ARGS.
4ba126e4
MM
2219
2220 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2221 as a virtual call, even if FN is virtual. (This flag is set when
2222 encountering an expression where the function name is explicitly
2223 qualified. For example a call to `X::f' never generates a virtual
2224 call.)
2225
2226 Returns code for the call. */
b4c4a9ec 2227
c8094d83 2228tree
9771b263 2229finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
c166b898 2230 bool koenig_p, tsubst_flags_t complain)
b4c4a9ec 2231{
d17811fd
MM
2232 tree result;
2233 tree orig_fn;
9771b263 2234 vec<tree, va_gc> *orig_args = NULL;
d17811fd 2235
c166b898 2236 if (fn == error_mark_node)
4ba126e4
MM
2237 return error_mark_node;
2238
4860b874 2239 gcc_assert (!TYPE_P (fn));
a759e627 2240
d17811fd 2241 orig_fn = fn;
d17811fd
MM
2242
2243 if (processing_template_decl)
2244 {
1770aeed
DS
2245 /* If the call expression is dependent, build a CALL_EXPR node
2246 with no type; type_dependent_expression_p recognizes
2247 expressions with no type as being dependent. */
d17811fd 2248 if (type_dependent_expression_p (fn)
1770aeed 2249 || any_type_dependent_arguments_p (*args)
712c948a
JM
2250 /* For a non-static member function that doesn't have an
2251 explicit object argument, we need to specifically
1770aeed
DS
2252 test the type dependency of the "this" pointer because it
2253 is not included in *ARGS even though it is considered to
2254 be part of the list of arguments. Note that this is
2255 related to CWG issues 515 and 1005. */
712c948a
JM
2256 || (TREE_CODE (fn) != COMPONENT_REF
2257 && non_static_member_function_p (fn)
1770aeed
DS
2258 && current_class_ref
2259 && type_dependent_expression_p (current_class_ref)))
6d80c4b9 2260 {
c166b898 2261 result = build_nt_call_vec (fn, *args);
8400e75e 2262 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
5094a795 2263 KOENIG_LOOKUP_P (result) = koenig_p;
be461b8f
JJ
2264 if (cfun)
2265 {
2266 do
2267 {
2268 tree fndecl = OVL_CURRENT (fn);
2269 if (TREE_CODE (fndecl) != FUNCTION_DECL
2270 || !TREE_THIS_VOLATILE (fndecl))
2271 break;
2272 fn = OVL_NEXT (fn);
2273 }
2274 while (fn);
2275 if (!fn)
2276 current_function_returns_abnormally = 1;
2277 }
6d80c4b9
MM
2278 return result;
2279 }
c166b898 2280 orig_args = make_tree_vector_copy (*args);
d17811fd
MM
2281 if (!BASELINK_P (fn)
2282 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2283 && TREE_TYPE (fn) != unknown_type_node)
2284 fn = build_non_dependent_expr (fn);
c166b898 2285 make_args_non_dependent (*args);
d17811fd
MM
2286 }
2287
deb9642d
JM
2288 if (TREE_CODE (fn) == COMPONENT_REF)
2289 {
2290 tree member = TREE_OPERAND (fn, 1);
2291 if (BASELINK_P (member))
2292 {
2293 tree object = TREE_OPERAND (fn, 0);
2294 return build_new_method_call (object, member,
2295 args, NULL_TREE,
2296 (disallow_virtual
2297 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2298 : LOOKUP_NORMAL),
2299 /*fn_p=*/NULL,
2300 complain);
2301 }
2302 }
2303
53677b17
PC
2304 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2305 if (TREE_CODE (fn) == ADDR_EXPR
2306 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2307 fn = TREE_OPERAND (fn, 0);
2308
eff3a276
MM
2309 if (is_overloaded_fn (fn))
2310 fn = baselink_for_fns (fn);
a723baf1 2311
d17811fd 2312 result = NULL_TREE;
4ba126e4 2313 if (BASELINK_P (fn))
03d82991 2314 {
4ba126e4
MM
2315 tree object;
2316
2317 /* A call to a member function. From [over.call.func]:
2318
2319 If the keyword this is in scope and refers to the class of
2320 that member function, or a derived class thereof, then the
2321 function call is transformed into a qualified function call
2322 using (*this) as the postfix-expression to the left of the
2323 . operator.... [Otherwise] a contrived object of type T
c8094d83 2324 becomes the implied object argument.
4ba126e4 2325
38f1276b 2326 In this situation:
4ba126e4
MM
2327
2328 struct A { void f(); };
2329 struct B : public A {};
2330 struct C : public A { void g() { B::f(); }};
2331
38f1276b
JM
2332 "the class of that member function" refers to `A'. But 11.2
2333 [class.access.base] says that we need to convert 'this' to B* as
2334 part of the access, so we pass 'B' to maybe_dummy_object. */
b4c4a9ec 2335
38f1276b
JM
2336 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2337 NULL);
b4c4a9ec 2338
d17811fd
MM
2339 if (processing_template_decl)
2340 {
2341 if (type_dependent_expression_p (object))
c166b898
ILT
2342 {
2343 tree ret = build_nt_call_vec (orig_fn, orig_args);
2344 release_tree_vector (orig_args);
2345 return ret;
2346 }
d17811fd
MM
2347 object = build_non_dependent_expr (object);
2348 }
2349
2350 result = build_new_method_call (object, fn, args, NULL_TREE,
c8094d83 2351 (disallow_virtual
c2aa8dc9
JM
2352 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2353 : LOOKUP_NORMAL),
5ade1ed2
DG
2354 /*fn_p=*/NULL,
2355 complain);
4ba126e4
MM
2356 }
2357 else if (is_overloaded_fn (fn))
16c35a1f
RH
2358 {
2359 /* If the function is an overloaded builtin, resolve it. */
2360 if (TREE_CODE (fn) == FUNCTION_DECL
58646b77
PB
2361 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2362 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
c2255bc4 2363 result = resolve_overloaded_builtin (input_location, fn, *args);
16c35a1f
RH
2364
2365 if (!result)
0d23cf7a
JJ
2366 {
2367 if (warn_sizeof_pointer_memaccess
32ad49af 2368 && (complain & tf_warning)
9771b263 2369 && !vec_safe_is_empty (*args)
0d23cf7a
JJ
2370 && !processing_template_decl)
2371 {
3a785c97
JJ
2372 location_t sizeof_arg_loc[3];
2373 tree sizeof_arg[3];
2374 unsigned int i;
2375 for (i = 0; i < 3; i++)
2376 {
2377 tree t;
2378
2379 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2380 sizeof_arg[i] = NULL_TREE;
9771b263 2381 if (i >= (*args)->length ())
3a785c97 2382 continue;
9771b263 2383 t = (**args)[i];
3a785c97
JJ
2384 if (TREE_CODE (t) != SIZEOF_EXPR)
2385 continue;
2386 if (SIZEOF_EXPR_TYPE_P (t))
2387 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2388 else
2389 sizeof_arg[i] = TREE_OPERAND (t, 0);
2390 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2391 }
0d23cf7a 2392 sizeof_pointer_memaccess_warning
3a785c97 2393 (sizeof_arg_loc, fn, *args,
0d23cf7a
JJ
2394 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2395 }
2396
2397 /* A call to a namespace-scope function. */
2398 result = build_new_function_call (fn, args, koenig_p, complain);
2399 }
16c35a1f 2400 }
a723baf1
MM
2401 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2402 {
9771b263 2403 if (!vec_safe_is_empty (*args))
a723baf1
MM
2404 error ("arguments to destructor are not allowed");
2405 /* Mark the pseudo-destructor call as having side-effects so
2406 that we do not issue warnings about its use. */
2407 result = build1 (NOP_EXPR,
2408 void_type_node,
2409 TREE_OPERAND (fn, 0));
2410 TREE_SIDE_EFFECTS (result) = 1;
a723baf1 2411 }
4ba126e4 2412 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
d17811fd
MM
2413 /* If the "function" is really an object of class type, it might
2414 have an overloaded `operator ()'. */
c166b898 2415 result = build_op_call (fn, args, complain);
16c35a1f 2416
d17811fd
MM
2417 if (!result)
2418 /* A call where the function is unknown. */
c166b898 2419 result = cp_build_function_call_vec (fn, args, complain);
4ba126e4 2420
bb4586d3 2421 if (processing_template_decl && result != error_mark_node)
6d80c4b9 2422 {
591cb3cf 2423 if (INDIRECT_REF_P (result))
bb4586d3 2424 result = TREE_OPERAND (result, 0);
c166b898 2425 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
6e04dcd5 2426 SET_EXPR_LOCATION (result, input_location);
5094a795 2427 KOENIG_LOOKUP_P (result) = koenig_p;
c166b898 2428 release_tree_vector (orig_args);
bb4586d3 2429 result = convert_from_reference (result);
6d80c4b9 2430 }
c166b898 2431
3a2cb4d0
JM
2432 if (koenig_p)
2433 {
2434 /* Free garbage OVERLOADs from arg-dependent lookup. */
2435 tree next = NULL_TREE;
2436 for (fn = orig_fn;
2437 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2438 fn = next)
2439 {
2440 if (processing_template_decl)
2441 /* In a template, we'll re-use them at instantiation time. */
2442 OVL_ARG_DEPENDENT (fn) = false;
2443 else
2444 {
2445 next = OVL_CHAIN (fn);
2446 ggc_free (fn);
2447 }
2448 }
2449 }
2450
d17811fd 2451 return result;
b4c4a9ec
MM
2452}
2453
2454/* Finish a call to a postfix increment or decrement or EXPR. (Which
2455 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2456 POSTDECREMENT_EXPR.) */
2457
c8094d83 2458tree
3a978d72 2459finish_increment_expr (tree expr, enum tree_code code)
b4c4a9ec 2460{
4fe977f2 2461 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
b4c4a9ec
MM
2462}
2463
2464/* Finish a use of `this'. Returns an expression for `this'. */
2465
c8094d83 2466tree
3a978d72 2467finish_this_expr (void)
b4c4a9ec 2468{
2bf492a1 2469 tree result = NULL_TREE;
b4c4a9ec 2470
c6be04ad
JM
2471 if (current_class_ptr)
2472 {
2473 tree type = TREE_TYPE (current_class_ref);
2474
2475 /* In a lambda expression, 'this' refers to the captured 'this'. */
2476 if (LAMBDA_TYPE_P (type))
0b360a07 2477 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
c6be04ad
JM
2478 else
2479 result = current_class_ptr;
c6be04ad 2480 }
b4c4a9ec 2481
2bf492a1
JM
2482 if (result)
2483 /* The keyword 'this' is a prvalue expression. */
2484 return rvalue (result);
374b2837 2485
2bf492a1
JM
2486 tree fn = current_nonlambda_function ();
2487 if (fn && DECL_STATIC_FUNCTION_P (fn))
2488 error ("%<this%> is unavailable for static member functions");
2489 else if (fn)
2490 error ("invalid use of %<this%> in non-member function");
2491 else
2492 error ("invalid use of %<this%> at top level");
2493 return error_mark_node;
b4c4a9ec
MM
2494}
2495
a723baf1
MM
2496/* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2497 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2498 the TYPE for the type given. If SCOPE is non-NULL, the expression
2499 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
b4c4a9ec 2500
c8094d83 2501tree
2dc6ed87
PC
2502finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2503 location_t loc)
b4c4a9ec 2504{
09b1ccd6 2505 if (object == error_mark_node || destructor == error_mark_node)
a723baf1 2506 return error_mark_node;
40242ccf 2507
50bc768d 2508 gcc_assert (TYPE_P (destructor));
b4c4a9ec 2509
a723baf1
MM
2510 if (!processing_template_decl)
2511 {
2512 if (scope == error_mark_node)
2513 {
2dc6ed87 2514 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
a723baf1
MM
2515 return error_mark_node;
2516 }
3df70c62
JM
2517 if (is_auto (destructor))
2518 destructor = TREE_TYPE (object);
5cf10afb
AP
2519 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2520 {
2dc6ed87
PC
2521 error_at (loc,
2522 "qualified type %qT does not match destructor name ~%qT",
2523 scope, destructor);
5cf10afb
AP
2524 return error_mark_node;
2525 }
2526
c8094d83 2527
26bcf8fc
MM
2528 /* [expr.pseudo] says both:
2529
0cbd7506 2530 The type designated by the pseudo-destructor-name shall be
26bcf8fc
MM
2531 the same as the object type.
2532
0cbd7506 2533 and:
26bcf8fc 2534
0cbd7506 2535 The cv-unqualified versions of the object type and of the
26bcf8fc
MM
2536 type designated by the pseudo-destructor-name shall be the
2537 same type.
2538
0cbd7506
MS
2539 We implement the more generous second sentence, since that is
2540 what most other compilers do. */
c8094d83 2541 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
26bcf8fc 2542 destructor))
a723baf1 2543 {
2dc6ed87 2544 error_at (loc, "%qE is not of type %qT", object, destructor);
a723baf1
MM
2545 return error_mark_node;
2546 }
2547 }
b4c4a9ec 2548
2dc6ed87
PC
2549 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2550 scope, destructor);
b4c4a9ec
MM
2551}
2552
ce4a0391
MM
2553/* Finish an expression of the form CODE EXPR. */
2554
2555tree
e59baf05
JM
2556finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2557 tsubst_flags_t complain)
ce4a0391 2558{
e59baf05
JM
2559 tree result = build_x_unary_op (loc, code, expr, complain);
2560 if ((complain & tf_warning)
2561 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
c2255bc4 2562 overflow_warning (input_location, result);
59c0753d 2563
ce4a0391
MM
2564 return result;
2565}
2566
a723baf1 2567/* Finish a compound-literal expression. TYPE is the type to which
09357846 2568 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
a723baf1
MM
2569
2570tree
834aa426
JM
2571finish_compound_literal (tree type, tree compound_literal,
2572 tsubst_flags_t complain)
a723baf1 2573{
326a4d4e
JJ
2574 if (type == error_mark_node)
2575 return error_mark_node;
2576
76186d20
JM
2577 if (TREE_CODE (type) == REFERENCE_TYPE)
2578 {
2579 compound_literal
2580 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2581 complain);
2582 return cp_build_c_cast (type, compound_literal, complain);
2583 }
2584
2b643eda
MM
2585 if (!TYPE_OBJ_P (type))
2586 {
834aa426
JM
2587 if (complain & tf_error)
2588 error ("compound literal of non-object type %qT", type);
2b643eda
MM
2589 return error_mark_node;
2590 }
2591
a723baf1 2592 if (processing_template_decl)
a723baf1 2593 {
e92fb501
MM
2594 TREE_TYPE (compound_literal) = type;
2595 /* Mark the expression as a compound literal. */
2596 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2597 return compound_literal;
a723baf1
MM
2598 }
2599
df794884 2600 type = complete_type (type);
09357846
JM
2601
2602 if (TYPE_NON_AGGREGATE_CLASS (type))
2603 {
2604 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2605 everywhere that deals with function arguments would be a pain, so
2606 just wrap it in a TREE_LIST. The parser set a flag so we know
2607 that it came from T{} rather than T({}). */
2608 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2609 compound_literal = build_tree_list (NULL_TREE, compound_literal);
834aa426 2610 return build_functional_cast (type, compound_literal, complain);
09357846
JM
2611 }
2612
23bee8f4
JJ
2613 if (TREE_CODE (type) == ARRAY_TYPE
2614 && check_array_initializer (NULL_TREE, type, compound_literal))
2615 return error_mark_node;
754af126 2616 compound_literal = reshape_init (type, compound_literal, complain);
25339f10 2617 if (SCALAR_TYPE_P (type)
c428869b 2618 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
6a6bdc3d
PC
2619 && !check_narrowing (type, compound_literal, complain))
2620 return error_mark_node;
80c6dcf5
JM
2621 if (TREE_CODE (type) == ARRAY_TYPE
2622 && TYPE_DOMAIN (type) == NULL_TREE)
2623 {
2624 cp_complete_array_type_or_error (&type, compound_literal,
2625 false, complain);
2626 if (type == error_mark_node)
2627 return error_mark_node;
2628 }
754af126 2629 compound_literal = digest_init (type, compound_literal, complain);
8c53f5e2
JM
2630 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2631 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
a63940ba
JM
2632 /* Put static/constant array temporaries in static variables, but always
2633 represent class temporaries with TARGET_EXPR so we elide copies. */
2634 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2635 && TREE_CODE (type) == ARRAY_TYPE
fa9ef321 2636 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
a63940ba
JM
2637 && initializer_constant_valid_p (compound_literal, type))
2638 {
2639 tree decl = create_temporary_var (type);
2640 DECL_INITIAL (decl) = compound_literal;
2641 TREE_STATIC (decl) = 1;
2642 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2643 {
2644 /* 5.19 says that a constant expression can include an
2645 lvalue-rvalue conversion applied to "a glvalue of literal type
2646 that refers to a non-volatile temporary object initialized
2647 with a constant expression". Rather than try to communicate
2648 that this VAR_DECL is a temporary, just mark it constexpr. */
2649 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2650 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2651 TREE_CONSTANT (decl) = true;
2652 }
2653 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2654 decl = pushdecl_top_level (decl);
2655 DECL_NAME (decl) = make_anon_name ();
2656 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
eca7fc57
JM
2657 /* Make sure the destructor is callable. */
2658 tree clean = cxx_maybe_build_cleanup (decl, complain);
2659 if (clean == error_mark_node)
2660 return error_mark_node;
a63940ba
JM
2661 return decl;
2662 }
2663 else
574cfaa4 2664 return get_target_expr_sfinae (compound_literal, complain);
a723baf1
MM
2665}
2666
5f261ba9
MM
2667/* Return the declaration for the function-name variable indicated by
2668 ID. */
2669
2670tree
2671finish_fname (tree id)
2672{
2673 tree decl;
c8094d83 2674
3ba09659 2675 decl = fname_decl (input_location, C_RID_CODE (id), id);
f2381074
KT
2676 if (processing_template_decl && current_function_decl
2677 && decl != error_mark_node)
10b1d5e7 2678 decl = DECL_NAME (decl);
5f261ba9
MM
2679 return decl;
2680}
2681
8014a339 2682/* Finish a translation unit. */
ce4a0391 2683
c8094d83 2684void
3a978d72 2685finish_translation_unit (void)
ce4a0391
MM
2686{
2687 /* In case there were missing closebraces,
2688 get us back to the global binding level. */
273a708f 2689 pop_everything ();
ce4a0391
MM
2690 while (current_namespace != global_namespace)
2691 pop_namespace ();
0ba8a114 2692
c6002625 2693 /* Do file scope __FUNCTION__ et al. */
0ba8a114 2694 finish_fname_decls ();
ce4a0391
MM
2695}
2696
b4c4a9ec
MM
2697/* Finish a template type parameter, specified as AGGR IDENTIFIER.
2698 Returns the parameter. */
2699
c8094d83 2700tree
3a978d72 2701finish_template_type_parm (tree aggr, tree identifier)
b4c4a9ec 2702{
6eabb241 2703 if (aggr != class_type_node)
b4c4a9ec 2704 {
cbe5f3b3 2705 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
b4c4a9ec
MM
2706 aggr = class_type_node;
2707 }
2708
2709 return build_tree_list (aggr, identifier);
2710}
2711
2712/* Finish a template template parameter, specified as AGGR IDENTIFIER.
2713 Returns the parameter. */
2714
c8094d83 2715tree
3a978d72 2716finish_template_template_parm (tree aggr, tree identifier)
b4c4a9ec 2717{
c2255bc4
AH
2718 tree decl = build_decl (input_location,
2719 TYPE_DECL, identifier, NULL_TREE);
971e17ff 2720
b4c4a9ec
MM
2721 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2722 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2723 DECL_TEMPLATE_RESULT (tmpl) = decl;
c727aa5e 2724 DECL_ARTIFICIAL (decl) = 1;
971e17ff
AS
2725
2726 // Associate the constraints with the underlying declaration,
2727 // not the template.
2728 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2729 tree constr = build_constraints (reqs, NULL_TREE);
2730 set_constraints (decl, constr);
2731
b4c4a9ec
MM
2732 end_template_decl ();
2733
50bc768d 2734 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
b37bf5bd 2735
85d85234
DG
2736 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2737 /*is_primary=*/true, /*is_partial=*/false,
2738 /*is_friend=*/0);
2739
b4c4a9ec
MM
2740 return finish_template_type_parm (aggr, tmpl);
2741}
ce4a0391 2742
8ba658ee
MM
2743/* ARGUMENT is the default-argument value for a template template
2744 parameter. If ARGUMENT is invalid, issue error messages and return
2745 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2746
2747tree
2748check_template_template_default_arg (tree argument)
2749{
2750 if (TREE_CODE (argument) != TEMPLATE_DECL
2751 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
8ba658ee
MM
2752 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2753 {
a3a503a5 2754 if (TREE_CODE (argument) == TYPE_DECL)
e488a090
VR
2755 error ("invalid use of type %qT as a default value for a template "
2756 "template-parameter", TREE_TYPE (argument));
a3a503a5
GB
2757 else
2758 error ("invalid default argument for a template template parameter");
8ba658ee
MM
2759 return error_mark_node;
2760 }
2761
2762 return argument;
2763}
2764
ce4a0391
MM
2765/* Begin a class definition, as indicated by T. */
2766
2767tree
e3c888eb 2768begin_class_definition (tree t)
ce4a0391 2769{
7d2f0ecd 2770 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
7437519c
ZW
2771 return error_mark_node;
2772
522d6614
NS
2773 if (processing_template_parmlist)
2774 {
a82e1a7d 2775 error ("definition of %q#T inside template parameter list", t);
522d6614
NS
2776 return error_mark_node;
2777 }
ebf0bf7f
JJ
2778
2779 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2780 are passed the same as decimal scalar types. */
cd924144
JM
2781 if (TREE_CODE (t) == RECORD_TYPE
2782 && !processing_template_decl)
ebf0bf7f 2783 {
cd924144
JM
2784 tree ns = TYPE_CONTEXT (t);
2785 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2786 && DECL_CONTEXT (ns) == std_node
935c0a5d 2787 && DECL_NAME (ns)
cd924144
JM
2788 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2789 {
2790 const char *n = TYPE_NAME_STRING (t);
2791 if ((strcmp (n, "decimal32") == 0)
2792 || (strcmp (n, "decimal64") == 0)
2793 || (strcmp (n, "decimal128") == 0))
2794 TYPE_TRANSPARENT_AGGR (t) = 1;
2795 }
ebf0bf7f
JJ
2796 }
2797
47ee8904
MM
2798 /* A non-implicit typename comes from code like:
2799
2800 template <typename T> struct A {
0cbd7506 2801 template <typename U> struct A<T>::B ...
47ee8904
MM
2802
2803 This is erroneous. */
2804 else if (TREE_CODE (t) == TYPENAME_TYPE)
2805 {
a82e1a7d 2806 error ("invalid definition of qualified type %qT", t);
47ee8904
MM
2807 t = error_mark_node;
2808 }
2809
9e1e64ec 2810 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
ce4a0391 2811 {
9e1e64ec 2812 t = make_class_type (RECORD_TYPE);
bd3d082e 2813 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
ce4a0391 2814 }
830fcda8 2815
4c571114 2816 if (TYPE_BEING_DEFINED (t))
ce4a0391 2817 {
9e1e64ec 2818 t = make_class_type (TREE_CODE (t));
bd3d082e 2819 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
ce4a0391 2820 }
ff350acd 2821 maybe_process_partial_specialization (t);
29370796 2822 pushclass (t);
ce4a0391 2823 TYPE_BEING_DEFINED (t) = 1;
5294e4c3 2824 class_binding_level->defining_class_p = 1;
b9e75696 2825
c0694c4b
MM
2826 if (flag_pack_struct)
2827 {
2828 tree v;
2829 TYPE_PACKED (t) = 1;
2830 /* Even though the type is being defined for the first time
2831 here, there might have been a forward declaration, so there
2832 might be cv-qualified variants of T. */
2833 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2834 TYPE_PACKED (v) = 1;
2835 }
ce4a0391
MM
2836 /* Reset the interface data, at the earliest possible
2837 moment, as it might have been set via a class foo;
2838 before. */
1951a1b6
JM
2839 if (! TYPE_ANONYMOUS_P (t))
2840 {
8400e75e
DM
2841 struct c_fileinfo *finfo = \
2842 get_fileinfo (LOCATION_FILE (input_location));
5d709b00 2843 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
1951a1b6 2844 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5d709b00 2845 (t, finfo->interface_unknown);
1951a1b6 2846 }
ce4a0391 2847 reset_specialization();
c8094d83 2848
b7975aed
MM
2849 /* Make a declaration for this class in its own scope. */
2850 build_self_reference ();
2851
830fcda8 2852 return t;
ce4a0391
MM
2853}
2854
61a127b3
MM
2855/* Finish the member declaration given by DECL. */
2856
2857void
3a978d72 2858finish_member_declaration (tree decl)
61a127b3
MM
2859{
2860 if (decl == error_mark_node || decl == NULL_TREE)
2861 return;
2862
2863 if (decl == void_type_node)
2864 /* The COMPONENT was a friend, not a member, and so there's
2865 nothing for us to do. */
2866 return;
2867
2868 /* We should see only one DECL at a time. */
910ad8de 2869 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
61a127b3
MM
2870
2871 /* Set up access control for DECL. */
c8094d83 2872 TREE_PRIVATE (decl)
61a127b3 2873 = (current_access_specifier == access_private_node);
c8094d83 2874 TREE_PROTECTED (decl)
61a127b3
MM
2875 = (current_access_specifier == access_protected_node);
2876 if (TREE_CODE (decl) == TEMPLATE_DECL)
2877 {
17aec3eb
RK
2878 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2879 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
61a127b3
MM
2880 }
2881
8d0d1915
JM
2882 /* Mark the DECL as a member of the current class, unless it's
2883 a member of an enumeration. */
2884 if (TREE_CODE (decl) != CONST_DECL)
2885 DECL_CONTEXT (decl) = current_class_type;
61a127b3 2886
b1d7b1c0 2887 /* Check for bare parameter packs in the member variable declaration. */
1ad8aeeb 2888 if (TREE_CODE (decl) == FIELD_DECL)
4439d02f 2889 {
7b3e2d46 2890 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4439d02f 2891 TREE_TYPE (decl) = error_mark_node;
7b3e2d46 2892 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
4439d02f
DG
2893 DECL_ATTRIBUTES (decl) = NULL_TREE;
2894 }
b1d7b1c0 2895
421844e7
MM
2896 /* [dcl.link]
2897
2898 A C language linkage is ignored for the names of class members
2899 and the member function type of class member functions. */
2900 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
5d2ed28c 2901 SET_DECL_LANGUAGE (decl, lang_cplusplus);
421844e7 2902
61a127b3
MM
2903 /* Put functions on the TYPE_METHODS list and everything else on the
2904 TYPE_FIELDS list. Note that these are built up in reverse order.
2905 We reverse them (to obtain declaration order) in finish_struct. */
9ededfc5 2906 if (DECL_DECLARES_FUNCTION_P (decl))
61a127b3
MM
2907 {
2908 /* We also need to add this function to the
2909 CLASSTYPE_METHOD_VEC. */
b77fe7b4
NS
2910 if (add_method (current_class_type, decl, NULL_TREE))
2911 {
5ce039df 2912 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
910ad8de 2913 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
b77fe7b4 2914 TYPE_METHODS (current_class_type) = decl;
f139561c 2915
b77fe7b4
NS
2916 maybe_add_class_template_decl_list (current_class_type, decl,
2917 /*friend_p=*/0);
2918 }
61a127b3 2919 }
fad882c6
JM
2920 /* Enter the DECL into the scope of the class, if the class
2921 isn't a closure (whose fields are supposed to be unnamed). */
2922 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2923 || pushdecl_class_level (decl))
61a127b3 2924 {
557831a9
FC
2925 if (TREE_CODE (decl) == USING_DECL)
2926 {
557831a9
FC
2927 /* For now, ignore class-scope USING_DECLS, so that
2928 debugging backends do not see them. */
2929 DECL_IGNORED_P (decl) = 1;
2930 }
2931
61a127b3
MM
2932 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2933 go at the beginning. The reason is that lookup_field_1
2934 searches the list in order, and we want a field name to
2935 override a type name so that the "struct stat hack" will
2936 work. In particular:
2937
2938 struct S { enum E { }; int E } s;
2939 s.E = 3;
2940
0e339752 2941 is valid. In addition, the FIELD_DECLs must be maintained in
61a127b3
MM
2942 declaration order so that class layout works as expected.
2943 However, we don't need that order until class layout, so we
2944 save a little time by putting FIELD_DECLs on in reverse order
2945 here, and then reversing them in finish_struct_1. (We could
2946 also keep a pointer to the correct insertion points in the
2947 list.) */
2948
2949 if (TREE_CODE (decl) == TYPE_DECL)
c8094d83 2950 TYPE_FIELDS (current_class_type)
61a127b3
MM
2951 = chainon (TYPE_FIELDS (current_class_type), decl);
2952 else
2953 {
910ad8de 2954 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
61a127b3
MM
2955 TYPE_FIELDS (current_class_type) = decl;
2956 }
8f032717 2957
c8094d83 2958 maybe_add_class_template_decl_list (current_class_type, decl,
f139561c 2959 /*friend_p=*/0);
61a127b3
MM
2960 }
2961}
2962
306ef644 2963/* Finish processing a complete template declaration. The PARMS are
36a117a5
MM
2964 the template parameters. */
2965
2966void
3a978d72 2967finish_template_decl (tree parms)
36a117a5
MM
2968{
2969 if (parms)
2970 end_template_decl ();
2971 else
2972 end_specialization ();
2973}
2974
971e17ff
AS
2975// Returns the template type of the class scope being entered. If we're
2976// entering a constrained class scope. TYPE is the class template
2977// scope being entered and we may need to match the intended type with
2978// a constrained specialization. For example:
2979//
2980// template<Object T>
2981// struct S { void f(); }; #1
2982//
2983// template<Object T>
2984// void S<T>::f() { } #2
2985//
2986// We check, in #2, that S<T> refers precisely to the type declared by
2987// #1 (i.e., that the constraints match). Note that the following should
2988// be an error since there is no specialization of S<T> that is
2989// unconstrained, but this is not diagnosed here.
2990//
2991// template<typename T>
2992// void S<T>::f() { }
2993//
2994// We cannot diagnose this problem here since this function also matches
2995// qualified template names that are not part of a definition. For example:
2996//
2997// template<Integral T, Floating_point U>
2998// typename pair<T, U>::first_type void f(T, U);
2999//
3000// Here, it is unlikely that there is a partial specialization of
3001// pair constrained for for Integral and Floating_point arguments.
3002//
3003// The general rule is: if a constrained specialization with matching
3004// constraints is found return that type. Also note that if TYPE is not a
3005// class-type (e.g. a typename type), then no fixup is needed.
3006
3007static tree
3008fixup_template_type (tree type)
3009{
3010 // Find the template parameter list at the a depth appropriate to
3011 // the scope we're trying to enter.
3012 tree parms = current_template_parms;
3013 int depth = template_class_depth (type);
3014 for (int n = processing_template_decl; n > depth && parms; --n)
3015 parms = TREE_CHAIN (parms);
3016 if (!parms)
3017 return type;
3018 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3019 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3020
3021 // Search for a specialization whose type and constraints match.
3022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3023 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3024 while (specs)
3025 {
3026 tree spec_constr = get_constraints (TREE_VALUE (specs));
3027
3028 // If the type and constraints match a specialization, then we
3029 // are entering that type.
3030 if (same_type_p (type, TREE_TYPE (specs))
3031 && equivalent_constraints (cur_constr, spec_constr))
3032 return TREE_TYPE (specs);
3033 specs = TREE_CHAIN (specs);
3034 }
3035
3036 // If no specialization matches, then must return the type
3037 // previously found.
3038 return type;
3039}
3040
509fc277 3041/* Finish processing a template-id (which names a type) of the form
36a117a5 3042 NAME < ARGS >. Return the TYPE_DECL for the type named by the
838dfd8a 3043 template-id. If ENTERING_SCOPE is nonzero we are about to enter
36a117a5
MM
3044 the scope of template-id indicated. */
3045
3046tree
3a978d72 3047finish_template_type (tree name, tree args, int entering_scope)
36a117a5 3048{
28704289 3049 tree type;
36a117a5 3050
28704289 3051 type = lookup_template_class (name, args,
42eaed49 3052 NULL_TREE, NULL_TREE, entering_scope,
23fca1f5 3053 tf_warning_or_error | tf_user);
971e17ff
AS
3054
3055 /* If we might be entering the scope of a partial specialization,
3056 find the one with the right constraints. */
3057 if (flag_concepts
3058 && entering_scope
3059 && CLASS_TYPE_P (type)
3060 && dependent_type_p (type)
3061 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3062 type = fixup_template_type (type);
3063
28704289
DS
3064 if (type == error_mark_node)
3065 return type;
3066 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3067 return TYPE_STUB_DECL (type);
3068 else
3069 return TYPE_NAME (type);
36a117a5 3070}
648f19f6 3071
ea6021e8
MM
3072/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3073 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3074 BASE_CLASS, or NULL_TREE if an error occurred. The
aba649ba 3075 ACCESS_SPECIFIER is one of
809e3e7f
NS
3076 access_{default,public,protected_private}_node. For a virtual base
3077 we set TREE_TYPE. */
ea6021e8 3078
c8094d83 3079tree
dbbf88d1 3080finish_base_specifier (tree base, tree access, bool virtual_p)
ea6021e8 3081{
ea6021e8
MM
3082 tree result;
3083
dbbf88d1 3084 if (base == error_mark_node)
acb044ee
GDR
3085 {
3086 error ("invalid base-class specification");
3087 result = NULL_TREE;
3088 }
9e1e64ec
PC
3089 else if (! MAYBE_CLASS_TYPE_P (base))
3090 {
3091 error ("%qT is not a class type", base);
3092 result = NULL_TREE;
3093 }
ea6021e8 3094 else
bb92901d 3095 {
dbbf88d1 3096 if (cp_type_quals (base) != 0)
0cbd7506 3097 {
546a4197
PC
3098 /* DR 484: Can a base-specifier name a cv-qualified
3099 class type? */
0cbd7506
MS
3100 base = TYPE_MAIN_VARIANT (base);
3101 }
dbbf88d1 3102 result = build_tree_list (access, base);
809e3e7f
NS
3103 if (virtual_p)
3104 TREE_TYPE (result) = integer_type_node;
bb92901d 3105 }
ea6021e8
MM
3106
3107 return result;
3108}
61a127b3 3109
eff3a276
MM
3110/* If FNS is a member function, a set of member functions, or a
3111 template-id referring to one or more member functions, return a
3112 BASELINK for FNS, incorporating the current access context.
3113 Otherwise, return FNS unchanged. */
3114
3115tree
3116baselink_for_fns (tree fns)
3117{
aef3a6b2 3118 tree scope;
eff3a276
MM
3119 tree cl;
3120
3121 if (BASELINK_P (fns)
eff3a276
MM
3122 || error_operand_p (fns))
3123 return fns;
aef3a6b2
JM
3124
3125 scope = ovl_scope (fns);
3126 if (!CLASS_TYPE_P (scope))
eff3a276
MM
3127 return fns;
3128
aef3a6b2 3129 cl = currently_open_derived_class (scope);
eff3a276 3130 if (!cl)
aef3a6b2 3131 cl = scope;
eff3a276 3132 cl = TYPE_BINFO (cl);
5a40306b 3133 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
eff3a276
MM
3134}
3135
9fd30fec 3136/* Returns true iff DECL is a variable from a function outside
d5f4eddd
JM
3137 the current one. */
3138
3139static bool
9fd30fec 3140outer_var_p (tree decl)
d5f4eddd 3141{
5a6ccc94 3142 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
d5f4eddd 3143 && DECL_FUNCTION_SCOPE_P (decl)
cdf47df0
JM
3144 && (DECL_CONTEXT (decl) != current_function_decl
3145 || parsing_nsdmi ()));
d5f4eddd
JM
3146}
3147
9fd30fec
JM
3148/* As above, but also checks that DECL is automatic. */
3149
548cb3d7 3150bool
9fd30fec
JM
3151outer_automatic_var_p (tree decl)
3152{
3153 return (outer_var_p (decl)
3154 && !TREE_STATIC (decl));
3155}
3156
548cb3d7
JM
3157/* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3158 rewrite it for lambda capture. */
3159
3160tree
3161process_outer_var_ref (tree decl, tsubst_flags_t complain)
3162{
3163 if (cp_unevaluated_operand)
3164 /* It's not a use (3.2) if we're in an unevaluated context. */
3165 return decl;
63d02f05
JM
3166 if (decl == error_mark_node)
3167 return decl;
548cb3d7
JM
3168
3169 tree context = DECL_CONTEXT (decl);
3170 tree containing_function = current_function_decl;
3171 tree lambda_stack = NULL_TREE;
3172 tree lambda_expr = NULL_TREE;
3173 tree initializer = convert_from_reference (decl);
3174
3175 /* Mark it as used now even if the use is ill-formed. */
9f635aba
PC
3176 if (!mark_used (decl, complain) && !(complain & tf_error))
3177 return error_mark_node;
548cb3d7
JM
3178
3179 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3180 support for an approach in which a reference to a local
3181 [constant] automatic variable in a nested class or lambda body
3182 would enter the expression as an rvalue, which would reduce
3183 the complexity of the problem"
3184
3185 FIXME update for final resolution of core issue 696. */
3186 if (decl_maybe_constant_var_p (decl))
3187 {
3188 if (processing_template_decl)
3189 /* In a template, the constant value may not be in a usable
3190 form, so wait until instantiation time. */
3191 return decl;
3192 else if (decl_constant_var_p (decl))
5af874fb
JM
3193 {
3194 tree t = maybe_constant_value (convert_from_reference (decl));
3195 if (TREE_CONSTANT (t))
3196 return t;
3197 }
548cb3d7
JM
3198 }
3199
3200 if (parsing_nsdmi ())
3201 containing_function = NULL_TREE;
3202 else
3203 /* If we are in a lambda function, we can move out until we hit
3204 1. the context,
3205 2. a non-lambda function, or
3206 3. a non-default capturing lambda function. */
3207 while (context != containing_function
3208 && LAMBDA_FUNCTION_P (containing_function))
3209 {
47867b4f
JM
3210 tree closure = DECL_CONTEXT (containing_function);
3211 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3212
3213 if (TYPE_CLASS_SCOPE_P (closure))
3214 /* A lambda in an NSDMI (c++/64496). */
3215 break;
548cb3d7
JM
3216
3217 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3218 == CPLD_NONE)
3219 break;
3220
3221 lambda_stack = tree_cons (NULL_TREE,
3222 lambda_expr,
3223 lambda_stack);
3224
3225 containing_function
3226 = decl_function_context (containing_function);
3227 }
3228
56a6f1d3 3229 if (lambda_expr && VAR_P (decl)
548cb3d7
JM
3230 && DECL_ANON_UNION_VAR_P (decl))
3231 {
3232 if (complain & tf_error)
3233 error ("cannot capture member %qD of anonymous union", decl);
3234 return error_mark_node;
3235 }
3236 if (context == containing_function)
3237 {
3238 decl = add_default_capture (lambda_stack,
3239 /*id=*/DECL_NAME (decl),
3240 initializer);
3241 }
3242 else if (lambda_expr)
3243 {
3244 if (complain & tf_error)
47867b4f
JM
3245 {
3246 error ("%qD is not captured", decl);
3247 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3248 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3249 == CPLD_NONE)
3250 inform (location_of (closure),
3251 "the lambda has no capture-default");
3252 else if (TYPE_CLASS_SCOPE_P (closure))
3253 inform (0, "lambda in local class %q+T cannot "
3254 "capture variables from the enclosing context",
3255 TYPE_CONTEXT (closure));
4b1cbcee 3256 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
47867b4f 3257 }
548cb3d7
JM
3258 return error_mark_node;
3259 }
3260 else
3261 {
3262 if (complain & tf_error)
3263 error (VAR_P (decl)
3264 ? G_("use of local variable with automatic storage from containing function")
3265 : G_("use of parameter from containing function"));
4b1cbcee 3266 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
548cb3d7
JM
3267 return error_mark_node;
3268 }
3269 return decl;
3270}
3271
b3445994
MM
3272/* ID_EXPRESSION is a representation of parsed, but unprocessed,
3273 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3274 if non-NULL, is the type or namespace used to explicitly qualify
3275 ID_EXPRESSION. DECL is the entity to which that name has been
c8094d83 3276 resolved.
b3445994
MM
3277
3278 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3279 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3280 be set to true if this expression isn't permitted in a
3281 constant-expression, but it is otherwise not set by this function.
3282 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3283 constant-expression, but a non-constant expression is also
3284 permissible.
3285
02ed62dd
MM
3286 DONE is true if this expression is a complete postfix-expression;
3287 it is false if this expression is followed by '->', '[', '(', etc.
3288 ADDRESS_P is true iff this expression is the operand of '&'.
3289 TEMPLATE_P is true iff the qualified-id was of the form
3290 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3291 appears as a template argument.
3292
b3445994
MM
3293 If an error occurs, and it is the kind of error that might cause
3294 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3295 is the caller's responsibility to issue the message. *ERROR_MSG
3296 will be a string with static storage duration, so the caller need
3297 not "free" it.
3298
3299 Return an expression for the entity, after issuing appropriate
3300 diagnostics. This function is also responsible for transforming a
3301 reference to a non-static member into a COMPONENT_REF that makes
c8094d83 3302 the use of "this" explicit.
b3445994
MM
3303
3304 Upon return, *IDK will be filled in appropriately. */
b3445994 3305tree
c8094d83 3306finish_id_expression (tree id_expression,
b3445994
MM
3307 tree decl,
3308 tree scope,
3309 cp_id_kind *idk,
67c03833
JM
3310 bool integral_constant_expression_p,
3311 bool allow_non_integral_constant_expression_p,
3312 bool *non_integral_constant_expression_p,
02ed62dd
MM
3313 bool template_p,
3314 bool done,
3315 bool address_p,
3316 bool template_arg_p,
2b7a3abf
DS
3317 const char **error_msg,
3318 location_t location)
b3445994 3319{
a9727434
FC
3320 decl = strip_using_decl (decl);
3321
b3445994
MM
3322 /* Initialize the output parameters. */
3323 *idk = CP_ID_KIND_NONE;
3324 *error_msg = NULL;
3325
3326 if (id_expression == error_mark_node)
3327 return error_mark_node;
3328 /* If we have a template-id, then no further lookup is
3329 required. If the template-id was for a template-class, we
3330 will sometimes have a TYPE_DECL at this point. */
3331 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
ee935db4 3332 || TREE_CODE (decl) == TYPE_DECL)
b3445994
MM
3333 ;
3334 /* Look up the name. */
c8094d83 3335 else
b3445994
MM
3336 {
3337 if (decl == error_mark_node)
3338 {
3339 /* Name lookup failed. */
c8094d83
MS
3340 if (scope
3341 && (!TYPE_P (scope)
4546865e 3342 || (!dependent_type_p (scope)
9dc6f476 3343 && !(identifier_p (id_expression)
4546865e
MM
3344 && IDENTIFIER_TYPENAME_P (id_expression)
3345 && dependent_type_p (TREE_TYPE (id_expression))))))
b3445994 3346 {
4546865e
MM
3347 /* If the qualifying type is non-dependent (and the name
3348 does not name a conversion operator to a dependent
3349 type), issue an error. */
2b7a3abf 3350 qualified_name_lookup_error (scope, id_expression, decl, location);
b3445994
MM
3351 return error_mark_node;
3352 }
3353 else if (!scope)
3354 {
3355 /* It may be resolved via Koenig lookup. */
3356 *idk = CP_ID_KIND_UNQUALIFIED;
3357 return id_expression;
3358 }
4546865e
MM
3359 else
3360 decl = id_expression;
b3445994
MM
3361 }
3362 /* If DECL is a variable that would be out of scope under
3363 ANSI/ISO rules, but in scope in the ARM, name lookup
3364 will succeed. Issue a diagnostic here. */
3365 else
3366 decl = check_for_out_of_scope_variable (decl);
3367
3368 /* Remember that the name was used in the definition of
3369 the current class so that we can check later to see if
3370 the meaning would have been different after the class
3371 was entirely defined. */
9dc6f476 3372 if (!scope && decl != error_mark_node && identifier_p (id_expression))
b3445994 3373 maybe_note_name_used_in_class (id_expression, decl);
8ca4bf25 3374
d5f4eddd
JM
3375 /* Disallow uses of local variables from containing functions, except
3376 within lambda-expressions. */
548cb3d7 3377 if (outer_automatic_var_p (decl))
cfb71cad
JM
3378 {
3379 decl = process_outer_var_ref (decl, tf_warning_or_error);
3380 if (decl == error_mark_node)
3381 return error_mark_node;
3382 }
da9bc840
JM
3383
3384 /* Also disallow uses of function parameters outside the function
3385 body, except inside an unevaluated context (i.e. decltype). */
3386 if (TREE_CODE (decl) == PARM_DECL
3387 && DECL_CONTEXT (decl) == NULL_TREE
3388 && !cp_unevaluated_operand)
3389 {
5d264d62 3390 *error_msg = "use of parameter outside function body";
da9bc840
JM
3391 return error_mark_node;
3392 }
b3445994
MM
3393 }
3394
3395 /* If we didn't find anything, or what we found was a type,
3396 then this wasn't really an id-expression. */
3397 if (TREE_CODE (decl) == TEMPLATE_DECL
3398 && !DECL_FUNCTION_TEMPLATE_P (decl))
3399 {
3400 *error_msg = "missing template arguments";
3401 return error_mark_node;
3402 }
3403 else if (TREE_CODE (decl) == TYPE_DECL
3404 || TREE_CODE (decl) == NAMESPACE_DECL)
3405 {
3406 *error_msg = "expected primary-expression";
3407 return error_mark_node;
3408 }
3409
3410 /* If the name resolved to a template parameter, there is no
931a9c05
GB
3411 need to look it up again later. */
3412 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3413 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
b3445994 3414 {
db24eb1f 3415 tree r;
c8094d83 3416
b3445994 3417 *idk = CP_ID_KIND_NONE;
931a9c05
GB
3418 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3419 decl = TEMPLATE_PARM_DECL (decl);
db24eb1f 3420 r = convert_from_reference (DECL_INITIAL (decl));
c8094d83
MS
3421
3422 if (integral_constant_expression_p
68deab91 3423 && !dependent_type_p (TREE_TYPE (decl))
db24eb1f 3424 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
931a9c05 3425 {
67c03833 3426 if (!allow_non_integral_constant_expression_p)
a82e1a7d 3427 error ("template parameter %qD of type %qT is not allowed in "
931a9c05
GB
3428 "an integral constant expression because it is not of "
3429 "integral or enumeration type", decl, TREE_TYPE (decl));
67c03833 3430 *non_integral_constant_expression_p = true;
931a9c05 3431 }
db24eb1f 3432 return r;
931a9c05 3433 }
b3445994
MM
3434 else
3435 {
a2b4cfaa 3436 bool dependent_p = type_dependent_expression_p (decl);
b3445994
MM
3437
3438 /* If the declaration was explicitly qualified indicate
3439 that. The semantics of `A::f(3)' are different than
3440 `f(3)' if `f' is virtual. */
c8094d83 3441 *idk = (scope
b3445994
MM
3442 ? CP_ID_KIND_QUALIFIED
3443 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3444 ? CP_ID_KIND_TEMPLATE_ID
a2b4cfaa
JM
3445 : (dependent_p
3446 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3447 : CP_ID_KIND_UNQUALIFIED)));
b3445994
MM
3448
3449 /* If the name was dependent on a template parameter, we will
3450 resolve the name at instantiation time. */
3451 if (dependent_p)
3452 {
a2b4cfaa
JM
3453 /* If we found a variable, then name lookup during the
3454 instantiation will always resolve to the same VAR_DECL
3455 (or an instantiation thereof). */
3456 if (VAR_P (decl)
3457 || TREE_CODE (decl) == CONST_DECL
3458 || TREE_CODE (decl) == PARM_DECL)
3459 {
3460 mark_used (decl);
3461 return convert_from_reference (decl);
3462 }
3463
b3445994
MM
3464 /* Create a SCOPE_REF for qualified names, if the scope is
3465 dependent. */
3466 if (scope)
3467 {
02ed62dd
MM
3468 if (TYPE_P (scope))
3469 {
3470 if (address_p && done)
3471 decl = finish_qualified_id_expr (scope, decl,
3472 done, address_p,
3473 template_p,
a378996b
PC
3474 template_arg_p,
3475 tf_warning_or_error);
7e361ae6
JM
3476 else
3477 {
3478 tree type = NULL_TREE;
3479 if (DECL_P (decl) && !dependent_scope_p (scope))
3480 type = TREE_TYPE (decl);
3481 decl = build_qualified_name (type,
3482 scope,
3483 id_expression,
3484 template_p);
3485 }
02ed62dd
MM
3486 }
3487 if (TREE_TYPE (decl))
3488 decl = convert_from_reference (decl);
3489 return decl;
b3445994
MM
3490 }
3491 /* A TEMPLATE_ID already contains all the information we
3492 need. */
3493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3494 return id_expression;
bad1f462
KL
3495 /* The same is true for FIELD_DECL, but we also need to
3496 make sure that the syntax is correct. */
3497 else if (TREE_CODE (decl) == FIELD_DECL)
a26ddf11
KL
3498 {
3499 /* Since SCOPE is NULL here, this is an unqualified name.
3500 Access checking has been performed during name lookup
3501 already. Turn off checking to avoid duplicate errors. */
3502 push_deferring_access_checks (dk_no_check);
3503 decl = finish_non_static_data_member
2defb926 3504 (decl, NULL_TREE,
a26ddf11
KL
3505 /*qualifying_scope=*/NULL_TREE);
3506 pop_deferring_access_checks ();
3507 return decl;
3508 }
10b1d5e7 3509 return id_expression;
b3445994
MM
3510 }
3511
415d4636 3512 if (TREE_CODE (decl) == NAMESPACE_DECL)
9e95d15f 3513 {
a82e1a7d 3514 error ("use of namespace %qD as expression", decl);
9e95d15f
NS
3515 return error_mark_node;
3516 }
3517 else if (DECL_CLASS_TEMPLATE_P (decl))
3518 {
a82e1a7d 3519 error ("use of class template %qT as expression", decl);
9e95d15f
NS
3520 return error_mark_node;
3521 }
3522 else if (TREE_CODE (decl) == TREE_LIST)
3523 {
3524 /* Ambiguous reference to base members. */
a82e1a7d 3525 error ("request for member %qD is ambiguous in "
9e95d15f
NS
3526 "multiple inheritance lattice", id_expression);
3527 print_candidates (decl);
3528 return error_mark_node;
3529 }
415d4636
MM
3530
3531 /* Mark variable-like entities as used. Functions are similarly
3532 marked either below or after overload resolution. */
5a6ccc94 3533 if ((VAR_P (decl)
f27a59cf
PC
3534 || TREE_CODE (decl) == PARM_DECL
3535 || TREE_CODE (decl) == CONST_DECL
3536 || TREE_CODE (decl) == RESULT_DECL)
3537 && !mark_used (decl))
3538 return error_mark_node;
415d4636 3539
aef4a215 3540 /* Only certain kinds of names are allowed in constant
8d0d1915 3541 expression. Template parameters have already
aef4a215 3542 been handled above. */
7f7d4b12
DS
3543 if (! error_operand_p (decl)
3544 && integral_constant_expression_p
aef4a215 3545 && ! decl_constant_var_p (decl)
8d0d1915 3546 && TREE_CODE (decl) != CONST_DECL
aef4a215
JM
3547 && ! builtin_valid_in_constant_expr_p (decl))
3548 {
3549 if (!allow_non_integral_constant_expression_p)
3550 {
3551 error ("%qD cannot appear in a constant-expression", decl);
3552 return error_mark_node;
3553 }
3554 *non_integral_constant_expression_p = true;
3555 }
3556
7c424acd 3557 tree wrap;
5a6ccc94 3558 if (VAR_P (decl)
7c424acd 3559 && !cp_unevaluated_operand
71d64cd4 3560 && !processing_template_decl
56363ffd 3561 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3048c0c7 3562 && CP_DECL_THREAD_LOCAL_P (decl)
7c424acd
JM
3563 && (wrap = get_tls_wrapper_fn (decl)))
3564 {
3565 /* Replace an evaluated use of the thread_local variable with
3566 a call to its wrapper. */
3567 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3568 }
4a4f287d
BO
3569 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3570 && variable_template_p (TREE_OPERAND (decl, 0)))
3571 {
3572 decl = finish_template_variable (decl);
5e0231c2 3573 mark_used (decl);
85171e78 3574 decl = convert_from_reference (decl);
4a4f287d 3575 }
7c424acd 3576 else if (scope)
415d4636 3577 {
c8094d83 3578 decl = (adjust_result_of_qualified_name_lookup
90677b8d 3579 (decl, scope, current_nonlambda_class_type()));
e20bcc5e
JH
3580
3581 if (TREE_CODE (decl) == FUNCTION_DECL)
3582 mark_used (decl);
3583
e467c9d2 3584 if (TYPE_P (scope))
02ed62dd
MM
3585 decl = finish_qualified_id_expr (scope,
3586 decl,
3587 done,
3588 address_p,
3589 template_p,
a378996b
PC
3590 template_arg_p,
3591 tf_warning_or_error);
db24eb1f 3592 else
e467c9d2 3593 decl = convert_from_reference (decl);
415d4636 3594 }
9e95d15f 3595 else if (TREE_CODE (decl) == FIELD_DECL)
a26ddf11
KL
3596 {
3597 /* Since SCOPE is NULL here, this is an unqualified name.
3598 Access checking has been performed during name lookup
3599 already. Turn off checking to avoid duplicate errors. */
3600 push_deferring_access_checks (dk_no_check);
2defb926 3601 decl = finish_non_static_data_member (decl, NULL_TREE,
a26ddf11
KL
3602 /*qualifying_scope=*/NULL_TREE);
3603 pop_deferring_access_checks ();
3604 }
9e95d15f
NS
3605 else if (is_overloaded_fn (decl))
3606 {
eff3a276 3607 tree first_fn;
b3445994 3608
294e855f 3609 first_fn = get_first_fn (decl);
9e95d15f
NS
3610 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3611 first_fn = DECL_TEMPLATE_RESULT (first_fn);
415d4636 3612
9965f21f
PC
3613 if (!really_overloaded_fn (decl)
3614 && !mark_used (first_fn))
3615 return error_mark_node;
415d4636 3616
02ed62dd
MM
3617 if (!template_arg_p
3618 && TREE_CODE (first_fn) == FUNCTION_DECL
821eaf2a
MM
3619 && DECL_FUNCTION_MEMBER_P (first_fn)
3620 && !shared_member_p (decl))
9e95d15f
NS
3621 {
3622 /* A set of member functions. */
3623 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
02ed62dd 3624 return finish_class_member_access_expr (decl, id_expression,
5ade1ed2
DG
3625 /*template_p=*/false,
3626 tf_warning_or_error);
9e95d15f 3627 }
eff3a276
MM
3628
3629 decl = baselink_for_fns (decl);
9e95d15f
NS
3630 }
3631 else
3632 {
9e95d15f 3633 if (DECL_P (decl) && DECL_NONLOCAL (decl)
24f58e74 3634 && DECL_CLASS_SCOPE_P (decl))
9e95d15f 3635 {
24f58e74
PC
3636 tree context = context_for_name_lookup (decl);
3637 if (context != current_class_type)
3638 {
3639 tree path = currently_open_derived_class (context);
3640 perform_or_defer_access_check (TYPE_BINFO (path),
0e69fdf0
PC
3641 decl, decl,
3642 tf_warning_or_error);
24f58e74 3643 }
9e95d15f 3644 }
c8094d83 3645
db24eb1f 3646 decl = convert_from_reference (decl);
9e95d15f 3647 }
b3445994
MM
3648 }
3649
b3445994
MM
3650 return decl;
3651}
3652
0213a355
JM
3653/* Implement the __typeof keyword: Return the type of EXPR, suitable for
3654 use as a type-specifier. */
3655
b894fc05 3656tree
3a978d72 3657finish_typeof (tree expr)
b894fc05 3658{
65a5559b
MM
3659 tree type;
3660
dffbbe80 3661 if (type_dependent_expression_p (expr))
b894fc05 3662 {
9e1e64ec 3663 type = cxx_make_type (TYPEOF_TYPE);
eb34af89 3664 TYPEOF_TYPE_EXPR (type) = expr;
3ad6a8e1 3665 SET_TYPE_STRUCTURAL_EQUALITY (type);
b894fc05 3666
65a5559b 3667 return type;
b894fc05
JM
3668 }
3669
03a904b5
JJ
3670 expr = mark_type_use (expr);
3671
3c38f0ff 3672 type = unlowered_expr_type (expr);
65a5559b
MM
3673
3674 if (!type || type == unknown_type_node)
3675 {
a82e1a7d 3676 error ("type of %qE is unknown", expr);
65a5559b
MM
3677 return error_mark_node;
3678 }
3679
3680 return type;
b894fc05 3681}
558475f0 3682
a0d260fc
PC
3683/* Implement the __underlying_type keyword: Return the underlying
3684 type of TYPE, suitable for use as a type-specifier. */
3685
3686tree
3687finish_underlying_type (tree type)
3688{
3689 tree underlying_type;
3690
3691 if (processing_template_decl)
3692 {
3693 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3694 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3695 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3696
3697 return underlying_type;
3698 }
3699
3700 complete_type (type);
3701
3702 if (TREE_CODE (type) != ENUMERAL_TYPE)
3703 {
ca2507dc 3704 error ("%qT is not an enumeration type", type);
a0d260fc
PC
3705 return error_mark_node;
3706 }
3707
3708 underlying_type = ENUM_UNDERLYING_TYPE (type);
3709
3710 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3711 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3712 See finish_enum_value_list for details. */
3713 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3714 underlying_type
3715 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3716 TYPE_UNSIGNED (underlying_type));
3717
3718 return underlying_type;
3719}
3720
4daba884
BK
3721/* Implement the __direct_bases keyword: Return the direct base classes
3722 of type */
3723
3724tree
3725calculate_direct_bases (tree type)
3726{
9771b263 3727 vec<tree, va_gc> *vector = make_tree_vector();
4daba884 3728 tree bases_vec = NULL_TREE;
9771b263 3729 vec<tree, va_gc> *base_binfos;
4daba884
BK
3730 tree binfo;
3731 unsigned i;
3732
3733 complete_type (type);
3734
3735 if (!NON_UNION_CLASS_TYPE_P (type))
3736 return make_tree_vec (0);
3737
3738 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3739
3740 /* Virtual bases are initialized first */
9771b263 3741 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4daba884
BK
3742 {
3743 if (BINFO_VIRTUAL_P (binfo))
3744 {
9771b263 3745 vec_safe_push (vector, binfo);
4daba884
BK
3746 }
3747 }
3748
3749 /* Now non-virtuals */
9771b263 3750 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4daba884
BK
3751 {
3752 if (!BINFO_VIRTUAL_P (binfo))
3753 {
9771b263 3754 vec_safe_push (vector, binfo);
4daba884
BK
3755 }
3756 }
3757
3758
9771b263 3759 bases_vec = make_tree_vec (vector->length ());
4daba884 3760
9771b263 3761 for (i = 0; i < vector->length (); ++i)
4daba884 3762 {
9771b263 3763 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4daba884
BK
3764 }
3765 return bases_vec;
3766}
3767
3768/* Implement the __bases keyword: Return the base classes
3769 of type */
3770
3771/* Find morally non-virtual base classes by walking binfo hierarchy */
3772/* Virtual base classes are handled separately in finish_bases */
3773
3774static tree
12308bc6 3775dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4daba884
BK
3776{
3777 /* Don't walk bases of virtual bases */
3778 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3779}
3780
3781static tree
3782dfs_calculate_bases_post (tree binfo, void *data_)
3783{
9771b263 3784 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4daba884
BK
3785 if (!BINFO_VIRTUAL_P (binfo))
3786 {
9771b263 3787 vec_safe_push (*data, BINFO_TYPE (binfo));
4daba884
BK
3788 }
3789 return NULL_TREE;
3790}
3791
3792/* Calculates the morally non-virtual base classes of a class */
9771b263 3793static vec<tree, va_gc> *
4daba884
BK
3794calculate_bases_helper (tree type)
3795{
9771b263 3796 vec<tree, va_gc> *vector = make_tree_vector();
4daba884
BK
3797
3798 /* Now add non-virtual base classes in order of construction */
3799 dfs_walk_all (TYPE_BINFO (type),
3800 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3801 return vector;
3802}
3803
3804tree
3805calculate_bases (tree type)
3806{
9771b263 3807 vec<tree, va_gc> *vector = make_tree_vector();
4daba884
BK
3808 tree bases_vec = NULL_TREE;
3809 unsigned i;
9771b263
DN
3810 vec<tree, va_gc> *vbases;
3811 vec<tree, va_gc> *nonvbases;
4daba884
BK
3812 tree binfo;
3813
3814 complete_type (type);
3815
3816 if (!NON_UNION_CLASS_TYPE_P (type))
3817 return make_tree_vec (0);
3818
3819 /* First go through virtual base classes */
3820 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
9771b263 3821 vec_safe_iterate (vbases, i, &binfo); i++)
4daba884 3822 {
9771b263
DN
3823 vec<tree, va_gc> *vbase_bases;
3824 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3825 vec_safe_splice (vector, vbase_bases);
4daba884
BK
3826 release_tree_vector (vbase_bases);
3827 }
3828
3829 /* Now for the non-virtual bases */
3830 nonvbases = calculate_bases_helper (type);
9771b263 3831 vec_safe_splice (vector, nonvbases);
4daba884
BK
3832 release_tree_vector (nonvbases);
3833
3834 /* Last element is entire class, so don't copy */
9771b263 3835 bases_vec = make_tree_vec (vector->length () - 1);
4daba884 3836
9771b263 3837 for (i = 0; i < vector->length () - 1; ++i)
4daba884 3838 {
9771b263 3839 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4daba884
BK
3840 }
3841 release_tree_vector (vector);
3842 return bases_vec;
3843}
3844
3845tree
3846finish_bases (tree type, bool direct)
3847{
3848 tree bases = NULL_TREE;
3849
3850 if (!processing_template_decl)
3851 {
3852 /* Parameter packs can only be used in templates */
3853 error ("Parameter pack __bases only valid in template declaration");
3854 return error_mark_node;
3855 }
3856
3857 bases = cxx_make_type (BASES);
3858 BASES_TYPE (bases) = type;
3859 BASES_DIRECT (bases) = direct;
3860 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3861
3862 return bases;
3863}
3864
c291f8b1
VR
3865/* Perform C++-specific checks for __builtin_offsetof before calling
3866 fold_offsetof. */
3867
3868tree
8591575f 3869finish_offsetof (tree expr, location_t loc)
c291f8b1 3870{
b433d944
JM
3871 /* If we're processing a template, we can't finish the semantics yet.
3872 Otherwise we can fold the entire expression now. */
3873 if (processing_template_decl)
3874 {
3875 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3876 SET_EXPR_LOCATION (expr, loc);
3877 return expr;
3878 }
3879
4c65a534
VR
3880 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3881 {
3882 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3883 TREE_OPERAND (expr, 2));
3884 return error_mark_node;
3885 }
c291f8b1
VR
3886 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3887 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
fbfc8363 3888 || TREE_TYPE (expr) == unknown_type_node)
c291f8b1 3889 {
98cf9ac9 3890 if (INDIRECT_REF_P (expr))
929f647a
PC
3891 error ("second operand of %<offsetof%> is neither a single "
3892 "identifier nor a sequence of member accesses and "
3893 "array references");
3894 else
3895 {
3896 if (TREE_CODE (expr) == COMPONENT_REF
3897 || TREE_CODE (expr) == COMPOUND_EXPR)
3898 expr = TREE_OPERAND (expr, 1);
3899 error ("cannot apply %<offsetof%> to member function %qD", expr);
3900 }
c291f8b1
VR
3901 return error_mark_node;
3902 }
31e292c7 3903 if (REFERENCE_REF_P (expr))
60c4d135 3904 expr = TREE_OPERAND (expr, 0);
a174e38c
JM
3905 if (TREE_CODE (expr) == COMPONENT_REF)
3906 {
3907 tree object = TREE_OPERAND (expr, 0);
3908 if (!complete_type_or_else (TREE_TYPE (object), object))
3909 return error_mark_node;
8591575f
JM
3910 if (warn_invalid_offsetof
3911 && CLASS_TYPE_P (TREE_TYPE (object))
3912 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3913 && cp_unevaluated_operand == 0)
3914 pedwarn (loc, OPT_Winvalid_offsetof,
3915 "offsetof within non-standard-layout type %qT is undefined",
3916 TREE_TYPE (object));
a174e38c 3917 }
cf9e9959 3918 return fold_offsetof (expr);
c291f8b1
VR
3919}
3920
9eeb200f
JM
3921/* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3922 function is broken out from the above for the benefit of the tree-ssa
3923 project. */
3924
3925void
3926simplify_aggr_init_expr (tree *tp)
3927{
3928 tree aggr_init_expr = *tp;
3929
3eb24f73 3930 /* Form an appropriate CALL_EXPR. */
5039610b
SL
3931 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3932 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
2692eb7d 3933 tree type = TREE_TYPE (slot);
9eeb200f
JM
3934
3935 tree call_expr;
3936 enum style_t { ctor, arg, pcc } style;
4977bab6 3937
3eb24f73 3938 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4977bab6
ZW
3939 style = ctor;
3940#ifdef PCC_STATIC_STRUCT_RETURN
3941 else if (1)
3942 style = pcc;
3943#endif
4977bab6 3944 else
315fb5db
NS
3945 {
3946 gcc_assert (TREE_ADDRESSABLE (type));
3947 style = arg;
3948 }
4977bab6 3949
db3927fb
AH
3950 call_expr = build_call_array_loc (input_location,
3951 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3952 fn,
3953 aggr_init_expr_nargs (aggr_init_expr),
3954 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
d8a0d13e 3955 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
fe6ebcf1 3956 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
5039610b 3957
fa47911c 3958 if (style == ctor)
3eb24f73 3959 {
fa47911c
JM
3960 /* Replace the first argument to the ctor with the address of the
3961 slot. */
dffd7eb6 3962 cxx_mark_addressable (slot);
5039610b
SL
3963 CALL_EXPR_ARG (call_expr, 0) =
3964 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3eb24f73 3965 }
5039610b 3966 else if (style == arg)
fa47911c
JM
3967 {
3968 /* Just mark it addressable here, and leave the rest to
3969 expand_call{,_inline}. */
3970 cxx_mark_addressable (slot);
3971 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
71d1add3 3972 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
fa47911c 3973 }
4977bab6 3974 else if (style == pcc)
3eb24f73 3975 {
4977bab6
ZW
3976 /* If we're using the non-reentrant PCC calling convention, then we
3977 need to copy the returned value out of the static buffer into the
3978 SLOT. */
78757caa 3979 push_deferring_access_checks (dk_no_check);
71d1add3
JM
3980 call_expr = build_aggr_init (slot, call_expr,
3981 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3982 tf_warning_or_error);
78757caa 3983 pop_deferring_access_checks ();
71d1add3 3984 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4561285b
JM
3985 }
3986
450a927a
JM
3987 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3988 {
3989 tree init = build_zero_init (type, NULL_TREE,
3990 /*static_storage_p=*/false);
3991 init = build2 (INIT_EXPR, void_type_node, slot, init);
71d1add3
JM
3992 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3993 init, call_expr);
450a927a
JM
3994 }
3995
71d1add3 3996 *tp = call_expr;
3eb24f73
MM
3997}
3998
31f8e4f3
MM
3999/* Emit all thunks to FN that should be emitted when FN is emitted. */
4000
e89d6010 4001void
3a978d72 4002emit_associated_thunks (tree fn)
31f8e4f3
MM
4003{
4004 /* When we use vcall offsets, we emit thunks with the virtual
4005 functions to which they thunk. The whole point of vcall offsets
4006 is so that you can know statically the entire set of thunks that
4007 will ever be needed for a given virtual function, thereby
4008 enabling you to output all the thunks with the function itself. */
4537ec0c
DN
4009 if (DECL_VIRTUAL_P (fn)
4010 /* Do not emit thunks for extern template instantiations. */
4011 && ! DECL_REALLY_EXTERN (fn))
31f8e4f3 4012 {
bb5e8a7f 4013 tree thunk;
c8094d83 4014
910ad8de 4015 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4977bab6 4016 {
e00853fd 4017 if (!THUNK_ALIAS (thunk))
4977bab6 4018 {
bb885938
NS
4019 use_thunk (thunk, /*emit_p=*/1);
4020 if (DECL_RESULT_THUNK_P (thunk))
4021 {
4022 tree probe;
c8094d83 4023
bb885938 4024 for (probe = DECL_THUNKS (thunk);
910ad8de 4025 probe; probe = DECL_CHAIN (probe))
bb885938
NS
4026 use_thunk (probe, /*emit_p=*/1);
4027 }
4977bab6 4028 }
bb885938 4029 else
50bc768d 4030 gcc_assert (!DECL_THUNKS (thunk));
4977bab6 4031 }
31f8e4f3
MM
4032 }
4033}
4034
558475f0
MM
4035/* Generate RTL for FN. */
4036
b2583345
JJ
4037bool
4038expand_or_defer_fn_1 (tree fn)
8cd2462c
JH
4039{
4040 /* When the parser calls us after finishing the body of a template
c353b8e3
MM
4041 function, we don't really want to expand the body. */
4042 if (processing_template_decl)
8cd2462c
JH
4043 {
4044 /* Normally, collection only occurs in rest_of_compilation. So,
4045 if we don't collect here, we never collect junk generated
4046 during the processing of templates until we hit a
27250734
MM
4047 non-template function. It's not safe to do this inside a
4048 nested class, though, as the parser may have local state that
4049 is not a GC root. */
4050 if (!function_depth)
4051 ggc_collect ();
b2583345 4052 return false;
8cd2462c
JH
4053 }
4054
a406865a 4055 gcc_assert (DECL_SAVED_TREE (fn));
726a989a 4056
4684cd27
MM
4057 /* We make a decision about linkage for these functions at the end
4058 of the compilation. Until that point, we do not want the back
4059 end to output them -- but we do want it to see the bodies of
1a10290c 4060 these functions so that it can inline them as appropriate. */
4684cd27
MM
4061 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4062 {
1ef0df47
MM
4063 if (DECL_INTERFACE_KNOWN (fn))
4064 /* We've already made a decision as to how this function will
4065 be handled. */;
4066 else if (!at_eof)
944b63db 4067 tentative_decl_linkage (fn);
4684cd27
MM
4068 else
4069 import_export_decl (fn);
1a10290c
MM
4070
4071 /* If the user wants us to keep all inline functions, then mark
4072 this function as needed so that finish_file will make sure to
fe2978fb
MM
4073 output it later. Similarly, all dllexport'd functions must
4074 be emitted; there may be callers in other DLLs. */
322d490e
KT
4075 if (DECL_DECLARED_INLINE_P (fn)
4076 && !DECL_REALLY_EXTERN (fn)
4077 && (flag_keep_inline_functions
4078 || (flag_keep_inline_dllexport
4079 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
ffca9d53
JM
4080 {
4081 mark_needed (fn);
4082 DECL_EXTERNAL (fn) = 0;
4083 }
4684cd27
MM
4084 }
4085
1f26ac87
JM
4086 /* If this is a constructor or destructor body, we have to clone
4087 it. */
4088 if (maybe_clone_body (fn))
4089 {
4090 /* We don't want to process FN again, so pretend we've written
4091 it out, even though we haven't. */
4092 TREE_ASM_WRITTEN (fn) = 1;
4093 /* If this is an instantiation of a constexpr function, keep
4094 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4095 if (!is_instantiation_of_constexpr (fn))
4096 DECL_SAVED_TREE (fn) = NULL_TREE;
4097 return false;
4098 }
4099
8cd2462c
JH
4100 /* There's no reason to do any of the work here if we're only doing
4101 semantic analysis; this code just generates RTL. */
4102 if (flag_syntax_only)
b2583345
JJ
4103 return false;
4104
4105 return true;
4106}
8cd2462c 4107
b2583345
JJ
4108void
4109expand_or_defer_fn (tree fn)
4110{
4111 if (expand_or_defer_fn_1 (fn))
4112 {
4113 function_depth++;
99edd65d 4114
b2583345 4115 /* Expand or defer, at the whim of the compilation unit manager. */
3dafb85c 4116 cgraph_node::finalize_function (fn, function_depth > 1);
6744a6ab 4117 emit_associated_thunks (fn);
99edd65d 4118
b2583345
JJ
4119 function_depth--;
4120 }
8cd2462c
JH
4121}
4122
6de9cd9a
DN
4123struct nrv_data
4124{
c203e8a7
TS
4125 nrv_data () : visited (37) {}
4126
6de9cd9a
DN
4127 tree var;
4128 tree result;
8d67ee55 4129 hash_table<nofree_ptr_hash <tree_node> > visited;
6de9cd9a 4130};
0d97bf4c 4131
6de9cd9a
DN
4132/* Helper function for walk_tree, used by finalize_nrv below. */
4133
4134static tree
4135finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
0d97bf4c 4136{
6de9cd9a 4137 struct nrv_data *dp = (struct nrv_data *)data;
703c8606 4138 tree_node **slot;
07b2f2fd
JM
4139
4140 /* No need to walk into types. There wouldn't be any need to walk into
4141 non-statements, except that we have to consider STMT_EXPRs. */
0d97bf4c
JM
4142 if (TYPE_P (*tp))
4143 *walk_subtrees = 0;
6de9cd9a
DN
4144 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4145 but differs from using NULL_TREE in that it indicates that we care
4146 about the value of the RESULT_DECL. */
5088b058
RH
4147 else if (TREE_CODE (*tp) == RETURN_EXPR)
4148 TREE_OPERAND (*tp, 0) = dp->result;
6de9cd9a
DN
4149 /* Change all cleanups for the NRV to only run when an exception is
4150 thrown. */
07b2f2fd 4151 else if (TREE_CODE (*tp) == CLEANUP_STMT
6de9cd9a 4152 && CLEANUP_DECL (*tp) == dp->var)
659e5a7a 4153 CLEANUP_EH_ONLY (*tp) = 1;
350fae66 4154 /* Replace the DECL_EXPR for the NRV with an initialization of the
6de9cd9a 4155 RESULT_DECL, if needed. */
350fae66
RK
4156 else if (TREE_CODE (*tp) == DECL_EXPR
4157 && DECL_EXPR_DECL (*tp) == dp->var)
6de9cd9a
DN
4158 {
4159 tree init;
4160 if (DECL_INITIAL (dp->var)
4161 && DECL_INITIAL (dp->var) != error_mark_node)
2d188530
JJ
4162 init = build2 (INIT_EXPR, void_type_node, dp->result,
4163 DECL_INITIAL (dp->var));
6de9cd9a 4164 else
c2255bc4 4165 init = build_empty_stmt (EXPR_LOCATION (*tp));
2d188530 4166 DECL_INITIAL (dp->var) = NULL_TREE;
5e278028 4167 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
6de9cd9a
DN
4168 *tp = init;
4169 }
4170 /* And replace all uses of the NRV with the RESULT_DECL. */
4171 else if (*tp == dp->var)
4172 *tp = dp->result;
4173
4174 /* Avoid walking into the same tree more than once. Unfortunately, we
4175 can't just use walk_tree_without duplicates because it would only call
4176 us for the first occurrence of dp->var in the function body. */
703c8606 4177 slot = dp->visited.find_slot (*tp, INSERT);
6de9cd9a
DN
4178 if (*slot)
4179 *walk_subtrees = 0;
4180 else
4181 *slot = *tp;
0d97bf4c
JM
4182
4183 /* Keep iterating. */
4184 return NULL_TREE;
4185}
4186
6de9cd9a 4187/* Called from finish_function to implement the named return value
5088b058 4188 optimization by overriding all the RETURN_EXPRs and pertinent
6de9cd9a
DN
4189 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4190 RESULT_DECL for the function. */
f444e36b 4191
4985cde3 4192void
6de9cd9a 4193finalize_nrv (tree *tp, tree var, tree result)
f444e36b 4194{
6de9cd9a
DN
4195 struct nrv_data data;
4196
add86e09 4197 /* Copy name from VAR to RESULT. */
6de9cd9a 4198 DECL_NAME (result) = DECL_NAME (var);
6de9cd9a
DN
4199 /* Don't forget that we take its address. */
4200 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
add86e09
JJ
4201 /* Finally set DECL_VALUE_EXPR to avoid assigning
4202 a stack slot at -O0 for the original var and debug info
4203 uses RESULT location for VAR. */
4204 SET_DECL_VALUE_EXPR (var, result);
4205 DECL_HAS_VALUE_EXPR_P (var) = 1;
6de9cd9a
DN
4206
4207 data.var = var;
4208 data.result = result;
14588106 4209 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
b850de4f 4210}
1799e5d5 4211\f
a68ab351
JJ
4212/* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4213
4214bool
4215cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
acf0174b
JJ
4216 bool need_copy_ctor, bool need_copy_assignment,
4217 bool need_dtor)
a68ab351
JJ
4218{
4219 int save_errorcount = errorcount;
4220 tree info, t;
4221
4222 /* Always allocate 3 elements for simplicity. These are the
4223 function decls for the ctor, dtor, and assignment op.
4224 This layout is known to the three lang hooks,
4225 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4226 and cxx_omp_clause_assign_op. */
4227 info = make_tree_vec (3);
4228 CP_OMP_CLAUSE_INFO (c) = info;
4229
ac177431 4230 if (need_default_ctor || need_copy_ctor)
a68ab351
JJ
4231 {
4232 if (need_default_ctor)
ac177431 4233 t = get_default_ctor (type);
a68ab351 4234 else
4577f730 4235 t = get_copy_ctor (type, tf_warning_or_error);
ac177431
JM
4236
4237 if (t && !trivial_fn_p (t))
4238 TREE_VEC_ELT (info, 0) = t;
a68ab351
JJ
4239 }
4240
acf0174b 4241 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4577f730 4242 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
a68ab351 4243
ac177431 4244 if (need_copy_assignment)
a68ab351 4245 {
ac177431
JM
4246 t = get_copy_assign (type);
4247
4248 if (t && !trivial_fn_p (t))
4249 TREE_VEC_ELT (info, 2) = t;
a68ab351
JJ
4250 }
4251
4252 return errorcount != save_errorcount;
4253}
4254
acf0174b
JJ
4255/* Helper function for handle_omp_array_sections. Called recursively
4256 to handle multiple array-section-subscripts. C is the clause,
4257 T current expression (initially OMP_CLAUSE_DECL), which is either
4258 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4259 expression if specified, TREE_VALUE length expression if specified,
4260 TREE_CHAIN is what it has been specified after, or some decl.
4261 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4262 set to true if any of the array-section-subscript could have length
4263 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4264 first array-section-subscript which is known not to have length
4265 of one. Given say:
4266 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4267 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4268 all are or may have length of 1, array-section-subscript [:2] is the
4269 first one knonwn not to have length 1. For array-section-subscript
4270 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4271 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4272 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4273 case though, as some lengths could be zero. */
4274
4275static tree
4276handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4277 bool &maybe_zero_len, unsigned int &first_non_one)
4278{
4279 tree ret, low_bound, length, type;
4280 if (TREE_CODE (t) != TREE_LIST)
4281 {
4282 if (error_operand_p (t))
4283 return error_mark_node;
56a6f1d3 4284 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
4285 {
4286 if (processing_template_decl)
4287 return NULL_TREE;
4288 if (DECL_P (t))
4289 error_at (OMP_CLAUSE_LOCATION (c),
4290 "%qD is not a variable in %qs clause", t,
4291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4292 else
4293 error_at (OMP_CLAUSE_LOCATION (c),
4294 "%qE is not a variable in %qs clause", t,
4295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4296 return error_mark_node;
4297 }
4298 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
3048c0c7 4299 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
4300 {
4301 error_at (OMP_CLAUSE_LOCATION (c),
4302 "%qD is threadprivate variable in %qs clause", t,
4303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4304 return error_mark_node;
4305 }
7da8534d
JJ
4306 if (type_dependent_expression_p (t))
4307 return NULL_TREE;
acf0174b
JJ
4308 t = convert_from_reference (t);
4309 return t;
4310 }
4311
4312 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4313 maybe_zero_len, first_non_one);
4314 if (ret == error_mark_node || ret == NULL_TREE)
4315 return ret;
4316
4317 type = TREE_TYPE (ret);
4318 low_bound = TREE_PURPOSE (t);
4319 length = TREE_VALUE (t);
4320 if ((low_bound && type_dependent_expression_p (low_bound))
4321 || (length && type_dependent_expression_p (length)))
4322 return NULL_TREE;
4323
4324 if (low_bound == error_mark_node || length == error_mark_node)
4325 return error_mark_node;
4326
4327 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4328 {
4329 error_at (OMP_CLAUSE_LOCATION (c),
4330 "low bound %qE of array section does not have integral type",
4331 low_bound);
4332 return error_mark_node;
4333 }
4334 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4335 {
4336 error_at (OMP_CLAUSE_LOCATION (c),
4337 "length %qE of array section does not have integral type",
4338 length);
4339 return error_mark_node;
4340 }
d90c0a59
JJ
4341 if (low_bound)
4342 low_bound = mark_rvalue_use (low_bound);
4343 if (length)
4344 length = mark_rvalue_use (length);
acf0174b
JJ
4345 if (low_bound
4346 && TREE_CODE (low_bound) == INTEGER_CST
4347 && TYPE_PRECISION (TREE_TYPE (low_bound))
4348 > TYPE_PRECISION (sizetype))
4349 low_bound = fold_convert (sizetype, low_bound);
4350 if (length
4351 && TREE_CODE (length) == INTEGER_CST
4352 && TYPE_PRECISION (TREE_TYPE (length))
4353 > TYPE_PRECISION (sizetype))
4354 length = fold_convert (sizetype, length);
4355 if (low_bound == NULL_TREE)
4356 low_bound = integer_zero_node;
4357
4358 if (length != NULL_TREE)
4359 {
4360 if (!integer_nonzerop (length))
4361 maybe_zero_len = true;
4362 if (first_non_one == types.length ()
4363 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4364 first_non_one++;
4365 }
4366 if (TREE_CODE (type) == ARRAY_TYPE)
4367 {
4368 if (length == NULL_TREE
4369 && (TYPE_DOMAIN (type) == NULL_TREE
4370 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4371 {
4372 error_at (OMP_CLAUSE_LOCATION (c),
4373 "for unknown bound array type length expression must "
4374 "be specified");
4375 return error_mark_node;
4376 }
4377 if (TREE_CODE (low_bound) == INTEGER_CST
4378 && tree_int_cst_sgn (low_bound) == -1)
4379 {
4380 error_at (OMP_CLAUSE_LOCATION (c),
4381 "negative low bound in array section in %qs clause",
4382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4383 return error_mark_node;
4384 }
4385 if (length != NULL_TREE
4386 && TREE_CODE (length) == INTEGER_CST
4387 && tree_int_cst_sgn (length) == -1)
4388 {
4389 error_at (OMP_CLAUSE_LOCATION (c),
4390 "negative length in array section in %qs clause",
4391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4392 return error_mark_node;
4393 }
4394 if (TYPE_DOMAIN (type)
4395 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4396 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4397 == INTEGER_CST)
4398 {
4399 tree size = size_binop (PLUS_EXPR,
4400 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4401 size_one_node);
4402 if (TREE_CODE (low_bound) == INTEGER_CST)
4403 {
4404 if (tree_int_cst_lt (size, low_bound))
4405 {
4406 error_at (OMP_CLAUSE_LOCATION (c),
4407 "low bound %qE above array section size "
4408 "in %qs clause", low_bound,
4409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4410 return error_mark_node;
4411 }
4412 if (tree_int_cst_equal (size, low_bound))
4413 maybe_zero_len = true;
4414 else if (length == NULL_TREE
4415 && first_non_one == types.length ()
4416 && tree_int_cst_equal
4417 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4418 low_bound))
4419 first_non_one++;
4420 }
4421 else if (length == NULL_TREE)
4422 {
4423 maybe_zero_len = true;
4424 if (first_non_one == types.length ())
4425 first_non_one++;
4426 }
4427 if (length && TREE_CODE (length) == INTEGER_CST)
4428 {
4429 if (tree_int_cst_lt (size, length))
4430 {
4431 error_at (OMP_CLAUSE_LOCATION (c),
4432 "length %qE above array section size "
4433 "in %qs clause", length,
4434 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4435 return error_mark_node;
4436 }
4437 if (TREE_CODE (low_bound) == INTEGER_CST)
4438 {
4439 tree lbpluslen
4440 = size_binop (PLUS_EXPR,
4441 fold_convert (sizetype, low_bound),
4442 fold_convert (sizetype, length));
4443 if (TREE_CODE (lbpluslen) == INTEGER_CST
4444 && tree_int_cst_lt (size, lbpluslen))
4445 {
4446 error_at (OMP_CLAUSE_LOCATION (c),
4447 "high bound %qE above array section size "
4448 "in %qs clause", lbpluslen,
4449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4450 return error_mark_node;
4451 }
4452 }
4453 }
4454 }
4455 else if (length == NULL_TREE)
4456 {
4457 maybe_zero_len = true;
4458 if (first_non_one == types.length ())
4459 first_non_one++;
4460 }
4461
4462 /* For [lb:] we will need to evaluate lb more than once. */
4463 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4464 {
4465 tree lb = cp_save_expr (low_bound);
4466 if (lb != low_bound)
4467 {
4468 TREE_PURPOSE (t) = lb;
4469 low_bound = lb;
4470 }
4471 }
4472 }
4473 else if (TREE_CODE (type) == POINTER_TYPE)
4474 {
4475 if (length == NULL_TREE)
4476 {
4477 error_at (OMP_CLAUSE_LOCATION (c),
4478 "for pointer type length expression must be specified");
4479 return error_mark_node;
4480 }
4481 /* If there is a pointer type anywhere but in the very first
4482 array-section-subscript, the array section can't be contiguous. */
4483 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4484 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4485 {
4486 error_at (OMP_CLAUSE_LOCATION (c),
4487 "array section is not contiguous in %qs clause",
4488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4489 return error_mark_node;
4490 }
4491 }
4492 else
4493 {
4494 error_at (OMP_CLAUSE_LOCATION (c),
4495 "%qE does not have pointer or array type", ret);
4496 return error_mark_node;
4497 }
4498 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4499 types.safe_push (TREE_TYPE (ret));
4500 /* We will need to evaluate lb more than once. */
4501 tree lb = cp_save_expr (low_bound);
4502 if (lb != low_bound)
4503 {
4504 TREE_PURPOSE (t) = lb;
4505 low_bound = lb;
4506 }
4507 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4508 return ret;
4509}
4510
4511/* Handle array sections for clause C. */
4512
4513static bool
4514handle_omp_array_sections (tree c)
4515{
4516 bool maybe_zero_len = false;
4517 unsigned int first_non_one = 0;
ef062b13 4518 auto_vec<tree> types;
acf0174b
JJ
4519 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4520 maybe_zero_len, first_non_one);
4521 if (first == error_mark_node)
ef062b13 4522 return true;
acf0174b 4523 if (first == NULL_TREE)
ef062b13 4524 return false;
acf0174b
JJ
4525 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4526 {
4527 tree t = OMP_CLAUSE_DECL (c);
4528 tree tem = NULL_TREE;
acf0174b
JJ
4529 if (processing_template_decl)
4530 return false;
4531 /* Need to evaluate side effects in the length expressions
4532 if any. */
4533 while (TREE_CODE (t) == TREE_LIST)
4534 {
4535 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4536 {
4537 if (tem == NULL_TREE)
4538 tem = TREE_VALUE (t);
4539 else
4540 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4541 TREE_VALUE (t), tem);
4542 }
4543 t = TREE_CHAIN (t);
4544 }
4545 if (tem)
4546 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4547 OMP_CLAUSE_DECL (c) = first;
4548 }
4549 else
4550 {
4551 unsigned int num = types.length (), i;
4552 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4553 tree condition = NULL_TREE;
4554
4555 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4556 maybe_zero_len = true;
4557 if (processing_template_decl && maybe_zero_len)
ef062b13 4558 return false;
acf0174b
JJ
4559
4560 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4561 t = TREE_CHAIN (t))
4562 {
4563 tree low_bound = TREE_PURPOSE (t);
4564 tree length = TREE_VALUE (t);
4565
4566 i--;
4567 if (low_bound
4568 && TREE_CODE (low_bound) == INTEGER_CST
4569 && TYPE_PRECISION (TREE_TYPE (low_bound))
4570 > TYPE_PRECISION (sizetype))
4571 low_bound = fold_convert (sizetype, low_bound);
4572 if (length
4573 && TREE_CODE (length) == INTEGER_CST
4574 && TYPE_PRECISION (TREE_TYPE (length))
4575 > TYPE_PRECISION (sizetype))
4576 length = fold_convert (sizetype, length);
4577 if (low_bound == NULL_TREE)
4578 low_bound = integer_zero_node;
4579 if (!maybe_zero_len && i > first_non_one)
4580 {
4581 if (integer_nonzerop (low_bound))
4582 goto do_warn_noncontiguous;
4583 if (length != NULL_TREE
4584 && TREE_CODE (length) == INTEGER_CST
4585 && TYPE_DOMAIN (types[i])
4586 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4587 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4588 == INTEGER_CST)
4589 {
4590 tree size;
4591 size = size_binop (PLUS_EXPR,
4592 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4593 size_one_node);
4594 if (!tree_int_cst_equal (length, size))
4595 {
4596 do_warn_noncontiguous:
4597 error_at (OMP_CLAUSE_LOCATION (c),
4598 "array section is not contiguous in %qs "
4599 "clause",
4600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
4601 return true;
4602 }
4603 }
4604 if (!processing_template_decl
4605 && length != NULL_TREE
4606 && TREE_SIDE_EFFECTS (length))
4607 {
4608 if (side_effects == NULL_TREE)
4609 side_effects = length;
4610 else
4611 side_effects = build2 (COMPOUND_EXPR,
4612 TREE_TYPE (side_effects),
4613 length, side_effects);
4614 }
4615 }
4616 else if (processing_template_decl)
4617 continue;
4618 else
4619 {
4620 tree l;
4621
4622 if (i > first_non_one && length && integer_nonzerop (length))
4623 continue;
4624 if (length)
4625 l = fold_convert (sizetype, length);
4626 else
4627 {
4628 l = size_binop (PLUS_EXPR,
4629 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4630 size_one_node);
4631 l = size_binop (MINUS_EXPR, l,
4632 fold_convert (sizetype, low_bound));
4633 }
4634 if (i > first_non_one)
4635 {
4636 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4637 size_zero_node);
4638 if (condition == NULL_TREE)
4639 condition = l;
4640 else
4641 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4642 l, condition);
4643 }
4644 else if (size == NULL_TREE)
4645 {
4646 size = size_in_bytes (TREE_TYPE (types[i]));
4647 size = size_binop (MULT_EXPR, size, l);
4648 if (condition)
4649 size = fold_build3 (COND_EXPR, sizetype, condition,
4650 size, size_zero_node);
4651 }
4652 else
4653 size = size_binop (MULT_EXPR, size, l);
4654 }
4655 }
acf0174b
JJ
4656 if (!processing_template_decl)
4657 {
4658 if (side_effects)
4659 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4660 OMP_CLAUSE_DECL (c) = first;
4661 OMP_CLAUSE_SIZE (c) = size;
4662 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4663 return false;
4664 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4665 OMP_CLAUSE_MAP);
41dbbb37 4666 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
4667 if (!cxx_mark_addressable (t))
4668 return false;
4669 OMP_CLAUSE_DECL (c2) = t;
4670 t = build_fold_addr_expr (first);
4671 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4672 ptrdiff_type_node, t);
4673 tree ptr = OMP_CLAUSE_DECL (c2);
4674 ptr = convert_from_reference (ptr);
4675 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4676 ptr = build_fold_addr_expr (ptr);
4677 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4678 ptrdiff_type_node, t,
4679 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4680 ptrdiff_type_node, ptr));
4681 OMP_CLAUSE_SIZE (c2) = t;
4682 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4683 OMP_CLAUSE_CHAIN (c) = c2;
4684 ptr = OMP_CLAUSE_DECL (c2);
4685 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4686 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4687 {
4688 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4689 OMP_CLAUSE_MAP);
41dbbb37 4690 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
acf0174b
JJ
4691 OMP_CLAUSE_DECL (c3) = ptr;
4692 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4693 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4694 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4695 OMP_CLAUSE_CHAIN (c2) = c3;
4696 }
4697 }
4698 }
4699 return false;
4700}
4701
4702/* Return identifier to look up for omp declare reduction. */
4703
4704tree
4705omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4706{
4707 const char *p = NULL;
4708 const char *m = NULL;
4709 switch (reduction_code)
4710 {
4711 case PLUS_EXPR:
4712 case MULT_EXPR:
4713 case MINUS_EXPR:
4714 case BIT_AND_EXPR:
4715 case BIT_XOR_EXPR:
4716 case BIT_IOR_EXPR:
4717 case TRUTH_ANDIF_EXPR:
4718 case TRUTH_ORIF_EXPR:
4719 reduction_id = ansi_opname (reduction_code);
4720 break;
4721 case MIN_EXPR:
4722 p = "min";
4723 break;
4724 case MAX_EXPR:
4725 p = "max";
4726 break;
4727 default:
4728 break;
4729 }
4730
4731 if (p == NULL)
4732 {
4733 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4734 return error_mark_node;
4735 p = IDENTIFIER_POINTER (reduction_id);
4736 }
4737
4738 if (type != NULL_TREE)
4739 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4740
4741 const char prefix[] = "omp declare reduction ";
4742 size_t lenp = sizeof (prefix);
4743 if (strncmp (p, prefix, lenp - 1) == 0)
4744 lenp = 1;
4745 size_t len = strlen (p);
4746 size_t lenm = m ? strlen (m) + 1 : 0;
4747 char *name = XALLOCAVEC (char, lenp + len + lenm);
4748 if (lenp > 1)
4749 memcpy (name, prefix, lenp - 1);
4750 memcpy (name + lenp - 1, p, len + 1);
4751 if (m)
4752 {
4753 name[lenp + len - 1] = '~';
4754 memcpy (name + lenp + len, m, lenm);
4755 }
4756 return get_identifier (name);
4757}
4758
4759/* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4760 FUNCTION_DECL or NULL_TREE if not found. */
4761
4762static tree
4763omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4764 vec<tree> *ambiguousp)
4765{
4766 tree orig_id = id;
4767 tree baselink = NULL_TREE;
4768 if (identifier_p (id))
4769 {
4770 cp_id_kind idk;
4771 bool nonint_cst_expression_p;
4772 const char *error_msg;
4773 id = omp_reduction_id (ERROR_MARK, id, type);
4774 tree decl = lookup_name (id);
4775 if (decl == NULL_TREE)
4776 decl = error_mark_node;
4777 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4778 &nonint_cst_expression_p, false, true, false,
4779 false, &error_msg, loc);
4780 if (idk == CP_ID_KIND_UNQUALIFIED
4781 && identifier_p (id))
4782 {
4783 vec<tree, va_gc> *args = NULL;
4784 vec_safe_push (args, build_reference_type (type));
cdc23b1b 4785 id = perform_koenig_lookup (id, args, tf_none);
acf0174b
JJ
4786 }
4787 }
4788 else if (TREE_CODE (id) == SCOPE_REF)
4789 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4790 omp_reduction_id (ERROR_MARK,
4791 TREE_OPERAND (id, 1),
4792 type),
4793 false, false);
4794 tree fns = id;
4795 if (id && is_overloaded_fn (id))
4796 id = get_fns (id);
4797 for (; id; id = OVL_NEXT (id))
4798 {
4799 tree fndecl = OVL_CURRENT (id);
4800 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4801 {
4802 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4803 if (same_type_p (TREE_TYPE (argtype), type))
4804 break;
4805 }
4806 }
4807 if (id && BASELINK_P (fns))
4808 {
4809 if (baselinkp)
4810 *baselinkp = fns;
4811 else
4812 baselink = fns;
4813 }
4814 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4815 {
4816 vec<tree> ambiguous = vNULL;
4817 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4818 unsigned int ix;
4819 if (ambiguousp == NULL)
4820 ambiguousp = &ambiguous;
4821 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4822 {
4823 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4824 baselinkp ? baselinkp : &baselink,
4825 ambiguousp);
4826 if (id == NULL_TREE)
4827 continue;
4828 if (!ambiguousp->is_empty ())
4829 ambiguousp->safe_push (id);
4830 else if (ret != NULL_TREE)
4831 {
4832 ambiguousp->safe_push (ret);
4833 ambiguousp->safe_push (id);
4834 ret = NULL_TREE;
4835 }
4836 else
4837 ret = id;
4838 }
4839 if (ambiguousp != &ambiguous)
4840 return ret;
4841 if (!ambiguous.is_empty ())
4842 {
4843 const char *str = _("candidates are:");
4844 unsigned int idx;
4845 tree udr;
4846 error_at (loc, "user defined reduction lookup is ambiguous");
4847 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4848 {
4849 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4850 if (idx == 0)
4851 str = get_spaces (str);
4852 }
4853 ambiguous.release ();
4854 ret = error_mark_node;
4855 baselink = NULL_TREE;
4856 }
4857 id = ret;
4858 }
4859 if (id && baselink)
4860 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4861 id, id, tf_warning_or_error);
4862 return id;
4863}
4864
4865/* Helper function for cp_parser_omp_declare_reduction_exprs
4866 and tsubst_omp_udr.
4867 Remove CLEANUP_STMT for data (omp_priv variable).
4868 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4869 DECL_EXPR. */
4870
4871tree
4872cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4873{
4874 if (TYPE_P (*tp))
4875 *walk_subtrees = 0;
4876 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4877 *tp = CLEANUP_BODY (*tp);
4878 else if (TREE_CODE (*tp) == DECL_EXPR)
4879 {
4880 tree decl = DECL_EXPR_DECL (*tp);
4881 if (!processing_template_decl
4882 && decl == (tree) data
4883 && DECL_INITIAL (decl)
4884 && DECL_INITIAL (decl) != error_mark_node)
4885 {
4886 tree list = NULL_TREE;
4887 append_to_statement_list_force (*tp, &list);
4888 tree init_expr = build2 (INIT_EXPR, void_type_node,
4889 decl, DECL_INITIAL (decl));
4890 DECL_INITIAL (decl) = NULL_TREE;
4891 append_to_statement_list_force (init_expr, &list);
4892 *tp = list;
4893 }
4894 }
4895 return NULL_TREE;
4896}
4897
4898/* Data passed from cp_check_omp_declare_reduction to
4899 cp_check_omp_declare_reduction_r. */
4900
4901struct cp_check_omp_declare_reduction_data
4902{
4903 location_t loc;
4904 tree stmts[7];
4905 bool combiner_p;
4906};
4907
4908/* Helper function for cp_check_omp_declare_reduction, called via
4909 cp_walk_tree. */
4910
4911static tree
4912cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4913{
4914 struct cp_check_omp_declare_reduction_data *udr_data
4915 = (struct cp_check_omp_declare_reduction_data *) data;
4916 if (SSA_VAR_P (*tp)
4917 && !DECL_ARTIFICIAL (*tp)
4918 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4919 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4920 {
4921 location_t loc = udr_data->loc;
4922 if (udr_data->combiner_p)
4923 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4924 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4925 *tp);
4926 else
4927 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4928 "to variable %qD which is not %<omp_priv%> nor "
4929 "%<omp_orig%>",
4930 *tp);
4931 return *tp;
4932 }
4933 return NULL_TREE;
4934}
4935
4936/* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4937
4938void
4939cp_check_omp_declare_reduction (tree udr)
4940{
4941 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4942 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4943 type = TREE_TYPE (type);
4944 int i;
4945 location_t loc = DECL_SOURCE_LOCATION (udr);
4946
4947 if (type == error_mark_node)
4948 return;
4949 if (ARITHMETIC_TYPE_P (type))
4950 {
4951 static enum tree_code predef_codes[]
4952 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4953 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4954 for (i = 0; i < 8; i++)
4955 {
4956 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4957 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4958 const char *n2 = IDENTIFIER_POINTER (id);
4959 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4960 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4961 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4962 break;
4963 }
4964
4965 if (i == 8
4966 && TREE_CODE (type) != COMPLEX_EXPR)
4967 {
4968 const char prefix_minmax[] = "omp declare reduction m";
4969 size_t prefix_size = sizeof (prefix_minmax) - 1;
4970 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4971 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4972 prefix_minmax, prefix_size) == 0
4973 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4974 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4975 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4976 i = 0;
4977 }
4978 if (i < 8)
4979 {
4980 error_at (loc, "predeclared arithmetic type %qT in "
4981 "%<#pragma omp declare reduction%>", type);
4982 return;
4983 }
4984 }
4985 else if (TREE_CODE (type) == FUNCTION_TYPE
4986 || TREE_CODE (type) == METHOD_TYPE
4987 || TREE_CODE (type) == ARRAY_TYPE)
4988 {
4989 error_at (loc, "function or array type %qT in "
4990 "%<#pragma omp declare reduction%>", type);
4991 return;
4992 }
4993 else if (TREE_CODE (type) == REFERENCE_TYPE)
4994 {
4995 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4996 type);
4997 return;
4998 }
4999 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5000 {
5001 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5002 "%<#pragma omp declare reduction%>", type);
5003 return;
5004 }
5005
5006 tree body = DECL_SAVED_TREE (udr);
5007 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5008 return;
5009
5010 tree_stmt_iterator tsi;
5011 struct cp_check_omp_declare_reduction_data data;
5012 memset (data.stmts, 0, sizeof data.stmts);
5013 for (i = 0, tsi = tsi_start (body);
5014 i < 7 && !tsi_end_p (tsi);
5015 i++, tsi_next (&tsi))
5016 data.stmts[i] = tsi_stmt (tsi);
5017 data.loc = loc;
5018 gcc_assert (tsi_end_p (tsi));
5019 if (i >= 3)
5020 {
5021 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5022 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5023 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5024 return;
5025 data.combiner_p = true;
5026 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5027 &data, NULL))
5028 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5029 }
5030 if (i >= 6)
5031 {
5032 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5033 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5034 data.combiner_p = false;
5035 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5036 &data, NULL)
5037 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5038 cp_check_omp_declare_reduction_r, &data, NULL))
5039 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5040 if (i == 7)
5041 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5042 }
5043}
5044
5045/* Helper function of finish_omp_clauses. Clone STMT as if we were making
5046 an inline call. But, remap
5047 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5048 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5049
5050static tree
5051clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5052 tree decl, tree placeholder)
5053{
5054 copy_body_data id;
b787e7a2 5055 hash_map<tree, tree> decl_map;
acf0174b 5056
b787e7a2
TS
5057 decl_map.put (omp_decl1, placeholder);
5058 decl_map.put (omp_decl2, decl);
acf0174b
JJ
5059 memset (&id, 0, sizeof (id));
5060 id.src_fn = DECL_CONTEXT (omp_decl1);
5061 id.dst_fn = current_function_decl;
5062 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 5063 id.decl_map = &decl_map;
acf0174b
JJ
5064
5065 id.copy_decl = copy_decl_no_change;
5066 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5067 id.transform_new_cfg = true;
5068 id.transform_return_to_modify = false;
5069 id.transform_lang_insert_block = NULL;
5070 id.eh_lp_nr = 0;
5071 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
5072 return stmt;
5073}
5074
5075/* Helper function of finish_omp_clauses, called via cp_walk_tree.
5076 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5077
5078static tree
5079find_omp_placeholder_r (tree *tp, int *, void *data)
5080{
5081 if (*tp == (tree) data)
5082 return *tp;
5083 return NULL_TREE;
5084}
5085
5086/* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5087 Return true if there is some error and the clause should be removed. */
5088
5089static bool
5090finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5091{
5092 tree t = OMP_CLAUSE_DECL (c);
5093 bool predefined = false;
5094 tree type = TREE_TYPE (t);
5095 if (TREE_CODE (type) == REFERENCE_TYPE)
5096 type = TREE_TYPE (type);
4ca56230
PC
5097 if (type == error_mark_node)
5098 return true;
5099 else if (ARITHMETIC_TYPE_P (type))
acf0174b
JJ
5100 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5101 {
5102 case PLUS_EXPR:
5103 case MULT_EXPR:
5104 case MINUS_EXPR:
5105 predefined = true;
5106 break;
5107 case MIN_EXPR:
5108 case MAX_EXPR:
5109 if (TREE_CODE (type) == COMPLEX_TYPE)
5110 break;
5111 predefined = true;
5112 break;
5113 case BIT_AND_EXPR:
5114 case BIT_IOR_EXPR:
5115 case BIT_XOR_EXPR:
652fea39
JJ
5116 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5117 break;
5118 predefined = true;
5119 break;
acf0174b
JJ
5120 case TRUTH_ANDIF_EXPR:
5121 case TRUTH_ORIF_EXPR:
5122 if (FLOAT_TYPE_P (type))
5123 break;
5124 predefined = true;
5125 break;
5126 default:
5127 break;
5128 }
5129 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5130 {
5131 error ("%qE has invalid type for %<reduction%>", t);
5132 return true;
5133 }
5134 else if (!processing_template_decl)
5135 {
5136 t = require_complete_type (t);
5137 if (t == error_mark_node)
5138 return true;
5139 OMP_CLAUSE_DECL (c) = t;
5140 }
5141
5142 if (predefined)
5143 {
5144 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5145 return false;
5146 }
5147 else if (processing_template_decl)
5148 return false;
5149
5150 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5151
5152 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5153 if (TREE_CODE (type) == REFERENCE_TYPE)
5154 type = TREE_TYPE (type);
5155 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5156 if (id == NULL_TREE)
5157 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5158 NULL_TREE, NULL_TREE);
5159 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5160 if (id)
5161 {
5162 if (id == error_mark_node)
5163 return true;
5164 id = OVL_CURRENT (id);
5165 mark_used (id);
5166 tree body = DECL_SAVED_TREE (id);
88957d5e
PC
5167 if (!body)
5168 return true;
acf0174b
JJ
5169 if (TREE_CODE (body) == STATEMENT_LIST)
5170 {
5171 tree_stmt_iterator tsi;
5172 tree placeholder = NULL_TREE;
5173 int i;
5174 tree stmts[7];
5175 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5176 atype = TREE_TYPE (atype);
5177 bool need_static_cast = !same_type_p (type, atype);
5178 memset (stmts, 0, sizeof stmts);
5179 for (i = 0, tsi = tsi_start (body);
5180 i < 7 && !tsi_end_p (tsi);
5181 i++, tsi_next (&tsi))
5182 stmts[i] = tsi_stmt (tsi);
5183 gcc_assert (tsi_end_p (tsi));
5184
5185 if (i >= 3)
5186 {
5187 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5188 && TREE_CODE (stmts[1]) == DECL_EXPR);
5189 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5190 DECL_ARTIFICIAL (placeholder) = 1;
5191 DECL_IGNORED_P (placeholder) = 1;
5192 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5193 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5194 cxx_mark_addressable (placeholder);
5195 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5196 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5197 != REFERENCE_TYPE)
5198 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5199 tree omp_out = placeholder;
5200 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5201 if (need_static_cast)
5202 {
5203 tree rtype = build_reference_type (atype);
5204 omp_out = build_static_cast (rtype, omp_out,
5205 tf_warning_or_error);
5206 omp_in = build_static_cast (rtype, omp_in,
5207 tf_warning_or_error);
5208 if (omp_out == error_mark_node || omp_in == error_mark_node)
5209 return true;
5210 omp_out = convert_from_reference (omp_out);
5211 omp_in = convert_from_reference (omp_in);
5212 }
5213 OMP_CLAUSE_REDUCTION_MERGE (c)
5214 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5215 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5216 }
5217 if (i >= 6)
5218 {
5219 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5220 && TREE_CODE (stmts[4]) == DECL_EXPR);
5221 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5222 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5223 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5224 cxx_mark_addressable (placeholder);
5225 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5226 tree omp_orig = placeholder;
5227 if (need_static_cast)
5228 {
5229 if (i == 7)
5230 {
5231 error_at (OMP_CLAUSE_LOCATION (c),
5232 "user defined reduction with constructor "
5233 "initializer for base class %qT", atype);
5234 return true;
5235 }
5236 tree rtype = build_reference_type (atype);
5237 omp_priv = build_static_cast (rtype, omp_priv,
5238 tf_warning_or_error);
5239 omp_orig = build_static_cast (rtype, omp_orig,
5240 tf_warning_or_error);
5241 if (omp_priv == error_mark_node
5242 || omp_orig == error_mark_node)
5243 return true;
5244 omp_priv = convert_from_reference (omp_priv);
5245 omp_orig = convert_from_reference (omp_orig);
5246 }
5247 if (i == 6)
5248 *need_default_ctor = true;
5249 OMP_CLAUSE_REDUCTION_INIT (c)
5250 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5251 DECL_EXPR_DECL (stmts[3]),
5252 omp_priv, omp_orig);
5253 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5254 find_omp_placeholder_r, placeholder, NULL))
5255 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5256 }
5257 else if (i >= 3)
5258 {
5259 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5260 *need_default_ctor = true;
5261 else
5262 {
5263 tree init;
5264 tree v = convert_from_reference (t);
5265 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5266 init = build_constructor (TREE_TYPE (v), NULL);
5267 else
5268 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5269 OMP_CLAUSE_REDUCTION_INIT (c)
5270 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5271 }
5272 }
5273 }
5274 }
5275 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5276 *need_dtor = true;
5277 else
5278 {
5279 error ("user defined reduction not found for %qD", t);
5280 return true;
5281 }
5282 return false;
5283}
5284
1799e5d5
RH
5285/* For all elements of CLAUSES, validate them vs OpenMP constraints.
5286 Remove any elements from the list that are invalid. */
5287
5288tree
5289finish_omp_clauses (tree clauses)
5290{
5291 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 5292 bitmap_head aligned_head;
f3316c6d 5293 tree c, t, *pc;
acf0174b
JJ
5294 bool branch_seen = false;
5295 bool copyprivate_seen = false;
1799e5d5
RH
5296
5297 bitmap_obstack_initialize (NULL);
5298 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5299 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5300 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 5301 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
1799e5d5
RH
5302
5303 for (pc = &clauses, c = clauses; c ; c = *pc)
5304 {
5305 bool remove = false;
5306
5307 switch (OMP_CLAUSE_CODE (c))
5308 {
5309 case OMP_CLAUSE_SHARED:
1799e5d5
RH
5310 goto check_dup_generic;
5311 case OMP_CLAUSE_PRIVATE:
1799e5d5
RH
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_REDUCTION:
1799e5d5
RH
5314 goto check_dup_generic;
5315 case OMP_CLAUSE_COPYPRIVATE:
acf0174b 5316 copyprivate_seen = true;
1799e5d5
RH
5317 goto check_dup_generic;
5318 case OMP_CLAUSE_COPYIN:
acf0174b
JJ
5319 goto check_dup_generic;
5320 case OMP_CLAUSE_LINEAR:
5321 t = OMP_CLAUSE_DECL (c);
7da8534d
JJ
5322 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5323 && !type_dependent_expression_p (t)
acf0174b
JJ
5324 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5325 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5326 {
5327 error ("linear clause applied to non-integral non-pointer "
5328 "variable with %qT type", TREE_TYPE (t));
5329 remove = true;
5330 break;
5331 }
5332 t = OMP_CLAUSE_LINEAR_STEP (c);
5333 if (t == NULL_TREE)
5334 t = integer_one_node;
5335 if (t == error_mark_node)
ee1d5a02
JJ
5336 {
5337 remove = true;
5338 break;
5339 }
acf0174b
JJ
5340 else if (!type_dependent_expression_p (t)
5341 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5342 {
5343 error ("linear step expression must be integral");
5344 remove = true;
ee1d5a02 5345 break;
acf0174b
JJ
5346 }
5347 else
5348 {
5349 t = mark_rvalue_use (t);
7da8534d
JJ
5350 if (!processing_template_decl
5351 && (VAR_P (OMP_CLAUSE_DECL (c))
5352 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
acf0174b 5353 {
0136f8f0
AH
5354 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5355 t = maybe_constant_value (t);
acf0174b
JJ
5356 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5357 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5358 == POINTER_TYPE)
5359 {
5360 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5361 OMP_CLAUSE_DECL (c), t);
5362 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5363 MINUS_EXPR, sizetype, t,
5364 OMP_CLAUSE_DECL (c));
5365 if (t == error_mark_node)
ee1d5a02
JJ
5366 {
5367 remove = true;
5368 break;
5369 }
acf0174b 5370 }
da6f124d
JJ
5371 else
5372 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
acf0174b
JJ
5373 }
5374 OMP_CLAUSE_LINEAR_STEP (c) = t;
5375 }
1799e5d5
RH
5376 goto check_dup_generic;
5377 check_dup_generic:
5378 t = OMP_CLAUSE_DECL (c);
5a6ccc94 5379 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1799e5d5
RH
5380 {
5381 if (processing_template_decl)
5382 break;
76dc15d4 5383 if (DECL_P (t))
acf0174b
JJ
5384 error ("%qD is not a variable in clause %qs", t,
5385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
76dc15d4 5386 else
acf0174b
JJ
5387 error ("%qE is not a variable in clause %qs", t,
5388 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1799e5d5
RH
5389 remove = true;
5390 }
5391 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5392 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5393 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5394 {
76dc15d4 5395 error ("%qD appears more than once in data clauses", t);
1799e5d5
RH
5396 remove = true;
5397 }
5398 else
5399 bitmap_set_bit (&generic_head, DECL_UID (t));
5400 break;
5401
5402 case OMP_CLAUSE_FIRSTPRIVATE:
5403 t = OMP_CLAUSE_DECL (c);
5a6ccc94 5404 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1799e5d5
RH
5405 {
5406 if (processing_template_decl)
5407 break;
85b20612
JJ
5408 if (DECL_P (t))
5409 error ("%qD is not a variable in clause %<firstprivate%>", t);
5410 else
5411 error ("%qE is not a variable in clause %<firstprivate%>", t);
1799e5d5
RH
5412 remove = true;
5413 }
5414 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5415 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5416 {
85b20612 5417 error ("%qD appears more than once in data clauses", t);
1799e5d5
RH
5418 remove = true;
5419 }
5420 else
5421 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5422 break;
5423
5424 case OMP_CLAUSE_LASTPRIVATE:
5425 t = OMP_CLAUSE_DECL (c);
5a6ccc94 5426 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1799e5d5
RH
5427 {
5428 if (processing_template_decl)
5429 break;
85b20612
JJ
5430 if (DECL_P (t))
5431 error ("%qD is not a variable in clause %<lastprivate%>", t);
5432 else
5433 error ("%qE is not a variable in clause %<lastprivate%>", t);
1799e5d5
RH
5434 remove = true;
5435 }
5436 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5437 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5438 {
85b20612 5439 error ("%qD appears more than once in data clauses", t);
1799e5d5
RH
5440 remove = true;
5441 }
5442 else
5443 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5444 break;
5445
5446 case OMP_CLAUSE_IF:
5447 t = OMP_CLAUSE_IF_EXPR (c);
5448 t = maybe_convert_cond (t);
5449 if (t == error_mark_node)
5450 remove = true;
b848354b
JJ
5451 else if (!processing_template_decl)
5452 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1799e5d5
RH
5453 OMP_CLAUSE_IF_EXPR (c) = t;
5454 break;
5455
20906c66
JJ
5456 case OMP_CLAUSE_FINAL:
5457 t = OMP_CLAUSE_FINAL_EXPR (c);
5458 t = maybe_convert_cond (t);
5459 if (t == error_mark_node)
5460 remove = true;
b848354b
JJ
5461 else if (!processing_template_decl)
5462 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
20906c66
JJ
5463 OMP_CLAUSE_FINAL_EXPR (c) = t;
5464 break;
5465
1799e5d5
RH
5466 case OMP_CLAUSE_NUM_THREADS:
5467 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5468 if (t == error_mark_node)
5469 remove = true;
6e684eee
JJ
5470 else if (!type_dependent_expression_p (t)
5471 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
1799e5d5
RH
5472 {
5473 error ("num_threads expression must be integral");
5474 remove = true;
5475 }
7d1362bc 5476 else
b848354b
JJ
5477 {
5478 t = mark_rvalue_use (t);
5479 if (!processing_template_decl)
5480 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5481 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5482 }
1799e5d5
RH
5483 break;
5484
5485 case OMP_CLAUSE_SCHEDULE:
5486 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5487 if (t == NULL)
5488 ;
5489 else if (t == error_mark_node)
5490 remove = true;
6e684eee 5491 else if (!type_dependent_expression_p (t)
9a771876
JJ
5492 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5493 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6e684eee 5494 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
1799e5d5
RH
5495 {
5496 error ("schedule chunk size expression must be integral");
5497 remove = true;
5498 }
7d1362bc 5499 else
b848354b
JJ
5500 {
5501 t = mark_rvalue_use (t);
5502 if (!processing_template_decl)
9a771876
JJ
5503 {
5504 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5505 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5506 {
5507 t = convert_to_integer (long_integer_type_node, t);
5508 if (t == error_mark_node)
5509 {
5510 remove = true;
5511 break;
5512 }
5513 }
5514 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5515 }
b848354b
JJ
5516 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5517 }
1799e5d5
RH
5518 break;
5519
acf0174b
JJ
5520 case OMP_CLAUSE_SIMDLEN:
5521 case OMP_CLAUSE_SAFELEN:
5522 t = OMP_CLAUSE_OPERAND (c, 0);
5523 if (t == error_mark_node)
5524 remove = true;
5525 else if (!type_dependent_expression_p (t)
5526 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5527 {
5528 error ("%qs length expression must be integral",
5529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5530 remove = true;
5531 }
5532 else
5533 {
5534 t = mark_rvalue_use (t);
5535 t = maybe_constant_value (t);
5536 if (!processing_template_decl)
5537 {
5538 if (TREE_CODE (t) != INTEGER_CST
5539 || tree_int_cst_sgn (t) != 1)
5540 {
5541 error ("%qs length expression must be positive constant"
5542 " integer expression",
5543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5544 remove = true;
5545 }
5546 }
5547 OMP_CLAUSE_OPERAND (c, 0) = t;
5548 }
5549 break;
5550
5551 case OMP_CLAUSE_NUM_TEAMS:
5552 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5553 if (t == error_mark_node)
5554 remove = true;
5555 else if (!type_dependent_expression_p (t)
5556 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5557 {
5558 error ("%<num_teams%> expression must be integral");
5559 remove = true;
5560 }
5561 else
5562 {
5563 t = mark_rvalue_use (t);
5564 if (!processing_template_decl)
5565 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5566 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5567 }
5568 break;
5569
41dbbb37
TS
5570 case OMP_CLAUSE_ASYNC:
5571 t = OMP_CLAUSE_ASYNC_EXPR (c);
5572 if (t == error_mark_node)
5573 remove = true;
5574 else if (!type_dependent_expression_p (t)
5575 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5576 {
5577 error ("%<async%> expression must be integral");
5578 remove = true;
5579 }
5580 else
5581 {
5582 t = mark_rvalue_use (t);
5583 if (!processing_template_decl)
5584 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5585 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5586 }
5587 break;
5588
5589 case OMP_CLAUSE_VECTOR_LENGTH:
5590 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5591 t = maybe_convert_cond (t);
5592 if (t == error_mark_node)
5593 remove = true;
5594 else if (!processing_template_decl)
5595 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5596 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5597 break;
5598
5599 case OMP_CLAUSE_WAIT:
5600 t = OMP_CLAUSE_WAIT_EXPR (c);
5601 if (t == error_mark_node)
5602 remove = true;
5603 else if (!processing_template_decl)
5604 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5605 OMP_CLAUSE_WAIT_EXPR (c) = t;
5606 break;
5607
acf0174b
JJ
5608 case OMP_CLAUSE_THREAD_LIMIT:
5609 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5610 if (t == error_mark_node)
5611 remove = true;
5612 else if (!type_dependent_expression_p (t)
5613 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5614 {
5615 error ("%<thread_limit%> expression must be integral");
5616 remove = true;
5617 }
5618 else
5619 {
5620 t = mark_rvalue_use (t);
5621 if (!processing_template_decl)
5622 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5623 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5624 }
5625 break;
5626
5627 case OMP_CLAUSE_DEVICE:
5628 t = OMP_CLAUSE_DEVICE_ID (c);
5629 if (t == error_mark_node)
5630 remove = true;
5631 else if (!type_dependent_expression_p (t)
5632 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5633 {
5634 error ("%<device%> id must be integral");
5635 remove = true;
5636 }
5637 else
5638 {
5639 t = mark_rvalue_use (t);
5640 if (!processing_template_decl)
5641 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5642 OMP_CLAUSE_DEVICE_ID (c) = t;
5643 }
5644 break;
5645
5646 case OMP_CLAUSE_DIST_SCHEDULE:
5647 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5648 if (t == NULL)
5649 ;
5650 else if (t == error_mark_node)
5651 remove = true;
5652 else if (!type_dependent_expression_p (t)
5653 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5654 {
5655 error ("%<dist_schedule%> chunk size expression must be "
5656 "integral");
5657 remove = true;
5658 }
5659 else
5660 {
5661 t = mark_rvalue_use (t);
5662 if (!processing_template_decl)
5663 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5664 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5665 }
5666 break;
5667
5668 case OMP_CLAUSE_ALIGNED:
5669 t = OMP_CLAUSE_DECL (c);
56a6f1d3 5670 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
5671 {
5672 if (processing_template_decl)
5673 break;
5674 if (DECL_P (t))
5675 error ("%qD is not a variable in %<aligned%> clause", t);
5676 else
5677 error ("%qE is not a variable in %<aligned%> clause", t);
5678 remove = true;
5679 }
5a9785fb
JJ
5680 else if (!type_dependent_expression_p (t)
5681 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5682 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5683 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5684 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5685 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5686 != ARRAY_TYPE))))
5687 {
5688 error_at (OMP_CLAUSE_LOCATION (c),
5689 "%qE in %<aligned%> clause is neither a pointer nor "
5690 "an array nor a reference to pointer or array", t);
5691 remove = true;
5692 }
acf0174b
JJ
5693 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5694 {
5695 error ("%qD appears more than once in %<aligned%> clauses", t);
5696 remove = true;
5697 }
5698 else
5699 bitmap_set_bit (&aligned_head, DECL_UID (t));
5700 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5701 if (t == error_mark_node)
5702 remove = true;
5703 else if (t == NULL_TREE)
5704 break;
5705 else if (!type_dependent_expression_p (t)
5706 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5707 {
5708 error ("%<aligned%> clause alignment expression must "
5709 "be integral");
5710 remove = true;
5711 }
5712 else
5713 {
5714 t = mark_rvalue_use (t);
5715 t = maybe_constant_value (t);
5716 if (!processing_template_decl)
5717 {
5718 if (TREE_CODE (t) != INTEGER_CST
5719 || tree_int_cst_sgn (t) != 1)
5720 {
5721 error ("%<aligned%> clause alignment expression must be "
5722 "positive constant integer expression");
5723 remove = true;
5724 }
5725 }
5726 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5727 }
5728 break;
5729
5730 case OMP_CLAUSE_DEPEND:
5731 t = OMP_CLAUSE_DECL (c);
5732 if (TREE_CODE (t) == TREE_LIST)
5733 {
5734 if (handle_omp_array_sections (c))
5735 remove = true;
5736 break;
5737 }
5738 if (t == error_mark_node)
5739 remove = true;
56a6f1d3 5740 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
5741 {
5742 if (processing_template_decl)
5743 break;
5744 if (DECL_P (t))
5745 error ("%qD is not a variable in %<depend%> clause", t);
5746 else
5747 error ("%qE is not a variable in %<depend%> clause", t);
5748 remove = true;
5749 }
5750 else if (!processing_template_decl
5751 && !cxx_mark_addressable (t))
5752 remove = true;
5753 break;
5754
5755 case OMP_CLAUSE_MAP:
5756 case OMP_CLAUSE_TO:
5757 case OMP_CLAUSE_FROM:
41dbbb37 5758 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
5759 t = OMP_CLAUSE_DECL (c);
5760 if (TREE_CODE (t) == TREE_LIST)
5761 {
5762 if (handle_omp_array_sections (c))
5763 remove = true;
5764 else
5765 {
5766 t = OMP_CLAUSE_DECL (c);
bce16b88
JJ
5767 if (TREE_CODE (t) != TREE_LIST
5768 && !type_dependent_expression_p (t)
5769 && !cp_omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
5770 {
5771 error_at (OMP_CLAUSE_LOCATION (c),
5772 "array section does not have mappable type "
5773 "in %qs clause",
5774 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5775 remove = true;
5776 }
5777 }
5778 break;
5779 }
5780 if (t == error_mark_node)
5781 remove = true;
56a6f1d3 5782 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
5783 {
5784 if (processing_template_decl)
5785 break;
5786 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 5787 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
acf0174b
JJ
5788 break;
5789 if (DECL_P (t))
5790 error ("%qD is not a variable in %qs clause", t,
5791 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5792 else
5793 error ("%qE is not a variable in %qs clause", t,
5794 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5795 remove = true;
5796 }
3048c0c7 5797 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
5798 {
5799 error ("%qD is threadprivate variable in %qs clause", t,
5800 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5801 remove = true;
5802 }
5803 else if (!processing_template_decl
5804 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5805 && !cxx_mark_addressable (t))
5806 remove = true;
5807 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 5808 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
bce16b88 5809 && !type_dependent_expression_p (t)
acf0174b
JJ
5810 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5811 == REFERENCE_TYPE)
5812 ? TREE_TYPE (TREE_TYPE (t))
5813 : TREE_TYPE (t)))
5814 {
5815 error_at (OMP_CLAUSE_LOCATION (c),
5816 "%qD does not have a mappable type in %qs clause", t,
5817 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5818 remove = true;
5819 }
5820 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5821 {
5822 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5823 error ("%qD appears more than once in motion clauses", t);
5824 else
5825 error ("%qD appears more than once in map clauses", t);
5826 remove = true;
5827 }
5828 else
5829 bitmap_set_bit (&generic_head, DECL_UID (t));
5830 break;
5831
5832 case OMP_CLAUSE_UNIFORM:
5833 t = OMP_CLAUSE_DECL (c);
5834 if (TREE_CODE (t) != PARM_DECL)
5835 {
5836 if (processing_template_decl)
5837 break;
5838 if (DECL_P (t))
5839 error ("%qD is not an argument in %<uniform%> clause", t);
5840 else
5841 error ("%qE is not an argument in %<uniform%> clause", t);
5842 remove = true;
ee1d5a02 5843 break;
acf0174b 5844 }
ee1d5a02 5845 goto check_dup_generic;
acf0174b 5846
1799e5d5
RH
5847 case OMP_CLAUSE_NOWAIT:
5848 case OMP_CLAUSE_ORDERED:
5849 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
5850 case OMP_CLAUSE_UNTIED:
5851 case OMP_CLAUSE_COLLAPSE:
20906c66 5852 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
5853 case OMP_CLAUSE_PARALLEL:
5854 case OMP_CLAUSE_FOR:
5855 case OMP_CLAUSE_SECTIONS:
5856 case OMP_CLAUSE_TASKGROUP:
5857 case OMP_CLAUSE_PROC_BIND:
9a771876 5858 case OMP_CLAUSE__CILK_FOR_COUNT_:
acf0174b
JJ
5859 break;
5860
5861 case OMP_CLAUSE_INBRANCH:
5862 case OMP_CLAUSE_NOTINBRANCH:
5863 if (branch_seen)
5864 {
5865 error ("%<inbranch%> clause is incompatible with "
5866 "%<notinbranch%>");
5867 remove = true;
5868 }
5869 branch_seen = true;
1799e5d5
RH
5870 break;
5871
5872 default:
5873 gcc_unreachable ();
5874 }
5875
5876 if (remove)
5877 *pc = OMP_CLAUSE_CHAIN (c);
5878 else
5879 pc = &OMP_CLAUSE_CHAIN (c);
5880 }
5881
5882 for (pc = &clauses, c = clauses; c ; c = *pc)
5883 {
81f40b79 5884 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
1799e5d5
RH
5885 bool remove = false;
5886 bool need_complete_non_reference = false;
5887 bool need_default_ctor = false;
5888 bool need_copy_ctor = false;
5889 bool need_copy_assignment = false;
5890 bool need_implicitly_determined = false;
acf0174b 5891 bool need_dtor = false;
1799e5d5
RH
5892 tree type, inner_type;
5893
5894 switch (c_kind)
5895 {
5896 case OMP_CLAUSE_SHARED:
1799e5d5
RH
5897 need_implicitly_determined = true;
5898 break;
5899 case OMP_CLAUSE_PRIVATE:
1799e5d5
RH
5900 need_complete_non_reference = true;
5901 need_default_ctor = true;
acf0174b 5902 need_dtor = true;
1799e5d5
RH
5903 need_implicitly_determined = true;
5904 break;
5905 case OMP_CLAUSE_FIRSTPRIVATE:
1799e5d5
RH
5906 need_complete_non_reference = true;
5907 need_copy_ctor = true;
acf0174b 5908 need_dtor = true;
1799e5d5
RH
5909 need_implicitly_determined = true;
5910 break;
5911 case OMP_CLAUSE_LASTPRIVATE:
1799e5d5
RH
5912 need_complete_non_reference = true;
5913 need_copy_assignment = true;
5914 need_implicitly_determined = true;
5915 break;
5916 case OMP_CLAUSE_REDUCTION:
1799e5d5
RH
5917 need_implicitly_determined = true;
5918 break;
5919 case OMP_CLAUSE_COPYPRIVATE:
1799e5d5
RH
5920 need_copy_assignment = true;
5921 break;
5922 case OMP_CLAUSE_COPYIN:
1799e5d5
RH
5923 need_copy_assignment = true;
5924 break;
acf0174b
JJ
5925 case OMP_CLAUSE_NOWAIT:
5926 if (copyprivate_seen)
5927 {
5928 error_at (OMP_CLAUSE_LOCATION (c),
5929 "%<nowait%> clause must not be used together "
5930 "with %<copyprivate%>");
5931 *pc = OMP_CLAUSE_CHAIN (c);
5932 continue;
5933 }
5934 /* FALLTHRU */
1799e5d5
RH
5935 default:
5936 pc = &OMP_CLAUSE_CHAIN (c);
5937 continue;
5938 }
5939
5940 t = OMP_CLAUSE_DECL (c);
5941 if (processing_template_decl
5a6ccc94 5942 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1799e5d5
RH
5943 {
5944 pc = &OMP_CLAUSE_CHAIN (c);
5945 continue;
5946 }
5947
5948 switch (c_kind)
5949 {
5950 case OMP_CLAUSE_LASTPRIVATE:
5951 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
acf0174b
JJ
5952 {
5953 need_default_ctor = true;
5954 need_dtor = true;
5955 }
1799e5d5
RH
5956 break;
5957
5958 case OMP_CLAUSE_REDUCTION:
acf0174b
JJ
5959 if (finish_omp_reduction_clause (c, &need_default_ctor,
5960 &need_dtor))
5961 remove = true;
5962 else
5963 t = OMP_CLAUSE_DECL (c);
1799e5d5
RH
5964 break;
5965
5966 case OMP_CLAUSE_COPYIN:
3048c0c7 5967 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
1799e5d5
RH
5968 {
5969 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5970 remove = true;
5971 }
5972 break;
5973
5974 default:
5975 break;
5976 }
5977
8a8c12a3 5978 if (need_complete_non_reference || need_copy_assignment)
1799e5d5
RH
5979 {
5980 t = require_complete_type (t);
5981 if (t == error_mark_node)
5982 remove = true;
8a8c12a3
JJ
5983 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5984 && need_complete_non_reference)
1799e5d5 5985 {
acf0174b
JJ
5986 error ("%qE has reference type for %qs", t,
5987 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1799e5d5
RH
5988 remove = true;
5989 }
5990 }
5991 if (need_implicitly_determined)
5992 {
5993 const char *share_name = NULL;
5994
3048c0c7 5995 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
1799e5d5
RH
5996 share_name = "threadprivate";
5997 else switch (cxx_omp_predetermined_sharing (t))
5998 {
5999 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6000 break;
6001 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
6002 /* const vars may be specified in firstprivate clause. */
6003 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6004 && cxx_omp_const_qual_no_mutable (t))
6005 break;
1799e5d5
RH
6006 share_name = "shared";
6007 break;
6008 case OMP_CLAUSE_DEFAULT_PRIVATE:
6009 share_name = "private";
6010 break;
6011 default:
6012 gcc_unreachable ();
6013 }
6014 if (share_name)
6015 {
6016 error ("%qE is predetermined %qs for %qs",
acf0174b 6017 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1799e5d5
RH
6018 remove = true;
6019 }
6020 }
6021
6022 /* We're interested in the base element, not arrays. */
6023 inner_type = type = TREE_TYPE (t);
6024 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6025 inner_type = TREE_TYPE (inner_type);
6026
acf0174b
JJ
6027 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6028 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6029 inner_type = TREE_TYPE (inner_type);
6030
84dc00e8 6031 /* Check for special function availability by building a call to one.
1799e5d5
RH
6032 Save the results, because later we won't be in the right context
6033 for making these queries. */
6034 if (CLASS_TYPE_P (inner_type)
8a8c12a3 6035 && COMPLETE_TYPE_P (inner_type)
acf0174b
JJ
6036 && (need_default_ctor || need_copy_ctor
6037 || need_copy_assignment || need_dtor)
a68ab351
JJ
6038 && !type_dependent_expression_p (t)
6039 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
acf0174b
JJ
6040 need_copy_ctor, need_copy_assignment,
6041 need_dtor))
a68ab351 6042 remove = true;
1799e5d5
RH
6043
6044 if (remove)
6045 *pc = OMP_CLAUSE_CHAIN (c);
6046 else
6047 pc = &OMP_CLAUSE_CHAIN (c);
6048 }
6049
6050 bitmap_obstack_release (NULL);
6051 return clauses;
6052}
6053
6054/* For all variables in the tree_list VARS, mark them as thread local. */
6055
6056void
6057finish_omp_threadprivate (tree vars)
6058{
6059 tree t;
6060
6061 /* Mark every variable in VARS to be assigned thread local storage. */
6062 for (t = vars; t; t = TREE_CHAIN (t))
6063 {
6064 tree v = TREE_PURPOSE (t);
6065
edb6000e
JJ
6066 if (error_operand_p (v))
6067 ;
5a6ccc94 6068 else if (!VAR_P (v))
edb6000e
JJ
6069 error ("%<threadprivate%> %qD is not file, namespace "
6070 "or block scope variable", v);
1799e5d5
RH
6071 /* If V had already been marked threadprivate, it doesn't matter
6072 whether it had been used prior to this point. */
edb6000e 6073 else if (TREE_USED (v)
1799e5d5
RH
6074 && (DECL_LANG_SPECIFIC (v) == NULL
6075 || !CP_DECL_THREADPRIVATE_P (v)))
6076 error ("%qE declared %<threadprivate%> after first use", v);
6077 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6078 error ("automatic variable %qE cannot be %<threadprivate%>", v);
57c3feb4 6079 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
1799e5d5 6080 error ("%<threadprivate%> %qE has incomplete type", v);
a68ab351
JJ
6081 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6082 && CP_DECL_CONTEXT (v) != current_class_type)
6083 error ("%<threadprivate%> %qE directive not "
6084 "in %qT definition", v, CP_DECL_CONTEXT (v));
1799e5d5
RH
6085 else
6086 {
6087 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6088 if (DECL_LANG_SPECIFIC (v) == NULL)
6089 {
6090 retrofit_lang_decl (v);
6091
6092 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6093 after the allocation of the lang_decl structure. */
6094 if (DECL_DISCRIMINATOR_P (v))
b97e8a14 6095 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
1799e5d5
RH
6096 }
6097
3048c0c7 6098 if (! CP_DECL_THREAD_LOCAL_P (v))
1799e5d5 6099 {
3048c0c7 6100 CP_DECL_THREAD_LOCAL_P (v) = true;
56363ffd 6101 set_decl_tls_model (v, decl_default_tls_model (v));
1799e5d5
RH
6102 /* If rtl has been already set for this var, call
6103 make_decl_rtl once again, so that encode_section_info
6104 has a chance to look at the new decl flags. */
6105 if (DECL_RTL_SET_P (v))
6106 make_decl_rtl (v);
6107 }
6108 CP_DECL_THREADPRIVATE_P (v) = 1;
6109 }
6110 }
6111}
6112
6113/* Build an OpenMP structured block. */
6114
6115tree
6116begin_omp_structured_block (void)
6117{
6118 return do_pushlevel (sk_omp);
6119}
6120
6121tree
6122finish_omp_structured_block (tree block)
6123{
6124 return do_poplevel (block);
6125}
6126
41dbbb37
TS
6127/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6128 statement. LOC is the location of the OACC_DATA. */
6129
6130tree
6131finish_oacc_data (tree clauses, tree block)
6132{
6133 tree stmt;
6134
6135 block = finish_omp_structured_block (block);
6136
6137 stmt = make_node (OACC_DATA);
6138 TREE_TYPE (stmt) = void_type_node;
6139 OACC_DATA_CLAUSES (stmt) = clauses;
6140 OACC_DATA_BODY (stmt) = block;
6141
6142 return add_stmt (stmt);
6143}
6144
6145/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6146 statement. LOC is the location of the OACC_KERNELS. */
6147
6148tree
6149finish_oacc_kernels (tree clauses, tree block)
6150{
6151 tree stmt;
6152
6153 block = finish_omp_structured_block (block);
6154
6155 stmt = make_node (OACC_KERNELS);
6156 TREE_TYPE (stmt) = void_type_node;
6157 OACC_KERNELS_CLAUSES (stmt) = clauses;
6158 OACC_KERNELS_BODY (stmt) = block;
6159
6160 return add_stmt (stmt);
6161}
6162
6163/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6164 statement. LOC is the location of the OACC_PARALLEL. */
6165
6166tree
6167finish_oacc_parallel (tree clauses, tree block)
6168{
6169 tree stmt;
6170
6171 block = finish_omp_structured_block (block);
6172
6173 stmt = make_node (OACC_PARALLEL);
6174 TREE_TYPE (stmt) = void_type_node;
6175 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6176 OACC_PARALLEL_BODY (stmt) = block;
6177
6178 return add_stmt (stmt);
6179}
6180
84dc00e8 6181/* Similarly, except force the retention of the BLOCK. */
1799e5d5
RH
6182
6183tree
6184begin_omp_parallel (void)
6185{
6186 keep_next_level (true);
6187 return begin_omp_structured_block ();
6188}
6189
6190tree
6191finish_omp_parallel (tree clauses, tree body)
6192{
6193 tree stmt;
6194
6195 body = finish_omp_structured_block (body);
b850de4f 6196
1799e5d5
RH
6197 stmt = make_node (OMP_PARALLEL);
6198 TREE_TYPE (stmt) = void_type_node;
6199 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6200 OMP_PARALLEL_BODY (stmt) = body;
54f7877c 6201
1799e5d5
RH
6202 return add_stmt (stmt);
6203}
6204
a68ab351
JJ
6205tree
6206begin_omp_task (void)
6207{
6208 keep_next_level (true);
6209 return begin_omp_structured_block ();
6210}
6211
6212tree
6213finish_omp_task (tree clauses, tree body)
6214{
6215 tree stmt;
6216
6217 body = finish_omp_structured_block (body);
6218
6219 stmt = make_node (OMP_TASK);
6220 TREE_TYPE (stmt) = void_type_node;
6221 OMP_TASK_CLAUSES (stmt) = clauses;
6222 OMP_TASK_BODY (stmt) = body;
6223
6224 return add_stmt (stmt);
6225}
6226
6227/* Helper function for finish_omp_for. Convert Ith random access iterator
6228 into integral iterator. Return FALSE if successful. */
6229
6230static bool
6231handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6232 tree condv, tree incrv, tree *body,
9a771876 6233 tree *pre_body, tree clauses, tree *lastp)
a68ab351
JJ
6234{
6235 tree diff, iter_init, iter_incr = NULL, last;
6236 tree incr_var = NULL, orig_pre_body, orig_body, c;
6237 tree decl = TREE_VEC_ELT (declv, i);
6238 tree init = TREE_VEC_ELT (initv, i);
6239 tree cond = TREE_VEC_ELT (condv, i);
6240 tree incr = TREE_VEC_ELT (incrv, i);
6241 tree iter = decl;
6242 location_t elocus = locus;
6243
6244 if (init && EXPR_HAS_LOCATION (init))
6245 elocus = EXPR_LOCATION (init);
6246
6247 switch (TREE_CODE (cond))
6248 {
6249 case GT_EXPR:
6250 case GE_EXPR:
6251 case LT_EXPR:
6252 case LE_EXPR:
9a771876 6253 case NE_EXPR:
c5cdb03f
JJ
6254 if (TREE_OPERAND (cond, 1) == iter)
6255 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6256 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
a68ab351
JJ
6257 if (TREE_OPERAND (cond, 0) != iter)
6258 cond = error_mark_node;
6259 else
6260 {
f330f599
PC
6261 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6262 TREE_CODE (cond),
4fe977f2 6263 iter, ERROR_MARK,
a68ab351
JJ
6264 TREE_OPERAND (cond, 1), ERROR_MARK,
6265 NULL, tf_warning_or_error);
6266 if (error_operand_p (tem))
6267 return true;
6268 }
6269 break;
6270 default:
6271 cond = error_mark_node;
6272 break;
6273 }
6274 if (cond == error_mark_node)
6275 {
69bc6bff 6276 error_at (elocus, "invalid controlling predicate");
a68ab351
JJ
6277 return true;
6278 }
f330f599 6279 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
a68ab351
JJ
6280 ERROR_MARK, iter, ERROR_MARK, NULL,
6281 tf_warning_or_error);
6282 if (error_operand_p (diff))
6283 return true;
6284 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6285 {
69bc6bff
MLI
6286 error_at (elocus, "difference between %qE and %qD does not have integer type",
6287 TREE_OPERAND (cond, 1), iter);
a68ab351
JJ
6288 return true;
6289 }
6290
6291 switch (TREE_CODE (incr))
6292 {
6293 case PREINCREMENT_EXPR:
6294 case PREDECREMENT_EXPR:
6295 case POSTINCREMENT_EXPR:
6296 case POSTDECREMENT_EXPR:
6297 if (TREE_OPERAND (incr, 0) != iter)
6298 {
6299 incr = error_mark_node;
6300 break;
6301 }
f330f599
PC
6302 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6303 TREE_CODE (incr), iter,
a68ab351
JJ
6304 tf_warning_or_error);
6305 if (error_operand_p (iter_incr))
6306 return true;
6307 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6308 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6309 incr = integer_one_node;
6310 else
6311 incr = integer_minus_one_node;
6312 break;
6313 case MODIFY_EXPR:
6314 if (TREE_OPERAND (incr, 0) != iter)
6315 incr = error_mark_node;
6316 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6317 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6318 {
6319 tree rhs = TREE_OPERAND (incr, 1);
6320 if (TREE_OPERAND (rhs, 0) == iter)
6321 {
6322 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6323 != INTEGER_TYPE)
6324 incr = error_mark_node;
6325 else
6326 {
f330f599
PC
6327 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6328 iter, TREE_CODE (rhs),
a68ab351
JJ
6329 TREE_OPERAND (rhs, 1),
6330 tf_warning_or_error);
6331 if (error_operand_p (iter_incr))
6332 return true;
6333 incr = TREE_OPERAND (rhs, 1);
4b978f96
PC
6334 incr = cp_convert (TREE_TYPE (diff), incr,
6335 tf_warning_or_error);
a68ab351
JJ
6336 if (TREE_CODE (rhs) == MINUS_EXPR)
6337 {
6338 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6339 incr = fold_if_not_in_template (incr);
6340 }
6341 if (TREE_CODE (incr) != INTEGER_CST
6342 && (TREE_CODE (incr) != NOP_EXPR
6343 || (TREE_CODE (TREE_OPERAND (incr, 0))
6344 != INTEGER_CST)))
6345 iter_incr = NULL;
6346 }
6347 }
6348 else if (TREE_OPERAND (rhs, 1) == iter)
6349 {
6350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6351 || TREE_CODE (rhs) != PLUS_EXPR)
6352 incr = error_mark_node;
6353 else
6354 {
f330f599
PC
6355 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6356 PLUS_EXPR,
a68ab351
JJ
6357 TREE_OPERAND (rhs, 0),
6358 ERROR_MARK, iter,
6359 ERROR_MARK, NULL,
6360 tf_warning_or_error);
6361 if (error_operand_p (iter_incr))
6362 return true;
f330f599
PC
6363 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6364 iter, NOP_EXPR,
a68ab351
JJ
6365 iter_incr,
6366 tf_warning_or_error);
6367 if (error_operand_p (iter_incr))
6368 return true;
6369 incr = TREE_OPERAND (rhs, 0);
6370 iter_incr = NULL;
6371 }
6372 }
6373 else
6374 incr = error_mark_node;
6375 }
6376 else
6377 incr = error_mark_node;
6378 break;
6379 default:
6380 incr = error_mark_node;
6381 break;
6382 }
6383
6384 if (incr == error_mark_node)
6385 {
69bc6bff 6386 error_at (elocus, "invalid increment expression");
a68ab351
JJ
6387 return true;
6388 }
6389
4b978f96 6390 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
a68ab351
JJ
6391 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6393 && OMP_CLAUSE_DECL (c) == iter)
6394 break;
6395
6396 decl = create_temporary_var (TREE_TYPE (diff));
6397 pushdecl (decl);
6398 add_decl_expr (decl);
6399 last = create_temporary_var (TREE_TYPE (diff));
6400 pushdecl (last);
6401 add_decl_expr (last);
6402 if (c && iter_incr == NULL)
6403 {
6404 incr_var = create_temporary_var (TREE_TYPE (diff));
6405 pushdecl (incr_var);
6406 add_decl_expr (incr_var);
6407 }
6408 gcc_assert (stmts_are_full_exprs_p ());
6409
6410 orig_pre_body = *pre_body;
6411 *pre_body = push_stmt_list ();
6412 if (orig_pre_body)
6413 add_stmt (orig_pre_body);
6414 if (init != NULL)
f330f599
PC
6415 finish_expr_stmt (build_x_modify_expr (elocus,
6416 iter, NOP_EXPR, init,
a68ab351
JJ
6417 tf_warning_or_error));
6418 init = build_int_cst (TREE_TYPE (diff), 0);
6419 if (c && iter_incr == NULL)
6420 {
f330f599
PC
6421 finish_expr_stmt (build_x_modify_expr (elocus,
6422 incr_var, NOP_EXPR,
a68ab351
JJ
6423 incr, tf_warning_or_error));
6424 incr = incr_var;
f330f599
PC
6425 iter_incr = build_x_modify_expr (elocus,
6426 iter, PLUS_EXPR, incr,
a68ab351
JJ
6427 tf_warning_or_error);
6428 }
f330f599
PC
6429 finish_expr_stmt (build_x_modify_expr (elocus,
6430 last, NOP_EXPR, init,
a68ab351
JJ
6431 tf_warning_or_error));
6432 *pre_body = pop_stmt_list (*pre_body);
6433
ba47d38d
AH
6434 cond = cp_build_binary_op (elocus,
6435 TREE_CODE (cond), decl, diff,
a68ab351 6436 tf_warning_or_error);
32e8bb8e 6437 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
c2255bc4 6438 elocus, incr, NULL_TREE);
a68ab351
JJ
6439
6440 orig_body = *body;
6441 *body = push_stmt_list ();
6442 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
f330f599
PC
6443 iter_init = build_x_modify_expr (elocus,
6444 iter, PLUS_EXPR, iter_init,
a68ab351 6445 tf_warning_or_error);
4e4d2c41
JJ
6446 if (iter_init != error_mark_node)
6447 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
a68ab351 6448 finish_expr_stmt (iter_init);
f330f599
PC
6449 finish_expr_stmt (build_x_modify_expr (elocus,
6450 last, NOP_EXPR, decl,
a68ab351
JJ
6451 tf_warning_or_error));
6452 add_stmt (orig_body);
6453 *body = pop_stmt_list (*body);
6454
6455 if (c)
6456 {
6457 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6458 finish_expr_stmt (iter_incr);
6459 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6460 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6461 }
6462
6463 TREE_VEC_ELT (declv, i) = decl;
6464 TREE_VEC_ELT (initv, i) = init;
6465 TREE_VEC_ELT (condv, i) = cond;
6466 TREE_VEC_ELT (incrv, i) = incr;
9a771876 6467 *lastp = last;
a68ab351
JJ
6468
6469 return false;
6470}
6471
1799e5d5
RH
6472/* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6473 are directly for their associated operands in the statement. DECL
6474 and INIT are a combo; if DECL is NULL then INIT ought to be a
6475 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6476 optional statements that need to go before the loop into its
6477 sk_omp scope. */
6478
6479tree
acf0174b
JJ
6480finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6481 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
a68ab351
JJ
6482{
6483 tree omp_for = NULL, orig_incr = NULL;
9a771876
JJ
6484 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6485 tree last = NULL_TREE;
a68ab351
JJ
6486 location_t elocus;
6487 int i;
6488
6489 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6490 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6491 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6492 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6493 {
6494 decl = TREE_VEC_ELT (declv, i);
6495 init = TREE_VEC_ELT (initv, i);
6496 cond = TREE_VEC_ELT (condv, i);
6497 incr = TREE_VEC_ELT (incrv, i);
6498 elocus = locus;
6499
6500 if (decl == NULL)
6501 {
6502 if (init != NULL)
6503 switch (TREE_CODE (init))
1799e5d5 6504 {
a68ab351 6505 case MODIFY_EXPR:
1799e5d5 6506 decl = TREE_OPERAND (init, 0);
a68ab351
JJ
6507 init = TREE_OPERAND (init, 1);
6508 break;
6509 case MODOP_EXPR:
6510 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6511 {
6512 decl = TREE_OPERAND (init, 0);
6513 init = TREE_OPERAND (init, 2);
6514 }
6515 break;
6516 default:
6517 break;
1799e5d5 6518 }
1799e5d5 6519
a68ab351
JJ
6520 if (decl == NULL)
6521 {
69bc6bff
MLI
6522 error_at (locus,
6523 "expected iteration declaration or initialization");
a68ab351
JJ
6524 return NULL;
6525 }
1799e5d5 6526 }
1799e5d5 6527
a68ab351
JJ
6528 if (init && EXPR_HAS_LOCATION (init))
6529 elocus = EXPR_LOCATION (init);
1799e5d5
RH
6530
6531 if (cond == NULL)
6532 {
69bc6bff 6533 error_at (elocus, "missing controlling predicate");
1799e5d5
RH
6534 return NULL;
6535 }
6536
6537 if (incr == NULL)
6538 {
69bc6bff 6539 error_at (elocus, "missing increment expression");
1799e5d5
RH
6540 return NULL;
6541 }
6542
a68ab351
JJ
6543 TREE_VEC_ELT (declv, i) = decl;
6544 TREE_VEC_ELT (initv, i) = init;
6545 }
6546
6547 if (dependent_omp_for_p (declv, initv, condv, incrv))
6548 {
6549 tree stmt;
6550
acf0174b 6551 stmt = make_node (code);
1799e5d5 6552
a68ab351
JJ
6553 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6554 {
6555 /* This is really just a place-holder. We'll be decomposing this
6556 again and going through the cp_build_modify_expr path below when
6557 we instantiate the thing. */
6558 TREE_VEC_ELT (initv, i)
6559 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6560 TREE_VEC_ELT (initv, i));
6561 }
1799e5d5
RH
6562
6563 TREE_TYPE (stmt) = void_type_node;
a68ab351
JJ
6564 OMP_FOR_INIT (stmt) = initv;
6565 OMP_FOR_COND (stmt) = condv;
6566 OMP_FOR_INCR (stmt) = incrv;
1799e5d5
RH
6567 OMP_FOR_BODY (stmt) = body;
6568 OMP_FOR_PRE_BODY (stmt) = pre_body;
a68ab351 6569 OMP_FOR_CLAUSES (stmt) = clauses;
1799e5d5
RH
6570
6571 SET_EXPR_LOCATION (stmt, locus);
6572 return add_stmt (stmt);
6573 }
6574
a68ab351
JJ
6575 if (processing_template_decl)
6576 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
1799e5d5 6577
a68ab351 6578 for (i = 0; i < TREE_VEC_LENGTH (declv); )
dadb19e0 6579 {
a68ab351
JJ
6580 decl = TREE_VEC_ELT (declv, i);
6581 init = TREE_VEC_ELT (initv, i);
6582 cond = TREE_VEC_ELT (condv, i);
6583 incr = TREE_VEC_ELT (incrv, i);
6584 if (orig_incr)
6585 TREE_VEC_ELT (orig_incr, i) = incr;
6586 elocus = locus;
6587
6588 if (init && EXPR_HAS_LOCATION (init))
dadb19e0 6589 elocus = EXPR_LOCATION (init);
dadb19e0 6590
a68ab351
JJ
6591 if (!DECL_P (decl))
6592 {
69bc6bff 6593 error_at (elocus, "expected iteration declaration or initialization");
a68ab351
JJ
6594 return NULL;
6595 }
969c111d 6596
a68ab351
JJ
6597 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6598 {
6599 if (orig_incr)
6600 TREE_VEC_ELT (orig_incr, i) = incr;
6601 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6602 TREE_CODE (TREE_OPERAND (incr, 1)),
6603 TREE_OPERAND (incr, 2),
6604 tf_warning_or_error);
6605 }
6606
6607 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6608 {
acf0174b
JJ
6609 if (code == OMP_SIMD)
6610 {
6611 error_at (elocus, "%<#pragma omp simd%> used with class "
6612 "iteration variable %qE", decl);
6613 return NULL;
6614 }
9a771876
JJ
6615 if (code == CILK_FOR && i == 0)
6616 orig_decl = decl;
a68ab351 6617 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
9a771876
JJ
6618 incrv, &body, &pre_body,
6619 clauses, &last))
a68ab351
JJ
6620 return NULL;
6621 continue;
6622 }
6623
6624 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
50e10fa8 6625 && !TYPE_PTR_P (TREE_TYPE (decl)))
a68ab351 6626 {
69bc6bff 6627 error_at (elocus, "invalid type for iteration variable %qE", decl);
a68ab351
JJ
6628 return NULL;
6629 }
969c111d 6630
b2ebd268 6631 if (!processing_template_decl)
8569b2d0
JJ
6632 {
6633 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6634 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6635 }
6636 else
6637 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
e4ebaef3
JJ
6638 if (cond
6639 && TREE_SIDE_EFFECTS (cond)
6640 && COMPARISON_CLASS_P (cond)
6641 && !processing_template_decl)
a68ab351 6642 {
e4ebaef3
JJ
6643 tree t = TREE_OPERAND (cond, 0);
6644 if (TREE_SIDE_EFFECTS (t)
6645 && t != decl
6646 && (TREE_CODE (t) != NOP_EXPR
6647 || TREE_OPERAND (t, 0) != decl))
6648 TREE_OPERAND (cond, 0)
6649 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
a68ab351 6650
e4ebaef3
JJ
6651 t = TREE_OPERAND (cond, 1);
6652 if (TREE_SIDE_EFFECTS (t)
6653 && t != decl
6654 && (TREE_CODE (t) != NOP_EXPR
6655 || TREE_OPERAND (t, 0) != decl))
6656 TREE_OPERAND (cond, 1)
a68ab351
JJ
6657 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6658 }
6659 if (decl == error_mark_node || init == error_mark_node)
6660 return NULL;
6661
6662 TREE_VEC_ELT (declv, i) = decl;
6663 TREE_VEC_ELT (initv, i) = init;
6664 TREE_VEC_ELT (condv, i) = cond;
6665 TREE_VEC_ELT (incrv, i) = incr;
6666 i++;
969c111d 6667 }
a68ab351
JJ
6668
6669 if (IS_EMPTY_STMT (pre_body))
6670 pre_body = NULL;
6671
9a771876
JJ
6672 if (code == CILK_FOR && !processing_template_decl)
6673 block = push_stmt_list ();
6674
acf0174b 6675 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
a68ab351
JJ
6676 body, pre_body);
6677
6678 if (omp_for == NULL)
9a771876
JJ
6679 {
6680 if (block)
6681 pop_stmt_list (block);
6682 return NULL;
6683 }
a68ab351
JJ
6684
6685 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
969c111d 6686 {
e4ebaef3
JJ
6687 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6688 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
969c111d 6689
a68ab351
JJ
6690 if (TREE_CODE (incr) != MODIFY_EXPR)
6691 continue;
6692
6693 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
e4ebaef3
JJ
6694 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6695 && !processing_template_decl)
a68ab351 6696 {
e4ebaef3
JJ
6697 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6698 if (TREE_SIDE_EFFECTS (t)
6699 && t != decl
6700 && (TREE_CODE (t) != NOP_EXPR
6701 || TREE_OPERAND (t, 0) != decl))
6702 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6703 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
a68ab351 6704
e4ebaef3
JJ
6705 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6706 if (TREE_SIDE_EFFECTS (t)
6707 && t != decl
6708 && (TREE_CODE (t) != NOP_EXPR
6709 || TREE_OPERAND (t, 0) != decl))
6710 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6711 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
a68ab351
JJ
6712 }
6713
6714 if (orig_incr)
6715 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
969c111d 6716 }
9a771876
JJ
6717 OMP_FOR_CLAUSES (omp_for) = clauses;
6718
6719 if (block)
6720 {
6721 tree omp_par = make_node (OMP_PARALLEL);
6722 TREE_TYPE (omp_par) = void_type_node;
6723 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6724 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6725 TREE_SIDE_EFFECTS (bind) = 1;
6726 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6727 OMP_PARALLEL_BODY (omp_par) = bind;
6728 if (OMP_FOR_PRE_BODY (omp_for))
6729 {
6730 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6731 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6732 }
6733 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6734 decl = TREE_OPERAND (init, 0);
6735 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6736 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6737 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6738 clauses = OMP_FOR_CLAUSES (omp_for);
6739 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6740 for (pc = &clauses; *pc; )
6741 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6742 {
6743 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6744 OMP_FOR_CLAUSES (omp_for) = *pc;
6745 *pc = OMP_CLAUSE_CHAIN (*pc);
6746 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6747 }
6748 else
6749 {
6750 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6751 pc = &OMP_CLAUSE_CHAIN (*pc);
6752 }
6753 if (TREE_CODE (t) != INTEGER_CST)
6754 {
6755 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6756 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6757 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6758 OMP_CLAUSE_CHAIN (c) = clauses;
6759 clauses = c;
6760 }
6761 if (TREE_CODE (incr) == MODIFY_EXPR)
6762 {
6763 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6764 if (TREE_CODE (t) != INTEGER_CST)
6765 {
6766 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6767 = get_temp_regvar (TREE_TYPE (t), t);
6768 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6769 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6770 OMP_CLAUSE_CHAIN (c) = clauses;
6771 clauses = c;
6772 }
6773 }
6774 t = TREE_OPERAND (init, 1);
6775 if (TREE_CODE (t) != INTEGER_CST)
6776 {
6777 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6778 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6779 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6780 OMP_CLAUSE_CHAIN (c) = clauses;
6781 clauses = c;
6782 }
6783 if (orig_decl && orig_decl != decl)
6784 {
6785 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6786 OMP_CLAUSE_DECL (c) = orig_decl;
6787 OMP_CLAUSE_CHAIN (c) = clauses;
6788 clauses = c;
6789 }
6790 if (last)
6791 {
6792 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6793 OMP_CLAUSE_DECL (c) = last;
6794 OMP_CLAUSE_CHAIN (c) = clauses;
6795 clauses = c;
6796 }
6797 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6798 OMP_CLAUSE_DECL (c) = decl;
6799 OMP_CLAUSE_CHAIN (c) = clauses;
6800 clauses = c;
6801 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6802 OMP_CLAUSE_OPERAND (c, 0)
6803 = cilk_for_number_of_iterations (omp_for);
6804 OMP_CLAUSE_CHAIN (c) = clauses;
6805 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6806 add_stmt (omp_par);
6807 return omp_par;
6808 }
6809 else if (code == CILK_FOR && processing_template_decl)
6810 {
6811 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6812 if (orig_decl && orig_decl != decl)
6813 {
6814 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6815 OMP_CLAUSE_DECL (c) = orig_decl;
6816 OMP_CLAUSE_CHAIN (c) = clauses;
6817 clauses = c;
6818 }
6819 if (last)
6820 {
6821 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6822 OMP_CLAUSE_DECL (c) = last;
6823 OMP_CLAUSE_CHAIN (c) = clauses;
6824 clauses = c;
6825 }
6826 OMP_FOR_CLAUSES (omp_for) = clauses;
6827 }
969c111d 6828 return omp_for;
1799e5d5
RH
6829}
6830
6831void
20906c66 6832finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
acf0174b 6833 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
1799e5d5 6834{
239371f9
JJ
6835 tree orig_lhs;
6836 tree orig_rhs;
20906c66
JJ
6837 tree orig_v;
6838 tree orig_lhs1;
6839 tree orig_rhs1;
239371f9 6840 bool dependent_p;
e6bd5565
MM
6841 tree stmt;
6842
239371f9
JJ
6843 orig_lhs = lhs;
6844 orig_rhs = rhs;
20906c66
JJ
6845 orig_v = v;
6846 orig_lhs1 = lhs1;
6847 orig_rhs1 = rhs1;
239371f9
JJ
6848 dependent_p = false;
6849 stmt = NULL_TREE;
6850
6851 /* Even in a template, we can detect invalid uses of the atomic
6852 pragma if neither LHS nor RHS is type-dependent. */
6853 if (processing_template_decl)
e6bd5565 6854 {
239371f9 6855 dependent_p = (type_dependent_expression_p (lhs)
20906c66
JJ
6856 || (rhs && type_dependent_expression_p (rhs))
6857 || (v && type_dependent_expression_p (v))
6858 || (lhs1 && type_dependent_expression_p (lhs1))
6859 || (rhs1 && type_dependent_expression_p (rhs1)));
239371f9 6860 if (!dependent_p)
e6bd5565
MM
6861 {
6862 lhs = build_non_dependent_expr (lhs);
20906c66
JJ
6863 if (rhs)
6864 rhs = build_non_dependent_expr (rhs);
6865 if (v)
6866 v = build_non_dependent_expr (v);
6867 if (lhs1)
6868 lhs1 = build_non_dependent_expr (lhs1);
6869 if (rhs1)
6870 rhs1 = build_non_dependent_expr (rhs1);
e6bd5565 6871 }
239371f9
JJ
6872 }
6873 if (!dependent_p)
6874 {
acf0174b
JJ
6875 bool swapped = false;
6876 if (rhs1 && cp_tree_equal (lhs, rhs))
6877 {
6b4db501 6878 std::swap (rhs, rhs1);
acf0174b
JJ
6879 swapped = !commutative_tree_code (opcode);
6880 }
6881 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6882 {
6883 if (code == OMP_ATOMIC)
6884 error ("%<#pragma omp atomic update%> uses two different "
6885 "expressions for memory");
6886 else
6887 error ("%<#pragma omp atomic capture%> uses two different "
6888 "expressions for memory");
6889 return;
6890 }
6891 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6892 {
6893 if (code == OMP_ATOMIC)
6894 error ("%<#pragma omp atomic update%> uses two different "
6895 "expressions for memory");
6896 else
6897 error ("%<#pragma omp atomic capture%> uses two different "
6898 "expressions for memory");
6899 return;
6900 }
20906c66 6901 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
acf0174b 6902 v, lhs1, rhs1, swapped, seq_cst);
239371f9
JJ
6903 if (stmt == error_mark_node)
6904 return;
e6bd5565 6905 }
239371f9 6906 if (processing_template_decl)
20906c66
JJ
6907 {
6908 if (code == OMP_ATOMIC_READ)
6909 {
f330f599
PC
6910 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6911 OMP_ATOMIC_READ, orig_lhs);
acf0174b 6912 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
20906c66
JJ
6913 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6914 }
6915 else
6916 {
6917 if (opcode == NOP_EXPR)
6918 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6919 else
6920 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6921 if (orig_rhs1)
f330f599
PC
6922 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6923 COMPOUND_EXPR, orig_rhs1, stmt);
20906c66
JJ
6924 if (code != OMP_ATOMIC)
6925 {
f330f599
PC
6926 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6927 code, orig_lhs1, stmt);
acf0174b 6928 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
20906c66
JJ
6929 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6930 }
6931 }
6932 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
acf0174b 6933 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
20906c66 6934 }
e3f505d8 6935 finish_expr_stmt (stmt);
1799e5d5
RH
6936}
6937
6938void
6939finish_omp_barrier (void)
6940{
e79983f4 6941 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
9771b263 6942 vec<tree, va_gc> *vec = make_tree_vector ();
c166b898
ILT
6943 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6944 release_tree_vector (vec);
1799e5d5
RH
6945 finish_expr_stmt (stmt);
6946}
6947
6948void
6949finish_omp_flush (void)
6950{
e79983f4 6951 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
9771b263 6952 vec<tree, va_gc> *vec = make_tree_vector ();
c166b898
ILT
6953 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6954 release_tree_vector (vec);
1799e5d5
RH
6955 finish_expr_stmt (stmt);
6956}
6957
a68ab351
JJ
6958void
6959finish_omp_taskwait (void)
1799e5d5 6960{
e79983f4 6961 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
9771b263 6962 vec<tree, va_gc> *vec = make_tree_vector ();
c166b898
ILT
6963 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6964 release_tree_vector (vec);
a68ab351 6965 finish_expr_stmt (stmt);
1799e5d5 6966}
20906c66
JJ
6967
6968void
6969finish_omp_taskyield (void)
6970{
e79983f4 6971 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
9771b263 6972 vec<tree, va_gc> *vec = make_tree_vector ();
20906c66
JJ
6973 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6974 release_tree_vector (vec);
6975 finish_expr_stmt (stmt);
6976}
acf0174b
JJ
6977
6978void
6979finish_omp_cancel (tree clauses)
6980{
6981 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6982 int mask = 0;
6983 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6984 mask = 1;
6985 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6986 mask = 2;
6987 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6988 mask = 4;
6989 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6990 mask = 8;
6991 else
6992 {
6993 error ("%<#pragma omp cancel must specify one of "
6994 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6995 return;
6996 }
6997 vec<tree, va_gc> *vec = make_tree_vector ();
6998 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6999 if (ifc != NULL_TREE)
7000 {
7001 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
7002 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
7003 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
7004 build_zero_cst (type));
7005 }
7006 else
7007 ifc = boolean_true_node;
7008 vec->quick_push (build_int_cst (integer_type_node, mask));
7009 vec->quick_push (ifc);
7010 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7011 release_tree_vector (vec);
7012 finish_expr_stmt (stmt);
7013}
7014
7015void
7016finish_omp_cancellation_point (tree clauses)
7017{
7018 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7019 int mask = 0;
7020 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7021 mask = 1;
7022 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7023 mask = 2;
7024 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7025 mask = 4;
7026 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7027 mask = 8;
7028 else
7029 {
7030 error ("%<#pragma omp cancellation point must specify one of "
7031 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7032 return;
7033 }
7034 vec<tree, va_gc> *vec
7035 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7036 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7037 release_tree_vector (vec);
7038 finish_expr_stmt (stmt);
7039}
1799e5d5 7040\f
0a35513e
AH
7041/* Begin a __transaction_atomic or __transaction_relaxed statement.
7042 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7043 should create an extra compound stmt. */
7044
7045tree
7046begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7047{
7048 tree r;
7049
7050 if (pcompound)
7051 *pcompound = begin_compound_stmt (0);
7052
7053 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7054
7055 /* Only add the statement to the function if support enabled. */
7056 if (flag_tm)
7057 add_stmt (r);
7058 else
7059 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7060 ? G_("%<__transaction_relaxed%> without "
7061 "transactional memory support enabled")
7062 : G_("%<__transaction_atomic%> without "
7063 "transactional memory support enabled")));
7064
7065 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6f2d959b 7066 TREE_SIDE_EFFECTS (r) = 1;
0a35513e
AH
7067 return r;
7068}
7069
7070/* End a __transaction_atomic or __transaction_relaxed statement.
7071 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
f0f3286a
TR
7072 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7073 a MUST_NOT_THROW_EXPR with NOEX as condition. */
0a35513e
AH
7074
7075void
f0f3286a 7076finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
0a35513e
AH
7077{
7078 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7079 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7080 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7081 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7082
f0f3286a
TR
7083 /* noexcept specifications are not allowed for function transactions. */
7084 gcc_assert (!(noex && compound_stmt));
7085 if (noex)
7086 {
7087 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7088 noex);
550dfe7f
MP
7089 /* This may not be true when the STATEMENT_LIST is empty. */
7090 if (EXPR_P (body))
7091 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
f0f3286a
TR
7092 TREE_SIDE_EFFECTS (body) = 1;
7093 TRANSACTION_EXPR_BODY (stmt) = body;
7094 }
7095
0a35513e
AH
7096 if (compound_stmt)
7097 finish_compound_stmt (compound_stmt);
0a35513e
AH
7098}
7099
f0f3286a
TR
7100/* Build a __transaction_atomic or __transaction_relaxed expression. If
7101 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7102 condition. */
0a35513e
AH
7103
7104tree
f0f3286a 7105build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
0a35513e
AH
7106{
7107 tree ret;
f0f3286a
TR
7108 if (noex)
7109 {
7110 expr = build_must_not_throw_expr (expr, noex);
830ce0a2
MP
7111 if (EXPR_P (expr))
7112 SET_EXPR_LOCATION (expr, loc);
f0f3286a
TR
7113 TREE_SIDE_EFFECTS (expr) = 1;
7114 }
0a35513e
AH
7115 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7116 if (flags & TM_STMT_ATTR_RELAXED)
7117 TRANSACTION_EXPR_RELAXED (ret) = 1;
6f2d959b 7118 TREE_SIDE_EFFECTS (ret) = 1;
0a35513e
AH
7119 SET_EXPR_LOCATION (ret, loc);
7120 return ret;
7121}
7122\f
54f7877c 7123void
3a978d72 7124init_cp_semantics (void)
54f7877c 7125{
54f7877c 7126}
55a3debe
DG
7127\f
7128/* Build a STATIC_ASSERT for a static assertion with the condition
7129 CONDITION and the message text MESSAGE. LOCATION is the location
7130 of the static assertion in the source code. When MEMBER_P, this
7131 static assertion is a member of a class. */
7132void
7133finish_static_assert (tree condition, tree message, location_t location,
7134 bool member_p)
7135{
61b6d4cd
DS
7136 if (message == NULL_TREE
7137 || message == error_mark_node
7138 || condition == NULL_TREE
7139 || condition == error_mark_node)
7140 return;
7141
7b3e2d46
DG
7142 if (check_for_bare_parameter_packs (condition))
7143 condition = error_mark_node;
7144
55a3debe
DG
7145 if (type_dependent_expression_p (condition)
7146 || value_dependent_expression_p (condition))
7147 {
7148 /* We're in a template; build a STATIC_ASSERT and put it in
7149 the right place. */
7150 tree assertion;
7151
7152 assertion = make_node (STATIC_ASSERT);
7153 STATIC_ASSERT_CONDITION (assertion) = condition;
7154 STATIC_ASSERT_MESSAGE (assertion) = message;
7155 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7156
7157 if (member_p)
7158 maybe_add_class_template_decl_list (current_class_type,
7159 assertion,
7160 /*friend_p=*/0);
7161 else
7162 add_stmt (assertion);
7163
7164 return;
7165 }
7166
7167 /* Fold the expression and convert it to a boolean value. */
234bef96 7168 condition = instantiate_non_dependent_expr (condition);
4b978f96 7169 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
fa2200cb 7170 condition = maybe_constant_value (condition);
55a3debe
DG
7171
7172 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7173 /* Do nothing; the condition is satisfied. */
7174 ;
7175 else
7176 {
7177 location_t saved_loc = input_location;
7178
7179 input_location = location;
7180 if (TREE_CODE (condition) == INTEGER_CST
7181 && integer_zerop (condition))
e79fc3d4
ESR
7182 {
7183 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
7184 (TREE_TYPE (TREE_TYPE (message))));
7185 int len = TREE_STRING_LENGTH (message) / sz - 1;
7186 /* Report the error. */
7187 if (len == 0)
7188 error ("static assertion failed");
7189 else
7190 error ("static assertion failed: %s",
7191 TREE_STRING_POINTER (message));
7192 }
55a3debe 7193 else if (condition && condition != error_mark_node)
fa2200cb
JM
7194 {
7195 error ("non-constant condition for static assertion");
9299bde0
PC
7196 if (require_potential_rvalue_constant_expression (condition))
7197 cxx_constant_value (condition);
fa2200cb 7198 }
55a3debe
DG
7199 input_location = saved_loc;
7200 }
7201}
3ad6a8e1
DG
7202\f
7203/* Implements the C++0x decltype keyword. Returns the type of EXPR,
7204 suitable for use as a type-specifier.
7205
7206 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7207 id-expression or a class member access, FALSE when it was parsed as
7208 a full expression. */
a77f94e2 7209
3ad6a8e1 7210tree
5b97c77f
JM
7211finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7212 tsubst_flags_t complain)
3ad6a8e1 7213{
056dd1af 7214 tree type = NULL_TREE;
3ad6a8e1 7215
e4fd5b87
DG
7216 if (!expr || error_operand_p (expr))
7217 return error_mark_node;
7218
7a547b93
JJ
7219 if (TYPE_P (expr)
7220 || TREE_CODE (expr) == TYPE_DECL
7221 || (TREE_CODE (expr) == BIT_NOT_EXPR
7222 && TYPE_P (TREE_OPERAND (expr, 0))))
7223 {
5b97c77f
JM
7224 if (complain & tf_error)
7225 error ("argument to decltype must be an expression");
7a547b93
JJ
7226 return error_mark_node;
7227 }
7228
2c905502
JM
7229 /* Depending on the resolution of DR 1172, we may later need to distinguish
7230 instantiation-dependent but not type-dependent expressions so that, say,
7231 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7232 if (instantiation_dependent_expression_p (expr))
3ad6a8e1 7233 {
9e1e64ec 7234 type = cxx_make_type (DECLTYPE_TYPE);
3ad6a8e1
DG
7235 DECLTYPE_TYPE_EXPR (type) = expr;
7236 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7237 = id_expression_or_member_access_p;
7238 SET_TYPE_STRUCTURAL_EQUALITY (type);
7239
7240 return type;
7241 }
7242
7243 /* The type denoted by decltype(e) is defined as follows: */
7244
ccb05613 7245 expr = resolve_nondeduced_context (expr);
48326487 7246
d3ea4c06 7247 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
6c74ff23
JM
7248 return error_mark_node;
7249
6cdb1428
JM
7250 if (type_unknown_p (expr))
7251 {
7252 if (complain & tf_error)
7253 error ("decltype cannot resolve address of overloaded function");
7254 return error_mark_node;
7255 }
7256
48326487
JM
7257 /* To get the size of a static data member declared as an array of
7258 unknown bound, we need to instantiate it. */
5a6ccc94 7259 if (VAR_P (expr)
48326487
JM
7260 && VAR_HAD_UNKNOWN_BOUND (expr)
7261 && DECL_TEMPLATE_INSTANTIATION (expr))
7262 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7263
3ad6a8e1
DG
7264 if (id_expression_or_member_access_p)
7265 {
7266 /* If e is an id-expression or a class member access (5.2.5
7267 [expr.ref]), decltype(e) is defined as the type of the entity
7268 named by e. If there is no such entity, or e names a set of
7269 overloaded functions, the program is ill-formed. */
9dc6f476 7270 if (identifier_p (expr))
3ad6a8e1
DG
7271 expr = lookup_name (expr);
7272
591cb3cf 7273 if (INDIRECT_REF_P (expr))
3ad6a8e1
DG
7274 /* This can happen when the expression is, e.g., "a.b". Just
7275 look at the underlying operand. */
7276 expr = TREE_OPERAND (expr, 0);
7277
7278 if (TREE_CODE (expr) == OFFSET_REF
cf3c30d3
JM
7279 || TREE_CODE (expr) == MEMBER_REF
7280 || TREE_CODE (expr) == SCOPE_REF)
3ad6a8e1
DG
7281 /* We're only interested in the field itself. If it is a
7282 BASELINK, we will need to see through it in the next
7283 step. */
7284 expr = TREE_OPERAND (expr, 1);
7285
c5ce25ce 7286 if (BASELINK_P (expr))
6cdb1428 7287 /* See through BASELINK nodes to the underlying function. */
3ad6a8e1
DG
7288 expr = BASELINK_FUNCTIONS (expr);
7289
3ad6a8e1
DG
7290 switch (TREE_CODE (expr))
7291 {
7292 case FIELD_DECL:
e76d7cc7 7293 if (DECL_BIT_FIELD_TYPE (expr))
3ad6a8e1
DG
7294 {
7295 type = DECL_BIT_FIELD_TYPE (expr);
7296 break;
7297 }
7298 /* Fall through for fields that aren't bitfields. */
7299
7300 case FUNCTION_DECL:
7301 case VAR_DECL:
7302 case CONST_DECL:
7303 case PARM_DECL:
7304 case RESULT_DECL:
088d4f95 7305 case TEMPLATE_PARM_INDEX:
03a904b5 7306 expr = mark_type_use (expr);
3ad6a8e1
DG
7307 type = TREE_TYPE (expr);
7308 break;
7309
7310 case ERROR_MARK:
7311 type = error_mark_node;
7312 break;
7313
7314 case COMPONENT_REF:
4882d82a 7315 case COMPOUND_EXPR:
03a904b5 7316 mark_type_use (expr);
3ad6a8e1
DG
7317 type = is_bitfield_expr_with_lowered_type (expr);
7318 if (!type)
7319 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7320 break;
7321
7322 case BIT_FIELD_REF:
7323 gcc_unreachable ();
7324
7325 case INTEGER_CST:
8733916b 7326 case PTRMEM_CST:
3ad6a8e1 7327 /* We can get here when the id-expression refers to an
8733916b 7328 enumerator or non-type template parameter. */
3ad6a8e1
DG
7329 type = TREE_TYPE (expr);
7330 break;
7331
7332 default:
57c16a5e
PC
7333 /* Handle instantiated template non-type arguments. */
7334 type = TREE_TYPE (expr);
7335 break;
3ad6a8e1
DG
7336 }
7337 }
7338 else
7339 {
c1e41527
JM
7340 /* Within a lambda-expression:
7341
7342 Every occurrence of decltype((x)) where x is a possibly
7343 parenthesized id-expression that names an entity of
7344 automatic storage duration is treated as if x were
7345 transformed into an access to a corresponding data member
7346 of the closure type that would have been declared if x
7347 were a use of the denoted entity. */
7348 if (outer_automatic_var_p (expr)
7349 && current_function_decl
7350 && LAMBDA_FUNCTION_P (current_function_decl))
7351 type = capture_decltype (expr);
7352 else if (error_operand_p (expr))
7353 type = error_mark_node;
7354 else if (expr == current_class_ptr)
7355 /* If the expression is just "this", we want the
7356 cv-unqualified pointer for the "this" type. */
7357 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7358 else
7359 {
7360 /* Otherwise, where T is the type of e, if e is an lvalue,
7361 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7362 cp_lvalue_kind clk = lvalue_kind (expr);
7363 type = unlowered_expr_type (expr);
7364 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7cea661c
MG
7365
7366 /* For vector types, pick a non-opaque variant. */
b55b02ea 7367 if (VECTOR_TYPE_P (type))
7cea661c
MG
7368 type = strip_typedefs (type);
7369
c1e41527
JM
7370 if (clk != clk_none && !(clk & clk_class))
7371 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7372 }
3ad6a8e1
DG
7373 }
7374
3ad6a8e1
DG
7375 return type;
7376}
cf22909c 7377
b29441ec
PC
7378/* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7379 __has_nothrow_copy, depending on assign_p. */
cb68ec50
PC
7380
7381static bool
b29441ec 7382classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
cb68ec50 7383{
b29441ec 7384 tree fns;
cb68ec50 7385
b29441ec
PC
7386 if (assign_p)
7387 {
7388 int ix;
7389 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7390 if (ix < 0)
cb68ec50 7391 return false;
9771b263 7392 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
b29441ec 7393 }
066ec0a4 7394 else if (TYPE_HAS_COPY_CTOR (type))
b29441ec
PC
7395 {
7396 /* If construction of the copy constructor was postponed, create
7397 it now. */
7398 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7399 lazily_declare_fn (sfk_copy_constructor, type);
d5f4eddd
JM
7400 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7401 lazily_declare_fn (sfk_move_constructor, type);
b29441ec 7402 fns = CLASSTYPE_CONSTRUCTORS (type);
cb68ec50
PC
7403 }
7404 else
7405 return false;
7406
b29441ec 7407 for (; fns; fns = OVL_NEXT (fns))
279086c3
PC
7408 {
7409 tree fn = OVL_CURRENT (fns);
7410
7411 if (assign_p)
7412 {
7413 if (copy_fn_p (fn) == 0)
7414 continue;
7415 }
7416 else if (copy_fn_p (fn) <= 0)
7417 continue;
7418
ba9146c1 7419 maybe_instantiate_noexcept (fn);
279086c3
PC
7420 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7421 return false;
7422 }
b29441ec 7423
cb68ec50
PC
7424 return true;
7425}
7426
7427/* Actually evaluates the trait. */
7428
7429static bool
7430trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7431{
7432 enum tree_code type_code1;
7433 tree t;
7434
7435 type_code1 = TREE_CODE (type1);
7436
7437 switch (kind)
7438 {
7439 case CPTK_HAS_NOTHROW_ASSIGN:
c32097d8 7440 type1 = strip_array_types (type1);
b29441ec
PC
7441 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7442 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7443 || (CLASS_TYPE_P (type1)
7444 && classtype_has_nothrow_assign_or_copy_p (type1,
7445 true))));
cb68ec50
PC
7446
7447 case CPTK_HAS_TRIVIAL_ASSIGN:
c32097d8
JM
7448 /* ??? The standard seems to be missing the "or array of such a class
7449 type" wording for this trait. */
7450 type1 = strip_array_types (type1);
cb68ec50 7451 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
c32097d8 7452 && (trivial_type_p (type1)
cb68ec50 7453 || (CLASS_TYPE_P (type1)
066ec0a4 7454 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
cb68ec50
PC
7455
7456 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7457 type1 = strip_array_types (type1);
7458 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7459 || (CLASS_TYPE_P (type1)
ac177431 7460 && (t = locate_ctor (type1))
ffabb761
JM
7461 && (maybe_instantiate_noexcept (t),
7462 TYPE_NOTHROW_P (TREE_TYPE (t)))));
cb68ec50
PC
7463
7464 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7465 type1 = strip_array_types (type1);
c32097d8 7466 return (trivial_type_p (type1)
cb68ec50
PC
7467 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7468
7469 case CPTK_HAS_NOTHROW_COPY:
c32097d8 7470 type1 = strip_array_types (type1);
cb68ec50
PC
7471 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7472 || (CLASS_TYPE_P (type1)
b29441ec 7473 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
cb68ec50
PC
7474
7475 case CPTK_HAS_TRIVIAL_COPY:
c32097d8
JM
7476 /* ??? The standard seems to be missing the "or array of such a class
7477 type" wording for this trait. */
7478 type1 = strip_array_types (type1);
7479 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
066ec0a4 7480 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
cb68ec50
PC
7481
7482 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7483 type1 = strip_array_types (type1);
c32097d8 7484 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
cb68ec50
PC
7485 || (CLASS_TYPE_P (type1)
7486 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7487
7488 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
46408846 7489 return type_has_virtual_destructor (type1);
cb68ec50
PC
7490
7491 case CPTK_IS_ABSTRACT:
94ccc95d 7492 return (ABSTRACT_CLASS_TYPE_P (type1));
cb68ec50
PC
7493
7494 case CPTK_IS_BASE_OF:
7495 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
1f872df7
PC
7496 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7497 || DERIVED_FROM_P (type1, type2)));
cb68ec50
PC
7498
7499 case CPTK_IS_CLASS:
7500 return (NON_UNION_CLASS_TYPE_P (type1));
7501
cb68ec50
PC
7502 case CPTK_IS_EMPTY:
7503 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7504
7505 case CPTK_IS_ENUM:
7506 return (type_code1 == ENUMERAL_TYPE);
7507
b3908fcc
JW
7508 case CPTK_IS_FINAL:
7509 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7510
3c0d13bf
PC
7511 case CPTK_IS_LITERAL_TYPE:
7512 return (literal_type_p (type1));
7513
cb68ec50
PC
7514 case CPTK_IS_POD:
7515 return (pod_type_p (type1));
7516
7517 case CPTK_IS_POLYMORPHIC:
7518 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7519
971e17ff
AS
7520 case CPTK_IS_SAME_AS:
7521 return same_type_p (type1, type2);
7522
c32097d8
JM
7523 case CPTK_IS_STD_LAYOUT:
7524 return (std_layout_type_p (type1));
7525
7526 case CPTK_IS_TRIVIAL:
7527 return (trivial_type_p (type1));
7528
dd5d5481
JM
7529 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7530 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7531
7532 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7533 return is_trivially_xible (INIT_EXPR, type1, type2);
7534
b752325e
JM
7535 case CPTK_IS_TRIVIALLY_COPYABLE:
7536 return (trivially_copyable_p (type1));
7537
cb68ec50
PC
7538 case CPTK_IS_UNION:
7539 return (type_code1 == UNION_TYPE);
7540
7541 default:
7542 gcc_unreachable ();
7543 return false;
7544 }
7545}
7546
4f75413f 7547/* If TYPE is an array of unknown bound, or (possibly cv-qualified)
dd5d5481 7548 void, or a complete type, returns true, otherwise false. */
ff284b4b 7549
dd5d5481 7550static bool
ff284b4b
PC
7551check_trait_type (tree type)
7552{
dd5d5481
JM
7553 if (type == NULL_TREE)
7554 return true;
7555
7556 if (TREE_CODE (type) == TREE_LIST)
7557 return (check_trait_type (TREE_VALUE (type))
7558 && check_trait_type (TREE_CHAIN (type)));
7559
f94ae987
JM
7560 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7561 && COMPLETE_TYPE_P (TREE_TYPE (type)))
dd5d5481 7562 return true;
ff284b4b
PC
7563
7564 if (VOID_TYPE_P (type))
dd5d5481 7565 return true;
ff284b4b 7566
dd5d5481 7567 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
ff284b4b
PC
7568}
7569
cb68ec50
PC
7570/* Process a trait expression. */
7571
7572tree
7573finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7574{
cb68ec50 7575 if (type1 == error_mark_node
dd5d5481 7576 || type2 == error_mark_node)
cb68ec50
PC
7577 return error_mark_node;
7578
7579 if (processing_template_decl)
7580 {
7581 tree trait_expr = make_node (TRAIT_EXPR);
7582 TREE_TYPE (trait_expr) = boolean_type_node;
7583 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7584 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7585 TRAIT_EXPR_KIND (trait_expr) = kind;
7586 return trait_expr;
7587 }
7588
ff284b4b 7589 switch (kind)
cb68ec50 7590 {
ff284b4b
PC
7591 case CPTK_HAS_NOTHROW_ASSIGN:
7592 case CPTK_HAS_TRIVIAL_ASSIGN:
7593 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7594 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7595 case CPTK_HAS_NOTHROW_COPY:
7596 case CPTK_HAS_TRIVIAL_COPY:
7597 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7598 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7599 case CPTK_IS_ABSTRACT:
7600 case CPTK_IS_EMPTY:
b3908fcc 7601 case CPTK_IS_FINAL:
3c0d13bf 7602 case CPTK_IS_LITERAL_TYPE:
ff284b4b
PC
7603 case CPTK_IS_POD:
7604 case CPTK_IS_POLYMORPHIC:
c32097d8
JM
7605 case CPTK_IS_STD_LAYOUT:
7606 case CPTK_IS_TRIVIAL:
b752325e 7607 case CPTK_IS_TRIVIALLY_COPYABLE:
ff284b4b 7608 if (!check_trait_type (type1))
4f75413f 7609 return error_mark_node;
ff284b4b 7610 break;
dd5d5481
JM
7611
7612 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7613 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7614 if (!check_trait_type (type1)
7615 || !check_trait_type (type2))
7616 return error_mark_node;
7617 break;
ff284b4b
PC
7618
7619 case CPTK_IS_BASE_OF:
7620 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7621 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
4f75413f
PC
7622 && !complete_type_or_else (type2, NULL_TREE))
7623 /* We already issued an error. */
7624 return error_mark_node;
ff284b4b
PC
7625 break;
7626
7627 case CPTK_IS_CLASS:
7628 case CPTK_IS_ENUM:
7629 case CPTK_IS_UNION:
971e17ff 7630 case CPTK_IS_SAME_AS:
ff284b4b 7631 break;
971e17ff 7632
ff284b4b
PC
7633 default:
7634 gcc_unreachable ();
cb68ec50
PC
7635 }
7636
7637 return (trait_expr_value (kind, type1, type2)
7638 ? boolean_true_node : boolean_false_node);
7639}
7640
6ec637a4
JJ
7641/* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7642 which is ignored for C++. */
7643
7644void
7645set_float_const_decimal64 (void)
7646{
7647}
7648
7649void
7650clear_float_const_decimal64 (void)
7651{
7652}
7653
7654bool
7655float_const_decimal64_p (void)
7656{
7657 return 0;
7658}
7659
3b49d762 7660\f
2d76680f 7661/* Return true if T designates the implied `this' parameter. */
7ecbca9d
GDR
7662
7663bool
2d76680f 7664is_this_parameter (tree t)
66e61a34 7665{
2d76680f 7666 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
66e61a34 7667 return false;
2d76680f 7668 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
fbf833b7
PC
7669 return true;
7670}
7671
852497a3 7672/* Insert the deduced return type for an auto function. */
d5f4eddd
JM
7673
7674void
852497a3 7675apply_deduced_return_type (tree fco, tree return_type)
d5f4eddd 7676{
d5f4eddd
JM
7677 tree result;
7678
d5f4eddd
JM
7679 if (return_type == error_mark_node)
7680 return;
7681
852497a3
JM
7682 if (LAMBDA_FUNCTION_P (fco))
7683 {
7684 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7685 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7686 }
7687
7688 if (DECL_CONV_FN_P (fco))
7689 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7690
643d4cd6 7691 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
d5f4eddd
JM
7692
7693 result = DECL_RESULT (fco);
7694 if (result == NULL_TREE)
7695 return;
852497a3
JM
7696 if (TREE_TYPE (result) == return_type)
7697 return;
d5f4eddd
JM
7698
7699 /* We already have a DECL_RESULT from start_preparsed_function.
7700 Now we need to redo the work it and allocate_struct_function
7701 did to reflect the new type. */
9b8662c2 7702 gcc_assert (current_function_decl == fco);
d5f4eddd
JM
7703 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7704 TYPE_MAIN_VARIANT (return_type));
7705 DECL_ARTIFICIAL (result) = 1;
7706 DECL_IGNORED_P (result) = 1;
7707 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7708 result);
7709
7710 DECL_RESULT (fco) = result;
7711
852497a3 7712 if (!processing_template_decl)
d5f4eddd 7713 {
3734964f
DT
7714 if (!VOID_TYPE_P (TREE_TYPE (result)))
7715 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
852497a3 7716 bool aggr = aggregate_value_p (result, fco);
d5f4eddd 7717#ifdef PCC_STATIC_STRUCT_RETURN
852497a3 7718 cfun->returns_pcc_struct = aggr;
d5f4eddd 7719#endif
852497a3 7720 cfun->returns_struct = aggr;
d5f4eddd
JM
7721 }
7722
7723}
7724
7725/* DECL is a local variable or parameter from the surrounding scope of a
7726 lambda-expression. Returns the decltype for a use of the capture field
7727 for DECL even if it hasn't been captured yet. */
7728
7729static tree
7730capture_decltype (tree decl)
7731{
7732 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7733 /* FIXME do lookup instead of list walk? */
7734 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7735 tree type;
7736
7737 if (cap)
7738 type = TREE_TYPE (TREE_PURPOSE (cap));
7739 else
7740 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7741 {
7742 case CPLD_NONE:
7743 error ("%qD is not captured", decl);
7744 return error_mark_node;
7745
7746 case CPLD_COPY:
7747 type = TREE_TYPE (decl);
7748 if (TREE_CODE (type) == REFERENCE_TYPE
7749 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7750 type = TREE_TYPE (type);
7751 break;
7752
7753 case CPLD_REFERENCE:
7754 type = TREE_TYPE (decl);
7755 if (TREE_CODE (type) != REFERENCE_TYPE)
7756 type = build_reference_type (TREE_TYPE (decl));
7757 break;
7758
7759 default:
7760 gcc_unreachable ();
7761 }
7762
7763 if (TREE_CODE (type) != REFERENCE_TYPE)
7764 {
7765 if (!LAMBDA_EXPR_MUTABLE_P (lam))
a3360e77 7766 type = cp_build_qualified_type (type, (cp_type_quals (type)
d5f4eddd
JM
7767 |TYPE_QUAL_CONST));
7768 type = build_reference_type (type);
7769 }
7770 return type;
7771}
7772
378b307d
AS
7773/* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
7774 this is a right unary fold. Otherwise it is a left unary fold. */
7775
7776static tree
7777finish_unary_fold_expr (tree expr, int op, tree_code dir)
7778{
7779 // Build a pack expansion (assuming expr has pack type).
7780 if (!uses_parameter_packs (expr))
7781 {
7782 error_at (location_of (expr), "operand of fold expression has no "
7783 "unexpanded parameter packs");
7784 return error_mark_node;
7785 }
7786 tree pack = make_pack_expansion (expr);
7787
7788 // Build the fold expression.
7789 tree code = build_int_cstu (integer_type_node, abs (op));
7790 tree fold = build_min (dir, unknown_type_node, code, pack);
7791 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
7792 return fold;
7793}
7794
7795tree
7796finish_left_unary_fold_expr (tree expr, int op)
7797{
7798 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
7799}
7800
7801tree
7802finish_right_unary_fold_expr (tree expr, int op)
7803{
7804 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
7805}
7806
7807/* Build a binary fold expression over EXPR1 and EXPR2. The
7808 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
7809 has an unexpanded parameter pack). */
7810
7811tree
7812finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
7813{
7814 pack = make_pack_expansion (pack);
7815 tree code = build_int_cstu (integer_type_node, abs (op));
7816 tree fold = build_min (dir, unknown_type_node, code, pack, init);
7817 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
7818 return fold;
7819}
7820
7821tree
7822finish_binary_fold_expr (tree expr1, tree expr2, int op)
7823{
7824 // Determine which expr has an unexpanded parameter pack and
7825 // set the pack and initial term.
7826 bool pack1 = uses_parameter_packs (expr1);
7827 bool pack2 = uses_parameter_packs (expr2);
7828 if (pack1 && !pack2)
7829 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
7830 else if (pack2 && !pack1)
7831 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
7832 else
7833 {
7834 if (pack1)
7835 error ("both arguments in binary fold have unexpanded parameter packs");
7836 else
7837 error ("no unexpanded parameter packs in binary fold");
7838 }
7839 return error_mark_node;
7840}
7841
cf22909c 7842#include "gt-cp-semantics.h"