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