OSDN Git Service

PR c++/57047
[pf3gnuchains/gcc-fork.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, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7                  2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
8    Written by Mark Mitchell (mmitchell@usa.net) based on code found
9    formerly in parse.y and pt.c.
10
11    This file is part of GCC.
12
13    GCC is free software; you can redistribute it and/or modify it
14    under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 3, or (at your option)
16    any later version.
17
18    GCC is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING3.  If not see
25 <http://www.gnu.org/licenses/>.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "c-family/c-common.h"
34 #include "c-family/c-objc.h"
35 #include "tree-inline.h"
36 #include "intl.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "timevar.h"
41 #include "diagnostic.h"
42 #include "cgraph.h"
43 #include "tree-iterator.h"
44 #include "vec.h"
45 #include "target.h"
46 #include "gimple.h"
47 #include "bitmap.h"
48
49 /* There routines provide a modular interface to perform many parsing
50    operations.  They may therefore be used during actual parsing, or
51    during template instantiation, which may be regarded as a
52    degenerate form of parsing.  */
53
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
57
58
59 /* Deferred Access Checking Overview
60    ---------------------------------
61
62    Most C++ expressions and declarations require access checking
63    to be performed during parsing.  However, in several cases,
64    this has to be treated differently.
65
66    For member declarations, access checking has to be deferred
67    until more information about the declaration is known.  For
68    example:
69
70      class A {
71          typedef int X;
72        public:
73          X f();
74      };
75
76      A::X A::f();
77      A::X g();
78
79    When we are parsing the function return type `A::X', we don't
80    really know if this is allowed until we parse the function name.
81
82    Furthermore, some contexts require that access checking is
83    never performed at all.  These include class heads, and template
84    instantiations.
85
86    Typical use of access checking functions is described here:
87
88    1. When we enter a context that requires certain access checking
89       mode, the function `push_deferring_access_checks' is called with
90       DEFERRING argument specifying the desired mode.  Access checking
91       may be performed immediately (dk_no_deferred), deferred
92       (dk_deferred), or not performed (dk_no_check).
93
94    2. When a declaration such as a type, or a variable, is encountered,
95       the function `perform_or_defer_access_check' is called.  It
96       maintains a VEC of all deferred checks.
97
98    3. The global `current_class_type' or `current_function_decl' is then
99       setup by the parser.  `enforce_access' relies on these information
100       to check access.
101
102    4. Upon exiting the context mentioned in step 1,
103       `perform_deferred_access_checks' is called to check all declaration
104       stored in the VEC. `pop_deferring_access_checks' is then
105       called to restore the previous access checking mode.
106
107       In case of parsing error, we simply call `pop_deferring_access_checks'
108       without `perform_deferred_access_checks'.  */
109
110 typedef struct GTY(()) deferred_access {
111   /* A VEC representing name-lookups for which we have deferred
112      checking access controls.  We cannot check the accessibility of
113      names used in a decl-specifier-seq until we know what is being
114      declared because code like:
115
116        class A {
117          class B {};
118          B* f();
119        }
120
121        A::B* A::f() { return 0; }
122
123      is valid, even though `A::B' is not generally accessible.  */
124   VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks;
125
126   /* The current mode of access checks.  */
127   enum deferring_kind deferring_access_checks_kind;
128
129 } deferred_access;
130 DEF_VEC_O (deferred_access);
131 DEF_VEC_ALLOC_O (deferred_access,gc);
132
133 /* Data for deferred access checking.  */
134 static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
135 static GTY(()) unsigned deferred_access_no_check;
136
137 /* Save the current deferred access states and start deferred
138    access checking iff DEFER_P is true.  */
139
140 void
141 push_deferring_access_checks (deferring_kind deferring)
142 {
143   /* For context like template instantiation, access checking
144      disabling applies to all nested context.  */
145   if (deferred_access_no_check || deferring == dk_no_check)
146     deferred_access_no_check++;
147   else
148     {
149       deferred_access *ptr;
150
151       ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL);
152       ptr->deferred_access_checks = NULL;
153       ptr->deferring_access_checks_kind = deferring;
154     }
155 }
156
157 /* Resume deferring access checks again after we stopped doing
158    this previously.  */
159
160 void
161 resume_deferring_access_checks (void)
162 {
163   if (!deferred_access_no_check)
164     VEC_last (deferred_access, deferred_access_stack)
165       ->deferring_access_checks_kind = dk_deferred;
166 }
167
168 /* Stop deferring access checks.  */
169
170 void
171 stop_deferring_access_checks (void)
172 {
173   if (!deferred_access_no_check)
174     VEC_last (deferred_access, deferred_access_stack)
175       ->deferring_access_checks_kind = dk_no_deferred;
176 }
177
178 /* Discard the current deferred access checks and restore the
179    previous states.  */
180
181 void
182 pop_deferring_access_checks (void)
183 {
184   if (deferred_access_no_check)
185     deferred_access_no_check--;
186   else
187     VEC_pop (deferred_access, deferred_access_stack);
188 }
189
190 /* Returns a TREE_LIST representing the deferred checks.
191    The TREE_PURPOSE of each node is the type through which the
192    access occurred; the TREE_VALUE is the declaration named.
193    */
194
195 VEC (deferred_access_check,gc)*
196 get_deferred_access_checks (void)
197 {
198   if (deferred_access_no_check)
199     return NULL;
200   else
201     return (VEC_last (deferred_access, deferred_access_stack)
202             ->deferred_access_checks);
203 }
204
205 /* Take current deferred checks and combine with the
206    previous states if we also defer checks previously.
207    Otherwise perform checks now.  */
208
209 void
210 pop_to_parent_deferring_access_checks (void)
211 {
212   if (deferred_access_no_check)
213     deferred_access_no_check--;
214   else
215     {
216       VEC (deferred_access_check,gc) *checks;
217       deferred_access *ptr;
218
219       checks = (VEC_last (deferred_access, deferred_access_stack)
220                 ->deferred_access_checks);
221
222       VEC_pop (deferred_access, deferred_access_stack);
223       ptr = VEC_last (deferred_access, deferred_access_stack);
224       if (ptr->deferring_access_checks_kind == dk_no_deferred)
225         {
226           /* Check access.  */
227           perform_access_checks (checks);
228         }
229       else
230         {
231           /* Merge with parent.  */
232           int i, j;
233           deferred_access_check *chk, *probe;
234
235           FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
236             {
237               FOR_EACH_VEC_ELT (deferred_access_check,
238                                 ptr->deferred_access_checks, j, probe)
239                 {
240                   if (probe->binfo == chk->binfo &&
241                       probe->decl == chk->decl &&
242                       probe->diag_decl == chk->diag_decl)
243                     goto found;
244                 }
245               /* Insert into parent's checks.  */
246               VEC_safe_push (deferred_access_check, gc,
247                              ptr->deferred_access_checks, chk);
248             found:;
249             }
250         }
251     }
252 }
253
254 /* Perform the access checks in CHECKS.  The TREE_PURPOSE of each node
255    is the BINFO indicating the qualifying scope used to access the
256    DECL node stored in the TREE_VALUE of the node.  */
257
258 void
259 perform_access_checks (VEC (deferred_access_check,gc)* checks)
260 {
261   int i;
262   deferred_access_check *chk;
263
264   if (!checks)
265     return;
266
267   FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
268     enforce_access (chk->binfo, chk->decl, chk->diag_decl);
269 }
270
271 /* Perform the deferred access checks.
272
273    After performing the checks, we still have to keep the list
274    `deferred_access_stack->deferred_access_checks' since we may want
275    to check access for them again later in a different context.
276    For example:
277
278      class A {
279        typedef int X;
280        static X a;
281      };
282      A::X A::a, x;      // No error for `A::a', error for `x'
283
284    We have to perform deferred access of `A::X', first with `A::a',
285    next with `x'.  */
286
287 void
288 perform_deferred_access_checks (void)
289 {
290   perform_access_checks (get_deferred_access_checks ());
291 }
292
293 /* Defer checking the accessibility of DECL, when looked up in
294    BINFO. DIAG_DECL is the declaration to use to print diagnostics.  */
295
296 void
297 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl)
298 {
299   int i;
300   deferred_access *ptr;
301   deferred_access_check *chk;
302   deferred_access_check *new_access;
303
304
305   /* Exit if we are in a context that no access checking is performed.
306      */
307   if (deferred_access_no_check)
308     return;
309
310   gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
311
312   ptr = VEC_last (deferred_access, deferred_access_stack);
313
314   /* If we are not supposed to defer access checks, just check now.  */
315   if (ptr->deferring_access_checks_kind == dk_no_deferred)
316     {
317       enforce_access (binfo, decl, diag_decl);
318       return;
319     }
320
321   /* See if we are already going to perform this check.  */
322   FOR_EACH_VEC_ELT  (deferred_access_check,
323                      ptr->deferred_access_checks, i, chk)
324     {
325       if (chk->decl == decl && chk->binfo == binfo &&
326           chk->diag_decl == diag_decl)
327         {
328           return;
329         }
330     }
331   /* If not, record the check.  */
332   new_access =
333     VEC_safe_push (deferred_access_check, gc,
334                    ptr->deferred_access_checks, 0);
335   new_access->binfo = binfo;
336   new_access->decl = decl;
337   new_access->diag_decl = diag_decl;
338 }
339
340 /* Used by build_over_call in LOOKUP_SPECULATIVE mode: return whether DECL
341    is accessible in BINFO, and possibly complain if not.  If we're not
342    checking access, everything is accessible.  */
343
344 bool
345 speculative_access_check (tree binfo, tree decl, tree diag_decl,
346                           bool complain)
347 {
348   if (deferred_access_no_check)
349     return true;
350
351   /* If we're checking for implicit delete, we don't want access
352      control errors.  */
353   if (!accessible_p (binfo, decl, true))
354     {
355       /* Unless we're under maybe_explain_implicit_delete.  */
356       if (complain)
357         enforce_access (binfo, decl, diag_decl);
358       return false;
359     }
360
361   return true;
362 }
363
364 /* Returns nonzero if the current statement is a full expression,
365    i.e. temporaries created during that statement should be destroyed
366    at the end of the statement.  */
367
368 int
369 stmts_are_full_exprs_p (void)
370 {
371   return current_stmt_tree ()->stmts_are_full_exprs_p;
372 }
373
374 /* T is a statement.  Add it to the statement-tree.  This is the C++
375    version.  The C/ObjC frontends have a slightly different version of
376    this function.  */
377
378 tree
379 add_stmt (tree t)
380 {
381   enum tree_code code = TREE_CODE (t);
382
383   if (EXPR_P (t) && code != LABEL_EXPR)
384     {
385       if (!EXPR_HAS_LOCATION (t))
386         SET_EXPR_LOCATION (t, input_location);
387
388       /* When we expand a statement-tree, we must know whether or not the
389          statements are full-expressions.  We record that fact here.  */
390       STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
391     }
392
393   /* Add T to the statement-tree.  Non-side-effect statements need to be
394      recorded during statement expressions.  */
395   gcc_checking_assert (!VEC_empty (tree, stmt_list_stack));
396   append_to_statement_list_force (t, &cur_stmt_list);
397
398   return t;
399 }
400
401 /* Returns the stmt_tree to which statements are currently being added.  */
402
403 stmt_tree
404 current_stmt_tree (void)
405 {
406   return (cfun
407           ? &cfun->language->base.x_stmt_tree
408           : &scope_chain->x_stmt_tree);
409 }
410
411 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR.  */
412
413 static tree
414 maybe_cleanup_point_expr (tree expr)
415 {
416   if (!processing_template_decl && stmts_are_full_exprs_p ())
417     expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
418   return expr;
419 }
420
421 /* Like maybe_cleanup_point_expr except have the type of the new expression be
422    void so we don't need to create a temporary variable to hold the inner
423    expression.  The reason why we do this is because the original type might be
424    an aggregate and we cannot create a temporary variable for that type.  */
425
426 tree
427 maybe_cleanup_point_expr_void (tree expr)
428 {
429   if (!processing_template_decl && stmts_are_full_exprs_p ())
430     expr = fold_build_cleanup_point_expr (void_type_node, expr);
431   return expr;
432 }
433
434
435
436 /* Create a declaration statement for the declaration given by the DECL.  */
437
438 void
439 add_decl_expr (tree decl)
440 {
441   tree r = build_stmt (input_location, DECL_EXPR, decl);
442   if (DECL_INITIAL (decl)
443       || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
444     r = maybe_cleanup_point_expr_void (r);
445   add_stmt (r);
446 }
447
448 /* Finish a scope.  */
449
450 tree
451 do_poplevel (tree stmt_list)
452 {
453   tree block = NULL;
454
455   if (stmts_are_full_exprs_p ())
456     block = poplevel (kept_level_p (), 1, 0);
457
458   stmt_list = pop_stmt_list (stmt_list);
459
460   if (!processing_template_decl)
461     {
462       stmt_list = c_build_bind_expr (input_location, block, stmt_list);
463       /* ??? See c_end_compound_stmt re statement expressions.  */
464     }
465
466   return stmt_list;
467 }
468
469 /* Begin a new scope.  */
470
471 static tree
472 do_pushlevel (scope_kind sk)
473 {
474   tree ret = push_stmt_list ();
475   if (stmts_are_full_exprs_p ())
476     begin_scope (sk, NULL);
477   return ret;
478 }
479
480 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
481    when the current scope is exited.  EH_ONLY is true when this is not
482    meant to apply to normal control flow transfer.  */
483
484 void
485 push_cleanup (tree decl, tree cleanup, bool eh_only)
486 {
487   tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
488   CLEANUP_EH_ONLY (stmt) = eh_only;
489   add_stmt (stmt);
490   CLEANUP_BODY (stmt) = push_stmt_list ();
491 }
492
493 /* Begin a conditional that might contain a declaration.  When generating
494    normal code, we want the declaration to appear before the statement
495    containing the conditional.  When generating template code, we want the
496    conditional to be rendered as the raw DECL_EXPR.  */
497
498 static void
499 begin_cond (tree *cond_p)
500 {
501   if (processing_template_decl)
502     *cond_p = push_stmt_list ();
503 }
504
505 /* Finish such a conditional.  */
506
507 static void
508 finish_cond (tree *cond_p, tree expr)
509 {
510   if (processing_template_decl)
511     {
512       tree cond = pop_stmt_list (*cond_p);
513       if (TREE_CODE (cond) == DECL_EXPR)
514         expr = cond;
515
516       if (check_for_bare_parameter_packs (expr))
517         *cond_p = error_mark_node;
518     }
519   *cond_p = expr;
520 }
521
522 /* If *COND_P specifies a conditional with a declaration, transform the
523    loop such that
524             while (A x = 42) { }
525             for (; A x = 42;) { }
526    becomes
527             while (true) { A x = 42; if (!x) break; }
528             for (;;) { A x = 42; if (!x) break; }
529    The statement list for BODY will be empty if the conditional did
530    not declare anything.  */
531
532 static void
533 simplify_loop_decl_cond (tree *cond_p, tree body)
534 {
535   tree cond, if_stmt;
536
537   if (!TREE_SIDE_EFFECTS (body))
538     return;
539
540   cond = *cond_p;
541   *cond_p = boolean_true_node;
542
543   if_stmt = begin_if_stmt ();
544   cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
545   finish_if_stmt_cond (cond, if_stmt);
546   finish_break_stmt ();
547   finish_then_clause (if_stmt);
548   finish_if_stmt (if_stmt);
549 }
550
551 /* Finish a goto-statement.  */
552
553 tree
554 finish_goto_stmt (tree destination)
555 {
556   if (TREE_CODE (destination) == IDENTIFIER_NODE)
557     destination = lookup_label (destination);
558
559   /* We warn about unused labels with -Wunused.  That means we have to
560      mark the used labels as used.  */
561   if (TREE_CODE (destination) == LABEL_DECL)
562     TREE_USED (destination) = 1;
563   else
564     {
565       destination = mark_rvalue_use (destination);
566       if (!processing_template_decl)
567         {
568           destination = cp_convert (ptr_type_node, destination);
569           if (error_operand_p (destination))
570             return NULL_TREE;
571         }
572     }
573
574   check_goto (destination);
575
576   return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
577 }
578
579 /* COND is the condition-expression for an if, while, etc.,
580    statement.  Convert it to a boolean value, if appropriate.
581    In addition, verify sequence points if -Wsequence-point is enabled.  */
582
583 static tree
584 maybe_convert_cond (tree cond)
585 {
586   /* Empty conditions remain empty.  */
587   if (!cond)
588     return NULL_TREE;
589
590   /* Wait until we instantiate templates before doing conversion.  */
591   if (processing_template_decl)
592     return cond;
593
594   if (warn_sequence_point)
595     verify_sequence_points (cond);
596
597   /* Do the conversion.  */
598   cond = convert_from_reference (cond);
599
600   if (TREE_CODE (cond) == MODIFY_EXPR
601       && !TREE_NO_WARNING (cond)
602       && warn_parentheses)
603     {
604       warning (OPT_Wparentheses,
605                "suggest parentheses around assignment used as truth value");
606       TREE_NO_WARNING (cond) = 1;
607     }
608
609   return condition_conversion (cond);
610 }
611
612 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
613
614 tree
615 finish_expr_stmt (tree expr)
616 {
617   tree r = NULL_TREE;
618
619   if (expr != NULL_TREE)
620     {
621       if (!processing_template_decl)
622         {
623           if (warn_sequence_point)
624             verify_sequence_points (expr);
625           expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
626         }
627       else if (!type_dependent_expression_p (expr))
628         convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT, 
629                          tf_warning_or_error);
630
631       if (check_for_bare_parameter_packs (expr))
632         expr = error_mark_node;
633
634       /* Simplification of inner statement expressions, compound exprs,
635          etc can result in us already having an EXPR_STMT.  */
636       if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
637         {
638           if (TREE_CODE (expr) != EXPR_STMT)
639             expr = build_stmt (input_location, EXPR_STMT, expr);
640           expr = maybe_cleanup_point_expr_void (expr);
641         }
642
643       r = add_stmt (expr);
644     }
645
646   finish_stmt ();
647
648   return r;
649 }
650
651
652 /* Begin an if-statement.  Returns a newly created IF_STMT if
653    appropriate.  */
654
655 tree
656 begin_if_stmt (void)
657 {
658   tree r, scope;
659   scope = do_pushlevel (sk_cond);
660   r = build_stmt (input_location, IF_STMT, NULL_TREE,
661                   NULL_TREE, NULL_TREE, scope);
662   begin_cond (&IF_COND (r));
663   return r;
664 }
665
666 /* Process the COND of an if-statement, which may be given by
667    IF_STMT.  */
668
669 void
670 finish_if_stmt_cond (tree cond, tree if_stmt)
671 {
672   finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
673   add_stmt (if_stmt);
674   THEN_CLAUSE (if_stmt) = push_stmt_list ();
675 }
676
677 /* Finish the then-clause of an if-statement, which may be given by
678    IF_STMT.  */
679
680 tree
681 finish_then_clause (tree if_stmt)
682 {
683   THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
684   return if_stmt;
685 }
686
687 /* Begin the else-clause of an if-statement.  */
688
689 void
690 begin_else_clause (tree if_stmt)
691 {
692   ELSE_CLAUSE (if_stmt) = push_stmt_list ();
693 }
694
695 /* Finish the else-clause of an if-statement, which may be given by
696    IF_STMT.  */
697
698 void
699 finish_else_clause (tree if_stmt)
700 {
701   ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
702 }
703
704 /* Finish an if-statement.  */
705
706 void
707 finish_if_stmt (tree if_stmt)
708 {
709   tree scope = IF_SCOPE (if_stmt);
710   IF_SCOPE (if_stmt) = NULL;
711   add_stmt (do_poplevel (scope));
712   finish_stmt ();
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)
734 {
735   finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
736   simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
737 }
738
739 /* Finish a while-statement, which may be given by WHILE_STMT.  */
740
741 void
742 finish_while_stmt (tree while_stmt)
743 {
744   WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
745   finish_stmt ();
746 }
747
748 /* Begin a do-statement.  Returns a newly created DO_STMT if
749    appropriate.  */
750
751 tree
752 begin_do_stmt (void)
753 {
754   tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
755   add_stmt (r);
756   DO_BODY (r) = push_stmt_list ();
757   return r;
758 }
759
760 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
761
762 void
763 finish_do_body (tree do_stmt)
764 {
765   tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
766
767   if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
768     body = STATEMENT_LIST_TAIL (body)->stmt;
769
770   if (IS_EMPTY_STMT (body))
771     warning (OPT_Wempty_body,
772             "suggest explicit braces around empty body in %<do%> statement");
773 }
774
775 /* Finish a do-statement, which may be given by DO_STMT, and whose
776    COND is as indicated.  */
777
778 void
779 finish_do_stmt (tree cond, tree do_stmt)
780 {
781   cond = maybe_convert_cond (cond);
782   DO_COND (do_stmt) = cond;
783   finish_stmt ();
784 }
785
786 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
787    indicated.  */
788
789 tree
790 finish_return_stmt (tree expr)
791 {
792   tree r;
793   bool no_warning;
794
795   expr = check_return_expr (expr, &no_warning);
796
797   if (flag_openmp && !check_omp_return ())
798     return error_mark_node;
799   if (!processing_template_decl)
800     {
801       if (warn_sequence_point)
802         verify_sequence_points (expr);
803       
804       if (DECL_DESTRUCTOR_P (current_function_decl)
805           || (DECL_CONSTRUCTOR_P (current_function_decl)
806               && targetm.cxx.cdtor_returns_this ()))
807         {
808           /* Similarly, all destructors must run destructors for
809              base-classes before returning.  So, all returns in a
810              destructor get sent to the DTOR_LABEL; finish_function emits
811              code to return a value there.  */
812           return finish_goto_stmt (cdtor_label);
813         }
814     }
815
816   r = build_stmt (input_location, RETURN_EXPR, expr);
817   TREE_NO_WARNING (r) |= no_warning;
818   r = maybe_cleanup_point_expr_void (r);
819   r = add_stmt (r);
820   finish_stmt ();
821
822   return r;
823 }
824
825 /* Begin the scope of a for-statement or a range-for-statement.
826    Both the returned trees are to be used in a call to
827    begin_for_stmt or begin_range_for_stmt.  */
828
829 tree
830 begin_for_scope (tree *init)
831 {
832   tree scope = NULL_TREE;
833   if (flag_new_for_scope > 0)
834     scope = do_pushlevel (sk_for);
835
836   if (processing_template_decl)
837     *init = push_stmt_list ();
838   else
839     *init = NULL_TREE;
840
841   return scope;
842 }
843
844 /* Begin a for-statement.  Returns a new FOR_STMT.
845    SCOPE and INIT should be the return of begin_for_scope,
846    or both NULL_TREE  */
847
848 tree
849 begin_for_stmt (tree scope, tree init)
850 {
851   tree r;
852
853   r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
854                   NULL_TREE, NULL_TREE, NULL_TREE);
855
856   if (scope == NULL_TREE)
857     {
858       gcc_assert (!init || !(flag_new_for_scope > 0));
859       if (!init)
860         scope = begin_for_scope (&init);
861     }
862   FOR_INIT_STMT (r) = init;
863   FOR_SCOPE (r) = scope;
864
865   return r;
866 }
867
868 /* Finish the for-init-statement of a for-statement, which may be
869    given by FOR_STMT.  */
870
871 void
872 finish_for_init_stmt (tree for_stmt)
873 {
874   if (processing_template_decl)
875     FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
876   add_stmt (for_stmt);
877   FOR_BODY (for_stmt) = do_pushlevel (sk_block);
878   begin_cond (&FOR_COND (for_stmt));
879 }
880
881 /* Finish the COND of a for-statement, which may be given by
882    FOR_STMT.  */
883
884 void
885 finish_for_cond (tree cond, tree for_stmt)
886 {
887   finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
888   simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
889 }
890
891 /* Finish the increment-EXPRESSION in a for-statement, which may be
892    given by FOR_STMT.  */
893
894 void
895 finish_for_expr (tree expr, tree for_stmt)
896 {
897   if (!expr)
898     return;
899   /* If EXPR is an overloaded function, issue an error; there is no
900      context available to use to perform overload resolution.  */
901   if (type_unknown_p (expr))
902     {
903       cxx_incomplete_type_error (expr, TREE_TYPE (expr));
904       expr = error_mark_node;
905     }
906   if (!processing_template_decl)
907     {
908       if (warn_sequence_point)
909         verify_sequence_points (expr);
910       expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
911                               tf_warning_or_error);
912     }
913   else if (!type_dependent_expression_p (expr))
914     convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
915                      tf_warning_or_error);
916   expr = maybe_cleanup_point_expr_void (expr);
917   if (check_for_bare_parameter_packs (expr))
918     expr = error_mark_node;
919   FOR_EXPR (for_stmt) = expr;
920 }
921
922 /* Finish the body of a for-statement, which may be given by
923    FOR_STMT.  The increment-EXPR for the loop must be
924    provided.
925    It can also finish RANGE_FOR_STMT. */
926
927 void
928 finish_for_stmt (tree for_stmt)
929 {
930   if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
931     RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
932   else
933     FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
934
935   /* Pop the scope for the body of the loop.  */
936   if (flag_new_for_scope > 0)
937     {
938       tree scope;
939       tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
940                          ? &RANGE_FOR_SCOPE (for_stmt)
941                          : &FOR_SCOPE (for_stmt));
942       scope = *scope_ptr;
943       *scope_ptr = NULL;
944       add_stmt (do_poplevel (scope));
945     }
946
947   finish_stmt ();
948 }
949
950 /* Begin a range-for-statement.  Returns a new RANGE_FOR_STMT.
951    SCOPE and INIT should be the return of begin_for_scope,
952    or both NULL_TREE  .
953    To finish it call finish_for_stmt(). */
954
955 tree
956 begin_range_for_stmt (tree scope, tree init)
957 {
958   tree r;
959
960   r = build_stmt (input_location, RANGE_FOR_STMT,
961                   NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
962
963   if (scope == NULL_TREE)
964     {
965       gcc_assert (!init || !(flag_new_for_scope > 0));
966       if (!init)
967         scope = begin_for_scope (&init);
968     }
969
970   /* RANGE_FOR_STMTs do not use nor save the init tree, so we
971      pop it now.  */
972   if (init)
973     pop_stmt_list (init);
974   RANGE_FOR_SCOPE (r) = scope;
975
976   return r;
977 }
978
979 /* Finish the head of a range-based for statement, which may
980    be given by RANGE_FOR_STMT. DECL must be the declaration
981    and EXPR must be the loop expression. */
982
983 void
984 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
985 {
986   RANGE_FOR_DECL (range_for_stmt) = decl;
987   RANGE_FOR_EXPR (range_for_stmt) = expr;
988   add_stmt (range_for_stmt);
989   RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
990 }
991
992 /* Finish a break-statement.  */
993
994 tree
995 finish_break_stmt (void)
996 {
997   /* In switch statements break is sometimes stylistically used after
998      a return statement.  This can lead to spurious warnings about
999      control reaching the end of a non-void function when it is
1000      inlined.  Note that we are calling block_may_fallthru with
1001      language specific tree nodes; this works because
1002      block_may_fallthru returns true when given something it does not
1003      understand.  */
1004   if (!block_may_fallthru (cur_stmt_list))
1005     return void_zero_node;
1006   return add_stmt (build_stmt (input_location, BREAK_STMT));
1007 }
1008
1009 /* Finish a continue-statement.  */
1010
1011 tree
1012 finish_continue_stmt (void)
1013 {
1014   return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1015 }
1016
1017 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
1018    appropriate.  */
1019
1020 tree
1021 begin_switch_stmt (void)
1022 {
1023   tree r, scope;
1024
1025   scope = do_pushlevel (sk_cond);
1026   r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1027
1028   begin_cond (&SWITCH_STMT_COND (r));
1029
1030   return r;
1031 }
1032
1033 /* Finish the cond of a switch-statement.  */
1034
1035 void
1036 finish_switch_cond (tree cond, tree switch_stmt)
1037 {
1038   tree orig_type = NULL;
1039   if (!processing_template_decl)
1040     {
1041       /* Convert the condition to an integer or enumeration type.  */
1042       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1043       if (cond == NULL_TREE)
1044         {
1045           error ("switch quantity not an integer");
1046           cond = error_mark_node;
1047         }
1048       orig_type = TREE_TYPE (cond);
1049       if (cond != error_mark_node)
1050         {
1051           /* [stmt.switch]
1052
1053              Integral promotions are performed.  */
1054           cond = perform_integral_promotions (cond);
1055           cond = maybe_cleanup_point_expr (cond);
1056         }
1057     }
1058   if (check_for_bare_parameter_packs (cond))
1059     cond = error_mark_node;
1060   else if (!processing_template_decl && warn_sequence_point)
1061     verify_sequence_points (cond);
1062
1063   finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1064   SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1065   add_stmt (switch_stmt);
1066   push_switch (switch_stmt);
1067   SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1068 }
1069
1070 /* Finish the body of a switch-statement, which may be given by
1071    SWITCH_STMT.  The COND to switch on is indicated.  */
1072
1073 void
1074 finish_switch_stmt (tree switch_stmt)
1075 {
1076   tree scope;
1077
1078   SWITCH_STMT_BODY (switch_stmt) =
1079     pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1080   pop_switch ();
1081   finish_stmt ();
1082
1083   scope = SWITCH_STMT_SCOPE (switch_stmt);
1084   SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1085   add_stmt (do_poplevel (scope));
1086 }
1087
1088 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
1089    appropriate.  */
1090
1091 tree
1092 begin_try_block (void)
1093 {
1094   tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1095   add_stmt (r);
1096   TRY_STMTS (r) = push_stmt_list ();
1097   return r;
1098 }
1099
1100 /* Likewise, for a function-try-block.  The block returned in
1101    *COMPOUND_STMT is an artificial outer scope, containing the
1102    function-try-block.  */
1103
1104 tree
1105 begin_function_try_block (tree *compound_stmt)
1106 {
1107   tree r;
1108   /* This outer scope does not exist in the C++ standard, but we need
1109      a place to put __FUNCTION__ and similar variables.  */
1110   *compound_stmt = begin_compound_stmt (0);
1111   r = begin_try_block ();
1112   FN_TRY_BLOCK_P (r) = 1;
1113   return r;
1114 }
1115
1116 /* Finish a try-block, which may be given by TRY_BLOCK.  */
1117
1118 void
1119 finish_try_block (tree try_block)
1120 {
1121   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1122   TRY_HANDLERS (try_block) = push_stmt_list ();
1123 }
1124
1125 /* Finish the body of a cleanup try-block, which may be given by
1126    TRY_BLOCK.  */
1127
1128 void
1129 finish_cleanup_try_block (tree try_block)
1130 {
1131   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1132 }
1133
1134 /* Finish an implicitly generated try-block, with a cleanup is given
1135    by CLEANUP.  */
1136
1137 void
1138 finish_cleanup (tree cleanup, tree try_block)
1139 {
1140   TRY_HANDLERS (try_block) = cleanup;
1141   CLEANUP_P (try_block) = 1;
1142 }
1143
1144 /* Likewise, for a function-try-block.  */
1145
1146 void
1147 finish_function_try_block (tree try_block)
1148 {
1149   finish_try_block (try_block);
1150   /* FIXME : something queer about CTOR_INITIALIZER somehow following
1151      the try block, but moving it inside.  */
1152   in_function_try_handler = 1;
1153 }
1154
1155 /* Finish a handler-sequence for a try-block, which may be given by
1156    TRY_BLOCK.  */
1157
1158 void
1159 finish_handler_sequence (tree try_block)
1160 {
1161   TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1162   check_handlers (TRY_HANDLERS (try_block));
1163 }
1164
1165 /* Finish the handler-seq for a function-try-block, given by
1166    TRY_BLOCK.  COMPOUND_STMT is the outer block created by
1167    begin_function_try_block.  */
1168
1169 void
1170 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1171 {
1172   in_function_try_handler = 0;
1173   finish_handler_sequence (try_block);
1174   finish_compound_stmt (compound_stmt);
1175 }
1176
1177 /* Begin a handler.  Returns a HANDLER if appropriate.  */
1178
1179 tree
1180 begin_handler (void)
1181 {
1182   tree r;
1183
1184   r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1185   add_stmt (r);
1186
1187   /* Create a binding level for the eh_info and the exception object
1188      cleanup.  */
1189   HANDLER_BODY (r) = do_pushlevel (sk_catch);
1190
1191   return r;
1192 }
1193
1194 /* Finish the handler-parameters for a handler, which may be given by
1195    HANDLER.  DECL is the declaration for the catch parameter, or NULL
1196    if this is a `catch (...)' clause.  */
1197
1198 void
1199 finish_handler_parms (tree decl, tree handler)
1200 {
1201   tree type = NULL_TREE;
1202   if (processing_template_decl)
1203     {
1204       if (decl)
1205         {
1206           decl = pushdecl (decl);
1207           decl = push_template_decl (decl);
1208           HANDLER_PARMS (handler) = decl;
1209           type = TREE_TYPE (decl);
1210         }
1211     }
1212   else
1213     type = expand_start_catch_block (decl);
1214   HANDLER_TYPE (handler) = type;
1215   if (!processing_template_decl && type)
1216     mark_used (eh_type_info (type));
1217 }
1218
1219 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
1220    the return value from the matching call to finish_handler_parms.  */
1221
1222 void
1223 finish_handler (tree handler)
1224 {
1225   if (!processing_template_decl)
1226     expand_end_catch_block ();
1227   HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1228 }
1229
1230 /* Begin a compound statement.  FLAGS contains some bits that control the
1231    behavior and context.  If BCS_NO_SCOPE is set, the compound statement
1232    does not define a scope.  If BCS_FN_BODY is set, this is the outermost
1233    block of a function.  If BCS_TRY_BLOCK is set, this is the block
1234    created on behalf of a TRY statement.  Returns a token to be passed to
1235    finish_compound_stmt.  */
1236
1237 tree
1238 begin_compound_stmt (unsigned int flags)
1239 {
1240   tree r;
1241
1242   if (flags & BCS_NO_SCOPE)
1243     {
1244       r = push_stmt_list ();
1245       STATEMENT_LIST_NO_SCOPE (r) = 1;
1246
1247       /* Normally, we try hard to keep the BLOCK for a statement-expression.
1248          But, if it's a statement-expression with a scopeless block, there's
1249          nothing to keep, and we don't want to accidentally keep a block
1250          *inside* the scopeless block.  */
1251       keep_next_level (false);
1252     }
1253   else
1254     r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1255
1256   /* When processing a template, we need to remember where the braces were,
1257      so that we can set up identical scopes when instantiating the template
1258      later.  BIND_EXPR is a handy candidate for this.
1259      Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1260      result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1261      processing templates.  */
1262   if (processing_template_decl)
1263     {
1264       r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1265       BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1266       BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1267       TREE_SIDE_EFFECTS (r) = 1;
1268     }
1269
1270   return r;
1271 }
1272
1273 /* Finish a compound-statement, which is given by STMT.  */
1274
1275 void
1276 finish_compound_stmt (tree stmt)
1277 {
1278   if (TREE_CODE (stmt) == BIND_EXPR)
1279     {
1280       tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1281       /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1282          discard the BIND_EXPR so it can be merged with the containing
1283          STATEMENT_LIST.  */
1284       if (TREE_CODE (body) == STATEMENT_LIST
1285           && STATEMENT_LIST_HEAD (body) == NULL
1286           && !BIND_EXPR_BODY_BLOCK (stmt)
1287           && !BIND_EXPR_TRY_BLOCK (stmt))
1288         stmt = body;
1289       else
1290         BIND_EXPR_BODY (stmt) = body;
1291     }
1292   else if (STATEMENT_LIST_NO_SCOPE (stmt))
1293     stmt = pop_stmt_list (stmt);
1294   else
1295     {
1296       /* Destroy any ObjC "super" receivers that may have been
1297          created.  */
1298       objc_clear_super_receiver ();
1299
1300       stmt = do_poplevel (stmt);
1301     }
1302
1303   /* ??? See c_end_compound_stmt wrt statement expressions.  */
1304   add_stmt (stmt);
1305   finish_stmt ();
1306 }
1307
1308 /* Finish an asm-statement, whose components are a STRING, some
1309    OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1310    LABELS.  Also note whether the asm-statement should be
1311    considered volatile.  */
1312
1313 tree
1314 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1315                  tree input_operands, tree clobbers, tree labels)
1316 {
1317   tree r;
1318   tree t;
1319   int ninputs = list_length (input_operands);
1320   int noutputs = list_length (output_operands);
1321
1322   if (!processing_template_decl)
1323     {
1324       const char *constraint;
1325       const char **oconstraints;
1326       bool allows_mem, allows_reg, is_inout;
1327       tree operand;
1328       int i;
1329
1330       oconstraints = XALLOCAVEC (const char *, noutputs);
1331
1332       string = resolve_asm_operand_names (string, output_operands,
1333                                           input_operands, labels);
1334
1335       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1336         {
1337           operand = TREE_VALUE (t);
1338
1339           /* ??? Really, this should not be here.  Users should be using a
1340              proper lvalue, dammit.  But there's a long history of using
1341              casts in the output operands.  In cases like longlong.h, this
1342              becomes a primitive form of typechecking -- if the cast can be
1343              removed, then the output operand had a type of the proper width;
1344              otherwise we'll get an error.  Gross, but ...  */
1345           STRIP_NOPS (operand);
1346
1347           operand = mark_lvalue_use (operand);
1348
1349           if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1350             operand = error_mark_node;
1351
1352           if (operand != error_mark_node
1353               && (TREE_READONLY (operand)
1354                   || CP_TYPE_CONST_P (TREE_TYPE (operand))
1355                   /* Functions are not modifiable, even though they are
1356                      lvalues.  */
1357                   || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1358                   || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1359                   /* If it's an aggregate and any field is const, then it is
1360                      effectively const.  */
1361                   || (CLASS_TYPE_P (TREE_TYPE (operand))
1362                       && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1363             cxx_readonly_error (operand, lv_asm);
1364
1365           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1366           oconstraints[i] = constraint;
1367
1368           if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1369                                        &allows_mem, &allows_reg, &is_inout))
1370             {
1371               /* If the operand is going to end up in memory,
1372                  mark it addressable.  */
1373               if (!allows_reg && !cxx_mark_addressable (operand))
1374                 operand = error_mark_node;
1375             }
1376           else
1377             operand = error_mark_node;
1378
1379           TREE_VALUE (t) = operand;
1380         }
1381
1382       for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1383         {
1384           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1385           operand = decay_conversion (TREE_VALUE (t));
1386
1387           /* If the type of the operand hasn't been determined (e.g.,
1388              because it involves an overloaded function), then issue
1389              an error message.  There's no context available to
1390              resolve the overloading.  */
1391           if (TREE_TYPE (operand) == unknown_type_node)
1392             {
1393               error ("type of asm operand %qE could not be determined",
1394                      TREE_VALUE (t));
1395               operand = error_mark_node;
1396             }
1397
1398           if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1399                                       oconstraints, &allows_mem, &allows_reg))
1400             {
1401               /* If the operand is going to end up in memory,
1402                  mark it addressable.  */
1403               if (!allows_reg && allows_mem)
1404                 {
1405                   /* Strip the nops as we allow this case.  FIXME, this really
1406                      should be rejected or made deprecated.  */
1407                   STRIP_NOPS (operand);
1408                   if (!cxx_mark_addressable (operand))
1409                     operand = error_mark_node;
1410                 }
1411             }
1412           else
1413             operand = error_mark_node;
1414
1415           TREE_VALUE (t) = operand;
1416         }
1417     }
1418
1419   r = build_stmt (input_location, ASM_EXPR, string,
1420                   output_operands, input_operands,
1421                   clobbers, labels);
1422   ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1423   r = maybe_cleanup_point_expr_void (r);
1424   return add_stmt (r);
1425 }
1426
1427 /* Finish a label with the indicated NAME.  Returns the new label.  */
1428
1429 tree
1430 finish_label_stmt (tree name)
1431 {
1432   tree decl = define_label (input_location, name);
1433
1434   if (decl == error_mark_node)
1435     return error_mark_node;
1436
1437   add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1438
1439   return decl;
1440 }
1441
1442 /* Finish a series of declarations for local labels.  G++ allows users
1443    to declare "local" labels, i.e., labels with scope.  This extension
1444    is useful when writing code involving statement-expressions.  */
1445
1446 void
1447 finish_label_decl (tree name)
1448 {
1449   if (!at_function_scope_p ())
1450     {
1451       error ("__label__ declarations are only allowed in function scopes");
1452       return;
1453     }
1454
1455   add_decl_expr (declare_local_label (name));
1456 }
1457
1458 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1459
1460 void
1461 finish_decl_cleanup (tree decl, tree cleanup)
1462 {
1463   push_cleanup (decl, cleanup, false);
1464 }
1465
1466 /* If the current scope exits with an exception, run CLEANUP.  */
1467
1468 void
1469 finish_eh_cleanup (tree cleanup)
1470 {
1471   push_cleanup (NULL, cleanup, true);
1472 }
1473
1474 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1475    order they were written by the user.  Each node is as for
1476    emit_mem_initializers.  */
1477
1478 void
1479 finish_mem_initializers (tree mem_inits)
1480 {
1481   /* Reorder the MEM_INITS so that they are in the order they appeared
1482      in the source program.  */
1483   mem_inits = nreverse (mem_inits);
1484
1485   if (processing_template_decl)
1486     {
1487       tree mem;
1488
1489       for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1490         {
1491           /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1492              check for bare parameter packs in the TREE_VALUE, because
1493              any parameter packs in the TREE_VALUE have already been
1494              bound as part of the TREE_PURPOSE.  See
1495              make_pack_expansion for more information.  */
1496           if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1497               && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1498             TREE_VALUE (mem) = error_mark_node;
1499         }
1500
1501       add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1502     }
1503   else
1504     emit_mem_initializers (mem_inits);
1505 }
1506
1507 /* Finish a parenthesized expression EXPR.  */
1508
1509 tree
1510 finish_parenthesized_expr (tree expr)
1511 {
1512   if (EXPR_P (expr))
1513     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1514     TREE_NO_WARNING (expr) = 1;
1515
1516   if (TREE_CODE (expr) == OFFSET_REF
1517       || TREE_CODE (expr) == SCOPE_REF)
1518     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1519        enclosed in parentheses.  */
1520     PTRMEM_OK_P (expr) = 0;
1521
1522   if (TREE_CODE (expr) == STRING_CST)
1523     PAREN_STRING_LITERAL_P (expr) = 1;
1524
1525   return expr;
1526 }
1527
1528 /* Finish a reference to a non-static data member (DECL) that is not
1529    preceded by `.' or `->'.  */
1530
1531 tree
1532 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1533 {
1534   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1535
1536   if (!object)
1537     {
1538       tree scope = qualifying_scope;
1539       if (scope == NULL_TREE)
1540         scope = context_for_name_lookup (decl);
1541       object = maybe_dummy_object (scope, NULL);
1542     }
1543
1544   if (object == error_mark_node)
1545     return error_mark_node;
1546
1547   /* DR 613: Can use non-static data members without an associated
1548      object in sizeof/decltype/alignof.  */
1549   if (is_dummy_object (object) && cp_unevaluated_operand == 0
1550       && (!processing_template_decl || !current_class_ref))
1551     {
1552       if (current_function_decl
1553           && DECL_STATIC_FUNCTION_P (current_function_decl))
1554         error ("invalid use of member %q+D in static member function", decl);
1555       else
1556         error ("invalid use of non-static data member %q+D", decl);
1557       error ("from this location");
1558
1559       return error_mark_node;
1560     }
1561
1562   if (current_class_ptr)
1563     TREE_USED (current_class_ptr) = 1;
1564   if (processing_template_decl && !qualifying_scope)
1565     {
1566       tree type = TREE_TYPE (decl);
1567
1568       if (TREE_CODE (type) == REFERENCE_TYPE)
1569         /* Quals on the object don't matter.  */;
1570       else
1571         {
1572           /* Set the cv qualifiers.  */
1573           int quals = cp_type_quals (TREE_TYPE (object));
1574
1575           if (DECL_MUTABLE_P (decl))
1576             quals &= ~TYPE_QUAL_CONST;
1577
1578           quals |= cp_type_quals (TREE_TYPE (decl));
1579           type = cp_build_qualified_type (type, quals);
1580         }
1581
1582       return (convert_from_reference
1583               (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1584     }
1585   /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1586      QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
1587      for now.  */
1588   else if (processing_template_decl)
1589     return build_qualified_name (TREE_TYPE (decl),
1590                                  qualifying_scope,
1591                                  DECL_NAME (decl),
1592                                  /*template_p=*/false);
1593   else
1594     {
1595       tree access_type = TREE_TYPE (object);
1596
1597       perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1598                                      decl);
1599
1600       /* If the data member was named `C::M', convert `*this' to `C'
1601          first.  */
1602       if (qualifying_scope)
1603         {
1604           tree binfo = NULL_TREE;
1605           object = build_scoped_ref (object, qualifying_scope,
1606                                      &binfo);
1607         }
1608
1609       return build_class_member_access_expr (object, decl,
1610                                              /*access_path=*/NULL_TREE,
1611                                              /*preserve_reference=*/false,
1612                                              tf_warning_or_error);
1613     }
1614 }
1615
1616 /* If we are currently parsing a template and we encountered a typedef
1617    TYPEDEF_DECL that is being accessed though CONTEXT, this function
1618    adds the typedef to a list tied to the current template.
1619    At tempate instantiatin time, that list is walked and access check
1620    performed for each typedef.
1621    LOCATION is the location of the usage point of TYPEDEF_DECL.  */
1622
1623 void
1624 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1625                                                   tree context,
1626                                                   location_t location)
1627 {
1628     tree template_info = NULL;
1629     tree cs = current_scope ();
1630
1631     if (!is_typedef_decl (typedef_decl)
1632         || !context
1633         || !CLASS_TYPE_P (context)
1634         || !cs)
1635       return;
1636
1637     if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1638       template_info = get_template_info (cs);
1639
1640     if (template_info
1641         && TI_TEMPLATE (template_info)
1642         && !currently_open_class (context))
1643       append_type_to_template_for_access_check (cs, typedef_decl,
1644                                                 context, location);
1645 }
1646
1647 /* DECL was the declaration to which a qualified-id resolved.  Issue
1648    an error message if it is not accessible.  If OBJECT_TYPE is
1649    non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1650    type of `*x', or `x', respectively.  If the DECL was named as
1651    `A::B' then NESTED_NAME_SPECIFIER is `A'.  */
1652
1653 void
1654 check_accessibility_of_qualified_id (tree decl,
1655                                      tree object_type,
1656                                      tree nested_name_specifier)
1657 {
1658   tree scope;
1659   tree qualifying_type = NULL_TREE;
1660
1661   /* If we are parsing a template declaration and if decl is a typedef,
1662      add it to a list tied to the template.
1663      At template instantiation time, that list will be walked and
1664      access check performed.  */
1665   add_typedef_to_current_template_for_access_check (decl,
1666                                                     nested_name_specifier
1667                                                     ? nested_name_specifier
1668                                                     : DECL_CONTEXT (decl),
1669                                                     input_location);
1670
1671   /* If we're not checking, return immediately.  */
1672   if (deferred_access_no_check)
1673     return;
1674
1675   /* Determine the SCOPE of DECL.  */
1676   scope = context_for_name_lookup (decl);
1677   /* If the SCOPE is not a type, then DECL is not a member.  */
1678   if (!TYPE_P (scope))
1679     return;
1680   /* Compute the scope through which DECL is being accessed.  */
1681   if (object_type
1682       /* OBJECT_TYPE might not be a class type; consider:
1683
1684            class A { typedef int I; };
1685            I *p;
1686            p->A::I::~I();
1687
1688          In this case, we will have "A::I" as the DECL, but "I" as the
1689          OBJECT_TYPE.  */
1690       && CLASS_TYPE_P (object_type)
1691       && DERIVED_FROM_P (scope, object_type))
1692     /* If we are processing a `->' or `.' expression, use the type of the
1693        left-hand side.  */
1694     qualifying_type = object_type;
1695   else if (nested_name_specifier)
1696     {
1697       /* If the reference is to a non-static member of the
1698          current class, treat it as if it were referenced through
1699          `this'.  */
1700       if (DECL_NONSTATIC_MEMBER_P (decl)
1701           && current_class_ptr
1702           && DERIVED_FROM_P (scope, current_class_type))
1703         qualifying_type = current_class_type;
1704       /* Otherwise, use the type indicated by the
1705          nested-name-specifier.  */
1706       else
1707         qualifying_type = nested_name_specifier;
1708     }
1709   else
1710     /* Otherwise, the name must be from the current class or one of
1711        its bases.  */
1712     qualifying_type = currently_open_derived_class (scope);
1713
1714   if (qualifying_type 
1715       /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1716          or similar in a default argument value.  */
1717       && CLASS_TYPE_P (qualifying_type)
1718       && !dependent_type_p (qualifying_type))
1719     perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1720                                    decl);
1721 }
1722
1723 /* EXPR is the result of a qualified-id.  The QUALIFYING_CLASS was the
1724    class named to the left of the "::" operator.  DONE is true if this
1725    expression is a complete postfix-expression; it is false if this
1726    expression is followed by '->', '[', '(', etc.  ADDRESS_P is true
1727    iff this expression is the operand of '&'.  TEMPLATE_P is true iff
1728    the qualified-id was of the form "A::template B".  TEMPLATE_ARG_P
1729    is true iff this qualified name appears as a template argument.  */
1730
1731 tree
1732 finish_qualified_id_expr (tree qualifying_class,
1733                           tree expr,
1734                           bool done,
1735                           bool address_p,
1736                           bool template_p,
1737                           bool template_arg_p)
1738 {
1739   gcc_assert (TYPE_P (qualifying_class));
1740
1741   if (error_operand_p (expr))
1742     return error_mark_node;
1743
1744   if (DECL_P (expr) || BASELINK_P (expr))
1745     mark_used (expr);
1746
1747   if (template_p)
1748     check_template_keyword (expr);
1749
1750   /* If EXPR occurs as the operand of '&', use special handling that
1751      permits a pointer-to-member.  */
1752   if (address_p && done)
1753     {
1754       if (TREE_CODE (expr) == SCOPE_REF)
1755         expr = TREE_OPERAND (expr, 1);
1756       expr = build_offset_ref (qualifying_class, expr,
1757                                /*address_p=*/true);
1758       return expr;
1759     }
1760
1761   /* Within the scope of a class, turn references to non-static
1762      members into expression of the form "this->...".  */
1763   if (template_arg_p)
1764     /* But, within a template argument, we do not want make the
1765        transformation, as there is no "this" pointer.  */
1766     ;
1767   else if (TREE_CODE (expr) == FIELD_DECL)
1768     {
1769       push_deferring_access_checks (dk_no_check);
1770       expr = finish_non_static_data_member (expr, NULL_TREE,
1771                                             qualifying_class);
1772       pop_deferring_access_checks ();
1773     }
1774   else if (BASELINK_P (expr) && !processing_template_decl)
1775     {
1776       tree ob;
1777
1778       /* See if any of the functions are non-static members.  */
1779       /* If so, the expression may be relative to 'this'.  */
1780       if (!shared_member_p (expr)
1781           && (ob = maybe_dummy_object (qualifying_class, NULL),
1782               !is_dummy_object (ob)))
1783         expr = (build_class_member_access_expr
1784                 (ob,
1785                  expr,
1786                  BASELINK_ACCESS_BINFO (expr),
1787                  /*preserve_reference=*/false,
1788                  tf_warning_or_error));
1789       else if (done)
1790         /* The expression is a qualified name whose address is not
1791            being taken.  */
1792         expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1793     }
1794
1795   return expr;
1796 }
1797
1798 /* Begin a statement-expression.  The value returned must be passed to
1799    finish_stmt_expr.  */
1800
1801 tree
1802 begin_stmt_expr (void)
1803 {
1804   return push_stmt_list ();
1805 }
1806
1807 /* Process the final expression of a statement expression. EXPR can be
1808    NULL, if the final expression is empty.  Return a STATEMENT_LIST
1809    containing all the statements in the statement-expression, or
1810    ERROR_MARK_NODE if there was an error.  */
1811
1812 tree
1813 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1814 {
1815   if (error_operand_p (expr))
1816     {
1817       /* The type of the statement-expression is the type of the last
1818          expression.  */
1819       TREE_TYPE (stmt_expr) = error_mark_node;
1820       return error_mark_node;
1821     }
1822
1823   /* If the last statement does not have "void" type, then the value
1824      of the last statement is the value of the entire expression.  */
1825   if (expr)
1826     {
1827       tree type = TREE_TYPE (expr);
1828
1829       if (processing_template_decl)
1830         {
1831           expr = build_stmt (input_location, EXPR_STMT, expr);
1832           expr = add_stmt (expr);
1833           /* Mark the last statement so that we can recognize it as such at
1834              template-instantiation time.  */
1835           EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1836         }
1837       else if (VOID_TYPE_P (type))
1838         {
1839           /* Just treat this like an ordinary statement.  */
1840           expr = finish_expr_stmt (expr);
1841         }
1842       else
1843         {
1844           /* It actually has a value we need to deal with.  First, force it
1845              to be an rvalue so that we won't need to build up a copy
1846              constructor call later when we try to assign it to something.  */
1847           expr = force_rvalue (expr, tf_warning_or_error);
1848           if (error_operand_p (expr))
1849             return error_mark_node;
1850
1851           /* Update for array-to-pointer decay.  */
1852           type = TREE_TYPE (expr);
1853
1854           /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1855              normal statement, but don't convert to void or actually add
1856              the EXPR_STMT.  */
1857           if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1858             expr = maybe_cleanup_point_expr (expr);
1859           add_stmt (expr);
1860         }
1861
1862       /* The type of the statement-expression is the type of the last
1863          expression.  */
1864       TREE_TYPE (stmt_expr) = type;
1865     }
1866
1867   return stmt_expr;
1868 }
1869
1870 /* Finish a statement-expression.  EXPR should be the value returned
1871    by the previous begin_stmt_expr.  Returns an expression
1872    representing the statement-expression.  */
1873
1874 tree
1875 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1876 {
1877   tree type;
1878   tree result;
1879
1880   if (error_operand_p (stmt_expr))
1881     {
1882       pop_stmt_list (stmt_expr);
1883       return error_mark_node;
1884     }
1885
1886   gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1887
1888   type = TREE_TYPE (stmt_expr);
1889   result = pop_stmt_list (stmt_expr);
1890   TREE_TYPE (result) = type;
1891
1892   if (processing_template_decl)
1893     {
1894       result = build_min (STMT_EXPR, type, result);
1895       TREE_SIDE_EFFECTS (result) = 1;
1896       STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1897     }
1898   else if (CLASS_TYPE_P (type))
1899     {
1900       /* Wrap the statement-expression in a TARGET_EXPR so that the
1901          temporary object created by the final expression is destroyed at
1902          the end of the full-expression containing the
1903          statement-expression.  */
1904       result = force_target_expr (type, result, tf_warning_or_error);
1905     }
1906
1907   return result;
1908 }
1909
1910 /* Returns the expression which provides the value of STMT_EXPR.  */
1911
1912 tree
1913 stmt_expr_value_expr (tree stmt_expr)
1914 {
1915   tree t = STMT_EXPR_STMT (stmt_expr);
1916
1917   if (TREE_CODE (t) == BIND_EXPR)
1918     t = BIND_EXPR_BODY (t);
1919
1920   if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1921     t = STATEMENT_LIST_TAIL (t)->stmt;
1922
1923   if (TREE_CODE (t) == EXPR_STMT)
1924     t = EXPR_STMT_EXPR (t);
1925
1926   return t;
1927 }
1928
1929 /* Return TRUE iff EXPR_STMT is an empty list of
1930    expression statements.  */
1931
1932 bool
1933 empty_expr_stmt_p (tree expr_stmt)
1934 {
1935   tree body = NULL_TREE;
1936
1937   if (expr_stmt == void_zero_node)
1938     return true;
1939
1940   if (expr_stmt)
1941     {
1942       if (TREE_CODE (expr_stmt) == EXPR_STMT)
1943         body = EXPR_STMT_EXPR (expr_stmt);
1944       else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1945         body = expr_stmt;
1946     }
1947
1948   if (body)
1949     {
1950       if (TREE_CODE (body) == STATEMENT_LIST)
1951         return tsi_end_p (tsi_start (body));
1952       else
1953         return empty_expr_stmt_p (body);
1954     }
1955   return false;
1956 }
1957
1958 /* Perform Koenig lookup.  FN is the postfix-expression representing
1959    the function (or functions) to call; ARGS are the arguments to the
1960    call; if INCLUDE_STD then the `std' namespace is automatically
1961    considered an associated namespace (used in range-based for loops).
1962    Returns the functions to be considered by overload resolution.  */
1963
1964 tree
1965 perform_koenig_lookup (tree fn, VEC(tree,gc) *args, bool include_std,
1966                        tsubst_flags_t complain)
1967 {
1968   tree identifier = NULL_TREE;
1969   tree functions = NULL_TREE;
1970   tree tmpl_args = NULL_TREE;
1971   bool template_id = false;
1972
1973   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1974     {
1975       /* Use a separate flag to handle null args.  */
1976       template_id = true;
1977       tmpl_args = TREE_OPERAND (fn, 1);
1978       fn = TREE_OPERAND (fn, 0);
1979     }
1980
1981   /* Find the name of the overloaded function.  */
1982   if (TREE_CODE (fn) == IDENTIFIER_NODE)
1983     identifier = fn;
1984   else if (is_overloaded_fn (fn))
1985     {
1986       functions = fn;
1987       identifier = DECL_NAME (get_first_fn (functions));
1988     }
1989   else if (DECL_P (fn))
1990     {
1991       functions = fn;
1992       identifier = DECL_NAME (fn);
1993     }
1994
1995   /* A call to a namespace-scope function using an unqualified name.
1996
1997      Do Koenig lookup -- unless any of the arguments are
1998      type-dependent.  */
1999   if (!any_type_dependent_arguments_p (args)
2000       && !any_dependent_template_arguments_p (tmpl_args))
2001     {
2002       fn = lookup_arg_dependent (identifier, functions, args, include_std);
2003       if (!fn)
2004         {
2005           /* The unqualified name could not be resolved.  */
2006           if (complain)
2007             fn = unqualified_fn_lookup_error (identifier);
2008           else
2009             fn = identifier;
2010         }
2011     }
2012
2013   if (fn && template_id)
2014     fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2015   
2016   return fn;
2017 }
2018
2019 /* Generate an expression for `FN (ARGS)'.  This may change the
2020    contents of ARGS.
2021
2022    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2023    as a virtual call, even if FN is virtual.  (This flag is set when
2024    encountering an expression where the function name is explicitly
2025    qualified.  For example a call to `X::f' never generates a virtual
2026    call.)
2027
2028    Returns code for the call.  */
2029
2030 tree
2031 finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
2032                   bool koenig_p, tsubst_flags_t complain)
2033 {
2034   tree result;
2035   tree orig_fn;
2036   VEC(tree,gc) *orig_args = NULL;
2037
2038   if (fn == error_mark_node)
2039     return error_mark_node;
2040
2041   gcc_assert (!TYPE_P (fn));
2042
2043   orig_fn = fn;
2044
2045   if (processing_template_decl)
2046     {
2047       /* If the call expression is dependent, build a CALL_EXPR node
2048          with no type; type_dependent_expression_p recognizes
2049          expressions with no type as being dependent.  */
2050       if (type_dependent_expression_p (fn)
2051           || any_type_dependent_arguments_p (*args)
2052           /* For a non-static member function that doesn't have an
2053              explicit object argument, we need to specifically
2054              test the type dependency of the "this" pointer because it
2055              is not included in *ARGS even though it is considered to
2056              be part of the list of arguments.  Note that this is
2057              related to CWG issues 515 and 1005.  */
2058           || (TREE_CODE (fn) != COMPONENT_REF
2059               && non_static_member_function_p (fn)
2060               && current_class_ref
2061               && type_dependent_expression_p (current_class_ref)))
2062         {
2063           result = build_nt_call_vec (fn, *args);
2064           KOENIG_LOOKUP_P (result) = koenig_p;
2065           if (cfun)
2066             {
2067               do
2068                 {
2069                   tree fndecl = OVL_CURRENT (fn);
2070                   if (TREE_CODE (fndecl) != FUNCTION_DECL
2071                       || !TREE_THIS_VOLATILE (fndecl))
2072                     break;
2073                   fn = OVL_NEXT (fn);
2074                 }
2075               while (fn);
2076               if (!fn)
2077                 current_function_returns_abnormally = 1;
2078             }
2079           return result;
2080         }
2081       orig_args = make_tree_vector_copy (*args);
2082       if (!BASELINK_P (fn)
2083           && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2084           && TREE_TYPE (fn) != unknown_type_node)
2085         fn = build_non_dependent_expr (fn);
2086       make_args_non_dependent (*args);
2087     }
2088
2089   if (TREE_CODE (fn) == COMPONENT_REF)
2090     {
2091       tree member = TREE_OPERAND (fn, 1);
2092       if (BASELINK_P (member))
2093         {
2094           tree object = TREE_OPERAND (fn, 0);
2095           return build_new_method_call (object, member,
2096                                         args, NULL_TREE,
2097                                         (disallow_virtual
2098                                          ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2099                                          : LOOKUP_NORMAL),
2100                                         /*fn_p=*/NULL,
2101                                         complain);
2102         }
2103     }
2104
2105   if (is_overloaded_fn (fn))
2106     fn = baselink_for_fns (fn);
2107
2108   result = NULL_TREE;
2109   if (BASELINK_P (fn))
2110     {
2111       tree object;
2112
2113       /* A call to a member function.  From [over.call.func]:
2114
2115            If the keyword this is in scope and refers to the class of
2116            that member function, or a derived class thereof, then the
2117            function call is transformed into a qualified function call
2118            using (*this) as the postfix-expression to the left of the
2119            . operator.... [Otherwise] a contrived object of type T
2120            becomes the implied object argument.
2121
2122         In this situation:
2123
2124           struct A { void f(); };
2125           struct B : public A {};
2126           struct C : public A { void g() { B::f(); }};
2127
2128         "the class of that member function" refers to `A'.  But 11.2
2129         [class.access.base] says that we need to convert 'this' to B* as
2130         part of the access, so we pass 'B' to maybe_dummy_object.  */
2131
2132       object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2133                                    NULL);
2134
2135       if (processing_template_decl)
2136         {
2137           if (type_dependent_expression_p (object))
2138             {
2139               tree ret = build_nt_call_vec (orig_fn, orig_args);
2140               release_tree_vector (orig_args);
2141               return ret;
2142             }
2143           object = build_non_dependent_expr (object);
2144         }
2145
2146       result = build_new_method_call (object, fn, args, NULL_TREE,
2147                                       (disallow_virtual
2148                                        ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2149                                        : LOOKUP_NORMAL),
2150                                       /*fn_p=*/NULL,
2151                                       complain);
2152     }
2153   else if (is_overloaded_fn (fn))
2154     {
2155       /* If the function is an overloaded builtin, resolve it.  */
2156       if (TREE_CODE (fn) == FUNCTION_DECL
2157           && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2158               || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2159         result = resolve_overloaded_builtin (input_location, fn, *args);
2160
2161       if (!result)
2162         /* A call to a namespace-scope function.  */
2163         result = build_new_function_call (fn, args, koenig_p, complain);
2164     }
2165   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2166     {
2167       if (!VEC_empty (tree, *args))
2168         error ("arguments to destructor are not allowed");
2169       /* Mark the pseudo-destructor call as having side-effects so
2170          that we do not issue warnings about its use.  */
2171       result = build1 (NOP_EXPR,
2172                        void_type_node,
2173                        TREE_OPERAND (fn, 0));
2174       TREE_SIDE_EFFECTS (result) = 1;
2175     }
2176   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2177     /* If the "function" is really an object of class type, it might
2178        have an overloaded `operator ()'.  */
2179     result = build_op_call (fn, args, complain);
2180
2181   if (!result)
2182     /* A call where the function is unknown.  */
2183     result = cp_build_function_call_vec (fn, args, complain);
2184
2185   if (processing_template_decl && result != error_mark_node)
2186     {
2187       if (TREE_CODE (result) == INDIRECT_REF)
2188         result = TREE_OPERAND (result, 0);
2189       result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2190       SET_EXPR_LOCATION (result, input_location);
2191       KOENIG_LOOKUP_P (result) = koenig_p;
2192       release_tree_vector (orig_args);
2193       result = convert_from_reference (result);
2194     }
2195
2196   if (koenig_p)
2197     {
2198       /* Free garbage OVERLOADs from arg-dependent lookup.  */
2199       tree next = NULL_TREE;
2200       for (fn = orig_fn;
2201            fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2202            fn = next)
2203         {
2204           if (processing_template_decl)
2205             /* In a template, we'll re-use them at instantiation time.  */
2206             OVL_ARG_DEPENDENT (fn) = false;
2207           else
2208             {
2209               next = OVL_CHAIN (fn);
2210               ggc_free (fn);
2211             }
2212         }
2213     }
2214
2215   return result;
2216 }
2217
2218 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
2219    is indicated by CODE, which should be POSTINCREMENT_EXPR or
2220    POSTDECREMENT_EXPR.)  */
2221
2222 tree
2223 finish_increment_expr (tree expr, enum tree_code code)
2224 {
2225   return build_x_unary_op (code, expr, tf_warning_or_error);
2226 }
2227
2228 /* Finish a use of `this'.  Returns an expression for `this'.  */
2229
2230 tree
2231 finish_this_expr (void)
2232 {
2233   tree result;
2234
2235   if (current_class_ptr)
2236     {
2237       tree type = TREE_TYPE (current_class_ref);
2238
2239       /* In a lambda expression, 'this' refers to the captured 'this'.  */
2240       if (LAMBDA_TYPE_P (type))
2241         result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2242       else
2243         result = current_class_ptr;
2244
2245     }
2246   else if (current_function_decl
2247            && DECL_STATIC_FUNCTION_P (current_function_decl))
2248     {
2249       error ("%<this%> is unavailable for static member functions");
2250       result = error_mark_node;
2251     }
2252   else
2253     {
2254       if (current_function_decl)
2255         error ("invalid use of %<this%> in non-member function");
2256       else
2257         error ("invalid use of %<this%> at top level");
2258       result = error_mark_node;
2259     }
2260
2261   return result;
2262 }
2263
2264 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
2265    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2266    the TYPE for the type given.  If SCOPE is non-NULL, the expression
2267    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
2268
2269 tree
2270 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2271 {
2272   if (object == error_mark_node || destructor == error_mark_node)
2273     return error_mark_node;
2274
2275   gcc_assert (TYPE_P (destructor));
2276
2277   if (!processing_template_decl)
2278     {
2279       if (scope == error_mark_node)
2280         {
2281           error ("invalid qualifying scope in pseudo-destructor name");
2282           return error_mark_node;
2283         }
2284       if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2285         {
2286           error ("qualified type %qT does not match destructor name ~%qT",
2287                  scope, destructor);
2288           return error_mark_node;
2289         }
2290
2291
2292       /* [expr.pseudo] says both:
2293
2294            The type designated by the pseudo-destructor-name shall be
2295            the same as the object type.
2296
2297          and:
2298
2299            The cv-unqualified versions of the object type and of the
2300            type designated by the pseudo-destructor-name shall be the
2301            same type.
2302
2303          We implement the more generous second sentence, since that is
2304          what most other compilers do.  */
2305       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2306                                                       destructor))
2307         {
2308           error ("%qE is not of type %qT", object, destructor);
2309           return error_mark_node;
2310         }
2311     }
2312
2313   return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2314 }
2315
2316 /* Finish an expression of the form CODE EXPR.  */
2317
2318 tree
2319 finish_unary_op_expr (enum tree_code code, tree expr)
2320 {
2321   tree result = build_x_unary_op (code, expr, tf_warning_or_error);
2322   if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2323     overflow_warning (input_location, result);
2324
2325   return result;
2326 }
2327
2328 /* Finish a compound-literal expression.  TYPE is the type to which
2329    the CONSTRUCTOR in COMPOUND_LITERAL is being cast.  */
2330
2331 tree
2332 finish_compound_literal (tree type, tree compound_literal,
2333                          tsubst_flags_t complain)
2334 {
2335   if (type == error_mark_node)
2336     return error_mark_node;
2337
2338   if (TREE_CODE (type) == REFERENCE_TYPE)
2339     {
2340       compound_literal
2341         = finish_compound_literal (TREE_TYPE (type), compound_literal,
2342                                    complain);
2343       return cp_build_c_cast (type, compound_literal, complain);
2344     }
2345
2346   if (!TYPE_OBJ_P (type))
2347     {
2348       if (complain & tf_error)
2349         error ("compound literal of non-object type %qT", type);
2350       return error_mark_node;
2351     }
2352
2353   if (processing_template_decl)
2354     {
2355       TREE_TYPE (compound_literal) = type;
2356       /* Mark the expression as a compound literal.  */
2357       TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2358       return compound_literal;
2359     }
2360
2361   type = complete_type (type);
2362
2363   if (TYPE_NON_AGGREGATE_CLASS (type))
2364     {
2365       /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2366          everywhere that deals with function arguments would be a pain, so
2367          just wrap it in a TREE_LIST.  The parser set a flag so we know
2368          that it came from T{} rather than T({}).  */
2369       CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2370       compound_literal = build_tree_list (NULL_TREE, compound_literal);
2371       return build_functional_cast (type, compound_literal, complain);
2372     }
2373
2374   if (TREE_CODE (type) == ARRAY_TYPE
2375       && check_array_initializer (NULL_TREE, type, compound_literal))
2376     return error_mark_node;
2377   compound_literal = reshape_init (type, compound_literal, complain);
2378   if (SCALAR_TYPE_P (type)
2379       && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2380       && (complain & tf_warning_or_error))
2381     check_narrowing (type, compound_literal);
2382   if (TREE_CODE (type) == ARRAY_TYPE
2383       && TYPE_DOMAIN (type) == NULL_TREE)
2384     {
2385       cp_complete_array_type_or_error (&type, compound_literal,
2386                                        false, complain);
2387       if (type == error_mark_node)
2388         return error_mark_node;
2389     }
2390   compound_literal = digest_init (type, compound_literal, complain);
2391   if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2392     TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2393   /* Put static/constant array temporaries in static variables, but always
2394      represent class temporaries with TARGET_EXPR so we elide copies.  */
2395   if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2396       && TREE_CODE (type) == ARRAY_TYPE
2397       && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2398       && initializer_constant_valid_p (compound_literal, type))
2399     {
2400       tree decl = create_temporary_var (type);
2401       DECL_INITIAL (decl) = compound_literal;
2402       TREE_STATIC (decl) = 1;
2403       if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2404         {
2405           /* 5.19 says that a constant expression can include an
2406              lvalue-rvalue conversion applied to "a glvalue of literal type
2407              that refers to a non-volatile temporary object initialized
2408              with a constant expression".  Rather than try to communicate
2409              that this VAR_DECL is a temporary, just mark it constexpr.  */
2410           DECL_DECLARED_CONSTEXPR_P (decl) = true;
2411           DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2412           TREE_CONSTANT (decl) = true;
2413         }
2414       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2415       decl = pushdecl_top_level (decl);
2416       DECL_NAME (decl) = make_anon_name ();
2417       SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2418       return decl;
2419     }
2420   else
2421     return get_target_expr_sfinae (compound_literal, complain);
2422 }
2423
2424 /* Return the declaration for the function-name variable indicated by
2425    ID.  */
2426
2427 tree
2428 finish_fname (tree id)
2429 {
2430   tree decl;
2431
2432   decl = fname_decl (input_location, C_RID_CODE (id), id);
2433   if (processing_template_decl && current_function_decl)
2434     decl = DECL_NAME (decl);
2435   return decl;
2436 }
2437
2438 /* Finish a translation unit.  */
2439
2440 void
2441 finish_translation_unit (void)
2442 {
2443   /* In case there were missing closebraces,
2444      get us back to the global binding level.  */
2445   pop_everything ();
2446   while (current_namespace != global_namespace)
2447     pop_namespace ();
2448
2449   /* Do file scope __FUNCTION__ et al.  */
2450   finish_fname_decls ();
2451 }
2452
2453 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2454    Returns the parameter.  */
2455
2456 tree
2457 finish_template_type_parm (tree aggr, tree identifier)
2458 {
2459   if (aggr != class_type_node)
2460     {
2461       permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2462       aggr = class_type_node;
2463     }
2464
2465   return build_tree_list (aggr, identifier);
2466 }
2467
2468 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2469    Returns the parameter.  */
2470
2471 tree
2472 finish_template_template_parm (tree aggr, tree identifier)
2473 {
2474   tree decl = build_decl (input_location,
2475                           TYPE_DECL, identifier, NULL_TREE);
2476   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2477   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2478   DECL_TEMPLATE_RESULT (tmpl) = decl;
2479   DECL_ARTIFICIAL (decl) = 1;
2480   end_template_decl ();
2481
2482   gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2483
2484   check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl), 
2485                            /*is_primary=*/true, /*is_partial=*/false,
2486                            /*is_friend=*/0);
2487
2488   return finish_template_type_parm (aggr, tmpl);
2489 }
2490
2491 /* ARGUMENT is the default-argument value for a template template
2492    parameter.  If ARGUMENT is invalid, issue error messages and return
2493    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
2494
2495 tree
2496 check_template_template_default_arg (tree argument)
2497 {
2498   if (TREE_CODE (argument) != TEMPLATE_DECL
2499       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2500       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2501     {
2502       if (TREE_CODE (argument) == TYPE_DECL)
2503         error ("invalid use of type %qT as a default value for a template "
2504                "template-parameter", TREE_TYPE (argument));
2505       else
2506         error ("invalid default argument for a template template parameter");
2507       return error_mark_node;
2508     }
2509
2510   return argument;
2511 }
2512
2513 /* Begin a class definition, as indicated by T.  */
2514
2515 tree
2516 begin_class_definition (tree t)
2517 {
2518   if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2519     return error_mark_node;
2520
2521   if (processing_template_parmlist)
2522     {
2523       error ("definition of %q#T inside template parameter list", t);
2524       return error_mark_node;
2525     }
2526
2527   /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2528      are passed the same as decimal scalar types.  */
2529   if (TREE_CODE (t) == RECORD_TYPE
2530       && !processing_template_decl)
2531     {
2532       tree ns = TYPE_CONTEXT (t);
2533       if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2534           && DECL_CONTEXT (ns) == std_node
2535           && DECL_NAME (ns)
2536           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2537         {
2538           const char *n = TYPE_NAME_STRING (t);
2539           if ((strcmp (n, "decimal32") == 0)
2540               || (strcmp (n, "decimal64") == 0)
2541               || (strcmp (n, "decimal128") == 0))
2542             TYPE_TRANSPARENT_AGGR (t) = 1;
2543         }
2544     }
2545
2546   /* A non-implicit typename comes from code like:
2547
2548        template <typename T> struct A {
2549          template <typename U> struct A<T>::B ...
2550
2551      This is erroneous.  */
2552   else if (TREE_CODE (t) == TYPENAME_TYPE)
2553     {
2554       error ("invalid definition of qualified type %qT", t);
2555       t = error_mark_node;
2556     }
2557
2558   if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2559     {
2560       t = make_class_type (RECORD_TYPE);
2561       pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2562     }
2563
2564   if (TYPE_BEING_DEFINED (t))
2565     {
2566       t = make_class_type (TREE_CODE (t));
2567       pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2568     }
2569   maybe_process_partial_specialization (t);
2570   pushclass (t);
2571   TYPE_BEING_DEFINED (t) = 1;
2572
2573   if (flag_pack_struct)
2574     {
2575       tree v;
2576       TYPE_PACKED (t) = 1;
2577       /* Even though the type is being defined for the first time
2578          here, there might have been a forward declaration, so there
2579          might be cv-qualified variants of T.  */
2580       for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2581         TYPE_PACKED (v) = 1;
2582     }
2583   /* Reset the interface data, at the earliest possible
2584      moment, as it might have been set via a class foo;
2585      before.  */
2586   if (! TYPE_ANONYMOUS_P (t))
2587     {
2588       struct c_fileinfo *finfo = get_fileinfo (input_filename);
2589       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2590       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2591         (t, finfo->interface_unknown);
2592     }
2593   reset_specialization();
2594
2595   /* Make a declaration for this class in its own scope.  */
2596   build_self_reference ();
2597
2598   return t;
2599 }
2600
2601 /* Finish the member declaration given by DECL.  */
2602
2603 void
2604 finish_member_declaration (tree decl)
2605 {
2606   if (decl == error_mark_node || decl == NULL_TREE)
2607     return;
2608
2609   if (decl == void_type_node)
2610     /* The COMPONENT was a friend, not a member, and so there's
2611        nothing for us to do.  */
2612     return;
2613
2614   /* We should see only one DECL at a time.  */
2615   gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2616
2617   /* Set up access control for DECL.  */
2618   TREE_PRIVATE (decl)
2619     = (current_access_specifier == access_private_node);
2620   TREE_PROTECTED (decl)
2621     = (current_access_specifier == access_protected_node);
2622   if (TREE_CODE (decl) == TEMPLATE_DECL)
2623     {
2624       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2625       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2626     }
2627
2628   /* Mark the DECL as a member of the current class.  */
2629   DECL_CONTEXT (decl) = current_class_type;
2630
2631   /* Check for bare parameter packs in the member variable declaration.  */
2632   if (TREE_CODE (decl) == FIELD_DECL)
2633     {
2634       if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2635         TREE_TYPE (decl) = error_mark_node;
2636       if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2637         DECL_ATTRIBUTES (decl) = NULL_TREE;
2638     }
2639
2640   /* [dcl.link]
2641
2642      A C language linkage is ignored for the names of class members
2643      and the member function type of class member functions.  */
2644   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2645     SET_DECL_LANGUAGE (decl, lang_cplusplus);
2646
2647   /* Put functions on the TYPE_METHODS list and everything else on the
2648      TYPE_FIELDS list.  Note that these are built up in reverse order.
2649      We reverse them (to obtain declaration order) in finish_struct.  */
2650   if (TREE_CODE (decl) == FUNCTION_DECL
2651       || DECL_FUNCTION_TEMPLATE_P (decl))
2652     {
2653       /* We also need to add this function to the
2654          CLASSTYPE_METHOD_VEC.  */
2655       if (add_method (current_class_type, decl, NULL_TREE))
2656         {
2657           DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2658           TYPE_METHODS (current_class_type) = decl;
2659
2660           maybe_add_class_template_decl_list (current_class_type, decl,
2661                                               /*friend_p=*/0);
2662         }
2663     }
2664   /* Enter the DECL into the scope of the class.  */
2665   else if (pushdecl_class_level (decl))
2666     {
2667       if (TREE_CODE (decl) == USING_DECL)
2668         {
2669           /* For now, ignore class-scope USING_DECLS, so that
2670              debugging backends do not see them. */
2671           DECL_IGNORED_P (decl) = 1;
2672         }
2673
2674       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2675          go at the beginning.  The reason is that lookup_field_1
2676          searches the list in order, and we want a field name to
2677          override a type name so that the "struct stat hack" will
2678          work.  In particular:
2679
2680            struct S { enum E { }; int E } s;
2681            s.E = 3;
2682
2683          is valid.  In addition, the FIELD_DECLs must be maintained in
2684          declaration order so that class layout works as expected.
2685          However, we don't need that order until class layout, so we
2686          save a little time by putting FIELD_DECLs on in reverse order
2687          here, and then reversing them in finish_struct_1.  (We could
2688          also keep a pointer to the correct insertion points in the
2689          list.)  */
2690
2691       if (TREE_CODE (decl) == TYPE_DECL)
2692         TYPE_FIELDS (current_class_type)
2693           = chainon (TYPE_FIELDS (current_class_type), decl);
2694       else
2695         {
2696           DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2697           TYPE_FIELDS (current_class_type) = decl;
2698         }
2699
2700       maybe_add_class_template_decl_list (current_class_type, decl,
2701                                           /*friend_p=*/0);
2702     }
2703
2704   if (pch_file)
2705     note_decl_for_pch (decl);
2706 }
2707
2708 /* DECL has been declared while we are building a PCH file.  Perform
2709    actions that we might normally undertake lazily, but which can be
2710    performed now so that they do not have to be performed in
2711    translation units which include the PCH file.  */
2712
2713 void
2714 note_decl_for_pch (tree decl)
2715 {
2716   gcc_assert (pch_file);
2717
2718   /* There's a good chance that we'll have to mangle names at some
2719      point, even if only for emission in debugging information.  */
2720   if ((TREE_CODE (decl) == VAR_DECL
2721        || TREE_CODE (decl) == FUNCTION_DECL)
2722       && !processing_template_decl)
2723     mangle_decl (decl);
2724 }
2725
2726 /* Finish processing a complete template declaration.  The PARMS are
2727    the template parameters.  */
2728
2729 void
2730 finish_template_decl (tree parms)
2731 {
2732   if (parms)
2733     end_template_decl ();
2734   else
2735     end_specialization ();
2736 }
2737
2738 /* Finish processing a template-id (which names a type) of the form
2739    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2740    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
2741    the scope of template-id indicated.  */
2742
2743 tree
2744 finish_template_type (tree name, tree args, int entering_scope)
2745 {
2746   tree type;
2747
2748   type = lookup_template_class (name, args,
2749                                 NULL_TREE, NULL_TREE, entering_scope,
2750                                 tf_warning_or_error | tf_user);
2751   if (type == error_mark_node)
2752     return type;
2753   else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2754     return TYPE_STUB_DECL (type);
2755   else
2756     return TYPE_NAME (type);
2757 }
2758
2759 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2760    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2761    BASE_CLASS, or NULL_TREE if an error occurred.  The
2762    ACCESS_SPECIFIER is one of
2763    access_{default,public,protected_private}_node.  For a virtual base
2764    we set TREE_TYPE.  */
2765
2766 tree
2767 finish_base_specifier (tree base, tree access, bool virtual_p)
2768 {
2769   tree result;
2770
2771   if (base == error_mark_node)
2772     {
2773       error ("invalid base-class specification");
2774       result = NULL_TREE;
2775     }
2776   else if (! MAYBE_CLASS_TYPE_P (base))
2777     {
2778       error ("%qT is not a class type", base);
2779       result = NULL_TREE;
2780     }
2781   else
2782     {
2783       if (cp_type_quals (base) != 0)
2784         {
2785           /* DR 484: Can a base-specifier name a cv-qualified
2786              class type?  */
2787           base = TYPE_MAIN_VARIANT (base);
2788         }
2789       result = build_tree_list (access, base);
2790       if (virtual_p)
2791         TREE_TYPE (result) = integer_type_node;
2792     }
2793
2794   return result;
2795 }
2796
2797 /* If FNS is a member function, a set of member functions, or a
2798    template-id referring to one or more member functions, return a
2799    BASELINK for FNS, incorporating the current access context.
2800    Otherwise, return FNS unchanged.  */
2801
2802 tree
2803 baselink_for_fns (tree fns)
2804 {
2805   tree scope;
2806   tree cl;
2807
2808   if (BASELINK_P (fns) 
2809       || error_operand_p (fns))
2810     return fns;
2811
2812   scope = ovl_scope (fns);
2813   if (!CLASS_TYPE_P (scope))
2814     return fns;
2815
2816   cl = currently_open_derived_class (scope);
2817   if (!cl)
2818     cl = scope;
2819   cl = TYPE_BINFO (cl);
2820   return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2821 }
2822
2823 /* Returns true iff DECL is a variable from a function outside
2824    the current one.  */
2825
2826 static bool
2827 outer_var_p (tree decl)
2828 {
2829   return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2830           && DECL_FUNCTION_SCOPE_P (decl)
2831           && DECL_CONTEXT (decl) != current_function_decl);
2832 }
2833
2834 /* As above, but also checks that DECL is automatic.  */
2835
2836 static bool
2837 outer_automatic_var_p (tree decl)
2838 {
2839   return (outer_var_p (decl)
2840           && !TREE_STATIC (decl));
2841 }
2842
2843 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2844    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
2845    if non-NULL, is the type or namespace used to explicitly qualify
2846    ID_EXPRESSION.  DECL is the entity to which that name has been
2847    resolved.
2848
2849    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2850    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
2851    be set to true if this expression isn't permitted in a
2852    constant-expression, but it is otherwise not set by this function.
2853    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2854    constant-expression, but a non-constant expression is also
2855    permissible.
2856
2857    DONE is true if this expression is a complete postfix-expression;
2858    it is false if this expression is followed by '->', '[', '(', etc.
2859    ADDRESS_P is true iff this expression is the operand of '&'.
2860    TEMPLATE_P is true iff the qualified-id was of the form
2861    "A::template B".  TEMPLATE_ARG_P is true iff this qualified name
2862    appears as a template argument.
2863
2864    If an error occurs, and it is the kind of error that might cause
2865    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
2866    is the caller's responsibility to issue the message.  *ERROR_MSG
2867    will be a string with static storage duration, so the caller need
2868    not "free" it.
2869
2870    Return an expression for the entity, after issuing appropriate
2871    diagnostics.  This function is also responsible for transforming a
2872    reference to a non-static member into a COMPONENT_REF that makes
2873    the use of "this" explicit.
2874
2875    Upon return, *IDK will be filled in appropriately.  */
2876 tree
2877 finish_id_expression (tree id_expression,
2878                       tree decl,
2879                       tree scope,
2880                       cp_id_kind *idk,
2881                       bool integral_constant_expression_p,
2882                       bool allow_non_integral_constant_expression_p,
2883                       bool *non_integral_constant_expression_p,
2884                       bool template_p,
2885                       bool done,
2886                       bool address_p,
2887                       bool template_arg_p,
2888                       const char **error_msg,
2889                       location_t location)
2890 {
2891   decl = strip_using_decl (decl);
2892
2893   /* Initialize the output parameters.  */
2894   *idk = CP_ID_KIND_NONE;
2895   *error_msg = NULL;
2896
2897   if (id_expression == error_mark_node)
2898     return error_mark_node;
2899   /* If we have a template-id, then no further lookup is
2900      required.  If the template-id was for a template-class, we
2901      will sometimes have a TYPE_DECL at this point.  */
2902   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2903            || TREE_CODE (decl) == TYPE_DECL)
2904     ;
2905   /* Look up the name.  */
2906   else
2907     {
2908       if (decl == error_mark_node)
2909         {
2910           /* Name lookup failed.  */
2911           if (scope
2912               && (!TYPE_P (scope)
2913                   || (!dependent_type_p (scope)
2914                       && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2915                            && IDENTIFIER_TYPENAME_P (id_expression)
2916                            && dependent_type_p (TREE_TYPE (id_expression))))))
2917             {
2918               /* If the qualifying type is non-dependent (and the name
2919                  does not name a conversion operator to a dependent
2920                  type), issue an error.  */
2921               qualified_name_lookup_error (scope, id_expression, decl, location);
2922               return error_mark_node;
2923             }
2924           else if (!scope)
2925             {
2926               /* It may be resolved via Koenig lookup.  */
2927               *idk = CP_ID_KIND_UNQUALIFIED;
2928               return id_expression;
2929             }
2930           else
2931             decl = id_expression;
2932         }
2933       /* If DECL is a variable that would be out of scope under
2934          ANSI/ISO rules, but in scope in the ARM, name lookup
2935          will succeed.  Issue a diagnostic here.  */
2936       else
2937         decl = check_for_out_of_scope_variable (decl);
2938
2939       /* Remember that the name was used in the definition of
2940          the current class so that we can check later to see if
2941          the meaning would have been different after the class
2942          was entirely defined.  */
2943       if (!scope && decl != error_mark_node
2944           && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2945         maybe_note_name_used_in_class (id_expression, decl);
2946
2947       /* Disallow uses of local variables from containing functions, except
2948          within lambda-expressions.  */
2949       if (!outer_var_p (decl)
2950           /* It's not a use (3.2) if we're in an unevaluated context.  */
2951           || cp_unevaluated_operand)
2952         /* OK.  */;
2953       else if (TREE_STATIC (decl))
2954         {
2955           if (processing_template_decl)
2956             /* For a use of an outer static var, return the identifier so
2957                that we'll look it up again in the instantiation.  */
2958             return id_expression;
2959         }
2960       else
2961         {
2962           tree context = DECL_CONTEXT (decl);
2963           tree containing_function = current_function_decl;
2964           tree lambda_stack = NULL_TREE;
2965           tree lambda_expr = NULL_TREE;
2966           tree initializer = convert_from_reference (decl);
2967
2968           /* Mark it as used now even if the use is ill-formed.  */
2969           mark_used (decl);
2970
2971           /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2972              support for an approach in which a reference to a local
2973              [constant] automatic variable in a nested class or lambda body
2974              would enter the expression as an rvalue, which would reduce
2975              the complexity of the problem"
2976
2977              FIXME update for final resolution of core issue 696.  */
2978           if (decl_constant_var_p (decl))
2979             {
2980               if (processing_template_decl)
2981                 /* In a template, the constant value may not be in a usable
2982                    form, so look it up again at instantiation time.  */
2983                 return id_expression;
2984               else
2985                 return integral_constant_value (decl);
2986             }
2987
2988           /* If we are in a lambda function, we can move out until we hit
2989              1. the context,
2990              2. a non-lambda function, or
2991              3. a non-default capturing lambda function.  */
2992           while (context != containing_function
2993                  && LAMBDA_FUNCTION_P (containing_function))
2994             {
2995               lambda_expr = CLASSTYPE_LAMBDA_EXPR
2996                 (DECL_CONTEXT (containing_function));
2997
2998               if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
2999                   == CPLD_NONE)
3000                 break;
3001
3002               lambda_stack = tree_cons (NULL_TREE,
3003                                         lambda_expr,
3004                                         lambda_stack);
3005
3006               containing_function
3007                 = decl_function_context (containing_function);
3008             }
3009
3010           if (context == containing_function)
3011             {
3012               decl = add_default_capture (lambda_stack,
3013                                           /*id=*/DECL_NAME (decl),
3014                                           initializer);
3015             }
3016           else if (lambda_expr)
3017             {
3018               error ("%qD is not captured", decl);
3019               return error_mark_node;
3020             }
3021           else
3022             {
3023               error (TREE_CODE (decl) == VAR_DECL
3024                      ? G_("use of %<auto%> variable from containing function")
3025                      : G_("use of parameter from containing function"));
3026               error ("  %q+#D declared here", decl);
3027               return error_mark_node;
3028             }
3029         }
3030
3031       /* Also disallow uses of function parameters outside the function
3032          body, except inside an unevaluated context (i.e. decltype).  */
3033       if (TREE_CODE (decl) == PARM_DECL
3034           && DECL_CONTEXT (decl) == NULL_TREE
3035           && !cp_unevaluated_operand)
3036         {
3037           error ("use of parameter %qD outside function body", decl);
3038           return error_mark_node;
3039         }
3040     }
3041
3042   /* If we didn't find anything, or what we found was a type,
3043      then this wasn't really an id-expression.  */
3044   if (TREE_CODE (decl) == TEMPLATE_DECL
3045       && !DECL_FUNCTION_TEMPLATE_P (decl))
3046     {
3047       *error_msg = "missing template arguments";
3048       return error_mark_node;
3049     }
3050   else if (TREE_CODE (decl) == TYPE_DECL
3051            || TREE_CODE (decl) == NAMESPACE_DECL)
3052     {
3053       *error_msg = "expected primary-expression";
3054       return error_mark_node;
3055     }
3056
3057   /* If the name resolved to a template parameter, there is no
3058      need to look it up again later.  */
3059   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3060       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3061     {
3062       tree r;
3063
3064       *idk = CP_ID_KIND_NONE;
3065       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3066         decl = TEMPLATE_PARM_DECL (decl);
3067       r = convert_from_reference (DECL_INITIAL (decl));
3068
3069       if (integral_constant_expression_p
3070           && !dependent_type_p (TREE_TYPE (decl))
3071           && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3072         {
3073           if (!allow_non_integral_constant_expression_p)
3074             error ("template parameter %qD of type %qT is not allowed in "
3075                    "an integral constant expression because it is not of "
3076                    "integral or enumeration type", decl, TREE_TYPE (decl));
3077           *non_integral_constant_expression_p = true;
3078         }
3079       return r;
3080     }
3081   /* Similarly, we resolve enumeration constants to their
3082      underlying values.  */
3083   else if (TREE_CODE (decl) == CONST_DECL)
3084     {
3085       *idk = CP_ID_KIND_NONE;
3086       if (!processing_template_decl)
3087         {
3088           used_types_insert (TREE_TYPE (decl));
3089           return DECL_INITIAL (decl);
3090         }
3091       return decl;
3092     }
3093   else
3094     {
3095       bool dependent_p;
3096
3097       /* If the declaration was explicitly qualified indicate
3098          that.  The semantics of `A::f(3)' are different than
3099          `f(3)' if `f' is virtual.  */
3100       *idk = (scope
3101               ? CP_ID_KIND_QUALIFIED
3102               : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3103                  ? CP_ID_KIND_TEMPLATE_ID
3104                  : CP_ID_KIND_UNQUALIFIED));
3105
3106
3107       /* [temp.dep.expr]
3108
3109          An id-expression is type-dependent if it contains an
3110          identifier that was declared with a dependent type.
3111
3112          The standard is not very specific about an id-expression that
3113          names a set of overloaded functions.  What if some of them
3114          have dependent types and some of them do not?  Presumably,
3115          such a name should be treated as a dependent name.  */
3116       /* Assume the name is not dependent.  */
3117       dependent_p = false;
3118       if (!processing_template_decl)
3119         /* No names are dependent outside a template.  */
3120         ;
3121       /* A template-id where the name of the template was not resolved
3122          is definitely dependent.  */
3123       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3124                && (TREE_CODE (TREE_OPERAND (decl, 0))
3125                    == IDENTIFIER_NODE))
3126         dependent_p = true;
3127       /* For anything except an overloaded function, just check its
3128          type.  */
3129       else if (!is_overloaded_fn (decl))
3130         dependent_p
3131           = dependent_type_p (TREE_TYPE (decl));
3132       /* For a set of overloaded functions, check each of the
3133          functions.  */
3134       else
3135         {
3136           tree fns = decl;
3137
3138           if (BASELINK_P (fns))
3139             fns = BASELINK_FUNCTIONS (fns);
3140
3141           /* For a template-id, check to see if the template
3142              arguments are dependent.  */
3143           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3144             {
3145               tree args = TREE_OPERAND (fns, 1);
3146               dependent_p = any_dependent_template_arguments_p (args);
3147               /* The functions are those referred to by the
3148                  template-id.  */
3149               fns = TREE_OPERAND (fns, 0);
3150             }
3151
3152           /* If there are no dependent template arguments, go through
3153              the overloaded functions.  */
3154           while (fns && !dependent_p)
3155             {
3156               tree fn = OVL_CURRENT (fns);
3157
3158               /* Member functions of dependent classes are
3159                  dependent.  */
3160               if (TREE_CODE (fn) == FUNCTION_DECL
3161                   && type_dependent_expression_p (fn))
3162                 dependent_p = true;
3163               else if (TREE_CODE (fn) == TEMPLATE_DECL
3164                        && dependent_template_p (fn))
3165                 dependent_p = true;
3166
3167               fns = OVL_NEXT (fns);
3168             }
3169         }
3170
3171       /* If the name was dependent on a template parameter, we will
3172          resolve the name at instantiation time.  */
3173       if (dependent_p)
3174         {
3175           /* Create a SCOPE_REF for qualified names, if the scope is
3176              dependent.  */
3177           if (scope)
3178             {
3179               if (TYPE_P (scope))
3180                 {
3181                   if (address_p && done)
3182                     decl = finish_qualified_id_expr (scope, decl,
3183                                                      done, address_p,
3184                                                      template_p,
3185                                                      template_arg_p);
3186                   else
3187                     {
3188                       tree type = NULL_TREE;
3189                       if (DECL_P (decl) && !dependent_scope_p (scope))
3190                         type = TREE_TYPE (decl);
3191                       decl = build_qualified_name (type,
3192                                                    scope,
3193                                                    id_expression,
3194                                                    template_p);
3195                     }
3196                 }
3197               if (TREE_TYPE (decl))
3198                 decl = convert_from_reference (decl);
3199               return decl;
3200             }
3201           /* A TEMPLATE_ID already contains all the information we
3202              need.  */
3203           if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3204             return id_expression;
3205           *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3206           /* If we found a variable, then name lookup during the
3207              instantiation will always resolve to the same VAR_DECL
3208              (or an instantiation thereof).  */
3209           if (TREE_CODE (decl) == VAR_DECL
3210               || TREE_CODE (decl) == PARM_DECL)
3211             {
3212               mark_used (decl);
3213               return convert_from_reference (decl);
3214             }
3215           /* The same is true for FIELD_DECL, but we also need to
3216              make sure that the syntax is correct.  */
3217           else if (TREE_CODE (decl) == FIELD_DECL)
3218             {
3219               /* Since SCOPE is NULL here, this is an unqualified name.
3220                  Access checking has been performed during name lookup
3221                  already.  Turn off checking to avoid duplicate errors.  */
3222               push_deferring_access_checks (dk_no_check);
3223               decl = finish_non_static_data_member
3224                        (decl, NULL_TREE,
3225                         /*qualifying_scope=*/NULL_TREE);
3226               pop_deferring_access_checks ();
3227               return decl;
3228             }
3229           return id_expression;
3230         }
3231
3232       if (TREE_CODE (decl) == NAMESPACE_DECL)
3233         {
3234           error ("use of namespace %qD as expression", decl);
3235           return error_mark_node;
3236         }
3237       else if (DECL_CLASS_TEMPLATE_P (decl))
3238         {
3239           error ("use of class template %qT as expression", decl);
3240           return error_mark_node;
3241         }
3242       else if (TREE_CODE (decl) == TREE_LIST)
3243         {
3244           /* Ambiguous reference to base members.  */
3245           error ("request for member %qD is ambiguous in "
3246                  "multiple inheritance lattice", id_expression);
3247           print_candidates (decl);
3248           return error_mark_node;
3249         }
3250
3251       /* Mark variable-like entities as used.  Functions are similarly
3252          marked either below or after overload resolution.  */
3253       if (TREE_CODE (decl) == VAR_DECL
3254           || TREE_CODE (decl) == PARM_DECL
3255           || TREE_CODE (decl) == RESULT_DECL)
3256         mark_used (decl);
3257
3258       /* Only certain kinds of names are allowed in constant
3259          expression.  Enumerators and template parameters have already
3260          been handled above.  */
3261       if (! error_operand_p (decl)
3262           && integral_constant_expression_p
3263           && ! decl_constant_var_p (decl)
3264           && ! builtin_valid_in_constant_expr_p (decl))
3265         {
3266           if (!allow_non_integral_constant_expression_p)
3267             {
3268               error ("%qD cannot appear in a constant-expression", decl);
3269               return error_mark_node;
3270             }
3271           *non_integral_constant_expression_p = true;
3272         }
3273
3274       if (scope)
3275         {
3276           decl = (adjust_result_of_qualified_name_lookup
3277                   (decl, scope, current_nonlambda_class_type()));
3278
3279           if (TREE_CODE (decl) == FUNCTION_DECL)
3280             mark_used (decl);
3281
3282           if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3283             decl = finish_qualified_id_expr (scope,
3284                                              decl,
3285                                              done,
3286                                              address_p,
3287                                              template_p,
3288                                              template_arg_p);
3289           else
3290             {
3291               tree r = convert_from_reference (decl);
3292
3293               /* In a template, return a SCOPE_REF for most qualified-ids
3294                  so that we can check access at instantiation time.  But if
3295                  we're looking at a member of the current instantiation, we
3296                  know we have access and building up the SCOPE_REF confuses
3297                  non-type template argument handling.  */
3298               if (processing_template_decl && TYPE_P (scope)
3299                   && !currently_open_class (scope))
3300                 r = build_qualified_name (TREE_TYPE (r),
3301                                           scope, decl,
3302                                           template_p);
3303               decl = r;
3304             }
3305         }
3306       else if (TREE_CODE (decl) == FIELD_DECL)
3307         {
3308           /* Since SCOPE is NULL here, this is an unqualified name.
3309              Access checking has been performed during name lookup
3310              already.  Turn off checking to avoid duplicate errors.  */
3311           push_deferring_access_checks (dk_no_check);
3312           decl = finish_non_static_data_member (decl, NULL_TREE,
3313                                                 /*qualifying_scope=*/NULL_TREE);
3314           pop_deferring_access_checks ();
3315         }
3316       else if (is_overloaded_fn (decl))
3317         {
3318           tree first_fn;
3319
3320           first_fn = get_first_fn (decl);
3321           if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3322             first_fn = DECL_TEMPLATE_RESULT (first_fn);
3323
3324           if (!really_overloaded_fn (decl)
3325               && !mark_used (first_fn))
3326             return error_mark_node;
3327
3328           if (!template_arg_p
3329               && TREE_CODE (first_fn) == FUNCTION_DECL
3330               && DECL_FUNCTION_MEMBER_P (first_fn)
3331               && !shared_member_p (decl))
3332             {
3333               /* A set of member functions.  */
3334               decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3335               return finish_class_member_access_expr (decl, id_expression,
3336                                                       /*template_p=*/false,
3337                                                       tf_warning_or_error);
3338             }
3339
3340           decl = baselink_for_fns (decl);
3341         }
3342       else
3343         {
3344           if (DECL_P (decl) && DECL_NONLOCAL (decl)
3345               && DECL_CLASS_SCOPE_P (decl))
3346             {
3347               tree context = context_for_name_lookup (decl); 
3348               if (context != current_class_type)
3349                 {
3350                   tree path = currently_open_derived_class (context);
3351                   perform_or_defer_access_check (TYPE_BINFO (path),
3352                                                  decl, decl);
3353                 }
3354             }
3355
3356           decl = convert_from_reference (decl);
3357         }
3358     }
3359
3360   if (TREE_DEPRECATED (decl))
3361     warn_deprecated_use (decl, NULL_TREE);
3362
3363   return decl;
3364 }
3365
3366 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3367    use as a type-specifier.  */
3368
3369 tree
3370 finish_typeof (tree expr)
3371 {
3372   tree type;
3373
3374   if (type_dependent_expression_p (expr))
3375     {
3376       type = cxx_make_type (TYPEOF_TYPE);
3377       TYPEOF_TYPE_EXPR (type) = expr;
3378       SET_TYPE_STRUCTURAL_EQUALITY (type);
3379
3380       return type;
3381     }
3382
3383   expr = mark_type_use (expr);
3384
3385   type = unlowered_expr_type (expr);
3386
3387   if (!type || type == unknown_type_node)
3388     {
3389       error ("type of %qE is unknown", expr);
3390       return error_mark_node;
3391     }
3392
3393   return type;
3394 }
3395
3396 /* Implement the __underlying_type keyword: Return the underlying
3397    type of TYPE, suitable for use as a type-specifier.  */
3398
3399 tree
3400 finish_underlying_type (tree type)
3401 {
3402   tree underlying_type;
3403
3404   if (processing_template_decl)
3405     {
3406       underlying_type = cxx_make_type (UNDERLYING_TYPE);
3407       UNDERLYING_TYPE_TYPE (underlying_type) = type;
3408       SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3409
3410       return underlying_type;
3411     }
3412
3413   complete_type (type);
3414
3415   if (TREE_CODE (type) != ENUMERAL_TYPE)
3416     {
3417       error ("%qT is not an enumeration type", type);
3418       return error_mark_node;
3419     }
3420
3421   underlying_type = ENUM_UNDERLYING_TYPE (type);
3422
3423   /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3424      includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3425      See finish_enum_value_list for details.  */
3426   if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3427     underlying_type
3428       = c_common_type_for_mode (TYPE_MODE (underlying_type),
3429                                 TYPE_UNSIGNED (underlying_type));
3430
3431   return underlying_type;
3432 }
3433
3434 /* Implement the __direct_bases keyword: Return the direct base classes
3435    of type */
3436
3437 tree
3438 calculate_direct_bases (tree type)
3439 {
3440   VEC(tree, gc) *vector = make_tree_vector();
3441   tree bases_vec = NULL_TREE;
3442   VEC(tree, none) *base_binfos;
3443   tree binfo;
3444   unsigned i;
3445
3446   complete_type (type);
3447
3448   if (!NON_UNION_CLASS_TYPE_P (type))
3449     return make_tree_vec (0);
3450
3451   base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3452
3453   /* Virtual bases are initialized first */
3454   for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3455     {
3456       if (BINFO_VIRTUAL_P (binfo))
3457        {
3458          VEC_safe_push (tree, gc, vector, binfo);
3459        }
3460     }
3461
3462   /* Now non-virtuals */
3463   for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3464     {
3465       if (!BINFO_VIRTUAL_P (binfo))
3466        {
3467          VEC_safe_push (tree, gc, vector, binfo);
3468        }
3469     }
3470
3471
3472   bases_vec = make_tree_vec (VEC_length (tree, vector));
3473
3474   for (i = 0; i < VEC_length (tree, vector); ++i)
3475     {
3476       TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE (VEC_index (tree, vector, i));
3477     }
3478   return bases_vec;
3479 }
3480
3481 /* Implement the __bases keyword: Return the base classes
3482    of type */
3483
3484 /* Find morally non-virtual base classes by walking binfo hierarchy */
3485 /* Virtual base classes are handled separately in finish_bases */
3486
3487 static tree
3488 dfs_calculate_bases_pre (tree binfo, ATTRIBUTE_UNUSED void *data_)
3489 {
3490   /* Don't walk bases of virtual bases */
3491   return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3492 }
3493
3494 static tree
3495 dfs_calculate_bases_post (tree binfo, void *data_)
3496 {
3497   VEC(tree, gc) **data = (VEC(tree, gc) **) data_;
3498   if (!BINFO_VIRTUAL_P (binfo))
3499     {
3500       VEC_safe_push (tree, gc, *data, BINFO_TYPE (binfo));
3501     }
3502   return NULL_TREE;
3503 }
3504
3505 /* Calculates the morally non-virtual base classes of a class */
3506 static VEC(tree, gc) *
3507 calculate_bases_helper (tree type)
3508 {
3509   VEC(tree, gc) *vector = make_tree_vector();
3510
3511   /* Now add non-virtual base classes in order of construction */
3512   dfs_walk_all (TYPE_BINFO (type),
3513                 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3514   return vector;
3515 }
3516
3517 tree
3518 calculate_bases (tree type)
3519 {
3520   VEC(tree, gc) *vector = make_tree_vector();
3521   tree bases_vec = NULL_TREE;
3522   unsigned i;
3523   VEC(tree, gc) *vbases;
3524   VEC(tree, gc) *nonvbases;
3525   tree binfo;
3526
3527   complete_type (type);
3528
3529   if (!NON_UNION_CLASS_TYPE_P (type))
3530     return make_tree_vec (0);
3531
3532   /* First go through virtual base classes */
3533   for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3534        VEC_iterate (tree, vbases, i, binfo); i++)
3535     {
3536       VEC(tree, gc) *vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3537       VEC_safe_splice (tree, gc, vector, vbase_bases);
3538       release_tree_vector (vbase_bases);
3539     }
3540
3541   /* Now for the non-virtual bases */
3542   nonvbases = calculate_bases_helper (type);
3543   VEC_safe_splice (tree, gc, vector, nonvbases);
3544   release_tree_vector (nonvbases);
3545
3546   /* Last element is entire class, so don't copy */
3547   bases_vec = make_tree_vec (VEC_length (tree, vector) - 1);
3548
3549   for (i = 0; i < VEC_length (tree, vector) - 1; ++i)
3550     {
3551       TREE_VEC_ELT (bases_vec, i) = VEC_index (tree, vector, i);
3552     }