OSDN Git Service

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