OSDN Git Service

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