OSDN Git Service

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