cgraphunit.c (cgraph_function_versioning,save_inline_function_body): Use DECL_COMDAT_...
[official-gcc.git] / gcc / cp / method.c
blobaf58afe135e3f21db45cabf09fbf16883695bec7
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)
13 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "rtl.h"
33 #include "expr.h"
34 #include "output.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "tm_p.h"
38 #include "target.h"
39 #include "tree-pass.h"
40 #include "diagnostic.h"
41 #include "cgraph.h"
43 /* Various flags to control the mangling process. */
45 enum mangling_flags
47 /* No flags. */
48 mf_none = 0,
49 /* The thing we are presently mangling is part of a template type,
50 rather than a fully instantiated type. Therefore, we may see
51 complex expressions where we would normally expect to see a
52 simple integer constant. */
53 mf_maybe_uninstantiated = 1,
54 /* When mangling a numeric value, use the form `_XX_' (instead of
55 just `XX') if the value has more than one digit. */
56 mf_use_underscores_around_value = 2
59 typedef enum mangling_flags mangling_flags;
61 static tree thunk_adjust (tree, bool, HOST_WIDE_INT, tree);
62 static void do_build_assign_ref (tree);
63 static void do_build_copy_constructor (tree);
64 static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
65 static tree make_alias_for_thunk (tree);
67 /* Called once to initialize method.c. */
69 void
70 init_method (void)
72 init_mangle ();
75 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
76 indicates whether it is a this or result adjusting thunk.
77 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
78 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
79 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
80 adjusting thunks, we scale it to a byte offset. For covariant
81 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
82 the returned thunk with finish_thunk. */
84 tree
85 make_thunk (tree function, bool this_adjusting,
86 tree fixed_offset, tree virtual_offset)
88 HOST_WIDE_INT d;
89 tree thunk;
91 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
92 /* We can have this thunks to covariant thunks, but not vice versa. */
93 gcc_assert (!DECL_THIS_THUNK_P (function));
94 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
96 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
97 if (this_adjusting && virtual_offset)
98 virtual_offset
99 = size_binop (MULT_EXPR,
100 virtual_offset,
101 convert (ssizetype,
102 TYPE_SIZE_UNIT (vtable_entry_type)));
104 d = tree_low_cst (fixed_offset, 0);
106 /* See if we already have the thunk in question. For this_adjusting
107 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
108 will be a BINFO. */
109 for (thunk = DECL_THUNKS (function); thunk; thunk = TREE_CHAIN (thunk))
110 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
111 && THUNK_FIXED_OFFSET (thunk) == d
112 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
113 && (!virtual_offset
114 || (this_adjusting
115 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
116 virtual_offset)
117 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
118 return thunk;
120 /* All thunks must be created before FUNCTION is actually emitted;
121 the ABI requires that all thunks be emitted together with the
122 function to which they transfer control. */
123 gcc_assert (!TREE_ASM_WRITTEN (function));
124 /* Likewise, we can only be adding thunks to a function declared in
125 the class currently being laid out. */
126 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
127 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
129 thunk = build_decl (DECL_SOURCE_LOCATION (function),
130 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
131 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
132 cxx_dup_lang_specific_decl (thunk);
133 DECL_THUNKS (thunk) = NULL_TREE;
135 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
136 TREE_READONLY (thunk) = TREE_READONLY (function);
137 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
138 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
139 SET_DECL_THUNK_P (thunk, this_adjusting);
140 THUNK_TARGET (thunk) = function;
141 THUNK_FIXED_OFFSET (thunk) = d;
142 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
143 THUNK_ALIAS (thunk) = NULL_TREE;
145 /* The thunk itself is not a constructor or destructor, even if
146 the thing it is thunking to is. */
147 DECL_INTERFACE_KNOWN (thunk) = 1;
148 DECL_NOT_REALLY_EXTERN (thunk) = 1;
149 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
150 DECL_DESTRUCTOR_P (thunk) = 0;
151 DECL_CONSTRUCTOR_P (thunk) = 0;
152 DECL_EXTERNAL (thunk) = 1;
153 DECL_ARTIFICIAL (thunk) = 1;
154 /* Even if this thunk is a member of a local class, we don't
155 need a static chain. */
156 DECL_NO_STATIC_CHAIN (thunk) = 1;
157 /* The THUNK is not a pending inline, even if the FUNCTION is. */
158 DECL_PENDING_INLINE_P (thunk) = 0;
159 DECL_DECLARED_INLINE_P (thunk) = 0;
160 /* Nor has it been deferred. */
161 DECL_DEFERRED_FN (thunk) = 0;
162 /* Nor is it a template instantiation. */
163 DECL_USE_TEMPLATE (thunk) = 0;
164 DECL_TEMPLATE_INFO (thunk) = NULL;
166 /* Add it to the list of thunks associated with FUNCTION. */
167 TREE_CHAIN (thunk) = DECL_THUNKS (function);
168 DECL_THUNKS (function) = thunk;
170 return thunk;
173 /* Finish THUNK, a thunk decl. */
175 void
176 finish_thunk (tree thunk)
178 tree function, name;
179 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
180 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
182 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
183 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
184 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
185 function = THUNK_TARGET (thunk);
186 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
187 fixed_offset, virtual_offset);
189 /* We can end up with declarations of (logically) different
190 covariant thunks, that do identical adjustments. The two thunks
191 will be adjusting between within different hierarchies, which
192 happen to have the same layout. We must nullify one of them to
193 refer to the other. */
194 if (DECL_RESULT_THUNK_P (thunk))
196 tree cov_probe;
198 for (cov_probe = DECL_THUNKS (function);
199 cov_probe; cov_probe = TREE_CHAIN (cov_probe))
200 if (DECL_NAME (cov_probe) == name)
202 gcc_assert (!DECL_THUNKS (thunk));
203 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
204 ? THUNK_ALIAS (cov_probe) : cov_probe);
205 break;
209 DECL_NAME (thunk) = name;
210 SET_DECL_ASSEMBLER_NAME (thunk, name);
213 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
214 offset indicated by VIRTUAL_OFFSET, if that is
215 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
216 zero for a result adjusting thunk. */
218 static tree
219 thunk_adjust (tree ptr, bool this_adjusting,
220 HOST_WIDE_INT fixed_offset, tree virtual_offset)
222 if (this_adjusting)
223 /* Adjust the pointer by the constant. */
224 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
225 size_int (fixed_offset));
227 /* If there's a virtual offset, look up that value in the vtable and
228 adjust the pointer again. */
229 if (virtual_offset)
231 tree vtable;
233 ptr = save_expr (ptr);
234 /* The vptr is always at offset zero in the object. */
235 vtable = build1 (NOP_EXPR,
236 build_pointer_type (build_pointer_type
237 (vtable_entry_type)),
238 ptr);
239 /* Form the vtable address. */
240 vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
241 /* Find the entry with the vcall offset. */
242 vtable = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtable), vtable,
243 fold_convert (sizetype, virtual_offset));
244 /* Get the offset itself. */
245 vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
246 /* Adjust the `this' pointer. */
247 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
248 fold_convert (sizetype, vtable));
251 if (!this_adjusting)
252 /* Adjust the pointer by the constant. */
253 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
254 size_int (fixed_offset));
256 return ptr;
259 static GTY (()) int thunk_labelno;
261 /* Create a static alias to function. */
263 tree
264 make_alias_for (tree function, tree newid)
266 tree alias = build_decl (DECL_SOURCE_LOCATION (function),
267 FUNCTION_DECL, newid, TREE_TYPE (function));
268 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (function);
269 cxx_dup_lang_specific_decl (alias);
270 DECL_CONTEXT (alias) = NULL;
271 TREE_READONLY (alias) = TREE_READONLY (function);
272 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (function);
273 TREE_PUBLIC (alias) = 0;
274 DECL_INTERFACE_KNOWN (alias) = 1;
275 DECL_NOT_REALLY_EXTERN (alias) = 1;
276 DECL_THIS_STATIC (alias) = 1;
277 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
278 DECL_DESTRUCTOR_P (alias) = 0;
279 DECL_CONSTRUCTOR_P (alias) = 0;
280 DECL_CLONED_FUNCTION (alias) = NULL_TREE;
281 DECL_EXTERNAL (alias) = 0;
282 DECL_ARTIFICIAL (alias) = 1;
283 DECL_NO_STATIC_CHAIN (alias) = 1;
284 DECL_PENDING_INLINE_P (alias) = 0;
285 DECL_DECLARED_INLINE_P (alias) = 0;
286 DECL_DEFERRED_FN (alias) = 0;
287 DECL_USE_TEMPLATE (alias) = 0;
288 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
289 DECL_TEMPLATE_INFO (alias) = NULL;
290 DECL_INITIAL (alias) = error_mark_node;
291 TREE_ADDRESSABLE (alias) = 1;
292 TREE_USED (alias) = 1;
293 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
294 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
295 return alias;
298 static tree
299 make_alias_for_thunk (tree function)
301 tree alias;
302 char buf[256];
304 ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
305 thunk_labelno++;
307 alias = make_alias_for (function, get_identifier (buf));
309 if (!flag_syntax_only)
310 assemble_alias (alias, DECL_ASSEMBLER_NAME (function));
312 return alias;
315 /* Emit the definition of a C++ multiple inheritance or covariant
316 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
317 immediately. */
319 void
320 use_thunk (tree thunk_fndecl, bool emit_p)
322 tree a, t, function, alias;
323 tree virtual_offset;
324 HOST_WIDE_INT fixed_offset, virtual_value;
325 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
327 /* We should have called finish_thunk to give it a name. */
328 gcc_assert (DECL_NAME (thunk_fndecl));
330 /* We should never be using an alias, always refer to the
331 aliased thunk. */
332 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
334 if (TREE_ASM_WRITTEN (thunk_fndecl))
335 return;
337 function = THUNK_TARGET (thunk_fndecl);
338 if (DECL_RESULT (thunk_fndecl))
339 /* We already turned this thunk into an ordinary function.
340 There's no need to process this thunk again. */
341 return;
343 if (DECL_THUNK_P (function))
344 /* The target is itself a thunk, process it now. */
345 use_thunk (function, emit_p);
347 /* Thunks are always addressable; they only appear in vtables. */
348 TREE_ADDRESSABLE (thunk_fndecl) = 1;
350 /* Figure out what function is being thunked to. It's referenced in
351 this translation unit. */
352 TREE_ADDRESSABLE (function) = 1;
353 mark_used (function);
354 if (!emit_p)
355 return;
357 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
358 alias = make_alias_for_thunk (function);
359 else
360 alias = function;
362 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
363 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
365 if (virtual_offset)
367 if (!this_adjusting)
368 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
369 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
370 gcc_assert (virtual_value);
372 else
373 virtual_value = 0;
375 /* And, if we need to emit the thunk, it's used. */
376 mark_used (thunk_fndecl);
377 /* This thunk is actually defined. */
378 DECL_EXTERNAL (thunk_fndecl) = 0;
379 /* The linkage of the function may have changed. FIXME in linkage
380 rewrite. */
381 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
382 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
383 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
384 = DECL_VISIBILITY_SPECIFIED (function);
385 if (DECL_ONE_ONLY (function))
386 make_decl_one_only (thunk_fndecl, cxx_comdat_group (thunk_fndecl));
388 if (flag_syntax_only)
390 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
391 return;
394 push_to_top_level ();
396 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
397 && targetm.have_named_sections)
399 resolve_unique_section (function, 0, flag_function_sections);
401 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
403 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
405 /* Output the thunk into the same section as function. */
406 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
410 /* Set up cloned argument trees for the thunk. */
411 t = NULL_TREE;
412 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
414 tree x = copy_node (a);
415 TREE_CHAIN (x) = t;
416 DECL_CONTEXT (x) = thunk_fndecl;
417 SET_DECL_RTL (x, NULL_RTX);
418 DECL_HAS_VALUE_EXPR_P (x) = 0;
419 t = x;
421 a = nreverse (t);
422 DECL_ARGUMENTS (thunk_fndecl) = a;
424 if (this_adjusting
425 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
426 virtual_value, alias))
428 const char *fnname;
429 tree fn_block;
431 current_function_decl = thunk_fndecl;
432 DECL_RESULT (thunk_fndecl)
433 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
434 RESULT_DECL, 0, integer_type_node);
435 fnname = IDENTIFIER_POINTER (DECL_NAME (thunk_fndecl));
436 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
437 create one. */
438 fn_block = make_node (BLOCK);
439 BLOCK_VARS (fn_block) = a;
440 DECL_INITIAL (thunk_fndecl) = fn_block;
441 init_function_start (thunk_fndecl);
442 cfun->is_thunk = 1;
443 assemble_start_function (thunk_fndecl, fnname);
445 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
446 fixed_offset, virtual_value, alias);
448 assemble_end_function (thunk_fndecl, fnname);
449 init_insn_lengths ();
450 current_function_decl = 0;
451 set_cfun (NULL);
452 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
454 else
456 int i;
457 tree *argarray = (tree *) alloca (list_length (a) * sizeof (tree));
458 /* If this is a covariant thunk, or we don't have the necessary
459 code for efficient thunks, generate a thunk function that
460 just makes a call to the real function. Unfortunately, this
461 doesn't work for varargs. */
463 if (varargs_function_p (function))
464 error ("generic thunk code fails for method %q#D which uses %<...%>",
465 function);
467 DECL_RESULT (thunk_fndecl) = NULL_TREE;
469 start_preparsed_function (thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
470 /* We don't bother with a body block for thunks. */
472 /* There's no need to check accessibility inside the thunk body. */
473 push_deferring_access_checks (dk_no_check);
475 t = a;
476 if (this_adjusting)
477 t = thunk_adjust (t, /*this_adjusting=*/1,
478 fixed_offset, virtual_offset);
480 /* Build up the call to the real function. */
481 argarray[0] = t;
482 for (i = 1, a = TREE_CHAIN (a); a; a = TREE_CHAIN (a), i++)
483 argarray[i] = a;
484 t = build_call_a (alias, i, argarray);
485 CALL_FROM_THUNK_P (t) = 1;
486 CALL_CANNOT_INLINE_P (t) = 1;
488 if (VOID_TYPE_P (TREE_TYPE (t)))
489 finish_expr_stmt (t);
490 else
492 if (!this_adjusting)
494 tree cond = NULL_TREE;
496 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
498 /* If the return type is a pointer, we need to
499 protect against NULL. We know there will be an
500 adjustment, because that's why we're emitting a
501 thunk. */
502 t = save_expr (t);
503 cond = cp_convert (boolean_type_node, t);
506 t = thunk_adjust (t, /*this_adjusting=*/0,
507 fixed_offset, virtual_offset);
508 if (cond)
509 t = build3 (COND_EXPR, TREE_TYPE (t), cond, t,
510 cp_convert (TREE_TYPE (t), integer_zero_node));
512 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
513 t = build_cplus_new (TREE_TYPE (t), t);
514 finish_return_stmt (t);
517 /* Since we want to emit the thunk, we explicitly mark its name as
518 referenced. */
519 mark_decl_referenced (thunk_fndecl);
521 /* But we don't want debugging information about it. */
522 DECL_IGNORED_P (thunk_fndecl) = 1;
524 /* Re-enable access control. */
525 pop_deferring_access_checks ();
527 thunk_fndecl = finish_function (0);
528 cgraph_add_new_function (thunk_fndecl, false);
531 pop_from_top_level ();
534 /* Code for synthesizing methods which have default semantics defined. */
536 /* Generate code for default X(X&) constructor. */
538 static void
539 do_build_copy_constructor (tree fndecl)
541 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
543 parm = convert_from_reference (parm);
545 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type)
546 && is_empty_class (current_class_type))
547 /* Don't copy the padding byte; it might not have been allocated
548 if *this is a base subobject. */;
549 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
551 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
552 finish_expr_stmt (t);
554 else
556 tree fields = TYPE_FIELDS (current_class_type);
557 tree member_init_list = NULL_TREE;
558 int cvquals = cp_type_quals (TREE_TYPE (parm));
559 int i;
560 tree binfo, base_binfo;
561 VEC(tree,gc) *vbases;
563 /* Initialize all the base-classes with the parameter converted
564 to their type so that we get their copy constructor and not
565 another constructor that takes current_class_type. We must
566 deal with the binfo's directly as a direct base might be
567 inaccessible due to ambiguity. */
568 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
569 VEC_iterate (tree, vbases, i, binfo); i++)
571 member_init_list
572 = tree_cons (binfo,
573 build_tree_list (NULL_TREE,
574 build_base_path (PLUS_EXPR, parm,
575 binfo, 1)),
576 member_init_list);
579 for (binfo = TYPE_BINFO (current_class_type), i = 0;
580 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
582 if (BINFO_VIRTUAL_P (base_binfo))
583 continue;
585 member_init_list
586 = tree_cons (base_binfo,
587 build_tree_list (NULL_TREE,
588 build_base_path (PLUS_EXPR, parm,
589 base_binfo, 1)),
590 member_init_list);
593 for (; fields; fields = TREE_CHAIN (fields))
595 tree init = parm;
596 tree field = fields;
597 tree expr_type;
599 if (TREE_CODE (field) != FIELD_DECL)
600 continue;
602 expr_type = TREE_TYPE (field);
603 if (DECL_NAME (field))
605 if (VFIELD_NAME_P (DECL_NAME (field)))
606 continue;
608 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
609 /* Just use the field; anonymous types can't have
610 nontrivial copy ctors or assignment ops. */;
611 else
612 continue;
614 /* Compute the type of "init->field". If the copy-constructor
615 parameter is, for example, "const S&", and the type of
616 the field is "T", then the type will usually be "const
617 T". (There are no cv-qualified variants of reference
618 types.) */
619 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
621 int quals = cvquals;
623 if (DECL_MUTABLE_P (field))
624 quals &= ~TYPE_QUAL_CONST;
625 expr_type = cp_build_qualified_type (expr_type, quals);
628 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
629 init = build_tree_list (NULL_TREE, init);
631 member_init_list = tree_cons (field, init, member_init_list);
633 finish_mem_initializers (member_init_list);
637 static void
638 do_build_assign_ref (tree fndecl)
640 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
641 tree compound_stmt;
643 compound_stmt = begin_compound_stmt (0);
644 parm = convert_from_reference (parm);
646 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
647 && is_empty_class (current_class_type))
648 /* Don't copy the padding byte; it might not have been allocated
649 if *this is a base subobject. */;
650 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
652 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
653 finish_expr_stmt (t);
655 else
657 tree fields;
658 int cvquals = cp_type_quals (TREE_TYPE (parm));
659 int i;
660 tree binfo, base_binfo;
662 /* Assign to each of the direct base classes. */
663 for (binfo = TYPE_BINFO (current_class_type), i = 0;
664 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
666 tree converted_parm;
667 VEC(tree,gc) *parmvec;
669 /* We must convert PARM directly to the base class
670 explicitly since the base class may be ambiguous. */
671 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
672 /* Call the base class assignment operator. */
673 parmvec = make_tree_vector_single (converted_parm);
674 finish_expr_stmt
675 (build_special_member_call (current_class_ref,
676 ansi_assopname (NOP_EXPR),
677 &parmvec,
678 base_binfo,
679 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
680 tf_warning_or_error));
681 release_tree_vector (parmvec);
684 /* Assign to each of the non-static data members. */
685 for (fields = TYPE_FIELDS (current_class_type);
686 fields;
687 fields = TREE_CHAIN (fields))
689 tree comp = current_class_ref;
690 tree init = parm;
691 tree field = fields;
692 tree expr_type;
693 int quals;
695 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
696 continue;
698 expr_type = TREE_TYPE (field);
700 if (CP_TYPE_CONST_P (expr_type))
702 error ("non-static const member %q#D, can't use default "
703 "assignment operator", field);
704 continue;
706 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
708 error ("non-static reference member %q#D, can't use "
709 "default assignment operator", field);
710 continue;
713 if (DECL_NAME (field))
715 if (VFIELD_NAME_P (DECL_NAME (field)))
716 continue;
718 else if (ANON_AGGR_TYPE_P (expr_type)
719 && TYPE_FIELDS (expr_type) != NULL_TREE)
720 /* Just use the field; anonymous types can't have
721 nontrivial copy ctors or assignment ops. */;
722 else
723 continue;
725 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
727 /* Compute the type of init->field */
728 quals = cvquals;
729 if (DECL_MUTABLE_P (field))
730 quals &= ~TYPE_QUAL_CONST;
731 expr_type = cp_build_qualified_type (expr_type, quals);
733 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
735 if (DECL_NAME (field))
736 init = cp_build_modify_expr (comp, NOP_EXPR, init,
737 tf_warning_or_error);
738 else
739 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
740 finish_expr_stmt (init);
743 finish_return_stmt (current_class_ref);
744 finish_compound_stmt (compound_stmt);
747 /* Synthesize FNDECL, a non-static member function. */
749 void
750 synthesize_method (tree fndecl)
752 bool nested = (current_function_decl != NULL_TREE);
753 tree context = decl_function_context (fndecl);
754 bool need_body = true;
755 tree stmt;
756 location_t save_input_location = input_location;
757 int error_count = errorcount;
758 int warning_count = warningcount;
760 /* Reset the source location, we might have been previously
761 deferred, and thus have saved where we were first needed. */
762 DECL_SOURCE_LOCATION (fndecl)
763 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
765 /* If we've been asked to synthesize a clone, just synthesize the
766 cloned function instead. Doing so will automatically fill in the
767 body for the clone. */
768 if (DECL_CLONED_FUNCTION_P (fndecl))
769 fndecl = DECL_CLONED_FUNCTION (fndecl);
771 /* We may be in the middle of deferred access check. Disable
772 it now. */
773 push_deferring_access_checks (dk_no_deferred);
775 if (! context)
776 push_to_top_level ();
777 else if (nested)
778 push_function_context ();
780 input_location = DECL_SOURCE_LOCATION (fndecl);
782 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
783 stmt = begin_function_body ();
785 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
787 do_build_assign_ref (fndecl);
788 need_body = false;
790 else if (DECL_CONSTRUCTOR_P (fndecl))
792 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
793 if (arg_chain != void_list_node)
794 do_build_copy_constructor (fndecl);
795 else
796 finish_mem_initializers (NULL_TREE);
799 /* If we haven't yet generated the body of the function, just
800 generate an empty compound statement. */
801 if (need_body)
803 tree compound_stmt;
804 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
805 finish_compound_stmt (compound_stmt);
808 finish_function_body (stmt);
809 expand_or_defer_fn (finish_function (0));
811 input_location = save_input_location;
813 if (! context)
814 pop_from_top_level ();
815 else if (nested)
816 pop_function_context ();
818 pop_deferring_access_checks ();
820 if (error_count != errorcount || warning_count != warningcount)
821 inform (input_location, "synthesized method %qD first required here ",
822 fndecl);
825 /* Use EXTRACTOR to locate the relevant function called for each base &
826 class field of TYPE. CLIENT allows additional information to be passed
827 to EXTRACTOR. Generates the union of all exceptions generated by those
828 functions. Note that we haven't updated TYPE_FIELDS and such of any
829 variants yet, so we need to look at the main one. */
831 static tree
832 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
833 void *client)
835 tree raises = empty_except_spec;
836 tree fields = TYPE_FIELDS (type);
837 tree binfo, base_binfo;
838 int i;
840 for (binfo = TYPE_BINFO (type), i = 0;
841 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
843 tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
844 if (fn)
846 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
848 raises = merge_exception_specifiers (raises, fn_raises);
851 for (; fields; fields = TREE_CHAIN (fields))
853 tree type = TREE_TYPE (fields);
854 tree fn;
856 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
857 continue;
858 while (TREE_CODE (type) == ARRAY_TYPE)
859 type = TREE_TYPE (type);
860 if (!CLASS_TYPE_P (type))
861 continue;
863 fn = (*extractor) (type, client);
864 if (fn)
866 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
868 raises = merge_exception_specifiers (raises, fn_raises);
871 return raises;
874 /* Locate the dtor of TYPE. */
876 tree
877 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
879 return CLASSTYPE_DESTRUCTORS (type);
882 /* Locate the default ctor of TYPE. */
884 tree
885 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
887 tree fns;
889 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
890 return NULL_TREE;
892 /* Call lookup_fnfields_1 to create the constructor declarations, if
893 necessary. */
894 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
895 return lazily_declare_fn (sfk_constructor, type);
897 for (fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
899 tree fn = OVL_CURRENT (fns);
900 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
902 parms = skip_artificial_parms_for (fn, parms);
904 if (sufficient_parms_p (parms))
905 return fn;
907 gcc_unreachable ();
910 struct copy_data
912 tree name;
913 int quals;
916 /* Locate the copy ctor or copy assignment of TYPE. CLIENT_
917 points to a COPY_DATA holding the name (NULL for the ctor)
918 and desired qualifiers of the source operand. */
920 tree
921 locate_copy (tree type, void *client_)
923 struct copy_data *client = (struct copy_data *)client_;
924 tree fns;
925 tree best = NULL_TREE;
926 bool excess_p = false;
928 if (client->name)
930 int ix;
931 ix = lookup_fnfields_1 (type, client->name);
932 if (ix < 0)
933 return NULL_TREE;
934 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
936 else if (TYPE_HAS_INIT_REF (type))
938 /* If construction of the copy constructor was postponed, create
939 it now. */
940 if (CLASSTYPE_LAZY_COPY_CTOR (type))
941 lazily_declare_fn (sfk_copy_constructor, type);
942 fns = CLASSTYPE_CONSTRUCTORS (type);
944 else
945 return NULL_TREE;
946 for (; fns; fns = OVL_NEXT (fns))
948 tree fn = OVL_CURRENT (fns);
949 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
950 tree src_type;
951 int excess;
952 int quals;
954 parms = skip_artificial_parms_for (fn, parms);
955 if (!parms)
956 continue;
957 src_type = non_reference (TREE_VALUE (parms));
959 if (src_type == error_mark_node)
960 return NULL_TREE;
962 if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
963 continue;
964 if (!sufficient_parms_p (TREE_CHAIN (parms)))
965 continue;
966 quals = cp_type_quals (src_type);
967 if (client->quals & ~quals)
968 continue;
969 excess = quals & ~client->quals;
970 if (!best || (excess_p && !excess))
972 best = fn;
973 excess_p = excess;
975 else
976 /* Ambiguous */
977 return NULL_TREE;
979 return best;
982 /* Implicitly declare the special function indicated by KIND, as a
983 member of TYPE. For copy constructors and assignment operators,
984 CONST_P indicates whether these functions should take a const
985 reference argument or a non-const reference. Returns the
986 FUNCTION_DECL for the implicitly declared function. */
988 static tree
989 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
991 tree fn;
992 tree parameter_types = void_list_node;
993 tree return_type;
994 tree fn_type;
995 tree raises = empty_except_spec;
996 tree rhs_parm_type = NULL_TREE;
997 tree this_parm;
998 tree name;
999 HOST_WIDE_INT saved_processing_template_decl;
1001 /* Because we create declarations for implicitly declared functions
1002 lazily, we may be creating the declaration for a member of TYPE
1003 while in some completely different context. However, TYPE will
1004 never be a dependent class (because we never want to do lookups
1005 for implicitly defined functions in a dependent class).
1006 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1007 because we only create clones for constructors and destructors
1008 when not in a template. */
1009 gcc_assert (!dependent_type_p (type));
1010 saved_processing_template_decl = processing_template_decl;
1011 processing_template_decl = 0;
1013 type = TYPE_MAIN_VARIANT (type);
1015 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1017 if (kind == sfk_destructor)
1018 /* See comment in check_special_function_return_type. */
1019 return_type = build_pointer_type (void_type_node);
1020 else
1021 return_type = build_pointer_type (type);
1023 else
1024 return_type = void_type_node;
1026 switch (kind)
1028 case sfk_destructor:
1029 /* Destructor. */
1030 name = constructor_name (type);
1031 raises = synthesize_exception_spec (type, &locate_dtor, 0);
1032 break;
1034 case sfk_constructor:
1035 /* Default constructor. */
1036 name = constructor_name (type);
1037 raises = synthesize_exception_spec (type, &locate_ctor, 0);
1038 break;
1040 case sfk_copy_constructor:
1041 case sfk_assignment_operator:
1043 struct copy_data data;
1045 data.name = NULL;
1046 data.quals = 0;
1047 if (kind == sfk_assignment_operator)
1049 return_type = build_reference_type (type);
1050 name = ansi_assopname (NOP_EXPR);
1051 data.name = name;
1053 else
1054 name = constructor_name (type);
1056 if (const_p)
1058 data.quals = TYPE_QUAL_CONST;
1059 rhs_parm_type = build_qualified_type (type, TYPE_QUAL_CONST);
1061 else
1062 rhs_parm_type = type;
1063 rhs_parm_type = build_reference_type (rhs_parm_type);
1064 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1065 raises = synthesize_exception_spec (type, &locate_copy, &data);
1066 break;
1068 default:
1069 gcc_unreachable ();
1072 /* Create the function. */
1073 fn_type = build_method_type_directly (type, return_type, parameter_types);
1074 if (raises)
1075 fn_type = build_exception_variant (fn_type, raises);
1076 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1077 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1078 if (kind == sfk_constructor || kind == sfk_copy_constructor)
1079 DECL_CONSTRUCTOR_P (fn) = 1;
1080 else if (kind == sfk_destructor)
1081 DECL_DESTRUCTOR_P (fn) = 1;
1082 else
1084 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1085 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1088 /* If pointers to member functions use the least significant bit to
1089 indicate whether a function is virtual, ensure a pointer
1090 to this function will have that bit clear. */
1091 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1092 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1093 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1095 /* Create the explicit arguments. */
1096 if (rhs_parm_type)
1098 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1099 want its type to be included in the mangled function
1100 name. */
1101 DECL_ARGUMENTS (fn) = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1102 TREE_READONLY (DECL_ARGUMENTS (fn)) = 1;
1104 /* Add the "this" parameter. */
1105 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1106 TREE_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1107 DECL_ARGUMENTS (fn) = this_parm;
1109 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1110 set_linkage_according_to_type (type, fn);
1111 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1112 DECL_IN_AGGR_P (fn) = 1;
1113 DECL_ARTIFICIAL (fn) = 1;
1114 DECL_DEFAULTED_FN (fn) = 1;
1115 DECL_NOT_REALLY_EXTERN (fn) = 1;
1116 DECL_DECLARED_INLINE_P (fn) = 1;
1117 gcc_assert (!TREE_USED (fn));
1119 /* Restore PROCESSING_TEMPLATE_DECL. */
1120 processing_template_decl = saved_processing_template_decl;
1122 return fn;
1125 /* Add an implicit declaration to TYPE for the kind of function
1126 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1127 declaration. */
1129 tree
1130 lazily_declare_fn (special_function_kind sfk, tree type)
1132 tree fn;
1133 bool const_p;
1135 /* Figure out whether or not the argument has a const reference
1136 type. */
1137 if (sfk == sfk_copy_constructor)
1138 const_p = TYPE_HAS_CONST_INIT_REF (type);
1139 else if (sfk == sfk_assignment_operator)
1140 const_p = TYPE_HAS_CONST_ASSIGN_REF (type);
1141 else
1142 /* In this case, CONST_P will be ignored. */
1143 const_p = false;
1144 /* Declare the function. */
1145 fn = implicitly_declare_fn (sfk, type, const_p);
1146 /* A destructor may be virtual. */
1147 if (sfk == sfk_destructor)
1148 check_for_override (fn, type);
1149 /* Add it to CLASSTYPE_METHOD_VEC. */
1150 add_method (type, fn, NULL_TREE);
1151 /* Add it to TYPE_METHODS. */
1152 if (sfk == sfk_destructor
1153 && DECL_VIRTUAL_P (fn)
1154 && abi_version_at_least (2))
1155 /* The ABI requires that a virtual destructor go at the end of the
1156 vtable. */
1157 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1158 else
1160 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1161 TYPE_METHODS list, which cause the destructor to be emitted
1162 in an incorrect location in the vtable. */
1163 if (warn_abi && DECL_VIRTUAL_P (fn))
1164 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1165 "and may change in a future version of GCC due to "
1166 "implicit virtual destructor",
1167 type);
1168 TREE_CHAIN (fn) = TYPE_METHODS (type);
1169 TYPE_METHODS (type) = fn;
1171 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1172 if (sfk == sfk_assignment_operator)
1173 CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
1174 else
1176 /* Remember that the function has been created. */
1177 if (sfk == sfk_constructor)
1178 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1179 else if (sfk == sfk_copy_constructor)
1180 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1181 else if (sfk == sfk_destructor)
1182 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1183 /* Create appropriate clones. */
1184 clone_function_decl (fn, /*update_method_vec=*/true);
1187 return fn;
1190 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1191 as there are artificial parms in FN. */
1193 tree
1194 skip_artificial_parms_for (const_tree fn, tree list)
1196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1197 list = TREE_CHAIN (list);
1198 else
1199 return list;
1201 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1202 list = TREE_CHAIN (list);
1203 if (DECL_HAS_VTT_PARM_P (fn))
1204 list = TREE_CHAIN (list);
1205 return list;
1208 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1209 artificial parms in FN. */
1212 num_artificial_parms_for (const_tree fn)
1214 int count = 0;
1216 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1217 count++;
1218 else
1219 return 0;
1221 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1222 count++;
1223 if (DECL_HAS_VTT_PARM_P (fn))
1224 count++;
1225 return count;
1229 #include "gt-cp-method.h"