OSDN Git Service

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