OSDN Git Service

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