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