OSDN Git Service

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