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