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