* friend.c (make_friend_class): Robustify.
[official-gcc.git] / gcc / cp / semantics.c
blobda17d3b3a01d814cc8bc6bbaab15a6e14884fa07
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.
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.
10 This file is part of GNU CC.
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.
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.
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. */
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 #include "flags.h"
36 /* There routines provide a modular interface to perform many parsing
37 operations. They may therefore be used during actual parsing, or
38 during template instantiation, which may be regarded as a
39 degenerate form of parsing. Since the current g++ parser is
40 lacking in several respects, and will be reimplemented, we are
41 attempting to move most code that is not directly related to
42 parsing into this file; that will make implementing the new parser
43 much easier since it will be able to make use of these routines. */
45 static tree expand_cond PROTO((tree));
46 static tree maybe_convert_cond PROTO((tree));
48 /* Record the fact that STMT was the last statement added to the
49 statement tree. */
51 #define SET_LAST_STMT(stmt) \
52 (current_stmt_tree->x_last_stmt = (stmt))
54 /* When parsing a template, LAST_TREE contains the last statement
55 parsed. These are chained together through the TREE_CHAIN field,
56 but often need to be re-organized since the parse is performed
57 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
58 STMT. */
60 #define RECHAIN_STMTS(stmt, substmt) \
61 do { \
62 substmt = TREE_CHAIN (stmt); \
63 TREE_CHAIN (stmt) = NULL_TREE; \
64 SET_LAST_STMT (stmt); \
65 } while (0)
67 /* Finish processing the COND, the SUBSTMT condition for STMT. */
69 #define FINISH_COND(cond, stmt, substmt) \
70 do { \
71 if (last_tree != stmt) \
72 { \
73 RECHAIN_STMTS (stmt, substmt); \
74 if (!processing_template_decl) \
75 { \
76 cond = build_tree_list (substmt, cond); \
77 substmt = cond; \
78 } \
79 } \
80 else \
81 substmt = cond; \
82 } while (0)
84 /* T is a statement. Add it to the statement-tree. */
86 void
87 add_tree (t)
88 tree t;
90 /* Add T to the statement-tree. */
91 TREE_CHAIN (last_tree) = t;
92 SET_LAST_STMT (t);
94 /* When we expand a statement-tree, we must know whether or not the
95 statements are full-expresions. We record that fact here. */
96 if (building_stmt_tree ())
97 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
100 /* COND is the condition-expression for an if, while, etc.,
101 statement. Convert it to a boolean value, if appropriate. */
103 static tree
104 maybe_convert_cond (cond)
105 tree cond;
107 /* Empty conditions remain empty. */
108 if (!cond)
109 return NULL_TREE;
111 /* Wait until we instantiate templates before doing conversion. */
112 if (processing_template_decl)
113 return cond;
115 /* Do the conversion. */
116 cond = convert_from_reference (cond);
117 return condition_conversion (cond);
120 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
122 void
123 finish_expr_stmt (expr)
124 tree expr;
126 if (expr != NULL_TREE)
128 if (building_stmt_tree ())
129 add_tree (build_min_nt (EXPR_STMT, expr));
130 else
132 emit_line_note (input_filename, lineno);
133 /* Do default conversion if safe and possibly important,
134 in case within ({...}). */
135 if (!stmts_are_full_exprs_p &&
136 ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
137 && lvalue_p (expr))
138 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
139 expr = default_conversion (expr);
141 if (stmts_are_full_exprs_p)
142 expand_start_target_temps ();
144 cplus_expand_expr_stmt (expr);
146 if (stmts_are_full_exprs_p)
148 expand_end_target_temps ();
149 clear_momentary ();
154 finish_stmt ();
156 /* This was an expression-statement, so we save the type of the
157 expression. */
158 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
161 /* Begin an if-statement. Returns a newly created IF_STMT if
162 appropriate. */
164 tree
165 begin_if_stmt ()
167 tree r;
169 do_pushlevel ();
171 if (building_stmt_tree ())
173 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
174 add_tree (r);
176 else
177 r = NULL_TREE;
179 return r;
182 /* Process the COND of an if-statement, which may be given by
183 IF_STMT. */
185 void
186 finish_if_stmt_cond (cond, if_stmt)
187 tree cond;
188 tree if_stmt;
190 cond = maybe_convert_cond (cond);
192 if (building_stmt_tree ())
193 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
194 else
196 emit_line_note (input_filename, lineno);
197 expand_start_cond (cond, 0);
201 /* Finish the then-clause of an if-statement, which may be given by
202 IF_STMT. */
204 tree
205 finish_then_clause (if_stmt)
206 tree if_stmt;
208 if (building_stmt_tree ())
210 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
211 SET_LAST_STMT (if_stmt);
212 return if_stmt;
214 else
215 return NULL_TREE;
218 /* Begin the else-clause of an if-statement. */
220 void
221 begin_else_clause ()
223 if (!building_stmt_tree ())
224 expand_start_else ();
227 /* Finish the else-clause of an if-statement, which may be given by
228 IF_STMT. */
230 void
231 finish_else_clause (if_stmt)
232 tree if_stmt;
234 if (building_stmt_tree ())
235 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
238 /* Finsh an if-statement. */
240 void
241 finish_if_stmt ()
243 if (!building_stmt_tree ())
244 expand_end_cond ();
246 do_poplevel ();
247 finish_stmt ();
250 /* Begin a while-statement. Returns a newly created WHILE_STMT if
251 appropriate. */
253 tree
254 begin_while_stmt ()
256 tree r;
258 if (building_stmt_tree ())
260 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
261 add_tree (r);
263 else
265 emit_nop ();
266 emit_line_note (input_filename, lineno);
267 expand_start_loop (1);
268 r = NULL_TREE;
271 do_pushlevel ();
273 return r;
276 /* Process the COND of an if-statement, which may be given by
277 WHILE_STMT. */
279 void
280 finish_while_stmt_cond (cond, while_stmt)
281 tree cond;
282 tree while_stmt;
284 cond = maybe_convert_cond (cond);
286 if (building_stmt_tree ())
287 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
288 else
290 emit_line_note (input_filename, lineno);
291 expand_exit_loop_if_false (0, cond);
294 /* If COND wasn't a declaration, clear out the
295 block we made for it and start a new one here so the
296 optimization in expand_end_loop will work. */
297 if (getdecls () == NULL_TREE)
299 do_poplevel ();
300 do_pushlevel ();
304 /* Finish a while-statement, which may be given by WHILE_STMT. */
306 void
307 finish_while_stmt (while_stmt)
308 tree while_stmt;
310 do_poplevel ();
312 if (building_stmt_tree ())
313 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
314 else
315 expand_end_loop ();
316 finish_stmt ();
319 /* Begin a do-statement. Returns a newly created DO_STMT if
320 appropriate. */
322 tree
323 begin_do_stmt ()
325 if (building_stmt_tree ())
327 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
328 add_tree (r);
329 return r;
331 else
333 emit_nop ();
334 emit_line_note (input_filename, lineno);
335 expand_start_loop_continue_elsewhere (1);
336 return NULL_TREE;
340 /* Finish the body of a do-statement, which may be given by DO_STMT. */
342 void
343 finish_do_body (do_stmt)
344 tree do_stmt;
346 if (building_stmt_tree ())
347 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
348 else
349 expand_loop_continue_here ();
352 /* Finish a do-statement, which may be given by DO_STMT, and whose
353 COND is as indicated. */
355 void
356 finish_do_stmt (cond, do_stmt)
357 tree cond;
358 tree do_stmt;
360 cond = maybe_convert_cond (cond);
362 if (building_stmt_tree ())
363 DO_COND (do_stmt) = cond;
364 else
366 emit_line_note (input_filename, lineno);
367 expand_exit_loop_if_false (0, cond);
368 expand_end_loop ();
371 clear_momentary ();
372 finish_stmt ();
375 /* Finish a return-statement. The EXPRESSION returned, if any, is as
376 indicated. */
378 void
379 finish_return_stmt (expr)
380 tree expr;
382 if (doing_semantic_analysis_p () && !processing_template_decl)
383 expr = check_return_expr (expr);
385 if (doing_semantic_analysis_p () && !processing_template_decl)
387 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
389 /* Even returns without a value in a constructor must return
390 `this'. We accomplish this by sending all returns in a
391 constructor to the CTOR_LABEL; finish_function emits code to
392 return a value there. When we finally generate the real
393 return statement, CTOR_LABEL is no longer set, and we fall
394 through into the normal return-processing code below. */
395 finish_goto_stmt (ctor_label);
396 return;
398 else if (DECL_DESTRUCTOR_P (current_function_decl))
400 /* Similarly, all destructors must run destructors for
401 base-classes before returning. So, all returns in a
402 destructor get sent to the DTOR_LABEL; finsh_function emits
403 code to return a value there. */
404 finish_goto_stmt (dtor_label);
405 return;
409 if (building_stmt_tree ())
410 add_tree (build_min_nt (RETURN_STMT, expr));
411 else
413 emit_line_note (input_filename, lineno);
414 c_expand_return (expr);
417 finish_stmt ();
420 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
422 tree
423 begin_for_stmt ()
425 tree r;
427 if (building_stmt_tree ())
429 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
430 NULL_TREE, NULL_TREE);
431 add_tree (r);
433 else
434 r = NULL_TREE;
436 if (flag_new_for_scope > 0)
438 do_pushlevel ();
439 note_level_for_for ();
442 return r;
445 /* Finish the for-init-statement of a for-statement, which may be
446 given by FOR_STMT. */
448 void
449 finish_for_init_stmt (for_stmt)
450 tree for_stmt;
452 if (building_stmt_tree ())
454 if (last_tree != for_stmt)
455 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
457 else
459 emit_nop ();
460 emit_line_note (input_filename, lineno);
461 expand_start_loop_continue_elsewhere (1);
464 do_pushlevel ();
467 /* Finish the COND of a for-statement, which may be given by
468 FOR_STMT. */
470 void
471 finish_for_cond (cond, for_stmt)
472 tree cond;
473 tree for_stmt;
475 cond = maybe_convert_cond (cond);
477 if (building_stmt_tree ())
478 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
479 else
481 emit_line_note (input_filename, lineno);
482 if (cond)
483 expand_exit_loop_if_false (0, cond);
486 /* If the cond wasn't a declaration, clear out the
487 block we made for it and start a new one here so the
488 optimization in expand_end_loop will work. */
489 if (getdecls () == NULL_TREE)
491 do_poplevel ();
492 do_pushlevel ();
496 /* Finish the increment-EXPRESSION in a for-statement, which may be
497 given by FOR_STMT. */
499 void
500 finish_for_expr (expr, for_stmt)
501 tree expr;
502 tree for_stmt;
504 if (building_stmt_tree ())
505 FOR_EXPR (for_stmt) = expr;
507 /* Don't let the tree nodes for EXPR be discarded
508 by clear_momentary during the parsing of the next stmt. */
509 push_momentary ();
512 /* Finish the body of a for-statement, which may be given by
513 FOR_STMT. The increment-EXPR for the loop must be
514 provided. */
516 void
517 finish_for_stmt (expr, for_stmt)
518 tree expr;
519 tree for_stmt;
521 /* Pop the scope for the body of the loop. */
522 do_poplevel ();
524 if (building_stmt_tree ())
525 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
526 else
528 emit_line_note (input_filename, lineno);
529 expand_loop_continue_here ();
530 if (expr)
531 finish_expr_stmt (expr);
532 expand_end_loop ();
535 pop_momentary ();
537 if (flag_new_for_scope > 0)
538 do_poplevel ();
540 finish_stmt ();
543 /* Finish a break-statement. */
545 void
546 finish_break_stmt ()
548 emit_line_note (input_filename, lineno);
549 if (building_stmt_tree ())
550 add_tree (build_min_nt (BREAK_STMT));
551 else if ( ! expand_exit_something ())
552 cp_error ("break statement not within loop or switch");
555 /* Finish a continue-statement. */
557 void
558 finish_continue_stmt ()
560 emit_line_note (input_filename, lineno);
561 if (building_stmt_tree ())
562 add_tree (build_min_nt (CONTINUE_STMT));
563 else if (! expand_continue_loop (0))
564 cp_error ("continue statement not within a loop");
567 /* Begin a switch-statement. Returns a new SWITCH_STMT if
568 appropriate. */
570 tree
571 begin_switch_stmt ()
573 tree r;
575 if (building_stmt_tree ())
577 r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
578 add_tree (r);
580 else
581 r = NULL_TREE;
583 do_pushlevel ();
585 return r;
588 /* Finish the cond of a switch-statement. */
590 void
591 finish_switch_cond (cond, switch_stmt)
592 tree cond;
593 tree switch_stmt;
595 if (building_stmt_tree ())
596 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
597 else if (cond != error_mark_node)
599 emit_line_note (input_filename, lineno);
600 c_expand_start_case (cond);
602 else
603 /* The code is in error, but we don't want expand_end_case to
604 crash. */
605 c_expand_start_case (boolean_false_node);
607 push_switch ();
609 /* Don't let the tree nodes for COND be discarded by
610 clear_momentary during the parsing of the next stmt. */
611 push_momentary ();
614 /* Finish the body of a switch-statement, which may be given by
615 SWITCH_STMT. The COND to switch on is indicated. */
617 void
618 finish_switch_stmt (cond, switch_stmt)
619 tree cond;
620 tree switch_stmt;
622 if (building_stmt_tree ())
623 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
624 else
625 expand_end_case (cond);
626 pop_momentary ();
627 pop_switch ();
628 do_poplevel ();
629 finish_stmt ();
632 /* Finish a case-label. */
634 void
635 finish_case_label (low_value, high_value)
636 tree low_value;
637 tree high_value;
639 if (building_stmt_tree ())
641 /* Add a representation for the case label to the statement
642 tree. */
643 add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
644 /* And warn about crossing initializations, etc. */
645 if (!processing_template_decl)
646 define_case_label ();
647 return;
650 do_case (low_value, high_value);
653 /* Finish a goto-statement. */
655 void
656 finish_goto_stmt (destination)
657 tree destination;
659 if (TREE_CODE (destination) == IDENTIFIER_NODE)
660 destination = lookup_label (destination);
662 /* We warn about unused labels with -Wunused. That means we have to
663 mark the used labels as used. */
664 if (TREE_CODE (destination) == LABEL_DECL)
665 TREE_USED (destination) = 1;
667 if (building_stmt_tree ())
668 add_tree (build_min_nt (GOTO_STMT, destination));
669 else
671 emit_line_note (input_filename, lineno);
673 if (TREE_CODE (destination) == LABEL_DECL)
675 label_rtx (destination);
676 expand_goto (destination);
678 else
679 expand_computed_goto (destination);
683 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
684 appropriate. */
686 tree
687 begin_try_block ()
689 if (building_stmt_tree ())
691 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
692 NULL_TREE);
693 add_tree (r);
694 return r;
696 else
698 emit_line_note (input_filename, lineno);
699 expand_start_try_stmts ();
700 return NULL_TREE;
704 /* Likewise, for a function-try-block. */
706 tree
707 begin_function_try_block ()
709 if (building_stmt_tree ())
711 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
712 NULL_TREE);
713 FN_TRY_BLOCK_P (r) = 1;
714 add_tree (r);
715 return r;
717 else
719 if (! current_function_parms_stored)
720 store_parm_decls ();
721 expand_start_early_try_stmts ();
722 return NULL_TREE;
726 /* Finish a try-block, which may be given by TRY_BLOCK. */
728 void
729 finish_try_block (try_block)
730 tree try_block;
732 if (building_stmt_tree ())
733 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
734 else
735 expand_start_all_catch ();
738 /* Finish the body of a cleanup try-block, which may be given by
739 TRY_BLOCK. */
741 void
742 finish_cleanup_try_block (try_block)
743 tree try_block;
745 if (building_stmt_tree ())
746 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
749 /* Finish an implicitly generated try-block, with a cleanup is given
750 by CLEANUP. */
752 void
753 finish_cleanup (cleanup, try_block)
754 tree cleanup;
755 tree try_block;
757 if (building_stmt_tree ())
759 TRY_HANDLERS (try_block) = cleanup;
760 CLEANUP_P (try_block) = 1;
762 else
763 expand_eh_region_end (protect_with_terminate (cleanup));
766 /* Likewise, for a function-try-block. */
768 void
769 finish_function_try_block (try_block)
770 tree try_block;
772 if (building_stmt_tree ())
774 if (TREE_CHAIN (try_block)
775 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
777 /* Chain the compound statement after the CTOR_INITIALIZER. */
778 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
779 /* And make the CTOR_INITIALIZER the body of the try-block. */
780 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
782 else
783 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
785 else
787 end_protect_partials ();
788 expand_start_all_catch ();
791 in_function_try_handler = 1;
794 /* Finish a handler-sequence for a try-block, which may be given by
795 TRY_BLOCK. */
797 void
798 finish_handler_sequence (try_block)
799 tree try_block;
801 if (building_stmt_tree ())
802 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
803 else
804 expand_end_all_catch ();
807 /* Likewise, for a function-try-block. */
809 void
810 finish_function_handler_sequence (try_block)
811 tree try_block;
813 in_function_try_handler = 0;
815 if (building_stmt_tree ())
816 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
817 else
818 expand_end_all_catch ();
821 /* Begin a handler. Returns a HANDLER if appropriate. */
823 tree
824 begin_handler ()
826 tree r;
828 if (building_stmt_tree ())
830 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
831 add_tree (r);
833 else
834 r = NULL_TREE;
836 do_pushlevel ();
838 return r;
841 /* Finish the handler-parameters for a handler, which may be given by
842 HANDLER. DECL is the declaration for the catch parameter, or NULL
843 if this is a `catch (...)' clause. */
845 tree
846 finish_handler_parms (decl, handler)
847 tree decl;
848 tree handler;
850 tree blocks = NULL_TREE;
852 if (processing_template_decl)
854 if (decl)
856 decl = pushdecl (decl);
857 decl = push_template_decl (decl);
858 add_decl_stmt (decl);
859 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
862 else if (building_stmt_tree ())
863 blocks = expand_start_catch_block (decl);
865 return blocks;
868 /* Note the beginning of a handler for TYPE. This function is called
869 at the point to which control should be transferred when an
870 appropriately-typed exception is thrown. */
872 void
873 begin_catch_block (type)
874 tree type;
876 if (building_stmt_tree ())
877 add_tree (build (START_CATCH_STMT, type));
878 else
879 start_catch_handler (type);
882 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
883 the return value from the matching call to finish_handler_parms. */
885 void
886 finish_handler (blocks, handler)
887 tree blocks;
888 tree handler;
890 if (!processing_template_decl)
892 if (building_stmt_tree ())
893 expand_end_catch_block (blocks);
895 if (!building_stmt_tree ())
897 /* Fall to outside the try statement when done executing
898 handler and we fall off end of handler. This is jump
899 Lresume in the documentation. */
900 expand_goto (top_label_entry (&caught_return_label_stack));
901 end_catch_handler ();
905 if (building_stmt_tree ())
906 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
908 do_poplevel ();
911 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
912 compound-statement does not define a scope. Returns a new
913 COMPOUND_STMT if appropriate. */
915 tree
916 begin_compound_stmt (has_no_scope)
917 int has_no_scope;
919 tree r;
921 if (building_stmt_tree ())
923 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
924 add_tree (r);
925 if (has_no_scope)
926 COMPOUND_STMT_NO_SCOPE (r) = 1;
928 else
929 r = NULL_TREE;
931 last_expr_type = NULL_TREE;
933 if (!has_no_scope)
934 do_pushlevel ();
935 else
936 /* Normally, we try hard to keep the BLOCK for a
937 statement-expression. But, if it's a statement-expression with
938 a scopeless block, there's nothing to keep, and we don't want
939 to accidentally keep a block *inside* the scopeless block. */
940 keep_next_level (0);
942 /* If this is the outermost block of the function, declare the
943 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
944 if (current_function
945 && !current_function_name_declared
946 && !processing_template_decl
947 && !has_no_scope)
949 declare_function_name ();
950 current_function_name_declared = 1;
953 return r;
957 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
958 If HAS_NO_SCOPE is non-zero, the compound statement does not define
959 a scope. */
961 tree
962 finish_compound_stmt (has_no_scope, compound_stmt)
963 int has_no_scope;
964 tree compound_stmt;
966 tree r;
967 tree t;
969 if (!has_no_scope)
970 r = do_poplevel ();
971 else
972 r = NULL_TREE;
974 if (building_stmt_tree ())
975 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
977 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
978 the precise purpose of that variable is store the type of the
979 last expression statement within the last compound statement, we
980 preserve the value. */
981 t = last_expr_type;
982 finish_stmt ();
983 last_expr_type = t;
985 return r;
988 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
989 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
990 CLOBBERS. */
992 void
993 finish_asm_stmt (cv_qualifier, string, output_operands,
994 input_operands, clobbers)
995 tree cv_qualifier;
996 tree string;
997 tree output_operands;
998 tree input_operands;
999 tree clobbers;
1001 if (TREE_CHAIN (string))
1003 if (building_stmt_tree ())
1004 /* We need to build the combined string on the permanent
1005 obstack so that we can use it during instantiations. */
1006 push_permanent_obstack ();
1008 string = combine_strings (string);
1010 if (building_stmt_tree ())
1011 pop_obstacks ();
1014 if (cv_qualifier != NULL_TREE
1015 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1017 cp_warning ("%s qualifier ignored on asm",
1018 IDENTIFIER_POINTER (cv_qualifier));
1019 cv_qualifier = NULL_TREE;
1022 if (building_stmt_tree ())
1024 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1025 output_operands, input_operands,
1026 clobbers);
1027 add_tree (r);
1029 else
1031 emit_line_note (input_filename, lineno);
1032 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1033 || clobbers != NULL_TREE)
1035 tree t;
1037 for (t = input_operands; t; t = TREE_CHAIN (t))
1038 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1040 c_expand_asm_operands (string, output_operands,
1041 input_operands,
1042 clobbers,
1043 cv_qualifier != NULL_TREE,
1044 input_filename, lineno);
1046 else
1047 expand_asm (string);
1049 finish_stmt ();
1053 /* Finish a label with the indicated NAME. */
1055 void
1056 finish_label_stmt (name)
1057 tree name;
1059 tree decl = define_label (input_filename, lineno, name);
1061 if (building_stmt_tree ())
1062 add_tree (build_min_nt (LABEL_STMT, decl));
1063 else if (decl)
1064 expand_label (decl);
1067 /* Finish a series of declarations for local labels. G++ allows users
1068 to declare "local" labels, i.e., labels with scope. This extension
1069 is useful when writing code involving statement-expressions. */
1071 void
1072 finish_label_decl (name)
1073 tree name;
1075 tree decl = declare_local_label (name);
1076 if (building_stmt_tree ())
1077 add_decl_stmt (decl);
1080 /* Create a declaration statement for the declaration given by the
1081 DECL. */
1083 void
1084 add_decl_stmt (decl)
1085 tree decl;
1087 tree decl_stmt;
1089 /* We need the type to last until instantiation time. */
1090 decl_stmt = build_min_nt (DECL_STMT, decl);
1091 add_tree (decl_stmt);
1094 /* We're in a constructor, and have just constructed a a subobject of
1095 *THIS. CLEANUP is code to run if an exception is thrown before the
1096 end of the current function is reached. */
1098 void
1099 finish_subobject (cleanup)
1100 tree cleanup;
1102 if (building_stmt_tree ())
1104 tree r = build_min_nt (SUBOBJECT, cleanup);
1105 add_tree (r);
1107 else
1108 add_partial_entry (cleanup);
1111 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1113 void
1114 finish_decl_cleanup (decl, cleanup)
1115 tree decl;
1116 tree cleanup;
1118 if (building_stmt_tree ())
1119 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1120 else if (!decl
1121 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1122 expand_decl_cleanup (decl, cleanup);
1125 /* Bind a name and initialization to the return value of
1126 the current function. */
1128 void
1129 finish_named_return_value (return_id, init)
1130 tree return_id, init;
1132 tree decl = DECL_RESULT (current_function_decl);
1134 if (pedantic)
1135 /* Give this error as many times as there are occurrences,
1136 so that users can use Emacs compilation buffers to find
1137 and fix all such places. */
1138 pedwarn ("ANSI C++ does not permit named return values");
1140 if (return_id != NULL_TREE)
1142 if (DECL_NAME (decl) == NULL_TREE)
1144 DECL_NAME (decl) = return_id;
1145 DECL_ASSEMBLER_NAME (decl) = return_id;
1147 else
1149 cp_error ("return identifier `%D' already in place", return_id);
1150 return;
1154 /* Can't let this happen for constructors. */
1155 if (DECL_CONSTRUCTOR_P (current_function_decl))
1157 error ("can't redefine default return value for constructors");
1158 return;
1161 /* If we have a named return value, put that in our scope as well. */
1162 if (DECL_NAME (decl) != NULL_TREE)
1164 /* Let `cp_finish_decl' know that this initializer is ok. */
1165 DECL_INITIAL (decl) = init;
1166 if (doing_semantic_analysis_p ())
1167 pushdecl (decl);
1169 if (building_stmt_tree ())
1170 add_tree (build_min_nt (RETURN_INIT, return_id, init));
1171 else
1173 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
1174 store_return_init (decl);
1179 /* Cache the value of this class's main virtual function table pointer
1180 in a register variable. This will save one indirection if a
1181 more than one virtual function call is made this function. */
1183 void
1184 setup_vtbl_ptr ()
1186 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1188 /* If we've already done this, there's no need to do it again. */
1189 if (vtbls_set_up_p)
1190 return;
1192 if (DECL_CONSTRUCTOR_P (current_function_decl))
1194 if (processing_template_decl)
1195 add_tree (build_min_nt
1196 (CTOR_INITIALIZER,
1197 current_member_init_list, current_base_init_list));
1198 else
1199 finish_expr_stmt (emit_base_init (current_class_type));
1201 else if (DECL_DESTRUCTOR_P (current_function_decl)
1202 && !processing_template_decl)
1204 tree binfo = TYPE_BINFO (current_class_type);
1205 tree if_stmt;
1206 tree compound_stmt;
1208 /* If the dtor is empty, and we know there is not possible way we
1209 could use any vtable entries, before they are possibly set by
1210 a base class dtor, we don't have to setup the vtables, as we
1211 know that any base class dtoring will set up any vtables it
1212 needs. We avoid MI, because one base class dtor can do a
1213 virtual dispatch to an overridden function that would need to
1214 have a non-related vtable set up, we cannot avoid setting up
1215 vtables in that case. We could change this to see if there is
1216 just one vtable. */
1217 if_stmt = begin_if_stmt ();
1219 /* If it is not safe to avoid setting up the vtables, then
1220 someone will change the condition to be boolean_true_node.
1221 (Actually, for now, we do not have code to set the condition
1222 appropriate, so we just assume that we always need to
1223 initialize the vtables.) */
1224 finish_if_stmt_cond (boolean_true_node, if_stmt);
1225 current_vcalls_possible_p = &IF_COND (if_stmt);
1226 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1228 /* Make all virtual function table pointers in non-virtual base
1229 classes point to CURRENT_CLASS_TYPE's virtual function
1230 tables. */
1231 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
1233 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1234 expand_indirect_vtbls_init (binfo, current_class_ref,
1235 current_class_ptr);
1237 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1238 finish_then_clause (if_stmt);
1239 finish_if_stmt ();
1242 /* Always keep the BLOCK node associated with the outermost pair of
1243 curly braces of a function. These are needed for correct
1244 operation of dwarfout.c. */
1245 keep_next_level (1);
1247 /* The virtual function tables are set up now. */
1248 vtbls_set_up_p = 1;
1251 /* Begin a new scope. */
1253 void
1254 do_pushlevel ()
1256 if (!building_stmt_tree ())
1258 emit_line_note (input_filename, lineno);
1259 clear_last_expr ();
1261 push_momentary ();
1262 if (stmts_are_full_exprs_p)
1264 pushlevel (0);
1265 if (!building_stmt_tree ()
1266 && !current_function->x_whole_function_mode_p)
1267 expand_start_bindings (0);
1268 else if (building_stmt_tree () && !processing_template_decl)
1270 tree ss = build_min_nt (SCOPE_STMT);
1271 SCOPE_BEGIN_P (ss) = 1;
1272 add_tree (ss);
1273 current_scope_stmt_stack
1274 = tree_cons (NULL_TREE, ss, current_scope_stmt_stack);
1279 /* Finish a scope. */
1281 tree
1282 do_poplevel ()
1284 tree t = NULL_TREE;
1286 if (stmts_are_full_exprs_p)
1288 if (!building_stmt_tree ()
1289 && !current_function->x_whole_function_mode_p)
1290 expand_end_bindings (getdecls (), kept_level_p (), 0);
1291 else if (building_stmt_tree () && !processing_template_decl)
1293 tree ss = build_min_nt (SCOPE_STMT);
1294 SCOPE_NULLIFIED_P (ss) = !kept_level_p ();
1295 SCOPE_NULLIFIED_P (TREE_VALUE (current_scope_stmt_stack))
1296 = SCOPE_NULLIFIED_P (ss);
1297 add_tree (ss);
1298 current_scope_stmt_stack = TREE_CHAIN (current_scope_stmt_stack);
1300 /* When not in function-at-a-time mode, expand_end_bindings
1301 will warn about unused variables. But, in
1302 function-at-a-time mode expand_end_bindings is not passed
1303 the list of variables in the current scope, and therefore
1304 no warning is emitted. So, we explicitly warn here. */
1305 warn_about_unused_variables (getdecls ());
1308 t = poplevel (kept_level_p (), 1, 0);
1310 pop_momentary ();
1311 return t;
1314 /* Finish a parenthesized expression EXPR. */
1316 tree
1317 finish_parenthesized_expr (expr)
1318 tree expr;
1320 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1321 /* This inhibits warnings in truthvalue_conversion. */
1322 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1324 return expr;
1327 /* Begin a statement-expression. The value returned must be passed to
1328 finish_stmt_expr. */
1330 tree
1331 begin_stmt_expr ()
1333 /* If we're outside a function, we won't have a statement-tree to
1334 work with. But, if we see a statement-expression we need to
1335 create one. */
1336 if (!current_function && !last_tree)
1337 begin_stmt_tree (&scope_chain->x_saved_tree);
1339 keep_next_level (1);
1340 /* If we're building a statement tree, then the upcoming compound
1341 statement will be chained onto the tree structure, starting at
1342 last_tree. We return last_tree so that we can later unhook the
1343 compound statement. */
1344 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
1347 /* Finish a statement-expression. RTL_EXPR should be the value
1348 returned by the previous begin_stmt_expr; EXPR is the
1349 statement-expression. Returns an expression representing the
1350 statement-expression. */
1352 tree
1353 finish_stmt_expr (rtl_expr)
1354 tree rtl_expr;
1356 tree result;
1358 if (!building_stmt_tree ())
1359 rtl_expr = expand_end_stmt_expr (rtl_expr);
1361 if (building_stmt_tree ())
1363 /* If the last thing in the statement-expression was not an
1364 expression-statement, then it has type `void'. */
1365 if (!last_expr_type)
1366 last_expr_type = void_type_node;
1367 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1368 TREE_SIDE_EFFECTS (result) = 1;
1370 /* Remove the compound statement from the tree structure; it is
1371 now saved in the STMT_EXPR. */
1372 SET_LAST_STMT (rtl_expr);
1373 TREE_CHAIN (last_tree) = NULL_TREE;
1375 else
1376 result = rtl_expr;
1378 /* If we created a statement-tree for this statement-expression,
1379 remove it now. */
1380 if (!current_function
1381 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1382 finish_stmt_tree (&scope_chain->x_saved_tree);
1384 return result;
1387 /* Finish a call to FN with ARGS. Returns a representation of the
1388 call. */
1390 tree
1391 finish_call_expr (fn, args, koenig)
1392 tree fn;
1393 tree args;
1394 int koenig;
1396 tree result;
1398 if (koenig)
1400 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1401 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1402 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1403 fn = do_identifier (fn, 2, args);
1405 result = build_x_function_call (fn, args, current_class_ref);
1407 if (TREE_CODE (result) == CALL_EXPR
1408 && (! TREE_TYPE (result)
1409 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1410 result = require_complete_type (result);
1412 return result;
1415 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1416 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1417 POSTDECREMENT_EXPR.) */
1419 tree
1420 finish_increment_expr (expr, code)
1421 tree expr;
1422 enum tree_code code;
1424 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1425 a COMPONENT_REF). This way if we've got, say, a reference to a
1426 static member that's being operated on, we don't end up trying to
1427 find a member operator for the class it's in. */
1429 if (TREE_CODE (expr) == OFFSET_REF)
1430 expr = resolve_offset_ref (expr);
1431 return build_x_unary_op (code, expr);
1434 /* Finish a use of `this'. Returns an expression for `this'. */
1436 tree
1437 finish_this_expr ()
1439 tree result;
1441 if (current_class_ptr)
1443 #ifdef WARNING_ABOUT_CCD
1444 TREE_USED (current_class_ptr) = 1;
1445 #endif
1446 result = current_class_ptr;
1448 else if (current_function_decl
1449 && DECL_STATIC_FUNCTION_P (current_function_decl))
1451 error ("`this' is unavailable for static member functions");
1452 result = error_mark_node;
1454 else
1456 if (current_function_decl)
1457 error ("invalid use of `this' in non-member function");
1458 else
1459 error ("invalid use of `this' at top level");
1460 result = error_mark_node;
1463 return result;
1466 /* Finish a member function call using OBJECT and ARGS as arguments to
1467 FN. Returns an expression for the call. */
1469 tree
1470 finish_object_call_expr (fn, object, args)
1471 tree fn;
1472 tree object;
1473 tree args;
1475 #if 0
1476 /* This is a future direction of this code, but because
1477 build_x_function_call cannot always undo what is done in
1478 build_component_ref entirely yet, we cannot do this. */
1480 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1481 return finish_call_expr (real_fn, args);
1482 #else
1483 if (DECL_DECLARES_TYPE_P (fn))
1485 if (processing_template_decl)
1486 /* This can happen on code like:
1488 class X;
1489 template <class T> void f(T t) {
1490 t.X();
1493 We just grab the underlying IDENTIFIER. */
1494 fn = DECL_NAME (fn);
1495 else
1497 cp_error ("calling type `%T' like a method", fn);
1498 return error_mark_node;
1502 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1503 #endif
1506 /* Finish a qualified member function call using OBJECT and ARGS as
1507 arguments to FN. Returns an expressino for the call. */
1509 tree
1510 finish_qualified_object_call_expr (fn, object, args)
1511 tree fn;
1512 tree object;
1513 tree args;
1515 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1516 TREE_OPERAND (fn, 1), args);
1519 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1520 being the scope, if any, of DESTRUCTOR. Returns an expression for
1521 the call. */
1523 tree
1524 finish_pseudo_destructor_call_expr (object, scope, destructor)
1525 tree object;
1526 tree scope;
1527 tree destructor;
1529 if (processing_template_decl)
1530 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1532 if (scope && scope != destructor)
1533 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1534 scope, destructor);
1536 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1537 && (TREE_CODE (TREE_TYPE (object)) !=
1538 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1539 cp_error ("`%E' is not of type `%T'", object, destructor);
1541 return cp_convert (void_type_node, object);
1544 /* Finish a call to a globally qualified member function FN using
1545 ARGS. Returns an expression for the call. */
1547 tree
1548 finish_qualified_call_expr (fn, args)
1549 tree fn;
1550 tree args;
1552 if (processing_template_decl)
1553 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1554 else
1555 return build_member_call (TREE_OPERAND (fn, 0),
1556 TREE_OPERAND (fn, 1),
1557 args);
1560 /* Finish an expression taking the address of LABEL. Returns an
1561 expression for the address. */
1563 tree
1564 finish_label_address_expr (label)
1565 tree label;
1567 tree result;
1569 label = lookup_label (label);
1570 if (label == NULL_TREE)
1571 result = null_pointer_node;
1572 else
1574 TREE_USED (label) = 1;
1575 result = build1 (ADDR_EXPR, ptr_type_node, label);
1576 TREE_CONSTANT (result) = 1;
1579 return result;
1582 /* Finish an expression of the form CODE EXPR. */
1584 tree
1585 finish_unary_op_expr (code, expr)
1586 enum tree_code code;
1587 tree expr;
1589 tree result = build_x_unary_op (code, expr);
1590 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1591 TREE_NEGATED_INT (result) = 1;
1592 overflow_warning (result);
1593 return result;
1596 /* Finish an id-expression. */
1598 tree
1599 finish_id_expr (expr)
1600 tree expr;
1602 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1603 expr = do_identifier (expr, 1, NULL_TREE);
1605 return expr;
1608 /* Begin a new-placement. */
1611 begin_new_placement ()
1613 /* The arguments to a placement new might be passed to a
1614 deallocation function, in the event that the allocation throws an
1615 exception. Since we don't expand exception handlers until the
1616 end of a function, we must make sure the arguments stay around
1617 that long. */
1618 return suspend_momentary ();
1621 /* Finish a new-placement. The ARGS are the placement arguments. The
1622 COOKIE is the value returned by the previous call to
1623 begin_new_placement. */
1625 tree
1626 finish_new_placement (args, cookie)
1627 tree args;
1628 int cookie;
1630 resume_momentary (cookie);
1631 return args;
1634 /* Begin a function defniition declared with DECL_SPECS and
1635 DECLARATOR. Returns non-zero if the function-declaration is
1636 legal. */
1639 begin_function_definition (decl_specs, declarator)
1640 tree decl_specs;
1641 tree declarator;
1643 tree specs;
1644 tree attrs;
1645 split_specs_attrs (decl_specs, &specs, &attrs);
1646 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1647 return 0;
1649 reinit_parse_for_function ();
1650 /* The things we're about to see are not directly qualified by any
1651 template headers we've seen thus far. */
1652 reset_specialization ();
1654 return 1;
1657 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1658 a SCOPE_REF. */
1660 tree
1661 begin_constructor_declarator (scope, name)
1662 tree scope;
1663 tree name;
1665 tree result = build_parse_node (SCOPE_REF, scope, name);
1666 enter_scope_of (result);
1667 return result;
1670 /* Finish an init-declarator. Returns a DECL. */
1672 tree
1673 finish_declarator (declarator, declspecs, attributes,
1674 prefix_attributes, initialized)
1675 tree declarator;
1676 tree declspecs;
1677 tree attributes;
1678 tree prefix_attributes;
1679 int initialized;
1681 return start_decl (declarator, declspecs, initialized, attributes,
1682 prefix_attributes);
1685 /* Finish a translation unit. */
1687 void
1688 finish_translation_unit ()
1690 /* In case there were missing closebraces,
1691 get us back to the global binding level. */
1692 pop_everything ();
1693 while (current_namespace != global_namespace)
1694 pop_namespace ();
1695 finish_file ();
1698 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1699 Returns the parameter. */
1701 tree
1702 finish_template_type_parm (aggr, identifier)
1703 tree aggr;
1704 tree identifier;
1706 if (aggr != class_type_node)
1708 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1709 aggr = class_type_node;
1712 return build_tree_list (aggr, identifier);
1715 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1716 Returns the parameter. */
1718 tree
1719 finish_template_template_parm (aggr, identifier)
1720 tree aggr;
1721 tree identifier;
1723 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1724 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1725 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1726 DECL_TEMPLATE_RESULT (tmpl) = decl;
1727 SET_DECL_ARTIFICIAL (decl);
1728 end_template_decl ();
1730 return finish_template_type_parm (aggr, tmpl);
1733 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1734 non-zero, the parameter list was terminated by a `...'. */
1736 tree
1737 finish_parmlist (parms, ellipsis)
1738 tree parms;
1739 int ellipsis;
1741 if (!ellipsis)
1742 chainon (parms, void_list_node);
1743 /* We mark the PARMS as a parmlist so that declarator processing can
1744 disambiguate certain constructs. */
1745 if (parms != NULL_TREE)
1746 TREE_PARMLIST (parms) = 1;
1748 return parms;
1751 /* Begin a class definition, as indicated by T. */
1753 tree
1754 begin_class_definition (t)
1755 tree t;
1757 push_permanent_obstack ();
1759 if (t == error_mark_node
1760 || ! IS_AGGR_TYPE (t))
1762 t = make_lang_type (RECORD_TYPE);
1763 pushtag (make_anon_name (), t, 0);
1766 /* In a definition of a member class template, we will get here with an
1767 implicit typename, a TYPENAME_TYPE with a type. */
1768 if (TREE_CODE (t) == TYPENAME_TYPE)
1769 t = TREE_TYPE (t);
1771 /* If we generated a partial instantiation of this type, but now
1772 we're seeing a real definition, we're actually looking at a
1773 partial specialization. Consider:
1775 template <class T, class U>
1776 struct Y {};
1778 template <class T>
1779 struct X {};
1781 template <class T, class U>
1782 void f()
1784 typename X<Y<T, U> >::A a;
1787 template <class T, class U>
1788 struct X<Y<T, U> >
1792 We have to undo the effects of the previous partial
1793 instantiation. */
1794 if (PARTIAL_INSTANTIATION_P (t))
1796 if (!pedantic)
1798 /* Unfortunately, when we're not in pedantic mode, we
1799 attempt to actually fill in some of the fields of the
1800 partial instantiation, in order to support the implicit
1801 typename extension. Clear those fields now, in
1802 preparation for the definition here. The fields cleared
1803 here must match those set in instantiate_class_template.
1804 Look for a comment mentioning begin_class_definition
1805 there. */
1806 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1807 TYPE_FIELDS (t) = NULL_TREE;
1808 TYPE_METHODS (t) = NULL_TREE;
1809 CLASSTYPE_TAGS (t) = NULL_TREE;
1810 TYPE_SIZE (t) = NULL_TREE;
1813 /* This isn't a partial instantiation any more. */
1814 PARTIAL_INSTANTIATION_P (t) = 0;
1816 /* If this type was already complete, and we see another definition,
1817 that's an error. */
1818 else if (TYPE_SIZE (t))
1819 duplicate_tag_error (t);
1821 /* Update the location of the decl. */
1822 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1823 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1825 if (TYPE_BEING_DEFINED (t))
1827 t = make_lang_type (TREE_CODE (t));
1828 pushtag (TYPE_IDENTIFIER (t), t, 0);
1830 maybe_process_partial_specialization (t);
1831 pushclass (t, 1);
1832 TYPE_BEING_DEFINED (t) = 1;
1833 /* Reset the interface data, at the earliest possible
1834 moment, as it might have been set via a class foo;
1835 before. */
1837 tree name = TYPE_IDENTIFIER (t);
1839 if (! ANON_AGGRNAME_P (name))
1841 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1842 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1843 (t, interface_unknown);
1846 /* Only leave this bit clear if we know this
1847 class is part of an interface-only specification. */
1848 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1849 || ! CLASSTYPE_INTERFACE_ONLY (t))
1850 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1852 #if 0
1853 tmp = TYPE_IDENTIFIER ($<ttype>0);
1854 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1855 overload_template_name (tmp, 1);
1856 #endif
1857 reset_specialization();
1859 /* Make a declaration for this class in its own scope. */
1860 build_self_reference ();
1862 return t;
1865 /* Finish the member declaration given by DECL. */
1867 void
1868 finish_member_declaration (decl)
1869 tree decl;
1871 if (decl == error_mark_node || decl == NULL_TREE)
1872 return;
1874 if (decl == void_type_node)
1875 /* The COMPONENT was a friend, not a member, and so there's
1876 nothing for us to do. */
1877 return;
1879 /* We should see only one DECL at a time. */
1880 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1882 /* Set up access control for DECL. */
1883 TREE_PRIVATE (decl)
1884 = (current_access_specifier == access_private_node);
1885 TREE_PROTECTED (decl)
1886 = (current_access_specifier == access_protected_node);
1887 if (TREE_CODE (decl) == TEMPLATE_DECL)
1889 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1890 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1893 /* Mark the DECL as a member of the current class. */
1894 if (TREE_CODE (decl) == FUNCTION_DECL
1895 || DECL_FUNCTION_TEMPLATE_P (decl))
1896 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1897 finish_struct. Presumably it is already set as the function is
1898 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1899 DECL_CLASS_CONTEXT (decl) = current_class_type;
1900 else
1901 DECL_CONTEXT (decl) = current_class_type;
1903 /* Put functions on the TYPE_METHODS list and everything else on the
1904 TYPE_FIELDS list. Note that these are built up in reverse order.
1905 We reverse them (to obtain declaration order) in finish_struct. */
1906 if (TREE_CODE (decl) == FUNCTION_DECL
1907 || DECL_FUNCTION_TEMPLATE_P (decl))
1909 /* We also need to add this function to the
1910 CLASSTYPE_METHOD_VEC. */
1911 add_method (current_class_type, 0, decl);
1913 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1914 TYPE_METHODS (current_class_type) = decl;
1916 else
1918 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1919 go at the beginning. The reason is that lookup_field_1
1920 searches the list in order, and we want a field name to
1921 override a type name so that the "struct stat hack" will
1922 work. In particular:
1924 struct S { enum E { }; int E } s;
1925 s.E = 3;
1927 is legal. In addition, the FIELD_DECLs must be maintained in
1928 declaration order so that class layout works as expected.
1929 However, we don't need that order until class layout, so we
1930 save a little time by putting FIELD_DECLs on in reverse order
1931 here, and then reversing them in finish_struct_1. (We could
1932 also keep a pointer to the correct insertion points in the
1933 list.) */
1935 if (TREE_CODE (decl) == TYPE_DECL)
1936 TYPE_FIELDS (current_class_type)
1937 = chainon (TYPE_FIELDS (current_class_type), decl);
1938 else
1940 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1941 TYPE_FIELDS (current_class_type) = decl;
1944 /* Enter the DECL into the scope of the class. */
1945 if (TREE_CODE (decl) != USING_DECL)
1946 pushdecl_class_level (decl);
1950 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1951 the definition is immediately followed by a semicolon. Returns the
1952 type. */
1954 tree
1955 finish_class_definition (t, attributes, semi, pop_scope_p)
1956 tree t;
1957 tree attributes;
1958 int semi;
1959 int pop_scope_p;
1961 /* finish_struct nukes this anyway; if finish_exception does too,
1962 then it can go. */
1963 if (semi)
1964 note_got_semicolon (t);
1966 /* If we got any attributes in class_head, xref_tag will stick them in
1967 TREE_TYPE of the type. Grab them now. */
1968 attributes = chainon (TREE_TYPE (t), attributes);
1969 TREE_TYPE (t) = NULL_TREE;
1971 if (TREE_CODE (t) == ENUMERAL_TYPE)
1973 else
1975 t = finish_struct (t, attributes);
1976 if (semi)
1977 note_got_semicolon (t);
1980 pop_obstacks ();
1982 if (! semi)
1983 check_for_missing_semicolon (t);
1984 if (pop_scope_p)
1985 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1986 if (current_scope () == current_function_decl)
1987 do_pending_defargs ();
1989 return t;
1992 /* Finish processing the default argument expressions cached during
1993 the processing of a class definition. */
1995 void
1996 begin_inline_definitions ()
1998 if (pending_inlines
1999 && current_scope () == current_function_decl)
2000 do_pending_inlines ();
2003 /* Finish processing the inline function definitions cached during the
2004 processing of a class definition. */
2006 void
2007 finish_inline_definitions ()
2009 if (current_class_type == NULL_TREE)
2010 clear_inline_text_obstack ();
2013 /* Finish processing the declaration of a member class template
2014 TYPES whose template parameters are given by PARMS. */
2016 tree
2017 finish_member_class_template (types)
2018 tree types;
2020 tree t;
2022 /* If there are declared, but undefined, partial specializations
2023 mixed in with the typespecs they will not yet have passed through
2024 maybe_process_partial_specialization, so we do that here. */
2025 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2026 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2027 maybe_process_partial_specialization (TREE_VALUE (t));
2029 note_list_got_semicolon (types);
2030 grok_x_components (types);
2031 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2032 /* The component was in fact a friend declaration. We avoid
2033 finish_member_template_decl performing certain checks by
2034 unsetting TYPES. */
2035 types = NULL_TREE;
2037 finish_member_template_decl (types);
2039 /* As with other component type declarations, we do
2040 not store the new DECL on the list of
2041 component_decls. */
2042 return NULL_TREE;
2045 /* Finish processsing a complete template declaration. The PARMS are
2046 the template parameters. */
2048 void
2049 finish_template_decl (parms)
2050 tree parms;
2052 if (parms)
2053 end_template_decl ();
2054 else
2055 end_specialization ();
2058 /* Finish processing a a template-id (which names a type) of the form
2059 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2060 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2061 the scope of template-id indicated. */
2063 tree
2064 finish_template_type (name, args, entering_scope)
2065 tree name;
2066 tree args;
2067 int entering_scope;
2069 tree decl;
2071 decl = lookup_template_class (name, args,
2072 NULL_TREE, NULL_TREE, entering_scope);
2073 if (decl != error_mark_node)
2074 decl = TYPE_STUB_DECL (decl);
2076 return decl;
2079 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2080 namespace scope or a class scope. */
2082 void
2083 enter_scope_of (sr)
2084 tree sr;
2086 tree scope = TREE_OPERAND (sr, 0);
2088 if (TREE_CODE (scope) == NAMESPACE_DECL)
2090 push_decl_namespace (scope);
2091 TREE_COMPLEXITY (sr) = -1;
2093 else if (scope != current_class_type)
2095 if (TREE_CODE (scope) == TYPENAME_TYPE)
2097 /* In a declarator for a template class member, the scope will
2098 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2099 scope = TREE_TYPE (scope);
2100 TREE_OPERAND (sr, 0) = scope;
2102 push_nested_class (scope, 3);
2103 TREE_COMPLEXITY (sr) = current_class_depth;
2107 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2108 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2109 BASE_CLASS, or NULL_TREE if an error occurred. The
2110 ACCESSS_SPECIFIER is one of
2111 access_{default,public,protected_private}[_virtual]_node.*/
2113 tree
2114 finish_base_specifier (access_specifier, base_class)
2115 tree access_specifier;
2116 tree base_class;
2118 tree type;
2119 tree result;
2121 if (base_class == NULL_TREE)
2123 error ("invalid base class");
2124 type = error_mark_node;
2126 else
2127 type = TREE_TYPE (base_class);
2129 if (! is_aggr_type (type, 1))
2130 result = NULL_TREE;
2131 else
2132 result = build_tree_list (access_specifier, type);
2134 return result;
2137 /* Called when multiple declarators are processed. If that is not
2138 premitted in this context, an error is issued. */
2140 void
2141 check_multiple_declarators ()
2143 /* [temp]
2145 In a template-declaration, explicit specialization, or explicit
2146 instantiation the init-declarator-list in the declaration shall
2147 contain at most one declarator.
2149 We don't just use PROCESSING_TEMPLATE_DECL for the first
2150 condition since that would disallow the perfectly legal code,
2151 like `template <class T> struct S { int i, j; };'. */
2152 tree scope = current_scope ();
2154 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2155 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2156 return;
2158 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2159 || processing_explicit_instantiation
2160 || processing_specialization)
2161 cp_error ("multiple declarators in template declaration");
2164 tree
2165 finish_typeof (expr)
2166 tree expr;
2168 if (processing_template_decl)
2170 tree t;
2172 push_permanent_obstack ();
2173 t = make_lang_type (TYPEOF_TYPE);
2174 TYPE_FIELDS (t) = expr;
2175 pop_obstacks ();
2177 return t;
2180 return TREE_TYPE (expr);
2183 /* Create an empty statement tree rooted at T. */
2185 void
2186 begin_stmt_tree (t)
2187 tree *t;
2189 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2190 what follows. We remove the extraneous statement in
2191 finish_stmt_tree. */
2192 *t = build_nt (EXPR_STMT, void_zero_node);
2193 SET_LAST_STMT (*t);
2194 last_expr_type = NULL_TREE;
2197 /* Finish the statement tree rooted at T. */
2199 void
2200 finish_stmt_tree (t)
2201 tree *t;
2203 tree stmt;
2205 /* Remove the fake extra statement added in begin_stmt_tree. */
2206 stmt = TREE_CHAIN (*t);
2207 *t = stmt;
2208 SET_LAST_STMT (NULL_TREE);
2210 if (current_function)
2212 /* The line-number recorded in the outermost statement in a function
2213 is the line number of the end of the function. */
2214 STMT_LINENO (stmt) = lineno;
2215 STMT_LINENO_FOR_FN_P (stmt) = 1;
2219 /* We're about to expand T, a statement. Set up appropriate context
2220 for the substitution. */
2222 void
2223 prep_stmt (t)
2224 tree t;
2226 if (!STMT_LINENO_FOR_FN_P (t))
2227 lineno = STMT_LINENO (t);
2228 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2231 /* Some statements, like for-statements or if-statements, require a
2232 condition. This condition can be a declaration. If T is such a
2233 declaration it is processed, and an expression appropriate to use
2234 as the condition is returned. Otherwise, T itself is returned. */
2236 static tree
2237 expand_cond (t)
2238 tree t;
2240 if (t && TREE_CODE (t) == TREE_LIST)
2242 expand_stmt (TREE_PURPOSE (t));
2243 return TREE_VALUE (t);
2245 else
2246 return t;
2249 /* Generate RTL for the statement T, and its substatements, and any
2250 other statements at its nesting level. */
2252 tree
2253 expand_stmt (t)
2254 tree t;
2256 tree rval = NULL_TREE;
2258 while (t && t != error_mark_node)
2260 int saved_stmts_are_full_exprs_p;
2262 /* Assume we'll have nothing to return. */
2263 rval = NULL_TREE;
2265 /* Set up context appropriately for handling this statement. */
2266 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2267 prep_stmt (t);
2269 switch (TREE_CODE (t))
2271 case RETURN_STMT:
2272 finish_return_stmt (RETURN_EXPR (t));
2273 break;
2275 case EXPR_STMT:
2276 finish_expr_stmt (EXPR_STMT_EXPR (t));
2277 break;
2279 case DECL_STMT:
2281 tree decl;
2282 int i = suspend_momentary ();
2284 emit_line_note (input_filename, lineno);
2285 decl = DECL_STMT_DECL (t);
2286 /* If this is a declaration for an automatic local
2287 variable, initialize it. Note that we might also see a
2288 declaration for a namespace-scope object (declared with
2289 `extern'). We don't have to handle the initialization
2290 of those objects here; they can only be declarations,
2291 rather than definitions. */
2292 if (TREE_CODE (decl) == VAR_DECL
2293 && !TREE_STATIC (decl)
2294 && !DECL_EXTERNAL (decl))
2296 /* Let the back-end know about this variable. */
2297 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2298 emit_local_var (decl);
2299 else
2300 expand_anon_union_decl (decl, NULL_TREE,
2301 DECL_ANON_UNION_ELEMS (decl));
2303 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2304 rest_of_decl_compilation
2305 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
2306 /*top_level=*/0, /*at_end=*/0);
2308 resume_momentary (i);
2310 break;
2312 case CLEANUP_STMT:
2313 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2314 break;
2316 case START_CATCH_STMT:
2317 begin_catch_block (TREE_TYPE (t));
2318 break;
2320 case FOR_STMT:
2322 tree tmp;
2324 begin_for_stmt ();
2325 expand_stmt (FOR_INIT_STMT (t));
2326 finish_for_init_stmt (NULL_TREE);
2327 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2328 tmp = FOR_EXPR (t);
2329 finish_for_expr (tmp, NULL_TREE);
2330 expand_stmt (FOR_BODY (t));
2331 finish_for_stmt (tmp, NULL_TREE);
2333 break;
2335 case WHILE_STMT:
2337 begin_while_stmt ();
2338 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2339 expand_stmt (WHILE_BODY (t));
2340 finish_while_stmt (NULL_TREE);
2342 break;
2344 case DO_STMT:
2346 begin_do_stmt ();
2347 expand_stmt (DO_BODY (t));
2348 finish_do_body (NULL_TREE);
2349 finish_do_stmt (DO_COND (t), NULL_TREE);
2351 break;
2353 case IF_STMT:
2354 begin_if_stmt ();
2355 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2356 if (THEN_CLAUSE (t))
2358 expand_stmt (THEN_CLAUSE (t));
2359 finish_then_clause (NULL_TREE);
2361 if (ELSE_CLAUSE (t))
2363 begin_else_clause ();
2364 expand_stmt (ELSE_CLAUSE (t));
2365 finish_else_clause (NULL_TREE);
2367 finish_if_stmt ();
2368 break;
2370 case COMPOUND_STMT:
2371 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2372 expand_stmt (COMPOUND_BODY (t));
2373 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2374 NULL_TREE);
2375 break;
2377 case BREAK_STMT:
2378 finish_break_stmt ();
2379 break;
2381 case CONTINUE_STMT:
2382 finish_continue_stmt ();
2383 break;
2385 case SWITCH_STMT:
2387 tree cond;
2389 begin_switch_stmt ();
2390 cond = expand_cond (SWITCH_COND (t));
2391 finish_switch_cond (cond, NULL_TREE);
2392 expand_stmt (SWITCH_BODY (t));
2393 finish_switch_stmt (cond, NULL_TREE);
2395 break;
2397 case CASE_LABEL:
2398 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2399 break;
2401 case LABEL_STMT:
2402 expand_label (LABEL_STMT_LABEL (t));
2403 break;
2405 case GOTO_STMT:
2406 finish_goto_stmt (GOTO_DESTINATION (t));
2407 break;
2409 case ASM_STMT:
2410 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2411 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2412 break;
2414 case TRY_BLOCK:
2415 if (CLEANUP_P (t))
2417 expand_eh_region_start ();
2418 expand_stmt (TRY_STMTS (t));
2419 finish_cleanup_try_block (NULL_TREE);
2420 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2422 else
2424 if (FN_TRY_BLOCK_P (t))
2425 begin_function_try_block ();
2426 else
2427 begin_try_block ();
2429 expand_stmt (TRY_STMTS (t));
2431 if (FN_TRY_BLOCK_P (t))
2433 finish_function_try_block (NULL_TREE);
2434 expand_stmt (TRY_HANDLERS (t));
2435 finish_function_handler_sequence (NULL_TREE);
2437 else
2439 finish_try_block (NULL_TREE);
2440 expand_stmt (TRY_HANDLERS (t));
2441 finish_handler_sequence (NULL_TREE);
2444 break;
2446 case HANDLER:
2447 begin_handler ();
2448 expand_stmt (HANDLER_BODY (t));
2449 finish_handler (NULL_TREE, NULL_TREE);
2450 break;
2452 case SUBOBJECT:
2453 finish_subobject (SUBOBJECT_CLEANUP (t));
2454 break;
2456 case SCOPE_STMT:
2457 if (SCOPE_BEGIN_P (t))
2458 expand_start_bindings (2 * SCOPE_NULLIFIED_P (t));
2459 else if (SCOPE_END_P (t))
2460 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
2461 break;
2463 case RETURN_INIT:
2464 /* Clear this out so that finish_named_return_value can set it
2465 again. */
2466 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2467 finish_named_return_value (TREE_OPERAND (t, 0),
2468 TREE_OPERAND (t, 1));
2469 break;
2471 default:
2472 my_friendly_abort (19990810);
2473 break;
2476 /* Restore saved state. */
2477 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2479 /* Go on to the next statement in this scope. */
2480 t = TREE_CHAIN (t);
2483 return rval;
2486 /* Generate RTL for FN. */
2488 void
2489 expand_body (fn)
2490 tree fn;
2492 int saved_lineno;
2493 char *saved_input_filename;
2495 /* When the parser calls us after finishing the body of a template
2496 function, we don't really want to expand the body. When we're
2497 processing an in-class definition of an inline function,
2498 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2499 to look at the function itself. */
2500 if (processing_template_decl
2501 || (DECL_LANG_SPECIFIC (fn)
2502 && DECL_TEMPLATE_INFO (fn)
2503 && uses_template_parms (DECL_TI_ARGS (fn))))
2504 return;
2506 /* There's no reason to do any of the work here if we're only doing
2507 semantic analysis; this code just generates RTL. */
2508 if (flag_syntax_only)
2509 return;
2511 /* Save the current file name and line number. When we expand the
2512 body of the function, we'll set LINENO and INPUT_FILENAME so that
2513 error-mesages come out in the right places. */
2514 saved_lineno = lineno;
2515 saved_input_filename = input_filename;
2516 lineno = DECL_SOURCE_LINE (fn);
2517 input_filename = DECL_SOURCE_FILE (fn);
2519 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2520 store_parm_decls ();
2522 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2523 any of the other magic variables we set up when starting a
2524 function body. */
2525 current_function_name_declared = 1;
2527 /* Expand the body. */
2528 expand_stmt (DECL_SAVED_TREE (fn));
2530 /* Statements should always be full-expressions at the outermost set
2531 of curly braces for a function. */
2532 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2534 /* The outermost statement for a function contains the line number
2535 recorded when we finished processing the function. */
2536 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2538 /* Generate code for the function. */
2539 finish_function (lineno, 0);
2541 /* And restore the current source position. */
2542 lineno = saved_lineno;
2543 input_filename = saved_input_filename;