OSDN Git Service

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