* except.c (expand_throw): Add static attribute to match
[official-gcc.git] / gcc / cp / semantics.c
blobc8368883d4eea213b1fe9d3927559644125ca6fa
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"
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 PROTO((tree));
49 static tree maybe_convert_cond PROTO((tree));
50 static tree simplify_aggr_init_exprs_r PROTO((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 an if-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 (current_function
980 && !current_function_name_declared
981 && !has_no_scope)
983 /* When we get callbacks from the middle-end, we need to know
984 we're in the midst of declaring these variables. */
985 current_function_name_declared = 2;
986 /* Actually insert the declarations. */
987 declare_function_name ();
988 /* And now just remember that we're all done. */
989 current_function_name_declared = 1;
992 return r;
996 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
997 If HAS_NO_SCOPE is non-zero, the compound statement does not define
998 a scope. */
1000 tree
1001 finish_compound_stmt (has_no_scope, compound_stmt)
1002 int has_no_scope;
1003 tree compound_stmt;
1005 tree r;
1006 tree t;
1008 if (!has_no_scope)
1009 r = do_poplevel ();
1010 else
1011 r = NULL_TREE;
1013 if (building_stmt_tree ())
1014 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1016 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1017 the precise purpose of that variable is store the type of the
1018 last expression statement within the last compound statement, we
1019 preserve the value. */
1020 t = last_expr_type;
1021 finish_stmt ();
1022 last_expr_type = t;
1024 return r;
1027 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1028 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1029 CLOBBERS. */
1031 void
1032 finish_asm_stmt (cv_qualifier, string, output_operands,
1033 input_operands, clobbers)
1034 tree cv_qualifier;
1035 tree string;
1036 tree output_operands;
1037 tree input_operands;
1038 tree clobbers;
1040 if (TREE_CHAIN (string))
1041 string = combine_strings (string);
1043 if (cv_qualifier != NULL_TREE
1044 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1046 cp_warning ("%s qualifier ignored on asm",
1047 IDENTIFIER_POINTER (cv_qualifier));
1048 cv_qualifier = NULL_TREE;
1051 if (building_stmt_tree ())
1053 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1054 output_operands, input_operands,
1055 clobbers);
1056 add_tree (r);
1058 else
1060 emit_line_note (input_filename, lineno);
1061 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1062 || clobbers != NULL_TREE)
1064 tree t;
1066 for (t = input_operands; t; t = TREE_CHAIN (t))
1067 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1069 c_expand_asm_operands (string, output_operands,
1070 input_operands,
1071 clobbers,
1072 cv_qualifier != NULL_TREE,
1073 input_filename, lineno);
1075 else
1076 expand_asm (string);
1078 finish_stmt ();
1082 /* Finish a label with the indicated NAME. */
1084 void
1085 finish_label_stmt (name)
1086 tree name;
1088 tree decl = define_label (input_filename, lineno, name);
1090 if (building_stmt_tree ())
1091 add_tree (build_min_nt (LABEL_STMT, decl));
1092 else if (decl)
1093 expand_label (decl);
1096 /* Finish a series of declarations for local labels. G++ allows users
1097 to declare "local" labels, i.e., labels with scope. This extension
1098 is useful when writing code involving statement-expressions. */
1100 void
1101 finish_label_decl (name)
1102 tree name;
1104 tree decl = declare_local_label (name);
1105 if (building_stmt_tree ())
1106 add_decl_stmt (decl);
1109 /* Create a declaration statement for the declaration given by the
1110 DECL. */
1112 void
1113 add_decl_stmt (decl)
1114 tree decl;
1116 tree decl_stmt;
1118 /* We need the type to last until instantiation time. */
1119 decl_stmt = build_min_nt (DECL_STMT, decl);
1120 add_tree (decl_stmt);
1123 /* We're in a constructor, and have just constructed a a subobject of
1124 *THIS. CLEANUP is code to run if an exception is thrown before the
1125 end of the current function is reached. */
1127 void
1128 finish_subobject (cleanup)
1129 tree cleanup;
1131 if (building_stmt_tree ())
1133 tree r = build_min_nt (SUBOBJECT, cleanup);
1134 add_tree (r);
1136 else
1137 add_partial_entry (cleanup);
1140 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1142 void
1143 finish_decl_cleanup (decl, cleanup)
1144 tree decl;
1145 tree cleanup;
1147 if (building_stmt_tree ())
1148 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1149 else if (!decl
1150 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1151 expand_decl_cleanup (decl, cleanup);
1154 /* Bind a name and initialization to the return value of
1155 the current function. */
1157 void
1158 finish_named_return_value (return_id, init)
1159 tree return_id, init;
1161 tree decl = DECL_RESULT (current_function_decl);
1163 if (pedantic)
1164 /* Give this error as many times as there are occurrences,
1165 so that users can use Emacs compilation buffers to find
1166 and fix all such places. */
1167 pedwarn ("ANSI C++ does not permit named return values");
1169 if (return_id != NULL_TREE)
1171 if (DECL_NAME (decl) == NULL_TREE)
1173 DECL_NAME (decl) = return_id;
1174 DECL_ASSEMBLER_NAME (decl) = return_id;
1176 else
1178 cp_error ("return identifier `%D' already in place", return_id);
1179 return;
1183 /* Can't let this happen for constructors. */
1184 if (DECL_CONSTRUCTOR_P (current_function_decl))
1186 error ("can't redefine default return value for constructors");
1187 return;
1190 /* If we have a named return value, put that in our scope as well. */
1191 if (DECL_NAME (decl) != NULL_TREE)
1193 /* Let `cp_finish_decl' know that this initializer is ok. */
1194 DECL_INITIAL (decl) = init;
1195 if (doing_semantic_analysis_p ())
1196 pushdecl (decl);
1198 if (building_stmt_tree ())
1199 add_tree (build_min_nt (RETURN_INIT, return_id, init));
1200 else
1202 cp_finish_decl (decl, init, NULL_TREE, 0);
1203 store_return_init (decl);
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 binfo = TYPE_BINFO (current_class_type);
1244 tree if_stmt;
1245 tree compound_stmt;
1246 int saved_cfnd;
1248 /* If the dtor is empty, and we know there is not possible way we
1249 could use any vtable entries, before they are possibly set by
1250 a base class dtor, we don't have to setup the vtables, as we
1251 know that any base class dtoring will set up any vtables it
1252 needs. We avoid MI, because one base class dtor can do a
1253 virtual dispatch to an overridden function that would need to
1254 have a non-related vtable set up, we cannot avoid setting up
1255 vtables in that case. We could change this to see if there is
1256 just one vtable. */
1257 if_stmt = begin_if_stmt ();
1259 /* If it is not safe to avoid setting up the vtables, then
1260 someone will change the condition to be boolean_true_node.
1261 (Actually, for now, we do not have code to set the condition
1262 appropriately, so we just assume that we always need to
1263 initialize the vtables.) */
1264 finish_if_stmt_cond (boolean_true_node, if_stmt);
1265 current_vcalls_possible_p = &IF_COND (if_stmt);
1267 /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1268 open the block for the if-body. */
1269 saved_cfnd = current_function_name_declared;
1270 current_function_name_declared = 1;
1271 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1272 current_function_name_declared = saved_cfnd;
1274 /* Make all virtual function table pointers in non-virtual base
1275 classes point to CURRENT_CLASS_TYPE's virtual function
1276 tables. */
1277 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
1279 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1280 expand_indirect_vtbls_init (binfo, current_class_ref,
1281 current_class_ptr);
1283 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1284 finish_then_clause (if_stmt);
1285 finish_if_stmt ();
1288 /* Always keep the BLOCK node associated with the outermost pair of
1289 curly braces of a function. These are needed for correct
1290 operation of dwarfout.c. */
1291 keep_next_level (1);
1293 /* The virtual function tables are set up now. */
1294 vtbls_set_up_p = 1;
1297 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
1298 whether this statements opens or closes a scope. PARTIAL_P is true
1299 for a partial scope, i.e, the scope that begins after a label when
1300 an object that needs a cleanup is created. If BEGIN_P is nonzero,
1301 returns a new TREE_LIST representing the top of the SCOPE_STMT
1302 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
1303 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1304 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1305 SCOPE_BEGIN_P set. */
1307 tree
1308 add_scope_stmt (begin_p, partial_p)
1309 int begin_p;
1310 int partial_p;
1312 tree ss;
1313 tree top;
1315 /* Build the statement. */
1316 ss = build_min_nt (SCOPE_STMT, NULL_TREE);
1317 SCOPE_BEGIN_P (ss) = begin_p;
1318 SCOPE_PARTIAL_P (ss) = partial_p;
1320 /* Keep the scope stack up to date. */
1321 if (begin_p)
1323 current_scope_stmt_stack
1324 = tree_cons (ss, NULL_TREE, current_scope_stmt_stack);
1325 top = current_scope_stmt_stack;
1327 else
1329 top = current_scope_stmt_stack;
1330 TREE_VALUE (top) = ss;
1331 current_scope_stmt_stack = TREE_CHAIN (top);
1334 /* Add the new statement to the statement-tree. */
1335 add_tree (ss);
1337 return top;
1340 /* Begin a new scope. */
1342 void
1343 do_pushlevel ()
1345 if (!building_stmt_tree ())
1347 emit_line_note (input_filename, lineno);
1348 clear_last_expr ();
1350 if (stmts_are_full_exprs_p)
1352 pushlevel (0);
1353 if (!building_stmt_tree ()
1354 && !current_function->x_whole_function_mode_p)
1355 my_friendly_abort (19991129);
1357 if (building_stmt_tree () && !processing_template_decl)
1358 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
1362 /* Finish a scope. */
1364 tree
1365 do_poplevel ()
1367 tree block = NULL_TREE;
1369 if (stmts_are_full_exprs_p)
1371 tree scope_stmts;
1373 if (building_stmt_tree () && !processing_template_decl)
1374 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
1375 else
1376 scope_stmts = NULL_TREE;
1378 block = poplevel (kept_level_p (), 1, 0);
1379 if (block && !processing_template_decl)
1381 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1382 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1386 return block;
1389 /* Finish a parenthesized expression EXPR. */
1391 tree
1392 finish_parenthesized_expr (expr)
1393 tree expr;
1395 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1396 /* This inhibits warnings in truthvalue_conversion. */
1397 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1399 return expr;
1402 /* Begin a statement-expression. The value returned must be passed to
1403 finish_stmt_expr. */
1405 tree
1406 begin_stmt_expr ()
1408 /* If we're outside a function, we won't have a statement-tree to
1409 work with. But, if we see a statement-expression we need to
1410 create one. */
1411 if (!current_function && !last_tree)
1412 begin_stmt_tree (&scope_chain->x_saved_tree);
1414 keep_next_level (1);
1415 /* If we're building a statement tree, then the upcoming compound
1416 statement will be chained onto the tree structure, starting at
1417 last_tree. We return last_tree so that we can later unhook the
1418 compound statement. */
1419 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
1422 /* Finish a statement-expression. RTL_EXPR should be the value
1423 returned by the previous begin_stmt_expr; EXPR is the
1424 statement-expression. Returns an expression representing the
1425 statement-expression. */
1427 tree
1428 finish_stmt_expr (rtl_expr)
1429 tree rtl_expr;
1431 tree result;
1433 if (!building_stmt_tree ())
1434 rtl_expr = expand_end_stmt_expr (rtl_expr);
1436 if (building_stmt_tree ())
1438 /* If the last thing in the statement-expression was not an
1439 expression-statement, then it has type `void'. */
1440 if (!last_expr_type)
1441 last_expr_type = void_type_node;
1442 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1443 TREE_SIDE_EFFECTS (result) = 1;
1445 /* Remove the compound statement from the tree structure; it is
1446 now saved in the STMT_EXPR. */
1447 SET_LAST_STMT (rtl_expr);
1448 TREE_CHAIN (last_tree) = NULL_TREE;
1450 else
1451 result = rtl_expr;
1453 /* If we created a statement-tree for this statement-expression,
1454 remove it now. */
1455 if (!current_function
1456 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1457 finish_stmt_tree (&scope_chain->x_saved_tree);
1459 return result;
1462 /* Finish a call to FN with ARGS. Returns a representation of the
1463 call. */
1465 tree
1466 finish_call_expr (fn, args, koenig)
1467 tree fn;
1468 tree args;
1469 int koenig;
1471 tree result;
1473 if (koenig)
1475 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1476 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1477 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1478 fn = do_identifier (fn, 2, args);
1480 result = build_x_function_call (fn, args, current_class_ref);
1482 if (TREE_CODE (result) == CALL_EXPR
1483 && (! TREE_TYPE (result)
1484 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1485 result = require_complete_type (result);
1487 return result;
1490 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1491 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1492 POSTDECREMENT_EXPR.) */
1494 tree
1495 finish_increment_expr (expr, code)
1496 tree expr;
1497 enum tree_code code;
1499 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1500 a COMPONENT_REF). This way if we've got, say, a reference to a
1501 static member that's being operated on, we don't end up trying to
1502 find a member operator for the class it's in. */
1504 if (TREE_CODE (expr) == OFFSET_REF)
1505 expr = resolve_offset_ref (expr);
1506 return build_x_unary_op (code, expr);
1509 /* Finish a use of `this'. Returns an expression for `this'. */
1511 tree
1512 finish_this_expr ()
1514 tree result;
1516 if (current_class_ptr)
1518 #ifdef WARNING_ABOUT_CCD
1519 TREE_USED (current_class_ptr) = 1;
1520 #endif
1521 result = current_class_ptr;
1523 else if (current_function_decl
1524 && DECL_STATIC_FUNCTION_P (current_function_decl))
1526 error ("`this' is unavailable for static member functions");
1527 result = error_mark_node;
1529 else
1531 if (current_function_decl)
1532 error ("invalid use of `this' in non-member function");
1533 else
1534 error ("invalid use of `this' at top level");
1535 result = error_mark_node;
1538 return result;
1541 /* Finish a member function call using OBJECT and ARGS as arguments to
1542 FN. Returns an expression for the call. */
1544 tree
1545 finish_object_call_expr (fn, object, args)
1546 tree fn;
1547 tree object;
1548 tree args;
1550 #if 0
1551 /* This is a future direction of this code, but because
1552 build_x_function_call cannot always undo what is done in
1553 build_component_ref entirely yet, we cannot do this. */
1555 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1556 return finish_call_expr (real_fn, args);
1557 #else
1558 if (DECL_DECLARES_TYPE_P (fn))
1560 if (processing_template_decl)
1561 /* This can happen on code like:
1563 class X;
1564 template <class T> void f(T t) {
1565 t.X();
1568 We just grab the underlying IDENTIFIER. */
1569 fn = DECL_NAME (fn);
1570 else
1572 cp_error ("calling type `%T' like a method", fn);
1573 return error_mark_node;
1577 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1578 #endif
1581 /* Finish a qualified member function call using OBJECT and ARGS as
1582 arguments to FN. Returns an expressino for the call. */
1584 tree
1585 finish_qualified_object_call_expr (fn, object, args)
1586 tree fn;
1587 tree object;
1588 tree args;
1590 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1591 TREE_OPERAND (fn, 1), args);
1594 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1595 being the scope, if any, of DESTRUCTOR. Returns an expression for
1596 the call. */
1598 tree
1599 finish_pseudo_destructor_call_expr (object, scope, destructor)
1600 tree object;
1601 tree scope;
1602 tree destructor;
1604 if (processing_template_decl)
1605 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1607 if (scope && scope != destructor)
1608 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1609 scope, destructor);
1611 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1612 && (TREE_CODE (TREE_TYPE (object)) !=
1613 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1614 cp_error ("`%E' is not of type `%T'", object, destructor);
1616 return cp_convert (void_type_node, object);
1619 /* Finish a call to a globally qualified member function FN using
1620 ARGS. Returns an expression for the call. */
1622 tree
1623 finish_qualified_call_expr (fn, args)
1624 tree fn;
1625 tree args;
1627 if (processing_template_decl)
1628 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1629 else
1630 return build_member_call (TREE_OPERAND (fn, 0),
1631 TREE_OPERAND (fn, 1),
1632 args);
1635 /* Finish an expression taking the address of LABEL. Returns an
1636 expression for the address. */
1638 tree
1639 finish_label_address_expr (label)
1640 tree label;
1642 tree result;
1644 label = lookup_label (label);
1645 if (label == NULL_TREE)
1646 result = null_pointer_node;
1647 else
1649 TREE_USED (label) = 1;
1650 result = build1 (ADDR_EXPR, ptr_type_node, label);
1651 TREE_CONSTANT (result) = 1;
1652 /* This function cannot be inlined. All jumps to the addressed
1653 label should wind up at the same point. */
1654 DECL_UNINLINABLE (current_function_decl) = 1;
1657 return result;
1660 /* Finish an expression of the form CODE EXPR. */
1662 tree
1663 finish_unary_op_expr (code, expr)
1664 enum tree_code code;
1665 tree expr;
1667 tree result = build_x_unary_op (code, expr);
1668 /* Inside a template, build_x_unary_op does not fold the
1669 expression. So check whether the result is folded before
1670 setting TREE_NEGATED_INT. */
1671 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1672 && TREE_CODE (result) == INTEGER_CST
1673 && !TREE_UNSIGNED (TREE_TYPE (result))
1674 && INT_CST_LT (result, integer_zero_node))
1675 TREE_NEGATED_INT (result) = 1;
1676 overflow_warning (result);
1677 return result;
1680 /* Finish an id-expression. */
1682 tree
1683 finish_id_expr (expr)
1684 tree expr;
1686 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1687 expr = do_identifier (expr, 1, NULL_TREE);
1689 return expr;
1692 /* Begin a function defniition declared with DECL_SPECS and
1693 DECLARATOR. Returns non-zero if the function-declaration is
1694 legal. */
1697 begin_function_definition (decl_specs, declarator)
1698 tree decl_specs;
1699 tree declarator;
1701 tree specs;
1702 tree attrs;
1703 split_specs_attrs (decl_specs, &specs, &attrs);
1704 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1705 return 0;
1707 reinit_parse_for_function ();
1708 /* The things we're about to see are not directly qualified by any
1709 template headers we've seen thus far. */
1710 reset_specialization ();
1712 return 1;
1715 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1716 a SCOPE_REF. */
1718 tree
1719 begin_constructor_declarator (scope, name)
1720 tree scope;
1721 tree name;
1723 tree result = build_parse_node (SCOPE_REF, scope, name);
1724 enter_scope_of (result);
1725 return result;
1728 /* Finish an init-declarator. Returns a DECL. */
1730 tree
1731 finish_declarator (declarator, declspecs, attributes,
1732 prefix_attributes, initialized)
1733 tree declarator;
1734 tree declspecs;
1735 tree attributes;
1736 tree prefix_attributes;
1737 int initialized;
1739 return start_decl (declarator, declspecs, initialized, attributes,
1740 prefix_attributes);
1743 /* Finish a translation unit. */
1745 void
1746 finish_translation_unit ()
1748 /* In case there were missing closebraces,
1749 get us back to the global binding level. */
1750 pop_everything ();
1751 while (current_namespace != global_namespace)
1752 pop_namespace ();
1753 finish_file ();
1756 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1757 Returns the parameter. */
1759 tree
1760 finish_template_type_parm (aggr, identifier)
1761 tree aggr;
1762 tree identifier;
1764 if (aggr != class_type_node)
1766 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1767 aggr = class_type_node;
1770 return build_tree_list (aggr, identifier);
1773 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1774 Returns the parameter. */
1776 tree
1777 finish_template_template_parm (aggr, identifier)
1778 tree aggr;
1779 tree identifier;
1781 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1782 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1783 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1784 DECL_TEMPLATE_RESULT (tmpl) = decl;
1785 SET_DECL_ARTIFICIAL (decl);
1786 end_template_decl ();
1788 return finish_template_type_parm (aggr, tmpl);
1791 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1792 non-zero, the parameter list was terminated by a `...'. */
1794 tree
1795 finish_parmlist (parms, ellipsis)
1796 tree parms;
1797 int ellipsis;
1799 if (!ellipsis)
1800 chainon (parms, void_list_node);
1801 /* We mark the PARMS as a parmlist so that declarator processing can
1802 disambiguate certain constructs. */
1803 if (parms != NULL_TREE)
1804 TREE_PARMLIST (parms) = 1;
1806 return parms;
1809 /* Begin a class definition, as indicated by T. */
1811 tree
1812 begin_class_definition (t)
1813 tree t;
1815 if (t == error_mark_node
1816 || ! IS_AGGR_TYPE (t))
1818 t = make_lang_type (RECORD_TYPE);
1819 pushtag (make_anon_name (), t, 0);
1822 /* In a definition of a member class template, we will get here with an
1823 implicit typename, a TYPENAME_TYPE with a type. */
1824 if (TREE_CODE (t) == TYPENAME_TYPE)
1825 t = TREE_TYPE (t);
1827 /* If we generated a partial instantiation of this type, but now
1828 we're seeing a real definition, we're actually looking at a
1829 partial specialization. Consider:
1831 template <class T, class U>
1832 struct Y {};
1834 template <class T>
1835 struct X {};
1837 template <class T, class U>
1838 void f()
1840 typename X<Y<T, U> >::A a;
1843 template <class T, class U>
1844 struct X<Y<T, U> >
1848 We have to undo the effects of the previous partial
1849 instantiation. */
1850 if (PARTIAL_INSTANTIATION_P (t))
1852 if (!pedantic)
1854 /* Unfortunately, when we're not in pedantic mode, we
1855 attempt to actually fill in some of the fields of the
1856 partial instantiation, in order to support the implicit
1857 typename extension. Clear those fields now, in
1858 preparation for the definition here. The fields cleared
1859 here must match those set in instantiate_class_template.
1860 Look for a comment mentioning begin_class_definition
1861 there. */
1862 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1863 TYPE_FIELDS (t) = NULL_TREE;
1864 TYPE_METHODS (t) = NULL_TREE;
1865 CLASSTYPE_TAGS (t) = NULL_TREE;
1866 TYPE_SIZE (t) = NULL_TREE;
1869 /* This isn't a partial instantiation any more. */
1870 PARTIAL_INSTANTIATION_P (t) = 0;
1872 /* If this type was already complete, and we see another definition,
1873 that's an error. */
1874 else if (TYPE_SIZE (t))
1875 duplicate_tag_error (t);
1877 /* Update the location of the decl. */
1878 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1879 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1881 if (TYPE_BEING_DEFINED (t))
1883 t = make_lang_type (TREE_CODE (t));
1884 pushtag (TYPE_IDENTIFIER (t), t, 0);
1886 maybe_process_partial_specialization (t);
1887 pushclass (t, 1);
1888 TYPE_BEING_DEFINED (t) = 1;
1889 /* Reset the interface data, at the earliest possible
1890 moment, as it might have been set via a class foo;
1891 before. */
1893 tree name = TYPE_IDENTIFIER (t);
1895 if (! ANON_AGGRNAME_P (name))
1897 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1898 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1899 (t, interface_unknown);
1902 /* Only leave this bit clear if we know this
1903 class is part of an interface-only specification. */
1904 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1905 || ! CLASSTYPE_INTERFACE_ONLY (t))
1906 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1908 #if 0
1909 tmp = TYPE_IDENTIFIER ($<ttype>0);
1910 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1911 overload_template_name (tmp, 1);
1912 #endif
1913 reset_specialization();
1915 /* Make a declaration for this class in its own scope. */
1916 build_self_reference ();
1918 return t;
1921 /* Finish the member declaration given by DECL. */
1923 void
1924 finish_member_declaration (decl)
1925 tree decl;
1927 if (decl == error_mark_node || decl == NULL_TREE)
1928 return;
1930 if (decl == void_type_node)
1931 /* The COMPONENT was a friend, not a member, and so there's
1932 nothing for us to do. */
1933 return;
1935 /* We should see only one DECL at a time. */
1936 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1938 /* Set up access control for DECL. */
1939 TREE_PRIVATE (decl)
1940 = (current_access_specifier == access_private_node);
1941 TREE_PROTECTED (decl)
1942 = (current_access_specifier == access_protected_node);
1943 if (TREE_CODE (decl) == TEMPLATE_DECL)
1945 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1946 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1949 /* Mark the DECL as a member of the current class. */
1950 if (TREE_CODE (decl) == FUNCTION_DECL
1951 || DECL_FUNCTION_TEMPLATE_P (decl))
1952 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1953 finish_struct. Presumably it is already set as the function is
1954 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1955 DECL_CLASS_CONTEXT (decl) = current_class_type;
1956 else
1957 DECL_CONTEXT (decl) = current_class_type;
1959 /* Put functions on the TYPE_METHODS list and everything else on the
1960 TYPE_FIELDS list. Note that these are built up in reverse order.
1961 We reverse them (to obtain declaration order) in finish_struct. */
1962 if (TREE_CODE (decl) == FUNCTION_DECL
1963 || DECL_FUNCTION_TEMPLATE_P (decl))
1965 /* We also need to add this function to the
1966 CLASSTYPE_METHOD_VEC. */
1967 add_method (current_class_type, 0, decl);
1969 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1970 TYPE_METHODS (current_class_type) = decl;
1972 else
1974 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1975 go at the beginning. The reason is that lookup_field_1
1976 searches the list in order, and we want a field name to
1977 override a type name so that the "struct stat hack" will
1978 work. In particular:
1980 struct S { enum E { }; int E } s;
1981 s.E = 3;
1983 is legal. In addition, the FIELD_DECLs must be maintained in
1984 declaration order so that class layout works as expected.
1985 However, we don't need that order until class layout, so we
1986 save a little time by putting FIELD_DECLs on in reverse order
1987 here, and then reversing them in finish_struct_1. (We could
1988 also keep a pointer to the correct insertion points in the
1989 list.) */
1991 if (TREE_CODE (decl) == TYPE_DECL)
1992 TYPE_FIELDS (current_class_type)
1993 = chainon (TYPE_FIELDS (current_class_type), decl);
1994 else
1996 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1997 TYPE_FIELDS (current_class_type) = decl;
2000 /* Enter the DECL into the scope of the class. */
2001 if (TREE_CODE (decl) != USING_DECL)
2002 pushdecl_class_level (decl);
2006 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
2007 the definition is immediately followed by a semicolon. Returns the
2008 type. */
2010 tree
2011 finish_class_definition (t, attributes, semi, pop_scope_p)
2012 tree t;
2013 tree attributes;
2014 int semi;
2015 int pop_scope_p;
2017 /* finish_struct nukes this anyway; if finish_exception does too,
2018 then it can go. */
2019 if (semi)
2020 note_got_semicolon (t);
2022 /* If we got any attributes in class_head, xref_tag will stick them in
2023 TREE_TYPE of the type. Grab them now. */
2024 attributes = chainon (TREE_TYPE (t), attributes);
2025 TREE_TYPE (t) = NULL_TREE;
2027 if (TREE_CODE (t) == ENUMERAL_TYPE)
2029 else
2031 t = finish_struct (t, attributes);
2032 if (semi)
2033 note_got_semicolon (t);
2036 if (! semi)
2037 check_for_missing_semicolon (t);
2038 if (pop_scope_p)
2039 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2040 if (current_scope () == current_function_decl)
2041 do_pending_defargs ();
2043 return t;
2046 /* Finish processing the default argument expressions cached during
2047 the processing of a class definition. */
2049 void
2050 begin_inline_definitions ()
2052 if (pending_inlines
2053 && current_scope () == current_function_decl)
2054 do_pending_inlines ();
2057 /* Finish processing the inline function definitions cached during the
2058 processing of a class definition. */
2060 void
2061 finish_inline_definitions ()
2063 if (current_class_type == NULL_TREE)
2064 clear_inline_text_obstack ();
2067 /* Finish processing the declaration of a member class template
2068 TYPES whose template parameters are given by PARMS. */
2070 tree
2071 finish_member_class_template (types)
2072 tree types;
2074 tree t;
2076 /* If there are declared, but undefined, partial specializations
2077 mixed in with the typespecs they will not yet have passed through
2078 maybe_process_partial_specialization, so we do that here. */
2079 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2080 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2081 maybe_process_partial_specialization (TREE_VALUE (t));
2083 note_list_got_semicolon (types);
2084 grok_x_components (types);
2085 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2086 /* The component was in fact a friend declaration. We avoid
2087 finish_member_template_decl performing certain checks by
2088 unsetting TYPES. */
2089 types = NULL_TREE;
2091 finish_member_template_decl (types);
2093 /* As with other component type declarations, we do
2094 not store the new DECL on the list of
2095 component_decls. */
2096 return NULL_TREE;
2099 /* Finish processsing a complete template declaration. The PARMS are
2100 the template parameters. */
2102 void
2103 finish_template_decl (parms)
2104 tree parms;
2106 if (parms)
2107 end_template_decl ();
2108 else
2109 end_specialization ();
2112 /* Finish processing a a template-id (which names a type) of the form
2113 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2114 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2115 the scope of template-id indicated. */
2117 tree
2118 finish_template_type (name, args, entering_scope)
2119 tree name;
2120 tree args;
2121 int entering_scope;
2123 tree decl;
2125 decl = lookup_template_class (name, args,
2126 NULL_TREE, NULL_TREE, entering_scope);
2127 if (decl != error_mark_node)
2128 decl = TYPE_STUB_DECL (decl);
2130 return decl;
2133 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2134 namespace scope or a class scope. */
2136 void
2137 enter_scope_of (sr)
2138 tree sr;
2140 tree scope = TREE_OPERAND (sr, 0);
2142 if (TREE_CODE (scope) == NAMESPACE_DECL)
2144 push_decl_namespace (scope);
2145 TREE_COMPLEXITY (sr) = -1;
2147 else if (scope != current_class_type)
2149 if (TREE_CODE (scope) == TYPENAME_TYPE)
2151 /* In a declarator for a template class member, the scope will
2152 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2153 scope = TREE_TYPE (scope);
2154 TREE_OPERAND (sr, 0) = scope;
2156 push_nested_class (scope, 3);
2157 TREE_COMPLEXITY (sr) = current_class_depth;
2161 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2162 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2163 BASE_CLASS, or NULL_TREE if an error occurred. The
2164 ACCESSS_SPECIFIER is one of
2165 access_{default,public,protected_private}[_virtual]_node.*/
2167 tree
2168 finish_base_specifier (access_specifier, base_class)
2169 tree access_specifier;
2170 tree base_class;
2172 tree type;
2173 tree result;
2175 if (base_class == NULL_TREE)
2177 error ("invalid base class");
2178 type = error_mark_node;
2180 else
2181 type = TREE_TYPE (base_class);
2183 if (! is_aggr_type (type, 1))
2184 result = NULL_TREE;
2185 else
2186 result = build_tree_list (access_specifier, type);
2188 return result;
2191 /* Called when multiple declarators are processed. If that is not
2192 premitted in this context, an error is issued. */
2194 void
2195 check_multiple_declarators ()
2197 /* [temp]
2199 In a template-declaration, explicit specialization, or explicit
2200 instantiation the init-declarator-list in the declaration shall
2201 contain at most one declarator.
2203 We don't just use PROCESSING_TEMPLATE_DECL for the first
2204 condition since that would disallow the perfectly legal code,
2205 like `template <class T> struct S { int i, j; };'. */
2206 tree scope = current_scope ();
2208 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2209 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2210 return;
2212 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2213 || processing_explicit_instantiation
2214 || processing_specialization)
2215 cp_error ("multiple declarators in template declaration");
2218 tree
2219 finish_typeof (expr)
2220 tree expr;
2222 if (processing_template_decl)
2224 tree t;
2226 t = make_lang_type (TYPEOF_TYPE);
2227 TYPE_FIELDS (t) = expr;
2229 return t;
2232 return TREE_TYPE (expr);
2235 /* Create an empty statement tree rooted at T. */
2237 void
2238 begin_stmt_tree (t)
2239 tree *t;
2241 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2242 what follows. We remove the extraneous statement in
2243 finish_stmt_tree. */
2244 *t = build_nt (EXPR_STMT, void_zero_node);
2245 SET_LAST_STMT (*t);
2246 last_expr_type = NULL_TREE;
2249 /* Finish the statement tree rooted at T. */
2251 void
2252 finish_stmt_tree (t)
2253 tree *t;
2255 tree stmt;
2257 /* Remove the fake extra statement added in begin_stmt_tree. */
2258 stmt = TREE_CHAIN (*t);
2259 *t = stmt;
2260 SET_LAST_STMT (NULL_TREE);
2262 if (current_function)
2264 /* The line-number recorded in the outermost statement in a function
2265 is the line number of the end of the function. */
2266 STMT_LINENO (stmt) = lineno;
2267 STMT_LINENO_FOR_FN_P (stmt) = 1;
2271 /* We're about to expand T, a statement. Set up appropriate context
2272 for the substitution. */
2274 void
2275 prep_stmt (t)
2276 tree t;
2278 if (!STMT_LINENO_FOR_FN_P (t))
2279 lineno = STMT_LINENO (t);
2280 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2283 /* Some statements, like for-statements or if-statements, require a
2284 condition. This condition can be a declaration. If T is such a
2285 declaration it is processed, and an expression appropriate to use
2286 as the condition is returned. Otherwise, T itself is returned. */
2288 static tree
2289 expand_cond (t)
2290 tree t;
2292 if (t && TREE_CODE (t) == TREE_LIST)
2294 expand_stmt (TREE_PURPOSE (t));
2295 return TREE_VALUE (t);
2297 else
2298 return t;
2301 /* Generate RTL for the statement T, and its substatements, and any
2302 other statements at its nesting level. */
2304 tree
2305 expand_stmt (t)
2306 tree t;
2308 tree rval = NULL_TREE;
2310 while (t && t != error_mark_node)
2312 int saved_stmts_are_full_exprs_p;
2314 /* Assume we'll have nothing to return. */
2315 rval = NULL_TREE;
2317 /* Set up context appropriately for handling this statement. */
2318 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2319 prep_stmt (t);
2321 switch (TREE_CODE (t))
2323 case RETURN_STMT:
2324 finish_return_stmt (RETURN_EXPR (t));
2325 break;
2327 case EXPR_STMT:
2328 finish_expr_stmt (EXPR_STMT_EXPR (t));
2329 break;
2331 case DECL_STMT:
2333 tree decl;
2335 emit_line_note (input_filename, lineno);
2336 decl = DECL_STMT_DECL (t);
2337 /* If this is a declaration for an automatic local
2338 variable, initialize it. Note that we might also see a
2339 declaration for a namespace-scope object (declared with
2340 `extern'). We don't have to handle the initialization
2341 of those objects here; they can only be declarations,
2342 rather than definitions. */
2343 if (TREE_CODE (decl) == VAR_DECL
2344 && !TREE_STATIC (decl)
2345 && !DECL_EXTERNAL (decl))
2347 /* Let the back-end know about this variable. */
2348 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2349 emit_local_var (decl);
2350 else
2351 expand_anon_union_decl (decl, NULL_TREE,
2352 DECL_ANON_UNION_ELEMS (decl));
2354 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2355 make_rtl_for_local_static (decl);
2357 break;
2359 case CLEANUP_STMT:
2360 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2361 break;
2363 case START_CATCH_STMT:
2364 begin_catch_block (TREE_TYPE (t));
2365 break;
2367 case CTOR_STMT:
2368 if (CTOR_BEGIN_P (t))
2369 begin_protect_partials ();
2370 else
2371 /* After this point, any exceptions will cause the
2372 destructor to be executed, so we no longer need to worry
2373 about destroying the various subobjects ourselves. */
2374 end_protect_partials ();
2375 break;
2377 case FOR_STMT:
2379 tree tmp;
2381 begin_for_stmt ();
2382 expand_stmt (FOR_INIT_STMT (t));
2383 finish_for_init_stmt (NULL_TREE);
2384 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2385 tmp = FOR_EXPR (t);
2386 finish_for_expr (tmp, NULL_TREE);
2387 expand_stmt (FOR_BODY (t));
2388 finish_for_stmt (tmp, NULL_TREE);
2390 break;
2392 case WHILE_STMT:
2394 begin_while_stmt ();
2395 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2396 expand_stmt (WHILE_BODY (t));
2397 finish_while_stmt (NULL_TREE);
2399 break;
2401 case DO_STMT:
2403 begin_do_stmt ();
2404 expand_stmt (DO_BODY (t));
2405 finish_do_body (NULL_TREE);
2406 finish_do_stmt (DO_COND (t), NULL_TREE);
2408 break;
2410 case IF_STMT:
2411 begin_if_stmt ();
2412 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2413 if (THEN_CLAUSE (t))
2415 expand_stmt (THEN_CLAUSE (t));
2416 finish_then_clause (NULL_TREE);
2418 if (ELSE_CLAUSE (t))
2420 begin_else_clause ();
2421 expand_stmt (ELSE_CLAUSE (t));
2422 finish_else_clause (NULL_TREE);
2424 finish_if_stmt ();
2425 break;
2427 case COMPOUND_STMT:
2428 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2429 expand_stmt (COMPOUND_BODY (t));
2430 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2431 NULL_TREE);
2432 break;
2434 case BREAK_STMT:
2435 finish_break_stmt ();
2436 break;
2438 case CONTINUE_STMT:
2439 finish_continue_stmt ();
2440 break;
2442 case SWITCH_STMT:
2444 tree cond;
2446 begin_switch_stmt ();
2447 cond = expand_cond (SWITCH_COND (t));
2448 finish_switch_cond (cond, NULL_TREE);
2449 expand_stmt (SWITCH_BODY (t));
2450 finish_switch_stmt (cond, NULL_TREE);
2452 break;
2454 case CASE_LABEL:
2455 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2456 break;
2458 case LABEL_STMT:
2459 expand_label (LABEL_STMT_LABEL (t));
2460 break;
2462 case GOTO_STMT:
2463 finish_goto_stmt (GOTO_DESTINATION (t));
2464 break;
2466 case ASM_STMT:
2467 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2468 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2469 break;
2471 case TRY_BLOCK:
2472 if (CLEANUP_P (t))
2474 expand_eh_region_start ();
2475 expand_stmt (TRY_STMTS (t));
2476 finish_cleanup_try_block (NULL_TREE);
2477 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2479 else
2481 if (FN_TRY_BLOCK_P (t))
2482 begin_function_try_block ();
2483 else
2484 begin_try_block ();
2486 expand_stmt (TRY_STMTS (t));
2488 if (FN_TRY_BLOCK_P (t))
2490 finish_function_try_block (NULL_TREE);
2491 expand_stmt (TRY_HANDLERS (t));
2492 finish_function_handler_sequence (NULL_TREE);
2494 else
2496 finish_try_block (NULL_TREE);
2497 expand_stmt (TRY_HANDLERS (t));
2498 finish_handler_sequence (NULL_TREE);
2501 break;
2503 case HANDLER:
2504 begin_handler ();
2505 expand_stmt (HANDLER_BODY (t));
2506 finish_handler (NULL_TREE, NULL_TREE);
2507 break;
2509 case SUBOBJECT:
2510 finish_subobject (SUBOBJECT_CLEANUP (t));
2511 break;
2513 case SCOPE_STMT:
2514 if (!SCOPE_NO_CLEANUPS_P (t))
2516 if (SCOPE_BEGIN_P (t))
2517 expand_start_bindings (2 * SCOPE_NULLIFIED_P (t));
2518 else if (SCOPE_END_P (t))
2519 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t),
2520 SCOPE_PARTIAL_P (t));
2522 else if (!SCOPE_NULLIFIED_P (t))
2523 emit_note (NULL,
2524 (SCOPE_BEGIN_P (t)
2525 ? NOTE_INSN_BLOCK_BEG
2526 : NOTE_INSN_BLOCK_END));
2527 break;
2529 case RETURN_INIT:
2530 /* Clear this out so that finish_named_return_value can set it
2531 again. */
2532 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2533 finish_named_return_value (TREE_OPERAND (t, 0),
2534 TREE_OPERAND (t, 1));
2535 break;
2537 default:
2538 my_friendly_abort (19990810);
2539 break;
2542 /* Restore saved state. */
2543 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2545 /* Go on to the next statement in this scope. */
2546 t = TREE_CHAIN (t);
2549 return rval;
2552 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2553 will equivalent CALL_EXPRs. */
2555 static tree
2556 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2557 tree *tp;
2558 int *walk_subtrees ATTRIBUTE_UNUSED;
2559 void *data ATTRIBUTE_UNUSED;
2561 tree aggr_init_expr;
2562 tree call_expr;
2563 tree fn;
2564 tree args;
2565 tree slot;
2566 tree type;
2567 tree call_type;
2568 int copy_from_buffer_p;
2570 /* Only AGGR_INIT_EXPRs are interesting. */
2571 aggr_init_expr = *tp;
2572 if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2573 return NULL_TREE;
2575 /* Form an appropriate CALL_EXPR. */
2576 fn = TREE_OPERAND (aggr_init_expr, 0);
2577 args = TREE_OPERAND (aggr_init_expr, 1);
2578 slot = TREE_OPERAND (aggr_init_expr, 2);
2579 type = TREE_TYPE (aggr_init_expr);
2580 call_type = type;
2581 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2583 /* Replace the first argument with the address of the third
2584 argument to the AGGR_INIT_EXPR. */
2585 call_type = build_pointer_type (type);
2586 mark_addressable (slot);
2587 args = tree_cons (NULL_TREE, build1 (ADDR_EXPR, call_type, slot),
2588 TREE_CHAIN (args));
2590 call_expr = build (CALL_EXPR, call_type, fn, args, NULL_TREE);
2591 TREE_SIDE_EFFECTS (call_expr) = 1;
2593 /* If we're using the non-reentrant PCC calling convention, then we
2594 need to copy the returned value out of the static buffer into the
2595 SLOT. */
2596 copy_from_buffer_p = 0;
2597 #ifdef PCC_STATIC_STRUCT_RETURN
2598 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2600 int old_ac;
2602 flag_access_control = 0;
2603 call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2604 flag_access_control = old_ac;
2605 copy_from_buffer_p = 1;
2607 #endif
2609 /* If this AGGR_INIT_EXPR indicates the value returned by a
2610 function, then we want to use the value of the initialized
2611 location as the result. */
2612 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2614 call_expr = build (COMPOUND_EXPR, type,
2615 call_expr, slot);
2616 TREE_SIDE_EFFECTS (call_expr) = 1;
2619 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2620 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2621 *tp = call_expr;
2623 /* Keep iterating. */
2624 return NULL_TREE;
2627 /* Generate RTL for FN. */
2629 void
2630 expand_body (fn)
2631 tree fn;
2633 int saved_lineno;
2634 char *saved_input_filename;
2636 /* When the parser calls us after finishing the body of a template
2637 function, we don't really want to expand the body. When we're
2638 processing an in-class definition of an inline function,
2639 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2640 to look at the function itself. */
2641 if (processing_template_decl
2642 || (DECL_LANG_SPECIFIC (fn)
2643 && DECL_TEMPLATE_INFO (fn)
2644 && uses_template_parms (DECL_TI_ARGS (fn))))
2646 /* Normally, collection only occurs in rest_of_compilation. So,
2647 if we don't collect here, we never collect junk generated
2648 during the processing of templates until we hit a
2649 non-template function. */
2650 ggc_collect ();
2651 return;
2654 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2655 walk_tree (&DECL_SAVED_TREE (fn), simplify_aggr_init_exprs_r, NULL);
2657 /* There's no reason to do any of the work here if we're only doing
2658 semantic analysis; this code just generates RTL. */
2659 if (flag_syntax_only)
2660 return;
2662 /* If possible, avoid generating RTL for this function. Instead,
2663 just record it as an inline function, and wait until end-of-file
2664 to decide whether to write it out or not. */
2665 if (/* We have to generate RTL if we can't inline trees. */
2666 flag_inline_trees
2667 /* Or if it's not an inline function. */
2668 && DECL_INLINE (fn)
2669 /* Or if we have to keep all inline functions anyhow. */
2670 && !flag_keep_inline_functions
2671 /* Or if we actually have a reference to the function. */
2672 && !DECL_NEEDED_P (fn)
2673 /* Or if we're at the end-of-file, and this function is not
2674 DECL_COMDAT. */
2675 && (!at_eof || DECL_COMDAT (fn))
2676 /* Or if this is a nested function. */
2677 && !hack_decl_function_context (fn))
2679 /* Give the function RTL now so that we can assign it to a
2680 function pointer, etc. */
2681 make_function_rtl (fn);
2682 /* Set DECL_EXTERNAL so that assemble_external will be called as
2683 necessary. We'll clear it again in finish_file. */
2684 if (!DECL_EXTERNAL (fn))
2686 DECL_NOT_REALLY_EXTERN (fn) = 1;
2687 DECL_EXTERNAL (fn) = 1;
2689 /* Remember this function. In finish_file we'll decide if
2690 we actually need to write this function out. */
2691 mark_inline_for_output (fn);
2692 return;
2695 /* Optimize the body of the function before expanding it. */
2696 optimize_function (fn);
2698 /* Save the current file name and line number. When we expand the
2699 body of the function, we'll set LINENO and INPUT_FILENAME so that
2700 error-mesages come out in the right places. */
2701 saved_lineno = lineno;
2702 saved_input_filename = input_filename;
2703 lineno = DECL_SOURCE_LINE (fn);
2704 input_filename = DECL_SOURCE_FILE (fn);
2706 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2707 store_parm_decls ();
2709 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2710 any of the other magic variables we set up when starting a
2711 function body. */
2712 current_function_name_declared = 1;
2714 /* Expand the body. */
2715 expand_stmt (DECL_SAVED_TREE (fn));
2717 /* Statements should always be full-expressions at the outermost set
2718 of curly braces for a function. */
2719 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2721 /* The outermost statement for a function contains the line number
2722 recorded when we finished processing the function. */
2723 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2725 /* Generate code for the function. */
2726 finish_function (lineno, 0);
2728 /* If possible, obliterate the body of the function so that it can
2729 be garbage collected. */
2730 if (flag_dump_translation_unit)
2731 /* Keep the body; we're going to dump it. */
2733 else if (DECL_INLINE (fn) && flag_inline_trees)
2734 /* We might need the body of this function so that we can expand
2735 it inline somewhere else. */
2737 else
2738 /* We don't need the body; blow it away. */
2739 DECL_SAVED_TREE (fn) = NULL_TREE;
2741 /* And restore the current source position. */
2742 lineno = saved_lineno;
2743 input_filename = saved_input_filename;