Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / cp / method.c
blob124a83ced3d3ab4c49352038f1818a5ce4fe4a00
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)
8 This file is part of GCC.
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.
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.
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/>. */
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"
42 /* Various flags to control the mangling process. */
44 enum mangling_flags
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
58 typedef enum mangling_flags mangling_flags;
60 static void do_build_assign_ref (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);
65 /* Called once to initialize method.c. */
67 void
68 init_method (void)
70 init_mangle ();
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. */
82 tree
83 make_thunk (tree function, bool this_adjusting,
84 tree fixed_offset, tree virtual_offset)
86 HOST_WIDE_INT d;
87 tree thunk;
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);
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)));
102 d = tree_low_cst (fixed_offset, 0);
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;
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)));
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;
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;
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;
159 /* Add it to the list of thunks associated with FUNCTION. */
160 TREE_CHAIN (thunk) = DECL_THUNKS (function);
161 DECL_THUNKS (function) = thunk;
163 return thunk;
166 /* Finish THUNK, a thunk decl. */
168 void
169 finish_thunk (tree thunk)
171 tree function, name;
172 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
173 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
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);
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))
189 tree cov_probe;
191 for (cov_probe = DECL_THUNKS (function);
192 cov_probe; cov_probe = TREE_CHAIN (cov_probe))
193 if (DECL_NAME (cov_probe) == name)
195 gcc_assert (!DECL_THUNKS (thunk));
196 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
197 ? THUNK_ALIAS (cov_probe) : cov_probe);
198 break;
202 DECL_NAME (thunk) = name;
203 SET_DECL_ASSEMBLER_NAME (thunk, name);
206 static GTY (()) int thunk_labelno;
208 /* Create a static alias to target. */
210 tree
211 make_alias_for (tree target, tree newid)
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))
224 DECL_NOT_REALLY_EXTERN (alias) = 1;
225 DECL_USE_TEMPLATE (alias) = 0;
226 DECL_TEMPLATE_INFO (alias) = NULL;
228 DECL_EXTERNAL (alias) = 0;
229 DECL_ARTIFICIAL (alias) = 1;
230 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
231 if (TREE_CODE (alias) == FUNCTION_DECL)
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));
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;
250 static tree
251 make_alias_for_thunk (tree function)
253 tree alias;
254 char buf[256];
256 ASM_GENERATE_INTERNAL_LABEL (buf, "LTHUNK", thunk_labelno);
257 thunk_labelno++;
259 alias = make_alias_for (function, get_identifier (buf));
261 if (!flag_syntax_only)
263 bool ok = cgraph_same_body_alias (alias, function);
264 DECL_ASSEMBLER_NAME (function);
265 gcc_assert (ok);
268 return alias;
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. */
275 void
276 use_thunk (tree thunk_fndecl, bool emit_p)
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);
283 /* We should have called finish_thunk to give it a name. */
284 gcc_assert (DECL_NAME (thunk_fndecl));
286 /* We should never be using an alias, always refer to the
287 aliased thunk. */
288 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
290 if (TREE_ASM_WRITTEN (thunk_fndecl))
291 return;
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;
299 if (DECL_THUNK_P (function))
300 /* The target is itself a thunk, process it now. */
301 use_thunk (function, emit_p);
303 /* Thunks are always addressable; they only appear in vtables. */
304 TREE_ADDRESSABLE (thunk_fndecl) = 1;
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;
313 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
314 alias = make_alias_for_thunk (function);
315 else
316 alias = function;
318 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
319 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
321 if (virtual_offset)
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);
328 else
329 virtual_value = 0;
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));
344 if (flag_syntax_only)
346 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
347 return;
350 push_to_top_level ();
352 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
353 && targetm.have_named_sections)
355 resolve_unique_section (function, 0, flag_function_sections);
357 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
359 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
361 /* Output the thunk into the same section as function. */
362 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
366 /* Set up cloned argument trees for the thunk. */
367 t = NULL_TREE;
368 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
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;
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);
384 if (!this_adjusting
385 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
386 virtual_value, alias))
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. */
393 if (varargs_function_p (function))
394 error ("generic thunk code fails for method %q#D which uses %<...%>",
395 function);
398 pop_from_top_level ();
401 /* Code for synthesizing methods which have default semantics defined. */
403 /* Generate code for default X(X&) or X(X&&) constructor. */
405 static void
406 do_build_copy_constructor (tree fndecl)
408 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
409 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
411 parm = convert_from_reference (parm);
413 if (TYPE_HAS_TRIVIAL_INIT_REF (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_INIT_REF (current_class_type))
419 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
420 finish_expr_stmt (t);
422 else
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;
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++)
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);
449 for (binfo = TYPE_BINFO (current_class_type), i = 0;
450 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
452 if (BINFO_VIRTUAL_P (base_binfo))
453 continue;
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);
464 for (; fields; fields = TREE_CHAIN (fields))
466 tree field = fields;
467 tree expr_type;
469 if (TREE_CODE (field) != FIELD_DECL)
470 continue;
472 expr_type = TREE_TYPE (field);
473 if (DECL_NAME (field))
475 if (VFIELD_NAME_P (DECL_NAME (field)))
476 continue;
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;
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)
491 int quals = cvquals;
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);
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);
504 member_init_list = tree_cons (field, init, member_init_list);
506 finish_mem_initializers (member_init_list);
510 static void
511 do_build_assign_ref (tree fndecl)
513 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
514 tree compound_stmt;
516 compound_stmt = begin_compound_stmt (0);
517 parm = convert_from_reference (parm);
519 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (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_ASSIGN_REF (current_class_type))
525 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
526 finish_expr_stmt (t);
528 else
530 tree fields;
531 int cvquals = cp_type_quals (TREE_TYPE (parm));
532 int i;
533 tree binfo, base_binfo;
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++)
539 tree converted_parm;
540 VEC(tree,gc) *parmvec;
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);
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))
562 tree comp = current_class_ref;
563 tree init = parm;
564 tree field = fields;
565 tree expr_type;
566 int quals;
568 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
569 continue;
571 expr_type = TREE_TYPE (field);
573 if (CP_TYPE_CONST_P (expr_type))
575 error ("non-static const member %q#D, can't use default "
576 "assignment operator", field);
577 continue;
579 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
581 error ("non-static reference member %q#D, can't use "
582 "default assignment operator", field);
583 continue;
586 if (DECL_NAME (field))
588 if (VFIELD_NAME_P (DECL_NAME (field)))
589 continue;
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;
598 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
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);
606 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
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);
616 finish_return_stmt (current_class_ref);
617 finish_compound_stmt (compound_stmt);
620 /* Synthesize FNDECL, a non-static member function. */
622 void
623 synthesize_method (tree fndecl)
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;
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)));
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);
644 /* We may be in the middle of deferred access check. Disable
645 it now. */
646 push_deferring_access_checks (dk_no_deferred);
648 if (! context)
649 push_to_top_level ();
650 else if (nested)
651 push_function_context ();
653 input_location = DECL_SOURCE_LOCATION (fndecl);
655 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
656 stmt = begin_function_body ();
658 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
660 do_build_assign_ref (fndecl);
661 need_body = false;
663 else if (DECL_CONSTRUCTOR_P (fndecl))
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);
672 /* If we haven't yet generated the body of the function, just
673 generate an empty compound statement. */
674 if (need_body)
676 tree compound_stmt;
677 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
678 finish_compound_stmt (compound_stmt);
681 finish_function_body (stmt);
682 expand_or_defer_fn (finish_function (0));
684 input_location = save_input_location;
686 if (! context)
687 pop_from_top_level ();
688 else if (nested)
689 pop_function_context ();
691 pop_deferring_access_checks ();
693 if (error_count != errorcount || warning_count != warningcount)
694 inform (input_location, "synthesized method %qD first required here ",
695 fndecl);
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. */
704 static tree
705 synthesize_exception_spec (tree type, tree (*extractor) (tree, void*),
706 void *client)
708 tree raises = empty_except_spec;
709 tree fields = TYPE_FIELDS (type);
710 tree binfo, base_binfo;
711 int i;
713 for (binfo = TYPE_BINFO (type), i = 0;
714 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
716 tree fn = (*extractor) (BINFO_TYPE (base_binfo), client);
717 if (fn)
719 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
721 raises = merge_exception_specifiers (raises, fn_raises);
724 for (; fields; fields = TREE_CHAIN (fields))
726 tree type = TREE_TYPE (fields);
727 tree fn;
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;
736 fn = (*extractor) (type, client);
737 if (fn)
739 tree fn_raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
741 raises = merge_exception_specifiers (raises, fn_raises);
744 return raises;
747 /* Locate the dtor of TYPE. */
749 tree
750 locate_dtor (tree type, void *client ATTRIBUTE_UNUSED)
752 return CLASSTYPE_DESTRUCTORS (type);
755 /* Locate the default ctor of TYPE. */
757 tree
758 locate_ctor (tree type, void *client ATTRIBUTE_UNUSED)
760 tree fns;
762 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
763 return NULL_TREE;
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);
770 for (fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
772 tree fn = OVL_CURRENT (fns);
773 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
775 parms = skip_artificial_parms_for (fn, parms);
777 if (sufficient_parms_p (parms))
778 return fn;
780 gcc_unreachable ();
783 struct copy_data
785 tree name;
786 int quals;
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. */
793 tree
794 locate_copy (tree type, void *client_)
796 struct copy_data *client = (struct copy_data *)client_;
797 tree fns;
798 tree best = NULL_TREE;
799 bool excess_p = false;
801 if (client->name)
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);
809 else if (TYPE_HAS_INIT_REF (type))
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);
819 else
820 return NULL_TREE;
821 for (; fns; fns = OVL_NEXT (fns))
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;
829 parms = skip_artificial_parms_for (fn, parms);
830 if (!parms)
831 continue;
832 src_type = non_reference (TREE_VALUE (parms));
834 if (src_type == error_mark_node)
835 return NULL_TREE;
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))
847 best = fn;
848 excess_p = excess;
850 else
851 /* Ambiguous */
852 return NULL_TREE;
854 return best;
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. */
863 static tree
864 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
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;
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;
888 type = TYPE_MAIN_VARIANT (type);
890 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
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);
898 else
899 return_type = void_type_node;
901 switch (kind)
903 case sfk_destructor:
904 /* Destructor. */
905 name = constructor_name (type);
906 raises = synthesize_exception_spec (type, &locate_dtor, 0);
907 break;
909 case sfk_constructor:
910 /* Default constructor. */
911 name = constructor_name (type);
912 raises = synthesize_exception_spec (type, &locate_ctor, 0);
913 break;
915 case sfk_copy_constructor:
916 case sfk_assignment_operator:
917 case sfk_move_constructor:
919 struct copy_data data;
921 data.name = NULL;
922 data.quals = 0;
923 if (kind == sfk_assignment_operator)
925 return_type = build_reference_type (type);
926 name = ansi_assopname (NOP_EXPR);
927 data.name = name;
929 else
930 name = constructor_name (type);
932 if (const_p)
934 data.quals = TYPE_QUAL_CONST;
935 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
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;
946 default:
947 gcc_unreachable ();
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
963 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
964 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
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;
974 /* Create the explicit arguments. */
975 if (rhs_parm_type)
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;
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;
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));
998 /* Restore PROCESSING_TEMPLATE_DECL. */
999 processing_template_decl = saved_processing_template_decl;
1001 return fn;
1004 /* Gives any errors about defaulted functions which need to be deferred
1005 until the containing class is complete. */
1007 void
1008 defaulted_late_check (tree fn)
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);
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))))
1021 error ("defaulted declaration %q+D", fn);
1022 error_at (DECL_SOURCE_LOCATION (fn),
1023 "does not match expected signature %qD", implicit_fn);
1027 /* Returns true iff FN can be explicitly defaulted, and gives any
1028 errors if defaulting FN is ill-formed. */
1030 bool
1031 defaultable_fn_check (tree fn)
1033 special_function_kind kind = sfk_none;
1035 if (DECL_CONSTRUCTOR_P (fn))
1037 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1038 kind = sfk_constructor;
1039 else if (copy_fn_p (fn) > 0
1040 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1041 == void_list_node))
1042 kind = sfk_copy_constructor;
1043 else if (move_fn_p (fn))
1044 kind = sfk_move_constructor;
1046 else if (DECL_DESTRUCTOR_P (fn))
1047 kind = sfk_destructor;
1048 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1049 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
1050 && copy_fn_p (fn))
1051 kind = sfk_assignment_operator;
1053 if (kind == sfk_none)
1055 error ("%qD cannot be defaulted", fn);
1056 return false;
1058 else
1060 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1061 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1062 if (TREE_PURPOSE (t))
1064 error ("defaulted function %q+D with default argument", fn);
1065 break;
1067 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1069 if (DECL_NONCONVERTING_P (fn))
1070 error ("%qD declared explicit cannot be defaulted in the class "
1071 "body", fn);
1072 if (current_access_specifier != access_public_node)
1073 error ("%qD declared with non-public access cannot be defaulted "
1074 "in the class body", fn);
1075 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1076 error ("function %q+D defaulted on its first declaration "
1077 "must not have an exception-specification", fn);
1078 if (DECL_VIRTUAL_P (fn))
1079 error ("%qD declared virtual cannot be defaulted in the class "
1080 "body", fn);
1082 else if (!processing_template_decl)
1083 defaulted_late_check (fn);
1085 return true;
1089 /* Add an implicit declaration to TYPE for the kind of function
1090 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1091 declaration. */
1093 tree
1094 lazily_declare_fn (special_function_kind sfk, tree type)
1096 tree fn;
1097 bool const_p;
1099 /* Figure out whether or not the argument has a const reference
1100 type. */
1101 if (sfk == sfk_copy_constructor)
1102 const_p = TYPE_HAS_CONST_INIT_REF (type);
1103 else if (sfk == sfk_assignment_operator)
1104 const_p = TYPE_HAS_CONST_ASSIGN_REF (type);
1105 else
1106 /* In this case, CONST_P will be ignored. */
1107 const_p = false;
1108 /* Declare the function. */
1109 fn = implicitly_declare_fn (sfk, type, const_p);
1110 /* A destructor may be virtual. */
1111 if (sfk == sfk_destructor)
1112 check_for_override (fn, type);
1113 /* Add it to CLASSTYPE_METHOD_VEC. */
1114 add_method (type, fn, NULL_TREE);
1115 /* Add it to TYPE_METHODS. */
1116 if (sfk == sfk_destructor
1117 && DECL_VIRTUAL_P (fn)
1118 && abi_version_at_least (2))
1119 /* The ABI requires that a virtual destructor go at the end of the
1120 vtable. */
1121 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1122 else
1124 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1125 TYPE_METHODS list, which cause the destructor to be emitted
1126 in an incorrect location in the vtable. */
1127 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1128 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1129 "and may change in a future version of GCC due to "
1130 "implicit virtual destructor",
1131 type);
1132 TREE_CHAIN (fn) = TYPE_METHODS (type);
1133 TYPE_METHODS (type) = fn;
1135 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1136 if (sfk == sfk_assignment_operator)
1137 CLASSTYPE_LAZY_ASSIGNMENT_OP (type) = 0;
1138 else
1140 /* Remember that the function has been created. */
1141 if (sfk == sfk_constructor)
1142 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1143 else if (sfk == sfk_copy_constructor)
1144 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1145 else if (sfk == sfk_move_constructor)
1146 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1147 else if (sfk == sfk_destructor)
1148 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1149 /* Create appropriate clones. */
1150 clone_function_decl (fn, /*update_method_vec=*/true);
1153 return fn;
1156 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1157 as there are artificial parms in FN. */
1159 tree
1160 skip_artificial_parms_for (const_tree fn, tree list)
1162 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1163 list = TREE_CHAIN (list);
1164 else
1165 return list;
1167 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1168 list = TREE_CHAIN (list);
1169 if (DECL_HAS_VTT_PARM_P (fn))
1170 list = TREE_CHAIN (list);
1171 return list;
1174 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1175 artificial parms in FN. */
1178 num_artificial_parms_for (const_tree fn)
1180 int count = 0;
1182 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1183 count++;
1184 else
1185 return 0;
1187 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1188 count++;
1189 if (DECL_HAS_VTT_PARM_P (fn))
1190 count++;
1191 return count;
1195 #include "gt-cp-method.h"