(read_braced_string): Check for EOF. If encountered issue an error message.
[official-gcc.git] / gcc / cp / semantics.c
blobbb55b75af88b592e754d9ff52760a88bc9fd51d2
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 (tree);
55 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
56 static void emit_associated_thunks (tree);
57 static void genrtl_try_block (tree);
58 static void genrtl_eh_spec_block (tree);
59 static void genrtl_handler (tree);
60 static void cp_expand_stmt (tree);
61 static void genrtl_start_function (tree);
62 static void genrtl_finish_function (tree);
63 static tree clear_decl_rtl (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 /* Deferred Access Checking Overview
83 ---------------------------------
85 Most C++ expressions and declarations require access checking
86 to be performed during parsing. However, in several cases,
87 this has to be treated differently.
89 For member declarations, access checking has to be deferred
90 until more information about the declaration is known. For
91 example:
93 class A {
94 typedef int X;
95 public:
96 X f();
99 A::X A::f();
100 A::X g();
102 When we are parsing the function return type `A::X', we don't
103 really know if this is allowed until we parse the function name.
105 Furthermore, some contexts require that access checking is
106 never performed at all. These include class heads, and template
107 instantiations.
109 Typical use of access checking functions is described here:
111 1. When we enter a context that requires certain access checking
112 mode, the function `push_deferring_access_checks' is called with
113 DEFERRING argument specifying the desired mode. Access checking
114 may be performed immediately (dk_no_deferred), deferred
115 (dk_deferred), or not performed (dk_no_check).
117 2. When a declaration such as a type, or a variable, is encountered,
118 the function `perform_or_defer_access_check' is called. It
119 maintains a TREE_LIST of all deferred checks.
121 3. The global `current_class_type' or `current_function_decl' is then
122 setup by the parser. `enforce_access' relies on these information
123 to check access.
125 4. Upon exiting the context mentioned in step 1,
126 `perform_deferred_access_checks' is called to check all declaration
127 stored in the TREE_LIST. `pop_deferring_access_checks' is then
128 called to restore the previous access checking mode.
130 In case of parsing error, we simply call `pop_deferring_access_checks'
131 without `perform_deferred_access_checks'. */
133 /* Data for deferred access checking. */
134 static GTY(()) deferred_access *deferred_access_stack;
135 static GTY(()) deferred_access *deferred_access_free_list;
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
140 void push_deferring_access_checks (deferring_kind deferring)
142 deferred_access *d;
144 /* For context like template instantiation, access checking
145 disabling applies to all nested context. */
146 if (deferred_access_stack
147 && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
148 deferring = dk_no_check;
150 /* Recycle previously used free store if available. */
151 if (deferred_access_free_list)
153 d = deferred_access_free_list;
154 deferred_access_free_list = d->next;
156 else
157 d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
159 d->next = deferred_access_stack;
160 d->deferred_access_checks = NULL_TREE;
161 d->deferring_access_checks_kind = deferring;
162 deferred_access_stack = d;
165 /* Resume deferring access checks again after we stopped doing
166 this previously. */
168 void resume_deferring_access_checks (void)
170 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
171 deferred_access_stack->deferring_access_checks_kind = dk_deferred;
174 /* Stop deferring access checks. */
176 void stop_deferring_access_checks (void)
178 if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
179 deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
182 /* Discard the current deferred access checks and restore the
183 previous states. */
185 void pop_deferring_access_checks (void)
187 deferred_access *d = deferred_access_stack;
188 deferred_access_stack = d->next;
190 /* Remove references to access checks TREE_LIST. */
191 d->deferred_access_checks = NULL_TREE;
193 /* Store in free list for later use. */
194 d->next = deferred_access_free_list;
195 deferred_access_free_list = d;
198 /* Returns a TREE_LIST representing the deferred checks.
199 The TREE_PURPOSE of each node is the type through which the
200 access occurred; the TREE_VALUE is the declaration named.
203 tree get_deferred_access_checks (void)
205 return deferred_access_stack->deferred_access_checks;
208 /* Take current deferred checks and combine with the
209 previous states if we also defer checks previously.
210 Otherwise perform checks now. */
212 void pop_to_parent_deferring_access_checks (void)
214 tree deferred_check = get_deferred_access_checks ();
215 deferred_access *d1 = deferred_access_stack;
216 deferred_access *d2 = deferred_access_stack->next;
217 deferred_access *d3 = deferred_access_stack->next->next;
219 /* Temporary swap the order of the top two states, just to make
220 sure the garbage collector will not reclaim the memory during
221 processing below. */
222 deferred_access_stack = d2;
223 d2->next = d1;
224 d1->next = d3;
226 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
227 /* Perform deferred check if required. */
228 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
229 TREE_VALUE (deferred_check));
231 deferred_access_stack = d1;
232 d1->next = d2;
233 d2->next = d3;
234 pop_deferring_access_checks ();
237 /* Perform the deferred access checks.
239 After performing the checks, we still have to keep the list
240 `deferred_access_stack->deferred_access_checks' since we may want
241 to check access for them again later in a different context.
242 For example:
244 class A {
245 typedef int X;
246 static X a;
248 A::X A::a, x; // No error for `A::a', error for `x'
250 We have to perform deferred access of `A::X', first with `A::a',
251 next with `x'. */
253 void perform_deferred_access_checks (void)
255 tree deferred_check;
256 for (deferred_check = deferred_access_stack->deferred_access_checks;
257 deferred_check;
258 deferred_check = TREE_CHAIN (deferred_check))
259 /* Check access. */
260 enforce_access (TREE_PURPOSE (deferred_check),
261 TREE_VALUE (deferred_check));
264 /* Defer checking the accessibility of DECL, when looked up in
265 CLASS_TYPE. */
267 void perform_or_defer_access_check (tree class_type, tree decl)
269 tree check;
271 /* If we are not supposed to defer access checks, just check now. */
272 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
274 enforce_access (class_type, decl);
275 return;
277 /* Exit if we are in a context that no access checking is performed. */
278 else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
279 return;
281 /* See if we are already going to perform this check. */
282 for (check = deferred_access_stack->deferred_access_checks;
283 check;
284 check = TREE_CHAIN (check))
285 if (TREE_VALUE (check) == decl
286 && TYPE_P (TREE_PURPOSE (check))
287 && same_type_p (TREE_PURPOSE (check), class_type))
288 return;
289 /* If not, record the check. */
290 deferred_access_stack->deferred_access_checks
291 = tree_cons (class_type, decl,
292 deferred_access_stack->deferred_access_checks);
295 /* Returns nonzero if the current statement is a full expression,
296 i.e. temporaries created during that statement should be destroyed
297 at the end of the statement. */
300 stmts_are_full_exprs_p (void)
302 return current_stmt_tree ()->stmts_are_full_exprs_p;
305 /* Returns the stmt_tree (if any) to which statements are currently
306 being added. If there is no active statement-tree, NULL is
307 returned. */
309 stmt_tree
310 current_stmt_tree (void)
312 return (cfun
313 ? &cfun->language->base.x_stmt_tree
314 : &scope_chain->x_stmt_tree);
317 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
318 flag for this because "A union for which objects or pointers are
319 declared is not an anonymous union" [class.union]. */
322 anon_aggr_type_p (tree node)
324 return ANON_AGGR_TYPE_P (node);
327 /* Finish a scope. */
329 tree
330 do_poplevel (void)
332 tree block = NULL_TREE;
334 if (stmts_are_full_exprs_p ())
336 tree scope_stmts = NULL_TREE;
338 block = poplevel (kept_level_p (), 1, 0);
339 if (!processing_template_decl)
341 /* This needs to come after the poplevel so that partial scopes
342 are properly nested. */
343 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
344 if (block)
346 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
347 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
352 return block;
355 /* Begin a new scope. */
357 void
358 do_pushlevel (scope_kind sk)
360 if (stmts_are_full_exprs_p ())
362 if (!processing_template_decl)
363 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
364 begin_scope (sk);
368 /* Finish a goto-statement. */
370 tree
371 finish_goto_stmt (tree destination)
373 if (TREE_CODE (destination) == IDENTIFIER_NODE)
374 destination = lookup_label (destination);
376 /* We warn about unused labels with -Wunused. That means we have to
377 mark the used labels as used. */
378 if (TREE_CODE (destination) == LABEL_DECL)
379 TREE_USED (destination) = 1;
381 if (TREE_CODE (destination) != LABEL_DECL)
382 /* We don't inline calls to functions with computed gotos.
383 Those functions are typically up to some funny business,
384 and may be depending on the labels being at particular
385 addresses, or some such. */
386 DECL_UNINLINABLE (current_function_decl) = 1;
388 check_goto (destination);
390 return add_stmt (build_stmt (GOTO_STMT, destination));
393 /* COND is the condition-expression for an if, while, etc.,
394 statement. Convert it to a boolean value, if appropriate. */
396 tree
397 maybe_convert_cond (tree cond)
399 /* Empty conditions remain empty. */
400 if (!cond)
401 return NULL_TREE;
403 /* Wait until we instantiate templates before doing conversion. */
404 if (processing_template_decl)
405 return cond;
407 /* Do the conversion. */
408 cond = convert_from_reference (cond);
409 return condition_conversion (cond);
412 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
414 tree
415 finish_expr_stmt (tree expr)
417 tree r = NULL_TREE;
418 tree expr_type = NULL_TREE;;
420 if (expr != NULL_TREE)
422 if (!processing_template_decl
423 && !(stmts_are_full_exprs_p ())
424 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
425 && lvalue_p (expr))
426 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
427 expr = default_conversion (expr);
429 /* Remember the type of the expression. */
430 expr_type = TREE_TYPE (expr);
432 if (stmts_are_full_exprs_p ())
433 expr = convert_to_void (expr, "statement");
435 r = add_stmt (build_stmt (EXPR_STMT, expr));
438 finish_stmt ();
440 /* This was an expression-statement, so we save the type of the
441 expression. */
442 last_expr_type = expr_type;
444 return r;
448 /* Begin an if-statement. Returns a newly created IF_STMT if
449 appropriate. */
451 tree
452 begin_if_stmt (void)
454 tree r;
455 do_pushlevel (sk_block);
456 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
457 add_stmt (r);
458 return r;
461 /* Process the COND of an if-statement, which may be given by
462 IF_STMT. */
464 void
465 finish_if_stmt_cond (tree cond, tree if_stmt)
467 cond = maybe_convert_cond (cond);
468 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
471 /* Finish the then-clause of an if-statement, which may be given by
472 IF_STMT. */
474 tree
475 finish_then_clause (tree if_stmt)
477 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
478 return if_stmt;
481 /* Begin the else-clause of an if-statement. */
483 void
484 begin_else_clause (void)
488 /* Finish the else-clause of an if-statement, which may be given by
489 IF_STMT. */
491 void
492 finish_else_clause (tree if_stmt)
494 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
497 /* Finish an if-statement. */
499 void
500 finish_if_stmt (void)
502 finish_stmt ();
503 do_poplevel ();
506 /* Begin a while-statement. Returns a newly created WHILE_STMT if
507 appropriate. */
509 tree
510 begin_while_stmt (void)
512 tree r;
513 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
514 add_stmt (r);
515 do_pushlevel (sk_block);
516 return r;
519 /* Process the COND of a while-statement, which may be given by
520 WHILE_STMT. */
522 void
523 finish_while_stmt_cond (tree cond, tree while_stmt)
525 cond = maybe_convert_cond (cond);
526 if (processing_template_decl)
527 /* Don't mess with condition decls in a template. */
528 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
529 else if (getdecls () == NULL_TREE)
530 /* It was a simple condition; install it. */
531 WHILE_COND (while_stmt) = cond;
532 else
534 /* If there was a declaration in the condition, we can't leave it
535 there; transform
536 while (A x = 42) { }
538 while (true) { A x = 42; if (!x) break; } */
539 tree if_stmt;
540 WHILE_COND (while_stmt) = boolean_true_node;
542 if_stmt = begin_if_stmt ();
543 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
544 finish_if_stmt_cond (cond, if_stmt);
545 finish_break_stmt ();
546 finish_then_clause (if_stmt);
547 finish_if_stmt ();
551 /* Finish a while-statement, which may be given by WHILE_STMT. */
553 void
554 finish_while_stmt (tree while_stmt)
556 do_poplevel ();
557 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
558 finish_stmt ();
561 /* Begin a do-statement. Returns a newly created DO_STMT if
562 appropriate. */
564 tree
565 begin_do_stmt (void)
567 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
568 add_stmt (r);
569 return r;
572 /* Finish the body of a do-statement, which may be given by DO_STMT. */
574 void
575 finish_do_body (tree do_stmt)
577 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
580 /* Finish a do-statement, which may be given by DO_STMT, and whose
581 COND is as indicated. */
583 void
584 finish_do_stmt (tree cond, tree do_stmt)
586 cond = maybe_convert_cond (cond);
587 DO_COND (do_stmt) = cond;
588 finish_stmt ();
591 /* Finish a return-statement. The EXPRESSION returned, if any, is as
592 indicated. */
594 tree
595 finish_return_stmt (tree expr)
597 tree r;
599 expr = check_return_expr (expr);
600 if (!processing_template_decl)
602 if (DECL_DESTRUCTOR_P (current_function_decl))
604 /* Similarly, all destructors must run destructors for
605 base-classes before returning. So, all returns in a
606 destructor get sent to the DTOR_LABEL; finish_function emits
607 code to return a value there. */
608 return finish_goto_stmt (dtor_label);
611 r = add_stmt (build_stmt (RETURN_STMT, expr));
612 finish_stmt ();
614 return r;
617 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
619 tree
620 begin_for_stmt (void)
622 tree r;
624 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
625 NULL_TREE, NULL_TREE);
626 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
627 if (NEW_FOR_SCOPE_P (r))
628 do_pushlevel (sk_for);
629 add_stmt (r);
631 return r;
634 /* Finish the for-init-statement of a for-statement, which may be
635 given by FOR_STMT. */
637 void
638 finish_for_init_stmt (tree for_stmt)
640 if (last_tree != for_stmt)
641 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
642 do_pushlevel (sk_block);
645 /* Finish the COND of a for-statement, which may be given by
646 FOR_STMT. */
648 void
649 finish_for_cond (tree cond, tree for_stmt)
651 cond = maybe_convert_cond (cond);
652 if (processing_template_decl)
653 /* Don't mess with condition decls in a template. */
654 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
655 else if (getdecls () == NULL_TREE)
656 /* It was a simple condition; install it. */
657 FOR_COND (for_stmt) = cond;
658 else
660 /* If there was a declaration in the condition, we can't leave it
661 there; transform
662 for (; A x = 42;) { }
664 for (;;) { A x = 42; if (!x) break; } */
665 tree if_stmt;
666 FOR_COND (for_stmt) = NULL_TREE;
668 if_stmt = begin_if_stmt ();
669 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
670 finish_if_stmt_cond (cond, if_stmt);
671 finish_break_stmt ();
672 finish_then_clause (if_stmt);
673 finish_if_stmt ();
677 /* Finish the increment-EXPRESSION in a for-statement, which may be
678 given by FOR_STMT. */
680 void
681 finish_for_expr (tree expr, tree for_stmt)
683 FOR_EXPR (for_stmt) = expr;
686 /* Finish the body of a for-statement, which may be given by
687 FOR_STMT. The increment-EXPR for the loop must be
688 provided. */
690 void
691 finish_for_stmt (tree for_stmt)
693 /* Pop the scope for the body of the loop. */
694 do_poplevel ();
695 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
696 if (NEW_FOR_SCOPE_P (for_stmt))
697 do_poplevel ();
698 finish_stmt ();
701 /* Finish a break-statement. */
703 tree
704 finish_break_stmt (void)
706 return add_stmt (build_break_stmt ());
709 /* Finish a continue-statement. */
711 tree
712 finish_continue_stmt (void)
714 return add_stmt (build_continue_stmt ());
717 /* Begin a switch-statement. Returns a new SWITCH_STMT if
718 appropriate. */
720 tree
721 begin_switch_stmt (void)
723 tree r;
724 do_pushlevel (sk_block);
725 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
726 add_stmt (r);
727 return r;
730 /* Finish the cond of a switch-statement. */
732 void
733 finish_switch_cond (tree cond, tree switch_stmt)
735 tree orig_type = NULL;
736 if (!processing_template_decl)
738 tree index;
740 /* Convert the condition to an integer or enumeration type. */
741 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
742 if (cond == NULL_TREE)
744 error ("switch quantity not an integer");
745 cond = error_mark_node;
747 orig_type = TREE_TYPE (cond);
748 if (cond != error_mark_node)
750 cond = default_conversion (cond);
751 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
754 if (cond != error_mark_node)
756 index = get_unwidened (cond, NULL_TREE);
757 /* We can't strip a conversion from a signed type to an unsigned,
758 because if we did, int_fits_type_p would do the wrong thing
759 when checking case values for being in range,
760 and it's too hard to do the right thing. */
761 if (TREE_UNSIGNED (TREE_TYPE (cond))
762 == TREE_UNSIGNED (TREE_TYPE (index)))
763 cond = index;
766 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
767 SWITCH_TYPE (switch_stmt) = orig_type;
768 push_switch (switch_stmt);
771 /* Finish the body of a switch-statement, which may be given by
772 SWITCH_STMT. The COND to switch on is indicated. */
774 void
775 finish_switch_stmt (tree switch_stmt)
777 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
778 pop_switch ();
779 finish_stmt ();
780 do_poplevel ();
783 /* Generate the RTL for T, which is a TRY_BLOCK. */
785 static void
786 genrtl_try_block (tree t)
788 if (CLEANUP_P (t))
790 expand_eh_region_start ();
791 expand_stmt (TRY_STMTS (t));
792 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
794 else
796 if (!FN_TRY_BLOCK_P (t))
797 emit_line_note (input_filename, input_line);
799 expand_eh_region_start ();
800 expand_stmt (TRY_STMTS (t));
802 if (FN_TRY_BLOCK_P (t))
804 expand_start_all_catch ();
805 in_function_try_handler = 1;
806 expand_stmt (TRY_HANDLERS (t));
807 in_function_try_handler = 0;
808 expand_end_all_catch ();
810 else
812 expand_start_all_catch ();
813 expand_stmt (TRY_HANDLERS (t));
814 expand_end_all_catch ();
819 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
821 static void
822 genrtl_eh_spec_block (tree t)
824 expand_eh_region_start ();
825 expand_stmt (EH_SPEC_STMTS (t));
826 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
827 build_call (call_unexpected_node,
828 tree_cons (NULL_TREE,
829 build_exc_ptr (),
830 NULL_TREE)));
833 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
834 appropriate. */
836 tree
837 begin_try_block (void)
839 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
840 add_stmt (r);
841 return r;
844 /* Likewise, for a function-try-block. */
846 tree
847 begin_function_try_block (void)
849 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
850 FN_TRY_BLOCK_P (r) = 1;
851 add_stmt (r);
852 return r;
855 /* Finish a try-block, which may be given by TRY_BLOCK. */
857 void
858 finish_try_block (tree try_block)
860 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
863 /* Finish the body of a cleanup try-block, which may be given by
864 TRY_BLOCK. */
866 void
867 finish_cleanup_try_block (tree try_block)
869 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
872 /* Finish an implicitly generated try-block, with a cleanup is given
873 by CLEANUP. */
875 void
876 finish_cleanup (tree cleanup, tree try_block)
878 TRY_HANDLERS (try_block) = cleanup;
879 CLEANUP_P (try_block) = 1;
882 /* Likewise, for a function-try-block. */
884 void
885 finish_function_try_block (tree try_block)
887 if (TREE_CHAIN (try_block)
888 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
890 /* Chain the compound statement after the CTOR_INITIALIZER. */
891 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
892 /* And make the CTOR_INITIALIZER the body of the try-block. */
893 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
895 else
896 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
897 in_function_try_handler = 1;
900 /* Finish a handler-sequence for a try-block, which may be given by
901 TRY_BLOCK. */
903 void
904 finish_handler_sequence (tree try_block)
906 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
907 check_handlers (TRY_HANDLERS (try_block));
910 /* Likewise, for a function-try-block. */
912 void
913 finish_function_handler_sequence (tree try_block)
915 in_function_try_handler = 0;
916 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
917 check_handlers (TRY_HANDLERS (try_block));
920 /* Generate the RTL for T, which is a HANDLER. */
922 static void
923 genrtl_handler (tree t)
925 genrtl_do_pushlevel ();
926 if (!processing_template_decl)
927 expand_start_catch (HANDLER_TYPE (t));
928 expand_stmt (HANDLER_BODY (t));
929 if (!processing_template_decl)
930 expand_end_catch ();
933 /* Begin a handler. Returns a HANDLER if appropriate. */
935 tree
936 begin_handler (void)
938 tree r;
939 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
940 add_stmt (r);
941 /* Create a binding level for the eh_info and the exception object
942 cleanup. */
943 do_pushlevel (sk_catch);
944 return r;
947 /* Finish the handler-parameters for a handler, which may be given by
948 HANDLER. DECL is the declaration for the catch parameter, or NULL
949 if this is a `catch (...)' clause. */
951 void
952 finish_handler_parms (tree decl, tree handler)
954 tree type = NULL_TREE;
955 if (processing_template_decl)
957 if (decl)
959 decl = pushdecl (decl);
960 decl = push_template_decl (decl);
961 add_decl_stmt (decl);
962 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
963 type = TREE_TYPE (decl);
966 else
967 type = expand_start_catch_block (decl);
969 HANDLER_TYPE (handler) = type;
972 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
973 the return value from the matching call to finish_handler_parms. */
975 void
976 finish_handler (tree handler)
978 if (!processing_template_decl)
979 expand_end_catch_block ();
980 do_poplevel ();
981 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
984 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
985 compound-statement does not define a scope. Returns a new
986 COMPOUND_STMT if appropriate. */
988 tree
989 begin_compound_stmt (int has_no_scope)
991 tree r;
992 int is_try = 0;
994 r = build_stmt (COMPOUND_STMT, NULL_TREE);
996 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
997 is_try = 1;
999 add_stmt (r);
1000 if (has_no_scope)
1001 COMPOUND_STMT_NO_SCOPE (r) = 1;
1003 last_expr_type = NULL_TREE;
1005 if (!has_no_scope)
1006 do_pushlevel (is_try ? sk_try : sk_block);
1007 else
1008 /* Normally, we try hard to keep the BLOCK for a
1009 statement-expression. But, if it's a statement-expression with
1010 a scopeless block, there's nothing to keep, and we don't want
1011 to accidentally keep a block *inside* the scopeless block. */
1012 keep_next_level (0);
1014 return r;
1017 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1018 If HAS_NO_SCOPE is nonzero, the compound statement does not define
1019 a scope. */
1021 tree
1022 finish_compound_stmt (int has_no_scope, tree compound_stmt)
1024 tree r;
1025 tree t;
1027 if (!has_no_scope)
1028 r = do_poplevel ();
1029 else
1030 r = NULL_TREE;
1032 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1034 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1035 the precise purpose of that variable is store the type of the
1036 last expression statement within the last compound statement, we
1037 preserve the value. */
1038 t = last_expr_type;
1039 finish_stmt ();
1040 last_expr_type = t;
1042 return r;
1045 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1046 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1047 CLOBBERS. */
1049 tree
1050 finish_asm_stmt (tree cv_qualifier,
1051 tree string,
1052 tree output_operands,
1053 tree input_operands,
1054 tree clobbers)
1056 tree r;
1057 tree t;
1059 if (cv_qualifier != NULL_TREE
1060 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1062 warning ("%s qualifier ignored on asm",
1063 IDENTIFIER_POINTER (cv_qualifier));
1064 cv_qualifier = NULL_TREE;
1067 if (!processing_template_decl)
1069 int i;
1070 int ninputs;
1071 int noutputs;
1073 for (t = input_operands; t; t = TREE_CHAIN (t))
1075 tree converted_operand
1076 = decay_conversion (TREE_VALUE (t));
1078 /* If the type of the operand hasn't been determined (e.g.,
1079 because it involves an overloaded function), then issue
1080 an error message. There's no context available to
1081 resolve the overloading. */
1082 if (TREE_TYPE (converted_operand) == unknown_type_node)
1084 error ("type of asm operand `%E' could not be determined",
1085 TREE_VALUE (t));
1086 converted_operand = error_mark_node;
1088 TREE_VALUE (t) = converted_operand;
1091 ninputs = list_length (input_operands);
1092 noutputs = list_length (output_operands);
1094 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1096 bool allows_mem;
1097 bool allows_reg;
1098 bool is_inout;
1099 const char *constraint;
1100 tree operand;
1102 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1103 operand = TREE_VALUE (t);
1105 if (!parse_output_constraint (&constraint,
1106 i, ninputs, noutputs,
1107 &allows_mem,
1108 &allows_reg,
1109 &is_inout))
1111 /* By marking this operand as erroneous, we will not try
1112 to process this operand again in expand_asm_operands. */
1113 TREE_VALUE (t) = error_mark_node;
1114 continue;
1117 /* If the operand is a DECL that is going to end up in
1118 memory, assume it is addressable. This is a bit more
1119 conservative than it would ideally be; the exact test is
1120 buried deep in expand_asm_operands and depends on the
1121 DECL_RTL for the OPERAND -- which we don't have at this
1122 point. */
1123 if (!allows_reg && DECL_P (operand))
1124 cxx_mark_addressable (operand);
1128 r = build_stmt (ASM_STMT, cv_qualifier, string,
1129 output_operands, input_operands,
1130 clobbers);
1131 return add_stmt (r);
1134 /* Finish a label with the indicated NAME. */
1136 tree
1137 finish_label_stmt (tree name)
1139 tree decl = define_label (input_filename, input_line, name);
1140 return add_stmt (build_stmt (LABEL_STMT, decl));
1143 /* Finish a series of declarations for local labels. G++ allows users
1144 to declare "local" labels, i.e., labels with scope. This extension
1145 is useful when writing code involving statement-expressions. */
1147 void
1148 finish_label_decl (tree name)
1150 tree decl = declare_local_label (name);
1151 add_decl_stmt (decl);
1154 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1156 void
1157 finish_decl_cleanup (tree decl, tree cleanup)
1159 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1162 /* If the current scope exits with an exception, run CLEANUP. */
1164 void
1165 finish_eh_cleanup (tree cleanup)
1167 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1168 CLEANUP_EH_ONLY (r) = 1;
1169 add_stmt (r);
1172 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1173 order they were written by the user. Each node is as for
1174 emit_mem_initializers. */
1176 void
1177 finish_mem_initializers (tree mem_inits)
1179 /* Reorder the MEM_INITS so that they are in the order they appeared
1180 in the source program. */
1181 mem_inits = nreverse (mem_inits);
1183 if (processing_template_decl)
1184 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1185 else
1186 emit_mem_initializers (mem_inits);
1189 /* Returns the stack of SCOPE_STMTs for the current function. */
1191 tree *
1192 current_scope_stmt_stack (void)
1194 return &cfun->language->base.x_scope_stmt_stack;
1197 /* Finish a parenthesized expression EXPR. */
1199 tree
1200 finish_parenthesized_expr (tree expr)
1202 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1203 /* This inhibits warnings in c_common_truthvalue_conversion. */
1204 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1206 if (TREE_CODE (expr) == OFFSET_REF)
1207 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1208 enclosed in parentheses. */
1209 PTRMEM_OK_P (expr) = 0;
1210 return expr;
1213 /* Finish a reference to a non-static data member (DECL) that is not
1214 preceded by `.' or `->'. */
1216 tree
1217 finish_non_static_data_member (tree decl, tree qualifying_scope)
1219 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1221 if (current_class_ptr == NULL_TREE)
1223 if (current_function_decl
1224 && DECL_STATIC_FUNCTION_P (current_function_decl))
1225 cp_error_at ("invalid use of member `%D' in static member function",
1226 decl);
1227 else
1228 cp_error_at ("invalid use of non-static data member `%D'", decl);
1229 error ("from this location");
1231 return error_mark_node;
1233 TREE_USED (current_class_ptr) = 1;
1234 if (processing_template_decl)
1235 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1236 current_class_ref, DECL_NAME (decl));
1237 else
1239 tree access_type = current_class_type;
1240 tree object = current_class_ref;
1242 while (access_type
1243 && !DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1245 access_type = TYPE_CONTEXT (access_type);
1246 while (access_type && DECL_P (access_type))
1247 access_type = DECL_CONTEXT (access_type);
1250 if (!access_type)
1252 cp_error_at ("object missing in reference to `%D'",
1253 decl);
1254 error ("from this location");
1255 return error_mark_node;
1258 perform_or_defer_access_check (access_type, decl);
1260 /* If the data member was named `C::M', convert `*this' to `C'
1261 first. */
1262 if (qualifying_scope)
1264 tree binfo = NULL_TREE;
1265 object = build_scoped_ref (object, qualifying_scope,
1266 &binfo);
1269 return build_class_member_access_expr (object, decl,
1270 /*access_path=*/NULL_TREE,
1271 /*preserve_reference=*/false);
1275 /* Begin a statement-expression. The value returned must be passed to
1276 finish_stmt_expr. */
1278 tree
1279 begin_stmt_expr (void)
1281 /* If we're outside a function, we won't have a statement-tree to
1282 work with. But, if we see a statement-expression we need to
1283 create one. */
1284 if (! cfun && !last_tree)
1285 begin_stmt_tree (&scope_chain->x_saved_tree);
1287 keep_next_level (1);
1288 /* If we're building a statement tree, then the upcoming compound
1289 statement will be chained onto the tree structure, starting at
1290 last_tree. We return last_tree so that we can later unhook the
1291 compound statement. */
1292 return last_tree;
1295 /* Used when beginning a statement-expression outside function scope.
1296 For example, when handling a file-scope initializer, we use this
1297 function. */
1299 tree
1300 begin_global_stmt_expr (void)
1302 if (! cfun && !last_tree)
1303 begin_stmt_tree (&scope_chain->x_saved_tree);
1305 keep_next_level (1);
1307 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1310 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1312 tree
1313 finish_global_stmt_expr (tree stmt_expr)
1315 stmt_expr = expand_end_stmt_expr (stmt_expr);
1317 if (! cfun
1318 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1319 finish_stmt_tree (&scope_chain->x_saved_tree);
1321 return stmt_expr;
1324 /* Finish a statement-expression. RTL_EXPR should be the value
1325 returned by the previous begin_stmt_expr; EXPR is the
1326 statement-expression. Returns an expression representing the
1327 statement-expression. */
1329 tree
1330 finish_stmt_expr (tree rtl_expr)
1332 tree result;
1334 /* If the last thing in the statement-expression was not an
1335 expression-statement, then it has type `void'. */
1336 if (!last_expr_type)
1337 last_expr_type = void_type_node;
1338 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1339 TREE_SIDE_EFFECTS (result) = 1;
1341 /* Remove the compound statement from the tree structure; it is
1342 now saved in the STMT_EXPR. */
1343 last_tree = rtl_expr;
1344 TREE_CHAIN (last_tree) = NULL_TREE;
1346 /* If we created a statement-tree for this statement-expression,
1347 remove it now. */
1348 if (! cfun
1349 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1350 finish_stmt_tree (&scope_chain->x_saved_tree);
1352 return result;
1355 /* Generate an expression for `FN (ARGS)'.
1357 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1358 as a virtual call, even if FN is virtual. (This flag is set when
1359 encountering an expression where the function name is explicitly
1360 qualified. For example a call to `X::f' never generates a virtual
1361 call.)
1363 Returns code for the call. */
1365 tree
1366 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1368 if (fn == error_mark_node || args == error_mark_node)
1369 return error_mark_node;
1371 if (processing_template_decl)
1372 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1374 /* ARGS should be a list of arguments. */
1375 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1376 20020712);
1378 /* A reference to a member function will appear as an overloaded
1379 function (rather than a BASELINK) if an unqualified name was used
1380 to refer to it. */
1381 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1383 tree f;
1385 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1386 f = get_first_fn (TREE_OPERAND (fn, 0));
1387 else
1388 f = get_first_fn (fn);
1389 if (DECL_FUNCTION_MEMBER_P (f))
1391 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1392 fn = build_baselink (TYPE_BINFO (type),
1393 TYPE_BINFO (type),
1394 fn, /*optype=*/NULL_TREE);
1398 if (BASELINK_P (fn))
1400 tree object;
1402 /* A call to a member function. From [over.call.func]:
1404 If the keyword this is in scope and refers to the class of
1405 that member function, or a derived class thereof, then the
1406 function call is transformed into a qualified function call
1407 using (*this) as the postfix-expression to the left of the
1408 . operator.... [Otherwise] a contrived object of type T
1409 becomes the implied object argument.
1411 This paragraph is unclear about this situation:
1413 struct A { void f(); };
1414 struct B : public A {};
1415 struct C : public A { void g() { B::f(); }};
1417 In particular, for `B::f', this paragraph does not make clear
1418 whether "the class of that member function" refers to `A' or
1419 to `B'. We believe it refers to `B'. */
1420 if (current_class_type
1421 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1422 current_class_type)
1423 && current_class_ref)
1424 object = current_class_ref;
1425 else
1427 tree representative_fn;
1429 representative_fn = BASELINK_FUNCTIONS (fn);
1430 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1431 representative_fn = TREE_OPERAND (representative_fn, 0);
1432 representative_fn = get_first_fn (representative_fn);
1433 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1436 return build_new_method_call (object, fn, args, NULL_TREE,
1437 (disallow_virtual
1438 ? LOOKUP_NONVIRTUAL : 0));
1440 else if (is_overloaded_fn (fn))
1441 /* A call to a namespace-scope function. */
1442 return build_new_function_call (fn, args);
1443 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1445 tree result;
1447 if (args)
1448 error ("arguments to destructor are not allowed");
1449 /* Mark the pseudo-destructor call as having side-effects so
1450 that we do not issue warnings about its use. */
1451 result = build1 (NOP_EXPR,
1452 void_type_node,
1453 TREE_OPERAND (fn, 0));
1454 TREE_SIDE_EFFECTS (result) = 1;
1455 return result;
1457 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1459 /* If the "function" is really an object of class type, it might
1460 have an overloaded `operator ()'. */
1461 tree result;
1462 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1463 if (result)
1464 return result;
1467 /* A call where the function is unknown. */
1468 return build_function_call (fn, args);
1471 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1472 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1473 POSTDECREMENT_EXPR.) */
1475 tree
1476 finish_increment_expr (tree expr, enum tree_code code)
1478 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1479 a COMPONENT_REF). This way if we've got, say, a reference to a
1480 static member that's being operated on, we don't end up trying to
1481 find a member operator for the class it's in. */
1483 if (TREE_CODE (expr) == OFFSET_REF)
1484 expr = resolve_offset_ref (expr);
1485 return build_x_unary_op (code, expr);
1488 /* Finish a use of `this'. Returns an expression for `this'. */
1490 tree
1491 finish_this_expr (void)
1493 tree result;
1495 if (current_class_ptr)
1497 result = current_class_ptr;
1499 else if (current_function_decl
1500 && DECL_STATIC_FUNCTION_P (current_function_decl))
1502 error ("`this' is unavailable for static member functions");
1503 result = error_mark_node;
1505 else
1507 if (current_function_decl)
1508 error ("invalid use of `this' in non-member function");
1509 else
1510 error ("invalid use of `this' at top level");
1511 result = error_mark_node;
1514 return result;
1517 /* Finish a member function call using OBJECT and ARGS as arguments to
1518 FN. Returns an expression for the call. */
1520 tree
1521 finish_object_call_expr (tree fn, tree object, tree args)
1523 if (DECL_DECLARES_TYPE_P (fn))
1525 if (processing_template_decl)
1526 /* This can happen on code like:
1528 class X;
1529 template <class T> void f(T t) {
1530 t.X();
1533 We just grab the underlying IDENTIFIER. */
1534 fn = DECL_NAME (fn);
1535 else
1537 error ("calling type `%T' like a method", fn);
1538 return error_mark_node;
1542 if (processing_template_decl)
1543 return build_nt (CALL_EXPR,
1544 build_nt (COMPONENT_REF, object, fn),
1545 args);
1547 if (name_p (fn))
1548 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1549 else
1550 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1553 /* Finish a qualified member function call using OBJECT and ARGS as
1554 arguments to FN. Returns an expression for the call. */
1556 tree
1557 finish_qualified_object_call_expr (tree fn, tree object, tree args)
1559 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1560 TREE_OPERAND (fn, 1), args);
1563 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1564 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1565 the TYPE for the type given. If SCOPE is non-NULL, the expression
1566 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1568 tree
1569 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1571 if (destructor == error_mark_node)
1572 return error_mark_node;
1574 my_friendly_assert (TYPE_P (destructor), 20010905);
1576 if (!processing_template_decl)
1578 if (scope == error_mark_node)
1580 error ("invalid qualifying scope in pseudo-destructor name");
1581 return error_mark_node;
1584 if (!same_type_p (TREE_TYPE (object), destructor))
1586 error ("`%E' is not of type `%T'", object, destructor);
1587 return error_mark_node;
1591 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1594 /* Finish an expression of the form CODE EXPR. */
1596 tree
1597 finish_unary_op_expr (enum tree_code code, tree expr)
1599 tree result = build_x_unary_op (code, expr);
1600 /* Inside a template, build_x_unary_op does not fold the
1601 expression. So check whether the result is folded before
1602 setting TREE_NEGATED_INT. */
1603 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1604 && TREE_CODE (result) == INTEGER_CST
1605 && !TREE_UNSIGNED (TREE_TYPE (result))
1606 && INT_CST_LT (result, integer_zero_node))
1607 TREE_NEGATED_INT (result) = 1;
1608 overflow_warning (result);
1609 return result;
1612 /* Finish a compound-literal expression. TYPE is the type to which
1613 the INITIALIZER_LIST is being cast. */
1615 tree
1616 finish_compound_literal (tree type, tree initializer_list)
1618 tree compound_literal;
1620 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1621 compound_literal = build_constructor (NULL_TREE, initializer_list);
1622 /* Mark it as a compound-literal. */
1623 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1624 if (processing_template_decl)
1625 TREE_TYPE (compound_literal) = type;
1626 else
1628 /* Check the initialization. */
1629 compound_literal = digest_init (type, compound_literal, NULL);
1630 /* If the TYPE was an array type with an unknown bound, then we can
1631 figure out the dimension now. For example, something like:
1633 `(int []) { 2, 3 }'
1635 implies that the array has two elements. */
1636 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1637 complete_array_type (type, compound_literal, 1);
1640 return compound_literal;
1643 /* Return the declaration for the function-name variable indicated by
1644 ID. */
1646 tree
1647 finish_fname (tree id)
1649 tree decl;
1651 decl = fname_decl (C_RID_CODE (id), id);
1652 if (processing_template_decl)
1653 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1654 return decl;
1657 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1658 and DECLARATOR. Returns nonzero if the function-declaration is
1659 valid. */
1662 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1664 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1665 return 0;
1667 /* The things we're about to see are not directly qualified by any
1668 template headers we've seen thus far. */
1669 reset_specialization ();
1671 return 1;
1674 /* Finish a translation unit. */
1676 void
1677 finish_translation_unit (void)
1679 /* In case there were missing closebraces,
1680 get us back to the global binding level. */
1681 pop_everything ();
1682 while (current_namespace != global_namespace)
1683 pop_namespace ();
1685 /* Do file scope __FUNCTION__ et al. */
1686 finish_fname_decls ();
1689 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1690 Returns the parameter. */
1692 tree
1693 finish_template_type_parm (tree aggr, tree identifier)
1695 if (aggr != class_type_node)
1697 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1698 aggr = class_type_node;
1701 return build_tree_list (aggr, identifier);
1704 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1705 Returns the parameter. */
1707 tree
1708 finish_template_template_parm (tree aggr, tree identifier)
1710 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1711 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1712 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1713 DECL_TEMPLATE_RESULT (tmpl) = decl;
1714 DECL_ARTIFICIAL (decl) = 1;
1715 end_template_decl ();
1717 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1719 return finish_template_type_parm (aggr, tmpl);
1722 /* ARGUMENT is the default-argument value for a template template
1723 parameter. If ARGUMENT is invalid, issue error messages and return
1724 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1726 tree
1727 check_template_template_default_arg (tree argument)
1729 if (TREE_CODE (argument) != TEMPLATE_DECL
1730 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1731 && TREE_CODE (argument) != TYPE_DECL
1732 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1734 error ("invalid default template argument");
1735 return error_mark_node;
1738 return argument;
1741 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1742 nonzero, the parameter list was terminated by a `...'. */
1744 tree
1745 finish_parmlist (tree parms, int ellipsis)
1747 if (parms)
1749 /* We mark the PARMS as a parmlist so that declarator processing can
1750 disambiguate certain constructs. */
1751 TREE_PARMLIST (parms) = 1;
1752 /* We do not append void_list_node here, but leave it to grokparms
1753 to do that. */
1754 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1756 return parms;
1759 /* Begin a class definition, as indicated by T. */
1761 tree
1762 begin_class_definition (tree t)
1764 if (t == error_mark_node)
1765 return error_mark_node;
1767 if (processing_template_parmlist)
1769 error ("definition of `%#T' inside template parameter list", t);
1770 return error_mark_node;
1772 /* A non-implicit typename comes from code like:
1774 template <typename T> struct A {
1775 template <typename U> struct A<T>::B ...
1777 This is erroneous. */
1778 else if (TREE_CODE (t) == TYPENAME_TYPE)
1780 error ("invalid definition of qualified type `%T'", t);
1781 t = error_mark_node;
1784 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1786 t = make_aggr_type (RECORD_TYPE);
1787 pushtag (make_anon_name (), t, 0);
1790 /* If this type was already complete, and we see another definition,
1791 that's an error. */
1792 if (COMPLETE_TYPE_P (t))
1794 error ("redefinition of `%#T'", t);
1795 cp_error_at ("previous definition of `%#T'", t);
1796 return error_mark_node;
1799 /* Update the location of the decl. */
1800 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
1802 if (TYPE_BEING_DEFINED (t))
1804 t = make_aggr_type (TREE_CODE (t));
1805 pushtag (TYPE_IDENTIFIER (t), t, 0);
1807 maybe_process_partial_specialization (t);
1808 pushclass (t, true);
1809 TYPE_BEING_DEFINED (t) = 1;
1810 TYPE_PACKED (t) = flag_pack_struct;
1811 /* Reset the interface data, at the earliest possible
1812 moment, as it might have been set via a class foo;
1813 before. */
1814 if (! TYPE_ANONYMOUS_P (t))
1816 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1817 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1818 (t, interface_unknown);
1820 reset_specialization();
1822 /* Make a declaration for this class in its own scope. */
1823 build_self_reference ();
1825 return t;
1828 /* Finish the member declaration given by DECL. */
1830 void
1831 finish_member_declaration (tree decl)
1833 if (decl == error_mark_node || decl == NULL_TREE)
1834 return;
1836 if (decl == void_type_node)
1837 /* The COMPONENT was a friend, not a member, and so there's
1838 nothing for us to do. */
1839 return;
1841 /* We should see only one DECL at a time. */
1842 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1844 /* Set up access control for DECL. */
1845 TREE_PRIVATE (decl)
1846 = (current_access_specifier == access_private_node);
1847 TREE_PROTECTED (decl)
1848 = (current_access_specifier == access_protected_node);
1849 if (TREE_CODE (decl) == TEMPLATE_DECL)
1851 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1852 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1855 /* Mark the DECL as a member of the current class. */
1856 DECL_CONTEXT (decl) = current_class_type;
1858 /* [dcl.link]
1860 A C language linkage is ignored for the names of class members
1861 and the member function type of class member functions. */
1862 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1863 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1865 /* Put functions on the TYPE_METHODS list and everything else on the
1866 TYPE_FIELDS list. Note that these are built up in reverse order.
1867 We reverse them (to obtain declaration order) in finish_struct. */
1868 if (TREE_CODE (decl) == FUNCTION_DECL
1869 || DECL_FUNCTION_TEMPLATE_P (decl))
1871 /* We also need to add this function to the
1872 CLASSTYPE_METHOD_VEC. */
1873 add_method (current_class_type, decl, /*error_p=*/0);
1875 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1876 TYPE_METHODS (current_class_type) = decl;
1878 maybe_add_class_template_decl_list (current_class_type, decl,
1879 /*friend_p=*/0);
1881 /* Enter the DECL into the scope of the class. */
1882 else if (TREE_CODE (decl) == USING_DECL || pushdecl_class_level (decl))
1884 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1885 go at the beginning. The reason is that lookup_field_1
1886 searches the list in order, and we want a field name to
1887 override a type name so that the "struct stat hack" will
1888 work. In particular:
1890 struct S { enum E { }; int E } s;
1891 s.E = 3;
1893 is valid. In addition, the FIELD_DECLs must be maintained in
1894 declaration order so that class layout works as expected.
1895 However, we don't need that order until class layout, so we
1896 save a little time by putting FIELD_DECLs on in reverse order
1897 here, and then reversing them in finish_struct_1. (We could
1898 also keep a pointer to the correct insertion points in the
1899 list.) */
1901 if (TREE_CODE (decl) == TYPE_DECL)
1902 TYPE_FIELDS (current_class_type)
1903 = chainon (TYPE_FIELDS (current_class_type), decl);
1904 else
1906 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1907 TYPE_FIELDS (current_class_type) = decl;
1910 maybe_add_class_template_decl_list (current_class_type, decl,
1911 /*friend_p=*/0);
1915 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1916 the definition is immediately followed by a semicolon. Returns the
1917 type. */
1919 tree
1920 finish_class_definition (tree t, tree attributes, int semi, int pop_scope_p)
1922 if (t == error_mark_node)
1923 return error_mark_node;
1925 /* finish_struct nukes this anyway; if finish_exception does too,
1926 then it can go. */
1927 if (semi)
1928 note_got_semicolon (t);
1930 /* If we got any attributes in class_head, xref_tag will stick them in
1931 TREE_TYPE of the type. Grab them now. */
1932 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1933 TYPE_ATTRIBUTES (t) = NULL_TREE;
1935 if (TREE_CODE (t) == ENUMERAL_TYPE)
1937 else
1939 t = finish_struct (t, attributes);
1940 if (semi)
1941 note_got_semicolon (t);
1944 if (pop_scope_p)
1945 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1947 return t;
1950 /* Finish processing the declaration of a member class template
1951 TYPES whose template parameters are given by PARMS. */
1953 tree
1954 finish_member_class_template (tree types)
1956 tree t;
1958 /* If there are declared, but undefined, partial specializations
1959 mixed in with the typespecs they will not yet have passed through
1960 maybe_process_partial_specialization, so we do that here. */
1961 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1962 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1963 maybe_process_partial_specialization (TREE_VALUE (t));
1965 note_list_got_semicolon (types);
1966 grok_x_components (types);
1967 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1968 /* The component was in fact a friend declaration. We avoid
1969 finish_member_template_decl performing certain checks by
1970 unsetting TYPES. */
1971 types = NULL_TREE;
1973 finish_member_template_decl (types);
1975 /* As with other component type declarations, we do
1976 not store the new DECL on the list of
1977 component_decls. */
1978 return NULL_TREE;
1981 /* Finish processing a complete template declaration. The PARMS are
1982 the template parameters. */
1984 void
1985 finish_template_decl (tree parms)
1987 if (parms)
1988 end_template_decl ();
1989 else
1990 end_specialization ();
1993 /* Finish processing a template-id (which names a type) of the form
1994 NAME < ARGS >. Return the TYPE_DECL for the type named by the
1995 template-id. If ENTERING_SCOPE is nonzero we are about to enter
1996 the scope of template-id indicated. */
1998 tree
1999 finish_template_type (tree name, tree args, int entering_scope)
2001 tree decl;
2003 decl = lookup_template_class (name, args,
2004 NULL_TREE, NULL_TREE,
2005 entering_scope, /*complain=*/1);
2006 if (decl != error_mark_node)
2007 decl = TYPE_STUB_DECL (decl);
2009 return decl;
2012 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2013 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2014 BASE_CLASS, or NULL_TREE if an error occurred. The
2015 ACCESS_SPECIFIER is one of
2016 access_{default,public,protected_private}[_virtual]_node.*/
2018 tree
2019 finish_base_specifier (tree base, tree access, bool virtual_p)
2021 tree result;
2023 if (base == error_mark_node)
2025 error ("invalid base-class specification");
2026 result = NULL_TREE;
2028 else if (! is_aggr_type (base, 1))
2029 result = NULL_TREE;
2030 else
2032 if (cp_type_quals (base) != 0)
2034 error ("base class `%T' has cv qualifiers", base);
2035 base = TYPE_MAIN_VARIANT (base);
2037 result = build_tree_list (access, base);
2038 TREE_VIA_VIRTUAL (result) = virtual_p;
2041 return result;
2044 /* Called when multiple declarators are processed. If that is not
2045 premitted in this context, an error is issued. */
2047 void
2048 check_multiple_declarators (void)
2050 /* [temp]
2052 In a template-declaration, explicit specialization, or explicit
2053 instantiation the init-declarator-list in the declaration shall
2054 contain at most one declarator.
2056 We don't just use PROCESSING_TEMPLATE_DECL for the first
2057 condition since that would disallow the perfectly valid code,
2058 like `template <class T> struct S { int i, j; };'. */
2059 if (at_function_scope_p ())
2060 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2061 return;
2063 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2064 || processing_explicit_instantiation
2065 || processing_specialization)
2066 error ("multiple declarators in template declaration");
2069 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2070 use as a type-specifier. */
2072 tree
2073 finish_typeof (tree expr)
2075 tree type;
2077 if (type_dependent_expression_p (expr))
2079 type = make_aggr_type (TYPEOF_TYPE);
2080 TYPE_FIELDS (type) = expr;
2082 return type;
2085 if (TREE_CODE (expr) == OFFSET_REF)
2086 expr = resolve_offset_ref (expr);
2088 type = TREE_TYPE (expr);
2090 if (!type || type == unknown_type_node)
2092 error ("type of `%E' is unknown", expr);
2093 return error_mark_node;
2096 return type;
2099 /* Compute the value of the `sizeof' operator. */
2101 tree
2102 finish_sizeof (tree t)
2104 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2107 /* Implement the __alignof keyword: Return the minimum required
2108 alignment of T, measured in bytes. */
2110 tree
2111 finish_alignof (tree t)
2113 if (processing_template_decl)
2114 return build_min (ALIGNOF_EXPR, size_type_node, t);
2116 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2119 /* Generate RTL for the statement T, and its substatements, and any
2120 other statements at its nesting level. */
2122 static void
2123 cp_expand_stmt (tree t)
2125 switch (TREE_CODE (t))
2127 case TRY_BLOCK:
2128 genrtl_try_block (t);
2129 break;
2131 case EH_SPEC_BLOCK:
2132 genrtl_eh_spec_block (t);
2133 break;
2135 case HANDLER:
2136 genrtl_handler (t);
2137 break;
2139 case USING_STMT:
2140 break;
2142 default:
2143 abort ();
2144 break;
2148 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2149 will equivalent CALL_EXPRs. */
2151 static tree
2152 simplify_aggr_init_exprs_r (tree* tp,
2153 int* walk_subtrees ATTRIBUTE_UNUSED ,
2154 void* data ATTRIBUTE_UNUSED )
2156 tree aggr_init_expr;
2157 tree call_expr;
2158 tree fn;
2159 tree args;
2160 tree slot;
2161 tree type;
2162 enum style_t { ctor, arg, pcc } style;
2164 aggr_init_expr = *tp;
2165 /* We don't need to walk into types; there's nothing in a type that
2166 needs simplification. (And, furthermore, there are places we
2167 actively don't want to go. For example, we don't want to wander
2168 into the default arguments for a FUNCTION_DECL that appears in a
2169 CALL_EXPR.) */
2170 if (TYPE_P (aggr_init_expr))
2172 *walk_subtrees = 0;
2173 return NULL_TREE;
2175 /* Only AGGR_INIT_EXPRs are interesting. */
2176 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2177 return NULL_TREE;
2179 /* Form an appropriate CALL_EXPR. */
2180 fn = TREE_OPERAND (aggr_init_expr, 0);
2181 args = TREE_OPERAND (aggr_init_expr, 1);
2182 slot = TREE_OPERAND (aggr_init_expr, 2);
2183 type = TREE_TYPE (aggr_init_expr);
2185 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2186 style = ctor;
2187 #ifdef PCC_STATIC_STRUCT_RETURN
2188 else if (1)
2189 style = pcc;
2190 #endif
2191 else if (TREE_ADDRESSABLE (type))
2192 style = arg;
2193 else
2194 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2195 handling. See build_cplus_new. */
2196 abort ();
2198 if (style == ctor || style == arg)
2200 /* Pass the address of the slot. If this is a constructor, we
2201 replace the first argument; otherwise, we tack on a new one. */
2202 if (style == ctor)
2203 args = TREE_CHAIN (args);
2205 cxx_mark_addressable (slot);
2206 args = tree_cons (NULL_TREE,
2207 build1 (ADDR_EXPR,
2208 build_pointer_type (TREE_TYPE (slot)),
2209 slot),
2210 args);
2213 call_expr = build (CALL_EXPR,
2214 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2215 fn, args, NULL_TREE);
2216 TREE_SIDE_EFFECTS (call_expr) = 1;
2218 if (style == arg)
2219 /* Tell the backend that we've added our return slot to the argument
2220 list. */
2221 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2222 else if (style == pcc)
2224 /* If we're using the non-reentrant PCC calling convention, then we
2225 need to copy the returned value out of the static buffer into the
2226 SLOT. */
2227 push_deferring_access_checks (dk_no_check);
2228 call_expr = build_aggr_init (slot, call_expr,
2229 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2230 pop_deferring_access_checks ();
2233 /* We want to use the value of the initialized location as the
2234 result. */
2235 call_expr = build (COMPOUND_EXPR, type,
2236 call_expr, slot);
2238 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2239 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2240 *tp = call_expr;
2242 /* Keep iterating. */
2243 return NULL_TREE;
2246 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2248 static void
2249 emit_associated_thunks (tree fn)
2251 /* When we use vcall offsets, we emit thunks with the virtual
2252 functions to which they thunk. The whole point of vcall offsets
2253 is so that you can know statically the entire set of thunks that
2254 will ever be needed for a given virtual function, thereby
2255 enabling you to output all the thunks with the function itself. */
2256 if (DECL_VIRTUAL_P (fn))
2258 tree thunk;
2260 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2262 use_thunk (thunk, /*emit_p=*/1);
2263 if (DECL_RESULT_THUNK_P (thunk))
2265 tree probe;
2267 for (probe = DECL_THUNKS (thunk);
2268 probe; probe = TREE_CHAIN (probe))
2269 use_thunk (probe, /*emit_p=*/1);
2275 /* Generate RTL for FN. */
2277 void
2278 expand_body (tree fn)
2280 location_t saved_loc;
2281 tree saved_function;
2283 /* When the parser calls us after finishing the body of a template
2284 function, we don't really want to expand the body. When we're
2285 processing an in-class definition of an inline function,
2286 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2287 to look at the function itself. */
2288 if (processing_template_decl
2289 || (DECL_LANG_SPECIFIC (fn)
2290 && DECL_TEMPLATE_INFO (fn)
2291 && uses_template_parms (DECL_TI_ARGS (fn))))
2293 /* Normally, collection only occurs in rest_of_compilation. So,
2294 if we don't collect here, we never collect junk generated
2295 during the processing of templates until we hit a
2296 non-template function. */
2297 ggc_collect ();
2298 return;
2301 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2302 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2303 simplify_aggr_init_exprs_r,
2304 NULL);
2306 /* If this is a constructor or destructor body, we have to clone
2307 it. */
2308 if (maybe_clone_body (fn))
2310 /* We don't want to process FN again, so pretend we've written
2311 it out, even though we haven't. */
2312 TREE_ASM_WRITTEN (fn) = 1;
2313 return;
2316 /* There's no reason to do any of the work here if we're only doing
2317 semantic analysis; this code just generates RTL. */
2318 if (flag_syntax_only)
2319 return;
2321 /* If possible, avoid generating RTL for this function. Instead,
2322 just record it as an inline function, and wait until end-of-file
2323 to decide whether to write it out or not. */
2324 if (/* We have to generate RTL if it's not an inline function. */
2325 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2326 /* Or if we have to emit code for inline functions anyhow. */
2327 && !flag_keep_inline_functions
2328 /* Or if we actually have a reference to the function. */
2329 && !DECL_NEEDED_P (fn))
2331 /* Set DECL_EXTERNAL so that assemble_external will be called as
2332 necessary. We'll clear it again in finish_file. */
2333 if (!DECL_EXTERNAL (fn))
2335 DECL_NOT_REALLY_EXTERN (fn) = 1;
2336 DECL_EXTERNAL (fn) = 1;
2338 /* Remember this function. In finish_file we'll decide if
2339 we actually need to write this function out. */
2340 defer_fn (fn);
2341 /* Let the back-end know that this function exists. */
2342 (*debug_hooks->deferred_inline_function) (fn);
2343 return;
2346 /* Compute the appropriate object-file linkage for inline
2347 functions. */
2348 if (DECL_DECLARED_INLINE_P (fn))
2349 import_export_decl (fn);
2351 /* If FN is external, then there's no point in generating RTL for
2352 it. This situation can arise with an inline function under
2353 `-fexternal-templates'; we instantiate the function, even though
2354 we're not planning on emitting it, in case we get a chance to
2355 inline it. */
2356 if (DECL_EXTERNAL (fn))
2357 return;
2359 /* Save the current file name and line number. When we expand the
2360 body of the function, we'll set INPUT_LOCATION so that
2361 error-mesages come out in the right places. */
2362 saved_loc = input_location;
2363 saved_function = current_function_decl;
2364 input_location = DECL_SOURCE_LOCATION (fn);
2365 current_function_decl = fn;
2367 timevar_push (TV_INTEGRATION);
2369 /* Optimize the body of the function before expanding it. */
2370 optimize_function (fn);
2372 timevar_pop (TV_INTEGRATION);
2373 timevar_push (TV_EXPAND);
2375 genrtl_start_function (fn);
2376 current_function_is_thunk = DECL_THUNK_P (fn);
2378 /* Expand the body. */
2379 expand_stmt (DECL_SAVED_TREE (fn));
2381 /* Statements should always be full-expressions at the outermost set
2382 of curly braces for a function. */
2383 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2385 /* The outermost statement for a function contains the line number
2386 recorded when we finished processing the function. */
2387 input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2389 /* Generate code for the function. */
2390 genrtl_finish_function (fn);
2392 /* If possible, obliterate the body of the function so that it can
2393 be garbage collected. */
2394 if (dump_enabled_p (TDI_all))
2395 /* Keep the body; we're going to dump it. */
2397 else if (DECL_INLINE (fn) && flag_inline_trees)
2398 /* We might need the body of this function so that we can expand
2399 it inline somewhere else. */
2401 else
2402 /* We don't need the body; blow it away. */
2403 DECL_SAVED_TREE (fn) = NULL_TREE;
2405 /* And restore the current source position. */
2406 current_function_decl = saved_function;
2407 input_location = saved_loc;
2408 extract_interface_info ();
2410 timevar_pop (TV_EXPAND);
2412 /* Emit any thunks that should be emitted at the same time as FN. */
2413 emit_associated_thunks (fn);
2416 /* Helper function for walk_tree, used by finish_function to override all
2417 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2418 value optimization. */
2420 tree
2421 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
2423 tree nrv = (tree) data;
2425 /* No need to walk into types. There wouldn't be any need to walk into
2426 non-statements, except that we have to consider STMT_EXPRs. */
2427 if (TYPE_P (*tp))
2428 *walk_subtrees = 0;
2429 else if (TREE_CODE (*tp) == RETURN_STMT)
2430 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2431 else if (TREE_CODE (*tp) == CLEANUP_STMT
2432 && CLEANUP_DECL (*tp) == nrv)
2433 CLEANUP_EH_ONLY (*tp) = 1;
2435 /* Keep iterating. */
2436 return NULL_TREE;
2439 /* Start generating the RTL for FN. */
2441 static void
2442 genrtl_start_function (tree fn)
2444 /* Tell everybody what function we're processing. */
2445 current_function_decl = fn;
2446 /* Get the RTL machinery going for this function. */
2447 init_function_start (fn);
2448 /* Let everybody know that we're expanding this function, not doing
2449 semantic analysis. */
2450 expanding_p = 1;
2452 /* Even though we're inside a function body, we still don't want to
2453 call expand_expr to calculate the size of a variable-sized array.
2454 We haven't necessarily assigned RTL to all variables yet, so it's
2455 not safe to try to expand expressions involving them. */
2456 immediate_size_expand = 0;
2457 cfun->x_dont_save_pending_sizes_p = 1;
2459 /* Let the user know we're compiling this function. */
2460 announce_function (fn);
2462 /* Initialize the per-function data. */
2463 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2464 if (DECL_SAVED_FUNCTION_DATA (fn))
2466 /* If we already parsed this function, and we're just expanding it
2467 now, restore saved state. */
2468 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2470 /* This function is being processed in whole-function mode; we
2471 already did semantic analysis. */
2472 cfun->x_whole_function_mode_p = 1;
2474 /* If we decided that we didn't want to inline this function,
2475 make sure the back-end knows that. */
2476 if (!current_function_cannot_inline)
2477 current_function_cannot_inline = cp_function_chain->cannot_inline;
2479 /* We don't need the saved data anymore. Unless this is an inline
2480 function; we need the named return value info for
2481 cp_copy_res_decl_for_inlining. */
2482 if (! DECL_INLINE (fn))
2483 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2486 /* Keep track of how many functions we're presently expanding. */
2487 ++function_depth;
2489 /* Create a binding level for the parameters. */
2490 expand_function_start (fn, /*parms_have_cleanups=*/0);
2491 /* If this function is `main'. */
2492 if (DECL_MAIN_P (fn))
2493 expand_main_function ();
2495 /* Give our named return value the same RTL as our RESULT_DECL. */
2496 if (current_function_return_value)
2497 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2500 /* Finish generating the RTL for FN. */
2502 static void
2503 genrtl_finish_function (tree fn)
2505 tree t;
2507 #if 0
2508 if (write_symbols != NO_DEBUG)
2510 /* Keep this code around in case we later want to control debug info
2511 based on whether a type is "used". (jason 1999-11-11) */
2513 tree ttype = target_type (fntype);
2514 tree parmdecl;
2516 if (IS_AGGR_TYPE (ttype))
2517 /* Let debugger know it should output info for this type. */
2518 note_debug_info_needed (ttype);
2520 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2522 ttype = target_type (TREE_TYPE (parmdecl));
2523 if (IS_AGGR_TYPE (ttype))
2524 /* Let debugger know it should output info for this type. */
2525 note_debug_info_needed (ttype);
2528 #endif
2530 /* Clean house because we will need to reorder insns here. */
2531 do_pending_stack_adjust ();
2533 /* If we have a named return value, we need to force a return so that
2534 the return register is USEd. */
2535 if (DECL_NAME (DECL_RESULT (fn)))
2536 emit_jump (return_label);
2538 /* We hard-wired immediate_size_expand to zero in start_function.
2539 Expand_function_end will decrement this variable. So, we set the
2540 variable to one here, so that after the decrement it will remain
2541 zero. */
2542 immediate_size_expand = 1;
2544 /* Generate rtl for function exit. */
2545 expand_function_end ();
2547 /* If this is a nested function (like a template instantiation that
2548 we're compiling in the midst of compiling something else), push a
2549 new GC context. That will keep local variables on the stack from
2550 being collected while we're doing the compilation of this
2551 function. */
2552 if (function_depth > 1)
2553 ggc_push_context ();
2555 /* There's no need to defer outputting this function any more; we
2556 know we want to output it. */
2557 DECL_DEFER_OUTPUT (fn) = 0;
2559 /* Run the optimizers and output the assembler code for this
2560 function. */
2561 rest_of_compilation (fn);
2563 /* Undo the call to ggc_push_context above. */
2564 if (function_depth > 1)
2565 ggc_pop_context ();
2567 #if 0
2568 /* Keep this code around in case we later want to control debug info
2569 based on whether a type is "used". (jason 1999-11-11) */
2571 if (ctype && TREE_ASM_WRITTEN (fn))
2572 note_debug_info_needed (ctype);
2573 #endif
2575 /* If this function is marked with the constructor attribute, add it
2576 to the list of functions to be called along with constructors
2577 from static duration objects. */
2578 if (DECL_STATIC_CONSTRUCTOR (fn))
2579 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2581 /* If this function is marked with the destructor attribute, add it
2582 to the list of functions to be called along with destructors from
2583 static duration objects. */
2584 if (DECL_STATIC_DESTRUCTOR (fn))
2585 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2587 --function_depth;
2589 /* In C++, we should never be saving RTL for the function. */
2590 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2592 /* Since we don't need the RTL for this function anymore, stop
2593 pointing to it. That's especially important for LABEL_DECLs,
2594 since you can reach all the instructions in the function from the
2595 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2596 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2597 local variables. */
2598 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2599 clear_decl_rtl,
2600 NULL);
2602 /* Clear out the RTL for the arguments. */
2603 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2605 SET_DECL_RTL (t, NULL_RTX);
2606 DECL_INCOMING_RTL (t) = NULL_RTX;
2609 if (!(flag_inline_trees && DECL_INLINE (fn)))
2610 /* DECL_INITIAL must remain nonzero so we know this was an
2611 actual function definition. */
2612 DECL_INITIAL (fn) = error_mark_node;
2614 /* Let the error reporting routines know that we're outside a
2615 function. For a nested function, this value is used in
2616 pop_cp_function_context and then reset via pop_function_context. */
2617 current_function_decl = NULL_TREE;
2620 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2621 its sub-blocks. */
2623 static tree
2624 clear_decl_rtl (tree* tp,
2625 int* walk_subtrees ATTRIBUTE_UNUSED ,
2626 void* data ATTRIBUTE_UNUSED )
2628 if (nonstatic_local_decl_p (*tp))
2629 SET_DECL_RTL (*tp, NULL_RTX);
2631 return NULL_TREE;
2634 /* Perform initialization related to this module. */
2636 void
2637 init_cp_semantics (void)
2639 lang_expand_stmt = cp_expand_stmt;
2642 #include "gt-cp-semantics.h"