OSDN Git Service

* call.c: Fix comment typos.
[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 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 "except.h"
36 #include "lex.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "timevar.h"
43 #include "debug.h"
44 #include "cgraph.h"
45
46 /* There routines provide a modular interface to perform many parsing
47    operations.  They may therefore be used during actual parsing, or
48    during template instantiation, which may be regarded as a
49    degenerate form of parsing.  Since the current g++ parser is
50    lacking in several respects, and will be reimplemented, we are
51    attempting to move most code that is not directly related to
52    parsing into this file; that will make implementing the new parser
53    much easier since it will be able to make use of these routines.  */
54
55 static tree maybe_convert_cond (tree);
56 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
57 static void emit_associated_thunks (tree);
58 static void genrtl_try_block (tree);
59 static void genrtl_eh_spec_block (tree);
60 static void genrtl_handler (tree);
61 static void cp_expand_stmt (tree);
62 static void genrtl_start_function (tree);
63 static void genrtl_finish_function (tree);
64 static tree clear_decl_rtl (tree *, int *, void *);
65
66 /* Finish processing the COND, the SUBSTMT condition for STMT.  */
67
68 #define FINISH_COND(COND, STMT, SUBSTMT)                \
69   do {                                                  \
70     if (last_tree != (STMT))                            \
71       {                                                 \
72         RECHAIN_STMTS (STMT, SUBSTMT);                  \
73         if (!processing_template_decl)                  \
74           {                                             \
75             (COND) = build_tree_list (SUBSTMT, COND);   \
76             (SUBSTMT) = (COND);                         \
77           }                                             \
78       }                                                 \
79     else                                                \
80       (SUBSTMT) = (COND);                               \
81   } while (0)
82
83 /* Deferred Access Checking Overview
84    ---------------------------------
85
86    Most C++ expressions and declarations require access checking
87    to be performed during parsing.  However, in several cases,
88    this has to be treated differently.
89
90    For member declarations, access checking has to be deferred
91    until more information about the declaration is known.  For
92    example:
93
94      class A {
95          typedef int X;
96        public:
97          X f();
98      };
99
100      A::X A::f();
101      A::X g();
102
103    When we are parsing the function return type `A::X', we don't
104    really know if this is allowed until we parse the function name.
105
106    Furthermore, some contexts require that access checking is
107    never performed at all.  These include class heads, and template
108    instantiations.
109
110    Typical use of access checking functions is described here:
111    
112    1. When we enter a context that requires certain access checking
113       mode, the function `push_deferring_access_checks' is called with
114       DEFERRING argument specifying the desired mode.  Access checking
115       may be performed immediately (dk_no_deferred), deferred
116       (dk_deferred), or not performed (dk_no_check).
117
118    2. When a declaration such as a type, or a variable, is encountered,
119       the function `perform_or_defer_access_check' is called.  It
120       maintains a TREE_LIST of all deferred checks.
121
122    3. The global `current_class_type' or `current_function_decl' is then
123       setup by the parser.  `enforce_access' relies on these information
124       to check access.
125
126    4. Upon exiting the context mentioned in step 1,
127       `perform_deferred_access_checks' is called to check all declaration
128       stored in the TREE_LIST.   `pop_deferring_access_checks' is then
129       called to restore the previous access checking mode.
130
131       In case of parsing error, we simply call `pop_deferring_access_checks'
132       without `perform_deferred_access_checks'.  */
133
134 /* Data for deferred access checking.  */
135 static GTY(()) deferred_access *deferred_access_stack;
136 static GTY(()) deferred_access *deferred_access_free_list;
137
138 /* Save the current deferred access states and start deferred
139    access checking iff DEFER_P is true.  */
140
141 void push_deferring_access_checks (deferring_kind deferring)
142 {
143   deferred_access *d;
144
145   /* For context like template instantiation, access checking
146      disabling applies to all nested context.  */
147   if (deferred_access_stack
148       && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
149     deferring = dk_no_check;
150
151   /* Recycle previously used free store if available.  */
152   if (deferred_access_free_list)
153     {
154       d = deferred_access_free_list;
155       deferred_access_free_list = d->next;
156     }
157   else
158     d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
159
160   d->next = deferred_access_stack;
161   d->deferred_access_checks = NULL_TREE;
162   d->deferring_access_checks_kind = deferring;
163   deferred_access_stack = d;
164 }
165
166 /* Resume deferring access checks again after we stopped doing
167    this previously.  */
168
169 void resume_deferring_access_checks (void)
170 {
171   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
172     deferred_access_stack->deferring_access_checks_kind = dk_deferred;
173 }
174
175 /* Stop deferring access checks.  */
176
177 void stop_deferring_access_checks (void)
178 {
179   if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
180     deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
181 }
182
183 /* Discard the current deferred access checks and restore the
184    previous states.  */
185
186 void pop_deferring_access_checks (void)
187 {
188   deferred_access *d = deferred_access_stack;
189   deferred_access_stack = d->next;
190
191   /* Remove references to access checks TREE_LIST.  */
192   d->deferred_access_checks = NULL_TREE;
193
194   /* Store in free list for later use.  */
195   d->next = deferred_access_free_list;
196   deferred_access_free_list = d;
197 }
198
199 /* Returns a TREE_LIST representing the deferred checks.  
200    The TREE_PURPOSE of each node is the type through which the 
201    access occurred; the TREE_VALUE is the declaration named.
202    */
203
204 tree get_deferred_access_checks (void)
205 {
206   return deferred_access_stack->deferred_access_checks;
207 }
208
209 /* Take current deferred checks and combine with the
210    previous states if we also defer checks previously.
211    Otherwise perform checks now.  */
212
213 void pop_to_parent_deferring_access_checks (void)
214 {
215   tree deferred_check = get_deferred_access_checks ();
216   deferred_access *d1 = deferred_access_stack;
217   deferred_access *d2 = deferred_access_stack->next;
218   deferred_access *d3 = deferred_access_stack->next->next;
219
220   /* Temporary swap the order of the top two states, just to make
221      sure the garbage collector will not reclaim the memory during 
222      processing below.  */
223   deferred_access_stack = d2;
224   d2->next = d1;
225   d1->next = d3;
226
227   for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
228     /* Perform deferred check if required.  */
229     perform_or_defer_access_check (TREE_PURPOSE (deferred_check), 
230                                    TREE_VALUE (deferred_check));
231
232   deferred_access_stack = d1;
233   d1->next = d2;
234   d2->next = d3;
235   pop_deferring_access_checks ();
236 }
237
238 /* Perform the deferred access checks.
239
240    After performing the checks, we still have to keep the list
241    `deferred_access_stack->deferred_access_checks' since we may want
242    to check access for them again later in a different context.
243    For example:
244
245      class A {
246        typedef int X;
247        static X a;
248      };
249      A::X A::a, x;      // No error for `A::a', error for `x'
250
251    We have to perform deferred access of `A::X', first with `A::a',
252    next with `x'.  */
253
254 void perform_deferred_access_checks (void)
255 {
256   tree deferred_check;
257   for (deferred_check = deferred_access_stack->deferred_access_checks;
258        deferred_check;
259        deferred_check = TREE_CHAIN (deferred_check))
260     /* Check access.  */
261     enforce_access (TREE_PURPOSE (deferred_check), 
262                     TREE_VALUE (deferred_check));
263 }
264
265 /* Defer checking the accessibility of DECL, when looked up in
266    BINFO.  */
267
268 void perform_or_defer_access_check (tree binfo, tree decl)
269 {
270   tree check;
271
272   my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
273   
274   /* If we are not supposed to defer access checks, just check now.  */
275   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
276     {
277       enforce_access (binfo, decl);
278       return;
279     }
280   /* Exit if we are in a context that no access checking is performed.  */
281   else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
282     return;
283
284   /* See if we are already going to perform this check.  */
285   for (check = deferred_access_stack->deferred_access_checks;
286        check;
287        check = TREE_CHAIN (check))
288     if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
289       return;
290   /* If not, record the check.  */
291   deferred_access_stack->deferred_access_checks
292     = tree_cons (binfo, decl,
293                  deferred_access_stack->deferred_access_checks);
294 }
295
296 /* Returns nonzero if the current statement is a full expression,
297    i.e. temporaries created during that statement should be destroyed
298    at the end of the statement.  */
299
300 int
301 stmts_are_full_exprs_p (void)
302 {
303   return current_stmt_tree ()->stmts_are_full_exprs_p;
304 }
305
306 /* Returns the stmt_tree (if any) to which statements are currently
307    being added.  If there is no active statement-tree, NULL is
308    returned.  */
309
310 stmt_tree
311 current_stmt_tree (void)
312 {
313   return (cfun 
314           ? &cfun->language->base.x_stmt_tree 
315           : &scope_chain->x_stmt_tree);
316 }
317
318 /* Nonzero if TYPE is an anonymous union or struct type.  We have to use a
319    flag for this because "A union for which objects or pointers are
320    declared is not an anonymous union" [class.union].  */
321
322 int
323 anon_aggr_type_p (tree node)
324 {
325   return ANON_AGGR_TYPE_P (node);
326 }
327
328 /* Finish a scope.  */
329
330 tree
331 do_poplevel (void)
332 {
333   tree block = NULL_TREE;
334
335   if (stmts_are_full_exprs_p ())
336     {
337       tree scope_stmts = NULL_TREE;
338
339       block = poplevel (kept_level_p (), 1, 0);
340       if (!processing_template_decl)
341         {
342           /* This needs to come after the poplevel so that partial scopes
343              are properly nested.  */
344           scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
345           if (block)
346             {
347               SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
348               SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
349             }
350         }
351     }
352
353   return block;
354 }
355
356 /* Begin a new scope.  */ 
357
358 void
359 do_pushlevel (scope_kind sk)
360 {
361   if (stmts_are_full_exprs_p ())
362     {
363       if (!processing_template_decl)
364         add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
365       begin_scope (sk);
366     }
367 }
368
369 /* Finish a goto-statement.  */
370
371 tree
372 finish_goto_stmt (tree destination)
373 {
374   if (TREE_CODE (destination) == IDENTIFIER_NODE)
375     destination = lookup_label (destination);
376
377   /* We warn about unused labels with -Wunused.  That means we have to
378      mark the used labels as used.  */
379   if (TREE_CODE (destination) == LABEL_DECL)
380     TREE_USED (destination) = 1;
381     
382   if (TREE_CODE (destination) != LABEL_DECL)
383     /* We don't inline calls to functions with computed gotos.
384        Those functions are typically up to some funny business,
385        and may be depending on the labels being at particular
386        addresses, or some such.  */
387     DECL_UNINLINABLE (current_function_decl) = 1;
388   
389   check_goto (destination);
390
391   return add_stmt (build_stmt (GOTO_STMT, destination));
392 }
393
394 /* COND is the condition-expression for an if, while, etc.,
395    statement.  Convert it to a boolean value, if appropriate.  */
396
397 static tree
398 maybe_convert_cond (tree cond)
399 {
400   /* Empty conditions remain empty.  */
401   if (!cond)
402     return NULL_TREE;
403
404   /* Wait until we instantiate templates before doing conversion.  */
405   if (processing_template_decl)
406     return cond;
407
408   /* Do the conversion.  */
409   cond = convert_from_reference (cond);
410   return condition_conversion (cond);
411 }
412
413 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
414
415 tree
416 finish_expr_stmt (tree expr)
417 {
418   tree r = NULL_TREE;
419   tree expr_type = NULL_TREE;;
420
421   if (expr != NULL_TREE)
422     {
423       if (!processing_template_decl
424           && !(stmts_are_full_exprs_p ())
425           && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
426                && lvalue_p (expr))
427               || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
428         expr = default_conversion (expr);
429       
430       /* Remember the type of the expression.  */
431       expr_type = TREE_TYPE (expr);
432
433       if (stmts_are_full_exprs_p ())
434         expr = convert_to_void (expr, "statement");
435       
436       r = add_stmt (build_stmt (EXPR_STMT, expr));
437     }
438
439   finish_stmt ();
440
441   /* This was an expression-statement, so we save the type of the
442      expression.  */
443   last_expr_type = expr_type;
444
445   return r;
446 }
447
448
449 /* Begin an if-statement.  Returns a newly created IF_STMT if
450    appropriate.  */
451
452 tree
453 begin_if_stmt (void)
454 {
455   tree r;
456   do_pushlevel (sk_block);
457   r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
458   add_stmt (r);
459   return r;
460 }
461
462 /* Process the COND of an if-statement, which may be given by
463    IF_STMT.  */
464
465 void 
466 finish_if_stmt_cond (tree cond, tree if_stmt)
467 {
468   cond = maybe_convert_cond (cond);
469   FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
470 }
471
472 /* Finish the then-clause of an if-statement, which may be given by
473    IF_STMT.  */
474
475 tree
476 finish_then_clause (tree if_stmt)
477 {
478   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
479   return if_stmt;
480 }
481
482 /* Begin the else-clause of an if-statement.  */
483
484 void 
485 begin_else_clause (void)
486 {
487 }
488
489 /* Finish the else-clause of an if-statement, which may be given by
490    IF_STMT.  */
491
492 void
493 finish_else_clause (tree if_stmt)
494 {
495   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
496 }
497
498 /* Finish an if-statement.  */
499
500 void 
501 finish_if_stmt (void)
502 {
503   finish_stmt ();
504   do_poplevel ();
505 }
506
507 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
508    appropriate.  */
509
510 tree
511 begin_while_stmt (void)
512 {
513   tree r;
514   r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
515   add_stmt (r);
516   do_pushlevel (sk_block);
517   return r;
518 }
519
520 /* Process the COND of a while-statement, which may be given by
521    WHILE_STMT.  */
522
523 void 
524 finish_while_stmt_cond (tree cond, tree while_stmt)
525 {
526   cond = maybe_convert_cond (cond);
527   if (processing_template_decl)
528     /* Don't mess with condition decls in a template.  */
529     FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
530   else if (getdecls () == NULL_TREE)
531     /* It was a simple condition; install it.  */
532     WHILE_COND (while_stmt) = cond;
533   else
534     {
535       /* If there was a declaration in the condition, we can't leave it
536          there; transform
537             while (A x = 42) { }
538          to
539             while (true) { A x = 42; if (!x) break; }  */
540       tree if_stmt;
541       WHILE_COND (while_stmt) = boolean_true_node;
542
543       if_stmt = begin_if_stmt ();
544       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
545       finish_if_stmt_cond (cond, if_stmt);
546       finish_break_stmt ();
547       finish_then_clause (if_stmt);
548       finish_if_stmt ();
549     }
550 }
551
552 /* Finish a while-statement, which may be given by WHILE_STMT.  */
553
554 void 
555 finish_while_stmt (tree while_stmt)
556 {
557   do_poplevel ();
558   RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
559   finish_stmt ();
560 }
561
562 /* Begin a do-statement.  Returns a newly created DO_STMT if
563    appropriate.  */
564
565 tree
566 begin_do_stmt (void)
567 {
568   tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
569   add_stmt (r);
570   return r;
571 }
572
573 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
574
575 void
576 finish_do_body (tree do_stmt)
577 {
578   RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
579 }
580
581 /* Finish a do-statement, which may be given by DO_STMT, and whose
582    COND is as indicated.  */
583
584 void
585 finish_do_stmt (tree cond, tree do_stmt)
586 {
587   cond = maybe_convert_cond (cond);
588   DO_COND (do_stmt) = cond;
589   finish_stmt ();
590 }
591
592 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
593    indicated.  */
594
595 tree
596 finish_return_stmt (tree expr)
597 {
598   tree r;
599
600   expr = check_return_expr (expr);
601   if (!processing_template_decl)
602     {
603       if (DECL_DESTRUCTOR_P (current_function_decl))
604         {
605           /* Similarly, all destructors must run destructors for
606              base-classes before returning.  So, all returns in a
607              destructor get sent to the DTOR_LABEL; finish_function emits
608              code to return a value there.  */
609           return finish_goto_stmt (dtor_label);
610         }
611     }
612   r = add_stmt (build_stmt (RETURN_STMT, expr));
613   finish_stmt ();
614
615   return r;
616 }
617
618 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
619
620 tree
621 begin_for_stmt (void)
622 {
623   tree r;
624
625   r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE, 
626                   NULL_TREE, NULL_TREE);
627   NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
628   if (NEW_FOR_SCOPE_P (r))
629     do_pushlevel (sk_for);
630   add_stmt (r);
631
632   return r;
633 }
634
635 /* Finish the for-init-statement of a for-statement, which may be
636    given by FOR_STMT.  */
637
638 void
639 finish_for_init_stmt (tree for_stmt)
640 {
641   if (last_tree != for_stmt)
642     RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
643   do_pushlevel (sk_block);
644 }
645
646 /* Finish the COND of a for-statement, which may be given by
647    FOR_STMT.  */
648
649 void
650 finish_for_cond (tree cond, tree for_stmt)
651 {
652   cond = maybe_convert_cond (cond);
653   if (processing_template_decl)
654     /* Don't mess with condition decls in a template.  */
655     FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
656   else if (getdecls () == NULL_TREE)
657     /* It was a simple condition; install it.  */
658     FOR_COND (for_stmt) = cond;
659   else
660     {
661       /* If there was a declaration in the condition, we can't leave it
662          there; transform
663             for (; A x = 42;) { }
664          to
665             for (;;) { A x = 42; if (!x) break; }  */
666       tree if_stmt;
667       FOR_COND (for_stmt) = NULL_TREE;
668
669       if_stmt = begin_if_stmt ();
670       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
671       finish_if_stmt_cond (cond, if_stmt);
672       finish_break_stmt ();
673       finish_then_clause (if_stmt);
674       finish_if_stmt ();
675     }
676 }
677
678 /* Finish the increment-EXPRESSION in a for-statement, which may be
679    given by FOR_STMT.  */
680
681 void
682 finish_for_expr (tree expr, tree for_stmt)
683 {
684   FOR_EXPR (for_stmt) = expr;
685 }
686
687 /* Finish the body of a for-statement, which may be given by
688    FOR_STMT.  The increment-EXPR for the loop must be
689    provided.  */
690
691 void
692 finish_for_stmt (tree for_stmt)
693 {
694   /* Pop the scope for the body of the loop.  */
695   do_poplevel ();
696   RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
697   if (NEW_FOR_SCOPE_P (for_stmt))
698     do_poplevel ();
699   finish_stmt (); 
700 }
701
702 /* Finish a break-statement.  */
703
704 tree
705 finish_break_stmt (void)
706 {
707   return add_stmt (build_break_stmt ());
708 }
709
710 /* Finish a continue-statement.  */
711
712 tree
713 finish_continue_stmt (void)
714 {
715   return add_stmt (build_continue_stmt ());
716 }
717
718 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
719    appropriate.  */
720
721 tree
722 begin_switch_stmt (void)
723 {
724   tree r;
725   do_pushlevel (sk_block);
726   r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
727   add_stmt (r);
728   return r;
729 }
730
731 /* Finish the cond of a switch-statement.  */
732
733 void
734 finish_switch_cond (tree cond, tree switch_stmt)
735 {
736   tree orig_type = NULL;
737   if (!processing_template_decl)
738     {
739       tree index;
740
741       /* Convert the condition to an integer or enumeration type.  */
742       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
743       if (cond == NULL_TREE)
744         {
745           error ("switch quantity not an integer");
746           cond = error_mark_node;
747         }
748       orig_type = TREE_TYPE (cond);
749       if (cond != error_mark_node)
750         {
751           cond = default_conversion (cond);
752           cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
753         }
754
755       if (cond != error_mark_node)
756         {
757           index = get_unwidened (cond, NULL_TREE);
758           /* We can't strip a conversion from a signed type to an unsigned,
759              because if we did, int_fits_type_p would do the wrong thing
760              when checking case values for being in range,
761              and it's too hard to do the right thing.  */
762           if (TREE_UNSIGNED (TREE_TYPE (cond))
763               == TREE_UNSIGNED (TREE_TYPE (index)))
764             cond = index;
765         }
766     }
767   FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
768   SWITCH_TYPE (switch_stmt) = orig_type;
769   push_switch (switch_stmt);
770 }
771
772 /* Finish the body of a switch-statement, which may be given by
773    SWITCH_STMT.  The COND to switch on is indicated.  */
774
775 void
776 finish_switch_stmt (tree switch_stmt)
777 {
778   RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
779   pop_switch (); 
780   finish_stmt ();
781   do_poplevel ();
782 }
783
784 /* Generate the RTL for T, which is a TRY_BLOCK.  */
785
786 static void 
787 genrtl_try_block (tree t)
788 {
789   if (CLEANUP_P (t))
790     {
791       expand_eh_region_start ();
792       expand_stmt (TRY_STMTS (t));
793       expand_eh_region_end_cleanup (TRY_HANDLERS (t));
794     }
795   else
796     {
797       if (!FN_TRY_BLOCK_P (t)) 
798         emit_line_note (input_filename, input_line);
799
800       expand_eh_region_start ();
801       expand_stmt (TRY_STMTS (t));
802
803       if (FN_TRY_BLOCK_P (t))
804         {
805           expand_start_all_catch ();
806           in_function_try_handler = 1;
807           expand_stmt (TRY_HANDLERS (t));
808           in_function_try_handler = 0;
809           expand_end_all_catch ();
810         }
811       else 
812         {
813           expand_start_all_catch ();  
814           expand_stmt (TRY_HANDLERS (t));
815           expand_end_all_catch ();
816         }
817     }
818 }
819
820 /* Generate the RTL for T, which is an EH_SPEC_BLOCK.  */
821
822 static void 
823 genrtl_eh_spec_block (tree t)
824 {
825   expand_eh_region_start ();
826   expand_stmt (EH_SPEC_STMTS (t));
827   expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
828                                 build_call (call_unexpected_node,
829                                             tree_cons (NULL_TREE,
830                                                        build_exc_ptr (),
831                                                        NULL_TREE)));
832 }
833
834 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
835    appropriate.  */
836
837 tree
838 begin_try_block (void)
839 {
840   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
841   add_stmt (r);
842   return r;
843 }
844
845 /* Likewise, for a function-try-block.  */
846
847 tree
848 begin_function_try_block (void)
849 {
850   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
851   FN_TRY_BLOCK_P (r) = 1;
852   add_stmt (r);
853   return r;
854 }
855
856 /* Finish a try-block, which may be given by TRY_BLOCK.  */
857
858 void
859 finish_try_block (tree try_block)
860 {
861   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
862 }
863
864 /* Finish the body of a cleanup try-block, which may be given by
865    TRY_BLOCK.  */
866
867 void
868 finish_cleanup_try_block (tree try_block)
869 {
870   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
871 }
872
873 /* Finish an implicitly generated try-block, with a cleanup is given
874    by CLEANUP.  */
875
876 void
877 finish_cleanup (tree cleanup, tree try_block)
878 {
879   TRY_HANDLERS (try_block) = cleanup;
880   CLEANUP_P (try_block) = 1;
881 }
882
883 /* Likewise, for a function-try-block.  */
884
885 void
886 finish_function_try_block (tree try_block)
887 {
888   if (TREE_CHAIN (try_block) 
889       && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
890     {
891       /* Chain the compound statement after the CTOR_INITIALIZER.  */
892       TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
893       /* And make the CTOR_INITIALIZER the body of the try-block.  */
894       RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
895     }
896   else
897     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
898   in_function_try_handler = 1;
899 }
900
901 /* Finish a handler-sequence for a try-block, which may be given by
902    TRY_BLOCK.  */
903
904 void
905 finish_handler_sequence (tree try_block)
906 {
907   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
908   check_handlers (TRY_HANDLERS (try_block));
909 }
910
911 /* Likewise, for a function-try-block.  */
912
913 void
914 finish_function_handler_sequence (tree try_block)
915 {
916   in_function_try_handler = 0;
917   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
918   check_handlers (TRY_HANDLERS (try_block));
919 }
920
921 /* Generate the RTL for T, which is a HANDLER.  */
922
923 static void
924 genrtl_handler (tree t)
925 {
926   genrtl_do_pushlevel ();
927   if (!processing_template_decl)
928     expand_start_catch (HANDLER_TYPE (t));
929   expand_stmt (HANDLER_BODY (t));
930   if (!processing_template_decl)
931     expand_end_catch ();
932 }
933
934 /* Begin a handler.  Returns a HANDLER if appropriate.  */
935
936 tree
937 begin_handler (void)
938 {
939   tree r;
940   r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
941   add_stmt (r);
942   /* Create a binding level for the eh_info and the exception object
943      cleanup.  */
944   do_pushlevel (sk_catch);
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           add_decl_stmt (decl);
963           RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
964           type = TREE_TYPE (decl);
965         }
966     }
967   else
968     type = expand_start_catch_block (decl);
969
970   HANDLER_TYPE (handler) = type;
971 }
972
973 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
974    the return value from the matching call to finish_handler_parms.  */
975
976 void
977 finish_handler (tree handler)
978 {
979   if (!processing_template_decl)
980     expand_end_catch_block ();
981   do_poplevel ();
982   RECHAIN_STMTS (handler, HANDLER_BODY (handler));
983 }
984
985 /* Begin a compound-statement.  If HAS_NO_SCOPE is nonzero, the
986    compound-statement does not define a scope.  Returns a new
987    COMPOUND_STMT if appropriate.  */
988
989 tree
990 begin_compound_stmt (int has_no_scope)
991 {
992   tree r; 
993   int is_try = 0;
994
995   r = build_stmt (COMPOUND_STMT, NULL_TREE);
996
997   if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
998     is_try = 1;
999
1000   add_stmt (r);
1001   if (has_no_scope)
1002     COMPOUND_STMT_NO_SCOPE (r) = 1;
1003
1004   last_expr_type = NULL_TREE;
1005
1006   if (!has_no_scope)
1007     do_pushlevel (is_try ? sk_try : sk_block);
1008   else
1009     /* Normally, we try hard to keep the BLOCK for a
1010        statement-expression.  But, if it's a statement-expression with
1011        a scopeless block, there's nothing to keep, and we don't want
1012        to accidentally keep a block *inside* the scopeless block.  */ 
1013     keep_next_level (0);
1014
1015   return r;
1016 }
1017
1018 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1019    If HAS_NO_SCOPE is nonzero, the compound statement does not define
1020    a scope.  */
1021
1022 tree
1023 finish_compound_stmt (int has_no_scope, tree compound_stmt)
1024 {
1025   tree r;
1026   tree t;
1027
1028   if (!has_no_scope)
1029     r = do_poplevel ();
1030   else
1031     r = NULL_TREE;
1032
1033   RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1034
1035   /* When we call finish_stmt we will lose LAST_EXPR_TYPE.  But, since
1036      the precise purpose of that variable is store the type of the
1037      last expression statement within the last compound statement, we
1038      preserve the value.  */
1039   t = last_expr_type;
1040   finish_stmt ();
1041   last_expr_type = t;
1042
1043   return r;
1044 }
1045
1046 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1047    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1048    CLOBBERS.  */
1049
1050 tree
1051 finish_asm_stmt (tree cv_qualifier, 
1052                  tree string, 
1053                  tree output_operands,
1054                  tree input_operands, 
1055                  tree clobbers)
1056 {
1057   tree r;
1058   tree t;
1059
1060   if (cv_qualifier != NULL_TREE
1061       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1062     {
1063       warning ("%s qualifier ignored on asm",
1064                   IDENTIFIER_POINTER (cv_qualifier));
1065       cv_qualifier = NULL_TREE;
1066     }
1067
1068   if (!processing_template_decl)
1069     {
1070       int i;
1071       int ninputs;
1072       int noutputs;
1073
1074       for (t = input_operands; t; t = TREE_CHAIN (t))
1075         {
1076           tree converted_operand 
1077             = decay_conversion (TREE_VALUE (t)); 
1078           
1079           /* If the type of the operand hasn't been determined (e.g.,
1080              because it involves an overloaded function), then issue
1081              an error message.  There's no context available to
1082              resolve the overloading.  */
1083           if (TREE_TYPE (converted_operand) == unknown_type_node)
1084             {
1085               error ("type of asm operand `%E' could not be determined", 
1086                         TREE_VALUE (t));
1087               converted_operand = error_mark_node;
1088             }
1089           TREE_VALUE (t) = converted_operand;
1090         }
1091
1092       ninputs = list_length (input_operands);
1093       noutputs = list_length (output_operands);
1094
1095       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1096         {
1097           bool allows_mem;
1098           bool allows_reg;
1099           bool is_inout;
1100           const char *constraint;
1101           tree operand;
1102
1103           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1104           operand = TREE_VALUE (t);
1105
1106           if (!parse_output_constraint (&constraint,
1107                                         i, ninputs, noutputs,
1108                                         &allows_mem,
1109                                         &allows_reg,
1110                                         &is_inout))
1111             {
1112               /* By marking this operand as erroneous, we will not try
1113                  to process this operand again in expand_asm_operands.  */
1114               TREE_VALUE (t) = error_mark_node;
1115               continue;
1116             }
1117
1118           /* If the operand is a DECL that is going to end up in
1119              memory, assume it is addressable.  This is a bit more
1120              conservative than it would ideally be; the exact test is
1121              buried deep in expand_asm_operands and depends on the
1122              DECL_RTL for the OPERAND -- which we don't have at this
1123              point.  */
1124           if (!allows_reg && DECL_P (operand))
1125             cxx_mark_addressable (operand);
1126         }
1127     }
1128
1129   r = build_stmt (ASM_STMT, cv_qualifier, string,
1130                   output_operands, input_operands,
1131                   clobbers);
1132   return add_stmt (r);
1133 }
1134
1135 /* Finish a label with the indicated NAME.  */
1136
1137 tree
1138 finish_label_stmt (tree name)
1139 {
1140   tree decl = define_label (input_location, name);
1141   return add_stmt (build_stmt (LABEL_STMT, decl));
1142 }
1143
1144 /* Finish a series of declarations for local labels.  G++ allows users
1145    to declare "local" labels, i.e., labels with scope.  This extension
1146    is useful when writing code involving statement-expressions.  */
1147
1148 void
1149 finish_label_decl (tree name)
1150 {
1151   tree decl = declare_local_label (name);
1152   add_decl_stmt (decl);
1153 }
1154
1155 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1156
1157 void 
1158 finish_decl_cleanup (tree decl, tree cleanup)
1159 {
1160   add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1161 }
1162
1163 /* If the current scope exits with an exception, run CLEANUP.  */
1164
1165 void
1166 finish_eh_cleanup (tree cleanup)
1167 {
1168   tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1169   CLEANUP_EH_ONLY (r) = 1;
1170   add_stmt (r);
1171 }
1172
1173 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1174    order they were written by the user.  Each node is as for
1175    emit_mem_initializers.  */
1176
1177 void
1178 finish_mem_initializers (tree mem_inits)
1179 {
1180   /* Reorder the MEM_INITS so that they are in the order they appeared
1181      in the source program.  */
1182   mem_inits = nreverse (mem_inits);
1183
1184   if (processing_template_decl)
1185     add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1186   else
1187     emit_mem_initializers (mem_inits);
1188 }
1189
1190 /* Returns the stack of SCOPE_STMTs for the current function.  */
1191
1192 tree *
1193 current_scope_stmt_stack (void)
1194 {
1195   return &cfun->language->base.x_scope_stmt_stack;
1196 }
1197
1198 /* Finish a parenthesized expression EXPR.  */
1199
1200 tree
1201 finish_parenthesized_expr (tree expr)
1202 {
1203   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1204     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1205     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1206
1207   if (TREE_CODE (expr) == OFFSET_REF)
1208     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1209        enclosed in parentheses.  */
1210     PTRMEM_OK_P (expr) = 0;
1211   return expr;
1212 }
1213
1214 /* Finish a reference to a non-static data member (DECL) that is not
1215    preceded by `.' or `->'.  */
1216
1217 tree
1218 finish_non_static_data_member (tree decl, tree qualifying_scope)
1219 {
1220   my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1221
1222   if (current_class_ptr == NULL_TREE)
1223     {
1224       if (current_function_decl 
1225           && DECL_STATIC_FUNCTION_P (current_function_decl))
1226         cp_error_at ("invalid use of member `%D' in static member function",
1227                      decl);
1228       else
1229         cp_error_at ("invalid use of non-static data member `%D'", decl);
1230       error ("from this location");
1231
1232       return error_mark_node;
1233     }
1234   TREE_USED (current_class_ptr) = 1;
1235   if (processing_template_decl)
1236     return build_min (COMPONENT_REF, TREE_TYPE (decl),
1237                       current_class_ref, DECL_NAME (decl));
1238   else
1239     {
1240       tree access_type = current_class_type;
1241       tree object = current_class_ref;
1242
1243       while (access_type
1244              && !DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1245         {
1246           access_type = TYPE_CONTEXT (access_type);
1247           while (access_type && DECL_P (access_type))
1248             access_type = DECL_CONTEXT (access_type);
1249         }
1250
1251       if (!access_type)
1252         {
1253           cp_error_at ("object missing in reference to `%D'",
1254                        decl);
1255           error ("from this location");
1256           return error_mark_node;
1257         }
1258
1259       perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1260
1261       /* If the data member was named `C::M', convert `*this' to `C'
1262          first.  */
1263       if (qualifying_scope)
1264         {
1265           tree binfo = NULL_TREE;
1266           object = build_scoped_ref (object, qualifying_scope,
1267                                      &binfo);
1268         }
1269
1270       return build_class_member_access_expr (object, decl,
1271                                              /*access_path=*/NULL_TREE,
1272                                              /*preserve_reference=*/false);
1273     }
1274 }
1275
1276 /* Begin a statement-expression.  The value returned must be passed to
1277    finish_stmt_expr.  */
1278
1279 tree 
1280 begin_stmt_expr (void)
1281 {
1282   /* If we're outside a function, we won't have a statement-tree to
1283      work with.  But, if we see a statement-expression we need to
1284      create one.  */
1285   if (! cfun && !last_tree)
1286     begin_stmt_tree (&scope_chain->x_saved_tree);
1287
1288   keep_next_level (1);
1289   /* If we're building a statement tree, then the upcoming compound
1290      statement will be chained onto the tree structure, starting at
1291      last_tree.  We return last_tree so that we can later unhook the
1292      compound statement.  */
1293   return last_tree; 
1294 }
1295
1296 /* Used when beginning a statement-expression outside function scope.
1297    For example, when handling a file-scope initializer, we use this
1298    function.  */
1299
1300 tree
1301 begin_global_stmt_expr (void)
1302 {
1303   if (! cfun && !last_tree)
1304     begin_stmt_tree (&scope_chain->x_saved_tree);
1305
1306   keep_next_level (1);
1307   
1308   return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1); 
1309 }
1310
1311 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr.  */
1312
1313 tree 
1314 finish_global_stmt_expr (tree stmt_expr)
1315 {
1316   stmt_expr = expand_end_stmt_expr (stmt_expr);
1317   
1318   if (! cfun
1319       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1320     finish_stmt_tree (&scope_chain->x_saved_tree);
1321
1322   return stmt_expr;
1323 }
1324
1325 /* Finish a statement-expression.  RTL_EXPR should be the value
1326    returned by the previous begin_stmt_expr; EXPR is the
1327    statement-expression.  Returns an expression representing the
1328    statement-expression.  */
1329
1330 tree 
1331 finish_stmt_expr (tree rtl_expr)
1332 {
1333   tree result;
1334
1335   /* If the last thing in the statement-expression was not an
1336      expression-statement, then it has type `void'.  */
1337   if (!last_expr_type)
1338     last_expr_type = void_type_node;
1339   result = build_min (STMT_EXPR, last_expr_type, last_tree);
1340   TREE_SIDE_EFFECTS (result) = 1;
1341   
1342   /* Remove the compound statement from the tree structure; it is
1343      now saved in the STMT_EXPR.  */
1344   last_tree = rtl_expr;
1345   TREE_CHAIN (last_tree) = NULL_TREE;
1346
1347   /* If we created a statement-tree for this statement-expression,
1348      remove it now.  */ 
1349   if (! cfun
1350       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1351     finish_stmt_tree (&scope_chain->x_saved_tree);
1352
1353   return result;
1354 }
1355
1356 /* Generate an expression for `FN (ARGS)'.
1357
1358    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1359    as a virtual call, even if FN is virtual.  (This flag is set when
1360    encountering an expression where the function name is explicitly
1361    qualified.  For example a call to `X::f' never generates a virtual
1362    call.)
1363
1364    Returns code for the call.  */
1365
1366 tree 
1367 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1368 {
1369   if (fn == error_mark_node || args == error_mark_node)
1370     return error_mark_node;
1371
1372   if (processing_template_decl)
1373     return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1374
1375   /* ARGS should be a list of arguments.  */
1376   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1377                       20020712);
1378
1379   /* A reference to a member function will appear as an overloaded
1380      function (rather than a BASELINK) if an unqualified name was used
1381      to refer to it.  */
1382   if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1383     {
1384       tree f;
1385
1386       if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1387         f = get_first_fn (TREE_OPERAND (fn, 0));
1388       else
1389         f = get_first_fn (fn);
1390       if (DECL_FUNCTION_MEMBER_P (f))
1391         {
1392           tree type = currently_open_derived_class (DECL_CONTEXT (f));
1393           fn = build_baselink (TYPE_BINFO (type),
1394                                TYPE_BINFO (type),
1395                                fn, /*optype=*/NULL_TREE);
1396         }
1397     }
1398
1399   if (BASELINK_P (fn))
1400     {
1401       tree object;
1402
1403       /* A call to a member function.  From [over.call.func]:
1404
1405            If the keyword this is in scope and refers to the class of
1406            that member function, or a derived class thereof, then the
1407            function call is transformed into a qualified function call
1408            using (*this) as the postfix-expression to the left of the
1409            . operator.... [Otherwise] a contrived object of type T
1410            becomes the implied object argument.  
1411
1412         This paragraph is unclear about this situation:
1413
1414           struct A { void f(); };
1415           struct B : public A {};
1416           struct C : public A { void g() { B::f(); }};
1417
1418         In particular, for `B::f', this paragraph does not make clear
1419         whether "the class of that member function" refers to `A' or 
1420         to `B'.  We believe it refers to `B'.  */
1421       if (current_class_type 
1422           && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1423                              current_class_type)
1424           && current_class_ref)
1425         object = current_class_ref;
1426       else
1427         {
1428           tree representative_fn;
1429
1430           representative_fn = BASELINK_FUNCTIONS (fn);
1431           if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1432             representative_fn = TREE_OPERAND (representative_fn, 0);
1433           representative_fn = get_first_fn (representative_fn);
1434           object = build_dummy_object (DECL_CONTEXT (representative_fn));
1435         }
1436
1437       return build_new_method_call (object, fn, args, NULL_TREE,
1438                                     (disallow_virtual 
1439                                      ? LOOKUP_NONVIRTUAL : 0));
1440     }
1441   else if (is_overloaded_fn (fn))
1442     /* A call to a namespace-scope function.  */
1443     return build_new_function_call (fn, args);
1444   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1445     {
1446       tree result;
1447
1448       if (args)
1449         error ("arguments to destructor are not allowed");
1450       /* Mark the pseudo-destructor call as having side-effects so
1451          that we do not issue warnings about its use.  */
1452       result = build1 (NOP_EXPR,
1453                        void_type_node,
1454                        TREE_OPERAND (fn, 0));
1455       TREE_SIDE_EFFECTS (result) = 1;
1456       return result;
1457     }
1458   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1459     {
1460       /* If the "function" is really an object of class type, it might
1461          have an overloaded `operator ()'.  */
1462       tree result;
1463       result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1464       if (result)
1465         return result;
1466     }
1467
1468   /* A call where the function is unknown.  */
1469   return build_function_call (fn, args);
1470 }
1471
1472 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1473    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1474    POSTDECREMENT_EXPR.)  */
1475
1476 tree 
1477 finish_increment_expr (tree expr, enum tree_code code)
1478 {
1479   return build_x_unary_op (code, expr);  
1480 }
1481
1482 /* Finish a use of `this'.  Returns an expression for `this'.  */
1483
1484 tree 
1485 finish_this_expr (void)
1486 {
1487   tree result;
1488
1489   if (current_class_ptr)
1490     {
1491       result = current_class_ptr;
1492     }
1493   else if (current_function_decl
1494            && DECL_STATIC_FUNCTION_P (current_function_decl))
1495     {
1496       error ("`this' is unavailable for static member functions");
1497       result = error_mark_node;
1498     }
1499   else
1500     {
1501       if (current_function_decl)
1502         error ("invalid use of `this' in non-member function");
1503       else
1504         error ("invalid use of `this' at top level");
1505       result = error_mark_node;
1506     }
1507
1508   return result;
1509 }
1510
1511 /* Finish a member function call using OBJECT and ARGS as arguments to
1512    FN.  Returns an expression for the call.  */
1513
1514 tree 
1515 finish_object_call_expr (tree fn, tree object, tree args)
1516 {
1517   if (DECL_DECLARES_TYPE_P (fn))
1518     {
1519       if (processing_template_decl)
1520         /* This can happen on code like:
1521
1522            class X;
1523            template <class T> void f(T t) {
1524              t.X();
1525            }  
1526
1527            We just grab the underlying IDENTIFIER.  */
1528         fn = DECL_NAME (fn);
1529       else
1530         {
1531           error ("calling type `%T' like a method", fn);
1532           return error_mark_node;
1533         }
1534     }
1535   
1536   if (processing_template_decl)
1537     return build_nt (CALL_EXPR,
1538                      build_nt (COMPONENT_REF, object, fn),
1539                      args);
1540
1541   if (name_p (fn))
1542     return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1543   else
1544     return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1545 }
1546
1547 /* Finish a qualified member function call using OBJECT and ARGS as
1548    arguments to FN.  Returns an expression for the call.  */
1549
1550 tree 
1551 finish_qualified_object_call_expr (tree fn, tree object, tree args)
1552 {
1553   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1554                                    TREE_OPERAND (fn, 1), args);
1555 }
1556
1557 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
1558    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1559    the TYPE for the type given.  If SCOPE is non-NULL, the expression
1560    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
1561
1562 tree 
1563 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1564 {
1565   if (destructor == error_mark_node)
1566     return error_mark_node;
1567
1568   my_friendly_assert (TYPE_P (destructor), 20010905);
1569
1570   if (!processing_template_decl)
1571     {
1572       if (scope == error_mark_node)
1573         {
1574           error ("invalid qualifying scope in pseudo-destructor name");
1575           return error_mark_node;
1576         }
1577       
1578       if (!same_type_p (TREE_TYPE (object), destructor))
1579         {
1580           error ("`%E' is not of type `%T'", object, destructor);
1581           return error_mark_node;
1582         }
1583     }
1584
1585   return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1586 }
1587
1588 /* Finish an expression of the form CODE EXPR.  */
1589
1590 tree
1591 finish_unary_op_expr (enum tree_code code, tree expr)
1592 {
1593   tree result = build_x_unary_op (code, expr);
1594   /* Inside a template, build_x_unary_op does not fold the
1595      expression. So check whether the result is folded before
1596      setting TREE_NEGATED_INT.  */
1597   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1598       && TREE_CODE (result) == INTEGER_CST
1599       && !TREE_UNSIGNED (TREE_TYPE (result))
1600       && INT_CST_LT (result, integer_zero_node))
1601     TREE_NEGATED_INT (result) = 1;
1602   overflow_warning (result);
1603   return result;
1604 }
1605
1606 /* Finish a compound-literal expression.  TYPE is the type to which
1607    the INITIALIZER_LIST is being cast.  */
1608
1609 tree
1610 finish_compound_literal (tree type, tree initializer_list)
1611 {
1612   tree compound_literal;
1613
1614   /* Build a CONSTRUCTOR for the INITIALIZER_LIST.  */
1615   compound_literal = build_constructor (NULL_TREE, initializer_list);
1616   /* Mark it as a compound-literal.  */
1617   TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1618   if (processing_template_decl)
1619     TREE_TYPE (compound_literal) = type;
1620   else
1621     {
1622       /* Check the initialization.  */
1623       compound_literal = digest_init (type, compound_literal, NULL);
1624       /* If the TYPE was an array type with an unknown bound, then we can
1625          figure out the dimension now.  For example, something like:
1626
1627            `(int []) { 2, 3 }'
1628
1629          implies that the array has two elements.  */
1630       if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1631         complete_array_type (type, compound_literal, 1);
1632     }
1633
1634   return compound_literal;
1635 }
1636
1637 /* Return the declaration for the function-name variable indicated by
1638    ID.  */
1639
1640 tree
1641 finish_fname (tree id)
1642 {
1643   tree decl;
1644   
1645   decl = fname_decl (C_RID_CODE (id), id);
1646   if (processing_template_decl)
1647     decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1648   return decl;
1649 }
1650
1651 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1652    and DECLARATOR.  Returns nonzero if the function-declaration is
1653    valid.  */
1654
1655 int
1656 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1657 {
1658   if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1659     return 0;
1660
1661   /* The things we're about to see are not directly qualified by any
1662      template headers we've seen thus far.  */
1663   reset_specialization ();
1664
1665   return 1;
1666 }
1667
1668 /* Finish a translation unit.  */
1669
1670 void 
1671 finish_translation_unit (void)
1672 {
1673   /* In case there were missing closebraces,
1674      get us back to the global binding level.  */
1675   pop_everything ();
1676   while (current_namespace != global_namespace)
1677     pop_namespace ();
1678
1679   /* Do file scope __FUNCTION__ et al.  */
1680   finish_fname_decls ();
1681 }
1682
1683 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1684    Returns the parameter.  */
1685
1686 tree 
1687 finish_template_type_parm (tree aggr, tree identifier)
1688 {
1689   if (aggr != class_type_node)
1690     {
1691       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1692       aggr = class_type_node;
1693     }
1694
1695   return build_tree_list (aggr, identifier);
1696 }
1697
1698 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1699    Returns the parameter.  */
1700
1701 tree 
1702 finish_template_template_parm (tree aggr, tree identifier)
1703 {
1704   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1705   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1706   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1707   DECL_TEMPLATE_RESULT (tmpl) = decl;
1708   DECL_ARTIFICIAL (decl) = 1;
1709   end_template_decl ();
1710
1711   my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1712
1713   return finish_template_type_parm (aggr, tmpl);
1714 }
1715
1716 /* ARGUMENT is the default-argument value for a template template
1717    parameter.  If ARGUMENT is invalid, issue error messages and return
1718    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
1719
1720 tree
1721 check_template_template_default_arg (tree argument)
1722 {
1723   if (TREE_CODE (argument) != TEMPLATE_DECL
1724       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1725       && TREE_CODE (argument) != TYPE_DECL
1726       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1727     {
1728       error ("invalid default template argument");
1729       return error_mark_node;
1730     }
1731
1732   return argument;
1733 }
1734
1735 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1736    nonzero, the parameter list was terminated by a `...'.  */
1737
1738 tree
1739 finish_parmlist (tree parms, int ellipsis)
1740 {
1741   if (parms)
1742     {
1743       /* We mark the PARMS as a parmlist so that declarator processing can
1744          disambiguate certain constructs.  */
1745       TREE_PARMLIST (parms) = 1;
1746       /* We do not append void_list_node here, but leave it to grokparms
1747          to do that.  */
1748       PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1749     }
1750   return parms;
1751 }
1752
1753 /* Begin a class definition, as indicated by T.  */
1754
1755 tree
1756 begin_class_definition (tree t)
1757 {
1758   if (t == error_mark_node)
1759     return error_mark_node;
1760
1761   if (processing_template_parmlist)
1762     {
1763       error ("definition of `%#T' inside template parameter list", t);
1764       return error_mark_node;
1765     }
1766   /* A non-implicit typename comes from code like:
1767
1768        template <typename T> struct A {
1769          template <typename U> struct A<T>::B ...
1770
1771      This is erroneous.  */
1772   else if (TREE_CODE (t) == TYPENAME_TYPE)
1773     {
1774       error ("invalid definition of qualified type `%T'", t);
1775       t = error_mark_node;
1776     }
1777
1778   if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1779     {
1780       t = make_aggr_type (RECORD_TYPE);
1781       pushtag (make_anon_name (), t, 0);
1782     }
1783
1784   /* If this type was already complete, and we see another definition,
1785      that's an error.  */
1786   if (COMPLETE_TYPE_P (t))
1787     {
1788       error ("redefinition of `%#T'", t);
1789       cp_error_at ("previous definition of `%#T'", t);
1790       return error_mark_node;
1791     }
1792
1793   /* Update the location of the decl.  */
1794   DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
1795   
1796   if (TYPE_BEING_DEFINED (t))
1797     {
1798       t = make_aggr_type (TREE_CODE (t));
1799       pushtag (TYPE_IDENTIFIER (t), t, 0);
1800     }
1801   maybe_process_partial_specialization (t);
1802   pushclass (t, true);
1803   TYPE_BEING_DEFINED (t) = 1;
1804   TYPE_PACKED (t) = flag_pack_struct;
1805   /* Reset the interface data, at the earliest possible
1806      moment, as it might have been set via a class foo;
1807      before.  */
1808   if (! TYPE_ANONYMOUS_P (t))
1809     {
1810       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1811       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1812         (t, interface_unknown);
1813     }
1814   reset_specialization();
1815   
1816   /* Make a declaration for this class in its own scope.  */
1817   build_self_reference ();
1818
1819   return t;
1820 }
1821
1822 /* Finish the member declaration given by DECL.  */
1823
1824 void
1825 finish_member_declaration (tree decl)
1826 {
1827   if (decl == error_mark_node || decl == NULL_TREE)
1828     return;
1829
1830   if (decl == void_type_node)
1831     /* The COMPONENT was a friend, not a member, and so there's
1832        nothing for us to do.  */
1833     return;
1834
1835   /* We should see only one DECL at a time.  */
1836   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1837
1838   /* Set up access control for DECL.  */
1839   TREE_PRIVATE (decl) 
1840     = (current_access_specifier == access_private_node);
1841   TREE_PROTECTED (decl) 
1842     = (current_access_specifier == access_protected_node);
1843   if (TREE_CODE (decl) == TEMPLATE_DECL)
1844     {
1845       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1846       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1847     }
1848
1849   /* Mark the DECL as a member of the current class.  */
1850   DECL_CONTEXT (decl) = current_class_type;
1851
1852   /* [dcl.link]
1853
1854      A C language linkage is ignored for the names of class members
1855      and the member function type of class member functions.  */
1856   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1857     SET_DECL_LANGUAGE (decl, lang_cplusplus);
1858
1859   /* Put functions on the TYPE_METHODS list and everything else on the
1860      TYPE_FIELDS list.  Note that these are built up in reverse order.
1861      We reverse them (to obtain declaration order) in finish_struct.  */
1862   if (TREE_CODE (decl) == FUNCTION_DECL 
1863       || DECL_FUNCTION_TEMPLATE_P (decl))
1864     {
1865       /* We also need to add this function to the
1866          CLASSTYPE_METHOD_VEC.  */
1867       add_method (current_class_type, decl, /*error_p=*/0);
1868
1869       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1870       TYPE_METHODS (current_class_type) = decl;
1871
1872       maybe_add_class_template_decl_list (current_class_type, decl, 
1873                                           /*friend_p=*/0);
1874     }
1875   /* Enter the DECL into the scope of the class.  */
1876   else if (TREE_CODE (decl) == USING_DECL || pushdecl_class_level (decl))
1877     {
1878       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
1879          go at the beginning.  The reason is that lookup_field_1
1880          searches the list in order, and we want a field name to
1881          override a type name so that the "struct stat hack" will
1882          work.  In particular:
1883
1884            struct S { enum E { }; int E } s;
1885            s.E = 3;
1886
1887          is valid.  In addition, the FIELD_DECLs must be maintained in
1888          declaration order so that class layout works as expected.
1889          However, we don't need that order until class layout, so we
1890          save a little time by putting FIELD_DECLs on in reverse order
1891          here, and then reversing them in finish_struct_1.  (We could
1892          also keep a pointer to the correct insertion points in the
1893          list.)  */
1894
1895       if (TREE_CODE (decl) == TYPE_DECL)
1896         TYPE_FIELDS (current_class_type) 
1897           = chainon (TYPE_FIELDS (current_class_type), decl);
1898       else
1899         {
1900           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1901           TYPE_FIELDS (current_class_type) = decl;
1902         }
1903
1904       maybe_add_class_template_decl_list (current_class_type, decl, 
1905                                           /*friend_p=*/0);
1906     }
1907 }
1908
1909 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
1910    the definition is immediately followed by a semicolon.  Returns the
1911    type.  */
1912
1913 tree
1914 finish_class_definition (tree t, tree attributes, int semi, int pop_scope_p)
1915 {
1916   if (t == error_mark_node)
1917     return error_mark_node;
1918
1919   /* finish_struct nukes this anyway; if finish_exception does too,
1920      then it can go.  */
1921   if (semi)
1922     note_got_semicolon (t);
1923
1924   /* If we got any attributes in class_head, xref_tag will stick them in
1925      TREE_TYPE of the type.  Grab them now.  */
1926   attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1927   TYPE_ATTRIBUTES (t) = NULL_TREE;
1928
1929   if (TREE_CODE (t) == ENUMERAL_TYPE)
1930     ;
1931   else
1932     {
1933       t = finish_struct (t, attributes);
1934       if (semi) 
1935         note_got_semicolon (t);
1936     }
1937
1938   if (pop_scope_p)
1939     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1940
1941   return t;
1942 }
1943
1944 /* Finish processing the declaration of a member class template
1945    TYPES whose template parameters are given by PARMS.  */
1946
1947 tree
1948 finish_member_class_template (tree types)
1949 {
1950   tree t;
1951
1952   /* If there are declared, but undefined, partial specializations
1953      mixed in with the typespecs they will not yet have passed through
1954      maybe_process_partial_specialization, so we do that here.  */
1955   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1956     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1957       maybe_process_partial_specialization (TREE_VALUE (t));
1958
1959   note_list_got_semicolon (types);
1960   grok_x_components (types);
1961   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1962     /* The component was in fact a friend declaration.  We avoid
1963        finish_member_template_decl performing certain checks by
1964        unsetting TYPES.  */
1965     types = NULL_TREE;
1966   
1967   finish_member_template_decl (types);
1968
1969   /* As with other component type declarations, we do
1970      not store the new DECL on the list of
1971      component_decls.  */
1972   return NULL_TREE;
1973 }
1974
1975 /* Finish processing a complete template declaration.  The PARMS are
1976    the template parameters.  */
1977
1978 void
1979 finish_template_decl (tree parms)
1980 {
1981   if (parms)
1982     end_template_decl ();
1983   else
1984     end_specialization ();
1985 }
1986
1987 /* Finish processing a template-id (which names a type) of the form
1988    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
1989    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
1990    the scope of template-id indicated.  */
1991
1992 tree
1993 finish_template_type (tree name, tree args, int entering_scope)
1994 {
1995   tree decl;
1996
1997   decl = lookup_template_class (name, args,
1998                                 NULL_TREE, NULL_TREE,
1999                                 entering_scope, /*complain=*/1);
2000   if (decl != error_mark_node)
2001     decl = TYPE_STUB_DECL (decl);
2002
2003   return decl;
2004 }
2005
2006 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2007    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2008    BASE_CLASS, or NULL_TREE if an error occurred.  The
2009    ACCESS_SPECIFIER is one of
2010    access_{default,public,protected_private}[_virtual]_node.*/
2011
2012 tree 
2013 finish_base_specifier (tree base, tree access, bool virtual_p)
2014 {
2015   tree result;
2016
2017   if (base == error_mark_node)
2018     {
2019       error ("invalid base-class specification");
2020       result = NULL_TREE;
2021     }
2022   else if (! is_aggr_type (base, 1))
2023     result = NULL_TREE;
2024   else
2025     {
2026       if (cp_type_quals (base) != 0)
2027         {
2028           error ("base class `%T' has cv qualifiers", base);
2029           base = TYPE_MAIN_VARIANT (base);
2030         }
2031       result = build_tree_list (access, base);
2032       TREE_VIA_VIRTUAL (result) = virtual_p;
2033     }
2034
2035   return result;
2036 }
2037
2038 /* Called when multiple declarators are processed.  If that is not
2039    premitted in this context, an error is issued.  */
2040
2041 void
2042 check_multiple_declarators (void)
2043 {
2044   /* [temp]
2045      
2046      In a template-declaration, explicit specialization, or explicit
2047      instantiation the init-declarator-list in the declaration shall
2048      contain at most one declarator.  
2049
2050      We don't just use PROCESSING_TEMPLATE_DECL for the first
2051      condition since that would disallow the perfectly valid code, 
2052      like `template <class T> struct S { int i, j; };'.  */
2053   if (at_function_scope_p ())
2054     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2055     return;
2056      
2057   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2058       || processing_explicit_instantiation
2059       || processing_specialization)
2060     error ("multiple declarators in template declaration");
2061 }
2062
2063 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2064    use as a type-specifier.  */
2065
2066 tree
2067 finish_typeof (tree expr)
2068 {
2069   tree type;
2070
2071   if (type_dependent_expression_p (expr))
2072     {
2073       type = make_aggr_type (TYPEOF_TYPE);
2074       TYPE_FIELDS (type) = expr;
2075
2076       return type;
2077     }
2078
2079   type = TREE_TYPE (expr);
2080
2081   if (!type || type == unknown_type_node)
2082     {
2083       error ("type of `%E' is unknown", expr);
2084       return error_mark_node;
2085     }
2086
2087   return type;
2088 }
2089
2090 /* Compute the value of the `sizeof' operator.  */
2091
2092 tree
2093 finish_sizeof (tree t)
2094 {
2095   return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2096 }
2097
2098 /* Implement the __alignof keyword: Return the minimum required
2099    alignment of T, measured in bytes.  */
2100
2101 tree
2102 finish_alignof (tree t)
2103 {
2104   if (processing_template_decl)
2105     return build_min (ALIGNOF_EXPR, size_type_node, t);
2106
2107   return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2108 }
2109
2110 /* Generate RTL for the statement T, and its substatements, and any
2111    other statements at its nesting level.  */
2112
2113 static void
2114 cp_expand_stmt (tree t)
2115 {
2116   switch (TREE_CODE (t))
2117     {
2118     case TRY_BLOCK:
2119       genrtl_try_block (t);
2120       break;
2121
2122     case EH_SPEC_BLOCK:
2123       genrtl_eh_spec_block (t);
2124       break;
2125
2126     case HANDLER:
2127       genrtl_handler (t);
2128       break;
2129
2130     case USING_STMT:
2131       break;
2132     
2133     default:
2134       abort ();
2135       break;
2136     }
2137 }
2138
2139 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2140    will equivalent CALL_EXPRs.  */
2141
2142 static tree
2143 simplify_aggr_init_exprs_r (tree* tp, 
2144                             int* walk_subtrees ATTRIBUTE_UNUSED , 
2145                             void* data ATTRIBUTE_UNUSED )
2146 {
2147   tree aggr_init_expr;
2148   tree call_expr;
2149   tree fn;
2150   tree args;
2151   tree slot;
2152   tree type;
2153   enum style_t { ctor, arg, pcc } style;
2154
2155   aggr_init_expr = *tp;
2156   /* We don't need to walk into types; there's nothing in a type that
2157      needs simplification.  (And, furthermore, there are places we
2158      actively don't want to go.  For example, we don't want to wander
2159      into the default arguments for a FUNCTION_DECL that appears in a
2160      CALL_EXPR.)  */
2161   if (TYPE_P (aggr_init_expr))
2162     {
2163       *walk_subtrees = 0;
2164       return NULL_TREE;
2165     }
2166   /* Only AGGR_INIT_EXPRs are interesting.  */
2167   else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2168     return NULL_TREE;
2169
2170   /* Form an appropriate CALL_EXPR.  */
2171   fn = TREE_OPERAND (aggr_init_expr, 0);
2172   args = TREE_OPERAND (aggr_init_expr, 1);
2173   slot = TREE_OPERAND (aggr_init_expr, 2);
2174   type = TREE_TYPE (aggr_init_expr);
2175
2176   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2177     style = ctor;
2178 #ifdef PCC_STATIC_STRUCT_RETURN
2179   else if (1)
2180     style = pcc;
2181 #endif
2182   else if (TREE_ADDRESSABLE (type))
2183     style = arg;
2184   else
2185     /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2186        handling.  See build_cplus_new.  */
2187     abort ();
2188
2189   if (style == ctor || style == arg)
2190     {
2191       /* Pass the address of the slot.  If this is a constructor, we
2192          replace the first argument; otherwise, we tack on a new one.  */
2193       if (style == ctor)
2194         args = TREE_CHAIN (args);
2195
2196       cxx_mark_addressable (slot);
2197       args = tree_cons (NULL_TREE, 
2198                         build1 (ADDR_EXPR, 
2199                                 build_pointer_type (TREE_TYPE (slot)),
2200                                 slot),
2201                         args);
2202     }
2203
2204   call_expr = build (CALL_EXPR, 
2205                      TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2206                      fn, args, NULL_TREE);
2207   TREE_SIDE_EFFECTS (call_expr) = 1;
2208
2209   if (style == arg)
2210     /* Tell the backend that we've added our return slot to the argument
2211        list.  */
2212     CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2213   else if (style == pcc)
2214     {
2215       /* If we're using the non-reentrant PCC calling convention, then we
2216          need to copy the returned value out of the static buffer into the
2217          SLOT.  */
2218       push_deferring_access_checks (dk_no_check);
2219       call_expr = build_aggr_init (slot, call_expr,
2220                                    DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2221       pop_deferring_access_checks ();
2222     }
2223
2224   /* We want to use the value of the initialized location as the
2225      result.  */
2226   call_expr = build (COMPOUND_EXPR, type,
2227                      call_expr, slot);
2228
2229   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2230   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2231   *tp = call_expr;
2232
2233   /* Keep iterating.  */
2234   return NULL_TREE;
2235 }
2236
2237 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
2238
2239 static void
2240 emit_associated_thunks (tree fn)
2241 {
2242   /* When we use vcall offsets, we emit thunks with the virtual
2243      functions to which they thunk. The whole point of vcall offsets
2244      is so that you can know statically the entire set of thunks that
2245      will ever be needed for a given virtual function, thereby
2246      enabling you to output all the thunks with the function itself.  */
2247   if (DECL_VIRTUAL_P (fn))
2248     {
2249       tree thunk;
2250       
2251       for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2252         {
2253           use_thunk (thunk, /*emit_p=*/1);
2254           if (DECL_RESULT_THUNK_P (thunk))
2255             {
2256               tree probe;
2257
2258               for (probe = DECL_THUNKS (thunk);
2259                    probe; probe = TREE_CHAIN (probe))
2260                 use_thunk (probe, /*emit_p=*/1);
2261             }
2262         }
2263     }
2264 }
2265
2266 /* Generate RTL for FN.  */
2267
2268 void
2269 expand_body (tree fn)
2270 {
2271   location_t saved_loc;
2272   tree saved_function;
2273   
2274   if (flag_unit_at_a_time && !cgraph_global_info_ready)
2275     abort ();
2276
2277   /* Compute the appropriate object-file linkage for inline
2278      functions.  */
2279   if (DECL_DECLARED_INLINE_P (fn))
2280     import_export_decl (fn);
2281
2282   /* If FN is external, then there's no point in generating RTL for
2283      it.  This situation can arise with an inline function under
2284      `-fexternal-templates'; we instantiate the function, even though
2285      we're not planning on emitting it, in case we get a chance to
2286      inline it.  */
2287   if (DECL_EXTERNAL (fn))
2288     return;
2289
2290   /* Save the current file name and line number.  When we expand the
2291      body of the function, we'll set INPUT_LOCATION so that
2292      error-messages come out in the right places.  */
2293   saved_loc = input_location;
2294   saved_function = current_function_decl;
2295   input_location = DECL_SOURCE_LOCATION (fn);
2296   current_function_decl = fn;
2297
2298   timevar_push (TV_INTEGRATION);
2299
2300   /* Optimize the body of the function before expanding it.  */
2301   optimize_function (fn);
2302
2303   timevar_pop (TV_INTEGRATION);
2304   timevar_push (TV_EXPAND);
2305
2306   genrtl_start_function (fn);
2307   current_function_is_thunk = DECL_THUNK_P (fn);
2308
2309   /* Expand the body.  */
2310   expand_stmt (DECL_SAVED_TREE (fn));
2311
2312   /* Statements should always be full-expressions at the outermost set
2313      of curly braces for a function.  */
2314   my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2315
2316   /* The outermost statement for a function contains the line number
2317      recorded when we finished processing the function.  */
2318   input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2319
2320   /* Generate code for the function.  */
2321   genrtl_finish_function (fn);
2322
2323   /* If possible, obliterate the body of the function so that it can
2324      be garbage collected.  */
2325   if (dump_enabled_p (TDI_all))
2326     /* Keep the body; we're going to dump it.  */
2327     ;
2328   else if (DECL_INLINE (fn) && flag_inline_trees)
2329     /* We might need the body of this function so that we can expand
2330        it inline somewhere else.  */
2331     ;
2332   else
2333     /* We don't need the body; blow it away.  */
2334     DECL_SAVED_TREE (fn) = NULL_TREE;
2335
2336   /* And restore the current source position.  */
2337   current_function_decl = saved_function;
2338   input_location = saved_loc;
2339   extract_interface_info ();
2340
2341   timevar_pop (TV_EXPAND);
2342
2343   /* Emit any thunks that should be emitted at the same time as FN.  */
2344   emit_associated_thunks (fn);
2345 }
2346
2347 /* Generate RTL for FN.  */
2348
2349 void
2350 expand_or_defer_fn (fn)
2351      tree fn;
2352 {
2353   /* When the parser calls us after finishing the body of a template
2354      function, we don't really want to expand the body.  When we're
2355      processing an in-class definition of an inline function,
2356      PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2357      to look at the function itself.  */
2358   if (processing_template_decl
2359       || (DECL_LANG_SPECIFIC (fn) 
2360           && DECL_TEMPLATE_INFO (fn)
2361           && uses_template_parms (DECL_TI_ARGS (fn))))
2362     {
2363       /* Normally, collection only occurs in rest_of_compilation.  So,
2364          if we don't collect here, we never collect junk generated
2365          during the processing of templates until we hit a
2366          non-template function.  */
2367       ggc_collect ();
2368       return;
2369     }
2370
2371   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2372   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2373                                 simplify_aggr_init_exprs_r,
2374                                 NULL);
2375
2376   /* If this is a constructor or destructor body, we have to clone
2377      it.  */
2378   if (maybe_clone_body (fn))
2379     {
2380       /* We don't want to process FN again, so pretend we've written
2381          it out, even though we haven't.  */
2382       TREE_ASM_WRITTEN (fn) = 1;
2383       return;
2384     }
2385
2386   /* There's no reason to do any of the work here if we're only doing
2387      semantic analysis; this code just generates RTL.  */
2388   if (flag_syntax_only)
2389     return;
2390
2391   if (flag_unit_at_a_time && cgraph_global_info_ready)
2392     abort ();
2393
2394   if (flag_unit_at_a_time && !cgraph_global_info_ready)
2395     {
2396       if (at_eof)
2397         {
2398           /* Compute the appropriate object-file linkage for inline
2399              functions.  */
2400           if (DECL_DECLARED_INLINE_P (fn))
2401             import_export_decl (fn);
2402           cgraph_finalize_function (fn, DECL_SAVED_TREE (fn));
2403         }
2404       else
2405         {
2406           if (!DECL_EXTERNAL (fn))
2407             {
2408               DECL_NOT_REALLY_EXTERN (fn) = 1;
2409               DECL_EXTERNAL (fn) = 1;
2410             }
2411           /* Remember this function.  In finish_file we'll decide if
2412              we actually need to write this function out.  */
2413           defer_fn (fn);
2414           /* Let the back-end know that this function exists.  */
2415           (*debug_hooks->deferred_inline_function) (fn);
2416         }
2417       return;
2418     }
2419
2420
2421   /* If possible, avoid generating RTL for this function.  Instead,
2422      just record it as an inline function, and wait until end-of-file
2423      to decide whether to write it out or not.  */
2424   if (/* We have to generate RTL if it's not an inline function.  */
2425       (DECL_INLINE (fn) || DECL_COMDAT (fn))
2426       /* Or if we have to emit code for inline functions anyhow.  */
2427       && !flag_keep_inline_functions
2428       /* Or if we actually have a reference to the function.  */
2429       && !DECL_NEEDED_P (fn))
2430     {
2431       /* Set DECL_EXTERNAL so that assemble_external will be called as
2432          necessary.  We'll clear it again in finish_file.  */
2433       if (!DECL_EXTERNAL (fn))
2434         {
2435           DECL_NOT_REALLY_EXTERN (fn) = 1;
2436           DECL_EXTERNAL (fn) = 1;
2437         }
2438       /* Remember this function.  In finish_file we'll decide if
2439          we actually need to write this function out.  */
2440       defer_fn (fn);
2441       /* Let the back-end know that this function exists.  */
2442       (*debug_hooks->deferred_inline_function) (fn);
2443       return;
2444     }
2445
2446   expand_body (fn);
2447 }
2448
2449 /* Helper function for walk_tree, used by finish_function to override all
2450    the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2451    value optimization.  */
2452
2453 tree
2454 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
2455 {
2456   tree nrv = (tree) data;
2457
2458   /* No need to walk into types.  There wouldn't be any need to walk into
2459      non-statements, except that we have to consider STMT_EXPRs.  */
2460   if (TYPE_P (*tp))
2461     *walk_subtrees = 0;
2462   else if (TREE_CODE (*tp) == RETURN_STMT)
2463     RETURN_STMT_EXPR (*tp) = NULL_TREE;
2464   else if (TREE_CODE (*tp) == CLEANUP_STMT
2465            && CLEANUP_DECL (*tp) == nrv)
2466     CLEANUP_EH_ONLY (*tp) = 1;
2467
2468   /* Keep iterating.  */
2469   return NULL_TREE;
2470 }
2471
2472 /* Start generating the RTL for FN.  */
2473
2474 static void
2475 genrtl_start_function (tree fn)
2476 {
2477   /* Tell everybody what function we're processing.  */
2478   current_function_decl = fn;
2479   /* Get the RTL machinery going for this function.  */
2480   init_function_start (fn);
2481   /* Let everybody know that we're expanding this function, not doing
2482      semantic analysis.  */
2483   expanding_p = 1;
2484
2485   /* Even though we're inside a function body, we still don't want to
2486      call expand_expr to calculate the size of a variable-sized array.
2487      We haven't necessarily assigned RTL to all variables yet, so it's
2488      not safe to try to expand expressions involving them.  */
2489   immediate_size_expand = 0;
2490   cfun->x_dont_save_pending_sizes_p = 1;
2491
2492   /* Let the user know we're compiling this function.  */
2493   announce_function (fn);
2494
2495   /* Initialize the per-function data.  */
2496   my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2497   if (DECL_SAVED_FUNCTION_DATA (fn))
2498     {
2499       /* If we already parsed this function, and we're just expanding it
2500          now, restore saved state.  */
2501       *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2502
2503       /* This function is being processed in whole-function mode; we
2504          already did semantic analysis.  */
2505       cfun->x_whole_function_mode_p = 1;
2506
2507       /* If we decided that we didn't want to inline this function,
2508          make sure the back-end knows that.  */
2509       if (!current_function_cannot_inline)
2510         current_function_cannot_inline = cp_function_chain->cannot_inline;
2511
2512       /* We don't need the saved data anymore.  Unless this is an inline
2513          function; we need the named return value info for
2514          cp_copy_res_decl_for_inlining.  */
2515       if (! DECL_INLINE (fn))
2516         DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2517     }
2518
2519   /* Keep track of how many functions we're presently expanding.  */
2520   ++function_depth;
2521
2522   /* Create a binding level for the parameters.  */
2523   expand_function_start (fn, /*parms_have_cleanups=*/0);
2524   /* If this function is `main'.  */
2525   if (DECL_MAIN_P (fn))
2526     expand_main_function ();
2527
2528   /* Give our named return value the same RTL as our RESULT_DECL.  */
2529   if (current_function_return_value)
2530     COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2531 }
2532
2533 /* Finish generating the RTL for FN.  */
2534
2535 static void
2536 genrtl_finish_function (tree fn)
2537 {
2538   tree t;
2539
2540 #if 0
2541   if (write_symbols != NO_DEBUG)
2542     {
2543       /* Keep this code around in case we later want to control debug info
2544          based on whether a type is "used".  (jason 1999-11-11) */
2545
2546       tree ttype = target_type (fntype);
2547       tree parmdecl;
2548
2549       if (IS_AGGR_TYPE (ttype))
2550         /* Let debugger know it should output info for this type.  */
2551         note_debug_info_needed (ttype);
2552
2553       for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2554         {
2555           ttype = target_type (TREE_TYPE (parmdecl));
2556           if (IS_AGGR_TYPE (ttype))
2557             /* Let debugger know it should output info for this type.  */
2558             note_debug_info_needed (ttype);
2559         }
2560     }
2561 #endif
2562
2563   /* Clean house because we will need to reorder insns here.  */
2564   do_pending_stack_adjust ();
2565
2566   /* If we have a named return value, we need to force a return so that
2567      the return register is USEd.  */
2568   if (DECL_NAME (DECL_RESULT (fn)))
2569     emit_jump (return_label);
2570
2571   /* We hard-wired immediate_size_expand to zero in start_function.
2572      Expand_function_end will decrement this variable.  So, we set the
2573      variable to one here, so that after the decrement it will remain
2574      zero.  */
2575   immediate_size_expand = 1;
2576
2577   /* Generate rtl for function exit.  */
2578   expand_function_end ();
2579
2580   /* If this is a nested function (like a template instantiation that
2581      we're compiling in the midst of compiling something else), push a
2582      new GC context.  That will keep local variables on the stack from
2583      being collected while we're doing the compilation of this
2584      function.  */
2585   if (function_depth > 1)
2586     ggc_push_context ();
2587
2588   /* There's no need to defer outputting this function any more; we
2589      know we want to output it.  */
2590   DECL_DEFER_OUTPUT (fn) = 0;
2591
2592   /* Run the optimizers and output the assembler code for this
2593      function.  */
2594   rest_of_compilation (fn);
2595
2596   /* Undo the call to ggc_push_context above.  */
2597   if (function_depth > 1)
2598     ggc_pop_context ();
2599
2600 #if 0
2601   /* Keep this code around in case we later want to control debug info
2602      based on whether a type is "used".  (jason 1999-11-11) */
2603
2604   if (ctype && TREE_ASM_WRITTEN (fn))
2605     note_debug_info_needed (ctype);
2606 #endif
2607
2608   /* If this function is marked with the constructor attribute, add it
2609      to the list of functions to be called along with constructors
2610      from static duration objects.  */
2611   if (DECL_STATIC_CONSTRUCTOR (fn))
2612     static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2613
2614   /* If this function is marked with the destructor attribute, add it
2615      to the list of functions to be called along with destructors from
2616      static duration objects.  */
2617   if (DECL_STATIC_DESTRUCTOR (fn))
2618     static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2619
2620   --function_depth;
2621
2622   /* In C++, we should never be saving RTL for the function.  */
2623   my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2624
2625   /* Since we don't need the RTL for this function anymore, stop
2626      pointing to it.  That's especially important for LABEL_DECLs,
2627      since you can reach all the instructions in the function from the
2628      CODE_LABEL stored in the DECL_RTL for the LABEL_DECL.  Walk the
2629      BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2630      local variables.  */
2631   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2632                                 clear_decl_rtl,
2633                                 NULL);
2634
2635   /* Clear out the RTL for the arguments.  */
2636   for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2637     {
2638       SET_DECL_RTL (t, NULL_RTX);
2639       DECL_INCOMING_RTL (t) = NULL_RTX;
2640     }
2641
2642   if (!(flag_inline_trees && DECL_INLINE (fn)))
2643     /* DECL_INITIAL must remain nonzero so we know this was an
2644        actual function definition.  */
2645     DECL_INITIAL (fn) = error_mark_node;
2646   
2647   /* Let the error reporting routines know that we're outside a
2648      function.  For a nested function, this value is used in
2649      pop_cp_function_context and then reset via pop_function_context.  */
2650   current_function_decl = NULL_TREE;
2651 }
2652
2653 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2654    its sub-blocks.  */
2655
2656 static tree
2657 clear_decl_rtl (tree* tp, 
2658                 int* walk_subtrees ATTRIBUTE_UNUSED , 
2659                 void* data ATTRIBUTE_UNUSED )
2660 {
2661   if (nonstatic_local_decl_p (*tp)) 
2662     SET_DECL_RTL (*tp, NULL_RTX);
2663     
2664   return NULL_TREE;
2665 }
2666
2667 /* Perform initialization related to this module.  */
2668
2669 void
2670 init_cp_semantics (void)
2671 {
2672   lang_expand_stmt = cp_expand_stmt;
2673 }
2674
2675 #include "gt-cp-semantics.h"