gcc
[official-gcc.git] / gcc / cp / semantics.c
blob9b2fa8763d648ed81cfd0e7b49dba7e66d91b904
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, 2000 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"
35 #include "ggc.h"
36 #include "rtl.h"
37 #include "output.h"
39 /* There routines provide a modular interface to perform many parsing
40 operations. They may therefore be used during actual parsing, or
41 during template instantiation, which may be regarded as a
42 degenerate form of parsing. Since the current g++ parser is
43 lacking in several respects, and will be reimplemented, we are
44 attempting to move most code that is not directly related to
45 parsing into this file; that will make implementing the new parser
46 much easier since it will be able to make use of these routines. */
48 static tree expand_cond PARAMS ((tree));
49 static tree maybe_convert_cond PARAMS ((tree));
50 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
52 /* Record the fact that STMT was the last statement added to the
53 statement tree. */
55 #define SET_LAST_STMT(stmt) \
56 (current_stmt_tree->x_last_stmt = (stmt))
58 /* When parsing a template, LAST_TREE contains the last statement
59 parsed. These are chained together through the TREE_CHAIN field,
60 but often need to be re-organized since the parse is performed
61 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
62 STMT. */
64 #define RECHAIN_STMTS(stmt, substmt) \
65 do { \
66 substmt = TREE_CHAIN (stmt); \
67 TREE_CHAIN (stmt) = NULL_TREE; \
68 SET_LAST_STMT (stmt); \
69 } while (0)
71 /* Finish processing the COND, the SUBSTMT condition for STMT. */
73 #define FINISH_COND(cond, stmt, substmt) \
74 do { \
75 if (last_tree != stmt) \
76 { \
77 RECHAIN_STMTS (stmt, substmt); \
78 if (!processing_template_decl) \
79 { \
80 cond = build_tree_list (substmt, cond); \
81 substmt = cond; \
82 } \
83 } \
84 else \
85 substmt = cond; \
86 } while (0)
88 /* T is a statement. Add it to the statement-tree. */
90 void
91 add_tree (t)
92 tree t;
94 /* Add T to the statement-tree. */
95 TREE_CHAIN (last_tree) = t;
96 SET_LAST_STMT (t);
98 /* When we expand a statement-tree, we must know whether or not the
99 statements are full-expresions. We record that fact here. */
100 if (building_stmt_tree ())
101 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
104 /* COND is the condition-expression for an if, while, etc.,
105 statement. Convert it to a boolean value, if appropriate. */
107 static tree
108 maybe_convert_cond (cond)
109 tree cond;
111 /* Empty conditions remain empty. */
112 if (!cond)
113 return NULL_TREE;
115 /* Wait until we instantiate templates before doing conversion. */
116 if (processing_template_decl)
117 return cond;
119 /* Do the conversion. */
120 cond = convert_from_reference (cond);
121 return condition_conversion (cond);
124 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
126 void
127 finish_expr_stmt (expr)
128 tree expr;
130 if (expr != NULL_TREE)
132 if (building_stmt_tree ())
134 /* Do default conversion if safe and possibly important,
135 in case within ({...}). */
136 if (!processing_template_decl
137 && !stmts_are_full_exprs_p
138 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
139 && lvalue_p (expr))
140 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
141 expr = default_conversion (expr);
143 if (!processing_template_decl)
144 expr = break_out_cleanups (expr);
146 add_tree (build_min_nt (EXPR_STMT, expr));
148 else
150 emit_line_note (input_filename, lineno);
152 if (stmts_are_full_exprs_p)
153 expand_start_target_temps ();
155 cplus_expand_expr_stmt (expr);
157 if (stmts_are_full_exprs_p)
158 expand_end_target_temps ();
162 finish_stmt ();
164 /* This was an expression-statement, so we save the type of the
165 expression. */
166 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
169 /* Begin an if-statement. Returns a newly created IF_STMT if
170 appropriate. */
172 tree
173 begin_if_stmt ()
175 tree r;
177 do_pushlevel ();
179 if (building_stmt_tree ())
181 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
182 add_tree (r);
184 else
185 r = NULL_TREE;
187 return r;
190 /* Process the COND of an if-statement, which may be given by
191 IF_STMT. */
193 void
194 finish_if_stmt_cond (cond, if_stmt)
195 tree cond;
196 tree if_stmt;
198 cond = maybe_convert_cond (cond);
200 if (building_stmt_tree ())
201 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
202 else
204 emit_line_note (input_filename, lineno);
205 expand_start_cond (cond, 0);
209 /* Finish the then-clause of an if-statement, which may be given by
210 IF_STMT. */
212 tree
213 finish_then_clause (if_stmt)
214 tree if_stmt;
216 if (building_stmt_tree ())
218 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
219 SET_LAST_STMT (if_stmt);
220 return if_stmt;
222 else
223 return NULL_TREE;
226 /* Begin the else-clause of an if-statement. */
228 void
229 begin_else_clause ()
231 if (!building_stmt_tree ())
232 expand_start_else ();
235 /* Finish the else-clause of an if-statement, which may be given by
236 IF_STMT. */
238 void
239 finish_else_clause (if_stmt)
240 tree if_stmt;
242 if (building_stmt_tree ())
243 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
246 /* Finsh an if-statement. */
248 void
249 finish_if_stmt ()
251 if (!building_stmt_tree ())
252 expand_end_cond ();
254 do_poplevel ();
255 finish_stmt ();
258 /* Begin a while-statement. Returns a newly created WHILE_STMT if
259 appropriate. */
261 tree
262 begin_while_stmt ()
264 tree r;
266 if (building_stmt_tree ())
268 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
269 add_tree (r);
271 else
273 emit_nop ();
274 emit_line_note (input_filename, lineno);
275 expand_start_loop (1);
276 r = NULL_TREE;
279 do_pushlevel ();
281 return r;
284 /* Process the COND of a while-statement, which may be given by
285 WHILE_STMT. */
287 void
288 finish_while_stmt_cond (cond, while_stmt)
289 tree cond;
290 tree while_stmt;
292 cond = maybe_convert_cond (cond);
294 if (building_stmt_tree ())
295 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
296 else
298 emit_line_note (input_filename, lineno);
299 expand_exit_loop_if_false (0, cond);
302 /* If COND wasn't a declaration, clear out the
303 block we made for it and start a new one here so the
304 optimization in expand_end_loop will work. */
305 if (getdecls () == NULL_TREE)
307 do_poplevel ();
308 do_pushlevel ();
312 /* Finish a while-statement, which may be given by WHILE_STMT. */
314 void
315 finish_while_stmt (while_stmt)
316 tree while_stmt;
318 do_poplevel ();
320 if (building_stmt_tree ())
321 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
322 else
323 expand_end_loop ();
324 finish_stmt ();
327 /* Begin a do-statement. Returns a newly created DO_STMT if
328 appropriate. */
330 tree
331 begin_do_stmt ()
333 if (building_stmt_tree ())
335 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
336 add_tree (r);
337 return r;
339 else
341 emit_nop ();
342 emit_line_note (input_filename, lineno);
343 expand_start_loop_continue_elsewhere (1);
344 return NULL_TREE;
348 /* Finish the body of a do-statement, which may be given by DO_STMT. */
350 void
351 finish_do_body (do_stmt)
352 tree do_stmt;
354 if (building_stmt_tree ())
355 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
356 else
357 expand_loop_continue_here ();
360 /* Finish a do-statement, which may be given by DO_STMT, and whose
361 COND is as indicated. */
363 void
364 finish_do_stmt (cond, do_stmt)
365 tree cond;
366 tree do_stmt;
368 cond = maybe_convert_cond (cond);
370 if (building_stmt_tree ())
371 DO_COND (do_stmt) = cond;
372 else
374 emit_line_note (input_filename, lineno);
375 expand_exit_loop_if_false (0, cond);
376 expand_end_loop ();
379 finish_stmt ();
382 /* Finish a return-statement. The EXPRESSION returned, if any, is as
383 indicated. */
385 void
386 finish_return_stmt (expr)
387 tree expr;
389 if (doing_semantic_analysis_p () && !processing_template_decl)
390 expr = check_return_expr (expr);
392 if (doing_semantic_analysis_p () && !processing_template_decl)
394 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
396 /* Even returns without a value in a constructor must return
397 `this'. We accomplish this by sending all returns in a
398 constructor to the CTOR_LABEL; finish_function emits code to
399 return a value there. When we finally generate the real
400 return statement, CTOR_LABEL is no longer set, and we fall
401 through into the normal return-processing code below. */
402 finish_goto_stmt (ctor_label);
403 return;
405 else if (DECL_DESTRUCTOR_P (current_function_decl))
407 /* Similarly, all destructors must run destructors for
408 base-classes before returning. So, all returns in a
409 destructor get sent to the DTOR_LABEL; finsh_function emits
410 code to return a value there. */
411 finish_goto_stmt (dtor_label);
412 return;
416 if (building_stmt_tree ())
417 add_tree (build_min_nt (RETURN_STMT, expr));
418 else
420 emit_line_note (input_filename, lineno);
421 c_expand_return (expr);
424 finish_stmt ();
427 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
429 tree
430 begin_for_stmt ()
432 tree r;
434 if (building_stmt_tree ())
436 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
437 NULL_TREE, NULL_TREE);
438 add_tree (r);
440 else
441 r = NULL_TREE;
443 if (flag_new_for_scope > 0)
445 do_pushlevel ();
446 note_level_for_for ();
449 return r;
452 /* Finish the for-init-statement of a for-statement, which may be
453 given by FOR_STMT. */
455 void
456 finish_for_init_stmt (for_stmt)
457 tree for_stmt;
459 if (building_stmt_tree ())
461 if (last_tree != for_stmt)
462 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
464 else
466 emit_nop ();
467 emit_line_note (input_filename, lineno);
468 expand_start_loop_continue_elsewhere (1);
471 do_pushlevel ();
474 /* Finish the COND of a for-statement, which may be given by
475 FOR_STMT. */
477 void
478 finish_for_cond (cond, for_stmt)
479 tree cond;
480 tree for_stmt;
482 cond = maybe_convert_cond (cond);
484 if (building_stmt_tree ())
485 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
486 else
488 emit_line_note (input_filename, lineno);
489 if (cond)
490 expand_exit_loop_if_false (0, cond);
493 /* If the cond wasn't a declaration, clear out the
494 block we made for it and start a new one here so the
495 optimization in expand_end_loop will work. */
496 if (getdecls () == NULL_TREE)
498 do_poplevel ();
499 do_pushlevel ();
503 /* Finish the increment-EXPRESSION in a for-statement, which may be
504 given by FOR_STMT. */
506 void
507 finish_for_expr (expr, for_stmt)
508 tree expr;
509 tree for_stmt;
511 if (building_stmt_tree ())
512 FOR_EXPR (for_stmt) = expr;
515 /* Finish the body of a for-statement, which may be given by
516 FOR_STMT. The increment-EXPR for the loop must be
517 provided. */
519 void
520 finish_for_stmt (expr, for_stmt)
521 tree expr;
522 tree for_stmt;
524 /* Pop the scope for the body of the loop. */
525 do_poplevel ();
527 if (building_stmt_tree ())
528 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
529 else
531 emit_line_note (input_filename, lineno);
532 expand_loop_continue_here ();
533 if (expr)
534 finish_expr_stmt (expr);
535 expand_end_loop ();
538 if (flag_new_for_scope > 0)
539 do_poplevel ();
541 finish_stmt ();
544 /* Finish a break-statement. */
546 void
547 finish_break_stmt ()
549 emit_line_note (input_filename, lineno);
550 if (building_stmt_tree ())
551 add_tree (build_min_nt (BREAK_STMT));
552 else if ( ! expand_exit_something ())
553 cp_error ("break statement not within loop or switch");
556 /* Finish a continue-statement. */
558 void
559 finish_continue_stmt ()
561 emit_line_note (input_filename, lineno);
562 if (building_stmt_tree ())
563 add_tree (build_min_nt (CONTINUE_STMT));
564 else if (! expand_continue_loop (0))
565 cp_error ("continue statement not within a loop");
568 /* Begin a switch-statement. Returns a new SWITCH_STMT if
569 appropriate. */
571 tree
572 begin_switch_stmt ()
574 tree r;
576 if (building_stmt_tree ())
578 r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
579 add_tree (r);
581 else
582 r = NULL_TREE;
584 do_pushlevel ();
586 return r;
589 /* Finish the cond of a switch-statement. */
591 void
592 finish_switch_cond (cond, switch_stmt)
593 tree cond;
594 tree switch_stmt;
596 if (building_stmt_tree ())
598 if (!processing_template_decl)
600 /* Convert the condition to an integer or enumeration type. */
601 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
602 if (cond == NULL_TREE)
604 error ("switch quantity not an integer");
605 cond = error_mark_node;
607 if (cond != error_mark_node)
609 tree idx;
610 tree type;
612 cond = default_conversion (cond);
613 type = TREE_TYPE (cond);
614 idx = get_unwidened (cond, 0);
615 /* We can't strip a conversion from a signed type to an unsigned,
616 because if we did, int_fits_type_p would do the wrong thing
617 when checking case values for being in range,
618 and it's too hard to do the right thing. */
619 if (TREE_UNSIGNED (TREE_TYPE (cond))
620 == TREE_UNSIGNED (TREE_TYPE (idx)))
621 cond = idx;
623 cond = fold (build1 (CLEANUP_POINT_EXPR, type, cond));
626 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
628 else if (cond != error_mark_node)
630 emit_line_note (input_filename, lineno);
631 c_expand_start_case (cond);
633 else
634 /* The code is in error, but we don't want expand_end_case to
635 crash. */
636 c_expand_start_case (boolean_false_node);
638 push_switch ();
641 /* Finish the body of a switch-statement, which may be given by
642 SWITCH_STMT. The COND to switch on is indicated. */
644 void
645 finish_switch_stmt (cond, switch_stmt)
646 tree cond;
647 tree switch_stmt;
649 if (building_stmt_tree ())
650 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
651 else
652 expand_end_case (cond);
653 pop_switch ();
654 do_poplevel ();
655 finish_stmt ();
658 /* Finish a case-label. */
660 void
661 finish_case_label (low_value, high_value)
662 tree low_value;
663 tree high_value;
665 if (building_stmt_tree ())
667 /* Add a representation for the case label to the statement
668 tree. */
669 add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
670 /* And warn about crossing initializations, etc. */
671 if (!processing_template_decl)
672 define_case_label ();
673 return;
676 do_case (low_value, high_value);
679 /* Finish a goto-statement. */
681 void
682 finish_goto_stmt (destination)
683 tree destination;
685 if (TREE_CODE (destination) == IDENTIFIER_NODE)
686 destination = lookup_label (destination);
688 /* We warn about unused labels with -Wunused. That means we have to
689 mark the used labels as used. */
690 if (TREE_CODE (destination) == LABEL_DECL)
691 TREE_USED (destination) = 1;
693 if (building_stmt_tree ())
695 if (TREE_CODE (destination) != LABEL_DECL)
696 /* We don't inline calls to functions with computed gotos.
697 Those functions are typically up to some funny business,
698 and may be depending on the labels being at particular
699 addresses, or some such. */
700 DECL_UNINLINABLE (current_function_decl) = 1;
702 add_tree (build_min_nt (GOTO_STMT, destination));
704 else
706 emit_line_note (input_filename, lineno);
708 if (TREE_CODE (destination) == LABEL_DECL)
710 label_rtx (destination);
711 expand_goto (destination);
713 else
714 expand_computed_goto (destination);
718 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
719 appropriate. */
721 tree
722 begin_try_block ()
724 if (building_stmt_tree ())
726 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
727 NULL_TREE);
728 add_tree (r);
729 return r;
731 else
733 emit_line_note (input_filename, lineno);
734 expand_start_try_stmts ();
735 return NULL_TREE;
739 /* Likewise, for a function-try-block. */
741 tree
742 begin_function_try_block ()
744 if (building_stmt_tree ())
746 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
747 NULL_TREE);
748 FN_TRY_BLOCK_P (r) = 1;
749 add_tree (r);
750 return r;
752 else
754 if (! current_function_parms_stored)
755 store_parm_decls ();
756 expand_start_early_try_stmts ();
757 return NULL_TREE;
761 /* Finish a try-block, which may be given by TRY_BLOCK. */
763 void
764 finish_try_block (try_block)
765 tree try_block;
767 if (building_stmt_tree ())
768 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
769 else
770 expand_start_all_catch ();
773 /* Finish the body of a cleanup try-block, which may be given by
774 TRY_BLOCK. */
776 void
777 finish_cleanup_try_block (try_block)
778 tree try_block;
780 if (building_stmt_tree ())
781 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
784 /* Finish an implicitly generated try-block, with a cleanup is given
785 by CLEANUP. */
787 void
788 finish_cleanup (cleanup, try_block)
789 tree cleanup;
790 tree try_block;
792 if (building_stmt_tree ())
794 TRY_HANDLERS (try_block) = cleanup;
795 CLEANUP_P (try_block) = 1;
797 else
798 expand_eh_region_end (protect_with_terminate (cleanup));
801 /* Likewise, for a function-try-block. */
803 void
804 finish_function_try_block (try_block)
805 tree try_block;
807 if (building_stmt_tree ())
809 if (TREE_CHAIN (try_block)
810 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
812 /* Chain the compound statement after the CTOR_INITIALIZER. */
813 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
814 /* And make the CTOR_INITIALIZER the body of the try-block. */
815 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
817 else
818 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
820 else
822 end_protect_partials ();
823 expand_start_all_catch ();
826 in_function_try_handler = 1;
829 /* Finish a handler-sequence for a try-block, which may be given by
830 TRY_BLOCK. */
832 void
833 finish_handler_sequence (try_block)
834 tree try_block;
836 if (building_stmt_tree ())
837 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
838 else
839 expand_end_all_catch ();
842 /* Likewise, for a function-try-block. */
844 void
845 finish_function_handler_sequence (try_block)
846 tree try_block;
848 in_function_try_handler = 0;
850 if (building_stmt_tree ())
851 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
852 else
853 expand_end_all_catch ();
856 /* Begin a handler. Returns a HANDLER if appropriate. */
858 tree
859 begin_handler ()
861 tree r;
863 if (building_stmt_tree ())
865 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
866 add_tree (r);
868 else
869 r = NULL_TREE;
871 do_pushlevel ();
873 return r;
876 /* Finish the handler-parameters for a handler, which may be given by
877 HANDLER. DECL is the declaration for the catch parameter, or NULL
878 if this is a `catch (...)' clause. */
880 tree
881 finish_handler_parms (decl, handler)
882 tree decl;
883 tree handler;
885 tree blocks = NULL_TREE;
887 if (processing_template_decl)
889 if (decl)
891 decl = pushdecl (decl);
892 decl = push_template_decl (decl);
893 add_decl_stmt (decl);
894 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
897 else if (building_stmt_tree ())
898 blocks = expand_start_catch_block (decl);
900 return blocks;
903 /* Note the beginning of a handler for TYPE. This function is called
904 at the point to which control should be transferred when an
905 appropriately-typed exception is thrown. */
907 void
908 begin_catch_block (type)
909 tree type;
911 if (building_stmt_tree ())
912 add_tree (build (START_CATCH_STMT, type));
913 else
914 start_catch_handler (type);
917 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
918 the return value from the matching call to finish_handler_parms. */
920 void
921 finish_handler (blocks, handler)
922 tree blocks;
923 tree handler;
925 if (!processing_template_decl)
927 if (building_stmt_tree ())
928 expand_end_catch_block (blocks);
930 if (!building_stmt_tree ())
932 /* Fall to outside the try statement when done executing
933 handler and we fall off end of handler. This is jump
934 Lresume in the documentation. */
935 expand_goto (top_label_entry (&caught_return_label_stack));
936 end_catch_handler ();
940 do_poplevel ();
942 if (building_stmt_tree ())
943 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
946 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
947 compound-statement does not define a scope. Returns a new
948 COMPOUND_STMT if appropriate. */
950 tree
951 begin_compound_stmt (has_no_scope)
952 int has_no_scope;
954 tree r;
956 if (building_stmt_tree ())
958 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
959 add_tree (r);
960 if (has_no_scope)
961 COMPOUND_STMT_NO_SCOPE (r) = 1;
963 else
964 r = NULL_TREE;
966 last_expr_type = NULL_TREE;
968 if (!has_no_scope)
969 do_pushlevel ();
970 else
971 /* Normally, we try hard to keep the BLOCK for a
972 statement-expression. But, if it's a statement-expression with
973 a scopeless block, there's nothing to keep, and we don't want
974 to accidentally keep a block *inside* the scopeless block. */
975 keep_next_level (0);
977 /* If this is the outermost block of the function, declare the
978 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
979 if (cfun
980 && !current_function_name_declared
981 && !has_no_scope)
983 current_function_name_declared = 1;
984 declare_function_name ();
987 return r;
991 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
992 If HAS_NO_SCOPE is non-zero, the compound statement does not define
993 a scope. */
995 tree
996 finish_compound_stmt (has_no_scope, compound_stmt)
997 int has_no_scope;
998 tree compound_stmt;
1000 tree r;
1001 tree t;
1003 if (!has_no_scope)
1004 r = do_poplevel ();
1005 else
1006 r = NULL_TREE;
1008 if (building_stmt_tree ())
1009 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1011 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1012 the precise purpose of that variable is store the type of the
1013 last expression statement within the last compound statement, we
1014 preserve the value. */
1015 t = last_expr_type;
1016 finish_stmt ();
1017 last_expr_type = t;
1019 return r;
1022 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1023 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1024 CLOBBERS. */
1026 void
1027 finish_asm_stmt (cv_qualifier, string, output_operands,
1028 input_operands, clobbers)
1029 tree cv_qualifier;
1030 tree string;
1031 tree output_operands;
1032 tree input_operands;
1033 tree clobbers;
1035 if (TREE_CHAIN (string))
1036 string = combine_strings (string);
1038 if (cv_qualifier != NULL_TREE
1039 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1041 cp_warning ("%s qualifier ignored on asm",
1042 IDENTIFIER_POINTER (cv_qualifier));
1043 cv_qualifier = NULL_TREE;
1046 if (building_stmt_tree ())
1048 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1049 output_operands, input_operands,
1050 clobbers);
1051 add_tree (r);
1053 else
1055 emit_line_note (input_filename, lineno);
1056 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1057 || clobbers != NULL_TREE)
1059 tree t;
1061 for (t = input_operands; t; t = TREE_CHAIN (t))
1062 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1064 c_expand_asm_operands (string, output_operands,
1065 input_operands,
1066 clobbers,
1067 cv_qualifier != NULL_TREE,
1068 input_filename, lineno);
1070 else
1071 expand_asm (string);
1073 finish_stmt ();
1077 /* Finish a label with the indicated NAME. */
1079 void
1080 finish_label_stmt (name)
1081 tree name;
1083 tree decl = define_label (input_filename, lineno, name);
1085 if (building_stmt_tree ())
1086 add_tree (build_min_nt (LABEL_STMT, decl));
1087 else if (decl)
1088 expand_label (decl);
1091 /* Finish a series of declarations for local labels. G++ allows users
1092 to declare "local" labels, i.e., labels with scope. This extension
1093 is useful when writing code involving statement-expressions. */
1095 void
1096 finish_label_decl (name)
1097 tree name;
1099 tree decl = declare_local_label (name);
1100 if (building_stmt_tree ())
1101 add_decl_stmt (decl);
1104 /* Create a declaration statement for the declaration given by the
1105 DECL. */
1107 void
1108 add_decl_stmt (decl)
1109 tree decl;
1111 tree decl_stmt;
1113 /* We need the type to last until instantiation time. */
1114 decl_stmt = build_min_nt (DECL_STMT, decl);
1115 add_tree (decl_stmt);
1118 /* We're in a constructor, and have just constructed a a subobject of
1119 *THIS. CLEANUP is code to run if an exception is thrown before the
1120 end of the current function is reached. */
1122 void
1123 finish_subobject (cleanup)
1124 tree cleanup;
1126 if (building_stmt_tree ())
1128 tree r = build_min_nt (SUBOBJECT, cleanup);
1129 add_tree (r);
1131 else
1132 add_partial_entry (cleanup);
1135 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1137 void
1138 finish_decl_cleanup (decl, cleanup)
1139 tree decl;
1140 tree cleanup;
1142 if (building_stmt_tree ())
1143 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1144 else if (!decl
1145 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1146 expand_decl_cleanup (decl, cleanup);
1149 /* Bind a name and initialization to the return value of
1150 the current function. */
1152 void
1153 finish_named_return_value (return_id, init)
1154 tree return_id, init;
1156 tree decl = DECL_RESULT (current_function_decl);
1158 if (pedantic)
1159 /* Give this error as many times as there are occurrences,
1160 so that users can use Emacs compilation buffers to find
1161 and fix all such places. */
1162 pedwarn ("ISO C++ does not permit named return values");
1164 if (return_id != NULL_TREE)
1166 if (DECL_NAME (decl) == NULL_TREE)
1168 DECL_NAME (decl) = return_id;
1169 DECL_ASSEMBLER_NAME (decl) = return_id;
1171 else
1173 cp_error ("return identifier `%D' already in place", return_id);
1174 return;
1178 /* Can't let this happen for constructors. */
1179 if (DECL_CONSTRUCTOR_P (current_function_decl))
1181 error ("can't redefine default return value for constructors");
1182 return;
1185 /* If we have a named return value, put that in our scope as well. */
1186 if (DECL_NAME (decl) != NULL_TREE)
1188 /* Let `cp_finish_decl' know that this initializer is ok. */
1189 DECL_INITIAL (decl) = init;
1190 if (doing_semantic_analysis_p ())
1191 pushdecl (decl);
1193 if (building_stmt_tree ())
1194 add_tree (build_min_nt (RETURN_INIT, return_id, init));
1195 else
1197 cp_finish_decl (decl, init, NULL_TREE, 0);
1198 store_return_init (decl);
1202 /* Don't use tree-inlining for functions with named return values.
1203 That doesn't work properly because we don't do any translation of
1204 the RETURN_INITs when they are copied. */
1205 DECL_UNINLINABLE (current_function_decl) = 1;
1208 /* Cache the value of this class's main virtual function table pointer
1209 in a register variable. This will save one indirection if a
1210 more than one virtual function call is made this function. */
1212 void
1213 setup_vtbl_ptr ()
1215 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1217 /* If we've already done this, there's no need to do it again. */
1218 if (vtbls_set_up_p)
1219 return;
1221 if (DECL_CONSTRUCTOR_P (current_function_decl))
1223 if (processing_template_decl)
1224 add_tree (build_min_nt
1225 (CTOR_INITIALIZER,
1226 current_member_init_list, current_base_init_list));
1227 else
1229 tree ctor_stmt;
1231 /* Mark the beginning of the constructor. */
1232 ctor_stmt = build_min_nt (CTOR_STMT);
1233 CTOR_BEGIN_P (ctor_stmt) = 1;
1234 add_tree (ctor_stmt);
1236 /* And actually initialize the base-classes and members. */
1237 finish_expr_stmt (emit_base_init (current_class_type));
1240 else if (DECL_DESTRUCTOR_P (current_function_decl)
1241 && !processing_template_decl)
1243 tree if_stmt;
1244 tree compound_stmt;
1245 int saved_cfnd;
1247 /* If the dtor is empty, and we know there is not possible way we
1248 could use any vtable entries, before they are possibly set by
1249 a base class dtor, we don't have to setup the vtables, as we
1250 know that any base class dtoring will set up any vtables it
1251 needs. We avoid MI, because one base class dtor can do a
1252 virtual dispatch to an overridden function that would need to
1253 have a non-related vtable set up, we cannot avoid setting up
1254 vtables in that case. We could change this to see if there is
1255 just one vtable. */
1256 if_stmt = begin_if_stmt ();
1258 /* If it is not safe to avoid setting up the vtables, then
1259 someone will change the condition to be boolean_true_node.
1260 (Actually, for now, we do not have code to set the condition
1261 appropriately, so we just assume that we always need to
1262 initialize the vtables.) */
1263 finish_if_stmt_cond (boolean_true_node, if_stmt);
1264 current_vcalls_possible_p = &IF_COND (if_stmt);
1266 /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1267 open the block for the if-body. */
1268 saved_cfnd = current_function_name_declared;
1269 current_function_name_declared = 1;
1270 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1271 current_function_name_declared = saved_cfnd;
1273 /* Make all virtual function table pointers in non-virtual base
1274 classes point to CURRENT_CLASS_TYPE's virtual function
1275 tables. */
1276 initialize_vtbl_ptrs (current_class_type,
1277 current_class_ptr);
1279 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1280 finish_then_clause (if_stmt);
1281 finish_if_stmt ();
1284 /* Always keep the BLOCK node associated with the outermost pair of
1285 curly braces of a function. These are needed for correct
1286 operation of dwarfout.c. */
1287 keep_next_level (1);
1289 /* The virtual function tables are set up now. */
1290 vtbls_set_up_p = 1;
1293 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
1294 whether this statements opens or closes a scope. PARTIAL_P is true
1295 for a partial scope, i.e, the scope that begins after a label when
1296 an object that needs a cleanup is created. If BEGIN_P is nonzero,
1297 returns a new TREE_LIST representing the top of the SCOPE_STMT
1298 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
1299 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1300 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1301 SCOPE_BEGIN_P set. */
1303 tree
1304 add_scope_stmt (begin_p, partial_p)
1305 int begin_p;
1306 int partial_p;
1308 tree ss;
1309 tree top;
1311 /* Build the statement. */
1312 ss = build_min_nt (SCOPE_STMT, NULL_TREE);
1313 SCOPE_BEGIN_P (ss) = begin_p;
1314 SCOPE_PARTIAL_P (ss) = partial_p;
1316 /* Keep the scope stack up to date. */
1317 if (begin_p)
1319 current_scope_stmt_stack
1320 = tree_cons (ss, NULL_TREE, current_scope_stmt_stack);
1321 top = current_scope_stmt_stack;
1323 else
1325 top = current_scope_stmt_stack;
1326 TREE_VALUE (top) = ss;
1327 current_scope_stmt_stack = TREE_CHAIN (top);
1330 /* Add the new statement to the statement-tree. */
1331 add_tree (ss);
1333 return top;
1336 /* Begin a new scope. */
1338 void
1339 do_pushlevel ()
1341 if (!building_stmt_tree ())
1343 emit_line_note (input_filename, lineno);
1344 clear_last_expr ();
1346 if (stmts_are_full_exprs_p)
1348 pushlevel (0);
1349 if (!building_stmt_tree ()
1350 && !cfun->x_whole_function_mode_p)
1351 my_friendly_abort (19991129);
1353 if (building_stmt_tree () && !processing_template_decl)
1354 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
1358 /* Finish a scope. */
1360 tree
1361 do_poplevel ()
1363 tree block = NULL_TREE;
1365 if (stmts_are_full_exprs_p)
1367 tree scope_stmts;
1369 if (building_stmt_tree () && !processing_template_decl)
1370 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
1371 else
1372 scope_stmts = NULL_TREE;
1374 block = poplevel (kept_level_p (), 1, 0);
1375 if (block && !processing_template_decl)
1377 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1378 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1382 return block;
1385 /* Finish a parenthesized expression EXPR. */
1387 tree
1388 finish_parenthesized_expr (expr)
1389 tree expr;
1391 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1392 /* This inhibits warnings in truthvalue_conversion. */
1393 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1395 return expr;
1398 /* Begin a statement-expression. The value returned must be passed to
1399 finish_stmt_expr. */
1401 tree
1402 begin_stmt_expr ()
1404 /* If we're outside a function, we won't have a statement-tree to
1405 work with. But, if we see a statement-expression we need to
1406 create one. */
1407 if (! cfun && !last_tree)
1408 begin_stmt_tree (&scope_chain->x_saved_tree);
1410 keep_next_level (1);
1411 /* If we're building a statement tree, then the upcoming compound
1412 statement will be chained onto the tree structure, starting at
1413 last_tree. We return last_tree so that we can later unhook the
1414 compound statement. */
1415 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
1418 /* Finish a statement-expression. RTL_EXPR should be the value
1419 returned by the previous begin_stmt_expr; EXPR is the
1420 statement-expression. Returns an expression representing the
1421 statement-expression. */
1423 tree
1424 finish_stmt_expr (rtl_expr)
1425 tree rtl_expr;
1427 tree result;
1429 if (!building_stmt_tree ())
1430 rtl_expr = expand_end_stmt_expr (rtl_expr);
1432 if (building_stmt_tree ())
1434 /* If the last thing in the statement-expression was not an
1435 expression-statement, then it has type `void'. */
1436 if (!last_expr_type)
1437 last_expr_type = void_type_node;
1438 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1439 TREE_SIDE_EFFECTS (result) = 1;
1441 /* Remove the compound statement from the tree structure; it is
1442 now saved in the STMT_EXPR. */
1443 SET_LAST_STMT (rtl_expr);
1444 TREE_CHAIN (last_tree) = NULL_TREE;
1446 else
1447 result = rtl_expr;
1449 /* If we created a statement-tree for this statement-expression,
1450 remove it now. */
1451 if (! cfun
1452 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1453 finish_stmt_tree (&scope_chain->x_saved_tree);
1455 return result;
1458 /* Finish a call to FN with ARGS. Returns a representation of the
1459 call. */
1461 tree
1462 finish_call_expr (fn, args, koenig)
1463 tree fn;
1464 tree args;
1465 int koenig;
1467 tree result;
1469 if (koenig)
1471 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1472 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1473 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1474 fn = do_identifier (fn, 2, args);
1476 result = build_x_function_call (fn, args, current_class_ref);
1478 if (TREE_CODE (result) == CALL_EXPR
1479 && (! TREE_TYPE (result)
1480 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1481 result = require_complete_type (result);
1483 return result;
1486 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1487 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1488 POSTDECREMENT_EXPR.) */
1490 tree
1491 finish_increment_expr (expr, code)
1492 tree expr;
1493 enum tree_code code;
1495 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1496 a COMPONENT_REF). This way if we've got, say, a reference to a
1497 static member that's being operated on, we don't end up trying to
1498 find a member operator for the class it's in. */
1500 if (TREE_CODE (expr) == OFFSET_REF)
1501 expr = resolve_offset_ref (expr);
1502 return build_x_unary_op (code, expr);
1505 /* Finish a use of `this'. Returns an expression for `this'. */
1507 tree
1508 finish_this_expr ()
1510 tree result;
1512 if (current_class_ptr)
1514 #ifdef WARNING_ABOUT_CCD
1515 TREE_USED (current_class_ptr) = 1;
1516 #endif
1517 result = current_class_ptr;
1519 else if (current_function_decl
1520 && DECL_STATIC_FUNCTION_P (current_function_decl))
1522 error ("`this' is unavailable for static member functions");
1523 result = error_mark_node;
1525 else
1527 if (current_function_decl)
1528 error ("invalid use of `this' in non-member function");
1529 else
1530 error ("invalid use of `this' at top level");
1531 result = error_mark_node;
1534 return result;
1537 /* Finish a member function call using OBJECT and ARGS as arguments to
1538 FN. Returns an expression for the call. */
1540 tree
1541 finish_object_call_expr (fn, object, args)
1542 tree fn;
1543 tree object;
1544 tree args;
1546 #if 0
1547 /* This is a future direction of this code, but because
1548 build_x_function_call cannot always undo what is done in
1549 build_component_ref entirely yet, we cannot do this. */
1551 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1552 return finish_call_expr (real_fn, args);
1553 #else
1554 if (DECL_DECLARES_TYPE_P (fn))
1556 if (processing_template_decl)
1557 /* This can happen on code like:
1559 class X;
1560 template <class T> void f(T t) {
1561 t.X();
1564 We just grab the underlying IDENTIFIER. */
1565 fn = DECL_NAME (fn);
1566 else
1568 cp_error ("calling type `%T' like a method", fn);
1569 return error_mark_node;
1573 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1574 #endif
1577 /* Finish a qualified member function call using OBJECT and ARGS as
1578 arguments to FN. Returns an expressino for the call. */
1580 tree
1581 finish_qualified_object_call_expr (fn, object, args)
1582 tree fn;
1583 tree object;
1584 tree args;
1586 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1587 TREE_OPERAND (fn, 1), args);
1590 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1591 being the scope, if any, of DESTRUCTOR. Returns an expression for
1592 the call. */
1594 tree
1595 finish_pseudo_destructor_call_expr (object, scope, destructor)
1596 tree object;
1597 tree scope;
1598 tree destructor;
1600 if (processing_template_decl)
1601 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1603 if (scope && scope != destructor)
1604 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1605 scope, destructor);
1607 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1608 && (TREE_CODE (TREE_TYPE (object)) !=
1609 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1610 cp_error ("`%E' is not of type `%T'", object, destructor);
1612 return cp_convert (void_type_node, object);
1615 /* Finish a call to a globally qualified member function FN using
1616 ARGS. Returns an expression for the call. */
1618 tree
1619 finish_qualified_call_expr (fn, args)
1620 tree fn;
1621 tree args;
1623 if (processing_template_decl)
1624 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1625 else
1626 return build_member_call (TREE_OPERAND (fn, 0),
1627 TREE_OPERAND (fn, 1),
1628 args);
1631 /* Finish an expression taking the address of LABEL. Returns an
1632 expression for the address. */
1634 tree
1635 finish_label_address_expr (label)
1636 tree label;
1638 tree result;
1640 label = lookup_label (label);
1641 if (label == NULL_TREE)
1642 result = null_pointer_node;
1643 else
1645 TREE_USED (label) = 1;
1646 result = build1 (ADDR_EXPR, ptr_type_node, label);
1647 TREE_CONSTANT (result) = 1;
1648 /* This function cannot be inlined. All jumps to the addressed
1649 label should wind up at the same point. */
1650 DECL_UNINLINABLE (current_function_decl) = 1;
1653 return result;
1656 /* Finish an expression of the form CODE EXPR. */
1658 tree
1659 finish_unary_op_expr (code, expr)
1660 enum tree_code code;
1661 tree expr;
1663 tree result = build_x_unary_op (code, expr);
1664 /* Inside a template, build_x_unary_op does not fold the
1665 expression. So check whether the result is folded before
1666 setting TREE_NEGATED_INT. */
1667 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1668 && TREE_CODE (result) == INTEGER_CST
1669 && !TREE_UNSIGNED (TREE_TYPE (result))
1670 && INT_CST_LT (result, integer_zero_node))
1671 TREE_NEGATED_INT (result) = 1;
1672 overflow_warning (result);
1673 return result;
1676 /* Finish an id-expression. */
1678 tree
1679 finish_id_expr (expr)
1680 tree expr;
1682 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1683 expr = do_identifier (expr, 1, NULL_TREE);
1685 return expr;
1688 static tree current_type_lookups;
1690 /* Perform deferred access control for types used in the type of a
1691 declaration. */
1693 static void
1694 deferred_type_access_control ()
1696 tree lookup = type_lookups;
1698 if (lookup == error_mark_node)
1699 return;
1701 for (; lookup; lookup = TREE_CHAIN (lookup))
1702 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1705 void
1706 decl_type_access_control (decl)
1707 tree decl;
1709 tree save_fn;
1711 if (type_lookups == error_mark_node)
1712 return;
1714 save_fn = current_function_decl;
1716 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1717 current_function_decl = decl;
1719 deferred_type_access_control ();
1721 current_function_decl = save_fn;
1723 /* Now strip away the checks for the current declarator; they were
1724 added to type_lookups after typed_declspecs saved the copy that
1725 ended up in current_type_lookups. */
1726 type_lookups = current_type_lookups;
1729 void
1730 save_type_access_control (lookups)
1731 tree lookups;
1733 current_type_lookups = lookups;
1736 /* Begin a function definition declared with DECL_SPECS and
1737 DECLARATOR. Returns non-zero if the function-declaration is
1738 legal. */
1741 begin_function_definition (decl_specs, declarator)
1742 tree decl_specs;
1743 tree declarator;
1745 tree specs;
1746 tree attrs;
1748 split_specs_attrs (decl_specs, &specs, &attrs);
1749 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1750 return 0;
1752 deferred_type_access_control ();
1753 type_lookups = error_mark_node;
1755 reinit_parse_for_function ();
1756 /* The things we're about to see are not directly qualified by any
1757 template headers we've seen thus far. */
1758 reset_specialization ();
1760 return 1;
1763 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1764 a SCOPE_REF. */
1766 tree
1767 begin_constructor_declarator (scope, name)
1768 tree scope;
1769 tree name;
1771 tree result = build_parse_node (SCOPE_REF, scope, name);
1772 enter_scope_of (result);
1773 return result;
1776 /* Finish an init-declarator. Returns a DECL. */
1778 tree
1779 finish_declarator (declarator, declspecs, attributes,
1780 prefix_attributes, initialized)
1781 tree declarator;
1782 tree declspecs;
1783 tree attributes;
1784 tree prefix_attributes;
1785 int initialized;
1787 return start_decl (declarator, declspecs, initialized, attributes,
1788 prefix_attributes);
1791 /* Finish a translation unit. */
1793 void
1794 finish_translation_unit ()
1796 /* In case there were missing closebraces,
1797 get us back to the global binding level. */
1798 pop_everything ();
1799 while (current_namespace != global_namespace)
1800 pop_namespace ();
1801 finish_file ();
1804 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1805 Returns the parameter. */
1807 tree
1808 finish_template_type_parm (aggr, identifier)
1809 tree aggr;
1810 tree identifier;
1812 if (aggr != class_type_node)
1814 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1815 aggr = class_type_node;
1818 return build_tree_list (aggr, identifier);
1821 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1822 Returns the parameter. */
1824 tree
1825 finish_template_template_parm (aggr, identifier)
1826 tree aggr;
1827 tree identifier;
1829 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1830 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1831 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1832 DECL_TEMPLATE_RESULT (tmpl) = decl;
1833 SET_DECL_ARTIFICIAL (decl);
1834 end_template_decl ();
1836 return finish_template_type_parm (aggr, tmpl);
1839 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1840 non-zero, the parameter list was terminated by a `...'. */
1842 tree
1843 finish_parmlist (parms, ellipsis)
1844 tree parms;
1845 int ellipsis;
1847 if (!ellipsis)
1848 chainon (parms, void_list_node);
1849 /* We mark the PARMS as a parmlist so that declarator processing can
1850 disambiguate certain constructs. */
1851 if (parms != NULL_TREE)
1852 TREE_PARMLIST (parms) = 1;
1854 return parms;
1857 /* Begin a class definition, as indicated by T. */
1859 tree
1860 begin_class_definition (t)
1861 tree t;
1863 if (t == error_mark_node
1864 || ! IS_AGGR_TYPE (t))
1866 t = make_aggr_type (RECORD_TYPE);
1867 pushtag (make_anon_name (), t, 0);
1870 /* In a definition of a member class template, we will get here with an
1871 implicit typename, a TYPENAME_TYPE with a type. */
1872 if (TREE_CODE (t) == TYPENAME_TYPE)
1873 t = TREE_TYPE (t);
1875 /* If we generated a partial instantiation of this type, but now
1876 we're seeing a real definition, we're actually looking at a
1877 partial specialization. Consider:
1879 template <class T, class U>
1880 struct Y {};
1882 template <class T>
1883 struct X {};
1885 template <class T, class U>
1886 void f()
1888 typename X<Y<T, U> >::A a;
1891 template <class T, class U>
1892 struct X<Y<T, U> >
1896 We have to undo the effects of the previous partial
1897 instantiation. */
1898 if (PARTIAL_INSTANTIATION_P (t))
1900 if (!pedantic)
1902 /* Unfortunately, when we're not in pedantic mode, we
1903 attempt to actually fill in some of the fields of the
1904 partial instantiation, in order to support the implicit
1905 typename extension. Clear those fields now, in
1906 preparation for the definition here. The fields cleared
1907 here must match those set in instantiate_class_template.
1908 Look for a comment mentioning begin_class_definition
1909 there. */
1910 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1911 TYPE_FIELDS (t) = NULL_TREE;
1912 TYPE_METHODS (t) = NULL_TREE;
1913 CLASSTYPE_TAGS (t) = NULL_TREE;
1914 TYPE_SIZE (t) = NULL_TREE;
1917 /* This isn't a partial instantiation any more. */
1918 PARTIAL_INSTANTIATION_P (t) = 0;
1920 /* If this type was already complete, and we see another definition,
1921 that's an error. */
1922 else if (COMPLETE_TYPE_P (t))
1923 duplicate_tag_error (t);
1925 /* Update the location of the decl. */
1926 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1927 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1929 if (TYPE_BEING_DEFINED (t))
1931 t = make_aggr_type (TREE_CODE (t));
1932 pushtag (TYPE_IDENTIFIER (t), t, 0);
1934 maybe_process_partial_specialization (t);
1935 pushclass (t, 1);
1936 TYPE_BEING_DEFINED (t) = 1;
1937 /* Reset the interface data, at the earliest possible
1938 moment, as it might have been set via a class foo;
1939 before. */
1941 tree name = TYPE_IDENTIFIER (t);
1943 if (! ANON_AGGRNAME_P (name))
1945 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1946 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1947 (t, interface_unknown);
1950 /* Only leave this bit clear if we know this
1951 class is part of an interface-only specification. */
1952 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1953 || ! CLASSTYPE_INTERFACE_ONLY (t))
1954 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1956 #if 0
1957 tmp = TYPE_IDENTIFIER ($<ttype>0);
1958 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1959 overload_template_name (tmp, 1);
1960 #endif
1961 reset_specialization();
1963 /* Make a declaration for this class in its own scope. */
1964 build_self_reference ();
1966 return t;
1969 /* Finish the member declaration given by DECL. */
1971 void
1972 finish_member_declaration (decl)
1973 tree decl;
1975 if (decl == error_mark_node || decl == NULL_TREE)
1976 return;
1978 if (decl == void_type_node)
1979 /* The COMPONENT was a friend, not a member, and so there's
1980 nothing for us to do. */
1981 return;
1983 /* We should see only one DECL at a time. */
1984 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1986 /* Set up access control for DECL. */
1987 TREE_PRIVATE (decl)
1988 = (current_access_specifier == access_private_node);
1989 TREE_PROTECTED (decl)
1990 = (current_access_specifier == access_protected_node);
1991 if (TREE_CODE (decl) == TEMPLATE_DECL)
1993 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1994 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1997 /* Mark the DECL as a member of the current class. */
1998 DECL_CONTEXT (decl) = current_class_type;
2000 /* Put functions on the TYPE_METHODS list and everything else on the
2001 TYPE_FIELDS list. Note that these are built up in reverse order.
2002 We reverse them (to obtain declaration order) in finish_struct. */
2003 if (TREE_CODE (decl) == FUNCTION_DECL
2004 || DECL_FUNCTION_TEMPLATE_P (decl))
2006 /* We also need to add this function to the
2007 CLASSTYPE_METHOD_VEC. */
2008 add_method (current_class_type, 0, decl);
2010 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2011 TYPE_METHODS (current_class_type) = decl;
2013 else
2015 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2016 go at the beginning. The reason is that lookup_field_1
2017 searches the list in order, and we want a field name to
2018 override a type name so that the "struct stat hack" will
2019 work. In particular:
2021 struct S { enum E { }; int E } s;
2022 s.E = 3;
2024 is legal. In addition, the FIELD_DECLs must be maintained in
2025 declaration order so that class layout works as expected.
2026 However, we don't need that order until class layout, so we
2027 save a little time by putting FIELD_DECLs on in reverse order
2028 here, and then reversing them in finish_struct_1. (We could
2029 also keep a pointer to the correct insertion points in the
2030 list.) */
2032 if (TREE_CODE (decl) == TYPE_DECL)
2033 TYPE_FIELDS (current_class_type)
2034 = chainon (TYPE_FIELDS (current_class_type), decl);
2035 else
2037 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2038 TYPE_FIELDS (current_class_type) = decl;
2041 /* Enter the DECL into the scope of the class. */
2042 if (TREE_CODE (decl) != USING_DECL)
2043 pushdecl_class_level (decl);
2047 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
2048 the definition is immediately followed by a semicolon. Returns the
2049 type. */
2051 tree
2052 finish_class_definition (t, attributes, semi, pop_scope_p)
2053 tree t;
2054 tree attributes;
2055 int semi;
2056 int pop_scope_p;
2058 /* finish_struct nukes this anyway; if finish_exception does too,
2059 then it can go. */
2060 if (semi)
2061 note_got_semicolon (t);
2063 /* If we got any attributes in class_head, xref_tag will stick them in
2064 TREE_TYPE of the type. Grab them now. */
2065 attributes = chainon (TREE_TYPE (t), attributes);
2066 TREE_TYPE (t) = NULL_TREE;
2068 if (TREE_CODE (t) == ENUMERAL_TYPE)
2070 else
2072 t = finish_struct (t, attributes);
2073 if (semi)
2074 note_got_semicolon (t);
2077 if (! semi)
2078 check_for_missing_semicolon (t);
2079 if (pop_scope_p)
2080 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2081 if (current_scope () == current_function_decl)
2082 do_pending_defargs ();
2084 return t;
2087 /* Finish processing the default argument expressions cached during
2088 the processing of a class definition. */
2090 void
2091 begin_inline_definitions ()
2093 if (pending_inlines
2094 && current_scope () == current_function_decl)
2095 do_pending_inlines ();
2098 /* Finish processing the inline function definitions cached during the
2099 processing of a class definition. */
2101 void
2102 finish_inline_definitions ()
2104 if (current_class_type == NULL_TREE)
2105 clear_inline_text_obstack ();
2108 /* Finish processing the declaration of a member class template
2109 TYPES whose template parameters are given by PARMS. */
2111 tree
2112 finish_member_class_template (types)
2113 tree types;
2115 tree t;
2117 /* If there are declared, but undefined, partial specializations
2118 mixed in with the typespecs they will not yet have passed through
2119 maybe_process_partial_specialization, so we do that here. */
2120 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2121 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2122 maybe_process_partial_specialization (TREE_VALUE (t));
2124 note_list_got_semicolon (types);
2125 grok_x_components (types);
2126 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2127 /* The component was in fact a friend declaration. We avoid
2128 finish_member_template_decl performing certain checks by
2129 unsetting TYPES. */
2130 types = NULL_TREE;
2132 finish_member_template_decl (types);
2134 /* As with other component type declarations, we do
2135 not store the new DECL on the list of
2136 component_decls. */
2137 return NULL_TREE;
2140 /* Finish processsing a complete template declaration. The PARMS are
2141 the template parameters. */
2143 void
2144 finish_template_decl (parms)
2145 tree parms;
2147 if (parms)
2148 end_template_decl ();
2149 else
2150 end_specialization ();
2153 /* Finish processing a a template-id (which names a type) of the form
2154 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2155 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2156 the scope of template-id indicated. */
2158 tree
2159 finish_template_type (name, args, entering_scope)
2160 tree name;
2161 tree args;
2162 int entering_scope;
2164 tree decl;
2166 decl = lookup_template_class (name, args,
2167 NULL_TREE, NULL_TREE, entering_scope);
2168 if (decl != error_mark_node)
2169 decl = TYPE_STUB_DECL (decl);
2171 return decl;
2174 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2175 namespace scope or a class scope. */
2177 void
2178 enter_scope_of (sr)
2179 tree sr;
2181 tree scope = TREE_OPERAND (sr, 0);
2183 if (TREE_CODE (scope) == NAMESPACE_DECL)
2185 push_decl_namespace (scope);
2186 TREE_COMPLEXITY (sr) = -1;
2188 else if (scope != current_class_type)
2190 if (TREE_CODE (scope) == TYPENAME_TYPE)
2192 /* In a declarator for a template class member, the scope will
2193 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2194 scope = TREE_TYPE (scope);
2195 TREE_OPERAND (sr, 0) = scope;
2197 push_nested_class (scope, 3);
2198 TREE_COMPLEXITY (sr) = current_class_depth;
2202 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2203 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2204 BASE_CLASS, or NULL_TREE if an error occurred. The
2205 ACCESSS_SPECIFIER is one of
2206 access_{default,public,protected_private}[_virtual]_node.*/
2208 tree
2209 finish_base_specifier (access_specifier, base_class)
2210 tree access_specifier;
2211 tree base_class;
2213 tree type;
2214 tree result;
2216 if (base_class == NULL_TREE)
2218 error ("invalid base class");
2219 type = error_mark_node;
2221 else
2222 type = TREE_TYPE (base_class);
2224 if (! is_aggr_type (type, 1))
2225 result = NULL_TREE;
2226 else
2227 result = build_tree_list (access_specifier, type);
2229 return result;
2232 /* Called when multiple declarators are processed. If that is not
2233 premitted in this context, an error is issued. */
2235 void
2236 check_multiple_declarators ()
2238 /* [temp]
2240 In a template-declaration, explicit specialization, or explicit
2241 instantiation the init-declarator-list in the declaration shall
2242 contain at most one declarator.
2244 We don't just use PROCESSING_TEMPLATE_DECL for the first
2245 condition since that would disallow the perfectly legal code,
2246 like `template <class T> struct S { int i, j; };'. */
2247 tree scope = current_scope ();
2249 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2250 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2251 return;
2253 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2254 || processing_explicit_instantiation
2255 || processing_specialization)
2256 cp_error ("multiple declarators in template declaration");
2259 tree
2260 finish_typeof (expr)
2261 tree expr;
2263 if (processing_template_decl)
2265 tree t;
2267 t = make_aggr_type (TYPEOF_TYPE);
2268 TYPE_FIELDS (t) = expr;
2270 return t;
2273 return TREE_TYPE (expr);
2276 /* Create an empty statement tree rooted at T. */
2278 void
2279 begin_stmt_tree (t)
2280 tree *t;
2282 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2283 what follows. We remove the extraneous statement in
2284 finish_stmt_tree. */
2285 *t = build_nt (EXPR_STMT, void_zero_node);
2286 SET_LAST_STMT (*t);
2287 last_expr_type = NULL_TREE;
2290 /* Finish the statement tree rooted at T. */
2292 void
2293 finish_stmt_tree (t)
2294 tree *t;
2296 tree stmt;
2298 /* Remove the fake extra statement added in begin_stmt_tree. */
2299 stmt = TREE_CHAIN (*t);
2300 *t = stmt;
2301 SET_LAST_STMT (NULL_TREE);
2303 if (cfun)
2305 /* The line-number recorded in the outermost statement in a function
2306 is the line number of the end of the function. */
2307 STMT_LINENO (stmt) = lineno;
2308 STMT_LINENO_FOR_FN_P (stmt) = 1;
2312 /* We're about to expand T, a statement. Set up appropriate context
2313 for the substitution. */
2315 void
2316 prep_stmt (t)
2317 tree t;
2319 if (!STMT_LINENO_FOR_FN_P (t))
2320 lineno = STMT_LINENO (t);
2321 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2324 /* Some statements, like for-statements or if-statements, require a
2325 condition. This condition can be a declaration. If T is such a
2326 declaration it is processed, and an expression appropriate to use
2327 as the condition is returned. Otherwise, T itself is returned. */
2329 static tree
2330 expand_cond (t)
2331 tree t;
2333 if (t && TREE_CODE (t) == TREE_LIST)
2335 expand_stmt (TREE_PURPOSE (t));
2336 return TREE_VALUE (t);
2338 else
2339 return t;
2342 /* Generate RTL for the statement T, and its substatements, and any
2343 other statements at its nesting level. */
2345 tree
2346 expand_stmt (t)
2347 tree t;
2349 tree rval = NULL_TREE;
2351 while (t && t != error_mark_node)
2353 int saved_stmts_are_full_exprs_p;
2355 /* Assume we'll have nothing to return. */
2356 rval = NULL_TREE;
2358 /* Set up context appropriately for handling this statement. */
2359 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2360 prep_stmt (t);
2362 switch (TREE_CODE (t))
2364 case RETURN_STMT:
2365 finish_return_stmt (RETURN_EXPR (t));
2366 break;
2368 case EXPR_STMT:
2369 finish_expr_stmt (EXPR_STMT_EXPR (t));
2370 break;
2372 case DECL_STMT:
2374 tree decl;
2376 emit_line_note (input_filename, lineno);
2377 decl = DECL_STMT_DECL (t);
2378 /* If this is a declaration for an automatic local
2379 variable, initialize it. Note that we might also see a
2380 declaration for a namespace-scope object (declared with
2381 `extern'). We don't have to handle the initialization
2382 of those objects here; they can only be declarations,
2383 rather than definitions. */
2384 if (TREE_CODE (decl) == VAR_DECL
2385 && !TREE_STATIC (decl)
2386 && !DECL_EXTERNAL (decl))
2388 /* Let the back-end know about this variable. */
2389 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2390 emit_local_var (decl);
2391 else
2392 expand_anon_union_decl (decl, NULL_TREE,
2393 DECL_ANON_UNION_ELEMS (decl));
2395 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2397 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
2398 /* Do not emit unused decls. This is not just an
2399 optimization. We really do not want to emit
2400 __PRETTY_FUNCTION__ etc, if they're never used. */
2401 DECL_IGNORED_P (decl) = 1;
2402 else
2403 make_rtl_for_local_static (decl);
2406 break;
2408 case CLEANUP_STMT:
2409 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2410 break;
2412 case START_CATCH_STMT:
2413 begin_catch_block (TREE_TYPE (t));
2414 break;
2416 case CTOR_STMT:
2417 if (CTOR_BEGIN_P (t))
2418 begin_protect_partials ();
2419 else
2420 /* After this point, any exceptions will cause the
2421 destructor to be executed, so we no longer need to worry
2422 about destroying the various subobjects ourselves. */
2423 end_protect_partials ();
2424 break;
2426 case FOR_STMT:
2428 tree tmp;
2430 begin_for_stmt ();
2431 expand_stmt (FOR_INIT_STMT (t));
2432 finish_for_init_stmt (NULL_TREE);
2433 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2434 tmp = FOR_EXPR (t);
2435 finish_for_expr (tmp, NULL_TREE);
2436 expand_stmt (FOR_BODY (t));
2437 finish_for_stmt (tmp, NULL_TREE);
2439 break;
2441 case WHILE_STMT:
2443 begin_while_stmt ();
2444 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2445 expand_stmt (WHILE_BODY (t));
2446 finish_while_stmt (NULL_TREE);
2448 break;
2450 case DO_STMT:
2452 begin_do_stmt ();
2453 expand_stmt (DO_BODY (t));
2454 finish_do_body (NULL_TREE);
2455 finish_do_stmt (DO_COND (t), NULL_TREE);
2457 break;
2459 case IF_STMT:
2460 begin_if_stmt ();
2461 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2462 if (THEN_CLAUSE (t))
2464 expand_stmt (THEN_CLAUSE (t));
2465 finish_then_clause (NULL_TREE);
2467 if (ELSE_CLAUSE (t))
2469 begin_else_clause ();
2470 expand_stmt (ELSE_CLAUSE (t));
2471 finish_else_clause (NULL_TREE);
2473 finish_if_stmt ();
2474 break;
2476 case COMPOUND_STMT:
2477 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2478 expand_stmt (COMPOUND_BODY (t));
2479 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2480 NULL_TREE);
2481 break;
2483 case BREAK_STMT:
2484 finish_break_stmt ();
2485 break;
2487 case CONTINUE_STMT:
2488 finish_continue_stmt ();
2489 break;
2491 case SWITCH_STMT:
2493 tree cond;
2495 begin_switch_stmt ();
2496 cond = expand_cond (SWITCH_COND (t));
2497 finish_switch_cond (cond, NULL_TREE);
2498 expand_stmt (SWITCH_BODY (t));
2499 finish_switch_stmt (cond, NULL_TREE);
2501 break;
2503 case CASE_LABEL:
2504 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2505 break;
2507 case LABEL_STMT:
2508 expand_label (LABEL_STMT_LABEL (t));
2509 break;
2511 case GOTO_STMT:
2512 finish_goto_stmt (GOTO_DESTINATION (t));
2513 break;
2515 case ASM_STMT:
2516 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2517 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2518 break;
2520 case TRY_BLOCK:
2521 if (CLEANUP_P (t))
2523 expand_eh_region_start ();
2524 expand_stmt (TRY_STMTS (t));
2525 finish_cleanup_try_block (NULL_TREE);
2526 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2528 else
2530 if (FN_TRY_BLOCK_P (t))
2531 begin_function_try_block ();
2532 else
2533 begin_try_block ();
2535 expand_stmt (TRY_STMTS (t));
2537 if (FN_TRY_BLOCK_P (t))
2539 finish_function_try_block (NULL_TREE);
2540 expand_stmt (TRY_HANDLERS (t));
2541 finish_function_handler_sequence (NULL_TREE);
2543 else
2545 finish_try_block (NULL_TREE);
2546 expand_stmt (TRY_HANDLERS (t));
2547 finish_handler_sequence (NULL_TREE);
2550 break;
2552 case HANDLER:
2553 begin_handler ();
2554 expand_stmt (HANDLER_BODY (t));
2555 finish_handler (NULL_TREE, NULL_TREE);
2556 break;
2558 case SUBOBJECT:
2559 finish_subobject (SUBOBJECT_CLEANUP (t));
2560 break;
2562 case SCOPE_STMT:
2563 if (!SCOPE_NO_CLEANUPS_P (t))
2565 if (SCOPE_BEGIN_P (t))
2566 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
2567 SCOPE_STMT_BLOCK (t));
2568 else if (SCOPE_END_P (t))
2569 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t),
2570 SCOPE_PARTIAL_P (t));
2572 else if (!SCOPE_NULLIFIED_P (t))
2574 rtx note = emit_note (NULL,
2575 (SCOPE_BEGIN_P (t)
2576 ? NOTE_INSN_BLOCK_BEG
2577 : NOTE_INSN_BLOCK_END));
2578 NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
2581 break;
2583 case RETURN_INIT:
2584 /* Clear this out so that finish_named_return_value can set it
2585 again. */
2586 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2587 finish_named_return_value (TREE_OPERAND (t, 0),
2588 TREE_OPERAND (t, 1));
2589 break;
2591 default:
2592 my_friendly_abort (19990810);
2593 break;
2596 /* Restore saved state. */
2597 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2599 /* Go on to the next statement in this scope. */
2600 t = TREE_CHAIN (t);
2603 return rval;
2606 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2607 will equivalent CALL_EXPRs. */
2609 static tree
2610 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2611 tree *tp;
2612 int *walk_subtrees ATTRIBUTE_UNUSED;
2613 void *data ATTRIBUTE_UNUSED;
2615 tree aggr_init_expr;
2616 tree call_expr;
2617 tree fn;
2618 tree args;
2619 tree slot;
2620 tree type;
2621 tree call_type;
2622 int copy_from_buffer_p;
2624 aggr_init_expr = *tp;
2625 /* We don't need to walk into types; there's nothing in a type that
2626 needs simplification. (And, furthermore, there are places we
2627 actively don't want to go. For example, we don't want to wander
2628 into the default arguments for a FUNCTION_DECL that appears in a
2629 CALL_EXPR.) */
2630 if (TYPE_P (aggr_init_expr))
2632 *walk_subtrees = 0;
2633 return NULL_TREE;
2635 /* Only AGGR_INIT_EXPRs are interesting. */
2636 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2637 return NULL_TREE;
2639 /* Form an appropriate CALL_EXPR. */
2640 fn = TREE_OPERAND (aggr_init_expr, 0);
2641 args = TREE_OPERAND (aggr_init_expr, 1);
2642 slot = TREE_OPERAND (aggr_init_expr, 2);
2643 type = TREE_TYPE (aggr_init_expr);
2644 call_type = type;
2645 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2647 /* Replace the first argument with the address of the third
2648 argument to the AGGR_INIT_EXPR. */
2649 call_type = build_pointer_type (type);
2650 mark_addressable (slot);
2651 args = tree_cons (NULL_TREE, build1 (ADDR_EXPR, call_type, slot),
2652 TREE_CHAIN (args));
2654 call_expr = build (CALL_EXPR, call_type, fn, args, NULL_TREE);
2655 TREE_SIDE_EFFECTS (call_expr) = 1;
2657 /* If we're using the non-reentrant PCC calling convention, then we
2658 need to copy the returned value out of the static buffer into the
2659 SLOT. */
2660 copy_from_buffer_p = 0;
2661 #ifdef PCC_STATIC_STRUCT_RETURN
2662 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2664 int old_ac;
2666 flag_access_control = 0;
2667 call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2668 flag_access_control = old_ac;
2669 copy_from_buffer_p = 1;
2671 #endif
2673 /* If this AGGR_INIT_EXPR indicates the value returned by a
2674 function, then we want to use the value of the initialized
2675 location as the result. */
2676 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2678 call_expr = build (COMPOUND_EXPR, type,
2679 call_expr, slot);
2680 TREE_SIDE_EFFECTS (call_expr) = 1;
2683 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2684 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2685 *tp = call_expr;
2687 /* Keep iterating. */
2688 return NULL_TREE;
2691 /* Generate RTL for FN. */
2693 void
2694 expand_body (fn)
2695 tree fn;
2697 int saved_lineno;
2698 char *saved_input_filename;
2700 /* When the parser calls us after finishing the body of a template
2701 function, we don't really want to expand the body. When we're
2702 processing an in-class definition of an inline function,
2703 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2704 to look at the function itself. */
2705 if (processing_template_decl
2706 || (DECL_LANG_SPECIFIC (fn)
2707 && DECL_TEMPLATE_INFO (fn)
2708 && uses_template_parms (DECL_TI_ARGS (fn))))
2710 /* Normally, collection only occurs in rest_of_compilation. So,
2711 if we don't collect here, we never collect junk generated
2712 during the processing of templates until we hit a
2713 non-template function. */
2714 ggc_collect ();
2715 return;
2718 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2719 walk_tree (&DECL_SAVED_TREE (fn), simplify_aggr_init_exprs_r, NULL);
2721 /* There's no reason to do any of the work here if we're only doing
2722 semantic analysis; this code just generates RTL. */
2723 if (flag_syntax_only)
2724 return;
2726 /* If possible, avoid generating RTL for this function. Instead,
2727 just record it as an inline function, and wait until end-of-file
2728 to decide whether to write it out or not. */
2729 if (/* We have to generate RTL if we can't inline trees. */
2730 flag_inline_trees
2731 /* Or if it's not an inline function. */
2732 && DECL_INLINE (fn)
2733 /* Or if we have to keep all inline functions anyhow. */
2734 && !flag_keep_inline_functions
2735 /* Or if we actually have a reference to the function. */
2736 && !DECL_NEEDED_P (fn)
2737 /* Or if we're at the end-of-file, and this function is not
2738 DECL_COMDAT. */
2739 && (!at_eof || DECL_COMDAT (fn))
2740 /* Or if this is a nested function. */
2741 && !decl_function_context (fn))
2743 /* Give the function RTL now so that we can assign it to a
2744 function pointer, etc. */
2745 make_function_rtl (fn);
2746 /* Set DECL_EXTERNAL so that assemble_external will be called as
2747 necessary. We'll clear it again in finish_file. */
2748 if (!DECL_EXTERNAL (fn))
2750 DECL_NOT_REALLY_EXTERN (fn) = 1;
2751 DECL_EXTERNAL (fn) = 1;
2753 /* Remember this function. In finish_file we'll decide if
2754 we actually need to write this function out. */
2755 mark_inline_for_output (fn);
2756 /* Let the back-end know that this funtion exists. */
2757 note_deferral_of_defined_inline_function (fn);
2758 return;
2761 /* Optimize the body of the function before expanding it. */
2762 optimize_function (fn);
2764 /* Save the current file name and line number. When we expand the
2765 body of the function, we'll set LINENO and INPUT_FILENAME so that
2766 error-mesages come out in the right places. */
2767 saved_lineno = lineno;
2768 saved_input_filename = input_filename;
2769 lineno = DECL_SOURCE_LINE (fn);
2770 input_filename = DECL_SOURCE_FILE (fn);
2772 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2773 store_parm_decls ();
2775 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2776 any of the other magic variables we set up when starting a
2777 function body. */
2778 current_function_name_declared = 1;
2780 /* Expand the body. */
2781 expand_stmt (DECL_SAVED_TREE (fn));
2783 /* Statements should always be full-expressions at the outermost set
2784 of curly braces for a function. */
2785 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2787 /* The outermost statement for a function contains the line number
2788 recorded when we finished processing the function. */
2789 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2791 /* Generate code for the function. */
2792 finish_function (lineno, 0);
2794 /* If possible, obliterate the body of the function so that it can
2795 be garbage collected. */
2796 if (flag_dump_translation_unit)
2797 /* Keep the body; we're going to dump it. */
2799 else if (DECL_INLINE (fn) && flag_inline_trees)
2800 /* We might need the body of this function so that we can expand
2801 it inline somewhere else. */
2803 else
2804 /* We don't need the body; blow it away. */
2805 DECL_SAVED_TREE (fn) = NULL_TREE;
2807 /* And restore the current source position. */
2808 lineno = saved_lineno;
2809 input_filename = saved_input_filename;