OSDN Git Service

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