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)
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
30 #include "coretypes.h"
34 #include "tree-inline.h"
46 /* There routines provide a modular interface to perform many parsing
47 operations. They may therefore be used during actual parsing, or
48 during template instantiation, which may be regarded as a
49 degenerate form of parsing. Since the current g++ parser is
50 lacking in several respects, and will be reimplemented, we are
51 attempting to move most code that is not directly related to
52 parsing into this file; that will make implementing the new parser
53 much easier since it will be able to make use of these routines. */
55 static tree maybe_convert_cond (tree);
56 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
57 static void emit_associated_thunks (tree);
58 static void genrtl_try_block (tree);
59 static void genrtl_eh_spec_block (tree);
60 static void genrtl_handler (tree);
61 static void cp_expand_stmt (tree);
62 static void genrtl_start_function (tree);
63 static void genrtl_finish_function (tree);
64 static tree clear_decl_rtl (tree *, int *, void *);
66 /* Finish processing the COND, the SUBSTMT condition for STMT. */
68 #define FINISH_COND(COND, STMT, SUBSTMT) \
70 if (last_tree != (STMT)) \
72 RECHAIN_STMTS (STMT, SUBSTMT); \
73 if (!processing_template_decl) \
75 (COND) = build_tree_list (SUBSTMT, COND); \
83 /* Deferred Access Checking Overview
84 ---------------------------------
86 Most C++ expressions and declarations require access checking
87 to be performed during parsing. However, in several cases,
88 this has to be treated differently.
90 For member declarations, access checking has to be deferred
91 until more information about the declaration is known. For
103 When we are parsing the function return type `A::X', we don't
104 really know if this is allowed until we parse the function name.
106 Furthermore, some contexts require that access checking is
107 never performed at all. These include class heads, and template
110 Typical use of access checking functions is described here:
112 1. When we enter a context that requires certain access checking
113 mode, the function `push_deferring_access_checks' is called with
114 DEFERRING argument specifying the desired mode. Access checking
115 may be performed immediately (dk_no_deferred), deferred
116 (dk_deferred), or not performed (dk_no_check).
118 2. When a declaration such as a type, or a variable, is encountered,
119 the function `perform_or_defer_access_check' is called. It
120 maintains a TREE_LIST of all deferred checks.
122 3. The global `current_class_type' or `current_function_decl' is then
123 setup by the parser. `enforce_access' relies on these information
126 4. Upon exiting the context mentioned in step 1,
127 `perform_deferred_access_checks' is called to check all declaration
128 stored in the TREE_LIST. `pop_deferring_access_checks' is then
129 called to restore the previous access checking mode.
131 In case of parsing error, we simply call `pop_deferring_access_checks'
132 without `perform_deferred_access_checks'. */
134 /* Data for deferred access checking. */
135 static GTY(()) deferred_access *deferred_access_stack;
136 static GTY(()) deferred_access *deferred_access_free_list;
138 /* Save the current deferred access states and start deferred
139 access checking iff DEFER_P is true. */
141 void push_deferring_access_checks (deferring_kind deferring)
145 /* For context like template instantiation, access checking
146 disabling applies to all nested context. */
147 if (deferred_access_stack
148 && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
149 deferring = dk_no_check;
151 /* Recycle previously used free store if available. */
152 if (deferred_access_free_list)
154 d = deferred_access_free_list;
155 deferred_access_free_list = d->next;
158 d = ggc_alloc (sizeof (deferred_access));
160 d->next = deferred_access_stack;
161 d->deferred_access_checks = NULL_TREE;
162 d->deferring_access_checks_kind = deferring;
163 deferred_access_stack = d;
166 /* Resume deferring access checks again after we stopped doing
169 void resume_deferring_access_checks (void)
171 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
172 deferred_access_stack->deferring_access_checks_kind = dk_deferred;
175 /* Stop deferring access checks. */
177 void stop_deferring_access_checks (void)
179 if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
180 deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
183 /* Discard the current deferred access checks and restore the
186 void pop_deferring_access_checks (void)
188 deferred_access *d = deferred_access_stack;
189 deferred_access_stack = d->next;
191 /* Remove references to access checks TREE_LIST. */
192 d->deferred_access_checks = NULL_TREE;
194 /* Store in free list for later use. */
195 d->next = deferred_access_free_list;
196 deferred_access_free_list = d;
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 tree get_deferred_access_checks (void)
206 return deferred_access_stack->deferred_access_checks;
209 /* Take current deferred checks and combine with the
210 previous states if we also defer checks previously.
211 Otherwise perform checks now. */
213 void pop_to_parent_deferring_access_checks (void)
215 tree deferred_check = get_deferred_access_checks ();
216 deferred_access *d1 = deferred_access_stack;
217 deferred_access *d2 = deferred_access_stack->next;
218 deferred_access *d3 = deferred_access_stack->next->next;
220 /* Temporary swap the order of the top two states, just to make
221 sure the garbage collector will not reclaim the memory during
223 deferred_access_stack = d2;
227 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
228 /* Perform deferred check if required. */
229 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
230 TREE_VALUE (deferred_check));
232 deferred_access_stack = d1;
235 pop_deferring_access_checks ();
238 /* Perform the deferred access checks.
240 After performing the checks, we still have to keep the list
241 `deferred_access_stack->deferred_access_checks' since we may want
242 to check access for them again later in a different context.
249 A::X A::a, x; // No error for `A::a', error for `x'
251 We have to perform deferred access of `A::X', first with `A::a',
254 void perform_deferred_access_checks (void)
257 for (deferred_check = deferred_access_stack->deferred_access_checks;
259 deferred_check = TREE_CHAIN (deferred_check))
261 enforce_access (TREE_PURPOSE (deferred_check),
262 TREE_VALUE (deferred_check));
265 /* Defer checking the accessibility of DECL, when looked up in
268 void perform_or_defer_access_check (tree binfo, tree decl)
272 my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
274 /* If we are not supposed to defer access checks, just check now. */
275 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
277 enforce_access (binfo, decl);
280 /* Exit if we are in a context that no access checking is performed. */
281 else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
284 /* See if we are already going to perform this check. */
285 for (check = deferred_access_stack->deferred_access_checks;
287 check = TREE_CHAIN (check))
288 if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
290 /* If not, record the check. */
291 deferred_access_stack->deferred_access_checks
292 = tree_cons (binfo, decl,
293 deferred_access_stack->deferred_access_checks);
296 /* Returns nonzero if the current statement is a full expression,
297 i.e. temporaries created during that statement should be destroyed
298 at the end of the statement. */
301 stmts_are_full_exprs_p (void)
303 return current_stmt_tree ()->stmts_are_full_exprs_p;
306 /* Returns the stmt_tree (if any) to which statements are currently
307 being added. If there is no active statement-tree, NULL is
311 current_stmt_tree (void)
314 ? &cfun->language->base.x_stmt_tree
315 : &scope_chain->x_stmt_tree);
318 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
319 flag for this because "A union for which objects or pointers are
320 declared is not an anonymous union" [class.union]. */
323 anon_aggr_type_p (tree node)
325 return ANON_AGGR_TYPE_P (node);
328 /* Finish a scope. */
333 tree block = NULL_TREE;
335 if (stmts_are_full_exprs_p ())
337 tree scope_stmts = NULL_TREE;
339 block = poplevel (kept_level_p (), 1, 0);
340 if (!processing_template_decl)
342 /* This needs to come after the poplevel so that partial scopes
343 are properly nested. */
344 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
347 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
348 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
356 /* Begin a new scope. */
359 do_pushlevel (scope_kind sk)
361 if (stmts_are_full_exprs_p ())
363 if (!processing_template_decl)
364 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
369 /* Finish a goto-statement. */
372 finish_goto_stmt (tree destination)
374 if (TREE_CODE (destination) == IDENTIFIER_NODE)
375 destination = lookup_label (destination);
377 /* We warn about unused labels with -Wunused. That means we have to
378 mark the used labels as used. */
379 if (TREE_CODE (destination) == LABEL_DECL)
380 TREE_USED (destination) = 1;
382 if (TREE_CODE (destination) != LABEL_DECL)
383 /* We don't inline calls to functions with computed gotos.
384 Those functions are typically up to some funny business,
385 and may be depending on the labels being at particular
386 addresses, or some such. */
387 DECL_UNINLINABLE (current_function_decl) = 1;
389 check_goto (destination);
391 return add_stmt (build_stmt (GOTO_STMT, destination));
394 /* COND is the condition-expression for an if, while, etc.,
395 statement. Convert it to a boolean value, if appropriate. */
398 maybe_convert_cond (tree cond)
400 /* Empty conditions remain empty. */
404 /* Wait until we instantiate templates before doing conversion. */
405 if (processing_template_decl)
408 /* Do the conversion. */
409 cond = convert_from_reference (cond);
410 return condition_conversion (cond);
413 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
416 finish_expr_stmt (tree expr)
419 tree expr_type = NULL_TREE;;
421 if (expr != NULL_TREE)
423 if (!processing_template_decl
424 && !(stmts_are_full_exprs_p ())
425 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
427 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
428 expr = decay_conversion (expr);
430 /* Remember the type of the expression. */
431 expr_type = TREE_TYPE (expr);
433 if (stmts_are_full_exprs_p ())
434 expr = convert_to_void (expr, "statement");
436 r = add_stmt (build_stmt (EXPR_STMT, expr));
441 /* This was an expression-statement, so we save the type of the
443 last_expr_type = expr_type;
449 /* Begin an if-statement. Returns a newly created IF_STMT if
456 do_pushlevel (sk_block);
457 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
462 /* Process the COND of an if-statement, which may be given by
466 finish_if_stmt_cond (tree cond, tree if_stmt)
468 cond = maybe_convert_cond (cond);
469 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
472 /* Finish the then-clause of an if-statement, which may be given by
476 finish_then_clause (tree if_stmt)
478 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
482 /* Begin the else-clause of an if-statement. */
485 begin_else_clause (void)
489 /* Finish the else-clause of an if-statement, which may be given by
493 finish_else_clause (tree if_stmt)
495 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
498 /* Finish an if-statement. */
501 finish_if_stmt (void)
507 /* Begin a while-statement. Returns a newly created WHILE_STMT if
511 begin_while_stmt (void)
514 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
516 do_pushlevel (sk_block);
520 /* Process the COND of a while-statement, which may be given by
524 finish_while_stmt_cond (tree cond, tree while_stmt)
526 cond = maybe_convert_cond (cond);
527 if (processing_template_decl)
528 /* Don't mess with condition decls in a template. */
529 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
530 else if (getdecls () == NULL_TREE)
531 /* It was a simple condition; install it. */
532 WHILE_COND (while_stmt) = cond;
535 /* If there was a declaration in the condition, we can't leave it
539 while (true) { A x = 42; if (!x) break; } */
541 WHILE_COND (while_stmt) = boolean_true_node;
543 if_stmt = begin_if_stmt ();
544 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
545 finish_if_stmt_cond (cond, if_stmt);
546 finish_break_stmt ();
547 finish_then_clause (if_stmt);
552 /* Finish a while-statement, which may be given by WHILE_STMT. */
555 finish_while_stmt (tree while_stmt)
558 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
562 /* Begin a do-statement. Returns a newly created DO_STMT if
568 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
573 /* Finish the body of a do-statement, which may be given by DO_STMT. */
576 finish_do_body (tree do_stmt)
578 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
581 /* Finish a do-statement, which may be given by DO_STMT, and whose
582 COND is as indicated. */
585 finish_do_stmt (tree cond, tree do_stmt)
587 cond = maybe_convert_cond (cond);
588 DO_COND (do_stmt) = cond;
592 /* Finish a return-statement. The EXPRESSION returned, if any, is as
596 finish_return_stmt (tree expr)
600 expr = check_return_expr (expr);
601 if (!processing_template_decl)
603 if (DECL_DESTRUCTOR_P (current_function_decl))
605 /* Similarly, all destructors must run destructors for
606 base-classes before returning. So, all returns in a
607 destructor get sent to the DTOR_LABEL; finish_function emits
608 code to return a value there. */
609 return finish_goto_stmt (dtor_label);
612 r = add_stmt (build_stmt (RETURN_STMT, expr));
618 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
621 begin_for_stmt (void)
625 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
626 NULL_TREE, NULL_TREE);
627 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
628 if (NEW_FOR_SCOPE_P (r))
629 do_pushlevel (sk_for);
635 /* Finish the for-init-statement of a for-statement, which may be
636 given by FOR_STMT. */
639 finish_for_init_stmt (tree for_stmt)
641 if (last_tree != for_stmt)
642 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
643 do_pushlevel (sk_block);
646 /* Finish the COND of a for-statement, which may be given by
650 finish_for_cond (tree cond, tree for_stmt)
652 cond = maybe_convert_cond (cond);
653 if (processing_template_decl)
654 /* Don't mess with condition decls in a template. */
655 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
656 else if (getdecls () == NULL_TREE)
657 /* It was a simple condition; install it. */
658 FOR_COND (for_stmt) = cond;
661 /* If there was a declaration in the condition, we can't leave it
663 for (; A x = 42;) { }
665 for (;;) { A x = 42; if (!x) break; } */
667 FOR_COND (for_stmt) = NULL_TREE;
669 if_stmt = begin_if_stmt ();
670 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
671 finish_if_stmt_cond (cond, if_stmt);
672 finish_break_stmt ();
673 finish_then_clause (if_stmt);
678 /* Finish the increment-EXPRESSION in a for-statement, which may be
679 given by FOR_STMT. */
682 finish_for_expr (tree expr, tree for_stmt)
684 FOR_EXPR (for_stmt) = expr;
687 /* Finish the body of a for-statement, which may be given by
688 FOR_STMT. The increment-EXPR for the loop must be
692 finish_for_stmt (tree for_stmt)
694 /* Pop the scope for the body of the loop. */
696 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
697 if (NEW_FOR_SCOPE_P (for_stmt))
702 /* Finish a break-statement. */
705 finish_break_stmt (void)
707 return add_stmt (build_break_stmt ());
710 /* Finish a continue-statement. */
713 finish_continue_stmt (void)
715 return add_stmt (build_continue_stmt ());
718 /* Begin a switch-statement. Returns a new SWITCH_STMT if
722 begin_switch_stmt (void)
725 do_pushlevel (sk_block);
726 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
731 /* Finish the cond of a switch-statement. */
734 finish_switch_cond (tree cond, tree switch_stmt)
736 tree orig_type = NULL;
737 if (!processing_template_decl)
741 /* Convert the condition to an integer or enumeration type. */
742 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
743 if (cond == NULL_TREE)
745 error ("switch quantity not an integer");
746 cond = error_mark_node;
748 orig_type = TREE_TYPE (cond);
749 if (cond != error_mark_node)
753 Integral promotions are performed. */
754 cond = perform_integral_promotions (cond);
755 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
758 if (cond != error_mark_node)
760 index = get_unwidened (cond, NULL_TREE);
761 /* We can't strip a conversion from a signed type to an unsigned,
762 because if we did, int_fits_type_p would do the wrong thing
763 when checking case values for being in range,
764 and it's too hard to do the right thing. */
765 if (TREE_UNSIGNED (TREE_TYPE (cond))
766 == TREE_UNSIGNED (TREE_TYPE (index)))
770 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
771 SWITCH_TYPE (switch_stmt) = orig_type;
772 push_switch (switch_stmt);
775 /* Finish the body of a switch-statement, which may be given by
776 SWITCH_STMT. The COND to switch on is indicated. */
779 finish_switch_stmt (tree switch_stmt)
781 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
787 /* Generate the RTL for T, which is a TRY_BLOCK. */
790 genrtl_try_block (tree t)
794 expand_eh_region_start ();
795 expand_stmt (TRY_STMTS (t));
796 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
800 if (!FN_TRY_BLOCK_P (t))
801 emit_line_note (input_location);
803 expand_eh_region_start ();
804 expand_stmt (TRY_STMTS (t));
806 if (FN_TRY_BLOCK_P (t))
808 expand_start_all_catch ();
809 in_function_try_handler = 1;
810 expand_stmt (TRY_HANDLERS (t));
811 in_function_try_handler = 0;
812 expand_end_all_catch ();
816 expand_start_all_catch ();
817 expand_stmt (TRY_HANDLERS (t));
818 expand_end_all_catch ();
823 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
826 genrtl_eh_spec_block (tree t)
828 expand_eh_region_start ();
829 expand_stmt (EH_SPEC_STMTS (t));
830 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
831 build_call (call_unexpected_node,
832 tree_cons (NULL_TREE,
837 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
841 begin_try_block (void)
843 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
848 /* Likewise, for a function-try-block. */
851 begin_function_try_block (void)
853 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
854 FN_TRY_BLOCK_P (r) = 1;
859 /* Finish a try-block, which may be given by TRY_BLOCK. */
862 finish_try_block (tree try_block)
864 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
867 /* Finish the body of a cleanup try-block, which may be given by
871 finish_cleanup_try_block (tree try_block)
873 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
876 /* Finish an implicitly generated try-block, with a cleanup is given
880 finish_cleanup (tree cleanup, tree try_block)
882 TRY_HANDLERS (try_block) = cleanup;
883 CLEANUP_P (try_block) = 1;
886 /* Likewise, for a function-try-block. */
889 finish_function_try_block (tree try_block)
891 if (TREE_CHAIN (try_block)
892 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
894 /* Chain the compound statement after the CTOR_INITIALIZER. */
895 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
896 /* And make the CTOR_INITIALIZER the body of the try-block. */
897 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
900 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
901 in_function_try_handler = 1;
904 /* Finish a handler-sequence for a try-block, which may be given by
908 finish_handler_sequence (tree try_block)
910 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
911 check_handlers (TRY_HANDLERS (try_block));
914 /* Likewise, for a function-try-block. */
917 finish_function_handler_sequence (tree try_block)
919 in_function_try_handler = 0;
920 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
921 check_handlers (TRY_HANDLERS (try_block));
924 /* Generate the RTL for T, which is a HANDLER. */
927 genrtl_handler (tree t)
929 genrtl_do_pushlevel ();
930 if (!processing_template_decl)
931 expand_start_catch (HANDLER_TYPE (t));
932 expand_stmt (HANDLER_BODY (t));
933 if (!processing_template_decl)
937 /* Begin a handler. Returns a HANDLER if appropriate. */
943 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
945 /* Create a binding level for the eh_info and the exception object
947 do_pushlevel (sk_catch);
951 /* Finish the handler-parameters for a handler, which may be given by
952 HANDLER. DECL is the declaration for the catch parameter, or NULL
953 if this is a `catch (...)' clause. */
956 finish_handler_parms (tree decl, tree handler)
958 tree type = NULL_TREE;
959 if (processing_template_decl)
963 decl = pushdecl (decl);
964 decl = push_template_decl (decl);
965 add_decl_stmt (decl);
966 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
967 type = TREE_TYPE (decl);
971 type = expand_start_catch_block (decl);
973 HANDLER_TYPE (handler) = type;
976 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
977 the return value from the matching call to finish_handler_parms. */
980 finish_handler (tree handler)
982 if (!processing_template_decl)
983 expand_end_catch_block ();
985 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
988 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
989 compound-statement does not define a scope. Returns a new
990 COMPOUND_STMT if appropriate. */
993 begin_compound_stmt (int has_no_scope)
998 r = build_stmt (COMPOUND_STMT, NULL_TREE);
1000 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
1005 COMPOUND_STMT_NO_SCOPE (r) = 1;
1007 last_expr_type = NULL_TREE;
1010 do_pushlevel (is_try ? sk_try : sk_block);
1012 /* Normally, we try hard to keep the BLOCK for a
1013 statement-expression. But, if it's a statement-expression with
1014 a scopeless block, there's nothing to keep, and we don't want
1015 to accidentally keep a block *inside* the scopeless block. */
1016 keep_next_level (0);
1021 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1022 If HAS_NO_SCOPE is nonzero, the compound statement does not define
1026 finish_compound_stmt (int has_no_scope, tree compound_stmt)
1036 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1038 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1039 the precise purpose of that variable is store the type of the
1040 last expression statement within the last compound statement, we
1041 preserve the value. */
1049 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1050 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1054 finish_asm_stmt (tree cv_qualifier,
1056 tree output_operands,
1057 tree input_operands,
1063 if (cv_qualifier != NULL_TREE
1064 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1066 warning ("%s qualifier ignored on asm",
1067 IDENTIFIER_POINTER (cv_qualifier));
1068 cv_qualifier = NULL_TREE;
1071 if (!processing_template_decl)
1077 for (t = input_operands; t; t = TREE_CHAIN (t))
1079 tree converted_operand
1080 = decay_conversion (TREE_VALUE (t));
1082 /* If the type of the operand hasn't been determined (e.g.,
1083 because it involves an overloaded function), then issue
1084 an error message. There's no context available to
1085 resolve the overloading. */
1086 if (TREE_TYPE (converted_operand) == unknown_type_node)
1088 error ("type of asm operand `%E' could not be determined",
1090 converted_operand = error_mark_node;
1092 TREE_VALUE (t) = converted_operand;
1095 ninputs = list_length (input_operands);
1096 noutputs = list_length (output_operands);
1098 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1103 const char *constraint;
1106 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1107 operand = TREE_VALUE (t);
1109 if (!parse_output_constraint (&constraint,
1110 i, ninputs, noutputs,
1115 /* By marking this operand as erroneous, we will not try
1116 to process this operand again in expand_asm_operands. */
1117 TREE_VALUE (t) = error_mark_node;
1121 /* If the operand is a DECL that is going to end up in
1122 memory, assume it is addressable. This is a bit more
1123 conservative than it would ideally be; the exact test is
1124 buried deep in expand_asm_operands and depends on the
1125 DECL_RTL for the OPERAND -- which we don't have at this
1127 if (!allows_reg && DECL_P (operand))
1128 cxx_mark_addressable (operand);
1132 r = build_stmt (ASM_STMT, cv_qualifier, string,
1133 output_operands, input_operands,
1135 return add_stmt (r);
1138 /* Finish a label with the indicated NAME. */
1141 finish_label_stmt (tree name)
1143 tree decl = define_label (input_location, name);
1144 return add_stmt (build_stmt (LABEL_STMT, decl));
1147 /* Finish a series of declarations for local labels. G++ allows users
1148 to declare "local" labels, i.e., labels with scope. This extension
1149 is useful when writing code involving statement-expressions. */
1152 finish_label_decl (tree name)
1154 tree decl = declare_local_label (name);
1155 add_decl_stmt (decl);
1158 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1161 finish_decl_cleanup (tree decl, tree cleanup)
1163 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1166 /* If the current scope exits with an exception, run CLEANUP. */
1169 finish_eh_cleanup (tree cleanup)
1171 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1172 CLEANUP_EH_ONLY (r) = 1;
1176 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1177 order they were written by the user. Each node is as for
1178 emit_mem_initializers. */
1181 finish_mem_initializers (tree mem_inits)
1183 /* Reorder the MEM_INITS so that they are in the order they appeared
1184 in the source program. */
1185 mem_inits = nreverse (mem_inits);
1187 if (processing_template_decl)
1188 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1190 emit_mem_initializers (mem_inits);
1193 /* Returns the stack of SCOPE_STMTs for the current function. */
1196 current_scope_stmt_stack (void)
1198 return &cfun->language->base.x_scope_stmt_stack;
1201 /* Finish a parenthesized expression EXPR. */
1204 finish_parenthesized_expr (tree expr)
1206 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1207 /* This inhibits warnings in c_common_truthvalue_conversion. */
1208 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1210 if (TREE_CODE (expr) == OFFSET_REF)
1211 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1212 enclosed in parentheses. */
1213 PTRMEM_OK_P (expr) = 0;
1217 /* Finish a reference to a non-static data member (DECL) that is not
1218 preceded by `.' or `->'. */
1221 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1223 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1227 if (current_function_decl
1228 && DECL_STATIC_FUNCTION_P (current_function_decl))
1229 cp_error_at ("invalid use of member `%D' in static member function",
1232 cp_error_at ("invalid use of non-static data member `%D'", decl);
1233 error ("from this location");
1235 return error_mark_node;
1237 TREE_USED (current_class_ptr) = 1;
1238 if (processing_template_decl)
1240 tree type = TREE_TYPE (decl);
1242 if (TREE_CODE (type) == REFERENCE_TYPE)
1243 type = TREE_TYPE (type);
1246 /* Set the cv qualifiers */
1247 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1249 if (DECL_MUTABLE_P (decl))
1250 quals &= ~TYPE_QUAL_CONST;
1252 quals |= cp_type_quals (TREE_TYPE (decl));
1253 type = cp_build_qualified_type (type, quals);
1256 return build_min (COMPONENT_REF, type, object, decl);
1260 tree access_type = TREE_TYPE (object);
1261 tree lookup_context = context_for_name_lookup (decl);
1263 while (!DERIVED_FROM_P (lookup_context, access_type))
1265 access_type = TYPE_CONTEXT (access_type);
1266 while (access_type && DECL_P (access_type))
1267 access_type = DECL_CONTEXT (access_type);
1271 cp_error_at ("object missing in reference to `%D'", decl);
1272 error ("from this location");
1273 return error_mark_node;
1277 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1279 /* If the data member was named `C::M', convert `*this' to `C'
1281 if (qualifying_scope)
1283 tree binfo = NULL_TREE;
1284 object = build_scoped_ref (object, qualifying_scope,
1288 return build_class_member_access_expr (object, decl,
1289 /*access_path=*/NULL_TREE,
1290 /*preserve_reference=*/false);
1294 /* DECL was the declaration to which a qualified-id resolved. Issue
1295 an error message if it is not accessible. If OBJECT_TYPE is
1296 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1297 type of `*x', or `x', respectively. If the DECL was named as
1298 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1301 check_accessibility_of_qualified_id (tree decl,
1303 tree nested_name_specifier)
1306 tree qualifying_type = NULL_TREE;
1308 /* Determine the SCOPE of DECL. */
1309 scope = context_for_name_lookup (decl);
1310 /* If the SCOPE is not a type, then DECL is not a member. */
1311 if (!TYPE_P (scope))
1313 /* Compute the scope through which DECL is being accessed. */
1315 /* OBJECT_TYPE might not be a class type; consider:
1317 class A { typedef int I; };
1321 In this case, we will have "A::I" as the DECL, but "I" as the
1323 && CLASS_TYPE_P (object_type)
1324 && DERIVED_FROM_P (scope, object_type))
1325 /* If we are processing a `->' or `.' expression, use the type of the
1327 qualifying_type = object_type;
1328 else if (nested_name_specifier)
1330 /* If the reference is to a non-static member of the
1331 current class, treat it as if it were referenced through
1333 if (DECL_NONSTATIC_MEMBER_P (decl)
1334 && current_class_ptr
1335 && DERIVED_FROM_P (scope, current_class_type))
1336 qualifying_type = current_class_type;
1337 /* Otherwise, use the type indicated by the
1338 nested-name-specifier. */
1340 qualifying_type = nested_name_specifier;
1343 /* Otherwise, the name must be from the current class or one of
1345 qualifying_type = currently_open_derived_class (scope);
1347 if (qualifying_type)
1348 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1351 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1352 class named to the left of the "::" operator. DONE is true if this
1353 expression is a complete postfix-expression; it is false if this
1354 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1355 iff this expression is the operand of '&'. */
1358 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1361 if (error_operand_p (expr))
1362 return error_mark_node;
1364 /* If EXPR occurs as the operand of '&', use special handling that
1365 permits a pointer-to-member. */
1366 if (address_p && done)
1368 if (TREE_CODE (expr) == SCOPE_REF)
1369 expr = TREE_OPERAND (expr, 1);
1370 expr = build_offset_ref (qualifying_class, expr,
1371 /*address_p=*/true);
1375 if (TREE_CODE (expr) == FIELD_DECL)
1376 expr = finish_non_static_data_member (expr, current_class_ref,
1378 else if (BASELINK_P (expr) && !processing_template_decl)
1383 /* See if any of the functions are non-static members. */
1384 fns = BASELINK_FUNCTIONS (expr);
1385 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1386 fns = TREE_OPERAND (fns, 0);
1387 for (fn = fns; fn; fn = OVL_NEXT (fn))
1388 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1390 /* If so, the expression may be relative to the current
1392 if (fn && current_class_type
1393 && DERIVED_FROM_P (qualifying_class, current_class_type))
1394 expr = (build_class_member_access_expr
1395 (maybe_dummy_object (qualifying_class, NULL),
1397 BASELINK_ACCESS_BINFO (expr),
1398 /*preserve_reference=*/false));
1400 /* The expression is a qualified name whose address is not
1402 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1408 /* Begin a statement-expression. The value returned must be passed to
1409 finish_stmt_expr. */
1412 begin_stmt_expr (void)
1414 /* If we're outside a function, we won't have a statement-tree to
1415 work with. But, if we see a statement-expression we need to
1417 if (! cfun && !last_tree)
1418 begin_stmt_tree (&scope_chain->x_saved_tree);
1420 keep_next_level (1);
1421 /* If we're building a statement tree, then the upcoming compound
1422 statement will be chained onto the tree structure, starting at
1423 last_tree. We return last_tree so that we can later unhook the
1424 compound statement. */
1428 /* Finish a statement-expression. RTL_EXPR should be the value
1429 returned by the previous begin_stmt_expr; EXPR is the
1430 statement-expression. Returns an expression representing the
1431 statement-expression. */
1434 finish_stmt_expr (tree rtl_expr)
1438 /* If the last thing in the statement-expression was not an
1439 expression-statement, then it has type `void'. In a template, we
1440 cannot distinguish the case where the last expression-statement
1441 had a dependent type from the case where the last statement was
1442 not an expression-statement. Therefore, we (incorrectly) treat
1443 the STMT_EXPR as dependent in that case. */
1444 if (!last_expr_type && !processing_template_decl)
1445 last_expr_type = void_type_node;
1446 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1447 TREE_SIDE_EFFECTS (result) = 1;
1449 /* Remove the compound statement from the tree structure; it is
1450 now saved in the STMT_EXPR. */
1451 last_tree = rtl_expr;
1452 TREE_CHAIN (last_tree) = NULL_TREE;
1454 /* If we created a statement-tree for this statement-expression,
1457 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1458 finish_stmt_tree (&scope_chain->x_saved_tree);
1463 /* Perform Koenig lookup. FN is the postfix-expression representing
1464 the call; ARGS are the arguments to the call. Returns the
1465 functions to be considered by overload resolution. */
1468 perform_koenig_lookup (tree fn, tree args)
1470 tree identifier = NULL_TREE;
1471 tree functions = NULL_TREE;
1473 /* Find the name of the overloaded function. */
1474 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1476 else if (is_overloaded_fn (fn))
1479 identifier = DECL_NAME (get_first_fn (functions));
1481 else if (DECL_P (fn))
1484 identifier = DECL_NAME (fn);
1487 /* A call to a namespace-scope function using an unqualified name.
1489 Do Koenig lookup -- unless any of the arguments are
1491 if (!any_type_dependent_arguments_p (args))
1493 fn = lookup_arg_dependent (identifier, functions, args);
1495 /* The unqualified name could not be resolved. */
1496 fn = unqualified_fn_lookup_error (identifier);
1504 /* Generate an expression for `FN (ARGS)'.
1506 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1507 as a virtual call, even if FN is virtual. (This flag is set when
1508 encountering an expression where the function name is explicitly
1509 qualified. For example a call to `X::f' never generates a virtual
1512 Returns code for the call. */
1515 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1521 if (fn == error_mark_node || args == error_mark_node)
1522 return error_mark_node;
1524 /* ARGS should be a list of arguments. */
1525 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1531 if (processing_template_decl)
1533 if (type_dependent_expression_p (fn)
1534 || any_type_dependent_arguments_p (args))
1535 return build_nt (CALL_EXPR, fn, args);
1536 if (!BASELINK_P (fn)
1537 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1538 && TREE_TYPE (fn) != unknown_type_node)
1539 fn = build_non_dependent_expr (fn);
1540 args = build_non_dependent_args (orig_args);
1543 /* A reference to a member function will appear as an overloaded
1544 function (rather than a BASELINK) if an unqualified name was used
1546 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1550 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1551 f = get_first_fn (TREE_OPERAND (fn, 0));
1553 f = get_first_fn (fn);
1554 if (DECL_FUNCTION_MEMBER_P (f))
1556 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1557 fn = build_baselink (TYPE_BINFO (type),
1559 fn, /*optype=*/NULL_TREE);
1564 if (BASELINK_P (fn))
1568 /* A call to a member function. From [over.call.func]:
1570 If the keyword this is in scope and refers to the class of
1571 that member function, or a derived class thereof, then the
1572 function call is transformed into a qualified function call
1573 using (*this) as the postfix-expression to the left of the
1574 . operator.... [Otherwise] a contrived object of type T
1575 becomes the implied object argument.
1577 This paragraph is unclear about this situation:
1579 struct A { void f(); };
1580 struct B : public A {};
1581 struct C : public A { void g() { B::f(); }};
1583 In particular, for `B::f', this paragraph does not make clear
1584 whether "the class of that member function" refers to `A' or
1585 to `B'. We believe it refers to `B'. */
1586 if (current_class_type
1587 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1589 && current_class_ref)
1590 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1594 tree representative_fn;
1596 representative_fn = BASELINK_FUNCTIONS (fn);
1597 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1598 representative_fn = TREE_OPERAND (representative_fn, 0);
1599 representative_fn = get_first_fn (representative_fn);
1600 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1603 if (processing_template_decl)
1605 if (type_dependent_expression_p (object))
1606 return build_nt (CALL_EXPR, orig_fn, orig_args);
1607 object = build_non_dependent_expr (object);
1610 result = build_new_method_call (object, fn, args, NULL_TREE,
1612 ? LOOKUP_NONVIRTUAL : 0));
1614 else if (is_overloaded_fn (fn))
1615 /* A call to a namespace-scope function. */
1616 result = build_new_function_call (fn, args);
1617 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1620 error ("arguments to destructor are not allowed");
1621 /* Mark the pseudo-destructor call as having side-effects so
1622 that we do not issue warnings about its use. */
1623 result = build1 (NOP_EXPR,
1625 TREE_OPERAND (fn, 0));
1626 TREE_SIDE_EFFECTS (result) = 1;
1628 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1629 /* If the "function" is really an object of class type, it might
1630 have an overloaded `operator ()'. */
1631 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1633 /* A call where the function is unknown. */
1634 result = build_function_call (fn, args);
1636 if (processing_template_decl)
1637 return build (CALL_EXPR, TREE_TYPE (result), orig_fn, orig_args);
1641 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1642 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1643 POSTDECREMENT_EXPR.) */
1646 finish_increment_expr (tree expr, enum tree_code code)
1648 return build_x_unary_op (code, expr);
1651 /* Finish a use of `this'. Returns an expression for `this'. */
1654 finish_this_expr (void)
1658 if (current_class_ptr)
1660 result = current_class_ptr;
1662 else if (current_function_decl
1663 && DECL_STATIC_FUNCTION_P (current_function_decl))
1665 error ("`this' is unavailable for static member functions");
1666 result = error_mark_node;
1670 if (current_function_decl)
1671 error ("invalid use of `this' in non-member function");
1673 error ("invalid use of `this' at top level");
1674 result = error_mark_node;
1680 /* Finish a member function call using OBJECT and ARGS as arguments to
1681 FN. Returns an expression for the call. */
1684 finish_object_call_expr (tree fn, tree object, tree args)
1686 if (DECL_DECLARES_TYPE_P (fn))
1688 if (processing_template_decl)
1689 /* This can happen on code like:
1692 template <class T> void f(T t) {
1696 We just grab the underlying IDENTIFIER. */
1697 fn = DECL_NAME (fn);
1700 error ("calling type `%T' like a method", fn);
1701 return error_mark_node;
1705 if (processing_template_decl)
1706 return build_nt (CALL_EXPR,
1707 build_nt (COMPONENT_REF, object, fn),
1711 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1713 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1716 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1717 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1718 the TYPE for the type given. If SCOPE is non-NULL, the expression
1719 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1722 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1724 if (destructor == error_mark_node)
1725 return error_mark_node;
1727 my_friendly_assert (TYPE_P (destructor), 20010905);
1729 if (!processing_template_decl)
1731 if (scope == error_mark_node)
1733 error ("invalid qualifying scope in pseudo-destructor name");
1734 return error_mark_node;
1737 if (!same_type_p (TREE_TYPE (object), destructor))
1739 error ("`%E' is not of type `%T'", object, destructor);
1740 return error_mark_node;
1744 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1747 /* Finish an expression of the form CODE EXPR. */
1750 finish_unary_op_expr (enum tree_code code, tree expr)
1752 tree result = build_x_unary_op (code, expr);
1753 /* Inside a template, build_x_unary_op does not fold the
1754 expression. So check whether the result is folded before
1755 setting TREE_NEGATED_INT. */
1756 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1757 && TREE_CODE (result) == INTEGER_CST
1758 && !TREE_UNSIGNED (TREE_TYPE (result))
1759 && INT_CST_LT (result, integer_zero_node))
1760 TREE_NEGATED_INT (result) = 1;
1761 overflow_warning (result);
1765 /* Finish a compound-literal expression. TYPE is the type to which
1766 the INITIALIZER_LIST is being cast. */
1769 finish_compound_literal (tree type, tree initializer_list)
1771 tree compound_literal;
1773 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1774 compound_literal = build_constructor (NULL_TREE, initializer_list);
1775 /* Mark it as a compound-literal. */
1776 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1777 if (processing_template_decl)
1778 TREE_TYPE (compound_literal) = type;
1781 /* Check the initialization. */
1782 compound_literal = digest_init (type, compound_literal, NULL);
1783 /* If the TYPE was an array type with an unknown bound, then we can
1784 figure out the dimension now. For example, something like:
1788 implies that the array has two elements. */
1789 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1790 complete_array_type (type, compound_literal, 1);
1793 return compound_literal;
1796 /* Return the declaration for the function-name variable indicated by
1800 finish_fname (tree id)
1804 decl = fname_decl (C_RID_CODE (id), id);
1805 if (processing_template_decl)
1806 decl = DECL_NAME (decl);
1810 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1811 and DECLARATOR. Returns nonzero if the function-declaration is
1815 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1817 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1820 /* The things we're about to see are not directly qualified by any
1821 template headers we've seen thus far. */
1822 reset_specialization ();
1827 /* Finish a translation unit. */
1830 finish_translation_unit (void)
1832 /* In case there were missing closebraces,
1833 get us back to the global binding level. */
1835 while (current_namespace != global_namespace)
1838 /* Do file scope __FUNCTION__ et al. */
1839 finish_fname_decls ();
1842 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1843 Returns the parameter. */
1846 finish_template_type_parm (tree aggr, tree identifier)
1848 if (aggr != class_type_node)
1850 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1851 aggr = class_type_node;
1854 return build_tree_list (aggr, identifier);
1857 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1858 Returns the parameter. */
1861 finish_template_template_parm (tree aggr, tree identifier)
1863 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1864 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1865 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1866 DECL_TEMPLATE_RESULT (tmpl) = decl;
1867 DECL_ARTIFICIAL (decl) = 1;
1868 end_template_decl ();
1870 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1872 return finish_template_type_parm (aggr, tmpl);
1875 /* ARGUMENT is the default-argument value for a template template
1876 parameter. If ARGUMENT is invalid, issue error messages and return
1877 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1880 check_template_template_default_arg (tree argument)
1882 if (TREE_CODE (argument) != TEMPLATE_DECL
1883 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1884 && TREE_CODE (argument) != TYPE_DECL
1885 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1887 error ("invalid default template argument");
1888 return error_mark_node;
1894 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1895 nonzero, the parameter list was terminated by a `...'. */
1898 finish_parmlist (tree parms, int ellipsis)
1902 /* We mark the PARMS as a parmlist so that declarator processing can
1903 disambiguate certain constructs. */
1904 TREE_PARMLIST (parms) = 1;
1905 /* We do not append void_list_node here, but leave it to grokparms
1907 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1912 /* Begin a class definition, as indicated by T. */
1915 begin_class_definition (tree t)
1917 if (t == error_mark_node)
1918 return error_mark_node;
1920 if (processing_template_parmlist)
1922 error ("definition of `%#T' inside template parameter list", t);
1923 return error_mark_node;
1925 /* A non-implicit typename comes from code like:
1927 template <typename T> struct A {
1928 template <typename U> struct A<T>::B ...
1930 This is erroneous. */
1931 else if (TREE_CODE (t) == TYPENAME_TYPE)
1933 error ("invalid definition of qualified type `%T'", t);
1934 t = error_mark_node;
1937 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1939 t = make_aggr_type (RECORD_TYPE);
1940 pushtag (make_anon_name (), t, 0);
1943 /* If this type was already complete, and we see another definition,
1945 if (COMPLETE_TYPE_P (t))
1947 error ("redefinition of `%#T'", t);
1948 cp_error_at ("previous definition of `%#T'", t);
1949 return error_mark_node;
1952 /* Update the location of the decl. */
1953 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
1955 if (TYPE_BEING_DEFINED (t))
1957 t = make_aggr_type (TREE_CODE (t));
1958 pushtag (TYPE_IDENTIFIER (t), t, 0);
1960 maybe_process_partial_specialization (t);
1961 pushclass (t, true);
1962 TYPE_BEING_DEFINED (t) = 1;
1963 TYPE_PACKED (t) = flag_pack_struct;
1964 /* Reset the interface data, at the earliest possible
1965 moment, as it might have been set via a class foo;
1967 if (! TYPE_ANONYMOUS_P (t))
1969 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1970 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1971 (t, interface_unknown);
1973 reset_specialization();
1975 /* Make a declaration for this class in its own scope. */
1976 build_self_reference ();
1981 /* Finish the member declaration given by DECL. */
1984 finish_member_declaration (tree decl)
1986 if (decl == error_mark_node || decl == NULL_TREE)
1989 if (decl == void_type_node)
1990 /* The COMPONENT was a friend, not a member, and so there's
1991 nothing for us to do. */
1994 /* We should see only one DECL at a time. */
1995 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1997 /* Set up access control for DECL. */
1999 = (current_access_specifier == access_private_node);
2000 TREE_PROTECTED (decl)
2001 = (current_access_specifier == access_protected_node);
2002 if (TREE_CODE (decl) == TEMPLATE_DECL)
2004 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2005 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2008 /* Mark the DECL as a member of the current class. */
2009 DECL_CONTEXT (decl) = current_class_type;
2013 A C language linkage is ignored for the names of class members
2014 and the member function type of class member functions. */
2015 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2016 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2018 /* Put functions on the TYPE_METHODS list and everything else on the
2019 TYPE_FIELDS list. Note that these are built up in reverse order.
2020 We reverse them (to obtain declaration order) in finish_struct. */
2021 if (TREE_CODE (decl) == FUNCTION_DECL
2022 || DECL_FUNCTION_TEMPLATE_P (decl))
2024 /* We also need to add this function to the
2025 CLASSTYPE_METHOD_VEC. */
2026 add_method (current_class_type, decl, /*error_p=*/0);
2028 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2029 TYPE_METHODS (current_class_type) = decl;
2031 maybe_add_class_template_decl_list (current_class_type, decl,
2034 /* Enter the DECL into the scope of the class. */
2035 else if (TREE_CODE (decl) == USING_DECL || pushdecl_class_level (decl))
2037 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2038 go at the beginning. The reason is that lookup_field_1
2039 searches the list in order, and we want a field name to
2040 override a type name so that the "struct stat hack" will
2041 work. In particular:
2043 struct S { enum E { }; int E } s;
2046 is valid. In addition, the FIELD_DECLs must be maintained in
2047 declaration order so that class layout works as expected.
2048 However, we don't need that order until class layout, so we
2049 save a little time by putting FIELD_DECLs on in reverse order
2050 here, and then reversing them in finish_struct_1. (We could
2051 also keep a pointer to the correct insertion points in the
2054 if (TREE_CODE (decl) == TYPE_DECL)
2055 TYPE_FIELDS (current_class_type)
2056 = chainon (TYPE_FIELDS (current_class_type), decl);
2059 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2060 TYPE_FIELDS (current_class_type) = decl;
2063 maybe_add_class_template_decl_list (current_class_type, decl,
2068 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
2069 the definition is immediately followed by a semicolon. Returns the
2073 finish_class_definition (tree t, tree attributes, int semi, int pop_scope_p)
2075 if (t == error_mark_node)
2076 return error_mark_node;
2078 /* finish_struct nukes this anyway; if finish_exception does too,
2081 note_got_semicolon (t);
2083 /* If we got any attributes in class_head, xref_tag will stick them in
2084 TREE_TYPE of the type. Grab them now. */
2085 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
2086 TYPE_ATTRIBUTES (t) = NULL_TREE;
2088 if (TREE_CODE (t) == ENUMERAL_TYPE)
2092 t = finish_struct (t, attributes);
2094 note_got_semicolon (t);
2098 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2103 /* Finish processing the declaration of a member class template
2104 TYPES whose template parameters are given by PARMS. */
2107 finish_member_class_template (tree types)
2111 /* If there are declared, but undefined, partial specializations
2112 mixed in with the typespecs they will not yet have passed through
2113 maybe_process_partial_specialization, so we do that here. */
2114 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2115 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2116 maybe_process_partial_specialization (TREE_VALUE (t));
2118 note_list_got_semicolon (types);
2119 grok_x_components (types);
2120 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2121 /* The component was in fact a friend declaration. We avoid
2122 finish_member_template_decl performing certain checks by
2126 finish_member_template_decl (types);
2128 /* As with other component type declarations, we do
2129 not store the new DECL on the list of
2134 /* Finish processing a complete template declaration. The PARMS are
2135 the template parameters. */
2138 finish_template_decl (tree parms)
2141 end_template_decl ();
2143 end_specialization ();
2146 /* Finish processing a template-id (which names a type) of the form
2147 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2148 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2149 the scope of template-id indicated. */
2152 finish_template_type (tree name, tree args, int entering_scope)
2156 decl = lookup_template_class (name, args,
2157 NULL_TREE, NULL_TREE, entering_scope,
2158 tf_error | tf_warning | tf_user);
2159 if (decl != error_mark_node)
2160 decl = TYPE_STUB_DECL (decl);
2165 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2166 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2167 BASE_CLASS, or NULL_TREE if an error occurred. The
2168 ACCESS_SPECIFIER is one of
2169 access_{default,public,protected_private}[_virtual]_node.*/
2172 finish_base_specifier (tree base, tree access, bool virtual_p)
2176 if (base == error_mark_node)
2178 error ("invalid base-class specification");
2181 else if (! is_aggr_type (base, 1))
2185 if (cp_type_quals (base) != 0)
2187 error ("base class `%T' has cv qualifiers", base);
2188 base = TYPE_MAIN_VARIANT (base);
2190 result = build_tree_list (access, base);
2191 TREE_VIA_VIRTUAL (result) = virtual_p;
2197 /* Called when multiple declarators are processed. If that is not
2198 premitted in this context, an error is issued. */
2201 check_multiple_declarators (void)
2205 In a template-declaration, explicit specialization, or explicit
2206 instantiation the init-declarator-list in the declaration shall
2207 contain at most one declarator.
2209 We don't just use PROCESSING_TEMPLATE_DECL for the first
2210 condition since that would disallow the perfectly valid code,
2211 like `template <class T> struct S { int i, j; };'. */
2212 if (at_function_scope_p ())
2213 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2216 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2217 || processing_explicit_instantiation
2218 || processing_specialization)
2219 error ("multiple declarators in template declaration");
2222 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2225 qualified_name_lookup_error (tree scope, tree name)
2229 if (!COMPLETE_TYPE_P (scope))
2230 error ("incomplete type `%T' used in nested name specifier", scope);
2232 error ("`%D' is not a member of `%T'", name, scope);
2234 else if (scope != global_namespace)
2235 error ("`%D' is not a member of `%D'", name, scope);
2237 error ("`::%D' has not been declared", name);
2240 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2241 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2242 if non-NULL, is the type or namespace used to explicitly qualify
2243 ID_EXPRESSION. DECL is the entity to which that name has been
2246 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2247 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2248 be set to true if this expression isn't permitted in a
2249 constant-expression, but it is otherwise not set by this function.
2250 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2251 constant-expression, but a non-constant expression is also
2254 If an error occurs, and it is the kind of error that might cause
2255 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2256 is the caller's responsibility to issue the message. *ERROR_MSG
2257 will be a string with static storage duration, so the caller need
2260 Return an expression for the entity, after issuing appropriate
2261 diagnostics. This function is also responsible for transforming a
2262 reference to a non-static member into a COMPONENT_REF that makes
2263 the use of "this" explicit.
2265 Upon return, *IDK will be filled in appropriately. */
2268 finish_id_expression (tree id_expression,
2272 tree *qualifying_class,
2273 bool constant_expression_p,
2274 bool allow_non_constant_expression_p,
2275 bool *non_constant_expression_p,
2276 const char **error_msg)
2278 /* Initialize the output parameters. */
2279 *idk = CP_ID_KIND_NONE;
2282 if (id_expression == error_mark_node)
2283 return error_mark_node;
2284 /* If we have a template-id, then no further lookup is
2285 required. If the template-id was for a template-class, we
2286 will sometimes have a TYPE_DECL at this point. */
2287 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2288 || TREE_CODE (decl) == TYPE_DECL)
2290 /* Look up the name. */
2293 if (decl == error_mark_node)
2295 /* Name lookup failed. */
2296 if (scope && (!TYPE_P (scope) || !dependent_type_p (scope)))
2298 /* Qualified name lookup failed, and the qualifying name
2299 was not a dependent type. That is always an
2301 qualified_name_lookup_error (scope, id_expression);
2302 return error_mark_node;
2306 /* It may be resolved via Koenig lookup. */
2307 *idk = CP_ID_KIND_UNQUALIFIED;
2308 return id_expression;
2311 /* If DECL is a variable that would be out of scope under
2312 ANSI/ISO rules, but in scope in the ARM, name lookup
2313 will succeed. Issue a diagnostic here. */
2315 decl = check_for_out_of_scope_variable (decl);
2317 /* Remember that the name was used in the definition of
2318 the current class so that we can check later to see if
2319 the meaning would have been different after the class
2320 was entirely defined. */
2321 if (!scope && decl != error_mark_node)
2322 maybe_note_name_used_in_class (id_expression, decl);
2325 /* If we didn't find anything, or what we found was a type,
2326 then this wasn't really an id-expression. */
2327 if (TREE_CODE (decl) == TEMPLATE_DECL
2328 && !DECL_FUNCTION_TEMPLATE_P (decl))
2330 *error_msg = "missing template arguments";
2331 return error_mark_node;
2333 else if (TREE_CODE (decl) == TYPE_DECL
2334 || TREE_CODE (decl) == NAMESPACE_DECL)
2336 *error_msg = "expected primary-expression";
2337 return error_mark_node;
2340 /* If the name resolved to a template parameter, there is no
2341 need to look it up again later. Similarly, we resolve
2342 enumeration constants to their underlying values. */
2343 if (TREE_CODE (decl) == CONST_DECL)
2345 *idk = CP_ID_KIND_NONE;
2346 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2347 return DECL_INITIAL (decl);
2354 /* If the declaration was explicitly qualified indicate
2355 that. The semantics of `A::f(3)' are different than
2356 `f(3)' if `f' is virtual. */
2358 ? CP_ID_KIND_QUALIFIED
2359 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2360 ? CP_ID_KIND_TEMPLATE_ID
2361 : CP_ID_KIND_UNQUALIFIED));
2366 An id-expression is type-dependent if it contains an
2367 identifier that was declared with a dependent type.
2369 The standard is not very specific about an id-expression that
2370 names a set of overloaded functions. What if some of them
2371 have dependent types and some of them do not? Presumably,
2372 such a name should be treated as a dependent name. */
2373 /* Assume the name is not dependent. */
2374 dependent_p = false;
2375 if (!processing_template_decl)
2376 /* No names are dependent outside a template. */
2378 /* A template-id where the name of the template was not resolved
2379 is definitely dependent. */
2380 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2381 && (TREE_CODE (TREE_OPERAND (decl, 0))
2382 == IDENTIFIER_NODE))
2384 /* For anything except an overloaded function, just check its
2386 else if (!is_overloaded_fn (decl))
2388 = dependent_type_p (TREE_TYPE (decl));
2389 /* For a set of overloaded functions, check each of the
2395 if (BASELINK_P (fns))
2396 fns = BASELINK_FUNCTIONS (fns);
2398 /* For a template-id, check to see if the template
2399 arguments are dependent. */
2400 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2402 tree args = TREE_OPERAND (fns, 1);
2403 dependent_p = any_dependent_template_arguments_p (args);
2404 /* The functions are those referred to by the
2406 fns = TREE_OPERAND (fns, 0);
2409 /* If there are no dependent template arguments, go through
2410 the overlaoded functions. */
2411 while (fns && !dependent_p)
2413 tree fn = OVL_CURRENT (fns);
2415 /* Member functions of dependent classes are
2417 if (TREE_CODE (fn) == FUNCTION_DECL
2418 && type_dependent_expression_p (fn))
2420 else if (TREE_CODE (fn) == TEMPLATE_DECL
2421 && dependent_template_p (fn))
2424 fns = OVL_NEXT (fns);
2428 /* If the name was dependent on a template parameter, we will
2429 resolve the name at instantiation time. */
2432 /* Create a SCOPE_REF for qualified names, if the scope is
2437 *qualifying_class = scope;
2438 /* Since this name was dependent, the expression isn't
2439 constant -- yet. No error is issued because it might
2440 be constant when things are instantiated. */
2441 if (constant_expression_p)
2442 *non_constant_expression_p = true;
2443 if (TYPE_P (scope) && dependent_type_p (scope))
2444 return build_nt (SCOPE_REF, scope, id_expression);
2445 else if (TYPE_P (scope) && DECL_P (decl))
2446 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2451 /* A TEMPLATE_ID already contains all the information we
2453 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2454 return id_expression;
2455 /* Since this name was dependent, the expression isn't
2456 constant -- yet. No error is issued because it might be
2457 constant when things are instantiated. */
2458 if (constant_expression_p)
2459 *non_constant_expression_p = true;
2460 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2461 return id_expression;
2464 /* Only certain kinds of names are allowed in constant
2465 expression. Enumerators have already been handled above. */
2466 if (constant_expression_p)
2468 /* Non-type template parameters of integral or enumeration
2470 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2471 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2473 /* Const variables or static data members of integral or
2474 enumeration types initialized with constant expressions
2476 else if (TREE_CODE (decl) == VAR_DECL
2477 && CP_TYPE_CONST_P (TREE_TYPE (decl))
2478 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2479 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2483 if (!allow_non_constant_expression_p)
2485 error ("`%D' cannot appear in a constant-expression", decl);
2486 return error_mark_node;
2488 *non_constant_expression_p = true;
2494 decl = (adjust_result_of_qualified_name_lookup
2495 (decl, scope, current_class_type));
2496 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2497 *qualifying_class = scope;
2498 else if (!processing_template_decl)
2499 decl = convert_from_reference (decl);
2500 else if (TYPE_P (scope))
2501 decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2503 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2505 error ("use of namespace `%D' as expression", decl);
2506 return error_mark_node;
2508 else if (DECL_CLASS_TEMPLATE_P (decl))
2510 error ("use of class template `%T' as expression", decl);
2511 return error_mark_node;
2513 else if (TREE_CODE (decl) == TREE_LIST)
2515 /* Ambiguous reference to base members. */
2516 error ("request for member `%D' is ambiguous in "
2517 "multiple inheritance lattice", id_expression);
2518 print_candidates (decl);
2519 return error_mark_node;
2521 else if (TREE_CODE (decl) == FIELD_DECL)
2522 decl = finish_non_static_data_member (decl, current_class_ref,
2523 /*qualifying_scope=*/NULL_TREE);
2524 else if (is_overloaded_fn (decl))
2526 tree first_fn = OVL_CURRENT (decl);
2528 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2529 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2531 if (TREE_CODE (first_fn) == FUNCTION_DECL
2532 && DECL_FUNCTION_MEMBER_P (first_fn))
2534 /* A set of member functions. */
2535 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2536 return finish_class_member_access_expr (decl, id_expression);
2538 else if (!really_overloaded_fn (decl))
2539 /* not really overloaded function */
2540 mark_used (first_fn);
2544 if (TREE_CODE (decl) == VAR_DECL
2545 || TREE_CODE (decl) == PARM_DECL
2546 || TREE_CODE (decl) == RESULT_DECL)
2548 tree context = decl_function_context (decl);
2550 if (context != NULL_TREE && context != current_function_decl
2551 && ! TREE_STATIC (decl))
2553 error ("use of %s from containing function",
2554 (TREE_CODE (decl) == VAR_DECL
2555 ? "`auto' variable" : "parameter"));
2556 cp_error_at (" `%#D' declared here", decl);
2557 return error_mark_node;
2561 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2562 && DECL_CLASS_SCOPE_P (decl)
2563 && DECL_CONTEXT (decl) != current_class_type)
2567 path = currently_open_derived_class (DECL_CONTEXT (decl));
2568 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2573 if (! processing_template_decl)
2574 decl = convert_from_reference (decl);
2577 /* Resolve references to variables of anonymous unions
2578 into COMPONENT_REFs. */
2579 if (TREE_CODE (decl) == ALIAS_DECL)
2580 decl = DECL_INITIAL (decl);
2583 if (TREE_DEPRECATED (decl))
2584 warn_deprecated_use (decl);
2589 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2590 use as a type-specifier. */
2593 finish_typeof (tree expr)
2597 if (type_dependent_expression_p (expr))
2599 type = make_aggr_type (TYPEOF_TYPE);
2600 TYPE_FIELDS (type) = expr;
2605 type = TREE_TYPE (expr);
2607 if (!type || type == unknown_type_node)
2609 error ("type of `%E' is unknown", expr);
2610 return error_mark_node;
2616 /* Compute the value of the `sizeof' operator. */
2619 finish_sizeof (tree t)
2621 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2624 /* Implement the __alignof keyword: Return the minimum required
2625 alignment of T, measured in bytes. */
2628 finish_alignof (tree t)
2630 if (processing_template_decl)
2631 return build_min (ALIGNOF_EXPR, size_type_node, t);
2633 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2636 /* Generate RTL for the statement T, and its substatements, and any
2637 other statements at its nesting level. */
2640 cp_expand_stmt (tree t)
2642 switch (TREE_CODE (t))
2645 genrtl_try_block (t);
2649 genrtl_eh_spec_block (t);
2665 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2666 will equivalent CALL_EXPRs. */
2669 simplify_aggr_init_exprs_r (tree* tp,
2670 int* walk_subtrees ATTRIBUTE_UNUSED ,
2671 void* data ATTRIBUTE_UNUSED )
2673 tree aggr_init_expr;
2679 enum style_t { ctor, arg, pcc } style;
2681 aggr_init_expr = *tp;
2682 /* We don't need to walk into types; there's nothing in a type that
2683 needs simplification. (And, furthermore, there are places we
2684 actively don't want to go. For example, we don't want to wander
2685 into the default arguments for a FUNCTION_DECL that appears in a
2687 if (TYPE_P (aggr_init_expr))
2692 /* Only AGGR_INIT_EXPRs are interesting. */
2693 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2696 /* Form an appropriate CALL_EXPR. */
2697 fn = TREE_OPERAND (aggr_init_expr, 0);
2698 args = TREE_OPERAND (aggr_init_expr, 1);
2699 slot = TREE_OPERAND (aggr_init_expr, 2);
2700 type = TREE_TYPE (aggr_init_expr);
2702 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2704 #ifdef PCC_STATIC_STRUCT_RETURN
2708 else if (TREE_ADDRESSABLE (type))
2711 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2712 handling. See build_cplus_new. */
2715 if (style == ctor || style == arg)
2717 /* Pass the address of the slot. If this is a constructor, we
2718 replace the first argument; otherwise, we tack on a new one. */
2720 args = TREE_CHAIN (args);
2722 cxx_mark_addressable (slot);
2723 args = tree_cons (NULL_TREE,
2725 build_pointer_type (TREE_TYPE (slot)),
2730 call_expr = build (CALL_EXPR,
2731 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2732 fn, args, NULL_TREE);
2733 TREE_SIDE_EFFECTS (call_expr) = 1;
2736 /* Tell the backend that we've added our return slot to the argument
2738 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2739 else if (style == pcc)
2741 /* If we're using the non-reentrant PCC calling convention, then we
2742 need to copy the returned value out of the static buffer into the
2744 push_deferring_access_checks (dk_no_check);
2745 call_expr = build_aggr_init (slot, call_expr,
2746 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2747 pop_deferring_access_checks ();
2750 /* We want to use the value of the initialized location as the
2752 call_expr = build (COMPOUND_EXPR, type,
2755 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2756 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2759 /* Keep iterating. */
2763 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2766 emit_associated_thunks (tree fn)
2768 /* When we use vcall offsets, we emit thunks with the virtual
2769 functions to which they thunk. The whole point of vcall offsets
2770 is so that you can know statically the entire set of thunks that
2771 will ever be needed for a given virtual function, thereby
2772 enabling you to output all the thunks with the function itself. */
2773 if (DECL_VIRTUAL_P (fn))
2777 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2779 use_thunk (thunk, /*emit_p=*/1);
2780 if (DECL_RESULT_THUNK_P (thunk))
2784 for (probe = DECL_THUNKS (thunk);
2785 probe; probe = TREE_CHAIN (probe))
2786 use_thunk (probe, /*emit_p=*/1);
2792 /* Generate RTL for FN. */
2795 expand_body (tree fn)
2797 location_t saved_loc;
2798 tree saved_function;
2800 if (flag_unit_at_a_time && !cgraph_global_info_ready)
2803 /* Compute the appropriate object-file linkage for inline
2805 if (DECL_DECLARED_INLINE_P (fn))
2806 import_export_decl (fn);
2808 /* If FN is external, then there's no point in generating RTL for
2809 it. This situation can arise with an inline function under
2810 `-fexternal-templates'; we instantiate the function, even though
2811 we're not planning on emitting it, in case we get a chance to
2813 if (DECL_EXTERNAL (fn))
2816 /* Save the current file name and line number. When we expand the
2817 body of the function, we'll set INPUT_LOCATION so that
2818 error-messages come out in the right places. */
2819 saved_loc = input_location;
2820 saved_function = current_function_decl;
2821 input_location = DECL_SOURCE_LOCATION (fn);
2822 current_function_decl = fn;
2824 timevar_push (TV_INTEGRATION);
2826 /* Optimize the body of the function before expanding it. */
2827 optimize_function (fn);
2829 timevar_pop (TV_INTEGRATION);
2830 timevar_push (TV_EXPAND);
2832 genrtl_start_function (fn);
2833 current_function_is_thunk = DECL_THUNK_P (fn);
2835 /* Expand the body. */
2836 expand_stmt (DECL_SAVED_TREE (fn));
2838 /* Statements should always be full-expressions at the outermost set
2839 of curly braces for a function. */
2840 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2842 /* The outermost statement for a function contains the line number
2843 recorded when we finished processing the function. */
2844 input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2846 /* Generate code for the function. */
2847 genrtl_finish_function (fn);
2849 /* If possible, obliterate the body of the function so that it can
2850 be garbage collected. */
2851 if (dump_enabled_p (TDI_all))
2852 /* Keep the body; we're going to dump it. */
2854 else if (DECL_INLINE (fn) && flag_inline_trees)
2855 /* We might need the body of this function so that we can expand
2856 it inline somewhere else. */
2859 /* We don't need the body; blow it away. */
2860 DECL_SAVED_TREE (fn) = NULL_TREE;
2862 /* And restore the current source position. */
2863 current_function_decl = saved_function;
2864 input_location = saved_loc;
2865 extract_interface_info ();
2867 timevar_pop (TV_EXPAND);
2869 /* Emit any thunks that should be emitted at the same time as FN. */
2870 emit_associated_thunks (fn);
2873 /* Generate RTL for FN. */
2876 expand_or_defer_fn (tree fn)
2878 /* When the parser calls us after finishing the body of a template
2879 function, we don't really want to expand the body. When we're
2880 processing an in-class definition of an inline function,
2881 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2882 to look at the function itself. */
2883 if (processing_template_decl
2884 || (DECL_LANG_SPECIFIC (fn)
2885 && DECL_TEMPLATE_INFO (fn)
2886 && uses_template_parms (DECL_TI_ARGS (fn))))
2888 /* Normally, collection only occurs in rest_of_compilation. So,
2889 if we don't collect here, we never collect junk generated
2890 during the processing of templates until we hit a
2891 non-template function. */
2896 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2897 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2898 simplify_aggr_init_exprs_r,
2901 /* If this is a constructor or destructor body, we have to clone
2903 if (maybe_clone_body (fn))
2905 /* We don't want to process FN again, so pretend we've written
2906 it out, even though we haven't. */
2907 TREE_ASM_WRITTEN (fn) = 1;
2911 /* There's no reason to do any of the work here if we're only doing
2912 semantic analysis; this code just generates RTL. */
2913 if (flag_syntax_only)
2916 if (flag_unit_at_a_time && cgraph_global_info_ready)
2919 if (flag_unit_at_a_time && !cgraph_global_info_ready)
2923 /* Compute the appropriate object-file linkage for inline
2925 if (DECL_DECLARED_INLINE_P (fn))
2926 import_export_decl (fn);
2927 cgraph_finalize_function (fn, DECL_SAVED_TREE (fn));
2931 if (!DECL_EXTERNAL (fn))
2933 DECL_NOT_REALLY_EXTERN (fn) = 1;
2934 DECL_EXTERNAL (fn) = 1;
2936 /* Remember this function. In finish_file we'll decide if
2937 we actually need to write this function out. */
2939 /* Let the back-end know that this function exists. */
2940 (*debug_hooks->deferred_inline_function) (fn);
2946 /* If possible, avoid generating RTL for this function. Instead,
2947 just record it as an inline function, and wait until end-of-file
2948 to decide whether to write it out or not. */
2949 if (/* We have to generate RTL if it's not an inline function. */
2950 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2951 /* Or if we have to emit code for inline functions anyhow. */
2952 && !flag_keep_inline_functions
2953 /* Or if we actually have a reference to the function. */
2954 && !DECL_NEEDED_P (fn))
2956 /* Set DECL_EXTERNAL so that assemble_external will be called as
2957 necessary. We'll clear it again in finish_file. */
2958 if (!DECL_EXTERNAL (fn))
2960 DECL_NOT_REALLY_EXTERN (fn) = 1;
2961 DECL_EXTERNAL (fn) = 1;
2963 /* Remember this function. In finish_file we'll decide if
2964 we actually need to write this function out. */
2966 /* Let the back-end know that this function exists. */
2967 (*debug_hooks->deferred_inline_function) (fn);
2974 /* Helper function for walk_tree, used by finish_function to override all
2975 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2976 value optimization. */
2979 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
2981 tree nrv = (tree) data;
2983 /* No need to walk into types. There wouldn't be any need to walk into
2984 non-statements, except that we have to consider STMT_EXPRs. */
2987 else if (TREE_CODE (*tp) == RETURN_STMT)
2988 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2989 else if (TREE_CODE (*tp) == CLEANUP_STMT
2990 && CLEANUP_DECL (*tp) == nrv)
2991 CLEANUP_EH_ONLY (*tp) = 1;
2993 /* Keep iterating. */
2997 /* Start generating the RTL for FN. */
3000 genrtl_start_function (tree fn)
3002 /* Tell everybody what function we're processing. */
3003 current_function_decl = fn;
3004 /* Get the RTL machinery going for this function. */
3005 init_function_start (fn);
3006 /* Let everybody know that we're expanding this function, not doing
3007 semantic analysis. */
3010 /* Even though we're inside a function body, we still don't want to
3011 call expand_expr to calculate the size of a variable-sized array.
3012 We haven't necessarily assigned RTL to all variables yet, so it's
3013 not safe to try to expand expressions involving them. */
3014 immediate_size_expand = 0;
3015 cfun->x_dont_save_pending_sizes_p = 1;
3017 /* Let the user know we're compiling this function. */
3018 announce_function (fn);
3020 /* Initialize the per-function data. */
3021 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
3022 if (DECL_SAVED_FUNCTION_DATA (fn))
3024 /* If we already parsed this function, and we're just expanding it
3025 now, restore saved state. */
3026 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
3028 /* This function is being processed in whole-function mode; we
3029 already did semantic analysis. */
3030 cfun->x_whole_function_mode_p = 1;
3032 /* If we decided that we didn't want to inline this function,
3033 make sure the back-end knows that. */
3034 if (!current_function_cannot_inline)
3035 current_function_cannot_inline = cp_function_chain->cannot_inline;
3037 /* We don't need the saved data anymore. Unless this is an inline
3038 function; we need the named return value info for
3039 cp_copy_res_decl_for_inlining. */
3040 if (! DECL_INLINE (fn))
3041 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
3044 /* Keep track of how many functions we're presently expanding. */
3047 /* Create a binding level for the parameters. */
3048 expand_function_start (fn, /*parms_have_cleanups=*/0);
3049 /* If this function is `main'. */
3050 if (DECL_MAIN_P (fn))
3051 expand_main_function ();
3053 /* Give our named return value the same RTL as our RESULT_DECL. */
3054 if (current_function_return_value)
3055 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
3058 /* Finish generating the RTL for FN. */
3061 genrtl_finish_function (tree fn)
3066 if (write_symbols != NO_DEBUG)
3068 /* Keep this code around in case we later want to control debug info
3069 based on whether a type is "used". (jason 1999-11-11) */
3071 tree ttype = target_type (fntype);
3074 if (IS_AGGR_TYPE (ttype))
3075 /* Let debugger know it should output info for this type. */
3076 note_debug_info_needed (ttype);
3078 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
3080 ttype = target_type (TREE_TYPE (parmdecl));
3081 if (IS_AGGR_TYPE (ttype))
3082 /* Let debugger know it should output info for this type. */
3083 note_debug_info_needed (ttype);
3088 /* Clean house because we will need to reorder insns here. */
3089 do_pending_stack_adjust ();
3091 /* If we have a named return value, we need to force a return so that
3092 the return register is USEd. */
3093 if (DECL_NAME (DECL_RESULT (fn)))
3094 emit_jump (return_label);
3096 /* We hard-wired immediate_size_expand to zero in start_function.
3097 Expand_function_end will decrement this variable. So, we set the
3098 variable to one here, so that after the decrement it will remain
3100 immediate_size_expand = 1;
3102 /* Generate rtl for function exit. */
3103 expand_function_end ();
3105 /* If this is a nested function (like a template instantiation that
3106 we're compiling in the midst of compiling something else), push a
3107 new GC context. That will keep local variables on the stack from
3108 being collected while we're doing the compilation of this
3110 if (function_depth > 1)
3111 ggc_push_context ();
3113 /* There's no need to defer outputting this function any more; we
3114 know we want to output it. */
3115 DECL_DEFER_OUTPUT (fn) = 0;
3117 /* Run the optimizers and output the assembler code for this
3119 rest_of_compilation (fn);
3121 /* Undo the call to ggc_push_context above. */
3122 if (function_depth > 1)
3126 /* Keep this code around in case we later want to control debug info
3127 based on whether a type is "used". (jason 1999-11-11) */
3129 if (ctype && TREE_ASM_WRITTEN (fn))
3130 note_debug_info_needed (ctype);
3133 /* If this function is marked with the constructor attribute, add it
3134 to the list of functions to be called along with constructors
3135 from static duration objects. */
3136 if (DECL_STATIC_CONSTRUCTOR (fn))
3137 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
3139 /* If this function is marked with the destructor attribute, add it
3140 to the list of functions to be called along with destructors from
3141 static duration objects. */
3142 if (DECL_STATIC_DESTRUCTOR (fn))
3143 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
3147 /* In C++, we should never be saving RTL for the function. */
3148 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
3150 /* Since we don't need the RTL for this function anymore, stop
3151 pointing to it. That's especially important for LABEL_DECLs,
3152 since you can reach all the instructions in the function from the
3153 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
3154 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
3156 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
3160 /* Clear out the RTL for the arguments. */
3161 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
3163 SET_DECL_RTL (t, NULL_RTX);
3164 DECL_INCOMING_RTL (t) = NULL_RTX;
3167 if (!(flag_inline_trees && DECL_INLINE (fn)))
3168 /* DECL_INITIAL must remain nonzero so we know this was an
3169 actual function definition. */
3170 DECL_INITIAL (fn) = error_mark_node;
3172 /* Let the error reporting routines know that we're outside a
3173 function. For a nested function, this value is used in
3174 pop_cp_function_context and then reset via pop_function_context. */
3175 current_function_decl = NULL_TREE;
3178 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
3182 clear_decl_rtl (tree* tp,
3183 int* walk_subtrees ATTRIBUTE_UNUSED ,
3184 void* data ATTRIBUTE_UNUSED )
3186 if (nonstatic_local_decl_p (*tp))
3187 SET_DECL_RTL (*tp, NULL_RTX);
3192 /* Perform initialization related to this module. */
3195 init_cp_semantics (void)
3197 lang_expand_stmt = cp_expand_stmt;
3200 #include "gt-cp-semantics.h"