OSDN Git Service

* c-common.h (add_decl_stmt): Move to cp-tree.h.
[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,
7    2003, 2004 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 2, 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 COPYING.  If not, write to the Free
25    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26    02111-1307, USA.  */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "tree-inline.h"
35 #include "tree-mudflap.h"
36 #include "except.h"
37 #include "lex.h"
38 #include "toplev.h"
39 #include "flags.h"
40 #include "rtl.h"
41 #include "expr.h"
42 #include "output.h"
43 #include "timevar.h"
44 #include "debug.h"
45 #include "diagnostic.h"
46 #include "cgraph.h"
47 #include "tree-iterator.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.  Since the current g++ parser is
53    lacking in several respects, and will be reimplemented, we are
54    attempting to move most code that is not directly related to
55    parsing into this file; that will make implementing the new parser
56    much easier since it will be able to make use of these routines.  */
57
58 static tree maybe_convert_cond (tree);
59 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
60 static void emit_associated_thunks (tree);
61 static tree finalize_nrv_r (tree *, int *, void *);
62
63
64 /* Deferred Access Checking Overview
65    ---------------------------------
66
67    Most C++ expressions and declarations require access checking
68    to be performed during parsing.  However, in several cases,
69    this has to be treated differently.
70
71    For member declarations, access checking has to be deferred
72    until more information about the declaration is known.  For
73    example:
74
75      class A {
76          typedef int X;
77        public:
78          X f();
79      };
80
81      A::X A::f();
82      A::X g();
83
84    When we are parsing the function return type `A::X', we don't
85    really know if this is allowed until we parse the function name.
86
87    Furthermore, some contexts require that access checking is
88    never performed at all.  These include class heads, and template
89    instantiations.
90
91    Typical use of access checking functions is described here:
92    
93    1. When we enter a context that requires certain access checking
94       mode, the function `push_deferring_access_checks' is called with
95       DEFERRING argument specifying the desired mode.  Access checking
96       may be performed immediately (dk_no_deferred), deferred
97       (dk_deferred), or not performed (dk_no_check).
98
99    2. When a declaration such as a type, or a variable, is encountered,
100       the function `perform_or_defer_access_check' is called.  It
101       maintains a TREE_LIST of all deferred checks.
102
103    3. The global `current_class_type' or `current_function_decl' is then
104       setup by the parser.  `enforce_access' relies on these information
105       to check access.
106
107    4. Upon exiting the context mentioned in step 1,
108       `perform_deferred_access_checks' is called to check all declaration
109       stored in the TREE_LIST.   `pop_deferring_access_checks' is then
110       called to restore the previous access checking mode.
111
112       In case of parsing error, we simply call `pop_deferring_access_checks'
113       without `perform_deferred_access_checks'.  */
114
115 /* Data for deferred access checking.  */
116 static GTY(()) deferred_access *deferred_access_stack;
117 static GTY(()) deferred_access *deferred_access_free_list;
118
119 /* Save the current deferred access states and start deferred
120    access checking iff DEFER_P is true.  */
121
122 void
123 push_deferring_access_checks (deferring_kind deferring)
124 {
125   deferred_access *d;
126
127   /* For context like template instantiation, access checking
128      disabling applies to all nested context.  */
129   if (deferred_access_stack
130       && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
131     deferring = dk_no_check;
132
133   /* Recycle previously used free store if available.  */
134   if (deferred_access_free_list)
135     {
136       d = deferred_access_free_list;
137       deferred_access_free_list = d->next;
138     }
139   else
140     d = ggc_alloc (sizeof (deferred_access));
141
142   d->next = deferred_access_stack;
143   d->deferred_access_checks = NULL_TREE;
144   d->deferring_access_checks_kind = deferring;
145   deferred_access_stack = d;
146 }
147
148 /* Resume deferring access checks again after we stopped doing
149    this previously.  */
150
151 void
152 resume_deferring_access_checks (void)
153 {
154   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
155     deferred_access_stack->deferring_access_checks_kind = dk_deferred;
156 }
157
158 /* Stop deferring access checks.  */
159
160 void
161 stop_deferring_access_checks (void)
162 {
163   if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
164     deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
165 }
166
167 /* Discard the current deferred access checks and restore the
168    previous states.  */
169
170 void
171 pop_deferring_access_checks (void)
172 {
173   deferred_access *d = deferred_access_stack;
174   deferred_access_stack = d->next;
175
176   /* Remove references to access checks TREE_LIST.  */
177   d->deferred_access_checks = NULL_TREE;
178
179   /* Store in free list for later use.  */
180   d->next = deferred_access_free_list;
181   deferred_access_free_list = d;
182 }
183
184 /* Returns a TREE_LIST representing the deferred checks.  
185    The TREE_PURPOSE of each node is the type through which the 
186    access occurred; the TREE_VALUE is the declaration named.
187    */
188
189 tree
190 get_deferred_access_checks (void)
191 {
192   return deferred_access_stack->deferred_access_checks;
193 }
194
195 /* Take current deferred checks and combine with the
196    previous states if we also defer checks previously.
197    Otherwise perform checks now.  */
198
199 void
200 pop_to_parent_deferring_access_checks (void)
201 {
202   tree deferred_check = get_deferred_access_checks ();
203   deferred_access *d1 = deferred_access_stack;
204   deferred_access *d2 = deferred_access_stack->next;
205   deferred_access *d3 = deferred_access_stack->next->next;
206
207   /* Temporary swap the order of the top two states, just to make
208      sure the garbage collector will not reclaim the memory during 
209      processing below.  */
210   deferred_access_stack = d2;
211   d2->next = d1;
212   d1->next = d3;
213
214   for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
215     /* Perform deferred check if required.  */
216     perform_or_defer_access_check (TREE_PURPOSE (deferred_check), 
217                                    TREE_VALUE (deferred_check));
218
219   deferred_access_stack = d1;
220   d1->next = d2;
221   d2->next = d3;
222   pop_deferring_access_checks ();
223 }
224
225 /* Perform the deferred access checks.
226
227    After performing the checks, we still have to keep the list
228    `deferred_access_stack->deferred_access_checks' since we may want
229    to check access for them again later in a different context.
230    For example:
231
232      class A {
233        typedef int X;
234        static X a;
235      };
236      A::X A::a, x;      // No error for `A::a', error for `x'
237
238    We have to perform deferred access of `A::X', first with `A::a',
239    next with `x'.  */
240
241 void
242 perform_deferred_access_checks (void)
243 {
244   tree deferred_check;
245   for (deferred_check = deferred_access_stack->deferred_access_checks;
246        deferred_check;
247        deferred_check = TREE_CHAIN (deferred_check))
248     /* Check access.  */
249     enforce_access (TREE_PURPOSE (deferred_check), 
250                     TREE_VALUE (deferred_check));
251 }
252
253 /* Defer checking the accessibility of DECL, when looked up in
254    BINFO.  */
255
256 void
257 perform_or_defer_access_check (tree binfo, tree decl)
258 {
259   tree check;
260
261   my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
262   
263   /* If we are not supposed to defer access checks, just check now.  */
264   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
265     {
266       enforce_access (binfo, decl);
267       return;
268     }
269   /* Exit if we are in a context that no access checking is performed.  */
270   else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
271     return;
272
273   /* See if we are already going to perform this check.  */
274   for (check = deferred_access_stack->deferred_access_checks;
275        check;
276        check = TREE_CHAIN (check))
277     if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
278       return;
279   /* If not, record the check.  */
280   deferred_access_stack->deferred_access_checks
281     = tree_cons (binfo, decl,
282                  deferred_access_stack->deferred_access_checks);
283 }
284
285 /* Returns nonzero if the current statement is a full expression,
286    i.e. temporaries created during that statement should be destroyed
287    at the end of the statement.  */
288
289 int
290 stmts_are_full_exprs_p (void)
291 {
292   return current_stmt_tree ()->stmts_are_full_exprs_p;
293 }
294
295 /* Returns the stmt_tree (if any) to which statements are currently
296    being added.  If there is no active statement-tree, NULL is
297    returned.  */
298
299 stmt_tree
300 current_stmt_tree (void)
301 {
302   return (cfun 
303           ? &cfun->language->base.x_stmt_tree 
304           : &scope_chain->x_stmt_tree);
305 }
306
307 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR.  */
308
309 static tree
310 maybe_cleanup_point_expr (tree expr)
311 {
312   if (!processing_template_decl && stmts_are_full_exprs_p ())
313     expr = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (expr), expr));
314   return expr;
315 }
316
317 /* Create a declaration statement for the declaration given by the DECL.  */
318
319 void
320 add_decl_stmt (tree decl)
321 {
322   tree r = build_stmt (DECL_STMT, decl);
323   if (DECL_INITIAL (decl))
324     r = maybe_cleanup_point_expr (r);
325   add_stmt (r);
326 }
327
328 /* Nonzero if TYPE is an anonymous union or struct type.  We have to use a
329    flag for this because "A union for which objects or pointers are
330    declared is not an anonymous union" [class.union].  */
331
332 int
333 anon_aggr_type_p (tree node)
334 {
335   return ANON_AGGR_TYPE_P (node);
336 }
337
338 /* Finish a scope.  */
339
340 static tree
341 do_poplevel (tree stmt_list)
342 {
343   tree block = NULL;
344
345   if (stmts_are_full_exprs_p ())
346     block = poplevel (kept_level_p (), 1, 0);
347
348   stmt_list = pop_stmt_list (stmt_list);
349   
350   if (!processing_template_decl)
351     {
352       stmt_list = c_build_bind_expr (block, stmt_list);
353       /* ??? See c_end_compound_stmt re statement expressions.  */
354     }
355
356   return stmt_list;
357 }
358
359 /* Begin a new scope.  */ 
360
361 static tree
362 do_pushlevel (scope_kind sk)
363 {
364   tree ret = push_stmt_list ();
365   if (stmts_are_full_exprs_p ())
366     begin_scope (sk, NULL);
367   return ret;
368 }
369
370 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
371    when the current scope is exited.  EH_ONLY is true when this is not
372    meant to apply to normal control flow transfer.  */
373
374 void
375 push_cleanup (tree decl, tree cleanup, bool eh_only)
376 {
377   tree stmt = build_stmt (CLEANUP_STMT, NULL, cleanup, decl);
378   CLEANUP_EH_ONLY (stmt) = eh_only;
379   add_stmt (stmt);
380   CLEANUP_BODY (stmt) = push_stmt_list ();
381 }
382
383 /* Begin a conditional that might contain a declaration.  When generating
384    normal code, we want the declaration to appear before the statement
385    containing the conditional.  When generating template code, we want the
386    conditional to be rendered as the raw DECL_STMT.  */
387
388 static void
389 begin_cond (tree *cond_p)
390 {
391   if (processing_template_decl)
392     *cond_p = push_stmt_list ();
393 }
394
395 /* Finish such a conditional.  */
396
397 static void
398 finish_cond (tree *cond_p, tree expr)
399 {
400   if (processing_template_decl)
401     {
402       tree cond = pop_stmt_list (*cond_p);
403       if (TREE_CODE (cond) == DECL_STMT)
404         expr = cond;
405     }
406   *cond_p = expr;
407 }
408
409 /* If *COND_P specifies a conditional with a declaration, transform the
410    loop such that
411             while (A x = 42) { }
412             for (; A x = 42;) { }
413    becomes
414             while (true) { A x = 42; if (!x) break; }
415             for (;;) { A x = 42; if (!x) break; }
416    The statement list for BODY will be empty if the conditional did
417    not declare anything.  */
418                                                                                 
419 static void
420 simplify_loop_decl_cond (tree *cond_p, tree body)
421 {
422   tree cond, if_stmt;
423
424   if (!TREE_SIDE_EFFECTS (body))
425     return;
426
427   cond = *cond_p;
428   *cond_p = boolean_true_node;
429    
430   if_stmt = begin_if_stmt ();
431   cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
432   finish_if_stmt_cond (cond, if_stmt);
433   finish_break_stmt ();
434   finish_then_clause (if_stmt);
435   finish_if_stmt (if_stmt);
436 }
437
438 /* Finish a goto-statement.  */
439
440 tree
441 finish_goto_stmt (tree destination)
442 {
443   if (TREE_CODE (destination) == IDENTIFIER_NODE)
444     destination = lookup_label (destination);
445
446   /* We warn about unused labels with -Wunused.  That means we have to
447      mark the used labels as used.  */
448   if (TREE_CODE (destination) == LABEL_DECL)
449     TREE_USED (destination) = 1;
450   else
451     {
452       /* The DESTINATION is being used as an rvalue.  */
453       if (!processing_template_decl)
454         destination = decay_conversion (destination);
455       /* We don't inline calls to functions with computed gotos.
456          Those functions are typically up to some funny business,
457          and may be depending on the labels being at particular
458          addresses, or some such.  */
459       DECL_UNINLINABLE (current_function_decl) = 1;
460     }
461   
462   check_goto (destination);
463
464   return add_stmt (build_stmt (GOTO_EXPR, destination));
465 }
466
467 /* COND is the condition-expression for an if, while, etc.,
468    statement.  Convert it to a boolean value, if appropriate.  */
469
470 static tree
471 maybe_convert_cond (tree cond)
472 {
473   /* Empty conditions remain empty.  */
474   if (!cond)
475     return NULL_TREE;
476
477   /* Wait until we instantiate templates before doing conversion.  */
478   if (processing_template_decl)
479     return cond;
480
481   /* Do the conversion.  */
482   cond = convert_from_reference (cond);
483   return condition_conversion (cond);
484 }
485
486 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
487
488 tree
489 finish_expr_stmt (tree expr)
490 {
491   tree r = NULL_TREE;
492
493   if (expr != NULL_TREE)
494     {
495       if (!processing_template_decl)
496         expr = convert_to_void (expr, "statement");
497       else if (!type_dependent_expression_p (expr))
498         convert_to_void (build_non_dependent_expr (expr), "statement");
499
500       /* Simplification of inner statement expressions, compound exprs,
501          etc can result in the us already having an EXPR_STMT.  */
502       if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
503         {
504           if (TREE_CODE (expr) != EXPR_STMT)
505             expr = build_stmt (EXPR_STMT, expr);
506           expr = maybe_cleanup_point_expr (expr);
507         }
508
509       r = add_stmt (expr);
510     }
511
512   finish_stmt ();
513
514   return r;
515 }
516
517
518 /* Begin an if-statement.  Returns a newly created IF_STMT if
519    appropriate.  */
520
521 tree
522 begin_if_stmt (void)
523 {
524   tree r, scope;
525   scope = do_pushlevel (sk_block);
526   r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
527   TREE_CHAIN (r) = scope;
528   begin_cond (&IF_COND (r));
529   return r;
530 }
531
532 /* Process the COND of an if-statement, which may be given by
533    IF_STMT.  */
534
535 void 
536 finish_if_stmt_cond (tree cond, tree if_stmt)
537 {
538   finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
539   add_stmt (if_stmt);
540   THEN_CLAUSE (if_stmt) = push_stmt_list ();
541 }
542
543 /* Finish the then-clause of an if-statement, which may be given by
544    IF_STMT.  */
545
546 tree
547 finish_then_clause (tree if_stmt)
548 {
549   THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
550   return if_stmt;
551 }
552
553 /* Begin the else-clause of an if-statement.  */
554
555 void
556 begin_else_clause (tree if_stmt)
557 {
558   ELSE_CLAUSE (if_stmt) = push_stmt_list ();
559 }
560
561 /* Finish the else-clause of an if-statement, which may be given by
562    IF_STMT.  */
563
564 void
565 finish_else_clause (tree if_stmt)
566 {
567   ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
568 }
569
570 /* Finish an if-statement.  */
571
572 void 
573 finish_if_stmt (tree if_stmt)
574 {
575   tree scope = TREE_CHAIN (if_stmt);
576   TREE_CHAIN (if_stmt) = NULL;
577   add_stmt (do_poplevel (scope));
578   finish_stmt ();
579 }
580
581 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
582    appropriate.  */
583
584 tree
585 begin_while_stmt (void)
586 {
587   tree r;
588   r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
589   add_stmt (r);
590   WHILE_BODY (r) = do_pushlevel (sk_block);
591   begin_cond (&WHILE_COND (r));
592   return r;
593 }
594
595 /* Process the COND of a while-statement, which may be given by
596    WHILE_STMT.  */
597
598 void 
599 finish_while_stmt_cond (tree cond, tree while_stmt)
600 {
601   finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
602   simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
603 }
604
605 /* Finish a while-statement, which may be given by WHILE_STMT.  */
606
607 void 
608 finish_while_stmt (tree while_stmt)
609 {
610   WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
611   finish_stmt ();
612 }
613
614 /* Begin a do-statement.  Returns a newly created DO_STMT if
615    appropriate.  */
616
617 tree
618 begin_do_stmt (void)
619 {
620   tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
621   add_stmt (r);
622   DO_BODY (r) = push_stmt_list ();
623   return r;
624 }
625
626 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
627
628 void
629 finish_do_body (tree do_stmt)
630 {
631   DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
632 }
633
634 /* Finish a do-statement, which may be given by DO_STMT, and whose
635    COND is as indicated.  */
636
637 void
638 finish_do_stmt (tree cond, tree do_stmt)
639 {
640   cond = maybe_convert_cond (cond);
641   DO_COND (do_stmt) = cond;
642   finish_stmt ();
643 }
644
645 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
646    indicated.  */
647
648 tree
649 finish_return_stmt (tree expr)
650 {
651   tree r;
652
653   expr = check_return_expr (expr);
654   if (!processing_template_decl)
655     {
656       if (DECL_DESTRUCTOR_P (current_function_decl))
657         {
658           /* Similarly, all destructors must run destructors for
659              base-classes before returning.  So, all returns in a
660              destructor get sent to the DTOR_LABEL; finish_function emits
661              code to return a value there.  */
662           return finish_goto_stmt (dtor_label);
663         }
664     }
665
666   r = build_stmt (RETURN_STMT, expr);
667   r = maybe_cleanup_point_expr (r);
668   r = add_stmt (r);
669   finish_stmt ();
670
671   return r;
672 }
673
674 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
675
676 tree
677 begin_for_stmt (void)
678 {
679   tree r;
680
681   r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE, 
682                   NULL_TREE, NULL_TREE);
683
684   if (flag_new_for_scope > 0)
685     TREE_CHAIN (r) = do_pushlevel (sk_for);
686
687   if (processing_template_decl)
688     FOR_INIT_STMT (r) = push_stmt_list ();
689
690   return r;
691 }
692
693 /* Finish the for-init-statement of a for-statement, which may be
694    given by FOR_STMT.  */
695
696 void
697 finish_for_init_stmt (tree for_stmt)
698 {
699   if (processing_template_decl)
700     FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
701   add_stmt (for_stmt);
702   FOR_BODY (for_stmt) = do_pushlevel (sk_block);
703   begin_cond (&FOR_COND (for_stmt));
704 }
705
706 /* Finish the COND of a for-statement, which may be given by
707    FOR_STMT.  */
708
709 void
710 finish_for_cond (tree cond, tree for_stmt)
711 {
712   finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
713   simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
714 }
715
716 /* Finish the increment-EXPRESSION in a for-statement, which may be
717    given by FOR_STMT.  */
718
719 void
720 finish_for_expr (tree expr, tree for_stmt)
721 {
722   if (!expr)
723     return;
724   /* If EXPR is an overloaded function, issue an error; there is no
725      context available to use to perform overload resolution.  */
726   if (type_unknown_p (expr))
727     {
728       cxx_incomplete_type_error (expr, TREE_TYPE (expr));
729       expr = error_mark_node;
730     }
731   expr = maybe_cleanup_point_expr (expr);
732   FOR_EXPR (for_stmt) = expr;
733 }
734
735 /* Finish the body of a for-statement, which may be given by
736    FOR_STMT.  The increment-EXPR for the loop must be
737    provided.  */
738
739 void
740 finish_for_stmt (tree for_stmt)
741 {
742   FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
743
744   /* Pop the scope for the body of the loop.  */
745   if (flag_new_for_scope > 0)
746     {
747       tree scope = TREE_CHAIN (for_stmt);
748       TREE_CHAIN (for_stmt) = NULL;
749       add_stmt (do_poplevel (scope));
750     }
751
752   finish_stmt (); 
753 }
754
755 /* Finish a break-statement.  */
756
757 tree
758 finish_break_stmt (void)
759 {
760   return add_stmt (build_break_stmt ());
761 }
762
763 /* Finish a continue-statement.  */
764
765 tree
766 finish_continue_stmt (void)
767 {
768   return add_stmt (build_continue_stmt ());
769 }
770
771 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
772    appropriate.  */
773
774 tree
775 begin_switch_stmt (void)
776 {
777   tree r, scope;
778
779   r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
780
781   scope = do_pushlevel (sk_block);
782   TREE_CHAIN (r) = scope;
783   begin_cond (&SWITCH_COND (r));
784
785   return r;
786 }
787
788 /* Finish the cond of a switch-statement.  */
789
790 void
791 finish_switch_cond (tree cond, tree switch_stmt)
792 {
793   tree orig_type = NULL;
794   if (!processing_template_decl)
795     {
796       tree index;
797
798       /* Convert the condition to an integer or enumeration type.  */
799       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
800       if (cond == NULL_TREE)
801         {
802           error ("switch quantity not an integer");
803           cond = error_mark_node;
804         }
805       orig_type = TREE_TYPE (cond);
806       if (cond != error_mark_node)
807         {
808           /* [stmt.switch]
809
810              Integral promotions are performed.  */
811           cond = perform_integral_promotions (cond);
812           cond = maybe_cleanup_point_expr (cond);
813         }
814
815       if (cond != error_mark_node)
816         {
817           index = get_unwidened (cond, NULL_TREE);
818           /* We can't strip a conversion from a signed type to an unsigned,
819              because if we did, int_fits_type_p would do the wrong thing
820              when checking case values for being in range,
821              and it's too hard to do the right thing.  */
822           if (TYPE_UNSIGNED (TREE_TYPE (cond))
823               == TYPE_UNSIGNED (TREE_TYPE (index)))
824             cond = index;
825         }
826     }
827   finish_cond (&SWITCH_COND (switch_stmt), cond);
828   SWITCH_TYPE (switch_stmt) = orig_type;
829   add_stmt (switch_stmt);
830   push_switch (switch_stmt);
831   SWITCH_BODY (switch_stmt) = push_stmt_list ();
832 }
833
834 /* Finish the body of a switch-statement, which may be given by
835    SWITCH_STMT.  The COND to switch on is indicated.  */
836
837 void
838 finish_switch_stmt (tree switch_stmt)
839 {
840   tree scope;
841
842   SWITCH_BODY (switch_stmt) = pop_stmt_list (SWITCH_BODY (switch_stmt));
843   pop_switch (); 
844   finish_stmt ();
845
846   scope = TREE_CHAIN (switch_stmt);
847   TREE_CHAIN (switch_stmt) = NULL;
848   add_stmt (do_poplevel (scope));
849 }
850
851 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
852    appropriate.  */
853
854 tree
855 begin_try_block (void)
856 {
857   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
858   add_stmt (r);
859   TRY_STMTS (r) = push_stmt_list ();
860   return r;
861 }
862
863 /* Likewise, for a function-try-block.  */
864
865 tree
866 begin_function_try_block (void)
867 {
868   tree r = begin_try_block ();
869   FN_TRY_BLOCK_P (r) = 1;
870   return r;
871 }
872
873 /* Finish a try-block, which may be given by TRY_BLOCK.  */
874
875 void
876 finish_try_block (tree try_block)
877 {
878   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
879   TRY_HANDLERS (try_block) = push_stmt_list ();
880 }
881
882 /* Finish the body of a cleanup try-block, which may be given by
883    TRY_BLOCK.  */
884
885 void
886 finish_cleanup_try_block (tree try_block)
887 {
888   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
889 }
890
891 /* Finish an implicitly generated try-block, with a cleanup is given
892    by CLEANUP.  */
893
894 void
895 finish_cleanup (tree cleanup, tree try_block)
896 {
897   TRY_HANDLERS (try_block) = cleanup;
898   CLEANUP_P (try_block) = 1;
899 }
900
901 /* Likewise, for a function-try-block.  */
902
903 void
904 finish_function_try_block (tree try_block)
905 {
906   finish_try_block (try_block);
907   /* FIXME : something queer about CTOR_INITIALIZER somehow following
908      the try block, but moving it inside.  */
909   in_function_try_handler = 1;
910 }
911
912 /* Finish a handler-sequence for a try-block, which may be given by
913    TRY_BLOCK.  */
914
915 void
916 finish_handler_sequence (tree try_block)
917 {
918   TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
919   check_handlers (TRY_HANDLERS (try_block));
920 }
921
922 /* Likewise, for a function-try-block.  */
923
924 void
925 finish_function_handler_sequence (tree try_block)
926 {
927   in_function_try_handler = 0;
928   finish_handler_sequence (try_block);
929 }
930
931 /* Begin a handler.  Returns a HANDLER if appropriate.  */
932
933 tree
934 begin_handler (void)
935 {
936   tree r;
937
938   r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
939   add_stmt (r);
940
941   /* Create a binding level for the eh_info and the exception object
942      cleanup.  */
943   HANDLER_BODY (r) = do_pushlevel (sk_catch);
944
945   return r;
946 }
947
948 /* Finish the handler-parameters for a handler, which may be given by
949    HANDLER.  DECL is the declaration for the catch parameter, or NULL
950    if this is a `catch (...)' clause.  */
951
952 void
953 finish_handler_parms (tree decl, tree handler)
954 {
955   tree type = NULL_TREE;
956   if (processing_template_decl)
957     {
958       if (decl)
959         {
960           decl = pushdecl (decl);
961           decl = push_template_decl (decl);
962           HANDLER_PARMS (handler) = decl;
963           type = TREE_TYPE (decl);
964         }
965     }
966   else
967     type = expand_start_catch_block (decl);
968
969   HANDLER_TYPE (handler) = type;
970   if (!processing_template_decl && type)
971     mark_used (eh_type_info (type));
972 }
973
974 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
975    the return value from the matching call to finish_handler_parms.  */
976
977 void
978 finish_handler (tree handler)
979 {
980   if (!processing_template_decl)
981     expand_end_catch_block ();
982   HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
983 }
984
985 /* Begin a compound statement.  FLAGS contains some bits that control the
986    behaviour and context.  If BCS_NO_SCOPE is set, the compound statement
987    does not define a scope.  If BCS_FN_BODY is set, this is the outermost
988    block of a function.  If BCS_TRY_BLOCK is set, this is the block 
989    created on behalf of a TRY statement.  Returns a token to be passed to
990    finish_compound_stmt.  */
991
992 tree
993 begin_compound_stmt (unsigned int flags)
994 {
995   tree r;
996
997   if (flags & BCS_NO_SCOPE)
998     {
999       r = push_stmt_list ();
1000       STATEMENT_LIST_NO_SCOPE (r) = 1;
1001
1002       /* Normally, we try hard to keep the BLOCK for a statement-expression.
1003          But, if it's a statement-expression with a scopeless block, there's
1004          nothing to keep, and we don't want to accidentally keep a block
1005          *inside* the scopeless block.  */ 
1006       keep_next_level (false);
1007     }
1008   else
1009     r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1010
1011   /* When processing a template, we need to remember where the braces were,
1012      so that we can set up identical scopes when instantiating the template
1013      later.  BIND_EXPR is a handy candidate for this.
1014      Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1015      result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1016      processing templates.  */
1017   if (processing_template_decl)
1018     {
1019       r = build (BIND_EXPR, NULL, NULL, r, NULL);
1020       BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1021       BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1022       TREE_SIDE_EFFECTS (r) = 1;
1023     }
1024
1025   return r;
1026 }
1027
1028 /* Finish a compound-statement, which is given by STMT.  */
1029
1030 void
1031 finish_compound_stmt (tree stmt)
1032 {
1033   if (TREE_CODE (stmt) == BIND_EXPR)
1034     BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt));
1035   else if (STATEMENT_LIST_NO_SCOPE (stmt))
1036     stmt = pop_stmt_list (stmt);
1037   else
1038     stmt = do_poplevel (stmt);
1039
1040   /* ??? See c_end_compound_stmt wrt statement expressions.  */
1041   add_stmt (stmt);
1042   finish_stmt ();
1043 }
1044
1045 /* Finish an asm-statement, whose components are a STRING, some
1046    OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS.  Also note
1047    whether the asm-statement should be considered volatile.  */
1048
1049 tree
1050 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1051                  tree input_operands, tree clobbers)
1052 {
1053   tree r;
1054   tree t;
1055
1056   if (!processing_template_decl)
1057     {
1058       int i;
1059       int ninputs;
1060       int noutputs;
1061
1062       for (t = input_operands; t; t = TREE_CHAIN (t))
1063         {
1064           tree converted_operand 
1065             = decay_conversion (TREE_VALUE (t)); 
1066           
1067           /* If the type of the operand hasn't been determined (e.g.,
1068              because it involves an overloaded function), then issue
1069              an error message.  There's no context available to
1070              resolve the overloading.  */
1071           if (TREE_TYPE (converted_operand) == unknown_type_node)
1072             {
1073               error ("type of asm operand `%E' could not be determined", 
1074                         TREE_VALUE (t));
1075               converted_operand = error_mark_node;
1076             }
1077           TREE_VALUE (t) = converted_operand;
1078         }
1079
1080       ninputs = list_length (input_operands);
1081       noutputs = list_length (output_operands);
1082
1083       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1084         {
1085           bool allows_mem;
1086           bool allows_reg;
1087           bool is_inout;
1088           const char *constraint;
1089           tree operand;
1090
1091           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1092           operand = TREE_VALUE (t);
1093
1094           if (!parse_output_constraint (&constraint,
1095                                         i, ninputs, noutputs,
1096                                         &allows_mem,
1097                                         &allows_reg,
1098                                         &is_inout))
1099             {
1100               /* By marking this operand as erroneous, we will not try
1101                  to process this operand again in expand_asm_operands.  */
1102               TREE_VALUE (t) = error_mark_node;
1103               continue;
1104             }
1105
1106           /* If the operand is a DECL that is going to end up in
1107              memory, assume it is addressable.  This is a bit more
1108              conservative than it would ideally be; the exact test is
1109              buried deep in expand_asm_operands and depends on the
1110              DECL_RTL for the OPERAND -- which we don't have at this
1111              point.  */
1112           if (!allows_reg && DECL_P (operand))
1113             cxx_mark_addressable (operand);
1114         }
1115     }
1116
1117   r = build_stmt (ASM_EXPR, string,
1118                   output_operands, input_operands,
1119                   clobbers);
1120   ASM_VOLATILE_P (r) = volatile_p;
1121   return add_stmt (r);
1122 }
1123
1124 /* Finish a label with the indicated NAME.  */
1125
1126 tree
1127 finish_label_stmt (tree name)
1128 {
1129   tree decl = define_label (input_location, name);
1130   return add_stmt (build_stmt (LABEL_EXPR, decl));
1131 }
1132
1133 /* Finish a series of declarations for local labels.  G++ allows users
1134    to declare "local" labels, i.e., labels with scope.  This extension
1135    is useful when writing code involving statement-expressions.  */
1136
1137 void
1138 finish_label_decl (tree name)
1139 {
1140   tree decl = declare_local_label (name);
1141   add_decl_stmt (decl);
1142 }
1143
1144 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1145
1146 void 
1147 finish_decl_cleanup (tree decl, tree cleanup)
1148 {
1149   push_cleanup (decl, cleanup, false);
1150 }
1151
1152 /* If the current scope exits with an exception, run CLEANUP.  */
1153
1154 void
1155 finish_eh_cleanup (tree cleanup)
1156 {
1157   push_cleanup (NULL, cleanup, true);
1158 }
1159
1160 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1161    order they were written by the user.  Each node is as for
1162    emit_mem_initializers.  */
1163
1164 void
1165 finish_mem_initializers (tree mem_inits)
1166 {
1167   /* Reorder the MEM_INITS so that they are in the order they appeared
1168      in the source program.  */
1169   mem_inits = nreverse (mem_inits);
1170
1171   if (processing_template_decl)
1172     add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1173   else
1174     emit_mem_initializers (mem_inits);
1175 }
1176
1177 /* Finish a parenthesized expression EXPR.  */
1178
1179 tree
1180 finish_parenthesized_expr (tree expr)
1181 {
1182   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1183     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1184     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1185
1186   if (TREE_CODE (expr) == OFFSET_REF)
1187     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1188        enclosed in parentheses.  */
1189     PTRMEM_OK_P (expr) = 0;
1190   return expr;
1191 }
1192
1193 /* Finish a reference to a non-static data member (DECL) that is not
1194    preceded by `.' or `->'.  */
1195
1196 tree
1197 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1198 {
1199   my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1200
1201   if (!object)
1202     {
1203       if (current_function_decl 
1204           && DECL_STATIC_FUNCTION_P (current_function_decl))
1205         cp_error_at ("invalid use of member `%D' in static member function",
1206                      decl);
1207       else
1208         cp_error_at ("invalid use of non-static data member `%D'", decl);
1209       error ("from this location");
1210
1211       return error_mark_node;
1212     }
1213   TREE_USED (current_class_ptr) = 1;
1214   if (processing_template_decl && !qualifying_scope)
1215     {
1216       tree type = TREE_TYPE (decl);
1217
1218       if (TREE_CODE (type) == REFERENCE_TYPE)
1219         type = TREE_TYPE (type);
1220       else
1221         {
1222           /* Set the cv qualifiers.  */
1223           int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1224           
1225           if (DECL_MUTABLE_P (decl))
1226             quals &= ~TYPE_QUAL_CONST;
1227
1228           quals |= cp_type_quals (TREE_TYPE (decl));
1229           type = cp_build_qualified_type (type, quals);
1230         }
1231       
1232       return build_min (COMPONENT_REF, type, object, decl);
1233     }
1234   else
1235     {
1236       tree access_type = TREE_TYPE (object);
1237       tree lookup_context = context_for_name_lookup (decl);
1238       
1239       while (!DERIVED_FROM_P (lookup_context, access_type))
1240         {
1241           access_type = TYPE_CONTEXT (access_type);
1242           while (access_type && DECL_P (access_type))
1243             access_type = DECL_CONTEXT (access_type);
1244
1245           if (!access_type)
1246             {
1247               cp_error_at ("object missing in reference to `%D'", decl);
1248               error ("from this location");
1249               return error_mark_node;
1250             }
1251         }
1252
1253       /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1254          QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
1255          for now.  */
1256       if (processing_template_decl)
1257         return build_min (SCOPE_REF, TREE_TYPE (decl),
1258                           qualifying_scope, DECL_NAME (decl));
1259
1260       perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1261
1262       /* If the data member was named `C::M', convert `*this' to `C'
1263          first.  */
1264       if (qualifying_scope)
1265         {
1266           tree binfo = NULL_TREE;
1267           object = build_scoped_ref (object, qualifying_scope,
1268                                      &binfo);
1269         }
1270
1271       return build_class_member_access_expr (object, decl,
1272                                              /*access_path=*/NULL_TREE,
1273                                              /*preserve_reference=*/false);
1274     }
1275 }
1276
1277 /* DECL was the declaration to which a qualified-id resolved.  Issue
1278    an error message if it is not accessible.  If OBJECT_TYPE is
1279    non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1280    type of `*x', or `x', respectively.  If the DECL was named as
1281    `A::B' then NESTED_NAME_SPECIFIER is `A'.  */
1282
1283 void
1284 check_accessibility_of_qualified_id (tree decl, 
1285                                      tree object_type, 
1286                                      tree nested_name_specifier)
1287 {
1288   tree scope;
1289   tree qualifying_type = NULL_TREE;
1290   
1291   /* Determine the SCOPE of DECL.  */
1292   scope = context_for_name_lookup (decl);
1293   /* If the SCOPE is not a type, then DECL is not a member.  */
1294   if (!TYPE_P (scope))
1295     return;
1296   /* Compute the scope through which DECL is being accessed.  */
1297   if (object_type 
1298       /* OBJECT_TYPE might not be a class type; consider:
1299
1300            class A { typedef int I; };
1301            I *p;
1302            p->A::I::~I();
1303
1304          In this case, we will have "A::I" as the DECL, but "I" as the
1305          OBJECT_TYPE.  */
1306       && CLASS_TYPE_P (object_type)
1307       && DERIVED_FROM_P (scope, object_type))
1308     /* If we are processing a `->' or `.' expression, use the type of the
1309        left-hand side.  */
1310     qualifying_type = object_type;
1311   else if (nested_name_specifier)
1312     {
1313       /* If the reference is to a non-static member of the
1314          current class, treat it as if it were referenced through
1315          `this'.  */
1316       if (DECL_NONSTATIC_MEMBER_P (decl)
1317           && current_class_ptr
1318           && DERIVED_FROM_P (scope, current_class_type))
1319         qualifying_type = current_class_type;
1320       /* Otherwise, use the type indicated by the
1321          nested-name-specifier.  */
1322       else
1323         qualifying_type = nested_name_specifier;
1324     }
1325   else
1326     /* Otherwise, the name must be from the current class or one of
1327        its bases.  */
1328     qualifying_type = currently_open_derived_class (scope);
1329
1330   if (qualifying_type)
1331     perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1332 }
1333
1334 /* EXPR is the result of a qualified-id.  The QUALIFYING_CLASS was the
1335    class named to the left of the "::" operator.  DONE is true if this
1336    expression is a complete postfix-expression; it is false if this
1337    expression is followed by '->', '[', '(', etc.  ADDRESS_P is true
1338    iff this expression is the operand of '&'.  */
1339
1340 tree
1341 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1342                           bool address_p)
1343 {
1344   if (error_operand_p (expr))
1345     return error_mark_node;
1346
1347   /* If EXPR occurs as the operand of '&', use special handling that
1348      permits a pointer-to-member.  */
1349   if (address_p && done)
1350     {
1351       if (TREE_CODE (expr) == SCOPE_REF)
1352         expr = TREE_OPERAND (expr, 1);
1353       expr = build_offset_ref (qualifying_class, expr, 
1354                                /*address_p=*/true);
1355       return expr;
1356     }
1357
1358   if (TREE_CODE (expr) == FIELD_DECL)
1359     expr = finish_non_static_data_member (expr, current_class_ref,
1360                                           qualifying_class);
1361   else if (BASELINK_P (expr) && !processing_template_decl)
1362     {
1363       tree fn;
1364       tree fns;
1365
1366       /* See if any of the functions are non-static members.  */
1367       fns = BASELINK_FUNCTIONS (expr);
1368       if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1369         fns = TREE_OPERAND (fns, 0);
1370       for (fn = fns; fn; fn = OVL_NEXT (fn))
1371         if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1372           break;
1373       /* If so, the expression may be relative to the current
1374          class.  */
1375       if (fn && current_class_type 
1376           && DERIVED_FROM_P (qualifying_class, current_class_type))
1377         expr = (build_class_member_access_expr 
1378                 (maybe_dummy_object (qualifying_class, NULL),
1379                  expr,
1380                  BASELINK_ACCESS_BINFO (expr),
1381                  /*preserve_reference=*/false));
1382       else if (done)
1383         /* The expression is a qualified name whose address is not
1384            being taken.  */
1385         expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1386     }
1387
1388   return expr;
1389 }
1390
1391 /* Begin a statement-expression.  The value returned must be passed to
1392    finish_stmt_expr.  */
1393
1394 tree 
1395 begin_stmt_expr (void)
1396 {
1397   return push_stmt_list ();
1398 }
1399
1400 /* Process the final expression of a statement expression. EXPR can be
1401    NULL, if the final expression is empty.  Build up a TARGET_EXPR so
1402    that the result value can be safely returned to the enclosing
1403    expression.  */
1404
1405 tree
1406 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1407 {
1408   tree result = NULL_TREE;
1409
1410   if (expr)
1411     {
1412       if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1413         {
1414           tree type = TREE_TYPE (expr);
1415
1416           if (TREE_CODE (type) == ARRAY_TYPE
1417               || TREE_CODE (type) == FUNCTION_TYPE)
1418             expr = decay_conversion (expr);
1419
1420           expr = convert_from_reference (expr);
1421           expr = require_complete_type (expr);
1422
1423           type = TREE_TYPE (expr);
1424
1425           /* Build a TARGET_EXPR for this aggregate.  finish_stmt_expr
1426              will then pull it apart so the lifetime of the target is
1427              within the scope of the expression containing this statement
1428              expression.  */
1429           if (TREE_CODE (expr) == TARGET_EXPR)
1430             ;
1431           else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1432             expr = build_target_expr_with_type (expr, type);
1433           else
1434             {
1435               /* Copy construct.  */
1436               expr = build_special_member_call
1437                 (NULL_TREE, complete_ctor_identifier,
1438                  build_tree_list (NULL_TREE, expr),
1439                  TYPE_BINFO (type), LOOKUP_NORMAL);
1440               expr = build_cplus_new (type, expr);
1441               my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1442             }
1443         }
1444
1445       if (expr != error_mark_node)
1446         {
1447           result = build_stmt (EXPR_STMT, expr);
1448           EXPR_STMT_STMT_EXPR_RESULT (result) = 1;
1449           add_stmt (result);
1450         }
1451     }
1452   
1453   finish_stmt ();
1454
1455   /* Remember the last expression so that finish_stmt_expr
1456      can pull it apart.  */
1457   TREE_TYPE (stmt_expr) = result;
1458   
1459   return result;
1460 }
1461
1462 /* Finish a statement-expression.  EXPR should be the value returned
1463    by the previous begin_stmt_expr.  Returns an expression
1464    representing the statement-expression.  */
1465
1466 tree 
1467 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1468 {
1469   tree result, result_stmt, type;
1470   tree *result_stmt_p = NULL;
1471
1472   result_stmt = TREE_TYPE (stmt_expr);
1473   TREE_TYPE (stmt_expr) = void_type_node;
1474   result = pop_stmt_list (stmt_expr);
1475
1476   if (!result_stmt || VOID_TYPE_P (result_stmt))
1477     type = void_type_node;
1478   else
1479     {
1480       /* We need to search the statement expression for the result_stmt,
1481          since we'll need to replace it entirely.  */
1482       tree t;
1483       result_stmt_p = &result;
1484       while (1)
1485         {
1486           t = *result_stmt_p;
1487           if (t == result_stmt)
1488             break;
1489
1490           switch (TREE_CODE (t))
1491             {
1492             case STATEMENT_LIST:
1493               {
1494                 tree_stmt_iterator i = tsi_last (t);
1495                 result_stmt_p = tsi_stmt_ptr (i);
1496                 break;
1497               }
1498             case BIND_EXPR:
1499               result_stmt_p = &BIND_EXPR_BODY (t);
1500               break;
1501             case TRY_FINALLY_EXPR:
1502             case TRY_CATCH_EXPR:
1503             case CLEANUP_STMT:
1504               result_stmt_p = &TREE_OPERAND (t, 0);
1505               break;
1506             default:
1507               abort ();
1508             }
1509         }
1510       type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1511     }
1512
1513   if (processing_template_decl)
1514     {
1515       result = build_min (STMT_EXPR, type, result);
1516       TREE_SIDE_EFFECTS (result) = 1;
1517       STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1518     }
1519   else if (!VOID_TYPE_P (type))
1520     {
1521       /* Pull out the TARGET_EXPR that is the final expression. Put
1522          the target's init_expr as the final expression and then put
1523          the statement expression itself as the target's init
1524          expr. Finally, return the target expression.  */
1525       tree init, target_expr = EXPR_STMT_EXPR (result_stmt);
1526       my_friendly_assert (TREE_CODE (target_expr) == TARGET_EXPR, 20030729);
1527
1528       /* The initializer will be void if the initialization is done by
1529          AGGR_INIT_EXPR; propagate that out to the statement-expression as
1530          a whole.  */
1531       init = TREE_OPERAND (target_expr, 1);
1532       type = TREE_TYPE (init);
1533
1534       init = maybe_cleanup_point_expr (init);
1535       *result_stmt_p = init;
1536
1537       if (VOID_TYPE_P (type))
1538         /* No frobbing needed.  */;
1539       else if (TREE_CODE (result) == BIND_EXPR)
1540         {
1541           /* The BIND_EXPR created in finish_compound_stmt is void; if we're
1542              returning a value directly, give it the appropriate type.  */
1543           if (VOID_TYPE_P (TREE_TYPE (result)))
1544             TREE_TYPE (result) = type;
1545           else if (same_type_p (TREE_TYPE (result), type))
1546             ;
1547           else
1548             abort ();
1549         }
1550       else if (TREE_CODE (result) == STATEMENT_LIST)
1551         /* We need to wrap a STATEMENT_LIST in a BIND_EXPR so it can have a
1552            type other than void.  FIXME why can't we just return a value
1553            from STATEMENT_LIST?  */
1554         result = build3 (BIND_EXPR, type, NULL, result, NULL);
1555
1556       TREE_OPERAND (target_expr, 1) = result;
1557       result = target_expr;
1558     }
1559
1560   return result;
1561 }
1562
1563 /* Perform Koenig lookup.  FN is the postfix-expression representing
1564    the function (or functions) to call; ARGS are the arguments to the
1565    call.  Returns the functions to be considered by overload
1566    resolution.  */
1567
1568 tree
1569 perform_koenig_lookup (tree fn, tree args)
1570 {
1571   tree identifier = NULL_TREE;
1572   tree functions = NULL_TREE;
1573
1574   /* Find the name of the overloaded function.  */
1575   if (TREE_CODE (fn) == IDENTIFIER_NODE)
1576     identifier = fn;
1577   else if (is_overloaded_fn (fn))
1578     {
1579       functions = fn;
1580       identifier = DECL_NAME (get_first_fn (functions));
1581     }
1582   else if (DECL_P (fn))
1583     {
1584       functions = fn;
1585       identifier = DECL_NAME (fn);
1586     }
1587
1588   /* A call to a namespace-scope function using an unqualified name.
1589
1590      Do Koenig lookup -- unless any of the arguments are
1591      type-dependent.  */
1592   if (!any_type_dependent_arguments_p (args))
1593     {
1594       fn = lookup_arg_dependent (identifier, functions, args);
1595       if (!fn)
1596         /* The unqualified name could not be resolved.  */
1597         fn = unqualified_fn_lookup_error (identifier);
1598     }
1599   else
1600     fn = identifier;
1601
1602   return fn;
1603 }
1604
1605 /* Generate an expression for `FN (ARGS)'.
1606
1607    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1608    as a virtual call, even if FN is virtual.  (This flag is set when
1609    encountering an expression where the function name is explicitly
1610    qualified.  For example a call to `X::f' never generates a virtual
1611    call.)
1612
1613    Returns code for the call.  */
1614
1615 tree 
1616 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1617 {
1618   tree result;
1619   tree orig_fn;
1620   tree orig_args;
1621
1622   if (fn == error_mark_node || args == error_mark_node)
1623     return error_mark_node;
1624
1625   /* ARGS should be a list of arguments.  */
1626   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1627                       20020712);
1628
1629   orig_fn = fn;
1630   orig_args = args;
1631
1632   if (processing_template_decl)
1633     {
1634       if (type_dependent_expression_p (fn)
1635           || any_type_dependent_arguments_p (args))
1636         {
1637           result = build_nt (CALL_EXPR, fn, args, NULL_TREE);
1638           KOENIG_LOOKUP_P (result) = koenig_p;
1639           return result;
1640         }
1641       if (!BASELINK_P (fn)
1642           && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1643           && TREE_TYPE (fn) != unknown_type_node)
1644         fn = build_non_dependent_expr (fn);
1645       args = build_non_dependent_args (orig_args);
1646     }
1647
1648   /* A reference to a member function will appear as an overloaded
1649      function (rather than a BASELINK) if an unqualified name was used
1650      to refer to it.  */
1651   if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1652     {
1653       tree f = fn;
1654
1655       if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1656         f = TREE_OPERAND (f, 0);
1657       f = get_first_fn (f);
1658       if (DECL_FUNCTION_MEMBER_P (f))
1659         {
1660           tree type = currently_open_derived_class (DECL_CONTEXT (f));
1661           if (!type)
1662             type = DECL_CONTEXT (f);
1663           fn = build_baselink (TYPE_BINFO (type),
1664                                TYPE_BINFO (type),
1665                                fn, /*optype=*/NULL_TREE);
1666         }
1667     }
1668
1669   result = NULL_TREE;
1670   if (BASELINK_P (fn))
1671     {
1672       tree object;
1673
1674       /* A call to a member function.  From [over.call.func]:
1675
1676            If the keyword this is in scope and refers to the class of
1677            that member function, or a derived class thereof, then the
1678            function call is transformed into a qualified function call
1679            using (*this) as the postfix-expression to the left of the
1680            . operator.... [Otherwise] a contrived object of type T
1681            becomes the implied object argument.  
1682
1683         This paragraph is unclear about this situation:
1684
1685           struct A { void f(); };
1686           struct B : public A {};
1687           struct C : public A { void g() { B::f(); }};
1688
1689         In particular, for `B::f', this paragraph does not make clear
1690         whether "the class of that member function" refers to `A' or 
1691         to `B'.  We believe it refers to `B'.  */
1692       if (current_class_type 
1693           && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1694                              current_class_type)
1695           && current_class_ref)
1696         object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1697                                      NULL);
1698       else
1699         {
1700           tree representative_fn;
1701
1702           representative_fn = BASELINK_FUNCTIONS (fn);
1703           if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1704             representative_fn = TREE_OPERAND (representative_fn, 0);
1705           representative_fn = get_first_fn (representative_fn);
1706           object = build_dummy_object (DECL_CONTEXT (representative_fn));
1707         }
1708
1709       if (processing_template_decl)
1710         {
1711           if (type_dependent_expression_p (object))
1712             return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE);
1713           object = build_non_dependent_expr (object);
1714         }
1715
1716       result = build_new_method_call (object, fn, args, NULL_TREE,
1717                                       (disallow_virtual 
1718                                        ? LOOKUP_NONVIRTUAL : 0));
1719     }
1720   else if (is_overloaded_fn (fn))
1721     /* A call to a namespace-scope function.  */
1722     result = build_new_function_call (fn, args);
1723   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1724     {
1725       if (args)
1726         error ("arguments to destructor are not allowed");
1727       /* Mark the pseudo-destructor call as having side-effects so
1728          that we do not issue warnings about its use.  */
1729       result = build1 (NOP_EXPR,
1730                        void_type_node,
1731                        TREE_OPERAND (fn, 0));
1732       TREE_SIDE_EFFECTS (result) = 1;
1733     }
1734   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1735     /* If the "function" is really an object of class type, it might
1736        have an overloaded `operator ()'.  */
1737     result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1738                            /*overloaded_p=*/NULL);
1739   if (!result)
1740     /* A call where the function is unknown.  */
1741     result = build_function_call (fn, args);
1742
1743   if (processing_template_decl)
1744     {
1745       result = build (CALL_EXPR, TREE_TYPE (result), orig_fn,
1746                       orig_args, NULL_TREE);
1747       KOENIG_LOOKUP_P (result) = koenig_p;
1748     }
1749   return result;
1750 }
1751
1752 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1753    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1754    POSTDECREMENT_EXPR.)  */
1755
1756 tree 
1757 finish_increment_expr (tree expr, enum tree_code code)
1758 {
1759   return build_x_unary_op (code, expr);  
1760 }
1761
1762 /* Finish a use of `this'.  Returns an expression for `this'.  */
1763
1764 tree 
1765 finish_this_expr (void)
1766 {
1767   tree result;
1768
1769   if (current_class_ptr)
1770     {
1771       result = current_class_ptr;
1772     }
1773   else if (current_function_decl
1774            && DECL_STATIC_FUNCTION_P (current_function_decl))
1775     {
1776       error ("`this' is unavailable for static member functions");
1777       result = error_mark_node;
1778     }
1779   else
1780     {
1781       if (current_function_decl)
1782         error ("invalid use of `this' in non-member function");
1783       else
1784         error ("invalid use of `this' at top level");
1785       result = error_mark_node;
1786     }
1787
1788   return result;
1789 }
1790
1791 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
1792    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1793    the TYPE for the type given.  If SCOPE is non-NULL, the expression
1794    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
1795
1796 tree 
1797 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1798 {
1799   if (destructor == error_mark_node)
1800     return error_mark_node;
1801
1802   my_friendly_assert (TYPE_P (destructor), 20010905);
1803
1804   if (!processing_template_decl)
1805     {
1806       if (scope == error_mark_node)
1807         {
1808           error ("invalid qualifying scope in pseudo-destructor name");
1809           return error_mark_node;
1810         }
1811       
1812       /* [expr.pseudo] says both:
1813
1814            The type designated by the pseudo-destructor-name shall be
1815            the same as the object type.
1816
1817          and:
1818
1819            The cv-unqualified versions of the object type and of the
1820            type designated by the pseudo-destructor-name shall be the
1821            same type.
1822
1823          We implement the more generous second sentence, since that is
1824          what most other compilers do.  */
1825       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), 
1826                                                       destructor))
1827         {
1828           error ("`%E' is not of type `%T'", object, destructor);
1829           return error_mark_node;
1830         }
1831     }
1832
1833   return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1834 }
1835
1836 /* Finish an expression of the form CODE EXPR.  */
1837
1838 tree
1839 finish_unary_op_expr (enum tree_code code, tree expr)
1840 {
1841   tree result = build_x_unary_op (code, expr);
1842   /* Inside a template, build_x_unary_op does not fold the
1843      expression. So check whether the result is folded before
1844      setting TREE_NEGATED_INT.  */
1845   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1846       && TREE_CODE (result) == INTEGER_CST
1847       && !TYPE_UNSIGNED (TREE_TYPE (result))
1848       && INT_CST_LT (result, integer_zero_node))
1849     TREE_NEGATED_INT (result) = 1;
1850   overflow_warning (result);
1851   return result;
1852 }
1853
1854 /* Finish a compound-literal expression.  TYPE is the type to which
1855    the INITIALIZER_LIST is being cast.  */
1856
1857 tree
1858 finish_compound_literal (tree type, tree initializer_list)
1859 {
1860   tree compound_literal;
1861
1862   /* Build a CONSTRUCTOR for the INITIALIZER_LIST.  */
1863   compound_literal = build_constructor (NULL_TREE, initializer_list);
1864   /* Mark it as a compound-literal.  */
1865   TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1866   if (processing_template_decl)
1867     TREE_TYPE (compound_literal) = type;
1868   else
1869     {
1870       /* Check the initialization.  */
1871       compound_literal = digest_init (type, compound_literal, NULL);
1872       /* If the TYPE was an array type with an unknown bound, then we can
1873          figure out the dimension now.  For example, something like:
1874
1875            `(int []) { 2, 3 }'
1876
1877          implies that the array has two elements.  */
1878       if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1879         complete_array_type (type, compound_literal, 1);
1880     }
1881
1882   return compound_literal;
1883 }
1884
1885 /* Return the declaration for the function-name variable indicated by
1886    ID.  */
1887
1888 tree
1889 finish_fname (tree id)
1890 {
1891   tree decl;
1892   
1893   decl = fname_decl (C_RID_CODE (id), id);
1894   if (processing_template_decl)
1895     decl = DECL_NAME (decl);
1896   return decl;
1897 }
1898
1899 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1900    and DECLARATOR.  Returns nonzero if the function-declaration is
1901    valid.  */
1902
1903 int
1904 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1905 {
1906   if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1907     return 0;
1908
1909   /* The things we're about to see are not directly qualified by any
1910      template headers we've seen thus far.  */
1911   reset_specialization ();
1912
1913   return 1;
1914 }
1915
1916 /* Finish a translation unit.  */
1917
1918 void 
1919 finish_translation_unit (void)
1920 {
1921   /* In case there were missing closebraces,
1922      get us back to the global binding level.  */
1923   pop_everything ();
1924   while (current_namespace != global_namespace)
1925     pop_namespace ();
1926
1927   /* Do file scope __FUNCTION__ et al.  */
1928   finish_fname_decls ();
1929 }
1930
1931 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1932    Returns the parameter.  */
1933
1934 tree 
1935 finish_template_type_parm (tree aggr, tree identifier)
1936 {
1937   if (aggr != class_type_node)
1938     {
1939       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1940       aggr = class_type_node;
1941     }
1942
1943   return build_tree_list (aggr, identifier);
1944 }
1945
1946 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1947    Returns the parameter.  */
1948
1949 tree 
1950 finish_template_template_parm (tree aggr, tree identifier)
1951 {
1952   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1953   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1954   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1955   DECL_TEMPLATE_RESULT (tmpl) = decl;
1956   DECL_ARTIFICIAL (decl) = 1;
1957   end_template_decl ();
1958
1959   my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1960
1961   return finish_template_type_parm (aggr, tmpl);
1962 }
1963
1964 /* ARGUMENT is the default-argument value for a template template
1965    parameter.  If ARGUMENT is invalid, issue error messages and return
1966    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
1967
1968 tree
1969 check_template_template_default_arg (tree argument)
1970 {
1971   if (TREE_CODE (argument) != TEMPLATE_DECL
1972       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1973       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1974     {
1975       if (TREE_CODE (argument) == TYPE_DECL)
1976         {
1977           tree t = TREE_TYPE (argument);
1978
1979           /* Try to emit a slightly smarter error message if we detect
1980              that the user is using a template instantiation.  */
1981           if (CLASSTYPE_TEMPLATE_INFO (t) 
1982               && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1983             error ("invalid use of type `%T' as a default value for a "
1984                    "template template-parameter", t);
1985           else
1986             error ("invalid use of `%D' as a default value for a template "
1987                    "template-parameter", argument);
1988         }
1989       else
1990         error ("invalid default argument for a template template parameter");
1991       return error_mark_node;
1992     }
1993
1994   return argument;
1995 }
1996
1997 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1998    nonzero, the parameter list was terminated by a `...'.  */
1999
2000 tree
2001 finish_parmlist (tree parms, int ellipsis)
2002 {
2003   if (parms)
2004     {
2005       /* We mark the PARMS as a parmlist so that declarator processing can
2006          disambiguate certain constructs.  */
2007       TREE_PARMLIST (parms) = 1;
2008       /* We do not append void_list_node here, but leave it to grokparms
2009          to do that.  */
2010       PARMLIST_ELLIPSIS_P (parms) = ellipsis;
2011     }
2012   return parms;
2013 }
2014
2015 /* Begin a class definition, as indicated by T.  */
2016
2017 tree
2018 begin_class_definition (tree t)
2019 {
2020   if (t == error_mark_node)
2021     return error_mark_node;
2022
2023   if (processing_template_parmlist)
2024     {
2025       error ("definition of `%#T' inside template parameter list", t);
2026       return error_mark_node;
2027     }
2028   /* A non-implicit typename comes from code like:
2029
2030        template <typename T> struct A {
2031          template <typename U> struct A<T>::B ...
2032
2033      This is erroneous.  */
2034   else if (TREE_CODE (t) == TYPENAME_TYPE)
2035     {
2036       error ("invalid definition of qualified type `%T'", t);
2037       t = error_mark_node;
2038     }
2039
2040   if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2041     {
2042       t = make_aggr_type (RECORD_TYPE);
2043       pushtag (make_anon_name (), t, 0);
2044     }
2045
2046   /* If this type was already complete, and we see another definition,
2047      that's an error.  */
2048   if (COMPLETE_TYPE_P (t))
2049     {
2050       error ("redefinition of `%#T'", t);
2051       cp_error_at ("previous definition of `%#T'", t);
2052       return error_mark_node;
2053     }
2054
2055   /* Update the location of the decl.  */
2056   DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2057   
2058   if (TYPE_BEING_DEFINED (t))
2059     {
2060       t = make_aggr_type (TREE_CODE (t));
2061       pushtag (TYPE_IDENTIFIER (t), t, 0);
2062     }
2063   maybe_process_partial_specialization (t);
2064   pushclass (t);
2065   TYPE_BEING_DEFINED (t) = 1;
2066   if (flag_pack_struct)
2067     {
2068       tree v;
2069       TYPE_PACKED (t) = 1;
2070       /* Even though the type is being defined for the first time
2071          here, there might have been a forward declaration, so there
2072          might be cv-qualified variants of T.  */
2073       for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2074         TYPE_PACKED (v) = 1;
2075     }
2076   /* Reset the interface data, at the earliest possible
2077      moment, as it might have been set via a class foo;
2078      before.  */
2079   if (! TYPE_ANONYMOUS_P (t))
2080     {
2081       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2082       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2083         (t, interface_unknown);
2084     }
2085   reset_specialization();
2086   
2087   /* Make a declaration for this class in its own scope.  */
2088   build_self_reference ();
2089
2090   return t;
2091 }
2092
2093 /* Finish the member declaration given by DECL.  */
2094
2095 void
2096 finish_member_declaration (tree decl)
2097 {
2098   if (decl == error_mark_node || decl == NULL_TREE)
2099     return;
2100
2101   if (decl == void_type_node)
2102     /* The COMPONENT was a friend, not a member, and so there's
2103        nothing for us to do.  */
2104     return;
2105
2106   /* We should see only one DECL at a time.  */
2107   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2108
2109   /* Set up access control for DECL.  */
2110   TREE_PRIVATE (decl) 
2111     = (current_access_specifier == access_private_node);
2112   TREE_PROTECTED (decl) 
2113     = (current_access_specifier == access_protected_node);
2114   if (TREE_CODE (decl) == TEMPLATE_DECL)
2115     {
2116       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2117       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2118     }
2119
2120   /* Mark the DECL as a member of the current class.  */
2121   DECL_CONTEXT (decl) = current_class_type;
2122
2123   /* [dcl.link]
2124
2125      A C language linkage is ignored for the names of class members
2126      and the member function type of class member functions.  */
2127   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2128     SET_DECL_LANGUAGE (decl, lang_cplusplus);
2129
2130   /* Put functions on the TYPE_METHODS list and everything else on the
2131      TYPE_FIELDS list.  Note that these are built up in reverse order.
2132      We reverse them (to obtain declaration order) in finish_struct.  */
2133   if (TREE_CODE (decl) == FUNCTION_DECL 
2134       || DECL_FUNCTION_TEMPLATE_P (decl))
2135     {
2136       /* We also need to add this function to the
2137          CLASSTYPE_METHOD_VEC.  */
2138       add_method (current_class_type, decl, /*error_p=*/0);
2139
2140       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2141       TYPE_METHODS (current_class_type) = decl;
2142
2143       maybe_add_class_template_decl_list (current_class_type, decl, 
2144                                           /*friend_p=*/0);
2145     }
2146   /* Enter the DECL into the scope of the class.  */
2147   else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2148            || pushdecl_class_level (decl))
2149     {
2150       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2151          go at the beginning.  The reason is that lookup_field_1
2152          searches the list in order, and we want a field name to
2153          override a type name so that the "struct stat hack" will
2154          work.  In particular:
2155
2156            struct S { enum E { }; int E } s;
2157            s.E = 3;
2158
2159          is valid.  In addition, the FIELD_DECLs must be maintained in
2160          declaration order so that class layout works as expected.
2161          However, we don't need that order until class layout, so we
2162          save a little time by putting FIELD_DECLs on in reverse order
2163          here, and then reversing them in finish_struct_1.  (We could
2164          also keep a pointer to the correct insertion points in the
2165          list.)  */
2166
2167       if (TREE_CODE (decl) == TYPE_DECL)
2168         TYPE_FIELDS (current_class_type) 
2169           = chainon (TYPE_FIELDS (current_class_type), decl);
2170       else
2171         {
2172           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2173           TYPE_FIELDS (current_class_type) = decl;
2174         }
2175
2176       maybe_add_class_template_decl_list (current_class_type, decl, 
2177                                           /*friend_p=*/0);
2178     }
2179 }
2180
2181 /* Finish processing the declaration of a member class template
2182    TYPES whose template parameters are given by PARMS.  */
2183
2184 tree
2185 finish_member_class_template (tree types)
2186 {
2187   tree t;
2188
2189   /* If there are declared, but undefined, partial specializations
2190      mixed in with the typespecs they will not yet have passed through
2191      maybe_process_partial_specialization, so we do that here.  */
2192   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2193     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2194       maybe_process_partial_specialization (TREE_VALUE (t));
2195
2196   grok_x_components (types);
2197   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2198     /* The component was in fact a friend declaration.  We avoid
2199        finish_member_template_decl performing certain checks by
2200        unsetting TYPES.  */
2201     types = NULL_TREE;
2202   
2203   finish_member_template_decl (types);
2204
2205   /* As with other component type declarations, we do
2206      not store the new DECL on the list of
2207      component_decls.  */
2208   return NULL_TREE;
2209 }
2210
2211 /* Finish processing a complete template declaration.  The PARMS are
2212    the template parameters.  */
2213
2214 void
2215 finish_template_decl (tree parms)
2216 {
2217   if (parms)
2218     end_template_decl ();
2219   else
2220     end_specialization ();
2221 }
2222
2223 /* Finish processing a template-id (which names a type) of the form
2224    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2225    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
2226    the scope of template-id indicated.  */
2227
2228 tree
2229 finish_template_type (tree name, tree args, int entering_scope)
2230 {
2231   tree decl;
2232
2233   decl = lookup_template_class (name, args,
2234                                 NULL_TREE, NULL_TREE, entering_scope,
2235                                 tf_error | tf_warning | tf_user);
2236   if (decl != error_mark_node)
2237     decl = TYPE_STUB_DECL (decl);
2238
2239   return decl;
2240 }
2241
2242 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2243    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2244    BASE_CLASS, or NULL_TREE if an error occurred.  The
2245    ACCESS_SPECIFIER is one of
2246    access_{default,public,protected_private}[_virtual]_node.*/
2247
2248 tree 
2249 finish_base_specifier (tree base, tree access, bool virtual_p)
2250 {
2251   tree result;
2252
2253   if (base == error_mark_node)
2254     {
2255       error ("invalid base-class specification");
2256       result = NULL_TREE;
2257     }
2258   else if (! is_aggr_type (base, 1))
2259     result = NULL_TREE;
2260   else
2261     {
2262       if (cp_type_quals (base) != 0)
2263         {
2264           error ("base class `%T' has cv qualifiers", base);
2265           base = TYPE_MAIN_VARIANT (base);
2266         }
2267       result = build_tree_list (access, base);
2268       TREE_VIA_VIRTUAL (result) = virtual_p;
2269     }
2270
2271   return result;
2272 }
2273
2274 /* Called when multiple declarators are processed.  If that is not
2275    permitted in this context, an error is issued.  */
2276
2277 void
2278 check_multiple_declarators (void)
2279 {
2280   /* [temp]
2281      
2282      In a template-declaration, explicit specialization, or explicit
2283      instantiation the init-declarator-list in the declaration shall
2284      contain at most one declarator.  
2285
2286      We don't just use PROCESSING_TEMPLATE_DECL for the first
2287      condition since that would disallow the perfectly valid code, 
2288      like `template <class T> struct S { int i, j; };'.  */
2289   if (at_function_scope_p ())
2290     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2291     return;
2292      
2293   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2294       || processing_explicit_instantiation
2295       || processing_specialization)
2296     error ("multiple declarators in template declaration");
2297 }
2298
2299 /* Issue a diagnostic that NAME cannot be found in SCOPE.  */
2300
2301 void
2302 qualified_name_lookup_error (tree scope, tree name)
2303 {
2304   if (TYPE_P (scope))
2305     {
2306       if (!COMPLETE_TYPE_P (scope))
2307         error ("incomplete type `%T' used in nested name specifier", scope);
2308       else
2309         error ("`%D' is not a member of `%T'", name, scope);
2310     }
2311   else if (scope != global_namespace)
2312     error ("`%D' is not a member of `%D'", name, scope);
2313   else
2314     error ("`::%D' has not been declared", name);
2315 }
2316               
2317 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2318    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
2319    if non-NULL, is the type or namespace used to explicitly qualify
2320    ID_EXPRESSION.  DECL is the entity to which that name has been
2321    resolved.  
2322
2323    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2324    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
2325    be set to true if this expression isn't permitted in a
2326    constant-expression, but it is otherwise not set by this function.
2327    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2328    constant-expression, but a non-constant expression is also
2329    permissible.
2330
2331    If an error occurs, and it is the kind of error that might cause
2332    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
2333    is the caller's responsibility to issue the message.  *ERROR_MSG
2334    will be a string with static storage duration, so the caller need
2335    not "free" it.
2336
2337    Return an expression for the entity, after issuing appropriate
2338    diagnostics.  This function is also responsible for transforming a
2339    reference to a non-static member into a COMPONENT_REF that makes
2340    the use of "this" explicit.  
2341
2342    Upon return, *IDK will be filled in appropriately.  */
2343
2344 tree
2345 finish_id_expression (tree id_expression, 
2346                       tree decl,
2347                       tree scope,
2348                       cp_id_kind *idk,
2349                       tree *qualifying_class,
2350                       bool integral_constant_expression_p,
2351                       bool allow_non_integral_constant_expression_p,
2352                       bool *non_integral_constant_expression_p,
2353                       const char **error_msg)
2354 {
2355   /* Initialize the output parameters.  */
2356   *idk = CP_ID_KIND_NONE;
2357   *error_msg = NULL;
2358
2359   if (id_expression == error_mark_node)
2360     return error_mark_node;
2361   /* If we have a template-id, then no further lookup is
2362      required.  If the template-id was for a template-class, we
2363      will sometimes have a TYPE_DECL at this point.  */
2364   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2365            || TREE_CODE (decl) == TYPE_DECL)
2366     ;
2367   /* Look up the name.  */
2368   else 
2369     {
2370       if (decl == error_mark_node)
2371         {
2372           /* Name lookup failed.  */
2373           if (scope 
2374               && (!TYPE_P (scope) 
2375                   || (!dependent_type_p (scope)
2376                       && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2377                            && IDENTIFIER_TYPENAME_P (id_expression)
2378                            && dependent_type_p (TREE_TYPE (id_expression))))))
2379             {
2380               /* If the qualifying type is non-dependent (and the name
2381                  does not name a conversion operator to a dependent
2382                  type), issue an error.  */
2383               qualified_name_lookup_error (scope, id_expression);
2384               return error_mark_node;
2385             }
2386           else if (!scope)
2387             {
2388               /* It may be resolved via Koenig lookup.  */
2389               *idk = CP_ID_KIND_UNQUALIFIED;
2390               return id_expression;
2391             }
2392           else
2393             decl = id_expression;
2394         }
2395       /* If DECL is a variable that would be out of scope under
2396          ANSI/ISO rules, but in scope in the ARM, name lookup
2397          will succeed.  Issue a diagnostic here.  */
2398       else
2399         decl = check_for_out_of_scope_variable (decl);
2400
2401       /* Remember that the name was used in the definition of
2402          the current class so that we can check later to see if
2403          the meaning would have been different after the class
2404          was entirely defined.  */
2405       if (!scope && decl != error_mark_node)
2406         maybe_note_name_used_in_class (id_expression, decl);
2407     }
2408
2409   /* If we didn't find anything, or what we found was a type,
2410      then this wasn't really an id-expression.  */
2411   if (TREE_CODE (decl) == TEMPLATE_DECL
2412       && !DECL_FUNCTION_TEMPLATE_P (decl))
2413     {
2414       *error_msg = "missing template arguments";
2415       return error_mark_node;
2416     }
2417   else if (TREE_CODE (decl) == TYPE_DECL
2418            || TREE_CODE (decl) == NAMESPACE_DECL)
2419     {
2420       *error_msg = "expected primary-expression";
2421       return error_mark_node;
2422     }
2423
2424   /* If the name resolved to a template parameter, there is no
2425      need to look it up again later.  */
2426   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2427       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2428     {
2429       *idk = CP_ID_KIND_NONE;
2430       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2431         decl = TEMPLATE_PARM_DECL (decl);
2432       if (integral_constant_expression_p 
2433           && !dependent_type_p (TREE_TYPE (decl))
2434           && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))) 
2435         {
2436           if (!allow_non_integral_constant_expression_p)
2437             error ("template parameter `%D' of type `%T' is not allowed in "
2438                    "an integral constant expression because it is not of "
2439                    "integral or enumeration type", decl, TREE_TYPE (decl));
2440           *non_integral_constant_expression_p = true;
2441         }
2442       return DECL_INITIAL (decl);
2443     }
2444   /* Similarly, we resolve enumeration constants to their 
2445      underlying values.  */
2446   else if (TREE_CODE (decl) == CONST_DECL)
2447     {
2448       *idk = CP_ID_KIND_NONE;
2449       if (!processing_template_decl)
2450         return DECL_INITIAL (decl);
2451       return decl;
2452     }
2453   else
2454     {
2455       bool dependent_p;
2456
2457       /* If the declaration was explicitly qualified indicate
2458          that.  The semantics of `A::f(3)' are different than
2459          `f(3)' if `f' is virtual.  */
2460       *idk = (scope 
2461               ? CP_ID_KIND_QUALIFIED
2462               : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2463                  ? CP_ID_KIND_TEMPLATE_ID
2464                  : CP_ID_KIND_UNQUALIFIED));
2465
2466
2467       /* [temp.dep.expr]
2468
2469          An id-expression is type-dependent if it contains an
2470          identifier that was declared with a dependent type.
2471
2472          The standard is not very specific about an id-expression that
2473          names a set of overloaded functions.  What if some of them
2474          have dependent types and some of them do not?  Presumably,
2475          such a name should be treated as a dependent name.  */
2476       /* Assume the name is not dependent.  */
2477       dependent_p = false;
2478       if (!processing_template_decl)
2479         /* No names are dependent outside a template.  */
2480         ;
2481       /* A template-id where the name of the template was not resolved
2482          is definitely dependent.  */
2483       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2484                && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2485                    == IDENTIFIER_NODE))
2486         dependent_p = true;
2487       /* For anything except an overloaded function, just check its
2488          type.  */
2489       else if (!is_overloaded_fn (decl))
2490         dependent_p 
2491           = dependent_type_p (TREE_TYPE (decl));
2492       /* For a set of overloaded functions, check each of the
2493          functions.  */
2494       else
2495         {
2496           tree fns = decl;
2497
2498           if (BASELINK_P (fns))
2499             fns = BASELINK_FUNCTIONS (fns);
2500
2501           /* For a template-id, check to see if the template
2502              arguments are dependent.  */
2503           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2504             {
2505               tree args = TREE_OPERAND (fns, 1);
2506               dependent_p = any_dependent_template_arguments_p (args);
2507               /* The functions are those referred to by the
2508                  template-id.  */
2509               fns = TREE_OPERAND (fns, 0);
2510             }
2511
2512           /* If there are no dependent template arguments, go through
2513              the overloaded functions.  */
2514           while (fns && !dependent_p)
2515             {
2516               tree fn = OVL_CURRENT (fns);
2517
2518               /* Member functions of dependent classes are
2519                  dependent.  */
2520               if (TREE_CODE (fn) == FUNCTION_DECL
2521                   && type_dependent_expression_p (fn))
2522                 dependent_p = true;
2523               else if (TREE_CODE (fn) == TEMPLATE_DECL
2524                        && dependent_template_p (fn))
2525                 dependent_p = true;
2526
2527               fns = OVL_NEXT (fns);
2528             }
2529         }
2530
2531       /* If the name was dependent on a template parameter, we will
2532          resolve the name at instantiation time.  */
2533       if (dependent_p)
2534         {
2535           /* Create a SCOPE_REF for qualified names, if the scope is
2536              dependent.  */
2537           if (scope)
2538             {
2539               if (TYPE_P (scope))
2540                 *qualifying_class = scope;
2541               /* Since this name was dependent, the expression isn't
2542                  constant -- yet.  No error is issued because it might
2543                  be constant when things are instantiated.  */
2544               if (integral_constant_expression_p)
2545                 *non_integral_constant_expression_p = true;
2546               if (TYPE_P (scope) && dependent_type_p (scope))
2547                 return build_nt (SCOPE_REF, scope, id_expression);
2548               else if (TYPE_P (scope) && DECL_P (decl))
2549                 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2550                               id_expression);
2551               else
2552                 return decl;
2553             }
2554           /* A TEMPLATE_ID already contains all the information we
2555              need.  */
2556           if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2557             return id_expression;
2558           /* Since this name was dependent, the expression isn't
2559              constant -- yet.  No error is issued because it might be
2560              constant when things are instantiated.  */
2561           if (integral_constant_expression_p)
2562             *non_integral_constant_expression_p = true;
2563           *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2564           /* If we found a variable, then name lookup during the
2565              instantiation will always resolve to the same VAR_DECL
2566              (or an instantiation thereof).  */
2567           if (TREE_CODE (decl) == VAR_DECL
2568               || TREE_CODE (decl) == PARM_DECL)
2569             return decl;
2570           return id_expression;
2571         }
2572
2573       /* Only certain kinds of names are allowed in constant
2574        expression.  Enumerators and template parameters 
2575        have already been handled above.  */
2576       if (integral_constant_expression_p)
2577         {
2578             /* Const variables or static data members of integral or
2579               enumeration types initialized with constant expressions
2580               are OK.  */
2581           if (TREE_CODE (decl) == VAR_DECL
2582               && CP_TYPE_CONST_P (TREE_TYPE (decl))
2583               && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2584               && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2585             ;
2586           else
2587             {
2588               if (!allow_non_integral_constant_expression_p)
2589                 {
2590                   error ("`%D' cannot appear in a constant-expression", decl);
2591                   return error_mark_node;
2592                 }
2593               *non_integral_constant_expression_p = true;
2594             }
2595         }
2596       
2597       if (TREE_CODE (decl) == NAMESPACE_DECL)
2598         {
2599           error ("use of namespace `%D' as expression", decl);
2600           return error_mark_node;
2601         }
2602       else if (DECL_CLASS_TEMPLATE_P (decl))
2603         {
2604           error ("use of class template `%T' as expression", decl);
2605           return error_mark_node;
2606         }
2607       else if (TREE_CODE (decl) == TREE_LIST)
2608         {
2609           /* Ambiguous reference to base members.  */
2610           error ("request for member `%D' is ambiguous in "
2611                  "multiple inheritance lattice", id_expression);
2612           print_candidates (decl);
2613           return error_mark_node;
2614         }
2615
2616       /* Mark variable-like entities as used.  Functions are similarly
2617          marked either below or after overload resolution.  */
2618       if (TREE_CODE (decl) == VAR_DECL
2619           || TREE_CODE (decl) == PARM_DECL
2620           || TREE_CODE (decl) == RESULT_DECL)
2621         mark_used (decl);
2622
2623       if (scope)
2624         {
2625           decl = (adjust_result_of_qualified_name_lookup 
2626                   (decl, scope, current_class_type));
2627
2628           if (TREE_CODE (decl) == FUNCTION_DECL)
2629             mark_used (decl);
2630
2631           if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2632             *qualifying_class = scope;
2633           else if (!processing_template_decl)
2634             decl = convert_from_reference (decl);
2635           else if (TYPE_P (scope))
2636             decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2637         }
2638       else if (TREE_CODE (decl) == FIELD_DECL)
2639         decl = finish_non_static_data_member (decl, current_class_ref,
2640                                               /*qualifying_scope=*/NULL_TREE);
2641       else if (is_overloaded_fn (decl))
2642         {
2643           tree first_fn = OVL_CURRENT (decl);
2644
2645           if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2646             first_fn = DECL_TEMPLATE_RESULT (first_fn);
2647
2648           if (!really_overloaded_fn (decl))
2649             mark_used (first_fn);
2650
2651           if (TREE_CODE (first_fn) == FUNCTION_DECL
2652               && DECL_FUNCTION_MEMBER_P (first_fn))
2653             {
2654               /* A set of member functions.  */
2655               decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2656               return finish_class_member_access_expr (decl, id_expression);
2657             }
2658         }
2659       else
2660         {
2661           if (TREE_CODE (decl) == VAR_DECL
2662               || TREE_CODE (decl) == PARM_DECL
2663               || TREE_CODE (decl) == RESULT_DECL)
2664             {
2665               tree context = decl_function_context (decl);
2666               
2667               if (context != NULL_TREE && context != current_function_decl
2668                   && ! TREE_STATIC (decl))
2669                 {
2670                   error ("use of %s from containing function",
2671                          (TREE_CODE (decl) == VAR_DECL
2672                           ? "`auto' variable" : "parameter"));
2673                   cp_error_at ("  `%#D' declared here", decl);
2674                   return error_mark_node;
2675                 }
2676             }
2677           
2678           if (DECL_P (decl) && DECL_NONLOCAL (decl)
2679               && DECL_CLASS_SCOPE_P (decl)
2680               && DECL_CONTEXT (decl) != current_class_type)
2681             {
2682               tree path;
2683               
2684               path = currently_open_derived_class (DECL_CONTEXT (decl));
2685               perform_or_defer_access_check (TYPE_BINFO (path), decl);
2686             }
2687           
2688           if (! processing_template_decl)
2689             decl = convert_from_reference (decl);
2690         }
2691       
2692       /* Resolve references to variables of anonymous unions
2693          into COMPONENT_REFs.  */
2694       if (TREE_CODE (decl) == ALIAS_DECL)
2695         decl = unshare_expr (DECL_INITIAL (decl));
2696     }
2697
2698   if (TREE_DEPRECATED (decl))
2699     warn_deprecated_use (decl);
2700
2701   return decl;
2702 }
2703
2704 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2705    use as a type-specifier.  */
2706
2707 tree
2708 finish_typeof (tree expr)
2709 {
2710   tree type;
2711
2712   if (type_dependent_expression_p (expr))
2713     {
2714       type = make_aggr_type (TYPEOF_TYPE);
2715       TYPEOF_TYPE_EXPR (type) = expr;
2716
2717       return type;
2718     }
2719
2720   type = TREE_TYPE (expr);
2721
2722   if (!type || type == unknown_type_node)
2723     {
2724       error ("type of `%E' is unknown", expr);
2725       return error_mark_node;
2726     }
2727
2728   return type;
2729 }
2730
2731 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2732    with equivalent CALL_EXPRs.  */
2733
2734 static tree
2735 simplify_aggr_init_exprs_r (tree* tp, 
2736                             int* walk_subtrees,
2737                             void* data ATTRIBUTE_UNUSED)
2738 {
2739   /* We don't need to walk into types; there's nothing in a type that
2740      needs simplification.  (And, furthermore, there are places we
2741      actively don't want to go.  For example, we don't want to wander
2742      into the default arguments for a FUNCTION_DECL that appears in a
2743      CALL_EXPR.)  */
2744   if (TYPE_P (*tp))
2745     {
2746       *walk_subtrees = 0;
2747       return NULL_TREE;
2748     }
2749   /* Only AGGR_INIT_EXPRs are interesting.  */
2750   else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2751     return NULL_TREE;
2752
2753   simplify_aggr_init_expr (tp);
2754
2755   /* Keep iterating.  */
2756   return NULL_TREE;
2757 }
2758
2759 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR.  This
2760    function is broken out from the above for the benefit of the tree-ssa
2761    project.  */
2762
2763 void
2764 simplify_aggr_init_expr (tree *tp)
2765 {
2766   tree aggr_init_expr = *tp;
2767
2768   /* Form an appropriate CALL_EXPR.  */
2769   tree fn = TREE_OPERAND (aggr_init_expr, 0);
2770   tree args = TREE_OPERAND (aggr_init_expr, 1);
2771   tree slot = TREE_OPERAND (aggr_init_expr, 2);
2772   tree type = TREE_TYPE (slot);
2773
2774   tree call_expr;
2775   enum style_t { ctor, arg, pcc } style;
2776
2777   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2778     style = ctor;
2779 #ifdef PCC_STATIC_STRUCT_RETURN
2780   else if (1)
2781     style = pcc;
2782 #endif
2783   else if (TREE_ADDRESSABLE (type))
2784     style = arg;
2785   else
2786     /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2787        handling.  See build_cplus_new.  */
2788     abort ();
2789
2790   if (style == ctor || style == arg)
2791     {
2792       /* Pass the address of the slot.  If this is a constructor, we
2793          replace the first argument; otherwise, we tack on a new one.  */
2794       tree addr;
2795
2796       if (style == ctor)
2797         args = TREE_CHAIN (args);
2798
2799       cxx_mark_addressable (slot);
2800       addr = build1 (ADDR_EXPR, build_pointer_type (type), slot);
2801       if (style == arg)
2802         {
2803           /* The return type might have different cv-quals from the slot.  */
2804           tree fntype = TREE_TYPE (TREE_TYPE (fn));
2805 #ifdef ENABLE_CHECKING
2806           if (TREE_CODE (fntype) != FUNCTION_TYPE
2807               && TREE_CODE (fntype) != METHOD_TYPE)
2808             abort ();
2809 #endif
2810           addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2811         }
2812
2813       args = tree_cons (NULL_TREE, addr, args);
2814     }
2815
2816   call_expr = build (CALL_EXPR, 
2817                      TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2818                      fn, args, NULL_TREE);
2819
2820   if (style == arg)
2821     /* Tell the backend that we've added our return slot to the argument
2822        list.  */
2823     CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2824   else if (style == pcc)
2825     {
2826       /* If we're using the non-reentrant PCC calling convention, then we
2827          need to copy the returned value out of the static buffer into the
2828          SLOT.  */
2829       push_deferring_access_checks (dk_no_check);
2830       call_expr = build_aggr_init (slot, call_expr,
2831                                    DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2832       pop_deferring_access_checks ();
2833     }
2834
2835   *tp = call_expr;
2836 }
2837
2838 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
2839
2840 static void
2841 emit_associated_thunks (tree fn)
2842 {
2843   /* When we use vcall offsets, we emit thunks with the virtual
2844      functions to which they thunk. The whole point of vcall offsets
2845      is so that you can know statically the entire set of thunks that
2846      will ever be needed for a given virtual function, thereby
2847      enabling you to output all the thunks with the function itself.  */
2848   if (DECL_VIRTUAL_P (fn))
2849     {
2850       tree thunk;
2851       
2852       for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2853         {
2854           if (!THUNK_ALIAS (thunk))
2855             {
2856               use_thunk (thunk, /*emit_p=*/1);
2857               if (DECL_RESULT_THUNK_P (thunk))
2858                 {
2859                   tree probe;
2860                   
2861                   for (probe = DECL_THUNKS (thunk);
2862                        probe; probe = TREE_CHAIN (probe))
2863                     use_thunk (probe, /*emit_p=*/1);
2864                 }
2865             }
2866           else
2867             my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2868         }
2869     }
2870 }
2871
2872 /* Generate RTL for FN.  */
2873
2874 void
2875 expand_body (tree fn)
2876 {
2877   tree saved_function;
2878
2879   /* Compute the appropriate object-file linkage for inline
2880      functions.  */
2881   if (DECL_DECLARED_INLINE_P (fn))
2882     import_export_decl (fn);
2883
2884   /* If FN is external, then there's no point in generating RTL for
2885      it.  This situation can arise with an inline function under
2886      `-fexternal-templates'; we instantiate the function, even though
2887      we're not planning on emitting it, in case we get a chance to
2888      inline it.  */
2889   if (DECL_EXTERNAL (fn))
2890     return;
2891
2892   /* ??? When is this needed?  */
2893   saved_function = current_function_decl;
2894
2895   /* Emit any thunks that should be emitted at the same time as FN.  */
2896   emit_associated_thunks (fn);
2897
2898   tree_rest_of_compilation (fn, function_depth > 1);
2899
2900   current_function_decl = saved_function;
2901
2902   extract_interface_info ();
2903
2904   /* If this function is marked with the constructor attribute, add it
2905      to the list of functions to be called along with constructors
2906      from static duration objects.  */
2907   if (DECL_STATIC_CONSTRUCTOR (fn))
2908     static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2909
2910   /* If this function is marked with the destructor attribute, add it
2911      to the list of functions to be called along with destructors from
2912      static duration objects.  */
2913   if (DECL_STATIC_DESTRUCTOR (fn))
2914     static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2915
2916   if (DECL_CLONED_FUNCTION_P (fn))
2917     {
2918       /* If this is a clone, go through the other clones now and mark
2919          their parameters used.  We have to do that here, as we don't
2920          know whether any particular clone will be expanded, and
2921          therefore cannot pick one arbitrarily.  */ 
2922       tree probe;
2923
2924       for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2925            probe && DECL_CLONED_FUNCTION_P (probe);
2926            probe = TREE_CHAIN (probe))
2927         {
2928           tree parms;
2929
2930           for (parms = DECL_ARGUMENTS (probe);
2931                parms; parms = TREE_CHAIN (parms))
2932             TREE_USED (parms) = 1;
2933         }
2934     }
2935 }
2936
2937 /* Generate RTL for FN.  */
2938
2939 void
2940 expand_or_defer_fn (tree fn)
2941 {
2942   /* When the parser calls us after finishing the body of a template
2943      function, we don't really want to expand the body.  */
2944   if (processing_template_decl)
2945     {
2946       /* Normally, collection only occurs in rest_of_compilation.  So,
2947          if we don't collect here, we never collect junk generated
2948          during the processing of templates until we hit a
2949          non-template function.  */
2950       ggc_collect ();
2951       return;
2952     }
2953
2954   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2955   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2956                                 simplify_aggr_init_exprs_r,
2957                                 NULL);
2958
2959   /* If this is a constructor or destructor body, we have to clone
2960      it.  */
2961   if (maybe_clone_body (fn))
2962     {
2963       /* We don't want to process FN again, so pretend we've written
2964          it out, even though we haven't.  */
2965       TREE_ASM_WRITTEN (fn) = 1;
2966       return;
2967     }
2968
2969   /* There's no reason to do any of the work here if we're only doing
2970      semantic analysis; this code just generates RTL.  */
2971   if (flag_syntax_only)
2972     return;
2973
2974   /* Compute the appropriate object-file linkage for inline functions.  */
2975   if (DECL_DECLARED_INLINE_P (fn))
2976     import_export_decl (fn);
2977
2978   function_depth++;
2979
2980   /* Expand or defer, at the whim of the compilation unit manager.  */
2981   cgraph_finalize_function (fn, function_depth > 1);
2982
2983   function_depth--;
2984 }
2985
2986 struct nrv_data
2987 {
2988   tree var;
2989   tree result;
2990   htab_t visited;
2991 };
2992
2993 /* Helper function for walk_tree, used by finalize_nrv below.  */
2994
2995 static tree
2996 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
2997 {
2998   struct nrv_data *dp = (struct nrv_data *)data;
2999   void **slot;
3000
3001   /* No need to walk into types.  There wouldn't be any need to walk into
3002      non-statements, except that we have to consider STMT_EXPRs.  */
3003   if (TYPE_P (*tp))
3004     *walk_subtrees = 0;
3005   /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3006      but differs from using NULL_TREE in that it indicates that we care
3007      about the value of the RESULT_DECL.  */
3008   else if (TREE_CODE (*tp) == RETURN_STMT)
3009     RETURN_STMT_EXPR (*tp) = dp->result;
3010   /* Change all cleanups for the NRV to only run when an exception is
3011      thrown.  */
3012   else if (TREE_CODE (*tp) == CLEANUP_STMT
3013            && CLEANUP_DECL (*tp) == dp->var)
3014     CLEANUP_EH_ONLY (*tp) = 1;
3015   /* Replace the DECL_STMT for the NRV with an initialization of the
3016      RESULT_DECL, if needed.  */
3017   else if (TREE_CODE (*tp) == DECL_STMT
3018            && DECL_STMT_DECL (*tp) == dp->var)
3019     {
3020       tree init;
3021       if (DECL_INITIAL (dp->var)
3022           && DECL_INITIAL (dp->var) != error_mark_node)
3023         {
3024           init = build (INIT_EXPR, void_type_node, dp->result,
3025                         DECL_INITIAL (dp->var));
3026           DECL_INITIAL (dp->var) = error_mark_node;
3027         }
3028       else
3029         init = build_empty_stmt ();
3030       SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp));
3031       *tp = init;
3032     }
3033   /* And replace all uses of the NRV with the RESULT_DECL.  */
3034   else if (*tp == dp->var)
3035     *tp = dp->result;
3036
3037   /* Avoid walking into the same tree more than once.  Unfortunately, we
3038      can't just use walk_tree_without duplicates because it would only call
3039      us for the first occurrence of dp->var in the function body.  */
3040   slot = htab_find_slot (dp->visited, *tp, INSERT);
3041   if (*slot)
3042     *walk_subtrees = 0;
3043   else
3044     *slot = *tp;
3045
3046   /* Keep iterating.  */
3047   return NULL_TREE;
3048 }
3049
3050 /* Called from finish_function to implement the named return value
3051    optimization by overriding all the RETURN_STMTs and pertinent
3052    CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3053    RESULT_DECL for the function.  */
3054
3055 void
3056 finalize_nrv (tree *tp, tree var, tree result)
3057 {
3058   struct nrv_data data;
3059
3060   /* Copy debugging information from VAR to RESULT.  */
3061   DECL_NAME (result) = DECL_NAME (var);
3062   DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var);
3063   DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var);
3064   /* Don't forget that we take its address.  */
3065   TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3066
3067   data.var = var;
3068   data.result = result;
3069   data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3070   walk_tree (tp, finalize_nrv_r, &data, 0);
3071   htab_delete (data.visited);
3072 }
3073
3074 /* Perform initialization related to this module.  */
3075
3076 void
3077 init_cp_semantics (void)
3078 {
3079 }
3080
3081 #include "gt-cp-semantics.h"