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 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_named_return_value PARAMS ((void));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
65 /* Finish processing the COND, the SUBSTMT condition for STMT. */
67 #define FINISH_COND(COND, STMT, SUBSTMT) \
69 if (last_tree != (STMT)) \
71 RECHAIN_STMTS (STMT, SUBSTMT); \
72 if (!processing_template_decl) \
74 (COND) = build_tree_list (SUBSTMT, COND); \
82 /* Returns non-zero if the current statement is a full expression,
83 i.e. temporaries created during that statement should be destroyed
84 at the end of the statement. */
87 stmts_are_full_exprs_p ()
89 return current_stmt_tree ()->stmts_are_full_exprs_p;
92 /* Returns the stmt_tree (if any) to which statements are currently
93 being added. If there is no active statement-tree, NULL is
100 ? &cfun->language->x_stmt_tree
101 : &scope_chain->x_stmt_tree);
104 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
105 flag for this because "A union for which objects or pointers are
106 declared is not an anonymous union" [class.union]. */
109 anon_aggr_type_p (node)
112 return (CLASS_TYPE_P (node) && TYPE_LANG_SPECIFIC(node)->anon_aggr);
115 /* Finish a scope. */
120 tree block = NULL_TREE;
122 if (stmts_are_full_exprs_p ())
124 tree scope_stmts = NULL_TREE;
126 if (!processing_template_decl)
127 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
129 block = poplevel (kept_level_p (), 1, 0);
130 if (block && !processing_template_decl)
132 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
133 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
140 /* Begin a new scope. */
145 if (stmts_are_full_exprs_p ())
148 if (!processing_template_decl)
149 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
153 /* Finish a goto-statement. */
156 finish_goto_stmt (destination)
159 if (TREE_CODE (destination) == IDENTIFIER_NODE)
160 destination = lookup_label (destination);
162 /* We warn about unused labels with -Wunused. That means we have to
163 mark the used labels as used. */
164 if (TREE_CODE (destination) == LABEL_DECL)
165 TREE_USED (destination) = 1;
167 if (TREE_CODE (destination) != LABEL_DECL)
168 /* We don't inline calls to functions with computed gotos.
169 Those functions are typically up to some funny business,
170 and may be depending on the labels being at particular
171 addresses, or some such. */
172 DECL_UNINLINABLE (current_function_decl) = 1;
174 check_goto (destination);
176 return add_stmt (build_stmt (GOTO_STMT, destination));
179 /* COND is the condition-expression for an if, while, etc.,
180 statement. Convert it to a boolean value, if appropriate. */
183 maybe_convert_cond (cond)
186 /* Empty conditions remain empty. */
190 /* Wait until we instantiate templates before doing conversion. */
191 if (processing_template_decl)
194 /* Do the conversion. */
195 cond = convert_from_reference (cond);
196 return condition_conversion (cond);
199 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
202 finish_expr_stmt (expr)
207 if (expr != NULL_TREE)
209 if (!processing_template_decl
210 && !(stmts_are_full_exprs_p ())
211 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
213 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
214 expr = default_conversion (expr);
216 if (stmts_are_full_exprs_p ())
217 expr = convert_to_void (expr, "statement");
219 r = add_stmt (build_stmt (EXPR_STMT, expr));
224 /* This was an expression-statement, so we save the type of the
226 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
232 /* Begin an if-statement. Returns a newly created IF_STMT if
240 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
245 /* Process the COND of an if-statement, which may be given by
249 finish_if_stmt_cond (cond, if_stmt)
253 cond = maybe_convert_cond (cond);
254 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
257 /* Finish the then-clause of an if-statement, which may be given by
261 finish_then_clause (if_stmt)
264 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
269 /* Begin the else-clause of an if-statement. */
276 /* Finish the else-clause of an if-statement, which may be given by
280 finish_else_clause (if_stmt)
283 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
286 /* Finish an if-statement. */
298 /* If COND wasn't a declaration, clear out the
299 block we made for it and start a new one here so the
300 optimization in expand_end_loop will work. */
301 if (getdecls () == NULL_TREE)
308 /* Begin a while-statement. Returns a newly created WHILE_STMT if
315 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
321 /* Process the COND of a while-statement, which may be given by
325 finish_while_stmt_cond (cond, while_stmt)
329 cond = maybe_convert_cond (cond);
330 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
334 /* Finish a while-statement, which may be given by WHILE_STMT. */
337 finish_while_stmt (while_stmt)
341 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
345 /* Begin a do-statement. Returns a newly created DO_STMT if
351 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
356 /* Finish the body of a do-statement, which may be given by DO_STMT. */
359 finish_do_body (do_stmt)
362 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
365 /* Finish a do-statement, which may be given by DO_STMT, and whose
366 COND is as indicated. */
369 finish_do_stmt (cond, do_stmt)
373 cond = maybe_convert_cond (cond);
374 DO_COND (do_stmt) = cond;
378 /* Finish a return-statement. The EXPRESSION returned, if any, is as
382 finish_return_stmt (expr)
387 if (!processing_template_decl)
388 expr = check_return_expr (expr);
389 if (!processing_template_decl)
391 if (DECL_DESTRUCTOR_P (current_function_decl))
393 /* Similarly, all destructors must run destructors for
394 base-classes before returning. So, all returns in a
395 destructor get sent to the DTOR_LABEL; finish_function emits
396 code to return a value there. */
397 return finish_goto_stmt (dtor_label);
400 r = add_stmt (build_stmt (RETURN_STMT, expr));
406 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
413 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
414 NULL_TREE, NULL_TREE);
415 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
417 if (NEW_FOR_SCOPE_P (r))
420 note_level_for_for ();
426 /* Finish the for-init-statement of a for-statement, which may be
427 given by FOR_STMT. */
430 finish_for_init_stmt (for_stmt)
433 if (last_tree != for_stmt)
434 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
438 /* Finish the COND of a for-statement, which may be given by
442 finish_for_cond (cond, for_stmt)
446 cond = maybe_convert_cond (cond);
447 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
451 /* Finish the increment-EXPRESSION in a for-statement, which may be
452 given by FOR_STMT. */
455 finish_for_expr (expr, for_stmt)
459 FOR_EXPR (for_stmt) = expr;
462 /* Finish the body of a for-statement, which may be given by
463 FOR_STMT. The increment-EXPR for the loop must be
467 finish_for_stmt (for_stmt)
470 /* Pop the scope for the body of the loop. */
472 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
473 if (NEW_FOR_SCOPE_P (for_stmt))
478 /* Finish a break-statement. */
483 return add_stmt (build_break_stmt ());
486 /* Finish a continue-statement. */
489 finish_continue_stmt ()
491 return add_stmt (build_continue_stmt ());
494 /* Begin a switch-statement. Returns a new SWITCH_STMT if
501 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
507 /* Finish the cond of a switch-statement. */
510 finish_switch_cond (cond, switch_stmt)
514 tree orig_type = NULL;
515 if (!processing_template_decl)
519 /* Convert the condition to an integer or enumeration type. */
520 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
521 if (cond == NULL_TREE)
523 error ("switch quantity not an integer");
524 cond = error_mark_node;
526 orig_type = TREE_TYPE (cond);
527 if (cond != error_mark_node)
529 cond = default_conversion (cond);
530 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
533 if (cond != error_mark_node)
535 index = get_unwidened (cond, NULL_TREE);
536 /* We can't strip a conversion from a signed type to an unsigned,
537 because if we did, int_fits_type_p would do the wrong thing
538 when checking case values for being in range,
539 and it's too hard to do the right thing. */
540 if (TREE_UNSIGNED (TREE_TYPE (cond))
541 == TREE_UNSIGNED (TREE_TYPE (index)))
545 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
546 SWITCH_TYPE (switch_stmt) = orig_type;
547 push_switch (switch_stmt);
550 /* Finish the body of a switch-statement, which may be given by
551 SWITCH_STMT. The COND to switch on is indicated. */
554 finish_switch_stmt (switch_stmt)
557 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
563 /* Generate the RTL for T, which is a TRY_BLOCK. */
571 expand_eh_region_start ();
572 expand_stmt (TRY_STMTS (t));
573 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
577 if (!FN_TRY_BLOCK_P (t))
578 emit_line_note (input_filename, lineno);
580 expand_eh_region_start ();
581 expand_stmt (TRY_STMTS (t));
583 if (FN_TRY_BLOCK_P (t))
585 expand_start_all_catch ();
586 in_function_try_handler = 1;
587 expand_stmt (TRY_HANDLERS (t));
588 in_function_try_handler = 0;
589 expand_end_all_catch ();
593 expand_start_all_catch ();
594 expand_stmt (TRY_HANDLERS (t));
595 expand_end_all_catch ();
600 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
603 genrtl_eh_spec_block (t)
606 expand_eh_region_start ();
607 expand_stmt (EH_SPEC_STMTS (t));
608 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
609 build_call (call_unexpected_node,
610 tree_cons (NULL_TREE,
615 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
621 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
626 /* Likewise, for a function-try-block. */
629 begin_function_try_block ()
631 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
632 FN_TRY_BLOCK_P (r) = 1;
637 /* Finish a try-block, which may be given by TRY_BLOCK. */
640 finish_try_block (try_block)
643 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
646 /* Finish the body of a cleanup try-block, which may be given by
650 finish_cleanup_try_block (try_block)
653 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
656 /* Finish an implicitly generated try-block, with a cleanup is given
660 finish_cleanup (cleanup, try_block)
664 TRY_HANDLERS (try_block) = cleanup;
665 CLEANUP_P (try_block) = 1;
668 /* Likewise, for a function-try-block. */
671 finish_function_try_block (try_block)
674 if (TREE_CHAIN (try_block)
675 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
677 /* Chain the compound statement after the CTOR_INITIALIZER. */
678 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
679 /* And make the CTOR_INITIALIZER the body of the try-block. */
680 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
683 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
684 in_function_try_handler = 1;
687 /* Finish a handler-sequence for a try-block, which may be given by
691 finish_handler_sequence (try_block)
694 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
695 check_handlers (TRY_HANDLERS (try_block));
698 /* Likewise, for a function-try-block. */
701 finish_function_handler_sequence (try_block)
704 in_function_try_handler = 0;
705 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
706 check_handlers (TRY_HANDLERS (try_block));
709 /* Generate the RTL for T, which is a HANDLER. */
715 genrtl_do_pushlevel ();
716 if (!processing_template_decl)
717 expand_start_catch (HANDLER_TYPE (t));
718 expand_stmt (HANDLER_BODY (t));
719 if (!processing_template_decl)
723 /* Begin a handler. Returns a HANDLER if appropriate. */
729 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
731 /* Create a binding level for the eh_info and the exception object
734 note_level_for_catch ();
738 /* Finish the handler-parameters for a handler, which may be given by
739 HANDLER. DECL is the declaration for the catch parameter, or NULL
740 if this is a `catch (...)' clause. */
743 finish_handler_parms (decl, handler)
747 tree type = NULL_TREE;
748 if (processing_template_decl)
752 decl = pushdecl (decl);
753 decl = push_template_decl (decl);
754 add_decl_stmt (decl);
755 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
756 type = TREE_TYPE (decl);
760 type = expand_start_catch_block (decl);
762 HANDLER_TYPE (handler) = type;
765 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
766 the return value from the matching call to finish_handler_parms. */
769 finish_handler (handler)
772 if (!processing_template_decl)
773 expand_end_catch_block ();
775 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
778 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
779 compound-statement does not define a scope. Returns a new
780 COMPOUND_STMT if appropriate. */
783 begin_compound_stmt (has_no_scope)
789 r = build_stmt (COMPOUND_STMT, NULL_TREE);
791 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
796 COMPOUND_STMT_NO_SCOPE (r) = 1;
798 last_expr_type = NULL_TREE;
804 note_level_for_try ();
807 /* Normally, we try hard to keep the BLOCK for a
808 statement-expression. But, if it's a statement-expression with
809 a scopeless block, there's nothing to keep, and we don't want
810 to accidentally keep a block *inside* the scopeless block. */
816 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
817 If HAS_NO_SCOPE is non-zero, the compound statement does not define
821 finish_compound_stmt (has_no_scope, compound_stmt)
833 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
835 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
836 the precise purpose of that variable is store the type of the
837 last expression statement within the last compound statement, we
838 preserve the value. */
846 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
847 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
851 finish_asm_stmt (cv_qualifier, string, output_operands,
852 input_operands, clobbers)
855 tree output_operands;
862 if (TREE_CHAIN (string))
863 string = combine_strings (string);
865 if (cv_qualifier != NULL_TREE
866 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
868 warning ("%s qualifier ignored on asm",
869 IDENTIFIER_POINTER (cv_qualifier));
870 cv_qualifier = NULL_TREE;
873 if (!processing_template_decl)
879 for (t = input_operands; t; t = TREE_CHAIN (t))
881 tree converted_operand
882 = decay_conversion (TREE_VALUE (t));
884 /* If the type of the operand hasn't been determined (e.g.,
885 because it involves an overloaded function), then issue
886 an error message. There's no context available to
887 resolve the overloading. */
888 if (TREE_TYPE (converted_operand) == unknown_type_node)
890 error ("type of asm operand `%E' could not be determined",
892 converted_operand = error_mark_node;
894 TREE_VALUE (t) = converted_operand;
897 ninputs = list_length (input_operands);
898 noutputs = list_length (output_operands);
900 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
905 const char *constraint;
908 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
909 operand = TREE_VALUE (output_operands);
911 if (!parse_output_constraint (&constraint,
912 i, ninputs, noutputs,
917 /* By marking the type as erroneous, we will not try to
918 process this operand again in expand_asm_operands. */
919 TREE_TYPE (operand) = error_mark_node;
923 /* If the operand is a DECL that is going to end up in
924 memory, assume it is addressable. This is a bit more
925 conservative than it would ideally be; the exact test is
926 buried deep in expand_asm_operands and depends on the
927 DECL_RTL for the OPERAND -- which we don't have at this
929 if (!allows_reg && DECL_P (operand))
930 cxx_mark_addressable (operand);
934 r = build_stmt (ASM_STMT, cv_qualifier, string,
935 output_operands, input_operands,
940 /* Finish a label with the indicated NAME. */
943 finish_label_stmt (name)
946 tree decl = define_label (input_filename, lineno, name);
947 add_stmt (build_stmt (LABEL_STMT, decl));
950 /* Finish a series of declarations for local labels. G++ allows users
951 to declare "local" labels, i.e., labels with scope. This extension
952 is useful when writing code involving statement-expressions. */
955 finish_label_decl (name)
958 tree decl = declare_local_label (name);
959 add_decl_stmt (decl);
962 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
965 finish_decl_cleanup (decl, cleanup)
969 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
972 /* If the current scope exits with an exception, run CLEANUP. */
975 finish_eh_cleanup (cleanup)
978 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
979 CLEANUP_EH_ONLY (r) = 1;
983 /* Generate the RTL for a RETURN_INIT. */
986 genrtl_named_return_value ()
988 tree decl = DECL_RESULT (current_function_decl);
990 /* If this named return value comes in a register, put it in a
992 if (DECL_REGISTER (decl))
994 /* Note that the mode of the old DECL_RTL may be wider than the
995 mode of DECL_RESULT, depending on the calling conventions for
996 the processor. For example, on the Alpha, a 32-bit integer
997 is returned in a DImode register -- the DECL_RESULT has
998 SImode but the DECL_RTL for the DECL_RESULT has DImode. So,
999 here, we use the mode the back-end has already assigned for
1000 the return value. */
1001 SET_DECL_RTL (decl, gen_reg_rtx (GET_MODE (DECL_RTL (decl))));
1002 if (TREE_ADDRESSABLE (decl))
1003 put_var_into_stack (decl);
1006 emit_local_var (decl);
1009 /* Bind a name and initialization to the return value of
1010 the current function. */
1013 finish_named_return_value (return_id, init)
1014 tree return_id, init;
1016 tree decl = DECL_RESULT (current_function_decl);
1018 /* Give this error as many times as there are occurrences, so that
1019 users can use Emacs compilation buffers to find and fix all such
1022 pedwarn ("ISO C++ does not permit named return values");
1023 cp_deprecated ("the named return value extension");
1025 if (return_id != NULL_TREE)
1027 if (DECL_NAME (decl) == NULL_TREE)
1028 DECL_NAME (decl) = return_id;
1031 error ("return identifier `%D' already in place", return_id);
1036 /* Can't let this happen for constructors. */
1037 if (DECL_CONSTRUCTOR_P (current_function_decl))
1039 error ("can't redefine default return value for constructors");
1043 /* If we have a named return value, put that in our scope as well. */
1044 if (DECL_NAME (decl) != NULL_TREE)
1046 /* Let `cp_finish_decl' know that this initializer is ok. */
1047 DECL_INITIAL (decl) = init;
1048 if (doing_semantic_analysis_p ())
1050 if (!processing_template_decl)
1052 cp_finish_decl (decl, init, NULL_TREE, 0);
1053 add_stmt (build_stmt (RETURN_INIT, NULL_TREE, NULL_TREE));
1056 add_stmt (build_stmt (RETURN_INIT, return_id, init));
1059 /* Don't use tree-inlining for functions with named return values.
1060 That doesn't work properly because we don't do any translation of
1061 the RETURN_INITs when they are copied. */
1062 DECL_UNINLINABLE (current_function_decl) = 1;
1065 /* The INIT_LIST is a list of mem-initializers, in the order they were
1066 written by the user. The TREE_VALUE of each node is a list of
1067 initializers for a particular subobject. The TREE_PURPOSE is a
1068 FIELD_DECL is the initializer is for a non-static data member, and
1069 a class type if the initializer is for a base class. */
1072 finish_mem_initializers (init_list)
1075 tree member_init_list;
1076 tree base_init_list;
1077 tree last_base_warned_about;
1081 member_init_list = NULL_TREE;
1082 base_init_list = NULL_TREE;
1083 last_base_warned_about = NULL_TREE;
1085 for (init = init_list; init; init = next)
1087 next = TREE_CHAIN (init);
1088 if (TREE_CODE (TREE_PURPOSE (init)) == FIELD_DECL)
1090 TREE_CHAIN (init) = member_init_list;
1091 member_init_list = init;
1093 /* We're running through the initializers from right to left
1094 as we process them here. So, if we see a data member
1095 initializer after we see a base initializer, that
1096 actually means that the base initializer preceded the
1097 data member initializer. */
1098 if (warn_reorder && last_base_warned_about != base_init_list)
1102 for (base = base_init_list;
1103 base != last_base_warned_about;
1104 base = TREE_CHAIN (base))
1106 warning ("base initializer for `%T'",
1107 TREE_PURPOSE (base));
1108 warning (" will be re-ordered to precede member initializations");
1111 last_base_warned_about = base_init_list;
1116 TREE_CHAIN (init) = base_init_list;
1117 base_init_list = init;
1121 if (processing_template_decl)
1122 add_stmt (build_min_nt (CTOR_INITIALIZER,
1123 member_init_list, base_init_list));
1125 emit_base_init (member_init_list, base_init_list);
1128 /* Returns the stack of SCOPE_STMTs for the current function. */
1131 current_scope_stmt_stack ()
1133 return &cfun->language->x_scope_stmt_stack;
1136 /* Finish a parenthesized expression EXPR. */
1139 finish_parenthesized_expr (expr)
1142 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1143 /* This inhibits warnings in c_common_truthvalue_conversion. */
1144 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1146 if (TREE_CODE (expr) == OFFSET_REF)
1147 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1148 enclosed in parentheses. */
1149 PTRMEM_OK_P (expr) = 0;
1153 /* Begin a statement-expression. The value returned must be passed to
1154 finish_stmt_expr. */
1159 /* If we're outside a function, we won't have a statement-tree to
1160 work with. But, if we see a statement-expression we need to
1162 if (! cfun && !last_tree)
1163 begin_stmt_tree (&scope_chain->x_saved_tree);
1165 keep_next_level (1);
1166 /* If we're building a statement tree, then the upcoming compound
1167 statement will be chained onto the tree structure, starting at
1168 last_tree. We return last_tree so that we can later unhook the
1169 compound statement. */
1173 /* Used when beginning a statement-expression outside function scope.
1174 For example, when handling a file-scope initializer, we use this
1178 begin_global_stmt_expr ()
1180 if (! cfun && !last_tree)
1181 begin_stmt_tree (&scope_chain->x_saved_tree);
1183 keep_next_level (1);
1185 return (last_tree != NULL_TREE) ? last_tree : expand_start_stmt_expr();
1188 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1191 finish_global_stmt_expr (stmt_expr)
1194 stmt_expr = expand_end_stmt_expr (stmt_expr);
1197 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1198 finish_stmt_tree (&scope_chain->x_saved_tree);
1203 /* Finish a statement-expression. RTL_EXPR should be the value
1204 returned by the previous begin_stmt_expr; EXPR is the
1205 statement-expression. Returns an expression representing the
1206 statement-expression. */
1209 finish_stmt_expr (rtl_expr)
1214 /* If the last thing in the statement-expression was not an
1215 expression-statement, then it has type `void'. */
1216 if (!last_expr_type)
1217 last_expr_type = void_type_node;
1218 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1219 TREE_SIDE_EFFECTS (result) = 1;
1221 /* Remove the compound statement from the tree structure; it is
1222 now saved in the STMT_EXPR. */
1223 last_tree = rtl_expr;
1224 TREE_CHAIN (last_tree) = NULL_TREE;
1226 /* If we created a statement-tree for this statement-expression,
1229 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1230 finish_stmt_tree (&scope_chain->x_saved_tree);
1235 /* Finish a call to FN with ARGS. Returns a representation of the
1239 finish_call_expr (fn, args, koenig)
1248 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1249 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1250 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1251 fn = do_identifier (fn, 2, args);
1253 result = build_x_function_call (fn, args, current_class_ref);
1255 if (TREE_CODE (result) == CALL_EXPR
1256 && (! TREE_TYPE (result)
1257 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1258 result = require_complete_type (result);
1263 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1264 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1265 POSTDECREMENT_EXPR.) */
1268 finish_increment_expr (expr, code)
1270 enum tree_code code;
1272 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1273 a COMPONENT_REF). This way if we've got, say, a reference to a
1274 static member that's being operated on, we don't end up trying to
1275 find a member operator for the class it's in. */
1277 if (TREE_CODE (expr) == OFFSET_REF)
1278 expr = resolve_offset_ref (expr);
1279 return build_x_unary_op (code, expr);
1282 /* Finish a use of `this'. Returns an expression for `this'. */
1289 if (current_class_ptr)
1291 result = current_class_ptr;
1293 else if (current_function_decl
1294 && DECL_STATIC_FUNCTION_P (current_function_decl))
1296 error ("`this' is unavailable for static member functions");
1297 result = error_mark_node;
1301 if (current_function_decl)
1302 error ("invalid use of `this' in non-member function");
1304 error ("invalid use of `this' at top level");
1305 result = error_mark_node;
1311 /* Finish a member function call using OBJECT and ARGS as arguments to
1312 FN. Returns an expression for the call. */
1315 finish_object_call_expr (fn, object, args)
1321 /* This is a future direction of this code, but because
1322 build_x_function_call cannot always undo what is done in
1323 build_component_ref entirely yet, we cannot do this. */
1325 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1326 return finish_call_expr (real_fn, args);
1328 if (DECL_DECLARES_TYPE_P (fn))
1330 if (processing_template_decl)
1331 /* This can happen on code like:
1334 template <class T> void f(T t) {
1338 We just grab the underlying IDENTIFIER. */
1339 fn = DECL_NAME (fn);
1342 error ("calling type `%T' like a method", fn);
1343 return error_mark_node;
1347 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1351 /* Finish a qualified member function call using OBJECT and ARGS as
1352 arguments to FN. Returns an expression for the call. */
1355 finish_qualified_object_call_expr (fn, object, args)
1360 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1361 TREE_OPERAND (fn, 1), args);
1364 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1365 being the scope, if any, of DESTRUCTOR. Returns an expression for
1369 finish_pseudo_destructor_call_expr (object, scope, destructor)
1374 if (processing_template_decl)
1375 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1377 if (scope && scope != destructor)
1378 error ("destructor specifier `%T::~%T()' must have matching names",
1381 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1382 && (TREE_CODE (TREE_TYPE (object)) !=
1383 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1384 error ("`%E' is not of type `%T'", object, destructor);
1386 return cp_convert (void_type_node, object);
1389 /* Finish a call to a globally qualified member function FN using
1390 ARGS. Returns an expression for the call. */
1393 finish_qualified_call_expr (fn, args)
1397 if (processing_template_decl)
1398 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1400 return build_member_call (TREE_OPERAND (fn, 0),
1401 TREE_OPERAND (fn, 1),
1405 /* Finish an expression of the form CODE EXPR. */
1408 finish_unary_op_expr (code, expr)
1409 enum tree_code code;
1412 tree result = build_x_unary_op (code, expr);
1413 /* Inside a template, build_x_unary_op does not fold the
1414 expression. So check whether the result is folded before
1415 setting TREE_NEGATED_INT. */
1416 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1417 && TREE_CODE (result) == INTEGER_CST
1418 && !TREE_UNSIGNED (TREE_TYPE (result))
1419 && INT_CST_LT (result, integer_zero_node))
1420 TREE_NEGATED_INT (result) = 1;
1421 overflow_warning (result);
1425 /* Finish an id-expression. */
1428 finish_id_expr (expr)
1431 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1432 expr = do_identifier (expr, 1, NULL_TREE);
1434 if (TREE_TYPE (expr) == error_mark_node)
1435 expr = error_mark_node;
1439 static tree current_type_lookups;
1441 /* Perform deferred access control for types used in the type of a
1445 deferred_type_access_control ()
1447 tree lookup = type_lookups;
1449 if (lookup == error_mark_node)
1452 for (; lookup; lookup = TREE_CHAIN (lookup))
1453 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1457 decl_type_access_control (decl)
1462 if (type_lookups == error_mark_node)
1465 save_fn = current_function_decl;
1467 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1468 current_function_decl = decl;
1470 deferred_type_access_control ();
1472 current_function_decl = save_fn;
1474 /* Now strip away the checks for the current declarator; they were
1475 added to type_lookups after typed_declspecs saved the copy that
1476 ended up in current_type_lookups. */
1477 type_lookups = current_type_lookups;
1481 save_type_access_control (lookups)
1484 current_type_lookups = lookups;
1487 /* Reset the deferred access control. */
1490 reset_type_access_control ()
1492 type_lookups = NULL_TREE;
1493 current_type_lookups = NULL_TREE;
1496 /* Begin a function definition declared with DECL_SPECS and
1497 DECLARATOR. Returns non-zero if the function-declaration is
1501 begin_function_definition (decl_specs, declarator)
1508 split_specs_attrs (decl_specs, &specs, &attrs);
1509 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1512 deferred_type_access_control ();
1513 type_lookups = error_mark_node;
1515 /* The things we're about to see are not directly qualified by any
1516 template headers we've seen thus far. */
1517 reset_specialization ();
1522 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1526 begin_constructor_declarator (scope, name)
1530 tree result = build_nt (SCOPE_REF, scope, name);
1531 enter_scope_of (result);
1535 /* Finish an init-declarator. Returns a DECL. */
1538 finish_declarator (declarator, declspecs, attributes,
1539 prefix_attributes, initialized)
1543 tree prefix_attributes;
1546 return start_decl (declarator, declspecs, initialized, attributes,
1550 /* Finish a translation unit. */
1553 finish_translation_unit ()
1555 /* In case there were missing closebraces,
1556 get us back to the global binding level. */
1558 while (current_namespace != global_namespace)
1561 /* Do file scope __FUNCTION__ et al. */
1562 finish_fname_decls ();
1567 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1568 Returns the parameter. */
1571 finish_template_type_parm (aggr, identifier)
1575 if (aggr != class_type_node)
1577 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1578 aggr = class_type_node;
1581 return build_tree_list (aggr, identifier);
1584 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1585 Returns the parameter. */
1588 finish_template_template_parm (aggr, identifier)
1592 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1593 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1594 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1595 DECL_TEMPLATE_RESULT (tmpl) = decl;
1596 DECL_ARTIFICIAL (decl) = 1;
1597 end_template_decl ();
1599 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1601 return finish_template_type_parm (aggr, tmpl);
1604 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1605 non-zero, the parameter list was terminated by a `...'. */
1608 finish_parmlist (parms, ellipsis)
1614 /* We mark the PARMS as a parmlist so that declarator processing can
1615 disambiguate certain constructs. */
1616 TREE_PARMLIST (parms) = 1;
1617 /* We do not append void_list_node here, but leave it to grokparms
1619 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1624 /* Begin a class definition, as indicated by T. */
1627 begin_class_definition (t)
1630 if (t == error_mark_node)
1631 return error_mark_node;
1633 /* Check the bases are accessible. */
1634 decl_type_access_control (TYPE_NAME (t));
1635 reset_type_access_control ();
1637 if (processing_template_parmlist)
1639 error ("definition of `%#T' inside template parameter list", t);
1640 return error_mark_node;
1643 /* In a definition of a member class template, we will get here with
1644 an implicit typename. */
1645 if (IMPLICIT_TYPENAME_P (t))
1647 /* A non-implicit typename comes from code like:
1649 template <typename T> struct A {
1650 template <typename U> struct A<T>::B ...
1652 This is erroneous. */
1653 else if (TREE_CODE (t) == TYPENAME_TYPE)
1655 error ("invalid definition of qualified type `%T'", t);
1656 t = error_mark_node;
1659 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1661 t = make_aggr_type (RECORD_TYPE);
1662 pushtag (make_anon_name (), t, 0);
1665 /* If we generated a partial instantiation of this type, but now
1666 we're seeing a real definition, we're actually looking at a
1667 partial specialization. Consider:
1669 template <class T, class U>
1675 template <class T, class U>
1678 typename X<Y<T, U> >::A a;
1681 template <class T, class U>
1686 We have to undo the effects of the previous partial
1688 if (PARTIAL_INSTANTIATION_P (t))
1692 /* Unfortunately, when we're not in pedantic mode, we
1693 attempt to actually fill in some of the fields of the
1694 partial instantiation, in order to support the implicit
1695 typename extension. Clear those fields now, in
1696 preparation for the definition here. The fields cleared
1697 here must match those set in instantiate_class_template.
1698 Look for a comment mentioning begin_class_definition
1700 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1701 TYPE_FIELDS (t) = NULL_TREE;
1702 TYPE_METHODS (t) = NULL_TREE;
1703 CLASSTYPE_TAGS (t) = NULL_TREE;
1704 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1705 TYPE_SIZE (t) = NULL_TREE;
1708 /* This isn't a partial instantiation any more. */
1709 PARTIAL_INSTANTIATION_P (t) = 0;
1711 /* If this type was already complete, and we see another definition,
1713 else if (COMPLETE_TYPE_P (t))
1714 duplicate_tag_error (t);
1716 /* Update the location of the decl. */
1717 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1718 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1720 if (TYPE_BEING_DEFINED (t))
1722 t = make_aggr_type (TREE_CODE (t));
1723 pushtag (TYPE_IDENTIFIER (t), t, 0);
1725 maybe_process_partial_specialization (t);
1727 TYPE_BEING_DEFINED (t) = 1;
1728 TYPE_PACKED (t) = flag_pack_struct;
1729 /* Reset the interface data, at the earliest possible
1730 moment, as it might have been set via a class foo;
1732 if (! TYPE_ANONYMOUS_P (t))
1734 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1735 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1736 (t, interface_unknown);
1738 reset_specialization();
1740 /* Make a declaration for this class in its own scope. */
1741 build_self_reference ();
1746 /* Finish the member declaration given by DECL. */
1749 finish_member_declaration (decl)
1752 if (decl == error_mark_node || decl == NULL_TREE)
1755 if (decl == void_type_node)
1756 /* The COMPONENT was a friend, not a member, and so there's
1757 nothing for us to do. */
1760 /* We should see only one DECL at a time. */
1761 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1763 /* Set up access control for DECL. */
1765 = (current_access_specifier == access_private_node);
1766 TREE_PROTECTED (decl)
1767 = (current_access_specifier == access_protected_node);
1768 if (TREE_CODE (decl) == TEMPLATE_DECL)
1770 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1771 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1774 /* Mark the DECL as a member of the current class. */
1775 DECL_CONTEXT (decl) = current_class_type;
1779 A C language linkage is ignored for the names of class members
1780 and the member function type of class member functions. */
1781 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1782 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1784 /* Put functions on the TYPE_METHODS list and everything else on the
1785 TYPE_FIELDS list. Note that these are built up in reverse order.
1786 We reverse them (to obtain declaration order) in finish_struct. */
1787 if (TREE_CODE (decl) == FUNCTION_DECL
1788 || DECL_FUNCTION_TEMPLATE_P (decl))
1790 /* We also need to add this function to the
1791 CLASSTYPE_METHOD_VEC. */
1792 add_method (current_class_type, decl, /*error_p=*/0);
1794 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1795 TYPE_METHODS (current_class_type) = decl;
1799 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1800 go at the beginning. The reason is that lookup_field_1
1801 searches the list in order, and we want a field name to
1802 override a type name so that the "struct stat hack" will
1803 work. In particular:
1805 struct S { enum E { }; int E } s;
1808 is legal. In addition, the FIELD_DECLs must be maintained in
1809 declaration order so that class layout works as expected.
1810 However, we don't need that order until class layout, so we
1811 save a little time by putting FIELD_DECLs on in reverse order
1812 here, and then reversing them in finish_struct_1. (We could
1813 also keep a pointer to the correct insertion points in the
1816 if (TREE_CODE (decl) == TYPE_DECL)
1817 TYPE_FIELDS (current_class_type)
1818 = chainon (TYPE_FIELDS (current_class_type), decl);
1821 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1822 TYPE_FIELDS (current_class_type) = decl;
1825 /* Enter the DECL into the scope of the class. */
1826 if (TREE_CODE (decl) != USING_DECL)
1827 pushdecl_class_level (decl);
1831 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1832 the definition is immediately followed by a semicolon. Returns the
1836 finish_class_definition (t, attributes, semi, pop_scope_p)
1842 if (t == error_mark_node)
1843 return error_mark_node;
1845 /* finish_struct nukes this anyway; if finish_exception does too,
1848 note_got_semicolon (t);
1850 /* If we got any attributes in class_head, xref_tag will stick them in
1851 TREE_TYPE of the type. Grab them now. */
1852 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1853 TYPE_ATTRIBUTES (t) = NULL_TREE;
1855 if (TREE_CODE (t) == ENUMERAL_TYPE)
1859 t = finish_struct (t, attributes);
1861 note_got_semicolon (t);
1865 check_for_missing_semicolon (t);
1867 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1868 if (current_scope () == current_function_decl)
1869 do_pending_defargs ();
1874 /* Finish processing the default argument expressions cached during
1875 the processing of a class definition. */
1878 begin_inline_definitions ()
1880 if (current_scope () == current_function_decl)
1881 do_pending_inlines ();
1884 /* Finish processing the inline function definitions cached during the
1885 processing of a class definition. */
1888 finish_inline_definitions ()
1890 if (current_class_type == NULL_TREE)
1891 clear_inline_text_obstack ();
1894 /* Finish processing the declaration of a member class template
1895 TYPES whose template parameters are given by PARMS. */
1898 finish_member_class_template (types)
1903 /* If there are declared, but undefined, partial specializations
1904 mixed in with the typespecs they will not yet have passed through
1905 maybe_process_partial_specialization, so we do that here. */
1906 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1907 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1908 maybe_process_partial_specialization (TREE_VALUE (t));
1910 note_list_got_semicolon (types);
1911 grok_x_components (types);
1912 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1913 /* The component was in fact a friend declaration. We avoid
1914 finish_member_template_decl performing certain checks by
1918 finish_member_template_decl (types);
1920 /* As with other component type declarations, we do
1921 not store the new DECL on the list of
1926 /* Finish processing a complete template declaration. The PARMS are
1927 the template parameters. */
1930 finish_template_decl (parms)
1934 end_template_decl ();
1936 end_specialization ();
1939 /* Finish processing a template-id (which names a type) of the form
1940 NAME < ARGS >. Return the TYPE_DECL for the type named by the
1941 template-id. If ENTERING_SCOPE is non-zero we are about to enter
1942 the scope of template-id indicated. */
1945 finish_template_type (name, args, entering_scope)
1952 decl = lookup_template_class (name, args,
1953 NULL_TREE, NULL_TREE,
1954 entering_scope, /*complain=*/1);
1955 if (decl != error_mark_node)
1956 decl = TYPE_STUB_DECL (decl);
1961 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
1962 namespace scope or a class scope. */
1968 tree scope = TREE_OPERAND (sr, 0);
1970 if (TREE_CODE (scope) == NAMESPACE_DECL)
1972 push_decl_namespace (scope);
1973 TREE_COMPLEXITY (sr) = -1;
1975 else if (scope != current_class_type)
1977 if (TREE_CODE (scope) == TYPENAME_TYPE)
1979 /* In a declarator for a template class member, the scope will
1980 get here as an implicit typename, a TYPENAME_TYPE with a type. */
1981 scope = TREE_TYPE (scope);
1982 TREE_OPERAND (sr, 0) = scope;
1984 push_nested_class (scope, 3);
1985 TREE_COMPLEXITY (sr) = current_class_depth;
1989 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
1990 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
1991 BASE_CLASS, or NULL_TREE if an error occurred. The
1992 ACCESS_SPECIFIER is one of
1993 access_{default,public,protected_private}[_virtual]_node.*/
1996 finish_base_specifier (access_specifier, base_class)
1997 tree access_specifier;
2002 if (base_class == error_mark_node)
2004 error ("invalid base-class specification");
2007 else if (! is_aggr_type (base_class, 1))
2011 if (cp_type_quals (base_class) != 0)
2013 error ("base class `%T' has cv qualifiers", base_class);
2014 base_class = TYPE_MAIN_VARIANT (base_class);
2016 result = build_tree_list (access_specifier, base_class);
2022 /* Called when multiple declarators are processed. If that is not
2023 premitted in this context, an error is issued. */
2026 check_multiple_declarators ()
2030 In a template-declaration, explicit specialization, or explicit
2031 instantiation the init-declarator-list in the declaration shall
2032 contain at most one declarator.
2034 We don't just use PROCESSING_TEMPLATE_DECL for the first
2035 condition since that would disallow the perfectly legal code,
2036 like `template <class T> struct S { int i, j; };'. */
2037 tree scope = current_scope ();
2039 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2040 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2043 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2044 || processing_explicit_instantiation
2045 || processing_specialization)
2046 error ("multiple declarators in template declaration");
2049 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2050 use as a type-specifier. */
2053 finish_typeof (expr)
2056 if (processing_template_decl)
2060 t = make_aggr_type (TYPEOF_TYPE);
2061 TYPE_FIELDS (t) = expr;
2066 if (TREE_CODE (expr) == OFFSET_REF)
2067 expr = resolve_offset_ref (expr);
2069 return TREE_TYPE (expr);
2072 /* Compute the value of the `sizeof' operator. */
2078 if (processing_template_decl)
2079 return build_min_nt (SIZEOF_EXPR, t);
2081 return TYPE_P (t) ? c_sizeof (t) : expr_sizeof (t);
2084 /* Implement the __alignof keyword: Return the minimum required
2085 alignment of T, measured in bytes. */
2091 if (processing_template_decl)
2092 return build_min_nt (ALIGNOF_EXPR, t);
2094 return TYPE_P (t) ? c_alignof (t) : c_alignof_expr (t);
2097 /* Generate RTL for the statement T, and its substatements, and any
2098 other statements at its nesting level. */
2104 switch (TREE_CODE (t))
2107 genrtl_try_block (t);
2111 genrtl_eh_spec_block (t);
2119 genrtl_named_return_value ();
2131 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2132 will equivalent CALL_EXPRs. */
2135 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2137 int *walk_subtrees ATTRIBUTE_UNUSED;
2138 void *data ATTRIBUTE_UNUSED;
2140 tree aggr_init_expr;
2146 int copy_from_buffer_p;
2148 aggr_init_expr = *tp;
2149 /* We don't need to walk into types; there's nothing in a type that
2150 needs simplification. (And, furthermore, there are places we
2151 actively don't want to go. For example, we don't want to wander
2152 into the default arguments for a FUNCTION_DECL that appears in a
2154 if (TYPE_P (aggr_init_expr))
2159 /* Only AGGR_INIT_EXPRs are interesting. */
2160 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2163 /* Form an appropriate CALL_EXPR. */
2164 fn = TREE_OPERAND (aggr_init_expr, 0);
2165 args = TREE_OPERAND (aggr_init_expr, 1);
2166 slot = TREE_OPERAND (aggr_init_expr, 2);
2167 type = TREE_TYPE (aggr_init_expr);
2168 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2170 /* Replace the first argument with the address of the third
2171 argument to the AGGR_INIT_EXPR. */
2172 cxx_mark_addressable (slot);
2173 args = tree_cons (NULL_TREE,
2175 build_pointer_type (TREE_TYPE (slot)),
2179 call_expr = build (CALL_EXPR,
2180 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2181 fn, args, NULL_TREE);
2182 TREE_SIDE_EFFECTS (call_expr) = 1;
2184 /* If we're using the non-reentrant PCC calling convention, then we
2185 need to copy the returned value out of the static buffer into the
2187 copy_from_buffer_p = 0;
2188 #ifdef PCC_STATIC_STRUCT_RETURN
2189 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2191 int old_ac = flag_access_control;
2193 flag_access_control = 0;
2194 call_expr = build_aggr_init (slot, call_expr,
2195 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2196 flag_access_control = old_ac;
2197 copy_from_buffer_p = 1;
2201 /* If this AGGR_INIT_EXPR indicates the value returned by a
2202 function, then we want to use the value of the initialized
2203 location as the result. */
2204 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2206 call_expr = build (COMPOUND_EXPR, type,
2208 TREE_SIDE_EFFECTS (call_expr) = 1;
2211 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2212 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2215 /* Keep iterating. */
2219 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2222 emit_associated_thunks (fn)
2225 /* When we use vcall offsets, we emit thunks with the virtual
2226 functions to which they thunk. The whole point of vcall offsets
2227 is so that you can know statically the entire set of thunks that
2228 will ever be needed for a given virtual function, thereby
2229 enabling you to output all the thunks with the function itself. */
2230 if (DECL_VIRTUAL_P (fn))
2235 for (binfo = TYPE_BINFO (DECL_CONTEXT (fn));
2237 binfo = TREE_CHAIN (binfo))
2238 for (v = BINFO_VIRTUALS (binfo); v; v = TREE_CHAIN (v))
2240 && (!integer_zerop (BV_DELTA (v))
2241 || BV_USE_VCALL_INDEX_P (v)))
2246 if (BV_USE_VCALL_INDEX_P (v))
2248 vcall_index = BV_VCALL_INDEX (v);
2249 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
2252 vcall_index = NULL_TREE;
2254 thunk = make_thunk (build1 (ADDR_EXPR,
2255 vfunc_ptr_type_node,
2259 use_thunk (thunk, /*emit_p=*/1);
2264 /* Generate RTL for FN. */
2271 const char *saved_input_filename;
2273 /* When the parser calls us after finishing the body of a template
2274 function, we don't really want to expand the body. When we're
2275 processing an in-class definition of an inline function,
2276 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2277 to look at the function itself. */
2278 if (processing_template_decl
2279 || (DECL_LANG_SPECIFIC (fn)
2280 && DECL_TEMPLATE_INFO (fn)
2281 && uses_template_parms (DECL_TI_ARGS (fn))))
2283 /* Normally, collection only occurs in rest_of_compilation. So,
2284 if we don't collect here, we never collect junk generated
2285 during the processing of templates until we hit a
2286 non-template function. */
2291 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2292 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2293 simplify_aggr_init_exprs_r,
2296 /* If this is a constructor or destructor body, we have to clone
2298 if (maybe_clone_body (fn))
2300 /* We don't want to process FN again, so pretend we've written
2301 it out, even though we haven't. */
2302 TREE_ASM_WRITTEN (fn) = 1;
2306 /* There's no reason to do any of the work here if we're only doing
2307 semantic analysis; this code just generates RTL. */
2308 if (flag_syntax_only)
2311 /* If possible, avoid generating RTL for this function. Instead,
2312 just record it as an inline function, and wait until end-of-file
2313 to decide whether to write it out or not. */
2314 if (/* We have to generate RTL if it's not an inline function. */
2315 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2316 /* Or if we have to emit code for inline functions anyhow. */
2317 && !flag_keep_inline_functions
2318 /* Or if we actually have a reference to the function. */
2319 && !DECL_NEEDED_P (fn))
2321 /* Set DECL_EXTERNAL so that assemble_external will be called as
2322 necessary. We'll clear it again in finish_file. */
2323 if (!DECL_EXTERNAL (fn))
2325 DECL_NOT_REALLY_EXTERN (fn) = 1;
2326 DECL_EXTERNAL (fn) = 1;
2328 /* Remember this function. In finish_file we'll decide if
2329 we actually need to write this function out. */
2331 /* Let the back-end know that this function exists. */
2332 (*debug_hooks->deferred_inline_function) (fn);
2336 /* Compute the appropriate object-file linkage for inline
2338 if (DECL_DECLARED_INLINE_P (fn))
2339 import_export_decl (fn);
2341 /* If FN is external, then there's no point in generating RTL for
2342 it. This situation can arise with an inline function under
2343 `-fexternal-templates'; we instantiate the function, even though
2344 we're not planning on emitting it, in case we get a chance to
2346 if (DECL_EXTERNAL (fn))
2349 timevar_push (TV_INTEGRATION);
2351 /* Optimize the body of the function before expanding it. */
2352 optimize_function (fn);
2354 timevar_pop (TV_INTEGRATION);
2355 timevar_push (TV_EXPAND);
2357 /* Save the current file name and line number. When we expand the
2358 body of the function, we'll set LINENO and INPUT_FILENAME so that
2359 error-mesages come out in the right places. */
2360 saved_lineno = lineno;
2361 saved_input_filename = input_filename;
2362 lineno = DECL_SOURCE_LINE (fn);
2363 input_filename = DECL_SOURCE_FILE (fn);
2365 genrtl_start_function (fn);
2366 current_function_is_thunk = DECL_THUNK_P (fn);
2368 /* Expand the body. */
2369 expand_stmt (DECL_SAVED_TREE (fn));
2371 /* Statements should always be full-expressions at the outermost set
2372 of curly braces for a function. */
2373 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2375 /* The outermost statement for a function contains the line number
2376 recorded when we finished processing the function. */
2377 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2379 /* Generate code for the function. */
2380 genrtl_finish_function (fn);
2382 /* If possible, obliterate the body of the function so that it can
2383 be garbage collected. */
2384 if (dump_enabled_p (TDI_all))
2385 /* Keep the body; we're going to dump it. */
2387 else if (DECL_INLINE (fn) && flag_inline_trees)
2388 /* We might need the body of this function so that we can expand
2389 it inline somewhere else. */
2392 /* We don't need the body; blow it away. */
2393 DECL_SAVED_TREE (fn) = NULL_TREE;
2395 /* And restore the current source position. */
2396 lineno = saved_lineno;
2397 input_filename = saved_input_filename;
2398 extract_interface_info ();
2400 timevar_pop (TV_EXPAND);
2402 /* Emit any thunks that should be emitted at the same time as FN. */
2403 emit_associated_thunks (fn);
2406 /* Helper function for walk_tree, used by finish_function to override all
2407 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2408 value optimization. */
2411 nullify_returns_r (tp, walk_subtrees, data)
2416 tree nrv = (tree) data;
2418 /* No need to walk into types. There wouldn't be any need to walk into
2419 non-statements, except that we have to consider STMT_EXPRs. */
2422 else if (TREE_CODE (*tp) == RETURN_STMT)
2423 RETURN_EXPR (*tp) = NULL_TREE;
2424 else if (TREE_CODE (*tp) == CLEANUP_STMT
2425 && CLEANUP_DECL (*tp) == nrv)
2426 CLEANUP_EH_ONLY (*tp) = 1;
2428 /* Keep iterating. */
2432 /* Start generating the RTL for FN. */
2435 genrtl_start_function (fn)
2438 /* Tell everybody what function we're processing. */
2439 current_function_decl = fn;
2440 /* Get the RTL machinery going for this function. */
2441 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2442 /* Let everybody know that we're expanding this function, not doing
2443 semantic analysis. */
2446 /* Even though we're inside a function body, we still don't want to
2447 call expand_expr to calculate the size of a variable-sized array.
2448 We haven't necessarily assigned RTL to all variables yet, so it's
2449 not safe to try to expand expressions involving them. */
2450 immediate_size_expand = 0;
2451 cfun->x_dont_save_pending_sizes_p = 1;
2453 /* Let the user know we're compiling this function. */
2454 announce_function (fn);
2456 /* Initialize the per-function data. */
2457 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2458 if (DECL_SAVED_FUNCTION_DATA (fn))
2460 /* If we already parsed this function, and we're just expanding it
2461 now, restore saved state. */
2462 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2464 /* This function is being processed in whole-function mode; we
2465 already did semantic analysis. */
2466 cfun->x_whole_function_mode_p = 1;
2468 /* If we decided that we didn't want to inline this function,
2469 make sure the back-end knows that. */
2470 if (!current_function_cannot_inline)
2471 current_function_cannot_inline = cp_function_chain->cannot_inline;
2473 /* We don't need the saved data anymore. Unless this is an inline
2474 function; we need the named return value info for
2475 cp_copy_res_decl_for_inlining. */
2476 if (! DECL_INLINE (fn))
2478 free (DECL_SAVED_FUNCTION_DATA (fn));
2479 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2483 /* Keep track of how many functions we're presently expanding. */
2486 /* Create a binding level for the parameters. */
2487 expand_function_start (fn, /*parms_have_cleanups=*/0);
2488 /* If this function is `main'. */
2489 if (DECL_MAIN_P (fn))
2490 expand_main_function ();
2492 /* Give our named return value the same RTL as our RESULT_DECL. */
2493 if (current_function_return_value)
2494 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2497 /* Finish generating the RTL for FN. */
2500 genrtl_finish_function (fn)
2506 if (write_symbols != NO_DEBUG)
2508 /* Keep this code around in case we later want to control debug info
2509 based on whether a type is "used". (jason 1999-11-11) */
2511 tree ttype = target_type (fntype);
2514 if (IS_AGGR_TYPE (ttype))
2515 /* Let debugger know it should output info for this type. */
2516 note_debug_info_needed (ttype);
2518 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2520 ttype = target_type (TREE_TYPE (parmdecl));
2521 if (IS_AGGR_TYPE (ttype))
2522 /* Let debugger know it should output info for this type. */
2523 note_debug_info_needed (ttype);
2528 /* Clean house because we will need to reorder insns here. */
2529 do_pending_stack_adjust ();
2531 /* If we have a named return value, we need to force a return so that
2532 the return register is USEd. */
2533 if (DECL_NAME (DECL_RESULT (fn)))
2534 emit_jump (return_label);
2536 /* We hard-wired immediate_size_expand to zero in start_function.
2537 Expand_function_end will decrement this variable. So, we set the
2538 variable to one here, so that after the decrement it will remain
2540 immediate_size_expand = 1;
2542 /* Generate rtl for function exit. */
2543 expand_function_end (input_filename, lineno, 0);
2545 /* If this is a nested function (like a template instantiation that
2546 we're compiling in the midst of compiling something else), push a
2547 new GC context. That will keep local variables on the stack from
2548 being collected while we're doing the compilation of this
2550 if (function_depth > 1)
2551 ggc_push_context ();
2553 /* There's no need to defer outputting this function any more; we
2554 know we want to output it. */
2555 DECL_DEFER_OUTPUT (fn) = 0;
2557 /* Run the optimizers and output the assembler code for this
2559 rest_of_compilation (fn);
2561 /* Undo the call to ggc_push_context above. */
2562 if (function_depth > 1)
2566 /* Keep this code around in case we later want to control debug info
2567 based on whether a type is "used". (jason 1999-11-11) */
2569 if (ctype && TREE_ASM_WRITTEN (fn))
2570 note_debug_info_needed (ctype);
2573 /* If this function is marked with the constructor attribute, add it
2574 to the list of functions to be called along with constructors
2575 from static duration objects. */
2576 if (DECL_STATIC_CONSTRUCTOR (fn))
2577 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2579 /* If this function is marked with the destructor attribute, add it
2580 to the list of functions to be called along with destructors from
2581 static duration objects. */
2582 if (DECL_STATIC_DESTRUCTOR (fn))
2583 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2587 /* In C++, we should never be saving RTL for the function. */
2588 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2590 /* Since we don't need the RTL for this function anymore, stop
2591 pointing to it. That's especially important for LABEL_DECLs,
2592 since you can reach all the instructions in the function from the
2593 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2594 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2596 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2600 /* Clear out the RTL for the arguments. */
2601 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2603 SET_DECL_RTL (t, NULL_RTX);
2604 DECL_INCOMING_RTL (t) = NULL_RTX;
2607 if (!(flag_inline_trees && DECL_INLINE (fn)))
2608 /* DECL_INITIAL must remain nonzero so we know this was an
2609 actual function definition. */
2610 DECL_INITIAL (fn) = error_mark_node;
2612 /* Let the error reporting routines know that we're outside a
2613 function. For a nested function, this value is used in
2614 pop_cp_function_context and then reset via pop_function_context. */
2615 current_function_decl = NULL_TREE;
2618 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2622 clear_decl_rtl (tp, walk_subtrees, data)
2624 int *walk_subtrees ATTRIBUTE_UNUSED;
2625 void *data ATTRIBUTE_UNUSED;
2627 if (nonstatic_local_decl_p (*tp))
2628 SET_DECL_RTL (*tp, NULL_RTX);
2633 /* Perform initialization related to this module. */
2636 init_cp_semantics ()
2638 lang_expand_stmt = cp_expand_stmt;