OSDN Git Service

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