OSDN Git Service

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