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