FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / semantics.c
blobcf0bec7249bf96ad2b88e7cba4c70385e09d860c
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, 2001, 2002,
7 2003 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "tree-inline.h"
35 #include "except.h"
36 #include "lex.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "timevar.h"
43 #include "debug.h"
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. Since the current g++ parser is
49 lacking in several respects, and will be reimplemented, we are
50 attempting to move most code that is not directly related to
51 parsing into this file; that will make implementing the new parser
52 much easier since it will be able to make use of these routines. */
54 static tree maybe_convert_cond PARAMS ((tree));
55 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
56 static void emit_associated_thunks PARAMS ((tree));
57 static void genrtl_try_block PARAMS ((tree));
58 static void genrtl_eh_spec_block PARAMS ((tree));
59 static void genrtl_handler PARAMS ((tree));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
65 /* Finish processing the COND, the SUBSTMT condition for STMT. */
67 #define FINISH_COND(COND, STMT, SUBSTMT) \
68 do { \
69 if (last_tree != (STMT)) \
70 { \
71 RECHAIN_STMTS (STMT, SUBSTMT); \
72 if (!processing_template_decl) \
73 { \
74 (COND) = build_tree_list (SUBSTMT, COND); \
75 (SUBSTMT) = (COND); \
76 } \
77 } \
78 else \
79 (SUBSTMT) = (COND); \
80 } while (0)
82 /* Data for deferred access checking. */
83 static GTY(()) deferred_access *deferred_access_stack;
84 static GTY(()) deferred_access *deferred_access_free_list;
86 /* Save the current deferred access states and start deferred
87 access checking iff DEFER_P is true. */
89 void push_deferring_access_checks (bool deferring_p)
91 deferred_access *d;
93 /* Recycle previously used free store if available. */
94 if (deferred_access_free_list)
96 d = deferred_access_free_list;
97 deferred_access_free_list = d->next;
99 else
100 d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
102 d->next = deferred_access_stack;
103 d->deferred_access_checks = NULL_TREE;
104 d->deferring_access_checks_p = deferring_p;
105 deferred_access_stack = d;
108 /* Resume deferring access checks again after we stopped doing
109 this previously. */
111 void resume_deferring_access_checks (void)
113 deferred_access_stack->deferring_access_checks_p = true;
116 /* Stop deferring access checks. */
118 void stop_deferring_access_checks (void)
120 deferred_access_stack->deferring_access_checks_p = false;
123 /* Discard the current deferred access checks and restore the
124 previous states. */
126 void pop_deferring_access_checks (void)
128 deferred_access *d = deferred_access_stack;
129 deferred_access_stack = d->next;
131 /* Remove references to access checks TREE_LIST. */
132 d->deferred_access_checks = NULL_TREE;
134 /* Store in free list for later use. */
135 d->next = deferred_access_free_list;
136 deferred_access_free_list = d;
139 /* Returns a TREE_LIST representing the deferred checks.
140 The TREE_PURPOSE of each node is the type through which the
141 access occurred; the TREE_VALUE is the declaration named.
144 tree get_deferred_access_checks (void)
146 return deferred_access_stack->deferred_access_checks;
149 /* Take current deferred checks and combine with the
150 previous states if we also defer checks previously.
151 Otherwise perform checks now. */
153 void pop_to_parent_deferring_access_checks (void)
155 tree deferred_check = get_deferred_access_checks ();
156 deferred_access *d1 = deferred_access_stack;
157 deferred_access *d2 = deferred_access_stack->next;
158 deferred_access *d3 = deferred_access_stack->next->next;
160 /* Temporary swap the order of the top two states, just to make
161 sure the garbage collector will not reclaim the memory during
162 processing below. */
163 deferred_access_stack = d2;
164 d2->next = d1;
165 d1->next = d3;
167 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
168 /* Perform deferred check if required. */
169 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
170 TREE_VALUE (deferred_check));
172 deferred_access_stack = d1;
173 d1->next = d2;
174 d2->next = d3;
175 pop_deferring_access_checks ();
178 /* Perform the deferred access checks. */
180 void perform_deferred_access_checks (void)
182 tree deferred_check;
183 for (deferred_check = deferred_access_stack->deferred_access_checks;
184 deferred_check;
185 deferred_check = TREE_CHAIN (deferred_check))
186 /* Check access. */
187 enforce_access (TREE_PURPOSE (deferred_check),
188 TREE_VALUE (deferred_check));
190 /* No more deferred checks. */
191 deferred_access_stack->deferred_access_checks = NULL_TREE;
194 /* Defer checking the accessibility of DECL, when looked up in
195 CLASS_TYPE. */
197 void perform_or_defer_access_check (tree class_type, tree decl)
199 tree check;
201 /* If we are not supposed to defer access checks, just check now. */
202 if (!deferred_access_stack->deferring_access_checks_p)
204 enforce_access (class_type, decl);
205 return;
208 /* See if we are already going to perform this check. */
209 for (check = deferred_access_stack->deferred_access_checks;
210 check;
211 check = TREE_CHAIN (check))
212 if (TREE_VALUE (check) == decl
213 && same_type_p (TREE_PURPOSE (check), class_type))
214 return;
215 /* If not, record the check. */
216 deferred_access_stack->deferred_access_checks
217 = tree_cons (class_type, decl,
218 deferred_access_stack->deferred_access_checks);
221 /* Returns nonzero if the current statement is a full expression,
222 i.e. temporaries created during that statement should be destroyed
223 at the end of the statement. */
226 stmts_are_full_exprs_p ()
228 return current_stmt_tree ()->stmts_are_full_exprs_p;
231 /* Returns the stmt_tree (if any) to which statements are currently
232 being added. If there is no active statement-tree, NULL is
233 returned. */
235 stmt_tree
236 current_stmt_tree ()
238 return (cfun
239 ? &cfun->language->base.x_stmt_tree
240 : &scope_chain->x_stmt_tree);
243 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
244 flag for this because "A union for which objects or pointers are
245 declared is not an anonymous union" [class.union]. */
248 anon_aggr_type_p (node)
249 tree node;
251 return ANON_AGGR_TYPE_P (node);
254 /* Finish a scope. */
256 tree
257 do_poplevel ()
259 tree block = NULL_TREE;
261 if (stmts_are_full_exprs_p ())
263 tree scope_stmts = NULL_TREE;
265 block = poplevel (kept_level_p (), 1, 0);
266 if (!processing_template_decl)
268 /* This needs to come after the poplevel so that partial scopes
269 are properly nested. */
270 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
271 if (block)
273 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
274 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
279 return block;
282 /* Begin a new scope. */
284 void
285 do_pushlevel (scope_kind sk)
287 if (stmts_are_full_exprs_p ())
289 if (!processing_template_decl)
290 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
291 begin_scope (sk);
295 /* Finish a goto-statement. */
297 tree
298 finish_goto_stmt (destination)
299 tree destination;
301 if (TREE_CODE (destination) == IDENTIFIER_NODE)
302 destination = lookup_label (destination);
304 /* We warn about unused labels with -Wunused. That means we have to
305 mark the used labels as used. */
306 if (TREE_CODE (destination) == LABEL_DECL)
307 TREE_USED (destination) = 1;
309 if (TREE_CODE (destination) != LABEL_DECL)
310 /* We don't inline calls to functions with computed gotos.
311 Those functions are typically up to some funny business,
312 and may be depending on the labels being at particular
313 addresses, or some such. */
314 DECL_UNINLINABLE (current_function_decl) = 1;
316 check_goto (destination);
318 return add_stmt (build_stmt (GOTO_STMT, destination));
321 /* COND is the condition-expression for an if, while, etc.,
322 statement. Convert it to a boolean value, if appropriate. */
324 tree
325 maybe_convert_cond (cond)
326 tree cond;
328 /* Empty conditions remain empty. */
329 if (!cond)
330 return NULL_TREE;
332 /* Wait until we instantiate templates before doing conversion. */
333 if (processing_template_decl)
334 return cond;
336 /* Do the conversion. */
337 cond = convert_from_reference (cond);
338 return condition_conversion (cond);
341 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
343 tree
344 finish_expr_stmt (expr)
345 tree expr;
347 tree r = NULL_TREE;
348 tree expr_type = NULL_TREE;;
350 if (expr != NULL_TREE)
352 if (!processing_template_decl
353 && !(stmts_are_full_exprs_p ())
354 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
355 && lvalue_p (expr))
356 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
357 expr = default_conversion (expr);
359 /* Remember the type of the expression. */
360 expr_type = TREE_TYPE (expr);
362 if (stmts_are_full_exprs_p ())
363 expr = convert_to_void (expr, "statement");
365 r = add_stmt (build_stmt (EXPR_STMT, expr));
368 finish_stmt ();
370 /* This was an expression-statement, so we save the type of the
371 expression. */
372 last_expr_type = expr_type;
374 return r;
378 /* Begin an if-statement. Returns a newly created IF_STMT if
379 appropriate. */
381 tree
382 begin_if_stmt ()
384 tree r;
385 do_pushlevel (sk_block);
386 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
387 add_stmt (r);
388 return r;
391 /* Process the COND of an if-statement, which may be given by
392 IF_STMT. */
394 void
395 finish_if_stmt_cond (cond, if_stmt)
396 tree cond;
397 tree if_stmt;
399 cond = maybe_convert_cond (cond);
400 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
403 /* Finish the then-clause of an if-statement, which may be given by
404 IF_STMT. */
406 tree
407 finish_then_clause (if_stmt)
408 tree if_stmt;
410 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
411 return if_stmt;
414 /* Begin the else-clause of an if-statement. */
416 void
417 begin_else_clause ()
421 /* Finish the else-clause of an if-statement, which may be given by
422 IF_STMT. */
424 void
425 finish_else_clause (if_stmt)
426 tree if_stmt;
428 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
431 /* Finish an if-statement. */
433 void
434 finish_if_stmt ()
436 finish_stmt ();
437 do_poplevel ();
440 /* Begin a while-statement. Returns a newly created WHILE_STMT if
441 appropriate. */
443 tree
444 begin_while_stmt ()
446 tree r;
447 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
448 add_stmt (r);
449 do_pushlevel (sk_block);
450 return r;
453 /* Process the COND of a while-statement, which may be given by
454 WHILE_STMT. */
456 void
457 finish_while_stmt_cond (cond, while_stmt)
458 tree cond;
459 tree while_stmt;
461 cond = maybe_convert_cond (cond);
462 if (processing_template_decl)
463 /* Don't mess with condition decls in a template. */
464 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
465 else if (getdecls () == NULL_TREE)
466 /* It was a simple condition; install it. */
467 WHILE_COND (while_stmt) = cond;
468 else
470 /* If there was a declaration in the condition, we can't leave it
471 there; transform
472 while (A x = 42) { }
474 while (true) { A x = 42; if (!x) break; } */
475 tree if_stmt;
476 WHILE_COND (while_stmt) = boolean_true_node;
478 if_stmt = begin_if_stmt ();
479 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
480 finish_if_stmt_cond (cond, if_stmt);
481 finish_break_stmt ();
482 finish_then_clause (if_stmt);
483 finish_if_stmt ();
487 /* Finish a while-statement, which may be given by WHILE_STMT. */
489 void
490 finish_while_stmt (while_stmt)
491 tree while_stmt;
493 do_poplevel ();
494 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
495 finish_stmt ();
498 /* Begin a do-statement. Returns a newly created DO_STMT if
499 appropriate. */
501 tree
502 begin_do_stmt ()
504 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
505 add_stmt (r);
506 return r;
509 /* Finish the body of a do-statement, which may be given by DO_STMT. */
511 void
512 finish_do_body (do_stmt)
513 tree do_stmt;
515 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
518 /* Finish a do-statement, which may be given by DO_STMT, and whose
519 COND is as indicated. */
521 void
522 finish_do_stmt (cond, do_stmt)
523 tree cond;
524 tree do_stmt;
526 cond = maybe_convert_cond (cond);
527 DO_COND (do_stmt) = cond;
528 finish_stmt ();
531 /* Finish a return-statement. The EXPRESSION returned, if any, is as
532 indicated. */
534 tree
535 finish_return_stmt (expr)
536 tree expr;
538 tree r;
540 expr = check_return_expr (expr);
541 if (!processing_template_decl)
543 if (DECL_DESTRUCTOR_P (current_function_decl))
545 /* Similarly, all destructors must run destructors for
546 base-classes before returning. So, all returns in a
547 destructor get sent to the DTOR_LABEL; finish_function emits
548 code to return a value there. */
549 return finish_goto_stmt (dtor_label);
552 r = add_stmt (build_stmt (RETURN_STMT, expr));
553 finish_stmt ();
555 return r;
558 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
560 tree
561 begin_for_stmt ()
563 tree r;
565 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
566 NULL_TREE, NULL_TREE);
567 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
568 if (NEW_FOR_SCOPE_P (r))
569 do_pushlevel (sk_for);
570 add_stmt (r);
572 return r;
575 /* Finish the for-init-statement of a for-statement, which may be
576 given by FOR_STMT. */
578 void
579 finish_for_init_stmt (for_stmt)
580 tree for_stmt;
582 if (last_tree != for_stmt)
583 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
584 do_pushlevel (sk_block);
587 /* Finish the COND of a for-statement, which may be given by
588 FOR_STMT. */
590 void
591 finish_for_cond (cond, for_stmt)
592 tree cond;
593 tree for_stmt;
595 cond = maybe_convert_cond (cond);
596 if (processing_template_decl)
597 /* Don't mess with condition decls in a template. */
598 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
599 else if (getdecls () == NULL_TREE)
600 /* It was a simple condition; install it. */
601 FOR_COND (for_stmt) = cond;
602 else
604 /* If there was a declaration in the condition, we can't leave it
605 there; transform
606 for (; A x = 42;) { }
608 for (;;) { A x = 42; if (!x) break; } */
609 tree if_stmt;
610 FOR_COND (for_stmt) = NULL_TREE;
612 if_stmt = begin_if_stmt ();
613 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
614 finish_if_stmt_cond (cond, if_stmt);
615 finish_break_stmt ();
616 finish_then_clause (if_stmt);
617 finish_if_stmt ();
621 /* Finish the increment-EXPRESSION in a for-statement, which may be
622 given by FOR_STMT. */
624 void
625 finish_for_expr (expr, for_stmt)
626 tree expr;
627 tree for_stmt;
629 FOR_EXPR (for_stmt) = expr;
632 /* Finish the body of a for-statement, which may be given by
633 FOR_STMT. The increment-EXPR for the loop must be
634 provided. */
636 void
637 finish_for_stmt (for_stmt)
638 tree for_stmt;
640 /* Pop the scope for the body of the loop. */
641 do_poplevel ();
642 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
643 if (NEW_FOR_SCOPE_P (for_stmt))
644 do_poplevel ();
645 finish_stmt ();
648 /* Finish a break-statement. */
650 tree
651 finish_break_stmt ()
653 return add_stmt (build_break_stmt ());
656 /* Finish a continue-statement. */
658 tree
659 finish_continue_stmt ()
661 return add_stmt (build_continue_stmt ());
664 /* Begin a switch-statement. Returns a new SWITCH_STMT if
665 appropriate. */
667 tree
668 begin_switch_stmt ()
670 tree r;
671 do_pushlevel (sk_block);
672 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
673 add_stmt (r);
674 return r;
677 /* Finish the cond of a switch-statement. */
679 void
680 finish_switch_cond (cond, switch_stmt)
681 tree cond;
682 tree switch_stmt;
684 tree orig_type = NULL;
685 if (!processing_template_decl)
687 tree index;
689 /* Convert the condition to an integer or enumeration type. */
690 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
691 if (cond == NULL_TREE)
693 error ("switch quantity not an integer");
694 cond = error_mark_node;
696 orig_type = TREE_TYPE (cond);
697 if (cond != error_mark_node)
699 cond = default_conversion (cond);
700 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
703 if (cond != error_mark_node)
705 index = get_unwidened (cond, NULL_TREE);
706 /* We can't strip a conversion from a signed type to an unsigned,
707 because if we did, int_fits_type_p would do the wrong thing
708 when checking case values for being in range,
709 and it's too hard to do the right thing. */
710 if (TREE_UNSIGNED (TREE_TYPE (cond))
711 == TREE_UNSIGNED (TREE_TYPE (index)))
712 cond = index;
715 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
716 SWITCH_TYPE (switch_stmt) = orig_type;
717 push_switch (switch_stmt);
720 /* Finish the body of a switch-statement, which may be given by
721 SWITCH_STMT. The COND to switch on is indicated. */
723 void
724 finish_switch_stmt (switch_stmt)
725 tree switch_stmt;
727 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
728 pop_switch ();
729 finish_stmt ();
730 do_poplevel ();
733 /* Generate the RTL for T, which is a TRY_BLOCK. */
735 static void
736 genrtl_try_block (t)
737 tree t;
739 if (CLEANUP_P (t))
741 expand_eh_region_start ();
742 expand_stmt (TRY_STMTS (t));
743 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
745 else
747 if (!FN_TRY_BLOCK_P (t))
748 emit_line_note (input_filename, lineno);
750 expand_eh_region_start ();
751 expand_stmt (TRY_STMTS (t));
753 if (FN_TRY_BLOCK_P (t))
755 expand_start_all_catch ();
756 in_function_try_handler = 1;
757 expand_stmt (TRY_HANDLERS (t));
758 in_function_try_handler = 0;
759 expand_end_all_catch ();
761 else
763 expand_start_all_catch ();
764 expand_stmt (TRY_HANDLERS (t));
765 expand_end_all_catch ();
770 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
772 static void
773 genrtl_eh_spec_block (t)
774 tree t;
776 expand_eh_region_start ();
777 expand_stmt (EH_SPEC_STMTS (t));
778 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
779 build_call (call_unexpected_node,
780 tree_cons (NULL_TREE,
781 build_exc_ptr (),
782 NULL_TREE)));
785 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
786 appropriate. */
788 tree
789 begin_try_block ()
791 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
792 add_stmt (r);
793 return r;
796 /* Likewise, for a function-try-block. */
798 tree
799 begin_function_try_block ()
801 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
802 FN_TRY_BLOCK_P (r) = 1;
803 add_stmt (r);
804 return r;
807 /* Finish a try-block, which may be given by TRY_BLOCK. */
809 void
810 finish_try_block (try_block)
811 tree try_block;
813 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
816 /* Finish the body of a cleanup try-block, which may be given by
817 TRY_BLOCK. */
819 void
820 finish_cleanup_try_block (try_block)
821 tree try_block;
823 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
826 /* Finish an implicitly generated try-block, with a cleanup is given
827 by CLEANUP. */
829 void
830 finish_cleanup (cleanup, try_block)
831 tree cleanup;
832 tree try_block;
834 TRY_HANDLERS (try_block) = cleanup;
835 CLEANUP_P (try_block) = 1;
838 /* Likewise, for a function-try-block. */
840 void
841 finish_function_try_block (try_block)
842 tree try_block;
844 if (TREE_CHAIN (try_block)
845 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
847 /* Chain the compound statement after the CTOR_INITIALIZER. */
848 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
849 /* And make the CTOR_INITIALIZER the body of the try-block. */
850 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
852 else
853 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
854 in_function_try_handler = 1;
857 /* Finish a handler-sequence for a try-block, which may be given by
858 TRY_BLOCK. */
860 void
861 finish_handler_sequence (try_block)
862 tree try_block;
864 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
865 check_handlers (TRY_HANDLERS (try_block));
868 /* Likewise, for a function-try-block. */
870 void
871 finish_function_handler_sequence (try_block)
872 tree try_block;
874 in_function_try_handler = 0;
875 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
876 check_handlers (TRY_HANDLERS (try_block));
879 /* Generate the RTL for T, which is a HANDLER. */
881 static void
882 genrtl_handler (t)
883 tree t;
885 genrtl_do_pushlevel ();
886 if (!processing_template_decl)
887 expand_start_catch (HANDLER_TYPE (t));
888 expand_stmt (HANDLER_BODY (t));
889 if (!processing_template_decl)
890 expand_end_catch ();
893 /* Begin a handler. Returns a HANDLER if appropriate. */
895 tree
896 begin_handler ()
898 tree r;
899 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
900 add_stmt (r);
901 /* Create a binding level for the eh_info and the exception object
902 cleanup. */
903 do_pushlevel (sk_catch);
904 return r;
907 /* Finish the handler-parameters for a handler, which may be given by
908 HANDLER. DECL is the declaration for the catch parameter, or NULL
909 if this is a `catch (...)' clause. */
911 void
912 finish_handler_parms (decl, handler)
913 tree decl;
914 tree handler;
916 tree type = NULL_TREE;
917 if (processing_template_decl)
919 if (decl)
921 decl = pushdecl (decl);
922 decl = push_template_decl (decl);
923 add_decl_stmt (decl);
924 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
925 type = TREE_TYPE (decl);
928 else
929 type = expand_start_catch_block (decl);
931 HANDLER_TYPE (handler) = type;
934 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
935 the return value from the matching call to finish_handler_parms. */
937 void
938 finish_handler (handler)
939 tree handler;
941 if (!processing_template_decl)
942 expand_end_catch_block ();
943 do_poplevel ();
944 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
947 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
948 compound-statement does not define a scope. Returns a new
949 COMPOUND_STMT if appropriate. */
951 tree
952 begin_compound_stmt (has_no_scope)
953 int has_no_scope;
955 tree r;
956 int is_try = 0;
958 r = build_stmt (COMPOUND_STMT, NULL_TREE);
960 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
961 is_try = 1;
963 add_stmt (r);
964 if (has_no_scope)
965 COMPOUND_STMT_NO_SCOPE (r) = 1;
967 last_expr_type = NULL_TREE;
969 if (!has_no_scope)
970 do_pushlevel (is_try ? sk_try : sk_block);
971 else
972 /* Normally, we try hard to keep the BLOCK for a
973 statement-expression. But, if it's a statement-expression with
974 a scopeless block, there's nothing to keep, and we don't want
975 to accidentally keep a block *inside* the scopeless block. */
976 keep_next_level (0);
978 return r;
981 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
982 If HAS_NO_SCOPE is nonzero, the compound statement does not define
983 a scope. */
985 tree
986 finish_compound_stmt (has_no_scope, compound_stmt)
987 int has_no_scope;
988 tree compound_stmt;
990 tree r;
991 tree t;
993 if (!has_no_scope)
994 r = do_poplevel ();
995 else
996 r = NULL_TREE;
998 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1000 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1001 the precise purpose of that variable is store the type of the
1002 last expression statement within the last compound statement, we
1003 preserve the value. */
1004 t = last_expr_type;
1005 finish_stmt ();
1006 last_expr_type = t;
1008 return r;
1011 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1012 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1013 CLOBBERS. */
1015 tree
1016 finish_asm_stmt (cv_qualifier, string, output_operands,
1017 input_operands, clobbers)
1018 tree cv_qualifier;
1019 tree string;
1020 tree output_operands;
1021 tree input_operands;
1022 tree clobbers;
1024 tree r;
1025 tree t;
1027 if (cv_qualifier != NULL_TREE
1028 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1030 warning ("%s qualifier ignored on asm",
1031 IDENTIFIER_POINTER (cv_qualifier));
1032 cv_qualifier = NULL_TREE;
1035 if (!processing_template_decl)
1037 int i;
1038 int ninputs;
1039 int noutputs;
1041 for (t = input_operands; t; t = TREE_CHAIN (t))
1043 tree converted_operand
1044 = decay_conversion (TREE_VALUE (t));
1046 /* If the type of the operand hasn't been determined (e.g.,
1047 because it involves an overloaded function), then issue
1048 an error message. There's no context available to
1049 resolve the overloading. */
1050 if (TREE_TYPE (converted_operand) == unknown_type_node)
1052 error ("type of asm operand `%E' could not be determined",
1053 TREE_VALUE (t));
1054 converted_operand = error_mark_node;
1056 TREE_VALUE (t) = converted_operand;
1059 ninputs = list_length (input_operands);
1060 noutputs = list_length (output_operands);
1062 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1064 bool allows_mem;
1065 bool allows_reg;
1066 bool is_inout;
1067 const char *constraint;
1068 tree operand;
1070 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1071 operand = TREE_VALUE (t);
1073 if (!parse_output_constraint (&constraint,
1074 i, ninputs, noutputs,
1075 &allows_mem,
1076 &allows_reg,
1077 &is_inout))
1079 /* By marking this operand as erroneous, we will not try
1080 to process this operand again in expand_asm_operands. */
1081 TREE_VALUE (t) = error_mark_node;
1082 continue;
1085 /* If the operand is a DECL that is going to end up in
1086 memory, assume it is addressable. This is a bit more
1087 conservative than it would ideally be; the exact test is
1088 buried deep in expand_asm_operands and depends on the
1089 DECL_RTL for the OPERAND -- which we don't have at this
1090 point. */
1091 if (!allows_reg && DECL_P (operand))
1092 cxx_mark_addressable (operand);
1096 r = build_stmt (ASM_STMT, cv_qualifier, string,
1097 output_operands, input_operands,
1098 clobbers);
1099 return add_stmt (r);
1102 /* Finish a label with the indicated NAME. */
1104 tree
1105 finish_label_stmt (name)
1106 tree name;
1108 tree decl = define_label (input_filename, lineno, name);
1109 return add_stmt (build_stmt (LABEL_STMT, decl));
1112 /* Finish a series of declarations for local labels. G++ allows users
1113 to declare "local" labels, i.e., labels with scope. This extension
1114 is useful when writing code involving statement-expressions. */
1116 void
1117 finish_label_decl (name)
1118 tree name;
1120 tree decl = declare_local_label (name);
1121 add_decl_stmt (decl);
1124 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1126 void
1127 finish_decl_cleanup (decl, cleanup)
1128 tree decl;
1129 tree cleanup;
1131 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1134 /* If the current scope exits with an exception, run CLEANUP. */
1136 void
1137 finish_eh_cleanup (cleanup)
1138 tree cleanup;
1140 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1141 CLEANUP_EH_ONLY (r) = 1;
1142 add_stmt (r);
1145 /* Begin processing a mem-initializer-list. */
1147 void
1148 begin_mem_initializers ()
1150 if (! DECL_CONSTRUCTOR_P (current_function_decl))
1151 error ("only constructors take base initializers");
1154 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1155 order they were written by the user. Each node is as for
1156 emit_mem_initializers. */
1158 void
1159 finish_mem_initializers (tree mem_inits)
1161 /* Reorder the MEM_INITS so that they are in the order they appeared
1162 in the source program. */
1163 mem_inits = nreverse (mem_inits);
1165 if (processing_template_decl)
1166 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1167 else
1168 emit_mem_initializers (mem_inits);
1171 /* Returns the stack of SCOPE_STMTs for the current function. */
1173 tree *
1174 current_scope_stmt_stack ()
1176 return &cfun->language->base.x_scope_stmt_stack;
1179 /* Finish a parenthesized expression EXPR. */
1181 tree
1182 finish_parenthesized_expr (expr)
1183 tree expr;
1185 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1186 /* This inhibits warnings in c_common_truthvalue_conversion. */
1187 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1189 if (TREE_CODE (expr) == OFFSET_REF)
1190 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1191 enclosed in parentheses. */
1192 PTRMEM_OK_P (expr) = 0;
1193 return expr;
1196 /* Finish a reference to a non-static data member (DECL) that is not
1197 preceded by `.' or `->'. */
1199 tree
1200 finish_non_static_data_member (tree decl, tree qualifying_scope)
1202 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1204 if (current_class_ptr == NULL_TREE)
1206 if (current_function_decl
1207 && DECL_STATIC_FUNCTION_P (current_function_decl))
1208 cp_error_at ("invalid use of member `%D' in static member function",
1209 decl);
1210 else
1211 cp_error_at ("invalid use of non-static data member `%D'", decl);
1212 error ("from this location");
1214 return error_mark_node;
1216 TREE_USED (current_class_ptr) = 1;
1217 if (processing_template_decl)
1218 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1219 current_class_ref, DECL_NAME (decl));
1220 else
1222 tree access_type = current_class_type;
1223 tree object = current_class_ref;
1225 while (!DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1227 access_type = TYPE_CONTEXT (access_type);
1228 while (DECL_P (access_type))
1229 access_type = DECL_CONTEXT (access_type);
1232 enforce_access (access_type, decl);
1234 /* If the data member was named `C::M', convert `*this' to `C'
1235 first. */
1236 if (qualifying_scope)
1238 tree binfo = NULL_TREE;
1239 object = build_scoped_ref (object, qualifying_scope,
1240 &binfo);
1243 return build_class_member_access_expr (object, decl,
1244 /*access_path=*/NULL_TREE,
1245 /*preserve_reference=*/false);
1249 /* Begin a statement-expression. The value returned must be passed to
1250 finish_stmt_expr. */
1252 tree
1253 begin_stmt_expr ()
1255 /* If we're outside a function, we won't have a statement-tree to
1256 work with. But, if we see a statement-expression we need to
1257 create one. */
1258 if (! cfun && !last_tree)
1259 begin_stmt_tree (&scope_chain->x_saved_tree);
1261 keep_next_level (1);
1262 /* If we're building a statement tree, then the upcoming compound
1263 statement will be chained onto the tree structure, starting at
1264 last_tree. We return last_tree so that we can later unhook the
1265 compound statement. */
1266 return last_tree;
1269 /* Used when beginning a statement-expression outside function scope.
1270 For example, when handling a file-scope initializer, we use this
1271 function. */
1273 tree
1274 begin_global_stmt_expr ()
1276 if (! cfun && !last_tree)
1277 begin_stmt_tree (&scope_chain->x_saved_tree);
1279 keep_next_level (1);
1281 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1284 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1286 tree
1287 finish_global_stmt_expr (stmt_expr)
1288 tree stmt_expr;
1290 stmt_expr = expand_end_stmt_expr (stmt_expr);
1292 if (! cfun
1293 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1294 finish_stmt_tree (&scope_chain->x_saved_tree);
1296 return stmt_expr;
1299 /* Finish a statement-expression. RTL_EXPR should be the value
1300 returned by the previous begin_stmt_expr; EXPR is the
1301 statement-expression. Returns an expression representing the
1302 statement-expression. */
1304 tree
1305 finish_stmt_expr (rtl_expr)
1306 tree rtl_expr;
1308 tree result;
1310 /* If the last thing in the statement-expression was not an
1311 expression-statement, then it has type `void'. */
1312 if (!last_expr_type)
1313 last_expr_type = void_type_node;
1314 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1315 TREE_SIDE_EFFECTS (result) = 1;
1317 /* Remove the compound statement from the tree structure; it is
1318 now saved in the STMT_EXPR. */
1319 last_tree = rtl_expr;
1320 TREE_CHAIN (last_tree) = NULL_TREE;
1322 /* If we created a statement-tree for this statement-expression,
1323 remove it now. */
1324 if (! cfun
1325 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1326 finish_stmt_tree (&scope_chain->x_saved_tree);
1328 return result;
1331 /* Generate an expression for `FN (ARGS)'.
1333 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1334 as a virtual call, even if FN is virtual. (This flag is set when
1335 encountering an expression where the function name is explicitly
1336 qualified. For example a call to `X::f' never generates a virtual
1337 call.)
1339 Returns code for the call. */
1341 tree
1342 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1344 if (fn == error_mark_node || args == error_mark_node)
1345 return error_mark_node;
1347 if (processing_template_decl)
1348 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1350 /* ARGS should be a list of arguments. */
1351 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1352 20020712);
1354 /* A reference to a member function will appear as an overloaded
1355 function (rather than a BASELINK) if an unqualified name was used
1356 to refer to it. */
1357 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1359 tree f;
1361 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1362 f = get_first_fn (TREE_OPERAND (fn, 0));
1363 else
1364 f = get_first_fn (fn);
1365 if (DECL_FUNCTION_MEMBER_P (f))
1367 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1368 fn = build_baselink (TYPE_BINFO (type),
1369 TYPE_BINFO (type),
1370 fn, /*optype=*/NULL_TREE);
1374 if (BASELINK_P (fn))
1376 tree object;
1378 /* A call to a member function. From [over.call.func]:
1380 If the keyword this is in scope and refers to the class of
1381 that member function, or a derived class thereof, then the
1382 function call is transformed into a qualified function call
1383 using (*this) as the postfix-expression to the left of the
1384 . operator.... [Otherwise] a contrived object of type T
1385 becomes the implied object argument.
1387 This paragraph is unclear about this situation:
1389 struct A { void f(); };
1390 struct B : public A {};
1391 struct C : public A { void g() { B::f(); }};
1393 In particular, for `B::f', this paragraph does not make clear
1394 whether "the class of that member function" refers to `A' or
1395 to `B'. We believe it refers to `B'. */
1396 if (current_class_type
1397 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1398 current_class_type)
1399 && current_class_ref)
1400 object = current_class_ref;
1401 else
1403 tree representative_fn;
1405 representative_fn = BASELINK_FUNCTIONS (fn);
1406 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1407 representative_fn = TREE_OPERAND (representative_fn, 0);
1408 representative_fn = get_first_fn (representative_fn);
1409 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1412 return build_new_method_call (object, fn, args, NULL_TREE,
1413 (disallow_virtual
1414 ? LOOKUP_NONVIRTUAL : 0));
1416 else if (is_overloaded_fn (fn))
1417 /* A call to a namespace-scope function. */
1418 return build_new_function_call (fn, args);
1419 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1421 tree result;
1423 if (args)
1424 error ("arguments to destructor are not allowed");
1425 /* Mark the pseudo-destructor call as having side-effects so
1426 that we do not issue warnings about its use. */
1427 result = build1 (NOP_EXPR,
1428 void_type_node,
1429 TREE_OPERAND (fn, 0));
1430 TREE_SIDE_EFFECTS (result) = 1;
1431 return result;
1433 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1435 /* If the "function" is really an object of class type, it might
1436 have an overloaded `operator ()'. */
1437 tree result;
1438 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1439 if (result)
1440 return result;
1443 /* A call where the function is unknown. */
1444 return build_function_call (fn, args);
1447 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1448 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1449 POSTDECREMENT_EXPR.) */
1451 tree
1452 finish_increment_expr (expr, code)
1453 tree expr;
1454 enum tree_code code;
1456 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1457 a COMPONENT_REF). This way if we've got, say, a reference to a
1458 static member that's being operated on, we don't end up trying to
1459 find a member operator for the class it's in. */
1461 if (TREE_CODE (expr) == OFFSET_REF)
1462 expr = resolve_offset_ref (expr);
1463 return build_x_unary_op (code, expr);
1466 /* Finish a use of `this'. Returns an expression for `this'. */
1468 tree
1469 finish_this_expr ()
1471 tree result;
1473 if (current_class_ptr)
1475 result = current_class_ptr;
1477 else if (current_function_decl
1478 && DECL_STATIC_FUNCTION_P (current_function_decl))
1480 error ("`this' is unavailable for static member functions");
1481 result = error_mark_node;
1483 else
1485 if (current_function_decl)
1486 error ("invalid use of `this' in non-member function");
1487 else
1488 error ("invalid use of `this' at top level");
1489 result = error_mark_node;
1492 return result;
1495 /* Finish a member function call using OBJECT and ARGS as arguments to
1496 FN. Returns an expression for the call. */
1498 tree
1499 finish_object_call_expr (fn, object, args)
1500 tree fn;
1501 tree object;
1502 tree args;
1504 if (DECL_DECLARES_TYPE_P (fn))
1506 if (processing_template_decl)
1507 /* This can happen on code like:
1509 class X;
1510 template <class T> void f(T t) {
1511 t.X();
1514 We just grab the underlying IDENTIFIER. */
1515 fn = DECL_NAME (fn);
1516 else
1518 error ("calling type `%T' like a method", fn);
1519 return error_mark_node;
1523 if (processing_template_decl)
1524 return build_nt (CALL_EXPR,
1525 build_nt (COMPONENT_REF, object, fn),
1526 args);
1528 if (name_p (fn))
1529 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1530 else
1531 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1534 /* Finish a qualified member function call using OBJECT and ARGS as
1535 arguments to FN. Returns an expression for the call. */
1537 tree
1538 finish_qualified_object_call_expr (fn, object, args)
1539 tree fn;
1540 tree object;
1541 tree args;
1543 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1544 TREE_OPERAND (fn, 1), args);
1547 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1548 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1549 the TYPE for the type given. If SCOPE is non-NULL, the expression
1550 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1552 tree
1553 finish_pseudo_destructor_expr (object, scope, destructor)
1554 tree object;
1555 tree scope;
1556 tree destructor;
1558 if (destructor == error_mark_node)
1559 return error_mark_node;
1561 my_friendly_assert (TYPE_P (destructor), 20010905);
1563 if (!processing_template_decl)
1565 if (scope == error_mark_node)
1567 error ("invalid qualifying scope in pseudo-destructor name");
1568 return error_mark_node;
1571 if (!same_type_p (TREE_TYPE (object), destructor))
1573 error ("`%E' is not of type `%T'", object, destructor);
1574 return error_mark_node;
1578 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1581 /* Finish an expression of the form CODE EXPR. */
1583 tree
1584 finish_unary_op_expr (code, expr)
1585 enum tree_code code;
1586 tree expr;
1588 tree result = build_x_unary_op (code, expr);
1589 /* Inside a template, build_x_unary_op does not fold the
1590 expression. So check whether the result is folded before
1591 setting TREE_NEGATED_INT. */
1592 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1593 && TREE_CODE (result) == INTEGER_CST
1594 && !TREE_UNSIGNED (TREE_TYPE (result))
1595 && INT_CST_LT (result, integer_zero_node))
1596 TREE_NEGATED_INT (result) = 1;
1597 overflow_warning (result);
1598 return result;
1601 /* Finish a compound-literal expression. TYPE is the type to which
1602 the INITIALIZER_LIST is being cast. */
1604 tree
1605 finish_compound_literal (type, initializer_list)
1606 tree type;
1607 tree initializer_list;
1609 tree compound_literal;
1611 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1612 compound_literal = build_nt (CONSTRUCTOR, NULL_TREE,
1613 initializer_list);
1614 /* Mark it as a compound-literal. */
1615 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1616 if (processing_template_decl)
1617 TREE_TYPE (compound_literal) = type;
1618 else
1620 /* Check the initialization. */
1621 compound_literal = digest_init (type, compound_literal, NULL);
1622 /* If the TYPE was an array type with an unknown bound, then we can
1623 figure out the dimension now. For example, something like:
1625 `(int []) { 2, 3 }'
1627 implies that the array has two elements. */
1628 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1629 complete_array_type (type, compound_literal, 1);
1632 return compound_literal;
1635 /* Return the declaration for the function-name variable indicated by
1636 ID. */
1638 tree
1639 finish_fname (tree id)
1641 tree decl;
1643 decl = fname_decl (C_RID_CODE (id), id);
1644 if (processing_template_decl)
1645 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1646 return decl;
1649 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1650 and DECLARATOR. Returns nonzero if the function-declaration is
1651 valid. */
1654 begin_function_definition (decl_specs, attributes, declarator)
1655 tree decl_specs;
1656 tree attributes;
1657 tree declarator;
1659 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1660 return 0;
1662 /* The things we're about to see are not directly qualified by any
1663 template headers we've seen thus far. */
1664 reset_specialization ();
1666 return 1;
1669 /* Finish an init-declarator. Returns a DECL. */
1671 tree
1672 finish_declarator (declarator, declspecs, attributes,
1673 prefix_attributes, initialized)
1674 tree declarator;
1675 tree declspecs;
1676 tree attributes;
1677 tree prefix_attributes;
1678 int initialized;
1680 return start_decl (declarator, declspecs, initialized, attributes,
1681 prefix_attributes);
1684 /* Finish a translation unit. */
1686 void
1687 finish_translation_unit ()
1689 /* In case there were missing closebraces,
1690 get us back to the global binding level. */
1691 pop_everything ();
1692 while (current_namespace != global_namespace)
1693 pop_namespace ();
1695 /* Do file scope __FUNCTION__ et al. */
1696 finish_fname_decls ();
1699 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1700 Returns the parameter. */
1702 tree
1703 finish_template_type_parm (aggr, identifier)
1704 tree aggr;
1705 tree identifier;
1707 if (aggr != class_type_node)
1709 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1710 aggr = class_type_node;
1713 return build_tree_list (aggr, identifier);
1716 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1717 Returns the parameter. */
1719 tree
1720 finish_template_template_parm (aggr, identifier)
1721 tree aggr;
1722 tree identifier;
1724 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1725 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1726 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1727 DECL_TEMPLATE_RESULT (tmpl) = decl;
1728 DECL_ARTIFICIAL (decl) = 1;
1729 end_template_decl ();
1731 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1733 return finish_template_type_parm (aggr, tmpl);
1736 /* ARGUMENT is the default-argument value for a template template
1737 parameter. If ARGUMENT is invalid, issue error messages and return
1738 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1740 tree
1741 check_template_template_default_arg (tree argument)
1743 if (TREE_CODE (argument) != TEMPLATE_DECL
1744 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1745 && TREE_CODE (argument) != TYPE_DECL
1746 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1748 error ("invalid default template argument");
1749 return error_mark_node;
1752 return argument;
1755 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1756 nonzero, the parameter list was terminated by a `...'. */
1758 tree
1759 finish_parmlist (parms, ellipsis)
1760 tree parms;
1761 int ellipsis;
1763 if (parms)
1765 /* We mark the PARMS as a parmlist so that declarator processing can
1766 disambiguate certain constructs. */
1767 TREE_PARMLIST (parms) = 1;
1768 /* We do not append void_list_node here, but leave it to grokparms
1769 to do that. */
1770 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1772 return parms;
1775 /* Begin a class definition, as indicated by T. */
1777 tree
1778 begin_class_definition (t)
1779 tree t;
1781 if (t == error_mark_node)
1782 return error_mark_node;
1784 if (processing_template_parmlist)
1786 error ("definition of `%#T' inside template parameter list", t);
1787 return error_mark_node;
1789 /* A non-implicit typename comes from code like:
1791 template <typename T> struct A {
1792 template <typename U> struct A<T>::B ...
1794 This is erroneous. */
1795 else if (TREE_CODE (t) == TYPENAME_TYPE)
1797 error ("invalid definition of qualified type `%T'", t);
1798 t = error_mark_node;
1801 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1803 t = make_aggr_type (RECORD_TYPE);
1804 pushtag (make_anon_name (), t, 0);
1807 /* If this type was already complete, and we see another definition,
1808 that's an error. */
1809 if (COMPLETE_TYPE_P (t))
1810 duplicate_tag_error (t);
1812 /* Update the location of the decl. */
1813 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1814 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1816 if (TYPE_BEING_DEFINED (t))
1818 t = make_aggr_type (TREE_CODE (t));
1819 pushtag (TYPE_IDENTIFIER (t), t, 0);
1821 maybe_process_partial_specialization (t);
1822 pushclass (t, true);
1823 TYPE_BEING_DEFINED (t) = 1;
1824 TYPE_PACKED (t) = flag_pack_struct;
1825 /* Reset the interface data, at the earliest possible
1826 moment, as it might have been set via a class foo;
1827 before. */
1828 if (! TYPE_ANONYMOUS_P (t))
1830 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1831 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1832 (t, interface_unknown);
1834 reset_specialization();
1836 /* Make a declaration for this class in its own scope. */
1837 build_self_reference ();
1839 return t;
1842 /* Finish the member declaration given by DECL. */
1844 void
1845 finish_member_declaration (decl)
1846 tree decl;
1848 if (decl == error_mark_node || decl == NULL_TREE)
1849 return;
1851 if (decl == void_type_node)
1852 /* The COMPONENT was a friend, not a member, and so there's
1853 nothing for us to do. */
1854 return;
1856 /* We should see only one DECL at a time. */
1857 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1859 /* Set up access control for DECL. */
1860 TREE_PRIVATE (decl)
1861 = (current_access_specifier == access_private_node);
1862 TREE_PROTECTED (decl)
1863 = (current_access_specifier == access_protected_node);
1864 if (TREE_CODE (decl) == TEMPLATE_DECL)
1866 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1867 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1870 /* Mark the DECL as a member of the current class. */
1871 DECL_CONTEXT (decl) = current_class_type;
1873 /* [dcl.link]
1875 A C language linkage is ignored for the names of class members
1876 and the member function type of class member functions. */
1877 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1878 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1880 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1882 /* Put functions on the TYPE_METHODS list and everything else on the
1883 TYPE_FIELDS list. Note that these are built up in reverse order.
1884 We reverse them (to obtain declaration order) in finish_struct. */
1885 if (TREE_CODE (decl) == FUNCTION_DECL
1886 || DECL_FUNCTION_TEMPLATE_P (decl))
1888 /* We also need to add this function to the
1889 CLASSTYPE_METHOD_VEC. */
1890 add_method (current_class_type, decl, /*error_p=*/0);
1892 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1893 TYPE_METHODS (current_class_type) = decl;
1895 else
1897 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1898 go at the beginning. The reason is that lookup_field_1
1899 searches the list in order, and we want a field name to
1900 override a type name so that the "struct stat hack" will
1901 work. In particular:
1903 struct S { enum E { }; int E } s;
1904 s.E = 3;
1906 is valid. In addition, the FIELD_DECLs must be maintained in
1907 declaration order so that class layout works as expected.
1908 However, we don't need that order until class layout, so we
1909 save a little time by putting FIELD_DECLs on in reverse order
1910 here, and then reversing them in finish_struct_1. (We could
1911 also keep a pointer to the correct insertion points in the
1912 list.) */
1914 if (TREE_CODE (decl) == TYPE_DECL)
1915 TYPE_FIELDS (current_class_type)
1916 = chainon (TYPE_FIELDS (current_class_type), decl);
1917 else
1919 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1920 TYPE_FIELDS (current_class_type) = decl;
1923 /* Enter the DECL into the scope of the class. */
1924 if (TREE_CODE (decl) != USING_DECL)
1925 pushdecl_class_level (decl);
1929 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1930 the definition is immediately followed by a semicolon. Returns the
1931 type. */
1933 tree
1934 finish_class_definition (t, attributes, semi, pop_scope_p)
1935 tree t;
1936 tree attributes;
1937 int semi;
1938 int pop_scope_p;
1940 if (t == error_mark_node)
1941 return error_mark_node;
1943 /* finish_struct nukes this anyway; if finish_exception does too,
1944 then it can go. */
1945 if (semi)
1946 note_got_semicolon (t);
1948 /* If we got any attributes in class_head, xref_tag will stick them in
1949 TREE_TYPE of the type. Grab them now. */
1950 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1951 TYPE_ATTRIBUTES (t) = NULL_TREE;
1953 if (TREE_CODE (t) == ENUMERAL_TYPE)
1955 else
1957 t = finish_struct (t, attributes);
1958 if (semi)
1959 note_got_semicolon (t);
1962 if (pop_scope_p)
1963 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1965 return t;
1968 /* Finish processing the declaration of a member class template
1969 TYPES whose template parameters are given by PARMS. */
1971 tree
1972 finish_member_class_template (types)
1973 tree types;
1975 tree t;
1977 /* If there are declared, but undefined, partial specializations
1978 mixed in with the typespecs they will not yet have passed through
1979 maybe_process_partial_specialization, so we do that here. */
1980 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1981 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1982 maybe_process_partial_specialization (TREE_VALUE (t));
1984 note_list_got_semicolon (types);
1985 grok_x_components (types);
1986 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1987 /* The component was in fact a friend declaration. We avoid
1988 finish_member_template_decl performing certain checks by
1989 unsetting TYPES. */
1990 types = NULL_TREE;
1992 finish_member_template_decl (types);
1994 /* As with other component type declarations, we do
1995 not store the new DECL on the list of
1996 component_decls. */
1997 return NULL_TREE;
2000 /* Finish processing a complete template declaration. The PARMS are
2001 the template parameters. */
2003 void
2004 finish_template_decl (parms)
2005 tree parms;
2007 if (parms)
2008 end_template_decl ();
2009 else
2010 end_specialization ();
2013 /* Finish processing a template-id (which names a type) of the form
2014 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2015 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2016 the scope of template-id indicated. */
2018 tree
2019 finish_template_type (name, args, entering_scope)
2020 tree name;
2021 tree args;
2022 int entering_scope;
2024 tree decl;
2026 decl = lookup_template_class (name, args,
2027 NULL_TREE, NULL_TREE,
2028 entering_scope, /*complain=*/1);
2029 if (decl != error_mark_node)
2030 decl = TYPE_STUB_DECL (decl);
2032 return decl;
2035 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2036 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2037 BASE_CLASS, or NULL_TREE if an error occurred. The
2038 ACCESS_SPECIFIER is one of
2039 access_{default,public,protected_private}[_virtual]_node.*/
2041 tree
2042 finish_base_specifier (tree base, tree access, bool virtual_p)
2044 tree result;
2046 if (base == error_mark_node)
2048 error ("invalid base-class specification");
2049 result = NULL_TREE;
2051 else if (! is_aggr_type (base, 1))
2052 result = NULL_TREE;
2053 else
2055 if (cp_type_quals (base) != 0)
2057 error ("base class `%T' has cv qualifiers", base);
2058 base = TYPE_MAIN_VARIANT (base);
2060 result = build_tree_list (access, base);
2061 TREE_VIA_VIRTUAL (result) = virtual_p;
2064 return result;
2067 /* Called when multiple declarators are processed. If that is not
2068 premitted in this context, an error is issued. */
2070 void
2071 check_multiple_declarators ()
2073 /* [temp]
2075 In a template-declaration, explicit specialization, or explicit
2076 instantiation the init-declarator-list in the declaration shall
2077 contain at most one declarator.
2079 We don't just use PROCESSING_TEMPLATE_DECL for the first
2080 condition since that would disallow the perfectly valid code,
2081 like `template <class T> struct S { int i, j; };'. */
2082 if (at_function_scope_p ())
2083 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2084 return;
2086 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2087 || processing_explicit_instantiation
2088 || processing_specialization)
2089 error ("multiple declarators in template declaration");
2092 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2093 use as a type-specifier. */
2095 tree
2096 finish_typeof (expr)
2097 tree expr;
2099 tree type;
2101 if (processing_template_decl)
2103 type = make_aggr_type (TYPEOF_TYPE);
2104 TYPE_FIELDS (type) = expr;
2106 return type;
2109 if (TREE_CODE (expr) == OFFSET_REF)
2110 expr = resolve_offset_ref (expr);
2112 type = TREE_TYPE (expr);
2114 if (!type || type == unknown_type_node)
2116 error ("type of `%E' is unknown", expr);
2117 return error_mark_node;
2120 return type;
2123 /* Compute the value of the `sizeof' operator. */
2125 tree
2126 finish_sizeof (t)
2127 tree t;
2129 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2132 /* Implement the __alignof keyword: Return the minimum required
2133 alignment of T, measured in bytes. */
2135 tree
2136 finish_alignof (t)
2137 tree t;
2139 if (processing_template_decl)
2140 return build_min (ALIGNOF_EXPR, size_type_node, t);
2142 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2145 /* Generate RTL for the statement T, and its substatements, and any
2146 other statements at its nesting level. */
2148 static void
2149 cp_expand_stmt (t)
2150 tree t;
2152 switch (TREE_CODE (t))
2154 case TRY_BLOCK:
2155 genrtl_try_block (t);
2156 break;
2158 case EH_SPEC_BLOCK:
2159 genrtl_eh_spec_block (t);
2160 break;
2162 case HANDLER:
2163 genrtl_handler (t);
2164 break;
2166 case USING_STMT:
2167 break;
2169 default:
2170 abort ();
2171 break;
2175 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2176 will equivalent CALL_EXPRs. */
2178 static tree
2179 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2180 tree *tp;
2181 int *walk_subtrees ATTRIBUTE_UNUSED;
2182 void *data ATTRIBUTE_UNUSED;
2184 tree aggr_init_expr;
2185 tree call_expr;
2186 tree fn;
2187 tree args;
2188 tree slot;
2189 tree type;
2190 enum style_t { ctor, arg, pcc } style;
2192 aggr_init_expr = *tp;
2193 /* We don't need to walk into types; there's nothing in a type that
2194 needs simplification. (And, furthermore, there are places we
2195 actively don't want to go. For example, we don't want to wander
2196 into the default arguments for a FUNCTION_DECL that appears in a
2197 CALL_EXPR.) */
2198 if (TYPE_P (aggr_init_expr))
2200 *walk_subtrees = 0;
2201 return NULL_TREE;
2203 /* Only AGGR_INIT_EXPRs are interesting. */
2204 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2205 return NULL_TREE;
2207 /* Form an appropriate CALL_EXPR. */
2208 fn = TREE_OPERAND (aggr_init_expr, 0);
2209 args = TREE_OPERAND (aggr_init_expr, 1);
2210 slot = TREE_OPERAND (aggr_init_expr, 2);
2211 type = TREE_TYPE (aggr_init_expr);
2213 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2214 style = ctor;
2215 #ifdef PCC_STATIC_STRUCT_RETURN
2216 else if (1)
2217 style = pcc;
2218 #endif
2219 else if (TREE_ADDRESSABLE (type))
2220 style = arg;
2221 else
2222 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2223 handling. See build_cplus_new. */
2224 abort ();
2226 if (style == ctor || style == arg)
2228 /* Pass the address of the slot. If this is a constructor, we
2229 replace the first argument; otherwise, we tack on a new one. */
2230 if (style == ctor)
2231 args = TREE_CHAIN (args);
2233 cxx_mark_addressable (slot);
2234 args = tree_cons (NULL_TREE,
2235 build1 (ADDR_EXPR,
2236 build_pointer_type (TREE_TYPE (slot)),
2237 slot),
2238 args);
2241 call_expr = build (CALL_EXPR,
2242 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2243 fn, args, NULL_TREE);
2244 TREE_SIDE_EFFECTS (call_expr) = 1;
2246 if (style == arg)
2247 /* Tell the backend that we've added our return slot to the argument
2248 list. */
2249 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2250 else if (style == pcc)
2252 /* If we're using the non-reentrant PCC calling convention, then we
2253 need to copy the returned value out of the static buffer into the
2254 SLOT. */
2255 int old_ac = flag_access_control;
2257 flag_access_control = 0;
2258 call_expr = build_aggr_init (slot, call_expr,
2259 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2260 flag_access_control = old_ac;
2263 /* We want to use the value of the initialized location as the
2264 result. */
2265 call_expr = build (COMPOUND_EXPR, type,
2266 call_expr, slot);
2268 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2269 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2270 *tp = call_expr;
2272 /* Keep iterating. */
2273 return NULL_TREE;
2276 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2278 static void
2279 emit_associated_thunks (fn)
2280 tree fn;
2282 /* When we use vcall offsets, we emit thunks with the virtual
2283 functions to which they thunk. The whole point of vcall offsets
2284 is so that you can know statically the entire set of thunks that
2285 will ever be needed for a given virtual function, thereby
2286 enabling you to output all the thunks with the function itself. */
2287 if (DECL_VIRTUAL_P (fn))
2289 tree thunk;
2291 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2293 use_thunk (thunk, /*emit_p=*/1);
2294 if (DECL_RESULT_THUNK_P (thunk))
2296 tree probe;
2298 for (probe = DECL_THUNKS (thunk);
2299 probe; probe = TREE_CHAIN (probe))
2300 use_thunk (probe, /*emit_p=*/1);
2306 /* Generate RTL for FN. */
2308 void
2309 expand_body (fn)
2310 tree fn;
2312 int saved_lineno;
2313 const char *saved_input_filename;
2314 tree saved_function;
2316 /* When the parser calls us after finishing the body of a template
2317 function, we don't really want to expand the body. When we're
2318 processing an in-class definition of an inline function,
2319 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2320 to look at the function itself. */
2321 if (processing_template_decl
2322 || (DECL_LANG_SPECIFIC (fn)
2323 && DECL_TEMPLATE_INFO (fn)
2324 && uses_template_parms (DECL_TI_ARGS (fn))))
2326 /* Normally, collection only occurs in rest_of_compilation. So,
2327 if we don't collect here, we never collect junk generated
2328 during the processing of templates until we hit a
2329 non-template function. */
2330 ggc_collect ();
2331 return;
2334 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2335 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2336 simplify_aggr_init_exprs_r,
2337 NULL);
2339 /* If this is a constructor or destructor body, we have to clone
2340 it. */
2341 if (maybe_clone_body (fn))
2343 /* We don't want to process FN again, so pretend we've written
2344 it out, even though we haven't. */
2345 TREE_ASM_WRITTEN (fn) = 1;
2346 return;
2349 /* There's no reason to do any of the work here if we're only doing
2350 semantic analysis; this code just generates RTL. */
2351 if (flag_syntax_only)
2352 return;
2354 /* If possible, avoid generating RTL for this function. Instead,
2355 just record it as an inline function, and wait until end-of-file
2356 to decide whether to write it out or not. */
2357 if (/* We have to generate RTL if it's not an inline function. */
2358 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2359 /* Or if we have to emit code for inline functions anyhow. */
2360 && !flag_keep_inline_functions
2361 /* Or if we actually have a reference to the function. */
2362 && !DECL_NEEDED_P (fn))
2364 /* Set DECL_EXTERNAL so that assemble_external will be called as
2365 necessary. We'll clear it again in finish_file. */
2366 if (!DECL_EXTERNAL (fn))
2368 DECL_NOT_REALLY_EXTERN (fn) = 1;
2369 DECL_EXTERNAL (fn) = 1;
2371 /* Remember this function. In finish_file we'll decide if
2372 we actually need to write this function out. */
2373 defer_fn (fn);
2374 /* Let the back-end know that this function exists. */
2375 (*debug_hooks->deferred_inline_function) (fn);
2376 return;
2379 /* Compute the appropriate object-file linkage for inline
2380 functions. */
2381 if (DECL_DECLARED_INLINE_P (fn))
2382 import_export_decl (fn);
2384 /* If FN is external, then there's no point in generating RTL for
2385 it. This situation can arise with an inline function under
2386 `-fexternal-templates'; we instantiate the function, even though
2387 we're not planning on emitting it, in case we get a chance to
2388 inline it. */
2389 if (DECL_EXTERNAL (fn))
2390 return;
2392 /* Save the current file name and line number. When we expand the
2393 body of the function, we'll set LINENO and INPUT_FILENAME so that
2394 error-mesages come out in the right places. */
2395 saved_lineno = lineno;
2396 saved_input_filename = input_filename;
2397 saved_function = current_function_decl;
2398 lineno = DECL_SOURCE_LINE (fn);
2399 input_filename = DECL_SOURCE_FILE (fn);
2400 current_function_decl = fn;
2402 timevar_push (TV_INTEGRATION);
2404 /* Optimize the body of the function before expanding it. */
2405 optimize_function (fn);
2407 timevar_pop (TV_INTEGRATION);
2408 timevar_push (TV_EXPAND);
2410 genrtl_start_function (fn);
2411 current_function_is_thunk = DECL_THUNK_P (fn);
2413 /* Expand the body. */
2414 expand_stmt (DECL_SAVED_TREE (fn));
2416 /* Statements should always be full-expressions at the outermost set
2417 of curly braces for a function. */
2418 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2420 /* The outermost statement for a function contains the line number
2421 recorded when we finished processing the function. */
2422 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2424 /* Generate code for the function. */
2425 genrtl_finish_function (fn);
2427 /* If possible, obliterate the body of the function so that it can
2428 be garbage collected. */
2429 if (dump_enabled_p (TDI_all))
2430 /* Keep the body; we're going to dump it. */
2432 else if (DECL_INLINE (fn) && flag_inline_trees)
2433 /* We might need the body of this function so that we can expand
2434 it inline somewhere else. */
2436 else
2437 /* We don't need the body; blow it away. */
2438 DECL_SAVED_TREE (fn) = NULL_TREE;
2440 /* And restore the current source position. */
2441 current_function_decl = saved_function;
2442 lineno = saved_lineno;
2443 input_filename = saved_input_filename;
2444 extract_interface_info ();
2446 timevar_pop (TV_EXPAND);
2448 /* Emit any thunks that should be emitted at the same time as FN. */
2449 emit_associated_thunks (fn);
2452 /* Helper function for walk_tree, used by finish_function to override all
2453 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2454 value optimization. */
2456 tree
2457 nullify_returns_r (tp, walk_subtrees, data)
2458 tree *tp;
2459 int *walk_subtrees;
2460 void *data;
2462 tree nrv = (tree) data;
2464 /* No need to walk into types. There wouldn't be any need to walk into
2465 non-statements, except that we have to consider STMT_EXPRs. */
2466 if (TYPE_P (*tp))
2467 *walk_subtrees = 0;
2468 else if (TREE_CODE (*tp) == RETURN_STMT)
2469 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2470 else if (TREE_CODE (*tp) == CLEANUP_STMT
2471 && CLEANUP_DECL (*tp) == nrv)
2472 CLEANUP_EH_ONLY (*tp) = 1;
2474 /* Keep iterating. */
2475 return NULL_TREE;
2478 /* Start generating the RTL for FN. */
2480 static void
2481 genrtl_start_function (fn)
2482 tree fn;
2484 /* Tell everybody what function we're processing. */
2485 current_function_decl = fn;
2486 /* Get the RTL machinery going for this function. */
2487 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2488 /* Let everybody know that we're expanding this function, not doing
2489 semantic analysis. */
2490 expanding_p = 1;
2492 /* Even though we're inside a function body, we still don't want to
2493 call expand_expr to calculate the size of a variable-sized array.
2494 We haven't necessarily assigned RTL to all variables yet, so it's
2495 not safe to try to expand expressions involving them. */
2496 immediate_size_expand = 0;
2497 cfun->x_dont_save_pending_sizes_p = 1;
2499 /* Let the user know we're compiling this function. */
2500 announce_function (fn);
2502 /* Initialize the per-function data. */
2503 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2504 if (DECL_SAVED_FUNCTION_DATA (fn))
2506 /* If we already parsed this function, and we're just expanding it
2507 now, restore saved state. */
2508 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2510 /* This function is being processed in whole-function mode; we
2511 already did semantic analysis. */
2512 cfun->x_whole_function_mode_p = 1;
2514 /* If we decided that we didn't want to inline this function,
2515 make sure the back-end knows that. */
2516 if (!current_function_cannot_inline)
2517 current_function_cannot_inline = cp_function_chain->cannot_inline;
2519 /* We don't need the saved data anymore. Unless this is an inline
2520 function; we need the named return value info for
2521 cp_copy_res_decl_for_inlining. */
2522 if (! DECL_INLINE (fn))
2523 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2526 /* Keep track of how many functions we're presently expanding. */
2527 ++function_depth;
2529 /* Create a binding level for the parameters. */
2530 expand_function_start (fn, /*parms_have_cleanups=*/0);
2531 /* If this function is `main'. */
2532 if (DECL_MAIN_P (fn))
2533 expand_main_function ();
2535 /* Give our named return value the same RTL as our RESULT_DECL. */
2536 if (current_function_return_value)
2537 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2540 /* Finish generating the RTL for FN. */
2542 static void
2543 genrtl_finish_function (fn)
2544 tree fn;
2546 tree t;
2548 #if 0
2549 if (write_symbols != NO_DEBUG)
2551 /* Keep this code around in case we later want to control debug info
2552 based on whether a type is "used". (jason 1999-11-11) */
2554 tree ttype = target_type (fntype);
2555 tree parmdecl;
2557 if (IS_AGGR_TYPE (ttype))
2558 /* Let debugger know it should output info for this type. */
2559 note_debug_info_needed (ttype);
2561 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2563 ttype = target_type (TREE_TYPE (parmdecl));
2564 if (IS_AGGR_TYPE (ttype))
2565 /* Let debugger know it should output info for this type. */
2566 note_debug_info_needed (ttype);
2569 #endif
2571 /* Clean house because we will need to reorder insns here. */
2572 do_pending_stack_adjust ();
2574 /* If we have a named return value, we need to force a return so that
2575 the return register is USEd. */
2576 if (DECL_NAME (DECL_RESULT (fn)))
2577 emit_jump (return_label);
2579 /* We hard-wired immediate_size_expand to zero in start_function.
2580 Expand_function_end will decrement this variable. So, we set the
2581 variable to one here, so that after the decrement it will remain
2582 zero. */
2583 immediate_size_expand = 1;
2585 /* Generate rtl for function exit. */
2586 expand_function_end (input_filename, lineno, 0);
2588 /* If this is a nested function (like a template instantiation that
2589 we're compiling in the midst of compiling something else), push a
2590 new GC context. That will keep local variables on the stack from
2591 being collected while we're doing the compilation of this
2592 function. */
2593 if (function_depth > 1)
2594 ggc_push_context ();
2596 /* There's no need to defer outputting this function any more; we
2597 know we want to output it. */
2598 DECL_DEFER_OUTPUT (fn) = 0;
2600 /* Run the optimizers and output the assembler code for this
2601 function. */
2602 rest_of_compilation (fn);
2604 /* Undo the call to ggc_push_context above. */
2605 if (function_depth > 1)
2606 ggc_pop_context ();
2608 #if 0
2609 /* Keep this code around in case we later want to control debug info
2610 based on whether a type is "used". (jason 1999-11-11) */
2612 if (ctype && TREE_ASM_WRITTEN (fn))
2613 note_debug_info_needed (ctype);
2614 #endif
2616 /* If this function is marked with the constructor attribute, add it
2617 to the list of functions to be called along with constructors
2618 from static duration objects. */
2619 if (DECL_STATIC_CONSTRUCTOR (fn))
2620 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2622 /* If this function is marked with the destructor attribute, add it
2623 to the list of functions to be called along with destructors from
2624 static duration objects. */
2625 if (DECL_STATIC_DESTRUCTOR (fn))
2626 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2628 --function_depth;
2630 /* In C++, we should never be saving RTL for the function. */
2631 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2633 /* Since we don't need the RTL for this function anymore, stop
2634 pointing to it. That's especially important for LABEL_DECLs,
2635 since you can reach all the instructions in the function from the
2636 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2637 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2638 local variables. */
2639 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2640 clear_decl_rtl,
2641 NULL);
2643 /* Clear out the RTL for the arguments. */
2644 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2646 SET_DECL_RTL (t, NULL_RTX);
2647 DECL_INCOMING_RTL (t) = NULL_RTX;
2650 if (!(flag_inline_trees && DECL_INLINE (fn)))
2651 /* DECL_INITIAL must remain nonzero so we know this was an
2652 actual function definition. */
2653 DECL_INITIAL (fn) = error_mark_node;
2655 /* Let the error reporting routines know that we're outside a
2656 function. For a nested function, this value is used in
2657 pop_cp_function_context and then reset via pop_function_context. */
2658 current_function_decl = NULL_TREE;
2661 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2662 its sub-blocks. */
2664 static tree
2665 clear_decl_rtl (tp, walk_subtrees, data)
2666 tree *tp;
2667 int *walk_subtrees ATTRIBUTE_UNUSED;
2668 void *data ATTRIBUTE_UNUSED;
2670 if (nonstatic_local_decl_p (*tp))
2671 SET_DECL_RTL (*tp, NULL_RTX);
2673 return NULL_TREE;
2676 /* Perform initialization related to this module. */
2678 void
2679 init_cp_semantics ()
2681 lang_expand_stmt = cp_expand_stmt;
2684 #include "gt-cp-semantics.h"