OSDN Git Service

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