OSDN Git Service

2007-08-06 Dan Hipschman <dsh@google.com>
[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, 2004, 2005, 2007
5    Free Software Foundation, Inc.
6    Contributed by Michael Tiemann (tiemann@cygnus.com)
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
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 #include "tree-pass.h"
40 #include "diagnostic.h"
41
42 /* Various flags to control the mangling process.  */
43
44 enum mangling_flags
45 {
46   /* No flags.  */
47   mf_none = 0,
48   /* The thing we are presently mangling is part of a template type,
49      rather than a fully instantiated type.  Therefore, we may see
50      complex expressions where we would normally expect to see a
51      simple integer constant.  */
52   mf_maybe_uninstantiated = 1,
53   /* When mangling a numeric value, use the form `_XX_' (instead of
54      just `XX') if the value has more than one digit.  */
55   mf_use_underscores_around_value = 2
56 };
57
58 typedef enum mangling_flags mangling_flags;
59
60 static tree thunk_adjust (tree, bool, HOST_WIDE_INT, tree);
61 static void do_build_assign_ref (tree);
62 static void do_build_copy_constructor (tree);
63 static tree synthesize_exception_spec (tree, tree (*) (tree, void *), void *);
64 static tree make_alias_for_thunk (tree);
65
66 /* Called once to initialize method.c.  */
67
68 void
69 init_method (void)
70 {
71   init_mangle ();
72 }
73 \f
74 /* Return a this or result adjusting thunk to FUNCTION.  THIS_ADJUSTING
75    indicates whether it is a this or result adjusting thunk.
76    FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
77    (see thunk_adjust).  VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
78    never is.  VIRTUAL_OFFSET is the /index/ into the vtable for this
79    adjusting thunks, we scale it to a byte offset. For covariant
80    thunks VIRTUAL_OFFSET is the virtual binfo.  You must post process
81    the returned thunk with finish_thunk.  */
82
83 tree
84 make_thunk (tree function, bool this_adjusting,
85             tree fixed_offset, tree virtual_offset)
86 {
87   HOST_WIDE_INT d;
88   tree thunk;
89
90   gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
91   /* We can have this thunks to covariant thunks, but not vice versa.  */
92   gcc_assert (!DECL_THIS_THUNK_P (function));
93   gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
94
95   /* Scale the VIRTUAL_OFFSET to be in terms of bytes.  */
96   if (this_adjusting && virtual_offset)
97     virtual_offset
98       = size_binop (MULT_EXPR,
99                     virtual_offset,
100                     convert (ssizetype,
101                              TYPE_SIZE_UNIT (vtable_entry_type)));
102
103   d = tree_low_cst (fixed_offset, 0);
104
105   /* See if we already have the thunk in question.  For this_adjusting
106      thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
107      will be a BINFO.  */
108   for (thunk = DECL_THUNKS (function); thunk; thunk = TREE_CHAIN (thunk))
109     if (DECL_THIS_THUNK_P (thunk) == this_adjusting
110         && THUNK_FIXED_OFFSET (thunk) == d
111         && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
112         && (!virtual_offset
113             || (this_adjusting
114                 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
115                                       virtual_offset)
116                 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
117       return thunk;
118
119   /* All thunks must be created before FUNCTION is actually emitted;
120      the ABI requires that all thunks be emitted together with the
121      function to which they transfer control.  */
122   gcc_assert (!TREE_ASM_WRITTEN (function));
123   /* Likewise, we can only be adding thunks to a function declared in
124      the class currently being laid out.  */
125   gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
126               && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
127
128   thunk = build_decl (FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
129   DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
130   cxx_dup_lang_specific_decl (thunk);
131   DECL_THUNKS (thunk) = NULL_TREE;
132
133   DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
134   TREE_READONLY (thunk) = TREE_READONLY (function);
135   TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
136   TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
137   SET_DECL_THUNK_P (thunk, this_adjusting);
138   THUNK_TARGET (thunk) = function;
139   THUNK_FIXED_OFFSET (thunk) = d;
140   THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
141   THUNK_ALIAS (thunk) = NULL_TREE;
142
143   /* The thunk itself is not a constructor or destructor, even if
144      the thing it is thunking to is.  */
145   DECL_INTERFACE_KNOWN (thunk) = 1;
146   DECL_NOT_REALLY_EXTERN (thunk) = 1;
147   DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
148   DECL_DESTRUCTOR_P (thunk) = 0;
149   DECL_CONSTRUCTOR_P (thunk) = 0;
150   DECL_EXTERNAL (thunk) = 1;
151   DECL_ARTIFICIAL (thunk) = 1;
152   /* Even if this thunk is a member of a local class, we don't
153      need a static chain.  */
154   DECL_NO_STATIC_CHAIN (thunk) = 1;
155   /* The THUNK is not a pending inline, even if the FUNCTION is.  */
156   DECL_PENDING_INLINE_P (thunk) = 0;
157   DECL_INLINE (thunk) = 0;
158   DECL_DECLARED_INLINE_P (thunk) = 0;
159   /* Nor has it been deferred.  */
160   DECL_DEFERRED_FN (thunk) = 0;
161   /* Nor is it a template instantiation.  */
162   DECL_USE_TEMPLATE (thunk) = 0;
163   DECL_TEMPLATE_INFO (thunk) = NULL;
164
165   /* Add it to the list of thunks associated with FUNCTION.  */
166   TREE_CHAIN (thunk) = DECL_THUNKS (function);
167   DECL_THUNKS (function) = thunk;
168
169   return thunk;
170 }
171
172 /* Finish THUNK, a thunk decl.  */
173
174 void
175 finish_thunk (tree thunk)
176 {
177   tree function, name;
178   tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
179   tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
180
181   gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
182   if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
183     virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
184   function = THUNK_TARGET (thunk);
185   name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
186                        fixed_offset, virtual_offset);
187
188   /* We can end up with declarations of (logically) different
189      covariant thunks, that do identical adjustments.  The two thunks
190      will be adjusting between within different hierarchies, which
191      happen to have the same layout.  We must nullify one of them to
192      refer to the other.  */
193   if (DECL_RESULT_THUNK_P (thunk))
194     {
195       tree cov_probe;
196
197       for (cov_probe = DECL_THUNKS (function);
198            cov_probe; cov_probe = TREE_CHAIN (cov_probe))
199         if (DECL_NAME (cov_probe) == name)
200           {
201             gcc_assert (!DECL_THUNKS (thunk));
202             THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
203                                    ? THUNK_ALIAS (cov_probe) : cov_probe);
204             break;
205           }
206     }
207
208   DECL_NAME (thunk) = name;
209   SET_DECL_ASSEMBLER_NAME (thunk, name);
210 }
211
212 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
213    offset indicated by VIRTUAL_OFFSET, if that is
214    non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
215    zero for a result adjusting thunk.  */
216
217 static tree
218 thunk_adjust (tree ptr, bool this_adjusting,
219               HOST_WIDE_INT fixed_offset, tree virtual_offset)
220 {
221   if (this_adjusting)
222     /* Adjust the pointer by the constant.  */
223     ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
224                        size_int (fixed_offset));
225
226   /* If there's a virtual offset, look up that value in the vtable and
227      adjust the pointer again.  */
228   if (virtual_offset)
229     {
230       tree vtable;
231
232       ptr = save_expr (ptr);
233       /* The vptr is always at offset zero in the object.  */
234       vtable = build1 (NOP_EXPR,
235                        build_pointer_type (build_pointer_type
236                                            (vtable_entry_type)),
237                        ptr);
238       /* Form the vtable address.  */
239       vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
240       /* Find the entry with the vcall offset.  */
241       vtable = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtable), vtable,
242                        fold_convert (sizetype, virtual_offset));
243       /* Get the offset itself.  */
244       vtable = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (vtable)), vtable);
245       /* Adjust the `this' pointer.  */
246       ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
247                          fold_convert (sizetype, vtable));
248     }
249
250   if (!this_adjusting)
251     /* Adjust the pointer by the constant.  */
252     ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
253                        size_int (fixed_offset));
254
255   return ptr;
256 }
257
258 static GTY (()) int thunk_labelno;
259
260 /* Create a static alias to function.  */
261
262 tree
263 make_alias_for (tree function, tree newid)
264 {
265   tree alias = build_decl (FUNCTION_DECL, newid, TREE_TYPE (function));
266   DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (function);
267   cxx_dup_lang_specific_decl (alias);
268   DECL_CONTEXT (alias) = NULL;
269   TREE_READONLY (alias) = TREE_READONLY (function);
270   TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (function);
271   TREE_PUBLIC (alias) = 0;
272   DECL_INTERFACE_KNOWN (alias) = 1;
273   DECL_NOT_REALLY_EXTERN (alias) = 1;
274   DECL_THIS_STATIC (alias) = 1;
275   DECL_SAVED_FUNCTION_DATA (alias) = NULL;
276   DECL_DESTRUCTOR_P (alias) = 0;
277   DECL_CONSTRUCTOR_P (alias) = 0;
278   DECL_CLONED_FUNCTION (alias) = NULL_TREE;
279   DECL_EXTERNAL (alias) = 0;
280   DECL_ARTIFICIAL (alias) = 1;
281   DECL_NO_STATIC_CHAIN (alias) = 1;
282   DECL_PENDING_INLINE_P (alias) = 0;
283   DECL_INLINE (alias) = 0;
284   DECL_DECLARED_INLINE_P (alias) = 0;
285   DECL_DEFERRED_FN (alias) = 0;
286   DECL_USE_TEMPLATE (alias) = 0;
287   DECL_TEMPLATE_INSTANTIATED (alias) = 0;
288   DECL_TEMPLATE_INFO (alias) = NULL;
289   DECL_INITIAL (alias) = error_mark_node;
290   TREE_ADDRESSABLE (alias) = 1;
291   TREE_USED (alias) = 1;
292   SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
293   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
294   return alias;
295 }
296
297 static tree
298 make_alias_for_thunk (tree function)
299 {
300   tree alias;
301   char buf[256];
302
303   ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
304   thunk_labelno++;
305
306   alias = make_alias_for (function, get_identifier (buf));
307
308   if (!flag_syntax_only)
309     assemble_alias (alias, DECL_ASSEMBLER_NAME (function));
310
311   return alias;
312 }
313
314 /* Emit the definition of a C++ multiple inheritance or covariant
315    return vtable thunk.  If EMIT_P is nonzero, the thunk is emitted
316    immediately.  */
317
318 void
319 use_thunk (tree thunk_fndecl, bool emit_p)
320 {
321   tree a, t, function, alias;
322   tree virtual_offset;
323   HOST_WIDE_INT fixed_offset, virtual_value;
324   bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
325
326   /* We should have called finish_thunk to give it a name.  */
327   gcc_assert (DECL_NAME (thunk_fndecl));
328
329   /* We should never be using an alias, always refer to the
330      aliased thunk.  */
331   gcc_assert (!THUNK_ALIAS (thunk_fndecl));
332
333   if (TREE_ASM_WRITTEN (thunk_fndecl))
334     return;
335
336   function = THUNK_TARGET (thunk_fndecl);
337   if (DECL_RESULT (thunk_fndecl))
338     /* We already turned this thunk into an ordinary function.
339        There's no need to process this thunk again.  */
340     return;
341
342   if (DECL_THUNK_P (function))
343     /* The target is itself a thunk, process it now.  */
344     use_thunk (function, emit_p);
345
346   /* Thunks are always addressable; they only appear in vtables.  */
347   TREE_ADDRESSABLE (thunk_fndecl) = 1;
348
349   /* Figure out what function is being thunked to.  It's referenced in
350      this translation unit.  */
351   TREE_ADDRESSABLE (function) = 1;
352   mark_used (function);
353   if (!emit_p)
354     return;
355
356   if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
357    alias = make_alias_for_thunk (function);
358   else
359    alias = function;
360
361   fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
362   virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
363
364   if (virtual_offset)
365     {
366       if (!this_adjusting)
367         virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
368       virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
369       gcc_assert (virtual_value);
370     }
371   else
372     virtual_value = 0;
373
374   /* And, if we need to emit the thunk, it's used.  */
375   mark_used (thunk_fndecl);
376   /* This thunk is actually defined.  */
377   DECL_EXTERNAL (thunk_fndecl) = 0;
378   /* The linkage of the function may have changed.  FIXME in linkage
379      rewrite.  */
380   TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
381   DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
382   DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
383     = DECL_VISIBILITY_SPECIFIED (function);
384   if (DECL_ONE_ONLY (function))
385     make_decl_one_only (thunk_fndecl);
386
387   if (flag_syntax_only)
388     {
389       TREE_ASM_WRITTEN (thunk_fndecl) = 1;
390       return;
391     }
392
393   push_to_top_level ();
394
395   if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
396       && targetm.have_named_sections)
397     {
398       resolve_unique_section (function, 0, flag_function_sections);
399
400       if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
401         {
402           resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
403
404           /* Output the thunk into the same section as function.  */
405           DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
406         }
407     }
408
409   /* Set up cloned argument trees for the thunk.  */
410   t = NULL_TREE;
411   for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
412     {
413       tree x = copy_node (a);
414       TREE_CHAIN (x) = t;
415       DECL_CONTEXT (x) = thunk_fndecl;
416       SET_DECL_RTL (x, NULL_RTX);
417       DECL_HAS_VALUE_EXPR_P (x) = 0;
418       t = x;
419     }
420   a = nreverse (t);
421   DECL_ARGUMENTS (thunk_fndecl) = a;
422
423   if (this_adjusting
424       && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
425                                               virtual_value, alias))
426     {
427       const char *fnname;
428       tree fn_block;
429       
430       current_function_decl = thunk_fndecl;
431       DECL_RESULT (thunk_fndecl)
432         = build_decl (RESULT_DECL, 0, integer_type_node);
433       fnname = IDENTIFIER_POINTER (DECL_NAME (thunk_fndecl));
434       /* The back end expects DECL_INITIAL to contain a BLOCK, so we
435          create one.  */
436       fn_block = make_node (BLOCK);
437       BLOCK_VARS (fn_block) = a;
438       DECL_INITIAL (thunk_fndecl) = fn_block;
439       init_function_start (thunk_fndecl);
440       current_function_is_thunk = 1;
441       assemble_start_function (thunk_fndecl, fnname);
442
443       targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
444                                        fixed_offset, virtual_value, alias);
445
446       assemble_end_function (thunk_fndecl, fnname);
447       init_insn_lengths ();
448       current_function_decl = 0;
449       cfun = 0;
450       TREE_ASM_WRITTEN (thunk_fndecl) = 1;
451     }
452   else
453     {
454       int i;
455       tree *argarray = (tree *) alloca (list_length (a) * sizeof (tree));
456       /* If this is a covariant thunk, or we don't have the necessary
457          code for efficient thunks, generate a thunk function that
458          just makes a call to the real function.  Unfortunately, this
459          doesn't work for varargs.  */
460
461       if (varargs_function_p (function))
462         error ("generic thunk code fails for method %q#D which uses %<...%>",
463                function);
464
465       DECL_RESULT (thunk_fndecl) = NULL_TREE;
466
467       start_preparsed_function (thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
468       /* We don't bother with a body block for thunks.  */
469
470       /* There's no need to check accessibility inside the thunk body.  */
471       push_deferring_access_checks (dk_no_check);
472
473       t = a;
474       if (this_adjusting)
475         t = thunk_adjust (t, /*this_adjusting=*/1,
476                           fixed_offset, virtual_offset);
477
478       /* Build up the call to the real function.  */
479       argarray[0] = t;
480       for (i = 1, a = TREE_CHAIN (a); a; a = TREE_CHAIN (a), i++)
481         argarray[i] = a;
482       t = build_call_a (alias, i, argarray);
483       CALL_FROM_THUNK_P (t) = 1;
484
485       if (VOID_TYPE_P (TREE_TYPE (t)))
486         finish_expr_stmt (t);
487       else
488         {
489           if (!this_adjusting)
490             {
491               tree cond = NULL_TREE;
492
493               if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
494                 {
495                   /* If the return type is a pointer, we need to
496                      protect against NULL.  We know there will be an
497                      adjustment, because that's why we're emitting a
498                      thunk.  */
499                   t = save_expr (t);
500                   cond = cp_convert (boolean_type_node, t);
501                 }
502
503               t = thunk_adjust (t, /*this_adjusting=*/0,
504                                 fixed_offset, virtual_offset);
505               if (cond)
506                 t = build3 (COND_EXPR, TREE_TYPE (t), cond, t,
507                             cp_convert (TREE_TYPE (t), integer_zero_node));
508             }
509           if (IS_AGGR_TYPE (TREE_TYPE (t)))
510             t = build_cplus_new (TREE_TYPE (t), t);
511           finish_return_stmt (t);
512         }
513
514       /* Since we want to emit the thunk, we explicitly mark its name as
515          referenced.  */
516       mark_decl_referenced (thunk_fndecl);
517
518       /* But we don't want debugging information about it.  */
519       DECL_IGNORED_P (thunk_fndecl) = 1;
520
521       /* Re-enable access control.  */
522       pop_deferring_access_checks ();
523
524       thunk_fndecl = finish_function (0);
525       tree_lowering_passes (thunk_fndecl);
526       expand_body (thunk_fndecl);
527     }
528
529   pop_from_top_level ();
530 }
531 \f
532 /* Code for synthesizing methods which have default semantics defined.  */
533
534 /* Generate code for default X(X&) constructor.  */
535
536 static void
537 do_build_copy_constructor (tree fndecl)
538 {
539   tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
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       tree t = build2 (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       tree member_init_list = NULL_TREE;
556       int cvquals = cp_type_quals (TREE_TYPE (parm));
557       int i;
558       tree binfo, base_binfo;
559       VEC(tree,gc) *vbases;
560
561       /* Initialize all the base-classes with the parameter converted
562          to their type so that we get their copy constructor and not
563          another constructor that takes current_class_type.  We must
564          deal with the binfo's directly as a direct base might be
565          inaccessible due to ambiguity.  */
566       for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
567            VEC_iterate (tree, vbases, i, binfo); i++)
568         {
569           member_init_list
570             = tree_cons (binfo,
571                          build_tree_list (NULL_TREE,
572                                           build_base_path (PLUS_EXPR, parm,
573                                                            binfo, 1)),
574                          member_init_list);
575         }
576
577       for (binfo = TYPE_BINFO (current_class_type), i = 0;
578            BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
579         {
580           if (BINFO_VIRTUAL_P (base_binfo))
581             continue;
582
583           member_init_list
584             = tree_cons (base_binfo,
585                          build_tree_list (NULL_TREE,
586                                           build_base_path (PLUS_EXPR, parm,
587                                                            base_binfo, 1)),
588                          member_init_list);
589         }
590
591       for (; fields; fields = TREE_CHAIN (fields))
592         {
593           tree init = parm;
594           tree field = fields;
595           tree expr_type;
596
597           if (TREE_CODE (field) != FIELD_DECL)
598             continue;
599
600           expr_type = TREE_TYPE (field);
601           if (DECL_NAME (field))
602             {
603               if (VFIELD_NAME_P (DECL_NAME (field)))
604                 continue;
605             }
606           else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
607             /* Just use the field; anonymous types can't have
608                nontrivial copy ctors or assignment ops.  */;
609           else
610             continue;
611
612           /* Compute the type of "init->field".  If the copy-constructor
613              parameter is, for example, "const S&", and the type of
614              the field is "T", then the type will usually be "const
615              T".  (There are no cv-qualified variants of reference
616              types.)  */
617           if (TREE_CODE (expr_type) != REFERENCE_TYPE)
618             {
619               int quals = cvquals;
620
621               if (DECL_MUTABLE_P (field))
622                 quals &= ~TYPE_QUAL_CONST;
623               expr_type = cp_build_qualified_type (expr_type, quals);
624             }
625
626           init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
627           init = build_tree_list (NULL_TREE, init);
628
629           member_init_list = tree_cons (field, init, member_init_list);
630         }
631       finish_mem_initializers (member_init_list);
632     }
633 }
634
635 static void
636 do_build_assign_ref (tree fndecl)
637 {
638   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
639   tree compound_stmt;
640
641   compound_stmt = begin_compound_stmt (0);
642   parm = convert_from_reference (parm);
643
644   if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
645       && is_empty_class (current_class_type))
646     /* Don't copy the padding byte; it might not have been allocated
647        if *this is a base subobject.  */;
648   else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
649     {
650       tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
651       finish_expr_stmt (t);
652     }
653   else
654     {
655       tree fields;
656       int cvquals = cp_type_quals (TREE_TYPE (parm));
657       int i;
658       tree binfo, base_binfo;
659
660       /* Assign to each of the direct base classes.  */
661       for (binfo = TYPE_BINFO (current_class_type), i = 0;
662            BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
663         {
664           tree converted_parm;
665
666           /* We must convert PARM directly to the base class
667              explicitly since the base class may be ambiguous.  */
668           converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1);
669           /* Call the base class assignment operator.  */
670           finish_expr_stmt
671             (build_special_member_call (current_class_ref,
672                                         ansi_assopname (NOP_EXPR),
673                                         build_tree_list (NULL_TREE,
674                                                          converted_parm),
675                                         base_binfo,
676                                         LOOKUP_NORMAL | LOOKUP_NONVIRTUAL));
677         }
678
679       /* Assign to each of the non-static data members.  */
680       for (fields = TYPE_FIELDS (current_class_type);
681            fields;
682            fields = TREE_CHAIN (fields))
683         {
684           tree comp = current_class_ref;
685           tree init = parm;
686           tree field = fields;
687           tree expr_type;
688           int quals;
689
690           if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
691             continue;
692
693           expr_type = TREE_TYPE (field);
694
695           if (CP_TYPE_CONST_P (expr_type))
696             {
697               error ("non-static const member %q#D, can't use default "
698                      "assignment operator", field);
699               continue;
700             }
701           else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
702             {
703               error ("non-static reference member %q#D, can't use "
704                      "default assignment operator", field);
705               continue;
706             }
707
708           if (DECL_NAME (field))
709             {
710               if (VFIELD_NAME_P (DECL_NAME (field)))
711                 continue;
712             }
713           else if (ANON_AGGR_TYPE_P (expr_type)
714                    && TYPE_FIELDS (expr_type) != NULL_TREE)
715             /* Just use the field; anonymous types can't have
716                nontrivial copy ctors or assignment ops.  */;
717           else
718             continue;
719
720           comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
721
722           /* Compute the type of init->field  */
723           quals = cvquals;
724           if (DECL_MUTABLE_P (field))
725             quals &= ~TYPE_QUAL_CONST;
726           expr_type = cp_build_qualified_type (expr_type, quals);
727
728           init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
729
730           if (DECL_NAME (field))
731             init = build_modify_expr (comp, NOP_EXPR, init);
732           else
733             init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
734           finish_expr_stmt (init);
735         }
736     }
737   finish_return_stmt (current_class_ref);
738   finish_compound_stmt (compound_stmt);
739 }
740
741 /* Synthesize FNDECL, a non-static member function.   */
742
743 void
744 synthesize_method (tree fndecl)
745 {
746   bool nested = (current_function_decl != NULL_TREE);
747   tree context = decl_function_context (fndecl);
748   bool need_body = true;
749   tree stmt;
750   location_t save_input_location = input_location;
751   int error_count = errorcount;
752   int warning_count = warningcount;
753
754   /* Reset the source location, we might have been previously
755      deferred, and thus have saved where we were first needed.  */
756   DECL_SOURCE_LOCATION (fndecl)
757     = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
758
759   /* If we've been asked to synthesize a clone, just synthesize the
760      cloned function instead.  Doing so will automatically fill in the
761      body for the clone.  */
762   if (DECL_CLONED_FUNCTION_P (fndecl))
763     fndecl = DECL_CLONED_FUNCTION (fndecl);
764
765   /* We may be in the middle of deferred access check.  Disable
766      it now.  */
767   push_deferring_access_checks (dk_no_deferred);
768
769   if (! context)
770     push_to_top_level ();
771   else if (nested)
772     push_function_context_to (context);
773
774   input_location = DECL_SOURCE_LOCATION (fndecl);
775
776   start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
777   stmt = begin_function_body ();
778
779   if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
780     {
781       do_build_assign_ref (fndecl);
782       need_body = false;
783     }
784   else if (DECL_CONSTRUCTOR_P (fndecl))
785     {
786       tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
787       if (arg_chain != void_list_node)
788         do_build_copy_constructor (fndecl);
789       else
790         finish_mem_initializers (NULL_TREE);
791     }
792
793   /* If we haven't yet generated the body of the function, just
794      generate an empty compound statement.  */
795   if (need_body)
796     {
797       tree compound_stmt;
798       compound_stmt = begin_compound_stmt (BCS_FN_BODY);
799       finish_compound_stmt (compound_stmt);
800     }
801
802   finish_function_body (stmt);
803   expand_or_defer_fn (finish_function (0));
804
805   input_location = save_input_location;
806
807   if (! context)
808     pop_from_top_level ();
809   else if (nested)
810     pop_function_context_from (context);
811
812   pop_deferring_access_checks ();
813
814   if (error_count != errorcount || warning_count != warningcount)
815     inform ("%Hsynthesized method %qD first required here ",
816             &input_location, fndecl);
817 }
818
819 /* Use EXTRACTOR to locate the relevant function called for each base &
820    class field of TYPE. CLIENT allows additional information to be passed
821    to EXTRACTOR.  Generates the union of all exceptions generated by those
822    functions.  Note that we haven't updated TYPE_FIELDS and such of any
823    variants yet, so we need to look at the main one.  */
824
825 static tree
826 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
827                            void *client)
828 {
829   tree raises = empty_except_spec;
830   tree fields = TYPE_FIELDS (type);
831   tree binfo, base_binfo;
832   int i;
833
834   for (binfo = TYPE_BINFO (type), i = 0;
835        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
836     {
837       tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
838       if (fn)
839         {
840           tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
841
842           raises = merge_exception_specifiers (raises, fn_raises);
843         }
844     }
845   for (; fields; fields = TREE_CHAIN (fields))
846     {
847       tree type = TREE_TYPE (fields);
848       tree fn;
849
850       if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
851         continue;
852       while (TREE_CODE (type) == ARRAY_TYPE)
853         type = TREE_TYPE (type);
854       if (!CLASS_TYPE_P (type))
855         continue;
856
857       fn = (*extractor) (type, client);
858       if (fn)
859         {
860           tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
861
862           raises = merge_exception_specifiers (raises, fn_raises);
863         }
864     }
865   return raises;
866 }
867
868 /* Locate the dtor of TYPE.  */
869
870 tree
871 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
872 {
873   return CLASSTYPE_DESTRUCTORS (type);
874 }
875
876 /* Locate the default ctor of TYPE.  */
877
878 tree
879 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
880 {
881   tree fns;
882
883   if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
884     return NULL_TREE;
885
886   /* Call lookup_fnfields_1 to create the constructor declarations, if
887      necessary.  */
888   if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
889     return lazily_declare_fn (sfk_constructor, type);
890
891   for (fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
892     {
893       tree fn = OVL_CURRENT (fns);
894       tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
895
896       parms = skip_artificial_parms_for (fn, parms);
897
898       if (sufficient_parms_p (parms))
899         return fn;
900     }
901   gcc_unreachable ();
902 }
903
904 struct copy_data
905 {
906   tree name;
907   int quals;
908 };
909
910 /* Locate the copy ctor or copy assignment of TYPE. CLIENT_
911    points to a COPY_DATA holding the name (NULL for the ctor)
912    and desired qualifiers of the source operand.  */
913
914 tree
915 locate_copy (tree type, void *client_)
916 {
917   struct copy_data *client = (struct copy_data *)client_;
918   tree fns;
919   tree best = NULL_TREE;
920   bool excess_p = false;
921
922   if (client->name)
923     {
924       int ix;
925       ix = lookup_fnfields_1 (type, client->name);
926       if (ix < 0)
927         return NULL_TREE;
928       fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
929     }
930   else if (TYPE_HAS_INIT_REF (type))
931     {
932       /* If construction of the copy constructor was postponed, create
933          it now.  */
934       if (CLASSTYPE_LAZY_COPY_CTOR (type))
935         lazily_declare_fn (sfk_copy_constructor, type);
936       fns = CLASSTYPE_CONSTRUCTORS (type);
937     }
938   else
939     return NULL_TREE;
940   for (; fns; fns = OVL_NEXT (fns))
941     {
942       tree fn = OVL_CURRENT (fns);
943       tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
944       tree src_type;
945       int excess;
946       int quals;
947
948       parms = skip_artificial_parms_for (fn, parms);
949       if (!parms)
950         continue;
951       src_type = non_reference (TREE_VALUE (parms));
952
953       if (src_type == error_mark_node)
954         return NULL_TREE;
955
956       if (!same_type_ignoring_top_level_qualifiers_p (src_type, type))
957         continue;
958       if (!sufficient_parms_p (TREE_CHAIN (parms)))
959         continue;
960       quals = cp_type_quals (src_type);
961       if (client->quals & ~quals)
962         continue;
963       excess = quals & ~client->quals;
964       if (!best || (excess_p && !excess))
965         {
966           best = fn;
967           excess_p = excess;
968         }
969       else
970         /* Ambiguous */
971         return NULL_TREE;
972     }
973   return best;
974 }
975
976 /* Implicitly declare the special function indicated by KIND, as a
977    member of TYPE.  For copy constructors and assignment operators,
978    CONST_P indicates whether these functions should take a const
979    reference argument or a non-const reference.  Returns the
980    FUNCTION_DECL for the implicitly declared function.  */
981
982 static tree
983 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
984 {
985   tree fn;
986   tree parameter_types = void_list_node;
987   tree return_type;
988   tree fn_type;
989   tree raises = empty_except_spec;
990   tree rhs_parm_type = NULL_TREE;
991   tree this_parm;
992   tree name;
993   HOST_WIDE_INT saved_processing_template_decl;
994
995   /* Because we create declarations for implicitly declared functions
996      lazily, we may be creating the declaration for a member of TYPE
997      while in some completely different context.  However, TYPE will
998      never be a dependent class (because we never want to do lookups
999      for implicitly defined functions in a dependent class).
1000      Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1001      because we only create clones for constructors and destructors
1002      when not in a template.  */
1003   gcc_assert (!dependent_type_p (type));
1004   saved_processing_template_decl = processing_template_decl;
1005   processing_template_decl = 0;
1006
1007   type = TYPE_MAIN_VARIANT (type);
1008
1009   if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1010     {
1011       if (kind == sfk_destructor)
1012         /* See comment in check_special_function_return_type.  */
1013         return_type = build_pointer_type (void_type_node);
1014       else
1015         return_type = build_pointer_type (type);
1016     }
1017   else
1018     return_type = void_type_node;
1019
1020   switch (kind)
1021     {
1022     case sfk_destructor:
1023       /* Destructor.  */
1024       name = constructor_name (type);
1025       raises = synthesize_exception_spec (type, &locate_dtor, 0);
1026       break;
1027
1028     case sfk_constructor:
1029       /* Default constructor.  */
1030       name = constructor_name (type);
1031       raises = synthesize_exception_spec (type, &locate_ctor, 0);
1032       break;
1033
1034     case sfk_copy_constructor:
1035     case sfk_assignment_operator:
1036     {
1037       struct copy_data data;
1038
1039       data.name = NULL;
1040       data.quals = 0;
1041       if (kind == sfk_assignment_operator)
1042         {
1043           return_type = build_reference_type (type);
1044           name = ansi_assopname (NOP_EXPR);
1045           data.name = name;
1046         }
1047       else
1048         name = constructor_name (type);
1049
1050       if (const_p)
1051         {
1052           data.quals = TYPE_QUAL_CONST;
1053           rhs_parm_type = build_qualified_type (type, TYPE_QUAL_CONST);
1054         }
1055       else
1056         rhs_parm_type = type;
1057       rhs_parm_type = build_reference_type (rhs_parm_type);
1058       parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1059       raises = synthesize_exception_spec (type, &locate_copy, &data);
1060       break;
1061     }
1062     default:
1063       gcc_unreachable ();
1064     }
1065
1066   /* Create the function.  */
1067   fn_type = build_method_type_directly (type, return_type, parameter_types);
1068   if (raises)
1069     fn_type = build_exception_variant (fn_type, raises);
1070   fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1071   DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1072   if (kind == sfk_constructor || kind == sfk_copy_constructor)
1073     DECL_CONSTRUCTOR_P (fn) = 1;
1074   else if (kind == sfk_destructor)
1075     DECL_DESTRUCTOR_P (fn) = 1;
1076   else
1077     {
1078       DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1079       SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1080     }
1081   
1082   /* If pointers to member functions use the least significant bit to
1083      indicate whether a function is virtual, ensure a pointer
1084      to this function will have that bit clear.  */
1085   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1086       && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1087     DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1088
1089   /* Create the explicit arguments.  */
1090   if (rhs_parm_type)
1091     {
1092       /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1093          want its type to be included in the mangled function
1094          name.  */
1095       DECL_ARGUMENTS (fn) = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1096       TREE_READONLY (DECL_ARGUMENTS (fn)) = 1;
1097     }
1098   /* Add the "this" parameter.  */
1099   this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1100   TREE_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1101   DECL_ARGUMENTS (fn) = this_parm;
1102
1103   grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1104   set_linkage_according_to_type (type, fn);
1105   rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1106   DECL_IN_AGGR_P (fn) = 1;
1107   DECL_ARTIFICIAL (fn) = 1;
1108   DECL_NOT_REALLY_EXTERN (fn) = 1;
1109   DECL_DECLARED_INLINE_P (fn) = 1;
1110   DECL_INLINE (fn) = 1;
1111   gcc_assert (!TREE_USED (fn));
1112
1113   /* Restore PROCESSING_TEMPLATE_DECL.  */
1114   processing_template_decl = saved_processing_template_decl;
1115
1116   return fn;
1117 }
1118
1119 /* Add an implicit declaration to TYPE for the kind of function
1120    indicated by SFK.  Return the FUNCTION_DECL for the new implicit
1121    declaration.  */
1122
1123 tree
1124 lazily_declare_fn (special_function_kind sfk, tree type)
1125 {
1126   tree fn;
1127   bool const_p;
1128
1129   /* Figure out whether or not the argument has a const reference
1130      type.  */
1131   if (sfk == sfk_copy_constructor)
1132     const_p = TYPE_HAS_CONST_INIT_REF (type);
1133   else if (sfk == sfk_assignment_operator)
1134     const_p = TYPE_HAS_CONST_ASSIGN_REF (type);
1135   else
1136     /* In this case, CONST_P will be ignored.  */
1137     const_p = false;
1138   /* Declare the function.  */
1139   fn = implicitly_declare_fn (sfk, type, const_p);
1140   /* A destructor may be virtual.  */
1141   if (sfk == sfk_destructor)
1142     check_for_override (fn, type);
1143   /* Add it to CLASSTYPE_METHOD_VEC.  */
1144   add_method (type, fn, NULL_TREE);
1145   /* Add it to TYPE_METHODS.  */
1146   if (sfk == sfk_destructor
1147       && DECL_VIRTUAL_P (fn)
1148       && abi_version_at_least (2))
1149     /* The ABI requires that a virtual destructor go at the end of the
1150        vtable.  */
1151     TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1152   else
1153     {
1154       /* G++ 3.2 put the implicit destructor at the *beginning* of the
1155          TYPE_METHODS list, which cause the destructor to be emitted
1156          in an incorrect location in the vtable.  */
1157       if (warn_abi && DECL_VIRTUAL_P (fn))
1158         warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1159                  "and may change in a future version of GCC due to "
1160                  "implicit virtual destructor",
1161                  type);
1162       TREE_CHAIN (fn) = TYPE_METHODS (type);
1163       TYPE_METHODS (type) = fn;
1164     }
1165   maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1166   if (sfk == sfk_assignment_operator)
1167     CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
1168   else
1169     {
1170       /* Remember that the function has been created.  */
1171       if (sfk == sfk_constructor)
1172         CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1173       else if (sfk == sfk_copy_constructor)
1174         CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1175       else if (sfk == sfk_destructor)
1176         CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1177       /* Create appropriate clones.  */
1178       clone_function_decl (fn, /*update_method_vec=*/true);
1179     }
1180
1181   return fn;
1182 }
1183
1184 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1185    as there are artificial parms in FN.  */
1186
1187 tree
1188 skip_artificial_parms_for (tree fn, tree list)
1189 {
1190   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1191     list = TREE_CHAIN (list);
1192   else
1193     return list;
1194
1195   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1196     list = TREE_CHAIN (list);
1197   if (DECL_HAS_VTT_PARM_P (fn))
1198     list = TREE_CHAIN (list);
1199   return list;
1200 }
1201
1202 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1203    artificial parms in FN.  */
1204
1205 int
1206 num_artificial_parms_for (tree fn)
1207 {
1208   int count = 0;
1209
1210   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1211     count++;
1212   else
1213     return 0;
1214
1215   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1216     count++;
1217   if (DECL_HAS_VTT_PARM_P (fn))
1218     count++;
1219   return count;
1220 }
1221
1222
1223 #include "gt-cp-method.h"