OSDN Git Service

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