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