* simplify-rtx.c (simplify_unary_operation,
[official-gcc.git] / gcc / cp / semantics.c
blob9c0b28b6b13fd54acc2d9e6017ee29359a1fbfb8
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, 2003 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 GCC.
12 GCC 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 GCC 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 GCC; 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 "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "tree-inline.h"
34 #include "except.h"
35 #include "lex.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "rtl.h"
39 #include "expr.h"
40 #include "output.h"
41 #include "timevar.h"
42 #include "debug.h"
44 /* There routines provide a modular interface to perform many parsing
45 operations. They may therefore be used during actual parsing, or
46 during template instantiation, which may be regarded as a
47 degenerate form of parsing. Since the current g++ parser is
48 lacking in several respects, and will be reimplemented, we are
49 attempting to move most code that is not directly related to
50 parsing into this file; that will make implementing the new parser
51 much easier since it will be able to make use of these routines. */
53 static tree maybe_convert_cond PARAMS ((tree));
54 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
55 static void emit_associated_thunks PARAMS ((tree));
56 static void genrtl_try_block PARAMS ((tree));
57 static void genrtl_eh_spec_block PARAMS ((tree));
58 static void genrtl_handler PARAMS ((tree));
59 static void cp_expand_stmt PARAMS ((tree));
60 static void genrtl_start_function PARAMS ((tree));
61 static void genrtl_finish_function PARAMS ((tree));
62 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
64 /* Finish processing the COND, the SUBSTMT condition for STMT. */
66 #define FINISH_COND(COND, STMT, SUBSTMT) \
67 do { \
68 if (last_tree != (STMT)) \
69 { \
70 RECHAIN_STMTS (STMT, SUBSTMT); \
71 if (!processing_template_decl) \
72 { \
73 (COND) = build_tree_list (SUBSTMT, COND); \
74 (SUBSTMT) = (COND); \
75 } \
76 } \
77 else \
78 (SUBSTMT) = (COND); \
79 } while (0)
81 /* Data for deferred access checking. */
82 static GTY(()) deferred_access *deferred_access_stack;
83 static GTY(()) deferred_access *deferred_access_free_list;
85 /* Save the current deferred access states and start deferred
86 access checking iff DEFER_P is true. */
88 void push_deferring_access_checks (bool deferring_p)
90 deferred_access *d;
92 /* Recycle previously used free store if available. */
93 if (deferred_access_free_list)
95 d = deferred_access_free_list;
96 deferred_access_free_list = d->next;
98 else
99 d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
101 d->next = deferred_access_stack;
102 d->deferred_access_checks = NULL_TREE;
103 d->deferring_access_checks_p = deferring_p;
104 deferred_access_stack = d;
107 /* Resume deferring access checks again after we stopped doing
108 this previously. */
110 void resume_deferring_access_checks (void)
112 deferred_access_stack->deferring_access_checks_p = true;
115 /* Stop deferring access checks. */
117 void stop_deferring_access_checks (void)
119 deferred_access_stack->deferring_access_checks_p = false;
122 /* Discard the current deferred access checks and restore the
123 previous states. */
125 void pop_deferring_access_checks (void)
127 deferred_access *d = deferred_access_stack;
128 deferred_access_stack = d->next;
130 /* Remove references to access checks TREE_LIST. */
131 d->deferred_access_checks = NULL_TREE;
133 /* Store in free list for later use. */
134 d->next = deferred_access_free_list;
135 deferred_access_free_list = d;
138 /* Returns a TREE_LIST representing the deferred checks.
139 The TREE_PURPOSE of each node is the type through which the
140 access occurred; the TREE_VALUE is the declaration named.
143 tree get_deferred_access_checks (void)
145 return deferred_access_stack->deferred_access_checks;
148 /* Take current deferred checks and combine with the
149 previous states if we also defer checks previously.
150 Otherwise perform checks now. */
152 void pop_to_parent_deferring_access_checks (void)
154 tree deferred_check = get_deferred_access_checks ();
155 deferred_access *d1 = deferred_access_stack;
156 deferred_access *d2 = deferred_access_stack->next;
157 deferred_access *d3 = deferred_access_stack->next->next;
159 /* Temporary swap the order of the top two states, just to make
160 sure the garbage collector will not reclaim the memory during
161 processing below. */
162 deferred_access_stack = d2;
163 d2->next = d1;
164 d1->next = d3;
166 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
167 /* Perform deferred check if required. */
168 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
169 TREE_VALUE (deferred_check));
171 deferred_access_stack = d1;
172 d1->next = d2;
173 d2->next = d3;
174 pop_deferring_access_checks ();
177 /* Perform the deferred access checks. */
179 void perform_deferred_access_checks (void)
181 tree deferred_check;
182 for (deferred_check = deferred_access_stack->deferred_access_checks;
183 deferred_check;
184 deferred_check = TREE_CHAIN (deferred_check))
185 /* Check access. */
186 enforce_access (TREE_PURPOSE (deferred_check),
187 TREE_VALUE (deferred_check));
189 /* No more deferred checks. */
190 deferred_access_stack->deferred_access_checks = NULL_TREE;
193 /* Defer checking the accessibility of DECL, when looked up in
194 CLASS_TYPE. */
196 void perform_or_defer_access_check (tree class_type, tree decl)
198 tree check;
200 /* If we are not supposed to defer access checks, just check now. */
201 if (!deferred_access_stack->deferring_access_checks_p)
203 enforce_access (class_type, decl);
204 return;
207 /* See if we are already going to perform this check. */
208 for (check = deferred_access_stack->deferred_access_checks;
209 check;
210 check = TREE_CHAIN (check))
211 if (TREE_VALUE (check) == decl
212 && same_type_p (TREE_PURPOSE (check), class_type))
213 return;
214 /* If not, record the check. */
215 deferred_access_stack->deferred_access_checks
216 = tree_cons (class_type, decl,
217 deferred_access_stack->deferred_access_checks);
220 /* Returns nonzero if the current statement is a full expression,
221 i.e. temporaries created during that statement should be destroyed
222 at the end of the statement. */
225 stmts_are_full_exprs_p ()
227 return current_stmt_tree ()->stmts_are_full_exprs_p;
230 /* Returns the stmt_tree (if any) to which statements are currently
231 being added. If there is no active statement-tree, NULL is
232 returned. */
234 stmt_tree
235 current_stmt_tree ()
237 return (cfun
238 ? &cfun->language->base.x_stmt_tree
239 : &scope_chain->x_stmt_tree);
242 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
243 flag for this because "A union for which objects or pointers are
244 declared is not an anonymous union" [class.union]. */
247 anon_aggr_type_p (node)
248 tree node;
250 return ANON_AGGR_TYPE_P (node);
253 /* Finish a scope. */
255 tree
256 do_poplevel ()
258 tree block = NULL_TREE;
260 if (stmts_are_full_exprs_p ())
262 tree scope_stmts = NULL_TREE;
264 block = poplevel (kept_level_p (), 1, 0);
265 if (!processing_template_decl)
267 /* This needs to come after the poplevel so that partial scopes
268 are properly nested. */
269 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
270 if (block)
272 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
273 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
278 return block;
281 /* Begin a new scope. */
283 void
284 do_pushlevel (scope_kind sk)
286 if (stmts_are_full_exprs_p ())
288 if (!processing_template_decl)
289 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
290 begin_scope (sk);
294 /* Finish a goto-statement. */
296 tree
297 finish_goto_stmt (destination)
298 tree destination;
300 if (TREE_CODE (destination) == IDENTIFIER_NODE)
301 destination = lookup_label (destination);
303 /* We warn about unused labels with -Wunused. That means we have to
304 mark the used labels as used. */
305 if (TREE_CODE (destination) == LABEL_DECL)
306 TREE_USED (destination) = 1;
308 if (TREE_CODE (destination) != LABEL_DECL)
309 /* We don't inline calls to functions with computed gotos.
310 Those functions are typically up to some funny business,
311 and may be depending on the labels being at particular
312 addresses, or some such. */
313 DECL_UNINLINABLE (current_function_decl) = 1;
315 check_goto (destination);
317 return add_stmt (build_stmt (GOTO_STMT, destination));
320 /* COND is the condition-expression for an if, while, etc.,
321 statement. Convert it to a boolean value, if appropriate. */
323 tree
324 maybe_convert_cond (cond)
325 tree cond;
327 /* Empty conditions remain empty. */
328 if (!cond)
329 return NULL_TREE;
331 /* Wait until we instantiate templates before doing conversion. */
332 if (processing_template_decl)
333 return cond;
335 /* Do the conversion. */
336 cond = convert_from_reference (cond);
337 return condition_conversion (cond);
340 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
342 tree
343 finish_expr_stmt (expr)
344 tree expr;
346 tree r = NULL_TREE;
347 tree expr_type = NULL_TREE;;
349 if (expr != NULL_TREE)
351 if (!processing_template_decl
352 && !(stmts_are_full_exprs_p ())
353 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
354 && lvalue_p (expr))
355 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
356 expr = default_conversion (expr);
358 /* Remember the type of the expression. */
359 expr_type = TREE_TYPE (expr);
361 if (stmts_are_full_exprs_p ())
362 expr = convert_to_void (expr, "statement");
364 r = add_stmt (build_stmt (EXPR_STMT, expr));
367 finish_stmt ();
369 /* This was an expression-statement, so we save the type of the
370 expression. */
371 last_expr_type = expr_type;
373 return r;
377 /* Begin an if-statement. Returns a newly created IF_STMT if
378 appropriate. */
380 tree
381 begin_if_stmt ()
383 tree r;
384 do_pushlevel (sk_block);
385 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
386 add_stmt (r);
387 return r;
390 /* Process the COND of an if-statement, which may be given by
391 IF_STMT. */
393 void
394 finish_if_stmt_cond (cond, if_stmt)
395 tree cond;
396 tree if_stmt;
398 cond = maybe_convert_cond (cond);
399 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
402 /* Finish the then-clause of an if-statement, which may be given by
403 IF_STMT. */
405 tree
406 finish_then_clause (if_stmt)
407 tree if_stmt;
409 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
410 return if_stmt;
413 /* Begin the else-clause of an if-statement. */
415 void
416 begin_else_clause ()
420 /* Finish the else-clause of an if-statement, which may be given by
421 IF_STMT. */
423 void
424 finish_else_clause (if_stmt)
425 tree if_stmt;
427 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
430 /* Finish an if-statement. */
432 void
433 finish_if_stmt ()
435 finish_stmt ();
436 do_poplevel ();
439 /* Begin a while-statement. Returns a newly created WHILE_STMT if
440 appropriate. */
442 tree
443 begin_while_stmt ()
445 tree r;
446 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
447 add_stmt (r);
448 do_pushlevel (sk_block);
449 return r;
452 /* Process the COND of a while-statement, which may be given by
453 WHILE_STMT. */
455 void
456 finish_while_stmt_cond (cond, while_stmt)
457 tree cond;
458 tree while_stmt;
460 cond = maybe_convert_cond (cond);
461 if (processing_template_decl)
462 /* Don't mess with condition decls in a template. */
463 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
464 else if (getdecls () == NULL_TREE)
465 /* It was a simple condition; install it. */
466 WHILE_COND (while_stmt) = cond;
467 else
469 /* If there was a declaration in the condition, we can't leave it
470 there; transform
471 while (A x = 42) { }
473 while (true) { A x = 42; if (!x) break; } */
474 tree if_stmt;
475 WHILE_COND (while_stmt) = boolean_true_node;
477 if_stmt = begin_if_stmt ();
478 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
479 finish_if_stmt_cond (cond, if_stmt);
480 finish_break_stmt ();
481 finish_then_clause (if_stmt);
482 finish_if_stmt ();
486 /* Finish a while-statement, which may be given by WHILE_STMT. */
488 void
489 finish_while_stmt (while_stmt)
490 tree while_stmt;
492 do_poplevel ();
493 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
494 finish_stmt ();
497 /* Begin a do-statement. Returns a newly created DO_STMT if
498 appropriate. */
500 tree
501 begin_do_stmt ()
503 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
504 add_stmt (r);
505 return r;
508 /* Finish the body of a do-statement, which may be given by DO_STMT. */
510 void
511 finish_do_body (do_stmt)
512 tree do_stmt;
514 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
517 /* Finish a do-statement, which may be given by DO_STMT, and whose
518 COND is as indicated. */
520 void
521 finish_do_stmt (cond, do_stmt)
522 tree cond;
523 tree do_stmt;
525 cond = maybe_convert_cond (cond);
526 DO_COND (do_stmt) = cond;
527 finish_stmt ();
530 /* Finish a return-statement. The EXPRESSION returned, if any, is as
531 indicated. */
533 tree
534 finish_return_stmt (expr)
535 tree expr;
537 tree r;
539 expr = check_return_expr (expr);
540 if (!processing_template_decl)
542 if (DECL_DESTRUCTOR_P (current_function_decl))
544 /* Similarly, all destructors must run destructors for
545 base-classes before returning. So, all returns in a
546 destructor get sent to the DTOR_LABEL; finish_function emits
547 code to return a value there. */
548 return finish_goto_stmt (dtor_label);
551 r = add_stmt (build_stmt (RETURN_STMT, expr));
552 finish_stmt ();
554 return r;
557 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
559 tree
560 begin_for_stmt ()
562 tree r;
564 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
565 NULL_TREE, NULL_TREE);
566 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
567 if (NEW_FOR_SCOPE_P (r))
568 do_pushlevel (sk_for);
569 add_stmt (r);
571 return r;
574 /* Finish the for-init-statement of a for-statement, which may be
575 given by FOR_STMT. */
577 void
578 finish_for_init_stmt (for_stmt)
579 tree for_stmt;
581 if (last_tree != for_stmt)
582 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
583 do_pushlevel (sk_block);
586 /* Finish the COND of a for-statement, which may be given by
587 FOR_STMT. */
589 void
590 finish_for_cond (cond, for_stmt)
591 tree cond;
592 tree for_stmt;
594 cond = maybe_convert_cond (cond);
595 if (processing_template_decl)
596 /* Don't mess with condition decls in a template. */
597 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
598 else if (getdecls () == NULL_TREE)
599 /* It was a simple condition; install it. */
600 FOR_COND (for_stmt) = cond;
601 else
603 /* If there was a declaration in the condition, we can't leave it
604 there; transform
605 for (; A x = 42;) { }
607 for (;;) { A x = 42; if (!x) break; } */
608 tree if_stmt;
609 FOR_COND (for_stmt) = NULL_TREE;
611 if_stmt = begin_if_stmt ();
612 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
613 finish_if_stmt_cond (cond, if_stmt);
614 finish_break_stmt ();
615 finish_then_clause (if_stmt);
616 finish_if_stmt ();
620 /* Finish the increment-EXPRESSION in a for-statement, which may be
621 given by FOR_STMT. */
623 void
624 finish_for_expr (expr, for_stmt)
625 tree expr;
626 tree for_stmt;
628 FOR_EXPR (for_stmt) = expr;
631 /* Finish the body of a for-statement, which may be given by
632 FOR_STMT. The increment-EXPR for the loop must be
633 provided. */
635 void
636 finish_for_stmt (for_stmt)
637 tree for_stmt;
639 /* Pop the scope for the body of the loop. */
640 do_poplevel ();
641 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
642 if (NEW_FOR_SCOPE_P (for_stmt))
643 do_poplevel ();
644 finish_stmt ();
647 /* Finish a break-statement. */
649 tree
650 finish_break_stmt ()
652 return add_stmt (build_break_stmt ());
655 /* Finish a continue-statement. */
657 tree
658 finish_continue_stmt ()
660 return add_stmt (build_continue_stmt ());
663 /* Begin a switch-statement. Returns a new SWITCH_STMT if
664 appropriate. */
666 tree
667 begin_switch_stmt ()
669 tree r;
670 do_pushlevel (sk_block);
671 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
672 add_stmt (r);
673 return r;
676 /* Finish the cond of a switch-statement. */
678 void
679 finish_switch_cond (cond, switch_stmt)
680 tree cond;
681 tree switch_stmt;
683 tree orig_type = NULL;
684 if (!processing_template_decl)
686 tree index;
688 /* Convert the condition to an integer or enumeration type. */
689 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
690 if (cond == NULL_TREE)
692 error ("switch quantity not an integer");
693 cond = error_mark_node;
695 orig_type = TREE_TYPE (cond);
696 if (cond != error_mark_node)
698 cond = default_conversion (cond);
699 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
702 if (cond != error_mark_node)
704 index = get_unwidened (cond, NULL_TREE);
705 /* We can't strip a conversion from a signed type to an unsigned,
706 because if we did, int_fits_type_p would do the wrong thing
707 when checking case values for being in range,
708 and it's too hard to do the right thing. */
709 if (TREE_UNSIGNED (TREE_TYPE (cond))
710 == TREE_UNSIGNED (TREE_TYPE (index)))
711 cond = index;
714 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
715 SWITCH_TYPE (switch_stmt) = orig_type;
716 push_switch (switch_stmt);
719 /* Finish the body of a switch-statement, which may be given by
720 SWITCH_STMT. The COND to switch on is indicated. */
722 void
723 finish_switch_stmt (switch_stmt)
724 tree switch_stmt;
726 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
727 pop_switch ();
728 finish_stmt ();
729 do_poplevel ();
732 /* Generate the RTL for T, which is a TRY_BLOCK. */
734 static void
735 genrtl_try_block (t)
736 tree t;
738 if (CLEANUP_P (t))
740 expand_eh_region_start ();
741 expand_stmt (TRY_STMTS (t));
742 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
744 else
746 if (!FN_TRY_BLOCK_P (t))
747 emit_line_note (input_filename, lineno);
749 expand_eh_region_start ();
750 expand_stmt (TRY_STMTS (t));
752 if (FN_TRY_BLOCK_P (t))
754 expand_start_all_catch ();
755 in_function_try_handler = 1;
756 expand_stmt (TRY_HANDLERS (t));
757 in_function_try_handler = 0;
758 expand_end_all_catch ();
760 else
762 expand_start_all_catch ();
763 expand_stmt (TRY_HANDLERS (t));
764 expand_end_all_catch ();
769 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
771 static void
772 genrtl_eh_spec_block (t)
773 tree t;
775 expand_eh_region_start ();
776 expand_stmt (EH_SPEC_STMTS (t));
777 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
778 build_call (call_unexpected_node,
779 tree_cons (NULL_TREE,
780 build_exc_ptr (),
781 NULL_TREE)));
784 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
785 appropriate. */
787 tree
788 begin_try_block ()
790 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
791 add_stmt (r);
792 return r;
795 /* Likewise, for a function-try-block. */
797 tree
798 begin_function_try_block ()
800 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
801 FN_TRY_BLOCK_P (r) = 1;
802 add_stmt (r);
803 return r;
806 /* Finish a try-block, which may be given by TRY_BLOCK. */
808 void
809 finish_try_block (try_block)
810 tree try_block;
812 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
815 /* Finish the body of a cleanup try-block, which may be given by
816 TRY_BLOCK. */
818 void
819 finish_cleanup_try_block (try_block)
820 tree try_block;
822 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
825 /* Finish an implicitly generated try-block, with a cleanup is given
826 by CLEANUP. */
828 void
829 finish_cleanup (cleanup, try_block)
830 tree cleanup;
831 tree try_block;
833 TRY_HANDLERS (try_block) = cleanup;
834 CLEANUP_P (try_block) = 1;
837 /* Likewise, for a function-try-block. */
839 void
840 finish_function_try_block (try_block)
841 tree try_block;
843 if (TREE_CHAIN (try_block)
844 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
846 /* Chain the compound statement after the CTOR_INITIALIZER. */
847 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
848 /* And make the CTOR_INITIALIZER the body of the try-block. */
849 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
851 else
852 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
853 in_function_try_handler = 1;
856 /* Finish a handler-sequence for a try-block, which may be given by
857 TRY_BLOCK. */
859 void
860 finish_handler_sequence (try_block)
861 tree try_block;
863 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
864 check_handlers (TRY_HANDLERS (try_block));
867 /* Likewise, for a function-try-block. */
869 void
870 finish_function_handler_sequence (try_block)
871 tree try_block;
873 in_function_try_handler = 0;
874 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
875 check_handlers (TRY_HANDLERS (try_block));
878 /* Generate the RTL for T, which is a HANDLER. */
880 static void
881 genrtl_handler (t)
882 tree t;
884 genrtl_do_pushlevel ();
885 if (!processing_template_decl)
886 expand_start_catch (HANDLER_TYPE (t));
887 expand_stmt (HANDLER_BODY (t));
888 if (!processing_template_decl)
889 expand_end_catch ();
892 /* Begin a handler. Returns a HANDLER if appropriate. */
894 tree
895 begin_handler ()
897 tree r;
898 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
899 add_stmt (r);
900 /* Create a binding level for the eh_info and the exception object
901 cleanup. */
902 do_pushlevel (sk_catch);
903 return r;
906 /* Finish the handler-parameters for a handler, which may be given by
907 HANDLER. DECL is the declaration for the catch parameter, or NULL
908 if this is a `catch (...)' clause. */
910 void
911 finish_handler_parms (decl, handler)
912 tree decl;
913 tree handler;
915 tree type = NULL_TREE;
916 if (processing_template_decl)
918 if (decl)
920 decl = pushdecl (decl);
921 decl = push_template_decl (decl);
922 add_decl_stmt (decl);
923 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
924 type = TREE_TYPE (decl);
927 else
928 type = expand_start_catch_block (decl);
930 HANDLER_TYPE (handler) = type;
933 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
934 the return value from the matching call to finish_handler_parms. */
936 void
937 finish_handler (handler)
938 tree handler;
940 if (!processing_template_decl)
941 expand_end_catch_block ();
942 do_poplevel ();
943 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
946 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
947 compound-statement does not define a scope. Returns a new
948 COMPOUND_STMT if appropriate. */
950 tree
951 begin_compound_stmt (has_no_scope)
952 int has_no_scope;
954 tree r;
955 int is_try = 0;
957 r = build_stmt (COMPOUND_STMT, NULL_TREE);
959 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
960 is_try = 1;
962 add_stmt (r);
963 if (has_no_scope)
964 COMPOUND_STMT_NO_SCOPE (r) = 1;
966 last_expr_type = NULL_TREE;
968 if (!has_no_scope)
969 do_pushlevel (is_try ? sk_try : sk_block);
970 else
971 /* Normally, we try hard to keep the BLOCK for a
972 statement-expression. But, if it's a statement-expression with
973 a scopeless block, there's nothing to keep, and we don't want
974 to accidentally keep a block *inside* the scopeless block. */
975 keep_next_level (0);
977 return r;
980 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
981 If HAS_NO_SCOPE is nonzero, the compound statement does not define
982 a scope. */
984 tree
985 finish_compound_stmt (has_no_scope, compound_stmt)
986 int has_no_scope;
987 tree compound_stmt;
989 tree r;
990 tree t;
992 if (!has_no_scope)
993 r = do_poplevel ();
994 else
995 r = NULL_TREE;
997 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
999 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1000 the precise purpose of that variable is store the type of the
1001 last expression statement within the last compound statement, we
1002 preserve the value. */
1003 t = last_expr_type;
1004 finish_stmt ();
1005 last_expr_type = t;
1007 return r;
1010 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1011 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1012 CLOBBERS. */
1014 tree
1015 finish_asm_stmt (cv_qualifier, string, output_operands,
1016 input_operands, clobbers)
1017 tree cv_qualifier;
1018 tree string;
1019 tree output_operands;
1020 tree input_operands;
1021 tree clobbers;
1023 tree r;
1024 tree t;
1026 if (cv_qualifier != NULL_TREE
1027 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1029 warning ("%s qualifier ignored on asm",
1030 IDENTIFIER_POINTER (cv_qualifier));
1031 cv_qualifier = NULL_TREE;
1034 if (!processing_template_decl)
1036 int i;
1037 int ninputs;
1038 int noutputs;
1040 for (t = input_operands; t; t = TREE_CHAIN (t))
1042 tree converted_operand
1043 = decay_conversion (TREE_VALUE (t));
1045 /* If the type of the operand hasn't been determined (e.g.,
1046 because it involves an overloaded function), then issue
1047 an error message. There's no context available to
1048 resolve the overloading. */
1049 if (TREE_TYPE (converted_operand) == unknown_type_node)
1051 error ("type of asm operand `%E' could not be determined",
1052 TREE_VALUE (t));
1053 converted_operand = error_mark_node;
1055 TREE_VALUE (t) = converted_operand;
1058 ninputs = list_length (input_operands);
1059 noutputs = list_length (output_operands);
1061 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1063 bool allows_mem;
1064 bool allows_reg;
1065 bool is_inout;
1066 const char *constraint;
1067 tree operand;
1069 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1070 operand = TREE_VALUE (t);
1072 if (!parse_output_constraint (&constraint,
1073 i, ninputs, noutputs,
1074 &allows_mem,
1075 &allows_reg,
1076 &is_inout))
1078 /* By marking this operand as erroneous, we will not try
1079 to process this operand again in expand_asm_operands. */
1080 TREE_VALUE (t) = error_mark_node;
1081 continue;
1084 /* If the operand is a DECL that is going to end up in
1085 memory, assume it is addressable. This is a bit more
1086 conservative than it would ideally be; the exact test is
1087 buried deep in expand_asm_operands and depends on the
1088 DECL_RTL for the OPERAND -- which we don't have at this
1089 point. */
1090 if (!allows_reg && DECL_P (operand))
1091 cxx_mark_addressable (operand);
1095 r = build_stmt (ASM_STMT, cv_qualifier, string,
1096 output_operands, input_operands,
1097 clobbers);
1098 return add_stmt (r);
1101 /* Finish a label with the indicated NAME. */
1103 tree
1104 finish_label_stmt (name)
1105 tree name;
1107 tree decl = define_label (input_filename, lineno, name);
1108 return add_stmt (build_stmt (LABEL_STMT, decl));
1111 /* Finish a series of declarations for local labels. G++ allows users
1112 to declare "local" labels, i.e., labels with scope. This extension
1113 is useful when writing code involving statement-expressions. */
1115 void
1116 finish_label_decl (name)
1117 tree name;
1119 tree decl = declare_local_label (name);
1120 add_decl_stmt (decl);
1123 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1125 void
1126 finish_decl_cleanup (decl, cleanup)
1127 tree decl;
1128 tree cleanup;
1130 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1133 /* If the current scope exits with an exception, run CLEANUP. */
1135 void
1136 finish_eh_cleanup (cleanup)
1137 tree cleanup;
1139 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1140 CLEANUP_EH_ONLY (r) = 1;
1141 add_stmt (r);
1144 /* Begin processing a mem-initializer-list. */
1146 void
1147 begin_mem_initializers ()
1149 if (! DECL_CONSTRUCTOR_P (current_function_decl))
1150 error ("only constructors take base initializers");
1153 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1154 order they were written by the user. Each node is as for
1155 emit_mem_initializers. */
1157 void
1158 finish_mem_initializers (tree mem_inits)
1160 /* Reorder the MEM_INITS so that they are in the order they appeared
1161 in the source program. */
1162 mem_inits = nreverse (mem_inits);
1164 if (processing_template_decl)
1165 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1166 else
1167 emit_mem_initializers (mem_inits);
1170 /* Returns the stack of SCOPE_STMTs for the current function. */
1172 tree *
1173 current_scope_stmt_stack ()
1175 return &cfun->language->base.x_scope_stmt_stack;
1178 /* Finish a parenthesized expression EXPR. */
1180 tree
1181 finish_parenthesized_expr (expr)
1182 tree expr;
1184 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1185 /* This inhibits warnings in c_common_truthvalue_conversion. */
1186 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1188 if (TREE_CODE (expr) == OFFSET_REF)
1189 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1190 enclosed in parentheses. */
1191 PTRMEM_OK_P (expr) = 0;
1192 return expr;
1195 /* Finish a reference to a non-static data member (DECL) that is not
1196 preceded by `.' or `->'. */
1198 tree
1199 finish_non_static_data_member (tree decl, tree qualifying_scope)
1201 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1203 if (current_class_ptr == NULL_TREE)
1205 if (current_function_decl
1206 && DECL_STATIC_FUNCTION_P (current_function_decl))
1207 cp_error_at ("invalid use of member `%D' in static member function",
1208 decl);
1209 else
1210 cp_error_at ("invalid use of non-static data member `%D'", decl);
1211 error ("from this location");
1213 return error_mark_node;
1215 TREE_USED (current_class_ptr) = 1;
1216 if (processing_template_decl)
1217 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1218 current_class_ref, DECL_NAME (decl));
1219 else
1221 tree access_type = current_class_type;
1222 tree object = current_class_ref;
1224 while (!DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1226 access_type = TYPE_CONTEXT (access_type);
1227 while (DECL_P (access_type))
1228 access_type = DECL_CONTEXT (access_type);
1231 enforce_access (access_type, decl);
1233 /* If the data member was named `C::M', convert `*this' to `C'
1234 first. */
1235 if (qualifying_scope)
1237 tree binfo = NULL_TREE;
1238 object = build_scoped_ref (object, qualifying_scope,
1239 &binfo);
1242 return build_class_member_access_expr (object, decl,
1243 /*access_path=*/NULL_TREE,
1244 /*preserve_reference=*/false);
1248 /* Begin a statement-expression. The value returned must be passed to
1249 finish_stmt_expr. */
1251 tree
1252 begin_stmt_expr ()
1254 /* If we're outside a function, we won't have a statement-tree to
1255 work with. But, if we see a statement-expression we need to
1256 create one. */
1257 if (! cfun && !last_tree)
1258 begin_stmt_tree (&scope_chain->x_saved_tree);
1260 keep_next_level (1);
1261 /* If we're building a statement tree, then the upcoming compound
1262 statement will be chained onto the tree structure, starting at
1263 last_tree. We return last_tree so that we can later unhook the
1264 compound statement. */
1265 return last_tree;
1268 /* Used when beginning a statement-expression outside function scope.
1269 For example, when handling a file-scope initializer, we use this
1270 function. */
1272 tree
1273 begin_global_stmt_expr ()
1275 if (! cfun && !last_tree)
1276 begin_stmt_tree (&scope_chain->x_saved_tree);
1278 keep_next_level (1);
1280 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1283 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1285 tree
1286 finish_global_stmt_expr (stmt_expr)
1287 tree stmt_expr;
1289 stmt_expr = expand_end_stmt_expr (stmt_expr);
1291 if (! cfun
1292 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1293 finish_stmt_tree (&scope_chain->x_saved_tree);
1295 return stmt_expr;
1298 /* Finish a statement-expression. RTL_EXPR should be the value
1299 returned by the previous begin_stmt_expr; EXPR is the
1300 statement-expression. Returns an expression representing the
1301 statement-expression. */
1303 tree
1304 finish_stmt_expr (rtl_expr)
1305 tree rtl_expr;
1307 tree result;
1309 /* If the last thing in the statement-expression was not an
1310 expression-statement, then it has type `void'. */
1311 if (!last_expr_type)
1312 last_expr_type = void_type_node;
1313 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1314 TREE_SIDE_EFFECTS (result) = 1;
1316 /* Remove the compound statement from the tree structure; it is
1317 now saved in the STMT_EXPR. */
1318 last_tree = rtl_expr;
1319 TREE_CHAIN (last_tree) = NULL_TREE;
1321 /* If we created a statement-tree for this statement-expression,
1322 remove it now. */
1323 if (! cfun
1324 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1325 finish_stmt_tree (&scope_chain->x_saved_tree);
1327 return result;
1330 /* Generate an expression for `FN (ARGS)'.
1332 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1333 as a virtual call, even if FN is virtual. (This flag is set when
1334 encountering an expression where the function name is explicitly
1335 qualified. For example a call to `X::f' never generates a virtual
1336 call.)
1338 Returns code for the call. */
1340 tree
1341 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1343 if (fn == error_mark_node || args == error_mark_node)
1344 return error_mark_node;
1346 if (processing_template_decl)
1347 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1349 /* ARGS should be a list of arguments. */
1350 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1351 20020712);
1353 /* A reference to a member function will appear as an overloaded
1354 function (rather than a BASELINK) if an unqualified name was used
1355 to refer to it. */
1356 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1358 tree f;
1360 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1361 f = get_first_fn (TREE_OPERAND (fn, 0));
1362 else
1363 f = get_first_fn (fn);
1364 if (DECL_FUNCTION_MEMBER_P (f))
1366 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1367 fn = build_baselink (TYPE_BINFO (type),
1368 TYPE_BINFO (type),
1369 fn, /*optype=*/NULL_TREE);
1373 if (BASELINK_P (fn))
1375 tree object;
1377 /* A call to a member function. From [over.call.func]:
1379 If the keyword this is in scope and refers to the class of
1380 that member function, or a derived class thereof, then the
1381 function call is transformed into a qualified function call
1382 using (*this) as the postfix-expression to the left of the
1383 . operator.... [Otherwise] a contrived object of type T
1384 becomes the implied object argument.
1386 This paragraph is unclear about this situation:
1388 struct A { void f(); };
1389 struct B : public A {};
1390 struct C : public A { void g() { B::f(); }};
1392 In particular, for `B::f', this paragraph does not make clear
1393 whether "the class of that member function" refers to `A' or
1394 to `B'. We believe it refers to `B'. */
1395 if (current_class_type
1396 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1397 current_class_type)
1398 && current_class_ref)
1399 object = current_class_ref;
1400 else
1402 tree representative_fn;
1404 representative_fn = BASELINK_FUNCTIONS (fn);
1405 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1406 representative_fn = TREE_OPERAND (representative_fn, 0);
1407 representative_fn = get_first_fn (representative_fn);
1408 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1411 return build_new_method_call (object, fn, args, NULL_TREE,
1412 (disallow_virtual
1413 ? LOOKUP_NONVIRTUAL : 0));
1415 else if (is_overloaded_fn (fn))
1416 /* A call to a namespace-scope function. */
1417 return build_new_function_call (fn, args);
1418 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1420 tree result;
1422 if (args)
1423 error ("arguments to destructor are not allowed");
1424 /* Mark the pseudo-destructor call as having side-effects so
1425 that we do not issue warnings about its use. */
1426 result = build1 (NOP_EXPR,
1427 void_type_node,
1428 TREE_OPERAND (fn, 0));
1429 TREE_SIDE_EFFECTS (result) = 1;
1430 return result;
1432 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1434 /* If the "function" is really an object of class type, it might
1435 have an overloaded `operator ()'. */
1436 tree result;
1437 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1438 if (result)
1439 return result;
1442 /* A call where the function is unknown. */
1443 return build_function_call (fn, args);
1446 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1447 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1448 POSTDECREMENT_EXPR.) */
1450 tree
1451 finish_increment_expr (expr, code)
1452 tree expr;
1453 enum tree_code code;
1455 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1456 a COMPONENT_REF). This way if we've got, say, a reference to a
1457 static member that's being operated on, we don't end up trying to
1458 find a member operator for the class it's in. */
1460 if (TREE_CODE (expr) == OFFSET_REF)
1461 expr = resolve_offset_ref (expr);
1462 return build_x_unary_op (code, expr);
1465 /* Finish a use of `this'. Returns an expression for `this'. */
1467 tree
1468 finish_this_expr ()
1470 tree result;
1472 if (current_class_ptr)
1474 result = current_class_ptr;
1476 else if (current_function_decl
1477 && DECL_STATIC_FUNCTION_P (current_function_decl))
1479 error ("`this' is unavailable for static member functions");
1480 result = error_mark_node;
1482 else
1484 if (current_function_decl)
1485 error ("invalid use of `this' in non-member function");
1486 else
1487 error ("invalid use of `this' at top level");
1488 result = error_mark_node;
1491 return result;
1494 /* Finish a member function call using OBJECT and ARGS as arguments to
1495 FN. Returns an expression for the call. */
1497 tree
1498 finish_object_call_expr (fn, object, args)
1499 tree fn;
1500 tree object;
1501 tree args;
1503 if (DECL_DECLARES_TYPE_P (fn))
1505 if (processing_template_decl)
1506 /* This can happen on code like:
1508 class X;
1509 template <class T> void f(T t) {
1510 t.X();
1513 We just grab the underlying IDENTIFIER. */
1514 fn = DECL_NAME (fn);
1515 else
1517 error ("calling type `%T' like a method", fn);
1518 return error_mark_node;
1522 if (processing_template_decl)
1523 return build_nt (CALL_EXPR,
1524 build_nt (COMPONENT_REF, object, fn),
1525 args);
1527 if (name_p (fn))
1528 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1529 else
1530 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1533 /* Finish a qualified member function call using OBJECT and ARGS as
1534 arguments to FN. Returns an expression for the call. */
1536 tree
1537 finish_qualified_object_call_expr (fn, object, args)
1538 tree fn;
1539 tree object;
1540 tree args;
1542 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1543 TREE_OPERAND (fn, 1), args);
1546 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1547 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1548 the TYPE for the type given. If SCOPE is non-NULL, the expression
1549 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1551 tree
1552 finish_pseudo_destructor_expr (object, scope, destructor)
1553 tree object;
1554 tree scope;
1555 tree destructor;
1557 if (destructor == error_mark_node)
1558 return error_mark_node;
1560 my_friendly_assert (TYPE_P (destructor), 20010905);
1562 if (!processing_template_decl)
1564 if (scope == error_mark_node)
1566 error ("invalid qualifying scope in pseudo-destructor name");
1567 return error_mark_node;
1570 if (!same_type_p (TREE_TYPE (object), destructor))
1572 error ("`%E' is not of type `%T'", object, destructor);
1573 return error_mark_node;
1577 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1580 /* Finish an expression of the form CODE EXPR. */
1582 tree
1583 finish_unary_op_expr (code, expr)
1584 enum tree_code code;
1585 tree expr;
1587 tree result = build_x_unary_op (code, expr);
1588 /* Inside a template, build_x_unary_op does not fold the
1589 expression. So check whether the result is folded before
1590 setting TREE_NEGATED_INT. */
1591 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1592 && TREE_CODE (result) == INTEGER_CST
1593 && !TREE_UNSIGNED (TREE_TYPE (result))
1594 && INT_CST_LT (result, integer_zero_node))
1595 TREE_NEGATED_INT (result) = 1;
1596 overflow_warning (result);
1597 return result;
1600 /* Finish a compound-literal expression. TYPE is the type to which
1601 the INITIALIZER_LIST is being cast. */
1603 tree
1604 finish_compound_literal (type, initializer_list)
1605 tree type;
1606 tree initializer_list;
1608 tree compound_literal;
1610 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1611 compound_literal = build_nt (CONSTRUCTOR, NULL_TREE,
1612 initializer_list);
1613 /* Mark it as a compound-literal. */
1614 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1615 if (processing_template_decl)
1616 TREE_TYPE (compound_literal) = type;
1617 else
1619 /* Check the initialization. */
1620 compound_literal = digest_init (type, compound_literal, NULL);
1621 /* If the TYPE was an array type with an unknown bound, then we can
1622 figure out the dimension now. For example, something like:
1624 `(int []) { 2, 3 }'
1626 implies that the array has two elements. */
1627 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1628 complete_array_type (type, compound_literal, 1);
1631 return compound_literal;
1634 /* Return the declaration for the function-name variable indicated by
1635 ID. */
1637 tree
1638 finish_fname (tree id)
1640 tree decl;
1642 decl = fname_decl (C_RID_CODE (id), id);
1643 if (processing_template_decl)
1644 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1645 return decl;
1648 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1649 and DECLARATOR. Returns nonzero if the function-declaration is
1650 valid. */
1653 begin_function_definition (decl_specs, attributes, declarator)
1654 tree decl_specs;
1655 tree attributes;
1656 tree declarator;
1658 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1659 return 0;
1661 /* The things we're about to see are not directly qualified by any
1662 template headers we've seen thus far. */
1663 reset_specialization ();
1665 return 1;
1668 /* Finish an init-declarator. Returns a DECL. */
1670 tree
1671 finish_declarator (declarator, declspecs, attributes,
1672 prefix_attributes, initialized)
1673 tree declarator;
1674 tree declspecs;
1675 tree attributes;
1676 tree prefix_attributes;
1677 int initialized;
1679 return start_decl (declarator, declspecs, initialized, attributes,
1680 prefix_attributes);
1683 /* Finish a translation unit. */
1685 void
1686 finish_translation_unit ()
1688 /* In case there were missing closebraces,
1689 get us back to the global binding level. */
1690 pop_everything ();
1691 while (current_namespace != global_namespace)
1692 pop_namespace ();
1694 /* Do file scope __FUNCTION__ et al. */
1695 finish_fname_decls ();
1698 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1699 Returns the parameter. */
1701 tree
1702 finish_template_type_parm (aggr, identifier)
1703 tree aggr;
1704 tree identifier;
1706 if (aggr != class_type_node)
1708 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1709 aggr = class_type_node;
1712 return build_tree_list (aggr, identifier);
1715 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1716 Returns the parameter. */
1718 tree
1719 finish_template_template_parm (aggr, identifier)
1720 tree aggr;
1721 tree identifier;
1723 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1724 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1725 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1726 DECL_TEMPLATE_RESULT (tmpl) = decl;
1727 DECL_ARTIFICIAL (decl) = 1;
1728 end_template_decl ();
1730 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1732 return finish_template_type_parm (aggr, tmpl);
1735 /* ARGUMENT is the default-argument value for a template template
1736 parameter. If ARGUMENT is invalid, issue error messages and return
1737 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1739 tree
1740 check_template_template_default_arg (tree argument)
1742 if (TREE_CODE (argument) != TEMPLATE_DECL
1743 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1744 && TREE_CODE (argument) != TYPE_DECL
1745 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1747 error ("invalid default template argument");
1748 return error_mark_node;
1751 return argument;
1754 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1755 nonzero, the parameter list was terminated by a `...'. */
1757 tree
1758 finish_parmlist (parms, ellipsis)
1759 tree parms;
1760 int ellipsis;
1762 if (parms)
1764 /* We mark the PARMS as a parmlist so that declarator processing can
1765 disambiguate certain constructs. */
1766 TREE_PARMLIST (parms) = 1;
1767 /* We do not append void_list_node here, but leave it to grokparms
1768 to do that. */
1769 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1771 return parms;
1774 /* Begin a class definition, as indicated by T. */
1776 tree
1777 begin_class_definition (t)
1778 tree t;
1780 if (t == error_mark_node)
1781 return error_mark_node;
1783 if (processing_template_parmlist)
1785 error ("definition of `%#T' inside template parameter list", t);
1786 return error_mark_node;
1788 /* A non-implicit typename comes from code like:
1790 template <typename T> struct A {
1791 template <typename U> struct A<T>::B ...
1793 This is erroneous. */
1794 else if (TREE_CODE (t) == TYPENAME_TYPE)
1796 error ("invalid definition of qualified type `%T'", t);
1797 t = error_mark_node;
1800 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1802 t = make_aggr_type (RECORD_TYPE);
1803 pushtag (make_anon_name (), t, 0);
1806 /* If this type was already complete, and we see another definition,
1807 that's an error. */
1808 if (COMPLETE_TYPE_P (t))
1809 duplicate_tag_error (t);
1811 /* Update the location of the decl. */
1812 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1813 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1815 if (TYPE_BEING_DEFINED (t))
1817 t = make_aggr_type (TREE_CODE (t));
1818 pushtag (TYPE_IDENTIFIER (t), t, 0);
1820 maybe_process_partial_specialization (t);
1821 pushclass (t, true);
1822 TYPE_BEING_DEFINED (t) = 1;
1823 TYPE_PACKED (t) = flag_pack_struct;
1824 /* Reset the interface data, at the earliest possible
1825 moment, as it might have been set via a class foo;
1826 before. */
1827 if (! TYPE_ANONYMOUS_P (t))
1829 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1830 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1831 (t, interface_unknown);
1833 reset_specialization();
1835 /* Make a declaration for this class in its own scope. */
1836 build_self_reference ();
1838 return t;
1841 /* Finish the member declaration given by DECL. */
1843 void
1844 finish_member_declaration (decl)
1845 tree decl;
1847 if (decl == error_mark_node || decl == NULL_TREE)
1848 return;
1850 if (decl == void_type_node)
1851 /* The COMPONENT was a friend, not a member, and so there's
1852 nothing for us to do. */
1853 return;
1855 /* We should see only one DECL at a time. */
1856 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1858 /* Set up access control for DECL. */
1859 TREE_PRIVATE (decl)
1860 = (current_access_specifier == access_private_node);
1861 TREE_PROTECTED (decl)
1862 = (current_access_specifier == access_protected_node);
1863 if (TREE_CODE (decl) == TEMPLATE_DECL)
1865 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1866 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1869 /* Mark the DECL as a member of the current class. */
1870 DECL_CONTEXT (decl) = current_class_type;
1872 /* [dcl.link]
1874 A C language linkage is ignored for the names of class members
1875 and the member function type of class member functions. */
1876 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1877 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1879 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1881 /* Put functions on the TYPE_METHODS list and everything else on the
1882 TYPE_FIELDS list. Note that these are built up in reverse order.
1883 We reverse them (to obtain declaration order) in finish_struct. */
1884 if (TREE_CODE (decl) == FUNCTION_DECL
1885 || DECL_FUNCTION_TEMPLATE_P (decl))
1887 /* We also need to add this function to the
1888 CLASSTYPE_METHOD_VEC. */
1889 add_method (current_class_type, decl, /*error_p=*/0);
1891 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1892 TYPE_METHODS (current_class_type) = decl;
1894 else
1896 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1897 go at the beginning. The reason is that lookup_field_1
1898 searches the list in order, and we want a field name to
1899 override a type name so that the "struct stat hack" will
1900 work. In particular:
1902 struct S { enum E { }; int E } s;
1903 s.E = 3;
1905 is valid. In addition, the FIELD_DECLs must be maintained in
1906 declaration order so that class layout works as expected.
1907 However, we don't need that order until class layout, so we
1908 save a little time by putting FIELD_DECLs on in reverse order
1909 here, and then reversing them in finish_struct_1. (We could
1910 also keep a pointer to the correct insertion points in the
1911 list.) */
1913 if (TREE_CODE (decl) == TYPE_DECL)
1914 TYPE_FIELDS (current_class_type)
1915 = chainon (TYPE_FIELDS (current_class_type), decl);
1916 else
1918 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1919 TYPE_FIELDS (current_class_type) = decl;
1922 /* Enter the DECL into the scope of the class. */
1923 if (TREE_CODE (decl) != USING_DECL)
1924 pushdecl_class_level (decl);
1928 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1929 the definition is immediately followed by a semicolon. Returns the
1930 type. */
1932 tree
1933 finish_class_definition (t, attributes, semi, pop_scope_p)
1934 tree t;
1935 tree attributes;
1936 int semi;
1937 int pop_scope_p;
1939 if (t == error_mark_node)
1940 return error_mark_node;
1942 /* finish_struct nukes this anyway; if finish_exception does too,
1943 then it can go. */
1944 if (semi)
1945 note_got_semicolon (t);
1947 /* If we got any attributes in class_head, xref_tag will stick them in
1948 TREE_TYPE of the type. Grab them now. */
1949 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1950 TYPE_ATTRIBUTES (t) = NULL_TREE;
1952 if (TREE_CODE (t) == ENUMERAL_TYPE)
1954 else
1956 t = finish_struct (t, attributes);
1957 if (semi)
1958 note_got_semicolon (t);
1961 if (pop_scope_p)
1962 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1964 return t;
1967 /* Finish processing the declaration of a member class template
1968 TYPES whose template parameters are given by PARMS. */
1970 tree
1971 finish_member_class_template (types)
1972 tree types;
1974 tree t;
1976 /* If there are declared, but undefined, partial specializations
1977 mixed in with the typespecs they will not yet have passed through
1978 maybe_process_partial_specialization, so we do that here. */
1979 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1980 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1981 maybe_process_partial_specialization (TREE_VALUE (t));
1983 note_list_got_semicolon (types);
1984 grok_x_components (types);
1985 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1986 /* The component was in fact a friend declaration. We avoid
1987 finish_member_template_decl performing certain checks by
1988 unsetting TYPES. */
1989 types = NULL_TREE;
1991 finish_member_template_decl (types);
1993 /* As with other component type declarations, we do
1994 not store the new DECL on the list of
1995 component_decls. */
1996 return NULL_TREE;
1999 /* Finish processing a complete template declaration. The PARMS are
2000 the template parameters. */
2002 void
2003 finish_template_decl (parms)
2004 tree parms;
2006 if (parms)
2007 end_template_decl ();
2008 else
2009 end_specialization ();
2012 /* Finish processing a template-id (which names a type) of the form
2013 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2014 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2015 the scope of template-id indicated. */
2017 tree
2018 finish_template_type (name, args, entering_scope)
2019 tree name;
2020 tree args;
2021 int entering_scope;
2023 tree decl;
2025 decl = lookup_template_class (name, args,
2026 NULL_TREE, NULL_TREE,
2027 entering_scope, /*complain=*/1);
2028 if (decl != error_mark_node)
2029 decl = TYPE_STUB_DECL (decl);
2031 return decl;
2034 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2035 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2036 BASE_CLASS, or NULL_TREE if an error occurred. The
2037 ACCESS_SPECIFIER is one of
2038 access_{default,public,protected_private}[_virtual]_node.*/
2040 tree
2041 finish_base_specifier (access_specifier, base_class)
2042 tree access_specifier;
2043 tree base_class;
2045 tree result;
2047 if (base_class == error_mark_node)
2049 error ("invalid base-class specification");
2050 result = NULL_TREE;
2052 else if (! is_aggr_type (base_class, 1))
2053 result = NULL_TREE;
2054 else
2056 if (cp_type_quals (base_class) != 0)
2058 error ("base class `%T' has cv qualifiers", base_class);
2059 base_class = TYPE_MAIN_VARIANT (base_class);
2061 result = build_tree_list (access_specifier, base_class);
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"