* cp/semantics.c (prune_unused_decls): New function.
[official-gcc.git] / gcc / cp / semantics.c
blob6584a4204f90e627396fddd5f7eca02d3e17f720
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"
38 #include "timevar.h"
40 /* There routines provide a modular interface to perform many parsing
41 operations. They may therefore be used during actual parsing, or
42 during template instantiation, which may be regarded as a
43 degenerate form of parsing. Since the current g++ parser is
44 lacking in several respects, and will be reimplemented, we are
45 attempting to move most code that is not directly related to
46 parsing into this file; that will make implementing the new parser
47 much easier since it will be able to make use of these routines. */
49 static tree maybe_convert_cond PARAMS ((tree));
50 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
51 static tree prune_unused_decls PARAMS ((tree *, int *, void *));
52 static void deferred_type_access_control PARAMS ((void));
53 static void emit_associated_thunks PARAMS ((tree));
55 /* Record the fact that STMT was the last statement added to the
56 statement tree. */
58 #define SET_LAST_STMT(stmt) \
59 (current_stmt_tree->x_last_stmt = (stmt))
61 /* When parsing a template, LAST_TREE contains the last statement
62 parsed. These are chained together through the TREE_CHAIN field,
63 but often need to be re-organized since the parse is performed
64 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
65 STMT. */
67 #define RECHAIN_STMTS(stmt, substmt) \
68 do { \
69 substmt = TREE_CHAIN (stmt); \
70 TREE_CHAIN (stmt) = NULL_TREE; \
71 SET_LAST_STMT (stmt); \
72 } while (0)
74 /* Finish processing the COND, the SUBSTMT condition for STMT. */
76 #define FINISH_COND(cond, stmt, substmt) \
77 do { \
78 if (last_tree != stmt) \
79 { \
80 RECHAIN_STMTS (stmt, substmt); \
81 if (!processing_template_decl) \
82 { \
83 cond = build_tree_list (substmt, cond); \
84 substmt = cond; \
85 } \
86 } \
87 else \
88 substmt = cond; \
89 } while (0)
91 /* Wrapper since C and C++ expand_expr_stmt are different. */
93 expand_expr_stmt_fn lang_expand_expr_stmt = cplus_expand_expr_stmt;
95 /* Wrapper function instead of #define for use with c-common code. */
97 void
98 set_current_function_name_declared (i)
99 int i;
101 cp_function_chain->name_declared = i;
104 /* Returns non-zero if the current statement is a full expression,
105 i.e. temporaries created during that statement should be destroyed
106 at the end of the statement. */
109 stmts_are_full_exprs_p ()
111 return current_stmt_tree->stmts_are_full_exprs_p;
114 /* One if we have already declared __FUNCTION__ (and related
115 variables) in the current function. Two if we are in the process
116 of doing so. */
119 current_function_name_declared ()
121 return cp_function_chain->name_declared;
124 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
125 flag for this because "A union for which objects or pointers are
126 declared is not an anonymous union" [class.union]. */
129 anon_aggr_type_p (node)
130 tree node;
132 return (CLASS_TYPE_P (node) && TYPE_LANG_SPECIFIC(node)->anon_aggr);
135 /* Finish a scope. */
137 tree
138 do_poplevel ()
140 tree block = NULL_TREE;
142 if (stmts_are_full_exprs_p ())
144 tree scope_stmts = NULL_TREE;
146 if (!processing_template_decl)
147 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
149 block = poplevel (kept_level_p (), 1, 0);
150 if (block && !processing_template_decl)
152 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
153 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
157 return block;
160 /* Begin a new scope. */
162 void
163 do_pushlevel ()
165 if (stmts_are_full_exprs_p ())
167 pushlevel (0);
168 if (!processing_template_decl)
169 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
173 /* T is a statement. Add it to the statement-tree. */
175 void
176 add_tree (t)
177 tree t;
179 /* Add T to the statement-tree. */
180 TREE_CHAIN (last_tree) = t;
181 SET_LAST_STMT (t);
182 /* When we expand a statement-tree, we must know whether or not the
183 statements are full-expresions. We record that fact here. */
184 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
187 /* Finish a goto-statement. */
189 void
190 finish_goto_stmt (destination)
191 tree destination;
193 if (TREE_CODE (destination) == IDENTIFIER_NODE)
194 destination = lookup_label (destination);
196 /* We warn about unused labels with -Wunused. That means we have to
197 mark the used labels as used. */
198 if (TREE_CODE (destination) == LABEL_DECL)
199 TREE_USED (destination) = 1;
201 if (TREE_CODE (destination) != LABEL_DECL)
202 /* We don't inline calls to functions with computed gotos.
203 Those functions are typically up to some funny business,
204 and may be depending on the labels being at particular
205 addresses, or some such. */
206 DECL_UNINLINABLE (current_function_decl) = 1;
208 check_goto (destination);
210 add_tree (build_stmt (GOTO_STMT, destination));
213 /* COND is the condition-expression for an if, while, etc.,
214 statement. Convert it to a boolean value, if appropriate. */
216 tree
217 maybe_convert_cond (cond)
218 tree cond;
220 /* Empty conditions remain empty. */
221 if (!cond)
222 return NULL_TREE;
224 /* Wait until we instantiate templates before doing conversion. */
225 if (processing_template_decl)
226 return cond;
228 /* Do the conversion. */
229 cond = convert_from_reference (cond);
230 return condition_conversion (cond);
233 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
235 void
236 finish_expr_stmt (expr)
237 tree expr;
239 if (expr != NULL_TREE)
241 if (!processing_template_decl
242 && !(stmts_are_full_exprs_p ())
243 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
244 && lvalue_p (expr))
245 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
246 expr = default_conversion (expr);
248 if (stmts_are_full_exprs_p ())
249 expr = convert_to_void (expr, "statement");
251 if (!processing_template_decl)
252 expr = break_out_cleanups (expr);
254 add_tree (build_stmt (EXPR_STMT, expr));
257 finish_stmt ();
259 /* This was an expression-statement, so we save the type of the
260 expression. */
261 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
265 /* Begin an if-statement. Returns a newly created IF_STMT if
266 appropriate. */
268 tree
269 begin_if_stmt ()
271 tree r;
272 do_pushlevel ();
273 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
274 add_tree (r);
275 return r;
278 /* Process the COND of an if-statement, which may be given by
279 IF_STMT. */
281 void
282 finish_if_stmt_cond (cond, if_stmt)
283 tree cond;
284 tree if_stmt;
286 cond = maybe_convert_cond (cond);
287 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
290 /* Finish the then-clause of an if-statement, which may be given by
291 IF_STMT. */
293 tree
294 finish_then_clause (if_stmt)
295 tree if_stmt;
297 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
298 SET_LAST_STMT (if_stmt);
299 return if_stmt;
302 /* Begin the else-clause of an if-statement. */
304 void
305 begin_else_clause ()
309 /* Finish the else-clause of an if-statement, which may be given by
310 IF_STMT. */
312 void
313 finish_else_clause (if_stmt)
314 tree if_stmt;
316 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
319 /* Finsh an if-statement. */
321 void
322 finish_if_stmt ()
324 do_poplevel ();
325 finish_stmt ();
328 void
329 clear_out_block ()
331 /* If COND wasn't a declaration, clear out the
332 block we made for it and start a new one here so the
333 optimization in expand_end_loop will work. */
334 if (getdecls () == NULL_TREE)
336 do_poplevel ();
337 do_pushlevel ();
341 /* Begin a while-statement. Returns a newly created WHILE_STMT if
342 appropriate. */
344 tree
345 begin_while_stmt ()
347 tree r;
348 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
349 add_tree (r);
350 do_pushlevel ();
351 return r;
354 /* Process the COND of a while-statement, which may be given by
355 WHILE_STMT. */
357 void
358 finish_while_stmt_cond (cond, while_stmt)
359 tree cond;
360 tree while_stmt;
362 cond = maybe_convert_cond (cond);
363 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
364 clear_out_block ();
367 /* Finish a while-statement, which may be given by WHILE_STMT. */
369 void
370 finish_while_stmt (while_stmt)
371 tree while_stmt;
373 do_poplevel ();
374 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
375 finish_stmt ();
378 /* Begin a do-statement. Returns a newly created DO_STMT if
379 appropriate. */
381 tree
382 begin_do_stmt ()
384 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
385 add_tree (r);
386 return r;
389 /* Finish the body of a do-statement, which may be given by DO_STMT. */
391 void
392 finish_do_body (do_stmt)
393 tree do_stmt;
395 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
398 /* Finish a do-statement, which may be given by DO_STMT, and whose
399 COND is as indicated. */
401 void
402 finish_do_stmt (cond, do_stmt)
403 tree cond;
404 tree do_stmt;
406 cond = maybe_convert_cond (cond);
407 DO_COND (do_stmt) = cond;
408 finish_stmt ();
411 /* Finish a return-statement. The EXPRESSION returned, if any, is as
412 indicated. */
414 void
415 finish_return_stmt (expr)
416 tree expr;
418 if (!processing_template_decl)
419 expr = check_return_expr (expr);
420 if (!processing_template_decl)
422 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
424 /* Even returns without a value in a constructor must return
425 `this'. We accomplish this by sending all returns in a
426 constructor to the CTOR_LABEL; finish_function emits code to
427 return a value there. When we finally generate the real
428 return statement, CTOR_LABEL is no longer set, and we fall
429 through into the normal return-processing code below. */
430 finish_goto_stmt (ctor_label);
431 return;
433 else if (DECL_DESTRUCTOR_P (current_function_decl))
435 /* Similarly, all destructors must run destructors for
436 base-classes before returning. So, all returns in a
437 destructor get sent to the DTOR_LABEL; finsh_function emits
438 code to return a value there. */
439 finish_goto_stmt (dtor_label);
440 return;
443 add_tree (build_stmt (RETURN_STMT, expr));
444 finish_stmt ();
447 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
449 tree
450 begin_for_stmt ()
452 tree r;
454 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
455 NULL_TREE, NULL_TREE);
456 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
457 add_tree (r);
458 if (NEW_FOR_SCOPE_P (r))
460 do_pushlevel ();
461 note_level_for_for ();
464 return r;
467 /* Finish the for-init-statement of a for-statement, which may be
468 given by FOR_STMT. */
470 void
471 finish_for_init_stmt (for_stmt)
472 tree for_stmt;
474 if (last_tree != for_stmt)
475 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
476 do_pushlevel ();
479 /* Finish the COND of a for-statement, which may be given by
480 FOR_STMT. */
482 void
483 finish_for_cond (cond, for_stmt)
484 tree cond;
485 tree for_stmt;
487 cond = maybe_convert_cond (cond);
488 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
489 clear_out_block ();
492 /* Finish the increment-EXPRESSION in a for-statement, which may be
493 given by FOR_STMT. */
495 void
496 finish_for_expr (expr, for_stmt)
497 tree expr;
498 tree for_stmt;
500 FOR_EXPR (for_stmt) = expr;
503 /* Finish the body of a for-statement, which may be given by
504 FOR_STMT. The increment-EXPR for the loop must be
505 provided. */
507 void
508 finish_for_stmt (for_stmt)
509 tree for_stmt;
511 /* Pop the scope for the body of the loop. */
512 do_poplevel ();
513 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
514 if (NEW_FOR_SCOPE_P (for_stmt))
515 do_poplevel ();
516 finish_stmt ();
519 /* Finish a break-statement. */
521 void
522 finish_break_stmt ()
524 add_tree (build_stmt (BREAK_STMT));
527 /* Finish a continue-statement. */
529 void
530 finish_continue_stmt ()
532 add_tree (build_stmt (CONTINUE_STMT));
535 /* Begin a switch-statement. Returns a new SWITCH_STMT if
536 appropriate. */
538 tree
539 begin_switch_stmt ()
541 tree r;
542 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE);
543 add_tree (r);
544 do_pushlevel ();
545 return r;
548 /* Finish the cond of a switch-statement. */
550 void
551 finish_switch_cond (cond, switch_stmt)
552 tree cond;
553 tree switch_stmt;
555 if (!processing_template_decl)
557 /* Convert the condition to an integer or enumeration type. */
558 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
559 if (cond == NULL_TREE)
561 error ("switch quantity not an integer");
562 cond = error_mark_node;
564 if (cond != error_mark_node)
566 cond = default_conversion (cond);
567 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
570 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
571 push_switch ();
574 /* Finish the body of a switch-statement, which may be given by
575 SWITCH_STMT. The COND to switch on is indicated. */
577 void
578 finish_switch_stmt (switch_stmt)
579 tree switch_stmt;
581 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
582 pop_switch ();
583 do_poplevel ();
584 finish_stmt ();
587 /* Finish a case-label. */
589 void
590 finish_case_label (low_value, high_value)
591 tree low_value;
592 tree high_value;
594 /* Add a representation for the case label to the statement
595 tree. */
596 add_tree (build_stmt (CASE_LABEL, low_value, high_value));
597 /* And warn about crossing initializations, etc. */
598 if (!processing_template_decl)
599 define_case_label ();
602 /* Generate the RTL for T, which is a TRY_BLOCK. */
604 void genrtl_try_block (t)
605 tree t;
607 if (CLEANUP_P (t))
609 expand_eh_region_start ();
610 expand_stmt (TRY_STMTS (t));
611 expand_eh_region_end (protect_with_terminate (TRY_HANDLERS (t)));
613 else
615 if (FN_TRY_BLOCK_P (t)) {
616 if (! current_function_parms_stored)
617 store_parm_decls ();
618 expand_start_early_try_stmts ();
620 else {
621 emit_line_note (input_filename, lineno);
622 expand_start_try_stmts ();
625 expand_stmt (TRY_STMTS (t));
627 if (FN_TRY_BLOCK_P (t))
629 end_protect_partials ();
630 expand_start_all_catch ();
631 in_function_try_handler = 1;
632 expand_stmt (TRY_HANDLERS (t));
633 in_function_try_handler = 0;
634 expand_end_all_catch ();
636 else
638 expand_start_all_catch ();
639 expand_stmt (TRY_HANDLERS (t));
640 expand_end_all_catch ();
645 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
646 appropriate. */
648 tree
649 begin_try_block ()
651 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
652 add_tree (r);
653 return r;
656 /* Likewise, for a function-try-block. */
658 tree
659 begin_function_try_block ()
661 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
662 FN_TRY_BLOCK_P (r) = 1;
663 add_tree (r);
664 return r;
667 /* Finish a try-block, which may be given by TRY_BLOCK. */
669 void
670 finish_try_block (try_block)
671 tree try_block;
673 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
676 /* Finish the body of a cleanup try-block, which may be given by
677 TRY_BLOCK. */
679 void
680 finish_cleanup_try_block (try_block)
681 tree try_block;
683 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
686 /* Finish an implicitly generated try-block, with a cleanup is given
687 by CLEANUP. */
689 void
690 finish_cleanup (cleanup, try_block)
691 tree cleanup;
692 tree try_block;
694 TRY_HANDLERS (try_block) = cleanup;
695 CLEANUP_P (try_block) = 1;
698 /* Likewise, for a function-try-block. */
700 void
701 finish_function_try_block (try_block)
702 tree try_block;
704 if (TREE_CHAIN (try_block)
705 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
707 /* Chain the compound statement after the CTOR_INITIALIZER. */
708 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
709 /* And make the CTOR_INITIALIZER the body of the try-block. */
710 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
712 else
713 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
714 in_function_try_handler = 1;
717 /* Finish a handler-sequence for a try-block, which may be given by
718 TRY_BLOCK. */
720 void
721 finish_handler_sequence (try_block)
722 tree try_block;
724 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
725 check_handlers (TRY_HANDLERS (try_block));
728 /* Likewise, for a function-try-block. */
730 void
731 finish_function_handler_sequence (try_block)
732 tree try_block;
734 in_function_try_handler = 0;
735 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
736 check_handlers (TRY_HANDLERS (try_block));
739 /* Generate the RTL for T, which is a HANDLER. */
741 void
742 genrtl_handler (t)
743 tree t;
745 genrtl_do_pushlevel ();
746 expand_stmt (HANDLER_BODY (t));
747 if (!processing_template_decl)
749 /* Fall to outside the try statement when done executing
750 handler and we fall off end of handler. This is jump
751 Lresume in the documentation. */
752 expand_goto (top_label_entry (&caught_return_label_stack));
753 end_catch_handler ();
757 /* Begin a handler. Returns a HANDLER if appropriate. */
759 tree
760 begin_handler ()
762 tree r;
763 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
764 add_tree (r);
765 do_pushlevel ();
766 return r;
769 /* Finish the handler-parameters for a handler, which may be given by
770 HANDLER. DECL is the declaration for the catch parameter, or NULL
771 if this is a `catch (...)' clause. */
773 tree
774 finish_handler_parms (decl, handler)
775 tree decl;
776 tree handler;
778 tree blocks = NULL_TREE;
780 if (processing_template_decl)
782 if (decl)
784 decl = pushdecl (decl);
785 decl = push_template_decl (decl);
786 add_decl_stmt (decl);
787 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
790 else
791 blocks = expand_start_catch_block (decl);
793 if (decl)
794 TREE_TYPE (handler) = TREE_TYPE (decl);
796 return blocks;
799 /* Generate the RTL for a CATCH_BLOCK. */
801 void
802 genrtl_catch_block (type)
803 tree type;
805 start_catch_handler (type);
808 /* Note the beginning of a handler for TYPE. This function is called
809 at the point to which control should be transferred when an
810 appropriately-typed exception is thrown. */
812 void
813 begin_catch_block (type)
814 tree type;
816 add_tree (build (START_CATCH_STMT, type));
819 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
820 the return value from the matching call to finish_handler_parms. */
822 void
823 finish_handler (blocks, handler)
824 tree blocks;
825 tree handler;
827 if (!processing_template_decl)
828 expand_end_catch_block (blocks);
829 do_poplevel ();
830 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
833 /* Generate the RTL for T, which is a CTOR_STMT. */
835 void
836 genrtl_ctor_stmt (t)
837 tree t;
839 if (CTOR_BEGIN_P (t))
840 begin_protect_partials ();
841 else
842 /* After this point, any exceptions will cause the
843 destructor to be executed, so we no longer need to worry
844 about destroying the various subobjects ourselves. */
845 end_protect_partials ();
848 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
849 compound-statement does not define a scope. Returns a new
850 COMPOUND_STMT if appropriate. */
852 tree
853 begin_compound_stmt (has_no_scope)
854 int has_no_scope;
856 tree r;
857 int is_try = 0;
859 r = build_stmt (COMPOUND_STMT, NULL_TREE);
861 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
862 is_try = 1;
864 add_tree (r);
865 if (has_no_scope)
866 COMPOUND_STMT_NO_SCOPE (r) = 1;
868 last_expr_type = NULL_TREE;
870 if (!has_no_scope)
872 do_pushlevel ();
873 if (is_try)
874 note_level_for_eh ();
876 else
877 /* Normally, we try hard to keep the BLOCK for a
878 statement-expression. But, if it's a statement-expression with
879 a scopeless block, there's nothing to keep, and we don't want
880 to accidentally keep a block *inside* the scopeless block. */
881 keep_next_level (0);
883 /* If this is the outermost block of the function, declare the
884 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
885 if (cfun
886 && !(current_function_name_declared () )
887 && !has_no_scope)
889 cp_function_chain->name_declared = 1;
890 declare_function_name ();
893 return r;
896 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
897 If HAS_NO_SCOPE is non-zero, the compound statement does not define
898 a scope. */
900 tree
901 finish_compound_stmt (has_no_scope, compound_stmt)
902 int has_no_scope;
903 tree compound_stmt;
905 tree r;
906 tree t;
908 if (!has_no_scope)
909 r = do_poplevel ();
910 else
911 r = NULL_TREE;
913 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
915 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
916 the precise purpose of that variable is store the type of the
917 last expression statement within the last compound statement, we
918 preserve the value. */
919 t = last_expr_type;
920 finish_stmt ();
921 last_expr_type = t;
923 return r;
926 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
927 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
928 CLOBBERS. */
930 void
931 finish_asm_stmt (cv_qualifier, string, output_operands,
932 input_operands, clobbers)
933 tree cv_qualifier;
934 tree string;
935 tree output_operands;
936 tree input_operands;
937 tree clobbers;
939 tree r;
940 tree t;
942 if (TREE_CHAIN (string))
943 string = combine_strings (string);
945 if (cv_qualifier != NULL_TREE
946 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
948 cp_warning ("%s qualifier ignored on asm",
949 IDENTIFIER_POINTER (cv_qualifier));
950 cv_qualifier = NULL_TREE;
953 if (!processing_template_decl)
954 for (t = input_operands; t; t = TREE_CHAIN (t))
955 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
957 r = build_stmt (ASM_STMT, cv_qualifier, string,
958 output_operands, input_operands,
959 clobbers);
960 add_tree (r);
963 /* Finish a label with the indicated NAME. */
965 void
966 finish_label_stmt (name)
967 tree name;
969 tree decl = define_label (input_filename, lineno, name);
970 add_tree (build_stmt (LABEL_STMT, decl));
973 /* Finish a series of declarations for local labels. G++ allows users
974 to declare "local" labels, i.e., labels with scope. This extension
975 is useful when writing code involving statement-expressions. */
977 void
978 finish_label_decl (name)
979 tree name;
981 tree decl = declare_local_label (name);
982 add_decl_stmt (decl);
985 /* Create a declaration statement for the declaration given by the
986 DECL. */
988 void
989 add_decl_stmt (decl)
990 tree decl;
992 tree decl_stmt;
994 /* We need the type to last until instantiation time. */
995 decl_stmt = build_stmt (DECL_STMT, decl);
996 add_tree (decl_stmt);
999 /* Generate the RTL for a SUBOBJECT. */
1001 void
1002 genrtl_subobject (cleanup)
1003 tree cleanup;
1005 add_partial_entry (cleanup);
1008 /* We're in a constructor, and have just constructed a a subobject of
1009 *THIS. CLEANUP is code to run if an exception is thrown before the
1010 end of the current function is reached. */
1012 void
1013 finish_subobject (cleanup)
1014 tree cleanup;
1016 tree r = build_stmt (SUBOBJECT, cleanup);
1017 add_tree (r);
1020 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1022 void
1023 finish_decl_cleanup (decl, cleanup)
1024 tree decl;
1025 tree cleanup;
1027 add_tree (build_stmt (CLEANUP_STMT, decl, cleanup));
1030 /* Generate the RTL for a RETURN_INIT. */
1032 void
1033 genrtl_named_return_value (return_id, init)
1034 tree return_id, init;
1036 tree decl;
1037 /* Clear this out so that finish_named_return_value can set it
1038 again. */
1039 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
1041 decl = DECL_RESULT (current_function_decl);
1042 if (pedantic)
1043 /* Give this error as many times as there are occurrences,
1044 so that users can use Emacs compilation buffers to find
1045 and fix all such places. */
1046 pedwarn ("ISO C++ does not permit named return values");
1048 if (return_id != NULL_TREE)
1050 if (DECL_NAME (decl) == NULL_TREE)
1052 DECL_NAME (decl) = return_id;
1053 DECL_ASSEMBLER_NAME (decl) = return_id;
1055 else
1057 cp_error ("return identifier `%D' already in place", return_id);
1058 return;
1062 /* Can't let this happen for constructors. */
1063 if (DECL_CONSTRUCTOR_P (current_function_decl))
1065 error ("can't redefine default return value for constructors");
1066 return;
1069 /* If we have a named return value, put that in our scope as well. */
1070 if (DECL_NAME (decl) != NULL_TREE)
1072 /* Let `cp_finish_decl' know that this initializer is ok. */
1073 DECL_INITIAL (decl) = init;
1074 cp_finish_decl (decl, init, NULL_TREE, 0);
1075 store_return_init (decl);
1078 /* Don't use tree-inlining for functions with named return values.
1079 That doesn't work properly because we don't do any translation of
1080 the RETURN_INITs when they are copied. */
1081 DECL_UNINLINABLE (current_function_decl) = 1;
1084 /* Bind a name and initialization to the return value of
1085 the current function. */
1087 void
1088 finish_named_return_value (return_id, init)
1089 tree return_id, init;
1091 tree decl = DECL_RESULT (current_function_decl);
1093 if (pedantic)
1094 /* Give this error as many times as there are occurrences,
1095 so that users can use Emacs compilation buffers to find
1096 and fix all such places. */
1097 pedwarn ("ISO C++ does not permit named return values");
1099 if (return_id != NULL_TREE)
1101 if (DECL_NAME (decl) == NULL_TREE)
1103 DECL_NAME (decl) = return_id;
1104 DECL_ASSEMBLER_NAME (decl) = return_id;
1106 else
1108 cp_error ("return identifier `%D' already in place", return_id);
1109 return;
1113 /* Can't let this happen for constructors. */
1114 if (DECL_CONSTRUCTOR_P (current_function_decl))
1116 error ("can't redefine default return value for constructors");
1117 return;
1120 /* If we have a named return value, put that in our scope as well. */
1121 if (DECL_NAME (decl) != NULL_TREE)
1123 /* Let `cp_finish_decl' know that this initializer is ok. */
1124 DECL_INITIAL (decl) = init;
1125 if (doing_semantic_analysis_p ())
1126 pushdecl (decl);
1127 add_tree (build_stmt (RETURN_INIT, return_id, init));
1130 /* Don't use tree-inlining for functions with named return values.
1131 That doesn't work properly because we don't do any translation of
1132 the RETURN_INITs when they are copied. */
1133 DECL_UNINLINABLE (current_function_decl) = 1;
1136 /* The INIT_LIST is a list of mem-initializers, in the order they were
1137 written by the user. The TREE_VALUE of each node is a list of
1138 initializers for a particular subobject. The TREE_PURPOSE is a
1139 FIELD_DECL is the initializer is for a non-static data member, and
1140 a class type if the initializer is for a base class. */
1142 void
1143 finish_mem_initializers (init_list)
1144 tree init_list;
1146 tree member_init_list;
1147 tree base_init_list;
1148 tree last_base_warned_about;
1149 tree next;
1150 tree init;
1152 member_init_list = NULL_TREE;
1153 base_init_list = NULL_TREE;
1154 last_base_warned_about = NULL_TREE;
1156 for (init = init_list; init; init = next)
1158 next = TREE_CHAIN (init);
1159 if (TREE_CODE (TREE_PURPOSE (init)) == FIELD_DECL)
1161 TREE_CHAIN (init) = member_init_list;
1162 member_init_list = init;
1164 /* We're running through the initializers from right to left
1165 as we process them here. So, if we see a data member
1166 initializer after we see a base initializer, that
1167 actually means that the base initializer preceeded the
1168 data member initializer. */
1169 if (warn_reorder && last_base_warned_about != base_init_list)
1171 tree base;
1173 for (base = base_init_list;
1174 base != last_base_warned_about;
1175 base = TREE_CHAIN (base))
1177 cp_warning ("base initializer for `%T'",
1178 TREE_PURPOSE (base));
1179 warning (" will be re-ordered to precede member initializations");
1182 last_base_warned_about = base_init_list;
1185 else
1187 TREE_CHAIN (init) = base_init_list;
1188 base_init_list = init;
1192 setup_vtbl_ptr (member_init_list, base_init_list);
1195 /* Cache the value of this class's main virtual function table pointer
1196 in a register variable. This will save one indirection if a
1197 more than one virtual function call is made this function. */
1199 void
1200 setup_vtbl_ptr (member_init_list, base_init_list)
1201 tree member_init_list;
1202 tree base_init_list;
1204 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1206 /* If we've already done this, there's no need to do it again. */
1207 if (vtbls_set_up_p)
1208 return;
1210 if (DECL_CONSTRUCTOR_P (current_function_decl))
1212 if (processing_template_decl)
1213 add_tree (build_min_nt
1214 (CTOR_INITIALIZER,
1215 member_init_list, base_init_list));
1216 else
1218 tree ctor_stmt;
1220 /* Mark the beginning of the constructor. */
1221 ctor_stmt = build_stmt (CTOR_STMT);
1222 CTOR_BEGIN_P (ctor_stmt) = 1;
1223 add_tree (ctor_stmt);
1225 /* And actually initialize the base-classes and members. */
1226 emit_base_init (member_init_list, base_init_list);
1229 else if (DECL_DESTRUCTOR_P (current_function_decl)
1230 && !processing_template_decl)
1232 tree if_stmt;
1233 tree compound_stmt;
1234 int saved_cfnd;
1236 /* If the dtor is empty, and we know there is not any possible
1237 way we could use any vtable entries, before they are possibly
1238 set by a base class dtor, we don't have to setup the vtables,
1239 as we know that any base class dtor will set up any vtables
1240 it needs. We avoid MI, because one base class dtor can do a
1241 virtual dispatch to an overridden function that would need to
1242 have a non-related vtable set up, we cannot avoid setting up
1243 vtables in that case. We could change this to see if there
1244 is just one vtable. */
1245 if_stmt = begin_if_stmt ();
1247 /* If it is not safe to avoid setting up the vtables, then
1248 someone will change the condition to be boolean_true_node.
1249 (Actually, for now, we do not have code to set the condition
1250 appropriately, so we just assume that we always need to
1251 initialize the vtables.) */
1252 finish_if_stmt_cond (boolean_true_node, if_stmt);
1253 current_vcalls_possible_p = &IF_COND (if_stmt);
1255 /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1256 open the block for the if-body. */
1257 saved_cfnd = current_function_name_declared ();
1258 cp_function_chain->name_declared = 1;
1259 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1260 cp_function_chain->name_declared = saved_cfnd;
1262 /* Make all virtual function table pointers in non-virtual base
1263 classes point to CURRENT_CLASS_TYPE's virtual function
1264 tables. */
1265 initialize_vtbl_ptrs (current_class_ptr);
1267 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1268 finish_then_clause (if_stmt);
1269 finish_if_stmt ();
1272 /* Always keep the BLOCK node associated with the outermost pair of
1273 curly braces of a function. These are needed for correct
1274 operation of dwarfout.c. */
1275 keep_next_level (1);
1277 /* The virtual function tables are set up now. */
1278 vtbls_set_up_p = 1;
1282 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
1283 whether this statements opens or closes a scope. PARTIAL_P is true
1284 for a partial scope, i.e, the scope that begins after a label when
1285 an object that needs a cleanup is created. If BEGIN_P is nonzero,
1286 returns a new TREE_LIST representing the top of the SCOPE_STMT
1287 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
1288 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1289 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1290 SCOPE_BEGIN_P set. */
1292 tree
1293 add_scope_stmt (begin_p, partial_p)
1294 int begin_p;
1295 int partial_p;
1297 tree ss;
1298 tree top;
1300 /* Build the statement. */
1301 ss = build_stmt (SCOPE_STMT, NULL_TREE);
1302 SCOPE_BEGIN_P (ss) = begin_p;
1303 SCOPE_PARTIAL_P (ss) = partial_p;
1305 /* Keep the scope stack up to date. */
1306 if (begin_p)
1308 current_scope_stmt_stack
1309 = tree_cons (ss, NULL_TREE, current_scope_stmt_stack);
1310 top = current_scope_stmt_stack;
1312 else
1314 top = current_scope_stmt_stack;
1315 TREE_VALUE (top) = ss;
1316 current_scope_stmt_stack = TREE_CHAIN (top);
1319 /* Add the new statement to the statement-tree. */
1320 add_tree (ss);
1322 return top;
1325 /* Finish a parenthesized expression EXPR. */
1327 tree
1328 finish_parenthesized_expr (expr)
1329 tree expr;
1331 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1332 /* This inhibits warnings in truthvalue_conversion. */
1333 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1335 if (TREE_CODE (expr) == OFFSET_REF)
1336 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1337 enclosed in parentheses. */
1338 PTRMEM_OK_P (expr) = 0;
1339 return expr;
1342 /* Begin a statement-expression. The value returned must be passed to
1343 finish_stmt_expr. */
1345 tree
1346 begin_stmt_expr ()
1348 /* If we're outside a function, we won't have a statement-tree to
1349 work with. But, if we see a statement-expression we need to
1350 create one. */
1351 if (! cfun && !last_tree)
1352 begin_stmt_tree (&scope_chain->x_saved_tree);
1354 keep_next_level (1);
1355 /* If we're building a statement tree, then the upcoming compound
1356 statement will be chained onto the tree structure, starting at
1357 last_tree. We return last_tree so that we can later unhook the
1358 compound statement. */
1359 return last_tree;
1362 /* Used when beginning a statement-expression outside function scope.
1363 For example, when handling a file-scope initializer, we use this
1364 function. */
1366 tree
1367 begin_global_stmt_expr ()
1369 if (! cfun && !last_tree)
1370 begin_stmt_tree (&scope_chain->x_saved_tree);
1372 keep_next_level (1);
1374 return (last_tree != NULL_TREE) ? last_tree : expand_start_stmt_expr();
1377 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1379 tree
1380 finish_global_stmt_expr (stmt_expr)
1381 tree stmt_expr;
1383 stmt_expr = expand_end_stmt_expr (stmt_expr);
1385 if (! cfun
1386 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1387 finish_stmt_tree (&scope_chain->x_saved_tree);
1389 return stmt_expr;
1392 /* Finish a statement-expression. RTL_EXPR should be the value
1393 returned by the previous begin_stmt_expr; EXPR is the
1394 statement-expression. Returns an expression representing the
1395 statement-expression. */
1397 tree
1398 finish_stmt_expr (rtl_expr)
1399 tree rtl_expr;
1401 tree result;
1403 /* If the last thing in the statement-expression was not an
1404 expression-statement, then it has type `void'. */
1405 if (!last_expr_type)
1406 last_expr_type = void_type_node;
1407 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1408 TREE_SIDE_EFFECTS (result) = 1;
1410 /* Remove the compound statement from the tree structure; it is
1411 now saved in the STMT_EXPR. */
1412 SET_LAST_STMT (rtl_expr);
1413 TREE_CHAIN (last_tree) = NULL_TREE;
1415 /* If we created a statement-tree for this statement-expression,
1416 remove it now. */
1417 if (! cfun
1418 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1419 finish_stmt_tree (&scope_chain->x_saved_tree);
1421 return result;
1424 /* Finish a call to FN with ARGS. Returns a representation of the
1425 call. */
1427 tree
1428 finish_call_expr (fn, args, koenig)
1429 tree fn;
1430 tree args;
1431 int koenig;
1433 tree result;
1435 if (koenig)
1437 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1438 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1439 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1440 fn = do_identifier (fn, 2, args);
1442 result = build_x_function_call (fn, args, current_class_ref);
1444 if (TREE_CODE (result) == CALL_EXPR
1445 && (! TREE_TYPE (result)
1446 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1447 result = require_complete_type (result);
1449 return result;
1452 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1453 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1454 POSTDECREMENT_EXPR.) */
1456 tree
1457 finish_increment_expr (expr, code)
1458 tree expr;
1459 enum tree_code code;
1461 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1462 a COMPONENT_REF). This way if we've got, say, a reference to a
1463 static member that's being operated on, we don't end up trying to
1464 find a member operator for the class it's in. */
1466 if (TREE_CODE (expr) == OFFSET_REF)
1467 expr = resolve_offset_ref (expr);
1468 return build_x_unary_op (code, expr);
1471 /* Finish a use of `this'. Returns an expression for `this'. */
1473 tree
1474 finish_this_expr ()
1476 tree result;
1478 if (current_class_ptr)
1480 #ifdef WARNING_ABOUT_CCD
1481 TREE_USED (current_class_ptr) = 1;
1482 #endif
1483 result = current_class_ptr;
1485 else if (current_function_decl
1486 && DECL_STATIC_FUNCTION_P (current_function_decl))
1488 error ("`this' is unavailable for static member functions");
1489 result = error_mark_node;
1491 else
1493 if (current_function_decl)
1494 error ("invalid use of `this' in non-member function");
1495 else
1496 error ("invalid use of `this' at top level");
1497 result = error_mark_node;
1500 return result;
1503 /* Finish a member function call using OBJECT and ARGS as arguments to
1504 FN. Returns an expression for the call. */
1506 tree
1507 finish_object_call_expr (fn, object, args)
1508 tree fn;
1509 tree object;
1510 tree args;
1512 #if 0
1513 /* This is a future direction of this code, but because
1514 build_x_function_call cannot always undo what is done in
1515 build_component_ref entirely yet, we cannot do this. */
1517 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1518 return finish_call_expr (real_fn, args);
1519 #else
1520 if (DECL_DECLARES_TYPE_P (fn))
1522 if (processing_template_decl)
1523 /* This can happen on code like:
1525 class X;
1526 template <class T> void f(T t) {
1527 t.X();
1530 We just grab the underlying IDENTIFIER. */
1531 fn = DECL_NAME (fn);
1532 else
1534 cp_error ("calling type `%T' like a method", fn);
1535 return error_mark_node;
1539 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1540 #endif
1543 /* Finish a qualified member function call using OBJECT and ARGS as
1544 arguments to FN. Returns an expressino for the call. */
1546 tree
1547 finish_qualified_object_call_expr (fn, object, args)
1548 tree fn;
1549 tree object;
1550 tree args;
1552 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1553 TREE_OPERAND (fn, 1), args);
1556 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1557 being the scope, if any, of DESTRUCTOR. Returns an expression for
1558 the call. */
1560 tree
1561 finish_pseudo_destructor_call_expr (object, scope, destructor)
1562 tree object;
1563 tree scope;
1564 tree destructor;
1566 if (processing_template_decl)
1567 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1569 if (scope && scope != destructor)
1570 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1571 scope, destructor);
1573 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1574 && (TREE_CODE (TREE_TYPE (object)) !=
1575 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1576 cp_error ("`%E' is not of type `%T'", object, destructor);
1578 return cp_convert (void_type_node, object);
1581 /* Finish a call to a globally qualified member function FN using
1582 ARGS. Returns an expression for the call. */
1584 tree
1585 finish_qualified_call_expr (fn, args)
1586 tree fn;
1587 tree args;
1589 if (processing_template_decl)
1590 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1591 else
1592 return build_member_call (TREE_OPERAND (fn, 0),
1593 TREE_OPERAND (fn, 1),
1594 args);
1597 /* Finish an expression taking the address of LABEL. Returns an
1598 expression for the address. */
1600 tree
1601 finish_label_address_expr (label)
1602 tree label;
1604 tree result;
1606 label = lookup_label (label);
1607 if (label == NULL_TREE)
1608 result = null_pointer_node;
1609 else
1611 TREE_USED (label) = 1;
1612 result = build1 (ADDR_EXPR, ptr_type_node, label);
1613 TREE_CONSTANT (result) = 1;
1614 /* This function cannot be inlined. All jumps to the addressed
1615 label should wind up at the same point. */
1616 DECL_UNINLINABLE (current_function_decl) = 1;
1619 return result;
1622 /* Finish an expression of the form CODE EXPR. */
1624 tree
1625 finish_unary_op_expr (code, expr)
1626 enum tree_code code;
1627 tree expr;
1629 tree result = build_x_unary_op (code, expr);
1630 /* Inside a template, build_x_unary_op does not fold the
1631 expression. So check whether the result is folded before
1632 setting TREE_NEGATED_INT. */
1633 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1634 && TREE_CODE (result) == INTEGER_CST
1635 && !TREE_UNSIGNED (TREE_TYPE (result))
1636 && INT_CST_LT (result, integer_zero_node))
1637 TREE_NEGATED_INT (result) = 1;
1638 overflow_warning (result);
1639 return result;
1642 /* Finish an id-expression. */
1644 tree
1645 finish_id_expr (expr)
1646 tree expr;
1648 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1649 expr = do_identifier (expr, 1, NULL_TREE);
1651 return expr;
1654 static tree current_type_lookups;
1656 /* Perform deferred access control for types used in the type of a
1657 declaration. */
1659 static void
1660 deferred_type_access_control ()
1662 tree lookup = type_lookups;
1664 if (lookup == error_mark_node)
1665 return;
1667 for (; lookup; lookup = TREE_CHAIN (lookup))
1668 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1671 void
1672 decl_type_access_control (decl)
1673 tree decl;
1675 tree save_fn;
1677 if (type_lookups == error_mark_node)
1678 return;
1680 save_fn = current_function_decl;
1682 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1683 current_function_decl = decl;
1685 deferred_type_access_control ();
1687 current_function_decl = save_fn;
1689 /* Now strip away the checks for the current declarator; they were
1690 added to type_lookups after typed_declspecs saved the copy that
1691 ended up in current_type_lookups. */
1692 type_lookups = current_type_lookups;
1695 void
1696 save_type_access_control (lookups)
1697 tree lookups;
1699 current_type_lookups = lookups;
1702 /* Begin a function definition declared with DECL_SPECS and
1703 DECLARATOR. Returns non-zero if the function-declaration is
1704 legal. */
1707 begin_function_definition (decl_specs, declarator)
1708 tree decl_specs;
1709 tree declarator;
1711 tree specs;
1712 tree attrs;
1714 split_specs_attrs (decl_specs, &specs, &attrs);
1715 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1716 return 0;
1718 deferred_type_access_control ();
1719 type_lookups = error_mark_node;
1721 /* The things we're about to see are not directly qualified by any
1722 template headers we've seen thus far. */
1723 reset_specialization ();
1725 return 1;
1728 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1729 a SCOPE_REF. */
1731 tree
1732 begin_constructor_declarator (scope, name)
1733 tree scope;
1734 tree name;
1736 tree result = build_parse_node (SCOPE_REF, scope, name);
1737 enter_scope_of (result);
1738 return result;
1741 /* Finish an init-declarator. Returns a DECL. */
1743 tree
1744 finish_declarator (declarator, declspecs, attributes,
1745 prefix_attributes, initialized)
1746 tree declarator;
1747 tree declspecs;
1748 tree attributes;
1749 tree prefix_attributes;
1750 int initialized;
1752 return start_decl (declarator, declspecs, initialized, attributes,
1753 prefix_attributes);
1756 /* Finish a translation unit. */
1758 void
1759 finish_translation_unit ()
1761 /* In case there were missing closebraces,
1762 get us back to the global binding level. */
1763 pop_everything ();
1764 while (current_namespace != global_namespace)
1765 pop_namespace ();
1766 finish_file ();
1769 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1770 Returns the parameter. */
1772 tree
1773 finish_template_type_parm (aggr, identifier)
1774 tree aggr;
1775 tree identifier;
1777 if (aggr != class_type_node)
1779 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1780 aggr = class_type_node;
1783 return build_tree_list (aggr, identifier);
1786 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1787 Returns the parameter. */
1789 tree
1790 finish_template_template_parm (aggr, identifier)
1791 tree aggr;
1792 tree identifier;
1794 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1795 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1796 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1797 DECL_TEMPLATE_RESULT (tmpl) = decl;
1798 DECL_ARTIFICIAL (decl) = 1;
1799 end_template_decl ();
1801 return finish_template_type_parm (aggr, tmpl);
1804 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1805 non-zero, the parameter list was terminated by a `...'. */
1807 tree
1808 finish_parmlist (parms, ellipsis)
1809 tree parms;
1810 int ellipsis;
1812 if (!ellipsis)
1813 chainon (parms, void_list_node);
1814 /* We mark the PARMS as a parmlist so that declarator processing can
1815 disambiguate certain constructs. */
1816 if (parms != NULL_TREE)
1817 TREE_PARMLIST (parms) = 1;
1819 return parms;
1822 /* Begin a class definition, as indicated by T. */
1824 tree
1825 begin_class_definition (t)
1826 tree t;
1828 if (t == error_mark_node
1829 || ! IS_AGGR_TYPE (t))
1831 t = make_aggr_type (RECORD_TYPE);
1832 pushtag (make_anon_name (), t, 0);
1835 /* In a definition of a member class template, we will get here with an
1836 implicit typename, a TYPENAME_TYPE with a type. */
1837 if (TREE_CODE (t) == TYPENAME_TYPE)
1838 t = TREE_TYPE (t);
1840 /* If we generated a partial instantiation of this type, but now
1841 we're seeing a real definition, we're actually looking at a
1842 partial specialization. Consider:
1844 template <class T, class U>
1845 struct Y {};
1847 template <class T>
1848 struct X {};
1850 template <class T, class U>
1851 void f()
1853 typename X<Y<T, U> >::A a;
1856 template <class T, class U>
1857 struct X<Y<T, U> >
1861 We have to undo the effects of the previous partial
1862 instantiation. */
1863 if (PARTIAL_INSTANTIATION_P (t))
1865 if (!pedantic)
1867 /* Unfortunately, when we're not in pedantic mode, we
1868 attempt to actually fill in some of the fields of the
1869 partial instantiation, in order to support the implicit
1870 typename extension. Clear those fields now, in
1871 preparation for the definition here. The fields cleared
1872 here must match those set in instantiate_class_template.
1873 Look for a comment mentioning begin_class_definition
1874 there. */
1875 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1876 TYPE_FIELDS (t) = NULL_TREE;
1877 TYPE_METHODS (t) = NULL_TREE;
1878 CLASSTYPE_TAGS (t) = NULL_TREE;
1879 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1880 TYPE_SIZE (t) = NULL_TREE;
1883 /* This isn't a partial instantiation any more. */
1884 PARTIAL_INSTANTIATION_P (t) = 0;
1886 /* If this type was already complete, and we see another definition,
1887 that's an error. */
1888 else if (COMPLETE_TYPE_P (t))
1889 duplicate_tag_error (t);
1891 /* Update the location of the decl. */
1892 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1893 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1895 if (TYPE_BEING_DEFINED (t))
1897 t = make_aggr_type (TREE_CODE (t));
1898 pushtag (TYPE_IDENTIFIER (t), t, 0);
1900 maybe_process_partial_specialization (t);
1901 pushclass (t, 1);
1902 TYPE_BEING_DEFINED (t) = 1;
1903 TYPE_PACKED (t) = flag_pack_struct;
1904 /* Reset the interface data, at the earliest possible
1905 moment, as it might have been set via a class foo;
1906 before. */
1908 tree name = TYPE_IDENTIFIER (t);
1910 if (! ANON_AGGRNAME_P (name))
1912 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1913 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1914 (t, interface_unknown);
1917 /* Only leave this bit clear if we know this
1918 class is part of an interface-only specification. */
1919 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1920 || ! CLASSTYPE_INTERFACE_ONLY (t))
1921 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1923 reset_specialization();
1925 /* Make a declaration for this class in its own scope. */
1926 build_self_reference ();
1928 return t;
1931 /* Finish the member declaration given by DECL. */
1933 void
1934 finish_member_declaration (decl)
1935 tree decl;
1937 if (decl == error_mark_node || decl == NULL_TREE)
1938 return;
1940 if (decl == void_type_node)
1941 /* The COMPONENT was a friend, not a member, and so there's
1942 nothing for us to do. */
1943 return;
1945 /* We should see only one DECL at a time. */
1946 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1948 /* Set up access control for DECL. */
1949 TREE_PRIVATE (decl)
1950 = (current_access_specifier == access_private_node);
1951 TREE_PROTECTED (decl)
1952 = (current_access_specifier == access_protected_node);
1953 if (TREE_CODE (decl) == TEMPLATE_DECL)
1955 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1956 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1959 /* Mark the DECL as a member of the current class. */
1960 DECL_CONTEXT (decl) = current_class_type;
1962 /* [dcl.link]
1964 A C language linkage is ignored for the names of class members
1965 and the member function type of class member functions. */
1966 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1967 DECL_LANGUAGE (decl) = lang_cplusplus;
1969 /* Put functions on the TYPE_METHODS list and everything else on the
1970 TYPE_FIELDS list. Note that these are built up in reverse order.
1971 We reverse them (to obtain declaration order) in finish_struct. */
1972 if (TREE_CODE (decl) == FUNCTION_DECL
1973 || DECL_FUNCTION_TEMPLATE_P (decl))
1975 /* We also need to add this function to the
1976 CLASSTYPE_METHOD_VEC. */
1977 add_method (current_class_type, decl, /*error_p=*/0);
1979 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1980 TYPE_METHODS (current_class_type) = decl;
1982 else
1984 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1985 go at the beginning. The reason is that lookup_field_1
1986 searches the list in order, and we want a field name to
1987 override a type name so that the "struct stat hack" will
1988 work. In particular:
1990 struct S { enum E { }; int E } s;
1991 s.E = 3;
1993 is legal. In addition, the FIELD_DECLs must be maintained in
1994 declaration order so that class layout works as expected.
1995 However, we don't need that order until class layout, so we
1996 save a little time by putting FIELD_DECLs on in reverse order
1997 here, and then reversing them in finish_struct_1. (We could
1998 also keep a pointer to the correct insertion points in the
1999 list.) */
2001 if (TREE_CODE (decl) == TYPE_DECL)
2002 TYPE_FIELDS (current_class_type)
2003 = chainon (TYPE_FIELDS (current_class_type), decl);
2004 else
2006 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2007 TYPE_FIELDS (current_class_type) = decl;
2010 /* Enter the DECL into the scope of the class. */
2011 if (TREE_CODE (decl) != USING_DECL)
2012 pushdecl_class_level (decl);
2016 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
2017 the definition is immediately followed by a semicolon. Returns the
2018 type. */
2020 tree
2021 finish_class_definition (t, attributes, semi, pop_scope_p)
2022 tree t;
2023 tree attributes;
2024 int semi;
2025 int pop_scope_p;
2027 /* finish_struct nukes this anyway; if finish_exception does too,
2028 then it can go. */
2029 if (semi)
2030 note_got_semicolon (t);
2032 /* If we got any attributes in class_head, xref_tag will stick them in
2033 TREE_TYPE of the type. Grab them now. */
2034 attributes = chainon (TREE_TYPE (t), attributes);
2035 TREE_TYPE (t) = NULL_TREE;
2037 if (TREE_CODE (t) == ENUMERAL_TYPE)
2039 else
2041 t = finish_struct (t, attributes);
2042 if (semi)
2043 note_got_semicolon (t);
2046 if (! semi)
2047 check_for_missing_semicolon (t);
2048 if (pop_scope_p)
2049 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2050 if (current_scope () == current_function_decl)
2051 do_pending_defargs ();
2053 return t;
2056 /* Finish processing the default argument expressions cached during
2057 the processing of a class definition. */
2059 void
2060 begin_inline_definitions ()
2062 if (pending_inlines
2063 && current_scope () == current_function_decl)
2064 do_pending_inlines ();
2067 /* Finish processing the inline function definitions cached during the
2068 processing of a class definition. */
2070 void
2071 finish_inline_definitions ()
2073 if (current_class_type == NULL_TREE)
2074 clear_inline_text_obstack ();
2077 /* Finish processing the declaration of a member class template
2078 TYPES whose template parameters are given by PARMS. */
2080 tree
2081 finish_member_class_template (types)
2082 tree types;
2084 tree t;
2086 /* If there are declared, but undefined, partial specializations
2087 mixed in with the typespecs they will not yet have passed through
2088 maybe_process_partial_specialization, so we do that here. */
2089 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2090 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2091 maybe_process_partial_specialization (TREE_VALUE (t));
2093 note_list_got_semicolon (types);
2094 grok_x_components (types);
2095 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2096 /* The component was in fact a friend declaration. We avoid
2097 finish_member_template_decl performing certain checks by
2098 unsetting TYPES. */
2099 types = NULL_TREE;
2101 finish_member_template_decl (types);
2103 /* As with other component type declarations, we do
2104 not store the new DECL on the list of
2105 component_decls. */
2106 return NULL_TREE;
2109 /* Finish processsing a complete template declaration. The PARMS are
2110 the template parameters. */
2112 void
2113 finish_template_decl (parms)
2114 tree parms;
2116 if (parms)
2117 end_template_decl ();
2118 else
2119 end_specialization ();
2122 /* Finish processing a a template-id (which names a type) of the form
2123 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2124 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2125 the scope of template-id indicated. */
2127 tree
2128 finish_template_type (name, args, entering_scope)
2129 tree name;
2130 tree args;
2131 int entering_scope;
2133 tree decl;
2135 decl = lookup_template_class (name, args,
2136 NULL_TREE, NULL_TREE, entering_scope);
2137 if (decl != error_mark_node)
2138 decl = TYPE_STUB_DECL (decl);
2140 return decl;
2143 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2144 namespace scope or a class scope. */
2146 void
2147 enter_scope_of (sr)
2148 tree sr;
2150 tree scope = TREE_OPERAND (sr, 0);
2152 if (TREE_CODE (scope) == NAMESPACE_DECL)
2154 push_decl_namespace (scope);
2155 TREE_COMPLEXITY (sr) = -1;
2157 else if (scope != current_class_type)
2159 if (TREE_CODE (scope) == TYPENAME_TYPE)
2161 /* In a declarator for a template class member, the scope will
2162 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2163 scope = TREE_TYPE (scope);
2164 TREE_OPERAND (sr, 0) = scope;
2166 push_nested_class (scope, 3);
2167 TREE_COMPLEXITY (sr) = current_class_depth;
2171 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2172 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2173 BASE_CLASS, or NULL_TREE if an error occurred. The
2174 ACCESSS_SPECIFIER is one of
2175 access_{default,public,protected_private}[_virtual]_node.*/
2177 tree
2178 finish_base_specifier (access_specifier, base_class)
2179 tree access_specifier;
2180 tree base_class;
2182 tree type;
2183 tree result;
2185 if (base_class == NULL_TREE)
2187 error ("invalid base class");
2188 type = error_mark_node;
2190 else
2191 type = TREE_TYPE (base_class);
2193 if (! is_aggr_type (type, 1))
2194 result = NULL_TREE;
2195 else
2196 result = build_tree_list (access_specifier, type);
2198 return result;
2201 /* Called when multiple declarators are processed. If that is not
2202 premitted in this context, an error is issued. */
2204 void
2205 check_multiple_declarators ()
2207 /* [temp]
2209 In a template-declaration, explicit specialization, or explicit
2210 instantiation the init-declarator-list in the declaration shall
2211 contain at most one declarator.
2213 We don't just use PROCESSING_TEMPLATE_DECL for the first
2214 condition since that would disallow the perfectly legal code,
2215 like `template <class T> struct S { int i, j; };'. */
2216 tree scope = current_scope ();
2218 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2219 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2220 return;
2222 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2223 || processing_explicit_instantiation
2224 || processing_specialization)
2225 cp_error ("multiple declarators in template declaration");
2228 tree
2229 finish_typeof (expr)
2230 tree expr;
2232 if (processing_template_decl)
2234 tree t;
2236 t = make_aggr_type (TYPEOF_TYPE);
2237 TYPE_FIELDS (t) = expr;
2239 return t;
2242 return TREE_TYPE (expr);
2245 /* Remove declarations of internal variables that are not used from a
2246 stmt tree. To qualify, the variable must have a name and must have
2247 a zero DECL_SOURCE_LINE. We tried to remove all variables for
2248 which TREE_USED was false, but it turns out that there's tons of
2249 variables for which TREE_USED is false but that are still in fact
2250 used. */
2252 static tree
2253 prune_unused_decls (tp, walk_subtrees, data)
2254 tree *tp;
2255 int *walk_subtrees ATTRIBUTE_UNUSED;
2256 void *data ATTRIBUTE_UNUSED;
2258 tree t = *tp;
2260 if (t == NULL_TREE)
2262 *walk_subtrees = 0;
2263 return NULL_TREE;
2266 if (TREE_CODE (t) == DECL_STMT)
2268 tree d = DECL_STMT_DECL (t);
2269 if (!TREE_USED (d) && DECL_NAME (d) && DECL_SOURCE_LINE (d) == 0)
2271 *tp = TREE_CHAIN (t);
2272 /* Recurse on the new value of tp, otherwise we will skip
2273 the next statement. */
2274 return prune_unused_decls (tp, walk_subtrees, data);
2277 else if (TREE_CODE (t) == BLOCK)
2279 /* walk_tree doesn't inspect BLOCK_VARS, so we must do it by hand. */
2280 tree *vp;
2282 for (vp = &BLOCK_VARS (t); *vp; )
2284 tree v = *vp;
2285 if (! TREE_USED (v) && DECL_NAME (v) && DECL_SOURCE_LINE (v) == 0)
2286 *vp = TREE_CHAIN (v); /* drop */
2287 else
2288 vp = &TREE_CHAIN (v); /* advance */
2290 if (BLOCK_VARS (t) == NULL_TREE)
2291 TREE_USED (t) = 0;
2293 return NULL_TREE;
2296 /* Create an empty statement tree rooted at T. */
2298 void
2299 begin_stmt_tree (t)
2300 tree *t;
2302 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2303 what follows. We remove the extraneous statement in
2304 finish_stmt_tree. */
2305 *t = build_nt (EXPR_STMT, void_zero_node);
2306 SET_LAST_STMT (*t);
2307 last_expr_type = NULL_TREE;
2310 /* Finish the statement tree rooted at T. */
2312 void
2313 finish_stmt_tree (t)
2314 tree *t;
2316 tree stmt;
2317 int old_lineno;
2319 /* Remove the fake extra statement added in begin_stmt_tree. */
2320 stmt = TREE_CHAIN (*t);
2321 *t = stmt;
2322 SET_LAST_STMT (NULL_TREE);
2324 /* Remove unused decls from the stmt tree. walk_tree messes with
2325 the line number, so save/restore it. */
2326 old_lineno = lineno;
2327 walk_tree (t, prune_unused_decls, 0);
2328 lineno = old_lineno;
2330 if (cfun)
2332 /* The line-number recorded in the outermost statement in a function
2333 is the line number of the end of the function. */
2334 STMT_LINENO (stmt) = lineno;
2335 STMT_LINENO_FOR_FN_P (stmt) = 1;
2339 /* We're about to expand T, a statement. Set up appropriate context
2340 for the substitution. */
2342 void
2343 prep_stmt (t)
2344 tree t;
2346 if (!STMT_LINENO_FOR_FN_P (t))
2347 lineno = STMT_LINENO (t);
2348 current_stmt_tree->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2351 /* Generate RTL for the statement T, and its substatements, and any
2352 other statements at its nesting level. */
2354 tree
2355 lang_expand_stmt (t)
2356 tree t;
2358 tree rval = NULL_TREE;
2360 while (t && t != error_mark_node)
2362 int saved_stmts_are_full_exprs_p;
2364 /* Assume we'll have nothing to return. */
2365 rval = NULL_TREE;
2367 /* Set up context appropriately for handling this statement. */
2368 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
2369 prep_stmt (t);
2371 switch (TREE_CODE (t))
2373 case RETURN_STMT:
2374 genrtl_return_stmt (RETURN_EXPR (t));
2375 break;
2377 case EXPR_STMT:
2378 genrtl_expr_stmt (EXPR_STMT_EXPR (t));
2379 break;
2381 case DECL_STMT:
2382 genrtl_decl_stmt (t);
2383 break;
2385 case CLEANUP_STMT:
2386 genrtl_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2387 break;
2389 case START_CATCH_STMT:
2390 genrtl_catch_block (TREE_TYPE (t));
2391 break;
2393 case CTOR_STMT:
2394 genrtl_ctor_stmt (t);
2395 break;
2397 case FOR_STMT:
2398 genrtl_for_stmt (t);
2399 break;
2401 case WHILE_STMT:
2402 genrtl_while_stmt (t);
2403 break;
2405 case DO_STMT:
2406 genrtl_do_stmt (t);
2407 break;
2409 case IF_STMT:
2410 genrtl_if_stmt (t);
2411 break;
2413 case COMPOUND_STMT:
2414 genrtl_compound_stmt (t);
2415 break;
2417 case BREAK_STMT:
2418 genrtl_break_stmt ();
2419 break;
2421 case CONTINUE_STMT:
2422 genrtl_continue_stmt ();
2423 break;
2425 case SWITCH_STMT:
2426 genrtl_switch_stmt (t);
2427 break;
2429 case CASE_LABEL:
2430 genrtl_case_label (CASE_LOW (t), CASE_HIGH (t));
2431 break;
2433 case LABEL_STMT:
2434 expand_label (LABEL_STMT_LABEL (t));
2435 break;
2437 case GOTO_STMT:
2438 genrtl_goto_stmt (GOTO_DESTINATION (t));
2439 break;
2441 case ASM_STMT:
2442 genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
2443 ASM_OUTPUTS (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2444 break;
2446 case TRY_BLOCK:
2447 genrtl_try_block (t);
2448 break;
2450 case HANDLER:
2451 genrtl_handler (t);
2452 break;
2454 case SUBOBJECT:
2455 genrtl_subobject (SUBOBJECT_CLEANUP (t));
2456 break;
2458 case SCOPE_STMT:
2459 genrtl_scope_stmt (t);
2460 break;
2462 case RETURN_INIT:
2463 genrtl_named_return_value (TREE_OPERAND (t, 0),
2464 TREE_OPERAND (t, 1));
2465 break;
2467 default:
2468 my_friendly_abort (19990810);
2469 break;
2472 /* Restore saved state. */
2473 current_stmt_tree->stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2475 /* Go on to the next statement in this scope. */
2476 t = TREE_CHAIN (t);
2479 return rval;
2482 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2483 will equivalent CALL_EXPRs. */
2485 static tree
2486 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2487 tree *tp;
2488 int *walk_subtrees ATTRIBUTE_UNUSED;
2489 void *data ATTRIBUTE_UNUSED;
2491 tree aggr_init_expr;
2492 tree call_expr;
2493 tree fn;
2494 tree args;
2495 tree slot;
2496 tree type;
2497 tree call_type;
2498 int copy_from_buffer_p;
2500 aggr_init_expr = *tp;
2501 /* We don't need to walk into types; there's nothing in a type that
2502 needs simplification. (And, furthermore, there are places we
2503 actively don't want to go. For example, we don't want to wander
2504 into the default arguments for a FUNCTION_DECL that appears in a
2505 CALL_EXPR.) */
2506 if (TYPE_P (aggr_init_expr))
2508 *walk_subtrees = 0;
2509 return NULL_TREE;
2511 /* Only AGGR_INIT_EXPRs are interesting. */
2512 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2513 return NULL_TREE;
2515 /* Form an appropriate CALL_EXPR. */
2516 fn = TREE_OPERAND (aggr_init_expr, 0);
2517 args = TREE_OPERAND (aggr_init_expr, 1);
2518 slot = TREE_OPERAND (aggr_init_expr, 2);
2519 type = TREE_TYPE (aggr_init_expr);
2520 call_type = type;
2521 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2523 /* Replace the first argument with the address of the third
2524 argument to the AGGR_INIT_EXPR. */
2525 call_type = build_pointer_type (type);
2526 mark_addressable (slot);
2527 args = tree_cons (NULL_TREE, build1 (ADDR_EXPR, call_type, slot),
2528 TREE_CHAIN (args));
2530 call_expr = build (CALL_EXPR, call_type, fn, args, NULL_TREE);
2531 TREE_SIDE_EFFECTS (call_expr) = 1;
2533 /* If we're using the non-reentrant PCC calling convention, then we
2534 need to copy the returned value out of the static buffer into the
2535 SLOT. */
2536 copy_from_buffer_p = 0;
2537 #ifdef PCC_STATIC_STRUCT_RETURN
2538 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2540 int old_ac;
2542 flag_access_control = 0;
2543 call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2544 flag_access_control = old_ac;
2545 copy_from_buffer_p = 1;
2547 #endif
2549 /* If this AGGR_INIT_EXPR indicates the value returned by a
2550 function, then we want to use the value of the initialized
2551 location as the result. */
2552 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2554 call_expr = build (COMPOUND_EXPR, type,
2555 call_expr, slot);
2556 TREE_SIDE_EFFECTS (call_expr) = 1;
2559 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2560 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2561 *tp = call_expr;
2563 /* Keep iterating. */
2564 return NULL_TREE;
2567 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2569 static void
2570 emit_associated_thunks (fn)
2571 tree fn;
2573 /* When we use vcall offsets, we emit thunks with the virtual
2574 functions to which they thunk. The whole point of vcall offsets
2575 is so that you can know statically the entire set of thunks that
2576 will ever be needed for a given virtual function, thereby
2577 enabling you to output all the thunks with the function itself. */
2578 if (vcall_offsets_in_vtable_p () && DECL_VIRTUAL_P (fn))
2580 tree binfo;
2581 tree v;
2583 for (binfo = TYPE_BINFO (DECL_CONTEXT (fn));
2584 binfo;
2585 binfo = TREE_CHAIN (binfo))
2586 for (v = BINFO_VIRTUALS (binfo); v; v = TREE_CHAIN (v))
2587 if (BV_FN (v) == fn
2588 && (!integer_zerop (BV_DELTA (v))
2589 || BV_VCALL_INDEX (v)))
2591 tree thunk;
2592 tree vcall_index;
2594 if (BV_USE_VCALL_INDEX_P (v))
2596 vcall_index = BV_VCALL_INDEX (v);
2597 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
2599 else
2600 vcall_index = NULL_TREE;
2602 thunk = make_thunk (build1 (ADDR_EXPR,
2603 vfunc_ptr_type_node,
2604 fn),
2605 BV_DELTA (v),
2606 vcall_index,
2607 /*generate_with_vtable_p=*/0);
2608 use_thunk (thunk, /*emit_p=*/1);
2614 /* Generate RTL for FN. */
2616 void
2617 expand_body (fn)
2618 tree fn;
2620 int saved_lineno;
2621 const char *saved_input_filename;
2623 /* When the parser calls us after finishing the body of a template
2624 function, we don't really want to expand the body. When we're
2625 processing an in-class definition of an inline function,
2626 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2627 to look at the function itself. */
2628 if (processing_template_decl
2629 || (DECL_LANG_SPECIFIC (fn)
2630 && DECL_TEMPLATE_INFO (fn)
2631 && uses_template_parms (DECL_TI_ARGS (fn))))
2633 /* Normally, collection only occurs in rest_of_compilation. So,
2634 if we don't collect here, we never collect junk generated
2635 during the processing of templates until we hit a
2636 non-template function. */
2637 ggc_collect ();
2638 return;
2641 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2642 walk_tree (&DECL_SAVED_TREE (fn), simplify_aggr_init_exprs_r, NULL);
2644 /* If this is a constructor or destructor body, we have to clone it
2645 under the new ABI. */
2646 if (maybe_clone_body (fn))
2648 /* We don't want to process FN again, so pretend we've written
2649 it out, even though we haven't. */
2650 TREE_ASM_WRITTEN (fn) = 1;
2651 return;
2654 /* There's no reason to do any of the work here if we're only doing
2655 semantic analysis; this code just generates RTL. */
2656 if (flag_syntax_only)
2657 return;
2659 /* If possible, avoid generating RTL for this function. Instead,
2660 just record it as an inline function, and wait until end-of-file
2661 to decide whether to write it out or not. */
2662 if (/* We have to generate RTL if it's not an inline function. */
2663 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2664 /* Or if we have to keep all inline functions anyhow. */
2665 && !flag_keep_inline_functions
2666 /* Or if we actually have a reference to the function. */
2667 && !DECL_NEEDED_P (fn)
2668 /* Or if this is a nested function. */
2669 && !decl_function_context (fn))
2671 /* Give the function RTL now so that we can assign it to a
2672 function pointer, etc. */
2673 make_function_rtl (fn);
2674 /* Set DECL_EXTERNAL so that assemble_external will be called as
2675 necessary. We'll clear it again in finish_file. */
2676 if (!DECL_EXTERNAL (fn))
2678 DECL_NOT_REALLY_EXTERN (fn) = 1;
2679 DECL_EXTERNAL (fn) = 1;
2681 /* Remember this function. In finish_file we'll decide if
2682 we actually need to write this function out. */
2683 defer_fn (fn);
2684 /* Let the back-end know that this funtion exists. */
2685 note_deferral_of_defined_inline_function (fn);
2686 return;
2689 /* Emit any thunks that should be emitted at the same time as FN. */
2690 emit_associated_thunks (fn);
2692 timevar_push (TV_INTEGRATION);
2694 /* Optimize the body of the function before expanding it. */
2695 optimize_function (fn);
2697 timevar_pop (TV_INTEGRATION);
2698 timevar_push (TV_EXPAND);
2700 /* Save the current file name and line number. When we expand the
2701 body of the function, we'll set LINENO and INPUT_FILENAME so that
2702 error-mesages come out in the right places. */
2703 saved_lineno = lineno;
2704 saved_input_filename = input_filename;
2705 lineno = DECL_SOURCE_LINE (fn);
2706 input_filename = DECL_SOURCE_FILE (fn);
2708 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2709 store_parm_decls ();
2710 current_function_is_thunk = DECL_THUNK_P (fn);
2712 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2713 any of the other magic variables we set up when starting a
2714 function body. */
2715 cp_function_chain->name_declared = 1;
2717 /* Expand the body. */
2718 expand_stmt (DECL_SAVED_TREE (fn));
2720 /* Statements should always be full-expressions at the outermost set
2721 of curly braces for a function. */
2722 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2724 /* The outermost statement for a function contains the line number
2725 recorded when we finished processing the function. */
2726 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2728 /* Generate code for the function. */
2729 finish_function (0);
2731 /* If possible, obliterate the body of the function so that it can
2732 be garbage collected. */
2733 if (flag_dump_translation_unit)
2734 /* Keep the body; we're going to dump it. */
2736 else if (DECL_INLINE (fn) && flag_inline_trees)
2737 /* We might need the body of this function so that we can expand
2738 it inline somewhere else. */
2740 else
2741 /* We don't need the body; blow it away. */
2742 DECL_SAVED_TREE (fn) = NULL_TREE;
2744 /* And restore the current source position. */
2745 lineno = saved_lineno;
2746 input_filename = saved_input_filename;
2747 extract_interface_info ();
2749 timevar_pop (TV_EXPAND);