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