OSDN Git Service

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