OSDN Git Service

* semantics.c (do_poplevel): Always initialize the return value.
[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 Free Software Foundation, Inc.
7    Written by Mark Mitchell (mmitchell@usa.net) based on code found
8    formerly in parse.y and pt.c.  
9
10    This file is part of GNU CC.
11
12    GNU CC is free software; you can redistribute it and/or modify it
13    under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16    
17    GNU CC is distributed in the hope that it will be useful, but
18    WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with GNU CC; see the file COPYING.  If not, write to the Free
24    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25    02111-1307, USA.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "except.h"
32 #include "lex.h"
33 #include "toplev.h"
34
35 /* There routines provide a modular interface to perform many parsing
36    operations.  They may therefore be used during actual parsing, or
37    during template instantiation, which may be regarded as a
38    degenerate form of parsing.  Since the current g++ parser is
39    lacking in several respects, and will be reimplemented, we are
40    attempting to move most code that is not directly related to
41    parsing into this file; that will make implementing the new parser
42    much easier since it will be able to make use of these routines.  */
43
44 static void expand_stmts PROTO((tree));
45 static void do_pushlevel PROTO((void));
46 static tree do_poplevel PROTO((void));
47
48 /* Non-zero if we should generate RTL for functions that we process.
49    When this is zero, we just accumulate tree structure, without
50    interacting with the back end.  */
51 int expanding_p = 1;
52
53 /* Non-zero if we should treat statements as full expressions.  In
54    particular, this variable is no-zero if at the end of a statement
55    we should destroy any temporaries created during that statement.
56    Similarly, if, at the end of a block, we should destroy any local
57    variables in this block.  Normally, this variable is non-zero,
58    since those are the normal semantics of C++.
59
60    However, in order to represent aggregate initialization code as
61    tree structure, we use statement-expressions.  The statements
62    within the statement expression should not result in cleanups being
63    run until the entire enclosing statement is complete.  */
64 int stmts_are_full_exprs_p = 1;
65
66 /* The type of the last expression-statement we have seen.  This is
67    required because the type of a statement-expression is the type of
68    the last expression statement.  */
69 tree last_expr_type;
70
71 /* When parsing a template, LAST_TREE contains the last statement
72    parsed.  These are chained together through the TREE_CHAIN field,
73    but often need to be re-organized since the parse is performed
74    bottom-up.  This macro makes LAST_TREE the indicated SUBSTMT of
75    STMT.  */
76
77 #define RECHAIN_STMTS(stmt, substmt, last)      \
78   do {                                          \
79     substmt = last;                             \
80     TREE_CHAIN (stmt) = NULL_TREE;              \
81     last_tree = stmt;                           \
82   } while (0)
83
84 #define RECHAIN_STMTS_FROM_LAST(stmt, substmt)  \
85   RECHAIN_STMTS (stmt, substmt, last_tree)
86
87 #define RECHAIN_STMTS_FROM_CHAIN(stmt, substmt) \
88   RECHAIN_STMTS (stmt, substmt, TREE_CHAIN (stmt))
89
90 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
91
92 void 
93 finish_expr_stmt (expr)
94      tree expr;
95 {
96   if (expr != NULL_TREE)
97     {
98       if (building_stmt_tree ())
99         add_tree (build_min_nt (EXPR_STMT, expr));
100       else
101         {
102           emit_line_note (input_filename, lineno);
103           /* Do default conversion if safe and possibly important,
104              in case within ({...}).  */
105           if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
106                && lvalue_p (expr))
107               || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
108             expr = default_conversion (expr);
109
110           if (stmts_are_full_exprs_p)
111             expand_start_target_temps ();
112             
113           cplus_expand_expr_stmt (expr);
114
115           if (stmts_are_full_exprs_p)
116             {
117               expand_end_target_temps ();
118               clear_momentary ();
119             }
120         }
121     }
122
123   finish_stmt ();
124
125   /* This was an expression-statement, so we save the type of the
126      expression.  */
127   last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
128 }
129
130 /* Begin an if-statement.  Returns a newly created IF_STMT if
131    appropriate.  */
132
133 tree
134 begin_if_stmt ()
135 {
136   tree r;
137
138   if (building_stmt_tree ())
139     {
140       r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
141       add_tree (r);
142     }
143   else
144     r = NULL_TREE;
145
146   do_pushlevel ();
147
148   return r;
149 }
150
151 /* Process the COND of an if-statement, which may be given by
152    IF_STMT.  */
153
154 void 
155 finish_if_stmt_cond (cond, if_stmt)
156      tree cond;
157      tree if_stmt;
158 {
159   if (building_stmt_tree ())
160     {
161       if (last_tree != if_stmt)
162         RECHAIN_STMTS_FROM_LAST (if_stmt, IF_COND (if_stmt));
163       else
164         IF_COND (if_stmt) = copy_to_permanent (cond);
165     }
166   else
167     {
168       emit_line_note (input_filename, lineno);
169       expand_start_cond (condition_conversion (cond), 0);
170     }
171 }
172
173 /* Finish the then-clause of an if-statement, which may be given by
174    IF_STMT.  */
175
176 tree
177 finish_then_clause (if_stmt)
178      tree if_stmt;
179 {
180   if (building_stmt_tree ())
181     {
182       RECHAIN_STMTS_FROM_CHAIN (if_stmt, 
183                                 THEN_CLAUSE (if_stmt));
184       last_tree = if_stmt;
185       return if_stmt;
186     }
187   else
188     return NULL_TREE;
189 }
190
191 /* Begin the else-clause of an if-statement.  */
192
193 void 
194 begin_else_clause ()
195 {
196   if (!building_stmt_tree ())
197     expand_start_else ();
198 }
199
200 /* Finish the else-clause of an if-statement, which may be given by
201    IF_STMT.  */
202
203 void
204 finish_else_clause (if_stmt)
205      tree if_stmt;
206 {
207   if (building_stmt_tree ())
208     RECHAIN_STMTS_FROM_CHAIN (if_stmt, ELSE_CLAUSE (if_stmt));
209 }
210
211 /* Finsh an if-statement.  */
212
213 void 
214 finish_if_stmt ()
215 {
216   if (!building_stmt_tree ())
217     expand_end_cond ();
218
219   do_poplevel ();
220   finish_stmt ();
221 }
222
223 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
224    appropriate.  */
225
226 tree
227 begin_while_stmt ()
228 {
229   tree r;
230
231   if (building_stmt_tree ())
232     {
233       r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
234       add_tree (r);
235     }
236   else
237     {
238       emit_nop ();
239       emit_line_note (input_filename, lineno);
240       expand_start_loop (1); 
241       r = NULL_TREE;
242     }
243
244   do_pushlevel ();
245
246   return r;
247 }
248
249 /* Process the COND of an if-statement, which may be given by
250    WHILE_STMT.  */
251
252 void 
253 finish_while_stmt_cond (cond, while_stmt)
254      tree cond;
255      tree while_stmt;
256 {
257   if (building_stmt_tree ())
258     {
259       if (last_tree != while_stmt)
260         RECHAIN_STMTS_FROM_LAST (while_stmt, WHILE_COND (while_stmt)); 
261       else
262         TREE_OPERAND (while_stmt, 0) = copy_to_permanent (cond);
263     }
264   else
265     {
266       emit_line_note (input_filename, lineno);
267       expand_exit_loop_if_false (0, condition_conversion (cond));
268     }
269
270   /* If COND wasn't a declaration, clear out the
271      block we made for it and start a new one here so the
272      optimization in expand_end_loop will work.  */
273   if (getdecls () == NULL_TREE)
274     {
275       do_poplevel ();
276       do_pushlevel ();
277     }
278 }
279
280 /* Finish a while-statement, which may be given by WHILE_STMT.  */
281
282 void 
283 finish_while_stmt (while_stmt)
284      tree while_stmt;
285 {
286   do_poplevel ();
287
288   if (building_stmt_tree ())
289     RECHAIN_STMTS_FROM_CHAIN (while_stmt, WHILE_BODY (while_stmt));
290   else
291     expand_end_loop ();
292   finish_stmt ();
293 }
294
295 /* Begin a do-statement.  Returns a newly created DO_STMT if
296    appropriate.  */
297
298 tree
299 begin_do_stmt ()
300 {
301   if (building_stmt_tree ())
302     {
303       tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
304       add_tree (r);
305       return r;
306     }
307   else
308     {
309       emit_nop ();
310       emit_line_note (input_filename, lineno);
311       expand_start_loop_continue_elsewhere (1);
312       return NULL_TREE;
313     }
314 }
315
316 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
317
318 void
319 finish_do_body (do_stmt)
320      tree do_stmt;
321 {
322   if (building_stmt_tree ())
323     RECHAIN_STMTS_FROM_CHAIN (do_stmt, DO_BODY (do_stmt));
324   else
325     expand_loop_continue_here ();
326 }
327
328 /* Finish a do-statement, which may be given by DO_STMT, and whose
329    COND is as indicated.  */
330
331 void
332 finish_do_stmt (cond, do_stmt)
333      tree cond;
334      tree do_stmt;
335 {
336   if (building_stmt_tree ())
337     DO_COND (do_stmt) = copy_to_permanent (cond);
338   else
339     {
340       emit_line_note (input_filename, lineno);
341       expand_exit_loop_if_false (0, condition_conversion (cond));
342       expand_end_loop ();
343     }
344
345   clear_momentary ();
346   finish_stmt ();
347 }
348
349 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
350    indicated.  */
351
352 void
353 finish_return_stmt (expr)
354      tree expr;
355 {
356   if (building_stmt_tree ())
357     add_tree (build_min_nt (RETURN_STMT, expr));
358   else
359     {
360       emit_line_note (input_filename, lineno);
361       c_expand_return (expr);
362     }
363
364   finish_stmt ();
365 }
366
367 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
368
369 tree
370 begin_for_stmt ()
371 {
372   tree r;
373
374   if (building_stmt_tree ())
375     {
376       r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE, 
377                         NULL_TREE, NULL_TREE);
378       add_tree (r);
379     }
380   else
381     r = NULL_TREE;
382
383   if (flag_new_for_scope > 0)
384     {
385       do_pushlevel ();
386       note_level_for_for ();
387     }
388
389   return r;
390 }
391
392 /* Finish the for-init-statement of a for-statement, which may be
393    given by FOR_STMT.  */
394
395 void
396 finish_for_init_stmt (for_stmt)
397      tree for_stmt;
398 {
399   if (building_stmt_tree ())
400     {
401       if (last_tree != for_stmt)
402         RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_INIT_STMT (for_stmt));
403     }
404   else
405     {
406       emit_nop ();
407       emit_line_note (input_filename, lineno);
408       expand_start_loop_continue_elsewhere (1); 
409     }
410
411   do_pushlevel ();
412 }
413
414 /* Finish the COND of a for-statement, which may be given by
415    FOR_STMT.  */
416
417 void
418 finish_for_cond (cond, for_stmt)
419      tree cond;
420      tree for_stmt;
421 {
422   if (building_stmt_tree ())
423     {
424       if (last_tree != for_stmt)
425         RECHAIN_STMTS_FROM_LAST (for_stmt, FOR_COND (for_stmt));
426       else
427         FOR_COND (for_stmt) = copy_to_permanent (cond);
428     }
429   else
430     {
431       emit_line_note (input_filename, lineno);
432       if (cond)
433         expand_exit_loop_if_false (0, condition_conversion (cond));
434     }
435   
436   /* If the cond wasn't a declaration, clear out the
437      block we made for it and start a new one here so the
438      optimization in expand_end_loop will work.  */
439   if (getdecls () == NULL_TREE)
440     {
441       do_poplevel ();
442       do_pushlevel ();
443     }  
444 }
445
446 /* Finish the increment-EXPRESSION in a for-statement, which may be
447    given by FOR_STMT.  */
448
449 void
450 finish_for_expr (expr, for_stmt)
451      tree expr;
452      tree for_stmt;
453 {
454   if (building_stmt_tree ())
455     FOR_EXPR (for_stmt) = copy_to_permanent (expr);
456
457   /* Don't let the tree nodes for EXPR be discarded
458      by clear_momentary during the parsing of the next stmt.  */
459   push_momentary ();
460 }
461
462 /* Finish the body of a for-statement, which may be given by
463    FOR_STMT.  The increment-EXPR for the loop must be
464    provided.  */
465
466 void
467 finish_for_stmt (expr, for_stmt)
468      tree expr;
469      tree for_stmt;
470 {
471   /* Pop the scope for the body of the loop.  */
472   do_poplevel ();
473
474   if (building_stmt_tree ())
475     RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_BODY (for_stmt));
476   else
477     {
478       emit_line_note (input_filename, lineno);
479       expand_loop_continue_here ();
480       if (expr) 
481         finish_expr_stmt (expr);
482       expand_end_loop ();
483     }
484
485   pop_momentary ();
486
487   if (flag_new_for_scope > 0)
488     do_poplevel ();
489
490   finish_stmt (); 
491 }
492
493 /* Finish a break-statement.  */
494
495 void
496 finish_break_stmt ()
497 {
498   emit_line_note (input_filename, lineno);
499   if (building_stmt_tree ())
500     add_tree (build_min_nt (BREAK_STMT));
501   else if ( ! expand_exit_something ())
502     cp_error ("break statement not within loop or switch");
503 }
504
505 /* Finish a continue-statement.  */
506
507 void
508 finish_continue_stmt ()
509 {
510   emit_line_note (input_filename, lineno);
511   if (building_stmt_tree ())
512     add_tree (build_min_nt (CONTINUE_STMT));
513   else if (! expand_continue_loop (0))
514     cp_error ("continue statement not within a loop");   
515 }
516
517 /* Begin a switch-statement.  */
518
519 void
520 begin_switch_stmt ()
521 {
522   do_pushlevel ();
523 }
524
525 /* Finish the cond of a switch-statement.  Returns a new
526    SWITCH_STMT if appropriate.  */ 
527
528 tree
529 finish_switch_cond (cond)
530      tree cond;
531 {
532   tree r;
533
534   if (building_stmt_tree ())
535     {
536       r = build_min_nt (SWITCH_STMT, cond, NULL_TREE);
537       add_tree (r);
538     }
539   else if (cond != error_mark_node)
540     {
541       emit_line_note (input_filename, lineno);
542       c_expand_start_case (cond);
543       r = NULL_TREE;
544     }
545   else
546     {
547       /* The code is in error, but we don't want expand_end_case to
548          crash. */
549       c_expand_start_case (boolean_false_node);
550       r = NULL_TREE;
551     }
552
553   push_switch ();
554
555   /* Don't let the tree nodes for COND be discarded by
556      clear_momentary during the parsing of the next stmt.  */
557   push_momentary ();
558
559   return r;
560 }
561
562 /* Finish the body of a switch-statement, which may be given by
563    SWITCH_STMT.  The COND to switch on is indicated.  */
564
565 void
566 finish_switch_stmt (cond, switch_stmt)
567      tree cond;
568      tree switch_stmt;
569 {
570   if (building_stmt_tree ())
571     RECHAIN_STMTS_FROM_CHAIN (switch_stmt, SWITCH_BODY (switch_stmt));
572   else
573     expand_end_case (cond);
574   pop_momentary ();
575   pop_switch (); 
576   do_poplevel ();
577   finish_stmt ();
578 }
579
580 /* Finish a case-label.  */
581
582 void 
583 finish_case_label (low_value, high_value)
584      tree low_value;
585      tree high_value;
586 {
587   if (building_stmt_tree ())
588     {
589       add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
590       return;
591     }
592
593   do_case (low_value, high_value);
594 }
595
596 /* Finish a goto-statement.  */
597
598 void
599 finish_goto_stmt (destination)
600      tree destination;
601 {
602   if (building_stmt_tree ())
603     add_tree (build_min_nt (GOTO_STMT, destination));
604   else
605     {
606       emit_line_note (input_filename, lineno);
607
608       if (TREE_CODE (destination) == IDENTIFIER_NODE)
609         {
610           tree decl = lookup_label (destination);
611           TREE_USED (decl) = 1;
612           expand_goto (decl); 
613         }
614       else
615         expand_computed_goto (destination);
616     }
617 }
618
619 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
620    appropriate.  */
621
622 tree
623 begin_try_block ()
624 {
625   if (building_stmt_tree ())
626     {
627       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
628                              NULL_TREE);
629       add_tree (r);
630       return r;
631     }
632   else
633     {
634       emit_line_note (input_filename, lineno);
635       expand_start_try_stmts ();
636       return NULL_TREE;
637     }
638 }
639
640 /* Likewise, for a function-try-block.  */
641
642 tree
643 begin_function_try_block ()
644 {
645   if (building_stmt_tree ())
646     {
647       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
648                              NULL_TREE);
649       add_tree (r);
650       return r;
651     }
652   else
653     {
654       if (! current_function_parms_stored)
655         store_parm_decls ();
656       expand_start_early_try_stmts ();
657       return NULL_TREE;
658     }
659 }
660
661 /* Finish a try-block, which may be given by TRY_BLOCK.  */
662
663 void
664 finish_try_block (try_block)
665      tree try_block;
666 {
667   if (building_stmt_tree ())
668     RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block));
669   else
670     expand_start_all_catch ();  
671 }
672
673 /* Finish an implicitly generated try-block, with a cleanup is given
674    by CLEANUP.  */
675
676 void
677 finish_cleanup (cleanup, try_block)
678      tree cleanup;
679      tree try_block;
680 {
681   if (building_stmt_tree ()) 
682     {
683       TRY_HANDLERS (try_block) = copy_to_permanent (cleanup);
684       CLEANUP_P (try_block) = 1;
685     }
686   else
687     expand_eh_region_end (protect_with_terminate (cleanup));
688 }
689
690 /* Likewise, for a function-try-block.  */
691
692 void
693 finish_function_try_block (try_block)
694      tree try_block; 
695 {
696   if (building_stmt_tree ())
697     RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block));
698   else
699     {
700       end_protect_partials ();
701       expand_start_all_catch ();
702       in_function_try_handler = 1;
703     }
704 }
705
706 /* Finish a handler-sequence for a try-block, which may be given by
707    TRY_BLOCK.  */
708
709 void
710 finish_handler_sequence (try_block)
711      tree try_block;
712 {
713   if (building_stmt_tree ())
714     RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block));
715   else
716     expand_end_all_catch ();
717 }
718
719 /* Likewise, for a function-try-block.  */
720
721 void
722 finish_function_handler_sequence (try_block)
723      tree try_block;
724 {
725   if (building_stmt_tree ())
726     RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block));
727   else
728     {
729       in_function_try_handler = 0;
730       expand_end_all_catch ();
731     }
732 }
733
734 /* Begin a handler.  Returns a HANDLER if appropriate.  */
735
736 tree
737 begin_handler ()
738 {
739   tree r;
740
741   if (building_stmt_tree ())
742     {
743       r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
744       add_tree (r);
745     }
746   else
747     r = NULL_TREE;
748
749   do_pushlevel ();
750
751   return r;
752 }
753
754 /* Finish the handler-parameters for a handler, which may be given by
755    HANDLER.  */
756
757 void
758 finish_handler_parms (handler)
759      tree handler;
760 {
761   if (building_stmt_tree ())
762     RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_PARMS (handler));
763 }
764
765 /* Finish a handler, which may be given by HANDLER.  */
766
767 void
768 finish_handler (handler)
769      tree handler;
770 {
771   if (building_stmt_tree ())
772     RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_BODY (handler));
773   else
774     expand_end_catch_block ();
775
776   do_poplevel ();
777 }
778
779 /* Begin a compound-statement.  If HAS_NO_SCOPE is non-zero, the
780    compound-statement does not define a scope.  Returns a new
781    COMPOUND_STMT if appropriate.  */
782
783 tree
784 begin_compound_stmt (has_no_scope)
785      int has_no_scope;
786 {
787   tree r; 
788
789   if (building_stmt_tree ())
790     {
791       r = build_min_nt (COMPOUND_STMT, NULL_TREE);
792       add_tree (r);
793       if (has_no_scope)
794         COMPOUND_STMT_NO_SCOPE (r) = 1;
795     }
796   else
797     r = NULL_TREE;
798
799   last_expr_type = NULL_TREE;
800
801   if (!has_no_scope)
802     do_pushlevel ();
803   else
804     /* Normally, we try hard to keep the BLOCK for a
805        statement-expression.  But, if it's a statement-expression with
806        a scopeless block, there's nothing to keep, and we don't want
807        to accidentally keep a block *inside* the scopeless block.  */ 
808     keep_next_level (0);
809
810   return r;
811 }
812
813
814 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
815    If HAS_NO_SCOPE is non-zero, the compound statement does not define
816    a scope.  */
817
818 tree
819 finish_compound_stmt (has_no_scope, compound_stmt)
820      int has_no_scope;
821      tree compound_stmt;
822 {
823   tree r;
824   tree t;
825
826   if (!has_no_scope)
827     r = do_poplevel ();
828   else
829     r = NULL_TREE;
830
831   if (building_stmt_tree ())
832     RECHAIN_STMTS_FROM_CHAIN (compound_stmt, 
833                               COMPOUND_BODY (compound_stmt));
834
835   /* When we call finish_stmt we will lost LAST_EXPR_TYPE.  But, since
836      the precise purpose of that variable is store the type of the
837      last expression statement within the last compound statement, we
838      preserve the value.  */
839   t = last_expr_type;
840   finish_stmt ();
841   last_expr_type = t;
842
843   return r;
844 }
845
846 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
847    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
848    CLOBBERS.  */
849
850 void
851 finish_asm_stmt (cv_qualifier, string, output_operands,
852                  input_operands, clobbers)
853      tree cv_qualifier;
854      tree string;
855      tree output_operands;
856      tree input_operands;
857      tree clobbers;
858 {
859   if (TREE_CHAIN (string))
860     {
861       if (building_stmt_tree ())
862         /* We need to build the combined string on the permanent
863            obstack so that we can use it during instantiations.  */
864         push_permanent_obstack ();
865
866       string = combine_strings (string);
867
868       if (building_stmt_tree ())
869         pop_obstacks ();
870     }
871
872   if (cv_qualifier != NULL_TREE
873       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
874     {
875       cp_warning ("%s qualifier ignored on asm",
876                   IDENTIFIER_POINTER (cv_qualifier));
877       cv_qualifier = NULL_TREE;
878     }
879
880   if (building_stmt_tree ())
881     {
882       tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
883                              output_operands, input_operands,
884                              clobbers);
885       add_tree (r);
886     }
887   else
888     {
889       emit_line_note (input_filename, lineno);
890       if (output_operands != NULL_TREE || input_operands != NULL_TREE
891             || clobbers != NULL_TREE)
892         {
893           tree t;
894
895           for (t = input_operands; t; t = TREE_CHAIN (t))
896             TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
897
898           c_expand_asm_operands (string, output_operands,
899                                  input_operands, 
900                                  clobbers,
901                                  cv_qualifier != NULL_TREE,
902                                  input_filename, lineno);
903         }
904       else
905         expand_asm (string);
906       
907       finish_stmt ();
908     }
909 }
910
911 /* Finish a label with the indicated NAME.  */
912
913 void
914 finish_label_stmt (name)
915      tree name;
916 {
917   tree decl;
918
919   if (building_stmt_tree ())
920     {
921       push_permanent_obstack ();
922       decl = build_decl (LABEL_DECL, name, void_type_node);
923       pop_obstacks ();
924       DECL_SOURCE_LINE (decl) = lineno;
925       DECL_SOURCE_FILE (decl) = input_filename;
926       add_tree (decl);
927     }
928   else
929     {
930       decl = define_label (input_filename, lineno, name);
931       if (decl)
932         expand_label (decl);
933     }
934 }
935
936 /* Create a declaration statement for the declaration given by the
937    DECL.  */
938
939 void
940 add_decl_stmt (decl)
941      tree decl;
942 {
943   tree decl_stmt;
944
945   /* We need the type to last until instantiation time.  */
946   TREE_TYPE (decl) = copy_to_permanent (TREE_TYPE (decl));
947   decl_stmt = build_min_nt (DECL_STMT, decl);
948   add_tree (decl_stmt);
949 }
950
951 /* We're in a constructor, and have just constructed a a subobject of
952    *THIS.  CLEANUP is code to run if an exception is thrown before the
953    end of the current function is reached.   */
954
955 void 
956 finish_subobject (cleanup)
957      tree cleanup;
958 {
959   if (building_stmt_tree ())
960     {
961       tree r = build_min_nt (SUBOBJECT, cleanup);
962       add_tree (r);
963     }
964   else
965     add_partial_entry (cleanup);
966 }
967
968 /* Bind a name and initialization to the return value of
969    the current function.  */
970
971 void
972 finish_named_return_value (return_id, init)
973      tree return_id, init;
974 {
975   tree decl = DECL_RESULT (current_function_decl);
976
977   if (pedantic)
978     /* Give this error as many times as there are occurrences,
979        so that users can use Emacs compilation buffers to find
980        and fix all such places.  */
981     pedwarn ("ANSI C++ does not permit named return values");
982
983   if (return_id != NULL_TREE)
984     {
985       if (DECL_NAME (decl) == NULL_TREE)
986         {
987           DECL_NAME (decl) = return_id;
988           DECL_ASSEMBLER_NAME (decl) = return_id;
989         }
990       else
991         {
992           cp_error ("return identifier `%D' already in place", return_id);
993           return;
994         }
995     }
996
997   /* Can't let this happen for constructors.  */
998   if (DECL_CONSTRUCTOR_P (current_function_decl))
999     {
1000       error ("can't redefine default return value for constructors");
1001       return;
1002     }
1003
1004   /* If we have a named return value, put that in our scope as well.  */
1005   if (DECL_NAME (decl) != NULL_TREE)
1006     {
1007       /* Let `cp_finish_decl' know that this initializer is ok.  */
1008       DECL_INITIAL (decl) = init;
1009       pushdecl (decl);
1010
1011       if (building_stmt_tree ())
1012         add_tree (build_min_nt (RETURN_INIT, return_id,
1013                                 copy_to_permanent (init)));
1014       else
1015         {
1016           cp_finish_decl (decl, init, NULL_TREE, 0, 0);
1017           store_return_init (decl);
1018         }
1019     }
1020 }
1021
1022 /* Cache the value of this class's main virtual function table pointer
1023    in a register variable.  This will save one indirection if a
1024    more than one virtual function call is made this function.  */
1025
1026 void
1027 setup_vtbl_ptr ()
1028 {
1029   extern tree base_init_expr;
1030
1031   if (base_init_expr == 0
1032       && DECL_CONSTRUCTOR_P (current_function_decl))
1033     {
1034       if (building_stmt_tree ())
1035         add_tree (build_min_nt
1036                   (CTOR_INITIALIZER,
1037                    current_member_init_list, current_base_init_list));
1038       else
1039         emit_base_init (current_class_type);
1040     }
1041
1042   /* Always keep the BLOCK node associated with the outermost pair of
1043      curley braces of a function.  These are needed for correct
1044      operation of dwarfout.c.  */
1045   keep_next_level (1);
1046 }
1047
1048 /* Begin a new scope.  */
1049
1050 static void
1051 do_pushlevel ()
1052 {
1053   if (!building_stmt_tree ())
1054     {
1055       emit_line_note (input_filename, lineno);
1056       clear_last_expr ();
1057     }
1058   push_momentary ();
1059   if (stmts_are_full_exprs_p)
1060     pushlevel (0);
1061   if (!building_stmt_tree () && stmts_are_full_exprs_p)
1062     expand_start_bindings (0);
1063 }
1064
1065 /* Finish a scope.  */
1066
1067 static tree
1068 do_poplevel ()
1069 {
1070   tree t;
1071
1072   if (!building_stmt_tree () && stmts_are_full_exprs_p)
1073     expand_end_bindings (getdecls (), kept_level_p (), 0);
1074   if (stmts_are_full_exprs_p)
1075     t = poplevel (kept_level_p (), 1, 0);
1076   else
1077     t = NULL_TREE;
1078   pop_momentary ();
1079   return t;
1080 }
1081
1082 /* Finish a parenthesized expression EXPR.  */
1083
1084 tree
1085 finish_parenthesized_expr (expr)
1086      tree expr;
1087 {
1088   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1089     /* This inhibits warnings in truthvalue_conversion.  */
1090     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1091
1092   return expr;
1093 }
1094
1095 /* Begin a statement-expression.  The value returned must be passed to
1096    finish_stmt_expr.  */
1097
1098 tree 
1099 begin_stmt_expr ()
1100 {
1101   keep_next_level (1);
1102   /* If we're building a statement tree, then the upcoming compound
1103      statement will be chained onto the tree structure, starting at
1104      last_tree.  We return last_tree so that we can later unhook the
1105      compound statement.  */
1106   return building_stmt_tree () ? last_tree : expand_start_stmt_expr(); 
1107 }
1108
1109 /* Finish a statement-expression.  RTL_EXPR should be the value
1110    returned by the previous begin_stmt_expr; EXPR is the
1111    statement-expression.  Returns an expression representing the
1112    statement-expression.  */
1113
1114 tree 
1115 finish_stmt_expr (rtl_expr, expr)
1116      tree rtl_expr;
1117      tree expr;
1118 {
1119   tree result;
1120
1121   if (!building_stmt_tree ())
1122     {
1123       rtl_expr = expand_end_stmt_expr (rtl_expr);
1124       /* The statements have side effects, so the group does.  */
1125       TREE_SIDE_EFFECTS (rtl_expr) = 1;
1126     }
1127
1128   if (building_stmt_tree ())
1129     {
1130       /* If the last thing in the statement-expression was not an
1131          expression-statement, then it has type `void'.  */
1132       if (!last_expr_type)
1133         last_expr_type = void_type_node;
1134       result = build_min (STMT_EXPR, last_expr_type, last_tree);
1135       TREE_SIDE_EFFECTS (result) = 1;
1136       
1137       /* Remove the compound statement from the tree structure; it is
1138          now saved in the STMT_EXPR.  */
1139       last_tree = rtl_expr;
1140       TREE_CHAIN (last_tree) = NULL_TREE;
1141     }
1142   else if (expr && TREE_CODE (expr) == BLOCK)
1143     {
1144       result = build (BIND_EXPR, TREE_TYPE (rtl_expr),
1145                       NULL_TREE, rtl_expr, expr);
1146       delete_block (expr);
1147     }
1148   else
1149     result = rtl_expr;
1150
1151   if (expr && TREE_CODE (expr) == BLOCK)
1152     /* Remove the block from the tree at this point.  It gets put back
1153        at the proper place when the STMT_EXPR or BIND_EXPR is
1154        expanded.  */
1155     delete_block (expr);
1156
1157   return result;
1158 }
1159
1160 /* Finish a call to FN with ARGS.  Returns a representation of the
1161    call.  */
1162
1163 tree 
1164 finish_call_expr (fn, args, koenig)
1165      tree fn;
1166      tree args;
1167      int koenig;
1168 {
1169   tree result;
1170
1171   if (koenig)
1172     {
1173       if (TREE_CODE (fn) == BIT_NOT_EXPR)
1174         fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1175       else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1176         fn = do_identifier (fn, 2, args);
1177     }
1178   result = build_x_function_call (fn, args, current_class_ref);
1179
1180   if (TREE_CODE (result) == CALL_EXPR
1181       && (! TREE_TYPE (result)
1182           || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1183     result = require_complete_type (result);
1184
1185   return result;
1186 }
1187
1188 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1189    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1190    POSTDECREMENT_EXPR.)  */
1191
1192 tree 
1193 finish_increment_expr (expr, code)
1194      tree expr;
1195      enum tree_code code;
1196 {
1197   /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1198      a COMPONENT_REF).  This way if we've got, say, a reference to a
1199      static member that's being operated on, we don't end up trying to
1200      find a member operator for the class it's in.  */
1201
1202   if (TREE_CODE (expr) == OFFSET_REF)
1203     expr = resolve_offset_ref (expr);
1204   return build_x_unary_op (code, expr);  
1205 }
1206
1207 /* Finish a use of `this'.  Returns an expression for `this'.  */
1208
1209 tree 
1210 finish_this_expr ()
1211 {
1212   tree result;
1213
1214   if (current_class_ptr)
1215     {
1216 #ifdef WARNING_ABOUT_CCD
1217       TREE_USED (current_class_ptr) = 1;
1218 #endif
1219       result = current_class_ptr;
1220     }
1221   else if (current_function_decl
1222            && DECL_STATIC_FUNCTION_P (current_function_decl))
1223     {
1224       error ("`this' is unavailable for static member functions");
1225       result = error_mark_node;
1226     }
1227   else
1228     {
1229       if (current_function_decl)
1230         error ("invalid use of `this' in non-member function");
1231       else
1232         error ("invalid use of `this' at top level");
1233       result = error_mark_node;
1234     }
1235
1236   return result;
1237 }
1238
1239 /* Finish a member function call using OBJECT and ARGS as arguments to
1240    FN.  Returns an expression for the call.  */
1241
1242 tree 
1243 finish_object_call_expr (fn, object, args)
1244      tree fn;
1245      tree object;
1246      tree args;
1247 {
1248 #if 0
1249   /* This is a future direction of this code, but because
1250      build_x_function_call cannot always undo what is done in
1251      build_component_ref entirely yet, we cannot do this.  */
1252
1253   tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1254   return finish_call_expr (real_fn, args);
1255 #else
1256   if (TREE_CODE (fn) == TYPE_DECL)
1257     {
1258       if (processing_template_decl)
1259         /* This can happen on code like:
1260
1261            class X;
1262            template <class T> void f(T t) {
1263              t.X();
1264            }  
1265
1266            We just grab the underlying IDENTIFIER.  */
1267         fn = DECL_NAME (fn);
1268       else
1269         {
1270           cp_error ("calling type `%T' like a method", fn);
1271           return error_mark_node;
1272         }
1273     }
1274
1275   return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1276 #endif
1277 }
1278
1279 /* Finish a qualified member function call using OBJECT and ARGS as
1280    arguments to FN.  Returns an expressino for the call.  */
1281
1282 tree 
1283 finish_qualified_object_call_expr (fn, object, args)
1284      tree fn;
1285      tree object;
1286      tree args;
1287 {
1288   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1289                                    TREE_OPERAND (fn, 1), args);
1290 }
1291
1292 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1293    being the scope, if any, of DESTRUCTOR.  Returns an expression for
1294    the call.  */
1295
1296 tree 
1297 finish_pseudo_destructor_call_expr (object, scope, destructor)
1298      tree object;
1299      tree scope;
1300      tree destructor;
1301 {
1302   if (processing_template_decl)
1303     return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1304
1305   if (scope && scope != destructor)
1306     cp_error ("destructor specifier `%T::~%T()' must have matching names", 
1307               scope, destructor);
1308
1309   if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1310       && (TREE_CODE (TREE_TYPE (object)) !=
1311           TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1312     cp_error ("`%E' is not of type `%T'", object, destructor);
1313
1314   return cp_convert (void_type_node, object);
1315 }
1316
1317 /* Finish a call to a globally qualified member function FN using
1318    ARGS.  Returns an expression for the call.  */
1319
1320 tree 
1321 finish_qualified_call_expr (fn, args)
1322      tree fn;
1323      tree args;
1324 {
1325   if (processing_template_decl)
1326     return build_min_nt (CALL_EXPR, copy_to_permanent (fn), args,
1327                          NULL_TREE);
1328   else
1329     return build_member_call (TREE_OPERAND (fn, 0),
1330                               TREE_OPERAND (fn, 1),
1331                               args);
1332 }
1333
1334 /* Finish an expression taking the address of LABEL.  Returns an
1335    expression for the address.  */
1336
1337 tree 
1338 finish_label_address_expr (label)
1339      tree label;
1340 {
1341   tree result;
1342
1343   label = lookup_label (label);
1344   if (label == NULL_TREE)
1345     result = null_pointer_node;
1346   else
1347     {
1348       TREE_USED (label) = 1;
1349       result = build1 (ADDR_EXPR, ptr_type_node, label);
1350       TREE_CONSTANT (result) = 1;
1351     }
1352
1353   return result;
1354 }
1355
1356 /* Finish an expression of the form CODE EXPR.  */
1357
1358 tree
1359 finish_unary_op_expr (code, expr)
1360      enum tree_code code;
1361      tree expr;
1362 {
1363   tree result = build_x_unary_op (code, expr);
1364   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1365     TREE_NEGATED_INT (result) = 1;
1366   overflow_warning (result);
1367   return result;
1368 }
1369
1370 /* Finish an id-expression.  */
1371
1372 tree
1373 finish_id_expr (expr)
1374      tree expr;
1375 {
1376   if (TREE_CODE (expr) == IDENTIFIER_NODE)
1377     expr = do_identifier (expr, 1, NULL_TREE);
1378
1379   return expr;
1380 }
1381
1382 /* Begin a new-placement.  */
1383
1384 int
1385 begin_new_placement ()
1386 {
1387   /* The arguments to a placement new might be passed to a
1388      deallocation function, in the event that the allocation throws an
1389      exception.  Since we don't expand exception handlers until the
1390      end of a function, we must make sure the arguments stay around
1391      that long.  */
1392   return suspend_momentary ();
1393 }
1394
1395 /* Finish a new-placement.  The ARGS are the placement arguments.  The
1396    COOKIE is the value returned by the previous call to
1397    begin_new_placement.  */
1398
1399 tree
1400 finish_new_placement (args, cookie)
1401      tree args;
1402      int cookie;
1403 {
1404   resume_momentary (cookie);
1405   return args;
1406 }
1407
1408 /* Begin a function defniition declared with DECL_SPECS and
1409    DECLARATOR.  Returns non-zero if the function-declaration is
1410    legal.  */
1411
1412 int
1413 begin_function_definition (decl_specs, declarator)
1414      tree decl_specs;
1415      tree declarator;
1416 {
1417   tree specs;
1418   tree attrs;
1419   split_specs_attrs (decl_specs, &specs, &attrs);
1420   if (!start_function (specs, declarator, attrs, 0))
1421     return 0;
1422   
1423   reinit_parse_for_function ();
1424   /* The things we're about to see are not directly qualified by any
1425      template headers we've seen thus far.  */
1426   reset_specialization ();
1427
1428   return 1;
1429 }
1430
1431 /* Begin a constructor declarator of the form `SCOPE::NAME'.  Returns
1432    a SCOPE_REF.  */
1433
1434 tree 
1435 begin_constructor_declarator (scope, name)
1436      tree scope;
1437      tree name;
1438 {
1439   tree result = build_parse_node (SCOPE_REF, scope, name);
1440   enter_scope_of (result);
1441   return result;
1442 }
1443
1444 /* Finish an init-declarator.  Returns a DECL.  */
1445
1446 tree
1447 finish_declarator (declarator, declspecs, attributes,
1448                    prefix_attributes, initialized)
1449      tree declarator;
1450      tree declspecs;
1451      tree attributes;
1452      tree prefix_attributes;
1453      int initialized;
1454 {
1455   return start_decl (declarator, declspecs, initialized, attributes,
1456                      prefix_attributes); 
1457 }
1458
1459 /* Finish a translation unit.  */
1460
1461 void 
1462 finish_translation_unit ()
1463 {
1464   /* In case there were missing closebraces,
1465      get us back to the global binding level.  */
1466   while (! toplevel_bindings_p ())
1467     poplevel (0, 0, 0);
1468   while (current_namespace != global_namespace)
1469     pop_namespace ();
1470   finish_file ();
1471 }
1472
1473 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1474    Returns the parameter.  */
1475
1476 tree 
1477 finish_template_type_parm (aggr, identifier)
1478      tree aggr;
1479      tree identifier;
1480 {
1481   if (aggr != class_type_node)
1482     {
1483       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1484       aggr = class_type_node;
1485     }
1486
1487   return build_tree_list (aggr, identifier);
1488 }
1489
1490 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1491    Returns the parameter.  */
1492
1493 tree 
1494 finish_template_template_parm (aggr, identifier)
1495      tree aggr;
1496      tree identifier;
1497 {
1498   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1499   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1500   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1501   DECL_TEMPLATE_RESULT (tmpl) = decl;
1502   SET_DECL_ARTIFICIAL (decl);
1503   end_template_decl ();
1504
1505   return finish_template_type_parm (aggr, tmpl);
1506 }
1507
1508 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1509    non-zero, the parameter list was terminated by a `...'.  */
1510
1511 tree
1512 finish_parmlist (parms, ellipsis)
1513      tree parms;
1514      int ellipsis;
1515 {
1516   if (!ellipsis)
1517     chainon (parms, void_list_node);
1518   /* We mark the PARMS as a parmlist so that declarator processing can
1519      disambiguate certain constructs.  */
1520   if (parms != NULL_TREE)
1521     TREE_PARMLIST (parms) = 1;
1522
1523   return parms;
1524 }
1525
1526 /* Begin a class definition, as indicated by T.  */
1527
1528 tree
1529 begin_class_definition (t)
1530      tree t;
1531 {
1532   push_permanent_obstack ();
1533
1534   if (t == error_mark_node
1535       || ! IS_AGGR_TYPE (t))
1536     {
1537       t = make_lang_type (RECORD_TYPE);
1538       pushtag (make_anon_name (), t, 0);
1539     }
1540
1541   /* In a definition of a member class template, we will get here with an
1542      implicit typename, a TYPENAME_TYPE with a type.  */
1543   if (TREE_CODE (t) == TYPENAME_TYPE)
1544     t = TREE_TYPE (t);
1545   
1546   /* If we generated a partial instantiation of this type, but now
1547      we're seeing a real definition, we're actually looking at a
1548      partial specialization.  Consider:
1549
1550        template <class T, class U>
1551        struct Y {};
1552
1553        template <class T>
1554        struct X {};
1555
1556        template <class T, class U>
1557        void f()
1558        {
1559          typename X<Y<T, U> >::A a;
1560        }
1561
1562        template <class T, class U>
1563        struct X<Y<T, U> >
1564        {
1565        };
1566
1567      We have to undo the effects of the previous partial
1568      instantiation.  */
1569   if (PARTIAL_INSTANTIATION_P (t))
1570     {
1571       if (!pedantic) 
1572         {
1573           /* Unfortunately, when we're not in pedantic mode, we
1574              attempt to actually fill in some of the fields of the
1575              partial instantiation, in order to support the implicit
1576              typename extension.  Clear those fields now, in
1577              preparation for the definition here.  The fields cleared
1578              here must match those set in instantiate_class_template.
1579              Look for a comment mentioning begin_class_definition
1580              there.  */
1581           TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1582           TYPE_FIELDS (t) = NULL_TREE;
1583           TYPE_METHODS (t) = NULL_TREE;
1584           CLASSTYPE_TAGS (t) = NULL_TREE;
1585           TYPE_SIZE (t) = NULL_TREE;
1586         }
1587
1588       /* This isn't a partial instantiation any more.  */
1589       PARTIAL_INSTANTIATION_P (t) = 0;
1590     }
1591   /* If this type was already complete, and we see another definition,
1592      that's an error.  */
1593   else if (TYPE_SIZE (t))
1594     duplicate_tag_error (t);
1595
1596   /* Update the location of the decl.  */
1597   DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1598   DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1599   
1600   if (TYPE_BEING_DEFINED (t))
1601     {
1602       t = make_lang_type (TREE_CODE (t));
1603       pushtag (TYPE_IDENTIFIER (t), t, 0);
1604     }
1605   maybe_process_partial_specialization (t);
1606   pushclass (t, 1);
1607   TYPE_BEING_DEFINED (t) = 1;
1608   /* Reset the interface data, at the earliest possible
1609      moment, as it might have been set via a class foo;
1610      before.  */
1611   {
1612     tree name = TYPE_IDENTIFIER (t);
1613     
1614     if (! ANON_AGGRNAME_P (name))
1615       {
1616         CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1617         SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1618           (t, interface_unknown);
1619       }
1620     
1621     /* Only leave this bit clear if we know this
1622        class is part of an interface-only specification.  */
1623     if (! CLASSTYPE_INTERFACE_KNOWN (t)
1624         || ! CLASSTYPE_INTERFACE_ONLY (t))
1625       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1626   }
1627 #if 0
1628   tmp = TYPE_IDENTIFIER ($<ttype>0);
1629   if (tmp && IDENTIFIER_TEMPLATE (tmp))
1630     overload_template_name (tmp, 1);
1631 #endif
1632   reset_specialization();
1633   
1634   /* In case this is a local class within a template
1635      function, we save the current tree structure so
1636      that we can get it back later.  */
1637   begin_tree ();
1638
1639   /* Make a declaration for this class in its own scope.  */
1640   build_self_reference ();
1641
1642   return t;
1643 }
1644
1645 /* Finish the member declaration given by DECL.  */
1646
1647 void
1648 finish_member_declaration (decl)
1649      tree decl;
1650 {
1651   if (decl == error_mark_node || decl == NULL_TREE)
1652     return;
1653
1654   if (decl == void_type_node)
1655     /* The COMPONENT was a friend, not a member, and so there's
1656        nothing for us to do.  */
1657     return;
1658
1659   /* We should see only one DECL at a time.  */
1660   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1661
1662   /* Set up access control for DECL.  */
1663   TREE_PRIVATE (decl) 
1664     = (current_access_specifier == access_private_node);
1665   TREE_PROTECTED (decl) 
1666     = (current_access_specifier == access_protected_node);
1667   if (TREE_CODE (decl) == TEMPLATE_DECL)
1668     {
1669       TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1670       TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1671     }
1672
1673   /* Mark the DECL as a member of the current class.  */
1674   if (TREE_CODE (decl) == FUNCTION_DECL 
1675       || DECL_FUNCTION_TEMPLATE_P (decl))
1676     /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1677        finish_struct.  Presumably it is already set as the function is
1678        parsed.  Perhaps DECL_CLASS_CONTEXT is already set, too?  */
1679     DECL_CLASS_CONTEXT (decl) = current_class_type;
1680   else
1681     DECL_CONTEXT (decl) = current_class_type;
1682
1683   /* Put functions on the TYPE_METHODS list and everything else on the
1684      TYPE_FIELDS list.  Note that these are built up in reverse order.
1685      We reverse them (to obtain declaration order) in finish_struct.  */
1686   if (TREE_CODE (decl) == FUNCTION_DECL 
1687       || DECL_FUNCTION_TEMPLATE_P (decl))
1688     {
1689       /* We also need to add this function to the
1690          CLASSTYPE_METHOD_VEC.  */
1691       add_method (current_class_type, 0, decl);
1692
1693       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1694       TYPE_METHODS (current_class_type) = decl;
1695     }
1696   else
1697     {
1698       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
1699          go at the beginning.  The reason is that lookup_field_1
1700          searches the list in order, and we want a field name to
1701          override a type name so that the "struct stat hack" will
1702          work.  In particular:
1703
1704            struct S { enum E { }; int E } s;
1705            s.E = 3;
1706
1707          is legal.  In addition, the FIELD_DECLs must be maintained in
1708          declaration order so that class layout works as expected.
1709          However, we don't need that order until class layout, so we
1710          save a little time by putting FIELD_DECLs on in reverse order
1711          here, and then reversing them in finish_struct_1.  (We could
1712          also keep a pointer to the correct insertion points in the
1713          list.)  */
1714
1715       if (TREE_CODE (decl) == TYPE_DECL)
1716         TYPE_FIELDS (current_class_type) 
1717           = chainon (TYPE_FIELDS (current_class_type), decl);
1718       else
1719         {
1720           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1721           TYPE_FIELDS (current_class_type) = decl;
1722         }
1723
1724       /* Enter the DECL into the scope of the class.  */
1725       if (TREE_CODE (decl) != USING_DECL)
1726         pushdecl_class_level (decl);
1727     }
1728 }
1729
1730 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
1731    the definition is immediately followed by a semicolon.  Returns the
1732    type.  */
1733
1734 tree
1735 finish_class_definition (t, attributes, semi, pop_scope_p)
1736      tree t;
1737      tree attributes;
1738      int semi;
1739      int pop_scope_p;
1740 {
1741   /* finish_struct nukes this anyway; if finish_exception does too,
1742      then it can go.  */
1743   if (semi)
1744     note_got_semicolon (t);
1745
1746   /* If we got any attributes in class_head, xref_tag will stick them in
1747      TREE_TYPE of the type.  Grab them now.  */
1748   attributes = chainon (TREE_TYPE (t), attributes);
1749   TREE_TYPE (t) = NULL_TREE;
1750
1751   if (TREE_CODE (t) == ENUMERAL_TYPE)
1752     ;
1753   else
1754     {
1755       t = finish_struct (t, attributes);
1756       if (semi) 
1757         note_got_semicolon (t);
1758     }
1759
1760   pop_obstacks ();
1761
1762   if (! semi)
1763     check_for_missing_semicolon (t); 
1764   if (pop_scope_p)
1765     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1766   if (current_scope () == current_function_decl)
1767     do_pending_defargs ();
1768
1769   return t;
1770 }
1771
1772 /* Finish processing the default argument expressions cached during
1773    the processing of a class definition.  */
1774
1775 void
1776 begin_inline_definitions ()
1777 {
1778   if (pending_inlines 
1779       && current_scope () == current_function_decl)
1780     do_pending_inlines ();
1781 }
1782
1783 /* Finish processing the inline function definitions cached during the
1784    processing of a class definition.  */
1785
1786 void
1787 finish_inline_definitions ()
1788 {
1789   if (current_class_type == NULL_TREE)
1790     clear_inline_text_obstack (); 
1791   
1792   /* Undo the begin_tree in begin_class_definition.  */
1793   end_tree ();
1794 }
1795
1796 /* Finish processing the declaration of a member class template
1797    TYPES whose template parameters are given by PARMS.  */
1798
1799 tree
1800 finish_member_class_template (types)
1801      tree types;
1802 {
1803   tree t;
1804
1805   /* If there are declared, but undefined, partial specializations
1806      mixed in with the typespecs they will not yet have passed through
1807      maybe_process_partial_specialization, so we do that here.  */
1808   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1809     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1810       maybe_process_partial_specialization (TREE_VALUE (t));
1811
1812   note_list_got_semicolon (types);
1813   grok_x_components (types);
1814   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1815     /* The component was in fact a friend declaration.  We avoid
1816        finish_member_template_decl performing certain checks by
1817        unsetting TYPES.  */
1818     types = NULL_TREE;
1819   
1820   finish_member_template_decl (types);
1821
1822   /* As with other component type declarations, we do
1823      not store the new DECL on the list of
1824      component_decls.  */
1825   return NULL_TREE;
1826 }
1827
1828 /* Finish processsing a complete template declaration.  The PARMS are
1829    the template parameters.  */
1830
1831 void
1832 finish_template_decl (parms)
1833      tree parms;
1834 {
1835   if (parms)
1836     end_template_decl ();
1837   else
1838     end_specialization ();
1839 }
1840
1841 /* Finish processing a a template-id (which names a type) of the form
1842    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
1843    template-id.  If ENTERING_SCOPE is non-zero we are about to enter
1844    the scope of template-id indicated.  */
1845
1846 tree
1847 finish_template_type (name, args, entering_scope)
1848      tree name;
1849      tree args;
1850      int entering_scope;
1851 {
1852   tree decl;
1853
1854   decl = lookup_template_class (name, args,
1855                                 NULL_TREE, NULL_TREE, entering_scope);
1856   if (decl != error_mark_node)
1857     decl = TYPE_STUB_DECL (decl);
1858
1859   return decl;
1860 }
1861
1862 /* SR is a SCOPE_REF node.  Enter the scope of SR, whether it is a
1863    namespace scope or a class scope.  */
1864
1865 void
1866 enter_scope_of (sr)
1867      tree sr;
1868 {
1869   tree scope = TREE_OPERAND (sr, 0);
1870
1871   if (TREE_CODE (scope) == NAMESPACE_DECL)
1872     {
1873       push_decl_namespace (scope);
1874       TREE_COMPLEXITY (sr) = -1;
1875     }
1876   else if (scope != current_class_type)
1877     {
1878       if (TREE_CODE (scope) == TYPENAME_TYPE)
1879         {
1880           /* In a declarator for a template class member, the scope will
1881              get here as an implicit typename, a TYPENAME_TYPE with a type.  */
1882           scope = TREE_TYPE (scope);
1883           TREE_OPERAND (sr, 0) = scope;
1884         }
1885       push_nested_class (scope, 3);
1886       TREE_COMPLEXITY (sr) = current_class_depth;
1887     }
1888 }
1889
1890 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
1891    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
1892    BASE_CLASS, or NULL_TREE if an error occurred.  The
1893    ACCESSS_SPECIFIER is one of
1894    access_{default,public,protected_private}[_virtual]_node.*/
1895
1896 tree 
1897 finish_base_specifier (access_specifier, base_class)
1898      tree access_specifier;
1899      tree base_class;
1900 {
1901   tree type;
1902   tree result;
1903
1904   if (base_class == NULL_TREE)
1905     {
1906       error ("invalid base class");
1907       type = error_mark_node;
1908     }
1909   else
1910     type = TREE_TYPE (base_class);
1911
1912   if (! is_aggr_type (type, 1))
1913     result = NULL_TREE;
1914   else
1915     result = build_tree_list (access_specifier, type);
1916
1917   return result;
1918 }
1919
1920 /* Called when multiple declarators are processed.  If that is not
1921    premitted in this context, an error is issued.  */
1922
1923 void
1924 check_multiple_declarators ()
1925 {
1926   /* [temp]
1927      
1928      In a template-declaration, explicit specialization, or explicit
1929      instantiation the init-declarator-list in the declaration shall
1930      contain at most one declarator.  
1931
1932      We don't just use PROCESSING_TEMPLATE_DECL for the first
1933      condition since that would disallow the perfectly legal code, 
1934      like `template <class T> struct S { int i, j; };'.  */
1935   tree scope = current_scope ();
1936
1937   if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1938     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
1939     return;
1940      
1941   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
1942       || processing_explicit_instantiation
1943       || processing_specialization)
1944     cp_error ("multiple declarators in template declaration");
1945 }
1946
1947 tree
1948 finish_typeof (expr)
1949      tree expr;
1950 {
1951   if (processing_template_decl)
1952     {
1953       tree t;
1954
1955       push_permanent_obstack ();
1956       t = make_lang_type (TYPEOF_TYPE);
1957       TYPE_FIELDS (t) = expr;
1958       pop_obstacks ();
1959
1960       return t;
1961     }
1962
1963   return TREE_TYPE (expr);
1964 }
1965
1966 /* Create an empty statement tree for FN.  */
1967
1968 void
1969 begin_stmt_tree (fn)
1970      tree fn;
1971 {
1972   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
1973      what follows.  We remove the extraneous statement in
1974      finish_stmt_tree.  */
1975   DECL_SAVED_TREE (fn) = build_nt (EXPR_STMT, void_zero_node);
1976   last_tree = DECL_SAVED_TREE (fn);
1977   last_expr_type = NULL_TREE;
1978 }
1979
1980 /* Finish the statement tree for FN.  */
1981
1982 void
1983 finish_stmt_tree (fn)
1984      tree fn;
1985 {
1986   DECL_SAVED_TREE (fn) = TREE_CHAIN (DECL_SAVED_TREE (fn));
1987 }
1988
1989 /* Generate RTL for the chain of statements T.  */
1990
1991 static void 
1992 expand_stmts (t)
1993      tree t;
1994 {
1995   while (t)
1996     {
1997       expand_stmt (t);
1998       t = TREE_CHAIN (t);
1999     }
2000 }
2001
2002 /* Generate RTL for the statement T, and its substatements.  */
2003
2004 tree
2005 expand_stmt (t)
2006      tree t;
2007 {
2008   if (t == NULL_TREE || t == error_mark_node)
2009     return NULL_TREE;
2010
2011   switch (TREE_CODE (t))
2012     {
2013     case RETURN_STMT:
2014       lineno = STMT_LINENO (t);
2015       finish_return_stmt (RETURN_EXPR (t));
2016       break;
2017
2018     case EXPR_STMT:
2019       lineno = STMT_LINENO (t);
2020       finish_expr_stmt (EXPR_STMT_EXPR (t));
2021       break;
2022
2023     case DECL_STMT:
2024       {
2025         tree decl;
2026         int i = suspend_momentary ();
2027
2028         lineno = STMT_LINENO (t);
2029         emit_line_note (input_filename, lineno);
2030         decl = DECL_STMT_DECL (t);
2031         /* We need to clear DECL_CONTEXT so that maybe_push_decl
2032            will push it into the current scope.  */
2033         if (DECL_CONTEXT (decl) == current_function_decl)
2034           DECL_CONTEXT (decl) = NULL_TREE;
2035         /* If we marked this variable as dead when we processed it
2036            before, we must undo that now.  The variable has been
2037            resuscitated.  */
2038         if (TREE_CODE (decl) == VAR_DECL)
2039           DECL_DEAD_FOR_LOCAL (decl) = 0;
2040         maybe_push_decl (decl);
2041         cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2042         resume_momentary (i);
2043       }
2044       break;
2045
2046     case FOR_STMT:
2047       {
2048         tree tmp;
2049
2050         lineno = STMT_LINENO (t);
2051         begin_for_stmt ();
2052         for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
2053           expand_stmt (tmp);
2054         finish_for_init_stmt (NULL_TREE);
2055         finish_for_cond (FOR_COND (t), NULL_TREE);
2056         tmp = FOR_EXPR (t);
2057         finish_for_expr (tmp, NULL_TREE);
2058         expand_stmt (FOR_BODY (t));
2059         finish_for_stmt (tmp, NULL_TREE);
2060       }
2061       break;
2062
2063     case WHILE_STMT:
2064       {
2065         lineno = STMT_LINENO (t);
2066         begin_while_stmt ();
2067         finish_while_stmt_cond (WHILE_COND (t), NULL_TREE);
2068         expand_stmt (WHILE_BODY (t));
2069         finish_while_stmt (NULL_TREE);
2070       }
2071       break;
2072
2073     case DO_STMT:
2074       {
2075         lineno = STMT_LINENO (t);
2076         begin_do_stmt ();
2077         expand_stmt (DO_BODY (t));
2078         finish_do_body (NULL_TREE);
2079         finish_do_stmt (DO_COND (t), NULL_TREE);
2080       }
2081       break;
2082
2083     case IF_STMT:
2084       lineno = STMT_LINENO (t);
2085       begin_if_stmt ();
2086       finish_if_stmt_cond (IF_COND (t), NULL_TREE);
2087       if (THEN_CLAUSE (t))
2088         {
2089           expand_stmt (THEN_CLAUSE (t));
2090           finish_then_clause (NULL_TREE);
2091         }
2092       if (ELSE_CLAUSE (t))
2093         {
2094           begin_else_clause ();
2095           expand_stmt (ELSE_CLAUSE (t));
2096           finish_else_clause (NULL_TREE);
2097         }
2098       finish_if_stmt ();
2099       break;
2100
2101     case COMPOUND_STMT:
2102       lineno = STMT_LINENO (t);
2103       begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2104       expand_stmts (COMPOUND_BODY (t));
2105       return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
2106                                    NULL_TREE);
2107
2108     case BREAK_STMT:
2109       lineno = STMT_LINENO (t);
2110       finish_break_stmt ();
2111       break;
2112
2113     case CONTINUE_STMT:
2114       lineno = STMT_LINENO (t);
2115       finish_continue_stmt ();
2116       break;
2117
2118     case SWITCH_STMT:
2119       lineno = STMT_LINENO (t);
2120       begin_switch_stmt ();
2121       finish_switch_cond (SWITCH_COND (t));
2122       if (TREE_OPERAND (t, 1))
2123         expand_stmt (SWITCH_BODY (t));
2124       finish_switch_stmt (SWITCH_COND (t), NULL_TREE);
2125       break;
2126
2127     case CASE_LABEL:
2128       finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2129       break;
2130
2131     case LABEL_DECL:
2132       t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2133                         DECL_NAME (t));
2134       if (t)
2135         expand_label (t);
2136       break;
2137
2138     case GOTO_STMT:
2139       lineno = STMT_LINENO (t);
2140       finish_goto_stmt (GOTO_DESTINATION (t));
2141       break;
2142
2143     case ASM_STMT:
2144       lineno = STMT_LINENO (t);
2145       finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2146                        (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2147       break;
2148
2149     case TRY_BLOCK:
2150       lineno = STMT_LINENO (t);
2151       if (CLEANUP_P (t))
2152         {
2153           expand_eh_region_start ();
2154           expand_stmt (TRY_STMTS (t));
2155           finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2156         }
2157       else
2158         {
2159           begin_try_block ();
2160           expand_stmt (TRY_STMTS (t));
2161           finish_try_block (NULL_TREE);
2162           expand_stmts (TRY_HANDLERS (t));
2163           finish_handler_sequence (NULL_TREE);
2164         }
2165       break;
2166
2167     case HANDLER:
2168       lineno = STMT_LINENO (t);
2169       begin_handler ();
2170       if (HANDLER_PARMS (t))
2171         {
2172           tree d = HANDLER_PARMS (t);
2173           expand_start_catch_block (TREE_OPERAND (d, 1),
2174                                     TREE_OPERAND (d, 0));
2175         }
2176       else
2177         expand_start_catch_block (NULL_TREE, NULL_TREE);
2178       finish_handler_parms (NULL_TREE);
2179       expand_stmt (HANDLER_BODY (t));
2180       finish_handler (NULL_TREE);
2181       break;
2182
2183     case SUBOBJECT:
2184       lineno = STMT_LINENO (t);
2185       finish_subobject (SUBOBJECT_CLEANUP (t));
2186       break;
2187
2188     default:
2189       my_friendly_abort (19990810);
2190       break;
2191     }
2192
2193   return NULL_TREE;
2194 }
2195
2196 /* Generate RTL for FN.  */
2197
2198 void
2199 expand_body (fn)
2200      tree fn;
2201 {
2202   int saved_expanding_p;
2203   int nested; 
2204   tree t;
2205   tree try_block;
2206
2207   /* Remember whether we're already processing a function definition
2208      so that we can tell finish_function.  */
2209   nested = in_function_p ();
2210
2211   /* Let the compiler know that now is the time to really generate
2212      actualy RTL.  */
2213   saved_expanding_p = expanding_p;
2214   expanding_p = 1;
2215
2216   start_function (NULL_TREE, fn, NULL_TREE, 1);
2217   store_parm_decls ();
2218
2219   /* There are a few things that we do not handle recursively.  For
2220      example, a function try-block is handled differently from an
2221      ordinary try-block, so we must handle it here.  */
2222   t = DECL_SAVED_TREE (fn);
2223   try_block = NULL_TREE;
2224   if (t && TREE_CODE (t) == TRY_BLOCK)
2225     {
2226       try_block = t;
2227       begin_function_try_block ();
2228       t = TRY_STMTS (try_block);
2229     }
2230
2231   if (t && TREE_CODE (t) == RETURN_INIT)
2232     {
2233       /* Clear this out so that finish_named_return_value can set it
2234          again.  */
2235       DECL_NAME (DECL_RESULT (fn)) = NULL_TREE;
2236       finish_named_return_value (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2237       t = TREE_CHAIN (t);
2238     }
2239
2240   if (t && TREE_CODE (t) == CTOR_INITIALIZER)
2241     {
2242       current_member_init_list = TREE_OPERAND (t, 0);
2243       current_base_init_list = TREE_OPERAND (t, 1);
2244       t = TREE_CHAIN (t);
2245     }
2246
2247   /* If this is a constructor, we need to initialize our members and
2248      base-classes.  */
2249   setup_vtbl_ptr ();
2250
2251   /* Expand the body.  */
2252   expand_stmt (t);
2253
2254   /* If there was a function try-block, expand the handlers.  */
2255   if (try_block)
2256     {
2257       finish_function_try_block (NULL_TREE);
2258       {
2259         tree handler = TRY_HANDLERS (try_block);
2260         for (; handler; handler = TREE_CHAIN (handler))
2261           expand_stmt (handler);
2262       }
2263       finish_function_handler_sequence (NULL_TREE);
2264     }
2265
2266   finish_function (lineno, 0, nested);
2267
2268   /* Restore EXPANDING_P.  */
2269   expanding_p = saved_expanding_p;
2270 }