* decl.c (initialize_local_var): Handle static variables here.
[official-gcc.git] / gcc / cp / semantics.c
blob950553586496636b8922decf65f116545596cf5f
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 /* When parsing a template, LAST_TREE contains the last statement
49 parsed. These are chained together through the TREE_CHAIN field,
50 but often need to be re-organized since the parse is performed
51 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
52 STMT. */
54 #define RECHAIN_STMTS(stmt, substmt) \
55 do { \
56 substmt = TREE_CHAIN (stmt); \
57 TREE_CHAIN (stmt) = NULL_TREE; \
58 last_tree = stmt; \
59 } while (0)
61 /* Finish processing the COND, the SUBSTMT condition for STMT. */
63 #define FINISH_COND(cond, stmt, substmt) \
64 do { \
65 if (last_tree != stmt) \
66 { \
67 RECHAIN_STMTS (stmt, substmt); \
68 if (!processing_template_decl) \
69 { \
70 cond = build_tree_list (substmt, cond); \
71 substmt = cond; \
72 } \
73 } \
74 else \
75 substmt = cond; \
76 } while (0)
78 /* T is a statement. Add it to the statement-tree. */
80 void
81 add_tree (t)
82 tree t;
84 /* Add T to the statement-tree. */
85 last_tree = TREE_CHAIN (last_tree) = t;
87 /* When we expand a statement-tree, we must know whether or not the
88 statements are full-expresions. We record that fact here. */
89 if (building_stmt_tree ())
90 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
93 /* COND is the condition-expression for an if, while, etc.,
94 statement. Convert it to a boolean value, if appropriate. */
96 static tree
97 maybe_convert_cond (cond)
98 tree cond;
100 /* Empty conditions remain empty. */
101 if (!cond)
102 return NULL_TREE;
104 /* Wait until we instantiate templates before doing conversion. */
105 if (processing_template_decl)
106 return cond;
108 /* Do the conversion. */
109 cond = convert_from_reference (cond);
110 return condition_conversion (cond);
113 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
115 void
116 finish_expr_stmt (expr)
117 tree expr;
119 if (expr != NULL_TREE)
121 if (building_stmt_tree ())
122 add_tree (build_min_nt (EXPR_STMT, expr));
123 else
125 emit_line_note (input_filename, lineno);
126 /* Do default conversion if safe and possibly important,
127 in case within ({...}). */
128 if (!stmts_are_full_exprs_p &&
129 ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
130 && lvalue_p (expr))
131 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
132 expr = default_conversion (expr);
134 if (stmts_are_full_exprs_p)
135 expand_start_target_temps ();
137 cplus_expand_expr_stmt (expr);
139 if (stmts_are_full_exprs_p)
141 expand_end_target_temps ();
142 clear_momentary ();
147 finish_stmt ();
149 /* This was an expression-statement, so we save the type of the
150 expression. */
151 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
154 /* Begin an if-statement. Returns a newly created IF_STMT if
155 appropriate. */
157 tree
158 begin_if_stmt ()
160 tree r;
162 do_pushlevel ();
164 if (building_stmt_tree ())
166 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
167 add_tree (r);
169 else
170 r = NULL_TREE;
172 return r;
175 /* Process the COND of an if-statement, which may be given by
176 IF_STMT. */
178 void
179 finish_if_stmt_cond (cond, if_stmt)
180 tree cond;
181 tree if_stmt;
183 cond = maybe_convert_cond (cond);
185 if (building_stmt_tree ())
186 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
187 else
189 emit_line_note (input_filename, lineno);
190 expand_start_cond (cond, 0);
194 /* Finish the then-clause of an if-statement, which may be given by
195 IF_STMT. */
197 tree
198 finish_then_clause (if_stmt)
199 tree if_stmt;
201 if (building_stmt_tree ())
203 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
204 last_tree = if_stmt;
205 return if_stmt;
207 else
208 return NULL_TREE;
211 /* Begin the else-clause of an if-statement. */
213 void
214 begin_else_clause ()
216 if (!building_stmt_tree ())
217 expand_start_else ();
220 /* Finish the else-clause of an if-statement, which may be given by
221 IF_STMT. */
223 void
224 finish_else_clause (if_stmt)
225 tree if_stmt;
227 if (building_stmt_tree ())
228 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
231 /* Finsh an if-statement. */
233 void
234 finish_if_stmt ()
236 if (!building_stmt_tree ())
237 expand_end_cond ();
239 do_poplevel ();
240 finish_stmt ();
243 /* Begin a while-statement. Returns a newly created WHILE_STMT if
244 appropriate. */
246 tree
247 begin_while_stmt ()
249 tree r;
251 if (building_stmt_tree ())
253 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
254 add_tree (r);
256 else
258 emit_nop ();
259 emit_line_note (input_filename, lineno);
260 expand_start_loop (1);
261 r = NULL_TREE;
264 do_pushlevel ();
266 return r;
269 /* Process the COND of an if-statement, which may be given by
270 WHILE_STMT. */
272 void
273 finish_while_stmt_cond (cond, while_stmt)
274 tree cond;
275 tree while_stmt;
277 cond = maybe_convert_cond (cond);
279 if (building_stmt_tree ())
280 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
281 else
283 emit_line_note (input_filename, lineno);
284 expand_exit_loop_if_false (0, cond);
287 /* If COND wasn't a declaration, clear out the
288 block we made for it and start a new one here so the
289 optimization in expand_end_loop will work. */
290 if (getdecls () == NULL_TREE)
292 do_poplevel ();
293 do_pushlevel ();
297 /* Finish a while-statement, which may be given by WHILE_STMT. */
299 void
300 finish_while_stmt (while_stmt)
301 tree while_stmt;
303 do_poplevel ();
305 if (building_stmt_tree ())
306 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
307 else
308 expand_end_loop ();
309 finish_stmt ();
312 /* Begin a do-statement. Returns a newly created DO_STMT if
313 appropriate. */
315 tree
316 begin_do_stmt ()
318 if (building_stmt_tree ())
320 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
321 add_tree (r);
322 return r;
324 else
326 emit_nop ();
327 emit_line_note (input_filename, lineno);
328 expand_start_loop_continue_elsewhere (1);
329 return NULL_TREE;
333 /* Finish the body of a do-statement, which may be given by DO_STMT. */
335 void
336 finish_do_body (do_stmt)
337 tree do_stmt;
339 if (building_stmt_tree ())
340 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
341 else
342 expand_loop_continue_here ();
345 /* Finish a do-statement, which may be given by DO_STMT, and whose
346 COND is as indicated. */
348 void
349 finish_do_stmt (cond, do_stmt)
350 tree cond;
351 tree do_stmt;
353 cond = maybe_convert_cond (cond);
355 if (building_stmt_tree ())
356 DO_COND (do_stmt) = cond;
357 else
359 emit_line_note (input_filename, lineno);
360 expand_exit_loop_if_false (0, cond);
361 expand_end_loop ();
364 clear_momentary ();
365 finish_stmt ();
368 /* Finish a return-statement. The EXPRESSION returned, if any, is as
369 indicated. */
371 void
372 finish_return_stmt (expr)
373 tree expr;
375 if (doing_semantic_analysis_p () && !processing_template_decl)
376 expr = check_return_expr (expr);
378 if (doing_semantic_analysis_p () && !processing_template_decl)
380 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
382 /* Even returns without a value in a constructor must return
383 `this'. We accomplish this by sending all returns in a
384 constructor to the CTOR_LABEL; finish_function emits code to
385 return a value there. When we finally generate the real
386 return statement, CTOR_LABEL is no longer set, and we fall
387 through into the normal return-processing code below. */
388 finish_goto_stmt (ctor_label);
389 return;
391 else if (DECL_DESTRUCTOR_P (current_function_decl))
393 /* Similarly, all destructors must run destructors for
394 base-classes before returning. So, all returns in a
395 destructor get sent to the DTOR_LABEL; finsh_function emits
396 code to return a value there. */
397 finish_goto_stmt (dtor_label);
398 return;
402 if (building_stmt_tree ())
403 add_tree (build_min_nt (RETURN_STMT, expr));
404 else
406 emit_line_note (input_filename, lineno);
407 c_expand_return (expr);
410 finish_stmt ();
413 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
415 tree
416 begin_for_stmt ()
418 tree r;
420 if (building_stmt_tree ())
422 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
423 NULL_TREE, NULL_TREE);
424 add_tree (r);
426 else
427 r = NULL_TREE;
429 if (flag_new_for_scope > 0)
431 do_pushlevel ();
432 note_level_for_for ();
435 return r;
438 /* Finish the for-init-statement of a for-statement, which may be
439 given by FOR_STMT. */
441 void
442 finish_for_init_stmt (for_stmt)
443 tree for_stmt;
445 if (building_stmt_tree ())
447 if (last_tree != for_stmt)
448 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
450 else
452 emit_nop ();
453 emit_line_note (input_filename, lineno);
454 expand_start_loop_continue_elsewhere (1);
457 do_pushlevel ();
460 /* Finish the COND of a for-statement, which may be given by
461 FOR_STMT. */
463 void
464 finish_for_cond (cond, for_stmt)
465 tree cond;
466 tree for_stmt;
468 cond = maybe_convert_cond (cond);
470 if (building_stmt_tree ())
471 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
472 else
474 emit_line_note (input_filename, lineno);
475 if (cond)
476 expand_exit_loop_if_false (0, cond);
479 /* If the cond wasn't a declaration, clear out the
480 block we made for it and start a new one here so the
481 optimization in expand_end_loop will work. */
482 if (getdecls () == NULL_TREE)
484 do_poplevel ();
485 do_pushlevel ();
489 /* Finish the increment-EXPRESSION in a for-statement, which may be
490 given by FOR_STMT. */
492 void
493 finish_for_expr (expr, for_stmt)
494 tree expr;
495 tree for_stmt;
497 if (building_stmt_tree ())
498 FOR_EXPR (for_stmt) = expr;
500 /* Don't let the tree nodes for EXPR be discarded
501 by clear_momentary during the parsing of the next stmt. */
502 push_momentary ();
505 /* Finish the body of a for-statement, which may be given by
506 FOR_STMT. The increment-EXPR for the loop must be
507 provided. */
509 void
510 finish_for_stmt (expr, for_stmt)
511 tree expr;
512 tree for_stmt;
514 /* Pop the scope for the body of the loop. */
515 do_poplevel ();
517 if (building_stmt_tree ())
518 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
519 else
521 emit_line_note (input_filename, lineno);
522 expand_loop_continue_here ();
523 if (expr)
524 finish_expr_stmt (expr);
525 expand_end_loop ();
528 pop_momentary ();
530 if (flag_new_for_scope > 0)
531 do_poplevel ();
533 finish_stmt ();
536 /* Finish a break-statement. */
538 void
539 finish_break_stmt ()
541 emit_line_note (input_filename, lineno);
542 if (building_stmt_tree ())
543 add_tree (build_min_nt (BREAK_STMT));
544 else if ( ! expand_exit_something ())
545 cp_error ("break statement not within loop or switch");
548 /* Finish a continue-statement. */
550 void
551 finish_continue_stmt ()
553 emit_line_note (input_filename, lineno);
554 if (building_stmt_tree ())
555 add_tree (build_min_nt (CONTINUE_STMT));
556 else if (! expand_continue_loop (0))
557 cp_error ("continue statement not within a loop");
560 /* Begin a switch-statement. Returns a new SWITCH_STMT if
561 appropriate. */
563 tree
564 begin_switch_stmt ()
566 tree r;
568 if (building_stmt_tree ())
570 r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
571 add_tree (r);
573 else
574 r = NULL_TREE;
576 do_pushlevel ();
578 return r;
581 /* Finish the cond of a switch-statement. */
583 void
584 finish_switch_cond (cond, switch_stmt)
585 tree cond;
586 tree switch_stmt;
588 if (building_stmt_tree ())
589 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
590 else if (cond != error_mark_node)
592 emit_line_note (input_filename, lineno);
593 c_expand_start_case (cond);
595 else
596 /* The code is in error, but we don't want expand_end_case to
597 crash. */
598 c_expand_start_case (boolean_false_node);
600 push_switch ();
602 /* Don't let the tree nodes for COND be discarded by
603 clear_momentary during the parsing of the next stmt. */
604 push_momentary ();
607 /* Finish the body of a switch-statement, which may be given by
608 SWITCH_STMT. The COND to switch on is indicated. */
610 void
611 finish_switch_stmt (cond, switch_stmt)
612 tree cond;
613 tree switch_stmt;
615 if (building_stmt_tree ())
616 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
617 else
618 expand_end_case (cond);
619 pop_momentary ();
620 pop_switch ();
621 do_poplevel ();
622 finish_stmt ();
625 /* Finish a case-label. */
627 void
628 finish_case_label (low_value, high_value)
629 tree low_value;
630 tree high_value;
632 if (building_stmt_tree ())
634 /* Add a representation for the case label to the statement
635 tree. */
636 add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
637 /* And warn about crossing initializations, etc. */
638 if (!processing_template_decl)
639 define_case_label ();
640 return;
643 do_case (low_value, high_value);
646 /* Finish a goto-statement. */
648 void
649 finish_goto_stmt (destination)
650 tree destination;
652 if (TREE_CODE (destination) == IDENTIFIER_NODE)
653 destination = lookup_label (destination);
655 /* We warn about unused labels with -Wunused. That means we have to
656 mark the used labels as used. */
657 if (TREE_CODE (destination) == LABEL_DECL)
658 TREE_USED (destination) = 1;
660 if (building_stmt_tree ())
661 add_tree (build_min_nt (GOTO_STMT, destination));
662 else
664 emit_line_note (input_filename, lineno);
666 if (TREE_CODE (destination) == LABEL_DECL)
668 label_rtx (destination);
669 expand_goto (destination);
671 else
672 expand_computed_goto (destination);
676 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
677 appropriate. */
679 tree
680 begin_try_block ()
682 if (building_stmt_tree ())
684 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
685 NULL_TREE);
686 add_tree (r);
687 return r;
689 else
691 emit_line_note (input_filename, lineno);
692 expand_start_try_stmts ();
693 return NULL_TREE;
697 /* Likewise, for a function-try-block. */
699 tree
700 begin_function_try_block ()
702 if (building_stmt_tree ())
704 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
705 NULL_TREE);
706 FN_TRY_BLOCK_P (r) = 1;
707 add_tree (r);
708 return r;
710 else
712 if (! current_function_parms_stored)
713 store_parm_decls ();
714 expand_start_early_try_stmts ();
715 return NULL_TREE;
719 /* Finish a try-block, which may be given by TRY_BLOCK. */
721 void
722 finish_try_block (try_block)
723 tree try_block;
725 if (building_stmt_tree ())
726 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
727 else
728 expand_start_all_catch ();
731 /* Finish the body of a cleanup try-block, which may be given by
732 TRY_BLOCK. */
734 void
735 finish_cleanup_try_block (try_block)
736 tree try_block;
738 if (building_stmt_tree ())
739 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
742 /* Finish an implicitly generated try-block, with a cleanup is given
743 by CLEANUP. */
745 void
746 finish_cleanup (cleanup, try_block)
747 tree cleanup;
748 tree try_block;
750 if (building_stmt_tree ())
752 TRY_HANDLERS (try_block) = cleanup;
753 CLEANUP_P (try_block) = 1;
755 else
756 expand_eh_region_end (protect_with_terminate (cleanup));
759 /* Likewise, for a function-try-block. */
761 void
762 finish_function_try_block (try_block)
763 tree try_block;
765 if (building_stmt_tree ())
767 if (TREE_CHAIN (try_block)
768 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
770 /* Chain the compound statement after the CTOR_INITIALIZER. */
771 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
772 /* And make the CTOR_INITIALIZER the body of the try-block. */
773 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
775 else
776 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
778 else
780 end_protect_partials ();
781 expand_start_all_catch ();
784 in_function_try_handler = 1;
787 /* Finish a handler-sequence for a try-block, which may be given by
788 TRY_BLOCK. */
790 void
791 finish_handler_sequence (try_block)
792 tree try_block;
794 if (building_stmt_tree ())
795 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
796 else
797 expand_end_all_catch ();
800 /* Likewise, for a function-try-block. */
802 void
803 finish_function_handler_sequence (try_block)
804 tree try_block;
806 in_function_try_handler = 0;
808 if (building_stmt_tree ())
809 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
810 else
811 expand_end_all_catch ();
814 /* Begin a handler. Returns a HANDLER if appropriate. */
816 tree
817 begin_handler ()
819 tree r;
821 if (building_stmt_tree ())
823 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
824 add_tree (r);
826 else
827 r = NULL_TREE;
829 do_pushlevel ();
831 return r;
834 /* Finish the handler-parameters for a handler, which may be given by
835 HANDLER. DECL is the declaration for the catch parameter, or NULL
836 if this is a `catch (...)' clause. */
838 tree
839 finish_handler_parms (decl, handler)
840 tree decl;
841 tree handler;
843 tree blocks = NULL_TREE;
845 if (processing_template_decl)
847 if (decl)
849 decl = pushdecl (decl);
850 decl = push_template_decl (decl);
851 add_decl_stmt (decl);
852 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
855 else if (building_stmt_tree ())
856 blocks = expand_start_catch_block (decl);
858 return blocks;
861 /* Note the beginning of a handler for TYPE. This function is called
862 at the point to which control should be transferred when an
863 appropriately-typed exception is thrown. */
865 void
866 begin_catch_block (type)
867 tree type;
869 if (building_stmt_tree ())
870 add_tree (build (START_CATCH_STMT, type));
871 else
872 start_catch_handler (type);
875 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
876 the return value from the matching call to finish_handler_parms. */
878 void
879 finish_handler (blocks, handler)
880 tree blocks;
881 tree handler;
883 if (!processing_template_decl)
885 if (building_stmt_tree ())
886 expand_end_catch_block (blocks);
888 if (!building_stmt_tree ())
890 /* Fall to outside the try statement when done executing
891 handler and we fall off end of handler. This is jump
892 Lresume in the documentation. */
893 expand_goto (top_label_entry (&caught_return_label_stack));
894 end_catch_handler ();
898 if (building_stmt_tree ())
899 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
901 do_poplevel ();
904 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
905 compound-statement does not define a scope. Returns a new
906 COMPOUND_STMT if appropriate. */
908 tree
909 begin_compound_stmt (has_no_scope)
910 int has_no_scope;
912 tree r;
914 if (building_stmt_tree ())
916 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
917 add_tree (r);
918 if (has_no_scope)
919 COMPOUND_STMT_NO_SCOPE (r) = 1;
921 else
922 r = NULL_TREE;
924 last_expr_type = NULL_TREE;
926 if (!has_no_scope)
927 do_pushlevel ();
928 else
929 /* Normally, we try hard to keep the BLOCK for a
930 statement-expression. But, if it's a statement-expression with
931 a scopeless block, there's nothing to keep, and we don't want
932 to accidentally keep a block *inside* the scopeless block. */
933 keep_next_level (0);
935 /* If this is the outermost block of the function, declare the
936 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
937 if (!current_function_name_declared
938 && !processing_template_decl
939 && !has_no_scope)
941 declare_function_name ();
942 current_function_name_declared = 1;
945 return r;
949 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
950 If HAS_NO_SCOPE is non-zero, the compound statement does not define
951 a scope. */
953 tree
954 finish_compound_stmt (has_no_scope, compound_stmt)
955 int has_no_scope;
956 tree compound_stmt;
958 tree r;
959 tree t;
961 if (!has_no_scope)
962 r = do_poplevel ();
963 else
964 r = NULL_TREE;
966 if (building_stmt_tree ())
967 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
969 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
970 the precise purpose of that variable is store the type of the
971 last expression statement within the last compound statement, we
972 preserve the value. */
973 t = last_expr_type;
974 finish_stmt ();
975 last_expr_type = t;
977 return r;
980 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
981 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
982 CLOBBERS. */
984 void
985 finish_asm_stmt (cv_qualifier, string, output_operands,
986 input_operands, clobbers)
987 tree cv_qualifier;
988 tree string;
989 tree output_operands;
990 tree input_operands;
991 tree clobbers;
993 if (TREE_CHAIN (string))
995 if (building_stmt_tree ())
996 /* We need to build the combined string on the permanent
997 obstack so that we can use it during instantiations. */
998 push_permanent_obstack ();
1000 string = combine_strings (string);
1002 if (building_stmt_tree ())
1003 pop_obstacks ();
1006 if (cv_qualifier != NULL_TREE
1007 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1009 cp_warning ("%s qualifier ignored on asm",
1010 IDENTIFIER_POINTER (cv_qualifier));
1011 cv_qualifier = NULL_TREE;
1014 if (building_stmt_tree ())
1016 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1017 output_operands, input_operands,
1018 clobbers);
1019 add_tree (r);
1021 else
1023 emit_line_note (input_filename, lineno);
1024 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1025 || clobbers != NULL_TREE)
1027 tree t;
1029 for (t = input_operands; t; t = TREE_CHAIN (t))
1030 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1032 c_expand_asm_operands (string, output_operands,
1033 input_operands,
1034 clobbers,
1035 cv_qualifier != NULL_TREE,
1036 input_filename, lineno);
1038 else
1039 expand_asm (string);
1041 finish_stmt ();
1045 /* Finish a label with the indicated NAME. */
1047 void
1048 finish_label_stmt (name)
1049 tree name;
1051 tree decl = define_label (input_filename, lineno, name);
1053 if (building_stmt_tree ())
1054 add_tree (build_min_nt (LABEL_STMT, decl));
1055 else if (decl)
1056 expand_label (decl);
1059 /* Finish a series of declarations for local labels. G++ allows users
1060 to declare "local" labels, i.e., labels with scope. This extension
1061 is useful when writing code involving statement-expressions. */
1063 void
1064 finish_label_decl (name)
1065 tree name;
1067 tree decl = declare_local_label (name);
1068 if (building_stmt_tree ())
1069 add_decl_stmt (decl);
1072 /* Create a declaration statement for the declaration given by the
1073 DECL. */
1075 void
1076 add_decl_stmt (decl)
1077 tree decl;
1079 tree decl_stmt;
1081 /* We need the type to last until instantiation time. */
1082 decl_stmt = build_min_nt (DECL_STMT, decl);
1083 add_tree (decl_stmt);
1086 /* We're in a constructor, and have just constructed a a subobject of
1087 *THIS. CLEANUP is code to run if an exception is thrown before the
1088 end of the current function is reached. */
1090 void
1091 finish_subobject (cleanup)
1092 tree cleanup;
1094 if (building_stmt_tree ())
1096 tree r = build_min_nt (SUBOBJECT, cleanup);
1097 add_tree (r);
1099 else
1100 add_partial_entry (cleanup);
1103 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1105 void
1106 finish_decl_cleanup (decl, cleanup)
1107 tree decl;
1108 tree cleanup;
1110 if (building_stmt_tree ())
1111 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1112 else if (!decl
1113 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1114 expand_decl_cleanup (decl, cleanup);
1117 /* Bind a name and initialization to the return value of
1118 the current function. */
1120 void
1121 finish_named_return_value (return_id, init)
1122 tree return_id, init;
1124 tree decl = DECL_RESULT (current_function_decl);
1126 if (pedantic)
1127 /* Give this error as many times as there are occurrences,
1128 so that users can use Emacs compilation buffers to find
1129 and fix all such places. */
1130 pedwarn ("ANSI C++ does not permit named return values");
1132 if (return_id != NULL_TREE)
1134 if (DECL_NAME (decl) == NULL_TREE)
1136 DECL_NAME (decl) = return_id;
1137 DECL_ASSEMBLER_NAME (decl) = return_id;
1139 else
1141 cp_error ("return identifier `%D' already in place", return_id);
1142 return;
1146 /* Can't let this happen for constructors. */
1147 if (DECL_CONSTRUCTOR_P (current_function_decl))
1149 error ("can't redefine default return value for constructors");
1150 return;
1153 /* If we have a named return value, put that in our scope as well. */
1154 if (DECL_NAME (decl) != NULL_TREE)
1156 /* Let `cp_finish_decl' know that this initializer is ok. */
1157 DECL_INITIAL (decl) = init;
1158 if (doing_semantic_analysis_p ())
1159 pushdecl (decl);
1161 if (building_stmt_tree ())
1162 add_tree (build_min_nt (RETURN_INIT, return_id, init));
1163 else
1165 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
1166 store_return_init (decl);
1171 /* Cache the value of this class's main virtual function table pointer
1172 in a register variable. This will save one indirection if a
1173 more than one virtual function call is made this function. */
1175 void
1176 setup_vtbl_ptr ()
1178 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1180 /* If we've already done this, there's no need to do it again. */
1181 if (vtbls_set_up_p)
1182 return;
1184 if (DECL_CONSTRUCTOR_P (current_function_decl))
1186 if (processing_template_decl)
1187 add_tree (build_min_nt
1188 (CTOR_INITIALIZER,
1189 current_member_init_list, current_base_init_list));
1190 else
1191 finish_expr_stmt (emit_base_init (current_class_type));
1193 else if (DECL_DESTRUCTOR_P (current_function_decl)
1194 && !processing_template_decl)
1196 tree binfo = TYPE_BINFO (current_class_type);
1197 tree if_stmt;
1198 tree compound_stmt;
1200 /* If the dtor is empty, and we know there is not possible way we
1201 could use any vtable entries, before they are possibly set by
1202 a base class dtor, we don't have to setup the vtables, as we
1203 know that any base class dtoring will set up any vtables it
1204 needs. We avoid MI, because one base class dtor can do a
1205 virtual dispatch to an overridden function that would need to
1206 have a non-related vtable set up, we cannot avoid setting up
1207 vtables in that case. We could change this to see if there is
1208 just one vtable. */
1209 if_stmt = begin_if_stmt ();
1211 /* If it is not safe to avoid setting up the vtables, then
1212 someone will change the condition to be boolean_true_node.
1213 (Actually, for now, we do not have code to set the condition
1214 appropriate, so we just assume that we always need to
1215 initialize the vtables.) */
1216 finish_if_stmt_cond (boolean_true_node, if_stmt);
1217 current_vcalls_possible_p = &IF_COND (if_stmt);
1218 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1220 /* Make all virtual function table pointers in non-virtual base
1221 classes point to CURRENT_CLASS_TYPE's virtual function
1222 tables. */
1223 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
1225 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1226 expand_indirect_vtbls_init (binfo, current_class_ref,
1227 current_class_ptr);
1229 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1230 finish_then_clause (if_stmt);
1231 finish_if_stmt ();
1234 /* Always keep the BLOCK node associated with the outermost pair of
1235 curly braces of a function. These are needed for correct
1236 operation of dwarfout.c. */
1237 keep_next_level (1);
1239 /* The virtual function tables are set up now. */
1240 vtbls_set_up_p = 1;
1243 /* Begin a new scope. */
1245 void
1246 do_pushlevel ()
1248 if (!building_stmt_tree ())
1250 emit_line_note (input_filename, lineno);
1251 clear_last_expr ();
1253 push_momentary ();
1254 if (stmts_are_full_exprs_p)
1256 pushlevel (0);
1257 if (!building_stmt_tree ()
1258 && !current_function->x_whole_function_mode_p)
1259 expand_start_bindings (0);
1260 else if (building_stmt_tree () && !processing_template_decl)
1262 tree ss = build_min_nt (SCOPE_STMT);
1263 SCOPE_BEGIN_P (ss) = 1;
1264 add_tree (ss);
1265 current_scope_stmt_stack
1266 = tree_cons (NULL_TREE, ss, current_scope_stmt_stack);
1271 /* Finish a scope. */
1273 tree
1274 do_poplevel ()
1276 tree t = NULL_TREE;
1278 if (stmts_are_full_exprs_p)
1280 if (!building_stmt_tree ()
1281 && !current_function->x_whole_function_mode_p)
1282 expand_end_bindings (getdecls (), kept_level_p (), 0);
1283 else if (building_stmt_tree () && !processing_template_decl)
1285 tree ss = build_min_nt (SCOPE_STMT);
1286 SCOPE_NULLIFIED_P (ss) = !kept_level_p ();
1287 SCOPE_NULLIFIED_P (TREE_VALUE (current_scope_stmt_stack))
1288 = SCOPE_NULLIFIED_P (ss);
1289 add_tree (ss);
1290 current_scope_stmt_stack = TREE_CHAIN (current_scope_stmt_stack);
1292 /* When not in function-at-a-time mode, expand_end_bindings
1293 will warn about unused variables. But, in
1294 function-at-a-time mode expand_end_bindings is not passed
1295 the list of variables in the current scope, and therefore
1296 no warning is emitted. So, we explicitly warn here. */
1297 warn_about_unused_variables (getdecls ());
1300 t = poplevel (kept_level_p (), 1, 0);
1302 pop_momentary ();
1303 return t;
1306 /* Finish a parenthesized expression EXPR. */
1308 tree
1309 finish_parenthesized_expr (expr)
1310 tree expr;
1312 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1313 /* This inhibits warnings in truthvalue_conversion. */
1314 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1316 return expr;
1319 /* Begin a statement-expression. The value returned must be passed to
1320 finish_stmt_expr. */
1322 tree
1323 begin_stmt_expr ()
1325 keep_next_level (1);
1326 /* If we're building a statement tree, then the upcoming compound
1327 statement will be chained onto the tree structure, starting at
1328 last_tree. We return last_tree so that we can later unhook the
1329 compound statement. */
1330 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
1333 /* Finish a statement-expression. RTL_EXPR should be the value
1334 returned by the previous begin_stmt_expr; EXPR is the
1335 statement-expression. Returns an expression representing the
1336 statement-expression. */
1338 tree
1339 finish_stmt_expr (rtl_expr)
1340 tree rtl_expr;
1342 tree result;
1344 if (!building_stmt_tree ())
1346 rtl_expr = expand_end_stmt_expr (rtl_expr);
1347 /* The statements have side effects, so the group does too. */
1348 TREE_SIDE_EFFECTS (rtl_expr) = 1;
1351 if (building_stmt_tree ())
1353 /* If the last thing in the statement-expression was not an
1354 expression-statement, then it has type `void'. */
1355 if (!last_expr_type)
1356 last_expr_type = void_type_node;
1357 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1358 TREE_SIDE_EFFECTS (result) = 1;
1360 /* Remove the compound statement from the tree structure; it is
1361 now saved in the STMT_EXPR. */
1362 last_tree = rtl_expr;
1363 TREE_CHAIN (last_tree) = NULL_TREE;
1365 else
1366 result = rtl_expr;
1368 return result;
1371 /* Finish a call to FN with ARGS. Returns a representation of the
1372 call. */
1374 tree
1375 finish_call_expr (fn, args, koenig)
1376 tree fn;
1377 tree args;
1378 int koenig;
1380 tree result;
1382 if (koenig)
1384 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1385 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1386 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1387 fn = do_identifier (fn, 2, args);
1389 result = build_x_function_call (fn, args, current_class_ref);
1391 if (TREE_CODE (result) == CALL_EXPR
1392 && (! TREE_TYPE (result)
1393 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1394 result = require_complete_type (result);
1396 return result;
1399 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1400 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1401 POSTDECREMENT_EXPR.) */
1403 tree
1404 finish_increment_expr (expr, code)
1405 tree expr;
1406 enum tree_code code;
1408 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1409 a COMPONENT_REF). This way if we've got, say, a reference to a
1410 static member that's being operated on, we don't end up trying to
1411 find a member operator for the class it's in. */
1413 if (TREE_CODE (expr) == OFFSET_REF)
1414 expr = resolve_offset_ref (expr);
1415 return build_x_unary_op (code, expr);
1418 /* Finish a use of `this'. Returns an expression for `this'. */
1420 tree
1421 finish_this_expr ()
1423 tree result;
1425 if (current_class_ptr)
1427 #ifdef WARNING_ABOUT_CCD
1428 TREE_USED (current_class_ptr) = 1;
1429 #endif
1430 result = current_class_ptr;
1432 else if (current_function_decl
1433 && DECL_STATIC_FUNCTION_P (current_function_decl))
1435 error ("`this' is unavailable for static member functions");
1436 result = error_mark_node;
1438 else
1440 if (current_function_decl)
1441 error ("invalid use of `this' in non-member function");
1442 else
1443 error ("invalid use of `this' at top level");
1444 result = error_mark_node;
1447 return result;
1450 /* Finish a member function call using OBJECT and ARGS as arguments to
1451 FN. Returns an expression for the call. */
1453 tree
1454 finish_object_call_expr (fn, object, args)
1455 tree fn;
1456 tree object;
1457 tree args;
1459 #if 0
1460 /* This is a future direction of this code, but because
1461 build_x_function_call cannot always undo what is done in
1462 build_component_ref entirely yet, we cannot do this. */
1464 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1465 return finish_call_expr (real_fn, args);
1466 #else
1467 if (TREE_CODE (fn) == TYPE_DECL)
1469 if (processing_template_decl)
1470 /* This can happen on code like:
1472 class X;
1473 template <class T> void f(T t) {
1474 t.X();
1477 We just grab the underlying IDENTIFIER. */
1478 fn = DECL_NAME (fn);
1479 else
1481 cp_error ("calling type `%T' like a method", fn);
1482 return error_mark_node;
1486 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1487 #endif
1490 /* Finish a qualified member function call using OBJECT and ARGS as
1491 arguments to FN. Returns an expressino for the call. */
1493 tree
1494 finish_qualified_object_call_expr (fn, object, args)
1495 tree fn;
1496 tree object;
1497 tree args;
1499 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1500 TREE_OPERAND (fn, 1), args);
1503 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1504 being the scope, if any, of DESTRUCTOR. Returns an expression for
1505 the call. */
1507 tree
1508 finish_pseudo_destructor_call_expr (object, scope, destructor)
1509 tree object;
1510 tree scope;
1511 tree destructor;
1513 if (processing_template_decl)
1514 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1516 if (scope && scope != destructor)
1517 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1518 scope, destructor);
1520 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1521 && (TREE_CODE (TREE_TYPE (object)) !=
1522 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1523 cp_error ("`%E' is not of type `%T'", object, destructor);
1525 return cp_convert (void_type_node, object);
1528 /* Finish a call to a globally qualified member function FN using
1529 ARGS. Returns an expression for the call. */
1531 tree
1532 finish_qualified_call_expr (fn, args)
1533 tree fn;
1534 tree args;
1536 if (processing_template_decl)
1537 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1538 else
1539 return build_member_call (TREE_OPERAND (fn, 0),
1540 TREE_OPERAND (fn, 1),
1541 args);
1544 /* Finish an expression taking the address of LABEL. Returns an
1545 expression for the address. */
1547 tree
1548 finish_label_address_expr (label)
1549 tree label;
1551 tree result;
1553 label = lookup_label (label);
1554 if (label == NULL_TREE)
1555 result = null_pointer_node;
1556 else
1558 TREE_USED (label) = 1;
1559 result = build1 (ADDR_EXPR, ptr_type_node, label);
1560 TREE_CONSTANT (result) = 1;
1563 return result;
1566 /* Finish an expression of the form CODE EXPR. */
1568 tree
1569 finish_unary_op_expr (code, expr)
1570 enum tree_code code;
1571 tree expr;
1573 tree result = build_x_unary_op (code, expr);
1574 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1575 TREE_NEGATED_INT (result) = 1;
1576 overflow_warning (result);
1577 return result;
1580 /* Finish an id-expression. */
1582 tree
1583 finish_id_expr (expr)
1584 tree expr;
1586 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1587 expr = do_identifier (expr, 1, NULL_TREE);
1589 return expr;
1592 /* Begin a new-placement. */
1595 begin_new_placement ()
1597 /* The arguments to a placement new might be passed to a
1598 deallocation function, in the event that the allocation throws an
1599 exception. Since we don't expand exception handlers until the
1600 end of a function, we must make sure the arguments stay around
1601 that long. */
1602 return suspend_momentary ();
1605 /* Finish a new-placement. The ARGS are the placement arguments. The
1606 COOKIE is the value returned by the previous call to
1607 begin_new_placement. */
1609 tree
1610 finish_new_placement (args, cookie)
1611 tree args;
1612 int cookie;
1614 resume_momentary (cookie);
1615 return args;
1618 /* Begin a function defniition declared with DECL_SPECS and
1619 DECLARATOR. Returns non-zero if the function-declaration is
1620 legal. */
1623 begin_function_definition (decl_specs, declarator)
1624 tree decl_specs;
1625 tree declarator;
1627 tree specs;
1628 tree attrs;
1629 split_specs_attrs (decl_specs, &specs, &attrs);
1630 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1631 return 0;
1633 reinit_parse_for_function ();
1634 /* The things we're about to see are not directly qualified by any
1635 template headers we've seen thus far. */
1636 reset_specialization ();
1638 return 1;
1641 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1642 a SCOPE_REF. */
1644 tree
1645 begin_constructor_declarator (scope, name)
1646 tree scope;
1647 tree name;
1649 tree result = build_parse_node (SCOPE_REF, scope, name);
1650 enter_scope_of (result);
1651 return result;
1654 /* Finish an init-declarator. Returns a DECL. */
1656 tree
1657 finish_declarator (declarator, declspecs, attributes,
1658 prefix_attributes, initialized)
1659 tree declarator;
1660 tree declspecs;
1661 tree attributes;
1662 tree prefix_attributes;
1663 int initialized;
1665 return start_decl (declarator, declspecs, initialized, attributes,
1666 prefix_attributes);
1669 /* Finish a translation unit. */
1671 void
1672 finish_translation_unit ()
1674 /* In case there were missing closebraces,
1675 get us back to the global binding level. */
1676 pop_everything ();
1677 while (current_namespace != global_namespace)
1678 pop_namespace ();
1679 finish_file ();
1682 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1683 Returns the parameter. */
1685 tree
1686 finish_template_type_parm (aggr, identifier)
1687 tree aggr;
1688 tree identifier;
1690 if (aggr != class_type_node)
1692 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1693 aggr = class_type_node;
1696 return build_tree_list (aggr, identifier);
1699 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1700 Returns the parameter. */
1702 tree
1703 finish_template_template_parm (aggr, identifier)
1704 tree aggr;
1705 tree identifier;
1707 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1708 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1709 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1710 DECL_TEMPLATE_RESULT (tmpl) = decl;
1711 SET_DECL_ARTIFICIAL (decl);
1712 end_template_decl ();
1714 return finish_template_type_parm (aggr, tmpl);
1717 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1718 non-zero, the parameter list was terminated by a `...'. */
1720 tree
1721 finish_parmlist (parms, ellipsis)
1722 tree parms;
1723 int ellipsis;
1725 if (!ellipsis)
1726 chainon (parms, void_list_node);
1727 /* We mark the PARMS as a parmlist so that declarator processing can
1728 disambiguate certain constructs. */
1729 if (parms != NULL_TREE)
1730 TREE_PARMLIST (parms) = 1;
1732 return parms;
1735 /* Begin a class definition, as indicated by T. */
1737 tree
1738 begin_class_definition (t)
1739 tree t;
1741 push_permanent_obstack ();
1743 if (t == error_mark_node
1744 || ! IS_AGGR_TYPE (t))
1746 t = make_lang_type (RECORD_TYPE);
1747 pushtag (make_anon_name (), t, 0);
1750 /* In a definition of a member class template, we will get here with an
1751 implicit typename, a TYPENAME_TYPE with a type. */
1752 if (TREE_CODE (t) == TYPENAME_TYPE)
1753 t = TREE_TYPE (t);
1755 /* If we generated a partial instantiation of this type, but now
1756 we're seeing a real definition, we're actually looking at a
1757 partial specialization. Consider:
1759 template <class T, class U>
1760 struct Y {};
1762 template <class T>
1763 struct X {};
1765 template <class T, class U>
1766 void f()
1768 typename X<Y<T, U> >::A a;
1771 template <class T, class U>
1772 struct X<Y<T, U> >
1776 We have to undo the effects of the previous partial
1777 instantiation. */
1778 if (PARTIAL_INSTANTIATION_P (t))
1780 if (!pedantic)
1782 /* Unfortunately, when we're not in pedantic mode, we
1783 attempt to actually fill in some of the fields of the
1784 partial instantiation, in order to support the implicit
1785 typename extension. Clear those fields now, in
1786 preparation for the definition here. The fields cleared
1787 here must match those set in instantiate_class_template.
1788 Look for a comment mentioning begin_class_definition
1789 there. */
1790 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1791 TYPE_FIELDS (t) = NULL_TREE;
1792 TYPE_METHODS (t) = NULL_TREE;
1793 CLASSTYPE_TAGS (t) = NULL_TREE;
1794 TYPE_SIZE (t) = NULL_TREE;
1797 /* This isn't a partial instantiation any more. */
1798 PARTIAL_INSTANTIATION_P (t) = 0;
1800 /* If this type was already complete, and we see another definition,
1801 that's an error. */
1802 else if (TYPE_SIZE (t))
1803 duplicate_tag_error (t);
1805 /* Update the location of the decl. */
1806 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1807 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1809 if (TYPE_BEING_DEFINED (t))
1811 t = make_lang_type (TREE_CODE (t));
1812 pushtag (TYPE_IDENTIFIER (t), t, 0);
1814 maybe_process_partial_specialization (t);
1815 pushclass (t, 1);
1816 TYPE_BEING_DEFINED (t) = 1;
1817 /* Reset the interface data, at the earliest possible
1818 moment, as it might have been set via a class foo;
1819 before. */
1821 tree name = TYPE_IDENTIFIER (t);
1823 if (! ANON_AGGRNAME_P (name))
1825 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1826 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1827 (t, interface_unknown);
1830 /* Only leave this bit clear if we know this
1831 class is part of an interface-only specification. */
1832 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1833 || ! CLASSTYPE_INTERFACE_ONLY (t))
1834 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1836 #if 0
1837 tmp = TYPE_IDENTIFIER ($<ttype>0);
1838 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1839 overload_template_name (tmp, 1);
1840 #endif
1841 reset_specialization();
1843 /* In case this is a local class within a template
1844 function, we save the current tree structure so
1845 that we can get it back later. */
1846 begin_tree ();
1848 /* Make a declaration for this class in its own scope. */
1849 build_self_reference ();
1851 return t;
1854 /* Finish the member declaration given by DECL. */
1856 void
1857 finish_member_declaration (decl)
1858 tree decl;
1860 if (decl == error_mark_node || decl == NULL_TREE)
1861 return;
1863 if (decl == void_type_node)
1864 /* The COMPONENT was a friend, not a member, and so there's
1865 nothing for us to do. */
1866 return;
1868 /* We should see only one DECL at a time. */
1869 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1871 /* Set up access control for DECL. */
1872 TREE_PRIVATE (decl)
1873 = (current_access_specifier == access_private_node);
1874 TREE_PROTECTED (decl)
1875 = (current_access_specifier == access_protected_node);
1876 if (TREE_CODE (decl) == TEMPLATE_DECL)
1878 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1879 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1882 /* Mark the DECL as a member of the current class. */
1883 if (TREE_CODE (decl) == FUNCTION_DECL
1884 || DECL_FUNCTION_TEMPLATE_P (decl))
1885 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1886 finish_struct. Presumably it is already set as the function is
1887 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1888 DECL_CLASS_CONTEXT (decl) = current_class_type;
1889 else
1890 DECL_CONTEXT (decl) = current_class_type;
1892 /* Put functions on the TYPE_METHODS list and everything else on the
1893 TYPE_FIELDS list. Note that these are built up in reverse order.
1894 We reverse them (to obtain declaration order) in finish_struct. */
1895 if (TREE_CODE (decl) == FUNCTION_DECL
1896 || DECL_FUNCTION_TEMPLATE_P (decl))
1898 /* We also need to add this function to the
1899 CLASSTYPE_METHOD_VEC. */
1900 add_method (current_class_type, 0, decl);
1902 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1903 TYPE_METHODS (current_class_type) = decl;
1905 else
1907 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1908 go at the beginning. The reason is that lookup_field_1
1909 searches the list in order, and we want a field name to
1910 override a type name so that the "struct stat hack" will
1911 work. In particular:
1913 struct S { enum E { }; int E } s;
1914 s.E = 3;
1916 is legal. In addition, the FIELD_DECLs must be maintained in
1917 declaration order so that class layout works as expected.
1918 However, we don't need that order until class layout, so we
1919 save a little time by putting FIELD_DECLs on in reverse order
1920 here, and then reversing them in finish_struct_1. (We could
1921 also keep a pointer to the correct insertion points in the
1922 list.) */
1924 if (TREE_CODE (decl) == TYPE_DECL)
1925 TYPE_FIELDS (current_class_type)
1926 = chainon (TYPE_FIELDS (current_class_type), decl);
1927 else
1929 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1930 TYPE_FIELDS (current_class_type) = decl;
1933 /* Enter the DECL into the scope of the class. */
1934 if (TREE_CODE (decl) != USING_DECL)
1935 pushdecl_class_level (decl);
1939 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1940 the definition is immediately followed by a semicolon. Returns the
1941 type. */
1943 tree
1944 finish_class_definition (t, attributes, semi, pop_scope_p)
1945 tree t;
1946 tree attributes;
1947 int semi;
1948 int pop_scope_p;
1950 /* finish_struct nukes this anyway; if finish_exception does too,
1951 then it can go. */
1952 if (semi)
1953 note_got_semicolon (t);
1955 /* If we got any attributes in class_head, xref_tag will stick them in
1956 TREE_TYPE of the type. Grab them now. */
1957 attributes = chainon (TREE_TYPE (t), attributes);
1958 TREE_TYPE (t) = NULL_TREE;
1960 if (TREE_CODE (t) == ENUMERAL_TYPE)
1962 else
1964 t = finish_struct (t, attributes);
1965 if (semi)
1966 note_got_semicolon (t);
1969 pop_obstacks ();
1971 if (! semi)
1972 check_for_missing_semicolon (t);
1973 if (pop_scope_p)
1974 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1975 if (current_scope () == current_function_decl)
1976 do_pending_defargs ();
1978 return t;
1981 /* Finish processing the default argument expressions cached during
1982 the processing of a class definition. */
1984 void
1985 begin_inline_definitions ()
1987 if (pending_inlines
1988 && current_scope () == current_function_decl)
1989 do_pending_inlines ();
1992 /* Finish processing the inline function definitions cached during the
1993 processing of a class definition. */
1995 void
1996 finish_inline_definitions ()
1998 if (current_class_type == NULL_TREE)
1999 clear_inline_text_obstack ();
2001 /* Undo the begin_tree in begin_class_definition. */
2002 end_tree ();
2005 /* Finish processing the declaration of a member class template
2006 TYPES whose template parameters are given by PARMS. */
2008 tree
2009 finish_member_class_template (types)
2010 tree types;
2012 tree t;
2014 /* If there are declared, but undefined, partial specializations
2015 mixed in with the typespecs they will not yet have passed through
2016 maybe_process_partial_specialization, so we do that here. */
2017 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2018 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2019 maybe_process_partial_specialization (TREE_VALUE (t));
2021 note_list_got_semicolon (types);
2022 grok_x_components (types);
2023 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2024 /* The component was in fact a friend declaration. We avoid
2025 finish_member_template_decl performing certain checks by
2026 unsetting TYPES. */
2027 types = NULL_TREE;
2029 finish_member_template_decl (types);
2031 /* As with other component type declarations, we do
2032 not store the new DECL on the list of
2033 component_decls. */
2034 return NULL_TREE;
2037 /* Finish processsing a complete template declaration. The PARMS are
2038 the template parameters. */
2040 void
2041 finish_template_decl (parms)
2042 tree parms;
2044 if (parms)
2045 end_template_decl ();
2046 else
2047 end_specialization ();
2050 /* Finish processing a a template-id (which names a type) of the form
2051 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2052 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2053 the scope of template-id indicated. */
2055 tree
2056 finish_template_type (name, args, entering_scope)
2057 tree name;
2058 tree args;
2059 int entering_scope;
2061 tree decl;
2063 decl = lookup_template_class (name, args,
2064 NULL_TREE, NULL_TREE, entering_scope);
2065 if (decl != error_mark_node)
2066 decl = TYPE_STUB_DECL (decl);
2068 return decl;
2071 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2072 namespace scope or a class scope. */
2074 void
2075 enter_scope_of (sr)
2076 tree sr;
2078 tree scope = TREE_OPERAND (sr, 0);
2080 if (TREE_CODE (scope) == NAMESPACE_DECL)
2082 push_decl_namespace (scope);
2083 TREE_COMPLEXITY (sr) = -1;
2085 else if (scope != current_class_type)
2087 if (TREE_CODE (scope) == TYPENAME_TYPE)
2089 /* In a declarator for a template class member, the scope will
2090 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2091 scope = TREE_TYPE (scope);
2092 TREE_OPERAND (sr, 0) = scope;
2094 push_nested_class (scope, 3);
2095 TREE_COMPLEXITY (sr) = current_class_depth;
2099 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2100 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2101 BASE_CLASS, or NULL_TREE if an error occurred. The
2102 ACCESSS_SPECIFIER is one of
2103 access_{default,public,protected_private}[_virtual]_node.*/
2105 tree
2106 finish_base_specifier (access_specifier, base_class)
2107 tree access_specifier;
2108 tree base_class;
2110 tree type;
2111 tree result;
2113 if (base_class == NULL_TREE)
2115 error ("invalid base class");
2116 type = error_mark_node;
2118 else
2119 type = TREE_TYPE (base_class);
2121 if (! is_aggr_type (type, 1))
2122 result = NULL_TREE;
2123 else
2124 result = build_tree_list (access_specifier, type);
2126 return result;
2129 /* Called when multiple declarators are processed. If that is not
2130 premitted in this context, an error is issued. */
2132 void
2133 check_multiple_declarators ()
2135 /* [temp]
2137 In a template-declaration, explicit specialization, or explicit
2138 instantiation the init-declarator-list in the declaration shall
2139 contain at most one declarator.
2141 We don't just use PROCESSING_TEMPLATE_DECL for the first
2142 condition since that would disallow the perfectly legal code,
2143 like `template <class T> struct S { int i, j; };'. */
2144 tree scope = current_scope ();
2146 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2147 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2148 return;
2150 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2151 || processing_explicit_instantiation
2152 || processing_specialization)
2153 cp_error ("multiple declarators in template declaration");
2156 tree
2157 finish_typeof (expr)
2158 tree expr;
2160 if (processing_template_decl)
2162 tree t;
2164 push_permanent_obstack ();
2165 t = make_lang_type (TYPEOF_TYPE);
2166 TYPE_FIELDS (t) = expr;
2167 pop_obstacks ();
2169 return t;
2172 return TREE_TYPE (expr);
2175 /* Create an empty statement tree for FN. */
2177 void
2178 begin_stmt_tree (fn)
2179 tree fn;
2181 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2182 what follows. We remove the extraneous statement in
2183 finish_stmt_tree. */
2184 DECL_SAVED_TREE (fn) = build_nt (EXPR_STMT, void_zero_node);
2185 last_tree = DECL_SAVED_TREE (fn);
2186 last_expr_type = NULL_TREE;
2189 /* Finish the statement tree for FN. */
2191 void
2192 finish_stmt_tree (fn)
2193 tree fn;
2195 tree stmt;
2197 /* Remove the fake extra statement added in begin_stmt_tree. */
2198 stmt = TREE_CHAIN (DECL_SAVED_TREE (fn));
2199 DECL_SAVED_TREE (fn) = stmt;
2201 /* The line-number recorded in the outermost statement in a function
2202 is the line number of the end of the function. */
2203 STMT_LINENO (stmt) = lineno;
2204 STMT_LINENO_FOR_FN_P (stmt) = 1;
2207 /* We're about to expand T, a statement. Set up appropriate context
2208 for the substitution. */
2210 void
2211 prep_stmt (t)
2212 tree t;
2214 if (!STMT_LINENO_FOR_FN_P (t))
2215 lineno = STMT_LINENO (t);
2216 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2219 /* Some statements, like for-statements or if-statements, require a
2220 condition. This condition can be a declaration. If T is such a
2221 declaration it is processed, and an expression appropriate to use
2222 as the condition is returned. Otherwise, T itself is returned. */
2224 static tree
2225 expand_cond (t)
2226 tree t;
2228 if (t && TREE_CODE (t) == TREE_LIST)
2230 expand_stmt (TREE_PURPOSE (t));
2231 return TREE_VALUE (t);
2233 else
2234 return t;
2237 /* Generate RTL for the statement T, and its substatements, and any
2238 other statements at its nesting level. */
2240 tree
2241 expand_stmt (t)
2242 tree t;
2244 tree rval = NULL_TREE;
2246 while (t && t != error_mark_node)
2248 int saved_stmts_are_full_exprs_p;
2250 /* Assume we'll have nothing to return. */
2251 rval = NULL_TREE;
2253 /* Set up context appropriately for handling this statement. */
2254 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2255 prep_stmt (t);
2257 switch (TREE_CODE (t))
2259 case RETURN_STMT:
2260 finish_return_stmt (RETURN_EXPR (t));
2261 break;
2263 case EXPR_STMT:
2264 finish_expr_stmt (EXPR_STMT_EXPR (t));
2265 break;
2267 case DECL_STMT:
2269 tree decl;
2270 int i = suspend_momentary ();
2272 emit_line_note (input_filename, lineno);
2273 decl = DECL_STMT_DECL (t);
2274 /* If this is a declaration for an automatic local
2275 variable, initialize it. Note that we might also see a
2276 declaration for a namespace-scope object (declared with
2277 `extern'). We don't have to handle the initialization
2278 of those objects here; they can only be declarations,
2279 rather than definitions. */
2280 if (TREE_CODE (decl) == VAR_DECL
2281 && !TREE_STATIC (decl)
2282 && !DECL_EXTERNAL (decl))
2284 /* Let the back-end know about this variable. */
2285 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2286 emit_local_var (decl);
2287 else
2288 expand_anon_union_decl (decl, NULL_TREE,
2289 DECL_ANON_UNION_ELEMS (decl));
2291 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2292 rest_of_decl_compilation
2293 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
2294 /*top_level=*/0, /*at_end=*/0);
2296 resume_momentary (i);
2298 break;
2300 case CLEANUP_STMT:
2301 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2302 break;
2304 case START_CATCH_STMT:
2305 begin_catch_block (TREE_TYPE (t));
2306 break;
2308 case FOR_STMT:
2310 tree tmp;
2312 begin_for_stmt ();
2313 expand_stmt (FOR_INIT_STMT (t));
2314 finish_for_init_stmt (NULL_TREE);
2315 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2316 tmp = FOR_EXPR (t);
2317 finish_for_expr (tmp, NULL_TREE);
2318 expand_stmt (FOR_BODY (t));
2319 finish_for_stmt (tmp, NULL_TREE);
2321 break;
2323 case WHILE_STMT:
2325 begin_while_stmt ();
2326 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2327 expand_stmt (WHILE_BODY (t));
2328 finish_while_stmt (NULL_TREE);
2330 break;
2332 case DO_STMT:
2334 begin_do_stmt ();
2335 expand_stmt (DO_BODY (t));
2336 finish_do_body (NULL_TREE);
2337 finish_do_stmt (DO_COND (t), NULL_TREE);
2339 break;
2341 case IF_STMT:
2342 begin_if_stmt ();
2343 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2344 if (THEN_CLAUSE (t))
2346 expand_stmt (THEN_CLAUSE (t));
2347 finish_then_clause (NULL_TREE);
2349 if (ELSE_CLAUSE (t))
2351 begin_else_clause ();
2352 expand_stmt (ELSE_CLAUSE (t));
2353 finish_else_clause (NULL_TREE);
2355 finish_if_stmt ();
2356 break;
2358 case COMPOUND_STMT:
2359 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2360 expand_stmt (COMPOUND_BODY (t));
2361 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2362 NULL_TREE);
2363 break;
2365 case BREAK_STMT:
2366 finish_break_stmt ();
2367 break;
2369 case CONTINUE_STMT:
2370 finish_continue_stmt ();
2371 break;
2373 case SWITCH_STMT:
2375 tree cond;
2377 begin_switch_stmt ();
2378 cond = expand_cond (SWITCH_COND (t));
2379 finish_switch_cond (cond, NULL_TREE);
2380 expand_stmt (SWITCH_BODY (t));
2381 finish_switch_stmt (cond, NULL_TREE);
2383 break;
2385 case CASE_LABEL:
2386 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2387 break;
2389 case LABEL_STMT:
2390 expand_label (LABEL_STMT_LABEL (t));
2391 break;
2393 case GOTO_STMT:
2394 finish_goto_stmt (GOTO_DESTINATION (t));
2395 break;
2397 case ASM_STMT:
2398 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2399 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2400 break;
2402 case TRY_BLOCK:
2403 if (CLEANUP_P (t))
2405 expand_eh_region_start ();
2406 expand_stmt (TRY_STMTS (t));
2407 finish_cleanup_try_block (NULL_TREE);
2408 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2410 else
2412 if (FN_TRY_BLOCK_P (t))
2413 begin_function_try_block ();
2414 else
2415 begin_try_block ();
2417 expand_stmt (TRY_STMTS (t));
2419 if (FN_TRY_BLOCK_P (t))
2421 finish_function_try_block (NULL_TREE);
2422 expand_stmt (TRY_HANDLERS (t));
2423 finish_function_handler_sequence (NULL_TREE);
2425 else
2427 finish_try_block (NULL_TREE);
2428 expand_stmt (TRY_HANDLERS (t));
2429 finish_handler_sequence (NULL_TREE);
2432 break;
2434 case HANDLER:
2435 begin_handler ();
2436 expand_stmt (HANDLER_BODY (t));
2437 finish_handler (NULL_TREE, NULL_TREE);
2438 break;
2440 case SUBOBJECT:
2441 finish_subobject (SUBOBJECT_CLEANUP (t));
2442 break;
2444 case SCOPE_STMT:
2445 if (SCOPE_BEGIN_P (t))
2446 expand_start_bindings (2 * SCOPE_NULLIFIED_P (t));
2447 else if (SCOPE_END_P (t))
2448 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
2449 break;
2451 case RETURN_INIT:
2452 /* Clear this out so that finish_named_return_value can set it
2453 again. */
2454 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2455 finish_named_return_value (TREE_OPERAND (t, 0),
2456 TREE_OPERAND (t, 1));
2457 break;
2459 default:
2460 my_friendly_abort (19990810);
2461 break;
2464 /* Restore saved state. */
2465 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2467 /* Go on to the next statement in this scope. */
2468 t = TREE_CHAIN (t);
2471 return rval;
2474 /* Generate RTL for FN. */
2476 void
2477 expand_body (fn)
2478 tree fn;
2480 int saved_lineno;
2481 char *saved_input_filename;
2483 /* When the parser calls us after finishing the body of a template
2484 function, we don't really want to expand the body. When we're
2485 processing an in-class definition of an inline function,
2486 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2487 to look at the function itself. */
2488 if (processing_template_decl
2489 || (DECL_LANG_SPECIFIC (fn)
2490 && DECL_TEMPLATE_INFO (fn)
2491 && uses_template_parms (DECL_TI_ARGS (fn))))
2492 return;
2494 /* There's no reason to do any of the work here if we're only doing
2495 semantic analysis; this code just generates RTL. */
2496 if (flag_syntax_only)
2497 return;
2499 /* Save the current file name and line number. When we expand the
2500 body of the function, we'll set LINENO and INPUT_FILENAME so that
2501 error-mesages come out in the right places. */
2502 saved_lineno = lineno;
2503 saved_input_filename = input_filename;
2504 lineno = DECL_SOURCE_LINE (fn);
2505 input_filename = DECL_SOURCE_FILE (fn);
2507 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2508 store_parm_decls ();
2510 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2511 any of the other magic variables we set up when starting a
2512 function body. */
2513 current_function_name_declared = 1;
2515 /* Expand the body. */
2516 expand_stmt (DECL_SAVED_TREE (fn));
2518 /* Statements should always be full-expressions at the outermost set
2519 of curly braces for a function. */
2520 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2522 /* The outermost statement for a function contains the line number
2523 recorded when we finished processing the function. */
2524 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2526 /* Generate code for the function. */
2527 finish_function (lineno, 0);
2529 /* And restore the current source position. */
2530 lineno = saved_lineno;
2531 input_filename = saved_input_filename;