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