OSDN Git Service

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