OSDN Git Service

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