OSDN Git Service

* cp-tree.def (LABEL_STMT): New tree node.
[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 (TREE_CODE (destination) == IDENTIFIER_NODE)
603     destination = lookup_label (destination);
604
605   if (building_stmt_tree ())
606     add_tree (build_min_nt (GOTO_STMT, destination));
607   else
608     {
609       emit_line_note (input_filename, lineno);
610
611       if (TREE_CODE (destination) == LABEL_DECL)
612         {
613           TREE_USED (destination) = 1;
614           expand_goto (destination); 
615         }
616       else
617         expand_computed_goto (destination);
618     }
619 }
620
621 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
622    appropriate.  */
623
624 tree
625 begin_try_block ()
626 {
627   if (building_stmt_tree ())
628     {
629       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
630                              NULL_TREE);
631       add_tree (r);
632       return r;
633     }
634   else
635     {
636       emit_line_note (input_filename, lineno);
637       expand_start_try_stmts ();
638       return NULL_TREE;
639     }
640 }
641
642 /* Likewise, for a function-try-block.  */
643
644 tree
645 begin_function_try_block ()
646 {
647   if (building_stmt_tree ())
648     {
649       tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
650                              NULL_TREE);
651       add_tree (r);
652       return r;
653     }
654   else
655     {
656       if (! current_function_parms_stored)
657         store_parm_decls ();
658       expand_start_early_try_stmts ();
659       return NULL_TREE;
660     }
661 }
662
663 /* Finish a try-block, which may be given by TRY_BLOCK.  */
664
665 void
666 finish_try_block (try_block)
667      tree try_block;
668 {
669   if (building_stmt_tree ())
670     RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block));
671   else
672     expand_start_all_catch ();  
673 }
674
675 /* Finish an implicitly generated try-block, with a cleanup is given
676    by CLEANUP.  */
677
678 void
679 finish_cleanup (cleanup, try_block)
680      tree cleanup;
681      tree try_block;
682 {
683   if (building_stmt_tree ()) 
684     {
685       TRY_HANDLERS (try_block) = copy_to_permanent (cleanup);
686       CLEANUP_P (try_block) = 1;
687     }
688   else
689     expand_eh_region_end (protect_with_terminate (cleanup));
690 }
691
692 /* Likewise, for a function-try-block.  */
693
694 void
695 finish_function_try_block (try_block)
696      tree try_block; 
697 {
698   if (building_stmt_tree ())
699     RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block));
700   else
701     {
702       end_protect_partials ();
703       expand_start_all_catch ();
704       in_function_try_handler = 1;
705     }
706 }
707
708 /* Finish a handler-sequence for a try-block, which may be given by
709    TRY_BLOCK.  */
710
711 void
712 finish_handler_sequence (try_block)
713      tree try_block;
714 {
715   if (building_stmt_tree ())
716     RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block));
717   else
718     expand_end_all_catch ();
719 }
720
721 /* Likewise, for a function-try-block.  */
722
723 void
724 finish_function_handler_sequence (try_block)
725      tree try_block;
726 {
727   if (building_stmt_tree ())
728     RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block));
729   else
730     {
731       in_function_try_handler = 0;
732       expand_end_all_catch ();
733     }
734 }
735
736 /* Begin a handler.  Returns a HANDLER if appropriate.  */
737
738 tree
739 begin_handler ()
740 {
741   tree r;
742
743   if (building_stmt_tree ())
744     {
745       r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
746       add_tree (r);
747     }
748   else
749     r = NULL_TREE;
750
751   do_pushlevel ();
752
753   return r;
754 }
755
756 /* Finish the handler-parameters for a handler, which may be given by
757    HANDLER.  */
758
759 void
760 finish_handler_parms (handler)
761      tree handler;
762 {
763   if (building_stmt_tree ())
764     RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_PARMS (handler));
765 }
766
767 /* Finish a handler, which may be given by HANDLER.  */
768
769 void
770 finish_handler (handler)
771      tree handler;
772 {
773   if (building_stmt_tree ())
774     RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_BODY (handler));
775   else
776     expand_end_catch_block ();
777
778   do_poplevel ();
779 }
780
781 /* Begin a compound-statement.  If HAS_NO_SCOPE is non-zero, the
782    compound-statement does not define a scope.  Returns a new
783    COMPOUND_STMT if appropriate.  */
784
785 tree
786 begin_compound_stmt (has_no_scope)
787      int has_no_scope;
788 {
789   tree r; 
790
791   if (building_stmt_tree ())
792     {
793       r = build_min_nt (COMPOUND_STMT, NULL_TREE);
794       add_tree (r);
795       if (has_no_scope)
796         COMPOUND_STMT_NO_SCOPE (r) = 1;
797     }
798   else
799     r = NULL_TREE;
800
801   last_expr_type = NULL_TREE;
802
803   if (!has_no_scope)
804     do_pushlevel ();
805   else
806     /* Normally, we try hard to keep the BLOCK for a
807        statement-expression.  But, if it's a statement-expression with
808        a scopeless block, there's nothing to keep, and we don't want
809        to accidentally keep a block *inside* the scopeless block.  */ 
810     keep_next_level (0);
811
812   return r;
813 }
814
815
816 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
817    If HAS_NO_SCOPE is non-zero, the compound statement does not define
818    a scope.  */
819
820 tree
821 finish_compound_stmt (has_no_scope, compound_stmt)
822      int has_no_scope;
823      tree compound_stmt;
824 {
825   tree r;
826   tree t;
827
828   if (!has_no_scope)
829     r = do_poplevel ();
830   else
831     r = NULL_TREE;
832
833   if (building_stmt_tree ())
834     RECHAIN_STMTS_FROM_CHAIN (compound_stmt, 
835                               COMPOUND_BODY (compound_stmt));
836
837   /* When we call finish_stmt we will lost LAST_EXPR_TYPE.  But, since
838      the precise purpose of that variable is store the type of the
839      last expression statement within the last compound statement, we
840      preserve the value.  */
841   t = last_expr_type;
842   finish_stmt ();
843   last_expr_type = t;
844
845   return r;
846 }
847
848 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
849    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
850    CLOBBERS.  */
851
852 void
853 finish_asm_stmt (cv_qualifier, string, output_operands,
854                  input_operands, clobbers)
855      tree cv_qualifier;
856      tree string;
857      tree output_operands;
858      tree input_operands;
859      tree clobbers;
860 {
861   if (TREE_CHAIN (string))
862     {
863       if (building_stmt_tree ())
864         /* We need to build the combined string on the permanent
865            obstack so that we can use it during instantiations.  */
866         push_permanent_obstack ();
867
868       string = combine_strings (string);
869
870       if (building_stmt_tree ())
871         pop_obstacks ();
872     }
873
874   if (cv_qualifier != NULL_TREE
875       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
876     {
877       cp_warning ("%s qualifier ignored on asm",
878                   IDENTIFIER_POINTER (cv_qualifier));
879       cv_qualifier = NULL_TREE;
880     }
881
882   if (building_stmt_tree ())
883     {
884       tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
885                              output_operands, input_operands,
886                              clobbers);
887       add_tree (r);
888     }
889   else
890     {
891       emit_line_note (input_filename, lineno);
892       if (output_operands != NULL_TREE || input_operands != NULL_TREE
893             || clobbers != NULL_TREE)
894         {
895           tree t;
896
897           for (t = input_operands; t; t = TREE_CHAIN (t))
898             TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
899
900           c_expand_asm_operands (string, output_operands,
901                                  input_operands, 
902                                  clobbers,
903                                  cv_qualifier != NULL_TREE,
904                                  input_filename, lineno);
905         }
906       else
907         expand_asm (string);
908       
909       finish_stmt ();
910     }
911 }
912
913 /* Finish a label with the indicated NAME.  */
914
915 void
916 finish_label_stmt (name)
917      tree name;
918 {
919   tree decl = define_label (input_filename, lineno, name);
920
921   if (building_stmt_tree ())
922     add_tree (build_min_nt (LABEL_STMT, decl));
923   else if (decl)
924     expand_label (decl);
925 }
926
927 /* Finish a series of declarations for local labels.  G++ allows users
928    to declare "local" labels, i.e., labels with scope.  This extension
929    is useful when writing code involving statement-expressions.  */
930
931 void
932 finish_label_decl (name)
933      tree name;
934 {
935   tree decl = declare_local_label (name);
936   if (building_stmt_tree ())
937     add_decl_stmt (decl);
938 }
939
940 /* Create a declaration statement for the declaration given by the
941    DECL.  */
942
943 void
944 add_decl_stmt (decl)
945      tree decl;
946 {
947   tree decl_stmt;
948
949   /* We need the type to last until instantiation time.  */
950   TREE_TYPE (decl) = copy_to_permanent (TREE_TYPE (decl));
951   decl_stmt = build_min_nt (DECL_STMT, decl);
952   add_tree (decl_stmt);
953 }
954
955 /* We're in a constructor, and have just constructed a a subobject of
956    *THIS.  CLEANUP is code to run if an exception is thrown before the
957    end of the current function is reached.   */
958
959 void 
960 finish_subobject (cleanup)
961      tree cleanup;
962 {
963   if (building_stmt_tree ())
964     {
965       tree r = build_min_nt (SUBOBJECT, cleanup);
966       add_tree (r);
967     }
968   else
969     add_partial_entry (cleanup);
970 }
971
972 /* Bind a name and initialization to the return value of
973    the current function.  */
974
975 void
976 finish_named_return_value (return_id, init)
977      tree return_id, init;
978 {
979   tree decl = DECL_RESULT (current_function_decl);
980
981   if (pedantic)
982     /* Give this error as many times as there are occurrences,
983        so that users can use Emacs compilation buffers to find
984        and fix all such places.  */
985     pedwarn ("ANSI C++ does not permit named return values");
986
987   if (return_id != NULL_TREE)
988     {
989       if (DECL_NAME (decl) == NULL_TREE)
990         {
991           DECL_NAME (decl) = return_id;
992           DECL_ASSEMBLER_NAME (decl) = return_id;
993         }
994       else
995         {
996           cp_error ("return identifier `%D' already in place", return_id);
997           return;
998         }
999     }
1000
1001   /* Can't let this happen for constructors.  */
1002   if (DECL_CONSTRUCTOR_P (current_function_decl))
1003     {
1004       error ("can't redefine default return value for constructors");
1005       return;
1006     }
1007
1008   /* If we have a named return value, put that in our scope as well.  */
1009   if (DECL_NAME (decl) != NULL_TREE)
1010     {
1011       /* Let `cp_finish_decl' know that this initializer is ok.  */
1012       DECL_INITIAL (decl) = init;
1013       pushdecl (decl);
1014
1015       if (building_stmt_tree ())
1016         add_tree (build_min_nt (RETURN_INIT, return_id,
1017                                 copy_to_permanent (init)));
1018       else
1019         {
1020           cp_finish_decl (decl, init, NULL_TREE, 0, 0);
1021           store_return_init (decl);
1022         }
1023     }
1024 }
1025
1026 /* Cache the value of this class's main virtual function table pointer
1027    in a register variable.  This will save one indirection if a
1028    more than one virtual function call is made this function.  */
1029
1030 void
1031 setup_vtbl_ptr ()
1032 {
1033   extern tree base_init_expr;
1034
1035   if (base_init_expr == 0
1036       && DECL_CONSTRUCTOR_P (current_function_decl))
1037     {
1038       if (building_stmt_tree ())
1039         add_tree (build_min_nt
1040                   (CTOR_INITIALIZER,
1041                    current_member_init_list, current_base_init_list));
1042       else
1043         emit_base_init (current_class_type);
1044     }
1045
1046   /* Always keep the BLOCK node associated with the outermost pair of
1047      curley braces of a function.  These are needed for correct
1048      operation of dwarfout.c.  */
1049   keep_next_level (1);
1050 }
1051
1052 /* Begin a new scope.  */
1053
1054 static void
1055 do_pushlevel ()
1056 {
1057   if (!building_stmt_tree ())
1058     {
1059       emit_line_note (input_filename, lineno);
1060       clear_last_expr ();
1061     }
1062   push_momentary ();
1063   if (stmts_are_full_exprs_p)
1064     pushlevel (0);
1065   if (!building_stmt_tree () && stmts_are_full_exprs_p)
1066     expand_start_bindings (0);
1067 }
1068
1069 /* Finish a scope.  */
1070
1071 static tree
1072 do_poplevel ()
1073 {
1074   tree t;
1075
1076   if (!building_stmt_tree () && stmts_are_full_exprs_p)
1077     expand_end_bindings (getdecls (), kept_level_p (), 0);
1078   if (stmts_are_full_exprs_p)
1079     t = poplevel (kept_level_p (), 1, 0);
1080   else
1081     t = NULL_TREE;
1082   pop_momentary ();
1083   return t;
1084 }
1085
1086 /* Finish a parenthesized expression EXPR.  */
1087
1088 tree
1089 finish_parenthesized_expr (expr)
1090      tree expr;
1091 {
1092   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1093     /* This inhibits warnings in truthvalue_conversion.  */
1094     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 
1095
1096   return expr;
1097 }
1098
1099 /* Begin a statement-expression.  The value returned must be passed to
1100    finish_stmt_expr.  */
1101
1102 tree 
1103 begin_stmt_expr ()
1104 {
1105   keep_next_level (1);
1106   /* If we're building a statement tree, then the upcoming compound
1107      statement will be chained onto the tree structure, starting at
1108      last_tree.  We return last_tree so that we can later unhook the
1109      compound statement.  */
1110   return building_stmt_tree () ? last_tree : expand_start_stmt_expr(); 
1111 }
1112
1113 /* Finish a statement-expression.  RTL_EXPR should be the value
1114    returned by the previous begin_stmt_expr; EXPR is the
1115    statement-expression.  Returns an expression representing the
1116    statement-expression.  */
1117
1118 tree 
1119 finish_stmt_expr (rtl_expr, expr)
1120      tree rtl_expr;
1121      tree expr;
1122 {
1123   tree result;
1124
1125   if (!building_stmt_tree ())
1126     {
1127       rtl_expr = expand_end_stmt_expr (rtl_expr);
1128       /* The statements have side effects, so the group does.  */
1129       TREE_SIDE_EFFECTS (rtl_expr) = 1;
1130     }
1131
1132   if (building_stmt_tree ())
1133     {
1134       /* If the last thing in the statement-expression was not an
1135          expression-statement, then it has type `void'.  */
1136       if (!last_expr_type)
1137         last_expr_type = void_type_node;
1138       result = build_min (STMT_EXPR, last_expr_type, last_tree);
1139       TREE_SIDE_EFFECTS (result) = 1;
1140       
1141       /* Remove the compound statement from the tree structure; it is
1142          now saved in the STMT_EXPR.  */
1143       last_tree = rtl_expr;
1144       TREE_CHAIN (last_tree) = NULL_TREE;
1145     }
1146   else if (expr && TREE_CODE (expr) == BLOCK)
1147     {
1148       result = build (BIND_EXPR, TREE_TYPE (rtl_expr),
1149                       NULL_TREE, rtl_expr, expr);
1150       delete_block (expr);
1151     }
1152   else
1153     result = rtl_expr;
1154
1155   if (expr && TREE_CODE (expr) == BLOCK)
1156     /* Remove the block from the tree at this point.  It gets put back
1157        at the proper place when the STMT_EXPR or BIND_EXPR is
1158        expanded.  */
1159     delete_block (expr);
1160
1161   return result;
1162 }
1163
1164 /* Finish a call to FN with ARGS.  Returns a representation of the
1165    call.  */
1166
1167 tree 
1168 finish_call_expr (fn, args, koenig)
1169      tree fn;
1170      tree args;
1171      int koenig;
1172 {
1173   tree result;
1174
1175   if (koenig)
1176     {
1177       if (TREE_CODE (fn) == BIT_NOT_EXPR)
1178         fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1179       else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1180         fn = do_identifier (fn, 2, args);
1181     }
1182   result = build_x_function_call (fn, args, current_class_ref);
1183
1184   if (TREE_CODE (result) == CALL_EXPR
1185       && (! TREE_TYPE (result)
1186           || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1187     result = require_complete_type (result);
1188
1189   return result;
1190 }
1191
1192 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1193    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1194    POSTDECREMENT_EXPR.)  */
1195
1196 tree 
1197 finish_increment_expr (expr, code)
1198      tree expr;
1199      enum tree_code code;
1200 {
1201   /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1202      a COMPONENT_REF).  This way if we've got, say, a reference to a
1203      static member that's being operated on, we don't end up trying to
1204      find a member operator for the class it's in.  */
1205
1206   if (TREE_CODE (expr) == OFFSET_REF)
1207     expr = resolve_offset_ref (expr);
1208   return build_x_unary_op (code, expr);  
1209 }
1210
1211 /* Finish a use of `this'.  Returns an expression for `this'.  */
1212
1213 tree 
1214 finish_this_expr ()
1215 {
1216   tree result;
1217
1218   if (current_class_ptr)
1219     {
1220 #ifdef WARNING_ABOUT_CCD
1221       TREE_USED (current_class_ptr) = 1;
1222 #endif
1223       result = current_class_ptr;
1224     }
1225   else if (current_function_decl
1226            && DECL_STATIC_FUNCTION_P (current_function_decl))
1227     {
1228       error ("`this' is unavailable for static member functions");
1229       result = error_mark_node;
1230     }
1231   else
1232     {
1233       if (current_function_decl)
1234         error ("invalid use of `this' in non-member function");
1235       else
1236         error ("invalid use of `this' at top level");
1237       result = error_mark_node;
1238     }
1239
1240   return result;
1241 }
1242
1243 /* Finish a member function call using OBJECT and ARGS as arguments to
1244    FN.  Returns an expression for the call.  */
1245
1246 tree 
1247 finish_object_call_expr (fn, object, args)
1248      tree fn;
1249      tree object;
1250      tree args;
1251 {
1252 #if 0
1253   /* This is a future direction of this code, but because
1254      build_x_function_call cannot always undo what is done in
1255      build_component_ref entirely yet, we cannot do this.  */
1256
1257   tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1258   return finish_call_expr (real_fn, args);
1259 #else
1260   if (TREE_CODE (fn) == TYPE_DECL)
1261     {
1262       if (processing_template_decl)
1263         /* This can happen on code like:
1264
1265            class X;
1266            template <class T> void f(T t) {
1267              t.X();
1268            }  
1269
1270            We just grab the underlying IDENTIFIER.  */
1271         fn = DECL_NAME (fn);
1272       else
1273         {
1274           cp_error ("calling type `%T' like a method", fn);
1275           return error_mark_node;
1276         }
1277     }
1278
1279   return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1280 #endif
1281 }
1282
1283 /* Finish a qualified member function call using OBJECT and ARGS as
1284    arguments to FN.  Returns an expressino for the call.  */
1285
1286 tree 
1287 finish_qualified_object_call_expr (fn, object, args)
1288      tree fn;
1289      tree object;
1290      tree args;
1291 {
1292   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1293                                    TREE_OPERAND (fn, 1), args);
1294 }
1295
1296 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1297    being the scope, if any, of DESTRUCTOR.  Returns an expression for
1298    the call.  */
1299
1300 tree 
1301 finish_pseudo_destructor_call_expr (object, scope, destructor)
1302      tree object;
1303      tree scope;
1304      tree destructor;
1305 {
1306   if (processing_template_decl)
1307     return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1308
1309   if (scope && scope != destructor)
1310     cp_error ("destructor specifier `%T::~%T()' must have matching names", 
1311               scope, destructor);
1312
1313   if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1314       && (TREE_CODE (TREE_TYPE (object)) !=
1315           TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1316     cp_error ("`%E' is not of type `%T'", object, destructor);
1317
1318   return cp_convert (void_type_node, object);
1319 }
1320
1321 /* Finish a call to a globally qualified member function FN using
1322    ARGS.  Returns an expression for the call.  */
1323
1324 tree 
1325 finish_qualified_call_expr (fn, args)
1326      tree fn;
1327      tree args;
1328 {
1329   if (processing_template_decl)
1330     return build_min_nt (CALL_EXPR, copy_to_permanent (fn), args,
1331                          NULL_TREE);
1332   else
1333     return build_member_call (TREE_OPERAND (fn, 0),
1334                               TREE_OPERAND (fn, 1),
1335                               args);
1336 }
1337
1338 /* Finish an expression taking the address of LABEL.  Returns an
1339    expression for the address.  */
1340
1341 tree 
1342 finish_label_address_expr (label)
1343      tree label;
1344 {
1345   tree result;
1346
1347   label = lookup_label (label);
1348   if (label == NULL_TREE)
1349     result = null_pointer_node;
1350   else
1351     {
1352       TREE_USED (label) = 1;
1353       result = build1 (ADDR_EXPR, ptr_type_node, label);
1354       TREE_CONSTANT (result) = 1;
1355     }
1356
1357   return result;
1358 }
1359
1360 /* Finish an expression of the form CODE EXPR.  */
1361
1362 tree
1363 finish_unary_op_expr (code, expr)
1364      enum tree_code code;
1365      tree expr;
1366 {
1367   tree result = build_x_unary_op (code, expr);
1368   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1369     TREE_NEGATED_INT (result) = 1;
1370   overflow_warning (result);
1371   return result;
1372 }
1373
1374 /* Finish an id-expression.  */
1375
1376 tree
1377 finish_id_expr (expr)
1378      tree expr;
1379 {
1380   if (TREE_CODE (expr) == IDENTIFIER_NODE)
1381     expr = do_identifier (expr, 1, NULL_TREE);
1382
1383   return expr;
1384 }
1385
1386 /* Begin a new-placement.  */
1387
1388 int
1389 begin_new_placement ()
1390 {
1391   /* The arguments to a placement new might be passed to a
1392      deallocation function, in the event that the allocation throws an
1393      exception.  Since we don't expand exception handlers until the
1394      end of a function, we must make sure the arguments stay around
1395      that long.  */
1396   return suspend_momentary ();
1397 }
1398
1399 /* Finish a new-placement.  The ARGS are the placement arguments.  The
1400    COOKIE is the value returned by the previous call to
1401    begin_new_placement.  */
1402
1403 tree
1404 finish_new_placement (args, cookie)
1405      tree args;
1406      int cookie;
1407 {
1408   resume_momentary (cookie);
1409   return args;
1410 }
1411
1412 /* Begin a function defniition declared with DECL_SPECS and
1413    DECLARATOR.  Returns non-zero if the function-declaration is
1414    legal.  */
1415
1416 int
1417 begin_function_definition (decl_specs, declarator)
1418      tree decl_specs;
1419      tree declarator;
1420 {
1421   tree specs;
1422   tree attrs;
1423   split_specs_attrs (decl_specs, &specs, &attrs);
1424   if (!start_function (specs, declarator, attrs, 0))
1425     return 0;
1426   
1427   reinit_parse_for_function ();
1428   /* The things we're about to see are not directly qualified by any
1429      template headers we've seen thus far.  */
1430   reset_specialization ();
1431
1432   return 1;
1433 }
1434
1435 /* Begin a constructor declarator of the form `SCOPE::NAME'.  Returns
1436    a SCOPE_REF.  */
1437
1438 tree 
1439 begin_constructor_declarator (scope, name)
1440      tree scope;
1441      tree name;
1442 {
1443   tree result = build_parse_node (SCOPE_REF, scope, name);
1444   enter_scope_of (result);
1445   return result;
1446 }
1447
1448 /* Finish an init-declarator.  Returns a DECL.  */
1449
1450 tree
1451 finish_declarator (declarator, declspecs, attributes,
1452                    prefix_attributes, initialized)
1453      tree declarator;
1454      tree declspecs;
1455      tree attributes;
1456      tree prefix_attributes;
1457      int initialized;
1458 {
1459   return start_decl (declarator, declspecs, initialized, attributes,
1460                      prefix_attributes); 
1461 }
1462
1463 /* Finish a translation unit.  */
1464
1465 void 
1466 finish_translation_unit ()
1467 {
1468   /* In case there were missing closebraces,
1469      get us back to the global binding level.  */
1470   while (! toplevel_bindings_p ())
1471     poplevel (0, 0, 0);
1472   while (current_namespace != global_namespace)
1473     pop_namespace ();
1474   finish_file ();
1475 }
1476
1477 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1478    Returns the parameter.  */
1479
1480 tree 
1481 finish_template_type_parm (aggr, identifier)
1482      tree aggr;
1483      tree identifier;
1484 {
1485   if (aggr != class_type_node)
1486     {
1487       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1488       aggr = class_type_node;
1489     }
1490
1491   return build_tree_list (aggr, identifier);
1492 }
1493
1494 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1495    Returns the parameter.  */
1496
1497 tree 
1498 finish_template_template_parm (aggr, identifier)
1499      tree aggr;
1500      tree identifier;
1501 {
1502   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1503   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1504   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1505   DECL_TEMPLATE_RESULT (tmpl) = decl;
1506   SET_DECL_ARTIFICIAL (decl);
1507   end_template_decl ();
1508
1509   return finish_template_type_parm (aggr, tmpl);
1510 }
1511
1512 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1513    non-zero, the parameter list was terminated by a `...'.  */
1514
1515 tree
1516 finish_parmlist (parms, ellipsis)
1517      tree parms;
1518      int ellipsis;
1519 {
1520   if (!ellipsis)
1521     chainon (parms, void_list_node);
1522   /* We mark the PARMS as a parmlist so that declarator processing can
1523      disambiguate certain constructs.  */
1524   if (parms != NULL_TREE)
1525     TREE_PARMLIST (parms) = 1;
1526
1527   return parms;
1528 }
1529
1530 /* Begin a class definition, as indicated by T.  */
1531
1532 tree
1533 begin_class_definition (t)
1534      tree t;
1535 {
1536   push_permanent_obstack ();
1537
1538   if (t == error_mark_node
1539       || ! IS_AGGR_TYPE (t))
1540     {
1541       t = make_lang_type (RECORD_TYPE);
1542       pushtag (make_anon_name (), t, 0);
1543     }
1544
1545   /* In a definition of a member class template, we will get here with an
1546      implicit typename, a TYPENAME_TYPE with a type.  */
1547   if (TREE_CODE (t) == TYPENAME_TYPE)
1548     t = TREE_TYPE (t);
1549   
1550   /* If we generated a partial instantiation of this type, but now
1551      we're seeing a real definition, we're actually looking at a
1552      partial specialization.  Consider:
1553
1554        template <class T, class U>
1555        struct Y {};
1556
1557        template <class T>
1558        struct X {};
1559
1560        template <class T, class U>
1561        void f()
1562        {
1563          typename X<Y<T, U> >::A a;
1564        }
1565
1566        template <class T, class U>
1567        struct X<Y<T, U> >
1568        {
1569        };
1570
1571      We have to undo the effects of the previous partial
1572      instantiation.  */
1573   if (PARTIAL_INSTANTIATION_P (t))
1574     {
1575       if (!pedantic) 
1576         {
1577           /* Unfortunately, when we're not in pedantic mode, we
1578              attempt to actually fill in some of the fields of the
1579              partial instantiation, in order to support the implicit
1580              typename extension.  Clear those fields now, in
1581              preparation for the definition here.  The fields cleared
1582              here must match those set in instantiate_class_template.
1583              Look for a comment mentioning begin_class_definition
1584              there.  */
1585           TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1586           TYPE_FIELDS (t) = NULL_TREE;
1587           TYPE_METHODS (t) = NULL_TREE;
1588           CLASSTYPE_TAGS (t) = NULL_TREE;
1589           TYPE_SIZE (t) = NULL_TREE;
1590         }
1591
1592       /* This isn't a partial instantiation any more.  */
1593       PARTIAL_INSTANTIATION_P (t) = 0;
1594     }
1595   /* If this type was already complete, and we see another definition,
1596      that's an error.  */
1597   else if (TYPE_SIZE (t))
1598     duplicate_tag_error (t);
1599
1600   /* Update the location of the decl.  */
1601   DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1602   DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1603   
1604   if (TYPE_BEING_DEFINED (t))
1605     {
1606       t = make_lang_type (TREE_CODE (t));
1607       pushtag (TYPE_IDENTIFIER (t), t, 0);
1608     }
1609   maybe_process_partial_specialization (t);
1610   pushclass (t, 1);
1611   TYPE_BEING_DEFINED (t) = 1;
1612   /* Reset the interface data, at the earliest possible
1613      moment, as it might have been set via a class foo;
1614      before.  */
1615   {
1616     tree name = TYPE_IDENTIFIER (t);
1617     
1618     if (! ANON_AGGRNAME_P (name))
1619       {
1620         CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1621         SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1622           (t, interface_unknown);
1623       }
1624     
1625     /* Only leave this bit clear if we know this
1626        class is part of an interface-only specification.  */
1627     if (! CLASSTYPE_INTERFACE_KNOWN (t)
1628         || ! CLASSTYPE_INTERFACE_ONLY (t))
1629       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1630   }
1631 #if 0
1632   tmp = TYPE_IDENTIFIER ($<ttype>0);
1633   if (tmp && IDENTIFIER_TEMPLATE (tmp))
1634     overload_template_name (tmp, 1);
1635 #endif
1636   reset_specialization();
1637   
1638   /* In case this is a local class within a template
1639      function, we save the current tree structure so
1640      that we can get it back later.  */
1641   begin_tree ();
1642
1643   /* Make a declaration for this class in its own scope.  */
1644   build_self_reference ();
1645
1646   return t;
1647 }
1648
1649 /* Finish the member declaration given by DECL.  */
1650
1651 void
1652 finish_member_declaration (decl)
1653      tree decl;
1654 {
1655   if (decl == error_mark_node || decl == NULL_TREE)
1656     return;
1657
1658   if (decl == void_type_node)
1659     /* The COMPONENT was a friend, not a member, and so there's
1660        nothing for us to do.  */
1661     return;
1662
1663   /* We should see only one DECL at a time.  */
1664   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1665
1666   /* Set up access control for DECL.  */
1667   TREE_PRIVATE (decl) 
1668     = (current_access_specifier == access_private_node);
1669   TREE_PROTECTED (decl) 
1670     = (current_access_specifier == access_protected_node);
1671   if (TREE_CODE (decl) == TEMPLATE_DECL)
1672     {
1673       TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1674       TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1675     }
1676
1677   /* Mark the DECL as a member of the current class.  */
1678   if (TREE_CODE (decl) == FUNCTION_DECL 
1679       || DECL_FUNCTION_TEMPLATE_P (decl))
1680     /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1681        finish_struct.  Presumably it is already set as the function is
1682        parsed.  Perhaps DECL_CLASS_CONTEXT is already set, too?  */
1683     DECL_CLASS_CONTEXT (decl) = current_class_type;
1684   else
1685     DECL_CONTEXT (decl) = current_class_type;
1686
1687   /* Put functions on the TYPE_METHODS list and everything else on the
1688      TYPE_FIELDS list.  Note that these are built up in reverse order.
1689      We reverse them (to obtain declaration order) in finish_struct.  */
1690   if (TREE_CODE (decl) == FUNCTION_DECL 
1691       || DECL_FUNCTION_TEMPLATE_P (decl))
1692     {
1693       /* We also need to add this function to the
1694          CLASSTYPE_METHOD_VEC.  */
1695       add_method (current_class_type, 0, decl);
1696
1697       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1698       TYPE_METHODS (current_class_type) = decl;
1699     }
1700   else
1701     {
1702       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
1703          go at the beginning.  The reason is that lookup_field_1
1704          searches the list in order, and we want a field name to
1705          override a type name so that the "struct stat hack" will
1706          work.  In particular:
1707
1708            struct S { enum E { }; int E } s;
1709            s.E = 3;
1710
1711          is legal.  In addition, the FIELD_DECLs must be maintained in
1712          declaration order so that class layout works as expected.
1713          However, we don't need that order until class layout, so we
1714          save a little time by putting FIELD_DECLs on in reverse order
1715          here, and then reversing them in finish_struct_1.  (We could
1716          also keep a pointer to the correct insertion points in the
1717          list.)  */
1718
1719       if (TREE_CODE (decl) == TYPE_DECL)
1720         TYPE_FIELDS (current_class_type) 
1721           = chainon (TYPE_FIELDS (current_class_type), decl);
1722       else
1723         {
1724           TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1725           TYPE_FIELDS (current_class_type) = decl;
1726         }
1727
1728       /* Enter the DECL into the scope of the class.  */
1729       if (TREE_CODE (decl) != USING_DECL)
1730         pushdecl_class_level (decl);
1731     }
1732 }
1733
1734 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
1735    the definition is immediately followed by a semicolon.  Returns the
1736    type.  */
1737
1738 tree
1739 finish_class_definition (t, attributes, semi, pop_scope_p)
1740      tree t;
1741      tree attributes;
1742      int semi;
1743      int pop_scope_p;
1744 {
1745   /* finish_struct nukes this anyway; if finish_exception does too,
1746      then it can go.  */
1747   if (semi)
1748     note_got_semicolon (t);
1749
1750   /* If we got any attributes in class_head, xref_tag will stick them in
1751      TREE_TYPE of the type.  Grab them now.  */
1752   attributes = chainon (TREE_TYPE (t), attributes);
1753   TREE_TYPE (t) = NULL_TREE;
1754
1755   if (TREE_CODE (t) == ENUMERAL_TYPE)
1756     ;
1757   else
1758     {
1759       t = finish_struct (t, attributes);
1760       if (semi) 
1761         note_got_semicolon (t);
1762     }
1763
1764   pop_obstacks ();
1765
1766   if (! semi)
1767     check_for_missing_semicolon (t); 
1768   if (pop_scope_p)
1769     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1770   if (current_scope () == current_function_decl)
1771     do_pending_defargs ();
1772
1773   return t;
1774 }
1775
1776 /* Finish processing the default argument expressions cached during
1777    the processing of a class definition.  */
1778
1779 void
1780 begin_inline_definitions ()
1781 {
1782   if (pending_inlines 
1783       && current_scope () == current_function_decl)
1784     do_pending_inlines ();
1785 }
1786
1787 /* Finish processing the inline function definitions cached during the
1788    processing of a class definition.  */
1789
1790 void
1791 finish_inline_definitions ()
1792 {
1793   if (current_class_type == NULL_TREE)
1794     clear_inline_text_obstack (); 
1795   
1796   /* Undo the begin_tree in begin_class_definition.  */
1797   end_tree ();
1798 }
1799
1800 /* Finish processing the declaration of a member class template
1801    TYPES whose template parameters are given by PARMS.  */
1802
1803 tree
1804 finish_member_class_template (types)
1805      tree types;
1806 {
1807   tree t;
1808
1809   /* If there are declared, but undefined, partial specializations
1810      mixed in with the typespecs they will not yet have passed through
1811      maybe_process_partial_specialization, so we do that here.  */
1812   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1813     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1814       maybe_process_partial_specialization (TREE_VALUE (t));
1815
1816   note_list_got_semicolon (types);
1817   grok_x_components (types);
1818   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1819     /* The component was in fact a friend declaration.  We avoid
1820        finish_member_template_decl performing certain checks by
1821        unsetting TYPES.  */
1822     types = NULL_TREE;
1823   
1824   finish_member_template_decl (types);
1825
1826   /* As with other component type declarations, we do
1827      not store the new DECL on the list of
1828      component_decls.  */
1829   return NULL_TREE;
1830 }
1831
1832 /* Finish processsing a complete template declaration.  The PARMS are
1833    the template parameters.  */
1834
1835 void
1836 finish_template_decl (parms)
1837      tree parms;
1838 {
1839   if (parms)
1840     end_template_decl ();
1841   else
1842     end_specialization ();
1843 }
1844
1845 /* Finish processing a a template-id (which names a type) of the form
1846    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
1847    template-id.  If ENTERING_SCOPE is non-zero we are about to enter
1848    the scope of template-id indicated.  */
1849
1850 tree
1851 finish_template_type (name, args, entering_scope)
1852      tree name;
1853      tree args;
1854      int entering_scope;
1855 {
1856   tree decl;
1857
1858   decl = lookup_template_class (name, args,
1859                                 NULL_TREE, NULL_TREE, entering_scope);
1860   if (decl != error_mark_node)
1861     decl = TYPE_STUB_DECL (decl);
1862
1863   return decl;
1864 }
1865
1866 /* SR is a SCOPE_REF node.  Enter the scope of SR, whether it is a
1867    namespace scope or a class scope.  */
1868
1869 void
1870 enter_scope_of (sr)
1871      tree sr;
1872 {
1873   tree scope = TREE_OPERAND (sr, 0);
1874
1875   if (TREE_CODE (scope) == NAMESPACE_DECL)
1876     {
1877       push_decl_namespace (scope);
1878       TREE_COMPLEXITY (sr) = -1;
1879     }
1880   else if (scope != current_class_type)
1881     {
1882       if (TREE_CODE (scope) == TYPENAME_TYPE)
1883         {
1884           /* In a declarator for a template class member, the scope will
1885              get here as an implicit typename, a TYPENAME_TYPE with a type.  */
1886           scope = TREE_TYPE (scope);
1887           TREE_OPERAND (sr, 0) = scope;
1888         }
1889       push_nested_class (scope, 3);
1890       TREE_COMPLEXITY (sr) = current_class_depth;
1891     }
1892 }
1893
1894 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
1895    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
1896    BASE_CLASS, or NULL_TREE if an error occurred.  The
1897    ACCESSS_SPECIFIER is one of
1898    access_{default,public,protected_private}[_virtual]_node.*/
1899
1900 tree 
1901 finish_base_specifier (access_specifier, base_class)
1902      tree access_specifier;
1903      tree base_class;
1904 {
1905   tree type;
1906   tree result;
1907
1908   if (base_class == NULL_TREE)
1909     {
1910       error ("invalid base class");
1911       type = error_mark_node;
1912     }
1913   else
1914     type = TREE_TYPE (base_class);
1915
1916   if (! is_aggr_type (type, 1))
1917     result = NULL_TREE;
1918   else
1919     result = build_tree_list (access_specifier, type);
1920
1921   return result;
1922 }
1923
1924 /* Called when multiple declarators are processed.  If that is not
1925    premitted in this context, an error is issued.  */
1926
1927 void
1928 check_multiple_declarators ()
1929 {
1930   /* [temp]
1931      
1932      In a template-declaration, explicit specialization, or explicit
1933      instantiation the init-declarator-list in the declaration shall
1934      contain at most one declarator.  
1935
1936      We don't just use PROCESSING_TEMPLATE_DECL for the first
1937      condition since that would disallow the perfectly legal code, 
1938      like `template <class T> struct S { int i, j; };'.  */
1939   tree scope = current_scope ();
1940
1941   if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1942     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
1943     return;
1944      
1945   if (PROCESSING_REAL_TEMPLATE_DECL_P () 
1946       || processing_explicit_instantiation
1947       || processing_specialization)
1948     cp_error ("multiple declarators in template declaration");
1949 }
1950
1951 tree
1952 finish_typeof (expr)
1953      tree expr;
1954 {
1955   if (processing_template_decl)
1956     {
1957       tree t;
1958
1959       push_permanent_obstack ();
1960       t = make_lang_type (TYPEOF_TYPE);
1961       TYPE_FIELDS (t) = expr;
1962       pop_obstacks ();
1963
1964       return t;
1965     }
1966
1967   return TREE_TYPE (expr);
1968 }
1969
1970 /* Create an empty statement tree for FN.  */
1971
1972 void
1973 begin_stmt_tree (fn)
1974      tree fn;
1975 {
1976   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
1977      what follows.  We remove the extraneous statement in
1978      finish_stmt_tree.  */
1979   DECL_SAVED_TREE (fn) = build_nt (EXPR_STMT, void_zero_node);
1980   last_tree = DECL_SAVED_TREE (fn);
1981   last_expr_type = NULL_TREE;
1982 }
1983
1984 /* Finish the statement tree for FN.  */
1985
1986 void
1987 finish_stmt_tree (fn)
1988      tree fn;
1989 {
1990   DECL_SAVED_TREE (fn) = TREE_CHAIN (DECL_SAVED_TREE (fn));
1991 }
1992
1993 /* Generate RTL for the chain of statements T.  */
1994
1995 static void 
1996 expand_stmts (t)
1997      tree t;
1998 {
1999   while (t)
2000     {
2001       expand_stmt (t);
2002       t = TREE_CHAIN (t);
2003     }
2004 }
2005
2006 /* Generate RTL for the statement T, and its substatements.  */
2007
2008 tree
2009 expand_stmt (t)
2010      tree t;
2011 {
2012   if (t == NULL_TREE || t == error_mark_node)
2013     return NULL_TREE;
2014
2015   switch (TREE_CODE (t))
2016     {
2017     case RETURN_STMT:
2018       lineno = STMT_LINENO (t);
2019       finish_return_stmt (RETURN_EXPR (t));
2020       break;
2021
2022     case EXPR_STMT:
2023       lineno = STMT_LINENO (t);
2024       finish_expr_stmt (EXPR_STMT_EXPR (t));
2025       break;
2026
2027     case DECL_STMT:
2028       {
2029         tree decl;
2030         int i = suspend_momentary ();
2031
2032         lineno = STMT_LINENO (t);
2033         emit_line_note (input_filename, lineno);
2034         decl = DECL_STMT_DECL (t);
2035         if (TREE_CODE (decl) == LABEL_DECL)
2036           finish_label_decl (DECL_NAME (decl));
2037         else
2038           {
2039             /* We need to clear DECL_CONTEXT so that maybe_push_decl
2040                will push it into the current scope.  */
2041             if (DECL_CONTEXT (decl) == current_function_decl)
2042               DECL_CONTEXT (decl) = NULL_TREE;
2043             /* If we marked this variable as dead when we processed it
2044                before, we must undo that now.  The variable has been
2045                resuscitated.  */
2046             if (TREE_CODE (decl) == VAR_DECL)
2047               DECL_DEAD_FOR_LOCAL (decl) = 0;
2048             maybe_push_decl (decl);
2049             cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2050           }
2051         resume_momentary (i);
2052       }
2053       break;
2054
2055     case FOR_STMT:
2056       {
2057         tree tmp;
2058
2059         lineno = STMT_LINENO (t);
2060         begin_for_stmt ();
2061         for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
2062           expand_stmt (tmp);
2063         finish_for_init_stmt (NULL_TREE);
2064         finish_for_cond (FOR_COND (t), NULL_TREE);
2065         tmp = FOR_EXPR (t);
2066         finish_for_expr (tmp, NULL_TREE);
2067         expand_stmt (FOR_BODY (t));
2068         finish_for_stmt (tmp, NULL_TREE);
2069       }
2070       break;
2071
2072     case WHILE_STMT:
2073       {
2074         lineno = STMT_LINENO (t);
2075         begin_while_stmt ();
2076         finish_while_stmt_cond (WHILE_COND (t), NULL_TREE);
2077         expand_stmt (WHILE_BODY (t));
2078         finish_while_stmt (NULL_TREE);
2079       }
2080       break;
2081
2082     case DO_STMT:
2083       {
2084         lineno = STMT_LINENO (t);
2085         begin_do_stmt ();
2086         expand_stmt (DO_BODY (t));
2087         finish_do_body (NULL_TREE);
2088         finish_do_stmt (DO_COND (t), NULL_TREE);
2089       }
2090       break;
2091
2092     case IF_STMT:
2093       lineno = STMT_LINENO (t);
2094       begin_if_stmt ();
2095       finish_if_stmt_cond (IF_COND (t), NULL_TREE);
2096       if (THEN_CLAUSE (t))
2097         {
2098           expand_stmt (THEN_CLAUSE (t));
2099           finish_then_clause (NULL_TREE);
2100         }
2101       if (ELSE_CLAUSE (t))
2102         {
2103           begin_else_clause ();
2104           expand_stmt (ELSE_CLAUSE (t));
2105           finish_else_clause (NULL_TREE);
2106         }
2107       finish_if_stmt ();
2108       break;
2109
2110     case COMPOUND_STMT:
2111       lineno = STMT_LINENO (t);
2112       begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2113       expand_stmts (COMPOUND_BODY (t));
2114       return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
2115                                    NULL_TREE);
2116
2117     case BREAK_STMT:
2118       lineno = STMT_LINENO (t);
2119       finish_break_stmt ();
2120       break;
2121
2122     case CONTINUE_STMT:
2123       lineno = STMT_LINENO (t);
2124       finish_continue_stmt ();
2125       break;
2126
2127     case SWITCH_STMT:
2128       lineno = STMT_LINENO (t);
2129       begin_switch_stmt ();
2130       finish_switch_cond (SWITCH_COND (t));
2131       if (TREE_OPERAND (t, 1))
2132         expand_stmt (SWITCH_BODY (t));
2133       finish_switch_stmt (SWITCH_COND (t), NULL_TREE);
2134       break;
2135
2136     case CASE_LABEL:
2137       finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2138       break;
2139
2140     case LABEL_STMT:
2141       lineno = STMT_LINENO (t);
2142       finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
2143       break;
2144
2145     case GOTO_STMT:
2146       lineno = STMT_LINENO (t);
2147       if (TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL)
2148         finish_goto_stmt (DECL_NAME (GOTO_DESTINATION (t)));
2149       else
2150         finish_goto_stmt (GOTO_DESTINATION (t));
2151       break;
2152
2153     case ASM_STMT:
2154       lineno = STMT_LINENO (t);
2155       finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2156                        (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2157       break;
2158
2159     case TRY_BLOCK:
2160       lineno = STMT_LINENO (t);
2161       if (CLEANUP_P (t))
2162         {
2163           expand_eh_region_start ();
2164           expand_stmt (TRY_STMTS (t));
2165           finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2166         }
2167       else
2168         {
2169           begin_try_block ();
2170           expand_stmt (TRY_STMTS (t));
2171           finish_try_block (NULL_TREE);
2172           expand_stmts (TRY_HANDLERS (t));
2173           finish_handler_sequence (NULL_TREE);
2174         }
2175       break;
2176
2177     case HANDLER:
2178       lineno = STMT_LINENO (t);
2179       begin_handler ();
2180       if (HANDLER_PARMS (t))
2181         {
2182           tree d = HANDLER_PARMS (t);
2183           expand_start_catch_block (TREE_OPERAND (d, 1),
2184                                     TREE_OPERAND (d, 0));
2185         }
2186       else
2187         expand_start_catch_block (NULL_TREE, NULL_TREE);
2188       finish_handler_parms (NULL_TREE);
2189       expand_stmt (HANDLER_BODY (t));
2190       finish_handler (NULL_TREE);
2191       break;
2192
2193     case SUBOBJECT:
2194       lineno = STMT_LINENO (t);
2195       finish_subobject (SUBOBJECT_CLEANUP (t));
2196       break;
2197
2198     default:
2199       my_friendly_abort (19990810);
2200       break;
2201     }
2202
2203   return NULL_TREE;
2204 }
2205
2206 /* Generate RTL for FN.  */
2207
2208 void
2209 expand_body (fn)
2210      tree fn;
2211 {
2212   int saved_expanding_p;
2213   int nested; 
2214   tree t;
2215   tree try_block;
2216
2217   /* Remember whether we're already processing a function definition
2218      so that we can tell finish_function.  */
2219   nested = in_function_p ();
2220
2221   /* Let the compiler know that now is the time to really generate
2222      actualy RTL.  */
2223   saved_expanding_p = expanding_p;
2224   expanding_p = 1;
2225
2226   start_function (NULL_TREE, fn, NULL_TREE, 1);
2227   store_parm_decls ();
2228
2229   /* There are a few things that we do not handle recursively.  For
2230      example, a function try-block is handled differently from an
2231      ordinary try-block, so we must handle it here.  */
2232   t = DECL_SAVED_TREE (fn);
2233   try_block = NULL_TREE;
2234   if (t && TREE_CODE (t) == TRY_BLOCK)
2235     {
2236       try_block = t;
2237       begin_function_try_block ();
2238       t = TRY_STMTS (try_block);
2239     }
2240
2241   if (t && TREE_CODE (t) == RETURN_INIT)
2242     {
2243       /* Clear this out so that finish_named_return_value can set it
2244          again.  */
2245       DECL_NAME (DECL_RESULT (fn)) = NULL_TREE;
2246       finish_named_return_value (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2247       t = TREE_CHAIN (t);
2248     }
2249
2250   if (t && TREE_CODE (t) == CTOR_INITIALIZER)
2251     {
2252       current_member_init_list = TREE_OPERAND (t, 0);
2253       current_base_init_list = TREE_OPERAND (t, 1);
2254       t = TREE_CHAIN (t);
2255     }
2256
2257   /* If this is a constructor, we need to initialize our members and
2258      base-classes.  */
2259   setup_vtbl_ptr ();
2260
2261   /* Expand the body.  */
2262   expand_stmt (t);
2263
2264   /* If there was a function try-block, expand the handlers.  */
2265   if (try_block)
2266     {
2267       finish_function_try_block (NULL_TREE);
2268       {
2269         tree handler = TRY_HANDLERS (try_block);
2270         for (; handler; handler = TREE_CHAIN (handler))
2271           expand_stmt (handler);
2272       }
2273       finish_function_handler_sequence (NULL_TREE);
2274     }
2275
2276   finish_function (lineno, 0, nested);
2277
2278   /* Restore EXPANDING_P.  */
2279   expanding_p = saved_expanding_p;
2280 }