2003-03-15 Glen Nakamura <glen@imodulo.com>
[official-gcc.git] / gcc / cp / semantics.c
bloba5b7c50398047e23dac6490b84d1eab975137200
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 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1146 order they were written by the user. Each node is as for
1147 emit_mem_initializers. */
1149 void
1150 finish_mem_initializers (tree mem_inits)
1152 /* Reorder the MEM_INITS so that they are in the order they appeared
1153 in the source program. */
1154 mem_inits = nreverse (mem_inits);
1156 if (processing_template_decl)
1157 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1158 else
1159 emit_mem_initializers (mem_inits);
1162 /* Returns the stack of SCOPE_STMTs for the current function. */
1164 tree *
1165 current_scope_stmt_stack ()
1167 return &cfun->language->base.x_scope_stmt_stack;
1170 /* Finish a parenthesized expression EXPR. */
1172 tree
1173 finish_parenthesized_expr (expr)
1174 tree expr;
1176 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1177 /* This inhibits warnings in c_common_truthvalue_conversion. */
1178 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1180 if (TREE_CODE (expr) == OFFSET_REF)
1181 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1182 enclosed in parentheses. */
1183 PTRMEM_OK_P (expr) = 0;
1184 return expr;
1187 /* Finish a reference to a non-static data member (DECL) that is not
1188 preceded by `.' or `->'. */
1190 tree
1191 finish_non_static_data_member (tree decl, tree qualifying_scope)
1193 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1195 if (current_class_ptr == NULL_TREE)
1197 if (current_function_decl
1198 && DECL_STATIC_FUNCTION_P (current_function_decl))
1199 cp_error_at ("invalid use of member `%D' in static member function",
1200 decl);
1201 else
1202 cp_error_at ("invalid use of non-static data member `%D'", decl);
1203 error ("from this location");
1205 return error_mark_node;
1207 TREE_USED (current_class_ptr) = 1;
1208 if (processing_template_decl)
1209 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1210 current_class_ref, DECL_NAME (decl));
1211 else
1213 tree access_type = current_class_type;
1214 tree object = current_class_ref;
1216 while (!DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1218 access_type = TYPE_CONTEXT (access_type);
1219 while (DECL_P (access_type))
1220 access_type = DECL_CONTEXT (access_type);
1223 enforce_access (access_type, decl);
1225 /* If the data member was named `C::M', convert `*this' to `C'
1226 first. */
1227 if (qualifying_scope)
1229 tree binfo = NULL_TREE;
1230 object = build_scoped_ref (object, qualifying_scope,
1231 &binfo);
1234 return build_class_member_access_expr (object, decl,
1235 /*access_path=*/NULL_TREE,
1236 /*preserve_reference=*/false);
1240 /* Begin a statement-expression. The value returned must be passed to
1241 finish_stmt_expr. */
1243 tree
1244 begin_stmt_expr ()
1246 /* If we're outside a function, we won't have a statement-tree to
1247 work with. But, if we see a statement-expression we need to
1248 create one. */
1249 if (! cfun && !last_tree)
1250 begin_stmt_tree (&scope_chain->x_saved_tree);
1252 keep_next_level (1);
1253 /* If we're building a statement tree, then the upcoming compound
1254 statement will be chained onto the tree structure, starting at
1255 last_tree. We return last_tree so that we can later unhook the
1256 compound statement. */
1257 return last_tree;
1260 /* Used when beginning a statement-expression outside function scope.
1261 For example, when handling a file-scope initializer, we use this
1262 function. */
1264 tree
1265 begin_global_stmt_expr ()
1267 if (! cfun && !last_tree)
1268 begin_stmt_tree (&scope_chain->x_saved_tree);
1270 keep_next_level (1);
1272 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1275 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1277 tree
1278 finish_global_stmt_expr (stmt_expr)
1279 tree stmt_expr;
1281 stmt_expr = expand_end_stmt_expr (stmt_expr);
1283 if (! cfun
1284 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1285 finish_stmt_tree (&scope_chain->x_saved_tree);
1287 return stmt_expr;
1290 /* Finish a statement-expression. RTL_EXPR should be the value
1291 returned by the previous begin_stmt_expr; EXPR is the
1292 statement-expression. Returns an expression representing the
1293 statement-expression. */
1295 tree
1296 finish_stmt_expr (rtl_expr)
1297 tree rtl_expr;
1299 tree result;
1301 /* If the last thing in the statement-expression was not an
1302 expression-statement, then it has type `void'. */
1303 if (!last_expr_type)
1304 last_expr_type = void_type_node;
1305 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1306 TREE_SIDE_EFFECTS (result) = 1;
1308 /* Remove the compound statement from the tree structure; it is
1309 now saved in the STMT_EXPR. */
1310 last_tree = rtl_expr;
1311 TREE_CHAIN (last_tree) = NULL_TREE;
1313 /* If we created a statement-tree for this statement-expression,
1314 remove it now. */
1315 if (! cfun
1316 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1317 finish_stmt_tree (&scope_chain->x_saved_tree);
1319 return result;
1322 /* Generate an expression for `FN (ARGS)'.
1324 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1325 as a virtual call, even if FN is virtual. (This flag is set when
1326 encountering an expression where the function name is explicitly
1327 qualified. For example a call to `X::f' never generates a virtual
1328 call.)
1330 Returns code for the call. */
1332 tree
1333 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1335 if (fn == error_mark_node || args == error_mark_node)
1336 return error_mark_node;
1338 if (processing_template_decl)
1339 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1341 /* ARGS should be a list of arguments. */
1342 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1343 20020712);
1345 /* A reference to a member function will appear as an overloaded
1346 function (rather than a BASELINK) if an unqualified name was used
1347 to refer to it. */
1348 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1350 tree f;
1352 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1353 f = get_first_fn (TREE_OPERAND (fn, 0));
1354 else
1355 f = get_first_fn (fn);
1356 if (DECL_FUNCTION_MEMBER_P (f))
1358 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1359 fn = build_baselink (TYPE_BINFO (type),
1360 TYPE_BINFO (type),
1361 fn, /*optype=*/NULL_TREE);
1365 if (BASELINK_P (fn))
1367 tree object;
1369 /* A call to a member function. From [over.call.func]:
1371 If the keyword this is in scope and refers to the class of
1372 that member function, or a derived class thereof, then the
1373 function call is transformed into a qualified function call
1374 using (*this) as the postfix-expression to the left of the
1375 . operator.... [Otherwise] a contrived object of type T
1376 becomes the implied object argument.
1378 This paragraph is unclear about this situation:
1380 struct A { void f(); };
1381 struct B : public A {};
1382 struct C : public A { void g() { B::f(); }};
1384 In particular, for `B::f', this paragraph does not make clear
1385 whether "the class of that member function" refers to `A' or
1386 to `B'. We believe it refers to `B'. */
1387 if (current_class_type
1388 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1389 current_class_type)
1390 && current_class_ref)
1391 object = current_class_ref;
1392 else
1394 tree representative_fn;
1396 representative_fn = BASELINK_FUNCTIONS (fn);
1397 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1398 representative_fn = TREE_OPERAND (representative_fn, 0);
1399 representative_fn = get_first_fn (representative_fn);
1400 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1403 return build_new_method_call (object, fn, args, NULL_TREE,
1404 (disallow_virtual
1405 ? LOOKUP_NONVIRTUAL : 0));
1407 else if (is_overloaded_fn (fn))
1408 /* A call to a namespace-scope function. */
1409 return build_new_function_call (fn, args);
1410 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1412 tree result;
1414 if (args)
1415 error ("arguments to destructor are not allowed");
1416 /* Mark the pseudo-destructor call as having side-effects so
1417 that we do not issue warnings about its use. */
1418 result = build1 (NOP_EXPR,
1419 void_type_node,
1420 TREE_OPERAND (fn, 0));
1421 TREE_SIDE_EFFECTS (result) = 1;
1422 return result;
1424 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1426 /* If the "function" is really an object of class type, it might
1427 have an overloaded `operator ()'. */
1428 tree result;
1429 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1430 if (result)
1431 return result;
1434 /* A call where the function is unknown. */
1435 return build_function_call (fn, args);
1438 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1439 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1440 POSTDECREMENT_EXPR.) */
1442 tree
1443 finish_increment_expr (expr, code)
1444 tree expr;
1445 enum tree_code code;
1447 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1448 a COMPONENT_REF). This way if we've got, say, a reference to a
1449 static member that's being operated on, we don't end up trying to
1450 find a member operator for the class it's in. */
1452 if (TREE_CODE (expr) == OFFSET_REF)
1453 expr = resolve_offset_ref (expr);
1454 return build_x_unary_op (code, expr);
1457 /* Finish a use of `this'. Returns an expression for `this'. */
1459 tree
1460 finish_this_expr ()
1462 tree result;
1464 if (current_class_ptr)
1466 result = current_class_ptr;
1468 else if (current_function_decl
1469 && DECL_STATIC_FUNCTION_P (current_function_decl))
1471 error ("`this' is unavailable for static member functions");
1472 result = error_mark_node;
1474 else
1476 if (current_function_decl)
1477 error ("invalid use of `this' in non-member function");
1478 else
1479 error ("invalid use of `this' at top level");
1480 result = error_mark_node;
1483 return result;
1486 /* Finish a member function call using OBJECT and ARGS as arguments to
1487 FN. Returns an expression for the call. */
1489 tree
1490 finish_object_call_expr (fn, object, args)
1491 tree fn;
1492 tree object;
1493 tree args;
1495 if (DECL_DECLARES_TYPE_P (fn))
1497 if (processing_template_decl)
1498 /* This can happen on code like:
1500 class X;
1501 template <class T> void f(T t) {
1502 t.X();
1505 We just grab the underlying IDENTIFIER. */
1506 fn = DECL_NAME (fn);
1507 else
1509 error ("calling type `%T' like a method", fn);
1510 return error_mark_node;
1514 if (processing_template_decl)
1515 return build_nt (CALL_EXPR,
1516 build_nt (COMPONENT_REF, object, fn),
1517 args);
1519 if (name_p (fn))
1520 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1521 else
1522 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1525 /* Finish a qualified member function call using OBJECT and ARGS as
1526 arguments to FN. Returns an expression for the call. */
1528 tree
1529 finish_qualified_object_call_expr (fn, object, args)
1530 tree fn;
1531 tree object;
1532 tree args;
1534 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1535 TREE_OPERAND (fn, 1), args);
1538 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1539 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1540 the TYPE for the type given. If SCOPE is non-NULL, the expression
1541 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1543 tree
1544 finish_pseudo_destructor_expr (object, scope, destructor)
1545 tree object;
1546 tree scope;
1547 tree destructor;
1549 if (destructor == error_mark_node)
1550 return error_mark_node;
1552 my_friendly_assert (TYPE_P (destructor), 20010905);
1554 if (!processing_template_decl)
1556 if (scope == error_mark_node)
1558 error ("invalid qualifying scope in pseudo-destructor name");
1559 return error_mark_node;
1562 if (!same_type_p (TREE_TYPE (object), destructor))
1564 error ("`%E' is not of type `%T'", object, destructor);
1565 return error_mark_node;
1569 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1572 /* Finish an expression of the form CODE EXPR. */
1574 tree
1575 finish_unary_op_expr (code, expr)
1576 enum tree_code code;
1577 tree expr;
1579 tree result = build_x_unary_op (code, expr);
1580 /* Inside a template, build_x_unary_op does not fold the
1581 expression. So check whether the result is folded before
1582 setting TREE_NEGATED_INT. */
1583 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1584 && TREE_CODE (result) == INTEGER_CST
1585 && !TREE_UNSIGNED (TREE_TYPE (result))
1586 && INT_CST_LT (result, integer_zero_node))
1587 TREE_NEGATED_INT (result) = 1;
1588 overflow_warning (result);
1589 return result;
1592 /* Finish a compound-literal expression. TYPE is the type to which
1593 the INITIALIZER_LIST is being cast. */
1595 tree
1596 finish_compound_literal (type, initializer_list)
1597 tree type;
1598 tree initializer_list;
1600 tree compound_literal;
1602 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1603 compound_literal = build_nt (CONSTRUCTOR, NULL_TREE,
1604 initializer_list);
1605 /* Mark it as a compound-literal. */
1606 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1607 if (processing_template_decl)
1608 TREE_TYPE (compound_literal) = type;
1609 else
1611 /* Check the initialization. */
1612 compound_literal = digest_init (type, compound_literal, NULL);
1613 /* If the TYPE was an array type with an unknown bound, then we can
1614 figure out the dimension now. For example, something like:
1616 `(int []) { 2, 3 }'
1618 implies that the array has two elements. */
1619 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1620 complete_array_type (type, compound_literal, 1);
1623 return compound_literal;
1626 /* Return the declaration for the function-name variable indicated by
1627 ID. */
1629 tree
1630 finish_fname (tree id)
1632 tree decl;
1634 decl = fname_decl (C_RID_CODE (id), id);
1635 if (processing_template_decl)
1636 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1637 return decl;
1640 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1641 and DECLARATOR. Returns nonzero if the function-declaration is
1642 valid. */
1645 begin_function_definition (decl_specs, attributes, declarator)
1646 tree decl_specs;
1647 tree attributes;
1648 tree declarator;
1650 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1651 return 0;
1653 /* The things we're about to see are not directly qualified by any
1654 template headers we've seen thus far. */
1655 reset_specialization ();
1657 return 1;
1660 /* Finish an init-declarator. Returns a DECL. */
1662 tree
1663 finish_declarator (declarator, declspecs, attributes,
1664 prefix_attributes, initialized)
1665 tree declarator;
1666 tree declspecs;
1667 tree attributes;
1668 tree prefix_attributes;
1669 int initialized;
1671 return start_decl (declarator, declspecs, initialized, attributes,
1672 prefix_attributes);
1675 /* Finish a translation unit. */
1677 void
1678 finish_translation_unit ()
1680 /* In case there were missing closebraces,
1681 get us back to the global binding level. */
1682 pop_everything ();
1683 while (current_namespace != global_namespace)
1684 pop_namespace ();
1686 /* Do file scope __FUNCTION__ et al. */
1687 finish_fname_decls ();
1690 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1691 Returns the parameter. */
1693 tree
1694 finish_template_type_parm (aggr, identifier)
1695 tree aggr;
1696 tree identifier;
1698 if (aggr != class_type_node)
1700 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1701 aggr = class_type_node;
1704 return build_tree_list (aggr, identifier);
1707 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1708 Returns the parameter. */
1710 tree
1711 finish_template_template_parm (aggr, identifier)
1712 tree aggr;
1713 tree identifier;
1715 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1716 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1717 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1718 DECL_TEMPLATE_RESULT (tmpl) = decl;
1719 DECL_ARTIFICIAL (decl) = 1;
1720 end_template_decl ();
1722 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1724 return finish_template_type_parm (aggr, tmpl);
1727 /* ARGUMENT is the default-argument value for a template template
1728 parameter. If ARGUMENT is invalid, issue error messages and return
1729 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1731 tree
1732 check_template_template_default_arg (tree argument)
1734 if (TREE_CODE (argument) != TEMPLATE_DECL
1735 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1736 && TREE_CODE (argument) != TYPE_DECL
1737 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1739 error ("invalid default template argument");
1740 return error_mark_node;
1743 return argument;
1746 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1747 nonzero, the parameter list was terminated by a `...'. */
1749 tree
1750 finish_parmlist (parms, ellipsis)
1751 tree parms;
1752 int ellipsis;
1754 if (parms)
1756 /* We mark the PARMS as a parmlist so that declarator processing can
1757 disambiguate certain constructs. */
1758 TREE_PARMLIST (parms) = 1;
1759 /* We do not append void_list_node here, but leave it to grokparms
1760 to do that. */
1761 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1763 return parms;
1766 /* Begin a class definition, as indicated by T. */
1768 tree
1769 begin_class_definition (t)
1770 tree t;
1772 if (t == error_mark_node)
1773 return error_mark_node;
1775 if (processing_template_parmlist)
1777 error ("definition of `%#T' inside template parameter list", t);
1778 return error_mark_node;
1780 /* A non-implicit typename comes from code like:
1782 template <typename T> struct A {
1783 template <typename U> struct A<T>::B ...
1785 This is erroneous. */
1786 else if (TREE_CODE (t) == TYPENAME_TYPE)
1788 error ("invalid definition of qualified type `%T'", t);
1789 t = error_mark_node;
1792 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1794 t = make_aggr_type (RECORD_TYPE);
1795 pushtag (make_anon_name (), t, 0);
1798 /* If this type was already complete, and we see another definition,
1799 that's an error. */
1800 if (COMPLETE_TYPE_P (t))
1801 duplicate_tag_error (t);
1803 /* Update the location of the decl. */
1804 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1805 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1807 if (TYPE_BEING_DEFINED (t))
1809 t = make_aggr_type (TREE_CODE (t));
1810 pushtag (TYPE_IDENTIFIER (t), t, 0);
1812 maybe_process_partial_specialization (t);
1813 pushclass (t, true);
1814 TYPE_BEING_DEFINED (t) = 1;
1815 TYPE_PACKED (t) = flag_pack_struct;
1816 /* Reset the interface data, at the earliest possible
1817 moment, as it might have been set via a class foo;
1818 before. */
1819 if (! TYPE_ANONYMOUS_P (t))
1821 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1822 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1823 (t, interface_unknown);
1825 reset_specialization();
1827 /* Make a declaration for this class in its own scope. */
1828 build_self_reference ();
1830 return t;
1833 /* Finish the member declaration given by DECL. */
1835 void
1836 finish_member_declaration (decl)
1837 tree decl;
1839 if (decl == error_mark_node || decl == NULL_TREE)
1840 return;
1842 if (decl == void_type_node)
1843 /* The COMPONENT was a friend, not a member, and so there's
1844 nothing for us to do. */
1845 return;
1847 /* We should see only one DECL at a time. */
1848 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1850 /* Set up access control for DECL. */
1851 TREE_PRIVATE (decl)
1852 = (current_access_specifier == access_private_node);
1853 TREE_PROTECTED (decl)
1854 = (current_access_specifier == access_protected_node);
1855 if (TREE_CODE (decl) == TEMPLATE_DECL)
1857 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1858 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1861 /* Mark the DECL as a member of the current class. */
1862 DECL_CONTEXT (decl) = current_class_type;
1864 /* [dcl.link]
1866 A C language linkage is ignored for the names of class members
1867 and the member function type of class member functions. */
1868 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1869 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1871 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1873 /* Put functions on the TYPE_METHODS list and everything else on the
1874 TYPE_FIELDS list. Note that these are built up in reverse order.
1875 We reverse them (to obtain declaration order) in finish_struct. */
1876 if (TREE_CODE (decl) == FUNCTION_DECL
1877 || DECL_FUNCTION_TEMPLATE_P (decl))
1879 /* We also need to add this function to the
1880 CLASSTYPE_METHOD_VEC. */
1881 add_method (current_class_type, decl, /*error_p=*/0);
1883 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1884 TYPE_METHODS (current_class_type) = decl;
1886 else
1888 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1889 go at the beginning. The reason is that lookup_field_1
1890 searches the list in order, and we want a field name to
1891 override a type name so that the "struct stat hack" will
1892 work. In particular:
1894 struct S { enum E { }; int E } s;
1895 s.E = 3;
1897 is valid. In addition, the FIELD_DECLs must be maintained in
1898 declaration order so that class layout works as expected.
1899 However, we don't need that order until class layout, so we
1900 save a little time by putting FIELD_DECLs on in reverse order
1901 here, and then reversing them in finish_struct_1. (We could
1902 also keep a pointer to the correct insertion points in the
1903 list.) */
1905 if (TREE_CODE (decl) == TYPE_DECL)
1906 TYPE_FIELDS (current_class_type)
1907 = chainon (TYPE_FIELDS (current_class_type), decl);
1908 else
1910 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1911 TYPE_FIELDS (current_class_type) = decl;
1914 /* Enter the DECL into the scope of the class. */
1915 if (TREE_CODE (decl) != USING_DECL)
1916 pushdecl_class_level (decl);
1920 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1921 the definition is immediately followed by a semicolon. Returns the
1922 type. */
1924 tree
1925 finish_class_definition (t, attributes, semi, pop_scope_p)
1926 tree t;
1927 tree attributes;
1928 int semi;
1929 int pop_scope_p;
1931 if (t == error_mark_node)
1932 return error_mark_node;
1934 /* finish_struct nukes this anyway; if finish_exception does too,
1935 then it can go. */
1936 if (semi)
1937 note_got_semicolon (t);
1939 /* If we got any attributes in class_head, xref_tag will stick them in
1940 TREE_TYPE of the type. Grab them now. */
1941 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1942 TYPE_ATTRIBUTES (t) = NULL_TREE;
1944 if (TREE_CODE (t) == ENUMERAL_TYPE)
1946 else
1948 t = finish_struct (t, attributes);
1949 if (semi)
1950 note_got_semicolon (t);
1953 if (pop_scope_p)
1954 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1956 return t;
1959 /* Finish processing the declaration of a member class template
1960 TYPES whose template parameters are given by PARMS. */
1962 tree
1963 finish_member_class_template (types)
1964 tree types;
1966 tree t;
1968 /* If there are declared, but undefined, partial specializations
1969 mixed in with the typespecs they will not yet have passed through
1970 maybe_process_partial_specialization, so we do that here. */
1971 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1972 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1973 maybe_process_partial_specialization (TREE_VALUE (t));
1975 note_list_got_semicolon (types);
1976 grok_x_components (types);
1977 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1978 /* The component was in fact a friend declaration. We avoid
1979 finish_member_template_decl performing certain checks by
1980 unsetting TYPES. */
1981 types = NULL_TREE;
1983 finish_member_template_decl (types);
1985 /* As with other component type declarations, we do
1986 not store the new DECL on the list of
1987 component_decls. */
1988 return NULL_TREE;
1991 /* Finish processing a complete template declaration. The PARMS are
1992 the template parameters. */
1994 void
1995 finish_template_decl (parms)
1996 tree parms;
1998 if (parms)
1999 end_template_decl ();
2000 else
2001 end_specialization ();
2004 /* Finish processing a template-id (which names a type) of the form
2005 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2006 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2007 the scope of template-id indicated. */
2009 tree
2010 finish_template_type (name, args, entering_scope)
2011 tree name;
2012 tree args;
2013 int entering_scope;
2015 tree decl;
2017 decl = lookup_template_class (name, args,
2018 NULL_TREE, NULL_TREE,
2019 entering_scope, /*complain=*/1);
2020 if (decl != error_mark_node)
2021 decl = TYPE_STUB_DECL (decl);
2023 return decl;
2026 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2027 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2028 BASE_CLASS, or NULL_TREE if an error occurred. The
2029 ACCESS_SPECIFIER is one of
2030 access_{default,public,protected_private}[_virtual]_node.*/
2032 tree
2033 finish_base_specifier (tree base, tree access, bool virtual_p)
2035 tree result;
2037 if (base == error_mark_node)
2039 error ("invalid base-class specification");
2040 result = NULL_TREE;
2042 else if (! is_aggr_type (base, 1))
2043 result = NULL_TREE;
2044 else
2046 if (cp_type_quals (base) != 0)
2048 error ("base class `%T' has cv qualifiers", base);
2049 base = TYPE_MAIN_VARIANT (base);
2051 result = build_tree_list (access, base);
2052 TREE_VIA_VIRTUAL (result) = virtual_p;
2055 return result;
2058 /* Called when multiple declarators are processed. If that is not
2059 premitted in this context, an error is issued. */
2061 void
2062 check_multiple_declarators ()
2064 /* [temp]
2066 In a template-declaration, explicit specialization, or explicit
2067 instantiation the init-declarator-list in the declaration shall
2068 contain at most one declarator.
2070 We don't just use PROCESSING_TEMPLATE_DECL for the first
2071 condition since that would disallow the perfectly valid code,
2072 like `template <class T> struct S { int i, j; };'. */
2073 if (at_function_scope_p ())
2074 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2075 return;
2077 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2078 || processing_explicit_instantiation
2079 || processing_specialization)
2080 error ("multiple declarators in template declaration");
2083 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2084 use as a type-specifier. */
2086 tree
2087 finish_typeof (expr)
2088 tree expr;
2090 tree type;
2092 if (processing_template_decl)
2094 type = make_aggr_type (TYPEOF_TYPE);
2095 TYPE_FIELDS (type) = expr;
2097 return type;
2100 if (TREE_CODE (expr) == OFFSET_REF)
2101 expr = resolve_offset_ref (expr);
2103 type = TREE_TYPE (expr);
2105 if (!type || type == unknown_type_node)
2107 error ("type of `%E' is unknown", expr);
2108 return error_mark_node;
2111 return type;
2114 /* Compute the value of the `sizeof' operator. */
2116 tree
2117 finish_sizeof (t)
2118 tree t;
2120 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2123 /* Implement the __alignof keyword: Return the minimum required
2124 alignment of T, measured in bytes. */
2126 tree
2127 finish_alignof (t)
2128 tree t;
2130 if (processing_template_decl)
2131 return build_min (ALIGNOF_EXPR, size_type_node, t);
2133 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2136 /* Generate RTL for the statement T, and its substatements, and any
2137 other statements at its nesting level. */
2139 static void
2140 cp_expand_stmt (t)
2141 tree t;
2143 switch (TREE_CODE (t))
2145 case TRY_BLOCK:
2146 genrtl_try_block (t);
2147 break;
2149 case EH_SPEC_BLOCK:
2150 genrtl_eh_spec_block (t);
2151 break;
2153 case HANDLER:
2154 genrtl_handler (t);
2155 break;
2157 case USING_STMT:
2158 break;
2160 default:
2161 abort ();
2162 break;
2166 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2167 will equivalent CALL_EXPRs. */
2169 static tree
2170 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2171 tree *tp;
2172 int *walk_subtrees ATTRIBUTE_UNUSED;
2173 void *data ATTRIBUTE_UNUSED;
2175 tree aggr_init_expr;
2176 tree call_expr;
2177 tree fn;
2178 tree args;
2179 tree slot;
2180 tree type;
2181 enum style_t { ctor, arg, pcc } style;
2183 aggr_init_expr = *tp;
2184 /* We don't need to walk into types; there's nothing in a type that
2185 needs simplification. (And, furthermore, there are places we
2186 actively don't want to go. For example, we don't want to wander
2187 into the default arguments for a FUNCTION_DECL that appears in a
2188 CALL_EXPR.) */
2189 if (TYPE_P (aggr_init_expr))
2191 *walk_subtrees = 0;
2192 return NULL_TREE;
2194 /* Only AGGR_INIT_EXPRs are interesting. */
2195 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2196 return NULL_TREE;
2198 /* Form an appropriate CALL_EXPR. */
2199 fn = TREE_OPERAND (aggr_init_expr, 0);
2200 args = TREE_OPERAND (aggr_init_expr, 1);
2201 slot = TREE_OPERAND (aggr_init_expr, 2);
2202 type = TREE_TYPE (aggr_init_expr);
2204 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2205 style = ctor;
2206 #ifdef PCC_STATIC_STRUCT_RETURN
2207 else if (1)
2208 style = pcc;
2209 #endif
2210 else if (TREE_ADDRESSABLE (type))
2211 style = arg;
2212 else
2213 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2214 handling. See build_cplus_new. */
2215 abort ();
2217 if (style == ctor || style == arg)
2219 /* Pass the address of the slot. If this is a constructor, we
2220 replace the first argument; otherwise, we tack on a new one. */
2221 if (style == ctor)
2222 args = TREE_CHAIN (args);
2224 cxx_mark_addressable (slot);
2225 args = tree_cons (NULL_TREE,
2226 build1 (ADDR_EXPR,
2227 build_pointer_type (TREE_TYPE (slot)),
2228 slot),
2229 args);
2232 call_expr = build (CALL_EXPR,
2233 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2234 fn, args, NULL_TREE);
2235 TREE_SIDE_EFFECTS (call_expr) = 1;
2237 if (style == arg)
2238 /* Tell the backend that we've added our return slot to the argument
2239 list. */
2240 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2241 else if (style == pcc)
2243 /* If we're using the non-reentrant PCC calling convention, then we
2244 need to copy the returned value out of the static buffer into the
2245 SLOT. */
2246 int old_ac = flag_access_control;
2248 flag_access_control = 0;
2249 call_expr = build_aggr_init (slot, call_expr,
2250 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2251 flag_access_control = old_ac;
2254 /* We want to use the value of the initialized location as the
2255 result. */
2256 call_expr = build (COMPOUND_EXPR, type,
2257 call_expr, slot);
2259 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2260 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2261 *tp = call_expr;
2263 /* Keep iterating. */
2264 return NULL_TREE;
2267 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2269 static void
2270 emit_associated_thunks (fn)
2271 tree fn;
2273 /* When we use vcall offsets, we emit thunks with the virtual
2274 functions to which they thunk. The whole point of vcall offsets
2275 is so that you can know statically the entire set of thunks that
2276 will ever be needed for a given virtual function, thereby
2277 enabling you to output all the thunks with the function itself. */
2278 if (DECL_VIRTUAL_P (fn))
2280 tree thunk;
2282 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2284 use_thunk (thunk, /*emit_p=*/1);
2285 if (DECL_RESULT_THUNK_P (thunk))
2287 tree probe;
2289 for (probe = DECL_THUNKS (thunk);
2290 probe; probe = TREE_CHAIN (probe))
2291 use_thunk (probe, /*emit_p=*/1);
2297 /* Generate RTL for FN. */
2299 void
2300 expand_body (fn)
2301 tree fn;
2303 int saved_lineno;
2304 const char *saved_input_filename;
2305 tree saved_function;
2307 /* When the parser calls us after finishing the body of a template
2308 function, we don't really want to expand the body. When we're
2309 processing an in-class definition of an inline function,
2310 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2311 to look at the function itself. */
2312 if (processing_template_decl
2313 || (DECL_LANG_SPECIFIC (fn)
2314 && DECL_TEMPLATE_INFO (fn)
2315 && uses_template_parms (DECL_TI_ARGS (fn))))
2317 /* Normally, collection only occurs in rest_of_compilation. So,
2318 if we don't collect here, we never collect junk generated
2319 during the processing of templates until we hit a
2320 non-template function. */
2321 ggc_collect ();
2322 return;
2325 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2326 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2327 simplify_aggr_init_exprs_r,
2328 NULL);
2330 /* If this is a constructor or destructor body, we have to clone
2331 it. */
2332 if (maybe_clone_body (fn))
2334 /* We don't want to process FN again, so pretend we've written
2335 it out, even though we haven't. */
2336 TREE_ASM_WRITTEN (fn) = 1;
2337 return;
2340 /* There's no reason to do any of the work here if we're only doing
2341 semantic analysis; this code just generates RTL. */
2342 if (flag_syntax_only)
2343 return;
2345 /* If possible, avoid generating RTL for this function. Instead,
2346 just record it as an inline function, and wait until end-of-file
2347 to decide whether to write it out or not. */
2348 if (/* We have to generate RTL if it's not an inline function. */
2349 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2350 /* Or if we have to emit code for inline functions anyhow. */
2351 && !flag_keep_inline_functions
2352 /* Or if we actually have a reference to the function. */
2353 && !DECL_NEEDED_P (fn))
2355 /* Set DECL_EXTERNAL so that assemble_external will be called as
2356 necessary. We'll clear it again in finish_file. */
2357 if (!DECL_EXTERNAL (fn))
2359 DECL_NOT_REALLY_EXTERN (fn) = 1;
2360 DECL_EXTERNAL (fn) = 1;
2362 /* Remember this function. In finish_file we'll decide if
2363 we actually need to write this function out. */
2364 defer_fn (fn);
2365 /* Let the back-end know that this function exists. */
2366 (*debug_hooks->deferred_inline_function) (fn);
2367 return;
2370 /* Compute the appropriate object-file linkage for inline
2371 functions. */
2372 if (DECL_DECLARED_INLINE_P (fn))
2373 import_export_decl (fn);
2375 /* If FN is external, then there's no point in generating RTL for
2376 it. This situation can arise with an inline function under
2377 `-fexternal-templates'; we instantiate the function, even though
2378 we're not planning on emitting it, in case we get a chance to
2379 inline it. */
2380 if (DECL_EXTERNAL (fn))
2381 return;
2383 /* Save the current file name and line number. When we expand the
2384 body of the function, we'll set LINENO and INPUT_FILENAME so that
2385 error-mesages come out in the right places. */
2386 saved_lineno = lineno;
2387 saved_input_filename = input_filename;
2388 saved_function = current_function_decl;
2389 lineno = DECL_SOURCE_LINE (fn);
2390 input_filename = DECL_SOURCE_FILE (fn);
2391 current_function_decl = fn;
2393 timevar_push (TV_INTEGRATION);
2395 /* Optimize the body of the function before expanding it. */
2396 optimize_function (fn);
2398 timevar_pop (TV_INTEGRATION);
2399 timevar_push (TV_EXPAND);
2401 genrtl_start_function (fn);
2402 current_function_is_thunk = DECL_THUNK_P (fn);
2404 /* Expand the body. */
2405 expand_stmt (DECL_SAVED_TREE (fn));
2407 /* Statements should always be full-expressions at the outermost set
2408 of curly braces for a function. */
2409 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2411 /* The outermost statement for a function contains the line number
2412 recorded when we finished processing the function. */
2413 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2415 /* Generate code for the function. */
2416 genrtl_finish_function (fn);
2418 /* If possible, obliterate the body of the function so that it can
2419 be garbage collected. */
2420 if (dump_enabled_p (TDI_all))
2421 /* Keep the body; we're going to dump it. */
2423 else if (DECL_INLINE (fn) && flag_inline_trees)
2424 /* We might need the body of this function so that we can expand
2425 it inline somewhere else. */
2427 else
2428 /* We don't need the body; blow it away. */
2429 DECL_SAVED_TREE (fn) = NULL_TREE;
2431 /* And restore the current source position. */
2432 current_function_decl = saved_function;
2433 lineno = saved_lineno;
2434 input_filename = saved_input_filename;
2435 extract_interface_info ();
2437 timevar_pop (TV_EXPAND);
2439 /* Emit any thunks that should be emitted at the same time as FN. */
2440 emit_associated_thunks (fn);
2443 /* Helper function for walk_tree, used by finish_function to override all
2444 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2445 value optimization. */
2447 tree
2448 nullify_returns_r (tp, walk_subtrees, data)
2449 tree *tp;
2450 int *walk_subtrees;
2451 void *data;
2453 tree nrv = (tree) data;
2455 /* No need to walk into types. There wouldn't be any need to walk into
2456 non-statements, except that we have to consider STMT_EXPRs. */
2457 if (TYPE_P (*tp))
2458 *walk_subtrees = 0;
2459 else if (TREE_CODE (*tp) == RETURN_STMT)
2460 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2461 else if (TREE_CODE (*tp) == CLEANUP_STMT
2462 && CLEANUP_DECL (*tp) == nrv)
2463 CLEANUP_EH_ONLY (*tp) = 1;
2465 /* Keep iterating. */
2466 return NULL_TREE;
2469 /* Start generating the RTL for FN. */
2471 static void
2472 genrtl_start_function (fn)
2473 tree fn;
2475 /* Tell everybody what function we're processing. */
2476 current_function_decl = fn;
2477 /* Get the RTL machinery going for this function. */
2478 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2479 /* Let everybody know that we're expanding this function, not doing
2480 semantic analysis. */
2481 expanding_p = 1;
2483 /* Even though we're inside a function body, we still don't want to
2484 call expand_expr to calculate the size of a variable-sized array.
2485 We haven't necessarily assigned RTL to all variables yet, so it's
2486 not safe to try to expand expressions involving them. */
2487 immediate_size_expand = 0;
2488 cfun->x_dont_save_pending_sizes_p = 1;
2490 /* Let the user know we're compiling this function. */
2491 announce_function (fn);
2493 /* Initialize the per-function data. */
2494 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2495 if (DECL_SAVED_FUNCTION_DATA (fn))
2497 /* If we already parsed this function, and we're just expanding it
2498 now, restore saved state. */
2499 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2501 /* This function is being processed in whole-function mode; we
2502 already did semantic analysis. */
2503 cfun->x_whole_function_mode_p = 1;
2505 /* If we decided that we didn't want to inline this function,
2506 make sure the back-end knows that. */
2507 if (!current_function_cannot_inline)
2508 current_function_cannot_inline = cp_function_chain->cannot_inline;
2510 /* We don't need the saved data anymore. Unless this is an inline
2511 function; we need the named return value info for
2512 cp_copy_res_decl_for_inlining. */
2513 if (! DECL_INLINE (fn))
2514 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2517 /* Keep track of how many functions we're presently expanding. */
2518 ++function_depth;
2520 /* Create a binding level for the parameters. */
2521 expand_function_start (fn, /*parms_have_cleanups=*/0);
2522 /* If this function is `main'. */
2523 if (DECL_MAIN_P (fn))
2524 expand_main_function ();
2526 /* Give our named return value the same RTL as our RESULT_DECL. */
2527 if (current_function_return_value)
2528 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2531 /* Finish generating the RTL for FN. */
2533 static void
2534 genrtl_finish_function (fn)
2535 tree fn;
2537 tree t;
2539 #if 0
2540 if (write_symbols != NO_DEBUG)
2542 /* Keep this code around in case we later want to control debug info
2543 based on whether a type is "used". (jason 1999-11-11) */
2545 tree ttype = target_type (fntype);
2546 tree parmdecl;
2548 if (IS_AGGR_TYPE (ttype))
2549 /* Let debugger know it should output info for this type. */
2550 note_debug_info_needed (ttype);
2552 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2554 ttype = target_type (TREE_TYPE (parmdecl));
2555 if (IS_AGGR_TYPE (ttype))
2556 /* Let debugger know it should output info for this type. */
2557 note_debug_info_needed (ttype);
2560 #endif
2562 /* Clean house because we will need to reorder insns here. */
2563 do_pending_stack_adjust ();
2565 /* If we have a named return value, we need to force a return so that
2566 the return register is USEd. */
2567 if (DECL_NAME (DECL_RESULT (fn)))
2568 emit_jump (return_label);
2570 /* We hard-wired immediate_size_expand to zero in start_function.
2571 Expand_function_end will decrement this variable. So, we set the
2572 variable to one here, so that after the decrement it will remain
2573 zero. */
2574 immediate_size_expand = 1;
2576 /* Generate rtl for function exit. */
2577 expand_function_end (input_filename, lineno, 0);
2579 /* If this is a nested function (like a template instantiation that
2580 we're compiling in the midst of compiling something else), push a
2581 new GC context. That will keep local variables on the stack from
2582 being collected while we're doing the compilation of this
2583 function. */
2584 if (function_depth > 1)
2585 ggc_push_context ();
2587 /* There's no need to defer outputting this function any more; we
2588 know we want to output it. */
2589 DECL_DEFER_OUTPUT (fn) = 0;
2591 /* Run the optimizers and output the assembler code for this
2592 function. */
2593 rest_of_compilation (fn);
2595 /* Undo the call to ggc_push_context above. */
2596 if (function_depth > 1)
2597 ggc_pop_context ();
2599 #if 0
2600 /* Keep this code around in case we later want to control debug info
2601 based on whether a type is "used". (jason 1999-11-11) */
2603 if (ctype && TREE_ASM_WRITTEN (fn))
2604 note_debug_info_needed (ctype);
2605 #endif
2607 /* If this function is marked with the constructor attribute, add it
2608 to the list of functions to be called along with constructors
2609 from static duration objects. */
2610 if (DECL_STATIC_CONSTRUCTOR (fn))
2611 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2613 /* If this function is marked with the destructor attribute, add it
2614 to the list of functions to be called along with destructors from
2615 static duration objects. */
2616 if (DECL_STATIC_DESTRUCTOR (fn))
2617 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2619 --function_depth;
2621 /* In C++, we should never be saving RTL for the function. */
2622 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2624 /* Since we don't need the RTL for this function anymore, stop
2625 pointing to it. That's especially important for LABEL_DECLs,
2626 since you can reach all the instructions in the function from the
2627 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2628 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2629 local variables. */
2630 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2631 clear_decl_rtl,
2632 NULL);
2634 /* Clear out the RTL for the arguments. */
2635 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2637 SET_DECL_RTL (t, NULL_RTX);
2638 DECL_INCOMING_RTL (t) = NULL_RTX;
2641 if (!(flag_inline_trees && DECL_INLINE (fn)))
2642 /* DECL_INITIAL must remain nonzero so we know this was an
2643 actual function definition. */
2644 DECL_INITIAL (fn) = error_mark_node;
2646 /* Let the error reporting routines know that we're outside a
2647 function. For a nested function, this value is used in
2648 pop_cp_function_context and then reset via pop_function_context. */
2649 current_function_decl = NULL_TREE;
2652 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2653 its sub-blocks. */
2655 static tree
2656 clear_decl_rtl (tp, walk_subtrees, data)
2657 tree *tp;
2658 int *walk_subtrees ATTRIBUTE_UNUSED;
2659 void *data ATTRIBUTE_UNUSED;
2661 if (nonstatic_local_decl_p (*tp))
2662 SET_DECL_RTL (*tp, NULL_RTX);
2664 return NULL_TREE;
2667 /* Perform initialization related to this module. */
2669 void
2670 init_cp_semantics ()
2672 lang_expand_stmt = cp_expand_stmt;
2675 #include "gt-cp-semantics.h"