OSDN Git Service

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