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 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GNU CC.
12 GNU CC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 GNU CC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU CC; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #include "tree-inline.h"
43 /* There routines provide a modular interface to perform many parsing
44 operations. They may therefore be used during actual parsing, or
45 during template instantiation, which may be regarded as a
46 degenerate form of parsing. Since the current g++ parser is
47 lacking in several respects, and will be reimplemented, we are
48 attempting to move most code that is not directly related to
49 parsing into this file; that will make implementing the new parser
50 much easier since it will be able to make use of these routines. */
52 static tree maybe_convert_cond PARAMS ((tree));
53 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
54 static void deferred_type_access_control PARAMS ((void));
55 static void emit_associated_thunks PARAMS ((tree));
56 static void genrtl_try_block PARAMS ((tree));
57 static void genrtl_eh_spec_block PARAMS ((tree));
58 static void genrtl_handler PARAMS ((tree));
59 static void genrtl_ctor_stmt PARAMS ((tree));
60 static void genrtl_subobject PARAMS ((tree));
61 static void genrtl_named_return_value PARAMS ((void));
62 static void cp_expand_stmt PARAMS ((tree));
63 static void genrtl_start_function PARAMS ((tree));
64 static void genrtl_finish_function PARAMS ((tree));
65 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
67 /* Finish processing the COND, the SUBSTMT condition for STMT. */
69 #define FINISH_COND(cond, stmt, substmt) \
71 if (last_tree != stmt) \
73 RECHAIN_STMTS (stmt, substmt); \
74 if (!processing_template_decl) \
76 cond = build_tree_list (substmt, cond); \
84 /* Returns non-zero if the current statement is a full expression,
85 i.e. temporaries created during that statement should be destroyed
86 at the end of the statement. */
89 stmts_are_full_exprs_p ()
91 return current_stmt_tree ()->stmts_are_full_exprs_p;
94 /* Returns the stmt_tree (if any) to which statements are currently
95 being added. If there is no active statement-tree, NULL is
102 ? &cfun->language->x_stmt_tree
103 : &scope_chain->x_stmt_tree);
106 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
107 flag for this because "A union for which objects or pointers are
108 declared is not an anonymous union" [class.union]. */
111 anon_aggr_type_p (node)
114 return (CLASS_TYPE_P (node) && TYPE_LANG_SPECIFIC(node)->anon_aggr);
117 /* Finish a scope. */
122 tree block = NULL_TREE;
124 if (stmts_are_full_exprs_p ())
126 tree scope_stmts = NULL_TREE;
128 if (!processing_template_decl)
129 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
131 block = poplevel (kept_level_p (), 1, 0);
132 if (block && !processing_template_decl)
134 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
135 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
142 /* Begin a new scope. */
147 if (stmts_are_full_exprs_p ())
150 if (!processing_template_decl)
151 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
155 /* Finish a goto-statement. */
158 finish_goto_stmt (destination)
161 if (TREE_CODE (destination) == IDENTIFIER_NODE)
162 destination = lookup_label (destination);
164 /* We warn about unused labels with -Wunused. That means we have to
165 mark the used labels as used. */
166 if (TREE_CODE (destination) == LABEL_DECL)
167 TREE_USED (destination) = 1;
169 if (TREE_CODE (destination) != LABEL_DECL)
170 /* We don't inline calls to functions with computed gotos.
171 Those functions are typically up to some funny business,
172 and may be depending on the labels being at particular
173 addresses, or some such. */
174 DECL_UNINLINABLE (current_function_decl) = 1;
176 check_goto (destination);
178 return add_stmt (build_stmt (GOTO_STMT, destination));
181 /* COND is the condition-expression for an if, while, etc.,
182 statement. Convert it to a boolean value, if appropriate. */
185 maybe_convert_cond (cond)
188 /* Empty conditions remain empty. */
192 /* Wait until we instantiate templates before doing conversion. */
193 if (processing_template_decl)
196 /* Do the conversion. */
197 cond = convert_from_reference (cond);
198 return condition_conversion (cond);
201 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
204 finish_expr_stmt (expr)
209 if (expr != NULL_TREE)
211 if (!processing_template_decl
212 && !(stmts_are_full_exprs_p ())
213 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
215 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
216 expr = default_conversion (expr);
218 if (stmts_are_full_exprs_p ())
219 expr = convert_to_void (expr, "statement");
221 r = add_stmt (build_stmt (EXPR_STMT, expr));
226 /* This was an expression-statement, so we save the type of the
228 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
234 /* Begin an if-statement. Returns a newly created IF_STMT if
242 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
247 /* Process the COND of an if-statement, which may be given by
251 finish_if_stmt_cond (cond, if_stmt)
255 cond = maybe_convert_cond (cond);
256 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
259 /* Finish the then-clause of an if-statement, which may be given by
263 finish_then_clause (if_stmt)
266 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
271 /* Begin the else-clause of an if-statement. */
278 /* Finish the else-clause of an if-statement, which may be given by
282 finish_else_clause (if_stmt)
285 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
288 /* Finish an if-statement. */
300 /* If COND wasn't a declaration, clear out the
301 block we made for it and start a new one here so the
302 optimization in expand_end_loop will work. */
303 if (getdecls () == NULL_TREE)
310 /* Begin a while-statement. Returns a newly created WHILE_STMT if
317 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
323 /* Process the COND of a while-statement, which may be given by
327 finish_while_stmt_cond (cond, while_stmt)
331 cond = maybe_convert_cond (cond);
332 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
336 /* Finish a while-statement, which may be given by WHILE_STMT. */
339 finish_while_stmt (while_stmt)
343 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
347 /* Begin a do-statement. Returns a newly created DO_STMT if
353 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
358 /* Finish the body of a do-statement, which may be given by DO_STMT. */
361 finish_do_body (do_stmt)
364 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
367 /* Finish a do-statement, which may be given by DO_STMT, and whose
368 COND is as indicated. */
371 finish_do_stmt (cond, do_stmt)
375 cond = maybe_convert_cond (cond);
376 DO_COND (do_stmt) = cond;
380 /* Finish a return-statement. The EXPRESSION returned, if any, is as
384 finish_return_stmt (expr)
389 if (!processing_template_decl)
390 expr = check_return_expr (expr);
391 if (!processing_template_decl)
393 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
395 /* Even returns without a value in a constructor must return
396 `this'. We accomplish this by sending all returns in a
397 constructor to the CTOR_LABEL; finish_function emits code to
398 return a value there. When we finally generate the real
399 return statement, CTOR_LABEL is no longer set, and we fall
400 through into the normal return-processing code below. */
401 return finish_goto_stmt (ctor_label);
403 else if (DECL_DESTRUCTOR_P (current_function_decl))
405 /* Similarly, all destructors must run destructors for
406 base-classes before returning. So, all returns in a
407 destructor get sent to the DTOR_LABEL; finish_function emits
408 code to return a value there. */
409 return finish_goto_stmt (dtor_label);
412 r = add_stmt (build_stmt (RETURN_STMT, expr));
418 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
425 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
426 NULL_TREE, NULL_TREE);
427 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
429 if (NEW_FOR_SCOPE_P (r))
432 note_level_for_for ();
438 /* Finish the for-init-statement of a for-statement, which may be
439 given by FOR_STMT. */
442 finish_for_init_stmt (for_stmt)
445 if (last_tree != for_stmt)
446 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
450 /* Finish the COND of a for-statement, which may be given by
454 finish_for_cond (cond, for_stmt)
458 cond = maybe_convert_cond (cond);
459 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
463 /* Finish the increment-EXPRESSION in a for-statement, which may be
464 given by FOR_STMT. */
467 finish_for_expr (expr, for_stmt)
471 FOR_EXPR (for_stmt) = expr;
474 /* Finish the body of a for-statement, which may be given by
475 FOR_STMT. The increment-EXPR for the loop must be
479 finish_for_stmt (for_stmt)
482 /* Pop the scope for the body of the loop. */
484 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
485 if (NEW_FOR_SCOPE_P (for_stmt))
490 /* Finish a break-statement. */
495 return add_stmt (build_break_stmt ());
498 /* Finish a continue-statement. */
501 finish_continue_stmt ()
503 return add_stmt (build_continue_stmt ());
506 /* Begin a switch-statement. Returns a new SWITCH_STMT if
513 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE);
519 /* Finish the cond of a switch-statement. */
522 finish_switch_cond (cond, switch_stmt)
526 if (!processing_template_decl)
531 /* Convert the condition to an integer or enumeration type. */
532 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
533 if (cond == NULL_TREE)
535 error ("switch quantity not an integer");
536 cond = error_mark_node;
538 if (cond != error_mark_node)
540 cond = default_conversion (cond);
541 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
544 type = TREE_TYPE (cond);
545 index = get_unwidened (cond, NULL_TREE);
546 /* We can't strip a conversion from a signed type to an unsigned,
547 because if we did, int_fits_type_p would do the wrong thing
548 when checking case values for being in range,
549 and it's too hard to do the right thing. */
550 if (TREE_UNSIGNED (TREE_TYPE (cond))
551 == TREE_UNSIGNED (TREE_TYPE (index)))
554 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
555 push_switch (switch_stmt);
558 /* Finish the body of a switch-statement, which may be given by
559 SWITCH_STMT. The COND to switch on is indicated. */
562 finish_switch_stmt (switch_stmt)
565 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
571 /* Generate the RTL for T, which is a TRY_BLOCK. */
579 expand_eh_region_start ();
580 expand_stmt (TRY_STMTS (t));
581 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
585 if (!FN_TRY_BLOCK_P (t))
586 emit_line_note (input_filename, lineno);
588 expand_eh_region_start ();
589 expand_stmt (TRY_STMTS (t));
591 if (FN_TRY_BLOCK_P (t))
593 end_protect_partials ();
594 expand_start_all_catch ();
595 in_function_try_handler = 1;
596 expand_stmt (TRY_HANDLERS (t));
597 in_function_try_handler = 0;
598 expand_end_all_catch ();
602 expand_start_all_catch ();
603 expand_stmt (TRY_HANDLERS (t));
604 expand_end_all_catch ();
609 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
612 genrtl_eh_spec_block (t)
615 expand_eh_region_start ();
616 expand_stmt (EH_SPEC_STMTS (t));
617 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
618 build_call (call_unexpected_node,
619 tree_cons (NULL_TREE,
624 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
630 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
635 /* Likewise, for a function-try-block. */
638 begin_function_try_block ()
640 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
641 FN_TRY_BLOCK_P (r) = 1;
646 /* Finish a try-block, which may be given by TRY_BLOCK. */
649 finish_try_block (try_block)
652 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
655 /* Finish the body of a cleanup try-block, which may be given by
659 finish_cleanup_try_block (try_block)
662 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
665 /* Finish an implicitly generated try-block, with a cleanup is given
669 finish_cleanup (cleanup, try_block)
673 TRY_HANDLERS (try_block) = cleanup;
674 CLEANUP_P (try_block) = 1;
677 /* Likewise, for a function-try-block. */
680 finish_function_try_block (try_block)
683 if (TREE_CHAIN (try_block)
684 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
686 /* Chain the compound statement after the CTOR_INITIALIZER. */
687 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
688 /* And make the CTOR_INITIALIZER the body of the try-block. */
689 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
692 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
693 in_function_try_handler = 1;
696 /* Finish a handler-sequence for a try-block, which may be given by
700 finish_handler_sequence (try_block)
703 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
704 check_handlers (TRY_HANDLERS (try_block));
707 /* Likewise, for a function-try-block. */
710 finish_function_handler_sequence (try_block)
713 in_function_try_handler = 0;
714 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
715 check_handlers (TRY_HANDLERS (try_block));
718 /* Generate the RTL for T, which is a HANDLER. */
724 genrtl_do_pushlevel ();
725 if (!processing_template_decl)
726 expand_start_catch (HANDLER_TYPE (t));
727 expand_stmt (HANDLER_BODY (t));
728 if (!processing_template_decl)
732 /* Begin a handler. Returns a HANDLER if appropriate. */
738 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
740 /* Create a binding level for the eh_info and the exception object
743 note_level_for_catch ();
747 /* Finish the handler-parameters for a handler, which may be given by
748 HANDLER. DECL is the declaration for the catch parameter, or NULL
749 if this is a `catch (...)' clause. */
752 finish_handler_parms (decl, handler)
756 tree type = NULL_TREE;
757 if (processing_template_decl)
761 decl = pushdecl (decl);
762 decl = push_template_decl (decl);
763 add_decl_stmt (decl);
764 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
765 type = TREE_TYPE (decl);
769 type = expand_start_catch_block (decl);
771 HANDLER_TYPE (handler) = type;
774 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
775 the return value from the matching call to finish_handler_parms. */
778 finish_handler (handler)
781 if (!processing_template_decl)
782 expand_end_catch_block ();
784 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
787 /* Generate the RTL for T, which is a CTOR_STMT. */
793 if (CTOR_BEGIN_P (t))
794 begin_protect_partials ();
796 /* After this point, any exceptions will cause the
797 destructor to be executed, so we no longer need to worry
798 about destroying the various subobjects ourselves. */
799 end_protect_partials ();
802 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
803 compound-statement does not define a scope. Returns a new
804 COMPOUND_STMT if appropriate. */
807 begin_compound_stmt (has_no_scope)
813 r = build_stmt (COMPOUND_STMT, NULL_TREE);
815 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
820 COMPOUND_STMT_NO_SCOPE (r) = 1;
822 last_expr_type = NULL_TREE;
828 note_level_for_try ();
831 /* Normally, we try hard to keep the BLOCK for a
832 statement-expression. But, if it's a statement-expression with
833 a scopeless block, there's nothing to keep, and we don't want
834 to accidentally keep a block *inside* the scopeless block. */
840 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
841 If HAS_NO_SCOPE is non-zero, the compound statement does not define
845 finish_compound_stmt (has_no_scope, compound_stmt)
857 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
859 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
860 the precise purpose of that variable is store the type of the
861 last expression statement within the last compound statement, we
862 preserve the value. */
870 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
871 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
875 finish_asm_stmt (cv_qualifier, string, output_operands,
876 input_operands, clobbers)
879 tree output_operands;
886 if (TREE_CHAIN (string))
887 string = combine_strings (string);
889 if (cv_qualifier != NULL_TREE
890 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
892 cp_warning ("%s qualifier ignored on asm",
893 IDENTIFIER_POINTER (cv_qualifier));
894 cv_qualifier = NULL_TREE;
897 if (!processing_template_decl)
903 for (t = input_operands; t; t = TREE_CHAIN (t))
905 tree converted_operand
906 = decay_conversion (TREE_VALUE (t));
908 /* If the type of the operand hasn't been determined (e.g.,
909 because it involves an overloaded function), then issue
910 an error message. There's no context available to
911 resolve the overloading. */
912 if (TREE_TYPE (converted_operand) == unknown_type_node)
914 cp_error ("type of asm operand `%E' could not be determined",
916 converted_operand = error_mark_node;
918 TREE_VALUE (t) = converted_operand;
921 ninputs = list_length (input_operands);
922 noutputs = list_length (output_operands);
924 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
929 const char *constraint;
932 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
933 operand = TREE_VALUE (output_operands);
935 if (!parse_output_constraint (&constraint,
936 i, ninputs, noutputs,
941 /* By marking the type as erroneous, we will not try to
942 process this operand again in expand_asm_operands. */
943 TREE_TYPE (operand) = error_mark_node;
947 /* If the operand is a DECL that is going to end up in
948 memory, assume it is addressable. This is a bit more
949 conservative than it would ideally be; the exact test is
950 buried deep in expand_asm_operands and depends on the
951 DECL_RTL for the OPERAND -- which we don't have at this
953 if (!allows_reg && DECL_P (operand))
954 mark_addressable (operand);
958 r = build_stmt (ASM_STMT, cv_qualifier, string,
959 output_operands, input_operands,
964 /* Finish a label with the indicated NAME. */
967 finish_label_stmt (name)
970 tree decl = define_label (input_filename, lineno, name);
971 add_stmt (build_stmt (LABEL_STMT, decl));
974 /* Finish a series of declarations for local labels. G++ allows users
975 to declare "local" labels, i.e., labels with scope. This extension
976 is useful when writing code involving statement-expressions. */
979 finish_label_decl (name)
982 tree decl = declare_local_label (name);
983 add_decl_stmt (decl);
986 /* Generate the RTL for a SUBOBJECT. */
989 genrtl_subobject (cleanup)
992 add_partial_entry (cleanup);
995 /* We're in a constructor, and have just constructed a a subobject of
996 *THIS. CLEANUP is code to run if an exception is thrown before the
997 end of the current function is reached. */
1000 finish_subobject (cleanup)
1003 tree r = build_stmt (SUBOBJECT, cleanup);
1007 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1010 finish_decl_cleanup (decl, cleanup)
1014 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1017 /* Generate the RTL for a RETURN_INIT. */
1020 genrtl_named_return_value ()
1022 tree decl = DECL_RESULT (current_function_decl);
1024 /* If this named return value comes in a register, put it in a
1026 if (DECL_REGISTER (decl))
1028 /* Note that the mode of the old DECL_RTL may be wider than the
1029 mode of DECL_RESULT, depending on the calling conventions for
1030 the processor. For example, on the Alpha, a 32-bit integer
1031 is returned in a DImode register -- the DECL_RESULT has
1032 SImode but the DECL_RTL for the DECL_RESULT has DImode. So,
1033 here, we use the mode the back-end has already assigned for
1034 the return value. */
1035 SET_DECL_RTL (decl, gen_reg_rtx (GET_MODE (DECL_RTL (decl))));
1036 if (TREE_ADDRESSABLE (decl))
1037 put_var_into_stack (decl);
1040 emit_local_var (decl);
1043 /* Bind a name and initialization to the return value of
1044 the current function. */
1047 finish_named_return_value (return_id, init)
1048 tree return_id, init;
1050 tree decl = DECL_RESULT (current_function_decl);
1052 /* Give this error as many times as there are occurrences, so that
1053 users can use Emacs compilation buffers to find and fix all such
1056 pedwarn ("ISO C++ does not permit named return values");
1057 cp_deprecated ("the named return value extension");
1059 if (return_id != NULL_TREE)
1061 if (DECL_NAME (decl) == NULL_TREE)
1062 DECL_NAME (decl) = return_id;
1065 cp_error ("return identifier `%D' already in place", return_id);
1070 /* Can't let this happen for constructors. */
1071 if (DECL_CONSTRUCTOR_P (current_function_decl))
1073 error ("can't redefine default return value for constructors");
1077 /* If we have a named return value, put that in our scope as well. */
1078 if (DECL_NAME (decl) != NULL_TREE)
1080 /* Let `cp_finish_decl' know that this initializer is ok. */
1081 DECL_INITIAL (decl) = init;
1082 if (doing_semantic_analysis_p ())
1084 if (!processing_template_decl)
1086 cp_finish_decl (decl, init, NULL_TREE, 0);
1087 add_stmt (build_stmt (RETURN_INIT, NULL_TREE, NULL_TREE));
1090 add_stmt (build_stmt (RETURN_INIT, return_id, init));
1093 /* Don't use tree-inlining for functions with named return values.
1094 That doesn't work properly because we don't do any translation of
1095 the RETURN_INITs when they are copied. */
1096 DECL_UNINLINABLE (current_function_decl) = 1;
1099 /* The INIT_LIST is a list of mem-initializers, in the order they were
1100 written by the user. The TREE_VALUE of each node is a list of
1101 initializers for a particular subobject. The TREE_PURPOSE is a
1102 FIELD_DECL is the initializer is for a non-static data member, and
1103 a class type if the initializer is for a base class. */
1106 finish_mem_initializers (init_list)
1109 tree member_init_list;
1110 tree base_init_list;
1111 tree last_base_warned_about;
1115 member_init_list = NULL_TREE;
1116 base_init_list = NULL_TREE;
1117 last_base_warned_about = NULL_TREE;
1119 for (init = init_list; init; init = next)
1121 next = TREE_CHAIN (init);
1122 if (TREE_CODE (TREE_PURPOSE (init)) == FIELD_DECL)
1124 TREE_CHAIN (init) = member_init_list;
1125 member_init_list = init;
1127 /* We're running through the initializers from right to left
1128 as we process them here. So, if we see a data member
1129 initializer after we see a base initializer, that
1130 actually means that the base initializer preceeded the
1131 data member initializer. */
1132 if (warn_reorder && last_base_warned_about != base_init_list)
1136 for (base = base_init_list;
1137 base != last_base_warned_about;
1138 base = TREE_CHAIN (base))
1140 cp_warning ("base initializer for `%T'",
1141 TREE_PURPOSE (base));
1142 warning (" will be re-ordered to precede member initializations");
1145 last_base_warned_about = base_init_list;
1150 TREE_CHAIN (init) = base_init_list;
1151 base_init_list = init;
1155 setup_vtbl_ptr (member_init_list, base_init_list);
1158 /* Cache the value of this class's main virtual function table pointer
1159 in a register variable. This will save one indirection if a
1160 more than one virtual function call is made this function. */
1163 setup_vtbl_ptr (member_init_list, base_init_list)
1164 tree member_init_list;
1165 tree base_init_list;
1167 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1169 /* If we've already done this, there's no need to do it again. */
1173 if (DECL_CONSTRUCTOR_P (current_function_decl))
1175 if (processing_template_decl)
1176 add_stmt (build_min_nt
1178 member_init_list, base_init_list));
1183 /* Mark the beginning of the constructor. */
1184 ctor_stmt = build_stmt (CTOR_STMT);
1185 CTOR_BEGIN_P (ctor_stmt) = 1;
1186 add_stmt (ctor_stmt);
1188 /* And actually initialize the base-classes and members. */
1189 emit_base_init (member_init_list, base_init_list);
1192 else if (DECL_DESTRUCTOR_P (current_function_decl)
1193 && !processing_template_decl)
1198 /* If the dtor is empty, and we know there is not any possible
1199 way we could use any vtable entries, before they are possibly
1200 set by a base class dtor, we don't have to setup the vtables,
1201 as we know that any base class dtor will set up any vtables
1202 it needs. We avoid MI, because one base class dtor can do a
1203 virtual dispatch to an overridden function that would need to
1204 have a non-related vtable set up, we cannot avoid setting up
1205 vtables in that case. We could change this to see if there
1206 is just one vtable. */
1207 if_stmt = begin_if_stmt ();
1209 /* If it is not safe to avoid setting up the vtables, then
1210 someone will change the condition to be boolean_true_node.
1211 (Actually, for now, we do not have code to set the condition
1212 appropriately, so we just assume that we always need to
1213 initialize the vtables.) */
1214 finish_if_stmt_cond (boolean_true_node, if_stmt);
1215 current_vcalls_possible_p = &IF_COND (if_stmt);
1217 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1219 /* Make all virtual function table pointers in non-virtual base
1220 classes point to CURRENT_CLASS_TYPE's virtual function
1222 initialize_vtbl_ptrs (current_class_ptr);
1224 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1225 finish_then_clause (if_stmt);
1229 /* Always keep the BLOCK node associated with the outermost pair of
1230 curly braces of a function. These are needed for correct
1231 operation of dwarfout.c. */
1232 keep_next_level (1);
1234 /* The virtual function tables are set up now. */
1238 /* Returns the stack of SCOPE_STMTs for the current function. */
1241 current_scope_stmt_stack ()
1243 return &cfun->language->x_scope_stmt_stack;
1246 /* Finish a parenthesized expression EXPR. */
1249 finish_parenthesized_expr (expr)
1252 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1253 /* This inhibits warnings in truthvalue_conversion. */
1254 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1256 if (TREE_CODE (expr) == OFFSET_REF)
1257 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1258 enclosed in parentheses. */
1259 PTRMEM_OK_P (expr) = 0;
1263 /* Begin a statement-expression. The value returned must be passed to
1264 finish_stmt_expr. */
1269 /* If we're outside a function, we won't have a statement-tree to
1270 work with. But, if we see a statement-expression we need to
1272 if (! cfun && !last_tree)
1273 begin_stmt_tree (&scope_chain->x_saved_tree);
1275 keep_next_level (1);
1276 /* If we're building a statement tree, then the upcoming compound
1277 statement will be chained onto the tree structure, starting at
1278 last_tree. We return last_tree so that we can later unhook the
1279 compound statement. */
1283 /* Used when beginning a statement-expression outside function scope.
1284 For example, when handling a file-scope initializer, we use this
1288 begin_global_stmt_expr ()
1290 if (! cfun && !last_tree)
1291 begin_stmt_tree (&scope_chain->x_saved_tree);
1293 keep_next_level (1);
1295 return (last_tree != NULL_TREE) ? last_tree : expand_start_stmt_expr();
1298 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1301 finish_global_stmt_expr (stmt_expr)
1304 stmt_expr = expand_end_stmt_expr (stmt_expr);
1307 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1308 finish_stmt_tree (&scope_chain->x_saved_tree);
1313 /* Finish a statement-expression. RTL_EXPR should be the value
1314 returned by the previous begin_stmt_expr; EXPR is the
1315 statement-expression. Returns an expression representing the
1316 statement-expression. */
1319 finish_stmt_expr (rtl_expr)
1324 /* If the last thing in the statement-expression was not an
1325 expression-statement, then it has type `void'. */
1326 if (!last_expr_type)
1327 last_expr_type = void_type_node;
1328 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1329 TREE_SIDE_EFFECTS (result) = 1;
1331 /* Remove the compound statement from the tree structure; it is
1332 now saved in the STMT_EXPR. */
1333 last_tree = rtl_expr;
1334 TREE_CHAIN (last_tree) = NULL_TREE;
1336 /* If we created a statement-tree for this statement-expression,
1339 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1340 finish_stmt_tree (&scope_chain->x_saved_tree);
1345 /* Finish a call to FN with ARGS. Returns a representation of the
1349 finish_call_expr (fn, args, koenig)
1358 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1359 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1360 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1361 fn = do_identifier (fn, 2, args);
1363 result = build_x_function_call (fn, args, current_class_ref);
1365 if (TREE_CODE (result) == CALL_EXPR
1366 && (! TREE_TYPE (result)
1367 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1368 result = require_complete_type (result);
1373 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1374 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1375 POSTDECREMENT_EXPR.) */
1378 finish_increment_expr (expr, code)
1380 enum tree_code code;
1382 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1383 a COMPONENT_REF). This way if we've got, say, a reference to a
1384 static member that's being operated on, we don't end up trying to
1385 find a member operator for the class it's in. */
1387 if (TREE_CODE (expr) == OFFSET_REF)
1388 expr = resolve_offset_ref (expr);
1389 return build_x_unary_op (code, expr);
1392 /* Finish a use of `this'. Returns an expression for `this'. */
1399 if (current_class_ptr)
1401 result = current_class_ptr;
1403 else if (current_function_decl
1404 && DECL_STATIC_FUNCTION_P (current_function_decl))
1406 error ("`this' is unavailable for static member functions");
1407 result = error_mark_node;
1411 if (current_function_decl)
1412 error ("invalid use of `this' in non-member function");
1414 error ("invalid use of `this' at top level");
1415 result = error_mark_node;
1421 /* Finish a member function call using OBJECT and ARGS as arguments to
1422 FN. Returns an expression for the call. */
1425 finish_object_call_expr (fn, object, args)
1431 /* This is a future direction of this code, but because
1432 build_x_function_call cannot always undo what is done in
1433 build_component_ref entirely yet, we cannot do this. */
1435 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1436 return finish_call_expr (real_fn, args);
1438 if (DECL_DECLARES_TYPE_P (fn))
1440 if (processing_template_decl)
1441 /* This can happen on code like:
1444 template <class T> void f(T t) {
1448 We just grab the underlying IDENTIFIER. */
1449 fn = DECL_NAME (fn);
1452 cp_error ("calling type `%T' like a method", fn);
1453 return error_mark_node;
1457 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1461 /* Finish a qualified member function call using OBJECT and ARGS as
1462 arguments to FN. Returns an expression for the call. */
1465 finish_qualified_object_call_expr (fn, object, args)
1470 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1471 TREE_OPERAND (fn, 1), args);
1474 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1475 being the scope, if any, of DESTRUCTOR. Returns an expression for
1479 finish_pseudo_destructor_call_expr (object, scope, destructor)
1484 if (processing_template_decl)
1485 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1487 if (scope && scope != destructor)
1488 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1491 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1492 && (TREE_CODE (TREE_TYPE (object)) !=
1493 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1494 cp_error ("`%E' is not of type `%T'", object, destructor);
1496 return cp_convert (void_type_node, object);
1499 /* Finish a call to a globally qualified member function FN using
1500 ARGS. Returns an expression for the call. */
1503 finish_qualified_call_expr (fn, args)
1507 if (processing_template_decl)
1508 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1510 return build_member_call (TREE_OPERAND (fn, 0),
1511 TREE_OPERAND (fn, 1),
1515 /* Finish an expression of the form CODE EXPR. */
1518 finish_unary_op_expr (code, expr)
1519 enum tree_code code;
1522 tree result = build_x_unary_op (code, expr);
1523 /* Inside a template, build_x_unary_op does not fold the
1524 expression. So check whether the result is folded before
1525 setting TREE_NEGATED_INT. */
1526 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1527 && TREE_CODE (result) == INTEGER_CST
1528 && !TREE_UNSIGNED (TREE_TYPE (result))
1529 && INT_CST_LT (result, integer_zero_node))
1530 TREE_NEGATED_INT (result) = 1;
1531 overflow_warning (result);
1535 /* Finish an id-expression. */
1538 finish_id_expr (expr)
1541 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1542 expr = do_identifier (expr, 1, NULL_TREE);
1544 if (TREE_TYPE (expr) == error_mark_node)
1545 expr = error_mark_node;
1549 static tree current_type_lookups;
1551 /* Perform deferred access control for types used in the type of a
1555 deferred_type_access_control ()
1557 tree lookup = type_lookups;
1559 if (lookup == error_mark_node)
1562 for (; lookup; lookup = TREE_CHAIN (lookup))
1563 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1567 decl_type_access_control (decl)
1572 if (type_lookups == error_mark_node)
1575 save_fn = current_function_decl;
1577 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1578 current_function_decl = decl;
1580 deferred_type_access_control ();
1582 current_function_decl = save_fn;
1584 /* Now strip away the checks for the current declarator; they were
1585 added to type_lookups after typed_declspecs saved the copy that
1586 ended up in current_type_lookups. */
1587 type_lookups = current_type_lookups;
1589 current_type_lookups = NULL_TREE;
1592 /* Record the lookups, if we're doing deferred access control. */
1595 save_type_access_control (lookups)
1598 if (type_lookups != error_mark_node)
1600 my_friendly_assert (!current_type_lookups, 20010301);
1601 current_type_lookups = lookups;
1604 my_friendly_assert (!lookups || lookups == error_mark_node, 20010301);
1607 /* Set things up so that the next deferred access control will succeed.
1608 This is needed for friend declarations see grokdeclarator for details. */
1611 skip_type_access_control ()
1613 type_lookups = NULL_TREE;
1616 /* Reset the deferred access control. */
1619 reset_type_access_control ()
1621 type_lookups = NULL_TREE;
1622 current_type_lookups = NULL_TREE;
1625 /* Begin a function definition declared with DECL_SPECS and
1626 DECLARATOR. Returns non-zero if the function-declaration is
1630 begin_function_definition (decl_specs, declarator)
1637 split_specs_attrs (decl_specs, &specs, &attrs);
1638 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1641 deferred_type_access_control ();
1642 type_lookups = error_mark_node;
1644 /* The things we're about to see are not directly qualified by any
1645 template headers we've seen thus far. */
1646 reset_specialization ();
1651 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1655 begin_constructor_declarator (scope, name)
1659 tree result = build_nt (SCOPE_REF, scope, name);
1660 enter_scope_of (result);
1664 /* Finish an init-declarator. Returns a DECL. */
1667 finish_declarator (declarator, declspecs, attributes,
1668 prefix_attributes, initialized)
1672 tree prefix_attributes;
1675 return start_decl (declarator, declspecs, initialized, attributes,
1679 /* Finish a translation unit. */
1682 finish_translation_unit ()
1684 /* In case there were missing closebraces,
1685 get us back to the global binding level. */
1687 while (current_namespace != global_namespace)
1690 /* Do file scope __FUNCTION__ et al. */
1691 finish_fname_decls ();
1696 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1697 Returns the parameter. */
1700 finish_template_type_parm (aggr, identifier)
1704 if (aggr != class_type_node)
1706 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1707 aggr = class_type_node;
1710 return build_tree_list (aggr, identifier);
1713 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1714 Returns the parameter. */
1717 finish_template_template_parm (aggr, identifier)
1721 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1722 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1723 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1724 DECL_TEMPLATE_RESULT (tmpl) = decl;
1725 DECL_ARTIFICIAL (decl) = 1;
1726 end_template_decl ();
1728 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1730 return finish_template_type_parm (aggr, tmpl);
1733 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1734 non-zero, the parameter list was terminated by a `...'. */
1737 finish_parmlist (parms, ellipsis)
1743 /* We mark the PARMS as a parmlist so that declarator processing can
1744 disambiguate certain constructs. */
1745 TREE_PARMLIST (parms) = 1;
1746 /* We do not append void_list_node here, but leave it to grokparms
1748 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1753 /* Begin a class definition, as indicated by T. */
1756 begin_class_definition (t)
1759 /* Check the bases are accessible. */
1760 decl_type_access_control (TYPE_NAME (t));
1761 reset_type_access_control ();
1763 if (processing_template_parmlist)
1765 cp_error ("definition of `%#T' inside template parameter list", t);
1766 return error_mark_node;
1769 /* In a definition of a member class template, we will get here with
1770 an implicit typename. */
1771 if (IMPLICIT_TYPENAME_P (t))
1773 /* A non-implicit typename comes from code like:
1775 template <typename T> struct A {
1776 template <typename U> struct A<T>::B ...
1778 This is erroneous. */
1779 else if (TREE_CODE (t) == TYPENAME_TYPE)
1781 cp_error ("invalid definition of qualified type `%T'", t);
1782 t = error_mark_node;
1785 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1787 t = make_aggr_type (RECORD_TYPE);
1788 pushtag (make_anon_name (), t, 0);
1791 /* If we generated a partial instantiation of this type, but now
1792 we're seeing a real definition, we're actually looking at a
1793 partial specialization. Consider:
1795 template <class T, class U>
1801 template <class T, class U>
1804 typename X<Y<T, U> >::A a;
1807 template <class T, class U>
1812 We have to undo the effects of the previous partial
1814 if (PARTIAL_INSTANTIATION_P (t))
1818 /* Unfortunately, when we're not in pedantic mode, we
1819 attempt to actually fill in some of the fields of the
1820 partial instantiation, in order to support the implicit
1821 typename extension. Clear those fields now, in
1822 preparation for the definition here. The fields cleared
1823 here must match those set in instantiate_class_template.
1824 Look for a comment mentioning begin_class_definition
1826 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1827 TYPE_FIELDS (t) = NULL_TREE;
1828 TYPE_METHODS (t) = NULL_TREE;
1829 CLASSTYPE_TAGS (t) = NULL_TREE;
1830 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1831 TYPE_SIZE (t) = NULL_TREE;
1834 /* This isn't a partial instantiation any more. */
1835 PARTIAL_INSTANTIATION_P (t) = 0;
1837 /* If this type was already complete, and we see another definition,
1839 else if (COMPLETE_TYPE_P (t))
1840 duplicate_tag_error (t);
1842 /* Update the location of the decl. */
1843 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1844 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1846 if (TYPE_BEING_DEFINED (t))
1848 t = make_aggr_type (TREE_CODE (t));
1849 pushtag (TYPE_IDENTIFIER (t), t, 0);
1851 maybe_process_partial_specialization (t);
1853 TYPE_BEING_DEFINED (t) = 1;
1854 TYPE_PACKED (t) = flag_pack_struct;
1855 /* Reset the interface data, at the earliest possible
1856 moment, as it might have been set via a class foo;
1858 if (! TYPE_ANONYMOUS_P (t))
1860 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1861 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1862 (t, interface_unknown);
1864 reset_specialization();
1866 /* Make a declaration for this class in its own scope. */
1867 build_self_reference ();
1872 /* Finish the member declaration given by DECL. */
1875 finish_member_declaration (decl)
1878 if (decl == error_mark_node || decl == NULL_TREE)
1881 if (decl == void_type_node)
1882 /* The COMPONENT was a friend, not a member, and so there's
1883 nothing for us to do. */
1886 /* We should see only one DECL at a time. */
1887 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1889 /* Set up access control for DECL. */
1891 = (current_access_specifier == access_private_node);
1892 TREE_PROTECTED (decl)
1893 = (current_access_specifier == access_protected_node);
1894 if (TREE_CODE (decl) == TEMPLATE_DECL)
1896 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1897 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1900 /* Mark the DECL as a member of the current class. */
1901 DECL_CONTEXT (decl) = current_class_type;
1905 A C language linkage is ignored for the names of class members
1906 and the member function type of class member functions. */
1907 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1908 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1910 /* Put functions on the TYPE_METHODS list and everything else on the
1911 TYPE_FIELDS list. Note that these are built up in reverse order.
1912 We reverse them (to obtain declaration order) in finish_struct. */
1913 if (TREE_CODE (decl) == FUNCTION_DECL
1914 || DECL_FUNCTION_TEMPLATE_P (decl))
1916 /* We also need to add this function to the
1917 CLASSTYPE_METHOD_VEC. */
1918 add_method (current_class_type, decl, /*error_p=*/0);
1920 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1921 TYPE_METHODS (current_class_type) = decl;
1925 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1926 go at the beginning. The reason is that lookup_field_1
1927 searches the list in order, and we want a field name to
1928 override a type name so that the "struct stat hack" will
1929 work. In particular:
1931 struct S { enum E { }; int E } s;
1934 is legal. In addition, the FIELD_DECLs must be maintained in
1935 declaration order so that class layout works as expected.
1936 However, we don't need that order until class layout, so we
1937 save a little time by putting FIELD_DECLs on in reverse order
1938 here, and then reversing them in finish_struct_1. (We could
1939 also keep a pointer to the correct insertion points in the
1942 if (TREE_CODE (decl) == TYPE_DECL)
1943 TYPE_FIELDS (current_class_type)
1944 = chainon (TYPE_FIELDS (current_class_type), decl);
1947 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1948 TYPE_FIELDS (current_class_type) = decl;
1951 /* Enter the DECL into the scope of the class. */
1952 if (TREE_CODE (decl) != USING_DECL)
1953 pushdecl_class_level (decl);
1957 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1958 the definition is immediately followed by a semicolon. Returns the
1962 finish_class_definition (t, attributes, semi, pop_scope_p)
1968 /* finish_struct nukes this anyway; if finish_exception does too,
1971 note_got_semicolon (t);
1973 /* If we got any attributes in class_head, xref_tag will stick them in
1974 TREE_TYPE of the type. Grab them now. */
1975 attributes = chainon (TREE_TYPE (t), attributes);
1976 TREE_TYPE (t) = NULL_TREE;
1978 if (TREE_CODE (t) == ENUMERAL_TYPE)
1982 t = finish_struct (t, attributes);
1984 note_got_semicolon (t);
1988 check_for_missing_semicolon (t);
1990 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1991 if (current_function_decl)
1992 type_lookups = error_mark_node;
1993 if (current_scope () == current_function_decl)
1994 do_pending_defargs ();
1999 /* Finish processing the default argument expressions cached during
2000 the processing of a class definition. */
2003 begin_inline_definitions ()
2005 if (current_scope () == current_function_decl)
2006 do_pending_inlines ();
2009 /* Finish processing the inline function definitions cached during the
2010 processing of a class definition. */
2013 finish_inline_definitions ()
2015 if (current_class_type == NULL_TREE)
2016 clear_inline_text_obstack ();
2019 /* Finish processing the declaration of a member class template
2020 TYPES whose template parameters are given by PARMS. */
2023 finish_member_class_template (types)
2028 /* If there are declared, but undefined, partial specializations
2029 mixed in with the typespecs they will not yet have passed through
2030 maybe_process_partial_specialization, so we do that here. */
2031 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2032 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2033 maybe_process_partial_specialization (TREE_VALUE (t));
2035 note_list_got_semicolon (types);
2036 grok_x_components (types);
2037 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2038 /* The component was in fact a friend declaration. We avoid
2039 finish_member_template_decl performing certain checks by
2043 finish_member_template_decl (types);
2045 /* As with other component type declarations, we do
2046 not store the new DECL on the list of
2051 /* Finish processsing a complete template declaration. The PARMS are
2052 the template parameters. */
2055 finish_template_decl (parms)
2059 end_template_decl ();
2061 end_specialization ();
2064 /* Finish processing a template-id (which names a type) of the form
2065 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2066 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2067 the scope of template-id indicated. */
2070 finish_template_type (name, args, entering_scope)
2077 decl = lookup_template_class (name, args,
2078 NULL_TREE, NULL_TREE,
2079 entering_scope, /*complain=*/1);
2080 if (decl != error_mark_node)
2081 decl = TYPE_STUB_DECL (decl);
2086 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2087 namespace scope or a class scope. */
2093 tree scope = TREE_OPERAND (sr, 0);
2095 if (TREE_CODE (scope) == NAMESPACE_DECL)
2097 push_decl_namespace (scope);
2098 TREE_COMPLEXITY (sr) = -1;
2100 else if (scope != current_class_type)
2102 if (TREE_CODE (scope) == TYPENAME_TYPE)
2104 /* In a declarator for a template class member, the scope will
2105 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2106 scope = TREE_TYPE (scope);
2107 TREE_OPERAND (sr, 0) = scope;
2109 push_nested_class (scope, 3);
2110 TREE_COMPLEXITY (sr) = current_class_depth;
2114 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2115 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2116 BASE_CLASS, or NULL_TREE if an error occurred. The
2117 ACCESSS_SPECIFIER is one of
2118 access_{default,public,protected_private}[_virtual]_node.*/
2121 finish_base_specifier (access_specifier, base_class)
2122 tree access_specifier;
2127 if (! is_aggr_type (base_class, 1))
2131 if (CP_TYPE_QUALS (base_class) != 0)
2133 cp_error ("base class `%T' has cv qualifiers", base_class);
2134 base_class = TYPE_MAIN_VARIANT (base_class);
2136 result = build_tree_list (access_specifier, base_class);
2142 /* Called when multiple declarators are processed. If that is not
2143 premitted in this context, an error is issued. */
2146 check_multiple_declarators ()
2150 In a template-declaration, explicit specialization, or explicit
2151 instantiation the init-declarator-list in the declaration shall
2152 contain at most one declarator.
2154 We don't just use PROCESSING_TEMPLATE_DECL for the first
2155 condition since that would disallow the perfectly legal code,
2156 like `template <class T> struct S { int i, j; };'. */
2157 tree scope = current_scope ();
2159 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2160 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2163 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2164 || processing_explicit_instantiation
2165 || processing_specialization)
2166 cp_error ("multiple declarators in template declaration");
2169 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2170 use as a type-specifier. */
2173 finish_typeof (expr)
2176 if (processing_template_decl)
2180 t = make_aggr_type (TYPEOF_TYPE);
2181 TYPE_FIELDS (t) = expr;
2186 if (TREE_CODE (expr) == OFFSET_REF)
2187 expr = resolve_offset_ref (expr);
2189 return TREE_TYPE (expr);
2192 /* Compute the value of the `sizeof' operator. */
2198 if (processing_template_decl)
2199 return build_min (SIZEOF_EXPR, sizetype, t);
2201 return TYPE_P (t) ? c_sizeof (t) : expr_sizeof (t);
2204 /* Implement the __alignof keyword: Return the minimum required
2205 alignment of T, measured in bytes. */
2211 if (processing_template_decl)
2212 return build_min (ALIGNOF_EXPR, sizetype, t);
2214 return TYPE_P (t) ? c_alignof (t) : c_alignof_expr (t);
2217 /* Generate RTL for the statement T, and its substatements, and any
2218 other statements at its nesting level. */
2224 switch (TREE_CODE (t))
2227 genrtl_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2231 genrtl_ctor_stmt (t);
2235 genrtl_try_block (t);
2239 genrtl_eh_spec_block (t);
2247 genrtl_subobject (SUBOBJECT_CLEANUP (t));
2251 genrtl_named_return_value ();
2258 my_friendly_abort (19990810);
2263 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2264 will equivalent CALL_EXPRs. */
2267 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2269 int *walk_subtrees ATTRIBUTE_UNUSED;
2270 void *data ATTRIBUTE_UNUSED;
2272 tree aggr_init_expr;
2278 int copy_from_buffer_p;
2280 aggr_init_expr = *tp;
2281 /* We don't need to walk into types; there's nothing in a type that
2282 needs simplification. (And, furthermore, there are places we
2283 actively don't want to go. For example, we don't want to wander
2284 into the default arguments for a FUNCTION_DECL that appears in a
2286 if (TYPE_P (aggr_init_expr))
2291 /* Only AGGR_INIT_EXPRs are interesting. */
2292 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2295 /* Form an appropriate CALL_EXPR. */
2296 fn = TREE_OPERAND (aggr_init_expr, 0);
2297 args = TREE_OPERAND (aggr_init_expr, 1);
2298 slot = TREE_OPERAND (aggr_init_expr, 2);
2299 type = TREE_TYPE (aggr_init_expr);
2300 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2302 /* Replace the first argument with the address of the third
2303 argument to the AGGR_INIT_EXPR. */
2304 mark_addressable (slot);
2305 args = tree_cons (NULL_TREE,
2307 build_pointer_type (TREE_TYPE (slot)),
2311 call_expr = build (CALL_EXPR,
2312 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2313 fn, args, NULL_TREE);
2314 TREE_SIDE_EFFECTS (call_expr) = 1;
2316 /* If we're using the non-reentrant PCC calling convention, then we
2317 need to copy the returned value out of the static buffer into the
2319 copy_from_buffer_p = 0;
2320 #ifdef PCC_STATIC_STRUCT_RETURN
2321 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2323 int old_ac = flag_access_control;
2325 flag_access_control = 0;
2326 call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2327 flag_access_control = old_ac;
2328 copy_from_buffer_p = 1;
2332 /* If this AGGR_INIT_EXPR indicates the value returned by a
2333 function, then we want to use the value of the initialized
2334 location as the result. */
2335 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2337 call_expr = build (COMPOUND_EXPR, type,
2339 TREE_SIDE_EFFECTS (call_expr) = 1;
2342 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2343 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2346 /* Keep iterating. */
2350 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2353 emit_associated_thunks (fn)
2356 /* When we use vcall offsets, we emit thunks with the virtual
2357 functions to which they thunk. The whole point of vcall offsets
2358 is so that you can know statically the entire set of thunks that
2359 will ever be needed for a given virtual function, thereby
2360 enabling you to output all the thunks with the function itself. */
2361 if (DECL_VIRTUAL_P (fn))
2366 for (binfo = TYPE_BINFO (DECL_CONTEXT (fn));
2368 binfo = TREE_CHAIN (binfo))
2369 for (v = BINFO_VIRTUALS (binfo); v; v = TREE_CHAIN (v))
2371 && (!integer_zerop (BV_DELTA (v))
2372 || BV_USE_VCALL_INDEX_P (v)))
2377 if (BV_USE_VCALL_INDEX_P (v))
2379 vcall_index = BV_VCALL_INDEX (v);
2380 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
2383 vcall_index = NULL_TREE;
2385 thunk = make_thunk (build1 (ADDR_EXPR,
2386 vfunc_ptr_type_node,
2390 use_thunk (thunk, /*emit_p=*/1);
2395 /* Generate RTL for FN. */
2402 const char *saved_input_filename;
2404 /* When the parser calls us after finishing the body of a template
2405 function, we don't really want to expand the body. When we're
2406 processing an in-class definition of an inline function,
2407 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2408 to look at the function itself. */
2409 if (processing_template_decl
2410 || (DECL_LANG_SPECIFIC (fn)
2411 && DECL_TEMPLATE_INFO (fn)
2412 && uses_template_parms (DECL_TI_ARGS (fn))))
2414 /* Normally, collection only occurs in rest_of_compilation. So,
2415 if we don't collect here, we never collect junk generated
2416 during the processing of templates until we hit a
2417 non-template function. */
2422 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2423 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2424 simplify_aggr_init_exprs_r,
2427 /* If this is a constructor or destructor body, we have to clone
2429 if (maybe_clone_body (fn))
2431 /* We don't want to process FN again, so pretend we've written
2432 it out, even though we haven't. */
2433 TREE_ASM_WRITTEN (fn) = 1;
2437 /* There's no reason to do any of the work here if we're only doing
2438 semantic analysis; this code just generates RTL. */
2439 if (flag_syntax_only)
2442 /* If possible, avoid generating RTL for this function. Instead,
2443 just record it as an inline function, and wait until end-of-file
2444 to decide whether to write it out or not. */
2445 if (/* We have to generate RTL if it's not an inline function. */
2446 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2447 /* Or if we have to emit code for inline functions anyhow. */
2448 && !flag_keep_inline_functions
2449 /* Or if we actually have a reference to the function. */
2450 && !DECL_NEEDED_P (fn))
2452 /* Set DECL_EXTERNAL so that assemble_external will be called as
2453 necessary. We'll clear it again in finish_file. */
2454 if (!DECL_EXTERNAL (fn))
2456 DECL_NOT_REALLY_EXTERN (fn) = 1;
2457 DECL_EXTERNAL (fn) = 1;
2459 /* Remember this function. In finish_file we'll decide if
2460 we actually need to write this function out. */
2462 /* Let the back-end know that this funtion exists. */
2463 (*debug_hooks->deferred_inline_function) (fn);
2467 /* Compute the appropriate object-file linkage for inline
2469 if (DECL_DECLARED_INLINE_P (fn))
2470 import_export_decl (fn);
2472 /* If FN is external, then there's no point in generating RTL for
2473 it. This situation can arise with an inline function under
2474 `-fexternal-tempaltes'; we instantiate the function, even though
2475 we're not planning on emitting it, in case we get a chance to
2477 if (DECL_EXTERNAL (fn))
2480 /* Emit any thunks that should be emitted at the same time as FN. */
2481 emit_associated_thunks (fn);
2483 timevar_push (TV_INTEGRATION);
2485 /* Optimize the body of the function before expanding it. */
2486 optimize_function (fn);
2488 timevar_pop (TV_INTEGRATION);
2489 timevar_push (TV_EXPAND);
2491 /* Save the current file name and line number. When we expand the
2492 body of the function, we'll set LINENO and INPUT_FILENAME so that
2493 error-mesages come out in the right places. */
2494 saved_lineno = lineno;
2495 saved_input_filename = input_filename;
2496 lineno = DECL_SOURCE_LINE (fn);
2497 input_filename = DECL_SOURCE_FILE (fn);
2499 genrtl_start_function (fn);
2500 current_function_is_thunk = DECL_THUNK_P (fn);
2502 /* Expand the body. */
2503 expand_stmt (DECL_SAVED_TREE (fn));
2505 /* Statements should always be full-expressions at the outermost set
2506 of curly braces for a function. */
2507 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2509 /* The outermost statement for a function contains the line number
2510 recorded when we finished processing the function. */
2511 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2513 /* Generate code for the function. */
2514 genrtl_finish_function (fn);
2516 /* If possible, obliterate the body of the function so that it can
2517 be garbage collected. */
2518 if (dump_enabled_p (TDI_all))
2519 /* Keep the body; we're going to dump it. */
2521 else if (DECL_INLINE (fn) && flag_inline_trees)
2522 /* We might need the body of this function so that we can expand
2523 it inline somewhere else. */
2526 /* We don't need the body; blow it away. */
2527 DECL_SAVED_TREE (fn) = NULL_TREE;
2529 /* And restore the current source position. */
2530 lineno = saved_lineno;
2531 input_filename = saved_input_filename;
2532 extract_interface_info ();
2534 timevar_pop (TV_EXPAND);
2537 /* Helper function for walk_tree, used by finish_function to override all
2538 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2539 value optimization. */
2542 nullify_returns_r (tp, walk_subtrees, data)
2547 tree nrv = (tree) data;
2549 /* No need to walk into types. There wouldn't be any need to walk into
2550 non-statements, except that we have to consider STMT_EXPRs. */
2553 else if (TREE_CODE (*tp) == RETURN_STMT)
2554 RETURN_EXPR (*tp) = NULL_TREE;
2555 else if (TREE_CODE (*tp) == CLEANUP_STMT
2556 && CLEANUP_DECL (*tp) == nrv)
2557 CLEANUP_EXPR (*tp) = NULL_TREE;
2559 /* Keep iterating. */
2563 /* Start generating the RTL for FN. */
2566 genrtl_start_function (fn)
2571 /* Tell everybody what function we're processing. */
2572 current_function_decl = fn;
2573 /* Get the RTL machinery going for this function. */
2574 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2575 /* Let everybody know that we're expanding this function, not doing
2576 semantic analysis. */
2579 /* Even though we're inside a function body, we still don't want to
2580 call expand_expr to calculate the size of a variable-sized array.
2581 We haven't necessarily assigned RTL to all variables yet, so it's
2582 not safe to try to expand expressions involving them. */
2583 immediate_size_expand = 0;
2584 cfun->x_dont_save_pending_sizes_p = 1;
2586 /* Let the user know we're compiling this function. */
2587 announce_function (fn);
2589 /* Initialize the per-function data. */
2590 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2591 if (DECL_SAVED_FUNCTION_DATA (fn))
2593 /* If we already parsed this function, and we're just expanding it
2594 now, restore saved state. */
2595 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2597 /* This function is being processed in whole-function mode; we
2598 already did semantic analysis. */
2599 cfun->x_whole_function_mode_p = 1;
2601 /* If we decided that we didn't want to inline this function,
2602 make sure the back-end knows that. */
2603 if (!current_function_cannot_inline)
2604 current_function_cannot_inline = cp_function_chain->cannot_inline;
2606 /* We don't need the saved data anymore. */
2607 free (DECL_SAVED_FUNCTION_DATA (fn));
2608 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2611 /* Tell the cross-reference machinery that we're defining this
2613 GNU_xref_function (fn, DECL_ARGUMENTS (fn));
2615 /* Keep track of how many functions we're presently expanding. */
2618 /* Create a binding level for the parameters. */
2619 expand_start_bindings (2);
2620 /* Go through the PARM_DECLs for this function to see if any need
2622 for (parm = DECL_ARGUMENTS (fn); parm; parm = TREE_CHAIN (parm))
2623 if (TREE_TYPE (parm) != error_mark_node
2624 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (parm)))
2626 expand_function_start (fn, /*parms_have_cleanups=*/1);
2630 expand_function_start (fn, /*parms_have_cleanups=*/0);
2631 /* If this function is `main'. */
2632 if (DECL_MAIN_P (fn))
2633 expand_main_function ();
2634 /* Create a binding contour which can be used to catch
2635 cleanup-generated temporaries. */
2636 expand_start_bindings (2);
2638 /* Give our named return value the same RTL as our RESULT_DECL. */
2639 if (current_function_return_value)
2640 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2643 /* Finish generating the RTL for FN. */
2646 genrtl_finish_function (fn)
2649 tree no_return_label = NULL_TREE;
2653 if (write_symbols != NO_DEBUG)
2655 /* Keep this code around in case we later want to control debug info
2656 based on whether a type is "used". (jason 1999-11-11) */
2658 tree ttype = target_type (fntype);
2661 if (IS_AGGR_TYPE (ttype))
2662 /* Let debugger know it should output info for this type. */
2663 note_debug_info_needed (ttype);
2665 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2667 ttype = target_type (TREE_TYPE (parmdecl));
2668 if (IS_AGGR_TYPE (ttype))
2669 /* Let debugger know it should output info for this type. */
2670 note_debug_info_needed (ttype);
2675 /* Clean house because we will need to reorder insns here. */
2676 do_pending_stack_adjust ();
2678 if (!dtor_label && !DECL_CONSTRUCTOR_P (fn)
2679 && return_label != NULL_RTX
2680 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
2681 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2683 /* If this function is supposed to return a value, ensure that
2684 we do not fall into the cleanups by mistake. The end of our
2685 function will look like this:
2687 user code (may have return stmt somewhere)
2688 goto no_return_label
2693 NOTE_INSN_FUNCTION_END
2697 If the user omits a return stmt in the USER CODE section, we
2698 will have a control path which reaches NOTE_INSN_FUNCTION_END.
2699 Otherwise, we won't. */
2700 if (no_return_label)
2702 DECL_CONTEXT (no_return_label) = fn;
2703 DECL_INITIAL (no_return_label) = error_mark_node;
2704 DECL_SOURCE_FILE (no_return_label) = input_filename;
2705 DECL_SOURCE_LINE (no_return_label) = lineno;
2706 expand_goto (no_return_label);
2711 /* Remove the binding contour which is used to catch
2712 cleanup-generated temporaries. */
2713 expand_end_bindings (0, 0, 0);
2716 /* Emit label at beginning of cleanup code for parameters. */
2717 emit_label (cleanup_label);
2720 /* Finish building code that will trigger warnings if users forget
2721 to make their functions return values. */
2723 emit_jump (return_label);
2724 if (no_return_label)
2726 /* We don't need to call `expand_*_return' here because we don't
2727 need any cleanups here--this path of code is only for error
2728 checking purposes. */
2729 expand_label (no_return_label);
2732 /* We hard-wired immediate_size_expand to zero in start_function.
2733 Expand_function_end will decrement this variable. So, we set the
2734 variable to one here, so that after the decrement it will remain
2736 immediate_size_expand = 1;
2738 /* Generate rtl for function exit. */
2739 expand_function_end (input_filename, lineno, 1);
2741 /* If this is a nested function (like a template instantiation that
2742 we're compiling in the midst of compiling something else), push a
2743 new GC context. That will keep local variables on the stack from
2744 being collected while we're doing the compilation of this
2746 if (function_depth > 1)
2747 ggc_push_context ();
2749 /* There's no need to defer outputting this function any more; we
2750 know we want to output it. */
2751 DECL_DEFER_OUTPUT (fn) = 0;
2753 /* Run the optimizers and output the assembler code for this
2755 rest_of_compilation (fn);
2757 /* Undo the call to ggc_push_context above. */
2758 if (function_depth > 1)
2762 /* Keep this code around in case we later want to control debug info
2763 based on whether a type is "used". (jason 1999-11-11) */
2765 if (ctype && TREE_ASM_WRITTEN (fn))
2766 note_debug_info_needed (ctype);
2769 /* If this function is marked with the constructor attribute, add it
2770 to the list of functions to be called along with constructors
2771 from static duration objects. */
2772 if (DECL_STATIC_CONSTRUCTOR (fn))
2773 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2775 /* If this function is marked with the destructor attribute, add it
2776 to the list of functions to be called along with destructors from
2777 static duration objects. */
2778 if (DECL_STATIC_DESTRUCTOR (fn))
2779 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2783 /* In C++, we should never be saving RTL for the function. */
2784 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2786 /* Since we don't need the RTL for this function anymore, stop
2787 pointing to it. That's especially important for LABEL_DECLs,
2788 since you can reach all the instructions in the function from the
2789 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2790 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2792 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2796 /* Clear out the RTL for the arguments. */
2797 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2799 SET_DECL_RTL (t, NULL_RTX);
2800 DECL_INCOMING_RTL (t) = NULL_RTX;
2803 if (!(flag_inline_trees && DECL_INLINE (fn)))
2804 /* DECL_INITIAL must remain nonzero so we know this was an
2805 actual function definition. */
2806 DECL_INITIAL (fn) = error_mark_node;
2808 /* Let the error reporting routines know that we're outside a
2809 function. For a nested function, this value is used in
2810 pop_cp_function_context and then reset via pop_function_context. */
2811 current_function_decl = NULL_TREE;
2814 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2818 clear_decl_rtl (tp, walk_subtrees, data)
2820 int *walk_subtrees ATTRIBUTE_UNUSED;
2821 void *data ATTRIBUTE_UNUSED;
2823 if (nonstatic_local_decl_p (*tp))
2824 SET_DECL_RTL (*tp, NULL_RTX);
2829 /* Perform initialization related to this module. */
2832 init_cp_semantics ()
2834 lang_expand_stmt = cp_expand_stmt;