OSDN Git Service

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