OSDN Git Service

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