OSDN Git Service

Finish implementation of VTTs.
[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 Free Software Foundation, Inc.
7    Written by Mark Mitchell (mmitchell@usa.net) based on code found
8    formerly in parse.y and pt.c.  
9
10    This file is part of GNU CC.
11
12    GNU CC is free software; you can redistribute it and/or modify it
13    under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16    
17    GNU CC is distributed in the hope that it will be useful, but
18    WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with GNU CC; see the file COPYING.  If not, write to the Free
24    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25    02111-1307, USA.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "except.h"
32 #include "lex.h"
33 #include "toplev.h"
34 #include "flags.h"
35 #include "ggc.h"
36 #include "rtl.h"
37 #include "output.h"
38
39 /* There routines provide a modular interface to perform many parsing
40    operations.  They may therefore be used during actual parsing, or
41    during template instantiation, which may be regarded as a
42    degenerate form of parsing.  Since the current g++ parser is
43    lacking in several respects, and will be reimplemented, we are
44    attempting to move most code that is not directly related to
45    parsing into this file; that will make implementing the new parser
46    much easier since it will be able to make use of these routines.  */
47
48 static tree expand_cond PARAMS ((tree));
49 static tree maybe_convert_cond PARAMS ((tree));
50 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
51 static void deferred_type_access_control PARAMS ((void));
52
53 /* Record the fact that STMT was the last statement added to the
54    statement tree.  */
55
56 #define SET_LAST_STMT(stmt) \
57   (current_stmt_tree->x_last_stmt = (stmt))
58
59 /* When parsing a template, LAST_TREE contains the last statement
60    parsed.  These are chained together through the TREE_CHAIN field,
61    but often need to be re-organized since the parse is performed
62    bottom-up.  This macro makes LAST_TREE the indicated SUBSTMT of
63    STMT.  */
64
65 #define RECHAIN_STMTS(stmt, substmt)    \
66   do {                                  \
67     substmt = TREE_CHAIN (stmt);        \
68     TREE_CHAIN (stmt) = NULL_TREE;      \
69     SET_LAST_STMT (stmt);               \
70   } while (0)
71
72 /* Finish processing the COND, the SUBSTMT condition for STMT.  */
73
74 #define FINISH_COND(cond, stmt, substmt)                \
75   do {                                                  \
76     if (last_tree != stmt)                              \
77       {                                                 \
78         RECHAIN_STMTS (stmt, substmt);                  \
79         if (!processing_template_decl)                  \
80           {                                             \
81             cond = build_tree_list (substmt, cond);     \
82             substmt = cond;                             \
83           }                                             \
84       }                                                 \
85     else                                                \
86       substmt = cond;                                   \
87   } while (0)
88   
89 /* T is a statement.  Add it to the statement-tree.  */
90
91 void
92 add_tree (t)
93      tree t;
94 {
95   /* Add T to the statement-tree.  */
96   TREE_CHAIN (last_tree) = t;
97   SET_LAST_STMT (t);
98
99   /* When we expand a statement-tree, we must know whether or not the
100      statements are full-expresions.  We record that fact here.  */
101   if (building_stmt_tree ())
102     STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
103 }
104
105 /* COND is the condition-expression for an if, while, etc.,
106    statement.  Convert it to a boolean value, if appropriate.  */
107
108 static tree
109 maybe_convert_cond (cond)
110      tree cond;
111 {
112   /* Empty conditions remain empty.  */
113   if (!cond)
114     return NULL_TREE;
115
116   /* Wait until we instantiate templates before doing conversion.  */
117   if (processing_template_decl)
118     return cond;
119
120   /* Do the conversion.  */
121   cond = convert_from_reference (cond);
122   return condition_conversion (cond);
123 }
124
125 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
126
127 void 
128 finish_expr_stmt (expr)
129      tree expr;
130 {
131   if (expr != NULL_TREE)
132     {
133       if (building_stmt_tree ())
134         {
135           /* Do default conversion if safe and possibly important,
136              in case within ({...}).  */
137           if (!processing_template_decl
138               && !stmts_are_full_exprs_p
139               && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
140                    && lvalue_p (expr))
141                   || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
142             expr = default_conversion (expr);
143
144           if (stmts_are_full_exprs_p)
145             expr = convert_to_void (expr, "statement");
146
147           if (!processing_template_decl)
148             expr = break_out_cleanups (expr);
149
150           add_tree (build_min_nt (EXPR_STMT, expr));
151         }
152       else
153         {
154           emit_line_note (input_filename, lineno);
155
156           if (stmts_are_full_exprs_p)
157             expand_start_target_temps ();
158             
159           cplus_expand_expr_stmt (expr);
160
161           if (stmts_are_full_exprs_p)
162             expand_end_target_temps ();
163         }
164     }
165
166   finish_stmt ();
167
168   /* This was an expression-statement, so we save the type of the
169      expression.  */
170   last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
171 }
172
173 /* Begin an if-statement.  Returns a newly created IF_STMT if
174    appropriate.  */
175
176 tree
177 begin_if_stmt ()
178 {
179   tree r;
180
181   do_pushlevel ();
182
183   if (building_stmt_tree ())
184     {
185       r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
186       add_tree (r);
187     }
188   else
189     r = NULL_TREE;
190
191   return r;
192 }
193
194 /* Process the COND of an if-statement, which may be given by
195    IF_STMT.  */
196
197 void 
198 finish_if_stmt_cond (cond, if_stmt)
199      tree cond;
200      tree if_stmt;
201 {
202   cond = maybe_convert_cond (cond);
203
204   if (building_stmt_tree ())
205     FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
206   else
207     {
208       emit_line_note (input_filename, lineno);
209       expand_start_cond (cond, 0);
210     }
211 }
212
213 /* Finish the then-clause of an if-statement, which may be given by
214    IF_STMT.  */
215
216 tree
217 finish_then_clause (if_stmt)
218      tree if_stmt;
219 {
220   if (building_stmt_tree ())
221     {
222       RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
223       SET_LAST_STMT (if_stmt);
224       return if_stmt;
225     }
226   else
227     return NULL_TREE;
228 }
229
230 /* Begin the else-clause of an if-statement.  */
231
232 void 
233 begin_else_clause ()
234 {
235   if (!building_stmt_tree ())
236     expand_start_else ();
237 }
238
239 /* Finish the else-clause of an if-statement, which may be given by
240    IF_STMT.  */
241
242 void
243 finish_else_clause (if_stmt)
244      tree if_stmt;
245 {
246   if (building_stmt_tree ())
247     RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
248 }
249
250 /* Finsh an if-statement.  */
251
252 void 
253 finish_if_stmt ()
254 {
255   if (!building_stmt_tree ())
256     expand_end_cond ();
257
258   do_poplevel ();
259   finish_stmt ();
260 }
261
262 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
263    appropriate.  */
264
265 tree
266 begin_while_stmt ()
267 {
268   tree r;
269
270   if (building_stmt_tree ())
271     {
272       r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
273       add_tree (r);
274     }
275   else
276     {
277       emit_nop ();
278       emit_line_note (input_filename, lineno);
279       expand_start_loop (1); 
280       r = NULL_TREE;
281     }
282
283   do_pushlevel ();
284
285   return r;
286 }
287
288 /* Process the COND of a while-statement, which may be given by
289    WHILE_STMT.  */
290
291 void 
292 finish_while_stmt_cond (cond, while_stmt)
293      tree cond;
294      tree while_stmt;
295 {
296   cond = maybe_convert_cond (cond);
297
298   if (building_stmt_tree ())
299     FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
300   else
301     {
302       emit_line_note (input_filename, lineno);
303       expand_exit_loop_if_false (0, cond);
304     }
305
306   /* If COND wasn't a declaration, clear out the
307      block we made for it and start a new one here so the
308      optimization in expand_end_loop will work.  */
309   if (getdecls () == NULL_TREE)
310     {
311       do_poplevel ();
312       do_pushlevel ();
313     }
314 }
315
316 /* Finish a while-statement, which may be given by WHILE_STMT.  */
317
318 void 
319 finish_while_stmt (while_stmt)
320      tree while_stmt;
321 {
322   do_poplevel ();
323
324   if (building_stmt_tree ())
325     RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
326   else
327     expand_end_loop ();
328   finish_stmt ();
329 }
330
331 /* Begin a do-statement.  Returns a newly created DO_STMT if
332    appropriate.  */
333
334 tree
335 begin_do_stmt ()
336 {
337   if (building_stmt_tree ())
338     {
339       tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
340       add_tree (r);
341       return r;
342     }
343   else
344     {
345       emit_nop ();
346       emit_line_note (input_filename, lineno);
347       expand_start_loop_continue_elsewhere (1);
348       return NULL_TREE;
349     }
350 }
351
352 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
353
354 void
355 finish_do_body (do_stmt)
356      tree do_stmt;
357 {
358   if (building_stmt_tree ())
359     RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
360   else
361     expand_loop_continue_here ();
362 }
363
364 /* Finish a do-statement, which may be given by DO_STMT, and whose
365    COND is as indicated.  */
366
367 void
368 finish_do_stmt (cond, do_stmt)
369      tree cond;
370      tree do_stmt;
371 {
372   cond = maybe_convert_cond (cond);
373
374   if (building_stmt_tree ())
375     DO_COND (do_stmt) = cond;
376   else
377     {
378       emit_line_note (input_filename, lineno);
379       expand_exit_loop_if_false (0, cond);
380       expand_end_loop ();
381     }
382
383   finish_stmt ();
384 }
385
386 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
387    indicated.  */
388
389 void
390 finish_return_stmt (expr)
391      tree expr;
392 {
393   if (doing_semantic_analysis_p () && !processing_template_decl)
394     expr = check_return_expr (expr);
395
396   if (doing_semantic_analysis_p () && !processing_template_decl)
397     {
398       if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
399         {
400           /* Even returns without a value in a constructor must return
401              `this'.  We accomplish this by sending all returns in a
402              constructor to the CTOR_LABEL; finish_function emits code to
403              return a value there.  When we finally generate the real
404              return statement, CTOR_LABEL is no longer set, and we fall
405              through into the normal return-processing code below.  */
406           finish_goto_stmt (ctor_label);
407           return;
408         }
409       else if (DECL_DESTRUCTOR_P (current_function_decl))
410         {
411           /* Similarly, all destructors must run destructors for
412              base-classes before returning.  So, all returns in a
413              destructor get sent to the DTOR_LABEL; finsh_function emits
414              code to return a value there.  */
415           finish_goto_stmt (dtor_label);
416           return;
417         }
418     }
419
420   if (building_stmt_tree ())
421     add_tree (build_min_nt (RETURN_STMT, expr));
422   else
423     {
424       emit_line_note (input_filename, lineno);
425       c_expand_return (expr);
426     }
427
428   finish_stmt ();
429 }
430
431 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
432
433 tree
434 begin_for_stmt ()
435 {
436   tree r;
437
438   if (building_stmt_tree ())
439     {
440       r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE, 
441                         NULL_TREE, NULL_TREE);
442       add_tree (r);
443     }
444   else
445     r = NULL_TREE;
446
447   if (flag_new_for_scope > 0)
448     {
449       do_pushlevel ();
450       note_level_for_for ();
451     }
452
453   return r;
454 }
455
456 /* Finish the for-init-statement of a for-statement, which may be
457    given by FOR_STMT.  */
458
459 void
460 finish_for_init_stmt (for_stmt)
461      tree for_stmt;
462 {
463   if (building_stmt_tree ())
464     {
465       if (last_tree != for_stmt)
466         RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
467     }
468   else
469     {
470       emit_nop ();
471       emit_line_note (input_filename, lineno);
472       expand_start_loop_continue_elsewhere (1); 
473     }
474
475   do_pushlevel ();
476 }
477
478 /* Finish the COND of a for-statement, which may be given by
479    FOR_STMT.  */
480
481 void
482 finish_for_cond (cond, for_stmt)
483      tree cond;
484      tree for_stmt;
485 {
486   cond = maybe_convert_cond (cond);
487
488   if (building_stmt_tree ())
489     FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
490   else
491     {
492       emit_line_note (input_filename, lineno);
493       if (cond)
494         expand_exit_loop_if_false (0, cond);
495     }
496   
497   /* If the cond wasn't a declaration, clear out the
498      block we made for it and start a new one here so the
499      optimization in expand_end_loop will work.  */
500   if (getdecls () == NULL_TREE)
501     {
502       do_poplevel ();
503       do_pushlevel ();
504     }  
505 }
506
507 /* Finish the increment-EXPRESSION in a for-statement, which may be
508    given by FOR_STMT.  */
509
510 void
511 finish_for_expr (expr, for_stmt)
512      tree expr;
513      tree for_stmt;
514 {
515   if (building_stmt_tree ())
516     FOR_EXPR (for_stmt) = expr;
517 }
518
519 /* Finish the body of a for-statement, which may be given by
520    FOR_STMT.  The increment-EXPR for the loop must be
521    provided.  */
522
523 void
524 finish_for_stmt (expr, for_stmt)
525      tree expr;
526      tree for_stmt;
527 {
528   /* Pop the scope for the body of the loop.  */
529   do_poplevel ();
530
531   if (building_stmt_tree ())
532     RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
533   else
534     {
535       emit_line_note (input_filename, lineno);
536       expand_loop_continue_here ();
537       if (expr) 
538         finish_expr_stmt (expr);
539       expand_end_loop ();
540     }
541
542   if (flag_new_for_scope > 0)
543     do_poplevel ();
544
545   finish_stmt (); 
546 }
547
548 /* Finish a break-statement.  */
549
550 void
551 finish_break_stmt ()
552 {
553   emit_line_note (input_filename, lineno);
554   if (building_stmt_tree ())
555     add_tree (build_min_nt (BREAK_STMT));
556   else if ( ! expand_exit_something ())
557     cp_error ("break statement not within loop or switch");
558 }
559
560 /* Finish a continue-statement.  */
561
562 void
563 finish_continue_stmt ()
564 {
565   emit_line_note (input_filename, lineno);
566   if (building_stmt_tree ())
567     add_tree (build_min_nt (CONTINUE_STMT));
568   else if (! expand_continue_loop (0))
569     cp_error ("continue statement not within a loop");   
570 }
571
572 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
573    appropriate.  */
574
575 tree
576 begin_switch_stmt ()
577 {
578   tree r;
579
580   if (building_stmt_tree ())
581     {
582       r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
583       add_tree (r);
584     }
585   else
586     r = NULL_TREE;
587
588   do_pushlevel ();
589
590   return r;
591 }
592
593 /* Finish the cond of a switch-statement.  */
594
595 void
596 finish_switch_cond (cond, switch_stmt)
597      tree cond;
598      tree switch_stmt;
599 {
600   if (building_stmt_tree ())
601     {
602       if (!processing_template_decl)
603         {
604           /* Convert the condition to an integer or enumeration type.  */
605           cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
606           if (cond == NULL_TREE)
607             {
608               error ("switch quantity not an integer");
609               cond = error_mark_node;
610             }
611           if (cond != error_mark_node)
612             {
613               tree idx;
614               tree type;
615               
616               cond = default_conversion (cond);
617               type = TREE_TYPE (cond);
618               idx = get_unwidened (cond, 0);
619               /* We can't strip a conversion from a signed type to an unsigned,
620                  because if we did, int_fits_type_p would do the wrong thing
621                  when checking case values for being in range,
622                  and it's too hard to do the right thing.  */
623               if (TREE_UNSIGNED (TREE_TYPE (cond)) 
624                   == TREE_UNSIGNED (TREE_TYPE (idx)))
625                 cond = idx;
626
627               cond = fold (build1 (CLEANUP_POINT_EXPR, type, cond));
628             }
629         }
630       FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
631     }
632   else if (cond != error_mark_node)
633     {
634       emit_line_note (input_filename, lineno);
635       c_expand_start_case (cond);
636     }
637   else
638     /* The code is in error, but we don't want expand_end_case to
639        crash. */
640     c_expand_start_case (boolean_false_node);
641
642   push_switch ();
643 }
644
645 /* Finish the body of a switch-statement, which may be given by
646    SWITCH_STMT.  The COND to switch on is indicated.  */
647
648 void
649 finish_switch_stmt (cond, switch_stmt)
650      tree cond;
651      tree switch_stmt;
652 {
653   if (building_stmt_tree ())
654     RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
655   else
656     expand_end_case (cond);
657   pop_switch (); 
658   do_poplevel ();
659   finish_stmt ();
660 }
661
662 /* Finish a case-label.  */
663
664 void 
665 finish_case_label (low_value, high_value)
666      tree low_value;
667      tree high_value;
668 {
669   if (building_stmt_tree ())
670     {
671       /* Add a representation for the case label to the statement
672          tree.  */
673       add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
674       /* And warn about crossing initializations, etc.  */
675       if (!processing_template_decl)
676         define_case_label ();
677       return;
678     }
679
680   do_case (low_value, high_value);
681 }
682
683 /* Finish a goto-statement.  */
684
685 void
686 finish_goto_stmt (destination)
687      tree destination;
688 {
689   if (TREE_CODE (destination) == IDENTIFIER_NODE)
690     destination = lookup_label (destination);
691
692   /* We warn about unused labels with -Wunused.  That means we have to
693      mark the used labels as used.  */
694   if (TREE_CODE (destination) == LABEL_DECL)
695     TREE_USED (destination) = 1;
696     
697   if (building_stmt_tree ())
698     {
699       if (TREE_CODE (destination) != LABEL_DECL)
700         /* We don't inline calls to functions with computed gotos.
701            Those functions are typically up to some funny business,
702            and may be depending on the labels being at particular
703            addresses, or some such.  */
704         DECL_UNINLINABLE (current_function_decl) = 1;
705
706       add_tree (build_min_nt (GOTO_STMT, destination));
707     }
708   else
709     {
710       emit_line_note (input_filename, lineno);
711
712       if (TREE_CODE (destination) == LABEL_DECL)
713         {
714           label_rtx (destination);
715           expand_goto (destination); 
716         }
717       else
718         expand_computed_goto (destination);
719     }
720 }
721
722 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
723    appropriate.  */
724
725 tree
726 begin_try_block ()
727 {
728   if (building_stmt_tree ())
729     {
730       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
731                              NULL_TREE);
732       add_tree (r);
733       return r;
734     }
735   else
736     {
737       emit_line_note (input_filename, lineno);
738       expand_start_try_stmts ();
739       return NULL_TREE;
740     }
741 }
742
743 /* Likewise, for a function-try-block.  */
744
745 tree
746 begin_function_try_block ()
747 {
748   if (building_stmt_tree ())
749     {
750       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
751                              NULL_TREE);
752       FN_TRY_BLOCK_P (r) = 1;
753       add_tree (r);
754       return r;
755     }
756   else
757     {
758       if (! current_function_parms_stored)
759         store_parm_decls ();
760       expand_start_early_try_stmts ();
761       return NULL_TREE;
762     }
763 }
764
765 /* Finish a try-block, which may be given by TRY_BLOCK.  */
766
767 void
768 finish_try_block (try_block)
769      tree try_block;
770 {
771   if (building_stmt_tree ())
772     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
773   else
774     expand_start_all_catch ();  
775 }
776
777 /* Finish the body of a cleanup try-block, which may be given by
778    TRY_BLOCK.  */
779
780 void
781 finish_cleanup_try_block (try_block)
782      tree try_block;
783 {
784   if (building_stmt_tree ())
785     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
786 }
787
788 /* Finish an implicitly generated try-block, with a cleanup is given
789    by CLEANUP.  */
790
791 void
792 finish_cleanup (cleanup, try_block)
793      tree cleanup;
794      tree try_block;
795 {
796   if (building_stmt_tree ()) 
797     {
798       TRY_HANDLERS (try_block) = cleanup;
799       CLEANUP_P (try_block) = 1;
800     }
801   else
802     expand_eh_region_end (protect_with_terminate (cleanup));
803 }
804
805 /* Likewise, for a function-try-block.  */
806
807 void
808 finish_function_try_block (try_block)
809      tree try_block; 
810 {
811   if (building_stmt_tree ())
812     {
813       if (TREE_CHAIN (try_block) 
814           && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
815         {
816           /* Chain the compound statement after the CTOR_INITIALIZER.  */
817           TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
818           /* And make the CTOR_INITIALIZER the body of the try-block.  */
819           RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
820         }
821       else
822         RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
823     }
824   else
825     {
826       end_protect_partials ();
827       expand_start_all_catch ();
828     }
829
830   in_function_try_handler = 1;
831 }
832
833 /* Finish a handler-sequence for a try-block, which may be given by
834    TRY_BLOCK.  */
835
836 void
837 finish_handler_sequence (try_block)
838      tree try_block;
839 {
840   if (building_stmt_tree ())
841     {
842       RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
843       check_handlers (TRY_HANDLERS (try_block));
844     }
845   else
846     expand_end_all_catch ();
847 }
848
849 /* Likewise, for a function-try-block.  */
850
851 void
852 finish_function_handler_sequence (try_block)
853      tree try_block;
854 {
855   in_function_try_handler = 0;
856
857   if (building_stmt_tree ())
858     {
859       RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
860       check_handlers (TRY_HANDLERS (try_block));
861     }
862   else
863     expand_end_all_catch ();
864 }
865
866 /* Begin a handler.  Returns a HANDLER if appropriate.  */
867
868 tree
869 begin_handler ()
870 {
871   tree r;
872
873   if (building_stmt_tree ())
874     {
875       r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
876       add_tree (r);
877     }
878   else
879     r = NULL_TREE;
880
881   do_pushlevel ();
882
883   return r;
884 }
885
886 /* Finish the handler-parameters for a handler, which may be given by
887    HANDLER.  DECL is the declaration for the catch parameter, or NULL
888    if this is a `catch (...)' clause.  */
889
890 tree
891 finish_handler_parms (decl, handler)
892      tree decl;
893      tree handler;
894 {
895   tree blocks = NULL_TREE;
896
897   if (processing_template_decl)
898     {
899       if (decl)
900         {
901           decl = pushdecl (decl);
902           decl = push_template_decl (decl);
903           add_decl_stmt (decl);
904           RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
905         }
906     }
907   else if (building_stmt_tree ())
908     blocks = expand_start_catch_block (decl);
909
910   if (decl)
911     TREE_TYPE (handler) = TREE_TYPE (decl);
912
913   return blocks;
914 }
915
916 /* Note the beginning of a handler for TYPE.  This function is called
917    at the point to which control should be transferred when an
918    appropriately-typed exception is thrown.  */
919
920 void
921 begin_catch_block (type)
922      tree type;
923 {
924   if (building_stmt_tree ())
925     add_tree (build (START_CATCH_STMT, type));
926   else
927     start_catch_handler (type);
928 }
929
930 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
931    the return value from the matching call to finish_handler_parms.  */
932
933 void
934 finish_handler (blocks, handler)
935      tree blocks;
936      tree handler;
937 {
938   if (!processing_template_decl)
939     {
940       if (building_stmt_tree ())
941         expand_end_catch_block (blocks);
942
943       if (!building_stmt_tree ())
944         {
945           /* Fall to outside the try statement when done executing
946              handler and we fall off end of handler.  This is jump
947              Lresume in the documentation.  */
948           expand_goto (top_label_entry (&caught_return_label_stack));
949           end_catch_handler ();
950         }
951     }
952
953   do_poplevel ();
954
955   if (building_stmt_tree ())
956     RECHAIN_STMTS (handler, HANDLER_BODY (handler));
957 }
958
959 /* Begin a compound-statement.  If HAS_NO_SCOPE is non-zero, the
960    compound-statement does not define a scope.  Returns a new
961    COMPOUND_STMT if appropriate.  */
962
963 tree
964 begin_compound_stmt (has_no_scope)
965      int has_no_scope;
966 {
967   tree r; 
968
969   if (building_stmt_tree ())
970     {
971       r = build_min_nt (COMPOUND_STMT, NULL_TREE);
972       add_tree (r);
973       if (has_no_scope)
974         COMPOUND_STMT_NO_SCOPE (r) = 1;
975     }
976   else
977     r = NULL_TREE;
978
979   last_expr_type = NULL_TREE;
980
981   if (!has_no_scope)
982     do_pushlevel ();
983   else
984     /* Normally, we try hard to keep the BLOCK for a
985        statement-expression.  But, if it's a statement-expression with
986        a scopeless block, there's nothing to keep, and we don't want
987        to accidentally keep a block *inside* the scopeless block.  */ 
988     keep_next_level (0);
989
990   /* If this is the outermost block of the function, declare the
991      variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth.  */
992   if (cfun
993       && !current_function_name_declared 
994       && !has_no_scope)
995     {
996       current_function_name_declared = 1;
997       declare_function_name ();
998     }
999
1000   return r;
1001 }
1002
1003
1004 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1005    If HAS_NO_SCOPE is non-zero, the compound statement does not define
1006    a scope.  */
1007
1008 tree
1009 finish_compound_stmt (has_no_scope, compound_stmt)
1010      int has_no_scope;
1011      tree compound_stmt;
1012 {
1013   tree r;
1014   tree t;
1015
1016   if (!has_no_scope)
1017     r = do_poplevel ();
1018   else
1019     r = NULL_TREE;
1020
1021   if (building_stmt_tree ())
1022     RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1023
1024   /* When we call finish_stmt we will lose LAST_EXPR_TYPE.  But, since
1025      the precise purpose of that variable is store the type of the
1026      last expression statement within the last compound statement, we
1027      preserve the value.  */
1028   t = last_expr_type;
1029   finish_stmt ();
1030   last_expr_type = t;
1031
1032   return r;
1033 }
1034
1035 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1036    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1037    CLOBBERS.  */
1038
1039 void
1040 finish_asm_stmt (cv_qualifier, string, output_operands,
1041                  input_operands, clobbers)
1042      tree cv_qualifier;
1043      tree string;
1044      tree output_operands;
1045      tree input_operands;
1046      tree clobbers;
1047 {
1048   if (TREE_CHAIN (string))
1049     string = combine_strings (string);
1050
1051   if (cv_qualifier != NULL_TREE
1052       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1053     {
1054       cp_warning ("%s qualifier ignored on asm",
1055                   IDENTIFIER_POINTER (cv_qualifier));
1056       cv_qualifier = NULL_TREE;
1057     }
1058
1059   if (building_stmt_tree ())
1060     {
1061       tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1062                              output_operands, input_operands,
1063                              clobbers);
1064       add_tree (r);
1065     }
1066   else
1067     {
1068       emit_line_note (input_filename, lineno);
1069       if (output_operands != NULL_TREE || input_operands != NULL_TREE
1070             || clobbers != NULL_TREE)
1071         {
1072           tree t;
1073
1074           for (t = input_operands; t; t = TREE_CHAIN (t))
1075             TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1076
1077           c_expand_asm_operands (string, output_operands,
1078                                  input_operands, 
1079                                  clobbers,
1080                                  cv_qualifier != NULL_TREE,
1081                                  input_filename, lineno);
1082         }
1083       else
1084         expand_asm (string);
1085       
1086       finish_stmt ();
1087     }
1088 }
1089
1090 /* Finish a label with the indicated NAME.  */
1091
1092 void
1093 finish_label_stmt (name)
1094      tree name;
1095 {
1096   tree decl = define_label (input_filename, lineno, name);
1097
1098   if (building_stmt_tree ())
1099     add_tree (build_min_nt (LABEL_STMT, decl));
1100   else if (decl)
1101     expand_label (decl);
1102 }
1103
1104 /* Finish a series of declarations for local labels.  G++ allows users
1105    to declare "local" labels, i.e., labels with scope.  This extension
1106    is useful when writing code involving statement-expressions.  */
1107
1108 void
1109 finish_label_decl (name)
1110      tree name;
1111 {
1112   tree decl = declare_local_label (name);
1113   if (building_stmt_tree ())
1114     add_decl_stmt (decl);
1115 }
1116
1117 /* Create a declaration statement for the declaration given by the
1118    DECL.  */
1119
1120 void
1121 add_decl_stmt (decl)
1122      tree decl;
1123 {
1124   tree decl_stmt;
1125
1126   /* We need the type to last until instantiation time.  */
1127   decl_stmt = build_min_nt (DECL_STMT, decl);
1128   add_tree (decl_stmt);
1129 }
1130
1131 /* We're in a constructor, and have just constructed a a subobject of
1132    *THIS.  CLEANUP is code to run if an exception is thrown before the
1133    end of the current function is reached.   */
1134
1135 void 
1136 finish_subobject (cleanup)
1137      tree cleanup;
1138 {
1139   if (building_stmt_tree ())
1140     {
1141       tree r = build_min_nt (SUBOBJECT, cleanup);
1142       add_tree (r);
1143     }
1144   else
1145     add_partial_entry (cleanup);
1146 }
1147
1148 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1149
1150 void 
1151 finish_decl_cleanup (decl, cleanup)
1152      tree decl;
1153      tree cleanup;
1154 {
1155   if (building_stmt_tree ())
1156     add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1157   else if (!decl 
1158            || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1159     expand_decl_cleanup (decl, cleanup);
1160 }
1161
1162 /* Bind a name and initialization to the return value of
1163    the current function.  */
1164
1165 void
1166 finish_named_return_value (return_id, init)
1167      tree return_id, init;
1168 {
1169   tree decl = DECL_RESULT (current_function_decl);
1170
1171   if (pedantic)
1172     /* Give this error as many times as there are occurrences,
1173        so that users can use Emacs compilation buffers to find
1174        and fix all such places.  */
1175     pedwarn ("ISO C++ does not permit named return values");
1176
1177   if (return_id != NULL_TREE)
1178     {
1179       if (DECL_NAME (decl) == NULL_TREE)
1180         {
1181           DECL_NAME (decl) = return_id;
1182           DECL_ASSEMBLER_NAME (decl) = return_id;
1183         }
1184       else
1185         {
1186           cp_error ("return identifier `%D' already in place", return_id);
1187           return;
1188         }
1189     }
1190
1191   /* Can't let this happen for constructors.  */
1192   if (DECL_CONSTRUCTOR_P (current_function_decl))
1193     {
1194       error ("can't redefine default return value for constructors");
1195       return;
1196     }
1197
1198   /* If we have a named return value, put that in our scope as well.  */
1199   if (DECL_NAME (decl) != NULL_TREE)
1200     {
1201       /* Let `cp_finish_decl' know that this initializer is ok.  */
1202       DECL_INITIAL (decl) = init;
1203       if (doing_semantic_analysis_p ())
1204         pushdecl (decl);
1205
1206       if (building_stmt_tree ())
1207         add_tree (build_min_nt (RETURN_INIT, return_id, init));
1208       else
1209         {
1210           cp_finish_decl (decl, init, NULL_TREE, 0);
1211           store_return_init (decl);
1212         }
1213     }
1214
1215   /* Don't use tree-inlining for functions with named return values.
1216      That doesn't work properly because we don't do any translation of
1217      the RETURN_INITs when they are copied.  */
1218   DECL_UNINLINABLE (current_function_decl) = 1;
1219 }
1220
1221 /* Cache the value of this class's main virtual function table pointer
1222    in a register variable.  This will save one indirection if a
1223    more than one virtual function call is made this function.  */
1224
1225 void
1226 setup_vtbl_ptr ()
1227 {
1228   my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1229
1230   /* If we've already done this, there's no need to do it again.  */
1231   if (vtbls_set_up_p)
1232     return;
1233
1234   if (DECL_CONSTRUCTOR_P (current_function_decl))
1235     {
1236       if (processing_template_decl)
1237         add_tree (build_min_nt
1238                   (CTOR_INITIALIZER,
1239                    current_member_init_list, current_base_init_list));
1240       else
1241         {
1242           tree ctor_stmt;
1243
1244           /* Mark the beginning of the constructor.  */
1245           ctor_stmt = build_min_nt (CTOR_STMT);
1246           CTOR_BEGIN_P (ctor_stmt) = 1;
1247           add_tree (ctor_stmt);
1248           
1249           /* And actually initialize the base-classes and members.  */
1250           emit_base_init ();
1251         }
1252     }
1253   else if (DECL_DESTRUCTOR_P (current_function_decl)
1254            && !processing_template_decl)
1255     {
1256       tree if_stmt;
1257       tree compound_stmt;
1258       int saved_cfnd;
1259
1260       /* If the dtor is empty, and we know there is not any possible
1261          way we could use any vtable entries, before they are possibly
1262          set by a base class dtor, we don't have to setup the vtables,
1263          as we know that any base class dtor will set up any vtables
1264          it needs.  We avoid MI, because one base class dtor can do a
1265          virtual dispatch to an overridden function that would need to
1266          have a non-related vtable set up, we cannot avoid setting up
1267          vtables in that case.  We could change this to see if there
1268          is just one vtable.  */
1269       if_stmt = begin_if_stmt ();
1270
1271       /* If it is not safe to avoid setting up the vtables, then
1272          someone will change the condition to be boolean_true_node.  
1273          (Actually, for now, we do not have code to set the condition
1274          appropriately, so we just assume that we always need to
1275          initialize the vtables.)  */
1276       finish_if_stmt_cond (boolean_true_node, if_stmt);
1277       current_vcalls_possible_p = &IF_COND (if_stmt);
1278
1279       /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1280          open the block for the if-body.  */
1281       saved_cfnd = current_function_name_declared;
1282       current_function_name_declared = 1;
1283       compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1284       current_function_name_declared = saved_cfnd;
1285
1286       /* Make all virtual function table pointers in non-virtual base
1287          classes point to CURRENT_CLASS_TYPE's virtual function
1288          tables.  */
1289       initialize_vtbl_ptrs (current_class_ptr);
1290
1291       finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1292       finish_then_clause (if_stmt);
1293       finish_if_stmt ();
1294     }
1295
1296   /* Always keep the BLOCK node associated with the outermost pair of
1297      curly braces of a function.  These are needed for correct
1298      operation of dwarfout.c.  */
1299   keep_next_level (1);
1300
1301   /* The virtual function tables are set up now.  */
1302   vtbls_set_up_p = 1;
1303 }
1304
1305 /* Add a scope-statement to the statement-tree.  BEGIN_P indicates
1306    whether this statements opens or closes a scope.  PARTIAL_P is true
1307    for a partial scope, i.e, the scope that begins after a label when
1308    an object that needs a cleanup is created.  If BEGIN_P is nonzero,
1309    returns a new TREE_LIST representing the top of the SCOPE_STMT
1310    stack.  The TREE_PURPOSE is the new SCOPE_STMT.  If BEGIN_P is
1311    zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1312    and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1313    SCOPE_BEGIN_P set.  */
1314
1315 tree
1316 add_scope_stmt (begin_p, partial_p)
1317      int begin_p;
1318      int partial_p;
1319 {
1320   tree ss;
1321   tree top;
1322
1323   /* Build the statement.  */
1324   ss = build_min_nt (SCOPE_STMT, NULL_TREE);
1325   SCOPE_BEGIN_P (ss) = begin_p;
1326   SCOPE_PARTIAL_P (ss) = partial_p;
1327
1328   /* Keep the scope stack up to date.  */
1329   if (begin_p)
1330     {
1331       current_scope_stmt_stack 
1332         = tree_cons (ss, NULL_TREE, current_scope_stmt_stack);
1333       top = current_scope_stmt_stack;
1334     }
1335   else
1336     {
1337       top = current_scope_stmt_stack;
1338       TREE_VALUE (top) = ss;
1339       current_scope_stmt_stack = TREE_CHAIN (top);
1340     }
1341
1342   /* Add the new statement to the statement-tree.  */
1343   add_tree (ss);
1344
1345   return top;
1346 }
1347
1348 /* Begin a new scope.  */
1349
1350 void
1351 do_pushlevel ()
1352 {
1353   if (!building_stmt_tree ())
1354     {
1355       emit_line_note (input_filename, lineno);
1356       clear_last_expr ();
1357     }
1358   if (stmts_are_full_exprs_p)
1359     {
1360       pushlevel (0);
1361       if (!building_stmt_tree ()
1362           && !cfun->x_whole_function_mode_p)
1363         my_friendly_abort (19991129);
1364
1365       if (building_stmt_tree () && !processing_template_decl)
1366         add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
1367     }
1368 }
1369
1370 /* Finish a scope.  */
1371
1372 tree
1373 do_poplevel ()
1374 {
1375   tree block = NULL_TREE;
1376
1377   if (stmts_are_full_exprs_p)
1378     {
1379       tree scope_stmts;
1380
1381       if (building_stmt_tree () && !processing_template_decl)
1382         scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
1383       else
1384         scope_stmts = NULL_TREE;
1385
1386       block = poplevel (kept_level_p (), 1, 0);
1387       if (block && !processing_template_decl)
1388         {
1389           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1390           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1391         }
1392     }
1393
1394   return block;
1395 }
1396
1397 /* Finish a parenthesized expression EXPR.  */
1398
1399 tree
1400 finish_parenthesized_expr (expr)
1401      tree expr;
1402 {
1403   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1404     /* This inhibits warnings in truthvalue_conversion.  */
1405     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1406
1407   return expr;
1408 }
1409
1410 /* Begin a statement-expression.  The value returned must be passed to
1411    finish_stmt_expr.  */
1412
1413 tree 
1414 begin_stmt_expr ()
1415 {
1416   /* If we're outside a function, we won't have a statement-tree to
1417      work with.  But, if we see a statement-expression we need to
1418      create one.  */
1419   if (! cfun && !last_tree)
1420     begin_stmt_tree (&scope_chain->x_saved_tree);
1421
1422   keep_next_level (1);
1423   /* If we're building a statement tree, then the upcoming compound
1424      statement will be chained onto the tree structure, starting at
1425      last_tree.  We return last_tree so that we can later unhook the
1426      compound statement.  */
1427   return building_stmt_tree () ? last_tree : expand_start_stmt_expr(); 
1428 }
1429
1430 /* Finish a statement-expression.  RTL_EXPR should be the value
1431    returned by the previous begin_stmt_expr; EXPR is the
1432    statement-expression.  Returns an expression representing the
1433    statement-expression.  */
1434
1435 tree 
1436 finish_stmt_expr (rtl_expr)
1437      tree rtl_expr;
1438 {
1439   tree result;
1440
1441   if (!building_stmt_tree ())
1442     rtl_expr = expand_end_stmt_expr (rtl_expr);
1443
1444   if (building_stmt_tree ())
1445     {
1446       /* If the last thing in the statement-expression was not an
1447          expression-statement, then it has type `void'.  */
1448       if (!last_expr_type)
1449         last_expr_type = void_type_node;
1450       result = build_min (STMT_EXPR, last_expr_type, last_tree);
1451       TREE_SIDE_EFFECTS (result) = 1;
1452       
1453       /* Remove the compound statement from the tree structure; it is
1454          now saved in the STMT_EXPR.  */
1455       SET_LAST_STMT (rtl_expr);
1456       TREE_CHAIN (last_tree) = NULL_TREE;
1457     }
1458   else 
1459     result = rtl_expr;
1460
1461   /* If we created a statement-tree for this statement-expression,
1462      remove it now.  */ 
1463   if (! cfun
1464       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1465     finish_stmt_tree (&scope_chain->x_saved_tree);
1466
1467   return result;
1468 }
1469
1470 /* Finish a call to FN with ARGS.  Returns a representation of the
1471    call.  */
1472
1473 tree 
1474 finish_call_expr (fn, args, koenig)
1475      tree fn;
1476      tree args;
1477      int koenig;
1478 {
1479   tree result;
1480
1481   if (koenig)
1482     {
1483       if (TREE_CODE (fn) == BIT_NOT_EXPR)
1484         fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1485       else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1486         fn = do_identifier (fn, 2, args);
1487     }
1488   result = build_x_function_call (fn, args, current_class_ref);
1489
1490   if (TREE_CODE (result) == CALL_EXPR
1491       && (! TREE_TYPE (result)
1492           || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1493     result = require_complete_type (result);
1494
1495   return result;
1496 }
1497
1498 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1499    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1500    POSTDECREMENT_EXPR.)  */
1501
1502 tree 
1503 finish_increment_expr (expr, code)
1504      tree expr;
1505      enum tree_code code;
1506 {
1507   /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1508      a COMPONENT_REF).  This way if we've got, say, a reference to a
1509      static member that's being operated on, we don't end up trying to
1510      find a member operator for the class it's in.  */
1511
1512   if (TREE_CODE (expr) == OFFSET_REF)
1513     expr = resolve_offset_ref (expr);
1514   return build_x_unary_op (code, expr);  
1515 }
1516
1517 /* Finish a use of `this'.  Returns an expression for `this'.  */
1518
1519 tree 
1520 finish_this_expr ()
1521 {
1522   tree result;
1523
1524   if (current_class_ptr)
1525     {
1526 #ifdef WARNING_ABOUT_CCD
1527       TREE_USED (current_class_ptr) = 1;
1528 #endif
1529       result = current_class_ptr;
1530     }
1531   else if (current_function_decl
1532            && DECL_STATIC_FUNCTION_P (current_function_decl))
1533     {
1534       error ("`this' is unavailable for static member functions");
1535       result = error_mark_node;
1536     }
1537   else
1538     {
1539       if (current_function_decl)
1540         error ("invalid use of `this' in non-member function");
1541       else
1542         error ("invalid use of `this' at top level");
1543       result = error_mark_node;
1544     }
1545
1546   return result;
1547 }
1548
1549 /* Finish a member function call using OBJECT and ARGS as arguments to
1550    FN.  Returns an expression for the call.  */
1551
1552 tree 
1553 finish_object_call_expr (fn, object, args)
1554      tree fn;
1555      tree object;
1556      tree args;
1557 {
1558 #if 0
1559   /* This is a future direction of this code, but because
1560      build_x_function_call cannot always undo what is done in
1561      build_component_ref entirely yet, we cannot do this.  */
1562
1563   tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1564   return finish_call_expr (real_fn, args);
1565 #else
1566   if (DECL_DECLARES_TYPE_P (fn))
1567     {
1568       if (processing_template_decl)
1569         /* This can happen on code like:
1570
1571            class X;
1572            template <class T> void f(T t) {
1573              t.X();
1574            }  
1575
1576            We just grab the underlying IDENTIFIER.  */
1577         fn = DECL_NAME (fn);
1578       else
1579         {
1580           cp_error ("calling type `%T' like a method", fn);
1581           return error_mark_node;
1582         }
1583     }
1584
1585   return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1586 #endif
1587 }
1588
1589 /* Finish a qualified member function call using OBJECT and ARGS as
1590    arguments to FN.  Returns an expressino for the call.  */
1591
1592 tree 
1593 finish_qualified_object_call_expr (fn, object, args)
1594      tree fn;
1595      tree object;
1596      tree args;
1597 {
1598   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1599                                    TREE_OPERAND (fn, 1), args);
1600 }
1601
1602 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1603    being the scope, if any, of DESTRUCTOR.  Returns an expression for
1604    the call.  */
1605
1606 tree 
1607 finish_pseudo_destructor_call_expr (object, scope, destructor)
1608      tree object;
1609      tree scope;
1610      tree destructor;
1611 {
1612   if (processing_template_decl)
1613     return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1614
1615   if (scope && scope != destructor)
1616     cp_error ("destructor specifier `%T::~%T()' must have matching names", 
1617               scope, destructor);
1618
1619   if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1620       && (TREE_CODE (TREE_TYPE (object)) !=
1621           TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1622     cp_error ("`%E' is not of type `%T'", object, destructor);
1623
1624   return cp_convert (void_type_node, object);
1625 }
1626
1627 /* Finish a call to a globally qualified member function FN using
1628    ARGS.  Returns an expression for the call.  */
1629
1630 tree 
1631 finish_qualified_call_expr (fn, args)
1632      tree fn;
1633      tree args;
1634 {
1635   if (processing_template_decl)
1636     return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1637   else
1638     return build_member_call (TREE_OPERAND (fn, 0),
1639                               TREE_OPERAND (fn, 1),
1640                               args);
1641 }
1642
1643 /* Finish an expression taking the address of LABEL.  Returns an
1644    expression for the address.  */
1645
1646 tree 
1647 finish_label_address_expr (label)
1648      tree label;
1649 {
1650   tree result;
1651
1652   label = lookup_label (label);
1653   if (label == NULL_TREE)
1654     result = null_pointer_node;
1655   else
1656     {
1657       TREE_USED (label) = 1;
1658       result = build1 (ADDR_EXPR, ptr_type_node, label);
1659       TREE_CONSTANT (result) = 1;
1660       /* This function cannot be inlined.  All jumps to the addressed
1661          label should wind up at the same point.  */
1662       DECL_UNINLINABLE (current_function_decl) = 1;
1663     }
1664
1665   return result;
1666 }
1667
1668 /* Finish an expression of the form CODE EXPR.  */
1669
1670 tree
1671 finish_unary_op_expr (code, expr)
1672      enum tree_code code;
1673      tree expr;
1674 {
1675   tree result = build_x_unary_op (code, expr);
1676   /* Inside a template, build_x_unary_op does not fold the
1677      expression. So check whether the result is folded before
1678      setting TREE_NEGATED_INT.  */
1679   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1680       && TREE_CODE (result) == INTEGER_CST
1681       && !TREE_UNSIGNED (TREE_TYPE (result))
1682       && INT_CST_LT (result, integer_zero_node))
1683     TREE_NEGATED_INT (result) = 1;
1684   overflow_warning (result);
1685   return result;
1686 }
1687
1688 /* Finish an id-expression.  */
1689
1690 tree
1691 finish_id_expr (expr)
1692      tree expr;
1693 {
1694   if (TREE_CODE (expr) == IDENTIFIER_NODE)
1695     expr = do_identifier (expr, 1, NULL_TREE);
1696
1697   return expr;
1698 }
1699
1700 static tree current_type_lookups;
1701
1702 /* Perform deferred access control for types used in the type of a
1703    declaration.  */
1704
1705 static void
1706 deferred_type_access_control ()
1707 {
1708   tree lookup = type_lookups;
1709
1710   if (lookup == error_mark_node)
1711     return;
1712
1713   for (; lookup; lookup = TREE_CHAIN (lookup))
1714     enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1715 }
1716
1717 void
1718 decl_type_access_control (decl)
1719      tree decl;
1720 {
1721   tree save_fn;
1722
1723   if (type_lookups == error_mark_node)
1724     return;
1725
1726   save_fn = current_function_decl;
1727
1728   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1729     current_function_decl = decl;
1730
1731   deferred_type_access_control ();
1732
1733   current_function_decl = save_fn;
1734   
1735   /* Now strip away the checks for the current declarator; they were
1736      added to type_lookups after typed_declspecs saved the copy that
1737      ended up in current_type_lookups.  */
1738   type_lookups = current_type_lookups;
1739 }
1740
1741 void
1742 save_type_access_control (lookups)
1743      tree lookups;
1744 {
1745   current_type_lookups = lookups;
1746 }
1747
1748 /* Begin a function definition declared with DECL_SPECS and
1749    DECLARATOR.  Returns non-zero if the function-declaration is
1750    legal.  */
1751
1752 int
1753 begin_function_definition (decl_specs, declarator)
1754      tree decl_specs;
1755      tree declarator;
1756 {
1757   tree specs;
1758   tree attrs;
1759
1760   split_specs_attrs (decl_specs, &specs, &attrs);
1761   if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1762     return 0;
1763
1764   deferred_type_access_control ();
1765   type_lookups = error_mark_node;
1766
1767   reinit_parse_for_function ();
1768   /* The things we're about to see are not directly qualified by any
1769      template headers we've seen thus far.  */
1770   reset_specialization ();
1771
1772   return 1;
1773 }
1774
1775 /* Begin a constructor declarator of the form `SCOPE::NAME'.  Returns
1776    a SCOPE_REF.  */
1777
1778 tree 
1779 begin_constructor_declarator (scope, name)
1780      tree scope;
1781      tree name;
1782 {
1783   tree result = build_parse_node (SCOPE_REF, scope, name);
1784   enter_scope_of (result);
1785   return result;
1786 }
1787
1788 /* Finish an init-declarator.  Returns a DECL.  */
1789
1790 tree
1791 finish_declarator (declarator, declspecs, attributes,
1792                    prefix_attributes, initialized)
1793      tree declarator;
1794      tree declspecs;
1795      tree attributes;
1796      tree prefix_attributes;
1797      int initialized;
1798 {
1799   return start_decl (declarator, declspecs, initialized, attributes,
1800                      prefix_attributes); 
1801 }
1802
1803 /* Finish a translation unit.  */
1804
1805 void 
1806 finish_translation_unit ()
1807 {
1808   /* In case there were missing closebraces,
1809      get us back to the global binding level.  */
1810   pop_everything ();
1811   while (current_namespace != global_namespace)
1812     pop_namespace ();
1813   finish_file ();
1814 }
1815
1816 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1817    Returns the parameter.  */
1818
1819 tree 
1820 finish_template_type_parm (aggr, identifier)
1821      tree aggr;
1822      tree identifier;
1823 {
1824   if (aggr != class_type_node)
1825     {
1826       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1827       aggr = class_type_node;
1828     }
1829
1830   return build_tree_list (aggr, identifier);
1831 }
1832
1833 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1834    Returns the parameter.  */
1835
1836 tree 
1837 finish_template_template_parm (aggr, identifier)
1838      tree aggr;
1839      tree identifier;
1840 {
1841   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1842   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1843   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1844   DECL_TEMPLATE_RESULT (tmpl) = decl;
1845   SET_DECL_ARTIFICIAL (decl);
1846   end_template_decl ();
1847
1848   return finish_template_type_parm (aggr, tmpl);
1849 }
1850
1851 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1852    non-zero, the parameter list was terminated by a `...'.  */
1853
1854 tree
1855 finish_parmlist (parms, ellipsis)
1856      tree parms;
1857      int ellipsis;
1858 {
1859   if (!ellipsis)
1860     chainon (parms, void_list_node);
1861   /* We mark the PARMS as a parmlist so that declarator processing can
1862      disambiguate certain constructs.  */
1863   if (parms != NULL_TREE)
1864     TREE_PARMLIST (parms) = 1;
1865
1866   return parms;
1867 }
1868
1869 /* Begin a class definition, as indicated by T.  */
1870
1871 tree
1872 begin_class_definition (t)
1873      tree t;
1874 {
1875   if (t == error_mark_node
1876       || ! IS_AGGR_TYPE (t))
1877     {
1878       t = make_aggr_type (RECORD_TYPE);
1879       pushtag (make_anon_name (), t, 0);
1880     }
1881
1882   /* In a definition of a member class template, we will get here with an
1883      implicit typename, a TYPENAME_TYPE with a type.  */
1884   if (TREE_CODE (t) == TYPENAME_TYPE)
1885     t = TREE_TYPE (t);
1886   
1887   /* If we generated a partial instantiation of this type, but now
1888      we're seeing a real definition, we're actually looking at a
1889      partial specialization.  Consider:
1890
1891        template <class T, class U>
1892        struct Y {};
1893
1894        template <class T>
1895        struct X {};
1896
1897        template <class T, class U>
1898        void f()
1899        {
1900          typename X<Y<T, U> >::A a;
1901        }
1902
1903        template <class T, class U>
1904        struct X<Y<T, U> >
1905        {
1906        };
1907
1908      We have to undo the effects of the previous partial
1909      instantiation.  */
1910   if (PARTIAL_INSTANTIATION_P (t))
1911     {
1912       if (!pedantic) 
1913         {
1914           /* Unfortunately, when we're not in pedantic mode, we
1915              attempt to actually fill in some of the fields of the
1916              partial instantiation, in order to support the implicit
1917              typename extension.  Clear those fields now, in
1918              preparation for the definition here.  The fields cleared
1919              here must match those set in instantiate_class_template.
1920              Look for a comment mentioning begin_class_definition
1921              there.  */
1922           TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1923           TYPE_FIELDS (t) = NULL_TREE;
1924           TYPE_METHODS (t) = NULL_TREE;
1925           CLASSTYPE_TAGS (t) = NULL_TREE;
1926           TYPE_SIZE (t) = NULL_TREE;
1927         }
1928
1929       /* This isn't a partial instantiation any more.  */
1930       PARTIAL_INSTANTIATION_P (t) = 0;
1931     }
1932   /* If this type was already complete, and we see another definition,
1933      that's an error.  */
1934   else if (COMPLETE_TYPE_P (t))
1935     duplicate_tag_error (t);
1936
1937   /* Update the location of the decl.  */
1938   DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1939   DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1940   
1941   if (TYPE_BEING_DEFINED (t))
1942     {
1943       t = make_aggr_type (TREE_CODE (t));
1944       pushtag (TYPE_IDENTIFIER (t), t, 0);
1945     }
1946   maybe_process_partial_specialization (t);
1947   pushclass (t, 1);
1948   TYPE_BEING_DEFINED (t) = 1;
1949   TYPE_PACKED (t) = flag_pack_struct;
1950   /* Reset the interface data, at the earliest possible
1951      moment, as it might have been set via a class foo;
1952      before.  */
1953   {
1954     tree name = TYPE_IDENTIFIER (t);
1955     
1956     if (! ANON_AGGRNAME_P (name))
1957       {
1958         CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1959         SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1960           (t, interface_unknown);
1961       }
1962     
1963     /* Only leave this bit clear if we know this
1964        class is part of an interface-only specification.  */
1965     if (! CLASSTYPE_INTERFACE_KNOWN (t)
1966         || ! CLASSTYPE_INTERFACE_ONLY (t))
1967       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1968   }
1969   reset_specialization();
1970   
1971   /* Make a declaration for this class in its own scope.  */
1972   build_self_reference ();
1973
1974   return t;
1975 }
1976
1977 /* Finish the member declaration given by DECL.  */
1978
1979 void
1980 finish_member_declaration (decl)
1981      tree decl;
1982 {
1983   if (decl == error_mark_node || decl == NULL_TREE)
1984     return;
1985
1986   if (decl == void_type_node)
1987     /* The COMPONENT was a friend, not a member, and so there's
1988        nothing for us to do.  */
1989     return;
1990
1991   /* We should see only one DECL at a time.  */
1992   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1993
1994   /* Set up access control for DECL.  */
1995   TREE_PRIVATE (decl) 
1996     = (current_access_specifier == access_private_node);
1997   TREE_PROTECTED (decl) 
1998     = (current_access_specifier == access_protected_node);
1999   if (TREE_CODE (decl) == TEMPLATE_DECL)
2000     {
2001       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2002       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2003     }
2004
2005   /* Mark the DECL as a member of the current class.  */
2006   DECL_CONTEXT (decl) = current_class_type;
2007
2008   /* [dcl.link]
2009
2010      A C language linkage is ignored for the names of class members
2011      and the member function type of class member functions.  */
2012   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2013     DECL_LANGUAGE (decl) = lang_cplusplus;
2014
2015   /* Put functions on the TYPE_METHODS list and everything else on the
2016      TYPE_FIELDS list.  Note that these are built up in reverse order.
2017      We reverse them (to obtain declaration order) in finish_struct.  */
2018   if (TREE_CODE (decl) == FUNCTION_DECL 
2019       || DECL_FUNCTION_TEMPLATE_P (decl))
2020     {
2021       /* We also need to add this function to the
2022          CLASSTYPE_METHOD_VEC.  */
2023       add_method (current_class_type, 0, decl);
2024
2025       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2026       TYPE_METHODS (current_class_type) = decl;
2027     }
2028   else
2029     {
2030       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2031          go at the beginning.  The reason is that lookup_field_1
2032          searches the list in order, and we want a field name to
2033          override a type name so that the "struct stat hack" will
2034          work.  In particular:
2035
2036            struct S { enum E { }; int E } s;
2037            s.E = 3;
2038
2039          is legal.  In addition, the FIELD_DECLs must be maintained in
2040          declaration order so that class layout works as expected.
2041          However, we don't need that order until class layout, so we
2042          save a little time by putting FIELD_DECLs on in reverse order
2043          here, and then reversing them in finish_struct_1.  (We could
2044          also keep a pointer to the correct insertion points in the
2045          list.)  */
2046
2047       if (TREE_CODE (decl) == TYPE_DECL)
2048         TYPE_FIELDS (current_class_type) 
2049           = chainon (TYPE_FIELDS (current_class_type), decl);
2050       else
2051         {
2052           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2053           TYPE_FIELDS (current_class_type) = decl;
2054         }
2055
2056       /* Enter the DECL into the scope of the class.  */
2057       if (TREE_CODE (decl) != USING_DECL)
2058         pushdecl_class_level (decl);
2059     }
2060 }
2061
2062 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
2063    the definition is immediately followed by a semicolon.  Returns the
2064    type.  */
2065
2066 tree
2067 finish_class_definition (t, attributes, semi, pop_scope_p)
2068      tree t;
2069      tree attributes;
2070      int semi;
2071      int pop_scope_p;
2072 {
2073   /* finish_struct nukes this anyway; if finish_exception does too,
2074      then it can go.  */
2075   if (semi)
2076     note_got_semicolon (t);
2077
2078   /* If we got any attributes in class_head, xref_tag will stick them in
2079      TREE_TYPE of the type.  Grab them now.  */
2080   attributes = chainon (TREE_TYPE (t), attributes);
2081   TREE_TYPE (t) = NULL_TREE;
2082
2083   if (TREE_CODE (t) == ENUMERAL_TYPE)
2084     ;
2085   else
2086     {
2087       t = finish_struct (t, attributes);
2088       if (semi) 
2089         note_got_semicolon (t);
2090     }
2091
2092   if (! semi)
2093     check_for_missing_semicolon (t); 
2094   if (pop_scope_p)
2095     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2096   if (current_scope () == current_function_decl)
2097     do_pending_defargs ();
2098
2099   return t;
2100 }
2101
2102 /* Finish processing the default argument expressions cached during
2103    the processing of a class definition.  */
2104
2105 void
2106 begin_inline_definitions ()
2107 {
2108   if (pending_inlines 
2109       && current_scope () == current_function_decl)
2110     do_pending_inlines ();
2111 }
2112
2113 /* Finish processing the inline function definitions cached during the
2114    processing of a class definition.  */
2115
2116 void
2117 finish_inline_definitions ()
2118 {
2119   if (current_class_type == NULL_TREE)
2120     clear_inline_text_obstack (); 
2121 }
2122
2123 /* Finish processing the declaration of a member class template
2124    TYPES whose template parameters are given by PARMS.  */
2125
2126 tree
2127 finish_member_class_template (types)
2128      tree types;
2129 {
2130   tree t;
2131
2132   /* If there are declared, but undefined, partial specializations
2133      mixed in with the typespecs they will not yet have passed through
2134      maybe_process_partial_specialization, so we do that here.  */
2135   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2136     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2137       maybe_process_partial_specialization (TREE_VALUE (t));
2138
2139   note_list_got_semicolon (types);
2140   grok_x_components (types);
2141   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2142     /* The component was in fact a friend declaration.  We avoid
2143        finish_member_template_decl performing certain checks by
2144        unsetting TYPES.  */
2145     types = NULL_TREE;
2146   
2147   finish_member_template_decl (types);
2148
2149   /* As with other component type declarations, we do
2150      not store the new DECL on the list of
2151      component_decls.  */
2152   return NULL_TREE;
2153 }
2154
2155 /* Finish processsing a complete template declaration.  The PARMS are
2156    the template parameters.  */
2157
2158 void
2159 finish_template_decl (parms)
2160      tree parms;
2161 {
2162   if (parms)
2163     end_template_decl ();
2164   else
2165     end_specialization ();
2166 }
2167
2168 /* Finish processing a a template-id (which names a type) of the form
2169    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2170    template-id.  If ENTERING_SCOPE is non-zero we are about to enter
2171    the scope of template-id indicated.  */
2172
2173 tree
2174 finish_template_type (name, args, entering_scope)
2175      tree name;
2176      tree args;
2177      int entering_scope;
2178 {
2179   tree decl;
2180
2181   decl = lookup_template_class (name, args,
2182                                 NULL_TREE, NULL_TREE, entering_scope);
2183   if (decl != error_mark_node)
2184     decl = TYPE_STUB_DECL (decl);
2185
2186   return decl;
2187 }
2188
2189 /* SR is a SCOPE_REF node.  Enter the scope of SR, whether it is a
2190    namespace scope or a class scope.  */
2191
2192 void
2193 enter_scope_of (sr)
2194      tree sr;
2195 {
2196   tree scope = TREE_OPERAND (sr, 0);
2197
2198   if (TREE_CODE (scope) == NAMESPACE_DECL)
2199     {
2200       push_decl_namespace (scope);
2201       TREE_COMPLEXITY (sr) = -1;
2202     }
2203   else if (scope != current_class_type)
2204     {
2205       if (TREE_CODE (scope) == TYPENAME_TYPE)
2206         {
2207           /* In a declarator for a template class member, the scope will
2208              get here as an implicit typename, a TYPENAME_TYPE with a type.  */
2209           scope = TREE_TYPE (scope);
2210           TREE_OPERAND (sr, 0) = scope;
2211         }
2212       push_nested_class (scope, 3);
2213       TREE_COMPLEXITY (sr) = current_class_depth;
2214     }
2215 }
2216
2217 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2218    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2219    BASE_CLASS, or NULL_TREE if an error occurred.  The
2220    ACCESSS_SPECIFIER is one of
2221    access_{default,public,protected_private}[_virtual]_node.*/
2222
2223 tree 
2224 finish_base_specifier (access_specifier, base_class)
2225      tree access_specifier;
2226      tree base_class;
2227 {
2228   tree type;
2229   tree result;
2230
2231   if (base_class == NULL_TREE)
2232     {
2233       error ("invalid base class");
2234       type = error_mark_node;
2235     }
2236   else
2237     type = TREE_TYPE (base_class);
2238
2239   if (! is_aggr_type (type, 1))
2240     result = NULL_TREE;
2241   else
2242     result = build_tree_list (access_specifier, type);
2243
2244   return result;
2245 }
2246
2247 /* Called when multiple declarators are processed.  If that is not
2248    premitted in this context, an error is issued.  */
2249
2250 void
2251 check_multiple_declarators ()
2252 {
2253   /* [temp]
2254      
2255      In a template-declaration, explicit specialization, or explicit
2256      instantiation the init-declarator-list in the declaration shall
2257      contain at most one declarator.  
2258
2259      We don't just use PROCESSING_TEMPLATE_DECL for the first
2260      condition since that would disallow the perfectly legal code, 
2261      like `template <class T> struct S { int i, j; };'.  */
2262   tree scope = current_scope ();
2263
2264   if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2265     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2266     return;
2267      
2268   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2269       || processing_explicit_instantiation
2270       || processing_specialization)
2271     cp_error ("multiple declarators in template declaration");
2272 }
2273
2274 tree
2275 finish_typeof (expr)
2276      tree expr;
2277 {
2278   if (processing_template_decl)
2279     {
2280       tree t;
2281
2282       t = make_aggr_type (TYPEOF_TYPE);
2283       TYPE_FIELDS (t) = expr;
2284
2285       return t;
2286     }
2287
2288   return TREE_TYPE (expr);
2289 }
2290
2291 /* Create an empty statement tree rooted at T.  */
2292
2293 void
2294 begin_stmt_tree (t)
2295      tree *t;
2296 {
2297   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2298      what follows.  We remove the extraneous statement in
2299      finish_stmt_tree.  */
2300   *t = build_nt (EXPR_STMT, void_zero_node);
2301   SET_LAST_STMT (*t);
2302   last_expr_type = NULL_TREE;
2303 }
2304
2305 /* Finish the statement tree rooted at T.  */
2306
2307 void
2308 finish_stmt_tree (t)
2309      tree *t;
2310 {
2311   tree stmt;
2312   
2313   /* Remove the fake extra statement added in begin_stmt_tree.  */
2314   stmt = TREE_CHAIN (*t);
2315   *t = stmt;
2316   SET_LAST_STMT (NULL_TREE);
2317
2318   if (cfun)
2319     {
2320       /* The line-number recorded in the outermost statement in a function
2321          is the line number of the end of the function.  */
2322       STMT_LINENO (stmt) = lineno;
2323       STMT_LINENO_FOR_FN_P (stmt) = 1;
2324     }
2325 }
2326
2327 /* We're about to expand T, a statement.  Set up appropriate context
2328    for the substitution.  */
2329
2330 void
2331 prep_stmt (t)
2332      tree t;
2333 {
2334   if (!STMT_LINENO_FOR_FN_P (t))
2335     lineno = STMT_LINENO (t);
2336   stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2337 }
2338
2339 /* Some statements, like for-statements or if-statements, require a
2340    condition.  This condition can be a declaration.  If T is such a
2341    declaration it is processed, and an expression appropriate to use
2342    as the condition is returned.  Otherwise, T itself is returned.  */
2343
2344 static tree
2345 expand_cond (t)
2346      tree t;
2347 {
2348   if (t && TREE_CODE (t) == TREE_LIST)
2349     {
2350       expand_stmt (TREE_PURPOSE (t));
2351       return TREE_VALUE (t);
2352     }
2353   else 
2354     return t;
2355 }
2356
2357 /* Generate RTL for the statement T, and its substatements, and any
2358    other statements at its nesting level.  */
2359
2360 tree
2361 expand_stmt (t)
2362      tree t;
2363 {
2364   tree rval = NULL_TREE;
2365
2366   while (t && t != error_mark_node)
2367     {
2368       int saved_stmts_are_full_exprs_p;
2369
2370       /* Assume we'll have nothing to return.  */
2371       rval = NULL_TREE;
2372
2373       /* Set up context appropriately for handling this statement.  */
2374       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2375       prep_stmt (t);
2376
2377       switch (TREE_CODE (t))
2378         {
2379         case RETURN_STMT:
2380           finish_return_stmt (RETURN_EXPR (t));
2381           break;
2382
2383         case EXPR_STMT:
2384           finish_expr_stmt (EXPR_STMT_EXPR (t));
2385           break;
2386
2387         case DECL_STMT:
2388           {
2389             tree decl;
2390
2391             emit_line_note (input_filename, lineno);
2392             decl = DECL_STMT_DECL (t);
2393             /* If this is a declaration for an automatic local
2394                variable, initialize it.  Note that we might also see a
2395                declaration for a namespace-scope object (declared with
2396                `extern').  We don't have to handle the initialization
2397                of those objects here; they can only be declarations,
2398                rather than definitions.  */
2399             if (TREE_CODE (decl) == VAR_DECL 
2400                 && !TREE_STATIC (decl)
2401                 && !DECL_EXTERNAL (decl))
2402               {
2403                 /* Let the back-end know about this variable.  */
2404                 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2405                   emit_local_var (decl);
2406                 else
2407                   expand_anon_union_decl (decl, NULL_TREE, 
2408                                           DECL_ANON_UNION_ELEMS (decl));
2409               }
2410             else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2411               {
2412                 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
2413                   /* Do not emit unused decls. This is not just an
2414                      optimization. We really do not want to emit
2415                      __PRETTY_FUNCTION__ etc, if they're never used.  */
2416                   DECL_IGNORED_P (decl) = 1;
2417                 else
2418                   make_rtl_for_local_static (decl);
2419               }
2420           }
2421           break;
2422
2423         case CLEANUP_STMT:
2424           finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2425           break;
2426
2427         case START_CATCH_STMT:
2428           begin_catch_block (TREE_TYPE (t));
2429           break;
2430
2431         case CTOR_STMT:
2432           if (CTOR_BEGIN_P (t))
2433             begin_protect_partials ();
2434           else
2435             /* After this point, any exceptions will cause the
2436                destructor to be executed, so we no longer need to worry
2437                about destroying the various subobjects ourselves.  */
2438             end_protect_partials ();
2439           break;
2440
2441         case FOR_STMT:
2442           {
2443             tree tmp;
2444
2445             begin_for_stmt ();
2446             expand_stmt (FOR_INIT_STMT (t));
2447             finish_for_init_stmt (NULL_TREE);
2448             finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2449             tmp = FOR_EXPR (t);
2450             finish_for_expr (tmp, NULL_TREE);
2451             expand_stmt (FOR_BODY (t));
2452             finish_for_stmt (tmp, NULL_TREE);
2453           }
2454           break;
2455
2456         case WHILE_STMT:
2457           {
2458             begin_while_stmt ();
2459             finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2460             expand_stmt (WHILE_BODY (t));
2461             finish_while_stmt (NULL_TREE);
2462           }
2463           break;
2464
2465         case DO_STMT:
2466           {
2467             begin_do_stmt ();
2468             expand_stmt (DO_BODY (t));
2469             finish_do_body (NULL_TREE);
2470             finish_do_stmt (DO_COND (t), NULL_TREE);
2471           }
2472           break;
2473
2474         case IF_STMT:
2475           begin_if_stmt ();
2476           finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2477           if (THEN_CLAUSE (t))
2478             {
2479               expand_stmt (THEN_CLAUSE (t));
2480               finish_then_clause (NULL_TREE);
2481             }
2482           if (ELSE_CLAUSE (t))
2483             {
2484               begin_else_clause ();
2485               expand_stmt (ELSE_CLAUSE (t));
2486               finish_else_clause (NULL_TREE);
2487             }
2488           finish_if_stmt ();
2489           break;
2490
2491         case COMPOUND_STMT:
2492           begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2493           expand_stmt (COMPOUND_BODY (t));
2494           rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
2495                                        NULL_TREE);
2496           break;
2497
2498         case BREAK_STMT:
2499           finish_break_stmt ();
2500           break;
2501
2502         case CONTINUE_STMT:
2503           finish_continue_stmt ();
2504           break;
2505
2506         case SWITCH_STMT:
2507           {
2508             tree cond;
2509
2510             begin_switch_stmt ();
2511             cond = expand_cond (SWITCH_COND (t));
2512             finish_switch_cond (cond, NULL_TREE);
2513             expand_stmt (SWITCH_BODY (t));
2514             finish_switch_stmt (cond, NULL_TREE);
2515           }
2516           break;
2517
2518         case CASE_LABEL:
2519           finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2520           break;
2521
2522         case LABEL_STMT:
2523           expand_label (LABEL_STMT_LABEL (t));
2524           break;
2525
2526         case GOTO_STMT:
2527           finish_goto_stmt (GOTO_DESTINATION (t));
2528           break;
2529
2530         case ASM_STMT:
2531           finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2532                            (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2533           break;
2534
2535         case TRY_BLOCK:
2536           if (CLEANUP_P (t))
2537             {
2538               expand_eh_region_start ();
2539               expand_stmt (TRY_STMTS (t));
2540               finish_cleanup_try_block (NULL_TREE);
2541               finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2542             }
2543           else
2544             {
2545               if (FN_TRY_BLOCK_P (t))
2546                 begin_function_try_block ();
2547               else
2548                 begin_try_block ();
2549
2550               expand_stmt (TRY_STMTS (t));
2551
2552               if (FN_TRY_BLOCK_P (t))
2553                 {
2554                   finish_function_try_block (NULL_TREE);
2555                   expand_stmt (TRY_HANDLERS (t));
2556                   finish_function_handler_sequence (NULL_TREE);
2557                 }
2558               else
2559                 {
2560                   finish_try_block (NULL_TREE);
2561                   expand_stmt (TRY_HANDLERS (t));
2562                   finish_handler_sequence (NULL_TREE);
2563                 }
2564             }
2565           break;
2566
2567         case HANDLER:
2568           begin_handler ();
2569           expand_stmt (HANDLER_BODY (t));
2570           finish_handler (NULL_TREE, NULL_TREE);
2571           break;
2572
2573         case SUBOBJECT:
2574           finish_subobject (SUBOBJECT_CLEANUP (t));
2575           break;
2576
2577         case SCOPE_STMT:
2578           if (!SCOPE_NO_CLEANUPS_P (t))
2579             {
2580               if (SCOPE_BEGIN_P (t))
2581                 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
2582                                                  SCOPE_STMT_BLOCK (t));
2583               else if (SCOPE_END_P (t))
2584                 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 
2585                                      SCOPE_PARTIAL_P (t));
2586             }
2587           else if (!SCOPE_NULLIFIED_P (t))
2588             {
2589               rtx note = emit_note (NULL,
2590                                     (SCOPE_BEGIN_P (t) 
2591                                      ? NOTE_INSN_BLOCK_BEG
2592                                      : NOTE_INSN_BLOCK_END));
2593               NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
2594             }
2595               
2596           break;
2597
2598         case RETURN_INIT:
2599           /* Clear this out so that finish_named_return_value can set it
2600              again.  */
2601           DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2602           finish_named_return_value (TREE_OPERAND (t, 0), 
2603                                      TREE_OPERAND (t, 1));
2604           break;
2605
2606         default:
2607           my_friendly_abort (19990810);
2608           break;
2609         }
2610
2611       /* Restore saved state.  */
2612       stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2613
2614       /* Go on to the next statement in this scope.  */
2615       t = TREE_CHAIN (t);
2616     }
2617
2618   return rval;
2619 }
2620
2621 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2622    will equivalent CALL_EXPRs.  */
2623
2624 static tree
2625 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2626      tree *tp;
2627      int *walk_subtrees ATTRIBUTE_UNUSED;
2628      void *data ATTRIBUTE_UNUSED;
2629 {
2630   tree aggr_init_expr;
2631   tree call_expr;
2632   tree fn;
2633   tree args;
2634   tree slot;
2635   tree type;
2636   tree call_type;
2637   int copy_from_buffer_p;
2638
2639   aggr_init_expr = *tp;
2640   /* We don't need to walk into types; there's nothing in a type that
2641      needs simplification.  (And, furthermore, there are places we
2642      actively don't want to go.  For example, we don't want to wander
2643      into the default arguments for a FUNCTION_DECL that appears in a
2644      CALL_EXPR.)  */
2645   if (TYPE_P (aggr_init_expr))
2646     {
2647       *walk_subtrees = 0;
2648       return NULL_TREE;
2649     }
2650   /* Only AGGR_INIT_EXPRs are interesting.  */
2651   else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2652     return NULL_TREE;
2653
2654   /* Form an appropriate CALL_EXPR.  */
2655   fn = TREE_OPERAND (aggr_init_expr, 0);
2656   args = TREE_OPERAND (aggr_init_expr, 1);
2657   slot = TREE_OPERAND (aggr_init_expr, 2);
2658   type = TREE_TYPE (aggr_init_expr);
2659   call_type = type;
2660   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2661     {
2662       /* Replace the first argument with the address of the third
2663          argument to the AGGR_INIT_EXPR.  */
2664       call_type = build_pointer_type (type);
2665       mark_addressable (slot);
2666       args = tree_cons (NULL_TREE, build1 (ADDR_EXPR, call_type, slot),
2667                         TREE_CHAIN (args));
2668     }
2669   call_expr = build (CALL_EXPR, call_type, fn, args, NULL_TREE);
2670   TREE_SIDE_EFFECTS (call_expr) = 1;
2671
2672   /* If we're using the non-reentrant PCC calling convention, then we
2673      need to copy the returned value out of the static buffer into the
2674      SLOT.  */
2675   copy_from_buffer_p = 0;
2676 #ifdef PCC_STATIC_STRUCT_RETURN  
2677   if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2678     {
2679       int old_ac;
2680
2681       flag_access_control = 0;
2682       call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2683       flag_access_control = old_ac;
2684       copy_from_buffer_p = 1;
2685     }
2686 #endif
2687
2688   /* If this AGGR_INIT_EXPR indicates the value returned by a
2689      function, then we want to use the value of the initialized
2690      location as the result.  */
2691   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2692     {
2693       call_expr = build (COMPOUND_EXPR, type,
2694                          call_expr, slot);
2695       TREE_SIDE_EFFECTS (call_expr) = 1;
2696     }
2697
2698   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2699   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2700   *tp = call_expr;
2701
2702   /* Keep iterating.  */
2703   return NULL_TREE;
2704 }
2705
2706 /* Generate RTL for FN.  */
2707
2708 void
2709 expand_body (fn)
2710      tree fn;
2711 {
2712   int saved_lineno;
2713   const char *saved_input_filename;
2714
2715   /* When the parser calls us after finishing the body of a template
2716      function, we don't really want to expand the body.  When we're
2717      processing an in-class definition of an inline function,
2718      PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2719      to look at the function itself.  */
2720   if (processing_template_decl
2721       || (DECL_LANG_SPECIFIC (fn) 
2722           && DECL_TEMPLATE_INFO (fn)
2723           && uses_template_parms (DECL_TI_ARGS (fn))))
2724     {
2725       /* Normally, collection only occurs in rest_of_compilation.  So,
2726          if we don't collect here, we never collect junk generated
2727          during the processing of templates until we hit a
2728          non-template function.  */
2729       ggc_collect ();
2730       return;
2731     }
2732
2733   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2734   walk_tree (&DECL_SAVED_TREE (fn), simplify_aggr_init_exprs_r, NULL);
2735
2736   /* If this is a constructor or destructor body, we have to clone it
2737      under the new ABI.  */
2738   if (maybe_clone_body (fn))
2739     {
2740       /* We don't want to process FN again, so pretend we've written
2741          it out, even though we haven't.  */
2742       TREE_ASM_WRITTEN (fn) = 1;
2743       return;
2744     }
2745
2746   /* There's no reason to do any of the work here if we're only doing
2747      semantic analysis; this code just generates RTL.  */
2748   if (flag_syntax_only)
2749     return;
2750
2751   /* If possible, avoid generating RTL for this function.  Instead,
2752      just record it as an inline function, and wait until end-of-file
2753      to decide whether to write it out or not.  */
2754   if (/* We have to generate RTL if it's not an inline function.  */
2755       (DECL_INLINE (fn) || DECL_COMDAT (fn))
2756       /* Or if we have to keep all inline functions anyhow.  */
2757       && !flag_keep_inline_functions
2758       /* Or if we actually have a reference to the function.  */
2759       && !DECL_NEEDED_P (fn)
2760       /* Or if this is a nested function.  */
2761       && !decl_function_context (fn))
2762     {
2763       /* Give the function RTL now so that we can assign it to a
2764          function pointer, etc.  */
2765       make_function_rtl (fn);
2766       /* Set DECL_EXTERNAL so that assemble_external will be called as
2767          necessary.  We'll clear it again in finish_file.  */
2768       if (!DECL_EXTERNAL (fn))
2769         {
2770           DECL_NOT_REALLY_EXTERN (fn) = 1;
2771           DECL_EXTERNAL (fn) = 1;
2772         }
2773       /* Remember this function.  In finish_file we'll decide if
2774          we actually need to write this function out.  */
2775       defer_fn (fn);
2776       /* Let the back-end know that this funtion exists.  */
2777       note_deferral_of_defined_inline_function (fn);
2778       return;
2779     }
2780
2781   /* Optimize the body of the function before expanding it.  */
2782   optimize_function (fn);
2783
2784   /* Save the current file name and line number.  When we expand the
2785      body of the function, we'll set LINENO and INPUT_FILENAME so that
2786      error-mesages come out in the right places.  */
2787   saved_lineno = lineno;
2788   saved_input_filename = input_filename;
2789   lineno = DECL_SOURCE_LINE (fn);
2790   input_filename = DECL_SOURCE_FILE (fn);
2791
2792   start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2793   store_parm_decls ();
2794   current_function_is_thunk = DECL_THUNK_P (fn);
2795
2796   /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2797      any of the other magic variables we set up when starting a
2798      function body.  */
2799   current_function_name_declared = 1;
2800
2801   /* Expand the body.  */
2802   expand_stmt (DECL_SAVED_TREE (fn));
2803
2804   /* Statements should always be full-expressions at the outermost set
2805      of curly braces for a function.  */
2806   my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2807
2808   /* The outermost statement for a function contains the line number
2809      recorded when we finished processing the function.  */
2810   lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2811
2812   /* Generate code for the function.  */
2813   finish_function (0);
2814
2815   /* If possible, obliterate the body of the function so that it can
2816      be garbage collected.  */
2817   if (flag_dump_translation_unit)
2818     /* Keep the body; we're going to dump it.  */
2819     ;
2820   else if (DECL_INLINE (fn) && flag_inline_trees)
2821     /* We might need the body of this function so that we can expand
2822        it inline somewhere else.  */
2823     ;
2824   else
2825     /* We don't need the body; blow it away.  */
2826     DECL_SAVED_TREE (fn) = NULL_TREE;
2827
2828   /* And restore the current source position.  */
2829   lineno = saved_lineno;
2830   input_filename = saved_input_filename;
2831 }