Merge from pch-branch.
[gcc.git] / gcc / cp / method.c
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 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24
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
40 /* Various flags to control the mangling process. */
41
42 enum mangling_flags
43 {
44 /* No flags. */
45 mf_none = 0,
46 /* The thing we are presently mangling is part of a template type,
47 rather than a fully instantiated type. Therefore, we may see
48 complex expressions where we would normally expect to see a
49 simple integer constant. */
50 mf_maybe_uninstantiated = 1,
51 /* When mangling a numeric value, use the form `_XX_' (instead of
52 just `XX') if the value has more than one digit. */
53 mf_use_underscores_around_value = 2,
54 };
55
56 typedef enum mangling_flags mangling_flags;
57
58 static tree thunk_adjust (tree, bool, HOST_WIDE_INT, tree);
59 static void do_build_assign_ref (tree);
60 static void do_build_copy_constructor (tree);
61 static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
62 static tree locate_dtor (tree, void *);
63 static tree locate_ctor (tree, void *);
64 static tree locate_copy (tree, void *);
65
66 /* Called once to initialize method.c. */
67
68 void
69 init_method (void)
70 {
71 init_mangle ();
72 }
73
74 \f
75 /* Set the mangled name (DECL_ASSEMBLER_NAME) for DECL. */
76
77 void
78 set_mangled_name_for_decl (tree decl)
79 {
80 if (processing_template_decl)
81 /* There's no need to mangle the name of a template function. */
82 return;
83
84 mangle_decl (decl);
85 }
86
87 \f
88 /* Given a tree_code CODE, and some arguments (at least one),
89 attempt to use an overloaded operator on the arguments.
90
91 For unary operators, only the first argument need be checked.
92 For binary operators, both arguments may need to be checked.
93
94 Member functions can convert class references to class pointers,
95 for one-level deep indirection. More than that is not supported.
96 Operators [](), ()(), and ->() must be member functions.
97
98 We call function call building calls with LOOKUP_COMPLAIN if they
99 are our only hope. This is true when we see a vanilla operator
100 applied to something of aggregate type. If this fails, we are free
101 to return `error_mark_node', because we will have reported the
102 error.
103
104 Operators NEW and DELETE overload in funny ways: operator new takes
105 a single `size' parameter, and operator delete takes a pointer to the
106 storage being deleted. When overloading these operators, success is
107 assumed. If there is a failure, report an error message and return
108 `error_mark_node'. */
109
110 /* NOSTRICT */
111 tree
112 build_opfncall (enum tree_code code, int flags,
113 tree xarg1, tree xarg2, tree arg3)
114 {
115 return build_new_op (code, flags, xarg1, xarg2, arg3);
116 }
117 \f
118 /* This function takes an identifier, ID, and attempts to figure out what
119 it means. There are a number of possible scenarios, presented in increasing
120 order of hair:
121
122 1) not in a class's scope
123 2) in class's scope, member name of the class's method
124 3) in class's scope, but not a member name of the class
125 4) in class's scope, member name of a class's variable
126
127 NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
128 VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
129
130 As a last ditch, try to look up the name as a label and return that
131 address.
132
133 Values which are declared as being of REFERENCE_TYPE are
134 automatically dereferenced here (as a hack to make the
135 compiler faster). */
136
137 tree
138 hack_identifier (tree value, tree name)
139 {
140 tree type;
141
142 if (value == error_mark_node)
143 return error_mark_node;
144
145 type = TREE_TYPE (value);
146 if (TREE_CODE (value) == FIELD_DECL)
147 value = finish_non_static_data_member (value,
148 /*qualifying_scope=*/NULL_TREE);
149 else if ((TREE_CODE (value) == FUNCTION_DECL
150 && DECL_FUNCTION_MEMBER_P (value))
151 || (TREE_CODE (value) == OVERLOAD
152 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (value))))
153 {
154 tree decl;
155
156 if (TREE_CODE (value) == OVERLOAD)
157 value = OVL_CURRENT (value);
158
159 decl = maybe_dummy_object (DECL_CONTEXT (value), 0);
160 value = finish_class_member_access_expr (decl, name);
161 }
162 else if (really_overloaded_fn (value))
163 ;
164 else if (TREE_CODE (value) == OVERLOAD)
165 /* not really overloaded function */
166 mark_used (OVL_FUNCTION (value));
167 else if (TREE_CODE (value) == TREE_LIST)
168 {
169 /* Ambiguous reference to base members, possibly other cases?. */
170 tree t = value;
171 while (t && TREE_CODE (t) == TREE_LIST)
172 {
173 mark_used (TREE_VALUE (t));
174 t = TREE_CHAIN (t);
175 }
176 }
177 else if (TREE_CODE (value) == NAMESPACE_DECL)
178 {
179 error ("use of namespace `%D' as expression", value);
180 return error_mark_node;
181 }
182 else if (DECL_CLASS_TEMPLATE_P (value))
183 {
184 error ("use of class template `%T' as expression", value);
185 return error_mark_node;
186 }
187 else
188 mark_used (value);
189
190 if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL
191 || TREE_CODE (value) == RESULT_DECL)
192 {
193 tree context = decl_function_context (value);
194 if (context != NULL_TREE && context != current_function_decl
195 && ! TREE_STATIC (value))
196 {
197 error ("use of %s from containing function",
198 (TREE_CODE (value) == VAR_DECL
199 ? "`auto' variable" : "parameter"));
200 cp_error_at (" `%#D' declared here", value);
201 value = error_mark_node;
202 }
203 }
204
205 if (DECL_P (value) && DECL_NONLOCAL (value))
206 {
207 if (DECL_CLASS_SCOPE_P (value)
208 && DECL_CONTEXT (value) != current_class_type)
209 {
210 tree path;
211 path = currently_open_derived_class (DECL_CONTEXT (value));
212 enforce_access (path, value);
213 }
214 }
215 else if (TREE_CODE (value) == TREE_LIST
216 && TREE_TYPE (value) == error_mark_node)
217 {
218 error ("\
219 request for member `%D' is ambiguous in multiple inheritance lattice",
220 name);
221 print_candidates (value);
222 return error_mark_node;
223 }
224
225 if (! processing_template_decl)
226 value = convert_from_reference (value);
227 return value;
228 }
229
230 \f
231 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
232 indicates whether it is a this or result adjusting thunk.
233 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
234 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
235 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
236 adjusting thunks, we scale it to a byte offset. For covariant
237 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
238 the returned thunk with finish_thunk. */
239
240 tree
241 make_thunk (tree function, bool this_adjusting,
242 tree fixed_offset, tree virtual_offset)
243 {
244 HOST_WIDE_INT d;
245 tree thunk;
246
247 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 20021025);
248 /* We can have this thunks to covariant thunks, but not vice versa. */
249 my_friendly_assert (!DECL_THIS_THUNK_P (function), 20021127);
250
251 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
252 if (this_adjusting && virtual_offset)
253 virtual_offset
254 = size_binop (MULT_EXPR,
255 virtual_offset,
256 convert (ssizetype,
257 TYPE_SIZE_UNIT (vtable_entry_type)));
258
259 d = tree_low_cst (fixed_offset, 0);
260
261 /* See if we already have the thunk in question. For this_adjusting
262 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
263 will be a BINFO. */
264 for (thunk = DECL_THUNKS (function); thunk; thunk = TREE_CHAIN (thunk))
265 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
266 && THUNK_FIXED_OFFSET (thunk) == d
267 && (this_adjusting
268 ? (!THUNK_VIRTUAL_OFFSET (thunk) == !virtual_offset
269 && (!virtual_offset
270 || tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
271 virtual_offset)))
272 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset))
273 return thunk;
274
275 /* All thunks must be created before FUNCTION is actually emitted;
276 the ABI requires that all thunks be emitted together with the
277 function to which they transfer control. */
278 my_friendly_assert (!TREE_ASM_WRITTEN (function), 20021025);
279
280 thunk = build_decl (FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
281 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
282 cxx_dup_lang_specific_decl (thunk);
283 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
284 TREE_READONLY (thunk) = TREE_READONLY (function);
285 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
286 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
287 if (flag_weak)
288 comdat_linkage (thunk);
289 SET_DECL_THUNK_P (thunk, this_adjusting);
290 THUNK_TARGET (thunk) = function;
291 THUNK_FIXED_OFFSET (thunk) = d;
292 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
293
294 /* The thunk itself is not a constructor or destructor, even if
295 the thing it is thunking to is. */
296 DECL_INTERFACE_KNOWN (thunk) = 1;
297 DECL_NOT_REALLY_EXTERN (thunk) = 1;
298 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
299 DECL_DESTRUCTOR_P (thunk) = 0;
300 DECL_CONSTRUCTOR_P (thunk) = 0;
301 /* And neither is it a clone. */
302 DECL_CLONED_FUNCTION (thunk) = NULL_TREE;
303 DECL_EXTERNAL (thunk) = 1;
304 DECL_ARTIFICIAL (thunk) = 1;
305 /* Even if this thunk is a member of a local class, we don't
306 need a static chain. */
307 DECL_NO_STATIC_CHAIN (thunk) = 1;
308 /* The THUNK is not a pending inline, even if the FUNCTION is. */
309 DECL_PENDING_INLINE_P (thunk) = 0;
310 DECL_INLINE (thunk) = 0;
311 DECL_DECLARED_INLINE_P (thunk) = 0;
312 /* Nor has it been deferred. */
313 DECL_DEFERRED_FN (thunk) = 0;
314 /* Add it to the list of thunks associated with FUNCTION. */
315 TREE_CHAIN (thunk) = DECL_THUNKS (function);
316 DECL_THUNKS (function) = thunk;
317
318 return thunk;
319 }
320
321 /* Finish THUNK, a thunk decl. */
322
323 void
324 finish_thunk (tree thunk)
325 {
326 tree function, name;
327 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
328 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
329
330 my_friendly_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk), 20021127);
331 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
332 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
333 function = THUNK_TARGET (thunk);
334 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
335 fixed_offset, virtual_offset);
336 DECL_NAME (thunk) = name;
337 SET_DECL_ASSEMBLER_NAME (thunk, name);
338 }
339
340 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
341 offset indicated by VIRTUAL_OFFSET, if that is
342 non-null. THIS_ADJUSTING is non-zero for a this adjusting thunk and
343 zero for a result adjusting thunk. */
344
345 static tree
346 thunk_adjust (tree ptr, bool this_adjusting,
347 HOST_WIDE_INT fixed_offset, tree virtual_offset)
348 {
349 if (this_adjusting)
350 /* Adjust the pointer by the constant. */
351 ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr,
352 ssize_int (fixed_offset)));
353
354 /* If there's a virtual offset, look up that value in the vtable and
355 adjust the pointer again. */
356 if (virtual_offset)
357 {
358 tree vtable;
359
360 ptr = save_expr (ptr);
361 /* The vptr is always at offset zero in the object. */
362 vtable = build1 (NOP_EXPR,
363 build_pointer_type (build_pointer_type
364 (vtable_entry_type)),
365 ptr);
366 /* Form the vtable address. */
367 vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
368 /* Find the entry with the vcall offset. */
369 vtable = build (PLUS_EXPR, TREE_TYPE (vtable), vtable, virtual_offset);
370 /* Get the offset itself. */
371 vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
372 /* Adjust the `this' pointer. */
373 ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr, vtable));
374 }
375
376 if (!this_adjusting)
377 /* Adjust the pointer by the constant. */
378 ptr = fold (build (PLUS_EXPR, TREE_TYPE (ptr), ptr,
379 ssize_int (fixed_offset)));
380
381 return ptr;
382 }
383
384 /* Emit the definition of a C++ multiple inheritance or covariant
385 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
386 immediately. */
387
388 void
389 use_thunk (tree thunk_fndecl, bool emit_p)
390 {
391 tree function;
392 tree virtual_offset;
393 HOST_WIDE_INT fixed_offset, virtual_value;
394 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
395
396 /* We should have called finish_thunk to give it a name. */
397 my_friendly_assert (DECL_NAME (thunk_fndecl), 20021127);
398
399 if (TREE_ASM_WRITTEN (thunk_fndecl))
400 return;
401
402 function = THUNK_TARGET (thunk_fndecl);
403 if (DECL_RESULT (thunk_fndecl))
404 /* We already turned this thunk into an ordinary function.
405 There's no need to process this thunk again. */
406 return;
407
408 /* Thunks are always addressable; they only appear in vtables. */
409 TREE_ADDRESSABLE (thunk_fndecl) = 1;
410
411 /* Figure out what function is being thunked to. It's referenced in
412 this translation unit. */
413 TREE_ADDRESSABLE (function) = 1;
414 mark_used (function);
415 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (function)) = 1;
416 if (!emit_p)
417 return;
418
419 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
420 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
421
422 if (virtual_offset)
423 {
424 if (!this_adjusting)
425 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
426 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
427 my_friendly_assert (virtual_value, 20021026);
428 }
429 else
430 virtual_value = 0;
431
432 /* And, if we need to emit the thunk, it's used. */
433 mark_used (thunk_fndecl);
434 /* This thunk is actually defined. */
435 DECL_EXTERNAL (thunk_fndecl) = 0;
436 /* The linkage of the function may have changed. FIXME in linkage
437 rewrite. */
438 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
439
440 if (flag_syntax_only)
441 {
442 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
443 return;
444 }
445
446 push_to_top_level ();
447
448 /* The back-end expects DECL_INITIAL to contain a BLOCK, so we
449 create one. */
450 DECL_INITIAL (thunk_fndecl) = make_node (BLOCK);
451 BLOCK_VARS (DECL_INITIAL (thunk_fndecl)) = DECL_ARGUMENTS (thunk_fndecl);
452
453 if (this_adjusting
454 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
455 virtual_value, function))
456 {
457 const char *fnname;
458 current_function_decl = thunk_fndecl;
459 DECL_RESULT (thunk_fndecl)
460 = build_decl (RESULT_DECL, 0, integer_type_node);
461 fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
462 init_function_start (thunk_fndecl, input_filename, lineno);
463 current_function_is_thunk = 1;
464 assemble_start_function (thunk_fndecl, fnname);
465
466 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
467 fixed_offset, virtual_value, function);
468
469 assemble_end_function (thunk_fndecl, fnname);
470 current_function_decl = 0;
471 cfun = 0;
472 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
473 }
474 else
475 {
476 /* If this is a covariant thunk, or we don't have the necessary
477 code for efficient thunks, generate a thunk function that
478 just makes a call to the real function. Unfortunately, this
479 doesn't work for varargs. */
480
481 tree a, t;
482 int saved_check_access;
483
484 if (varargs_function_p (function))
485 error ("generic thunk code fails for method `%#D' which uses `...'",
486 function);
487
488 /* Set up cloned argument trees for the thunk. */
489 t = NULL_TREE;
490 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
491 {
492 tree x = copy_node (a);
493 TREE_CHAIN (x) = t;
494 DECL_CONTEXT (x) = thunk_fndecl;
495 t = x;
496 }
497 a = nreverse (t);
498 DECL_ARGUMENTS (thunk_fndecl) = a;
499 DECL_RESULT (thunk_fndecl) = NULL_TREE;
500
501 start_function (NULL_TREE, thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
502 /* We don't bother with a body block for thunks. */
503
504 /* There's no need to check accessibility inside the thunk body. */
505 saved_check_access = scope_chain->check_access;
506 scope_chain->check_access = 0;
507
508 t = a;
509 if (this_adjusting)
510 t = thunk_adjust (t, /*this_adjusting=*/1,
511 fixed_offset, virtual_offset);
512
513 /* Build up the call to the real function. */
514 t = tree_cons (NULL_TREE, t, NULL_TREE);
515 for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
516 t = tree_cons (NULL_TREE, a, t);
517 t = nreverse (t);
518 t = build_call (function, t);
519 if (!this_adjusting)
520 t = thunk_adjust (t, /*this_adjusting=*/0,
521 fixed_offset, virtual_offset);
522
523 if (VOID_TYPE_P (TREE_TYPE (t)))
524 finish_expr_stmt (t);
525 else
526 finish_return_stmt (t);
527
528 /* Since we want to emit the thunk, we explicitly mark its name as
529 referenced. */
530 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (thunk_fndecl)) = 1;
531
532 /* But we don't want debugging information about it. */
533 DECL_IGNORED_P (thunk_fndecl) = 1;
534
535 /* Re-enable access control. */
536 scope_chain->check_access = saved_check_access;
537
538 expand_body (finish_function (0));
539 }
540
541 pop_from_top_level ();
542 }
543 \f
544 /* Code for synthesizing methods which have default semantics defined. */
545
546 /* Generate code for default X(X&) constructor. */
547
548 static void
549 do_build_copy_constructor (tree fndecl)
550 {
551 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
552 tree t;
553
554 parm = convert_from_reference (parm);
555
556 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type)
557 && is_empty_class (current_class_type))
558 /* Don't copy the padding byte; it might not have been allocated
559 if *this is a base subobject. */;
560 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
561 {
562 t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
563 finish_expr_stmt (t);
564 }
565 else
566 {
567 tree fields = TYPE_FIELDS (current_class_type);
568 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
569 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
570 tree member_init_list = NULL_TREE;
571 int cvquals = cp_type_quals (TREE_TYPE (parm));
572 int i;
573
574 /* Initialize all the base-classes with the parameter converted
575 to their type so that we get their copy constructor and not
576 another constructor that takes current_class_type. We must
577 deal with the binfo's directly as a direct base might be
578 inaccessible due to ambiguity. */
579 for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
580 t = TREE_CHAIN (t))
581 {
582 tree binfo = TREE_VALUE (t);
583
584 member_init_list
585 = tree_cons (binfo,
586 build_tree_list (NULL_TREE,
587 build_base_path (PLUS_EXPR, parm,
588 binfo, 1)),
589 member_init_list);
590 }
591
592 for (i = 0; i < n_bases; ++i)
593 {
594 tree binfo = TREE_VEC_ELT (binfos, i);
595 if (TREE_VIA_VIRTUAL (binfo))
596 continue;
597
598 member_init_list
599 = tree_cons (binfo,
600 build_tree_list (NULL_TREE,
601 build_base_path (PLUS_EXPR, parm,
602 binfo, 1)),
603 member_init_list);
604 }
605
606 for (; fields; fields = TREE_CHAIN (fields))
607 {
608 tree init;
609 tree field = fields;
610 tree expr_type;
611
612 if (TREE_CODE (field) != FIELD_DECL)
613 continue;
614
615 init = parm;
616 if (DECL_NAME (field))
617 {
618 if (VFIELD_NAME_P (DECL_NAME (field)))
619 continue;
620
621 /* True for duplicate members. */
622 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
623 continue;
624 }
625 else if ((t = TREE_TYPE (field)) != NULL_TREE
626 && ANON_AGGR_TYPE_P (t)
627 && TYPE_FIELDS (t) != NULL_TREE)
628 /* Just use the field; anonymous types can't have
629 nontrivial copy ctors or assignment ops. */;
630 else
631 continue;
632
633 /* Compute the type of "init->field". If the copy-constructor
634 parameter is, for example, "const S&", and the type of
635 the field is "T", then the type will usually be "const
636 T". (There are no cv-qualified variants of reference
637 types.) */
638 expr_type = TREE_TYPE (field);
639 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
640 expr_type = cp_build_qualified_type (expr_type, cvquals);
641 init = build (COMPONENT_REF, expr_type, init, field);
642 init = build_tree_list (NULL_TREE, init);
643
644 member_init_list
645 = tree_cons (field, init, member_init_list);
646 }
647 finish_mem_initializers (member_init_list);
648 }
649 }
650
651 static void
652 do_build_assign_ref (tree fndecl)
653 {
654 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
655 tree compound_stmt;
656
657 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
658 parm = convert_from_reference (parm);
659
660 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
661 && is_empty_class (current_class_type))
662 /* Don't copy the padding byte; it might not have been allocated
663 if *this is a base subobject. */;
664 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
665 {
666 tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
667 finish_expr_stmt (t);
668 }
669 else
670 {
671 tree fields;
672 int cvquals = cp_type_quals (TREE_TYPE (parm));
673 int i;
674
675 /* Assign to each of thedirect base classes. */
676 for (i = 0; i < CLASSTYPE_N_BASECLASSES (current_class_type); ++i)
677 {
678 tree binfo;
679 tree converted_parm;
680
681 binfo = BINFO_BASETYPE (TYPE_BINFO (current_class_type), i);
682 /* We must convert PARM directly to the base class
683 explicitly since the base class may be ambiguous. */
684 converted_parm = build_base_path (PLUS_EXPR, parm, binfo, 1);
685 /* Call the base class assignment operator. */
686 finish_expr_stmt
687 (build_special_member_call (current_class_ref,
688 ansi_assopname (NOP_EXPR),
689 build_tree_list (NULL_TREE,
690 converted_parm),
691 binfo,
692 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL));
693 }
694
695 /* Assign to each of the non-static data members. */
696 for (fields = TYPE_FIELDS (current_class_type);
697 fields;
698 fields = TREE_CHAIN (fields))
699 {
700 tree comp, init, t;
701 tree field = fields;
702
703 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
704 continue;
705
706 if (CP_TYPE_CONST_P (TREE_TYPE (field)))
707 {
708 error ("non-static const member `%#D', can't use default assignment operator", field);
709 continue;
710 }
711 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
712 {
713 error ("non-static reference member `%#D', can't use default assignment operator", field);
714 continue;
715 }
716
717 comp = current_class_ref;
718 init = parm;
719
720 if (DECL_NAME (field))
721 {
722 if (VFIELD_NAME_P (DECL_NAME (field)))
723 continue;
724
725 /* True for duplicate members. */
726 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
727 continue;
728 }
729 else if ((t = TREE_TYPE (field)) != NULL_TREE
730 && ANON_AGGR_TYPE_P (t)
731 && TYPE_FIELDS (t) != NULL_TREE)
732 /* Just use the field; anonymous types can't have
733 nontrivial copy ctors or assignment ops. */;
734 else
735 continue;
736
737 comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field);
738 init = build (COMPONENT_REF,
739 cp_build_qualified_type (TREE_TYPE (field), cvquals),
740 init, field);
741
742 if (DECL_NAME (field))
743 finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
744 else
745 finish_expr_stmt (build (MODIFY_EXPR, TREE_TYPE (comp), comp,
746 init));
747 }
748 }
749 finish_return_stmt (current_class_ref);
750 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
751 }
752
753 void
754 synthesize_method (tree fndecl)
755 {
756 bool nested = (current_function_decl != NULL_TREE);
757 tree context = decl_function_context (fndecl);
758 bool need_body = true;
759 tree stmt;
760
761 if (at_eof)
762 import_export_decl (fndecl);
763
764 /* If we've been asked to synthesize a clone, just synthesize the
765 cloned function instead. Doing so will automatically fill in the
766 body for the clone. */
767 if (DECL_CLONED_FUNCTION_P (fndecl))
768 {
769 synthesize_method (DECL_CLONED_FUNCTION (fndecl));
770 return;
771 }
772
773 if (! context)
774 push_to_top_level ();
775 else if (nested)
776 push_function_context_to (context);
777
778 /* Put the function definition at the position where it is needed,
779 rather than within the body of the class. That way, an error
780 during the generation of the implicit body points at the place
781 where the attempt to generate the function occurs, giving the
782 user a hint as to why we are attempting to generate the
783 function. */
784 DECL_SOURCE_LINE (fndecl) = lineno;
785 DECL_SOURCE_FILE (fndecl) = input_filename;
786
787 interface_unknown = 1;
788 start_function (NULL_TREE, fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
789 clear_last_expr ();
790 stmt = begin_function_body ();
791
792 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
793 {
794 do_build_assign_ref (fndecl);
795 need_body = false;
796 }
797 else if (DECL_CONSTRUCTOR_P (fndecl))
798 {
799 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
800 if (arg_chain != void_list_node)
801 do_build_copy_constructor (fndecl);
802 else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
803 finish_mem_initializers (NULL_TREE);
804 }
805
806 /* If we haven't yet generated the body of the function, just
807 generate an empty compound statement. */
808 if (need_body)
809 {
810 tree compound_stmt;
811 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
812 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
813 }
814
815 finish_function_body (stmt);
816 expand_body (finish_function (0));
817
818 extract_interface_info ();
819 if (! context)
820 pop_from_top_level ();
821 else if (nested)
822 pop_function_context_from (context);
823 }
824
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. */
830
831 static tree
832 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
833 void *client)
834 {
835 tree raises = empty_except_spec;
836 tree fields = TYPE_FIELDS (type);
837 int i, n_bases = CLASSTYPE_N_BASECLASSES (type);
838 tree binfos = TYPE_BINFO_BASETYPES (type);
839
840 for (i = 0; i != n_bases; i++)
841 {
842 tree base = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
843 tree fn = (*extractor) (base, client);
844 if (fn)
845 {
846 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
847
848 raises = merge_exception_specifiers (raises, fn_raises);
849 }
850 }
851 for (; fields; fields = TREE_CHAIN (fields))
852 {
853 tree type = TREE_TYPE (fields);
854 tree fn;
855
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 (TREE_CODE (type) != RECORD_TYPE)
861 continue;
862
863 fn = (*extractor) (type, client);
864 if (fn)
865 {
866 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
867
868 raises = merge_exception_specifiers (raises, fn_raises);
869 }
870 }
871 return raises;
872 }
873
874 /* Locate the dtor of TYPE. */
875
876 static tree
877 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
878 {
879 tree fns;
880
881 if (!TYPE_HAS_DESTRUCTOR (type))
882 return NULL_TREE;
883 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type),
884 CLASSTYPE_DESTRUCTOR_SLOT);
885 return fns;
886 }
887
888 /* Locate the default ctor of TYPE. */
889
890 static tree
891 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
892 {
893 tree fns;
894
895 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
896 return NULL_TREE;
897
898 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type),
899 CLASSTYPE_CONSTRUCTOR_SLOT);
900 for (; fns; fns = OVL_NEXT (fns))
901 {
902 tree fn = OVL_CURRENT (fns);
903 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
904
905 if (sufficient_parms_p (TREE_CHAIN (parms)))
906 return fn;
907 }
908 return NULL_TREE;
909 }
910
911 struct copy_data
912 {
913 tree name;
914 int quals;
915 };
916
917 /* Locate the copy ctor or copy assignment of TYPE. CLIENT_
918 points to a COPY_DATA holding the name (NULL for the ctor)
919 and desired qualifiers of the source operand. */
920
921 static tree
922 locate_copy (tree type, void *client_)
923 {
924 struct copy_data *client = (struct copy_data *)client_;
925 tree fns;
926 int ix = -1;
927 tree best = NULL_TREE;
928 bool excess_p = false;
929
930 if (client->name)
931 {
932 if (TYPE_HAS_ASSIGN_REF (type))
933 ix = lookup_fnfields_1 (type, client->name);
934 }
935 else if (TYPE_HAS_INIT_REF (type))
936 ix = CLASSTYPE_CONSTRUCTOR_SLOT;
937 if (ix < 0)
938 return NULL_TREE;
939 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
940
941 for (; fns; fns = OVL_NEXT (fns))
942 {
943 tree fn = OVL_CURRENT (fns);
944 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
945 tree src_type;
946 int excess;
947 int quals;
948
949 parms = TREE_CHAIN (parms);
950 if (!parms)
951 continue;
952 src_type = TREE_VALUE (parms);
953 if (TREE_CODE (src_type) == REFERENCE_TYPE)
954 src_type = TREE_TYPE (src_type);
955 if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
956 continue;
957 if (!sufficient_parms_p (TREE_CHAIN (parms)))
958 continue;
959 quals = cp_type_quals (src_type);
960 if (client->quals & ~quals)
961 continue;
962 excess = quals & ~client->quals;
963 if (!best || (excess_p && !excess))
964 {
965 best = fn;
966 excess_p = excess;
967 }
968 else
969 /* Ambiguous */
970 return NULL_TREE;
971 }
972 return best;
973 }
974
975 /* Implicitly declare the special function indicated by KIND, as a
976 member of TYPE. For copy constructors and assignment operators,
977 CONST_P indicates whether these functions should take a const
978 reference argument or a non-const reference. */
979
980 tree
981 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
982 {
983 tree declspecs = NULL_TREE;
984 tree fn, args = NULL_TREE;
985 tree raises = empty_except_spec;
986 bool retref = false;
987 bool has_parm = false;
988 tree name = constructor_name (type);
989
990 switch (kind)
991 {
992 case sfk_destructor:
993 /* Destructor. */
994 name = build_nt (BIT_NOT_EXPR, name);
995 args = void_list_node;
996 raises = synthesize_exception_spec (type, &locate_dtor, 0);
997 break;
998
999 case sfk_constructor:
1000 /* Default constructor. */
1001 args = void_list_node;
1002 raises = synthesize_exception_spec (type, &locate_ctor, 0);
1003 break;
1004
1005 case sfk_copy_constructor:
1006 case sfk_assignment_operator:
1007 {
1008 struct copy_data data;
1009 tree argtype = type;
1010
1011 has_parm = true;
1012 data.name = NULL;
1013 data.quals = 0;
1014 if (kind == sfk_assignment_operator)
1015 {
1016 retref = true;
1017 declspecs = build_tree_list (NULL_TREE, type);
1018
1019 name = ansi_assopname (NOP_EXPR);
1020 data.name = name;
1021 }
1022 if (const_p)
1023 {
1024 data.quals = TYPE_QUAL_CONST;
1025 argtype = build_qualified_type (argtype, TYPE_QUAL_CONST);
1026 }
1027
1028 argtype = build_reference_type (argtype);
1029 args = build_tree_list (hash_tree_chain (argtype, NULL_TREE),
1030 get_identifier ("_ctor_arg"));
1031 args = tree_cons (NULL_TREE, args, void_list_node);
1032
1033 raises = synthesize_exception_spec (type, &locate_copy, &data);
1034 break;
1035 }
1036 default:
1037 abort ();
1038 }
1039
1040 TREE_PARMLIST (args) = 1;
1041
1042 {
1043 tree declarator = make_call_declarator (name, args, NULL_TREE, raises);
1044
1045 if (retref)
1046 declarator = build_nt (ADDR_EXPR, declarator);
1047
1048 fn = grokfield (declarator, declspecs, NULL_TREE, NULL_TREE, NULL_TREE);
1049 if (has_parm)
1050 TREE_USED (FUNCTION_FIRST_USER_PARM (fn)) = 1;
1051 }
1052
1053 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 20000408);
1054
1055 DECL_ARTIFICIAL (fn) = 1;
1056 DECL_NOT_REALLY_EXTERN (fn) = 1;
1057 DECL_DECLARED_INLINE_P (fn) = 1;
1058 DECL_INLINE (fn) = 1;
1059 defer_fn (fn);
1060
1061 return fn;
1062 }
1063
1064 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1065 as there are artificial parms in FN. */
1066
1067 tree
1068 skip_artificial_parms_for (tree fn, tree list)
1069 {
1070 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1071 list = TREE_CHAIN (list);
1072 else
1073 return list;
1074
1075 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1076 list = TREE_CHAIN (list);
1077 if (DECL_HAS_VTT_PARM_P (fn))
1078 list = TREE_CHAIN (list);
1079 return list;
1080 }