- Update GCC to version 3.4.3.
[dragonfly.git] / contrib / gcc-3.4 / 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   /* If EXPR is an overloaded function, issue an error; there is no
682      context available to use to perform overload resolution.  */
683   if (expr && type_unknown_p (expr))
684     {
685       cxx_incomplete_type_error (expr, TREE_TYPE (expr));
686       expr = error_mark_node;
687     }
688   FOR_EXPR (for_stmt) = expr;
689 }
690
691 /* Finish the body of a for-statement, which may be given by
692    FOR_STMT.  The increment-EXPR for the loop must be
693    provided.  */
694
695 void
696 finish_for_stmt (tree for_stmt)
697 {
698   /* Pop the scope for the body of the loop.  */
699   do_poplevel ();
700   RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
701   if (NEW_FOR_SCOPE_P (for_stmt))
702     do_poplevel ();
703   finish_stmt (); 
704 }
705
706 /* Finish a break-statement.  */
707
708 tree
709 finish_break_stmt (void)
710 {
711   return add_stmt (build_break_stmt ());
712 }
713
714 /* Finish a continue-statement.  */
715
716 tree
717 finish_continue_stmt (void)
718 {
719   return add_stmt (build_continue_stmt ());
720 }
721
722 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
723    appropriate.  */
724
725 tree
726 begin_switch_stmt (void)
727 {
728   tree r;
729   do_pushlevel (sk_block);
730   r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
731   add_stmt (r);
732   return r;
733 }
734
735 /* Finish the cond of a switch-statement.  */
736
737 void
738 finish_switch_cond (tree cond, tree switch_stmt)
739 {
740   tree orig_type = NULL;
741   if (!processing_template_decl)
742     {
743       tree index;
744
745       /* Convert the condition to an integer or enumeration type.  */
746       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
747       if (cond == NULL_TREE)
748         {
749           error ("switch quantity not an integer");
750           cond = error_mark_node;
751         }
752       orig_type = TREE_TYPE (cond);
753       if (cond != error_mark_node)
754         {
755           /* [stmt.switch]
756
757              Integral promotions are performed.  */
758           cond = perform_integral_promotions (cond);
759           cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
760         }
761
762       if (cond != error_mark_node)
763         {
764           index = get_unwidened (cond, NULL_TREE);
765           /* We can't strip a conversion from a signed type to an unsigned,
766              because if we did, int_fits_type_p would do the wrong thing
767              when checking case values for being in range,
768              and it's too hard to do the right thing.  */
769           if (TREE_UNSIGNED (TREE_TYPE (cond))
770               == TREE_UNSIGNED (TREE_TYPE (index)))
771             cond = index;
772         }
773     }
774   FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
775   SWITCH_TYPE (switch_stmt) = orig_type;
776   push_switch (switch_stmt);
777 }
778
779 /* Finish the body of a switch-statement, which may be given by
780    SWITCH_STMT.  The COND to switch on is indicated.  */
781
782 void
783 finish_switch_stmt (tree switch_stmt)
784 {
785   RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
786   pop_switch (); 
787   finish_stmt ();
788   do_poplevel ();
789 }
790
791 /* Generate the RTL for T, which is a TRY_BLOCK.  */
792
793 static void 
794 genrtl_try_block (tree t)
795 {
796   if (CLEANUP_P (t))
797     {
798       expand_eh_region_start ();
799       expand_stmt (TRY_STMTS (t));
800       expand_eh_region_end_cleanup (TRY_HANDLERS (t));
801     }
802   else
803     {
804       if (!FN_TRY_BLOCK_P (t)) 
805         emit_line_note (input_location);
806
807       expand_eh_region_start ();
808       expand_stmt (TRY_STMTS (t));
809
810       if (FN_TRY_BLOCK_P (t))
811         {
812           expand_start_all_catch ();
813           in_function_try_handler = 1;
814           expand_stmt (TRY_HANDLERS (t));
815           in_function_try_handler = 0;
816           expand_end_all_catch ();
817         }
818       else 
819         {
820           expand_start_all_catch ();  
821           expand_stmt (TRY_HANDLERS (t));
822           expand_end_all_catch ();
823         }
824     }
825 }
826
827 /* Generate the RTL for T, which is an EH_SPEC_BLOCK.  */
828
829 static void 
830 genrtl_eh_spec_block (tree t)
831 {
832   expand_eh_region_start ();
833   expand_stmt (EH_SPEC_STMTS (t));
834   expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
835                                 build_call (call_unexpected_node,
836                                             tree_cons (NULL_TREE,
837                                                        build_exc_ptr (),
838                                                        NULL_TREE)));
839 }
840
841 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
842    appropriate.  */
843
844 tree
845 begin_try_block (void)
846 {
847   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
848   add_stmt (r);
849   return r;
850 }
851
852 /* Likewise, for a function-try-block.  */
853
854 tree
855 begin_function_try_block (void)
856 {
857   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
858   FN_TRY_BLOCK_P (r) = 1;
859   add_stmt (r);
860   return r;
861 }
862
863 /* Finish a try-block, which may be given by TRY_BLOCK.  */
864
865 void
866 finish_try_block (tree try_block)
867 {
868   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
869 }
870
871 /* Finish the body of a cleanup try-block, which may be given by
872    TRY_BLOCK.  */
873
874 void
875 finish_cleanup_try_block (tree try_block)
876 {
877   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
878 }
879
880 /* Finish an implicitly generated try-block, with a cleanup is given
881    by CLEANUP.  */
882
883 void
884 finish_cleanup (tree cleanup, tree try_block)
885 {
886   TRY_HANDLERS (try_block) = cleanup;
887   CLEANUP_P (try_block) = 1;
888 }
889
890 /* Likewise, for a function-try-block.  */
891
892 void
893 finish_function_try_block (tree try_block)
894 {
895   if (TREE_CHAIN (try_block) 
896       && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
897     {
898       /* Chain the compound statement after the CTOR_INITIALIZER.  */
899       TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
900       /* And make the CTOR_INITIALIZER the body of the try-block.  */
901       RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
902     }
903   else
904     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
905   in_function_try_handler = 1;
906 }
907
908 /* Finish a handler-sequence for a try-block, which may be given by
909    TRY_BLOCK.  */
910
911 void
912 finish_handler_sequence (tree try_block)
913 {
914   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
915   check_handlers (TRY_HANDLERS (try_block));
916 }
917
918 /* Likewise, for a function-try-block.  */
919
920 void
921 finish_function_handler_sequence (tree try_block)
922 {
923   in_function_try_handler = 0;
924   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
925   check_handlers (TRY_HANDLERS (try_block));
926 }
927
928 /* Generate the RTL for T, which is a HANDLER.  */
929
930 static void
931 genrtl_handler (tree t)
932 {
933   genrtl_do_pushlevel ();
934   if (!processing_template_decl)
935     expand_start_catch (HANDLER_TYPE (t));
936   expand_stmt (HANDLER_BODY (t));
937   if (!processing_template_decl)
938     expand_end_catch ();
939 }
940
941 /* Begin a handler.  Returns a HANDLER if appropriate.  */
942
943 tree
944 begin_handler (void)
945 {
946   tree r;
947   r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
948   add_stmt (r);
949   /* Create a binding level for the eh_info and the exception object
950      cleanup.  */
951   do_pushlevel (sk_catch);
952   return r;
953 }
954
955 /* Finish the handler-parameters for a handler, which may be given by
956    HANDLER.  DECL is the declaration for the catch parameter, or NULL
957    if this is a `catch (...)' clause.  */
958
959 void
960 finish_handler_parms (tree decl, tree handler)
961 {
962   tree type = NULL_TREE;
963   if (processing_template_decl)
964     {
965       if (decl)
966         {
967           decl = pushdecl (decl);
968           decl = push_template_decl (decl);
969           add_decl_stmt (decl);
970           RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
971           type = TREE_TYPE (decl);
972         }
973     }
974   else
975     type = expand_start_catch_block (decl);
976
977   HANDLER_TYPE (handler) = type;
978   if (!processing_template_decl && type)
979     mark_used (eh_type_info (type));
980 }
981
982 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
983    the return value from the matching call to finish_handler_parms.  */
984
985 void
986 finish_handler (tree handler)
987 {
988   if (!processing_template_decl)
989     expand_end_catch_block ();
990   do_poplevel ();
991   RECHAIN_STMTS (handler, HANDLER_BODY (handler));
992 }
993
994 /* Begin a compound-statement.  If HAS_NO_SCOPE is true, the
995    compound-statement does not define a scope.  Returns a new
996    COMPOUND_STMT.  */
997
998 tree
999 begin_compound_stmt (bool has_no_scope)
1000 {
1001   tree r; 
1002   int is_try = 0;
1003
1004   r = build_stmt (COMPOUND_STMT, NULL_TREE);
1005
1006   if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
1007     is_try = 1;
1008
1009   add_stmt (r);
1010   if (has_no_scope)
1011     COMPOUND_STMT_NO_SCOPE (r) = 1;
1012
1013   last_expr_type = NULL_TREE;
1014
1015   if (!has_no_scope)
1016     do_pushlevel (is_try ? sk_try : sk_block);
1017   else
1018     /* Normally, we try hard to keep the BLOCK for a
1019        statement-expression.  But, if it's a statement-expression with
1020        a scopeless block, there's nothing to keep, and we don't want
1021        to accidentally keep a block *inside* the scopeless block.  */ 
1022     keep_next_level (false);
1023
1024   return r;
1025 }
1026
1027 /* Finish a compound-statement, which is given by COMPOUND_STMT.  */
1028
1029 tree
1030 finish_compound_stmt (tree compound_stmt)
1031 {
1032   tree r;
1033   tree t;
1034
1035   if (COMPOUND_STMT_NO_SCOPE (compound_stmt))
1036     r = NULL_TREE;
1037   else
1038     r = do_poplevel ();
1039
1040   RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1041
1042   /* When we call finish_stmt we will lose LAST_EXPR_TYPE.  But, since
1043      the precise purpose of that variable is store the type of the
1044      last expression statement within the last compound statement, we
1045      preserve the value.  */
1046   t = last_expr_type;
1047   finish_stmt ();
1048   last_expr_type = t;
1049
1050   return r;
1051 }
1052
1053 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1054    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1055    CLOBBERS.  */
1056
1057 tree
1058 finish_asm_stmt (tree cv_qualifier, 
1059                  tree string, 
1060                  tree output_operands,
1061                  tree input_operands, 
1062                  tree clobbers)
1063 {
1064   tree r;
1065   tree t;
1066
1067   if (cv_qualifier != NULL_TREE
1068       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1069     {
1070       warning ("%s qualifier ignored on asm",
1071                   IDENTIFIER_POINTER (cv_qualifier));
1072       cv_qualifier = NULL_TREE;
1073     }
1074
1075   if (!processing_template_decl)
1076     {
1077       int i;
1078       int ninputs;
1079       int noutputs;
1080
1081       for (t = input_operands; t; t = TREE_CHAIN (t))
1082         {
1083           tree converted_operand 
1084             = decay_conversion (TREE_VALUE (t)); 
1085           
1086           /* If the type of the operand hasn't been determined (e.g.,
1087              because it involves an overloaded function), then issue
1088              an error message.  There's no context available to
1089              resolve the overloading.  */
1090           if (TREE_TYPE (converted_operand) == unknown_type_node)
1091             {
1092               error ("type of asm operand `%E' could not be determined", 
1093                         TREE_VALUE (t));
1094               converted_operand = error_mark_node;
1095             }
1096           TREE_VALUE (t) = converted_operand;
1097         }
1098
1099       ninputs = list_length (input_operands);
1100       noutputs = list_length (output_operands);
1101
1102       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1103         {
1104           bool allows_mem;
1105           bool allows_reg;
1106           bool is_inout;
1107           const char *constraint;
1108           tree operand;
1109
1110           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1111           operand = TREE_VALUE (t);
1112
1113           if (!parse_output_constraint (&constraint,
1114                                         i, ninputs, noutputs,
1115                                         &allows_mem,
1116                                         &allows_reg,
1117                                         &is_inout))
1118             {
1119               /* By marking this operand as erroneous, we will not try
1120                  to process this operand again in expand_asm_operands.  */
1121               TREE_VALUE (t) = error_mark_node;
1122               continue;
1123             }
1124
1125           /* If the operand is a DECL that is going to end up in
1126              memory, assume it is addressable.  This is a bit more
1127              conservative than it would ideally be; the exact test is
1128              buried deep in expand_asm_operands and depends on the
1129              DECL_RTL for the OPERAND -- which we don't have at this
1130              point.  */
1131           if (!allows_reg && DECL_P (operand))
1132             cxx_mark_addressable (operand);
1133         }
1134     }
1135
1136   r = build_stmt (ASM_STMT, cv_qualifier, string,
1137                   output_operands, input_operands,
1138                   clobbers);
1139   return add_stmt (r);
1140 }
1141
1142 /* Finish a label with the indicated NAME.  */
1143
1144 tree
1145 finish_label_stmt (tree name)
1146 {
1147   tree decl = define_label (input_location, name);
1148   return add_stmt (build_stmt (LABEL_STMT, decl));
1149 }
1150
1151 /* Finish a series of declarations for local labels.  G++ allows users
1152    to declare "local" labels, i.e., labels with scope.  This extension
1153    is useful when writing code involving statement-expressions.  */
1154
1155 void
1156 finish_label_decl (tree name)
1157 {
1158   tree decl = declare_local_label (name);
1159   add_decl_stmt (decl);
1160 }
1161
1162 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1163
1164 void 
1165 finish_decl_cleanup (tree decl, tree cleanup)
1166 {
1167   add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1168 }
1169
1170 /* If the current scope exits with an exception, run CLEANUP.  */
1171
1172 void
1173 finish_eh_cleanup (tree cleanup)
1174 {
1175   tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1176   CLEANUP_EH_ONLY (r) = 1;
1177   add_stmt (r);
1178 }
1179
1180 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1181    order they were written by the user.  Each node is as for
1182    emit_mem_initializers.  */
1183
1184 void
1185 finish_mem_initializers (tree mem_inits)
1186 {
1187   /* Reorder the MEM_INITS so that they are in the order they appeared
1188      in the source program.  */
1189   mem_inits = nreverse (mem_inits);
1190
1191   if (processing_template_decl)
1192     add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1193   else
1194     emit_mem_initializers (mem_inits);
1195 }
1196
1197 /* Returns the stack of SCOPE_STMTs for the current function.  */
1198
1199 tree *
1200 current_scope_stmt_stack (void)
1201 {
1202   return &cfun->language->base.x_scope_stmt_stack;
1203 }
1204
1205 /* Finish a parenthesized expression EXPR.  */
1206
1207 tree
1208 finish_parenthesized_expr (tree expr)
1209 {
1210   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1211     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1212     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1213
1214   if (TREE_CODE (expr) == OFFSET_REF)
1215     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1216        enclosed in parentheses.  */
1217     PTRMEM_OK_P (expr) = 0;
1218   return expr;
1219 }
1220
1221 /* Finish a reference to a non-static data member (DECL) that is not
1222    preceded by `.' or `->'.  */
1223
1224 tree
1225 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1226 {
1227   my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1228
1229   if (!object)
1230     {
1231       if (current_function_decl 
1232           && DECL_STATIC_FUNCTION_P (current_function_decl))
1233         cp_error_at ("invalid use of member `%D' in static member function",
1234                      decl);
1235       else
1236         cp_error_at ("invalid use of non-static data member `%D'", decl);
1237       error ("from this location");
1238
1239       return error_mark_node;
1240     }
1241   TREE_USED (current_class_ptr) = 1;
1242   if (processing_template_decl && !qualifying_scope)
1243     {
1244       tree type = TREE_TYPE (decl);
1245
1246       if (TREE_CODE (type) == REFERENCE_TYPE)
1247         type = TREE_TYPE (type);
1248       else
1249         {
1250           /* Set the cv qualifiers.  */
1251           int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1252           
1253           if (DECL_MUTABLE_P (decl))
1254             quals &= ~TYPE_QUAL_CONST;
1255
1256           quals |= cp_type_quals (TREE_TYPE (decl));
1257           type = cp_build_qualified_type (type, quals);
1258         }
1259       
1260       return build_min (COMPONENT_REF, type, object, decl);
1261     }
1262   else
1263     {
1264       tree access_type = TREE_TYPE (object);
1265       tree lookup_context = context_for_name_lookup (decl);
1266       
1267       while (!DERIVED_FROM_P (lookup_context, access_type))
1268         {
1269           access_type = TYPE_CONTEXT (access_type);
1270           while (access_type && DECL_P (access_type))
1271             access_type = DECL_CONTEXT (access_type);
1272
1273           if (!access_type)
1274             {
1275               cp_error_at ("object missing in reference to `%D'", decl);
1276               error ("from this location");
1277               return error_mark_node;
1278             }
1279         }
1280
1281       /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1282          QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
1283          for now.  */
1284       if (processing_template_decl)
1285         return build_min (SCOPE_REF, TREE_TYPE (decl),
1286                           qualifying_scope, DECL_NAME (decl));
1287
1288       perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1289
1290       /* If the data member was named `C::M', convert `*this' to `C'
1291          first.  */
1292       if (qualifying_scope)
1293         {
1294           tree binfo = NULL_TREE;
1295           object = build_scoped_ref (object, qualifying_scope,
1296                                      &binfo);
1297         }
1298
1299       return build_class_member_access_expr (object, decl,
1300                                              /*access_path=*/NULL_TREE,
1301                                              /*preserve_reference=*/false);
1302     }
1303 }
1304
1305 /* DECL was the declaration to which a qualified-id resolved.  Issue
1306    an error message if it is not accessible.  If OBJECT_TYPE is
1307    non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1308    type of `*x', or `x', respectively.  If the DECL was named as
1309    `A::B' then NESTED_NAME_SPECIFIER is `A'.  */
1310
1311 void
1312 check_accessibility_of_qualified_id (tree decl, 
1313                                      tree object_type, 
1314                                      tree nested_name_specifier)
1315 {
1316   tree scope;
1317   tree qualifying_type = NULL_TREE;
1318   
1319   /* Determine the SCOPE of DECL.  */
1320   scope = context_for_name_lookup (decl);
1321   /* If the SCOPE is not a type, then DECL is not a member.  */
1322   if (!TYPE_P (scope))
1323     return;
1324   /* Compute the scope through which DECL is being accessed.  */
1325   if (object_type 
1326       /* OBJECT_TYPE might not be a class type; consider:
1327
1328            class A { typedef int I; };
1329            I *p;
1330            p->A::I::~I();
1331
1332          In this case, we will have "A::I" as the DECL, but "I" as the
1333          OBJECT_TYPE.  */
1334       && CLASS_TYPE_P (object_type)
1335       && DERIVED_FROM_P (scope, object_type))
1336     /* If we are processing a `->' or `.' expression, use the type of the
1337        left-hand side.  */
1338     qualifying_type = object_type;
1339   else if (nested_name_specifier)
1340     {
1341       /* If the reference is to a non-static member of the
1342          current class, treat it as if it were referenced through
1343          `this'.  */
1344       if (DECL_NONSTATIC_MEMBER_P (decl)
1345           && current_class_ptr
1346           && DERIVED_FROM_P (scope, current_class_type))
1347         qualifying_type = current_class_type;
1348       /* Otherwise, use the type indicated by the
1349          nested-name-specifier.  */
1350       else
1351         qualifying_type = nested_name_specifier;
1352     }
1353   else
1354     /* Otherwise, the name must be from the current class or one of
1355        its bases.  */
1356     qualifying_type = currently_open_derived_class (scope);
1357
1358   if (qualifying_type)
1359     perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1360 }
1361
1362 /* EXPR is the result of a qualified-id.  The QUALIFYING_CLASS was the
1363    class named to the left of the "::" operator.  DONE is true if this
1364    expression is a complete postfix-expression; it is false if this
1365    expression is followed by '->', '[', '(', etc.  ADDRESS_P is true
1366    iff this expression is the operand of '&'.  */
1367
1368 tree
1369 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1370                           bool address_p)
1371 {
1372   if (error_operand_p (expr))
1373     return error_mark_node;
1374
1375   /* If EXPR occurs as the operand of '&', use special handling that
1376      permits a pointer-to-member.  */
1377   if (address_p && done)
1378     {
1379       if (TREE_CODE (expr) == SCOPE_REF)
1380         expr = TREE_OPERAND (expr, 1);
1381       expr = build_offset_ref (qualifying_class, expr, 
1382                                /*address_p=*/true);
1383       return expr;
1384     }
1385
1386   if (TREE_CODE (expr) == FIELD_DECL)
1387     expr = finish_non_static_data_member (expr, current_class_ref,
1388                                           qualifying_class);
1389   else if (BASELINK_P (expr) && !processing_template_decl)
1390     {
1391       tree fns;
1392
1393       /* See if any of the functions are non-static members.  */
1394       fns = BASELINK_FUNCTIONS (expr);
1395       if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1396         fns = TREE_OPERAND (fns, 0);
1397       /* If so, the expression may be relative to the current
1398          class.  */
1399       if (!shared_member_p (fns)
1400           && current_class_type 
1401           && DERIVED_FROM_P (qualifying_class, current_class_type))
1402         expr = (build_class_member_access_expr 
1403                 (maybe_dummy_object (qualifying_class, NULL),
1404                  expr,
1405                  BASELINK_ACCESS_BINFO (expr),
1406                  /*preserve_reference=*/false));
1407       else if (done)
1408         /* The expression is a qualified name whose address is not
1409            being taken.  */
1410         expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1411     }
1412
1413   return expr;
1414 }
1415
1416 /* Begin a statement-expression.  The value returned must be passed to
1417    finish_stmt_expr.  */
1418
1419 tree 
1420 begin_stmt_expr (void)
1421 {
1422   /* If we're outside a function, we won't have a statement-tree to
1423      work with.  But, if we see a statement-expression we need to
1424      create one.  */
1425   if (! cfun && !last_tree)
1426     begin_stmt_tree (&scope_chain->x_saved_tree);
1427
1428   last_expr_type = NULL_TREE;
1429   
1430   keep_next_level (true);
1431
1432   return last_tree; 
1433 }
1434
1435 /* Process the final expression of a statement expression. EXPR can be
1436    NULL, if the final expression is empty.  Build up a TARGET_EXPR so
1437    that the result value can be safely returned to the enclosing
1438    expression.  */
1439
1440 tree
1441 finish_stmt_expr_expr (tree expr)
1442 {
1443   tree result = NULL_TREE;
1444   tree type = void_type_node;
1445
1446   if (expr)
1447     {
1448       type = TREE_TYPE (expr);
1449       
1450       if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1451         {
1452           if (TREE_CODE (type) == ARRAY_TYPE
1453               || TREE_CODE (type) == FUNCTION_TYPE)
1454             expr = decay_conversion (expr);
1455
1456           expr = convert_from_reference (expr);
1457           expr = require_complete_type (expr);
1458
1459           /* Build a TARGET_EXPR for this aggregate.  finish_stmt_expr
1460              will then pull it apart so the lifetime of the target is
1461              within the scope of the expression containing this statement
1462              expression.  */
1463           if (TREE_CODE (expr) == TARGET_EXPR)
1464             ;
1465           else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1466             expr = build_target_expr_with_type (expr, type);
1467           else
1468             {
1469               /* Copy construct.  */
1470               expr = build_special_member_call
1471                 (NULL_TREE, complete_ctor_identifier,
1472                  build_tree_list (NULL_TREE, expr),
1473                  TYPE_BINFO (type), LOOKUP_NORMAL);
1474               expr = build_cplus_new (type, expr);
1475               my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1476             }
1477         }
1478
1479       if (expr != error_mark_node)
1480         {
1481           result = build_stmt (EXPR_STMT, expr);
1482           add_stmt (result);
1483         }
1484     }
1485   
1486   finish_stmt ();
1487
1488   /* Remember the last expression so that finish_stmt_expr can pull it
1489      apart.  */
1490   last_expr_type = result ? result : void_type_node;
1491   
1492   return result;
1493 }
1494
1495 /* Finish a statement-expression.  EXPR should be the value returned
1496    by the previous begin_stmt_expr.  Returns an expression
1497    representing the statement-expression.  */
1498
1499 tree 
1500 finish_stmt_expr (tree rtl_expr, bool has_no_scope)
1501 {
1502   tree result;
1503   tree result_stmt = last_expr_type;
1504   tree type;
1505   
1506   if (!last_expr_type)
1507     type = void_type_node;
1508   else
1509     {
1510       if (result_stmt == void_type_node)
1511         {
1512           type = void_type_node;
1513           result_stmt = NULL_TREE;
1514         }
1515       else
1516         type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1517     }
1518   
1519   result = build_min (STMT_EXPR, type, last_tree);
1520   TREE_SIDE_EFFECTS (result) = 1;
1521   STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1522   
1523   last_expr_type = NULL_TREE;
1524   
1525   /* Remove the compound statement from the tree structure; it is
1526      now saved in the STMT_EXPR.  */
1527   last_tree = rtl_expr;
1528   TREE_CHAIN (last_tree) = NULL_TREE;
1529
1530   /* If we created a statement-tree for this statement-expression,
1531      remove it now.  */ 
1532   if (! cfun
1533       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1534     finish_stmt_tree (&scope_chain->x_saved_tree);
1535
1536   if (processing_template_decl)
1537     return result;
1538
1539   if (!VOID_TYPE_P (type))
1540     {
1541       /* Pull out the TARGET_EXPR that is the final expression. Put
1542          the target's init_expr as the final expression and then put
1543          the statement expression itself as the target's init
1544          expr. Finally, return the target expression.  */
1545       tree last_expr = EXPR_STMT_EXPR (result_stmt);
1546       
1547       my_friendly_assert (TREE_CODE (last_expr) == TARGET_EXPR, 20030729);
1548       EXPR_STMT_EXPR (result_stmt) = TREE_OPERAND (last_expr, 1);
1549       TREE_OPERAND (last_expr, 1) = result;
1550       result = last_expr;
1551     }
1552   return result;
1553 }
1554
1555 /* Perform Koenig lookup.  FN is the postfix-expression representing
1556    the function (or functions) to call; ARGS are the arguments to the
1557    call.  Returns the functions to be considered by overload
1558    resolution.  */
1559
1560 tree
1561 perform_koenig_lookup (tree fn, tree args)
1562 {
1563   tree identifier = NULL_TREE;
1564   tree functions = NULL_TREE;
1565
1566   /* Find the name of the overloaded function.  */
1567   if (TREE_CODE (fn) == IDENTIFIER_NODE)
1568     identifier = fn;
1569   else if (is_overloaded_fn (fn))
1570     {
1571       functions = fn;
1572       identifier = DECL_NAME (get_first_fn (functions));
1573     }
1574   else if (DECL_P (fn))
1575     {
1576       functions = fn;
1577       identifier = DECL_NAME (fn);
1578     }
1579
1580   /* A call to a namespace-scope function using an unqualified name.
1581
1582      Do Koenig lookup -- unless any of the arguments are
1583      type-dependent.  */
1584   if (!any_type_dependent_arguments_p (args))
1585     {
1586       fn = lookup_arg_dependent (identifier, functions, args);
1587       if (!fn)
1588         /* The unqualified name could not be resolved.  */
1589         fn = unqualified_fn_lookup_error (identifier);
1590     }
1591   else
1592     fn = identifier;
1593
1594   return fn;
1595 }
1596
1597 /* Generate an expression for `FN (ARGS)'.
1598
1599    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1600    as a virtual call, even if FN is virtual.  (This flag is set when
1601    encountering an expression where the function name is explicitly
1602    qualified.  For example a call to `X::f' never generates a virtual
1603    call.)
1604
1605    Returns code for the call.  */
1606
1607 tree 
1608 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1609 {
1610   tree result;
1611   tree orig_fn;
1612   tree orig_args;
1613
1614   if (fn == error_mark_node || args == error_mark_node)
1615     return error_mark_node;
1616
1617   /* ARGS should be a list of arguments.  */
1618   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1619                       20020712);
1620
1621   orig_fn = fn;
1622   orig_args = args;
1623
1624   if (processing_template_decl)
1625     {
1626       if (type_dependent_expression_p (fn)
1627           || any_type_dependent_arguments_p (args))
1628         {
1629           result = build_nt (CALL_EXPR, fn, args);
1630           KOENIG_LOOKUP_P (result) = koenig_p;
1631           return result;
1632         }
1633       if (!BASELINK_P (fn)
1634           && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1635           && TREE_TYPE (fn) != unknown_type_node)
1636         fn = build_non_dependent_expr (fn);
1637       args = build_non_dependent_args (orig_args);
1638     }
1639
1640   /* A reference to a member function will appear as an overloaded
1641      function (rather than a BASELINK) if an unqualified name was used
1642      to refer to it.  */
1643   if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1644     {
1645       tree f = fn;
1646
1647       if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1648         f = TREE_OPERAND (f, 0);
1649       f = get_first_fn (f);
1650       if (DECL_FUNCTION_MEMBER_P (f))
1651         {
1652           tree type = currently_open_derived_class (DECL_CONTEXT (f));
1653           if (!type)
1654             type = DECL_CONTEXT (f);
1655           fn = build_baselink (TYPE_BINFO (type),
1656                                TYPE_BINFO (type),
1657                                fn, /*optype=*/NULL_TREE);
1658         }
1659     }
1660
1661   result = NULL_TREE;
1662   if (BASELINK_P (fn))
1663     {
1664       tree object;
1665
1666       /* A call to a member function.  From [over.call.func]:
1667
1668            If the keyword this is in scope and refers to the class of
1669            that member function, or a derived class thereof, then the
1670            function call is transformed into a qualified function call
1671            using (*this) as the postfix-expression to the left of the
1672            . operator.... [Otherwise] a contrived object of type T
1673            becomes the implied object argument.  
1674
1675         This paragraph is unclear about this situation:
1676
1677           struct A { void f(); };
1678           struct B : public A {};
1679           struct C : public A { void g() { B::f(); }};
1680
1681         In particular, for `B::f', this paragraph does not make clear
1682         whether "the class of that member function" refers to `A' or 
1683         to `B'.  We believe it refers to `B'.  */
1684       if (current_class_type 
1685           && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1686                              current_class_type)
1687           && current_class_ref)
1688         object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1689                                      NULL);
1690       else
1691         {
1692           tree representative_fn;
1693
1694           representative_fn = BASELINK_FUNCTIONS (fn);
1695           if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1696             representative_fn = TREE_OPERAND (representative_fn, 0);
1697           representative_fn = get_first_fn (representative_fn);
1698           object = build_dummy_object (DECL_CONTEXT (representative_fn));
1699         }
1700
1701       if (processing_template_decl)
1702         {
1703           if (type_dependent_expression_p (object))
1704             return build_nt (CALL_EXPR, orig_fn, orig_args);
1705           object = build_non_dependent_expr (object);
1706         }
1707
1708       result = build_new_method_call (object, fn, args, NULL_TREE,
1709                                       (disallow_virtual 
1710                                        ? LOOKUP_NONVIRTUAL : 0));
1711     }
1712   else if (is_overloaded_fn (fn))
1713     /* A call to a namespace-scope function.  */
1714     result = build_new_function_call (fn, args);
1715   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1716     {
1717       if (args)
1718         error ("arguments to destructor are not allowed");
1719       /* Mark the pseudo-destructor call as having side-effects so
1720          that we do not issue warnings about its use.  */
1721       result = build1 (NOP_EXPR,
1722                        void_type_node,
1723                        TREE_OPERAND (fn, 0));
1724       TREE_SIDE_EFFECTS (result) = 1;
1725     }
1726   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1727     /* If the "function" is really an object of class type, it might
1728        have an overloaded `operator ()'.  */
1729     result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1730                            /*overloaded_p=*/NULL);
1731   if (!result)
1732     /* A call where the function is unknown.  */
1733     result = build_function_call (fn, args);
1734
1735   if (processing_template_decl)
1736     {
1737       result = build (CALL_EXPR, TREE_TYPE (result), orig_fn, orig_args);
1738       KOENIG_LOOKUP_P (result) = koenig_p;
1739     }
1740   return result;
1741 }
1742
1743 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1744    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1745    POSTDECREMENT_EXPR.)  */
1746
1747 tree 
1748 finish_increment_expr (tree expr, enum tree_code code)
1749 {
1750   return build_x_unary_op (code, expr);  
1751 }
1752
1753 /* Finish a use of `this'.  Returns an expression for `this'.  */
1754
1755 tree 
1756 finish_this_expr (void)
1757 {
1758   tree result;
1759
1760   if (current_class_ptr)
1761     {
1762       result = current_class_ptr;
1763     }
1764   else if (current_function_decl
1765            && DECL_STATIC_FUNCTION_P (current_function_decl))
1766     {
1767       error ("`this' is unavailable for static member functions");
1768       result = error_mark_node;
1769     }
1770   else
1771     {
1772       if (current_function_decl)
1773         error ("invalid use of `this' in non-member function");
1774       else
1775         error ("invalid use of `this' at top level");
1776       result = error_mark_node;
1777     }
1778
1779   return result;
1780 }
1781
1782 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
1783    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1784    the TYPE for the type given.  If SCOPE is non-NULL, the expression
1785    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
1786
1787 tree 
1788 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1789 {
1790   if (destructor == error_mark_node)
1791     return error_mark_node;
1792
1793   my_friendly_assert (TYPE_P (destructor), 20010905);
1794
1795   if (!processing_template_decl)
1796     {
1797       if (scope == error_mark_node)
1798         {
1799           error ("invalid qualifying scope in pseudo-destructor name");
1800           return error_mark_node;
1801         }
1802       
1803       /* [expr.pseudo] says both:
1804
1805            The type designated by the pseudo-destructor-name shall be
1806            the same as the object type.
1807
1808          and:
1809
1810            The cv-unqualified versions of the object type and of the
1811            type designated by the pseudo-destructor-name shall be the
1812            same type.
1813
1814          We implement the more generous second sentence, since that is
1815          what most other compilers do.  */
1816       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), 
1817                                                       destructor))
1818         {
1819           error ("`%E' is not of type `%T'", object, destructor);
1820           return error_mark_node;
1821         }
1822     }
1823
1824   return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1825 }
1826
1827 /* Finish an expression of the form CODE EXPR.  */
1828
1829 tree
1830 finish_unary_op_expr (enum tree_code code, tree expr)
1831 {
1832   tree result = build_x_unary_op (code, expr);
1833   /* Inside a template, build_x_unary_op does not fold the
1834      expression. So check whether the result is folded before
1835      setting TREE_NEGATED_INT.  */
1836   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1837       && TREE_CODE (result) == INTEGER_CST
1838       && !TREE_UNSIGNED (TREE_TYPE (result))
1839       && INT_CST_LT (result, integer_zero_node))
1840     TREE_NEGATED_INT (result) = 1;
1841   overflow_warning (result);
1842   return result;
1843 }
1844
1845 /* Finish a compound-literal expression.  TYPE is the type to which
1846    the INITIALIZER_LIST is being cast.  */
1847
1848 tree
1849 finish_compound_literal (tree type, tree initializer_list)
1850 {
1851   tree compound_literal;
1852
1853   /* Build a CONSTRUCTOR for the INITIALIZER_LIST.  */
1854   compound_literal = build_constructor (NULL_TREE, initializer_list);
1855   /* Mark it as a compound-literal.  */
1856   TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1857   if (processing_template_decl)
1858     TREE_TYPE (compound_literal) = type;
1859   else
1860     {
1861       /* Check the initialization.  */
1862       compound_literal = digest_init (type, compound_literal, NULL);
1863       /* If the TYPE was an array type with an unknown bound, then we can
1864          figure out the dimension now.  For example, something like:
1865
1866            `(int []) { 2, 3 }'
1867
1868          implies that the array has two elements.  */
1869       if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1870         complete_array_type (type, compound_literal, 1);
1871     }
1872
1873   return compound_literal;
1874 }
1875
1876 /* Return the declaration for the function-name variable indicated by
1877    ID.  */
1878
1879 tree
1880 finish_fname (tree id)
1881 {
1882   tree decl;
1883   
1884   decl = fname_decl (C_RID_CODE (id), id);
1885   if (processing_template_decl)
1886     decl = DECL_NAME (decl);
1887   return decl;
1888 }
1889
1890 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1891    and DECLARATOR.  Returns nonzero if the function-declaration is
1892    valid.  */
1893
1894 int
1895 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1896 {
1897   if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1898     return 0;
1899
1900   /* The things we're about to see are not directly qualified by any
1901      template headers we've seen thus far.  */
1902   reset_specialization ();
1903
1904   return 1;
1905 }
1906
1907 /* Finish a translation unit.  */
1908
1909 void 
1910 finish_translation_unit (void)
1911 {
1912   /* In case there were missing closebraces,
1913      get us back to the global binding level.  */
1914   pop_everything ();
1915   while (current_namespace != global_namespace)
1916     pop_namespace ();
1917
1918   /* Do file scope __FUNCTION__ et al.  */
1919   finish_fname_decls ();
1920 }
1921
1922 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1923    Returns the parameter.  */
1924
1925 tree 
1926 finish_template_type_parm (tree aggr, tree identifier)
1927 {
1928   if (aggr != class_type_node)
1929     {
1930       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1931       aggr = class_type_node;
1932     }
1933
1934   return build_tree_list (aggr, identifier);
1935 }
1936
1937 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1938    Returns the parameter.  */
1939
1940 tree 
1941 finish_template_template_parm (tree aggr, tree identifier)
1942 {
1943   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1944   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1945   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1946   DECL_TEMPLATE_RESULT (tmpl) = decl;
1947   DECL_ARTIFICIAL (decl) = 1;
1948   end_template_decl ();
1949
1950   my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1951
1952   return finish_template_type_parm (aggr, tmpl);
1953 }
1954
1955 /* ARGUMENT is the default-argument value for a template template
1956    parameter.  If ARGUMENT is invalid, issue error messages and return
1957    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
1958
1959 tree
1960 check_template_template_default_arg (tree argument)
1961 {
1962   if (TREE_CODE (argument) != TEMPLATE_DECL
1963       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1964       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1965     {
1966       if (TREE_CODE (argument) == TYPE_DECL)
1967         {
1968           tree t = TREE_TYPE (argument);
1969
1970           /* Try to emit a slightly smarter error message if we detect
1971              that the user is using a template instantiation.  */
1972           if (CLASSTYPE_TEMPLATE_INFO (t) 
1973               && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1974             error ("invalid use of type `%T' as a default value for a "
1975                    "template template-parameter", t);
1976           else
1977             error ("invalid use of `%D' as a default value for a template "
1978                    "template-parameter", argument);
1979         }
1980       else
1981         error ("invalid default argument for a template template parameter");
1982       return error_mark_node;
1983     }
1984
1985   return argument;
1986 }
1987
1988 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1989    nonzero, the parameter list was terminated by a `...'.  */
1990
1991 tree
1992 finish_parmlist (tree parms, int ellipsis)
1993 {
1994   if (parms)
1995     {
1996       /* We mark the PARMS as a parmlist so that declarator processing can
1997          disambiguate certain constructs.  */
1998       TREE_PARMLIST (parms) = 1;
1999       /* We do not append void_list_node here, but leave it to grokparms
2000          to do that.  */
2001       PARMLIST_ELLIPSIS_P (parms) = ellipsis;
2002     }
2003   return parms;
2004 }
2005
2006 /* Begin a class definition, as indicated by T.  */
2007
2008 tree
2009 begin_class_definition (tree t)
2010 {
2011   if (t == error_mark_node)
2012     return error_mark_node;
2013
2014   if (processing_template_parmlist)
2015     {
2016       error ("definition of `%#T' inside template parameter list", t);
2017       return error_mark_node;
2018     }
2019   /* A non-implicit typename comes from code like:
2020
2021        template <typename T> struct A {
2022          template <typename U> struct A<T>::B ...
2023
2024      This is erroneous.  */
2025   else if (TREE_CODE (t) == TYPENAME_TYPE)
2026     {
2027       error ("invalid definition of qualified type `%T'", t);
2028       t = error_mark_node;
2029     }
2030
2031   if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2032     {
2033       t = make_aggr_type (RECORD_TYPE);
2034       pushtag (make_anon_name (), t, 0);
2035     }
2036
2037   /* If this type was already complete, and we see another definition,
2038      that's an error.  */
2039   if (COMPLETE_TYPE_P (t))
2040     {
2041       error ("redefinition of `%#T'", t);
2042       cp_error_at ("previous definition of `%#T'", t);
2043       return error_mark_node;
2044     }
2045
2046   /* Update the location of the decl.  */
2047   DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2048   
2049   if (TYPE_BEING_DEFINED (t))
2050     {
2051       t = make_aggr_type (TREE_CODE (t));
2052       pushtag (TYPE_IDENTIFIER (t), t, 0);
2053     }
2054   maybe_process_partial_specialization (t);
2055   pushclass (t);
2056   TYPE_BEING_DEFINED (t) = 1;
2057   if (flag_pack_struct)
2058     {
2059       tree v;
2060       TYPE_PACKED (t) = 1;
2061       /* Even though the type is being defined for the first time
2062          here, there might have been a forward declaration, so there
2063          might be cv-qualified variants of T.  */
2064       for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2065         TYPE_PACKED (v) = 1;
2066     }
2067   /* Reset the interface data, at the earliest possible
2068      moment, as it might have been set via a class foo;
2069      before.  */
2070   if (! TYPE_ANONYMOUS_P (t))
2071     {
2072       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2073       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2074         (t, interface_unknown);
2075     }
2076   reset_specialization();
2077   
2078   /* Make a declaration for this class in its own scope.  */
2079   build_self_reference ();
2080
2081   return t;
2082 }
2083
2084 /* Finish the member declaration given by DECL.  */
2085
2086 void
2087 finish_member_declaration (tree decl)
2088 {
2089   if (decl == error_mark_node || decl == NULL_TREE)
2090     return;
2091
2092   if (decl == void_type_node)
2093     /* The COMPONENT was a friend, not a member, and so there's
2094        nothing for us to do.  */
2095     return;
2096
2097   /* We should see only one DECL at a time.  */
2098   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2099
2100   /* Set up access control for DECL.  */
2101   TREE_PRIVATE (decl) 
2102     = (current_access_specifier == access_private_node);
2103   TREE_PROTECTED (decl) 
2104     = (current_access_specifier == access_protected_node);
2105   if (TREE_CODE (decl) == TEMPLATE_DECL)
2106     {
2107       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2108       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2109     }
2110
2111   /* Mark the DECL as a member of the current class.  */
2112   DECL_CONTEXT (decl) = current_class_type;
2113
2114   /* [dcl.link]
2115
2116      A C language linkage is ignored for the names of class members
2117      and the member function type of class member functions.  */
2118   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2119     SET_DECL_LANGUAGE (decl, lang_cplusplus);
2120
2121   /* Put functions on the TYPE_METHODS list and everything else on the
2122      TYPE_FIELDS list.  Note that these are built up in reverse order.
2123      We reverse them (to obtain declaration order) in finish_struct.  */
2124   if (TREE_CODE (decl) == FUNCTION_DECL 
2125       || DECL_FUNCTION_TEMPLATE_P (decl))
2126     {
2127       /* We also need to add this function to the
2128          CLASSTYPE_METHOD_VEC.  */
2129       add_method (current_class_type, decl, /*error_p=*/0);
2130
2131       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2132       TYPE_METHODS (current_class_type) = decl;
2133
2134       maybe_add_class_template_decl_list (current_class_type, decl, 
2135                                           /*friend_p=*/0);
2136     }
2137   /* Enter the DECL into the scope of the class.  */
2138   else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2139            || pushdecl_class_level (decl))
2140     {
2141       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2142          go at the beginning.  The reason is that lookup_field_1
2143          searches the list in order, and we want a field name to
2144          override a type name so that the "struct stat hack" will
2145          work.  In particular:
2146
2147            struct S { enum E { }; int E } s;
2148            s.E = 3;
2149
2150          is valid.  In addition, the FIELD_DECLs must be maintained in
2151          declaration order so that class layout works as expected.
2152          However, we don't need that order until class layout, so we
2153          save a little time by putting FIELD_DECLs on in reverse order
2154          here, and then reversing them in finish_struct_1.  (We could
2155          also keep a pointer to the correct insertion points in the
2156          list.)  */
2157
2158       if (TREE_CODE (decl) == TYPE_DECL)
2159         TYPE_FIELDS (current_class_type) 
2160           = chainon (TYPE_FIELDS (current_class_type), decl);
2161       else
2162         {
2163           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2164           TYPE_FIELDS (current_class_type) = decl;
2165         }
2166
2167       maybe_add_class_template_decl_list (current_class_type, decl, 
2168                                           /*friend_p=*/0);
2169     }
2170 }
2171
2172 /* Finish processing the declaration of a member class template
2173    TYPES whose template parameters are given by PARMS.  */
2174
2175 tree
2176 finish_member_class_template (tree types)
2177 {
2178   tree t;
2179
2180   /* If there are declared, but undefined, partial specializations
2181      mixed in with the typespecs they will not yet have passed through
2182      maybe_process_partial_specialization, so we do that here.  */
2183   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2184     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2185       maybe_process_partial_specialization (TREE_VALUE (t));
2186
2187   grok_x_components (types);
2188   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2189     /* The component was in fact a friend declaration.  We avoid
2190        finish_member_template_decl performing certain checks by
2191        unsetting TYPES.  */
2192     types = NULL_TREE;
2193   
2194   finish_member_template_decl (types);
2195
2196   /* As with other component type declarations, we do
2197      not store the new DECL on the list of
2198      component_decls.  */
2199   return NULL_TREE;
2200 }
2201
2202 /* Finish processing a complete template declaration.  The PARMS are
2203    the template parameters.  */
2204
2205 void
2206 finish_template_decl (tree parms)
2207 {
2208   if (parms)
2209     end_template_decl ();
2210   else
2211     end_specialization ();
2212 }
2213
2214 /* Finish processing a template-id (which names a type) of the form
2215    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2216    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
2217    the scope of template-id indicated.  */
2218
2219 tree
2220 finish_template_type (tree name, tree args, int entering_scope)
2221 {
2222   tree decl;
2223
2224   decl = lookup_template_class (name, args,
2225                                 NULL_TREE, NULL_TREE, entering_scope,
2226                                 tf_error | tf_warning | tf_user);
2227   if (decl != error_mark_node)
2228     decl = TYPE_STUB_DECL (decl);
2229
2230   return decl;
2231 }
2232
2233 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2234    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2235    BASE_CLASS, or NULL_TREE if an error occurred.  The
2236    ACCESS_SPECIFIER is one of
2237    access_{default,public,protected_private}[_virtual]_node.*/
2238
2239 tree 
2240 finish_base_specifier (tree base, tree access, bool virtual_p)
2241 {
2242   tree result;
2243
2244   if (base == error_mark_node)
2245     {
2246       error ("invalid base-class specification");
2247       result = NULL_TREE;
2248     }
2249   else if (! is_aggr_type (base, 1))
2250     result = NULL_TREE;
2251   else
2252     {
2253       if (cp_type_quals (base) != 0)
2254         {
2255           error ("base class `%T' has cv qualifiers", base);
2256           base = TYPE_MAIN_VARIANT (base);
2257         }
2258       result = build_tree_list (access, base);
2259       TREE_VIA_VIRTUAL (result) = virtual_p;
2260     }
2261
2262   return result;
2263 }
2264
2265 /* Called when multiple declarators are processed.  If that is not
2266    permitted in this context, an error is issued.  */
2267
2268 void
2269 check_multiple_declarators (void)
2270 {
2271   /* [temp]
2272      
2273      In a template-declaration, explicit specialization, or explicit
2274      instantiation the init-declarator-list in the declaration shall
2275      contain at most one declarator.  
2276
2277      We don't just use PROCESSING_TEMPLATE_DECL for the first
2278      condition since that would disallow the perfectly valid code, 
2279      like `template <class T> struct S { int i, j; };'.  */
2280   if (at_function_scope_p ())
2281     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2282     return;
2283      
2284   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
2285       || processing_explicit_instantiation
2286       || processing_specialization)
2287     error ("multiple declarators in template declaration");
2288 }
2289
2290 /* Issue a diagnostic that NAME cannot be found in SCOPE.  */
2291
2292 void
2293 qualified_name_lookup_error (tree scope, tree name)
2294 {
2295   if (TYPE_P (scope))
2296     {
2297       if (!COMPLETE_TYPE_P (scope))
2298         error ("incomplete type `%T' used in nested name specifier", scope);
2299       else
2300         error ("`%D' is not a member of `%T'", name, scope);
2301     }
2302   else if (scope != global_namespace)
2303     error ("`%D' is not a member of `%D'", name, scope);
2304   else
2305     error ("`::%D' has not been declared", name);
2306 }
2307               
2308 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2309    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
2310    if non-NULL, is the type or namespace used to explicitly qualify
2311    ID_EXPRESSION.  DECL is the entity to which that name has been
2312    resolved.  
2313
2314    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2315    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
2316    be set to true if this expression isn't permitted in a
2317    constant-expression, but it is otherwise not set by this function.
2318    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2319    constant-expression, but a non-constant expression is also
2320    permissible.
2321
2322    If an error occurs, and it is the kind of error that might cause
2323    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
2324    is the caller's responsibility to issue the message.  *ERROR_MSG
2325    will be a string with static storage duration, so the caller need
2326    not "free" it.
2327
2328    Return an expression for the entity, after issuing appropriate
2329    diagnostics.  This function is also responsible for transforming a
2330    reference to a non-static member into a COMPONENT_REF that makes
2331    the use of "this" explicit.  
2332
2333    Upon return, *IDK will be filled in appropriately.  */
2334
2335 tree
2336 finish_id_expression (tree id_expression, 
2337                       tree decl,
2338                       tree scope,
2339                       cp_id_kind *idk,
2340                       tree *qualifying_class,
2341                       bool integral_constant_expression_p,
2342                       bool allow_non_integral_constant_expression_p,
2343                       bool *non_integral_constant_expression_p,
2344                       const char **error_msg)
2345 {
2346   /* Initialize the output parameters.  */
2347   *idk = CP_ID_KIND_NONE;
2348   *error_msg = NULL;
2349
2350   if (id_expression == error_mark_node)
2351     return error_mark_node;
2352   /* If we have a template-id, then no further lookup is
2353      required.  If the template-id was for a template-class, we
2354      will sometimes have a TYPE_DECL at this point.  */
2355   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2356            || TREE_CODE (decl) == TYPE_DECL)
2357     ;
2358   /* Look up the name.  */
2359   else 
2360     {
2361       if (decl == error_mark_node)
2362         {
2363           /* Name lookup failed.  */
2364           if (scope 
2365               && (!TYPE_P (scope) 
2366                   || (!dependent_type_p (scope)
2367                       && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2368                            && IDENTIFIER_TYPENAME_P (id_expression)
2369                            && dependent_type_p (TREE_TYPE (id_expression))))))
2370             {
2371               /* If the qualifying type is non-dependent (and the name
2372                  does not name a conversion operator to a dependent
2373                  type), issue an error.  */
2374               qualified_name_lookup_error (scope, id_expression);
2375               return error_mark_node;
2376             }
2377           else if (!scope)
2378             {
2379               /* It may be resolved via Koenig lookup.  */
2380               *idk = CP_ID_KIND_UNQUALIFIED;
2381               return id_expression;
2382             }
2383           else
2384             decl = id_expression;
2385         }
2386       /* If DECL is a variable that would be out of scope under
2387          ANSI/ISO rules, but in scope in the ARM, name lookup
2388          will succeed.  Issue a diagnostic here.  */
2389       else
2390         decl = check_for_out_of_scope_variable (decl);
2391
2392       /* Remember that the name was used in the definition of
2393          the current class so that we can check later to see if
2394          the meaning would have been different after the class
2395          was entirely defined.  */
2396       if (!scope && decl != error_mark_node)
2397         maybe_note_name_used_in_class (id_expression, decl);
2398     }
2399
2400   /* If we didn't find anything, or what we found was a type,
2401      then this wasn't really an id-expression.  */
2402   if (TREE_CODE (decl) == TEMPLATE_DECL
2403       && !DECL_FUNCTION_TEMPLATE_P (decl))
2404     {
2405       *error_msg = "missing template arguments";
2406       return error_mark_node;
2407     }
2408   else if (TREE_CODE (decl) == TYPE_DECL
2409            || TREE_CODE (decl) == NAMESPACE_DECL)
2410     {
2411       *error_msg = "expected primary-expression";
2412       return error_mark_node;
2413     }
2414
2415   /* If the name resolved to a template parameter, there is no
2416      need to look it up again later.  */
2417   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2418       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2419     {
2420       *idk = CP_ID_KIND_NONE;
2421       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2422         decl = TEMPLATE_PARM_DECL (decl);
2423       if (integral_constant_expression_p 
2424           && !dependent_type_p (TREE_TYPE (decl))
2425           && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))) 
2426         {
2427           if (!allow_non_integral_constant_expression_p)
2428             error ("template parameter `%D' of type `%T' is not allowed in "
2429                    "an integral constant expression because it is not of "
2430                    "integral or enumeration type", decl, TREE_TYPE (decl));
2431           *non_integral_constant_expression_p = true;
2432         }
2433       return DECL_INITIAL (decl);
2434     }
2435   /* Similarly, we resolve enumeration constants to their 
2436      underlying values.  */
2437   else if (TREE_CODE (decl) == CONST_DECL)
2438     {
2439       *idk = CP_ID_KIND_NONE;
2440       if (!processing_template_decl)
2441         return DECL_INITIAL (decl);
2442       return decl;
2443     }
2444   else
2445     {
2446       bool dependent_p;
2447
2448       /* If the declaration was explicitly qualified indicate
2449          that.  The semantics of `A::f(3)' are different than
2450          `f(3)' if `f' is virtual.  */
2451       *idk = (scope 
2452               ? CP_ID_KIND_QUALIFIED
2453               : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2454                  ? CP_ID_KIND_TEMPLATE_ID
2455                  : CP_ID_KIND_UNQUALIFIED));
2456
2457
2458       /* [temp.dep.expr]
2459
2460          An id-expression is type-dependent if it contains an
2461          identifier that was declared with a dependent type.
2462
2463          The standard is not very specific about an id-expression that
2464          names a set of overloaded functions.  What if some of them
2465          have dependent types and some of them do not?  Presumably,
2466          such a name should be treated as a dependent name.  */
2467       /* Assume the name is not dependent.  */
2468       dependent_p = false;
2469       if (!processing_template_decl)
2470         /* No names are dependent outside a template.  */
2471         ;
2472       /* A template-id where the name of the template was not resolved
2473          is definitely dependent.  */
2474       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2475                && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2476                    == IDENTIFIER_NODE))
2477         dependent_p = true;
2478       /* For anything except an overloaded function, just check its
2479          type.  */
2480       else if (!is_overloaded_fn (decl))
2481         dependent_p 
2482           = dependent_type_p (TREE_TYPE (decl));
2483       /* For a set of overloaded functions, check each of the
2484          functions.  */
2485       else
2486         {
2487           tree fns = decl;
2488
2489           if (BASELINK_P (fns))
2490             fns = BASELINK_FUNCTIONS (fns);
2491
2492           /* For a template-id, check to see if the template
2493              arguments are dependent.  */
2494           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2495             {
2496               tree args = TREE_OPERAND (fns, 1);
2497               dependent_p = any_dependent_template_arguments_p (args);
2498               /* The functions are those referred to by the
2499                  template-id.  */
2500               fns = TREE_OPERAND (fns, 0);
2501             }
2502
2503           /* If there are no dependent template arguments, go through
2504              the overloaded functions.  */
2505           while (fns && !dependent_p)
2506             {
2507               tree fn = OVL_CURRENT (fns);
2508
2509               /* Member functions of dependent classes are
2510                  dependent.  */
2511               if (TREE_CODE (fn) == FUNCTION_DECL
2512                   && type_dependent_expression_p (fn))
2513                 dependent_p = true;
2514               else if (TREE_CODE (fn) == TEMPLATE_DECL
2515                        && dependent_template_p (fn))
2516                 dependent_p = true;
2517
2518               fns = OVL_NEXT (fns);
2519             }
2520         }
2521
2522       /* If the name was dependent on a template parameter, we will
2523          resolve the name at instantiation time.  */
2524       if (dependent_p)
2525         {
2526           /* Create a SCOPE_REF for qualified names, if the scope is
2527              dependent.  */
2528           if (scope)
2529             {
2530               if (TYPE_P (scope))
2531                 *qualifying_class = scope;
2532               /* Since this name was dependent, the expression isn't
2533                  constant -- yet.  No error is issued because it might
2534                  be constant when things are instantiated.  */
2535               if (integral_constant_expression_p)
2536                 *non_integral_constant_expression_p = true;
2537               if (TYPE_P (scope) && dependent_type_p (scope))
2538                 return build_nt (SCOPE_REF, scope, id_expression);
2539               else if (TYPE_P (scope) && DECL_P (decl))
2540                 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2541                               id_expression);
2542               else
2543                 return decl;
2544             }
2545           /* A TEMPLATE_ID already contains all the information we
2546              need.  */
2547           if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2548             return id_expression;
2549           /* Since this name was dependent, the expression isn't
2550              constant -- yet.  No error is issued because it might be
2551              constant when things are instantiated.  */
2552           if (integral_constant_expression_p)
2553             *non_integral_constant_expression_p = true;
2554           *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2555           /* If we found a variable, then name lookup during the
2556              instantiation will always resolve to the same VAR_DECL
2557              (or an instantiation thereof).  */
2558           if (TREE_CODE (decl) == VAR_DECL
2559               || TREE_CODE (decl) == PARM_DECL)
2560             return decl;
2561           return id_expression;
2562         }
2563
2564       /* Only certain kinds of names are allowed in constant
2565        expression.  Enumerators and template parameters 
2566        have already been handled above.  */
2567       if (integral_constant_expression_p
2568           && !DECL_INTEGRAL_CONSTANT_VAR_P (decl))
2569         {
2570           if (!allow_non_integral_constant_expression_p)
2571             {
2572               error ("`%D' cannot appear in a constant-expression", decl);
2573               return error_mark_node;
2574             }
2575           *non_integral_constant_expression_p = true;
2576         }
2577       
2578       if (TREE_CODE (decl) == NAMESPACE_DECL)
2579         {
2580           error ("use of namespace `%D' as expression", decl);
2581           return error_mark_node;
2582         }
2583       else if (DECL_CLASS_TEMPLATE_P (decl))
2584         {
2585           error ("use of class template `%T' as expression", decl);
2586           return error_mark_node;
2587         }
2588       else if (TREE_CODE (decl) == TREE_LIST)
2589         {
2590           /* Ambiguous reference to base members.  */
2591           error ("request for member `%D' is ambiguous in "
2592                  "multiple inheritance lattice", id_expression);
2593           print_candidates (decl);
2594           return error_mark_node;
2595         }
2596
2597       /* Mark variable-like entities as used.  Functions are similarly
2598          marked either below or after overload resolution.  */
2599       if (TREE_CODE (decl) == VAR_DECL
2600           || TREE_CODE (decl) == PARM_DECL
2601           || TREE_CODE (decl) == RESULT_DECL)
2602         mark_used (decl);
2603
2604       if (scope)
2605         {
2606           decl = (adjust_result_of_qualified_name_lookup 
2607                   (decl, scope, current_class_type));
2608
2609           if (TREE_CODE (decl) == FUNCTION_DECL)
2610             mark_used (decl);
2611
2612           if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2613             *qualifying_class = scope;
2614           else if (!processing_template_decl)
2615             decl = convert_from_reference (decl);
2616           else if (TYPE_P (scope))
2617             decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2618         }
2619       else if (TREE_CODE (decl) == FIELD_DECL)
2620         decl = finish_non_static_data_member (decl, current_class_ref,
2621                                               /*qualifying_scope=*/NULL_TREE);
2622       else if (is_overloaded_fn (decl))
2623         {
2624           tree first_fn = OVL_CURRENT (decl);
2625
2626           if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2627             first_fn = DECL_TEMPLATE_RESULT (first_fn);
2628
2629           if (!really_overloaded_fn (decl))
2630             mark_used (first_fn);
2631
2632           if (TREE_CODE (first_fn) == FUNCTION_DECL
2633               && DECL_FUNCTION_MEMBER_P (first_fn)
2634               && !shared_member_p (decl))
2635             {
2636               /* A set of member functions.  */
2637               decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2638               return finish_class_member_access_expr (decl, id_expression);
2639             }
2640         }
2641       else
2642         {
2643           if (TREE_CODE (decl) == VAR_DECL
2644               || TREE_CODE (decl) == PARM_DECL
2645               || TREE_CODE (decl) == RESULT_DECL)
2646             {
2647               tree context = decl_function_context (decl);
2648               
2649               if (context != NULL_TREE && context != current_function_decl
2650                   && ! TREE_STATIC (decl))
2651                 {
2652                   error ("use of %s from containing function",
2653                          (TREE_CODE (decl) == VAR_DECL
2654                           ? "`auto' variable" : "parameter"));
2655                   cp_error_at ("  `%#D' declared here", decl);
2656                   return error_mark_node;
2657                 }
2658             }
2659           
2660           if (DECL_P (decl) && DECL_NONLOCAL (decl)
2661               && DECL_CLASS_SCOPE_P (decl)
2662               && DECL_CONTEXT (decl) != current_class_type)
2663             {
2664               tree path;
2665               
2666               path = currently_open_derived_class (DECL_CONTEXT (decl));
2667               perform_or_defer_access_check (TYPE_BINFO (path), decl);
2668             }
2669           
2670           if (! processing_template_decl)
2671             decl = convert_from_reference (decl);
2672         }
2673       
2674       /* Resolve references to variables of anonymous unions
2675          into COMPONENT_REFs.  */
2676       if (TREE_CODE (decl) == ALIAS_DECL)
2677         decl = DECL_INITIAL (decl);
2678     }
2679
2680   if (TREE_DEPRECATED (decl))
2681     warn_deprecated_use (decl);
2682
2683   return decl;
2684 }
2685
2686 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2687    use as a type-specifier.  */
2688
2689 tree
2690 finish_typeof (tree expr)
2691 {
2692   tree type;
2693
2694   if (type_dependent_expression_p (expr))
2695     {
2696       type = make_aggr_type (TYPEOF_TYPE);
2697       TYPE_FIELDS (type) = expr;
2698
2699       return type;
2700     }
2701
2702   type = TREE_TYPE (expr);
2703
2704   if (!type || type == unknown_type_node)
2705     {
2706       error ("type of `%E' is unknown", expr);
2707       return error_mark_node;
2708     }
2709
2710   return type;
2711 }
2712
2713 /* Generate RTL for the statement T, and its substatements, and any
2714    other statements at its nesting level.  */
2715
2716 static void
2717 cp_expand_stmt (tree t)
2718 {
2719   switch (TREE_CODE (t))
2720     {
2721     case TRY_BLOCK:
2722       genrtl_try_block (t);
2723       break;
2724
2725     case EH_SPEC_BLOCK:
2726       genrtl_eh_spec_block (t);
2727       break;
2728
2729     case HANDLER:
2730       genrtl_handler (t);
2731       break;
2732
2733     case USING_STMT:
2734       break;
2735     
2736     default:
2737       abort ();
2738       break;
2739     }
2740 }
2741
2742 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2743    will equivalent CALL_EXPRs.  */
2744
2745 static tree
2746 simplify_aggr_init_exprs_r (tree* tp, 
2747                             int* walk_subtrees,
2748                             void* data ATTRIBUTE_UNUSED)
2749 {
2750   /* We don't need to walk into types; there's nothing in a type that
2751      needs simplification.  (And, furthermore, there are places we
2752      actively don't want to go.  For example, we don't want to wander
2753      into the default arguments for a FUNCTION_DECL that appears in a
2754      CALL_EXPR.)  */
2755   if (TYPE_P (*tp))
2756     {
2757       *walk_subtrees = 0;
2758       return NULL_TREE;
2759     }
2760   /* Only AGGR_INIT_EXPRs are interesting.  */
2761   else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2762     return NULL_TREE;
2763
2764   simplify_aggr_init_expr (tp);
2765
2766   /* Keep iterating.  */
2767   return NULL_TREE;
2768 }
2769
2770 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR.  This
2771    function is broken out from the above for the benefit of the tree-ssa
2772    project.  */
2773
2774 void
2775 simplify_aggr_init_expr (tree *tp)
2776 {
2777   tree aggr_init_expr = *tp;
2778
2779   /* Form an appropriate CALL_EXPR.  */
2780   tree fn = TREE_OPERAND (aggr_init_expr, 0);
2781   tree args = TREE_OPERAND (aggr_init_expr, 1);
2782   tree slot = TREE_OPERAND (aggr_init_expr, 2);
2783   tree type = TREE_TYPE (aggr_init_expr);
2784
2785   tree call_expr;
2786   enum style_t { ctor, arg, pcc } style;
2787
2788   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2789     style = ctor;
2790 #ifdef PCC_STATIC_STRUCT_RETURN
2791   else if (1)
2792     style = pcc;
2793 #endif
2794   else if (TREE_ADDRESSABLE (type))
2795     style = arg;
2796   else
2797     /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2798        handling.  See build_cplus_new.  */
2799     abort ();
2800
2801   if (style == ctor || style == arg)
2802     {
2803       /* Pass the address of the slot.  If this is a constructor, we
2804          replace the first argument; otherwise, we tack on a new one.  */
2805       tree addr;
2806
2807       if (style == ctor)
2808         args = TREE_CHAIN (args);
2809
2810       cxx_mark_addressable (slot);
2811       addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (slot)), slot);
2812       if (style == arg)
2813         {
2814           /* The return type might have different cv-quals from the slot.  */
2815           tree fntype = TREE_TYPE (TREE_TYPE (fn));
2816 #ifdef ENABLE_CHECKING
2817           if (TREE_CODE (fntype) != FUNCTION_TYPE
2818               && TREE_CODE (fntype) != METHOD_TYPE)
2819             abort ();
2820 #endif
2821           addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2822         }
2823
2824       args = tree_cons (NULL_TREE, addr, args);
2825     }
2826
2827   call_expr = build (CALL_EXPR, 
2828                      TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2829                      fn, args, NULL_TREE);
2830
2831   if (style == arg)
2832     /* Tell the backend that we've added our return slot to the argument
2833        list.  */
2834     CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2835   else if (style == pcc)
2836     {
2837       /* If we're using the non-reentrant PCC calling convention, then we
2838          need to copy the returned value out of the static buffer into the
2839          SLOT.  */
2840       push_deferring_access_checks (dk_no_check);
2841       call_expr = build_aggr_init (slot, call_expr,
2842                                    DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2843       pop_deferring_access_checks ();
2844     }
2845
2846   /* We want to use the value of the initialized location as the
2847      result.  */
2848   call_expr = build (COMPOUND_EXPR, type,
2849                      call_expr, slot);
2850
2851   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2852   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2853   *tp = call_expr;
2854 }
2855
2856 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
2857
2858 static void
2859 emit_associated_thunks (tree fn)
2860 {
2861   /* When we use vcall offsets, we emit thunks with the virtual
2862      functions to which they thunk. The whole point of vcall offsets
2863      is so that you can know statically the entire set of thunks that
2864      will ever be needed for a given virtual function, thereby
2865      enabling you to output all the thunks with the function itself.  */
2866   if (DECL_VIRTUAL_P (fn))
2867     {
2868       tree thunk;
2869       
2870       for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2871         {
2872           if (!THUNK_ALIAS (thunk))
2873             {
2874               use_thunk (thunk, /*emit_p=*/1);
2875               if (DECL_RESULT_THUNK_P (thunk))
2876                 {
2877                   tree probe;
2878                   
2879                   for (probe = DECL_THUNKS (thunk);
2880                        probe; probe = TREE_CHAIN (probe))
2881                     use_thunk (probe, /*emit_p=*/1);
2882                 }
2883             }
2884           else
2885             my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2886         }
2887     }
2888 }
2889
2890 /* Generate RTL for FN.  */
2891
2892 void
2893 expand_body (tree fn)
2894 {
2895   tree saved_function;
2896   
2897   /* Compute the appropriate object-file linkage for inline
2898      functions.  */
2899   if (DECL_DECLARED_INLINE_P (fn))
2900     import_export_decl (fn);
2901
2902   /* If FN is external, then there's no point in generating RTL for
2903      it.  This situation can arise with an inline function under
2904      `-fexternal-templates'; we instantiate the function, even though
2905      we're not planning on emitting it, in case we get a chance to
2906      inline it.  */
2907   if (DECL_EXTERNAL (fn))
2908     return;
2909
2910   /* ??? When is this needed?  */
2911   saved_function = current_function_decl;
2912
2913   /* Emit any thunks that should be emitted at the same time as FN.  */
2914   emit_associated_thunks (fn);
2915
2916   timevar_push (TV_INTEGRATION);
2917   optimize_function (fn);
2918   timevar_pop (TV_INTEGRATION);
2919
2920   tree_rest_of_compilation (fn, function_depth > 1);
2921
2922   current_function_decl = saved_function;
2923
2924   extract_interface_info ();
2925
2926   if (DECL_CLONED_FUNCTION_P (fn))
2927     {
2928       /* If this is a clone, go through the other clones now and mark
2929          their parameters used.  We have to do that here, as we don't
2930          know whether any particular clone will be expanded, and
2931          therefore cannot pick one arbitrarily.  */ 
2932       tree probe;
2933
2934       for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2935            probe && DECL_CLONED_FUNCTION_P (probe);
2936            probe = TREE_CHAIN (probe))
2937         {
2938           tree parms;
2939
2940           for (parms = DECL_ARGUMENTS (probe);
2941                parms; parms = TREE_CHAIN (parms))
2942             TREE_USED (parms) = 1;
2943         }
2944     }
2945 }
2946
2947 /* Generate RTL for FN.  */
2948
2949 void
2950 expand_or_defer_fn (tree fn)
2951 {
2952   /* When the parser calls us after finishing the body of a template
2953      function, we don't really want to expand the body.  */
2954   if (processing_template_decl)
2955     {
2956       /* Normally, collection only occurs in rest_of_compilation.  So,
2957          if we don't collect here, we never collect junk generated
2958          during the processing of templates until we hit a
2959          non-template function.  */
2960       ggc_collect ();
2961       return;
2962     }
2963
2964   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2965   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2966                                 simplify_aggr_init_exprs_r,
2967                                 NULL);
2968
2969   /* If this is a constructor or destructor body, we have to clone
2970      it.  */
2971   if (maybe_clone_body (fn))
2972     {
2973       /* We don't want to process FN again, so pretend we've written
2974          it out, even though we haven't.  */
2975       TREE_ASM_WRITTEN (fn) = 1;
2976       return;
2977     }
2978
2979   /* There's no reason to do any of the work here if we're only doing
2980      semantic analysis; this code just generates RTL.  */
2981   if (flag_syntax_only)
2982     return;
2983
2984   /* Compute the appropriate object-file linkage for inline functions.  */
2985   if (DECL_DECLARED_INLINE_P (fn))
2986     import_export_decl (fn);
2987
2988   /* If this function is marked with the constructor attribute, add it
2989      to the list of functions to be called along with constructors
2990      from static duration objects.  */
2991   if (DECL_STATIC_CONSTRUCTOR (fn))
2992     static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2993
2994   /* If this function is marked with the destructor attribute, add it
2995      to the list of functions to be called along with destructors from
2996      static duration objects.  */
2997   if (DECL_STATIC_DESTRUCTOR (fn))
2998     static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2999
3000   function_depth++;
3001
3002   /* Expand or defer, at the whim of the compilation unit manager.  */
3003   cgraph_finalize_function (fn, function_depth > 1);
3004
3005   function_depth--;
3006 }
3007
3008 /* Helper function for walk_tree, used by finish_function to override all
3009    the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
3010    value optimization.  */
3011
3012 tree
3013 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
3014 {
3015   tree nrv = (tree) data;
3016
3017   /* No need to walk into types.  There wouldn't be any need to walk into
3018      non-statements, except that we have to consider STMT_EXPRs.  */
3019   if (TYPE_P (*tp))
3020     *walk_subtrees = 0;
3021   else if (TREE_CODE (*tp) == RETURN_STMT)
3022     RETURN_STMT_EXPR (*tp) = NULL_TREE;
3023   else if (TREE_CODE (*tp) == CLEANUP_STMT
3024            && CLEANUP_DECL (*tp) == nrv)
3025     CLEANUP_EH_ONLY (*tp) = 1;
3026   /* Replace the DECL_STMT for the NRV with an initialization of the
3027      RESULT_DECL, if needed.  */
3028   else if (TREE_CODE (*tp) == DECL_STMT
3029            && DECL_STMT_DECL (*tp) == nrv)
3030     {
3031       tree init;
3032       if (DECL_INITIAL (nrv)
3033           && DECL_INITIAL (nrv) != error_mark_node)
3034         {
3035           init = build (INIT_EXPR, void_type_node,
3036                         DECL_RESULT (current_function_decl),
3037                         DECL_INITIAL (nrv));
3038           DECL_INITIAL (nrv) = error_mark_node;
3039         }
3040       else
3041         init = NULL_TREE;
3042       init = build_stmt (EXPR_STMT, init);
3043       TREE_CHAIN (init) = TREE_CHAIN (*tp);
3044       STMT_LINENO (init) = STMT_LINENO (*tp);
3045       *tp = init;
3046     }
3047
3048   /* Keep iterating.  */
3049   return NULL_TREE;
3050 }
3051
3052 /* Start generating the RTL for FN.  */
3053
3054 void
3055 cxx_expand_function_start (void)
3056 {
3057   /* Give our named return value the same RTL as our RESULT_DECL.  */
3058   if (current_function_return_value)
3059     COPY_DECL_RTL (DECL_RESULT (cfun->decl), current_function_return_value);
3060 }
3061
3062 /* Perform initialization related to this module.  */
3063
3064 void
3065 init_cp_semantics (void)
3066 {
3067   lang_expand_stmt = cp_expand_stmt;
3068 }
3069
3070 #include "gt-cp-semantics.h"