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