OSDN Git Service

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