OSDN Git Service

* class.c (indirect_primary_base_p): New function.
[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       check_goto (destination);
707
708       add_tree (build_min_nt (GOTO_STMT, destination));
709     }
710   else
711     {
712       emit_line_note (input_filename, lineno);
713
714       if (TREE_CODE (destination) == LABEL_DECL)
715         {
716           label_rtx (destination);
717           expand_goto (destination); 
718         }
719       else
720         expand_computed_goto (destination);
721     }
722 }
723
724 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
725    appropriate.  */
726
727 tree
728 begin_try_block ()
729 {
730   if (building_stmt_tree ())
731     {
732       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
733                              NULL_TREE);
734       add_tree (r);
735       return r;
736     }
737   else
738     {
739       emit_line_note (input_filename, lineno);
740       expand_start_try_stmts ();
741       return NULL_TREE;
742     }
743 }
744
745 /* Likewise, for a function-try-block.  */
746
747 tree
748 begin_function_try_block ()
749 {
750   if (building_stmt_tree ())
751     {
752       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
753                              NULL_TREE);
754       FN_TRY_BLOCK_P (r) = 1;
755       add_tree (r);
756       return r;
757     }
758   else
759     {
760       if (! current_function_parms_stored)
761         store_parm_decls ();
762       expand_start_early_try_stmts ();
763       return NULL_TREE;
764     }
765 }
766
767 /* Finish a try-block, which may be given by TRY_BLOCK.  */
768
769 void
770 finish_try_block (try_block)
771      tree try_block;
772 {
773   if (building_stmt_tree ())
774     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
775   else
776     expand_start_all_catch ();  
777 }
778
779 /* Finish the body of a cleanup try-block, which may be given by
780    TRY_BLOCK.  */
781
782 void
783 finish_cleanup_try_block (try_block)
784      tree try_block;
785 {
786   if (building_stmt_tree ())
787     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
788 }
789
790 /* Finish an implicitly generated try-block, with a cleanup is given
791    by CLEANUP.  */
792
793 void
794 finish_cleanup (cleanup, try_block)
795      tree cleanup;
796      tree try_block;
797 {
798   if (building_stmt_tree ()) 
799     {
800       TRY_HANDLERS (try_block) = cleanup;
801       CLEANUP_P (try_block) = 1;
802     }
803   else
804     expand_eh_region_end (protect_with_terminate (cleanup));
805 }
806
807 /* Likewise, for a function-try-block.  */
808
809 void
810 finish_function_try_block (try_block)
811      tree try_block; 
812 {
813   if (building_stmt_tree ())
814     {
815       if (TREE_CHAIN (try_block) 
816           && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
817         {
818           /* Chain the compound statement after the CTOR_INITIALIZER.  */
819           TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
820           /* And make the CTOR_INITIALIZER the body of the try-block.  */
821           RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
822         }
823       else
824         RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
825     }
826   else
827     {
828       end_protect_partials ();
829       expand_start_all_catch ();
830     }
831
832   in_function_try_handler = 1;
833 }
834
835 /* Finish a handler-sequence for a try-block, which may be given by
836    TRY_BLOCK.  */
837
838 void
839 finish_handler_sequence (try_block)
840      tree try_block;
841 {
842   if (building_stmt_tree ())
843     {
844       RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
845       check_handlers (TRY_HANDLERS (try_block));
846     }
847   else
848     expand_end_all_catch ();
849 }
850
851 /* Likewise, for a function-try-block.  */
852
853 void
854 finish_function_handler_sequence (try_block)
855      tree try_block;
856 {
857   in_function_try_handler = 0;
858
859   if (building_stmt_tree ())
860     {
861       RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
862       check_handlers (TRY_HANDLERS (try_block));
863     }
864   else
865     expand_end_all_catch ();
866 }
867
868 /* Begin a handler.  Returns a HANDLER if appropriate.  */
869
870 tree
871 begin_handler ()
872 {
873   tree r;
874
875   if (building_stmt_tree ())
876     {
877       r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
878       add_tree (r);
879     }
880   else
881     r = NULL_TREE;
882
883   do_pushlevel ();
884
885   return r;
886 }
887
888 /* Finish the handler-parameters for a handler, which may be given by
889    HANDLER.  DECL is the declaration for the catch parameter, or NULL
890    if this is a `catch (...)' clause.  */
891
892 tree
893 finish_handler_parms (decl, handler)
894      tree decl;
895      tree handler;
896 {
897   tree blocks = NULL_TREE;
898
899   if (processing_template_decl)
900     {
901       if (decl)
902         {
903           decl = pushdecl (decl);
904           decl = push_template_decl (decl);
905           add_decl_stmt (decl);
906           RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
907         }
908     }
909   else if (building_stmt_tree ())
910     blocks = expand_start_catch_block (decl);
911
912   if (decl)
913     TREE_TYPE (handler) = TREE_TYPE (decl);
914
915   return blocks;
916 }
917
918 /* Note the beginning of a handler for TYPE.  This function is called
919    at the point to which control should be transferred when an
920    appropriately-typed exception is thrown.  */
921
922 void
923 begin_catch_block (type)
924      tree type;
925 {
926   if (building_stmt_tree ())
927     add_tree (build (START_CATCH_STMT, type));
928   else
929     start_catch_handler (type);
930 }
931
932 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
933    the return value from the matching call to finish_handler_parms.  */
934
935 void
936 finish_handler (blocks, handler)
937      tree blocks;
938      tree handler;
939 {
940   if (!processing_template_decl)
941     {
942       if (building_stmt_tree ())
943         expand_end_catch_block (blocks);
944
945       if (!building_stmt_tree ())
946         {
947           /* Fall to outside the try statement when done executing
948              handler and we fall off end of handler.  This is jump
949              Lresume in the documentation.  */
950           expand_goto (top_label_entry (&caught_return_label_stack));
951           end_catch_handler ();
952         }
953     }
954
955   do_poplevel ();
956
957   if (building_stmt_tree ())
958     RECHAIN_STMTS (handler, HANDLER_BODY (handler));
959 }
960
961 /* Begin a compound-statement.  If HAS_NO_SCOPE is non-zero, the
962    compound-statement does not define a scope.  Returns a new
963    COMPOUND_STMT if appropriate.  */
964
965 tree
966 begin_compound_stmt (has_no_scope)
967      int has_no_scope;
968 {
969   tree r; 
970   int is_try = 0;
971
972   if (building_stmt_tree ())
973     {
974       r = build_min_nt (COMPOUND_STMT, NULL_TREE);
975       /* Mark that this block is for a try so that we can yell at
976          people trying to jump in.  */
977       if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
978         is_try = 1;
979       add_tree (r);
980       if (has_no_scope)
981         COMPOUND_STMT_NO_SCOPE (r) = 1;
982     }
983   else
984     r = NULL_TREE;
985
986   last_expr_type = NULL_TREE;
987
988   if (!has_no_scope)
989     {
990       do_pushlevel ();
991       if (is_try)
992         note_level_for_eh ();
993     }
994   else
995     /* Normally, we try hard to keep the BLOCK for a
996        statement-expression.  But, if it's a statement-expression with
997        a scopeless block, there's nothing to keep, and we don't want
998        to accidentally keep a block *inside* the scopeless block.  */ 
999     keep_next_level (0);
1000
1001   /* If this is the outermost block of the function, declare the
1002      variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth.  */
1003   if (cfun
1004       && !current_function_name_declared 
1005       && !has_no_scope)
1006     {
1007       current_function_name_declared = 1;
1008       declare_function_name ();
1009     }
1010
1011   return r;
1012 }
1013
1014
1015 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1016    If HAS_NO_SCOPE is non-zero, the compound statement does not define
1017    a scope.  */
1018
1019 tree
1020 finish_compound_stmt (has_no_scope, compound_stmt)
1021      int has_no_scope;
1022      tree compound_stmt;
1023 {
1024   tree r;
1025   tree t;
1026
1027   if (!has_no_scope)
1028     r = do_poplevel ();
1029   else
1030     r = NULL_TREE;
1031
1032   if (building_stmt_tree ())
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 void
1051 finish_asm_stmt (cv_qualifier, string, output_operands,
1052                  input_operands, clobbers)
1053      tree cv_qualifier;
1054      tree string;
1055      tree output_operands;
1056      tree input_operands;
1057      tree clobbers;
1058 {
1059   if (TREE_CHAIN (string))
1060     string = combine_strings (string);
1061
1062   if (cv_qualifier != NULL_TREE
1063       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1064     {
1065       cp_warning ("%s qualifier ignored on asm",
1066                   IDENTIFIER_POINTER (cv_qualifier));
1067       cv_qualifier = NULL_TREE;
1068     }
1069
1070   if (building_stmt_tree ())
1071     {
1072       tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1073                              output_operands, input_operands,
1074                              clobbers);
1075       add_tree (r);
1076     }
1077   else
1078     {
1079       emit_line_note (input_filename, lineno);
1080       if (output_operands != NULL_TREE || input_operands != NULL_TREE
1081             || clobbers != NULL_TREE)
1082         {
1083           tree t;
1084
1085           for (t = input_operands; t; t = TREE_CHAIN (t))
1086             TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1087
1088           c_expand_asm_operands (string, output_operands,
1089                                  input_operands, 
1090                                  clobbers,
1091                                  cv_qualifier != NULL_TREE,
1092                                  input_filename, lineno);
1093         }
1094       else
1095         expand_asm (string);
1096       
1097       finish_stmt ();
1098     }
1099 }
1100
1101 /* Finish a label with the indicated NAME.  */
1102
1103 void
1104 finish_label_stmt (name)
1105      tree name;
1106 {
1107   tree decl = define_label (input_filename, lineno, name);
1108
1109   if (building_stmt_tree ())
1110     add_tree (build_min_nt (LABEL_STMT, decl));
1111   else if (decl)
1112     expand_label (decl);
1113 }
1114
1115 /* Finish a series of declarations for local labels.  G++ allows users
1116    to declare "local" labels, i.e., labels with scope.  This extension
1117    is useful when writing code involving statement-expressions.  */
1118
1119 void
1120 finish_label_decl (name)
1121      tree name;
1122 {
1123   tree decl = declare_local_label (name);
1124   if (building_stmt_tree ())
1125     add_decl_stmt (decl);
1126 }
1127
1128 /* Create a declaration statement for the declaration given by the
1129    DECL.  */
1130
1131 void
1132 add_decl_stmt (decl)
1133      tree decl;
1134 {
1135   tree decl_stmt;
1136
1137   /* We need the type to last until instantiation time.  */
1138   decl_stmt = build_min_nt (DECL_STMT, decl);
1139   add_tree (decl_stmt);
1140 }
1141
1142 /* We're in a constructor, and have just constructed a a subobject of
1143    *THIS.  CLEANUP is code to run if an exception is thrown before the
1144    end of the current function is reached.   */
1145
1146 void 
1147 finish_subobject (cleanup)
1148      tree cleanup;
1149 {
1150   if (building_stmt_tree ())
1151     {
1152       tree r = build_min_nt (SUBOBJECT, cleanup);
1153       add_tree (r);
1154     }
1155   else
1156     add_partial_entry (cleanup);
1157 }
1158
1159 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1160
1161 void 
1162 finish_decl_cleanup (decl, cleanup)
1163      tree decl;
1164      tree cleanup;
1165 {
1166   if (building_stmt_tree ())
1167     add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1168   else if (!decl 
1169            || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1170     expand_decl_cleanup (decl, cleanup);
1171 }
1172
1173 /* Bind a name and initialization to the return value of
1174    the current function.  */
1175
1176 void
1177 finish_named_return_value (return_id, init)
1178      tree return_id, init;
1179 {
1180   tree decl = DECL_RESULT (current_function_decl);
1181
1182   if (pedantic)
1183     /* Give this error as many times as there are occurrences,
1184        so that users can use Emacs compilation buffers to find
1185        and fix all such places.  */
1186     pedwarn ("ISO C++ does not permit named return values");
1187
1188   if (return_id != NULL_TREE)
1189     {
1190       if (DECL_NAME (decl) == NULL_TREE)
1191         {
1192           DECL_NAME (decl) = return_id;
1193           DECL_ASSEMBLER_NAME (decl) = return_id;
1194         }
1195       else
1196         {
1197           cp_error ("return identifier `%D' already in place", return_id);
1198           return;
1199         }
1200     }
1201
1202   /* Can't let this happen for constructors.  */
1203   if (DECL_CONSTRUCTOR_P (current_function_decl))
1204     {
1205       error ("can't redefine default return value for constructors");
1206       return;
1207     }
1208
1209   /* If we have a named return value, put that in our scope as well.  */
1210   if (DECL_NAME (decl) != NULL_TREE)
1211     {
1212       /* Let `cp_finish_decl' know that this initializer is ok.  */
1213       DECL_INITIAL (decl) = init;
1214       if (doing_semantic_analysis_p ())
1215         pushdecl (decl);
1216
1217       if (building_stmt_tree ())
1218         add_tree (build_min_nt (RETURN_INIT, return_id, init));
1219       else
1220         {
1221           cp_finish_decl (decl, init, NULL_TREE, 0);
1222           store_return_init (decl);
1223         }
1224     }
1225
1226   /* Don't use tree-inlining for functions with named return values.
1227      That doesn't work properly because we don't do any translation of
1228      the RETURN_INITs when they are copied.  */
1229   DECL_UNINLINABLE (current_function_decl) = 1;
1230 }
1231
1232 /* Cache the value of this class's main virtual function table pointer
1233    in a register variable.  This will save one indirection if a
1234    more than one virtual function call is made this function.  */
1235
1236 void
1237 setup_vtbl_ptr ()
1238 {
1239   my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1240
1241   /* If we've already done this, there's no need to do it again.  */
1242   if (vtbls_set_up_p)
1243     return;
1244
1245   if (DECL_CONSTRUCTOR_P (current_function_decl))
1246     {
1247       if (processing_template_decl)
1248         add_tree (build_min_nt
1249                   (CTOR_INITIALIZER,
1250                    current_member_init_list, current_base_init_list));
1251       else
1252         {
1253           tree ctor_stmt;
1254
1255           /* Mark the beginning of the constructor.  */
1256           ctor_stmt = build_min_nt (CTOR_STMT);
1257           CTOR_BEGIN_P (ctor_stmt) = 1;
1258           add_tree (ctor_stmt);
1259           
1260           /* And actually initialize the base-classes and members.  */
1261           emit_base_init ();
1262         }
1263     }
1264   else if (DECL_DESTRUCTOR_P (current_function_decl)
1265            && !processing_template_decl)
1266     {
1267       tree if_stmt;
1268       tree compound_stmt;
1269       int saved_cfnd;
1270
1271       /* If the dtor is empty, and we know there is not any possible
1272          way we could use any vtable entries, before they are possibly
1273          set by a base class dtor, we don't have to setup the vtables,
1274          as we know that any base class dtor will set up any vtables
1275          it needs.  We avoid MI, because one base class dtor can do a
1276          virtual dispatch to an overridden function that would need to
1277          have a non-related vtable set up, we cannot avoid setting up
1278          vtables in that case.  We could change this to see if there
1279          is just one vtable.  */
1280       if_stmt = begin_if_stmt ();
1281
1282       /* If it is not safe to avoid setting up the vtables, then
1283          someone will change the condition to be boolean_true_node.  
1284          (Actually, for now, we do not have code to set the condition
1285          appropriately, so we just assume that we always need to
1286          initialize the vtables.)  */
1287       finish_if_stmt_cond (boolean_true_node, if_stmt);
1288       current_vcalls_possible_p = &IF_COND (if_stmt);
1289
1290       /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1291          open the block for the if-body.  */
1292       saved_cfnd = current_function_name_declared;
1293       current_function_name_declared = 1;
1294       compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1295       current_function_name_declared = saved_cfnd;
1296
1297       /* Make all virtual function table pointers in non-virtual base
1298          classes point to CURRENT_CLASS_TYPE's virtual function
1299          tables.  */
1300       initialize_vtbl_ptrs (current_class_ptr);
1301
1302       finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1303       finish_then_clause (if_stmt);
1304       finish_if_stmt ();
1305     }
1306
1307   /* Always keep the BLOCK node associated with the outermost pair of
1308      curly braces of a function.  These are needed for correct
1309      operation of dwarfout.c.  */
1310   keep_next_level (1);
1311
1312   /* The virtual function tables are set up now.  */
1313   vtbls_set_up_p = 1;
1314 }
1315
1316 /* Add a scope-statement to the statement-tree.  BEGIN_P indicates
1317    whether this statements opens or closes a scope.  PARTIAL_P is true
1318    for a partial scope, i.e, the scope that begins after a label when
1319    an object that needs a cleanup is created.  If BEGIN_P is nonzero,
1320    returns a new TREE_LIST representing the top of the SCOPE_STMT
1321    stack.  The TREE_PURPOSE is the new SCOPE_STMT.  If BEGIN_P is
1322    zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1323    and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1324    SCOPE_BEGIN_P set.  */
1325
1326 tree
1327 add_scope_stmt (begin_p, partial_p)
1328      int begin_p;
1329      int partial_p;
1330 {
1331   tree ss;
1332   tree top;
1333
1334   /* Build the statement.  */
1335   ss = build_min_nt (SCOPE_STMT, NULL_TREE);
1336   SCOPE_BEGIN_P (ss) = begin_p;
1337   SCOPE_PARTIAL_P (ss) = partial_p;
1338
1339   /* Keep the scope stack up to date.  */
1340   if (begin_p)
1341     {
1342       current_scope_stmt_stack 
1343         = tree_cons (ss, NULL_TREE, current_scope_stmt_stack);
1344       top = current_scope_stmt_stack;
1345     }
1346   else
1347     {
1348       top = current_scope_stmt_stack;
1349       TREE_VALUE (top) = ss;
1350       current_scope_stmt_stack = TREE_CHAIN (top);
1351     }
1352
1353   /* Add the new statement to the statement-tree.  */
1354   add_tree (ss);
1355
1356   return top;
1357 }
1358
1359 /* Begin a new scope.  */
1360
1361 void
1362 do_pushlevel ()
1363 {
1364   if (!building_stmt_tree ())
1365     {
1366       emit_line_note (input_filename, lineno);
1367       clear_last_expr ();
1368     }
1369   if (stmts_are_full_exprs_p)
1370     {
1371       pushlevel (0);
1372       if (!building_stmt_tree ()
1373           && !cfun->x_whole_function_mode_p)
1374         my_friendly_abort (19991129);
1375
1376       if (building_stmt_tree () && !processing_template_decl)
1377         add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
1378     }
1379 }
1380
1381 /* Finish a scope.  */
1382
1383 tree
1384 do_poplevel ()
1385 {
1386   tree block = NULL_TREE;
1387
1388   if (stmts_are_full_exprs_p)
1389     {
1390       tree scope_stmts;
1391
1392       if (building_stmt_tree () && !processing_template_decl)
1393         scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
1394       else
1395         scope_stmts = NULL_TREE;
1396
1397       block = poplevel (kept_level_p (), 1, 0);
1398       if (block && !processing_template_decl)
1399         {
1400           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1401           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1402         }
1403     }
1404
1405   return block;
1406 }
1407
1408 /* Finish a parenthesized expression EXPR.  */
1409
1410 tree
1411 finish_parenthesized_expr (expr)
1412      tree expr;
1413 {
1414   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1415     /* This inhibits warnings in truthvalue_conversion.  */
1416     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1417
1418   return expr;
1419 }
1420
1421 /* Begin a statement-expression.  The value returned must be passed to
1422    finish_stmt_expr.  */
1423
1424 tree 
1425 begin_stmt_expr ()
1426 {
1427   /* If we're outside a function, we won't have a statement-tree to
1428      work with.  But, if we see a statement-expression we need to
1429      create one.  */
1430   if (! cfun && !last_tree)
1431     begin_stmt_tree (&scope_chain->x_saved_tree);
1432
1433   keep_next_level (1);
1434   /* If we're building a statement tree, then the upcoming compound
1435      statement will be chained onto the tree structure, starting at
1436      last_tree.  We return last_tree so that we can later unhook the
1437      compound statement.  */
1438   return building_stmt_tree () ? last_tree : expand_start_stmt_expr(); 
1439 }
1440
1441 /* Finish a statement-expression.  RTL_EXPR should be the value
1442    returned by the previous begin_stmt_expr; EXPR is the
1443    statement-expression.  Returns an expression representing the
1444    statement-expression.  */
1445
1446 tree 
1447 finish_stmt_expr (rtl_expr)
1448      tree rtl_expr;
1449 {
1450   tree result;
1451
1452   if (!building_stmt_tree ())
1453     rtl_expr = expand_end_stmt_expr (rtl_expr);
1454
1455   if (building_stmt_tree ())
1456     {
1457       /* If the last thing in the statement-expression was not an
1458          expression-statement, then it has type `void'.  */
1459       if (!last_expr_type)
1460         last_expr_type = void_type_node;
1461       result = build_min (STMT_EXPR, last_expr_type, last_tree);
1462       TREE_SIDE_EFFECTS (result) = 1;
1463       
1464       /* Remove the compound statement from the tree structure; it is
1465          now saved in the STMT_EXPR.  */
1466       SET_LAST_STMT (rtl_expr);
1467       TREE_CHAIN (last_tree) = NULL_TREE;
1468     }
1469   else 
1470     result = rtl_expr;
1471
1472   /* If we created a statement-tree for this statement-expression,
1473      remove it now.  */ 
1474   if (! cfun
1475       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1476     finish_stmt_tree (&scope_chain->x_saved_tree);
1477
1478   return result;
1479 }
1480
1481 /* Finish a call to FN with ARGS.  Returns a representation of the
1482    call.  */
1483
1484 tree 
1485 finish_call_expr (fn, args, koenig)
1486      tree fn;
1487      tree args;
1488      int koenig;
1489 {
1490   tree result;
1491
1492   if (koenig)
1493     {
1494       if (TREE_CODE (fn) == BIT_NOT_EXPR)
1495         fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1496       else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1497         fn = do_identifier (fn, 2, args);
1498     }
1499   result = build_x_function_call (fn, args, current_class_ref);
1500
1501   if (TREE_CODE (result) == CALL_EXPR
1502       && (! TREE_TYPE (result)
1503           || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1504     result = require_complete_type (result);
1505
1506   return result;
1507 }
1508
1509 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1510    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1511    POSTDECREMENT_EXPR.)  */
1512
1513 tree 
1514 finish_increment_expr (expr, code)
1515      tree expr;
1516      enum tree_code code;
1517 {
1518   /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1519      a COMPONENT_REF).  This way if we've got, say, a reference to a
1520      static member that's being operated on, we don't end up trying to
1521      find a member operator for the class it's in.  */
1522
1523   if (TREE_CODE (expr) == OFFSET_REF)
1524     expr = resolve_offset_ref (expr);
1525   return build_x_unary_op (code, expr);  
1526 }
1527
1528 /* Finish a use of `this'.  Returns an expression for `this'.  */
1529
1530 tree 
1531 finish_this_expr ()
1532 {
1533   tree result;
1534
1535   if (current_class_ptr)
1536     {
1537 #ifdef WARNING_ABOUT_CCD
1538       TREE_USED (current_class_ptr) = 1;
1539 #endif
1540       result = current_class_ptr;
1541     }
1542   else if (current_function_decl
1543            && DECL_STATIC_FUNCTION_P (current_function_decl))
1544     {
1545       error ("`this' is unavailable for static member functions");
1546       result = error_mark_node;
1547     }
1548   else
1549     {
1550       if (current_function_decl)
1551         error ("invalid use of `this' in non-member function");
1552       else
1553         error ("invalid use of `this' at top level");
1554       result = error_mark_node;
1555     }
1556
1557   return result;
1558 }
1559
1560 /* Finish a member function call using OBJECT and ARGS as arguments to
1561    FN.  Returns an expression for the call.  */
1562
1563 tree 
1564 finish_object_call_expr (fn, object, args)
1565      tree fn;
1566      tree object;
1567      tree args;
1568 {
1569 #if 0
1570   /* This is a future direction of this code, but because
1571      build_x_function_call cannot always undo what is done in
1572      build_component_ref entirely yet, we cannot do this.  */
1573
1574   tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1575   return finish_call_expr (real_fn, args);
1576 #else
1577   if (DECL_DECLARES_TYPE_P (fn))
1578     {
1579       if (processing_template_decl)
1580         /* This can happen on code like:
1581
1582            class X;
1583            template <class T> void f(T t) {
1584              t.X();
1585            }  
1586
1587            We just grab the underlying IDENTIFIER.  */
1588         fn = DECL_NAME (fn);
1589       else
1590         {
1591           cp_error ("calling type `%T' like a method", fn);
1592           return error_mark_node;
1593         }
1594     }
1595
1596   return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1597 #endif
1598 }
1599
1600 /* Finish a qualified member function call using OBJECT and ARGS as
1601    arguments to FN.  Returns an expressino for the call.  */
1602
1603 tree 
1604 finish_qualified_object_call_expr (fn, object, args)
1605      tree fn;
1606      tree object;
1607      tree args;
1608 {
1609   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1610                                    TREE_OPERAND (fn, 1), args);
1611 }
1612
1613 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1614    being the scope, if any, of DESTRUCTOR.  Returns an expression for
1615    the call.  */
1616
1617 tree 
1618 finish_pseudo_destructor_call_expr (object, scope, destructor)
1619      tree object;
1620      tree scope;
1621      tree destructor;
1622 {
1623   if (processing_template_decl)
1624     return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1625
1626   if (scope && scope != destructor)
1627     cp_error ("destructor specifier `%T::~%T()' must have matching names", 
1628               scope, destructor);
1629
1630   if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1631       && (TREE_CODE (TREE_TYPE (object)) !=
1632           TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1633     cp_error ("`%E' is not of type `%T'", object, destructor);
1634
1635   return cp_convert (void_type_node, object);
1636 }
1637
1638 /* Finish a call to a globally qualified member function FN using
1639    ARGS.  Returns an expression for the call.  */
1640
1641 tree 
1642 finish_qualified_call_expr (fn, args)
1643      tree fn;
1644      tree args;
1645 {
1646   if (processing_template_decl)
1647     return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1648   else
1649     return build_member_call (TREE_OPERAND (fn, 0),
1650                               TREE_OPERAND (fn, 1),
1651                               args);
1652 }
1653
1654 /* Finish an expression taking the address of LABEL.  Returns an
1655    expression for the address.  */
1656
1657 tree 
1658 finish_label_address_expr (label)
1659      tree label;
1660 {
1661   tree result;
1662
1663   label = lookup_label (label);
1664   if (label == NULL_TREE)
1665     result = null_pointer_node;
1666   else
1667     {
1668       TREE_USED (label) = 1;
1669       result = build1 (ADDR_EXPR, ptr_type_node, label);
1670       TREE_CONSTANT (result) = 1;
1671       /* This function cannot be inlined.  All jumps to the addressed
1672          label should wind up at the same point.  */
1673       DECL_UNINLINABLE (current_function_decl) = 1;
1674     }
1675
1676   return result;
1677 }
1678
1679 /* Finish an expression of the form CODE EXPR.  */
1680
1681 tree
1682 finish_unary_op_expr (code, expr)
1683      enum tree_code code;
1684      tree expr;
1685 {
1686   tree result = build_x_unary_op (code, expr);
1687   /* Inside a template, build_x_unary_op does not fold the
1688      expression. So check whether the result is folded before
1689      setting TREE_NEGATED_INT.  */
1690   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1691       && TREE_CODE (result) == INTEGER_CST
1692       && !TREE_UNSIGNED (TREE_TYPE (result))
1693       && INT_CST_LT (result, integer_zero_node))
1694     TREE_NEGATED_INT (result) = 1;
1695   overflow_warning (result);
1696   return result;
1697 }
1698
1699 /* Finish an id-expression.  */
1700
1701 tree
1702 finish_id_expr (expr)
1703      tree expr;
1704 {
1705   if (TREE_CODE (expr) == IDENTIFIER_NODE)
1706     expr = do_identifier (expr, 1, NULL_TREE);
1707
1708   return expr;
1709 }
1710
1711 static tree current_type_lookups;
1712
1713 /* Perform deferred access control for types used in the type of a
1714    declaration.  */
1715
1716 static void
1717 deferred_type_access_control ()
1718 {
1719   tree lookup = type_lookups;
1720
1721   if (lookup == error_mark_node)
1722     return;
1723
1724   for (; lookup; lookup = TREE_CHAIN (lookup))
1725     enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1726 }
1727
1728 void
1729 decl_type_access_control (decl)
1730      tree decl;
1731 {
1732   tree save_fn;
1733
1734   if (type_lookups == error_mark_node)
1735     return;
1736
1737   save_fn = current_function_decl;
1738
1739   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1740     current_function_decl = decl;
1741
1742   deferred_type_access_control ();
1743
1744   current_function_decl = save_fn;
1745   
1746   /* Now strip away the checks for the current declarator; they were
1747      added to type_lookups after typed_declspecs saved the copy that
1748      ended up in current_type_lookups.  */
1749   type_lookups = current_type_lookups;
1750 }
1751
1752 void
1753 save_type_access_control (lookups)
1754      tree lookups;
1755 {
1756   current_type_lookups = lookups;
1757 }
1758
1759 /* Begin a function definition declared with DECL_SPECS and
1760    DECLARATOR.  Returns non-zero if the function-declaration is
1761    legal.  */
1762
1763 int
1764 begin_function_definition (decl_specs, declarator)
1765      tree decl_specs;
1766      tree declarator;
1767 {
1768   tree specs;
1769   tree attrs;
1770
1771   split_specs_attrs (decl_specs, &specs, &attrs);
1772   if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1773     return 0;
1774
1775   deferred_type_access_control ();
1776   type_lookups = error_mark_node;
1777
1778   reinit_parse_for_function ();
1779   /* The things we're about to see are not directly qualified by any
1780      template headers we've seen thus far.  */
1781   reset_specialization ();
1782
1783   return 1;
1784 }
1785
1786 /* Begin a constructor declarator of the form `SCOPE::NAME'.  Returns
1787    a SCOPE_REF.  */
1788
1789 tree 
1790 begin_constructor_declarator (scope, name)
1791      tree scope;
1792      tree name;
1793 {
1794   tree result = build_parse_node (SCOPE_REF, scope, name);
1795   enter_scope_of (result);
1796   return result;
1797 }
1798
1799 /* Finish an init-declarator.  Returns a DECL.  */
1800
1801 tree
1802 finish_declarator (declarator, declspecs, attributes,
1803                    prefix_attributes, initialized)
1804      tree declarator;
1805      tree declspecs;
1806      tree attributes;
1807      tree prefix_attributes;
1808      int initialized;
1809 {
1810   return start_decl (declarator, declspecs, initialized, attributes,
1811                      prefix_attributes); 
1812 }
1813
1814 /* Finish a translation unit.  */
1815
1816 void 
1817 finish_translation_unit ()
1818 {
1819   /* In case there were missing closebraces,
1820      get us back to the global binding level.  */
1821   pop_everything ();
1822   while (current_namespace != global_namespace)
1823     pop_namespace ();
1824   finish_file ();
1825 }
1826
1827 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1828    Returns the parameter.  */
1829
1830 tree 
1831 finish_template_type_parm (aggr, identifier)
1832      tree aggr;
1833      tree identifier;
1834 {
1835   if (aggr != class_type_node)
1836     {
1837       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1838       aggr = class_type_node;
1839     }
1840
1841   return build_tree_list (aggr, identifier);
1842 }
1843
1844 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1845    Returns the parameter.  */
1846
1847 tree 
1848 finish_template_template_parm (aggr, identifier)
1849      tree aggr;
1850      tree identifier;
1851 {
1852   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1853   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1854   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1855   DECL_TEMPLATE_RESULT (tmpl) = decl;
1856   SET_DECL_ARTIFICIAL (decl);
1857   end_template_decl ();
1858
1859   return finish_template_type_parm (aggr, tmpl);
1860 }
1861
1862 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1863    non-zero, the parameter list was terminated by a `...'.  */
1864
1865 tree
1866 finish_parmlist (parms, ellipsis)
1867      tree parms;
1868      int ellipsis;
1869 {
1870   if (!ellipsis)
1871     chainon (parms, void_list_node);
1872   /* We mark the PARMS as a parmlist so that declarator processing can
1873      disambiguate certain constructs.  */
1874   if (parms != NULL_TREE)
1875     TREE_PARMLIST (parms) = 1;
1876
1877   return parms;
1878 }
1879
1880 /* Begin a class definition, as indicated by T.  */
1881
1882 tree
1883 begin_class_definition (t)
1884      tree t;
1885 {
1886   if (t == error_mark_node
1887       || ! IS_AGGR_TYPE (t))
1888     {
1889       t = make_aggr_type (RECORD_TYPE);
1890       pushtag (make_anon_name (), t, 0);
1891     }
1892
1893   /* In a definition of a member class template, we will get here with an
1894      implicit typename, a TYPENAME_TYPE with a type.  */
1895   if (TREE_CODE (t) == TYPENAME_TYPE)
1896     t = TREE_TYPE (t);
1897   
1898   /* If we generated a partial instantiation of this type, but now
1899      we're seeing a real definition, we're actually looking at a
1900      partial specialization.  Consider:
1901
1902        template <class T, class U>
1903        struct Y {};
1904
1905        template <class T>
1906        struct X {};
1907
1908        template <class T, class U>
1909        void f()
1910        {
1911          typename X<Y<T, U> >::A a;
1912        }
1913
1914        template <class T, class U>
1915        struct X<Y<T, U> >
1916        {
1917        };
1918
1919      We have to undo the effects of the previous partial
1920      instantiation.  */
1921   if (PARTIAL_INSTANTIATION_P (t))
1922     {
1923       if (!pedantic) 
1924         {
1925           /* Unfortunately, when we're not in pedantic mode, we
1926              attempt to actually fill in some of the fields of the
1927              partial instantiation, in order to support the implicit
1928              typename extension.  Clear those fields now, in
1929              preparation for the definition here.  The fields cleared
1930              here must match those set in instantiate_class_template.
1931              Look for a comment mentioning begin_class_definition
1932              there.  */
1933           TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1934           TYPE_FIELDS (t) = NULL_TREE;
1935           TYPE_METHODS (t) = NULL_TREE;
1936           CLASSTYPE_TAGS (t) = NULL_TREE;
1937           TYPE_SIZE (t) = NULL_TREE;
1938         }
1939
1940       /* This isn't a partial instantiation any more.  */
1941       PARTIAL_INSTANTIATION_P (t) = 0;
1942     }
1943   /* If this type was already complete, and we see another definition,
1944      that's an error.  */
1945   else if (COMPLETE_TYPE_P (t))
1946     duplicate_tag_error (t);
1947
1948   /* Update the location of the decl.  */
1949   DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1950   DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1951   
1952   if (TYPE_BEING_DEFINED (t))
1953     {
1954       t = make_aggr_type (TREE_CODE (t));
1955       pushtag (TYPE_IDENTIFIER (t), t, 0);
1956     }
1957   maybe_process_partial_specialization (t);
1958   pushclass (t, 1);
1959   TYPE_BEING_DEFINED (t) = 1;
1960   TYPE_PACKED (t) = flag_pack_struct;
1961   /* Reset the interface data, at the earliest possible
1962      moment, as it might have been set via a class foo;
1963      before.  */
1964   {
1965     tree name = TYPE_IDENTIFIER (t);
1966     
1967     if (! ANON_AGGRNAME_P (name))
1968       {
1969         CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1970         SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1971           (t, interface_unknown);
1972       }
1973     
1974     /* Only leave this bit clear if we know this
1975        class is part of an interface-only specification.  */
1976     if (! CLASSTYPE_INTERFACE_KNOWN (t)
1977         || ! CLASSTYPE_INTERFACE_ONLY (t))
1978       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1979   }
1980   reset_specialization();
1981   
1982   /* Make a declaration for this class in its own scope.  */
1983   build_self_reference ();
1984
1985   return t;
1986 }
1987
1988 /* Finish the member declaration given by DECL.  */
1989
1990 void
1991 finish_member_declaration (decl)
1992      tree decl;
1993 {
1994   if (decl == error_mark_node || decl == NULL_TREE)
1995     return;
1996
1997   if (decl == void_type_node)
1998     /* The COMPONENT was a friend, not a member, and so there's
1999        nothing for us to do.  */
2000     return;
2001
2002   /* We should see only one DECL at a time.  */
2003   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2004
2005   /* Set up access control for DECL.  */
2006   TREE_PRIVATE (decl) 
2007     = (current_access_specifier == access_private_node);
2008   TREE_PROTECTED (decl) 
2009     = (current_access_specifier == access_protected_node);
2010   if (TREE_CODE (decl) == TEMPLATE_DECL)
2011     {
2012       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2013       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2014     }
2015
2016   /* Mark the DECL as a member of the current class.  */
2017   DECL_CONTEXT (decl) = current_class_type;
2018
2019   /* [dcl.link]
2020
2021      A C language linkage is ignored for the names of class members
2022      and the member function type of class member functions.  */
2023   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2024     DECL_LANGUAGE (decl) = lang_cplusplus;
2025
2026   /* Put functions on the TYPE_METHODS list and everything else on the
2027      TYPE_FIELDS list.  Note that these are built up in reverse order.
2028      We reverse them (to obtain declaration order) in finish_struct.  */
2029   if (TREE_CODE (decl) == FUNCTION_DECL 
2030       || DECL_FUNCTION_TEMPLATE_P (decl))
2031     {
2032       /* We also need to add this function to the
2033          CLASSTYPE_METHOD_VEC.  */
2034       add_method (current_class_type, 0, decl);
2035
2036       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2037       TYPE_METHODS (current_class_type) = decl;
2038     }
2039   else
2040     {
2041       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2042          go at the beginning.  The reason is that lookup_field_1
2043          searches the list in order, and we want a field name to
2044          override a type name so that the "struct stat hack" will
2045          work.  In particular:
2046
2047            struct S { enum E { }; int E } s;
2048            s.E = 3;
2049
2050          is legal.  In addition, the FIELD_DECLs must be maintained in
2051          declaration order so that class layout works as expected.
2052          However, we don't need that order until class layout, so we
2053          save a little time by putting FIELD_DECLs on in reverse order
2054          here, and then reversing them in finish_struct_1.  (We could
2055          also keep a pointer to the correct insertion points in the
2056          list.)  */
2057
2058       if (TREE_CODE (decl) == TYPE_DECL)
2059         TYPE_FIELDS (current_class_type) 
2060           = chainon (TYPE_FIELDS (current_class_type), decl);
2061       else
2062         {
2063           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2064           TYPE_FIELDS (current_class_type) = decl;
2065         }
2066
2067       /* Enter the DECL into the scope of the class.  */
2068       if (TREE_CODE (decl) != USING_DECL)
2069         pushdecl_class_level (decl);
2070     }
2071 }
2072
2073 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
2074    the definition is immediately followed by a semicolon.  Returns the
2075    type.  */
2076
2077 tree
2078 finish_class_definition (t, attributes, semi, pop_scope_p)
2079      tree t;
2080      tree attributes;
2081      int semi;
2082      int pop_scope_p;
2083 {
2084   /* finish_struct nukes this anyway; if finish_exception does too,
2085      then it can go.  */
2086   if (semi)
2087     note_got_semicolon (t);
2088
2089   /* If we got any attributes in class_head, xref_tag will stick them in
2090      TREE_TYPE of the type.  Grab them now.  */
2091   attributes = chainon (TREE_TYPE (t), attributes);
2092   TREE_TYPE (t) = NULL_TREE;
2093
2094   if (TREE_CODE (t) == ENUMERAL_TYPE)
2095     ;
2096   else
2097     {
2098       t = finish_struct (t, attributes);
2099       if (semi) 
2100         note_got_semicolon (t);
2101     }
2102
2103   if (! semi)
2104     check_for_missing_semicolon (t); 
2105   if (pop_scope_p)
2106     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2107   if (current_scope () == current_function_decl)
2108     do_pending_defargs ();
2109
2110   return t;
2111 }
2112
2113 /* Finish processing the default argument expressions cached during
2114    the processing of a class definition.  */
2115
2116 void
2117 begin_inline_definitions ()
2118 {
2119   if (pending_inlines 
2120       && current_scope () == current_function_decl)
2121     do_pending_inlines ();
2122 }
2123
2124 /* Finish processing the inline function definitions cached during the
2125    processing of a class definition.  */
2126
2127 void
2128 finish_inline_definitions ()
2129 {
2130   if (current_class_type == NULL_TREE)
2131     clear_inline_text_obstack (); 
2132 }
2133
2134 /* Finish processing the declaration of a member class template
2135    TYPES whose template parameters are given by PARMS.  */
2136
2137 tree
2138 finish_member_class_template (types)
2139      tree types;
2140 {
2141   tree t;
2142
2143   /* If there are declared, but undefined, partial specializations
2144      mixed in with the typespecs they will not yet have passed through
2145      maybe_process_partial_specialization, so we do that here.  */
2146   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2147     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2148       maybe_process_partial_specialization (TREE_VALUE (t));
2149
2150   note_list_got_semicolon (types);
2151   grok_x_components (types);
2152   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2153     /* The component was in fact a friend declaration.  We avoid
2154        finish_member_template_decl performing certain checks by
2155        unsetting TYPES.  */
2156     types = NULL_TREE;
2157   
2158   finish_member_template_decl (types);
2159
2160   /* As with other component type declarations, we do
2161      not store the new DECL on the list of
2162      component_decls.  */
2163   return NULL_TREE;
2164 }
2165
2166 /* Finish processsing a complete template declaration.  The PARMS are
2167    the template parameters.  */
2168
2169 void
2170 finish_template_decl (parms)
2171      tree parms;
2172 {
2173   if (parms)
2174     end_template_decl ();
2175   else
2176     end_specialization ();
2177 }
2178
2179 /* Finish processing a a template-id (which names a type) of the form
2180    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2181    template-id.  If ENTERING_SCOPE is non-zero we are about to enter
2182    the scope of template-id indicated.  */
2183
2184 tree
2185 finish_template_type (name, args, entering_scope)
2186      tree name;
2187      tree args;
2188      int entering_scope;
2189 {
2190   tree decl;
2191
2192   decl = lookup_template_class (name, args,
2193                                 NULL_TREE, NULL_TREE, entering_scope);
2194   if (decl != error_mark_node)
2195     decl = TYPE_STUB_DECL (decl);
2196
2197   return decl;
2198 }
2199
2200 /* SR is a SCOPE_REF node.  Enter the scope of SR, whether it is a
2201    namespace scope or a class scope.  */
2202
2203 void
2204 enter_scope_of (sr)
2205      tree sr;
2206 {
2207   tree scope = TREE_OPERAND (sr, 0);
2208
2209   if (TREE_CODE (scope) == NAMESPACE_DECL)
2210     {
2211       push_decl_namespace (scope);
2212       TREE_COMPLEXITY (sr) = -1;
2213     }
2214   else if (scope != current_class_type)
2215     {
2216       if (TREE_CODE (scope) == TYPENAME_TYPE)
2217         {
2218           /* In a declarator for a template class member, the scope will
2219              get here as an implicit typename, a TYPENAME_TYPE with a type.  */
2220           scope = TREE_TYPE (scope);
2221           TREE_OPERAND (sr, 0) = scope;
2222         }
2223       push_nested_class (scope, 3);
2224       TREE_COMPLEXITY (sr) = current_class_depth;
2225     }
2226 }
2227
2228 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2229    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2230    BASE_CLASS, or NULL_TREE if an error occurred.  The
2231    ACCESSS_SPECIFIER is one of
2232    access_{default,public,protected_private}[_virtual]_node.*/
2233
2234 tree 
2235 finish_base_specifier (access_specifier, base_class)
2236      tree access_specifier;
2237      tree base_class;
2238 {
2239   tree type;
2240   tree result;
2241
2242   if (base_class == NULL_TREE)
2243     {
2244       error ("invalid base class");
2245       type = error_mark_node;
2246     }
2247   else
2248     type = TREE_TYPE (base_class);
2249
2250   if (! is_aggr_type (type, 1))
2251     result = NULL_TREE;
2252   else
2253     result = build_tree_list (access_specifier, type);
2254
2255   return result;
2256 }
2257
2258 /* Called when multiple declarators are processed.  If that is not
2259    premitted in this context, an error is issued.  */
2260
2261 void
2262 check_multiple_declarators ()
2263 {
2264   /* [temp]
2265      
2266      In a template-declaration, explicit specialization, or explicit
2267      instantiation the init-declarator-list in the declaration shall
2268      contain at most one declarator.  
2269
2270      We don't just use PROCESSING_TEMPLATE_DECL for the first
2271      condition since that would disallow the perfectly legal code, 
2272      like `template <class T> struct S { int i, j; };'.  */
2273   tree scope = current_scope ();
2274
2275   if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2276     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2277     return;
2278      
2279   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2280       || processing_explicit_instantiation
2281       || processing_specialization)
2282     cp_error ("multiple declarators in template declaration");
2283 }
2284
2285 tree
2286 finish_typeof (expr)
2287      tree expr;
2288 {
2289   if (processing_template_decl)
2290     {
2291       tree t;
2292
2293       t = make_aggr_type (TYPEOF_TYPE);
2294       TYPE_FIELDS (t) = expr;
2295
2296       return t;
2297     }
2298
2299   return TREE_TYPE (expr);
2300 }
2301
2302 /* Create an empty statement tree rooted at T.  */
2303
2304 void
2305 begin_stmt_tree (t)
2306      tree *t;
2307 {
2308   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2309      what follows.  We remove the extraneous statement in
2310      finish_stmt_tree.  */
2311   *t = build_nt (EXPR_STMT, void_zero_node);
2312   SET_LAST_STMT (*t);
2313   last_expr_type = NULL_TREE;
2314 }
2315
2316 /* Finish the statement tree rooted at T.  */
2317
2318 void
2319 finish_stmt_tree (t)
2320      tree *t;
2321 {
2322   tree stmt;
2323   
2324   /* Remove the fake extra statement added in begin_stmt_tree.  */
2325   stmt = TREE_CHAIN (*t);
2326   *t = stmt;
2327   SET_LAST_STMT (NULL_TREE);
2328
2329   if (cfun)
2330     {
2331       /* The line-number recorded in the outermost statement in a function
2332          is the line number of the end of the function.  */
2333       STMT_LINENO (stmt) = lineno;
2334       STMT_LINENO_FOR_FN_P (stmt) = 1;
2335     }
2336 }
2337
2338 /* We're about to expand T, a statement.  Set up appropriate context
2339    for the substitution.  */
2340
2341 void
2342 prep_stmt (t)
2343      tree t;
2344 {
2345   if (!STMT_LINENO_FOR_FN_P (t))
2346     lineno = STMT_LINENO (t);
2347   stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2348 }
2349
2350 /* Some statements, like for-statements or if-statements, require a
2351    condition.  This condition can be a declaration.  If T is such a
2352    declaration it is processed, and an expression appropriate to use
2353    as the condition is returned.  Otherwise, T itself is returned.  */
2354
2355 static tree
2356 expand_cond (t)
2357      tree t;
2358 {
2359   if (t && TREE_CODE (t) == TREE_LIST)
2360     {
2361       expand_stmt (TREE_PURPOSE (t));
2362       return TREE_VALUE (t);
2363     }
2364   else 
2365     return t;
2366 }
2367
2368 /* Generate RTL for the statement T, and its substatements, and any
2369    other statements at its nesting level.  */
2370
2371 tree
2372 expand_stmt (t)
2373      tree t;
2374 {
2375   tree rval = NULL_TREE;
2376
2377   while (t && t != error_mark_node)
2378     {
2379       int saved_stmts_are_full_exprs_p;
2380
2381       /* Assume we'll have nothing to return.  */
2382       rval = NULL_TREE;
2383
2384       /* Set up context appropriately for handling this statement.  */
2385       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2386       prep_stmt (t);
2387
2388       switch (TREE_CODE (t))
2389         {
2390         case RETURN_STMT:
2391           finish_return_stmt (RETURN_EXPR (t));
2392           break;
2393
2394         case EXPR_STMT:
2395           finish_expr_stmt (EXPR_STMT_EXPR (t));
2396           break;
2397
2398         case DECL_STMT:
2399           {
2400             tree decl;
2401
2402             emit_line_note (input_filename, lineno);
2403             decl = DECL_STMT_DECL (t);
2404             /* If this is a declaration for an automatic local
2405                variable, initialize it.  Note that we might also see a
2406                declaration for a namespace-scope object (declared with
2407                `extern').  We don't have to handle the initialization
2408                of those objects here; they can only be declarations,
2409                rather than definitions.  */
2410             if (TREE_CODE (decl) == VAR_DECL 
2411                 && !TREE_STATIC (decl)
2412                 && !DECL_EXTERNAL (decl))
2413               {
2414                 /* Let the back-end know about this variable.  */
2415                 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2416                   emit_local_var (decl);
2417                 else
2418                   expand_anon_union_decl (decl, NULL_TREE, 
2419                                           DECL_ANON_UNION_ELEMS (decl));
2420               }
2421             else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2422               {
2423                 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
2424                   /* Do not emit unused decls. This is not just an
2425                      optimization. We really do not want to emit
2426                      __PRETTY_FUNCTION__ etc, if they're never used.  */
2427                   DECL_IGNORED_P (decl) = 1;
2428                 else
2429                   make_rtl_for_local_static (decl);
2430               }
2431           }
2432           break;
2433
2434         case CLEANUP_STMT:
2435           finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2436           break;
2437
2438         case START_CATCH_STMT:
2439           begin_catch_block (TREE_TYPE (t));
2440           break;
2441
2442         case CTOR_STMT:
2443           if (CTOR_BEGIN_P (t))
2444             begin_protect_partials ();
2445           else
2446             /* After this point, any exceptions will cause the
2447                destructor to be executed, so we no longer need to worry
2448                about destroying the various subobjects ourselves.  */
2449             end_protect_partials ();
2450           break;
2451
2452         case FOR_STMT:
2453           {
2454             tree tmp;
2455
2456             begin_for_stmt ();
2457             expand_stmt (FOR_INIT_STMT (t));
2458             finish_for_init_stmt (NULL_TREE);
2459             finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2460             tmp = FOR_EXPR (t);
2461             finish_for_expr (tmp, NULL_TREE);
2462             expand_stmt (FOR_BODY (t));
2463             finish_for_stmt (tmp, NULL_TREE);
2464           }
2465           break;
2466
2467         case WHILE_STMT:
2468           {
2469             begin_while_stmt ();
2470             finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2471             expand_stmt (WHILE_BODY (t));
2472             finish_while_stmt (NULL_TREE);
2473           }
2474           break;
2475
2476         case DO_STMT:
2477           {
2478             begin_do_stmt ();
2479             expand_stmt (DO_BODY (t));
2480             finish_do_body (NULL_TREE);
2481             finish_do_stmt (DO_COND (t), NULL_TREE);
2482           }
2483           break;
2484
2485         case IF_STMT:
2486           begin_if_stmt ();
2487           finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2488           if (THEN_CLAUSE (t))
2489             {
2490               expand_stmt (THEN_CLAUSE (t));
2491               finish_then_clause (NULL_TREE);
2492             }
2493           if (ELSE_CLAUSE (t))
2494             {
2495               begin_else_clause ();
2496               expand_stmt (ELSE_CLAUSE (t));
2497               finish_else_clause (NULL_TREE);
2498             }
2499           finish_if_stmt ();
2500           break;
2501
2502         case COMPOUND_STMT:
2503           begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2504           expand_stmt (COMPOUND_BODY (t));
2505           rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
2506                                        NULL_TREE);
2507           break;
2508
2509         case BREAK_STMT:
2510           finish_break_stmt ();
2511           break;
2512
2513         case CONTINUE_STMT:
2514           finish_continue_stmt ();
2515           break;
2516
2517         case SWITCH_STMT:
2518           {
2519             tree cond;
2520
2521             begin_switch_stmt ();
2522             cond = expand_cond (SWITCH_COND (t));
2523             finish_switch_cond (cond, NULL_TREE);
2524             expand_stmt (SWITCH_BODY (t));
2525             finish_switch_stmt (cond, NULL_TREE);
2526           }
2527           break;
2528
2529         case CASE_LABEL:
2530           finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2531           break;
2532
2533         case LABEL_STMT:
2534           expand_label (LABEL_STMT_LABEL (t));
2535           break;
2536
2537         case GOTO_STMT:
2538           finish_goto_stmt (GOTO_DESTINATION (t));
2539           break;
2540
2541         case ASM_STMT:
2542           finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2543                            (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2544           break;
2545
2546         case TRY_BLOCK:
2547           if (CLEANUP_P (t))
2548             {
2549               expand_eh_region_start ();
2550               expand_stmt (TRY_STMTS (t));
2551               finish_cleanup_try_block (NULL_TREE);
2552               finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2553             }
2554           else
2555             {
2556               if (FN_TRY_BLOCK_P (t))
2557                 begin_function_try_block ();
2558               else
2559                 begin_try_block ();
2560
2561               expand_stmt (TRY_STMTS (t));
2562
2563               if (FN_TRY_BLOCK_P (t))
2564                 {
2565                   finish_function_try_block (NULL_TREE);
2566                   expand_stmt (TRY_HANDLERS (t));
2567                   finish_function_handler_sequence (NULL_TREE);
2568                 }
2569               else
2570                 {
2571                   finish_try_block (NULL_TREE);
2572                   expand_stmt (TRY_HANDLERS (t));
2573                   finish_handler_sequence (NULL_TREE);
2574                 }
2575             }
2576           break;
2577
2578         case HANDLER:
2579           begin_handler ();
2580           expand_stmt (HANDLER_BODY (t));
2581           finish_handler (NULL_TREE, NULL_TREE);
2582           break;
2583
2584         case SUBOBJECT:
2585           finish_subobject (SUBOBJECT_CLEANUP (t));
2586           break;
2587
2588         case SCOPE_STMT:
2589           if (!SCOPE_NO_CLEANUPS_P (t))
2590             {
2591               if (SCOPE_BEGIN_P (t))
2592                 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
2593                                                  SCOPE_STMT_BLOCK (t));
2594               else if (SCOPE_END_P (t))
2595                 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
2596             }
2597           else if (!SCOPE_NULLIFIED_P (t))
2598             {
2599               rtx note = emit_note (NULL,
2600                                     (SCOPE_BEGIN_P (t) 
2601                                      ? NOTE_INSN_BLOCK_BEG
2602                                      : NOTE_INSN_BLOCK_END));
2603               NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
2604             }
2605               
2606           break;
2607
2608         case RETURN_INIT:
2609           /* Clear this out so that finish_named_return_value can set it
2610              again.  */
2611           DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2612           finish_named_return_value (TREE_OPERAND (t, 0), 
2613                                      TREE_OPERAND (t, 1));
2614           break;
2615
2616         default:
2617           my_friendly_abort (19990810);
2618           break;
2619         }
2620
2621       /* Restore saved state.  */
2622       stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2623
2624       /* Go on to the next statement in this scope.  */
2625       t = TREE_CHAIN (t);
2626     }
2627
2628   return rval;
2629 }
2630
2631 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2632    will equivalent CALL_EXPRs.  */
2633
2634 static tree
2635 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2636      tree *tp;
2637      int *walk_subtrees ATTRIBUTE_UNUSED;
2638      void *data ATTRIBUTE_UNUSED;
2639 {
2640   tree aggr_init_expr;
2641   tree call_expr;
2642   tree fn;
2643   tree args;
2644   tree slot;
2645   tree type;
2646   tree call_type;
2647   int copy_from_buffer_p;
2648
2649   aggr_init_expr = *tp;
2650   /* We don't need to walk into types; there's nothing in a type that
2651      needs simplification.  (And, furthermore, there are places we
2652      actively don't want to go.  For example, we don't want to wander
2653      into the default arguments for a FUNCTION_DECL that appears in a
2654      CALL_EXPR.)  */
2655   if (TYPE_P (aggr_init_expr))
2656     {
2657       *walk_subtrees = 0;
2658       return NULL_TREE;
2659     }
2660   /* Only AGGR_INIT_EXPRs are interesting.  */
2661   else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2662     return NULL_TREE;
2663
2664   /* Form an appropriate CALL_EXPR.  */
2665   fn = TREE_OPERAND (aggr_init_expr, 0);
2666   args = TREE_OPERAND (aggr_init_expr, 1);
2667   slot = TREE_OPERAND (aggr_init_expr, 2);
2668   type = TREE_TYPE (aggr_init_expr);
2669   call_type = type;
2670   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2671     {
2672       /* Replace the first argument with the address of the third
2673          argument to the AGGR_INIT_EXPR.  */
2674       call_type = build_pointer_type (type);
2675       mark_addressable (slot);
2676       args = tree_cons (NULL_TREE, build1 (ADDR_EXPR, call_type, slot),
2677                         TREE_CHAIN (args));
2678     }
2679   call_expr = build (CALL_EXPR, call_type, fn, args, NULL_TREE);
2680   TREE_SIDE_EFFECTS (call_expr) = 1;
2681
2682   /* If we're using the non-reentrant PCC calling convention, then we
2683      need to copy the returned value out of the static buffer into the
2684      SLOT.  */
2685   copy_from_buffer_p = 0;
2686 #ifdef PCC_STATIC_STRUCT_RETURN  
2687   if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2688     {
2689       int old_ac;
2690
2691       flag_access_control = 0;
2692       call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2693       flag_access_control = old_ac;
2694       copy_from_buffer_p = 1;
2695     }
2696 #endif
2697
2698   /* If this AGGR_INIT_EXPR indicates the value returned by a
2699      function, then we want to use the value of the initialized
2700      location as the result.  */
2701   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2702     {
2703       call_expr = build (COMPOUND_EXPR, type,
2704                          call_expr, slot);
2705       TREE_SIDE_EFFECTS (call_expr) = 1;
2706     }
2707
2708   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2709   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2710   *tp = call_expr;
2711
2712   /* Keep iterating.  */
2713   return NULL_TREE;
2714 }
2715
2716 /* Generate RTL for FN.  */
2717
2718 void
2719 expand_body (fn)
2720      tree fn;
2721 {
2722   int saved_lineno;
2723   const char *saved_input_filename;
2724
2725   /* When the parser calls us after finishing the body of a template
2726      function, we don't really want to expand the body.  When we're
2727      processing an in-class definition of an inline function,
2728      PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2729      to look at the function itself.  */
2730   if (processing_template_decl
2731       || (DECL_LANG_SPECIFIC (fn) 
2732           && DECL_TEMPLATE_INFO (fn)
2733           && uses_template_parms (DECL_TI_ARGS (fn))))
2734     {
2735       /* Normally, collection only occurs in rest_of_compilation.  So,
2736          if we don't collect here, we never collect junk generated
2737          during the processing of templates until we hit a
2738          non-template function.  */
2739       ggc_collect ();
2740       return;
2741     }
2742
2743   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2744   walk_tree (&DECL_SAVED_TREE (fn), simplify_aggr_init_exprs_r, NULL);
2745
2746   /* If this is a constructor or destructor body, we have to clone it
2747      under the new ABI.  */
2748   if (maybe_clone_body (fn))
2749     {
2750       /* We don't want to process FN again, so pretend we've written
2751          it out, even though we haven't.  */
2752       TREE_ASM_WRITTEN (fn) = 1;
2753       return;
2754     }
2755
2756   /* There's no reason to do any of the work here if we're only doing
2757      semantic analysis; this code just generates RTL.  */
2758   if (flag_syntax_only)
2759     return;
2760
2761   /* If possible, avoid generating RTL for this function.  Instead,
2762      just record it as an inline function, and wait until end-of-file
2763      to decide whether to write it out or not.  */
2764   if (/* We have to generate RTL if it's not an inline function.  */
2765       (DECL_INLINE (fn) || DECL_COMDAT (fn))
2766       /* Or if we have to keep all inline functions anyhow.  */
2767       && !flag_keep_inline_functions
2768       /* Or if we actually have a reference to the function.  */
2769       && !DECL_NEEDED_P (fn)
2770       /* Or if this is a nested function.  */
2771       && !decl_function_context (fn))
2772     {
2773       /* Give the function RTL now so that we can assign it to a
2774          function pointer, etc.  */
2775       make_function_rtl (fn);
2776       /* Set DECL_EXTERNAL so that assemble_external will be called as
2777          necessary.  We'll clear it again in finish_file.  */
2778       if (!DECL_EXTERNAL (fn))
2779         {
2780           DECL_NOT_REALLY_EXTERN (fn) = 1;
2781           DECL_EXTERNAL (fn) = 1;
2782         }
2783       /* Remember this function.  In finish_file we'll decide if
2784          we actually need to write this function out.  */
2785       defer_fn (fn);
2786       /* Let the back-end know that this funtion exists.  */
2787       note_deferral_of_defined_inline_function (fn);
2788       return;
2789     }
2790
2791   /* Optimize the body of the function before expanding it.  */
2792   optimize_function (fn);
2793
2794   /* Save the current file name and line number.  When we expand the
2795      body of the function, we'll set LINENO and INPUT_FILENAME so that
2796      error-mesages come out in the right places.  */
2797   saved_lineno = lineno;
2798   saved_input_filename = input_filename;
2799   lineno = DECL_SOURCE_LINE (fn);
2800   input_filename = DECL_SOURCE_FILE (fn);
2801
2802   start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2803   store_parm_decls ();
2804   current_function_is_thunk = DECL_THUNK_P (fn);
2805
2806   /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2807      any of the other magic variables we set up when starting a
2808      function body.  */
2809   current_function_name_declared = 1;
2810
2811   /* Expand the body.  */
2812   expand_stmt (DECL_SAVED_TREE (fn));
2813
2814   /* Statements should always be full-expressions at the outermost set
2815      of curly braces for a function.  */
2816   my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2817
2818   /* The outermost statement for a function contains the line number
2819      recorded when we finished processing the function.  */
2820   lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2821
2822   /* Generate code for the function.  */
2823   finish_function (0);
2824
2825   /* If possible, obliterate the body of the function so that it can
2826      be garbage collected.  */
2827   if (flag_dump_translation_unit)
2828     /* Keep the body; we're going to dump it.  */
2829     ;
2830   else if (DECL_INLINE (fn) && flag_inline_trees)
2831     /* We might need the body of this function so that we can expand
2832        it inline somewhere else.  */
2833     ;
2834   else
2835     /* We don't need the body; blow it away.  */
2836     DECL_SAVED_TREE (fn) = NULL_TREE;
2837
2838   /* And restore the current source position.  */
2839   lineno = saved_lineno;
2840   input_filename = saved_input_filename;
2841 }