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 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
40 /* There routines provide a modular interface to perform many parsing
41 operations. They may therefore be used during actual parsing, or
42 during template instantiation, which may be regarded as a
43 degenerate form of parsing. Since the current g++ parser is
44 lacking in several respects, and will be reimplemented, we are
45 attempting to move most code that is not directly related to
46 parsing into this file; that will make implementing the new parser
47 much easier since it will be able to make use of these routines. */
49 static tree maybe_convert_cond
PARAMS ((tree
));
50 static tree simplify_aggr_init_exprs_r
PARAMS ((tree
*, int *, void *));
51 static void deferred_type_access_control
PARAMS ((void));
52 static void emit_associated_thunks
PARAMS ((tree
));
54 /* When parsing a template, LAST_TREE contains the last statement
55 parsed. These are chained together through the TREE_CHAIN field,
56 but often need to be re-organized since the parse is performed
57 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
60 #define RECHAIN_STMTS(stmt, substmt) \
62 substmt = TREE_CHAIN (stmt); \
63 TREE_CHAIN (stmt) = NULL_TREE; \
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 /* Wrapper since C and C++ expand_expr_stmt are different. */
86 expand_expr_stmt_fn lang_expand_expr_stmt
= cplus_expand_expr_stmt
;
88 /* Wrapper function instead of #define for use with c-common code. */
91 set_current_function_name_declared (i
)
94 cp_function_chain
->name_declared
= i
;
97 /* Returns non-zero if the current statement is a full expression,
98 i.e. temporaries created during that statement should be destroyed
99 at the end of the statement. */
102 stmts_are_full_exprs_p ()
104 return current_stmt_tree ()->stmts_are_full_exprs_p
;
107 /* Returns the stmt_tree (if any) to which statements are currently
108 being added. If there is no active statement-tree, NULL is
115 ? &cfun
->language
->x_stmt_tree
116 : &scope_chain
->x_stmt_tree
);
119 /* One if we have already declared __FUNCTION__ (and related
120 variables) in the current function. Two if we are in the process
124 current_function_name_declared ()
126 return cp_function_chain
->name_declared
;
129 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
130 flag for this because "A union for which objects or pointers are
131 declared is not an anonymous union" [class.union]. */
134 anon_aggr_type_p (node
)
137 return (CLASS_TYPE_P (node
) && TYPE_LANG_SPECIFIC(node
)->anon_aggr
);
140 /* Finish a scope. */
145 tree block
= NULL_TREE
;
147 if (stmts_are_full_exprs_p ())
149 tree scope_stmts
= NULL_TREE
;
151 if (!processing_template_decl
)
152 scope_stmts
= add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
154 block
= poplevel (kept_level_p (), 1, 0);
155 if (block
&& !processing_template_decl
)
157 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts
)) = block
;
158 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts
)) = block
;
165 /* Begin a new scope. */
170 if (stmts_are_full_exprs_p ())
173 if (!processing_template_decl
)
174 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
178 /* Finish a goto-statement. */
181 finish_goto_stmt (destination
)
184 if (TREE_CODE (destination
) == IDENTIFIER_NODE
)
185 destination
= lookup_label (destination
);
187 /* We warn about unused labels with -Wunused. That means we have to
188 mark the used labels as used. */
189 if (TREE_CODE (destination
) == LABEL_DECL
)
190 TREE_USED (destination
) = 1;
192 if (TREE_CODE (destination
) != LABEL_DECL
)
193 /* We don't inline calls to functions with computed gotos.
194 Those functions are typically up to some funny business,
195 and may be depending on the labels being at particular
196 addresses, or some such. */
197 DECL_UNINLINABLE (current_function_decl
) = 1;
199 check_goto (destination
);
201 add_stmt (build_stmt (GOTO_STMT
, destination
));
204 /* COND is the condition-expression for an if, while, etc.,
205 statement. Convert it to a boolean value, if appropriate. */
208 maybe_convert_cond (cond
)
211 /* Empty conditions remain empty. */
215 /* Wait until we instantiate templates before doing conversion. */
216 if (processing_template_decl
)
219 /* Do the conversion. */
220 cond
= convert_from_reference (cond
);
221 return condition_conversion (cond
);
224 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
227 finish_expr_stmt (expr
)
230 if (expr
!= NULL_TREE
)
232 if (!processing_template_decl
233 && !(stmts_are_full_exprs_p ())
234 && ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
236 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
))
237 expr
= default_conversion (expr
);
239 if (stmts_are_full_exprs_p ())
240 expr
= convert_to_void (expr
, "statement");
242 if (!processing_template_decl
)
243 expr
= break_out_cleanups (expr
);
245 add_stmt (build_stmt (EXPR_STMT
, expr
));
250 /* This was an expression-statement, so we save the type of the
252 last_expr_type
= expr
? TREE_TYPE (expr
) : NULL_TREE
;
256 /* Begin an if-statement. Returns a newly created IF_STMT if
264 r
= build_stmt (IF_STMT
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
269 /* Process the COND of an if-statement, which may be given by
273 finish_if_stmt_cond (cond
, if_stmt
)
277 cond
= maybe_convert_cond (cond
);
278 FINISH_COND (cond
, if_stmt
, IF_COND (if_stmt
));
281 /* Finish the then-clause of an if-statement, which may be given by
285 finish_then_clause (if_stmt
)
288 RECHAIN_STMTS (if_stmt
, THEN_CLAUSE (if_stmt
));
293 /* Begin the else-clause of an if-statement. */
300 /* Finish the else-clause of an if-statement, which may be given by
304 finish_else_clause (if_stmt
)
307 RECHAIN_STMTS (if_stmt
, ELSE_CLAUSE (if_stmt
));
310 /* Finsh an if-statement. */
322 /* If COND wasn't a declaration, clear out the
323 block we made for it and start a new one here so the
324 optimization in expand_end_loop will work. */
325 if (getdecls () == NULL_TREE
)
332 /* Begin a while-statement. Returns a newly created WHILE_STMT if
339 r
= build_stmt (WHILE_STMT
, NULL_TREE
, NULL_TREE
);
345 /* Process the COND of a while-statement, which may be given by
349 finish_while_stmt_cond (cond
, while_stmt
)
353 cond
= maybe_convert_cond (cond
);
354 FINISH_COND (cond
, while_stmt
, WHILE_COND (while_stmt
));
358 /* Finish a while-statement, which may be given by WHILE_STMT. */
361 finish_while_stmt (while_stmt
)
365 RECHAIN_STMTS (while_stmt
, WHILE_BODY (while_stmt
));
369 /* Begin a do-statement. Returns a newly created DO_STMT if
375 tree r
= build_stmt (DO_STMT
, NULL_TREE
, NULL_TREE
);
380 /* Finish the body of a do-statement, which may be given by DO_STMT. */
383 finish_do_body (do_stmt
)
386 RECHAIN_STMTS (do_stmt
, DO_BODY (do_stmt
));
389 /* Finish a do-statement, which may be given by DO_STMT, and whose
390 COND is as indicated. */
393 finish_do_stmt (cond
, do_stmt
)
397 cond
= maybe_convert_cond (cond
);
398 DO_COND (do_stmt
) = cond
;
402 /* Finish a return-statement. The EXPRESSION returned, if any, is as
406 finish_return_stmt (expr
)
409 if (!processing_template_decl
)
410 expr
= check_return_expr (expr
);
411 if (!processing_template_decl
)
413 if (DECL_CONSTRUCTOR_P (current_function_decl
) && ctor_label
)
415 /* Even returns without a value in a constructor must return
416 `this'. We accomplish this by sending all returns in a
417 constructor to the CTOR_LABEL; finish_function emits code to
418 return a value there. When we finally generate the real
419 return statement, CTOR_LABEL is no longer set, and we fall
420 through into the normal return-processing code below. */
421 finish_goto_stmt (ctor_label
);
424 else if (DECL_DESTRUCTOR_P (current_function_decl
))
426 /* Similarly, all destructors must run destructors for
427 base-classes before returning. So, all returns in a
428 destructor get sent to the DTOR_LABEL; finsh_function emits
429 code to return a value there. */
430 finish_goto_stmt (dtor_label
);
434 add_stmt (build_stmt (RETURN_STMT
, expr
));
438 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
445 r
= build_stmt (FOR_STMT
, NULL_TREE
, NULL_TREE
,
446 NULL_TREE
, NULL_TREE
);
447 NEW_FOR_SCOPE_P (r
) = flag_new_for_scope
> 0;
449 if (NEW_FOR_SCOPE_P (r
))
452 note_level_for_for ();
458 /* Finish the for-init-statement of a for-statement, which may be
459 given by FOR_STMT. */
462 finish_for_init_stmt (for_stmt
)
465 if (last_tree
!= for_stmt
)
466 RECHAIN_STMTS (for_stmt
, FOR_INIT_STMT (for_stmt
));
470 /* Finish the COND of a for-statement, which may be given by
474 finish_for_cond (cond
, for_stmt
)
478 cond
= maybe_convert_cond (cond
);
479 FINISH_COND (cond
, for_stmt
, FOR_COND (for_stmt
));
483 /* Finish the increment-EXPRESSION in a for-statement, which may be
484 given by FOR_STMT. */
487 finish_for_expr (expr
, for_stmt
)
491 FOR_EXPR (for_stmt
) = expr
;
494 /* Finish the body of a for-statement, which may be given by
495 FOR_STMT. The increment-EXPR for the loop must be
499 finish_for_stmt (for_stmt
)
502 /* Pop the scope for the body of the loop. */
504 RECHAIN_STMTS (for_stmt
, FOR_BODY (for_stmt
));
505 if (NEW_FOR_SCOPE_P (for_stmt
))
510 /* Finish a break-statement. */
515 add_stmt (build_stmt (BREAK_STMT
));
518 /* Finish a continue-statement. */
521 finish_continue_stmt ()
523 add_stmt (build_stmt (CONTINUE_STMT
));
526 /* Begin a switch-statement. Returns a new SWITCH_STMT if
533 r
= build_stmt (SWITCH_STMT
, NULL_TREE
, NULL_TREE
);
539 /* Finish the cond of a switch-statement. */
542 finish_switch_cond (cond
, switch_stmt
)
546 if (!processing_template_decl
)
548 /* Convert the condition to an integer or enumeration type. */
549 cond
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, cond
, 1);
550 if (cond
== NULL_TREE
)
552 error ("switch quantity not an integer");
553 cond
= error_mark_node
;
555 if (cond
!= error_mark_node
)
557 cond
= default_conversion (cond
);
558 cond
= fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (cond
), cond
));
561 FINISH_COND (cond
, switch_stmt
, SWITCH_COND (switch_stmt
));
565 /* Finish the body of a switch-statement, which may be given by
566 SWITCH_STMT. The COND to switch on is indicated. */
569 finish_switch_stmt (switch_stmt
)
572 RECHAIN_STMTS (switch_stmt
, SWITCH_BODY (switch_stmt
));
578 /* Finish a case-label. */
581 finish_case_label (low_value
, high_value
)
585 /* Add a representation for the case label to the statement
587 add_stmt (build_stmt (CASE_LABEL
, low_value
, high_value
));
588 /* And warn about crossing initializations, etc. */
589 if (!processing_template_decl
)
590 define_case_label ();
593 /* Generate the RTL for T, which is a TRY_BLOCK. */
595 void genrtl_try_block (t
)
600 expand_eh_region_start ();
601 expand_stmt (TRY_STMTS (t
));
602 expand_eh_region_end (protect_with_terminate (TRY_HANDLERS (t
)));
606 if (FN_TRY_BLOCK_P (t
)) {
607 if (! current_function_parms_stored
)
609 expand_start_early_try_stmts ();
612 emit_line_note (input_filename
, lineno
);
613 expand_start_try_stmts ();
616 expand_stmt (TRY_STMTS (t
));
618 if (FN_TRY_BLOCK_P (t
))
620 end_protect_partials ();
621 expand_start_all_catch ();
622 in_function_try_handler
= 1;
623 expand_stmt (TRY_HANDLERS (t
));
624 in_function_try_handler
= 0;
625 expand_end_all_catch ();
629 expand_start_all_catch ();
630 expand_stmt (TRY_HANDLERS (t
));
631 expand_end_all_catch ();
636 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
642 tree r
= build_stmt (TRY_BLOCK
, NULL_TREE
, NULL_TREE
);
647 /* Likewise, for a function-try-block. */
650 begin_function_try_block ()
652 tree r
= build_stmt (TRY_BLOCK
, NULL_TREE
, NULL_TREE
);
653 FN_TRY_BLOCK_P (r
) = 1;
658 /* Finish a try-block, which may be given by TRY_BLOCK. */
661 finish_try_block (try_block
)
664 RECHAIN_STMTS (try_block
, TRY_STMTS (try_block
));
667 /* Finish the body of a cleanup try-block, which may be given by
671 finish_cleanup_try_block (try_block
)
674 RECHAIN_STMTS (try_block
, TRY_STMTS (try_block
));
677 /* Finish an implicitly generated try-block, with a cleanup is given
681 finish_cleanup (cleanup
, try_block
)
685 TRY_HANDLERS (try_block
) = cleanup
;
686 CLEANUP_P (try_block
) = 1;
689 /* Likewise, for a function-try-block. */
692 finish_function_try_block (try_block
)
695 if (TREE_CHAIN (try_block
)
696 && TREE_CODE (TREE_CHAIN (try_block
)) == CTOR_INITIALIZER
)
698 /* Chain the compound statement after the CTOR_INITIALIZER. */
699 TREE_CHAIN (TREE_CHAIN (try_block
)) = last_tree
;
700 /* And make the CTOR_INITIALIZER the body of the try-block. */
701 RECHAIN_STMTS (try_block
, TRY_STMTS (try_block
));
704 RECHAIN_STMTS (try_block
, TRY_STMTS (try_block
));
705 in_function_try_handler
= 1;
708 /* Finish a handler-sequence for a try-block, which may be given by
712 finish_handler_sequence (try_block
)
715 RECHAIN_STMTS (try_block
, TRY_HANDLERS (try_block
));
716 check_handlers (TRY_HANDLERS (try_block
));
719 /* Likewise, for a function-try-block. */
722 finish_function_handler_sequence (try_block
)
725 in_function_try_handler
= 0;
726 RECHAIN_STMTS (try_block
, TRY_HANDLERS (try_block
));
727 check_handlers (TRY_HANDLERS (try_block
));
730 /* Generate the RTL for T, which is a HANDLER. */
736 genrtl_do_pushlevel ();
737 expand_stmt (HANDLER_BODY (t
));
738 if (!processing_template_decl
)
740 /* Fall to outside the try statement when done executing
741 handler and we fall off end of handler. This is jump
742 Lresume in the documentation. */
743 expand_goto (top_label_entry (&caught_return_label_stack
));
744 end_catch_handler ();
748 /* Begin a handler. Returns a HANDLER if appropriate. */
754 r
= build_stmt (HANDLER
, NULL_TREE
, NULL_TREE
);
760 /* Finish the handler-parameters for a handler, which may be given by
761 HANDLER. DECL is the declaration for the catch parameter, or NULL
762 if this is a `catch (...)' clause. */
765 finish_handler_parms (decl
, handler
)
769 tree blocks
= NULL_TREE
;
771 if (processing_template_decl
)
775 decl
= pushdecl (decl
);
776 decl
= push_template_decl (decl
);
777 add_decl_stmt (decl
);
778 RECHAIN_STMTS (handler
, HANDLER_PARMS (handler
));
782 blocks
= expand_start_catch_block (decl
);
785 TREE_TYPE (handler
) = TREE_TYPE (decl
);
790 /* Generate the RTL for a CATCH_BLOCK. */
793 genrtl_catch_block (type
)
796 start_catch_handler (type
);
799 /* Note the beginning of a handler for TYPE. This function is called
800 at the point to which control should be transferred when an
801 appropriately-typed exception is thrown. */
804 begin_catch_block (type
)
807 add_stmt (build (START_CATCH_STMT
, type
));
810 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
811 the return value from the matching call to finish_handler_parms. */
814 finish_handler (blocks
, handler
)
818 if (!processing_template_decl
)
819 expand_end_catch_block (blocks
);
821 RECHAIN_STMTS (handler
, HANDLER_BODY (handler
));
824 /* Generate the RTL for T, which is a CTOR_STMT. */
830 if (CTOR_BEGIN_P (t
))
831 begin_protect_partials ();
833 /* After this point, any exceptions will cause the
834 destructor to be executed, so we no longer need to worry
835 about destroying the various subobjects ourselves. */
836 end_protect_partials ();
839 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
840 compound-statement does not define a scope. Returns a new
841 COMPOUND_STMT if appropriate. */
844 begin_compound_stmt (has_no_scope
)
850 r
= build_stmt (COMPOUND_STMT
, NULL_TREE
);
852 if (last_tree
&& TREE_CODE (last_tree
) == TRY_BLOCK
)
857 COMPOUND_STMT_NO_SCOPE (r
) = 1;
859 last_expr_type
= NULL_TREE
;
865 note_level_for_eh ();
868 /* Normally, we try hard to keep the BLOCK for a
869 statement-expression. But, if it's a statement-expression with
870 a scopeless block, there's nothing to keep, and we don't want
871 to accidentally keep a block *inside* the scopeless block. */
874 /* If this is the outermost block of the function, declare the
875 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
877 && !(current_function_name_declared () )
880 cp_function_chain
->name_declared
= 1;
881 declare_function_name ();
887 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
888 If HAS_NO_SCOPE is non-zero, the compound statement does not define
892 finish_compound_stmt (has_no_scope
, compound_stmt
)
904 RECHAIN_STMTS (compound_stmt
, COMPOUND_BODY (compound_stmt
));
906 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
907 the precise purpose of that variable is store the type of the
908 last expression statement within the last compound statement, we
909 preserve the value. */
917 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
918 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
922 finish_asm_stmt (cv_qualifier
, string
, output_operands
,
923 input_operands
, clobbers
)
926 tree output_operands
;
933 if (TREE_CHAIN (string
))
934 string
= combine_strings (string
);
936 if (cv_qualifier
!= NULL_TREE
937 && cv_qualifier
!= ridpointers
[(int) RID_VOLATILE
])
939 cp_warning ("%s qualifier ignored on asm",
940 IDENTIFIER_POINTER (cv_qualifier
));
941 cv_qualifier
= NULL_TREE
;
944 if (!processing_template_decl
)
945 for (t
= input_operands
; t
; t
= TREE_CHAIN (t
))
946 TREE_VALUE (t
) = decay_conversion (TREE_VALUE (t
));
948 r
= build_stmt (ASM_STMT
, cv_qualifier
, string
,
949 output_operands
, input_operands
,
954 /* Finish a label with the indicated NAME. */
957 finish_label_stmt (name
)
960 tree decl
= define_label (input_filename
, lineno
, name
);
961 add_stmt (build_stmt (LABEL_STMT
, decl
));
964 /* Finish a series of declarations for local labels. G++ allows users
965 to declare "local" labels, i.e., labels with scope. This extension
966 is useful when writing code involving statement-expressions. */
969 finish_label_decl (name
)
972 tree decl
= declare_local_label (name
);
973 add_decl_stmt (decl
);
976 /* Create a declaration statement for the declaration given by the
985 /* We need the type to last until instantiation time. */
986 decl_stmt
= build_stmt (DECL_STMT
, decl
);
987 add_stmt (decl_stmt
);
990 /* Generate the RTL for a SUBOBJECT. */
993 genrtl_subobject (cleanup
)
996 add_partial_entry (cleanup
);
999 /* We're in a constructor, and have just constructed a a subobject of
1000 *THIS. CLEANUP is code to run if an exception is thrown before the
1001 end of the current function is reached. */
1004 finish_subobject (cleanup
)
1007 tree r
= build_stmt (SUBOBJECT
, cleanup
);
1011 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1014 finish_decl_cleanup (decl
, cleanup
)
1018 add_stmt (build_stmt (CLEANUP_STMT
, decl
, cleanup
));
1021 /* Generate the RTL for a RETURN_INIT. */
1024 genrtl_named_return_value ()
1028 decl
= DECL_RESULT (current_function_decl
);
1030 emit_local_var (decl
);
1032 /* If this named return value comes in a register, put it in a
1034 if (DECL_REGISTER (decl
))
1036 original_result_rtx
= DECL_RTL (decl
);
1037 /* Note that the mode of the old DECL_RTL may be wider than the
1038 mode of DECL_RESULT, depending on the calling conventions for
1039 the processor. For example, on the Alpha, a 32-bit integer
1040 is returned in a DImode register -- the DECL_RESULT has
1041 SImode but the DECL_RTL for the DECL_RESULT has DImode. So,
1042 here, we use the mode the back-end has already assigned for
1043 the return value. */
1044 DECL_RTL (decl
) = gen_reg_rtx (GET_MODE (original_result_rtx
));
1045 if (TREE_ADDRESSABLE (decl
))
1046 put_var_into_stack (decl
);
1050 /* Bind a name and initialization to the return value of
1051 the current function. */
1054 finish_named_return_value (return_id
, init
)
1055 tree return_id
, init
;
1057 tree decl
= DECL_RESULT (current_function_decl
);
1059 /* Give this error as many times as there are occurrences, so that
1060 users can use Emacs compilation buffers to find and fix all such
1063 pedwarn ("ISO C++ does not permit named return values");
1064 cp_deprecated ("the named return value extension");
1066 if (return_id
!= NULL_TREE
)
1068 if (DECL_NAME (decl
) == NULL_TREE
)
1070 DECL_NAME (decl
) = return_id
;
1071 DECL_ASSEMBLER_NAME (decl
) = return_id
;
1075 cp_error ("return identifier `%D' already in place", return_id
);
1080 /* Can't let this happen for constructors. */
1081 if (DECL_CONSTRUCTOR_P (current_function_decl
))
1083 error ("can't redefine default return value for constructors");
1087 /* If we have a named return value, put that in our scope as well. */
1088 if (DECL_NAME (decl
) != NULL_TREE
)
1090 /* Let `cp_finish_decl' know that this initializer is ok. */
1091 DECL_INITIAL (decl
) = init
;
1092 if (doing_semantic_analysis_p ())
1094 if (!processing_template_decl
)
1096 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
1097 add_stmt (build_stmt (RETURN_INIT
, NULL_TREE
, NULL_TREE
));
1100 add_stmt (build_stmt (RETURN_INIT
, return_id
, init
));
1103 /* Don't use tree-inlining for functions with named return values.
1104 That doesn't work properly because we don't do any translation of
1105 the RETURN_INITs when they are copied. */
1106 DECL_UNINLINABLE (current_function_decl
) = 1;
1109 /* The INIT_LIST is a list of mem-initializers, in the order they were
1110 written by the user. The TREE_VALUE of each node is a list of
1111 initializers for a particular subobject. The TREE_PURPOSE is a
1112 FIELD_DECL is the initializer is for a non-static data member, and
1113 a class type if the initializer is for a base class. */
1116 finish_mem_initializers (init_list
)
1119 tree member_init_list
;
1120 tree base_init_list
;
1121 tree last_base_warned_about
;
1125 member_init_list
= NULL_TREE
;
1126 base_init_list
= NULL_TREE
;
1127 last_base_warned_about
= NULL_TREE
;
1129 for (init
= init_list
; init
; init
= next
)
1131 next
= TREE_CHAIN (init
);
1132 if (TREE_CODE (TREE_PURPOSE (init
)) == FIELD_DECL
)
1134 TREE_CHAIN (init
) = member_init_list
;
1135 member_init_list
= init
;
1137 /* We're running through the initializers from right to left
1138 as we process them here. So, if we see a data member
1139 initializer after we see a base initializer, that
1140 actually means that the base initializer preceeded the
1141 data member initializer. */
1142 if (warn_reorder
&& last_base_warned_about
!= base_init_list
)
1146 for (base
= base_init_list
;
1147 base
!= last_base_warned_about
;
1148 base
= TREE_CHAIN (base
))
1150 cp_warning ("base initializer for `%T'",
1151 TREE_PURPOSE (base
));
1152 warning (" will be re-ordered to precede member initializations");
1155 last_base_warned_about
= base_init_list
;
1160 TREE_CHAIN (init
) = base_init_list
;
1161 base_init_list
= init
;
1165 setup_vtbl_ptr (member_init_list
, base_init_list
);
1168 /* Cache the value of this class's main virtual function table pointer
1169 in a register variable. This will save one indirection if a
1170 more than one virtual function call is made this function. */
1173 setup_vtbl_ptr (member_init_list
, base_init_list
)
1174 tree member_init_list
;
1175 tree base_init_list
;
1177 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1179 /* If we've already done this, there's no need to do it again. */
1183 if (DECL_CONSTRUCTOR_P (current_function_decl
))
1185 if (processing_template_decl
)
1186 add_stmt (build_min_nt
1188 member_init_list
, base_init_list
));
1193 /* Mark the beginning of the constructor. */
1194 ctor_stmt
= build_stmt (CTOR_STMT
);
1195 CTOR_BEGIN_P (ctor_stmt
) = 1;
1196 add_stmt (ctor_stmt
);
1198 /* And actually initialize the base-classes and members. */
1199 emit_base_init (member_init_list
, base_init_list
);
1202 else if (DECL_DESTRUCTOR_P (current_function_decl
)
1203 && !processing_template_decl
)
1209 /* If the dtor is empty, and we know there is not any possible
1210 way we could use any vtable entries, before they are possibly
1211 set by a base class dtor, we don't have to setup the vtables,
1212 as we know that any base class dtor will set up any vtables
1213 it needs. We avoid MI, because one base class dtor can do a
1214 virtual dispatch to an overridden function that would need to
1215 have a non-related vtable set up, we cannot avoid setting up
1216 vtables in that case. We could change this to see if there
1217 is just one vtable. */
1218 if_stmt
= begin_if_stmt ();
1220 /* If it is not safe to avoid setting up the vtables, then
1221 someone will change the condition to be boolean_true_node.
1222 (Actually, for now, we do not have code to set the condition
1223 appropriately, so we just assume that we always need to
1224 initialize the vtables.) */
1225 finish_if_stmt_cond (boolean_true_node
, if_stmt
);
1226 current_vcalls_possible_p
= &IF_COND (if_stmt
);
1228 /* Don't declare __PRETTY_FUNCTION__ and friends here when we
1229 open the block for the if-body. */
1230 saved_cfnd
= current_function_name_declared ();
1231 cp_function_chain
->name_declared
= 1;
1232 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
1233 cp_function_chain
->name_declared
= saved_cfnd
;
1235 /* Make all virtual function table pointers in non-virtual base
1236 classes point to CURRENT_CLASS_TYPE's virtual function
1238 initialize_vtbl_ptrs (current_class_ptr
);
1240 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
1241 finish_then_clause (if_stmt
);
1245 /* Always keep the BLOCK node associated with the outermost pair of
1246 curly braces of a function. These are needed for correct
1247 operation of dwarfout.c. */
1248 keep_next_level (1);
1250 /* The virtual function tables are set up now. */
1255 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
1256 whether this statements opens or closes a scope. PARTIAL_P is true
1257 for a partial scope, i.e, the scope that begins after a label when
1258 an object that needs a cleanup is created. If BEGIN_P is nonzero,
1259 returns a new TREE_LIST representing the top of the SCOPE_STMT
1260 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
1261 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
1262 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
1263 SCOPE_BEGIN_P set. */
1266 add_scope_stmt (begin_p
, partial_p
)
1273 /* Build the statement. */
1274 ss
= build_stmt (SCOPE_STMT
, NULL_TREE
);
1275 SCOPE_BEGIN_P (ss
) = begin_p
;
1276 SCOPE_PARTIAL_P (ss
) = partial_p
;
1278 /* Keep the scope stack up to date. */
1281 current_scope_stmt_stack
1282 = tree_cons (ss
, NULL_TREE
, current_scope_stmt_stack
);
1283 top
= current_scope_stmt_stack
;
1287 top
= current_scope_stmt_stack
;
1288 TREE_VALUE (top
) = ss
;
1289 current_scope_stmt_stack
= TREE_CHAIN (top
);
1292 /* Add the new statement to the statement-tree. */
1298 /* Finish a parenthesized expression EXPR. */
1301 finish_parenthesized_expr (expr
)
1304 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr
))))
1305 /* This inhibits warnings in truthvalue_conversion. */
1306 C_SET_EXP_ORIGINAL_CODE (expr
, ERROR_MARK
);
1308 if (TREE_CODE (expr
) == OFFSET_REF
)
1309 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1310 enclosed in parentheses. */
1311 PTRMEM_OK_P (expr
) = 0;
1315 /* Begin a statement-expression. The value returned must be passed to
1316 finish_stmt_expr. */
1321 /* If we're outside a function, we won't have a statement-tree to
1322 work with. But, if we see a statement-expression we need to
1324 if (! cfun
&& !last_tree
)
1325 begin_stmt_tree (&scope_chain
->x_saved_tree
);
1327 keep_next_level (1);
1328 /* If we're building a statement tree, then the upcoming compound
1329 statement will be chained onto the tree structure, starting at
1330 last_tree. We return last_tree so that we can later unhook the
1331 compound statement. */
1335 /* Used when beginning a statement-expression outside function scope.
1336 For example, when handling a file-scope initializer, we use this
1340 begin_global_stmt_expr ()
1342 if (! cfun
&& !last_tree
)
1343 begin_stmt_tree (&scope_chain
->x_saved_tree
);
1345 keep_next_level (1);
1347 return (last_tree
!= NULL_TREE
) ? last_tree
: expand_start_stmt_expr();
1350 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1353 finish_global_stmt_expr (stmt_expr
)
1356 stmt_expr
= expand_end_stmt_expr (stmt_expr
);
1359 && TREE_CHAIN (scope_chain
->x_saved_tree
) == NULL_TREE
)
1360 finish_stmt_tree (&scope_chain
->x_saved_tree
);
1365 /* Finish a statement-expression. RTL_EXPR should be the value
1366 returned by the previous begin_stmt_expr; EXPR is the
1367 statement-expression. Returns an expression representing the
1368 statement-expression. */
1371 finish_stmt_expr (rtl_expr
)
1376 /* If the last thing in the statement-expression was not an
1377 expression-statement, then it has type `void'. */
1378 if (!last_expr_type
)
1379 last_expr_type
= void_type_node
;
1380 result
= build_min (STMT_EXPR
, last_expr_type
, last_tree
);
1381 TREE_SIDE_EFFECTS (result
) = 1;
1383 /* Remove the compound statement from the tree structure; it is
1384 now saved in the STMT_EXPR. */
1385 last_tree
= rtl_expr
;
1386 TREE_CHAIN (last_tree
) = NULL_TREE
;
1388 /* If we created a statement-tree for this statement-expression,
1391 && TREE_CHAIN (scope_chain
->x_saved_tree
) == NULL_TREE
)
1392 finish_stmt_tree (&scope_chain
->x_saved_tree
);
1397 /* Finish a call to FN with ARGS. Returns a representation of the
1401 finish_call_expr (fn
, args
, koenig
)
1410 if (TREE_CODE (fn
) == BIT_NOT_EXPR
)
1411 fn
= build_x_unary_op (BIT_NOT_EXPR
, TREE_OPERAND (fn
, 0));
1412 else if (TREE_CODE (fn
) != TEMPLATE_ID_EXPR
)
1413 fn
= do_identifier (fn
, 2, args
);
1415 result
= build_x_function_call (fn
, args
, current_class_ref
);
1417 if (TREE_CODE (result
) == CALL_EXPR
1418 && (! TREE_TYPE (result
)
1419 || TREE_CODE (TREE_TYPE (result
)) != VOID_TYPE
))
1420 result
= require_complete_type (result
);
1425 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1426 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1427 POSTDECREMENT_EXPR.) */
1430 finish_increment_expr (expr
, code
)
1432 enum tree_code code
;
1434 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1435 a COMPONENT_REF). This way if we've got, say, a reference to a
1436 static member that's being operated on, we don't end up trying to
1437 find a member operator for the class it's in. */
1439 if (TREE_CODE (expr
) == OFFSET_REF
)
1440 expr
= resolve_offset_ref (expr
);
1441 return build_x_unary_op (code
, expr
);
1444 /* Finish a use of `this'. Returns an expression for `this'. */
1451 if (current_class_ptr
)
1453 #ifdef WARNING_ABOUT_CCD
1454 TREE_USED (current_class_ptr
) = 1;
1456 result
= current_class_ptr
;
1458 else if (current_function_decl
1459 && DECL_STATIC_FUNCTION_P (current_function_decl
))
1461 error ("`this' is unavailable for static member functions");
1462 result
= error_mark_node
;
1466 if (current_function_decl
)
1467 error ("invalid use of `this' in non-member function");
1469 error ("invalid use of `this' at top level");
1470 result
= error_mark_node
;
1476 /* Finish a member function call using OBJECT and ARGS as arguments to
1477 FN. Returns an expression for the call. */
1480 finish_object_call_expr (fn
, object
, args
)
1486 /* This is a future direction of this code, but because
1487 build_x_function_call cannot always undo what is done in
1488 build_component_ref entirely yet, we cannot do this. */
1490 tree real_fn
= build_component_ref (object
, fn
, NULL_TREE
, 1);
1491 return finish_call_expr (real_fn
, args
);
1493 if (DECL_DECLARES_TYPE_P (fn
))
1495 if (processing_template_decl
)
1496 /* This can happen on code like:
1499 template <class T> void f(T t) {
1503 We just grab the underlying IDENTIFIER. */
1504 fn
= DECL_NAME (fn
);
1507 cp_error ("calling type `%T' like a method", fn
);
1508 return error_mark_node
;
1512 return build_method_call (object
, fn
, args
, NULL_TREE
, LOOKUP_NORMAL
);
1516 /* Finish a qualified member function call using OBJECT and ARGS as
1517 arguments to FN. Returns an expressino for the call. */
1520 finish_qualified_object_call_expr (fn
, object
, args
)
1525 return build_scoped_method_call (object
, TREE_OPERAND (fn
, 0),
1526 TREE_OPERAND (fn
, 1), args
);
1529 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1530 being the scope, if any, of DESTRUCTOR. Returns an expression for
1534 finish_pseudo_destructor_call_expr (object
, scope
, destructor
)
1539 if (processing_template_decl
)
1540 return build_min_nt (PSEUDO_DTOR_EXPR
, object
, scope
, destructor
);
1542 if (scope
&& scope
!= destructor
)
1543 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1546 if ((scope
== NULL_TREE
|| IDENTIFIER_GLOBAL_VALUE (destructor
))
1547 && (TREE_CODE (TREE_TYPE (object
)) !=
1548 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor
)))))
1549 cp_error ("`%E' is not of type `%T'", object
, destructor
);
1551 return cp_convert (void_type_node
, object
);
1554 /* Finish a call to a globally qualified member function FN using
1555 ARGS. Returns an expression for the call. */
1558 finish_qualified_call_expr (fn
, args
)
1562 if (processing_template_decl
)
1563 return build_min_nt (CALL_EXPR
, fn
, args
, NULL_TREE
);
1565 return build_member_call (TREE_OPERAND (fn
, 0),
1566 TREE_OPERAND (fn
, 1),
1570 /* Finish an expression taking the address of LABEL. Returns an
1571 expression for the address. */
1574 finish_label_address_expr (label
)
1579 label
= lookup_label (label
);
1580 if (label
== NULL_TREE
)
1581 result
= null_pointer_node
;
1584 TREE_USED (label
) = 1;
1585 result
= build1 (ADDR_EXPR
, ptr_type_node
, label
);
1586 TREE_CONSTANT (result
) = 1;
1587 /* This function cannot be inlined. All jumps to the addressed
1588 label should wind up at the same point. */
1589 DECL_UNINLINABLE (current_function_decl
) = 1;
1595 /* Finish an expression of the form CODE EXPR. */
1598 finish_unary_op_expr (code
, expr
)
1599 enum tree_code code
;
1602 tree result
= build_x_unary_op (code
, expr
);
1603 /* Inside a template, build_x_unary_op does not fold the
1604 expression. So check whether the result is folded before
1605 setting TREE_NEGATED_INT. */
1606 if (code
== NEGATE_EXPR
&& TREE_CODE (expr
) == INTEGER_CST
1607 && TREE_CODE (result
) == INTEGER_CST
1608 && !TREE_UNSIGNED (TREE_TYPE (result
))
1609 && INT_CST_LT (result
, integer_zero_node
))
1610 TREE_NEGATED_INT (result
) = 1;
1611 overflow_warning (result
);
1615 /* Finish an id-expression. */
1618 finish_id_expr (expr
)
1621 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
1622 expr
= do_identifier (expr
, 1, NULL_TREE
);
1627 static tree current_type_lookups
;
1629 /* Perform deferred access control for types used in the type of a
1633 deferred_type_access_control ()
1635 tree lookup
= type_lookups
;
1637 if (lookup
== error_mark_node
)
1640 for (; lookup
; lookup
= TREE_CHAIN (lookup
))
1641 enforce_access (TREE_PURPOSE (lookup
), TREE_VALUE (lookup
));
1645 decl_type_access_control (decl
)
1650 if (type_lookups
== error_mark_node
)
1653 save_fn
= current_function_decl
;
1655 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
1656 current_function_decl
= decl
;
1658 deferred_type_access_control ();
1660 current_function_decl
= save_fn
;
1662 /* Now strip away the checks for the current declarator; they were
1663 added to type_lookups after typed_declspecs saved the copy that
1664 ended up in current_type_lookups. */
1665 type_lookups
= current_type_lookups
;
1669 save_type_access_control (lookups
)
1672 current_type_lookups
= lookups
;
1675 /* Begin a function definition declared with DECL_SPECS and
1676 DECLARATOR. Returns non-zero if the function-declaration is
1680 begin_function_definition (decl_specs
, declarator
)
1687 split_specs_attrs (decl_specs
, &specs
, &attrs
);
1688 if (!start_function (specs
, declarator
, attrs
, SF_DEFAULT
))
1691 deferred_type_access_control ();
1692 type_lookups
= error_mark_node
;
1694 /* The things we're about to see are not directly qualified by any
1695 template headers we've seen thus far. */
1696 reset_specialization ();
1701 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1705 begin_constructor_declarator (scope
, name
)
1709 tree result
= build_parse_node (SCOPE_REF
, scope
, name
);
1710 enter_scope_of (result
);
1714 /* Finish an init-declarator. Returns a DECL. */
1717 finish_declarator (declarator
, declspecs
, attributes
,
1718 prefix_attributes
, initialized
)
1722 tree prefix_attributes
;
1725 return start_decl (declarator
, declspecs
, initialized
, attributes
,
1729 /* Finish a translation unit. */
1732 finish_translation_unit ()
1734 /* In case there were missing closebraces,
1735 get us back to the global binding level. */
1737 while (current_namespace
!= global_namespace
)
1742 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1743 Returns the parameter. */
1746 finish_template_type_parm (aggr
, identifier
)
1750 if (aggr
!= class_type_node
)
1752 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1753 aggr
= class_type_node
;
1756 return build_tree_list (aggr
, identifier
);
1759 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1760 Returns the parameter. */
1763 finish_template_template_parm (aggr
, identifier
)
1767 tree decl
= build_decl (TYPE_DECL
, identifier
, NULL_TREE
);
1768 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, identifier
, NULL_TREE
);
1769 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
1770 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1771 DECL_ARTIFICIAL (decl
) = 1;
1772 end_template_decl ();
1774 return finish_template_type_parm (aggr
, tmpl
);
1777 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1778 non-zero, the parameter list was terminated by a `...'. */
1781 finish_parmlist (parms
, ellipsis
)
1786 chainon (parms
, void_list_node
);
1787 /* We mark the PARMS as a parmlist so that declarator processing can
1788 disambiguate certain constructs. */
1789 if (parms
!= NULL_TREE
)
1790 TREE_PARMLIST (parms
) = 1;
1795 /* Begin a class definition, as indicated by T. */
1798 begin_class_definition (t
)
1801 if (t
== error_mark_node
1802 || ! IS_AGGR_TYPE (t
))
1804 t
= make_aggr_type (RECORD_TYPE
);
1805 pushtag (make_anon_name (), t
, 0);
1808 /* In a definition of a member class template, we will get here with an
1809 implicit typename, a TYPENAME_TYPE with a type. */
1810 if (TREE_CODE (t
) == TYPENAME_TYPE
)
1813 /* If we generated a partial instantiation of this type, but now
1814 we're seeing a real definition, we're actually looking at a
1815 partial specialization. Consider:
1817 template <class T, class U>
1823 template <class T, class U>
1826 typename X<Y<T, U> >::A a;
1829 template <class T, class U>
1834 We have to undo the effects of the previous partial
1836 if (PARTIAL_INSTANTIATION_P (t
))
1840 /* Unfortunately, when we're not in pedantic mode, we
1841 attempt to actually fill in some of the fields of the
1842 partial instantiation, in order to support the implicit
1843 typename extension. Clear those fields now, in
1844 preparation for the definition here. The fields cleared
1845 here must match those set in instantiate_class_template.
1846 Look for a comment mentioning begin_class_definition
1848 TYPE_BINFO_BASETYPES (t
) = NULL_TREE
;
1849 TYPE_FIELDS (t
) = NULL_TREE
;
1850 TYPE_METHODS (t
) = NULL_TREE
;
1851 CLASSTYPE_TAGS (t
) = NULL_TREE
;
1852 CLASSTYPE_VBASECLASSES (t
) = NULL_TREE
;
1853 TYPE_SIZE (t
) = NULL_TREE
;
1856 /* This isn't a partial instantiation any more. */
1857 PARTIAL_INSTANTIATION_P (t
) = 0;
1859 /* If this type was already complete, and we see another definition,
1861 else if (COMPLETE_TYPE_P (t
))
1862 duplicate_tag_error (t
);
1864 /* Update the location of the decl. */
1865 DECL_SOURCE_FILE (TYPE_NAME (t
)) = input_filename
;
1866 DECL_SOURCE_LINE (TYPE_NAME (t
)) = lineno
;
1868 if (TYPE_BEING_DEFINED (t
))
1870 t
= make_aggr_type (TREE_CODE (t
));
1871 pushtag (TYPE_IDENTIFIER (t
), t
, 0);
1873 maybe_process_partial_specialization (t
);
1875 TYPE_BEING_DEFINED (t
) = 1;
1876 TYPE_PACKED (t
) = flag_pack_struct
;
1877 /* Reset the interface data, at the earliest possible
1878 moment, as it might have been set via a class foo;
1881 tree name
= TYPE_IDENTIFIER (t
);
1883 if (! ANON_AGGRNAME_P (name
))
1885 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
1886 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1887 (t
, interface_unknown
);
1890 /* Only leave this bit clear if we know this
1891 class is part of an interface-only specification. */
1892 if (! CLASSTYPE_INTERFACE_KNOWN (t
)
1893 || ! CLASSTYPE_INTERFACE_ONLY (t
))
1894 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = 1;
1896 reset_specialization();
1898 /* Make a declaration for this class in its own scope. */
1899 build_self_reference ();
1904 /* Finish the member declaration given by DECL. */
1907 finish_member_declaration (decl
)
1910 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
1913 if (decl
== void_type_node
)
1914 /* The COMPONENT was a friend, not a member, and so there's
1915 nothing for us to do. */
1918 /* We should see only one DECL at a time. */
1919 my_friendly_assert (TREE_CHAIN (decl
) == NULL_TREE
, 0);
1921 /* Set up access control for DECL. */
1923 = (current_access_specifier
== access_private_node
);
1924 TREE_PROTECTED (decl
)
1925 = (current_access_specifier
== access_protected_node
);
1926 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1928 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl
)) = TREE_PRIVATE (decl
);
1929 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl
)) = TREE_PROTECTED (decl
);
1932 /* Mark the DECL as a member of the current class. */
1933 DECL_CONTEXT (decl
) = current_class_type
;
1937 A C language linkage is ignored for the names of class members
1938 and the member function type of class member functions. */
1939 if (DECL_LANG_SPECIFIC (decl
) && DECL_LANGUAGE (decl
) == lang_c
)
1940 DECL_LANGUAGE (decl
) = lang_cplusplus
;
1942 /* Put functions on the TYPE_METHODS list and everything else on the
1943 TYPE_FIELDS list. Note that these are built up in reverse order.
1944 We reverse them (to obtain declaration order) in finish_struct. */
1945 if (TREE_CODE (decl
) == FUNCTION_DECL
1946 || DECL_FUNCTION_TEMPLATE_P (decl
))
1948 /* We also need to add this function to the
1949 CLASSTYPE_METHOD_VEC. */
1950 add_method (current_class_type
, decl
, /*error_p=*/0);
1952 TREE_CHAIN (decl
) = TYPE_METHODS (current_class_type
);
1953 TYPE_METHODS (current_class_type
) = decl
;
1957 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1958 go at the beginning. The reason is that lookup_field_1
1959 searches the list in order, and we want a field name to
1960 override a type name so that the "struct stat hack" will
1961 work. In particular:
1963 struct S { enum E { }; int E } s;
1966 is legal. In addition, the FIELD_DECLs must be maintained in
1967 declaration order so that class layout works as expected.
1968 However, we don't need that order until class layout, so we
1969 save a little time by putting FIELD_DECLs on in reverse order
1970 here, and then reversing them in finish_struct_1. (We could
1971 also keep a pointer to the correct insertion points in the
1974 if (TREE_CODE (decl
) == TYPE_DECL
)
1975 TYPE_FIELDS (current_class_type
)
1976 = chainon (TYPE_FIELDS (current_class_type
), decl
);
1979 TREE_CHAIN (decl
) = TYPE_FIELDS (current_class_type
);
1980 TYPE_FIELDS (current_class_type
) = decl
;
1983 /* Enter the DECL into the scope of the class. */
1984 if (TREE_CODE (decl
) != USING_DECL
)
1985 pushdecl_class_level (decl
);
1989 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1990 the definition is immediately followed by a semicolon. Returns the
1994 finish_class_definition (t
, attributes
, semi
, pop_scope_p
)
2000 /* finish_struct nukes this anyway; if finish_exception does too,
2003 note_got_semicolon (t
);
2005 /* If we got any attributes in class_head, xref_tag will stick them in
2006 TREE_TYPE of the type. Grab them now. */
2007 attributes
= chainon (TREE_TYPE (t
), attributes
);
2008 TREE_TYPE (t
) = NULL_TREE
;
2010 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
2014 t
= finish_struct (t
, attributes
);
2016 note_got_semicolon (t
);
2020 check_for_missing_semicolon (t
);
2022 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t
)));
2023 if (current_scope () == current_function_decl
)
2024 do_pending_defargs ();
2029 /* Finish processing the default argument expressions cached during
2030 the processing of a class definition. */
2033 begin_inline_definitions ()
2035 if (current_scope () == current_function_decl
)
2036 do_pending_inlines ();
2039 /* Finish processing the inline function definitions cached during the
2040 processing of a class definition. */
2043 finish_inline_definitions ()
2045 if (current_class_type
== NULL_TREE
)
2046 clear_inline_text_obstack ();
2049 /* Finish processing the declaration of a member class template
2050 TYPES whose template parameters are given by PARMS. */
2053 finish_member_class_template (types
)
2058 /* If there are declared, but undefined, partial specializations
2059 mixed in with the typespecs they will not yet have passed through
2060 maybe_process_partial_specialization, so we do that here. */
2061 for (t
= types
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2062 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t
))))
2063 maybe_process_partial_specialization (TREE_VALUE (t
));
2065 note_list_got_semicolon (types
);
2066 grok_x_components (types
);
2067 if (TYPE_CONTEXT (TREE_VALUE (types
)) != current_class_type
)
2068 /* The component was in fact a friend declaration. We avoid
2069 finish_member_template_decl performing certain checks by
2073 finish_member_template_decl (types
);
2075 /* As with other component type declarations, we do
2076 not store the new DECL on the list of
2081 /* Finish processsing a complete template declaration. The PARMS are
2082 the template parameters. */
2085 finish_template_decl (parms
)
2089 end_template_decl ();
2091 end_specialization ();
2094 /* Finish processing a a template-id (which names a type) of the form
2095 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2096 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2097 the scope of template-id indicated. */
2100 finish_template_type (name
, args
, entering_scope
)
2107 decl
= lookup_template_class (name
, args
,
2108 NULL_TREE
, NULL_TREE
, entering_scope
);
2109 if (decl
!= error_mark_node
)
2110 decl
= TYPE_STUB_DECL (decl
);
2115 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2116 namespace scope or a class scope. */
2122 tree scope
= TREE_OPERAND (sr
, 0);
2124 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2126 push_decl_namespace (scope
);
2127 TREE_COMPLEXITY (sr
) = -1;
2129 else if (scope
!= current_class_type
)
2131 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
2133 /* In a declarator for a template class member, the scope will
2134 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2135 scope
= TREE_TYPE (scope
);
2136 TREE_OPERAND (sr
, 0) = scope
;
2138 push_nested_class (scope
, 3);
2139 TREE_COMPLEXITY (sr
) = current_class_depth
;
2143 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2144 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2145 BASE_CLASS, or NULL_TREE if an error occurred. The
2146 ACCESSS_SPECIFIER is one of
2147 access_{default,public,protected_private}[_virtual]_node.*/
2150 finish_base_specifier (access_specifier
, base_class
)
2151 tree access_specifier
;
2157 if (base_class
== NULL_TREE
)
2159 error ("invalid base class");
2160 type
= error_mark_node
;
2163 type
= TREE_TYPE (base_class
);
2165 if (! is_aggr_type (type
, 1))
2168 result
= build_tree_list (access_specifier
, type
);
2173 /* Called when multiple declarators are processed. If that is not
2174 premitted in this context, an error is issued. */
2177 check_multiple_declarators ()
2181 In a template-declaration, explicit specialization, or explicit
2182 instantiation the init-declarator-list in the declaration shall
2183 contain at most one declarator.
2185 We don't just use PROCESSING_TEMPLATE_DECL for the first
2186 condition since that would disallow the perfectly legal code,
2187 like `template <class T> struct S { int i, j; };'. */
2188 tree scope
= current_scope ();
2190 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
2191 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2194 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2195 || processing_explicit_instantiation
2196 || processing_specialization
)
2197 cp_error ("multiple declarators in template declaration");
2201 finish_typeof (expr
)
2204 if (processing_template_decl
)
2208 t
= make_aggr_type (TYPEOF_TYPE
);
2209 TYPE_FIELDS (t
) = expr
;
2214 return TREE_TYPE (expr
);
2217 /* We're about to expand T, a statement. Set up appropriate context
2218 for the substitution. */
2224 if (!STMT_LINENO_FOR_FN_P (t
))
2225 lineno
= STMT_LINENO (t
);
2226 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
2229 /* Generate RTL for the statement T, and its substatements, and any
2230 other statements at its nesting level. */
2233 lang_expand_stmt (t
)
2236 tree rval
= NULL_TREE
;
2238 while (t
&& t
!= error_mark_node
)
2240 int saved_stmts_are_full_exprs_p
;
2242 /* Assume we'll have nothing to return. */
2245 /* Set up context appropriately for handling this statement. */
2246 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
2249 switch (TREE_CODE (t
))
2252 genrtl_return_stmt (RETURN_EXPR (t
));
2256 genrtl_expr_stmt (EXPR_STMT_EXPR (t
));
2260 genrtl_decl_stmt (t
);
2264 genrtl_decl_cleanup (CLEANUP_DECL (t
), CLEANUP_EXPR (t
));
2267 case START_CATCH_STMT
:
2268 genrtl_catch_block (TREE_TYPE (t
));
2272 genrtl_ctor_stmt (t
);
2276 genrtl_for_stmt (t
);
2280 genrtl_while_stmt (t
);
2292 genrtl_compound_stmt (t
);
2296 genrtl_break_stmt ();
2300 genrtl_continue_stmt ();
2304 genrtl_switch_stmt (t
);
2308 genrtl_case_label (CASE_LOW (t
), CASE_HIGH (t
));
2312 expand_label (LABEL_STMT_LABEL (t
));
2316 genrtl_goto_stmt (GOTO_DESTINATION (t
));
2320 genrtl_asm_stmt (ASM_CV_QUAL (t
), ASM_STRING (t
),
2321 ASM_OUTPUTS (t
), ASM_INPUTS (t
), ASM_CLOBBERS (t
));
2325 genrtl_try_block (t
);
2333 genrtl_subobject (SUBOBJECT_CLEANUP (t
));
2337 genrtl_scope_stmt (t
);
2341 genrtl_named_return_value ();
2345 my_friendly_abort (19990810);
2349 /* Restore saved state. */
2350 current_stmt_tree ()->stmts_are_full_exprs_p
=
2351 saved_stmts_are_full_exprs_p
;
2353 /* Go on to the next statement in this scope. */
2360 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2361 will equivalent CALL_EXPRs. */
2364 simplify_aggr_init_exprs_r (tp
, walk_subtrees
, data
)
2366 int *walk_subtrees ATTRIBUTE_UNUSED
;
2367 void *data ATTRIBUTE_UNUSED
;
2369 tree aggr_init_expr
;
2376 int copy_from_buffer_p
;
2378 aggr_init_expr
= *tp
;
2379 /* We don't need to walk into types; there's nothing in a type that
2380 needs simplification. (And, furthermore, there are places we
2381 actively don't want to go. For example, we don't want to wander
2382 into the default arguments for a FUNCTION_DECL that appears in a
2384 if (TYPE_P (aggr_init_expr
))
2389 /* Only AGGR_INIT_EXPRs are interesting. */
2390 else if (TREE_CODE (aggr_init_expr
) != AGGR_INIT_EXPR
)
2393 /* Form an appropriate CALL_EXPR. */
2394 fn
= TREE_OPERAND (aggr_init_expr
, 0);
2395 args
= TREE_OPERAND (aggr_init_expr
, 1);
2396 slot
= TREE_OPERAND (aggr_init_expr
, 2);
2397 type
= TREE_TYPE (aggr_init_expr
);
2399 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr
))
2401 /* Replace the first argument with the address of the third
2402 argument to the AGGR_INIT_EXPR. */
2403 call_type
= build_pointer_type (type
);
2404 mark_addressable (slot
);
2405 args
= tree_cons (NULL_TREE
, build1 (ADDR_EXPR
, call_type
, slot
),
2408 call_expr
= build (CALL_EXPR
, call_type
, fn
, args
, NULL_TREE
);
2409 TREE_SIDE_EFFECTS (call_expr
) = 1;
2411 /* If we're using the non-reentrant PCC calling convention, then we
2412 need to copy the returned value out of the static buffer into the
2414 copy_from_buffer_p
= 0;
2415 #ifdef PCC_STATIC_STRUCT_RETURN
2416 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr
) && aggregate_value_p (type
))
2420 flag_access_control
= 0;
2421 call_expr
= build_aggr_init (slot
, call_expr
, LOOKUP_ONLYCONVERTING
);
2422 flag_access_control
= old_ac
;
2423 copy_from_buffer_p
= 1;
2427 /* If this AGGR_INIT_EXPR indicates the value returned by a
2428 function, then we want to use the value of the initialized
2429 location as the result. */
2430 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr
) || copy_from_buffer_p
)
2432 call_expr
= build (COMPOUND_EXPR
, type
,
2434 TREE_SIDE_EFFECTS (call_expr
) = 1;
2437 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2438 TREE_CHAIN (call_expr
) = TREE_CHAIN (aggr_init_expr
);
2441 /* Keep iterating. */
2445 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2448 emit_associated_thunks (fn
)
2451 /* When we use vcall offsets, we emit thunks with the virtual
2452 functions to which they thunk. The whole point of vcall offsets
2453 is so that you can know statically the entire set of thunks that
2454 will ever be needed for a given virtual function, thereby
2455 enabling you to output all the thunks with the function itself. */
2456 if (vcall_offsets_in_vtable_p () && DECL_VIRTUAL_P (fn
))
2461 for (binfo
= TYPE_BINFO (DECL_CONTEXT (fn
));
2463 binfo
= TREE_CHAIN (binfo
))
2464 for (v
= BINFO_VIRTUALS (binfo
); v
; v
= TREE_CHAIN (v
))
2466 && (!integer_zerop (BV_DELTA (v
))
2467 || BV_VCALL_INDEX (v
)))
2472 if (BV_USE_VCALL_INDEX_P (v
))
2474 vcall_index
= BV_VCALL_INDEX (v
);
2475 my_friendly_assert (vcall_index
!= NULL_TREE
, 20000621);
2478 vcall_index
= NULL_TREE
;
2480 thunk
= make_thunk (build1 (ADDR_EXPR
,
2481 vfunc_ptr_type_node
,
2485 /*generate_with_vtable_p=*/0);
2486 use_thunk (thunk
, /*emit_p=*/1);
2492 /* Generate RTL for FN. */
2499 const char *saved_input_filename
;
2501 /* When the parser calls us after finishing the body of a template
2502 function, we don't really want to expand the body. When we're
2503 processing an in-class definition of an inline function,
2504 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2505 to look at the function itself. */
2506 if (processing_template_decl
2507 || (DECL_LANG_SPECIFIC (fn
)
2508 && DECL_TEMPLATE_INFO (fn
)
2509 && uses_template_parms (DECL_TI_ARGS (fn
))))
2511 /* Normally, collection only occurs in rest_of_compilation. So,
2512 if we don't collect here, we never collect junk generated
2513 during the processing of templates until we hit a
2514 non-template function. */
2519 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2520 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn
),
2521 simplify_aggr_init_exprs_r
,
2524 /* If this is a constructor or destructor body, we have to clone it
2525 under the new ABI. */
2526 if (maybe_clone_body (fn
))
2528 /* We don't want to process FN again, so pretend we've written
2529 it out, even though we haven't. */
2530 TREE_ASM_WRITTEN (fn
) = 1;
2534 /* There's no reason to do any of the work here if we're only doing
2535 semantic analysis; this code just generates RTL. */
2536 if (flag_syntax_only
)
2539 /* If possible, avoid generating RTL for this function. Instead,
2540 just record it as an inline function, and wait until end-of-file
2541 to decide whether to write it out or not. */
2542 if (/* We have to generate RTL if it's not an inline function. */
2543 (DECL_INLINE (fn
) || DECL_COMDAT (fn
))
2544 /* Or if we have to keep all inline functions anyhow. */
2545 && !flag_keep_inline_functions
2546 /* Or if we actually have a reference to the function. */
2547 && !DECL_NEEDED_P (fn
)
2548 /* Or if this is a nested function. */
2549 && !decl_function_context (fn
))
2551 /* Give the function RTL now so that we can assign it to a
2552 function pointer, etc. */
2553 make_function_rtl (fn
);
2554 /* Set DECL_EXTERNAL so that assemble_external will be called as
2555 necessary. We'll clear it again in finish_file. */
2556 if (!DECL_EXTERNAL (fn
))
2558 DECL_NOT_REALLY_EXTERN (fn
) = 1;
2559 DECL_EXTERNAL (fn
) = 1;
2561 /* Remember this function. In finish_file we'll decide if
2562 we actually need to write this function out. */
2564 /* Let the back-end know that this funtion exists. */
2565 note_deferral_of_defined_inline_function (fn
);
2569 /* Emit any thunks that should be emitted at the same time as FN. */
2570 emit_associated_thunks (fn
);
2572 timevar_push (TV_INTEGRATION
);
2574 /* Optimize the body of the function before expanding it. */
2575 optimize_function (fn
);
2577 timevar_pop (TV_INTEGRATION
);
2578 timevar_push (TV_EXPAND
);
2580 /* Save the current file name and line number. When we expand the
2581 body of the function, we'll set LINENO and INPUT_FILENAME so that
2582 error-mesages come out in the right places. */
2583 saved_lineno
= lineno
;
2584 saved_input_filename
= input_filename
;
2585 lineno
= DECL_SOURCE_LINE (fn
);
2586 input_filename
= DECL_SOURCE_FILE (fn
);
2588 start_function (NULL_TREE
, fn
, NULL_TREE
, SF_PRE_PARSED
| SF_EXPAND
);
2589 store_parm_decls ();
2590 current_function_is_thunk
= DECL_THUNK_P (fn
);
2592 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2593 any of the other magic variables we set up when starting a
2595 cp_function_chain
->name_declared
= 1;
2597 /* Expand the body. */
2598 expand_stmt (DECL_SAVED_TREE (fn
));
2600 /* Statements should always be full-expressions at the outermost set
2601 of curly braces for a function. */
2602 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2604 /* The outermost statement for a function contains the line number
2605 recorded when we finished processing the function. */
2606 lineno
= STMT_LINENO (DECL_SAVED_TREE (fn
));
2608 /* Generate code for the function. */
2609 finish_function (0);
2611 /* If possible, obliterate the body of the function so that it can
2612 be garbage collected. */
2613 if (flag_dump_translation_unit
)
2614 /* Keep the body; we're going to dump it. */
2616 else if (DECL_INLINE (fn
) && flag_inline_trees
)
2617 /* We might need the body of this function so that we can expand
2618 it inline somewhere else. */
2621 /* We don't need the body; blow it away. */
2622 DECL_SAVED_TREE (fn
) = NULL_TREE
;
2624 /* And restore the current source position. */
2625 lineno
= saved_lineno
;
2626 input_filename
= saved_input_filename
;
2627 extract_interface_info ();
2629 timevar_pop (TV_EXPAND
);