]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/method.c
java-gimplify.c (java_gimplify_block): New argument to build_empty_stmt.
[thirdparty/gcc.git] / gcc / cp / method.c
CommitLineData
8d08fdba
MS
1/* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
c8094d83 3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
c166b898 4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
e77f031d 5 Free Software Foundation, Inc.
8d08fdba
MS
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
7
f5adbb8d 8This file is part of GCC.
c8094d83 9
f5adbb8d 10GCC is free software; you can redistribute it and/or modify
8d08fdba 11it under the terms of the GNU General Public License as published by
e77f031d 12the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
13any later version.
14
f5adbb8d 15GCC is distributed in the hope that it will be useful,
8d08fdba
MS
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
e77f031d
NC
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
8d08fdba
MS
23
24
8d08fdba 25/* Handle method declarations. */
8d08fdba 26#include "config.h"
e817b5e3 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
8d08fdba
MS
30#include "tree.h"
31#include "cp-tree.h"
8926095f
MS
32#include "rtl.h"
33#include "expr.h"
34#include "output.h"
8926095f 35#include "flags.h"
54f92bfb 36#include "toplev.h"
b1afd7f4 37#include "tm_p.h"
483ab821 38#include "target.h"
e21aff8a 39#include "tree-pass.h"
3e3935a9 40#include "diagnostic.h"
ff2c88a5 41#include "cgraph.h"
8d08fdba 42
669ec2b4
JM
43/* Various flags to control the mangling process. */
44
45enum mangling_flags
46{
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. */
de94b46c 56 mf_use_underscores_around_value = 2
669ec2b4
JM
57};
58
59typedef enum mangling_flags mangling_flags;
60
4977bab6
ZW
61static tree thunk_adjust (tree, bool, HOST_WIDE_INT, tree);
62static void do_build_assign_ref (tree);
63static void do_build_copy_constructor (tree);
64static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
d46c570d 65static tree make_alias_for_thunk (tree);
669ec2b4 66
669ec2b4
JM
67/* Called once to initialize method.c. */
68
69void
4977bab6 70init_method (void)
669ec2b4 71{
1f84ec23 72 init_mangle ();
669ec2b4 73}
8926095f 74\f
4977bab6
ZW
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. */
1f6e1acc 83
8926095f 84tree
4977bab6
ZW
85make_thunk (tree function, bool this_adjusting,
86 tree fixed_offset, tree virtual_offset)
8926095f 87{
31f8e4f3 88 HOST_WIDE_INT d;
4977bab6 89 tree thunk;
c8094d83 90
50bc768d 91 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
9bcb9aae 92 /* We can have this thunks to covariant thunks, but not vice versa. */
50bc768d
NS
93 gcc_assert (!DECL_THIS_THUNK_P (function));
94 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
c8094d83 95
4977bab6
ZW
96 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
97 if (this_adjusting && virtual_offset)
c8094d83 98 virtual_offset
31f8e4f3 99 = size_binop (MULT_EXPR,
0cbd7506
MS
100 virtual_offset,
101 convert (ssizetype,
102 TYPE_SIZE_UNIT (vtable_entry_type)));
c8094d83 103
4977bab6 104 d = tree_low_cst (fixed_offset, 0);
c8094d83 105
4977bab6
ZW
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
9bcb9aae 108 will be a BINFO. */
bb5e8a7f 109 for (thunk = DECL_THUNKS (function); thunk; thunk = TREE_CHAIN (thunk))
e00853fd
NS
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;
c8094d83 119
bb5e8a7f
MM
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. */
50bc768d 123 gcc_assert (!TREE_ASM_WRITTEN (function));
90d46c28
NS
124 /* Likewise, we can only be adding thunks to a function declared in
125 the class currently being laid out. */
50bc768d
NS
126 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
127 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
bb5e8a7f 128
c2255bc4
AH
129 thunk = build_decl (DECL_SOURCE_LOCATION (function),
130 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
bb5e8a7f 131 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
07fa4878 132 cxx_dup_lang_specific_decl (thunk);
bb885938 133 DECL_THUNKS (thunk) = NULL_TREE;
c8094d83 134
bb5e8a7f
MM
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);
4977bab6 139 SET_DECL_THUNK_P (thunk, this_adjusting);
07fa4878
NS
140 THUNK_TARGET (thunk) = function;
141 THUNK_FIXED_OFFSET (thunk) = d;
4977bab6 142 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
e00853fd 143 THUNK_ALIAS (thunk) = NULL_TREE;
c8094d83 144
bb5e8a7f
MM
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;
bb5e8a7f
MM
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;
eab5474f 159 DECL_DECLARED_INLINE_P (thunk) = 0;
bb5e8a7f
MM
160 /* Nor has it been deferred. */
161 DECL_DEFERRED_FN (thunk) = 0;
cf5131b4
JM
162 /* Nor is it a template instantiation. */
163 DECL_USE_TEMPLATE (thunk) = 0;
164 DECL_TEMPLATE_INFO (thunk) = NULL;
c8094d83 165
bb5e8a7f
MM
166 /* Add it to the list of thunks associated with FUNCTION. */
167 TREE_CHAIN (thunk) = DECL_THUNKS (function);
168 DECL_THUNKS (function) = thunk;
cc600f33 169
8926095f
MS
170 return thunk;
171}
172
07fa4878 173/* Finish THUNK, a thunk decl. */
eb448459 174
8926095f 175void
07fa4878 176finish_thunk (tree thunk)
4977bab6
ZW
177{
178 tree function, name;
ce552f75 179 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
07fa4878
NS
180 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
181
50bc768d 182 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
07fa4878
NS
183 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
184 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
185 function = THUNK_TARGET (thunk);
4977bab6 186 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
07fa4878 187 fixed_offset, virtual_offset);
bb885938
NS
188
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))
195 {
196 tree cov_probe;
197
198 for (cov_probe = DECL_THUNKS (function);
199 cov_probe; cov_probe = TREE_CHAIN (cov_probe))
200 if (DECL_NAME (cov_probe) == name)
201 {
50bc768d 202 gcc_assert (!DECL_THUNKS (thunk));
e00853fd 203 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
bb885938
NS
204 ? THUNK_ALIAS (cov_probe) : cov_probe);
205 break;
206 }
207 }
c8094d83 208
4977bab6
ZW
209 DECL_NAME (thunk) = name;
210 SET_DECL_ASSEMBLER_NAME (thunk, name);
211}
212
213/* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
214 offset indicated by VIRTUAL_OFFSET, if that is
4de8668e 215 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
9bcb9aae 216 zero for a result adjusting thunk. */
4977bab6
ZW
217
218static tree
219thunk_adjust (tree ptr, bool this_adjusting,
220 HOST_WIDE_INT fixed_offset, tree virtual_offset)
221{
222 if (this_adjusting)
223 /* Adjust the pointer by the constant. */
5be014d5
AP
224 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
225 size_int (fixed_offset));
4977bab6
ZW
226
227 /* If there's a virtual offset, look up that value in the vtable and
228 adjust the pointer again. */
229 if (virtual_offset)
230 {
231 tree vtable;
232
4977bab6
ZW
233 ptr = save_expr (ptr);
234 /* The vptr is always at offset zero in the object. */
235 vtable = build1 (NOP_EXPR,
c8094d83 236 build_pointer_type (build_pointer_type
4977bab6
ZW
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. */
5be014d5
AP
242 vtable = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtable), vtable,
243 fold_convert (sizetype, virtual_offset));
4977bab6
ZW
244 /* Get the offset itself. */
245 vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
246 /* Adjust the `this' pointer. */
5be014d5
AP
247 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
248 fold_convert (sizetype, vtable));
4977bab6 249 }
c8094d83 250
4977bab6
ZW
251 if (!this_adjusting)
252 /* Adjust the pointer by the constant. */
5be014d5
AP
253 ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
254 size_int (fixed_offset));
4977bab6
ZW
255
256 return ptr;
257}
258
89ce1c8f
JJ
259static GTY (()) int thunk_labelno;
260
261/* Create a static alias to function. */
262
6de33afa
RH
263tree
264make_alias_for (tree function, tree newid)
89ce1c8f 265{
c2255bc4
AH
266 tree alias = build_decl (DECL_SOURCE_LOCATION (function),
267 FUNCTION_DECL, newid, TREE_TYPE (function));
89ce1c8f
JJ
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;
89ce1c8f
JJ
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;
6de33afa
RH
295 return alias;
296}
297
298static tree
299make_alias_for_thunk (tree function)
300{
301 tree alias;
302 char buf[256];
303
304 ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
305 thunk_labelno++;
306
307 alias = make_alias_for (function, get_identifier (buf));
308
89ce1c8f
JJ
309 if (!flag_syntax_only)
310 assemble_alias (alias, DECL_ASSEMBLER_NAME (function));
6de33afa 311
89ce1c8f
JJ
312 return alias;
313}
314
4977bab6
ZW
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. */
318
319void
320use_thunk (tree thunk_fndecl, bool emit_p)
8926095f 321{
7a3e01c4 322 tree a, t, function, alias;
4977bab6
ZW
323 tree virtual_offset;
324 HOST_WIDE_INT fixed_offset, virtual_value;
07fa4878 325 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
4977bab6 326
9bcb9aae 327 /* We should have called finish_thunk to give it a name. */
50bc768d 328 gcc_assert (DECL_NAME (thunk_fndecl));
8926095f 329
bb885938
NS
330 /* We should never be using an alias, always refer to the
331 aliased thunk. */
50bc768d 332 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
bb885938 333
8926095f
MS
334 if (TREE_ASM_WRITTEN (thunk_fndecl))
335 return;
c8094d83 336
07fa4878
NS
337 function = THUNK_TARGET (thunk_fndecl);
338 if (DECL_RESULT (thunk_fndecl))
6462c441 339 /* We already turned this thunk into an ordinary function.
dff94ad7 340 There's no need to process this thunk again. */
6462c441
MM
341 return;
342
742f25b3
NS
343 if (DECL_THUNK_P (function))
344 /* The target is itself a thunk, process it now. */
345 use_thunk (function, emit_p);
c8094d83 346
31f8e4f3
MM
347 /* Thunks are always addressable; they only appear in vtables. */
348 TREE_ADDRESSABLE (thunk_fndecl) = 1;
a0a33927 349
31f8e4f3
MM
350 /* Figure out what function is being thunked to. It's referenced in
351 this translation unit. */
809c8c30
JM
352 TREE_ADDRESSABLE (function) = 1;
353 mark_used (function);
31f8e4f3
MM
354 if (!emit_p)
355 return;
809c8c30 356
4a77e08c
DS
357 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
358 alias = make_alias_for_thunk (function);
359 else
360 alias = function;
89ce1c8f 361
4977bab6
ZW
362 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
363 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
3961e8fe 364
07fa4878
NS
365 if (virtual_offset)
366 {
367 if (!this_adjusting)
368 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
369 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
50bc768d 370 gcc_assert (virtual_value);
07fa4878
NS
371 }
372 else
373 virtual_value = 0;
c8094d83 374
31f8e4f3
MM
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;
15eb1e43
JM
379 /* The linkage of the function may have changed. FIXME in linkage
380 rewrite. */
381 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
968b41a1 382 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
c8094d83 383 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
73a8adb6 384 = DECL_VISIBILITY_SPECIFIED (function);
bf2f234e
NS
385 if (DECL_ONE_ONLY (function))
386 make_decl_one_only (thunk_fndecl);
a0128b67 387
6462c441
MM
388 if (flag_syntax_only)
389 {
390 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
391 return;
392 }
393
31f8e4f3
MM
394 push_to_top_level ();
395
4a77e08c
DS
396 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
397 && targetm.have_named_sections)
89ce1c8f
JJ
398 {
399 resolve_unique_section (function, 0, flag_function_sections);
400
401 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
402 {
403 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
404
405 /* Output the thunk into the same section as function. */
406 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
407 }
408 }
89ce1c8f 409
7a3e01c4
JDA
410 /* Set up cloned argument trees for the thunk. */
411 t = NULL_TREE;
412 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
413 {
414 tree x = copy_node (a);
415 TREE_CHAIN (x) = t;
416 DECL_CONTEXT (x) = thunk_fndecl;
417 SET_DECL_RTL (x, NULL_RTX);
dbb87a8a 418 DECL_HAS_VALUE_EXPR_P (x) = 0;
7a3e01c4
JDA
419 t = x;
420 }
421 a = nreverse (t);
422 DECL_ARGUMENTS (thunk_fndecl) = a;
c8094d83 423
07fa4878 424 if (this_adjusting
4977bab6 425 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
89ce1c8f 426 virtual_value, alias))
3b62f224 427 {
3cce094d 428 const char *fnname;
72c4a4ca
GK
429 tree fn_block;
430
3b62f224 431 current_function_decl = thunk_fndecl;
3b62f224 432 DECL_RESULT (thunk_fndecl)
c2255bc4
AH
433 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
434 RESULT_DECL, 0, integer_type_node);
af34b82f 435 fnname = IDENTIFIER_POINTER (DECL_NAME (thunk_fndecl));
3b426391 436 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
72c4a4ca
GK
437 create one. */
438 fn_block = make_node (BLOCK);
439 BLOCK_VARS (fn_block) = a;
440 DECL_INITIAL (thunk_fndecl) = fn_block;
ee6b0296 441 init_function_start (thunk_fndecl);
3c072c6b 442 cfun->is_thunk = 1;
3b62f224 443 assemble_start_function (thunk_fndecl, fnname);
eb0424da 444
4977bab6 445 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
89ce1c8f 446 fixed_offset, virtual_value, alias);
3961e8fe 447
3b62f224 448 assemble_end_function (thunk_fndecl, fnname);
8402b3e1 449 init_insn_lengths ();
3b62f224 450 current_function_decl = 0;
db2960f4 451 set_cfun (NULL);
6462c441 452 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
3b62f224 453 }
4e7512c9 454 else
14691f8d 455 {
94a0dd7b
SL
456 int i;
457 tree *argarray = (tree *) alloca (list_length (a) * sizeof (tree));
4977bab6
ZW
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. */
14691f8d 462
14691f8d 463 if (varargs_function_p (function))
2a13a625 464 error ("generic thunk code fails for method %q#D which uses %<...%>",
14691f8d
RH
465 function);
466
14691f8d
RH
467 DECL_RESULT (thunk_fndecl) = NULL_TREE;
468
058b15c1 469 start_preparsed_function (thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
14691f8d
RH
470 /* We don't bother with a body block for thunks. */
471
1bb2cc34 472 /* There's no need to check accessibility inside the thunk body. */
78757caa 473 push_deferring_access_checks (dk_no_check);
1bb2cc34 474
4977bab6 475 t = a;
07fa4878 476 if (this_adjusting)
4977bab6
ZW
477 t = thunk_adjust (t, /*this_adjusting=*/1,
478 fixed_offset, virtual_offset);
c8094d83 479
14691f8d 480 /* Build up the call to the real function. */
94a0dd7b
SL
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);
dd292d0a 485 CALL_FROM_THUNK_P (t) = 1;
62cbbe84 486 CALL_CANNOT_INLINE_P (t) = 1;
c8094d83 487
14691f8d
RH
488 if (VOID_TYPE_P (TREE_TYPE (t)))
489 finish_expr_stmt (t);
490 else
59445d74 491 {
59445d74 492 if (!this_adjusting)
38a37714
NS
493 {
494 tree cond = NULL_TREE;
495
496 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
497 {
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);
504 }
c8094d83 505
38a37714
NS
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));
511 }
9e1e64ec 512 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
831d8bd7 513 t = build_cplus_new (TREE_TYPE (t), t);
59445d74
RH
514 finish_return_stmt (t);
515 }
14691f8d
RH
516
517 /* Since we want to emit the thunk, we explicitly mark its name as
518 referenced. */
bb9a388d 519 mark_decl_referenced (thunk_fndecl);
14691f8d
RH
520
521 /* But we don't want debugging information about it. */
522 DECL_IGNORED_P (thunk_fndecl) = 1;
523
1bb2cc34 524 /* Re-enable access control. */
78757caa 525 pop_deferring_access_checks ();
1bb2cc34 526
e21aff8a 527 thunk_fndecl = finish_function (0);
ff2c88a5 528 cgraph_add_new_function (thunk_fndecl, false);
14691f8d 529 }
31f8e4f3
MM
530
531 pop_from_top_level ();
8926095f 532}
f376e137
MS
533\f
534/* Code for synthesizing methods which have default semantics defined. */
535
f376e137 536/* Generate code for default X(X&) constructor. */
e92cc029 537
824b9a4c 538static void
4977bab6 539do_build_copy_constructor (tree fndecl)
f376e137 540{
e0fff4b3 541 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
f376e137 542
f376e137
MS
543 parm = convert_from_reference (parm);
544
a59ca936
JM
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))
f376e137 550 {
fd749a60 551 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
f1dedc31 552 finish_expr_stmt (t);
f376e137
MS
553 }
554 else
555 {
556 tree fields = TYPE_FIELDS (current_class_type);
fd74ca0b 557 tree member_init_list = NULL_TREE;
89d684bb 558 int cvquals = cp_type_quals (TREE_TYPE (parm));
f376e137 559 int i;
fa743e8c 560 tree binfo, base_binfo;
d4e6fecb 561 VEC(tree,gc) *vbases;
f376e137 562
713ccd0c
NS
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. */
9ba5ff0f
NS
568 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
569 VEC_iterate (tree, vbases, i, binfo); i++)
01f9e964 570 {
c8094d83 571 member_init_list
2282d28d
MM
572 = tree_cons (binfo,
573 build_tree_list (NULL_TREE,
574 build_base_path (PLUS_EXPR, parm,
575 binfo, 1)),
576 member_init_list);
01f9e964
JM
577 }
578
fa743e8c
NS
579 for (binfo = TYPE_BINFO (current_class_type), i = 0;
580 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
f376e137 581 {
fa743e8c 582 if (BINFO_VIRTUAL_P (base_binfo))
c8094d83 583 continue;
f376e137 584
c8094d83 585 member_init_list
fa743e8c 586 = tree_cons (base_binfo,
2282d28d
MM
587 build_tree_list (NULL_TREE,
588 build_base_path (PLUS_EXPR, parm,
fa743e8c 589 base_binfo, 1)),
2282d28d 590 member_init_list);
f376e137 591 }
1b5f5f76 592
f376e137
MS
593 for (; fields; fields = TREE_CHAIN (fields))
594 {
fd749a60 595 tree init = parm;
a5894242 596 tree field = fields;
33dd07ee 597 tree expr_type;
a5894242
MS
598
599 if (TREE_CODE (field) != FIELD_DECL)
f376e137 600 continue;
8dff1027 601
fd749a60 602 expr_type = TREE_TYPE (field);
a5894242 603 if (DECL_NAME (field))
f376e137 604 {
a5894242 605 if (VFIELD_NAME_P (DECL_NAME (field)))
f376e137 606 continue;
f376e137 607 }
fd749a60 608 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
6bdb8141
JM
609 /* Just use the field; anonymous types can't have
610 nontrivial copy ctors or assignment ops. */;
0171aeab
JM
611 else
612 continue;
f376e137 613
33dd07ee
MM
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.) */
33dd07ee 619 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
fd749a60
NS
620 {
621 int quals = cvquals;
c8094d83 622
fd749a60
NS
623 if (DECL_MUTABLE_P (field))
624 quals &= ~TYPE_QUAL_CONST;
625 expr_type = cp_build_qualified_type (expr_type, quals);
626 }
c8094d83 627
f293ce4b 628 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
f376e137
MS
629 init = build_tree_list (NULL_TREE, init);
630
fd749a60 631 member_init_list = tree_cons (field, init, member_init_list);
f376e137 632 }
2282d28d 633 finish_mem_initializers (member_init_list);
f376e137 634 }
f376e137
MS
635}
636
824b9a4c 637static void
4977bab6 638do_build_assign_ref (tree fndecl)
f376e137
MS
639{
640 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
f1dedc31 641 tree compound_stmt;
f376e137 642
325c3691 643 compound_stmt = begin_compound_stmt (0);
f376e137
MS
644 parm = convert_from_reference (parm);
645
a59ca936
JM
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))
f376e137 651 {
f293ce4b 652 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
f1dedc31 653 finish_expr_stmt (t);
f376e137
MS
654 }
655 else
656 {
4ba126e4 657 tree fields;
89d684bb 658 int cvquals = cp_type_quals (TREE_TYPE (parm));
f376e137 659 int i;
fa743e8c 660 tree binfo, base_binfo;
f376e137 661
22ed7e5f 662 /* Assign to each of the direct base classes. */
fa743e8c
NS
663 for (binfo = TYPE_BINFO (current_class_type), i = 0;
664 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
f376e137 665 {
4ba126e4 666 tree converted_parm;
c166b898 667 VEC(tree,gc) *parmvec;
4ba126e4 668
4ba126e4
MM
669 /* We must convert PARM directly to the base class
670 explicitly since the base class may be ambiguous. */
fa743e8c 671 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
4ba126e4 672 /* Call the base class assignment operator. */
c166b898 673 parmvec = make_tree_vector_single (converted_parm);
c8094d83
MS
674 finish_expr_stmt
675 (build_special_member_call (current_class_ref,
4ba126e4 676 ansi_assopname (NOP_EXPR),
c166b898 677 &parmvec,
fa743e8c 678 base_binfo,
5ade1ed2
DG
679 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
680 tf_warning_or_error));
c166b898 681 release_tree_vector (parmvec);
f376e137 682 }
4ba126e4
MM
683
684 /* Assign to each of the non-static data members. */
c8094d83
MS
685 for (fields = TYPE_FIELDS (current_class_type);
686 fields;
4ba126e4 687 fields = TREE_CHAIN (fields))
f376e137 688 {
fd749a60
NS
689 tree comp = current_class_ref;
690 tree init = parm;
a5894242 691 tree field = fields;
fd749a60
NS
692 tree expr_type;
693 int quals;
a5894242 694
17bbb839 695 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
f376e137 696 continue;
e349ee73 697
fd749a60 698 expr_type = TREE_TYPE (field);
c8094d83 699
fd749a60 700 if (CP_TYPE_CONST_P (expr_type))
e349ee73 701 {
0cbd7506
MS
702 error ("non-static const member %q#D, can't use default "
703 "assignment operator", field);
e349ee73
MS
704 continue;
705 }
fd749a60 706 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
e349ee73 707 {
2a13a625 708 error ("non-static reference member %q#D, can't use "
0cbd7506 709 "default assignment operator", field);
e349ee73
MS
710 continue;
711 }
712
a5894242 713 if (DECL_NAME (field))
f376e137 714 {
a5894242 715 if (VFIELD_NAME_P (DECL_NAME (field)))
f376e137 716 continue;
f376e137 717 }
fd749a60
NS
718 else if (ANON_AGGR_TYPE_P (expr_type)
719 && TYPE_FIELDS (expr_type) != NULL_TREE)
6bdb8141
JM
720 /* Just use the field; anonymous types can't have
721 nontrivial copy ctors or assignment ops. */;
0171aeab
JM
722 else
723 continue;
f376e137 724
fd749a60 725 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
c8094d83 726
fd749a60
NS
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);
c8094d83 732
fd749a60 733 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
f376e137 734
a1c2b86d 735 if (DECL_NAME (field))
5ade1ed2
DG
736 init = cp_build_modify_expr (comp, NOP_EXPR, init,
737 tf_warning_or_error);
a1c2b86d 738 else
fd749a60
NS
739 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
740 finish_expr_stmt (init);
f376e137
MS
741 }
742 }
62409b39 743 finish_return_stmt (current_class_ref);
7a3397c7 744 finish_compound_stmt (compound_stmt);
f376e137
MS
745}
746
3e3935a9
NS
747/* Synthesize FNDECL, a non-static member function. */
748
f376e137 749void
4977bab6 750synthesize_method (tree fndecl)
f376e137 751{
4977bab6 752 bool nested = (current_function_decl != NULL_TREE);
4f1c5b7d 753 tree context = decl_function_context (fndecl);
4977bab6 754 bool need_body = true;
ade3dc07 755 tree stmt;
39a87435 756 location_t save_input_location = input_location;
3e3935a9
NS
757 int error_count = errorcount;
758 int warning_count = warningcount;
db5ae43f 759
3e3935a9
NS
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)));
c8094d83 764
db9b2174
MM
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))
3e3935a9 769 fndecl = DECL_CLONED_FUNCTION (fndecl);
db9b2174 770
afb19ffb
KL
771 /* We may be in the middle of deferred access check. Disable
772 it now. */
773 push_deferring_access_checks (dk_no_deferred);
774
9a3b49ac
MS
775 if (! context)
776 push_to_top_level ();
777 else if (nested)
d2784db4 778 push_function_context ();
db5ae43f 779
39a87435 780 input_location = DECL_SOURCE_LOCATION (fndecl);
62409b39 781
058b15c1 782 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
ade3dc07 783 stmt = begin_function_body ();
db5ae43f 784
596ea4e5 785 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
62409b39
MM
786 {
787 do_build_assign_ref (fndecl);
4977bab6 788 need_body = false;
62409b39 789 }
cdd2559c 790 else if (DECL_CONSTRUCTOR_P (fndecl))
db5ae43f 791 {
e0fff4b3 792 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
db5ae43f
MS
793 if (arg_chain != void_list_node)
794 do_build_copy_constructor (fndecl);
c7b0e027 795 else
cdd2559c 796 finish_mem_initializers (NULL_TREE);
62409b39 797 }
f18a14bc 798
62409b39
MM
799 /* If we haven't yet generated the body of the function, just
800 generate an empty compound statement. */
801 if (need_body)
802 {
803 tree compound_stmt;
325c3691 804 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7a3397c7 805 finish_compound_stmt (compound_stmt);
db5ae43f
MS
806 }
807
ade3dc07 808 finish_function_body (stmt);
8cd2462c 809 expand_or_defer_fn (finish_function (0));
28cbf42c 810
39a87435
AO
811 input_location = save_input_location;
812
9a3b49ac
MS
813 if (! context)
814 pop_from_top_level ();
815 else if (nested)
d2784db4 816 pop_function_context ();
afb19ffb
KL
817
818 pop_deferring_access_checks ();
3e3935a9
NS
819
820 if (error_count != errorcount || warning_count != warningcount)
1f5b3869
MLI
821 inform (input_location, "synthesized method %qD first required here ",
822 fndecl);
f376e137 823}
9eb71d8c 824
03378143
NS
825/* Use EXTRACTOR to locate the relevant function called for each base &
826 class field of TYPE. CLIENT allows additional information to be passed
f62ea157
JM
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. */
03378143
NS
830
831static tree
4977bab6 832synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
0cbd7506 833 void *client)
03378143
NS
834{
835 tree raises = empty_except_spec;
836 tree fields = TYPE_FIELDS (type);
fa743e8c
NS
837 tree binfo, base_binfo;
838 int i;
f62ea157 839
fa743e8c
NS
840 for (binfo = TYPE_BINFO (type), i = 0;
841 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
03378143 842 {
fa743e8c 843 tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
03378143 844 if (fn)
0cbd7506
MS
845 {
846 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
c8094d83 847
0cbd7506
MS
848 raises = merge_exception_specifiers (raises, fn_raises);
849 }
03378143
NS
850 }
851 for (; fields; fields = TREE_CHAIN (fields))
852 {
853 tree type = TREE_TYPE (fields);
854 tree fn;
c8094d83 855
17bbb839 856 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
0cbd7506 857 continue;
03378143 858 while (TREE_CODE (type) == ARRAY_TYPE)
0cbd7506 859 type = TREE_TYPE (type);
6276e725 860 if (!CLASS_TYPE_P (type))
0cbd7506 861 continue;
c8094d83 862
03378143
NS
863 fn = (*extractor) (type, client);
864 if (fn)
0cbd7506
MS
865 {
866 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
c8094d83 867
0cbd7506
MS
868 raises = merge_exception_specifiers (raises, fn_raises);
869 }
03378143
NS
870 }
871 return raises;
872}
873
874/* Locate the dtor of TYPE. */
875
cb68ec50 876tree
4977bab6 877locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
03378143 878{
9f4faeae 879 return CLASSTYPE_DESTRUCTORS (type);
03378143
NS
880}
881
882/* Locate the default ctor of TYPE. */
883
cb68ec50 884tree
4977bab6 885locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
03378143
NS
886{
887 tree fns;
c8094d83 888
03378143
NS
889 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
890 return NULL_TREE;
aaaa46d2 891
508a1c9c
MM
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);
896
aaaa46d2 897 for (fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
03378143
NS
898 {
899 tree fn = OVL_CURRENT (fns);
900 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
c8094d83 901
63752e29
JM
902 parms = skip_artificial_parms_for (fn, parms);
903
904 if (sufficient_parms_p (parms))
0cbd7506 905 return fn;
03378143 906 }
6276e725 907 gcc_unreachable ();
03378143
NS
908}
909
910struct copy_data
911{
912 tree name;
913 int quals;
914};
915
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. */
919
cb68ec50 920tree
4977bab6 921locate_copy (tree type, void *client_)
03378143
NS
922{
923 struct copy_data *client = (struct copy_data *)client_;
924 tree fns;
03378143 925 tree best = NULL_TREE;
4977bab6 926 bool excess_p = false;
c8094d83 927
03378143
NS
928 if (client->name)
929 {
508a1c9c
MM
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);
03378143
NS
935 }
936 else if (TYPE_HAS_INIT_REF (type))
508a1c9c
MM
937 {
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);
943 }
944 else
03378143 945 return NULL_TREE;
03378143
NS
946 for (; fns; fns = OVL_NEXT (fns))
947 {
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;
c8094d83 953
6276e725 954 parms = skip_artificial_parms_for (fn, parms);
03378143 955 if (!parms)
0cbd7506 956 continue;
ee76b931 957 src_type = non_reference (TREE_VALUE (parms));
492b73bd
LM
958
959 if (src_type == error_mark_node)
960 return NULL_TREE;
961
03378143 962 if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
0cbd7506 963 continue;
03378143 964 if (!sufficient_parms_p (TREE_CHAIN (parms)))
0cbd7506 965 continue;
89d684bb 966 quals = cp_type_quals (src_type);
03378143 967 if (client->quals & ~quals)
0cbd7506 968 continue;
03378143
NS
969 excess = quals & ~client->quals;
970 if (!best || (excess_p && !excess))
0cbd7506
MS
971 {
972 best = fn;
973 excess_p = excess;
974 }
03378143 975 else
0cbd7506
MS
976 /* Ambiguous */
977 return NULL_TREE;
03378143
NS
978 }
979 return best;
980}
981
9eb71d8c
MM
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
27d6592c
MM
985 reference argument or a non-const reference. Returns the
986 FUNCTION_DECL for the implicitly declared function. */
9eb71d8c 987
993acaec 988static tree
4977bab6 989implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
9eb71d8c 990{
058b15c1
MM
991 tree fn;
992 tree parameter_types = void_list_node;
44d10c10 993 tree return_type;
058b15c1 994 tree fn_type;
03378143 995 tree raises = empty_except_spec;
058b15c1 996 tree rhs_parm_type = NULL_TREE;
e2537f2c 997 tree this_parm;
058b15c1 998 tree name;
64b2bdb3
MM
999 HOST_WIDE_INT saved_processing_template_decl;
1000
b2b800a0 1001 /* Because we create declarations for implicitly declared functions
64b2bdb3
MM
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;
9eb71d8c 1012
508a1c9c
MM
1013 type = TYPE_MAIN_VARIANT (type);
1014
44d10c10
PB
1015 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1016 {
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);
1022 }
1023 else
1024 return_type = void_type_node;
1025
9eb71d8c
MM
1026 switch (kind)
1027 {
9eb71d8c 1028 case sfk_destructor:
03378143 1029 /* Destructor. */
058b15c1 1030 name = constructor_name (type);
03378143 1031 raises = synthesize_exception_spec (type, &locate_dtor, 0);
9eb71d8c
MM
1032 break;
1033
1034 case sfk_constructor:
1035 /* Default constructor. */
058b15c1 1036 name = constructor_name (type);
03378143 1037 raises = synthesize_exception_spec (type, &locate_ctor, 0);
9eb71d8c
MM
1038 break;
1039
1040 case sfk_copy_constructor:
9eb71d8c 1041 case sfk_assignment_operator:
03378143
NS
1042 {
1043 struct copy_data data;
c8094d83 1044
a2095778
NS
1045 data.name = NULL;
1046 data.quals = 0;
1047 if (kind == sfk_assignment_operator)
0cbd7506 1048 {
058b15c1 1049 return_type = build_reference_type (type);
0cbd7506
MS
1050 name = ansi_assopname (NOP_EXPR);
1051 data.name = name;
1052 }
058b15c1
MM
1053 else
1054 name = constructor_name (type);
1055
9eb71d8c 1056 if (const_p)
0cbd7506
MS
1057 {
1058 data.quals = TYPE_QUAL_CONST;
058b15c1 1059 rhs_parm_type = build_qualified_type (type, TYPE_QUAL_CONST);
0cbd7506 1060 }
058b15c1
MM
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);
03378143 1065 raises = synthesize_exception_spec (type, &locate_copy, &data);
9eb71d8c 1066 break;
03378143 1067 }
9eb71d8c 1068 default:
8dc2b103 1069 gcc_unreachable ();
9eb71d8c
MM
1070 }
1071
058b15c1
MM
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);
aaaa46d2 1077 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
058b15c1
MM
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
1083 {
1084 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1085 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1086 }
b21a6ea1
NS
1087
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;
1094
e2537f2c 1095 /* Create the explicit arguments. */
058b15c1
MM
1096 if (rhs_parm_type)
1097 {
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;
1103 }
3db45ab5 1104 /* Add the "this" parameter. */
e2537f2c
MM
1105 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1106 TREE_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1107 DECL_ARGUMENTS (fn) = this_parm;
9eb71d8c 1108
e2537f2c 1109 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
4684cd27 1110 set_linkage_according_to_type (type, fn);
0e6df31e 1111 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
058b15c1 1112 DECL_IN_AGGR_P (fn) = 1;
c727aa5e 1113 DECL_ARTIFICIAL (fn) = 1;
b87d79e6 1114 DECL_DEFAULTED_FN (fn) = 1;
9eb71d8c 1115 DECL_NOT_REALLY_EXTERN (fn) = 1;
79065db2 1116 DECL_DECLARED_INLINE_P (fn) = 1;
8dc2b103 1117 gcc_assert (!TREE_USED (fn));
9f4faeae 1118
64b2bdb3
MM
1119 /* Restore PROCESSING_TEMPLATE_DECL. */
1120 processing_template_decl = saved_processing_template_decl;
1121
9eb71d8c
MM
1122 return fn;
1123}
e0fff4b3 1124
508a1c9c
MM
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. */
1128
1129tree
1130lazily_declare_fn (special_function_kind sfk, tree type)
1131{
1132 tree fn;
1133 bool const_p;
1134
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);
9f4faeae
MM
1146 /* A destructor may be virtual. */
1147 if (sfk == sfk_destructor)
1148 check_for_override (fn, type);
508a1c9c 1149 /* Add it to CLASSTYPE_METHOD_VEC. */
b2a9b208 1150 add_method (type, fn, NULL_TREE);
508a1c9c 1151 /* Add it to TYPE_METHODS. */
c8094d83 1152 if (sfk == sfk_destructor
9f4faeae
MM
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
1159 {
1160 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1161 TYPE_METHODS list, which cause the destructor to be emitted
c8094d83 1162 in an incorrect location in the vtable. */
9f4faeae 1163 if (warn_abi && DECL_VIRTUAL_P (fn))
b323323f 1164 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
9f4faeae
MM
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;
1170 }
508a1c9c 1171 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
9f4faeae
MM
1172 if (sfk == sfk_assignment_operator)
1173 CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
1174 else
508a1c9c
MM
1175 {
1176 /* Remember that the function has been created. */
1177 if (sfk == sfk_constructor)
1178 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
9f4faeae 1179 else if (sfk == sfk_copy_constructor)
508a1c9c 1180 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
9f4faeae
MM
1181 else if (sfk == sfk_destructor)
1182 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
508a1c9c
MM
1183 /* Create appropriate clones. */
1184 clone_function_decl (fn, /*update_method_vec=*/true);
1185 }
1186
1187 return fn;
1188}
1189
e0fff4b3
JM
1190/* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1191 as there are artificial parms in FN. */
1192
1193tree
58f9752a 1194skip_artificial_parms_for (const_tree fn, tree list)
e0fff4b3
JM
1195{
1196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1197 list = TREE_CHAIN (list);
1198 else
1199 return list;
1200
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;
1206}
89ce1c8f 1207
94a0dd7b
SL
1208/* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1209 artificial parms in FN. */
1210
1211int
58f9752a 1212num_artificial_parms_for (const_tree fn)
94a0dd7b
SL
1213{
1214 int count = 0;
1215
1216 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1217 count++;
1218 else
1219 return 0;
1220
1221 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1222 count++;
1223 if (DECL_HAS_VTT_PARM_P (fn))
1224 count++;
1225 return count;
1226}
1227
1228
89ce1c8f 1229#include "gt-cp-method.h"