OSDN Git Service

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