OSDN Git Service

PR c++/13810
[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,
7    2003, 2004 Free Software Foundation, Inc.
8    Written by Mark Mitchell (mmitchell@usa.net) based on code found
9    formerly in parse.y and pt.c.  
10
11    This file is part of GCC.
12
13    GCC is free software; you can redistribute it and/or modify it
14    under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2, or (at your option)
16    any later version.
17    
18    GCC is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22    
23    You should have received a copy of the GNU General Public License
24    along with GCC; see the file COPYING.  If not, write to the Free
25    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26    02111-1307, USA.  */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "tree-inline.h"
35 #include "except.h"
36 #include "lex.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "timevar.h"
43 #include "debug.h"
44 #include "cgraph.h"
45
46 /* There routines provide a modular interface to perform many parsing
47    operations.  They may therefore be used during actual parsing, or
48    during template instantiation, which may be regarded as a
49    degenerate form of parsing.  Since the current g++ parser is
50    lacking in several respects, and will be reimplemented, we are
51    attempting to move most code that is not directly related to
52    parsing into this file; that will make implementing the new parser
53    much easier since it will be able to make use of these routines.  */
54
55 static tree maybe_convert_cond (tree);
56 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
57 static void emit_associated_thunks (tree);
58 static void genrtl_try_block (tree);
59 static void genrtl_eh_spec_block (tree);
60 static void genrtl_handler (tree);
61 static void cp_expand_stmt (tree);
62
63
64 /* Finish processing the COND, the SUBSTMT condition for STMT.  */
65
66 #define FINISH_COND(COND, STMT, SUBSTMT)                \
67   do {                                                  \
68     if (last_tree != (STMT))                            \
69       {                                                 \
70         RECHAIN_STMTS (STMT, SUBSTMT);                  \
71         if (!processing_template_decl)                  \
72           {                                             \
73             (COND) = build_tree_list (SUBSTMT, COND);   \
74             (SUBSTMT) = (COND);                         \
75           }                                             \
76       }                                                 \
77     else                                                \
78       (SUBSTMT) = (COND);                               \
79   } while (0)
80
81 /* Deferred Access Checking Overview
82    ---------------------------------
83
84    Most C++ expressions and declarations require access checking
85    to be performed during parsing.  However, in several cases,
86    this has to be treated differently.
87
88    For member declarations, access checking has to be deferred
89    until more information about the declaration is known.  For
90    example:
91
92      class A {
93          typedef int X;
94        public:
95          X f();
96      };
97
98      A::X A::f();
99      A::X g();
100
101    When we are parsing the function return type `A::X', we don't
102    really know if this is allowed until we parse the function name.
103
104    Furthermore, some contexts require that access checking is
105    never performed at all.  These include class heads, and template
106    instantiations.
107
108    Typical use of access checking functions is described here:
109    
110    1. When we enter a context that requires certain access checking
111       mode, the function `push_deferring_access_checks' is called with
112       DEFERRING argument specifying the desired mode.  Access checking
113       may be performed immediately (dk_no_deferred), deferred
114       (dk_deferred), or not performed (dk_no_check).
115
116    2. When a declaration such as a type, or a variable, is encountered,
117       the function `perform_or_defer_access_check' is called.  It
118       maintains a TREE_LIST of all deferred checks.
119
120    3. The global `current_class_type' or `current_function_decl' is then
121       setup by the parser.  `enforce_access' relies on these information
122       to check access.
123
124    4. Upon exiting the context mentioned in step 1,
125       `perform_deferred_access_checks' is called to check all declaration
126       stored in the TREE_LIST.   `pop_deferring_access_checks' is then
127       called to restore the previous access checking mode.
128
129       In case of parsing error, we simply call `pop_deferring_access_checks'
130       without `perform_deferred_access_checks'.  */
131
132 /* Data for deferred access checking.  */
133 static GTY(()) deferred_access *deferred_access_stack;
134 static GTY(()) deferred_access *deferred_access_free_list;
135
136 /* Save the current deferred access states and start deferred
137    access checking iff DEFER_P is true.  */
138
139 void
140 push_deferring_access_checks (deferring_kind deferring)
141 {
142   deferred_access *d;
143
144   /* For context like template instantiation, access checking
145      disabling applies to all nested context.  */
146   if (deferred_access_stack
147       && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
148     deferring = dk_no_check;
149
150   /* Recycle previously used free store if available.  */
151   if (deferred_access_free_list)
152     {
153       d = deferred_access_free_list;
154       deferred_access_free_list = d->next;
155     }
156   else
157     d = ggc_alloc (sizeof (deferred_access));
158
159   d->next = deferred_access_stack;
160   d->deferred_access_checks = NULL_TREE;
161   d->deferring_access_checks_kind = deferring;
162   deferred_access_stack = d;
163 }
164
165 /* Resume deferring access checks again after we stopped doing
166    this previously.  */
167
168 void
169 resume_deferring_access_checks (void)
170 {
171   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
172     deferred_access_stack->deferring_access_checks_kind = dk_deferred;
173 }
174
175 /* Stop deferring access checks.  */
176
177 void
178 stop_deferring_access_checks (void)
179 {
180   if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
181     deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
182 }
183
184 /* Discard the current deferred access checks and restore the
185    previous states.  */
186
187 void
188 pop_deferring_access_checks (void)
189 {
190   deferred_access *d = deferred_access_stack;
191   deferred_access_stack = d->next;
192
193   /* Remove references to access checks TREE_LIST.  */
194   d->deferred_access_checks = NULL_TREE;
195
196   /* Store in free list for later use.  */
197   d->next = deferred_access_free_list;
198   deferred_access_free_list = d;
199 }
200
201 /* Returns a TREE_LIST representing the deferred checks.  
202    The TREE_PURPOSE of each node is the type through which the 
203    access occurred; the TREE_VALUE is the declaration named.
204    */
205
206 tree
207 get_deferred_access_checks (void)
208 {
209   return deferred_access_stack->deferred_access_checks;
210 }
211
212 /* Take current deferred checks and combine with the
213    previous states if we also defer checks previously.
214    Otherwise perform checks now.  */
215
216 void
217 pop_to_parent_deferring_access_checks (void)
218 {
219   tree deferred_check = get_deferred_access_checks ();
220   deferred_access *d1 = deferred_access_stack;
221   deferred_access *d2 = deferred_access_stack->next;
222   deferred_access *d3 = deferred_access_stack->next->next;
223
224   /* Temporary swap the order of the top two states, just to make
225      sure the garbage collector will not reclaim the memory during 
226      processing below.  */
227   deferred_access_stack = d2;
228   d2->next = d1;
229   d1->next = d3;
230
231   for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
232     /* Perform deferred check if required.  */
233     perform_or_defer_access_check (TREE_PURPOSE (deferred_check), 
234                                    TREE_VALUE (deferred_check));
235
236   deferred_access_stack = d1;
237   d1->next = d2;
238   d2->next = d3;
239   pop_deferring_access_checks ();
240 }
241
242 /* Perform the deferred access checks.
243
244    After performing the checks, we still have to keep the list
245    `deferred_access_stack->deferred_access_checks' since we may want
246    to check access for them again later in a different context.
247    For example:
248
249      class A {
250        typedef int X;
251        static X a;
252      };
253      A::X A::a, x;      // No error for `A::a', error for `x'
254
255    We have to perform deferred access of `A::X', first with `A::a',
256    next with `x'.  */
257
258 void
259 perform_deferred_access_checks (void)
260 {
261   tree deferred_check;
262   for (deferred_check = deferred_access_stack->deferred_access_checks;
263        deferred_check;
264        deferred_check = TREE_CHAIN (deferred_check))
265     /* Check access.  */
266     enforce_access (TREE_PURPOSE (deferred_check), 
267                     TREE_VALUE (deferred_check));
268 }
269
270 /* Defer checking the accessibility of DECL, when looked up in
271    BINFO.  */
272
273 void
274 perform_or_defer_access_check (tree binfo, tree decl)
275 {
276   tree check;
277
278   my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
279   
280   /* If we are not supposed to defer access checks, just check now.  */
281   if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
282     {
283       enforce_access (binfo, decl);
284       return;
285     }
286   /* Exit if we are in a context that no access checking is performed.  */
287   else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
288     return;
289
290   /* See if we are already going to perform this check.  */
291   for (check = deferred_access_stack->deferred_access_checks;
292        check;
293        check = TREE_CHAIN (check))
294     if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
295       return;
296   /* If not, record the check.  */
297   deferred_access_stack->deferred_access_checks
298     = tree_cons (binfo, decl,
299                  deferred_access_stack->deferred_access_checks);
300 }
301
302 /* Returns nonzero if the current statement is a full expression,
303    i.e. temporaries created during that statement should be destroyed
304    at the end of the statement.  */
305
306 int
307 stmts_are_full_exprs_p (void)
308 {
309   return current_stmt_tree ()->stmts_are_full_exprs_p;
310 }
311
312 /* Returns the stmt_tree (if any) to which statements are currently
313    being added.  If there is no active statement-tree, NULL is
314    returned.  */
315
316 stmt_tree
317 current_stmt_tree (void)
318 {
319   return (cfun 
320           ? &cfun->language->base.x_stmt_tree 
321           : &scope_chain->x_stmt_tree);
322 }
323
324 /* Nonzero if TYPE is an anonymous union or struct type.  We have to use a
325    flag for this because "A union for which objects or pointers are
326    declared is not an anonymous union" [class.union].  */
327
328 int
329 anon_aggr_type_p (tree node)
330 {
331   return ANON_AGGR_TYPE_P (node);
332 }
333
334 /* Finish a scope.  */
335
336 tree
337 do_poplevel (void)
338 {
339   tree block = NULL_TREE;
340
341   if (stmts_are_full_exprs_p ())
342     {
343       tree scope_stmts = NULL_TREE;
344
345       block = poplevel (kept_level_p (), 1, 0);
346       if (!processing_template_decl)
347         {
348           /* This needs to come after the poplevel so that partial scopes
349              are properly nested.  */
350           scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
351           if (block)
352             {
353               SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
354               SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
355             }
356         }
357     }
358
359   return block;
360 }
361
362 /* Begin a new scope.  */ 
363
364 void
365 do_pushlevel (scope_kind sk)
366 {
367   if (stmts_are_full_exprs_p ())
368     {
369       if (!processing_template_decl)
370         add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
371       begin_scope (sk, NULL);
372     }
373 }
374
375 /* Finish a goto-statement.  */
376
377 tree
378 finish_goto_stmt (tree destination)
379 {
380   if (TREE_CODE (destination) == IDENTIFIER_NODE)
381     destination = lookup_label (destination);
382
383   /* We warn about unused labels with -Wunused.  That means we have to
384      mark the used labels as used.  */
385   if (TREE_CODE (destination) == LABEL_DECL)
386     TREE_USED (destination) = 1;
387   else
388     {
389       /* The DESTINATION is being used as an rvalue.  */
390       if (!processing_template_decl)
391         destination = decay_conversion (destination);
392       /* We don't inline calls to functions with computed gotos.
393          Those functions are typically up to some funny business,
394          and may be depending on the labels being at particular
395          addresses, or some such.  */
396       DECL_UNINLINABLE (current_function_decl) = 1;
397     }
398   
399   check_goto (destination);
400
401   return add_stmt (build_stmt (GOTO_STMT, destination));
402 }
403
404 /* COND is the condition-expression for an if, while, etc.,
405    statement.  Convert it to a boolean value, if appropriate.  */
406
407 static tree
408 maybe_convert_cond (tree cond)
409 {
410   /* Empty conditions remain empty.  */
411   if (!cond)
412     return NULL_TREE;
413
414   /* Wait until we instantiate templates before doing conversion.  */
415   if (processing_template_decl)
416     return cond;
417
418   /* Do the conversion.  */
419   cond = convert_from_reference (cond);
420   return condition_conversion (cond);
421 }
422
423 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
424
425 tree
426 finish_expr_stmt (tree expr)
427 {
428   tree r = NULL_TREE;
429
430   if (expr != NULL_TREE)
431     {
432       if (!processing_template_decl)
433         expr = convert_to_void (expr, "statement");
434       else if (!type_dependent_expression_p (expr))
435         convert_to_void (build_non_dependent_expr (expr), "statement");
436       
437       r = add_stmt (build_stmt (EXPR_STMT, expr));
438     }
439
440   finish_stmt ();
441
442   return r;
443 }
444
445
446 /* Begin an if-statement.  Returns a newly created IF_STMT if
447    appropriate.  */
448
449 tree
450 begin_if_stmt (void)
451 {
452   tree r;
453   do_pushlevel (sk_block);
454   r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
455   add_stmt (r);
456   return r;
457 }
458
459 /* Process the COND of an if-statement, which may be given by
460    IF_STMT.  */
461
462 void 
463 finish_if_stmt_cond (tree cond, tree if_stmt)
464 {
465   cond = maybe_convert_cond (cond);
466   FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
467 }
468
469 /* Finish the then-clause of an if-statement, which may be given by
470    IF_STMT.  */
471
472 tree
473 finish_then_clause (tree if_stmt)
474 {
475   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
476   return if_stmt;
477 }
478
479 /* Begin the else-clause of an if-statement.  */
480
481 void 
482 begin_else_clause (void)
483 {
484 }
485
486 /* Finish the else-clause of an if-statement, which may be given by
487    IF_STMT.  */
488
489 void
490 finish_else_clause (tree if_stmt)
491 {
492   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
493 }
494
495 /* Finish an if-statement.  */
496
497 void 
498 finish_if_stmt (void)
499 {
500   finish_stmt ();
501   do_poplevel ();
502 }
503
504 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
505    appropriate.  */
506
507 tree
508 begin_while_stmt (void)
509 {
510   tree r;
511   r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
512   add_stmt (r);
513   do_pushlevel (sk_block);
514   return r;
515 }
516
517 /* Process the COND of a while-statement, which may be given by
518    WHILE_STMT.  */
519
520 void 
521 finish_while_stmt_cond (tree cond, tree while_stmt)
522 {
523   cond = maybe_convert_cond (cond);
524   if (processing_template_decl)
525     /* Don't mess with condition decls in a template.  */
526     FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
527   else if (getdecls () == NULL_TREE)
528     /* It was a simple condition; install it.  */
529     WHILE_COND (while_stmt) = cond;
530   else
531     {
532       /* If there was a declaration in the condition, we can't leave it
533          there; transform
534             while (A x = 42) { }
535          to
536             while (true) { A x = 42; if (!x) break; }  */
537       tree if_stmt;
538       WHILE_COND (while_stmt) = boolean_true_node;
539
540       if_stmt = begin_if_stmt ();
541       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
542       finish_if_stmt_cond (cond, if_stmt);
543       finish_break_stmt ();
544       finish_then_clause (if_stmt);
545       finish_if_stmt ();
546     }
547 }
548
549 /* Finish a while-statement, which may be given by WHILE_STMT.  */
550
551 void 
552 finish_while_stmt (tree while_stmt)
553 {
554   do_poplevel ();
555   RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
556   finish_stmt ();
557 }
558
559 /* Begin a do-statement.  Returns a newly created DO_STMT if
560    appropriate.  */
561
562 tree
563 begin_do_stmt (void)
564 {
565   tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
566   add_stmt (r);
567   return r;
568 }
569
570 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
571
572 void
573 finish_do_body (tree do_stmt)
574 {
575   RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
576 }
577
578 /* Finish a do-statement, which may be given by DO_STMT, and whose
579    COND is as indicated.  */
580
581 void
582 finish_do_stmt (tree cond, tree do_stmt)
583 {
584   cond = maybe_convert_cond (cond);
585   DO_COND (do_stmt) = cond;
586   finish_stmt ();
587 }
588
589 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
590    indicated.  */
591
592 tree
593 finish_return_stmt (tree expr)
594 {
595   tree r;
596
597   expr = check_return_expr (expr);
598   if (!processing_template_decl)
599     {
600       if (DECL_DESTRUCTOR_P (current_function_decl))
601         {
602           /* Similarly, all destructors must run destructors for
603              base-classes before returning.  So, all returns in a
604              destructor get sent to the DTOR_LABEL; finish_function emits
605              code to return a value there.  */
606           return finish_goto_stmt (dtor_label);
607         }
608     }
609   r = add_stmt (build_stmt (RETURN_STMT, expr));
610   finish_stmt ();
611
612   return r;
613 }
614
615 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
616
617 tree
618 begin_for_stmt (void)
619 {
620   tree r;
621
622   r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE, 
623                   NULL_TREE, NULL_TREE);
624   NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
625   if (NEW_FOR_SCOPE_P (r))
626     do_pushlevel (sk_for);
627   add_stmt (r);
628
629   return r;
630 }
631
632 /* Finish the for-init-statement of a for-statement, which may be
633    given by FOR_STMT.  */
634
635 void
636 finish_for_init_stmt (tree for_stmt)
637 {
638   if (last_tree != for_stmt)
639     RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
640   do_pushlevel (sk_block);
641 }
642
643 /* Finish the COND of a for-statement, which may be given by
644    FOR_STMT.  */
645
646 void
647 finish_for_cond (tree cond, tree for_stmt)
648 {
649   cond = maybe_convert_cond (cond);
650   if (processing_template_decl)
651     /* Don't mess with condition decls in a template.  */
652     FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
653   else if (getdecls () == NULL_TREE)
654     /* It was a simple condition; install it.  */
655     FOR_COND (for_stmt) = cond;
656   else
657     {
658       /* If there was a declaration in the condition, we can't leave it
659          there; transform
660             for (; A x = 42;) { }
661          to
662             for (;;) { A x = 42; if (!x) break; }  */
663       tree if_stmt;
664       FOR_COND (for_stmt) = NULL_TREE;
665
666       if_stmt = begin_if_stmt ();
667       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
668       finish_if_stmt_cond (cond, if_stmt);
669       finish_break_stmt ();
670       finish_then_clause (if_stmt);
671       finish_if_stmt ();
672     }
673 }
674
675 /* Finish the increment-EXPRESSION in a for-statement, which may be
676    given by FOR_STMT.  */
677
678 void
679 finish_for_expr (tree expr, tree for_stmt)
680 {
681   FOR_EXPR (for_stmt) = expr;
682 }
683
684 /* Finish the body of a for-statement, which may be given by
685    FOR_STMT.  The increment-EXPR for the loop must be
686    provided.  */
687
688 void
689 finish_for_stmt (tree for_stmt)
690 {
691   /* Pop the scope for the body of the loop.  */
692   do_poplevel ();
693   RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
694   if (NEW_FOR_SCOPE_P (for_stmt))
695     do_poplevel ();
696   finish_stmt (); 
697 }
698
699 /* Finish a break-statement.  */
700
701 tree
702 finish_break_stmt (void)
703 {
704   return add_stmt (build_break_stmt ());
705 }
706
707 /* Finish a continue-statement.  */
708
709 tree
710 finish_continue_stmt (void)
711 {
712   return add_stmt (build_continue_stmt ());
713 }
714
715 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
716    appropriate.  */
717
718 tree
719 begin_switch_stmt (void)
720 {
721   tree r;
722   do_pushlevel (sk_block);
723   r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
724   add_stmt (r);
725   return r;
726 }
727
728 /* Finish the cond of a switch-statement.  */
729
730 void
731 finish_switch_cond (tree cond, tree switch_stmt)
732 {
733   tree orig_type = NULL;
734   if (!processing_template_decl)
735     {
736       tree index;
737
738       /* Convert the condition to an integer or enumeration type.  */
739       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
740       if (cond == NULL_TREE)
741         {
742           error ("switch quantity not an integer");
743           cond = error_mark_node;
744         }
745       orig_type = TREE_TYPE (cond);
746       if (cond != error_mark_node)
747         {
748           /* [stmt.switch]
749
750              Integral promotions are performed.  */
751           cond = perform_integral_promotions (cond);
752           cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
753         }
754
755       if (cond != error_mark_node)
756         {
757           index = get_unwidened (cond, NULL_TREE);
758           /* We can't strip a conversion from a signed type to an unsigned,
759              because if we did, int_fits_type_p would do the wrong thing
760              when checking case values for being in range,
761              and it's too hard to do the right thing.  */
762           if (TREE_UNSIGNED (TREE_TYPE (cond))
763               == TREE_UNSIGNED (TREE_TYPE (index)))
764             cond = index;
765         }
766     }
767   FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
768   SWITCH_TYPE (switch_stmt) = orig_type;
769   push_switch (switch_stmt);
770 }
771
772 /* Finish the body of a switch-statement, which may be given by
773    SWITCH_STMT.  The COND to switch on is indicated.  */
774
775 void
776 finish_switch_stmt (tree switch_stmt)
777 {
778   RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
779   pop_switch (); 
780   finish_stmt ();
781   do_poplevel ();
782 }
783
784 /* Generate the RTL for T, which is a TRY_BLOCK.  */
785
786 static void 
787 genrtl_try_block (tree t)
788 {
789   if (CLEANUP_P (t))
790     {
791       expand_eh_region_start ();
792       expand_stmt (TRY_STMTS (t));
793       expand_eh_region_end_cleanup (TRY_HANDLERS (t));
794     }
795   else
796     {
797       if (!FN_TRY_BLOCK_P (t)) 
798         emit_line_note (input_location);
799
800       expand_eh_region_start ();
801       expand_stmt (TRY_STMTS (t));
802
803       if (FN_TRY_BLOCK_P (t))
804         {
805           expand_start_all_catch ();
806           in_function_try_handler = 1;
807           expand_stmt (TRY_HANDLERS (t));
808           in_function_try_handler = 0;
809           expand_end_all_catch ();
810         }
811       else 
812         {
813           expand_start_all_catch ();  
814           expand_stmt (TRY_HANDLERS (t));
815           expand_end_all_catch ();
816         }
817     }
818 }
819
820 /* Generate the RTL for T, which is an EH_SPEC_BLOCK.  */
821
822 static void 
823 genrtl_eh_spec_block (tree t)
824 {
825   expand_eh_region_start ();
826   expand_stmt (EH_SPEC_STMTS (t));
827   expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
828                                 build_call (call_unexpected_node,
829                                             tree_cons (NULL_TREE,
830                                                        build_exc_ptr (),
831                                                        NULL_TREE)));
832 }
833
834 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
835    appropriate.  */
836
837 tree
838 begin_try_block (void)
839 {
840   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
841   add_stmt (r);
842   return r;
843 }
844
845 /* Likewise, for a function-try-block.  */
846
847 tree
848 begin_function_try_block (void)
849 {
850   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
851   FN_TRY_BLOCK_P (r) = 1;
852   add_stmt (r);
853   return r;
854 }
855
856 /* Finish a try-block, which may be given by TRY_BLOCK.  */
857
858 void
859 finish_try_block (tree try_block)
860 {
861   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
862 }
863
864 /* Finish the body of a cleanup try-block, which may be given by
865    TRY_BLOCK.  */
866
867 void
868 finish_cleanup_try_block (tree try_block)
869 {
870   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
871 }
872
873 /* Finish an implicitly generated try-block, with a cleanup is given
874    by CLEANUP.  */
875
876 void
877 finish_cleanup (tree cleanup, tree try_block)
878 {
879   TRY_HANDLERS (try_block) = cleanup;
880   CLEANUP_P (try_block) = 1;
881 }
882
883 /* Likewise, for a function-try-block.  */
884
885 void
886 finish_function_try_block (tree try_block)
887 {
888   if (TREE_CHAIN (try_block) 
889       && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
890     {
891       /* Chain the compound statement after the CTOR_INITIALIZER.  */
892       TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
893       /* And make the CTOR_INITIALIZER the body of the try-block.  */
894       RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
895     }
896   else
897     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
898   in_function_try_handler = 1;
899 }
900
901 /* Finish a handler-sequence for a try-block, which may be given by
902    TRY_BLOCK.  */
903
904 void
905 finish_handler_sequence (tree try_block)
906 {
907   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
908   check_handlers (TRY_HANDLERS (try_block));
909 }
910
911 /* Likewise, for a function-try-block.  */
912
913 void
914 finish_function_handler_sequence (tree try_block)
915 {
916   in_function_try_handler = 0;
917   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
918   check_handlers (TRY_HANDLERS (try_block));
919 }
920
921 /* Generate the RTL for T, which is a HANDLER.  */
922
923 static void
924 genrtl_handler (tree t)
925 {
926   genrtl_do_pushlevel ();
927   if (!processing_template_decl)
928     expand_start_catch (HANDLER_TYPE (t));
929   expand_stmt (HANDLER_BODY (t));
930   if (!processing_template_decl)
931     expand_end_catch ();
932 }
933
934 /* Begin a handler.  Returns a HANDLER if appropriate.  */
935
936 tree
937 begin_handler (void)
938 {
939   tree r;
940   r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
941   add_stmt (r);
942   /* Create a binding level for the eh_info and the exception object
943      cleanup.  */
944   do_pushlevel (sk_catch);
945   return r;
946 }
947
948 /* Finish the handler-parameters for a handler, which may be given by
949    HANDLER.  DECL is the declaration for the catch parameter, or NULL
950    if this is a `catch (...)' clause.  */
951
952 void
953 finish_handler_parms (tree decl, tree handler)
954 {
955   tree type = NULL_TREE;
956   if (processing_template_decl)
957     {
958       if (decl)
959         {
960           decl = pushdecl (decl);
961           decl = push_template_decl (decl);
962           add_decl_stmt (decl);
963           RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
964           type = TREE_TYPE (decl);
965         }
966     }
967   else
968     type = expand_start_catch_block (decl);
969
970   HANDLER_TYPE (handler) = type;
971   if (!processing_template_decl && type)
972     mark_used (eh_type_info (type));
973 }
974
975 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
976    the return value from the matching call to finish_handler_parms.  */
977
978 void
979 finish_handler (tree handler)
980 {
981   if (!processing_template_decl)
982     expand_end_catch_block ();
983   do_poplevel ();
984   RECHAIN_STMTS (handler, HANDLER_BODY (handler));
985 }
986
987 /* Begin a compound-statement.  If HAS_NO_SCOPE is true, the
988    compound-statement does not define a scope.  Returns a new
989    COMPOUND_STMT.  */
990
991 tree
992 begin_compound_stmt (bool has_no_scope)
993 {
994   tree r; 
995   int is_try = 0;
996
997   r = build_stmt (COMPOUND_STMT, NULL_TREE);
998
999   if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
1000     is_try = 1;
1001
1002   add_stmt (r);
1003   if (has_no_scope)
1004     COMPOUND_STMT_NO_SCOPE (r) = 1;
1005
1006   last_expr_type = NULL_TREE;
1007
1008   if (!has_no_scope)
1009     do_pushlevel (is_try ? sk_try : sk_block);
1010   else
1011     /* Normally, we try hard to keep the BLOCK for a
1012        statement-expression.  But, if it's a statement-expression with
1013        a scopeless block, there's nothing to keep, and we don't want
1014        to accidentally keep a block *inside* the scopeless block.  */ 
1015     keep_next_level (false);
1016
1017   return r;
1018 }
1019
1020 /* Finish a compound-statement, which is given by COMPOUND_STMT.  */
1021
1022 tree
1023 finish_compound_stmt (tree compound_stmt)
1024 {
1025   tree r;
1026   tree t;
1027
1028   if (COMPOUND_STMT_NO_SCOPE (compound_stmt))
1029     r = NULL_TREE;
1030   else
1031     r = do_poplevel ();
1032
1033   RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1034
1035   /* When we call finish_stmt we will lose LAST_EXPR_TYPE.  But, since
1036      the precise purpose of that variable is store the type of the
1037      last expression statement within the last compound statement, we
1038      preserve the value.  */
1039   t = last_expr_type;
1040   finish_stmt ();
1041   last_expr_type = t;
1042
1043   return r;
1044 }
1045
1046 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1047    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1048    CLOBBERS.  */
1049
1050 tree
1051 finish_asm_stmt (tree cv_qualifier, 
1052                  tree string, 
1053                  tree output_operands,
1054                  tree input_operands, 
1055                  tree clobbers)
1056 {
1057   tree r;
1058   tree t;
1059
1060   if (cv_qualifier != NULL_TREE
1061       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1062     {
1063       warning ("%s qualifier ignored on asm",
1064                   IDENTIFIER_POINTER (cv_qualifier));
1065       cv_qualifier = NULL_TREE;
1066     }
1067
1068   if (!processing_template_decl)
1069     {
1070       int i;
1071       int ninputs;
1072       int noutputs;
1073
1074       for (t = input_operands; t; t = TREE_CHAIN (t))
1075         {
1076           tree converted_operand 
1077             = decay_conversion (TREE_VALUE (t)); 
1078           
1079           /* If the type of the operand hasn't been determined (e.g.,
1080              because it involves an overloaded function), then issue
1081              an error message.  There's no context available to
1082              resolve the overloading.  */
1083           if (TREE_TYPE (converted_operand) == unknown_type_node)
1084             {
1085               error ("type of asm operand `%E' could not be determined", 
1086                         TREE_VALUE (t));
1087               converted_operand = error_mark_node;
1088             }
1089           TREE_VALUE (t) = converted_operand;
1090         }
1091
1092       ninputs = list_length (input_operands);
1093       noutputs = list_length (output_operands);
1094
1095       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1096         {
1097           bool allows_mem;
1098           bool allows_reg;
1099           bool is_inout;
1100           const char *constraint;
1101           tree operand;
1102
1103           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1104           operand = TREE_VALUE (t);
1105
1106           if (!parse_output_constraint (&constraint,
1107                                         i, ninputs, noutputs,
1108                                         &allows_mem,
1109                                         &allows_reg,
1110                                         &is_inout))
1111             {
1112               /* By marking this operand as erroneous, we will not try
1113                  to process this operand again in expand_asm_operands.  */
1114               TREE_VALUE (t) = error_mark_node;
1115               continue;
1116             }
1117
1118           /* If the operand is a DECL that is going to end up in
1119              memory, assume it is addressable.  This is a bit more
1120              conservative than it would ideally be; the exact test is
1121              buried deep in expand_asm_operands and depends on the
1122              DECL_RTL for the OPERAND -- which we don't have at this
1123              point.  */
1124           if (!allows_reg && DECL_P (operand))
1125             cxx_mark_addressable (operand);
1126         }
1127     }
1128
1129   r = build_stmt (ASM_STMT, cv_qualifier, string,
1130                   output_operands, input_operands,
1131                   clobbers);
1132   return add_stmt (r);
1133 }
1134
1135 /* Finish a label with the indicated NAME.  */
1136
1137 tree
1138 finish_label_stmt (tree name)
1139 {
1140   tree decl = define_label (input_location, name);
1141   return add_stmt (build_stmt (LABEL_STMT, decl));
1142 }
1143
1144 /* Finish a series of declarations for local labels.  G++ allows users
1145    to declare "local" labels, i.e., labels with scope.  This extension
1146    is useful when writing code involving statement-expressions.  */
1147
1148 void
1149 finish_label_decl (tree name)
1150 {
1151   tree decl = declare_local_label (name);
1152   add_decl_stmt (decl);
1153 }
1154
1155 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1156
1157 void 
1158 finish_decl_cleanup (tree decl, tree cleanup)
1159 {
1160   add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1161 }
1162
1163 /* If the current scope exits with an exception, run CLEANUP.  */
1164
1165 void
1166 finish_eh_cleanup (tree cleanup)
1167 {
1168   tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1169   CLEANUP_EH_ONLY (r) = 1;
1170   add_stmt (r);
1171 }
1172
1173 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1174    order they were written by the user.  Each node is as for
1175    emit_mem_initializers.  */
1176
1177 void
1178 finish_mem_initializers (tree mem_inits)
1179 {
1180   /* Reorder the MEM_INITS so that they are in the order they appeared
1181      in the source program.  */
1182   mem_inits = nreverse (mem_inits);
1183
1184   if (processing_template_decl)
1185     add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1186   else
1187     emit_mem_initializers (mem_inits);
1188 }
1189
1190 /* Returns the stack of SCOPE_STMTs for the current function.  */
1191
1192 tree *
1193 current_scope_stmt_stack (void)
1194 {
1195   return &cfun->language->base.x_scope_stmt_stack;
1196 }
1197
1198 /* Finish a parenthesized expression EXPR.  */
1199
1200 tree
1201 finish_parenthesized_expr (tree expr)
1202 {
1203   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1204     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1205     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1206
1207   if (TREE_CODE (expr) == OFFSET_REF)
1208     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1209        enclosed in parentheses.  */
1210     PTRMEM_OK_P (expr) = 0;
1211   return expr;
1212 }
1213
1214 /* Finish a reference to a non-static data member (DECL) that is not
1215    preceded by `.' or `->'.  */
1216
1217 tree
1218 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1219 {
1220   my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1221
1222   if (!object)
1223     {
1224       if (current_function_decl 
1225           && DECL_STATIC_FUNCTION_P (current_function_decl))
1226         cp_error_at ("invalid use of member `%D' in static member function",
1227                      decl);
1228       else
1229         cp_error_at ("invalid use of non-static data member `%D'", decl);
1230       error ("from this location");
1231
1232       return error_mark_node;
1233     }
1234   TREE_USED (current_class_ptr) = 1;
1235   if (processing_template_decl && !qualifying_scope)
1236     {
1237       tree type = TREE_TYPE (decl);
1238
1239       if (TREE_CODE (type) == REFERENCE_TYPE)
1240         type = TREE_TYPE (type);
1241       else
1242         {
1243           /* Set the cv qualifiers.  */
1244           int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1245           
1246           if (DECL_MUTABLE_P (decl))
1247             quals &= ~TYPE_QUAL_CONST;
1248
1249           quals |= cp_type_quals (TREE_TYPE (decl));
1250           type = cp_build_qualified_type (type, quals);
1251         }
1252       
1253       return build_min (COMPONENT_REF, type, object, decl);
1254     }
1255   else
1256     {
1257       tree access_type = TREE_TYPE (object);
1258       tree lookup_context = context_for_name_lookup (decl);
1259       
1260       while (!DERIVED_FROM_P (lookup_context, access_type))
1261         {
1262           access_type = TYPE_CONTEXT (access_type);
1263           while (access_type && DECL_P (access_type))
1264             access_type = DECL_CONTEXT (access_type);
1265
1266           if (!access_type)
1267             {
1268               cp_error_at ("object missing in reference to `%D'", decl);
1269               error ("from this location");
1270               return error_mark_node;
1271             }
1272         }
1273
1274       /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1275          QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
1276          for now.  */
1277       if (processing_template_decl)
1278         return build_min (SCOPE_REF, TREE_TYPE (decl),
1279                           qualifying_scope, DECL_NAME (decl));
1280
1281       perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1282
1283       /* If the data member was named `C::M', convert `*this' to `C'
1284          first.  */
1285       if (qualifying_scope)
1286         {
1287           tree binfo = NULL_TREE;
1288           object = build_scoped_ref (object, qualifying_scope,
1289                                      &binfo);
1290         }
1291
1292       return build_class_member_access_expr (object, decl,
1293                                              /*access_path=*/NULL_TREE,
1294                                              /*preserve_reference=*/false);
1295     }
1296 }
1297
1298 /* DECL was the declaration to which a qualified-id resolved.  Issue
1299    an error message if it is not accessible.  If OBJECT_TYPE is
1300    non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1301    type of `*x', or `x', respectively.  If the DECL was named as
1302    `A::B' then NESTED_NAME_SPECIFIER is `A'.  */
1303
1304 void
1305 check_accessibility_of_qualified_id (tree decl, 
1306                                      tree object_type, 
1307                                      tree nested_name_specifier)
1308 {
1309   tree scope;
1310   tree qualifying_type = NULL_TREE;
1311   
1312   /* Determine the SCOPE of DECL.  */
1313   scope = context_for_name_lookup (decl);
1314   /* If the SCOPE is not a type, then DECL is not a member.  */
1315   if (!TYPE_P (scope))
1316     return;
1317   /* Compute the scope through which DECL is being accessed.  */
1318   if (object_type 
1319       /* OBJECT_TYPE might not be a class type; consider:
1320
1321            class A { typedef int I; };
1322            I *p;
1323            p->A::I::~I();
1324
1325          In this case, we will have "A::I" as the DECL, but "I" as the
1326          OBJECT_TYPE.  */
1327       && CLASS_TYPE_P (object_type)
1328       && DERIVED_FROM_P (scope, object_type))
1329     /* If we are processing a `->' or `.' expression, use the type of the
1330        left-hand side.  */
1331     qualifying_type = object_type;
1332   else if (nested_name_specifier)
1333     {
1334       /* If the reference is to a non-static member of the
1335          current class, treat it as if it were referenced through
1336          `this'.  */
1337       if (DECL_NONSTATIC_MEMBER_P (decl)
1338           && current_class_ptr
1339           && DERIVED_FROM_P (scope, current_class_type))
1340         qualifying_type = current_class_type;
1341       /* Otherwise, use the type indicated by the
1342          nested-name-specifier.  */
1343       else
1344         qualifying_type = nested_name_specifier;
1345     }
1346   else
1347     /* Otherwise, the name must be from the current class or one of
1348        its bases.  */
1349     qualifying_type = currently_open_derived_class (scope);
1350
1351   if (qualifying_type)
1352     perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1353 }
1354
1355 /* EXPR is the result of a qualified-id.  The QUALIFYING_CLASS was the
1356    class named to the left of the "::" operator.  DONE is true if this
1357    expression is a complete postfix-expression; it is false if this
1358    expression is followed by '->', '[', '(', etc.  ADDRESS_P is true
1359    iff this expression is the operand of '&'.  */
1360
1361 tree
1362 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1363                           bool address_p)
1364 {
1365   if (error_operand_p (expr))
1366     return error_mark_node;
1367
1368   /* If EXPR occurs as the operand of '&', use special handling that
1369      permits a pointer-to-member.  */
1370   if (address_p && done)
1371     {
1372       if (TREE_CODE (expr) == SCOPE_REF)
1373         expr = TREE_OPERAND (expr, 1);
1374       expr = build_offset_ref (qualifying_class, expr, 
1375                                /*address_p=*/true);
1376       return expr;
1377     }
1378
1379   if (TREE_CODE (expr) == FIELD_DECL)
1380     expr = finish_non_static_data_member (expr, current_class_ref,
1381                                           qualifying_class);
1382   else if (BASELINK_P (expr) && !processing_template_decl)
1383     {
1384       tree fn;
1385       tree fns;
1386
1387       /* See if any of the functions are non-static members.  */
1388       fns = BASELINK_FUNCTIONS (expr);
1389       if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1390         fns = TREE_OPERAND (fns, 0);
1391       for (fn = fns; fn; fn = OVL_NEXT (fn))
1392         if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1393           break;
1394       /* If so, the expression may be relative to the current
1395          class.  */
1396       if (fn && current_class_type 
1397           && DERIVED_FROM_P (qualifying_class, current_class_type))
1398         expr = (build_class_member_access_expr 
1399                 (maybe_dummy_object (qualifying_class, NULL),
1400                  expr,
1401                  BASELINK_ACCESS_BINFO (expr),
1402                  /*preserve_reference=*/false));
1403       else if (done)
1404         /* The expression is a qualified name whose address is not
1405            being taken.  */
1406         expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1407     }
1408
1409   return expr;
1410 }
1411
1412 /* Begin a statement-expression.  The value returned must be passed to
1413    finish_stmt_expr.  */
1414
1415 tree 
1416 begin_stmt_expr (void)
1417 {
1418   /* If we're outside a function, we won't have a statement-tree to
1419      work with.  But, if we see a statement-expression we need to
1420      create one.  */
1421   if (! cfun && !last_tree)
1422     begin_stmt_tree (&scope_chain->x_saved_tree);
1423
1424   last_expr_type = NULL_TREE;
1425   
1426   keep_next_level (true);
1427
1428   return last_tree; 
1429 }
1430
1431 /* Process the final expression of a statement expression. EXPR can be
1432    NULL, if the final expression is empty.  Build up a TARGET_EXPR so
1433    that the result value can be safely returned to the enclosing
1434    expression.  */
1435
1436 tree
1437 finish_stmt_expr_expr (tree expr)
1438 {
1439   tree result = NULL_TREE;
1440   tree type = void_type_node;
1441
1442   if (expr)
1443     {
1444       type = TREE_TYPE (expr);
1445       
1446       if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1447         {
1448           if (TREE_CODE (type) == ARRAY_TYPE
1449               || TREE_CODE (type) == FUNCTION_TYPE)
1450             expr = decay_conversion (expr);
1451
1452           expr = convert_from_reference (expr);
1453           expr = require_complete_type (expr);
1454
1455           /* Build a TARGET_EXPR for this aggregate.  finish_stmt_expr
1456              will then pull it apart so the lifetime of the target is
1457              within the scope of the expression containing this statement
1458              expression.  */
1459           if (TREE_CODE (expr) == TARGET_EXPR)
1460             ;
1461           else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1462             expr = build_target_expr_with_type (expr, type);
1463           else
1464             {
1465               /* Copy construct.  */
1466               expr = build_special_member_call
1467                 (NULL_TREE, complete_ctor_identifier,
1468                  build_tree_list (NULL_TREE, expr),
1469                  TYPE_BINFO (type), LOOKUP_NORMAL);
1470               expr = build_cplus_new (type, expr);
1471               my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1472             }
1473         }
1474
1475       if (expr != error_mark_node)
1476         {
1477           result = build_stmt (EXPR_STMT, expr);
1478           add_stmt (result);
1479         }
1480     }
1481   
1482   finish_stmt ();
1483
1484   /* Remember the last expression so that finish_stmt_expr can pull it
1485      apart.  */
1486   last_expr_type = result ? result : void_type_node;
1487   
1488   return result;
1489 }
1490
1491 /* Finish a statement-expression.  EXPR should be the value returned
1492    by the previous begin_stmt_expr.  Returns an expression
1493    representing the statement-expression.  */
1494
1495 tree 
1496 finish_stmt_expr (tree rtl_expr, bool has_no_scope)
1497 {
1498   tree result;
1499   tree result_stmt = last_expr_type;
1500   tree type;
1501   
1502   if (!last_expr_type)
1503     type = void_type_node;
1504   else
1505     {
1506       if (result_stmt == void_type_node)
1507         {
1508           type = void_type_node;
1509           result_stmt = NULL_TREE;
1510         }
1511       else
1512         type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1513     }
1514   
1515   result = build_min (STMT_EXPR, type, last_tree);
1516   TREE_SIDE_EFFECTS (result) = 1;
1517   STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1518   
1519   last_expr_type = NULL_TREE;
1520   
1521   /* Remove the compound statement from the tree structure; it is
1522      now saved in the STMT_EXPR.  */
1523   last_tree = rtl_expr;
1524   TREE_CHAIN (last_tree) = NULL_TREE;
1525
1526   /* If we created a statement-tree for this statement-expression,
1527      remove it now.  */ 
1528   if (! cfun
1529       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1530     finish_stmt_tree (&scope_chain->x_saved_tree);
1531
1532   if (processing_template_decl)
1533     return result;
1534
1535   if (!VOID_TYPE_P (type))
1536     {
1537       /* Pull out the TARGET_EXPR that is the final expression. Put
1538          the target's init_expr as the final expression and then put
1539          the statement expression itself as the target's init
1540          expr. Finally, return the target expression.  */
1541       tree last_expr = EXPR_STMT_EXPR (result_stmt);
1542       
1543       my_friendly_assert (TREE_CODE (last_expr) == TARGET_EXPR, 20030729);
1544       EXPR_STMT_EXPR (result_stmt) = TREE_OPERAND (last_expr, 1);
1545       TREE_OPERAND (last_expr, 1) = result;
1546       result = last_expr;
1547     }
1548   return result;
1549 }
1550
1551 /* Perform Koenig lookup.  FN is the postfix-expression representing
1552    the function (or functions) to call; ARGS are the arguments to the
1553    call.  Returns the functions to be considered by overload
1554    resolution.  */
1555
1556 tree
1557 perform_koenig_lookup (tree fn, tree args)
1558 {
1559   tree identifier = NULL_TREE;
1560   tree functions = NULL_TREE;
1561
1562   /* Find the name of the overloaded function.  */
1563   if (TREE_CODE (fn) == IDENTIFIER_NODE)
1564     identifier = fn;
1565   else if (is_overloaded_fn (fn))
1566     {
1567       functions = fn;
1568       identifier = DECL_NAME (get_first_fn (functions));
1569     }
1570   else if (DECL_P (fn))
1571     {
1572       functions = fn;
1573       identifier = DECL_NAME (fn);
1574     }
1575
1576   /* A call to a namespace-scope function using an unqualified name.
1577
1578      Do Koenig lookup -- unless any of the arguments are
1579      type-dependent.  */
1580   if (!any_type_dependent_arguments_p (args))
1581     {
1582       fn = lookup_arg_dependent (identifier, functions, args);
1583       if (!fn)
1584         /* The unqualified name could not be resolved.  */
1585         fn = unqualified_fn_lookup_error (identifier);
1586     }
1587   else
1588     fn = identifier;
1589
1590   return fn;
1591 }
1592
1593 /* Generate an expression for `FN (ARGS)'.
1594
1595    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1596    as a virtual call, even if FN is virtual.  (This flag is set when
1597    encountering an expression where the function name is explicitly
1598    qualified.  For example a call to `X::f' never generates a virtual
1599    call.)
1600
1601    Returns code for the call.  */
1602
1603 tree 
1604 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1605 {
1606   tree result;
1607   tree orig_fn;
1608   tree orig_args;
1609
1610   if (fn == error_mark_node || args == error_mark_node)
1611     return error_mark_node;
1612
1613   /* ARGS should be a list of arguments.  */
1614   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1615                       20020712);
1616
1617   orig_fn = fn;
1618   orig_args = args;
1619
1620   if (processing_template_decl)
1621     {
1622       if (type_dependent_expression_p (fn)
1623           || any_type_dependent_arguments_p (args))
1624         {
1625           result = build_nt (CALL_EXPR, fn, args);
1626           KOENIG_LOOKUP_P (result) = koenig_p;
1627           return result;
1628         }
1629       if (!BASELINK_P (fn)
1630           && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1631           && TREE_TYPE (fn) != unknown_type_node)
1632         fn = build_non_dependent_expr (fn);
1633       args = build_non_dependent_args (orig_args);
1634     }
1635
1636   /* A reference to a member function will appear as an overloaded
1637      function (rather than a BASELINK) if an unqualified name was used
1638      to refer to it.  */
1639   if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1640     {
1641       tree f = fn;
1642
1643       if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1644         f = TREE_OPERAND (f, 0);
1645       f = get_first_fn (f);
1646       if (DECL_FUNCTION_MEMBER_P (f))
1647         {
1648           tree type = currently_open_derived_class (DECL_CONTEXT (f));
1649           if (!type)
1650             type = DECL_CONTEXT (f);
1651           fn = build_baselink (TYPE_BINFO (type),
1652                                TYPE_BINFO (type),
1653                                fn, /*optype=*/NULL_TREE);
1654         }
1655     }
1656
1657   result = NULL_TREE;
1658   if (BASELINK_P (fn))
1659     {
1660       tree object;
1661
1662       /* A call to a member function.  From [over.call.func]:
1663
1664            If the keyword this is in scope and refers to the class of
1665            that member function, or a derived class thereof, then the
1666            function call is transformed into a qualified function call
1667            using (*this) as the postfix-expression to the left of the
1668            . operator.... [Otherwise] a contrived object of type T
1669            becomes the implied object argument.  
1670
1671         This paragraph is unclear about this situation:
1672
1673           struct A { void f(); };
1674           struct B : public A {};
1675           struct C : public A { void g() { B::f(); }};
1676
1677         In particular, for `B::f', this paragraph does not make clear
1678         whether "the class of that member function" refers to `A' or 
1679         to `B'.  We believe it refers to `B'.  */
1680       if (current_class_type 
1681           && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1682                              current_class_type)
1683           && current_class_ref)
1684         object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1685                                      NULL);
1686       else
1687         {
1688           tree representative_fn;
1689
1690           representative_fn = BASELINK_FUNCTIONS (fn);
1691           if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1692             representative_fn = TREE_OPERAND (representative_fn, 0);
1693           representative_fn = get_first_fn (representative_fn);
1694           object = build_dummy_object (DECL_CONTEXT (representative_fn));
1695         }
1696
1697       if (processing_template_decl)
1698         {
1699           if (type_dependent_expression_p (object))
1700             return build_nt (CALL_EXPR, orig_fn, orig_args);
1701           object = build_non_dependent_expr (object);
1702         }
1703
1704       result = build_new_method_call (object, fn, args, NULL_TREE,
1705                                       (disallow_virtual 
1706                                        ? LOOKUP_NONVIRTUAL : 0));
1707     }
1708   else if (is_overloaded_fn (fn))
1709     /* A call to a namespace-scope function.  */
1710     result = build_new_function_call (fn, args);
1711   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1712     {
1713       if (args)
1714         error ("arguments to destructor are not allowed");
1715       /* Mark the pseudo-destructor call as having side-effects so
1716          that we do not issue warnings about its use.  */
1717       result = build1 (NOP_EXPR,
1718                        void_type_node,
1719                        TREE_OPERAND (fn, 0));
1720       TREE_SIDE_EFFECTS (result) = 1;
1721     }
1722   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1723     /* If the "function" is really an object of class type, it might
1724        have an overloaded `operator ()'.  */
1725     result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1726   if (!result)
1727     /* A call where the function is unknown.  */
1728     result = build_function_call (fn, args);
1729
1730   if (processing_template_decl)
1731     {
1732       result = build (CALL_EXPR, TREE_TYPE (result), orig_fn, orig_args);
1733       KOENIG_LOOKUP_P (result) = koenig_p;
1734     }
1735   return result;
1736 }
1737
1738 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1739    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1740    POSTDECREMENT_EXPR.)  */
1741
1742 tree 
1743 finish_increment_expr (tree expr, enum tree_code code)
1744 {
1745   return build_x_unary_op (code, expr);  
1746 }
1747
1748 /* Finish a use of `this'.  Returns an expression for `this'.  */
1749
1750 tree 
1751 finish_this_expr (void)
1752 {
1753   tree result;
1754
1755   if (current_class_ptr)
1756     {
1757       result = current_class_ptr;
1758     }
1759   else if (current_function_decl
1760            && DECL_STATIC_FUNCTION_P (current_function_decl))
1761     {
1762       error ("`this' is unavailable for static member functions");
1763       result = error_mark_node;
1764     }
1765   else
1766     {
1767       if (current_function_decl)
1768         error ("invalid use of `this' in non-member function");
1769       else
1770         error ("invalid use of `this' at top level");
1771       result = error_mark_node;
1772     }
1773
1774   return result;
1775 }
1776
1777 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
1778    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1779    the TYPE for the type given.  If SCOPE is non-NULL, the expression
1780    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
1781
1782 tree 
1783 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1784 {
1785   if (destructor == error_mark_node)
1786     return error_mark_node;
1787
1788   my_friendly_assert (TYPE_P (destructor), 20010905);
1789
1790   if (!processing_template_decl)
1791     {
1792       if (scope == error_mark_node)
1793         {
1794           error ("invalid qualifying scope in pseudo-destructor name");
1795           return error_mark_node;
1796         }
1797       
1798       if (!same_type_p (TREE_TYPE (object), destructor))
1799         {
1800           error ("`%E' is not of type `%T'", object, destructor);
1801           return error_mark_node;
1802         }
1803     }
1804
1805   return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1806 }
1807
1808 /* Finish an expression of the form CODE EXPR.  */
1809
1810 tree
1811 finish_unary_op_expr (enum tree_code code, tree expr)
1812 {
1813   tree result = build_x_unary_op (code, expr);
1814   /* Inside a template, build_x_unary_op does not fold the
1815      expression. So check whether the result is folded before
1816      setting TREE_NEGATED_INT.  */
1817   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1818       && TREE_CODE (result) == INTEGER_CST
1819       && !TREE_UNSIGNED (TREE_TYPE (result))
1820       && INT_CST_LT (result, integer_zero_node))
1821     TREE_NEGATED_INT (result) = 1;
1822   overflow_warning (result);
1823   return result;
1824 }
1825
1826 /* Finish a compound-literal expression.  TYPE is the type to which
1827    the INITIALIZER_LIST is being cast.  */
1828
1829 tree
1830 finish_compound_literal (tree type, tree initializer_list)
1831 {
1832   tree compound_literal;
1833
1834   /* Build a CONSTRUCTOR for the INITIALIZER_LIST.  */
1835   compound_literal = build_constructor (NULL_TREE, initializer_list);
1836   /* Mark it as a compound-literal.  */
1837   TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1838   if (processing_template_decl)
1839     TREE_TYPE (compound_literal) = type;
1840   else
1841     {
1842       /* Check the initialization.  */
1843       compound_literal = digest_init (type, compound_literal, NULL);
1844       /* If the TYPE was an array type with an unknown bound, then we can
1845          figure out the dimension now.  For example, something like:
1846
1847            `(int []) { 2, 3 }'
1848
1849          implies that the array has two elements.  */
1850       if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1851         complete_array_type (type, compound_literal, 1);
1852     }
1853
1854   return compound_literal;
1855 }
1856
1857 /* Return the declaration for the function-name variable indicated by
1858    ID.  */
1859
1860 tree
1861 finish_fname (tree id)
1862 {
1863   tree decl;
1864   
1865   decl = fname_decl (C_RID_CODE (id), id);
1866   if (processing_template_decl)
1867     decl = DECL_NAME (decl);
1868   return decl;
1869 }
1870
1871 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1872    and DECLARATOR.  Returns nonzero if the function-declaration is
1873    valid.  */
1874
1875 int
1876 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1877 {
1878   if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1879     return 0;
1880
1881   /* The things we're about to see are not directly qualified by any
1882      template headers we've seen thus far.  */
1883   reset_specialization ();
1884
1885   return 1;
1886 }
1887
1888 /* Finish a translation unit.  */
1889
1890 void 
1891 finish_translation_unit (void)
1892 {
1893   /* In case there were missing closebraces,
1894      get us back to the global binding level.  */
1895   pop_everything ();
1896   while (current_namespace != global_namespace)
1897     pop_namespace ();
1898
1899   /* Do file scope __FUNCTION__ et al.  */
1900   finish_fname_decls ();
1901 }
1902
1903 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1904    Returns the parameter.  */
1905
1906 tree 
1907 finish_template_type_parm (tree aggr, tree identifier)
1908 {
1909   if (aggr != class_type_node)
1910     {
1911       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1912       aggr = class_type_node;
1913     }
1914
1915   return build_tree_list (aggr, identifier);
1916 }
1917
1918 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1919    Returns the parameter.  */
1920
1921 tree 
1922 finish_template_template_parm (tree aggr, tree identifier)
1923 {
1924   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1925   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1926   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1927   DECL_TEMPLATE_RESULT (tmpl) = decl;
1928   DECL_ARTIFICIAL (decl) = 1;
1929   end_template_decl ();
1930
1931   my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1932
1933   return finish_template_type_parm (aggr, tmpl);
1934 }
1935
1936 /* ARGUMENT is the default-argument value for a template template
1937    parameter.  If ARGUMENT is invalid, issue error messages and return
1938    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
1939
1940 tree
1941 check_template_template_default_arg (tree argument)
1942 {
1943   if (TREE_CODE (argument) != TEMPLATE_DECL
1944       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1945       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1946     {
1947       if (TREE_CODE (argument) == TYPE_DECL)
1948         {
1949           tree t = TREE_TYPE (argument);
1950
1951           /* Try to emit a slightly smarter error message if we detect
1952              that the user is using a template instantiation.  */
1953           if (CLASSTYPE_TEMPLATE_INFO (t) 
1954               && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1955             error ("invalid use of type `%T' as a default value for a "
1956                    "template template-parameter", t);
1957           else
1958             error ("invalid use of `%D' as a default value for a template "
1959                    "template-parameter", argument);
1960         }
1961       else
1962         error ("invalid default argument for a template template parameter");
1963       return error_mark_node;
1964     }
1965
1966   return argument;
1967 }
1968
1969 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1970    nonzero, the parameter list was terminated by a `...'.  */
1971
1972 tree
1973 finish_parmlist (tree parms, int ellipsis)
1974 {
1975   if (parms)
1976     {
1977       /* We mark the PARMS as a parmlist so that declarator processing can
1978          disambiguate certain constructs.  */
1979       TREE_PARMLIST (parms) = 1;
1980       /* We do not append void_list_node here, but leave it to grokparms
1981          to do that.  */
1982       PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1983     }
1984   return parms;
1985 }
1986
1987 /* Begin a class definition, as indicated by T.  */
1988
1989 tree
1990 begin_class_definition (tree t)
1991 {
1992   if (t == error_mark_node)
1993     return error_mark_node;
1994
1995   if (processing_template_parmlist)
1996     {
1997       error ("definition of `%#T' inside template parameter list", t);
1998       return error_mark_node;
1999     }
2000   /* A non-implicit typename comes from code like:
2001
2002        template <typename T> struct A {
2003          template <typename U> struct A<T>::B ...
2004
2005      This is erroneous.  */
2006   else if (TREE_CODE (t) == TYPENAME_TYPE)
2007     {
2008       error ("invalid definition of qualified type `%T'", t);
2009       t = error_mark_node;
2010     }
2011
2012   if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2013     {
2014       t = make_aggr_type (RECORD_TYPE);
2015       pushtag (make_anon_name (), t, 0);
2016     }
2017
2018   /* If this type was already complete, and we see another definition,
2019      that's an error.  */
2020   if (COMPLETE_TYPE_P (t))
2021     {
2022       error ("redefinition of `%#T'", t);
2023       cp_error_at ("previous definition of `%#T'", t);
2024       return error_mark_node;
2025     }
2026
2027   /* Update the location of the decl.  */
2028   DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2029   
2030   if (TYPE_BEING_DEFINED (t))
2031     {
2032       t = make_aggr_type (TREE_CODE (t));
2033       pushtag (TYPE_IDENTIFIER (t), t, 0);
2034     }
2035   maybe_process_partial_specialization (t);
2036   pushclass (t);
2037   TYPE_BEING_DEFINED (t) = 1;
2038   TYPE_PACKED (t) = flag_pack_struct;
2039   /* Reset the interface data, at the earliest possible
2040      moment, as it might have been set via a class foo;
2041      before.  */
2042   if (! TYPE_ANONYMOUS_P (t))
2043     {
2044       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2045       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2046         (t, interface_unknown);
2047     }
2048   reset_specialization();
2049   
2050   /* Make a declaration for this class in its own scope.  */
2051   build_self_reference ();
2052
2053   return t;
2054 }
2055
2056 /* Finish the member declaration given by DECL.  */
2057
2058 void
2059 finish_member_declaration (tree decl)
2060 {
2061   if (decl == error_mark_node || decl == NULL_TREE)
2062     return;
2063
2064   if (decl == void_type_node)
2065     /* The COMPONENT was a friend, not a member, and so there's
2066        nothing for us to do.  */
2067     return;
2068
2069   /* We should see only one DECL at a time.  */
2070   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2071
2072   /* Set up access control for DECL.  */
2073   TREE_PRIVATE (decl) 
2074     = (current_access_specifier == access_private_node);
2075   TREE_PROTECTED (decl) 
2076     = (current_access_specifier == access_protected_node);
2077   if (TREE_CODE (decl) == TEMPLATE_DECL)
2078     {
2079       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2080       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2081     }
2082
2083   /* Mark the DECL as a member of the current class.  */
2084   DECL_CONTEXT (decl) = current_class_type;
2085
2086   /* [dcl.link]
2087
2088      A C language linkage is ignored for the names of class members
2089      and the member function type of class member functions.  */
2090   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2091     SET_DECL_LANGUAGE (decl, lang_cplusplus);
2092
2093   /* Put functions on the TYPE_METHODS list and everything else on the
2094      TYPE_FIELDS list.  Note that these are built up in reverse order.
2095      We reverse them (to obtain declaration order) in finish_struct.  */
2096   if (TREE_CODE (decl) == FUNCTION_DECL 
2097       || DECL_FUNCTION_TEMPLATE_P (decl))
2098     {
2099       /* We also need to add this function to the
2100          CLASSTYPE_METHOD_VEC.  */
2101       add_method (current_class_type, decl, /*error_p=*/0);
2102
2103       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2104       TYPE_METHODS (current_class_type) = decl;
2105
2106       maybe_add_class_template_decl_list (current_class_type, decl, 
2107                                           /*friend_p=*/0);
2108     }
2109   /* Enter the DECL into the scope of the class.  */
2110   else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2111            || pushdecl_class_level (decl))
2112     {
2113       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2114          go at the beginning.  The reason is that lookup_field_1
2115          searches the list in order, and we want a field name to
2116          override a type name so that the "struct stat hack" will
2117          work.  In particular:
2118
2119            struct S { enum E { }; int E } s;
2120            s.E = 3;
2121
2122          is valid.  In addition, the FIELD_DECLs must be maintained in
2123          declaration order so that class layout works as expected.
2124          However, we don't need that order until class layout, so we
2125          save a little time by putting FIELD_DECLs on in reverse order
2126          here, and then reversing them in finish_struct_1.  (We could
2127          also keep a pointer to the correct insertion points in the
2128          list.)  */
2129
2130       if (TREE_CODE (decl) == TYPE_DECL)
2131         TYPE_FIELDS (current_class_type) 
2132           = chainon (TYPE_FIELDS (current_class_type), decl);
2133       else
2134         {
2135           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2136           TYPE_FIELDS (current_class_type) = decl;
2137         }
2138
2139       maybe_add_class_template_decl_list (current_class_type, decl, 
2140                                           /*friend_p=*/0);
2141     }
2142 }
2143
2144 /* Finish processing the declaration of a member class template
2145    TYPES whose template parameters are given by PARMS.  */
2146
2147 tree
2148 finish_member_class_template (tree types)
2149 {
2150   tree t;
2151
2152   /* If there are declared, but undefined, partial specializations
2153      mixed in with the typespecs they will not yet have passed through
2154      maybe_process_partial_specialization, so we do that here.  */
2155   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2156     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2157       maybe_process_partial_specialization (TREE_VALUE (t));
2158
2159   grok_x_components (types);
2160   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2161     /* The component was in fact a friend declaration.  We avoid
2162        finish_member_template_decl performing certain checks by
2163        unsetting TYPES.  */
2164     types = NULL_TREE;
2165   
2166   finish_member_template_decl (types);
2167
2168   /* As with other component type declarations, we do
2169      not store the new DECL on the list of
2170      component_decls.  */
2171   return NULL_TREE;
2172 }
2173
2174 /* Finish processing a complete template declaration.  The PARMS are
2175    the template parameters.  */
2176
2177 void
2178 finish_template_decl (tree parms)
2179 {
2180   if (parms)
2181     end_template_decl ();
2182   else
2183     end_specialization ();
2184 }
2185
2186 /* Finish processing a template-id (which names a type) of the form
2187    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2188    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
2189    the scope of template-id indicated.  */
2190
2191 tree
2192 finish_template_type (tree name, tree args, int entering_scope)
2193 {
2194   tree decl;
2195
2196   decl = lookup_template_class (name, args,
2197                                 NULL_TREE, NULL_TREE, entering_scope,
2198                                 tf_error | tf_warning | tf_user);
2199   if (decl != error_mark_node)
2200     decl = TYPE_STUB_DECL (decl);
2201
2202   return decl;
2203 }
2204
2205 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2206    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2207    BASE_CLASS, or NULL_TREE if an error occurred.  The
2208    ACCESS_SPECIFIER is one of
2209    access_{default,public,protected_private}[_virtual]_node.*/
2210
2211 tree 
2212 finish_base_specifier (tree base, tree access, bool virtual_p)
2213 {
2214   tree result;
2215
2216   if (base == error_mark_node)
2217     {
2218       error ("invalid base-class specification");
2219       result = NULL_TREE;
2220     }
2221   else if (! is_aggr_type (base, 1))
2222     result = NULL_TREE;
2223   else
2224     {
2225       if (cp_type_quals (base) != 0)
2226         {
2227           error ("base class `%T' has cv qualifiers", base);
2228           base = TYPE_MAIN_VARIANT (base);
2229         }
2230       result = build_tree_list (access, base);
2231       TREE_VIA_VIRTUAL (result) = virtual_p;
2232     }
2233
2234   return result;
2235 }
2236
2237 /* Called when multiple declarators are processed.  If that is not
2238    permitted in this context, an error is issued.  */
2239
2240 void
2241 check_multiple_declarators (void)
2242 {
2243   /* [temp]
2244      
2245      In a template-declaration, explicit specialization, or explicit
2246      instantiation the init-declarator-list in the declaration shall
2247      contain at most one declarator.  
2248
2249      We don't just use PROCESSING_TEMPLATE_DECL for the first
2250      condition since that would disallow the perfectly valid code, 
2251      like `template <class T> struct S { int i, j; };'.  */
2252   if (at_function_scope_p ())
2253     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2254     return;
2255      
2256   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2257       || processing_explicit_instantiation
2258       || processing_specialization)
2259     error ("multiple declarators in template declaration");
2260 }
2261
2262 /* Issue a diagnostic that NAME cannot be found in SCOPE.  */
2263
2264 void
2265 qualified_name_lookup_error (tree scope, tree name)
2266 {
2267   if (TYPE_P (scope))
2268     {
2269       if (!COMPLETE_TYPE_P (scope))
2270         error ("incomplete type `%T' used in nested name specifier", scope);
2271       else
2272         error ("`%D' is not a member of `%T'", name, scope);
2273     }
2274   else if (scope != global_namespace)
2275     error ("`%D' is not a member of `%D'", name, scope);
2276   else
2277     error ("`::%D' has not been declared", name);
2278 }
2279               
2280 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2281    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
2282    if non-NULL, is the type or namespace used to explicitly qualify
2283    ID_EXPRESSION.  DECL is the entity to which that name has been
2284    resolved.  
2285
2286    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2287    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
2288    be set to true if this expression isn't permitted in a
2289    constant-expression, but it is otherwise not set by this function.
2290    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2291    constant-expression, but a non-constant expression is also
2292    permissible.
2293
2294    If an error occurs, and it is the kind of error that might cause
2295    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
2296    is the caller's responsibility to issue the message.  *ERROR_MSG
2297    will be a string with static storage duration, so the caller need
2298    not "free" it.
2299
2300    Return an expression for the entity, after issuing appropriate
2301    diagnostics.  This function is also responsible for transforming a
2302    reference to a non-static member into a COMPONENT_REF that makes
2303    the use of "this" explicit.  
2304
2305    Upon return, *IDK will be filled in appropriately.  */
2306
2307 tree
2308 finish_id_expression (tree id_expression, 
2309                       tree decl,
2310                       tree scope,
2311                       cp_id_kind *idk,
2312                       tree *qualifying_class,
2313                       bool integral_constant_expression_p,
2314                       bool allow_non_integral_constant_expression_p,
2315                       bool *non_integral_constant_expression_p,
2316                       const char **error_msg)
2317 {
2318   /* Initialize the output parameters.  */
2319   *idk = CP_ID_KIND_NONE;
2320   *error_msg = NULL;
2321
2322   if (id_expression == error_mark_node)
2323     return error_mark_node;
2324   /* If we have a template-id, then no further lookup is
2325      required.  If the template-id was for a template-class, we
2326      will sometimes have a TYPE_DECL at this point.  */
2327   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2328            || TREE_CODE (decl) == TYPE_DECL)
2329     ;
2330   /* Look up the name.  */
2331   else 
2332     {
2333       if (decl == error_mark_node)
2334         {
2335           /* Name lookup failed.  */
2336           if (scope && (!TYPE_P (scope) || !dependent_type_p (scope)))
2337             {
2338               /* Qualified name lookup failed, and the qualifying name
2339                  was not a dependent type.  That is always an
2340                  error.  */
2341               qualified_name_lookup_error (scope, id_expression);
2342               return error_mark_node;
2343             }
2344           else if (!scope)
2345             {
2346               /* It may be resolved via Koenig lookup.  */
2347               *idk = CP_ID_KIND_UNQUALIFIED;
2348               return id_expression;
2349             }
2350         }
2351       /* If DECL is a variable that would be out of scope under
2352          ANSI/ISO rules, but in scope in the ARM, name lookup
2353          will succeed.  Issue a diagnostic here.  */
2354       else
2355         decl = check_for_out_of_scope_variable (decl);
2356
2357       /* Remember that the name was used in the definition of
2358          the current class so that we can check later to see if
2359          the meaning would have been different after the class
2360          was entirely defined.  */
2361       if (!scope && decl != error_mark_node)
2362         maybe_note_name_used_in_class (id_expression, decl);
2363     }
2364
2365   /* If we didn't find anything, or what we found was a type,
2366      then this wasn't really an id-expression.  */
2367   if (TREE_CODE (decl) == TEMPLATE_DECL
2368       && !DECL_FUNCTION_TEMPLATE_P (decl))
2369     {
2370       *error_msg = "missing template arguments";
2371       return error_mark_node;
2372     }
2373   else if (TREE_CODE (decl) == TYPE_DECL
2374            || TREE_CODE (decl) == NAMESPACE_DECL)
2375     {
2376       *error_msg = "expected primary-expression";
2377       return error_mark_node;
2378     }
2379
2380   /* If the name resolved to a template parameter, there is no
2381      need to look it up again later.  */
2382   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2383       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2384     {
2385       *idk = CP_ID_KIND_NONE;
2386       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2387         decl = TEMPLATE_PARM_DECL (decl);
2388       if (integral_constant_expression_p 
2389           && !dependent_type_p (TREE_TYPE (decl))
2390           && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))) 
2391         {
2392           if (!allow_non_integral_constant_expression_p)
2393             error ("template parameter `%D' of type `%T' is not allowed in "
2394                    "an integral constant expression because it is not of "
2395                    "integral or enumeration type", decl, TREE_TYPE (decl));
2396           *non_integral_constant_expression_p = true;
2397         }
2398       return DECL_INITIAL (decl);
2399     }
2400   /* Similarly, we resolve enumeration constants to their 
2401      underlying values.  */
2402   else if (TREE_CODE (decl) == CONST_DECL)
2403     {
2404       *idk = CP_ID_KIND_NONE;
2405       if (!processing_template_decl)
2406         return DECL_INITIAL (decl);
2407       return decl;
2408     }
2409   else
2410     {
2411       bool dependent_p;
2412
2413       /* If the declaration was explicitly qualified indicate
2414          that.  The semantics of `A::f(3)' are different than
2415          `f(3)' if `f' is virtual.  */
2416       *idk = (scope 
2417               ? CP_ID_KIND_QUALIFIED
2418               : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2419                  ? CP_ID_KIND_TEMPLATE_ID
2420                  : CP_ID_KIND_UNQUALIFIED));
2421
2422
2423       /* [temp.dep.expr]
2424
2425          An id-expression is type-dependent if it contains an
2426          identifier that was declared with a dependent type.
2427
2428          The standard is not very specific about an id-expression that
2429          names a set of overloaded functions.  What if some of them
2430          have dependent types and some of them do not?  Presumably,
2431          such a name should be treated as a dependent name.  */
2432       /* Assume the name is not dependent.  */
2433       dependent_p = false;
2434       if (!processing_template_decl)
2435         /* No names are dependent outside a template.  */
2436         ;
2437       /* A template-id where the name of the template was not resolved
2438          is definitely dependent.  */
2439       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2440                && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2441                    == IDENTIFIER_NODE))
2442         dependent_p = true;
2443       /* For anything except an overloaded function, just check its
2444          type.  */
2445       else if (!is_overloaded_fn (decl))
2446         dependent_p 
2447           = dependent_type_p (TREE_TYPE (decl));
2448       /* For a set of overloaded functions, check each of the
2449          functions.  */
2450       else
2451         {
2452           tree fns = decl;
2453
2454           if (BASELINK_P (fns))
2455             fns = BASELINK_FUNCTIONS (fns);
2456
2457           /* For a template-id, check to see if the template
2458              arguments are dependent.  */
2459           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2460             {
2461               tree args = TREE_OPERAND (fns, 1);
2462               dependent_p = any_dependent_template_arguments_p (args);
2463               /* The functions are those referred to by the
2464                  template-id.  */
2465               fns = TREE_OPERAND (fns, 0);
2466             }
2467
2468           /* If there are no dependent template arguments, go through
2469              the overloaded functions.  */
2470           while (fns && !dependent_p)
2471             {
2472               tree fn = OVL_CURRENT (fns);
2473
2474               /* Member functions of dependent classes are
2475                  dependent.  */
2476               if (TREE_CODE (fn) == FUNCTION_DECL
2477                   && type_dependent_expression_p (fn))
2478                 dependent_p = true;
2479               else if (TREE_CODE (fn) == TEMPLATE_DECL
2480                        && dependent_template_p (fn))
2481                 dependent_p = true;
2482
2483               fns = OVL_NEXT (fns);
2484             }
2485         }
2486
2487       /* If the name was dependent on a template parameter, we will
2488          resolve the name at instantiation time.  */
2489       if (dependent_p)
2490         {
2491           /* Create a SCOPE_REF for qualified names, if the scope is
2492              dependent.  */
2493           if (scope)
2494             {
2495               if (TYPE_P (scope))
2496                 *qualifying_class = scope;
2497               /* Since this name was dependent, the expression isn't
2498                  constant -- yet.  No error is issued because it might
2499                  be constant when things are instantiated.  */
2500               if (integral_constant_expression_p)
2501                 *non_integral_constant_expression_p = true;
2502               if (TYPE_P (scope) && dependent_type_p (scope))
2503                 return build_nt (SCOPE_REF, scope, id_expression);
2504               else if (TYPE_P (scope) && DECL_P (decl))
2505                 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2506                               id_expression);
2507               else
2508                 return decl;
2509             }
2510           /* A TEMPLATE_ID already contains all the information we
2511              need.  */
2512           if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2513             return id_expression;
2514           /* Since this name was dependent, the expression isn't
2515              constant -- yet.  No error is issued because it might be
2516              constant when things are instantiated.  */
2517           if (integral_constant_expression_p)
2518             *non_integral_constant_expression_p = true;
2519           *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2520           return id_expression;
2521         }
2522
2523       /* Only certain kinds of names are allowed in constant
2524        expression.  Enumerators and template parameters 
2525        have already been handled above.  */
2526       if (integral_constant_expression_p)
2527         {
2528             /* Const variables or static data members of integral or
2529               enumeration types initialized with constant expressions
2530               are OK.  */
2531           if (TREE_CODE (decl) == VAR_DECL
2532               && CP_TYPE_CONST_P (TREE_TYPE (decl))
2533               && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2534               && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2535             ;
2536           else
2537             {
2538               if (!allow_non_integral_constant_expression_p)
2539                 {
2540                   error ("`%D' cannot appear in a constant-expression", decl);
2541                   return error_mark_node;
2542                 }
2543               *non_integral_constant_expression_p = true;
2544             }
2545         }
2546       
2547       if (TREE_CODE (decl) == NAMESPACE_DECL)
2548         {
2549           error ("use of namespace `%D' as expression", decl);
2550           return error_mark_node;
2551         }
2552       else if (DECL_CLASS_TEMPLATE_P (decl))
2553         {
2554           error ("use of class template `%T' as expression", decl);
2555           return error_mark_node;
2556         }
2557       else if (TREE_CODE (decl) == TREE_LIST)
2558         {
2559           /* Ambiguous reference to base members.  */
2560           error ("request for member `%D' is ambiguous in "
2561                  "multiple inheritance lattice", id_expression);
2562           print_candidates (decl);
2563           return error_mark_node;
2564         }
2565
2566       /* Mark variable-like entities as used.  Functions are similarly
2567          marked either below or after overload resolution.  */
2568       if (TREE_CODE (decl) == VAR_DECL
2569           || TREE_CODE (decl) == PARM_DECL
2570           || TREE_CODE (decl) == RESULT_DECL)
2571         mark_used (decl);
2572
2573       if (scope)
2574         {
2575           decl = (adjust_result_of_qualified_name_lookup 
2576                   (decl, scope, current_class_type));
2577
2578           if (TREE_CODE (decl) == FUNCTION_DECL)
2579             mark_used (decl);
2580
2581           if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2582             *qualifying_class = scope;
2583           else if (!processing_template_decl)
2584             decl = convert_from_reference (decl);
2585           else if (TYPE_P (scope))
2586             decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2587         }
2588       else if (TREE_CODE (decl) == FIELD_DECL)
2589         decl = finish_non_static_data_member (decl, current_class_ref,
2590                                               /*qualifying_scope=*/NULL_TREE);
2591       else if (is_overloaded_fn (decl))
2592         {
2593           tree first_fn = OVL_CURRENT (decl);
2594
2595           if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2596             first_fn = DECL_TEMPLATE_RESULT (first_fn);
2597
2598           if (!really_overloaded_fn (decl))
2599             mark_used (first_fn);
2600
2601           if (TREE_CODE (first_fn) == FUNCTION_DECL
2602               && DECL_FUNCTION_MEMBER_P (first_fn))
2603             {
2604               /* A set of member functions.  */
2605               decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2606               return finish_class_member_access_expr (decl, id_expression);
2607             }
2608         }
2609       else
2610         {
2611           if (TREE_CODE (decl) == VAR_DECL
2612               || TREE_CODE (decl) == PARM_DECL
2613               || TREE_CODE (decl) == RESULT_DECL)
2614             {
2615               tree context = decl_function_context (decl);
2616               
2617               if (context != NULL_TREE && context != current_function_decl
2618                   && ! TREE_STATIC (decl))
2619                 {
2620                   error ("use of %s from containing function",
2621                          (TREE_CODE (decl) == VAR_DECL
2622                           ? "`auto' variable" : "parameter"));
2623                   cp_error_at ("  `%#D' declared here", decl);
2624                   return error_mark_node;
2625                 }
2626             }
2627           
2628           if (DECL_P (decl) && DECL_NONLOCAL (decl)
2629               && DECL_CLASS_SCOPE_P (decl)
2630               && DECL_CONTEXT (decl) != current_class_type)
2631             {
2632               tree path;
2633               
2634               path = currently_open_derived_class (DECL_CONTEXT (decl));
2635               perform_or_defer_access_check (TYPE_BINFO (path), decl);
2636             }
2637           
2638           if (! processing_template_decl)
2639             decl = convert_from_reference (decl);
2640         }
2641       
2642       /* Resolve references to variables of anonymous unions
2643          into COMPONENT_REFs.  */
2644       if (TREE_CODE (decl) == ALIAS_DECL)
2645         decl = DECL_INITIAL (decl);
2646     }
2647
2648   if (TREE_DEPRECATED (decl))
2649     warn_deprecated_use (decl);
2650
2651   return decl;
2652 }
2653
2654 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2655    use as a type-specifier.  */
2656
2657 tree
2658 finish_typeof (tree expr)
2659 {
2660   tree type;
2661
2662   if (type_dependent_expression_p (expr))
2663     {
2664       type = make_aggr_type (TYPEOF_TYPE);
2665       TYPE_FIELDS (type) = expr;
2666
2667       return type;
2668     }
2669
2670   type = TREE_TYPE (expr);
2671
2672   if (!type || type == unknown_type_node)
2673     {
2674       error ("type of `%E' is unknown", expr);
2675       return error_mark_node;
2676     }
2677
2678   return type;
2679 }
2680
2681 /* Generate RTL for the statement T, and its substatements, and any
2682    other statements at its nesting level.  */
2683
2684 static void
2685 cp_expand_stmt (tree t)
2686 {
2687   switch (TREE_CODE (t))
2688     {
2689     case TRY_BLOCK:
2690       genrtl_try_block (t);
2691       break;
2692
2693     case EH_SPEC_BLOCK:
2694       genrtl_eh_spec_block (t);
2695       break;
2696
2697     case HANDLER:
2698       genrtl_handler (t);
2699       break;
2700
2701     case USING_STMT:
2702       break;
2703     
2704     default:
2705       abort ();
2706       break;
2707     }
2708 }
2709
2710 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2711    will equivalent CALL_EXPRs.  */
2712
2713 static tree
2714 simplify_aggr_init_exprs_r (tree* tp, 
2715                             int* walk_subtrees,
2716                             void* data ATTRIBUTE_UNUSED)
2717 {
2718   /* We don't need to walk into types; there's nothing in a type that
2719      needs simplification.  (And, furthermore, there are places we
2720      actively don't want to go.  For example, we don't want to wander
2721      into the default arguments for a FUNCTION_DECL that appears in a
2722      CALL_EXPR.)  */
2723   if (TYPE_P (*tp))
2724     {
2725       *walk_subtrees = 0;
2726       return NULL_TREE;
2727     }
2728   /* Only AGGR_INIT_EXPRs are interesting.  */
2729   else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2730     return NULL_TREE;
2731
2732   simplify_aggr_init_expr (tp);
2733
2734   /* Keep iterating.  */
2735   return NULL_TREE;
2736 }
2737
2738 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR.  This
2739    function is broken out from the above for the benefit of the tree-ssa
2740    project.  */
2741
2742 void
2743 simplify_aggr_init_expr (tree *tp)
2744 {
2745   tree aggr_init_expr = *tp;
2746
2747   /* Form an appropriate CALL_EXPR.  */
2748   tree fn = TREE_OPERAND (aggr_init_expr, 0);
2749   tree args = TREE_OPERAND (aggr_init_expr, 1);
2750   tree slot = TREE_OPERAND (aggr_init_expr, 2);
2751   tree type = TREE_TYPE (aggr_init_expr);
2752
2753   tree call_expr;
2754   enum style_t { ctor, arg, pcc } style;
2755
2756   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2757     style = ctor;
2758 #ifdef PCC_STATIC_STRUCT_RETURN
2759   else if (1)
2760     style = pcc;
2761 #endif
2762   else if (TREE_ADDRESSABLE (type))
2763     style = arg;
2764   else
2765     /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2766        handling.  See build_cplus_new.  */
2767     abort ();
2768
2769   if (style == ctor || style == arg)
2770     {
2771       /* Pass the address of the slot.  If this is a constructor, we
2772          replace the first argument; otherwise, we tack on a new one.  */
2773       tree addr;
2774
2775       if (style == ctor)
2776         args = TREE_CHAIN (args);
2777
2778       cxx_mark_addressable (slot);
2779       addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (slot)), slot);
2780       if (style == arg)
2781         {
2782           /* The return type might have different cv-quals from the slot.  */
2783           tree fntype = TREE_TYPE (TREE_TYPE (fn));
2784 #ifdef ENABLE_CHECKING
2785           if (TREE_CODE (fntype) != FUNCTION_TYPE
2786               && TREE_CODE (fntype) != METHOD_TYPE)
2787             abort ();
2788 #endif
2789           addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2790         }
2791
2792       args = tree_cons (NULL_TREE, addr, args);
2793     }
2794
2795   call_expr = build (CALL_EXPR, 
2796                      TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2797                      fn, args, NULL_TREE);
2798
2799   if (style == arg)
2800     /* Tell the backend that we've added our return slot to the argument
2801        list.  */
2802     CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2803   else if (style == pcc)
2804     {
2805       /* If we're using the non-reentrant PCC calling convention, then we
2806          need to copy the returned value out of the static buffer into the
2807          SLOT.  */
2808       push_deferring_access_checks (dk_no_check);
2809       call_expr = build_aggr_init (slot, call_expr,
2810                                    DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2811       pop_deferring_access_checks ();
2812     }
2813
2814   /* We want to use the value of the initialized location as the
2815      result.  */
2816   call_expr = build (COMPOUND_EXPR, type,
2817                      call_expr, slot);
2818
2819   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2820   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2821   *tp = call_expr;
2822 }
2823
2824 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
2825
2826 static void
2827 emit_associated_thunks (tree fn)
2828 {
2829   /* When we use vcall offsets, we emit thunks with the virtual
2830      functions to which they thunk. The whole point of vcall offsets
2831      is so that you can know statically the entire set of thunks that
2832      will ever be needed for a given virtual function, thereby
2833      enabling you to output all the thunks with the function itself.  */
2834   if (DECL_VIRTUAL_P (fn))
2835     {
2836       tree thunk;
2837       
2838       for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2839         {
2840           if (!THUNK_ALIAS (thunk))
2841             {
2842               use_thunk (thunk, /*emit_p=*/1);
2843               if (DECL_RESULT_THUNK_P (thunk))
2844                 {
2845                   tree probe;
2846                   
2847                   for (probe = DECL_THUNKS (thunk);
2848                        probe; probe = TREE_CHAIN (probe))
2849                     use_thunk (probe, /*emit_p=*/1);
2850                 }
2851             }
2852           else
2853             my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2854         }
2855     }
2856 }
2857
2858 /* Generate RTL for FN.  */
2859
2860 void
2861 expand_body (tree fn)
2862 {
2863   tree saved_function;
2864   
2865   /* Compute the appropriate object-file linkage for inline
2866      functions.  */
2867   if (DECL_DECLARED_INLINE_P (fn))
2868     import_export_decl (fn);
2869
2870   /* If FN is external, then there's no point in generating RTL for
2871      it.  This situation can arise with an inline function under
2872      `-fexternal-templates'; we instantiate the function, even though
2873      we're not planning on emitting it, in case we get a chance to
2874      inline it.  */
2875   if (DECL_EXTERNAL (fn))
2876     return;
2877
2878   /* ??? When is this needed?  */
2879   saved_function = current_function_decl;
2880
2881   timevar_push (TV_INTEGRATION);
2882   optimize_function (fn);
2883   timevar_pop (TV_INTEGRATION);
2884
2885   tree_rest_of_compilation (fn, function_depth > 1);
2886
2887   current_function_decl = saved_function;
2888
2889   extract_interface_info ();
2890
2891   /* Emit any thunks that should be emitted at the same time as FN.  */
2892   emit_associated_thunks (fn);
2893
2894   /* If this function is marked with the constructor attribute, add it
2895      to the list of functions to be called along with constructors
2896      from static duration objects.  */
2897   if (DECL_STATIC_CONSTRUCTOR (fn))
2898     static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2899
2900   /* If this function is marked with the destructor attribute, add it
2901      to the list of functions to be called along with destructors from
2902      static duration objects.  */
2903   if (DECL_STATIC_DESTRUCTOR (fn))
2904     static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2905
2906   if (DECL_CLONED_FUNCTION_P (fn))
2907     {
2908       /* If this is a clone, go through the other clones now and mark
2909          their parameters used.  We have to do that here, as we don't
2910          know whether any particular clone will be expanded, and
2911          therefore cannot pick one arbitrarily.  */ 
2912       tree probe;
2913
2914       for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2915            probe && DECL_CLONED_FUNCTION_P (probe);
2916            probe = TREE_CHAIN (probe))
2917         {
2918           tree parms;
2919
2920           for (parms = DECL_ARGUMENTS (probe);
2921                parms; parms = TREE_CHAIN (parms))
2922             TREE_USED (parms) = 1;
2923         }
2924     }
2925 }
2926
2927 /* Generate RTL for FN.  */
2928
2929 void
2930 expand_or_defer_fn (tree fn)
2931 {
2932   /* When the parser calls us after finishing the body of a template
2933      function, we don't really want to expand the body.  When we're
2934      processing an in-class definition of an inline function,
2935      PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2936      to look at the function itself.  */
2937   if (processing_template_decl
2938       || (DECL_LANG_SPECIFIC (fn) 
2939           && DECL_TEMPLATE_INFO (fn)
2940           && uses_template_parms (DECL_TI_ARGS (fn))))
2941     {
2942       /* Normally, collection only occurs in rest_of_compilation.  So,
2943          if we don't collect here, we never collect junk generated
2944          during the processing of templates until we hit a
2945          non-template function.  */
2946       ggc_collect ();
2947       return;
2948     }
2949
2950   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2951   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2952                                 simplify_aggr_init_exprs_r,
2953                                 NULL);
2954
2955   /* If this is a constructor or destructor body, we have to clone
2956      it.  */
2957   if (maybe_clone_body (fn))
2958     {
2959       /* We don't want to process FN again, so pretend we've written
2960          it out, even though we haven't.  */
2961       TREE_ASM_WRITTEN (fn) = 1;
2962       return;
2963     }
2964
2965   /* There's no reason to do any of the work here if we're only doing
2966      semantic analysis; this code just generates RTL.  */
2967   if (flag_syntax_only)
2968     return;
2969
2970   /* Compute the appropriate object-file linkage for inline functions.  */
2971   if (DECL_DECLARED_INLINE_P (fn))
2972     import_export_decl (fn);
2973
2974   function_depth++;
2975
2976   /* Expand or defer, at the whim of the compilation unit manager.  */
2977   cgraph_finalize_function (fn, function_depth > 1);
2978
2979   function_depth--;
2980 }
2981
2982 /* Helper function for walk_tree, used by finish_function to override all
2983    the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2984    value optimization.  */
2985
2986 tree
2987 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
2988 {
2989   tree nrv = (tree) data;
2990
2991   /* No need to walk into types.  There wouldn't be any need to walk into
2992      non-statements, except that we have to consider STMT_EXPRs.  */
2993   if (TYPE_P (*tp))
2994     *walk_subtrees = 0;
2995   else if (TREE_CODE (*tp) == RETURN_STMT)
2996     RETURN_STMT_EXPR (*tp) = NULL_TREE;
2997   else if (TREE_CODE (*tp) == CLEANUP_STMT
2998            && CLEANUP_DECL (*tp) == nrv)
2999     CLEANUP_EH_ONLY (*tp) = 1;
3000
3001   /* Keep iterating.  */
3002   return NULL_TREE;
3003 }
3004
3005 /* Start generating the RTL for FN.  */
3006
3007 void
3008 cxx_expand_function_start (void)
3009 {
3010   /* Give our named return value the same RTL as our RESULT_DECL.  */
3011   if (current_function_return_value)
3012     COPY_DECL_RTL (DECL_RESULT (cfun->decl), current_function_return_value);
3013 }
3014
3015 /* Perform initialization related to this module.  */
3016
3017 void
3018 init_cp_semantics (void)
3019 {
3020   lang_expand_stmt = cp_expand_stmt;
3021 }
3022
3023 #include "gt-cp-semantics.h"