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