1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* Handle method declarations. */
28 #include "coretypes.h"
37 #include "tree-pass.h"
38 #include "diagnostic.h"
42 /* Various flags to control the mangling process. */
48 /* The thing we are presently mangling is part of a template type,
49 rather than a fully instantiated type. Therefore, we may see
50 complex expressions where we would normally expect to see a
51 simple integer constant. */
52 mf_maybe_uninstantiated
= 1,
53 /* When mangling a numeric value, use the form `_XX_' (instead of
54 just `XX') if the value has more than one digit. */
55 mf_use_underscores_around_value
= 2
58 typedef enum mangling_flags mangling_flags
;
60 static void do_build_assign_ref (tree
);
61 static void do_build_copy_constructor (tree
);
62 static tree
synthesize_exception_spec (tree
, tree (*) (tree
, void *), void *);
63 static tree
make_alias_for_thunk (tree
);
65 /* Called once to initialize method.c. */
73 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
74 indicates whether it is a this or result adjusting thunk.
75 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
76 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
77 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
78 adjusting thunks, we scale it to a byte offset. For covariant
79 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
80 the returned thunk with finish_thunk. */
83 make_thunk (tree function
, bool this_adjusting
,
84 tree fixed_offset
, tree virtual_offset
)
89 gcc_assert (TREE_CODE (function
) == FUNCTION_DECL
);
90 /* We can have this thunks to covariant thunks, but not vice versa. */
91 gcc_assert (!DECL_THIS_THUNK_P (function
));
92 gcc_assert (!DECL_RESULT_THUNK_P (function
) || this_adjusting
);
94 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
95 if (this_adjusting
&& virtual_offset
)
97 = size_binop (MULT_EXPR
,
100 TYPE_SIZE_UNIT (vtable_entry_type
)));
102 d
= tree_low_cst (fixed_offset
, 0);
104 /* See if we already have the thunk in question. For this_adjusting
105 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
107 for (thunk
= DECL_THUNKS (function
); thunk
; thunk
= TREE_CHAIN (thunk
))
108 if (DECL_THIS_THUNK_P (thunk
) == this_adjusting
109 && THUNK_FIXED_OFFSET (thunk
) == d
110 && !virtual_offset
== !THUNK_VIRTUAL_OFFSET (thunk
)
113 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk
),
115 : THUNK_VIRTUAL_OFFSET (thunk
) == virtual_offset
)))
118 /* All thunks must be created before FUNCTION is actually emitted;
119 the ABI requires that all thunks be emitted together with the
120 function to which they transfer control. */
121 gcc_assert (!TREE_ASM_WRITTEN (function
));
122 /* Likewise, we can only be adding thunks to a function declared in
123 the class currently being laid out. */
124 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function
))
125 && TYPE_BEING_DEFINED (DECL_CONTEXT (function
)));
127 thunk
= build_decl (DECL_SOURCE_LOCATION (function
),
128 FUNCTION_DECL
, NULL_TREE
, TREE_TYPE (function
));
129 DECL_LANG_SPECIFIC (thunk
) = DECL_LANG_SPECIFIC (function
);
130 cxx_dup_lang_specific_decl (thunk
);
131 DECL_THUNKS (thunk
) = NULL_TREE
;
133 DECL_CONTEXT (thunk
) = DECL_CONTEXT (function
);
134 TREE_READONLY (thunk
) = TREE_READONLY (function
);
135 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
136 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
);
137 SET_DECL_THUNK_P (thunk
, this_adjusting
);
138 THUNK_TARGET (thunk
) = function
;
139 THUNK_FIXED_OFFSET (thunk
) = d
;
140 THUNK_VIRTUAL_OFFSET (thunk
) = virtual_offset
;
141 THUNK_ALIAS (thunk
) = NULL_TREE
;
143 /* The thunk itself is not a constructor or destructor, even if
144 the thing it is thunking to is. */
145 DECL_INTERFACE_KNOWN (thunk
) = 1;
146 DECL_NOT_REALLY_EXTERN (thunk
) = 1;
147 DECL_SAVED_FUNCTION_DATA (thunk
) = NULL
;
148 DECL_DESTRUCTOR_P (thunk
) = 0;
149 DECL_CONSTRUCTOR_P (thunk
) = 0;
150 DECL_EXTERNAL (thunk
) = 1;
151 DECL_ARTIFICIAL (thunk
) = 1;
152 /* The THUNK is not a pending inline, even if the FUNCTION is. */
153 DECL_PENDING_INLINE_P (thunk
) = 0;
154 DECL_DECLARED_INLINE_P (thunk
) = 0;
155 /* Nor is it a template instantiation. */
156 DECL_USE_TEMPLATE (thunk
) = 0;
157 DECL_TEMPLATE_INFO (thunk
) = NULL
;
159 /* Add it to the list of thunks associated with FUNCTION. */
160 TREE_CHAIN (thunk
) = DECL_THUNKS (function
);
161 DECL_THUNKS (function
) = thunk
;
166 /* Finish THUNK, a thunk decl. */
169 finish_thunk (tree thunk
)
172 tree fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (thunk
));
173 tree virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk
);
175 gcc_assert (!DECL_NAME (thunk
) && DECL_THUNK_P (thunk
));
176 if (virtual_offset
&& DECL_RESULT_THUNK_P (thunk
))
177 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
178 function
= THUNK_TARGET (thunk
);
179 name
= mangle_thunk (function
, DECL_THIS_THUNK_P (thunk
),
180 fixed_offset
, virtual_offset
);
182 /* We can end up with declarations of (logically) different
183 covariant thunks, that do identical adjustments. The two thunks
184 will be adjusting between within different hierarchies, which
185 happen to have the same layout. We must nullify one of them to
186 refer to the other. */
187 if (DECL_RESULT_THUNK_P (thunk
))
191 for (cov_probe
= DECL_THUNKS (function
);
192 cov_probe
; cov_probe
= TREE_CHAIN (cov_probe
))
193 if (DECL_NAME (cov_probe
) == name
)
195 gcc_assert (!DECL_THUNKS (thunk
));
196 THUNK_ALIAS (thunk
) = (THUNK_ALIAS (cov_probe
)
197 ? THUNK_ALIAS (cov_probe
) : cov_probe
);
202 DECL_NAME (thunk
) = name
;
203 SET_DECL_ASSEMBLER_NAME (thunk
, name
);
206 static GTY (()) int thunk_labelno
;
208 /* Create a static alias to target. */
211 make_alias_for (tree target
, tree newid
)
213 tree alias
= build_decl (DECL_SOURCE_LOCATION (target
),
214 TREE_CODE (target
), newid
, TREE_TYPE (target
));
215 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (target
);
216 cxx_dup_lang_specific_decl (alias
);
217 DECL_CONTEXT (alias
) = NULL
;
218 TREE_READONLY (alias
) = TREE_READONLY (target
);
219 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (target
);
220 TREE_PUBLIC (alias
) = 0;
221 DECL_INTERFACE_KNOWN (alias
) = 1;
222 if (DECL_LANG_SPECIFIC (alias
))
224 DECL_NOT_REALLY_EXTERN (alias
) = 1;
225 DECL_USE_TEMPLATE (alias
) = 0;
226 DECL_TEMPLATE_INFO (alias
) = NULL
;
228 DECL_EXTERNAL (alias
) = 0;
229 DECL_ARTIFICIAL (alias
) = 1;
230 DECL_TEMPLATE_INSTANTIATED (alias
) = 0;
231 if (TREE_CODE (alias
) == FUNCTION_DECL
)
233 DECL_SAVED_FUNCTION_DATA (alias
) = NULL
;
234 DECL_DESTRUCTOR_P (alias
) = 0;
235 DECL_CONSTRUCTOR_P (alias
) = 0;
236 DECL_PENDING_INLINE_P (alias
) = 0;
237 DECL_DECLARED_INLINE_P (alias
) = 0;
238 DECL_INITIAL (alias
) = error_mark_node
;
239 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (target
));
242 TREE_STATIC (alias
) = 1;
243 TREE_ADDRESSABLE (alias
) = 1;
244 TREE_USED (alias
) = 1;
245 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
246 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias
)) = 1;
251 make_alias_for_thunk (tree function
)
256 ASM_GENERATE_INTERNAL_LABEL (buf
, "LTHUNK", thunk_labelno
);
259 alias
= make_alias_for (function
, get_identifier (buf
));
261 if (!flag_syntax_only
)
263 bool ok
= cgraph_same_body_alias (alias
, function
);
264 DECL_ASSEMBLER_NAME (function
);
271 /* Emit the definition of a C++ multiple inheritance or covariant
272 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
276 use_thunk (tree thunk_fndecl
, bool emit_p
)
278 tree a
, t
, function
, alias
;
280 HOST_WIDE_INT fixed_offset
, virtual_value
;
281 bool this_adjusting
= DECL_THIS_THUNK_P (thunk_fndecl
);
283 /* We should have called finish_thunk to give it a name. */
284 gcc_assert (DECL_NAME (thunk_fndecl
));
286 /* We should never be using an alias, always refer to the
288 gcc_assert (!THUNK_ALIAS (thunk_fndecl
));
290 if (TREE_ASM_WRITTEN (thunk_fndecl
))
293 function
= THUNK_TARGET (thunk_fndecl
);
294 if (DECL_RESULT (thunk_fndecl
))
295 /* We already turned this thunk into an ordinary function.
296 There's no need to process this thunk again. */
299 if (DECL_THUNK_P (function
))
300 /* The target is itself a thunk, process it now. */
301 use_thunk (function
, emit_p
);
303 /* Thunks are always addressable; they only appear in vtables. */
304 TREE_ADDRESSABLE (thunk_fndecl
) = 1;
306 /* Figure out what function is being thunked to. It's referenced in
307 this translation unit. */
308 TREE_ADDRESSABLE (function
) = 1;
309 mark_used (function
);
313 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
314 alias
= make_alias_for_thunk (function
);
318 fixed_offset
= THUNK_FIXED_OFFSET (thunk_fndecl
);
319 virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk_fndecl
);
324 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
325 virtual_value
= tree_low_cst (virtual_offset
, /*pos=*/0);
326 gcc_assert (virtual_value
);
331 /* And, if we need to emit the thunk, it's used. */
332 mark_used (thunk_fndecl
);
333 /* This thunk is actually defined. */
334 DECL_EXTERNAL (thunk_fndecl
) = 0;
335 /* The linkage of the function may have changed. FIXME in linkage
337 TREE_PUBLIC (thunk_fndecl
) = TREE_PUBLIC (function
);
338 DECL_VISIBILITY (thunk_fndecl
) = DECL_VISIBILITY (function
);
339 DECL_VISIBILITY_SPECIFIED (thunk_fndecl
)
340 = DECL_VISIBILITY_SPECIFIED (function
);
341 if (DECL_ONE_ONLY (function
) || DECL_WEAK (function
))
342 make_decl_one_only (thunk_fndecl
, cxx_comdat_group (thunk_fndecl
));
344 if (flag_syntax_only
)
346 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
350 push_to_top_level ();
352 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
353 && targetm
.have_named_sections
)
355 resolve_unique_section (function
, 0, flag_function_sections
);
357 if (DECL_SECTION_NAME (function
) != NULL
&& DECL_ONE_ONLY (function
))
359 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
361 /* Output the thunk into the same section as function. */
362 DECL_SECTION_NAME (thunk_fndecl
) = DECL_SECTION_NAME (function
);
366 /* Set up cloned argument trees for the thunk. */
368 for (a
= DECL_ARGUMENTS (function
); a
; a
= TREE_CHAIN (a
))
370 tree x
= copy_node (a
);
372 DECL_CONTEXT (x
) = thunk_fndecl
;
373 SET_DECL_RTL (x
, NULL
);
374 DECL_HAS_VALUE_EXPR_P (x
) = 0;
378 DECL_ARGUMENTS (thunk_fndecl
) = a
;
379 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
380 cgraph_add_thunk (thunk_fndecl
, function
,
381 this_adjusting
, fixed_offset
, virtual_value
,
382 virtual_offset
, alias
);
385 || !targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
386 virtual_value
, alias
))
388 /* If this is a covariant thunk, or we don't have the necessary
389 code for efficient thunks, generate a thunk function that
390 just makes a call to the real function. Unfortunately, this
391 doesn't work for varargs. */
393 if (varargs_function_p (function
))
394 error ("generic thunk code fails for method %q#D which uses %<...%>",
398 pop_from_top_level ();
401 /* Code for synthesizing methods which have default semantics defined. */
403 /* Generate code for default X(X&) or X(X&&) constructor. */
406 do_build_copy_constructor (tree fndecl
)
408 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
409 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
411 parm
= convert_from_reference (parm
);
413 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type
)
414 && is_empty_class (current_class_type
))
415 /* Don't copy the padding byte; it might not have been allocated
416 if *this is a base subobject. */;
417 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type
))
419 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
420 finish_expr_stmt (t
);
424 tree fields
= TYPE_FIELDS (current_class_type
);
425 tree member_init_list
= NULL_TREE
;
426 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
428 tree binfo
, base_binfo
;
430 VEC(tree
,gc
) *vbases
;
432 /* Initialize all the base-classes with the parameter converted
433 to their type so that we get their copy constructor and not
434 another constructor that takes current_class_type. We must
435 deal with the binfo's directly as a direct base might be
436 inaccessible due to ambiguity. */
437 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
438 VEC_iterate (tree
, vbases
, i
, binfo
); i
++)
440 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1);
445 build_tree_list (NULL_TREE
, init
),
449 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
450 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
452 if (BINFO_VIRTUAL_P (base_binfo
))
455 init
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1);
459 = tree_cons (base_binfo
,
460 build_tree_list (NULL_TREE
, init
),
464 for (; fields
; fields
= TREE_CHAIN (fields
))
469 if (TREE_CODE (field
) != FIELD_DECL
)
472 expr_type
= TREE_TYPE (field
);
473 if (DECL_NAME (field
))
475 if (VFIELD_NAME_P (DECL_NAME (field
)))
478 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
479 /* Just use the field; anonymous types can't have
480 nontrivial copy ctors or assignment ops. */;
484 /* Compute the type of "init->field". If the copy-constructor
485 parameter is, for example, "const S&", and the type of
486 the field is "T", then the type will usually be "const
487 T". (There are no cv-qualified variants of reference
489 if (TREE_CODE (expr_type
) != REFERENCE_TYPE
)
493 if (DECL_MUTABLE_P (field
))
494 quals
&= ~TYPE_QUAL_CONST
;
495 quals
|= cp_type_quals (expr_type
);
496 expr_type
= cp_build_qualified_type (expr_type
, quals
);
499 init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
500 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
)
502 init
= build_tree_list (NULL_TREE
, init
);
504 member_init_list
= tree_cons (field
, init
, member_init_list
);
506 finish_mem_initializers (member_init_list
);
511 do_build_assign_ref (tree fndecl
)
513 tree parm
= TREE_CHAIN (DECL_ARGUMENTS (fndecl
));
516 compound_stmt
= begin_compound_stmt (0);
517 parm
= convert_from_reference (parm
);
519 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type
)
520 && is_empty_class (current_class_type
))
521 /* Don't copy the padding byte; it might not have been allocated
522 if *this is a base subobject. */;
523 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type
))
525 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
526 finish_expr_stmt (t
);
531 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
533 tree binfo
, base_binfo
;
535 /* Assign to each of the direct base classes. */
536 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
537 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
540 VEC(tree
,gc
) *parmvec
;
542 /* We must convert PARM directly to the base class
543 explicitly since the base class may be ambiguous. */
544 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1);
545 /* Call the base class assignment operator. */
546 parmvec
= make_tree_vector_single (converted_parm
);
548 (build_special_member_call (current_class_ref
,
549 ansi_assopname (NOP_EXPR
),
552 LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
,
553 tf_warning_or_error
));
554 release_tree_vector (parmvec
);
557 /* Assign to each of the non-static data members. */
558 for (fields
= TYPE_FIELDS (current_class_type
);
560 fields
= TREE_CHAIN (fields
))
562 tree comp
= current_class_ref
;
568 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
571 expr_type
= TREE_TYPE (field
);
573 if (CP_TYPE_CONST_P (expr_type
))
575 error ("non-static const member %q#D, can't use default "
576 "assignment operator", field
);
579 else if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
581 error ("non-static reference member %q#D, can't use "
582 "default assignment operator", field
);
586 if (DECL_NAME (field
))
588 if (VFIELD_NAME_P (DECL_NAME (field
)))
591 else if (ANON_AGGR_TYPE_P (expr_type
)
592 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
593 /* Just use the field; anonymous types can't have
594 nontrivial copy ctors or assignment ops. */;
598 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
600 /* Compute the type of init->field */
602 if (DECL_MUTABLE_P (field
))
603 quals
&= ~TYPE_QUAL_CONST
;
604 expr_type
= cp_build_qualified_type (expr_type
, quals
);
606 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
608 if (DECL_NAME (field
))
609 init
= cp_build_modify_expr (comp
, NOP_EXPR
, init
,
610 tf_warning_or_error
);
612 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
613 finish_expr_stmt (init
);
616 finish_return_stmt (current_class_ref
);
617 finish_compound_stmt (compound_stmt
);
620 /* Synthesize FNDECL, a non-static member function. */
623 synthesize_method (tree fndecl
)
625 bool nested
= (current_function_decl
!= NULL_TREE
);
626 tree context
= decl_function_context (fndecl
);
627 bool need_body
= true;
629 location_t save_input_location
= input_location
;
630 int error_count
= errorcount
;
631 int warning_count
= warningcount
;
633 /* Reset the source location, we might have been previously
634 deferred, and thus have saved where we were first needed. */
635 DECL_SOURCE_LOCATION (fndecl
)
636 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
638 /* If we've been asked to synthesize a clone, just synthesize the
639 cloned function instead. Doing so will automatically fill in the
640 body for the clone. */
641 if (DECL_CLONED_FUNCTION_P (fndecl
))
642 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
644 /* We may be in the middle of deferred access check. Disable
646 push_deferring_access_checks (dk_no_deferred
);
649 push_to_top_level ();
651 push_function_context ();
653 input_location
= DECL_SOURCE_LOCATION (fndecl
);
655 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
656 stmt
= begin_function_body ();
658 if (DECL_OVERLOADED_OPERATOR_P (fndecl
) == NOP_EXPR
)
660 do_build_assign_ref (fndecl
);
663 else if (DECL_CONSTRUCTOR_P (fndecl
))
665 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
666 if (arg_chain
!= void_list_node
)
667 do_build_copy_constructor (fndecl
);
669 finish_mem_initializers (NULL_TREE
);
672 /* If we haven't yet generated the body of the function, just
673 generate an empty compound statement. */
677 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
678 finish_compound_stmt (compound_stmt
);
681 finish_function_body (stmt
);
682 expand_or_defer_fn (finish_function (0));
684 input_location
= save_input_location
;
687 pop_from_top_level ();
689 pop_function_context ();
691 pop_deferring_access_checks ();
693 if (error_count
!= errorcount
|| warning_count
!= warningcount
)
694 inform (input_location
, "synthesized method %qD first required here ",
698 /* Use EXTRACTOR to locate the relevant function called for each base &
699 class field of TYPE. CLIENT allows additional information to be passed
700 to EXTRACTOR. Generates the union of all exceptions generated by those
701 functions. Note that we haven't updated TYPE_FIELDS and such of any
702 variants yet, so we need to look at the main one. */
705 synthesize_exception_spec (tree type
, tree (*extractor
) (tree
, void*),
708 tree raises
= empty_except_spec
;
709 tree fields
= TYPE_FIELDS (type
);
710 tree binfo
, base_binfo
;
713 for (binfo
= TYPE_BINFO (type
), i
= 0;
714 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
716 tree fn
= (*extractor
) (BINFO_TYPE (base_binfo
), client
);
719 tree fn_raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
721 raises
= merge_exception_specifiers (raises
, fn_raises
);
724 for (; fields
; fields
= TREE_CHAIN (fields
))
726 tree type
= TREE_TYPE (fields
);
729 if (TREE_CODE (fields
) != FIELD_DECL
|| DECL_ARTIFICIAL (fields
))
731 while (TREE_CODE (type
) == ARRAY_TYPE
)
732 type
= TREE_TYPE (type
);
733 if (!CLASS_TYPE_P (type
))
736 fn
= (*extractor
) (type
, client
);
739 tree fn_raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
741 raises
= merge_exception_specifiers (raises
, fn_raises
);
747 /* Locate the dtor of TYPE. */
750 locate_dtor (tree type
, void *client ATTRIBUTE_UNUSED
)
752 return CLASSTYPE_DESTRUCTORS (type
);
755 /* Locate the default ctor of TYPE. */
758 locate_ctor (tree type
, void *client ATTRIBUTE_UNUSED
)
762 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
765 /* Call lookup_fnfields_1 to create the constructor declarations, if
767 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type
))
768 return lazily_declare_fn (sfk_constructor
, type
);
770 for (fns
= CLASSTYPE_CONSTRUCTORS (type
); fns
; fns
= OVL_NEXT (fns
))
772 tree fn
= OVL_CURRENT (fns
);
773 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
775 parms
= skip_artificial_parms_for (fn
, parms
);
777 if (sufficient_parms_p (parms
))
789 /* Locate the copy ctor or copy assignment of TYPE. CLIENT_
790 points to a COPY_DATA holding the name (NULL for the ctor)
791 and desired qualifiers of the source operand. */
794 locate_copy (tree type
, void *client_
)
796 struct copy_data
*client
= (struct copy_data
*)client_
;
798 tree best
= NULL_TREE
;
799 bool excess_p
= false;
804 ix
= lookup_fnfields_1 (type
, client
->name
);
807 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (type
), ix
);
809 else if (TYPE_HAS_INIT_REF (type
))
811 /* If construction of the copy constructor was postponed, create
813 if (CLASSTYPE_LAZY_COPY_CTOR (type
))
814 lazily_declare_fn (sfk_copy_constructor
, type
);
815 if (CLASSTYPE_LAZY_MOVE_CTOR (type
))
816 lazily_declare_fn (sfk_move_constructor
, type
);
817 fns
= CLASSTYPE_CONSTRUCTORS (type
);
821 for (; fns
; fns
= OVL_NEXT (fns
))
823 tree fn
= OVL_CURRENT (fns
);
824 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
829 parms
= skip_artificial_parms_for (fn
, parms
);
832 src_type
= non_reference (TREE_VALUE (parms
));
834 if (src_type
== error_mark_node
)
837 if (!same_type_ignoring_top_level_qualifiers_p (src_type
, type
))
839 if (!sufficient_parms_p (TREE_CHAIN (parms
)))
841 quals
= cp_type_quals (src_type
);
842 if (client
->quals
& ~quals
)
844 excess
= quals
& ~client
->quals
;
845 if (!best
|| (excess_p
&& !excess
))
857 /* Implicitly declare the special function indicated by KIND, as a
858 member of TYPE. For copy constructors and assignment operators,
859 CONST_P indicates whether these functions should take a const
860 reference argument or a non-const reference. Returns the
861 FUNCTION_DECL for the implicitly declared function. */
864 implicitly_declare_fn (special_function_kind kind
, tree type
, bool const_p
)
867 tree parameter_types
= void_list_node
;
870 tree raises
= empty_except_spec
;
871 tree rhs_parm_type
= NULL_TREE
;
874 HOST_WIDE_INT saved_processing_template_decl
;
876 /* Because we create declarations for implicitly declared functions
877 lazily, we may be creating the declaration for a member of TYPE
878 while in some completely different context. However, TYPE will
879 never be a dependent class (because we never want to do lookups
880 for implicitly defined functions in a dependent class).
881 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
882 because we only create clones for constructors and destructors
883 when not in a template. */
884 gcc_assert (!dependent_type_p (type
));
885 saved_processing_template_decl
= processing_template_decl
;
886 processing_template_decl
= 0;
888 type
= TYPE_MAIN_VARIANT (type
);
890 if (targetm
.cxx
.cdtor_returns_this () && !TYPE_FOR_JAVA (type
))
892 if (kind
== sfk_destructor
)
893 /* See comment in check_special_function_return_type. */
894 return_type
= build_pointer_type (void_type_node
);
896 return_type
= build_pointer_type (type
);
899 return_type
= void_type_node
;
905 name
= constructor_name (type
);
906 raises
= synthesize_exception_spec (type
, &locate_dtor
, 0);
909 case sfk_constructor
:
910 /* Default constructor. */
911 name
= constructor_name (type
);
912 raises
= synthesize_exception_spec (type
, &locate_ctor
, 0);
915 case sfk_copy_constructor
:
916 case sfk_assignment_operator
:
917 case sfk_move_constructor
:
919 struct copy_data data
;
923 if (kind
== sfk_assignment_operator
)
925 return_type
= build_reference_type (type
);
926 name
= ansi_assopname (NOP_EXPR
);
930 name
= constructor_name (type
);
934 data
.quals
= TYPE_QUAL_CONST
;
935 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
938 rhs_parm_type
= type
;
940 = cp_build_reference_type (rhs_parm_type
,
941 kind
== sfk_move_constructor
);
942 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
943 raises
= synthesize_exception_spec (type
, &locate_copy
, &data
);
950 /* Create the function. */
951 fn_type
= build_method_type_directly (type
, return_type
, parameter_types
);
953 fn_type
= build_exception_variant (fn_type
, raises
);
954 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
955 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
956 if (kind
== sfk_constructor
|| kind
== sfk_copy_constructor
957 || kind
== sfk_move_constructor
)
958 DECL_CONSTRUCTOR_P (fn
) = 1;
959 else if (kind
== sfk_destructor
)
960 DECL_DESTRUCTOR_P (fn
) = 1;
963 DECL_ASSIGNMENT_OPERATOR_P (fn
) = 1;
964 SET_OVERLOADED_OPERATOR_CODE (fn
, NOP_EXPR
);
967 /* If pointers to member functions use the least significant bit to
968 indicate whether a function is virtual, ensure a pointer
969 to this function will have that bit clear. */
970 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
971 && DECL_ALIGN (fn
) < 2 * BITS_PER_UNIT
)
972 DECL_ALIGN (fn
) = 2 * BITS_PER_UNIT
;
974 /* Create the explicit arguments. */
977 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
978 want its type to be included in the mangled function
980 DECL_ARGUMENTS (fn
) = cp_build_parm_decl (NULL_TREE
, rhs_parm_type
);
981 TREE_READONLY (DECL_ARGUMENTS (fn
)) = 1;
983 /* Add the "this" parameter. */
984 this_parm
= build_this_parm (fn_type
, TYPE_UNQUALIFIED
);
985 TREE_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
986 DECL_ARGUMENTS (fn
) = this_parm
;
988 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
989 set_linkage_according_to_type (type
, fn
);
990 rest_of_decl_compilation (fn
, toplevel_bindings_p (), at_eof
);
991 DECL_IN_AGGR_P (fn
) = 1;
992 DECL_ARTIFICIAL (fn
) = 1;
993 DECL_DEFAULTED_FN (fn
) = 1;
994 DECL_NOT_REALLY_EXTERN (fn
) = 1;
995 DECL_DECLARED_INLINE_P (fn
) = 1;
996 gcc_assert (!TREE_USED (fn
));
998 /* Restore PROCESSING_TEMPLATE_DECL. */
999 processing_template_decl
= saved_processing_template_decl
;
1004 /* Gives any errors about defaulted functions which need to be deferred
1005 until the containing class is complete. */
1008 defaulted_late_check (tree fn
)
1010 /* Complain about invalid signature for defaulted fn. */
1011 tree ctx
= DECL_CONTEXT (fn
);
1012 special_function_kind kind
= special_function_p (fn
);
1013 bool fn_const_p
= (copy_fn_p (fn
) == 2);
1014 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
);
1016 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
1017 TREE_TYPE (TREE_TYPE (implicit_fn
)))
1018 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1019 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
1021 error ("defaulted declaration %q+D", fn
);
1022 error_at (DECL_SOURCE_LOCATION (fn
),
1023 "does not match expected signature %qD", implicit_fn
);
1027 /* Returns true iff FN can be explicitly defaulted, and gives any
1028 errors if defaulting FN is ill-formed. */
1031 defaultable_fn_check (tree fn
)
1033 special_function_kind kind
= sfk_none
;
1035 if (DECL_CONSTRUCTOR_P (fn
))
1037 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
1038 kind
= sfk_constructor
;
1039 else if (copy_fn_p (fn
) > 0
1040 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
1042 kind
= sfk_copy_constructor
;
1043 else if (move_fn_p (fn
))
1044 kind
= sfk_move_constructor
;
1046 else if (DECL_DESTRUCTOR_P (fn
))
1047 kind
= sfk_destructor
;
1048 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
1049 && DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
1051 kind
= sfk_assignment_operator
;
1053 if (kind
== sfk_none
)
1055 error ("%qD cannot be defaulted", fn
);
1060 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
1061 for (; t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1062 if (TREE_PURPOSE (t
))
1064 error ("defaulted function %q+D with default argument", fn
);
1067 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn
)))
1069 if (DECL_NONCONVERTING_P (fn
))
1070 error ("%qD declared explicit cannot be defaulted in the class "
1072 if (current_access_specifier
!= access_public_node
)
1073 error ("%qD declared with non-public access cannot be defaulted "
1074 "in the class body", fn
);
1075 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
1076 error ("function %q+D defaulted on its first declaration "
1077 "must not have an exception-specification", fn
);
1078 if (DECL_VIRTUAL_P (fn
))
1079 error ("%qD declared virtual cannot be defaulted in the class "
1082 else if (!processing_template_decl
)
1083 defaulted_late_check (fn
);
1089 /* Add an implicit declaration to TYPE for the kind of function
1090 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1094 lazily_declare_fn (special_function_kind sfk
, tree type
)
1099 /* Figure out whether or not the argument has a const reference
1101 if (sfk
== sfk_copy_constructor
)
1102 const_p
= TYPE_HAS_CONST_INIT_REF (type
);
1103 else if (sfk
== sfk_assignment_operator
)
1104 const_p
= TYPE_HAS_CONST_ASSIGN_REF (type
);
1106 /* In this case, CONST_P will be ignored. */
1108 /* Declare the function. */
1109 fn
= implicitly_declare_fn (sfk
, type
, const_p
);
1110 /* A destructor may be virtual. */
1111 if (sfk
== sfk_destructor
)
1112 check_for_override (fn
, type
);
1113 /* Add it to CLASSTYPE_METHOD_VEC. */
1114 add_method (type
, fn
, NULL_TREE
);
1115 /* Add it to TYPE_METHODS. */
1116 if (sfk
== sfk_destructor
1117 && DECL_VIRTUAL_P (fn
)
1118 && abi_version_at_least (2))
1119 /* The ABI requires that a virtual destructor go at the end of the
1121 TYPE_METHODS (type
) = chainon (TYPE_METHODS (type
), fn
);
1124 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1125 TYPE_METHODS list, which cause the destructor to be emitted
1126 in an incorrect location in the vtable. */
1127 if (warn_abi
&& sfk
== sfk_destructor
&& DECL_VIRTUAL_P (fn
))
1128 warning (OPT_Wabi
, "vtable layout for class %qT may not be ABI-compliant"
1129 "and may change in a future version of GCC due to "
1130 "implicit virtual destructor",
1132 TREE_CHAIN (fn
) = TYPE_METHODS (type
);
1133 TYPE_METHODS (type
) = fn
;
1135 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
1136 if (sfk
== sfk_assignment_operator
)
1137 CLASSTYPE_LAZY_ASSIGNMENT_OP (type
) = 0;
1140 /* Remember that the function has been created. */
1141 if (sfk
== sfk_constructor
)
1142 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
1143 else if (sfk
== sfk_copy_constructor
)
1144 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
1145 else if (sfk
== sfk_move_constructor
)
1146 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
1147 else if (sfk
== sfk_destructor
)
1148 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
1149 /* Create appropriate clones. */
1150 clone_function_decl (fn
, /*update_method_vec=*/true);
1156 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1157 as there are artificial parms in FN. */
1160 skip_artificial_parms_for (const_tree fn
, tree list
)
1162 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1163 list
= TREE_CHAIN (list
);
1167 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1168 list
= TREE_CHAIN (list
);
1169 if (DECL_HAS_VTT_PARM_P (fn
))
1170 list
= TREE_CHAIN (list
);
1174 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1175 artificial parms in FN. */
1178 num_artificial_parms_for (const_tree fn
)
1182 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1187 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1189 if (DECL_HAS_VTT_PARM_P (fn
))
1195 #include "gt-cp-method.h"