DR 1170 PR c++/51213
[gcc.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
4 2012
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 /* High-level class interface. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "cgraph.h"
38 #include "dumpfile.h"
39 #include "splay-tree.h"
40 #include "pointer-set.h"
41
42 /* The number of nested classes being processed. If we are not in the
43 scope of any class, this is zero. */
44
45 int current_class_depth;
46
47 /* In order to deal with nested classes, we keep a stack of classes.
48 The topmost entry is the innermost class, and is the entry at index
49 CURRENT_CLASS_DEPTH */
50
51 typedef struct class_stack_node {
52 /* The name of the class. */
53 tree name;
54
55 /* The _TYPE node for the class. */
56 tree type;
57
58 /* The access specifier pending for new declarations in the scope of
59 this class. */
60 tree access;
61
62 /* If were defining TYPE, the names used in this class. */
63 splay_tree names_used;
64
65 /* Nonzero if this class is no longer open, because of a call to
66 push_to_top_level. */
67 size_t hidden;
68 }* class_stack_node_t;
69
70 typedef struct vtbl_init_data_s
71 {
72 /* The base for which we're building initializers. */
73 tree binfo;
74 /* The type of the most-derived type. */
75 tree derived;
76 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
77 unless ctor_vtbl_p is true. */
78 tree rtti_binfo;
79 /* The negative-index vtable initializers built up so far. These
80 are in order from least negative index to most negative index. */
81 VEC(constructor_elt,gc) *inits;
82 /* The binfo for the virtual base for which we're building
83 vcall offset initializers. */
84 tree vbase;
85 /* The functions in vbase for which we have already provided vcall
86 offsets. */
87 VEC(tree,gc) *fns;
88 /* The vtable index of the next vcall or vbase offset. */
89 tree index;
90 /* Nonzero if we are building the initializer for the primary
91 vtable. */
92 int primary_vtbl_p;
93 /* Nonzero if we are building the initializer for a construction
94 vtable. */
95 int ctor_vtbl_p;
96 /* True when adding vcall offset entries to the vtable. False when
97 merely computing the indices. */
98 bool generate_vcall_entries;
99 } vtbl_init_data;
100
101 /* The type of a function passed to walk_subobject_offsets. */
102 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
103
104 /* The stack itself. This is a dynamically resized array. The
105 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
106 static int current_class_stack_size;
107 static class_stack_node_t current_class_stack;
108
109 /* The size of the largest empty class seen in this translation unit. */
110 static GTY (()) tree sizeof_biggest_empty_class;
111
112 /* An array of all local classes present in this translation unit, in
113 declaration order. */
114 VEC(tree,gc) *local_classes;
115
116 static tree get_vfield_name (tree);
117 static void finish_struct_anon (tree);
118 static tree get_vtable_name (tree);
119 static tree get_basefndecls (tree, tree);
120 static int build_primary_vtable (tree, tree);
121 static int build_secondary_vtable (tree);
122 static void finish_vtbls (tree);
123 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
124 static void finish_struct_bits (tree);
125 static int alter_access (tree, tree, tree);
126 static void handle_using_decl (tree, tree);
127 static tree dfs_modify_vtables (tree, void *);
128 static tree modify_all_vtables (tree, tree);
129 static void determine_primary_bases (tree);
130 static void finish_struct_methods (tree);
131 static void maybe_warn_about_overly_private_class (tree);
132 static int method_name_cmp (const void *, const void *);
133 static int resort_method_name_cmp (const void *, const void *);
134 static void add_implicitly_declared_members (tree, int, int);
135 static tree fixed_type_or_null (tree, int *, int *);
136 static tree build_simple_base_path (tree expr, tree binfo);
137 static tree build_vtbl_ref_1 (tree, tree);
138 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
139 VEC(constructor_elt,gc) **);
140 static int count_fields (tree);
141 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
142 static void insert_into_classtype_sorted_fields (tree, tree, int);
143 static bool check_bitfield_decl (tree);
144 static void check_field_decl (tree, tree, int *, int *, int *);
145 static void check_field_decls (tree, tree *, int *, int *);
146 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
147 static void build_base_fields (record_layout_info, splay_tree, tree *);
148 static void check_methods (tree);
149 static void remove_zero_width_bit_fields (tree);
150 static void check_bases (tree, int *, int *);
151 static void check_bases_and_members (tree);
152 static tree create_vtable_ptr (tree, tree *);
153 static void include_empty_classes (record_layout_info);
154 static void layout_class_type (tree, tree *);
155 static void propagate_binfo_offsets (tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider_pre (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static tree get_primary_binfo (tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_class_hierarchy_1 (FILE *, int, tree);
172 static void dump_array (FILE *, tree);
173 static void dump_vtable (tree, tree, tree);
174 static void dump_vtt (tree, tree);
175 static void dump_thunk (FILE *, int, tree);
176 static tree build_vtable (tree, tree, tree);
177 static void initialize_vtable (tree, VEC(constructor_elt,gc) *);
178 static void layout_nonempty_base_or_field (record_layout_info,
179 tree, tree, splay_tree);
180 static tree end_of_class (tree, int);
181 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
182 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 VEC(constructor_elt,gc) **);
184 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
185 VEC(constructor_elt,gc) **);
186 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
187 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static void build_ctor_vtbl_group (tree, tree);
192 static void build_vtt (tree);
193 static tree binfo_ctor_vtable (tree);
194 static void build_vtt_inits (tree, tree, VEC(constructor_elt,gc) **, tree *);
195 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
196 static tree dfs_fixup_binfo_vtbls (tree, void *);
197 static int record_subobject_offset (tree, tree, splay_tree);
198 static int check_subobject_offset (tree, tree, splay_tree);
199 static int walk_subobject_offsets (tree, subobject_offset_fn,
200 tree, splay_tree, tree, int);
201 static void record_subobject_offsets (tree, tree, splay_tree, bool);
202 static int layout_conflict_p (tree, tree, splay_tree, int);
203 static int splay_tree_compare_integer_csts (splay_tree_key k1,
204 splay_tree_key k2);
205 static void warn_about_ambiguous_bases (tree);
206 static bool type_requires_array_cookie (tree);
207 static bool contains_empty_class_p (tree);
208 static bool base_derived_from (tree, tree);
209 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
210 static tree end_of_base (tree);
211 static tree get_vcall_index (tree, tree);
212
213 /* Variables shared between class.c and call.c. */
214
215 #ifdef GATHER_STATISTICS
216 int n_vtables = 0;
217 int n_vtable_entries = 0;
218 int n_vtable_searches = 0;
219 int n_vtable_elems = 0;
220 int n_convert_harshness = 0;
221 int n_compute_conversion_costs = 0;
222 int n_inner_fields_searched = 0;
223 #endif
224
225 /* Convert to or from a base subobject. EXPR is an expression of type
226 `A' or `A*', an expression of type `B' or `B*' is returned. To
227 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
228 the B base instance within A. To convert base A to derived B, CODE
229 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
230 In this latter case, A must not be a morally virtual base of B.
231 NONNULL is true if EXPR is known to be non-NULL (this is only
232 needed when EXPR is of pointer type). CV qualifiers are preserved
233 from EXPR. */
234
235 tree
236 build_base_path (enum tree_code code,
237 tree expr,
238 tree binfo,
239 int nonnull,
240 tsubst_flags_t complain)
241 {
242 tree v_binfo = NULL_TREE;
243 tree d_binfo = NULL_TREE;
244 tree probe;
245 tree offset;
246 tree target_type;
247 tree null_test = NULL;
248 tree ptr_target_type;
249 int fixed_type_p;
250 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
251 bool has_empty = false;
252 bool virtual_access;
253
254 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
255 return error_mark_node;
256
257 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
258 {
259 d_binfo = probe;
260 if (is_empty_class (BINFO_TYPE (probe)))
261 has_empty = true;
262 if (!v_binfo && BINFO_VIRTUAL_P (probe))
263 v_binfo = probe;
264 }
265
266 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
267 if (want_pointer)
268 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
269
270 if (code == PLUS_EXPR
271 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
272 {
273 /* This can happen when adjust_result_of_qualified_name_lookup can't
274 find a unique base binfo in a call to a member function. We
275 couldn't give the diagnostic then since we might have been calling
276 a static member function, so we do it now. */
277 if (complain & tf_error)
278 {
279 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
280 ba_unique, NULL);
281 gcc_assert (base == error_mark_node);
282 }
283 return error_mark_node;
284 }
285
286 gcc_assert ((code == MINUS_EXPR
287 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
288 || code == PLUS_EXPR);
289
290 if (binfo == d_binfo)
291 /* Nothing to do. */
292 return expr;
293
294 if (code == MINUS_EXPR && v_binfo)
295 {
296 if (complain & tf_error)
297 error ("cannot convert from base %qT to derived type %qT via "
298 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
299 BINFO_TYPE (v_binfo));
300 return error_mark_node;
301 }
302
303 if (!want_pointer)
304 /* This must happen before the call to save_expr. */
305 expr = cp_build_addr_expr (expr, complain);
306 else
307 expr = mark_rvalue_use (expr);
308
309 offset = BINFO_OFFSET (binfo);
310 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
311 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
312 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
313 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
314 expression returned matches the input. */
315 target_type = cp_build_qualified_type
316 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
317 ptr_target_type = build_pointer_type (target_type);
318
319 /* Do we need to look in the vtable for the real offset? */
320 virtual_access = (v_binfo && fixed_type_p <= 0);
321
322 /* Don't bother with the calculations inside sizeof; they'll ICE if the
323 source type is incomplete and the pointer value doesn't matter. In a
324 template (even in fold_non_dependent_expr), we don't have vtables set
325 up properly yet, and the value doesn't matter there either; we're just
326 interested in the result of overload resolution. */
327 if (cp_unevaluated_operand != 0
328 || in_template_function ())
329 {
330 expr = build_nop (ptr_target_type, expr);
331 if (!want_pointer)
332 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
333 return expr;
334 }
335
336 /* If we're in an NSDMI, we don't have the full constructor context yet
337 that we need for converting to a virtual base, so just build a stub
338 CONVERT_EXPR and expand it later in bot_replace. */
339 if (virtual_access && fixed_type_p < 0
340 && current_scope () != current_function_decl)
341 {
342 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
343 CONVERT_EXPR_VBASE_PATH (expr) = true;
344 if (!want_pointer)
345 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
346 return expr;
347 }
348
349 /* Do we need to check for a null pointer? */
350 if (want_pointer && !nonnull)
351 {
352 /* If we know the conversion will not actually change the value
353 of EXPR, then we can avoid testing the expression for NULL.
354 We have to avoid generating a COMPONENT_REF for a base class
355 field, because other parts of the compiler know that such
356 expressions are always non-NULL. */
357 if (!virtual_access && integer_zerop (offset))
358 return build_nop (ptr_target_type, expr);
359 null_test = error_mark_node;
360 }
361
362 /* Protect against multiple evaluation if necessary. */
363 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
364 expr = save_expr (expr);
365
366 /* Now that we've saved expr, build the real null test. */
367 if (null_test)
368 {
369 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
370 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
371 expr, zero);
372 }
373
374 /* If this is a simple base reference, express it as a COMPONENT_REF. */
375 if (code == PLUS_EXPR && !virtual_access
376 /* We don't build base fields for empty bases, and they aren't very
377 interesting to the optimizers anyway. */
378 && !has_empty)
379 {
380 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
381 expr = build_simple_base_path (expr, binfo);
382 if (want_pointer)
383 expr = build_address (expr);
384 target_type = TREE_TYPE (expr);
385 goto out;
386 }
387
388 if (virtual_access)
389 {
390 /* Going via virtual base V_BINFO. We need the static offset
391 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
392 V_BINFO. That offset is an entry in D_BINFO's vtable. */
393 tree v_offset;
394
395 if (fixed_type_p < 0 && in_base_initializer)
396 {
397 /* In a base member initializer, we cannot rely on the
398 vtable being set up. We have to indirect via the
399 vtt_parm. */
400 tree t;
401
402 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
403 t = build_pointer_type (t);
404 v_offset = convert (t, current_vtt_parm);
405 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
406 }
407 else
408 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
409 complain),
410 TREE_TYPE (TREE_TYPE (expr)));
411
412 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
413 v_offset = build1 (NOP_EXPR,
414 build_pointer_type (ptrdiff_type_node),
415 v_offset);
416 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
417 TREE_CONSTANT (v_offset) = 1;
418
419 offset = convert_to_integer (ptrdiff_type_node,
420 size_diffop_loc (input_location, offset,
421 BINFO_OFFSET (v_binfo)));
422
423 if (!integer_zerop (offset))
424 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
425
426 if (fixed_type_p < 0)
427 /* Negative fixed_type_p means this is a constructor or destructor;
428 virtual base layout is fixed in in-charge [cd]tors, but not in
429 base [cd]tors. */
430 offset = build3 (COND_EXPR, ptrdiff_type_node,
431 build2 (EQ_EXPR, boolean_type_node,
432 current_in_charge_parm, integer_zero_node),
433 v_offset,
434 convert_to_integer (ptrdiff_type_node,
435 BINFO_OFFSET (binfo)));
436 else
437 offset = v_offset;
438 }
439
440 if (want_pointer)
441 target_type = ptr_target_type;
442
443 expr = build1 (NOP_EXPR, ptr_target_type, expr);
444
445 if (!integer_zerop (offset))
446 {
447 offset = fold_convert (sizetype, offset);
448 if (code == MINUS_EXPR)
449 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
450 expr = fold_build_pointer_plus (expr, offset);
451 }
452 else
453 null_test = NULL;
454
455 if (!want_pointer)
456 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
457
458 out:
459 if (null_test)
460 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
461 build_zero_cst (target_type));
462
463 return expr;
464 }
465
466 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
467 Perform a derived-to-base conversion by recursively building up a
468 sequence of COMPONENT_REFs to the appropriate base fields. */
469
470 static tree
471 build_simple_base_path (tree expr, tree binfo)
472 {
473 tree type = BINFO_TYPE (binfo);
474 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
475 tree field;
476
477 if (d_binfo == NULL_TREE)
478 {
479 tree temp;
480
481 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
482
483 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
484 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
485 an lvalue in the front end; only _DECLs and _REFs are lvalues
486 in the back end. */
487 temp = unary_complex_lvalue (ADDR_EXPR, expr);
488 if (temp)
489 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
490
491 return expr;
492 }
493
494 /* Recurse. */
495 expr = build_simple_base_path (expr, d_binfo);
496
497 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
498 field; field = DECL_CHAIN (field))
499 /* Is this the base field created by build_base_field? */
500 if (TREE_CODE (field) == FIELD_DECL
501 && DECL_FIELD_IS_BASE (field)
502 && TREE_TYPE (field) == type
503 /* If we're looking for a field in the most-derived class,
504 also check the field offset; we can have two base fields
505 of the same type if one is an indirect virtual base and one
506 is a direct non-virtual base. */
507 && (BINFO_INHERITANCE_CHAIN (d_binfo)
508 || tree_int_cst_equal (byte_position (field),
509 BINFO_OFFSET (binfo))))
510 {
511 /* We don't use build_class_member_access_expr here, as that
512 has unnecessary checks, and more importantly results in
513 recursive calls to dfs_walk_once. */
514 int type_quals = cp_type_quals (TREE_TYPE (expr));
515
516 expr = build3 (COMPONENT_REF,
517 cp_build_qualified_type (type, type_quals),
518 expr, field, NULL_TREE);
519 expr = fold_if_not_in_template (expr);
520
521 /* Mark the expression const or volatile, as appropriate.
522 Even though we've dealt with the type above, we still have
523 to mark the expression itself. */
524 if (type_quals & TYPE_QUAL_CONST)
525 TREE_READONLY (expr) = 1;
526 if (type_quals & TYPE_QUAL_VOLATILE)
527 TREE_THIS_VOLATILE (expr) = 1;
528
529 return expr;
530 }
531
532 /* Didn't find the base field?!? */
533 gcc_unreachable ();
534 }
535
536 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
537 type is a class type or a pointer to a class type. In the former
538 case, TYPE is also a class type; in the latter it is another
539 pointer type. If CHECK_ACCESS is true, an error message is emitted
540 if TYPE is inaccessible. If OBJECT has pointer type, the value is
541 assumed to be non-NULL. */
542
543 tree
544 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
545 tsubst_flags_t complain)
546 {
547 tree binfo;
548 tree object_type;
549 base_access access;
550
551 if (TYPE_PTR_P (TREE_TYPE (object)))
552 {
553 object_type = TREE_TYPE (TREE_TYPE (object));
554 type = TREE_TYPE (type);
555 }
556 else
557 object_type = TREE_TYPE (object);
558
559 access = check_access ? ba_check : ba_unique;
560 if (!(complain & tf_error))
561 access |= ba_quiet;
562 binfo = lookup_base (object_type, type,
563 access,
564 NULL);
565 if (!binfo || binfo == error_mark_node)
566 return error_mark_node;
567
568 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
569 }
570
571 /* EXPR is an expression with unqualified class type. BASE is a base
572 binfo of that class type. Returns EXPR, converted to the BASE
573 type. This function assumes that EXPR is the most derived class;
574 therefore virtual bases can be found at their static offsets. */
575
576 tree
577 convert_to_base_statically (tree expr, tree base)
578 {
579 tree expr_type;
580
581 expr_type = TREE_TYPE (expr);
582 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
583 {
584 /* If this is a non-empty base, use a COMPONENT_REF. */
585 if (!is_empty_class (BINFO_TYPE (base)))
586 return build_simple_base_path (expr, base);
587
588 /* We use fold_build2 and fold_convert below to simplify the trees
589 provided to the optimizers. It is not safe to call these functions
590 when processing a template because they do not handle C++-specific
591 trees. */
592 gcc_assert (!processing_template_decl);
593 expr = cp_build_addr_expr (expr, tf_warning_or_error);
594 if (!integer_zerop (BINFO_OFFSET (base)))
595 expr = fold_build_pointer_plus_loc (input_location,
596 expr, BINFO_OFFSET (base));
597 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
598 expr = build_fold_indirect_ref_loc (input_location, expr);
599 }
600
601 return expr;
602 }
603
604 \f
605 tree
606 build_vfield_ref (tree datum, tree type)
607 {
608 tree vfield, vcontext;
609
610 if (datum == error_mark_node)
611 return error_mark_node;
612
613 /* First, convert to the requested type. */
614 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
615 datum = convert_to_base (datum, type, /*check_access=*/false,
616 /*nonnull=*/true, tf_warning_or_error);
617
618 /* Second, the requested type may not be the owner of its own vptr.
619 If not, convert to the base class that owns it. We cannot use
620 convert_to_base here, because VCONTEXT may appear more than once
621 in the inheritance hierarchy of TYPE, and thus direct conversion
622 between the types may be ambiguous. Following the path back up
623 one step at a time via primary bases avoids the problem. */
624 vfield = TYPE_VFIELD (type);
625 vcontext = DECL_CONTEXT (vfield);
626 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
627 {
628 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
629 type = TREE_TYPE (datum);
630 }
631
632 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
633 }
634
635 /* Given an object INSTANCE, return an expression which yields the
636 vtable element corresponding to INDEX. There are many special
637 cases for INSTANCE which we take care of here, mainly to avoid
638 creating extra tree nodes when we don't have to. */
639
640 static tree
641 build_vtbl_ref_1 (tree instance, tree idx)
642 {
643 tree aref;
644 tree vtbl = NULL_TREE;
645
646 /* Try to figure out what a reference refers to, and
647 access its virtual function table directly. */
648
649 int cdtorp = 0;
650 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
651
652 tree basetype = non_reference (TREE_TYPE (instance));
653
654 if (fixed_type && !cdtorp)
655 {
656 tree binfo = lookup_base (fixed_type, basetype,
657 ba_unique | ba_quiet, NULL);
658 if (binfo)
659 vtbl = unshare_expr (BINFO_VTABLE (binfo));
660 }
661
662 if (!vtbl)
663 vtbl = build_vfield_ref (instance, basetype);
664
665 aref = build_array_ref (input_location, vtbl, idx);
666 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
667
668 return aref;
669 }
670
671 tree
672 build_vtbl_ref (tree instance, tree idx)
673 {
674 tree aref = build_vtbl_ref_1 (instance, idx);
675
676 return aref;
677 }
678
679 /* Given a stable object pointer INSTANCE_PTR, return an expression which
680 yields a function pointer corresponding to vtable element INDEX. */
681
682 tree
683 build_vfn_ref (tree instance_ptr, tree idx)
684 {
685 tree aref;
686
687 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
688 tf_warning_or_error),
689 idx);
690
691 /* When using function descriptors, the address of the
692 vtable entry is treated as a function pointer. */
693 if (TARGET_VTABLE_USES_DESCRIPTORS)
694 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
695 cp_build_addr_expr (aref, tf_warning_or_error));
696
697 /* Remember this as a method reference, for later devirtualization. */
698 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
699
700 return aref;
701 }
702
703 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
704 for the given TYPE. */
705
706 static tree
707 get_vtable_name (tree type)
708 {
709 return mangle_vtbl_for_type (type);
710 }
711
712 /* DECL is an entity associated with TYPE, like a virtual table or an
713 implicitly generated constructor. Determine whether or not DECL
714 should have external or internal linkage at the object file
715 level. This routine does not deal with COMDAT linkage and other
716 similar complexities; it simply sets TREE_PUBLIC if it possible for
717 entities in other translation units to contain copies of DECL, in
718 the abstract. */
719
720 void
721 set_linkage_according_to_type (tree type ATTRIBUTE_UNUSED, tree decl)
722 {
723 TREE_PUBLIC (decl) = 1;
724 determine_visibility (decl);
725 }
726
727 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
728 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
729 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
730
731 static tree
732 build_vtable (tree class_type, tree name, tree vtable_type)
733 {
734 tree decl;
735
736 decl = build_lang_decl (VAR_DECL, name, vtable_type);
737 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
738 now to avoid confusion in mangle_decl. */
739 SET_DECL_ASSEMBLER_NAME (decl, name);
740 DECL_CONTEXT (decl) = class_type;
741 DECL_ARTIFICIAL (decl) = 1;
742 TREE_STATIC (decl) = 1;
743 TREE_READONLY (decl) = 1;
744 DECL_VIRTUAL_P (decl) = 1;
745 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
746 DECL_VTABLE_OR_VTT_P (decl) = 1;
747 /* At one time the vtable info was grabbed 2 words at a time. This
748 fails on sparc unless you have 8-byte alignment. (tiemann) */
749 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
750 DECL_ALIGN (decl));
751 set_linkage_according_to_type (class_type, decl);
752 /* The vtable has not been defined -- yet. */
753 DECL_EXTERNAL (decl) = 1;
754 DECL_NOT_REALLY_EXTERN (decl) = 1;
755
756 /* Mark the VAR_DECL node representing the vtable itself as a
757 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
758 is rather important that such things be ignored because any
759 effort to actually generate DWARF for them will run into
760 trouble when/if we encounter code like:
761
762 #pragma interface
763 struct S { virtual void member (); };
764
765 because the artificial declaration of the vtable itself (as
766 manufactured by the g++ front end) will say that the vtable is
767 a static member of `S' but only *after* the debug output for
768 the definition of `S' has already been output. This causes
769 grief because the DWARF entry for the definition of the vtable
770 will try to refer back to an earlier *declaration* of the
771 vtable as a static member of `S' and there won't be one. We
772 might be able to arrange to have the "vtable static member"
773 attached to the member list for `S' before the debug info for
774 `S' get written (which would solve the problem) but that would
775 require more intrusive changes to the g++ front end. */
776 DECL_IGNORED_P (decl) = 1;
777
778 return decl;
779 }
780
781 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
782 or even complete. If this does not exist, create it. If COMPLETE is
783 nonzero, then complete the definition of it -- that will render it
784 impossible to actually build the vtable, but is useful to get at those
785 which are known to exist in the runtime. */
786
787 tree
788 get_vtable_decl (tree type, int complete)
789 {
790 tree decl;
791
792 if (CLASSTYPE_VTABLES (type))
793 return CLASSTYPE_VTABLES (type);
794
795 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
796 CLASSTYPE_VTABLES (type) = decl;
797
798 if (complete)
799 {
800 DECL_EXTERNAL (decl) = 1;
801 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
802 }
803
804 return decl;
805 }
806
807 /* Build the primary virtual function table for TYPE. If BINFO is
808 non-NULL, build the vtable starting with the initial approximation
809 that it is the same as the one which is the head of the association
810 list. Returns a nonzero value if a new vtable is actually
811 created. */
812
813 static int
814 build_primary_vtable (tree binfo, tree type)
815 {
816 tree decl;
817 tree virtuals;
818
819 decl = get_vtable_decl (type, /*complete=*/0);
820
821 if (binfo)
822 {
823 if (BINFO_NEW_VTABLE_MARKED (binfo))
824 /* We have already created a vtable for this base, so there's
825 no need to do it again. */
826 return 0;
827
828 virtuals = copy_list (BINFO_VIRTUALS (binfo));
829 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
830 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
831 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
832 }
833 else
834 {
835 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
836 virtuals = NULL_TREE;
837 }
838
839 #ifdef GATHER_STATISTICS
840 n_vtables += 1;
841 n_vtable_elems += list_length (virtuals);
842 #endif
843
844 /* Initialize the association list for this type, based
845 on our first approximation. */
846 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
847 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
848 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
849 return 1;
850 }
851
852 /* Give BINFO a new virtual function table which is initialized
853 with a skeleton-copy of its original initialization. The only
854 entry that changes is the `delta' entry, so we can really
855 share a lot of structure.
856
857 FOR_TYPE is the most derived type which caused this table to
858 be needed.
859
860 Returns nonzero if we haven't met BINFO before.
861
862 The order in which vtables are built (by calling this function) for
863 an object must remain the same, otherwise a binary incompatibility
864 can result. */
865
866 static int
867 build_secondary_vtable (tree binfo)
868 {
869 if (BINFO_NEW_VTABLE_MARKED (binfo))
870 /* We already created a vtable for this base. There's no need to
871 do it again. */
872 return 0;
873
874 /* Remember that we've created a vtable for this BINFO, so that we
875 don't try to do so again. */
876 SET_BINFO_NEW_VTABLE_MARKED (binfo);
877
878 /* Make fresh virtual list, so we can smash it later. */
879 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
880
881 /* Secondary vtables are laid out as part of the same structure as
882 the primary vtable. */
883 BINFO_VTABLE (binfo) = NULL_TREE;
884 return 1;
885 }
886
887 /* Create a new vtable for BINFO which is the hierarchy dominated by
888 T. Return nonzero if we actually created a new vtable. */
889
890 static int
891 make_new_vtable (tree t, tree binfo)
892 {
893 if (binfo == TYPE_BINFO (t))
894 /* In this case, it is *type*'s vtable we are modifying. We start
895 with the approximation that its vtable is that of the
896 immediate base class. */
897 return build_primary_vtable (binfo, t);
898 else
899 /* This is our very own copy of `basetype' to play with. Later,
900 we will fill in all the virtual functions that override the
901 virtual functions in these base classes which are not defined
902 by the current type. */
903 return build_secondary_vtable (binfo);
904 }
905
906 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
907 (which is in the hierarchy dominated by T) list FNDECL as its
908 BV_FN. DELTA is the required constant adjustment from the `this'
909 pointer where the vtable entry appears to the `this' required when
910 the function is actually called. */
911
912 static void
913 modify_vtable_entry (tree t,
914 tree binfo,
915 tree fndecl,
916 tree delta,
917 tree *virtuals)
918 {
919 tree v;
920
921 v = *virtuals;
922
923 if (fndecl != BV_FN (v)
924 || !tree_int_cst_equal (delta, BV_DELTA (v)))
925 {
926 /* We need a new vtable for BINFO. */
927 if (make_new_vtable (t, binfo))
928 {
929 /* If we really did make a new vtable, we also made a copy
930 of the BINFO_VIRTUALS list. Now, we have to find the
931 corresponding entry in that list. */
932 *virtuals = BINFO_VIRTUALS (binfo);
933 while (BV_FN (*virtuals) != BV_FN (v))
934 *virtuals = TREE_CHAIN (*virtuals);
935 v = *virtuals;
936 }
937
938 BV_DELTA (v) = delta;
939 BV_VCALL_INDEX (v) = NULL_TREE;
940 BV_FN (v) = fndecl;
941 }
942 }
943
944 \f
945 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
946 the USING_DECL naming METHOD. Returns true if the method could be
947 added to the method vec. */
948
949 bool
950 add_method (tree type, tree method, tree using_decl)
951 {
952 unsigned slot;
953 tree overload;
954 bool template_conv_p = false;
955 bool conv_p;
956 VEC(tree,gc) *method_vec;
957 bool complete_p;
958 bool insert_p = false;
959 tree current_fns;
960 tree fns;
961
962 if (method == error_mark_node)
963 return false;
964
965 complete_p = COMPLETE_TYPE_P (type);
966 conv_p = DECL_CONV_FN_P (method);
967 if (conv_p)
968 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
969 && DECL_TEMPLATE_CONV_FN_P (method));
970
971 method_vec = CLASSTYPE_METHOD_VEC (type);
972 if (!method_vec)
973 {
974 /* Make a new method vector. We start with 8 entries. We must
975 allocate at least two (for constructors and destructors), and
976 we're going to end up with an assignment operator at some
977 point as well. */
978 method_vec = VEC_alloc (tree, gc, 8);
979 /* Create slots for constructors and destructors. */
980 VEC_quick_push (tree, method_vec, NULL_TREE);
981 VEC_quick_push (tree, method_vec, NULL_TREE);
982 CLASSTYPE_METHOD_VEC (type) = method_vec;
983 }
984
985 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
986 grok_special_member_properties (method);
987
988 /* Constructors and destructors go in special slots. */
989 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
990 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
991 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
992 {
993 slot = CLASSTYPE_DESTRUCTOR_SLOT;
994
995 if (TYPE_FOR_JAVA (type))
996 {
997 if (!DECL_ARTIFICIAL (method))
998 error ("Java class %qT cannot have a destructor", type);
999 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1000 error ("Java class %qT cannot have an implicit non-trivial "
1001 "destructor",
1002 type);
1003 }
1004 }
1005 else
1006 {
1007 tree m;
1008
1009 insert_p = true;
1010 /* See if we already have an entry with this name. */
1011 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1012 VEC_iterate (tree, method_vec, slot, m);
1013 ++slot)
1014 {
1015 m = OVL_CURRENT (m);
1016 if (template_conv_p)
1017 {
1018 if (TREE_CODE (m) == TEMPLATE_DECL
1019 && DECL_TEMPLATE_CONV_FN_P (m))
1020 insert_p = false;
1021 break;
1022 }
1023 if (conv_p && !DECL_CONV_FN_P (m))
1024 break;
1025 if (DECL_NAME (m) == DECL_NAME (method))
1026 {
1027 insert_p = false;
1028 break;
1029 }
1030 if (complete_p
1031 && !DECL_CONV_FN_P (m)
1032 && DECL_NAME (m) > DECL_NAME (method))
1033 break;
1034 }
1035 }
1036 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1037
1038 /* Check to see if we've already got this method. */
1039 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1040 {
1041 tree fn = OVL_CURRENT (fns);
1042 tree fn_type;
1043 tree method_type;
1044 tree parms1;
1045 tree parms2;
1046
1047 if (TREE_CODE (fn) != TREE_CODE (method))
1048 continue;
1049
1050 /* [over.load] Member function declarations with the
1051 same name and the same parameter types cannot be
1052 overloaded if any of them is a static member
1053 function declaration.
1054
1055 [namespace.udecl] When a using-declaration brings names
1056 from a base class into a derived class scope, member
1057 functions in the derived class override and/or hide member
1058 functions with the same name and parameter types in a base
1059 class (rather than conflicting). */
1060 fn_type = TREE_TYPE (fn);
1061 method_type = TREE_TYPE (method);
1062 parms1 = TYPE_ARG_TYPES (fn_type);
1063 parms2 = TYPE_ARG_TYPES (method_type);
1064
1065 /* Compare the quals on the 'this' parm. Don't compare
1066 the whole types, as used functions are treated as
1067 coming from the using class in overload resolution. */
1068 if (! DECL_STATIC_FUNCTION_P (fn)
1069 && ! DECL_STATIC_FUNCTION_P (method)
1070 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1071 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1072 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1073 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1074 continue;
1075
1076 /* For templates, the return type and template parameters
1077 must be identical. */
1078 if (TREE_CODE (fn) == TEMPLATE_DECL
1079 && (!same_type_p (TREE_TYPE (fn_type),
1080 TREE_TYPE (method_type))
1081 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1082 DECL_TEMPLATE_PARMS (method))))
1083 continue;
1084
1085 if (! DECL_STATIC_FUNCTION_P (fn))
1086 parms1 = TREE_CHAIN (parms1);
1087 if (! DECL_STATIC_FUNCTION_P (method))
1088 parms2 = TREE_CHAIN (parms2);
1089
1090 if (compparms (parms1, parms2)
1091 && (!DECL_CONV_FN_P (fn)
1092 || same_type_p (TREE_TYPE (fn_type),
1093 TREE_TYPE (method_type))))
1094 {
1095 if (using_decl)
1096 {
1097 if (DECL_CONTEXT (fn) == type)
1098 /* Defer to the local function. */
1099 return false;
1100 }
1101 else
1102 {
1103 error ("%q+#D cannot be overloaded", method);
1104 error ("with %q+#D", fn);
1105 }
1106
1107 /* We don't call duplicate_decls here to merge the
1108 declarations because that will confuse things if the
1109 methods have inline definitions. In particular, we
1110 will crash while processing the definitions. */
1111 return false;
1112 }
1113 }
1114
1115 /* A class should never have more than one destructor. */
1116 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1117 return false;
1118
1119 /* Add the new binding. */
1120 if (using_decl)
1121 {
1122 overload = ovl_cons (method, current_fns);
1123 OVL_USED (overload) = true;
1124 }
1125 else
1126 overload = build_overload (method, current_fns);
1127
1128 if (conv_p)
1129 TYPE_HAS_CONVERSION (type) = 1;
1130 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1131 push_class_level_binding (DECL_NAME (method), overload);
1132
1133 if (insert_p)
1134 {
1135 bool reallocated;
1136
1137 /* We only expect to add few methods in the COMPLETE_P case, so
1138 just make room for one more method in that case. */
1139 if (complete_p)
1140 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1141 else
1142 reallocated = VEC_reserve (tree, gc, method_vec, 1);
1143 if (reallocated)
1144 CLASSTYPE_METHOD_VEC (type) = method_vec;
1145 if (slot == VEC_length (tree, method_vec))
1146 VEC_quick_push (tree, method_vec, overload);
1147 else
1148 VEC_quick_insert (tree, method_vec, slot, overload);
1149 }
1150 else
1151 /* Replace the current slot. */
1152 VEC_replace (tree, method_vec, slot, overload);
1153 return true;
1154 }
1155
1156 /* Subroutines of finish_struct. */
1157
1158 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1159 legit, otherwise return 0. */
1160
1161 static int
1162 alter_access (tree t, tree fdecl, tree access)
1163 {
1164 tree elem;
1165
1166 if (!DECL_LANG_SPECIFIC (fdecl))
1167 retrofit_lang_decl (fdecl);
1168
1169 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1170
1171 elem = purpose_member (t, DECL_ACCESS (fdecl));
1172 if (elem)
1173 {
1174 if (TREE_VALUE (elem) != access)
1175 {
1176 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1177 error ("conflicting access specifications for method"
1178 " %q+D, ignored", TREE_TYPE (fdecl));
1179 else
1180 error ("conflicting access specifications for field %qE, ignored",
1181 DECL_NAME (fdecl));
1182 }
1183 else
1184 {
1185 /* They're changing the access to the same thing they changed
1186 it to before. That's OK. */
1187 ;
1188 }
1189 }
1190 else
1191 {
1192 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1193 tf_warning_or_error);
1194 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1195 return 1;
1196 }
1197 return 0;
1198 }
1199
1200 /* Process the USING_DECL, which is a member of T. */
1201
1202 static void
1203 handle_using_decl (tree using_decl, tree t)
1204 {
1205 tree decl = USING_DECL_DECLS (using_decl);
1206 tree name = DECL_NAME (using_decl);
1207 tree access
1208 = TREE_PRIVATE (using_decl) ? access_private_node
1209 : TREE_PROTECTED (using_decl) ? access_protected_node
1210 : access_public_node;
1211 tree flist = NULL_TREE;
1212 tree old_value;
1213
1214 gcc_assert (!processing_template_decl && decl);
1215
1216 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1217 tf_warning_or_error);
1218 if (old_value)
1219 {
1220 if (is_overloaded_fn (old_value))
1221 old_value = OVL_CURRENT (old_value);
1222
1223 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1224 /* OK */;
1225 else
1226 old_value = NULL_TREE;
1227 }
1228
1229 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1230
1231 if (is_overloaded_fn (decl))
1232 flist = decl;
1233
1234 if (! old_value)
1235 ;
1236 else if (is_overloaded_fn (old_value))
1237 {
1238 if (flist)
1239 /* It's OK to use functions from a base when there are functions with
1240 the same name already present in the current class. */;
1241 else
1242 {
1243 error ("%q+D invalid in %q#T", using_decl, t);
1244 error (" because of local method %q+#D with same name",
1245 OVL_CURRENT (old_value));
1246 return;
1247 }
1248 }
1249 else if (!DECL_ARTIFICIAL (old_value))
1250 {
1251 error ("%q+D invalid in %q#T", using_decl, t);
1252 error (" because of local member %q+#D with same name", old_value);
1253 return;
1254 }
1255
1256 /* Make type T see field decl FDECL with access ACCESS. */
1257 if (flist)
1258 for (; flist; flist = OVL_NEXT (flist))
1259 {
1260 add_method (t, OVL_CURRENT (flist), using_decl);
1261 alter_access (t, OVL_CURRENT (flist), access);
1262 }
1263 else
1264 alter_access (t, decl, access);
1265 }
1266 \f
1267 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1268 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1269 properties of the bases. */
1270
1271 static void
1272 check_bases (tree t,
1273 int* cant_have_const_ctor_p,
1274 int* no_const_asn_ref_p)
1275 {
1276 int i;
1277 bool seen_non_virtual_nearly_empty_base_p = 0;
1278 int seen_tm_mask = 0;
1279 tree base_binfo;
1280 tree binfo;
1281 tree field = NULL_TREE;
1282
1283 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1284 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1285 if (TREE_CODE (field) == FIELD_DECL)
1286 break;
1287
1288 for (binfo = TYPE_BINFO (t), i = 0;
1289 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1290 {
1291 tree basetype = TREE_TYPE (base_binfo);
1292
1293 gcc_assert (COMPLETE_TYPE_P (basetype));
1294
1295 if (CLASSTYPE_FINAL (basetype))
1296 error ("cannot derive from %<final%> base %qT in derived type %qT",
1297 basetype, t);
1298
1299 /* If any base class is non-literal, so is the derived class. */
1300 if (!CLASSTYPE_LITERAL_P (basetype))
1301 CLASSTYPE_LITERAL_P (t) = false;
1302
1303 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1304 here because the case of virtual functions but non-virtual
1305 dtor is handled in finish_struct_1. */
1306 if (!TYPE_POLYMORPHIC_P (basetype))
1307 warning (OPT_Weffc__,
1308 "base class %q#T has a non-virtual destructor", basetype);
1309
1310 /* If the base class doesn't have copy constructors or
1311 assignment operators that take const references, then the
1312 derived class cannot have such a member automatically
1313 generated. */
1314 if (TYPE_HAS_COPY_CTOR (basetype)
1315 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1316 *cant_have_const_ctor_p = 1;
1317 if (TYPE_HAS_COPY_ASSIGN (basetype)
1318 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1319 *no_const_asn_ref_p = 1;
1320
1321 if (BINFO_VIRTUAL_P (base_binfo))
1322 /* A virtual base does not effect nearly emptiness. */
1323 ;
1324 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1325 {
1326 if (seen_non_virtual_nearly_empty_base_p)
1327 /* And if there is more than one nearly empty base, then the
1328 derived class is not nearly empty either. */
1329 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1330 else
1331 /* Remember we've seen one. */
1332 seen_non_virtual_nearly_empty_base_p = 1;
1333 }
1334 else if (!is_empty_class (basetype))
1335 /* If the base class is not empty or nearly empty, then this
1336 class cannot be nearly empty. */
1337 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1338
1339 /* A lot of properties from the bases also apply to the derived
1340 class. */
1341 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1342 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1343 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1344 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1345 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1346 || !TYPE_HAS_COPY_ASSIGN (basetype));
1347 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1348 || !TYPE_HAS_COPY_CTOR (basetype));
1349 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1350 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1351 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1352 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1353 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1354 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1355 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1356 || TYPE_HAS_COMPLEX_DFLT (basetype));
1357
1358 /* A standard-layout class is a class that:
1359 ...
1360 * has no non-standard-layout base classes, */
1361 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1362 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1363 {
1364 tree basefield;
1365 /* ...has no base classes of the same type as the first non-static
1366 data member... */
1367 if (field && DECL_CONTEXT (field) == t
1368 && (same_type_ignoring_top_level_qualifiers_p
1369 (TREE_TYPE (field), basetype)))
1370 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1371 else
1372 /* ...either has no non-static data members in the most-derived
1373 class and at most one base class with non-static data
1374 members, or has no base classes with non-static data
1375 members */
1376 for (basefield = TYPE_FIELDS (basetype); basefield;
1377 basefield = DECL_CHAIN (basefield))
1378 if (TREE_CODE (basefield) == FIELD_DECL)
1379 {
1380 if (field)
1381 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1382 else
1383 field = basefield;
1384 break;
1385 }
1386 }
1387
1388 /* Don't bother collecting tm attributes if transactional memory
1389 support is not enabled. */
1390 if (flag_tm)
1391 {
1392 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1393 if (tm_attr)
1394 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1395 }
1396 }
1397
1398 /* If one of the base classes had TM attributes, and the current class
1399 doesn't define its own, then the current class inherits one. */
1400 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1401 {
1402 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1403 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1404 }
1405 }
1406
1407 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1408 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1409 that have had a nearly-empty virtual primary base stolen by some
1410 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1411 T. */
1412
1413 static void
1414 determine_primary_bases (tree t)
1415 {
1416 unsigned i;
1417 tree primary = NULL_TREE;
1418 tree type_binfo = TYPE_BINFO (t);
1419 tree base_binfo;
1420
1421 /* Determine the primary bases of our bases. */
1422 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1423 base_binfo = TREE_CHAIN (base_binfo))
1424 {
1425 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1426
1427 /* See if we're the non-virtual primary of our inheritance
1428 chain. */
1429 if (!BINFO_VIRTUAL_P (base_binfo))
1430 {
1431 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1432 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1433
1434 if (parent_primary
1435 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1436 BINFO_TYPE (parent_primary)))
1437 /* We are the primary binfo. */
1438 BINFO_PRIMARY_P (base_binfo) = 1;
1439 }
1440 /* Determine if we have a virtual primary base, and mark it so.
1441 */
1442 if (primary && BINFO_VIRTUAL_P (primary))
1443 {
1444 tree this_primary = copied_binfo (primary, base_binfo);
1445
1446 if (BINFO_PRIMARY_P (this_primary))
1447 /* Someone already claimed this base. */
1448 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1449 else
1450 {
1451 tree delta;
1452
1453 BINFO_PRIMARY_P (this_primary) = 1;
1454 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1455
1456 /* A virtual binfo might have been copied from within
1457 another hierarchy. As we're about to use it as a
1458 primary base, make sure the offsets match. */
1459 delta = size_diffop_loc (input_location,
1460 convert (ssizetype,
1461 BINFO_OFFSET (base_binfo)),
1462 convert (ssizetype,
1463 BINFO_OFFSET (this_primary)));
1464
1465 propagate_binfo_offsets (this_primary, delta);
1466 }
1467 }
1468 }
1469
1470 /* First look for a dynamic direct non-virtual base. */
1471 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1472 {
1473 tree basetype = BINFO_TYPE (base_binfo);
1474
1475 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1476 {
1477 primary = base_binfo;
1478 goto found;
1479 }
1480 }
1481
1482 /* A "nearly-empty" virtual base class can be the primary base
1483 class, if no non-virtual polymorphic base can be found. Look for
1484 a nearly-empty virtual dynamic base that is not already a primary
1485 base of something in the hierarchy. If there is no such base,
1486 just pick the first nearly-empty virtual base. */
1487
1488 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1489 base_binfo = TREE_CHAIN (base_binfo))
1490 if (BINFO_VIRTUAL_P (base_binfo)
1491 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1492 {
1493 if (!BINFO_PRIMARY_P (base_binfo))
1494 {
1495 /* Found one that is not primary. */
1496 primary = base_binfo;
1497 goto found;
1498 }
1499 else if (!primary)
1500 /* Remember the first candidate. */
1501 primary = base_binfo;
1502 }
1503
1504 found:
1505 /* If we've got a primary base, use it. */
1506 if (primary)
1507 {
1508 tree basetype = BINFO_TYPE (primary);
1509
1510 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1511 if (BINFO_PRIMARY_P (primary))
1512 /* We are stealing a primary base. */
1513 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1514 BINFO_PRIMARY_P (primary) = 1;
1515 if (BINFO_VIRTUAL_P (primary))
1516 {
1517 tree delta;
1518
1519 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1520 /* A virtual binfo might have been copied from within
1521 another hierarchy. As we're about to use it as a primary
1522 base, make sure the offsets match. */
1523 delta = size_diffop_loc (input_location, ssize_int (0),
1524 convert (ssizetype, BINFO_OFFSET (primary)));
1525
1526 propagate_binfo_offsets (primary, delta);
1527 }
1528
1529 primary = TYPE_BINFO (basetype);
1530
1531 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1532 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1533 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1534 }
1535 }
1536
1537 /* Update the variant types of T. */
1538
1539 void
1540 fixup_type_variants (tree t)
1541 {
1542 tree variants;
1543
1544 if (!t)
1545 return;
1546
1547 for (variants = TYPE_NEXT_VARIANT (t);
1548 variants;
1549 variants = TYPE_NEXT_VARIANT (variants))
1550 {
1551 /* These fields are in the _TYPE part of the node, not in
1552 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1553 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1554 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1555 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1556 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1557
1558 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1559
1560 TYPE_BINFO (variants) = TYPE_BINFO (t);
1561
1562 /* Copy whatever these are holding today. */
1563 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1564 TYPE_METHODS (variants) = TYPE_METHODS (t);
1565 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1566 }
1567 }
1568
1569 /* Early variant fixups: we apply attributes at the beginning of the class
1570 definition, and we need to fix up any variants that have already been
1571 made via elaborated-type-specifier so that check_qualified_type works. */
1572
1573 void
1574 fixup_attribute_variants (tree t)
1575 {
1576 tree variants;
1577
1578 if (!t)
1579 return;
1580
1581 for (variants = TYPE_NEXT_VARIANT (t);
1582 variants;
1583 variants = TYPE_NEXT_VARIANT (variants))
1584 {
1585 /* These are the two fields that check_qualified_type looks at and
1586 are affected by attributes. */
1587 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1588 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1589 }
1590 }
1591 \f
1592 /* Set memoizing fields and bits of T (and its variants) for later
1593 use. */
1594
1595 static void
1596 finish_struct_bits (tree t)
1597 {
1598 /* Fix up variants (if any). */
1599 fixup_type_variants (t);
1600
1601 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1602 /* For a class w/o baseclasses, 'finish_struct' has set
1603 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1604 Similarly for a class whose base classes do not have vtables.
1605 When neither of these is true, we might have removed abstract
1606 virtuals (by providing a definition), added some (by declaring
1607 new ones), or redeclared ones from a base class. We need to
1608 recalculate what's really an abstract virtual at this point (by
1609 looking in the vtables). */
1610 get_pure_virtuals (t);
1611
1612 /* If this type has a copy constructor or a destructor, force its
1613 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1614 nonzero. This will cause it to be passed by invisible reference
1615 and prevent it from being returned in a register. */
1616 if (type_has_nontrivial_copy_init (t)
1617 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1618 {
1619 tree variants;
1620 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1621 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1622 {
1623 SET_TYPE_MODE (variants, BLKmode);
1624 TREE_ADDRESSABLE (variants) = 1;
1625 }
1626 }
1627 }
1628
1629 /* Issue warnings about T having private constructors, but no friends,
1630 and so forth.
1631
1632 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1633 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1634 non-private static member functions. */
1635
1636 static void
1637 maybe_warn_about_overly_private_class (tree t)
1638 {
1639 int has_member_fn = 0;
1640 int has_nonprivate_method = 0;
1641 tree fn;
1642
1643 if (!warn_ctor_dtor_privacy
1644 /* If the class has friends, those entities might create and
1645 access instances, so we should not warn. */
1646 || (CLASSTYPE_FRIEND_CLASSES (t)
1647 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1648 /* We will have warned when the template was declared; there's
1649 no need to warn on every instantiation. */
1650 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1651 /* There's no reason to even consider warning about this
1652 class. */
1653 return;
1654
1655 /* We only issue one warning, if more than one applies, because
1656 otherwise, on code like:
1657
1658 class A {
1659 // Oops - forgot `public:'
1660 A();
1661 A(const A&);
1662 ~A();
1663 };
1664
1665 we warn several times about essentially the same problem. */
1666
1667 /* Check to see if all (non-constructor, non-destructor) member
1668 functions are private. (Since there are no friends or
1669 non-private statics, we can't ever call any of the private member
1670 functions.) */
1671 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1672 /* We're not interested in compiler-generated methods; they don't
1673 provide any way to call private members. */
1674 if (!DECL_ARTIFICIAL (fn))
1675 {
1676 if (!TREE_PRIVATE (fn))
1677 {
1678 if (DECL_STATIC_FUNCTION_P (fn))
1679 /* A non-private static member function is just like a
1680 friend; it can create and invoke private member
1681 functions, and be accessed without a class
1682 instance. */
1683 return;
1684
1685 has_nonprivate_method = 1;
1686 /* Keep searching for a static member function. */
1687 }
1688 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1689 has_member_fn = 1;
1690 }
1691
1692 if (!has_nonprivate_method && has_member_fn)
1693 {
1694 /* There are no non-private methods, and there's at least one
1695 private member function that isn't a constructor or
1696 destructor. (If all the private members are
1697 constructors/destructors we want to use the code below that
1698 issues error messages specifically referring to
1699 constructors/destructors.) */
1700 unsigned i;
1701 tree binfo = TYPE_BINFO (t);
1702
1703 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1704 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1705 {
1706 has_nonprivate_method = 1;
1707 break;
1708 }
1709 if (!has_nonprivate_method)
1710 {
1711 warning (OPT_Wctor_dtor_privacy,
1712 "all member functions in class %qT are private", t);
1713 return;
1714 }
1715 }
1716
1717 /* Even if some of the member functions are non-private, the class
1718 won't be useful for much if all the constructors or destructors
1719 are private: such an object can never be created or destroyed. */
1720 fn = CLASSTYPE_DESTRUCTORS (t);
1721 if (fn && TREE_PRIVATE (fn))
1722 {
1723 warning (OPT_Wctor_dtor_privacy,
1724 "%q#T only defines a private destructor and has no friends",
1725 t);
1726 return;
1727 }
1728
1729 /* Warn about classes that have private constructors and no friends. */
1730 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1731 /* Implicitly generated constructors are always public. */
1732 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1733 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1734 {
1735 int nonprivate_ctor = 0;
1736
1737 /* If a non-template class does not define a copy
1738 constructor, one is defined for it, enabling it to avoid
1739 this warning. For a template class, this does not
1740 happen, and so we would normally get a warning on:
1741
1742 template <class T> class C { private: C(); };
1743
1744 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1745 complete non-template or fully instantiated classes have this
1746 flag set. */
1747 if (!TYPE_HAS_COPY_CTOR (t))
1748 nonprivate_ctor = 1;
1749 else
1750 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1751 {
1752 tree ctor = OVL_CURRENT (fn);
1753 /* Ideally, we wouldn't count copy constructors (or, in
1754 fact, any constructor that takes an argument of the
1755 class type as a parameter) because such things cannot
1756 be used to construct an instance of the class unless
1757 you already have one. But, for now at least, we're
1758 more generous. */
1759 if (! TREE_PRIVATE (ctor))
1760 {
1761 nonprivate_ctor = 1;
1762 break;
1763 }
1764 }
1765
1766 if (nonprivate_ctor == 0)
1767 {
1768 warning (OPT_Wctor_dtor_privacy,
1769 "%q#T only defines private constructors and has no friends",
1770 t);
1771 return;
1772 }
1773 }
1774 }
1775
1776 static struct {
1777 gt_pointer_operator new_value;
1778 void *cookie;
1779 } resort_data;
1780
1781 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1782
1783 static int
1784 method_name_cmp (const void* m1_p, const void* m2_p)
1785 {
1786 const tree *const m1 = (const tree *) m1_p;
1787 const tree *const m2 = (const tree *) m2_p;
1788
1789 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1790 return 0;
1791 if (*m1 == NULL_TREE)
1792 return -1;
1793 if (*m2 == NULL_TREE)
1794 return 1;
1795 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1796 return -1;
1797 return 1;
1798 }
1799
1800 /* This routine compares two fields like method_name_cmp but using the
1801 pointer operator in resort_field_decl_data. */
1802
1803 static int
1804 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1805 {
1806 const tree *const m1 = (const tree *) m1_p;
1807 const tree *const m2 = (const tree *) m2_p;
1808 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1809 return 0;
1810 if (*m1 == NULL_TREE)
1811 return -1;
1812 if (*m2 == NULL_TREE)
1813 return 1;
1814 {
1815 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1816 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1817 resort_data.new_value (&d1, resort_data.cookie);
1818 resort_data.new_value (&d2, resort_data.cookie);
1819 if (d1 < d2)
1820 return -1;
1821 }
1822 return 1;
1823 }
1824
1825 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1826
1827 void
1828 resort_type_method_vec (void* obj,
1829 void* orig_obj ATTRIBUTE_UNUSED ,
1830 gt_pointer_operator new_value,
1831 void* cookie)
1832 {
1833 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1834 int len = VEC_length (tree, method_vec);
1835 size_t slot;
1836 tree fn;
1837
1838 /* The type conversion ops have to live at the front of the vec, so we
1839 can't sort them. */
1840 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1841 VEC_iterate (tree, method_vec, slot, fn);
1842 ++slot)
1843 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1844 break;
1845
1846 if (len - slot > 1)
1847 {
1848 resort_data.new_value = new_value;
1849 resort_data.cookie = cookie;
1850 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1851 resort_method_name_cmp);
1852 }
1853 }
1854
1855 /* Warn about duplicate methods in fn_fields.
1856
1857 Sort methods that are not special (i.e., constructors, destructors,
1858 and type conversion operators) so that we can find them faster in
1859 search. */
1860
1861 static void
1862 finish_struct_methods (tree t)
1863 {
1864 tree fn_fields;
1865 VEC(tree,gc) *method_vec;
1866 int slot, len;
1867
1868 method_vec = CLASSTYPE_METHOD_VEC (t);
1869 if (!method_vec)
1870 return;
1871
1872 len = VEC_length (tree, method_vec);
1873
1874 /* Clear DECL_IN_AGGR_P for all functions. */
1875 for (fn_fields = TYPE_METHODS (t); fn_fields;
1876 fn_fields = DECL_CHAIN (fn_fields))
1877 DECL_IN_AGGR_P (fn_fields) = 0;
1878
1879 /* Issue warnings about private constructors and such. If there are
1880 no methods, then some public defaults are generated. */
1881 maybe_warn_about_overly_private_class (t);
1882
1883 /* The type conversion ops have to live at the front of the vec, so we
1884 can't sort them. */
1885 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1886 VEC_iterate (tree, method_vec, slot, fn_fields);
1887 ++slot)
1888 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1889 break;
1890 if (len - slot > 1)
1891 qsort (VEC_address (tree, method_vec) + slot,
1892 len-slot, sizeof (tree), method_name_cmp);
1893 }
1894
1895 /* Make BINFO's vtable have N entries, including RTTI entries,
1896 vbase and vcall offsets, etc. Set its type and call the back end
1897 to lay it out. */
1898
1899 static void
1900 layout_vtable_decl (tree binfo, int n)
1901 {
1902 tree atype;
1903 tree vtable;
1904
1905 atype = build_array_of_n_type (vtable_entry_type, n);
1906 layout_type (atype);
1907
1908 /* We may have to grow the vtable. */
1909 vtable = get_vtbl_decl_for_binfo (binfo);
1910 if (!same_type_p (TREE_TYPE (vtable), atype))
1911 {
1912 TREE_TYPE (vtable) = atype;
1913 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1914 layout_decl (vtable, 0);
1915 }
1916 }
1917
1918 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1919 have the same signature. */
1920
1921 int
1922 same_signature_p (const_tree fndecl, const_tree base_fndecl)
1923 {
1924 /* One destructor overrides another if they are the same kind of
1925 destructor. */
1926 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1927 && special_function_p (base_fndecl) == special_function_p (fndecl))
1928 return 1;
1929 /* But a non-destructor never overrides a destructor, nor vice
1930 versa, nor do different kinds of destructors override
1931 one-another. For example, a complete object destructor does not
1932 override a deleting destructor. */
1933 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1934 return 0;
1935
1936 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1937 || (DECL_CONV_FN_P (fndecl)
1938 && DECL_CONV_FN_P (base_fndecl)
1939 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1940 DECL_CONV_FN_TYPE (base_fndecl))))
1941 {
1942 tree types, base_types;
1943 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1944 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1945 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1946 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1947 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1948 return 1;
1949 }
1950 return 0;
1951 }
1952
1953 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1954 subobject. */
1955
1956 static bool
1957 base_derived_from (tree derived, tree base)
1958 {
1959 tree probe;
1960
1961 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1962 {
1963 if (probe == derived)
1964 return true;
1965 else if (BINFO_VIRTUAL_P (probe))
1966 /* If we meet a virtual base, we can't follow the inheritance
1967 any more. See if the complete type of DERIVED contains
1968 such a virtual base. */
1969 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1970 != NULL_TREE);
1971 }
1972 return false;
1973 }
1974
1975 typedef struct find_final_overrider_data_s {
1976 /* The function for which we are trying to find a final overrider. */
1977 tree fn;
1978 /* The base class in which the function was declared. */
1979 tree declaring_base;
1980 /* The candidate overriders. */
1981 tree candidates;
1982 /* Path to most derived. */
1983 VEC(tree,heap) *path;
1984 } find_final_overrider_data;
1985
1986 /* Add the overrider along the current path to FFOD->CANDIDATES.
1987 Returns true if an overrider was found; false otherwise. */
1988
1989 static bool
1990 dfs_find_final_overrider_1 (tree binfo,
1991 find_final_overrider_data *ffod,
1992 unsigned depth)
1993 {
1994 tree method;
1995
1996 /* If BINFO is not the most derived type, try a more derived class.
1997 A definition there will overrider a definition here. */
1998 if (depth)
1999 {
2000 depth--;
2001 if (dfs_find_final_overrider_1
2002 (VEC_index (tree, ffod->path, depth), ffod, depth))
2003 return true;
2004 }
2005
2006 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2007 if (method)
2008 {
2009 tree *candidate = &ffod->candidates;
2010
2011 /* Remove any candidates overridden by this new function. */
2012 while (*candidate)
2013 {
2014 /* If *CANDIDATE overrides METHOD, then METHOD
2015 cannot override anything else on the list. */
2016 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2017 return true;
2018 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2019 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2020 *candidate = TREE_CHAIN (*candidate);
2021 else
2022 candidate = &TREE_CHAIN (*candidate);
2023 }
2024
2025 /* Add the new function. */
2026 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2027 return true;
2028 }
2029
2030 return false;
2031 }
2032
2033 /* Called from find_final_overrider via dfs_walk. */
2034
2035 static tree
2036 dfs_find_final_overrider_pre (tree binfo, void *data)
2037 {
2038 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2039
2040 if (binfo == ffod->declaring_base)
2041 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
2042 VEC_safe_push (tree, heap, ffod->path, binfo);
2043
2044 return NULL_TREE;
2045 }
2046
2047 static tree
2048 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
2049 {
2050 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2051 VEC_pop (tree, ffod->path);
2052
2053 return NULL_TREE;
2054 }
2055
2056 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2057 FN and whose TREE_VALUE is the binfo for the base where the
2058 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2059 DERIVED) is the base object in which FN is declared. */
2060
2061 static tree
2062 find_final_overrider (tree derived, tree binfo, tree fn)
2063 {
2064 find_final_overrider_data ffod;
2065
2066 /* Getting this right is a little tricky. This is valid:
2067
2068 struct S { virtual void f (); };
2069 struct T { virtual void f (); };
2070 struct U : public S, public T { };
2071
2072 even though calling `f' in `U' is ambiguous. But,
2073
2074 struct R { virtual void f(); };
2075 struct S : virtual public R { virtual void f (); };
2076 struct T : virtual public R { virtual void f (); };
2077 struct U : public S, public T { };
2078
2079 is not -- there's no way to decide whether to put `S::f' or
2080 `T::f' in the vtable for `R'.
2081
2082 The solution is to look at all paths to BINFO. If we find
2083 different overriders along any two, then there is a problem. */
2084 if (DECL_THUNK_P (fn))
2085 fn = THUNK_TARGET (fn);
2086
2087 /* Determine the depth of the hierarchy. */
2088 ffod.fn = fn;
2089 ffod.declaring_base = binfo;
2090 ffod.candidates = NULL_TREE;
2091 ffod.path = VEC_alloc (tree, heap, 30);
2092
2093 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2094 dfs_find_final_overrider_post, &ffod);
2095
2096 VEC_free (tree, heap, ffod.path);
2097
2098 /* If there was no winner, issue an error message. */
2099 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2100 return error_mark_node;
2101
2102 return ffod.candidates;
2103 }
2104
2105 /* Return the index of the vcall offset for FN when TYPE is used as a
2106 virtual base. */
2107
2108 static tree
2109 get_vcall_index (tree fn, tree type)
2110 {
2111 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2112 tree_pair_p p;
2113 unsigned ix;
2114
2115 FOR_EACH_VEC_ELT (tree_pair_s, indices, ix, p)
2116 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2117 || same_signature_p (fn, p->purpose))
2118 return p->value;
2119
2120 /* There should always be an appropriate index. */
2121 gcc_unreachable ();
2122 }
2123
2124 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2125 dominated by T. FN is the old function; VIRTUALS points to the
2126 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2127 of that entry in the list. */
2128
2129 static void
2130 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2131 unsigned ix)
2132 {
2133 tree b;
2134 tree overrider;
2135 tree delta;
2136 tree virtual_base;
2137 tree first_defn;
2138 tree overrider_fn, overrider_target;
2139 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2140 tree over_return, base_return;
2141 bool lost = false;
2142
2143 /* Find the nearest primary base (possibly binfo itself) which defines
2144 this function; this is the class the caller will convert to when
2145 calling FN through BINFO. */
2146 for (b = binfo; ; b = get_primary_binfo (b))
2147 {
2148 gcc_assert (b);
2149 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2150 break;
2151
2152 /* The nearest definition is from a lost primary. */
2153 if (BINFO_LOST_PRIMARY_P (b))
2154 lost = true;
2155 }
2156 first_defn = b;
2157
2158 /* Find the final overrider. */
2159 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2160 if (overrider == error_mark_node)
2161 {
2162 error ("no unique final overrider for %qD in %qT", target_fn, t);
2163 return;
2164 }
2165 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2166
2167 /* Check for adjusting covariant return types. */
2168 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2169 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2170
2171 if (POINTER_TYPE_P (over_return)
2172 && TREE_CODE (over_return) == TREE_CODE (base_return)
2173 && CLASS_TYPE_P (TREE_TYPE (over_return))
2174 && CLASS_TYPE_P (TREE_TYPE (base_return))
2175 /* If the overrider is invalid, don't even try. */
2176 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2177 {
2178 /* If FN is a covariant thunk, we must figure out the adjustment
2179 to the final base FN was converting to. As OVERRIDER_TARGET might
2180 also be converting to the return type of FN, we have to
2181 combine the two conversions here. */
2182 tree fixed_offset, virtual_offset;
2183
2184 over_return = TREE_TYPE (over_return);
2185 base_return = TREE_TYPE (base_return);
2186
2187 if (DECL_THUNK_P (fn))
2188 {
2189 gcc_assert (DECL_RESULT_THUNK_P (fn));
2190 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2191 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2192 }
2193 else
2194 fixed_offset = virtual_offset = NULL_TREE;
2195
2196 if (virtual_offset)
2197 /* Find the equivalent binfo within the return type of the
2198 overriding function. We will want the vbase offset from
2199 there. */
2200 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2201 over_return);
2202 else if (!same_type_ignoring_top_level_qualifiers_p
2203 (over_return, base_return))
2204 {
2205 /* There was no existing virtual thunk (which takes
2206 precedence). So find the binfo of the base function's
2207 return type within the overriding function's return type.
2208 We cannot call lookup base here, because we're inside a
2209 dfs_walk, and will therefore clobber the BINFO_MARKED
2210 flags. Fortunately we know the covariancy is valid (it
2211 has already been checked), so we can just iterate along
2212 the binfos, which have been chained in inheritance graph
2213 order. Of course it is lame that we have to repeat the
2214 search here anyway -- we should really be caching pieces
2215 of the vtable and avoiding this repeated work. */
2216 tree thunk_binfo, base_binfo;
2217
2218 /* Find the base binfo within the overriding function's
2219 return type. We will always find a thunk_binfo, except
2220 when the covariancy is invalid (which we will have
2221 already diagnosed). */
2222 for (base_binfo = TYPE_BINFO (base_return),
2223 thunk_binfo = TYPE_BINFO (over_return);
2224 thunk_binfo;
2225 thunk_binfo = TREE_CHAIN (thunk_binfo))
2226 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2227 BINFO_TYPE (base_binfo)))
2228 break;
2229
2230 /* See if virtual inheritance is involved. */
2231 for (virtual_offset = thunk_binfo;
2232 virtual_offset;
2233 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2234 if (BINFO_VIRTUAL_P (virtual_offset))
2235 break;
2236
2237 if (virtual_offset
2238 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2239 {
2240 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2241
2242 if (virtual_offset)
2243 {
2244 /* We convert via virtual base. Adjust the fixed
2245 offset to be from there. */
2246 offset =
2247 size_diffop (offset,
2248 convert (ssizetype,
2249 BINFO_OFFSET (virtual_offset)));
2250 }
2251 if (fixed_offset)
2252 /* There was an existing fixed offset, this must be
2253 from the base just converted to, and the base the
2254 FN was thunking to. */
2255 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2256 else
2257 fixed_offset = offset;
2258 }
2259 }
2260
2261 if (fixed_offset || virtual_offset)
2262 /* Replace the overriding function with a covariant thunk. We
2263 will emit the overriding function in its own slot as
2264 well. */
2265 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2266 fixed_offset, virtual_offset);
2267 }
2268 else
2269 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2270 !DECL_THUNK_P (fn));
2271
2272 /* If we need a covariant thunk, then we may need to adjust first_defn.
2273 The ABI specifies that the thunks emitted with a function are
2274 determined by which bases the function overrides, so we need to be
2275 sure that we're using a thunk for some overridden base; even if we
2276 know that the necessary this adjustment is zero, there may not be an
2277 appropriate zero-this-adjusment thunk for us to use since thunks for
2278 overriding virtual bases always use the vcall offset.
2279
2280 Furthermore, just choosing any base that overrides this function isn't
2281 quite right, as this slot won't be used for calls through a type that
2282 puts a covariant thunk here. Calling the function through such a type
2283 will use a different slot, and that slot is the one that determines
2284 the thunk emitted for that base.
2285
2286 So, keep looking until we find the base that we're really overriding
2287 in this slot: the nearest primary base that doesn't use a covariant
2288 thunk in this slot. */
2289 if (overrider_target != overrider_fn)
2290 {
2291 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2292 /* We already know that the overrider needs a covariant thunk. */
2293 b = get_primary_binfo (b);
2294 for (; ; b = get_primary_binfo (b))
2295 {
2296 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2297 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2298 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2299 break;
2300 if (BINFO_LOST_PRIMARY_P (b))
2301 lost = true;
2302 }
2303 first_defn = b;
2304 }
2305
2306 /* Assume that we will produce a thunk that convert all the way to
2307 the final overrider, and not to an intermediate virtual base. */
2308 virtual_base = NULL_TREE;
2309
2310 /* See if we can convert to an intermediate virtual base first, and then
2311 use the vcall offset located there to finish the conversion. */
2312 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2313 {
2314 /* If we find the final overrider, then we can stop
2315 walking. */
2316 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2317 BINFO_TYPE (TREE_VALUE (overrider))))
2318 break;
2319
2320 /* If we find a virtual base, and we haven't yet found the
2321 overrider, then there is a virtual base between the
2322 declaring base (first_defn) and the final overrider. */
2323 if (BINFO_VIRTUAL_P (b))
2324 {
2325 virtual_base = b;
2326 break;
2327 }
2328 }
2329
2330 /* Compute the constant adjustment to the `this' pointer. The
2331 `this' pointer, when this function is called, will point at BINFO
2332 (or one of its primary bases, which are at the same offset). */
2333 if (virtual_base)
2334 /* The `this' pointer needs to be adjusted from the declaration to
2335 the nearest virtual base. */
2336 delta = size_diffop_loc (input_location,
2337 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2338 convert (ssizetype, BINFO_OFFSET (first_defn)));
2339 else if (lost)
2340 /* If the nearest definition is in a lost primary, we don't need an
2341 entry in our vtable. Except possibly in a constructor vtable,
2342 if we happen to get our primary back. In that case, the offset
2343 will be zero, as it will be a primary base. */
2344 delta = size_zero_node;
2345 else
2346 /* The `this' pointer needs to be adjusted from pointing to
2347 BINFO to pointing at the base where the final overrider
2348 appears. */
2349 delta = size_diffop_loc (input_location,
2350 convert (ssizetype,
2351 BINFO_OFFSET (TREE_VALUE (overrider))),
2352 convert (ssizetype, BINFO_OFFSET (binfo)));
2353
2354 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2355
2356 if (virtual_base)
2357 BV_VCALL_INDEX (*virtuals)
2358 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2359 else
2360 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2361
2362 BV_LOST_PRIMARY (*virtuals) = lost;
2363 }
2364
2365 /* Called from modify_all_vtables via dfs_walk. */
2366
2367 static tree
2368 dfs_modify_vtables (tree binfo, void* data)
2369 {
2370 tree t = (tree) data;
2371 tree virtuals;
2372 tree old_virtuals;
2373 unsigned ix;
2374
2375 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2376 /* A base without a vtable needs no modification, and its bases
2377 are uninteresting. */
2378 return dfs_skip_bases;
2379
2380 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2381 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2382 /* Don't do the primary vtable, if it's new. */
2383 return NULL_TREE;
2384
2385 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2386 /* There's no need to modify the vtable for a non-virtual primary
2387 base; we're not going to use that vtable anyhow. We do still
2388 need to do this for virtual primary bases, as they could become
2389 non-primary in a construction vtable. */
2390 return NULL_TREE;
2391
2392 make_new_vtable (t, binfo);
2393
2394 /* Now, go through each of the virtual functions in the virtual
2395 function table for BINFO. Find the final overrider, and update
2396 the BINFO_VIRTUALS list appropriately. */
2397 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2398 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2399 virtuals;
2400 ix++, virtuals = TREE_CHAIN (virtuals),
2401 old_virtuals = TREE_CHAIN (old_virtuals))
2402 update_vtable_entry_for_fn (t,
2403 binfo,
2404 BV_FN (old_virtuals),
2405 &virtuals, ix);
2406
2407 return NULL_TREE;
2408 }
2409
2410 /* Update all of the primary and secondary vtables for T. Create new
2411 vtables as required, and initialize their RTTI information. Each
2412 of the functions in VIRTUALS is declared in T and may override a
2413 virtual function from a base class; find and modify the appropriate
2414 entries to point to the overriding functions. Returns a list, in
2415 declaration order, of the virtual functions that are declared in T,
2416 but do not appear in the primary base class vtable, and which
2417 should therefore be appended to the end of the vtable for T. */
2418
2419 static tree
2420 modify_all_vtables (tree t, tree virtuals)
2421 {
2422 tree binfo = TYPE_BINFO (t);
2423 tree *fnsp;
2424
2425 /* Update all of the vtables. */
2426 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2427
2428 /* Add virtual functions not already in our primary vtable. These
2429 will be both those introduced by this class, and those overridden
2430 from secondary bases. It does not include virtuals merely
2431 inherited from secondary bases. */
2432 for (fnsp = &virtuals; *fnsp; )
2433 {
2434 tree fn = TREE_VALUE (*fnsp);
2435
2436 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2437 || DECL_VINDEX (fn) == error_mark_node)
2438 {
2439 /* We don't need to adjust the `this' pointer when
2440 calling this function. */
2441 BV_DELTA (*fnsp) = integer_zero_node;
2442 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2443
2444 /* This is a function not already in our vtable. Keep it. */
2445 fnsp = &TREE_CHAIN (*fnsp);
2446 }
2447 else
2448 /* We've already got an entry for this function. Skip it. */
2449 *fnsp = TREE_CHAIN (*fnsp);
2450 }
2451
2452 return virtuals;
2453 }
2454
2455 /* Get the base virtual function declarations in T that have the
2456 indicated NAME. */
2457
2458 static tree
2459 get_basefndecls (tree name, tree t)
2460 {
2461 tree methods;
2462 tree base_fndecls = NULL_TREE;
2463 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2464 int i;
2465
2466 /* Find virtual functions in T with the indicated NAME. */
2467 i = lookup_fnfields_1 (t, name);
2468 if (i != -1)
2469 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2470 methods;
2471 methods = OVL_NEXT (methods))
2472 {
2473 tree method = OVL_CURRENT (methods);
2474
2475 if (TREE_CODE (method) == FUNCTION_DECL
2476 && DECL_VINDEX (method))
2477 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2478 }
2479
2480 if (base_fndecls)
2481 return base_fndecls;
2482
2483 for (i = 0; i < n_baseclasses; i++)
2484 {
2485 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2486 base_fndecls = chainon (get_basefndecls (name, basetype),
2487 base_fndecls);
2488 }
2489
2490 return base_fndecls;
2491 }
2492
2493 /* If this declaration supersedes the declaration of
2494 a method declared virtual in the base class, then
2495 mark this field as being virtual as well. */
2496
2497 void
2498 check_for_override (tree decl, tree ctype)
2499 {
2500 bool overrides_found = false;
2501 if (TREE_CODE (decl) == TEMPLATE_DECL)
2502 /* In [temp.mem] we have:
2503
2504 A specialization of a member function template does not
2505 override a virtual function from a base class. */
2506 return;
2507 if ((DECL_DESTRUCTOR_P (decl)
2508 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2509 || DECL_CONV_FN_P (decl))
2510 && look_for_overrides (ctype, decl)
2511 && !DECL_STATIC_FUNCTION_P (decl))
2512 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2513 the error_mark_node so that we know it is an overriding
2514 function. */
2515 {
2516 DECL_VINDEX (decl) = decl;
2517 overrides_found = true;
2518 }
2519
2520 if (DECL_VIRTUAL_P (decl))
2521 {
2522 if (!DECL_VINDEX (decl))
2523 DECL_VINDEX (decl) = error_mark_node;
2524 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2525 if (DECL_DESTRUCTOR_P (decl))
2526 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2527 }
2528 else if (DECL_FINAL_P (decl))
2529 error ("%q+#D marked final, but is not virtual", decl);
2530 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2531 error ("%q+#D marked override, but does not override", decl);
2532 }
2533
2534 /* Warn about hidden virtual functions that are not overridden in t.
2535 We know that constructors and destructors don't apply. */
2536
2537 static void
2538 warn_hidden (tree t)
2539 {
2540 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2541 tree fns;
2542 size_t i;
2543
2544 /* We go through each separately named virtual function. */
2545 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2546 VEC_iterate (tree, method_vec, i, fns);
2547 ++i)
2548 {
2549 tree fn;
2550 tree name;
2551 tree fndecl;
2552 tree base_fndecls;
2553 tree base_binfo;
2554 tree binfo;
2555 int j;
2556
2557 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2558 have the same name. Figure out what name that is. */
2559 name = DECL_NAME (OVL_CURRENT (fns));
2560 /* There are no possibly hidden functions yet. */
2561 base_fndecls = NULL_TREE;
2562 /* Iterate through all of the base classes looking for possibly
2563 hidden functions. */
2564 for (binfo = TYPE_BINFO (t), j = 0;
2565 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2566 {
2567 tree basetype = BINFO_TYPE (base_binfo);
2568 base_fndecls = chainon (get_basefndecls (name, basetype),
2569 base_fndecls);
2570 }
2571
2572 /* If there are no functions to hide, continue. */
2573 if (!base_fndecls)
2574 continue;
2575
2576 /* Remove any overridden functions. */
2577 for (fn = fns; fn; fn = OVL_NEXT (fn))
2578 {
2579 fndecl = OVL_CURRENT (fn);
2580 if (DECL_VINDEX (fndecl))
2581 {
2582 tree *prev = &base_fndecls;
2583
2584 while (*prev)
2585 /* If the method from the base class has the same
2586 signature as the method from the derived class, it
2587 has been overridden. */
2588 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2589 *prev = TREE_CHAIN (*prev);
2590 else
2591 prev = &TREE_CHAIN (*prev);
2592 }
2593 }
2594
2595 /* Now give a warning for all base functions without overriders,
2596 as they are hidden. */
2597 while (base_fndecls)
2598 {
2599 /* Here we know it is a hider, and no overrider exists. */
2600 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2601 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2602 base_fndecls = TREE_CHAIN (base_fndecls);
2603 }
2604 }
2605 }
2606
2607 /* Check for things that are invalid. There are probably plenty of other
2608 things we should check for also. */
2609
2610 static void
2611 finish_struct_anon (tree t)
2612 {
2613 tree field;
2614
2615 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2616 {
2617 if (TREE_STATIC (field))
2618 continue;
2619 if (TREE_CODE (field) != FIELD_DECL)
2620 continue;
2621
2622 if (DECL_NAME (field) == NULL_TREE
2623 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2624 {
2625 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2626 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2627 for (; elt; elt = DECL_CHAIN (elt))
2628 {
2629 /* We're generally only interested in entities the user
2630 declared, but we also find nested classes by noticing
2631 the TYPE_DECL that we create implicitly. You're
2632 allowed to put one anonymous union inside another,
2633 though, so we explicitly tolerate that. We use
2634 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2635 we also allow unnamed types used for defining fields. */
2636 if (DECL_ARTIFICIAL (elt)
2637 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2638 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2639 continue;
2640
2641 if (TREE_CODE (elt) != FIELD_DECL)
2642 {
2643 if (is_union)
2644 permerror (input_location, "%q+#D invalid; an anonymous union can "
2645 "only have non-static data members", elt);
2646 else
2647 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2648 "only have non-static data members", elt);
2649 continue;
2650 }
2651
2652 if (TREE_PRIVATE (elt))
2653 {
2654 if (is_union)
2655 permerror (input_location, "private member %q+#D in anonymous union", elt);
2656 else
2657 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2658 }
2659 else if (TREE_PROTECTED (elt))
2660 {
2661 if (is_union)
2662 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2663 else
2664 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2665 }
2666
2667 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2668 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2669 }
2670 }
2671 }
2672 }
2673
2674 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2675 will be used later during class template instantiation.
2676 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2677 a non-static member data (FIELD_DECL), a member function
2678 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2679 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2680 When FRIEND_P is nonzero, T is either a friend class
2681 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2682 (FUNCTION_DECL, TEMPLATE_DECL). */
2683
2684 void
2685 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2686 {
2687 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2688 if (CLASSTYPE_TEMPLATE_INFO (type))
2689 CLASSTYPE_DECL_LIST (type)
2690 = tree_cons (friend_p ? NULL_TREE : type,
2691 t, CLASSTYPE_DECL_LIST (type));
2692 }
2693
2694 /* This function is called from declare_virt_assop_and_dtor via
2695 dfs_walk_all.
2696
2697 DATA is a type that direcly or indirectly inherits the base
2698 represented by BINFO. If BINFO contains a virtual assignment [copy
2699 assignment or move assigment] operator or a virtual constructor,
2700 declare that function in DATA if it hasn't been already declared. */
2701
2702 static tree
2703 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2704 {
2705 tree bv, fn, t = (tree)data;
2706 tree opname = ansi_assopname (NOP_EXPR);
2707
2708 gcc_assert (t && CLASS_TYPE_P (t));
2709 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2710
2711 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2712 /* A base without a vtable needs no modification, and its bases
2713 are uninteresting. */
2714 return dfs_skip_bases;
2715
2716 if (BINFO_PRIMARY_P (binfo))
2717 /* If this is a primary base, then we have already looked at the
2718 virtual functions of its vtable. */
2719 return NULL_TREE;
2720
2721 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2722 {
2723 fn = BV_FN (bv);
2724
2725 if (DECL_NAME (fn) == opname)
2726 {
2727 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2728 lazily_declare_fn (sfk_copy_assignment, t);
2729 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2730 lazily_declare_fn (sfk_move_assignment, t);
2731 }
2732 else if (DECL_DESTRUCTOR_P (fn)
2733 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2734 lazily_declare_fn (sfk_destructor, t);
2735 }
2736
2737 return NULL_TREE;
2738 }
2739
2740 /* If the class type T has a direct or indirect base that contains a
2741 virtual assignment operator or a virtual destructor, declare that
2742 function in T if it hasn't been already declared. */
2743
2744 static void
2745 declare_virt_assop_and_dtor (tree t)
2746 {
2747 if (!(TYPE_POLYMORPHIC_P (t)
2748 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2749 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2750 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2751 return;
2752
2753 dfs_walk_all (TYPE_BINFO (t),
2754 dfs_declare_virt_assop_and_dtor,
2755 NULL, t);
2756 }
2757
2758 /* Create default constructors, assignment operators, and so forth for
2759 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2760 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2761 the class cannot have a default constructor, copy constructor
2762 taking a const reference argument, or an assignment operator taking
2763 a const reference, respectively. */
2764
2765 static void
2766 add_implicitly_declared_members (tree t,
2767 int cant_have_const_cctor,
2768 int cant_have_const_assignment)
2769 {
2770 bool move_ok = false;
2771
2772 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2773 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2774 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2775 move_ok = true;
2776
2777 /* Destructor. */
2778 if (!CLASSTYPE_DESTRUCTORS (t))
2779 {
2780 /* In general, we create destructors lazily. */
2781 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2782
2783 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2784 && TYPE_FOR_JAVA (t))
2785 /* But if this is a Java class, any non-trivial destructor is
2786 invalid, even if compiler-generated. Therefore, if the
2787 destructor is non-trivial we create it now. */
2788 lazily_declare_fn (sfk_destructor, t);
2789 }
2790
2791 /* [class.ctor]
2792
2793 If there is no user-declared constructor for a class, a default
2794 constructor is implicitly declared. */
2795 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2796 {
2797 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2798 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2799 if (cxx_dialect >= cxx0x)
2800 TYPE_HAS_CONSTEXPR_CTOR (t)
2801 /* This might force the declaration. */
2802 = type_has_constexpr_default_constructor (t);
2803 }
2804
2805 /* [class.ctor]
2806
2807 If a class definition does not explicitly declare a copy
2808 constructor, one is declared implicitly. */
2809 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2810 {
2811 TYPE_HAS_COPY_CTOR (t) = 1;
2812 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2813 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2814 if (move_ok)
2815 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2816 }
2817
2818 /* If there is no assignment operator, one will be created if and
2819 when it is needed. For now, just record whether or not the type
2820 of the parameter to the assignment operator will be a const or
2821 non-const reference. */
2822 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2823 {
2824 TYPE_HAS_COPY_ASSIGN (t) = 1;
2825 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2826 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2827 if (move_ok)
2828 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2829 }
2830
2831 /* We can't be lazy about declaring functions that might override
2832 a virtual function from a base class. */
2833 declare_virt_assop_and_dtor (t);
2834 }
2835
2836 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
2837 count the number of fields in TYPE, including anonymous union
2838 members. */
2839
2840 static int
2841 count_fields (tree fields)
2842 {
2843 tree x;
2844 int n_fields = 0;
2845 for (x = fields; x; x = DECL_CHAIN (x))
2846 {
2847 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2848 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2849 else
2850 n_fields += 1;
2851 }
2852 return n_fields;
2853 }
2854
2855 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
2856 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
2857 elts, starting at offset IDX. */
2858
2859 static int
2860 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2861 {
2862 tree x;
2863 for (x = fields; x; x = DECL_CHAIN (x))
2864 {
2865 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2866 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2867 else
2868 field_vec->elts[idx++] = x;
2869 }
2870 return idx;
2871 }
2872
2873 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
2874 starting at offset IDX. */
2875
2876 static int
2877 add_enum_fields_to_record_type (tree enumtype,
2878 struct sorted_fields_type *field_vec,
2879 int idx)
2880 {
2881 tree values;
2882 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
2883 field_vec->elts[idx++] = TREE_VALUE (values);
2884 return idx;
2885 }
2886
2887 /* FIELD is a bit-field. We are finishing the processing for its
2888 enclosing type. Issue any appropriate messages and set appropriate
2889 flags. Returns false if an error has been diagnosed. */
2890
2891 static bool
2892 check_bitfield_decl (tree field)
2893 {
2894 tree type = TREE_TYPE (field);
2895 tree w;
2896
2897 /* Extract the declared width of the bitfield, which has been
2898 temporarily stashed in DECL_INITIAL. */
2899 w = DECL_INITIAL (field);
2900 gcc_assert (w != NULL_TREE);
2901 /* Remove the bit-field width indicator so that the rest of the
2902 compiler does not treat that value as an initializer. */
2903 DECL_INITIAL (field) = NULL_TREE;
2904
2905 /* Detect invalid bit-field type. */
2906 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2907 {
2908 error ("bit-field %q+#D with non-integral type", field);
2909 w = error_mark_node;
2910 }
2911 else
2912 {
2913 location_t loc = input_location;
2914 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2915 STRIP_NOPS (w);
2916
2917 /* detect invalid field size. */
2918 input_location = DECL_SOURCE_LOCATION (field);
2919 w = cxx_constant_value (w);
2920 input_location = loc;
2921
2922 if (TREE_CODE (w) != INTEGER_CST)
2923 {
2924 error ("bit-field %q+D width not an integer constant", field);
2925 w = error_mark_node;
2926 }
2927 else if (tree_int_cst_sgn (w) < 0)
2928 {
2929 error ("negative width in bit-field %q+D", field);
2930 w = error_mark_node;
2931 }
2932 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2933 {
2934 error ("zero width for bit-field %q+D", field);
2935 w = error_mark_node;
2936 }
2937 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2938 && TREE_CODE (type) != ENUMERAL_TYPE
2939 && TREE_CODE (type) != BOOLEAN_TYPE)
2940 warning (0, "width of %q+D exceeds its type", field);
2941 else if (TREE_CODE (type) == ENUMERAL_TYPE
2942 && (0 > (compare_tree_int
2943 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
2944 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2945 }
2946
2947 if (w != error_mark_node)
2948 {
2949 DECL_SIZE (field) = convert (bitsizetype, w);
2950 DECL_BIT_FIELD (field) = 1;
2951 return true;
2952 }
2953 else
2954 {
2955 /* Non-bit-fields are aligned for their type. */
2956 DECL_BIT_FIELD (field) = 0;
2957 CLEAR_DECL_C_BIT_FIELD (field);
2958 return false;
2959 }
2960 }
2961
2962 /* FIELD is a non bit-field. We are finishing the processing for its
2963 enclosing type T. Issue any appropriate messages and set appropriate
2964 flags. */
2965
2966 static void
2967 check_field_decl (tree field,
2968 tree t,
2969 int* cant_have_const_ctor,
2970 int* no_const_asn_ref,
2971 int* any_default_members)
2972 {
2973 tree type = strip_array_types (TREE_TYPE (field));
2974
2975 /* In C++98 an anonymous union cannot contain any fields which would change
2976 the settings of CANT_HAVE_CONST_CTOR and friends. */
2977 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
2978 ;
2979 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
2980 structs. So, we recurse through their fields here. */
2981 else if (ANON_AGGR_TYPE_P (type))
2982 {
2983 tree fields;
2984
2985 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2986 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2987 check_field_decl (fields, t, cant_have_const_ctor,
2988 no_const_asn_ref, any_default_members);
2989 }
2990 /* Check members with class type for constructors, destructors,
2991 etc. */
2992 else if (CLASS_TYPE_P (type))
2993 {
2994 /* Never let anything with uninheritable virtuals
2995 make it through without complaint. */
2996 abstract_virtuals_error (field, type);
2997
2998 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
2999 {
3000 static bool warned;
3001 int oldcount = errorcount;
3002 if (TYPE_NEEDS_CONSTRUCTING (type))
3003 error ("member %q+#D with constructor not allowed in union",
3004 field);
3005 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3006 error ("member %q+#D with destructor not allowed in union", field);
3007 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3008 error ("member %q+#D with copy assignment operator not allowed in union",
3009 field);
3010 if (!warned && errorcount > oldcount)
3011 {
3012 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3013 "only available with -std=c++11 or -std=gnu++11");
3014 warned = true;
3015 }
3016 }
3017 else
3018 {
3019 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3020 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3021 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3022 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3023 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3024 || !TYPE_HAS_COPY_ASSIGN (type));
3025 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3026 || !TYPE_HAS_COPY_CTOR (type));
3027 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3028 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3029 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3030 || TYPE_HAS_COMPLEX_DFLT (type));
3031 }
3032
3033 if (TYPE_HAS_COPY_CTOR (type)
3034 && !TYPE_HAS_CONST_COPY_CTOR (type))
3035 *cant_have_const_ctor = 1;
3036
3037 if (TYPE_HAS_COPY_ASSIGN (type)
3038 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3039 *no_const_asn_ref = 1;
3040 }
3041 if (DECL_INITIAL (field) != NULL_TREE)
3042 {
3043 /* `build_class_init_list' does not recognize
3044 non-FIELD_DECLs. */
3045 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3046 error ("multiple fields in union %qT initialized", t);
3047 *any_default_members = 1;
3048 }
3049 }
3050
3051 /* Check the data members (both static and non-static), class-scoped
3052 typedefs, etc., appearing in the declaration of T. Issue
3053 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3054 declaration order) of access declarations; each TREE_VALUE in this
3055 list is a USING_DECL.
3056
3057 In addition, set the following flags:
3058
3059 EMPTY_P
3060 The class is empty, i.e., contains no non-static data members.
3061
3062 CANT_HAVE_CONST_CTOR_P
3063 This class cannot have an implicitly generated copy constructor
3064 taking a const reference.
3065
3066 CANT_HAVE_CONST_ASN_REF
3067 This class cannot have an implicitly generated assignment
3068 operator taking a const reference.
3069
3070 All of these flags should be initialized before calling this
3071 function.
3072
3073 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3074 fields can be added by adding to this chain. */
3075
3076 static void
3077 check_field_decls (tree t, tree *access_decls,
3078 int *cant_have_const_ctor_p,
3079 int *no_const_asn_ref_p)
3080 {
3081 tree *field;
3082 tree *next;
3083 bool has_pointers;
3084 int any_default_members;
3085 int cant_pack = 0;
3086 int field_access = -1;
3087
3088 /* Assume there are no access declarations. */
3089 *access_decls = NULL_TREE;
3090 /* Assume this class has no pointer members. */
3091 has_pointers = false;
3092 /* Assume none of the members of this class have default
3093 initializations. */
3094 any_default_members = 0;
3095
3096 for (field = &TYPE_FIELDS (t); *field; field = next)
3097 {
3098 tree x = *field;
3099 tree type = TREE_TYPE (x);
3100 int this_field_access;
3101
3102 next = &DECL_CHAIN (x);
3103
3104 if (TREE_CODE (x) == USING_DECL)
3105 {
3106 /* Save the access declarations for our caller. */
3107 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3108 continue;
3109 }
3110
3111 if (TREE_CODE (x) == TYPE_DECL
3112 || TREE_CODE (x) == TEMPLATE_DECL)
3113 continue;
3114
3115 /* If we've gotten this far, it's a data member, possibly static,
3116 or an enumerator. */
3117 if (TREE_CODE (x) != CONST_DECL)
3118 DECL_CONTEXT (x) = t;
3119
3120 /* When this goes into scope, it will be a non-local reference. */
3121 DECL_NONLOCAL (x) = 1;
3122
3123 if (TREE_CODE (t) == UNION_TYPE)
3124 {
3125 /* [class.union]
3126
3127 If a union contains a static data member, or a member of
3128 reference type, the program is ill-formed. */
3129 if (TREE_CODE (x) == VAR_DECL)
3130 {
3131 error ("%q+D may not be static because it is a member of a union", x);
3132 continue;
3133 }
3134 if (TREE_CODE (type) == REFERENCE_TYPE)
3135 {
3136 error ("%q+D may not have reference type %qT because"
3137 " it is a member of a union",
3138 x, type);
3139 continue;
3140 }
3141 }
3142
3143 /* Perform error checking that did not get done in
3144 grokdeclarator. */
3145 if (TREE_CODE (type) == FUNCTION_TYPE)
3146 {
3147 error ("field %q+D invalidly declared function type", x);
3148 type = build_pointer_type (type);
3149 TREE_TYPE (x) = type;
3150 }
3151 else if (TREE_CODE (type) == METHOD_TYPE)
3152 {
3153 error ("field %q+D invalidly declared method type", x);
3154 type = build_pointer_type (type);
3155 TREE_TYPE (x) = type;
3156 }
3157
3158 if (type == error_mark_node)
3159 continue;
3160
3161 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3162 continue;
3163
3164 /* Now it can only be a FIELD_DECL. */
3165
3166 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3167 CLASSTYPE_NON_AGGREGATE (t) = 1;
3168
3169 /* If at least one non-static data member is non-literal, the whole
3170 class becomes non-literal. Note: if the type is incomplete we
3171 will complain later on. */
3172 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3173 CLASSTYPE_LITERAL_P (t) = false;
3174
3175 /* A standard-layout class is a class that:
3176 ...
3177 has the same access control (Clause 11) for all non-static data members,
3178 ... */
3179 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3180 if (field_access == -1)
3181 field_access = this_field_access;
3182 else if (this_field_access != field_access)
3183 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3184
3185 /* If this is of reference type, check if it needs an init. */
3186 if (TREE_CODE (type) == REFERENCE_TYPE)
3187 {
3188 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3189 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3190 if (DECL_INITIAL (x) == NULL_TREE)
3191 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3192
3193 /* ARM $12.6.2: [A member initializer list] (or, for an
3194 aggregate, initialization by a brace-enclosed list) is the
3195 only way to initialize nonstatic const and reference
3196 members. */
3197 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3198 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3199 }
3200
3201 type = strip_array_types (type);
3202
3203 if (TYPE_PACKED (t))
3204 {
3205 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3206 {
3207 warning
3208 (0,
3209 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3210 x);
3211 cant_pack = 1;
3212 }
3213 else if (DECL_C_BIT_FIELD (x)
3214 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3215 DECL_PACKED (x) = 1;
3216 }
3217
3218 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3219 /* We don't treat zero-width bitfields as making a class
3220 non-empty. */
3221 ;
3222 else
3223 {
3224 /* The class is non-empty. */
3225 CLASSTYPE_EMPTY_P (t) = 0;
3226 /* The class is not even nearly empty. */
3227 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3228 /* If one of the data members contains an empty class,
3229 so does T. */
3230 if (CLASS_TYPE_P (type)
3231 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3232 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3233 }
3234
3235 /* This is used by -Weffc++ (see below). Warn only for pointers
3236 to members which might hold dynamic memory. So do not warn
3237 for pointers to functions or pointers to members. */
3238 if (TYPE_PTR_P (type)
3239 && !TYPE_PTRFN_P (type))
3240 has_pointers = true;
3241
3242 if (CLASS_TYPE_P (type))
3243 {
3244 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3245 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3246 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3247 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3248 }
3249
3250 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3251 CLASSTYPE_HAS_MUTABLE (t) = 1;
3252
3253 if (! layout_pod_type_p (type))
3254 /* DR 148 now allows pointers to members (which are POD themselves),
3255 to be allowed in POD structs. */
3256 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3257
3258 if (!std_layout_type_p (type))
3259 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3260
3261 if (! zero_init_p (type))
3262 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3263
3264 /* We set DECL_C_BIT_FIELD in grokbitfield.
3265 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3266 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3267 check_field_decl (x, t,
3268 cant_have_const_ctor_p,
3269 no_const_asn_ref_p,
3270 &any_default_members);
3271
3272 /* Now that we've removed bit-field widths from DECL_INITIAL,
3273 anything left in DECL_INITIAL is an NSDMI that makes the class
3274 non-aggregate. */
3275 if (DECL_INITIAL (x))
3276 CLASSTYPE_NON_AGGREGATE (t) = true;
3277
3278 /* If any field is const, the structure type is pseudo-const. */
3279 if (CP_TYPE_CONST_P (type))
3280 {
3281 C_TYPE_FIELDS_READONLY (t) = 1;
3282 if (DECL_INITIAL (x) == NULL_TREE)
3283 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3284
3285 /* ARM $12.6.2: [A member initializer list] (or, for an
3286 aggregate, initialization by a brace-enclosed list) is the
3287 only way to initialize nonstatic const and reference
3288 members. */
3289 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3290 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3291 }
3292 /* A field that is pseudo-const makes the structure likewise. */
3293 else if (CLASS_TYPE_P (type))
3294 {
3295 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3296 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3297 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3298 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3299 }
3300
3301 /* Core issue 80: A nonstatic data member is required to have a
3302 different name from the class iff the class has a
3303 user-declared constructor. */
3304 if (constructor_name_p (DECL_NAME (x), t)
3305 && TYPE_HAS_USER_CONSTRUCTOR (t))
3306 permerror (input_location, "field %q+#D with same name as class", x);
3307 }
3308
3309 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3310 it should also define a copy constructor and an assignment operator to
3311 implement the correct copy semantic (deep vs shallow, etc.). As it is
3312 not feasible to check whether the constructors do allocate dynamic memory
3313 and store it within members, we approximate the warning like this:
3314
3315 -- Warn only if there are members which are pointers
3316 -- Warn only if there is a non-trivial constructor (otherwise,
3317 there cannot be memory allocated).
3318 -- Warn only if there is a non-trivial destructor. We assume that the
3319 user at least implemented the cleanup correctly, and a destructor
3320 is needed to free dynamic memory.
3321
3322 This seems enough for practical purposes. */
3323 if (warn_ecpp
3324 && has_pointers
3325 && TYPE_HAS_USER_CONSTRUCTOR (t)
3326 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3327 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3328 {
3329 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3330
3331 if (! TYPE_HAS_COPY_CTOR (t))
3332 {
3333 warning (OPT_Weffc__,
3334 " but does not override %<%T(const %T&)%>", t, t);
3335 if (!TYPE_HAS_COPY_ASSIGN (t))
3336 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3337 }
3338 else if (! TYPE_HAS_COPY_ASSIGN (t))
3339 warning (OPT_Weffc__,
3340 " but does not override %<operator=(const %T&)%>", t);
3341 }
3342
3343 /* Non-static data member initializers make the default constructor
3344 non-trivial. */
3345 if (any_default_members)
3346 {
3347 TYPE_NEEDS_CONSTRUCTING (t) = true;
3348 TYPE_HAS_COMPLEX_DFLT (t) = true;
3349 }
3350
3351 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3352 if (cant_pack)
3353 TYPE_PACKED (t) = 0;
3354
3355 /* Check anonymous struct/anonymous union fields. */
3356 finish_struct_anon (t);
3357
3358 /* We've built up the list of access declarations in reverse order.
3359 Fix that now. */
3360 *access_decls = nreverse (*access_decls);
3361 }
3362
3363 /* If TYPE is an empty class type, records its OFFSET in the table of
3364 OFFSETS. */
3365
3366 static int
3367 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3368 {
3369 splay_tree_node n;
3370
3371 if (!is_empty_class (type))
3372 return 0;
3373
3374 /* Record the location of this empty object in OFFSETS. */
3375 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3376 if (!n)
3377 n = splay_tree_insert (offsets,
3378 (splay_tree_key) offset,
3379 (splay_tree_value) NULL_TREE);
3380 n->value = ((splay_tree_value)
3381 tree_cons (NULL_TREE,
3382 type,
3383 (tree) n->value));
3384
3385 return 0;
3386 }
3387
3388 /* Returns nonzero if TYPE is an empty class type and there is
3389 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3390
3391 static int
3392 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3393 {
3394 splay_tree_node n;
3395 tree t;
3396
3397 if (!is_empty_class (type))
3398 return 0;
3399
3400 /* Record the location of this empty object in OFFSETS. */
3401 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3402 if (!n)
3403 return 0;
3404
3405 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3406 if (same_type_p (TREE_VALUE (t), type))
3407 return 1;
3408
3409 return 0;
3410 }
3411
3412 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3413 F for every subobject, passing it the type, offset, and table of
3414 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3415 be traversed.
3416
3417 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3418 than MAX_OFFSET will not be walked.
3419
3420 If F returns a nonzero value, the traversal ceases, and that value
3421 is returned. Otherwise, returns zero. */
3422
3423 static int
3424 walk_subobject_offsets (tree type,
3425 subobject_offset_fn f,
3426 tree offset,
3427 splay_tree offsets,
3428 tree max_offset,
3429 int vbases_p)
3430 {
3431 int r = 0;
3432 tree type_binfo = NULL_TREE;
3433
3434 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3435 stop. */
3436 if (max_offset && INT_CST_LT (max_offset, offset))
3437 return 0;
3438
3439 if (type == error_mark_node)
3440 return 0;
3441
3442 if (!TYPE_P (type))
3443 {
3444 if (abi_version_at_least (2))
3445 type_binfo = type;
3446 type = BINFO_TYPE (type);
3447 }
3448
3449 if (CLASS_TYPE_P (type))
3450 {
3451 tree field;
3452 tree binfo;
3453 int i;
3454
3455 /* Avoid recursing into objects that are not interesting. */
3456 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3457 return 0;
3458
3459 /* Record the location of TYPE. */
3460 r = (*f) (type, offset, offsets);
3461 if (r)
3462 return r;
3463
3464 /* Iterate through the direct base classes of TYPE. */
3465 if (!type_binfo)
3466 type_binfo = TYPE_BINFO (type);
3467 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3468 {
3469 tree binfo_offset;
3470
3471 if (abi_version_at_least (2)
3472 && BINFO_VIRTUAL_P (binfo))
3473 continue;
3474
3475 if (!vbases_p
3476 && BINFO_VIRTUAL_P (binfo)
3477 && !BINFO_PRIMARY_P (binfo))
3478 continue;
3479
3480 if (!abi_version_at_least (2))
3481 binfo_offset = size_binop (PLUS_EXPR,
3482 offset,
3483 BINFO_OFFSET (binfo));
3484 else
3485 {
3486 tree orig_binfo;
3487 /* We cannot rely on BINFO_OFFSET being set for the base
3488 class yet, but the offsets for direct non-virtual
3489 bases can be calculated by going back to the TYPE. */
3490 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3491 binfo_offset = size_binop (PLUS_EXPR,
3492 offset,
3493 BINFO_OFFSET (orig_binfo));
3494 }
3495
3496 r = walk_subobject_offsets (binfo,
3497 f,
3498 binfo_offset,
3499 offsets,
3500 max_offset,
3501 (abi_version_at_least (2)
3502 ? /*vbases_p=*/0 : vbases_p));
3503 if (r)
3504 return r;
3505 }
3506
3507 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3508 {
3509 unsigned ix;
3510 VEC(tree,gc) *vbases;
3511
3512 /* Iterate through the virtual base classes of TYPE. In G++
3513 3.2, we included virtual bases in the direct base class
3514 loop above, which results in incorrect results; the
3515 correct offsets for virtual bases are only known when
3516 working with the most derived type. */
3517 if (vbases_p)
3518 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3519 VEC_iterate (tree, vbases, ix, binfo); ix++)
3520 {
3521 r = walk_subobject_offsets (binfo,
3522 f,
3523 size_binop (PLUS_EXPR,
3524 offset,
3525 BINFO_OFFSET (binfo)),
3526 offsets,
3527 max_offset,
3528 /*vbases_p=*/0);
3529 if (r)
3530 return r;
3531 }
3532 else
3533 {
3534 /* We still have to walk the primary base, if it is
3535 virtual. (If it is non-virtual, then it was walked
3536 above.) */
3537 tree vbase = get_primary_binfo (type_binfo);
3538
3539 if (vbase && BINFO_VIRTUAL_P (vbase)
3540 && BINFO_PRIMARY_P (vbase)
3541 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3542 {
3543 r = (walk_subobject_offsets
3544 (vbase, f, offset,
3545 offsets, max_offset, /*vbases_p=*/0));
3546 if (r)
3547 return r;
3548 }
3549 }
3550 }
3551
3552 /* Iterate through the fields of TYPE. */
3553 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3554 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3555 {
3556 tree field_offset;
3557
3558 if (abi_version_at_least (2))
3559 field_offset = byte_position (field);
3560 else
3561 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3562 field_offset = DECL_FIELD_OFFSET (field);
3563
3564 r = walk_subobject_offsets (TREE_TYPE (field),
3565 f,
3566 size_binop (PLUS_EXPR,
3567 offset,
3568 field_offset),
3569 offsets,
3570 max_offset,
3571 /*vbases_p=*/1);
3572 if (r)
3573 return r;
3574 }
3575 }
3576 else if (TREE_CODE (type) == ARRAY_TYPE)
3577 {
3578 tree element_type = strip_array_types (type);
3579 tree domain = TYPE_DOMAIN (type);
3580 tree index;
3581
3582 /* Avoid recursing into objects that are not interesting. */
3583 if (!CLASS_TYPE_P (element_type)
3584 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3585 return 0;
3586
3587 /* Step through each of the elements in the array. */
3588 for (index = size_zero_node;
3589 /* G++ 3.2 had an off-by-one error here. */
3590 (abi_version_at_least (2)
3591 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3592 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3593 index = size_binop (PLUS_EXPR, index, size_one_node))
3594 {
3595 r = walk_subobject_offsets (TREE_TYPE (type),
3596 f,
3597 offset,
3598 offsets,
3599 max_offset,
3600 /*vbases_p=*/1);
3601 if (r)
3602 return r;
3603 offset = size_binop (PLUS_EXPR, offset,
3604 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3605 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3606 there's no point in iterating through the remaining
3607 elements of the array. */
3608 if (max_offset && INT_CST_LT (max_offset, offset))
3609 break;
3610 }
3611 }
3612
3613 return 0;
3614 }
3615
3616 /* Record all of the empty subobjects of TYPE (either a type or a
3617 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3618 is being placed at OFFSET; otherwise, it is a base class that is
3619 being placed at OFFSET. */
3620
3621 static void
3622 record_subobject_offsets (tree type,
3623 tree offset,
3624 splay_tree offsets,
3625 bool is_data_member)
3626 {
3627 tree max_offset;
3628 /* If recording subobjects for a non-static data member or a
3629 non-empty base class , we do not need to record offsets beyond
3630 the size of the biggest empty class. Additional data members
3631 will go at the end of the class. Additional base classes will go
3632 either at offset zero (if empty, in which case they cannot
3633 overlap with offsets past the size of the biggest empty class) or
3634 at the end of the class.
3635
3636 However, if we are placing an empty base class, then we must record
3637 all offsets, as either the empty class is at offset zero (where
3638 other empty classes might later be placed) or at the end of the
3639 class (where other objects might then be placed, so other empty
3640 subobjects might later overlap). */
3641 if (is_data_member
3642 || !is_empty_class (BINFO_TYPE (type)))
3643 max_offset = sizeof_biggest_empty_class;
3644 else
3645 max_offset = NULL_TREE;
3646 walk_subobject_offsets (type, record_subobject_offset, offset,
3647 offsets, max_offset, is_data_member);
3648 }
3649
3650 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3651 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3652 virtual bases of TYPE are examined. */
3653
3654 static int
3655 layout_conflict_p (tree type,
3656 tree offset,
3657 splay_tree offsets,
3658 int vbases_p)
3659 {
3660 splay_tree_node max_node;
3661
3662 /* Get the node in OFFSETS that indicates the maximum offset where
3663 an empty subobject is located. */
3664 max_node = splay_tree_max (offsets);
3665 /* If there aren't any empty subobjects, then there's no point in
3666 performing this check. */
3667 if (!max_node)
3668 return 0;
3669
3670 return walk_subobject_offsets (type, check_subobject_offset, offset,
3671 offsets, (tree) (max_node->key),
3672 vbases_p);
3673 }
3674
3675 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3676 non-static data member of the type indicated by RLI. BINFO is the
3677 binfo corresponding to the base subobject, OFFSETS maps offsets to
3678 types already located at those offsets. This function determines
3679 the position of the DECL. */
3680
3681 static void
3682 layout_nonempty_base_or_field (record_layout_info rli,
3683 tree decl,
3684 tree binfo,
3685 splay_tree offsets)
3686 {
3687 tree offset = NULL_TREE;
3688 bool field_p;
3689 tree type;
3690
3691 if (binfo)
3692 {
3693 /* For the purposes of determining layout conflicts, we want to
3694 use the class type of BINFO; TREE_TYPE (DECL) will be the
3695 CLASSTYPE_AS_BASE version, which does not contain entries for
3696 zero-sized bases. */
3697 type = TREE_TYPE (binfo);
3698 field_p = false;
3699 }
3700 else
3701 {
3702 type = TREE_TYPE (decl);
3703 field_p = true;
3704 }
3705
3706 /* Try to place the field. It may take more than one try if we have
3707 a hard time placing the field without putting two objects of the
3708 same type at the same address. */
3709 while (1)
3710 {
3711 struct record_layout_info_s old_rli = *rli;
3712
3713 /* Place this field. */
3714 place_field (rli, decl);
3715 offset = byte_position (decl);
3716
3717 /* We have to check to see whether or not there is already
3718 something of the same type at the offset we're about to use.
3719 For example, consider:
3720
3721 struct S {};
3722 struct T : public S { int i; };
3723 struct U : public S, public T {};
3724
3725 Here, we put S at offset zero in U. Then, we can't put T at
3726 offset zero -- its S component would be at the same address
3727 as the S we already allocated. So, we have to skip ahead.
3728 Since all data members, including those whose type is an
3729 empty class, have nonzero size, any overlap can happen only
3730 with a direct or indirect base-class -- it can't happen with
3731 a data member. */
3732 /* In a union, overlap is permitted; all members are placed at
3733 offset zero. */
3734 if (TREE_CODE (rli->t) == UNION_TYPE)
3735 break;
3736 /* G++ 3.2 did not check for overlaps when placing a non-empty
3737 virtual base. */
3738 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3739 break;
3740 if (layout_conflict_p (field_p ? type : binfo, offset,
3741 offsets, field_p))
3742 {
3743 /* Strip off the size allocated to this field. That puts us
3744 at the first place we could have put the field with
3745 proper alignment. */
3746 *rli = old_rli;
3747
3748 /* Bump up by the alignment required for the type. */
3749 rli->bitpos
3750 = size_binop (PLUS_EXPR, rli->bitpos,
3751 bitsize_int (binfo
3752 ? CLASSTYPE_ALIGN (type)
3753 : TYPE_ALIGN (type)));
3754 normalize_rli (rli);
3755 }
3756 else
3757 /* There was no conflict. We're done laying out this field. */
3758 break;
3759 }
3760
3761 /* Now that we know where it will be placed, update its
3762 BINFO_OFFSET. */
3763 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3764 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3765 this point because their BINFO_OFFSET is copied from another
3766 hierarchy. Therefore, we may not need to add the entire
3767 OFFSET. */
3768 propagate_binfo_offsets (binfo,
3769 size_diffop_loc (input_location,
3770 convert (ssizetype, offset),
3771 convert (ssizetype,
3772 BINFO_OFFSET (binfo))));
3773 }
3774
3775 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3776
3777 static int
3778 empty_base_at_nonzero_offset_p (tree type,
3779 tree offset,
3780 splay_tree offsets ATTRIBUTE_UNUSED)
3781 {
3782 return is_empty_class (type) && !integer_zerop (offset);
3783 }
3784
3785 /* Layout the empty base BINFO. EOC indicates the byte currently just
3786 past the end of the class, and should be correctly aligned for a
3787 class of the type indicated by BINFO; OFFSETS gives the offsets of
3788 the empty bases allocated so far. T is the most derived
3789 type. Return nonzero iff we added it at the end. */
3790
3791 static bool
3792 layout_empty_base (record_layout_info rli, tree binfo,
3793 tree eoc, splay_tree offsets)
3794 {
3795 tree alignment;
3796 tree basetype = BINFO_TYPE (binfo);
3797 bool atend = false;
3798
3799 /* This routine should only be used for empty classes. */
3800 gcc_assert (is_empty_class (basetype));
3801 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3802
3803 if (!integer_zerop (BINFO_OFFSET (binfo)))
3804 {
3805 if (abi_version_at_least (2))
3806 propagate_binfo_offsets
3807 (binfo, size_diffop_loc (input_location,
3808 size_zero_node, BINFO_OFFSET (binfo)));
3809 else
3810 warning (OPT_Wabi,
3811 "offset of empty base %qT may not be ABI-compliant and may"
3812 "change in a future version of GCC",
3813 BINFO_TYPE (binfo));
3814 }
3815
3816 /* This is an empty base class. We first try to put it at offset
3817 zero. */
3818 if (layout_conflict_p (binfo,
3819 BINFO_OFFSET (binfo),
3820 offsets,
3821 /*vbases_p=*/0))
3822 {
3823 /* That didn't work. Now, we move forward from the next
3824 available spot in the class. */
3825 atend = true;
3826 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3827 while (1)
3828 {
3829 if (!layout_conflict_p (binfo,
3830 BINFO_OFFSET (binfo),
3831 offsets,
3832 /*vbases_p=*/0))
3833 /* We finally found a spot where there's no overlap. */
3834 break;
3835
3836 /* There's overlap here, too. Bump along to the next spot. */
3837 propagate_binfo_offsets (binfo, alignment);
3838 }
3839 }
3840
3841 if (CLASSTYPE_USER_ALIGN (basetype))
3842 {
3843 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3844 if (warn_packed)
3845 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3846 TYPE_USER_ALIGN (rli->t) = 1;
3847 }
3848
3849 return atend;
3850 }
3851
3852 /* Layout the base given by BINFO in the class indicated by RLI.
3853 *BASE_ALIGN is a running maximum of the alignments of
3854 any base class. OFFSETS gives the location of empty base
3855 subobjects. T is the most derived type. Return nonzero if the new
3856 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3857 *NEXT_FIELD, unless BINFO is for an empty base class.
3858
3859 Returns the location at which the next field should be inserted. */
3860
3861 static tree *
3862 build_base_field (record_layout_info rli, tree binfo,
3863 splay_tree offsets, tree *next_field)
3864 {
3865 tree t = rli->t;
3866 tree basetype = BINFO_TYPE (binfo);
3867
3868 if (!COMPLETE_TYPE_P (basetype))
3869 /* This error is now reported in xref_tag, thus giving better
3870 location information. */
3871 return next_field;
3872
3873 /* Place the base class. */
3874 if (!is_empty_class (basetype))
3875 {
3876 tree decl;
3877
3878 /* The containing class is non-empty because it has a non-empty
3879 base class. */
3880 CLASSTYPE_EMPTY_P (t) = 0;
3881
3882 /* Create the FIELD_DECL. */
3883 decl = build_decl (input_location,
3884 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3885 DECL_ARTIFICIAL (decl) = 1;
3886 DECL_IGNORED_P (decl) = 1;
3887 DECL_FIELD_CONTEXT (decl) = t;
3888 if (CLASSTYPE_AS_BASE (basetype))
3889 {
3890 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3891 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3892 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3893 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3894 DECL_MODE (decl) = TYPE_MODE (basetype);
3895 DECL_FIELD_IS_BASE (decl) = 1;
3896
3897 /* Try to place the field. It may take more than one try if we
3898 have a hard time placing the field without putting two
3899 objects of the same type at the same address. */
3900 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3901 /* Add the new FIELD_DECL to the list of fields for T. */
3902 DECL_CHAIN (decl) = *next_field;
3903 *next_field = decl;
3904 next_field = &DECL_CHAIN (decl);
3905 }
3906 }
3907 else
3908 {
3909 tree eoc;
3910 bool atend;
3911
3912 /* On some platforms (ARM), even empty classes will not be
3913 byte-aligned. */
3914 eoc = round_up_loc (input_location,
3915 rli_size_unit_so_far (rli),
3916 CLASSTYPE_ALIGN_UNIT (basetype));
3917 atend = layout_empty_base (rli, binfo, eoc, offsets);
3918 /* A nearly-empty class "has no proper base class that is empty,
3919 not morally virtual, and at an offset other than zero." */
3920 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3921 {
3922 if (atend)
3923 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3924 /* The check above (used in G++ 3.2) is insufficient because
3925 an empty class placed at offset zero might itself have an
3926 empty base at a nonzero offset. */
3927 else if (walk_subobject_offsets (basetype,
3928 empty_base_at_nonzero_offset_p,
3929 size_zero_node,
3930 /*offsets=*/NULL,
3931 /*max_offset=*/NULL_TREE,
3932 /*vbases_p=*/true))
3933 {
3934 if (abi_version_at_least (2))
3935 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3936 else
3937 warning (OPT_Wabi,
3938 "class %qT will be considered nearly empty in a "
3939 "future version of GCC", t);
3940 }
3941 }
3942
3943 /* We do not create a FIELD_DECL for empty base classes because
3944 it might overlap some other field. We want to be able to
3945 create CONSTRUCTORs for the class by iterating over the
3946 FIELD_DECLs, and the back end does not handle overlapping
3947 FIELD_DECLs. */
3948
3949 /* An empty virtual base causes a class to be non-empty
3950 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3951 here because that was already done when the virtual table
3952 pointer was created. */
3953 }
3954
3955 /* Record the offsets of BINFO and its base subobjects. */
3956 record_subobject_offsets (binfo,
3957 BINFO_OFFSET (binfo),
3958 offsets,
3959 /*is_data_member=*/false);
3960
3961 return next_field;
3962 }
3963
3964 /* Layout all of the non-virtual base classes. Record empty
3965 subobjects in OFFSETS. T is the most derived type. Return nonzero
3966 if the type cannot be nearly empty. The fields created
3967 corresponding to the base classes will be inserted at
3968 *NEXT_FIELD. */
3969
3970 static void
3971 build_base_fields (record_layout_info rli,
3972 splay_tree offsets, tree *next_field)
3973 {
3974 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3975 subobjects. */
3976 tree t = rli->t;
3977 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3978 int i;
3979
3980 /* The primary base class is always allocated first. */
3981 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3982 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3983 offsets, next_field);
3984
3985 /* Now allocate the rest of the bases. */
3986 for (i = 0; i < n_baseclasses; ++i)
3987 {
3988 tree base_binfo;
3989
3990 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3991
3992 /* The primary base was already allocated above, so we don't
3993 need to allocate it again here. */
3994 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3995 continue;
3996
3997 /* Virtual bases are added at the end (a primary virtual base
3998 will have already been added). */
3999 if (BINFO_VIRTUAL_P (base_binfo))
4000 continue;
4001
4002 next_field = build_base_field (rli, base_binfo,
4003 offsets, next_field);
4004 }
4005 }
4006
4007 /* Go through the TYPE_METHODS of T issuing any appropriate
4008 diagnostics, figuring out which methods override which other
4009 methods, and so forth. */
4010
4011 static void
4012 check_methods (tree t)
4013 {
4014 tree x;
4015
4016 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4017 {
4018 check_for_override (x, t);
4019 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4020 error ("initializer specified for non-virtual method %q+D", x);
4021 /* The name of the field is the original field name
4022 Save this in auxiliary field for later overloading. */
4023 if (DECL_VINDEX (x))
4024 {
4025 TYPE_POLYMORPHIC_P (t) = 1;
4026 if (DECL_PURE_VIRTUAL_P (x))
4027 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
4028 }
4029 /* All user-provided destructors are non-trivial.
4030 Constructors and assignment ops are handled in
4031 grok_special_member_properties. */
4032 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4033 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4034 }
4035 }
4036
4037 /* FN is a constructor or destructor. Clone the declaration to create
4038 a specialized in-charge or not-in-charge version, as indicated by
4039 NAME. */
4040
4041 static tree
4042 build_clone (tree fn, tree name)
4043 {
4044 tree parms;
4045 tree clone;
4046
4047 /* Copy the function. */
4048 clone = copy_decl (fn);
4049 /* Reset the function name. */
4050 DECL_NAME (clone) = name;
4051 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4052 /* Remember where this function came from. */
4053 DECL_ABSTRACT_ORIGIN (clone) = fn;
4054 /* Make it easy to find the CLONE given the FN. */
4055 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4056 DECL_CHAIN (fn) = clone;
4057
4058 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4059 if (TREE_CODE (clone) == TEMPLATE_DECL)
4060 {
4061 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4062 DECL_TEMPLATE_RESULT (clone) = result;
4063 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4064 DECL_TI_TEMPLATE (result) = clone;
4065 TREE_TYPE (clone) = TREE_TYPE (result);
4066 return clone;
4067 }
4068
4069 DECL_CLONED_FUNCTION (clone) = fn;
4070 /* There's no pending inline data for this function. */
4071 DECL_PENDING_INLINE_INFO (clone) = NULL;
4072 DECL_PENDING_INLINE_P (clone) = 0;
4073
4074 /* The base-class destructor is not virtual. */
4075 if (name == base_dtor_identifier)
4076 {
4077 DECL_VIRTUAL_P (clone) = 0;
4078 if (TREE_CODE (clone) != TEMPLATE_DECL)
4079 DECL_VINDEX (clone) = NULL_TREE;
4080 }
4081
4082 /* If there was an in-charge parameter, drop it from the function
4083 type. */
4084 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4085 {
4086 tree basetype;
4087 tree parmtypes;
4088 tree exceptions;
4089
4090 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4091 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4092 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4093 /* Skip the `this' parameter. */
4094 parmtypes = TREE_CHAIN (parmtypes);
4095 /* Skip the in-charge parameter. */
4096 parmtypes = TREE_CHAIN (parmtypes);
4097 /* And the VTT parm, in a complete [cd]tor. */
4098 if (DECL_HAS_VTT_PARM_P (fn)
4099 && ! DECL_NEEDS_VTT_PARM_P (clone))
4100 parmtypes = TREE_CHAIN (parmtypes);
4101 /* If this is subobject constructor or destructor, add the vtt
4102 parameter. */
4103 TREE_TYPE (clone)
4104 = build_method_type_directly (basetype,
4105 TREE_TYPE (TREE_TYPE (clone)),
4106 parmtypes);
4107 if (exceptions)
4108 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4109 exceptions);
4110 TREE_TYPE (clone)
4111 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4112 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4113 }
4114
4115 /* Copy the function parameters. */
4116 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4117 /* Remove the in-charge parameter. */
4118 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4119 {
4120 DECL_CHAIN (DECL_ARGUMENTS (clone))
4121 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4122 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4123 }
4124 /* And the VTT parm, in a complete [cd]tor. */
4125 if (DECL_HAS_VTT_PARM_P (fn))
4126 {
4127 if (DECL_NEEDS_VTT_PARM_P (clone))
4128 DECL_HAS_VTT_PARM_P (clone) = 1;
4129 else
4130 {
4131 DECL_CHAIN (DECL_ARGUMENTS (clone))
4132 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4133 DECL_HAS_VTT_PARM_P (clone) = 0;
4134 }
4135 }
4136
4137 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4138 {
4139 DECL_CONTEXT (parms) = clone;
4140 cxx_dup_lang_specific_decl (parms);
4141 }
4142
4143 /* Create the RTL for this function. */
4144 SET_DECL_RTL (clone, NULL);
4145 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4146
4147 if (pch_file)
4148 note_decl_for_pch (clone);
4149
4150 return clone;
4151 }
4152
4153 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4154 not invoke this function directly.
4155
4156 For a non-thunk function, returns the address of the slot for storing
4157 the function it is a clone of. Otherwise returns NULL_TREE.
4158
4159 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4160 cloned_function is unset. This is to support the separate
4161 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4162 on a template makes sense, but not the former. */
4163
4164 tree *
4165 decl_cloned_function_p (const_tree decl, bool just_testing)
4166 {
4167 tree *ptr;
4168 if (just_testing)
4169 decl = STRIP_TEMPLATE (decl);
4170
4171 if (TREE_CODE (decl) != FUNCTION_DECL
4172 || !DECL_LANG_SPECIFIC (decl)
4173 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4174 {
4175 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4176 if (!just_testing)
4177 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4178 else
4179 #endif
4180 return NULL;
4181 }
4182
4183 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4184 if (just_testing && *ptr == NULL_TREE)
4185 return NULL;
4186 else
4187 return ptr;
4188 }
4189
4190 /* Produce declarations for all appropriate clones of FN. If
4191 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4192 CLASTYPE_METHOD_VEC as well. */
4193
4194 void
4195 clone_function_decl (tree fn, int update_method_vec_p)
4196 {
4197 tree clone;
4198
4199 /* Avoid inappropriate cloning. */
4200 if (DECL_CHAIN (fn)
4201 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4202 return;
4203
4204 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4205 {
4206 /* For each constructor, we need two variants: an in-charge version
4207 and a not-in-charge version. */
4208 clone = build_clone (fn, complete_ctor_identifier);
4209 if (update_method_vec_p)
4210 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4211 clone = build_clone (fn, base_ctor_identifier);
4212 if (update_method_vec_p)
4213 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4214 }
4215 else
4216 {
4217 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4218
4219 /* For each destructor, we need three variants: an in-charge
4220 version, a not-in-charge version, and an in-charge deleting
4221 version. We clone the deleting version first because that
4222 means it will go second on the TYPE_METHODS list -- and that
4223 corresponds to the correct layout order in the virtual
4224 function table.
4225
4226 For a non-virtual destructor, we do not build a deleting
4227 destructor. */
4228 if (DECL_VIRTUAL_P (fn))
4229 {
4230 clone = build_clone (fn, deleting_dtor_identifier);
4231 if (update_method_vec_p)
4232 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4233 }
4234 clone = build_clone (fn, complete_dtor_identifier);
4235 if (update_method_vec_p)
4236 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4237 clone = build_clone (fn, base_dtor_identifier);
4238 if (update_method_vec_p)
4239 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4240 }
4241
4242 /* Note that this is an abstract function that is never emitted. */
4243 DECL_ABSTRACT (fn) = 1;
4244 }
4245
4246 /* DECL is an in charge constructor, which is being defined. This will
4247 have had an in class declaration, from whence clones were
4248 declared. An out-of-class definition can specify additional default
4249 arguments. As it is the clones that are involved in overload
4250 resolution, we must propagate the information from the DECL to its
4251 clones. */
4252
4253 void
4254 adjust_clone_args (tree decl)
4255 {
4256 tree clone;
4257
4258 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4259 clone = DECL_CHAIN (clone))
4260 {
4261 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4262 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4263 tree decl_parms, clone_parms;
4264
4265 clone_parms = orig_clone_parms;
4266
4267 /* Skip the 'this' parameter. */
4268 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4269 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4270
4271 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4272 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4273 if (DECL_HAS_VTT_PARM_P (decl))
4274 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4275
4276 clone_parms = orig_clone_parms;
4277 if (DECL_HAS_VTT_PARM_P (clone))
4278 clone_parms = TREE_CHAIN (clone_parms);
4279
4280 for (decl_parms = orig_decl_parms; decl_parms;
4281 decl_parms = TREE_CHAIN (decl_parms),
4282 clone_parms = TREE_CHAIN (clone_parms))
4283 {
4284 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4285 TREE_TYPE (clone_parms)));
4286
4287 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4288 {
4289 /* A default parameter has been added. Adjust the
4290 clone's parameters. */
4291 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4292 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4293 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4294 tree type;
4295
4296 clone_parms = orig_decl_parms;
4297
4298 if (DECL_HAS_VTT_PARM_P (clone))
4299 {
4300 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4301 TREE_VALUE (orig_clone_parms),
4302 clone_parms);
4303 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4304 }
4305 type = build_method_type_directly (basetype,
4306 TREE_TYPE (TREE_TYPE (clone)),
4307 clone_parms);
4308 if (exceptions)
4309 type = build_exception_variant (type, exceptions);
4310 if (attrs)
4311 type = cp_build_type_attribute_variant (type, attrs);
4312 TREE_TYPE (clone) = type;
4313
4314 clone_parms = NULL_TREE;
4315 break;
4316 }
4317 }
4318 gcc_assert (!clone_parms);
4319 }
4320 }
4321
4322 /* For each of the constructors and destructors in T, create an
4323 in-charge and not-in-charge variant. */
4324
4325 static void
4326 clone_constructors_and_destructors (tree t)
4327 {
4328 tree fns;
4329
4330 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4331 out now. */
4332 if (!CLASSTYPE_METHOD_VEC (t))
4333 return;
4334
4335 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4336 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4337 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4338 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4339 }
4340
4341 /* Deduce noexcept for a destructor DTOR. */
4342
4343 void
4344 deduce_noexcept_on_destructor (tree dtor)
4345 {
4346 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4347 {
4348 tree ctx = DECL_CONTEXT (dtor);
4349 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4350 /*const_p=*/false);
4351 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4352 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4353 }
4354 }
4355
4356 /* For each destructor in T, deduce noexcept:
4357
4358 12.4/3: A declaration of a destructor that does not have an
4359 exception-specification is implicitly considered to have the
4360 same exception-specification as an implicit declaration (15.4). */
4361
4362 static void
4363 deduce_noexcept_on_destructors (tree t)
4364 {
4365 tree fns;
4366
4367 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4368 out now. */
4369 if (!CLASSTYPE_METHOD_VEC (t))
4370 return;
4371
4372 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4373 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4374 }
4375
4376 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4377 of TYPE for virtual functions which FNDECL overrides. Return a
4378 mask of the tm attributes found therein. */
4379
4380 static int
4381 look_for_tm_attr_overrides (tree type, tree fndecl)
4382 {
4383 tree binfo = TYPE_BINFO (type);
4384 tree base_binfo;
4385 int ix, found = 0;
4386
4387 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4388 {
4389 tree o, basetype = BINFO_TYPE (base_binfo);
4390
4391 if (!TYPE_POLYMORPHIC_P (basetype))
4392 continue;
4393
4394 o = look_for_overrides_here (basetype, fndecl);
4395 if (o)
4396 found |= tm_attr_to_mask (find_tm_attribute
4397 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4398 else
4399 found |= look_for_tm_attr_overrides (basetype, fndecl);
4400 }
4401
4402 return found;
4403 }
4404
4405 /* Subroutine of set_method_tm_attributes. Handle the checks and
4406 inheritance for one virtual method FNDECL. */
4407
4408 static void
4409 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4410 {
4411 tree tm_attr;
4412 int found, have;
4413
4414 found = look_for_tm_attr_overrides (type, fndecl);
4415
4416 /* If FNDECL doesn't actually override anything (i.e. T is the
4417 class that first declares FNDECL virtual), then we're done. */
4418 if (found == 0)
4419 return;
4420
4421 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4422 have = tm_attr_to_mask (tm_attr);
4423
4424 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4425 tm_pure must match exactly, otherwise no weakening of
4426 tm_safe > tm_callable > nothing. */
4427 /* ??? The tm_pure attribute didn't make the transition to the
4428 multivendor language spec. */
4429 if (have == TM_ATTR_PURE)
4430 {
4431 if (found != TM_ATTR_PURE)
4432 {
4433 found &= -found;
4434 goto err_override;
4435 }
4436 }
4437 /* If the overridden function is tm_pure, then FNDECL must be. */
4438 else if (found == TM_ATTR_PURE && tm_attr)
4439 goto err_override;
4440 /* Look for base class combinations that cannot be satisfied. */
4441 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4442 {
4443 found &= ~TM_ATTR_PURE;
4444 found &= -found;
4445 error_at (DECL_SOURCE_LOCATION (fndecl),
4446 "method overrides both %<transaction_pure%> and %qE methods",
4447 tm_mask_to_attr (found));
4448 }
4449 /* If FNDECL did not declare an attribute, then inherit the most
4450 restrictive one. */
4451 else if (tm_attr == NULL)
4452 {
4453 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4454 }
4455 /* Otherwise validate that we're not weaker than a function
4456 that is being overridden. */
4457 else
4458 {
4459 found &= -found;
4460 if (found <= TM_ATTR_CALLABLE && have > found)
4461 goto err_override;
4462 }
4463 return;
4464
4465 err_override:
4466 error_at (DECL_SOURCE_LOCATION (fndecl),
4467 "method declared %qE overriding %qE method",
4468 tm_attr, tm_mask_to_attr (found));
4469 }
4470
4471 /* For each of the methods in T, propagate a class-level tm attribute. */
4472
4473 static void
4474 set_method_tm_attributes (tree t)
4475 {
4476 tree class_tm_attr, fndecl;
4477
4478 /* Don't bother collecting tm attributes if transactional memory
4479 support is not enabled. */
4480 if (!flag_tm)
4481 return;
4482
4483 /* Process virtual methods first, as they inherit directly from the
4484 base virtual function and also require validation of new attributes. */
4485 if (TYPE_CONTAINS_VPTR_P (t))
4486 {
4487 tree vchain;
4488 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4489 vchain = TREE_CHAIN (vchain))
4490 {
4491 fndecl = BV_FN (vchain);
4492 if (DECL_THUNK_P (fndecl))
4493 fndecl = THUNK_TARGET (fndecl);
4494 set_one_vmethod_tm_attributes (t, fndecl);
4495 }
4496 }
4497
4498 /* If the class doesn't have an attribute, nothing more to do. */
4499 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4500 if (class_tm_attr == NULL)
4501 return;
4502
4503 /* Any method that does not yet have a tm attribute inherits
4504 the one from the class. */
4505 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4506 {
4507 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4508 apply_tm_attr (fndecl, class_tm_attr);
4509 }
4510 }
4511
4512 /* Returns true iff class T has a user-defined constructor other than
4513 the default constructor. */
4514
4515 bool
4516 type_has_user_nondefault_constructor (tree t)
4517 {
4518 tree fns;
4519
4520 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4521 return false;
4522
4523 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4524 {
4525 tree fn = OVL_CURRENT (fns);
4526 if (!DECL_ARTIFICIAL (fn)
4527 && (TREE_CODE (fn) == TEMPLATE_DECL
4528 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4529 != NULL_TREE)))
4530 return true;
4531 }
4532
4533 return false;
4534 }
4535
4536 /* Returns the defaulted constructor if T has one. Otherwise, returns
4537 NULL_TREE. */
4538
4539 tree
4540 in_class_defaulted_default_constructor (tree t)
4541 {
4542 tree fns, args;
4543
4544 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4545 return NULL_TREE;
4546
4547 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4548 {
4549 tree fn = OVL_CURRENT (fns);
4550
4551 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4552 {
4553 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4554 while (args && TREE_PURPOSE (args))
4555 args = TREE_CHAIN (args);
4556 if (!args || args == void_list_node)
4557 return fn;
4558 }
4559 }
4560
4561 return NULL_TREE;
4562 }
4563
4564 /* Returns true iff FN is a user-provided function, i.e. user-declared
4565 and not defaulted at its first declaration; or explicit, private,
4566 protected, or non-const. */
4567
4568 bool
4569 user_provided_p (tree fn)
4570 {
4571 if (TREE_CODE (fn) == TEMPLATE_DECL)
4572 return true;
4573 else
4574 return (!DECL_ARTIFICIAL (fn)
4575 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4576 }
4577
4578 /* Returns true iff class T has a user-provided constructor. */
4579
4580 bool
4581 type_has_user_provided_constructor (tree t)
4582 {
4583 tree fns;
4584
4585 if (!CLASS_TYPE_P (t))
4586 return false;
4587
4588 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4589 return false;
4590
4591 /* This can happen in error cases; avoid crashing. */
4592 if (!CLASSTYPE_METHOD_VEC (t))
4593 return false;
4594
4595 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4596 if (user_provided_p (OVL_CURRENT (fns)))
4597 return true;
4598
4599 return false;
4600 }
4601
4602 /* Returns true iff class T has a user-provided default constructor. */
4603
4604 bool
4605 type_has_user_provided_default_constructor (tree t)
4606 {
4607 tree fns;
4608
4609 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4610 return false;
4611
4612 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4613 {
4614 tree fn = OVL_CURRENT (fns);
4615 if (TREE_CODE (fn) == FUNCTION_DECL
4616 && user_provided_p (fn)
4617 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4618 return true;
4619 }
4620
4621 return false;
4622 }
4623
4624 /* If default-initialization leaves part of TYPE uninitialized, returns
4625 a DECL for the field or TYPE itself (DR 253). */
4626
4627 tree
4628 default_init_uninitialized_part (tree type)
4629 {
4630 tree t, r, binfo;
4631 int i;
4632
4633 type = strip_array_types (type);
4634 if (!CLASS_TYPE_P (type))
4635 return type;
4636 if (type_has_user_provided_default_constructor (type))
4637 return NULL_TREE;
4638 for (binfo = TYPE_BINFO (type), i = 0;
4639 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4640 {
4641 r = default_init_uninitialized_part (BINFO_TYPE (t));
4642 if (r)
4643 return r;
4644 }
4645 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4646 if (TREE_CODE (t) == FIELD_DECL
4647 && !DECL_ARTIFICIAL (t)
4648 && !DECL_INITIAL (t))
4649 {
4650 r = default_init_uninitialized_part (TREE_TYPE (t));
4651 if (r)
4652 return DECL_P (r) ? r : t;
4653 }
4654
4655 return NULL_TREE;
4656 }
4657
4658 /* Returns true iff for class T, a trivial synthesized default constructor
4659 would be constexpr. */
4660
4661 bool
4662 trivial_default_constructor_is_constexpr (tree t)
4663 {
4664 /* A defaulted trivial default constructor is constexpr
4665 if there is nothing to initialize. */
4666 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4667 return is_really_empty_class (t);
4668 }
4669
4670 /* Returns true iff class T has a constexpr default constructor. */
4671
4672 bool
4673 type_has_constexpr_default_constructor (tree t)
4674 {
4675 tree fns;
4676
4677 if (!CLASS_TYPE_P (t))
4678 {
4679 /* The caller should have stripped an enclosing array. */
4680 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4681 return false;
4682 }
4683 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4684 {
4685 if (!TYPE_HAS_COMPLEX_DFLT (t))
4686 return trivial_default_constructor_is_constexpr (t);
4687 /* Non-trivial, we need to check subobject constructors. */
4688 lazily_declare_fn (sfk_constructor, t);
4689 }
4690 fns = locate_ctor (t);
4691 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4692 }
4693
4694 /* Returns true iff class TYPE has a virtual destructor. */
4695
4696 bool
4697 type_has_virtual_destructor (tree type)
4698 {
4699 tree dtor;
4700
4701 if (!CLASS_TYPE_P (type))
4702 return false;
4703
4704 gcc_assert (COMPLETE_TYPE_P (type));
4705 dtor = CLASSTYPE_DESTRUCTORS (type);
4706 return (dtor && DECL_VIRTUAL_P (dtor));
4707 }
4708
4709 /* Returns true iff class T has a move constructor. */
4710
4711 bool
4712 type_has_move_constructor (tree t)
4713 {
4714 tree fns;
4715
4716 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4717 {
4718 gcc_assert (COMPLETE_TYPE_P (t));
4719 lazily_declare_fn (sfk_move_constructor, t);
4720 }
4721
4722 if (!CLASSTYPE_METHOD_VEC (t))
4723 return false;
4724
4725 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4726 if (move_fn_p (OVL_CURRENT (fns)))
4727 return true;
4728
4729 return false;
4730 }
4731
4732 /* Returns true iff class T has a move assignment operator. */
4733
4734 bool
4735 type_has_move_assign (tree t)
4736 {
4737 tree fns;
4738
4739 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4740 {
4741 gcc_assert (COMPLETE_TYPE_P (t));
4742 lazily_declare_fn (sfk_move_assignment, t);
4743 }
4744
4745 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4746 fns; fns = OVL_NEXT (fns))
4747 if (move_fn_p (OVL_CURRENT (fns)))
4748 return true;
4749
4750 return false;
4751 }
4752
4753 /* Returns true iff class T has a move constructor that was explicitly
4754 declared in the class body. Note that this is different from
4755 "user-provided", which doesn't include functions that are defaulted in
4756 the class. */
4757
4758 bool
4759 type_has_user_declared_move_constructor (tree t)
4760 {
4761 tree fns;
4762
4763 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4764 return false;
4765
4766 if (!CLASSTYPE_METHOD_VEC (t))
4767 return false;
4768
4769 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4770 {
4771 tree fn = OVL_CURRENT (fns);
4772 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4773 return true;
4774 }
4775
4776 return false;
4777 }
4778
4779 /* Returns true iff class T has a move assignment operator that was
4780 explicitly declared in the class body. */
4781
4782 bool
4783 type_has_user_declared_move_assign (tree t)
4784 {
4785 tree fns;
4786
4787 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4788 return false;
4789
4790 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4791 fns; fns = OVL_NEXT (fns))
4792 {
4793 tree fn = OVL_CURRENT (fns);
4794 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4795 return true;
4796 }
4797
4798 return false;
4799 }
4800
4801 /* Nonzero if we need to build up a constructor call when initializing an
4802 object of this class, either because it has a user-provided constructor
4803 or because it doesn't have a default constructor (so we need to give an
4804 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
4805 what you care about is whether or not an object can be produced by a
4806 constructor (e.g. so we don't set TREE_READONLY on const variables of
4807 such type); use this function when what you care about is whether or not
4808 to try to call a constructor to create an object. The latter case is
4809 the former plus some cases of constructors that cannot be called. */
4810
4811 bool
4812 type_build_ctor_call (tree t)
4813 {
4814 tree inner;
4815 if (TYPE_NEEDS_CONSTRUCTING (t))
4816 return true;
4817 inner = strip_array_types (t);
4818 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
4819 && !ANON_AGGR_TYPE_P (inner));
4820 }
4821
4822 /* Remove all zero-width bit-fields from T. */
4823
4824 static void
4825 remove_zero_width_bit_fields (tree t)
4826 {
4827 tree *fieldsp;
4828
4829 fieldsp = &TYPE_FIELDS (t);
4830 while (*fieldsp)
4831 {
4832 if (TREE_CODE (*fieldsp) == FIELD_DECL
4833 && DECL_C_BIT_FIELD (*fieldsp)
4834 /* We should not be confused by the fact that grokbitfield
4835 temporarily sets the width of the bit field into
4836 DECL_INITIAL (*fieldsp).
4837 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4838 to that width. */
4839 && integer_zerop (DECL_SIZE (*fieldsp)))
4840 *fieldsp = DECL_CHAIN (*fieldsp);
4841 else
4842 fieldsp = &DECL_CHAIN (*fieldsp);
4843 }
4844 }
4845
4846 /* Returns TRUE iff we need a cookie when dynamically allocating an
4847 array whose elements have the indicated class TYPE. */
4848
4849 static bool
4850 type_requires_array_cookie (tree type)
4851 {
4852 tree fns;
4853 bool has_two_argument_delete_p = false;
4854
4855 gcc_assert (CLASS_TYPE_P (type));
4856
4857 /* If there's a non-trivial destructor, we need a cookie. In order
4858 to iterate through the array calling the destructor for each
4859 element, we'll have to know how many elements there are. */
4860 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4861 return true;
4862
4863 /* If the usual deallocation function is a two-argument whose second
4864 argument is of type `size_t', then we have to pass the size of
4865 the array to the deallocation function, so we will need to store
4866 a cookie. */
4867 fns = lookup_fnfields (TYPE_BINFO (type),
4868 ansi_opname (VEC_DELETE_EXPR),
4869 /*protect=*/0);
4870 /* If there are no `operator []' members, or the lookup is
4871 ambiguous, then we don't need a cookie. */
4872 if (!fns || fns == error_mark_node)
4873 return false;
4874 /* Loop through all of the functions. */
4875 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4876 {
4877 tree fn;
4878 tree second_parm;
4879
4880 /* Select the current function. */
4881 fn = OVL_CURRENT (fns);
4882 /* See if this function is a one-argument delete function. If
4883 it is, then it will be the usual deallocation function. */
4884 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4885 if (second_parm == void_list_node)
4886 return false;
4887 /* Do not consider this function if its second argument is an
4888 ellipsis. */
4889 if (!second_parm)
4890 continue;
4891 /* Otherwise, if we have a two-argument function and the second
4892 argument is `size_t', it will be the usual deallocation
4893 function -- unless there is one-argument function, too. */
4894 if (TREE_CHAIN (second_parm) == void_list_node
4895 && same_type_p (TREE_VALUE (second_parm), size_type_node))
4896 has_two_argument_delete_p = true;
4897 }
4898
4899 return has_two_argument_delete_p;
4900 }
4901
4902 /* Finish computing the `literal type' property of class type T.
4903
4904 At this point, we have already processed base classes and
4905 non-static data members. We need to check whether the copy
4906 constructor is trivial, the destructor is trivial, and there
4907 is a trivial default constructor or at least one constexpr
4908 constructor other than the copy constructor. */
4909
4910 static void
4911 finalize_literal_type_property (tree t)
4912 {
4913 tree fn;
4914
4915 if (cxx_dialect < cxx0x
4916 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4917 CLASSTYPE_LITERAL_P (t) = false;
4918 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
4919 && CLASSTYPE_NON_AGGREGATE (t)
4920 && !TYPE_HAS_CONSTEXPR_CTOR (t))
4921 CLASSTYPE_LITERAL_P (t) = false;
4922
4923 if (!CLASSTYPE_LITERAL_P (t))
4924 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
4925 if (DECL_DECLARED_CONSTEXPR_P (fn)
4926 && TREE_CODE (fn) != TEMPLATE_DECL
4927 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
4928 && !DECL_CONSTRUCTOR_P (fn))
4929 {
4930 DECL_DECLARED_CONSTEXPR_P (fn) = false;
4931 if (!DECL_GENERATED_P (fn))
4932 {
4933 error ("enclosing class of constexpr non-static member "
4934 "function %q+#D is not a literal type", fn);
4935 explain_non_literal_class (t);
4936 }
4937 }
4938 }
4939
4940 /* T is a non-literal type used in a context which requires a constant
4941 expression. Explain why it isn't literal. */
4942
4943 void
4944 explain_non_literal_class (tree t)
4945 {
4946 static struct pointer_set_t *diagnosed;
4947
4948 if (!CLASS_TYPE_P (t))
4949 return;
4950 t = TYPE_MAIN_VARIANT (t);
4951
4952 if (diagnosed == NULL)
4953 diagnosed = pointer_set_create ();
4954 if (pointer_set_insert (diagnosed, t) != 0)
4955 /* Already explained. */
4956 return;
4957
4958 inform (0, "%q+T is not literal because:", t);
4959 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4960 inform (0, " %q+T has a non-trivial destructor", t);
4961 else if (CLASSTYPE_NON_AGGREGATE (t)
4962 && !TYPE_HAS_TRIVIAL_DFLT (t)
4963 && !TYPE_HAS_CONSTEXPR_CTOR (t))
4964 {
4965 inform (0, " %q+T is not an aggregate, does not have a trivial "
4966 "default constructor, and has no constexpr constructor that "
4967 "is not a copy or move constructor", t);
4968 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
4969 && !type_has_user_provided_default_constructor (t))
4970 {
4971 /* Note that we can't simply call locate_ctor because when the
4972 constructor is deleted it just returns NULL_TREE. */
4973 tree fns;
4974 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4975 {
4976 tree fn = OVL_CURRENT (fns);
4977 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
4978
4979 parms = skip_artificial_parms_for (fn, parms);
4980
4981 if (sufficient_parms_p (parms))
4982 {
4983 if (DECL_DELETED_FN (fn))
4984 maybe_explain_implicit_delete (fn);
4985 else
4986 explain_invalid_constexpr_fn (fn);
4987 break;
4988 }
4989 }
4990 }
4991 }
4992 else
4993 {
4994 tree binfo, base_binfo, field; int i;
4995 for (binfo = TYPE_BINFO (t), i = 0;
4996 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4997 {
4998 tree basetype = TREE_TYPE (base_binfo);
4999 if (!CLASSTYPE_LITERAL_P (basetype))
5000 {
5001 inform (0, " base class %qT of %q+T is non-literal",
5002 basetype, t);
5003 explain_non_literal_class (basetype);
5004 return;
5005 }
5006 }
5007 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5008 {
5009 tree ftype;
5010 if (TREE_CODE (field) != FIELD_DECL)
5011 continue;
5012 ftype = TREE_TYPE (field);
5013 if (!literal_type_p (ftype))
5014 {
5015 inform (0, " non-static data member %q+D has "
5016 "non-literal type", field);
5017 if (CLASS_TYPE_P (ftype))
5018 explain_non_literal_class (ftype);
5019 }
5020 }
5021 }
5022 }
5023
5024 /* Check the validity of the bases and members declared in T. Add any
5025 implicitly-generated functions (like copy-constructors and
5026 assignment operators). Compute various flag bits (like
5027 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5028 level: i.e., independently of the ABI in use. */
5029
5030 static void
5031 check_bases_and_members (tree t)
5032 {
5033 /* Nonzero if the implicitly generated copy constructor should take
5034 a non-const reference argument. */
5035 int cant_have_const_ctor;
5036 /* Nonzero if the implicitly generated assignment operator
5037 should take a non-const reference argument. */
5038 int no_const_asn_ref;
5039 tree access_decls;
5040 bool saved_complex_asn_ref;
5041 bool saved_nontrivial_dtor;
5042 tree fn;
5043
5044 /* By default, we use const reference arguments and generate default
5045 constructors. */
5046 cant_have_const_ctor = 0;
5047 no_const_asn_ref = 0;
5048
5049 /* Deduce noexcept on destructors. */
5050 if (cxx_dialect >= cxx0x)
5051 deduce_noexcept_on_destructors (t);
5052
5053 /* Check all the base-classes. */
5054 check_bases (t, &cant_have_const_ctor,
5055 &no_const_asn_ref);
5056
5057 /* Check all the method declarations. */
5058 check_methods (t);
5059
5060 /* Save the initial values of these flags which only indicate whether
5061 or not the class has user-provided functions. As we analyze the
5062 bases and members we can set these flags for other reasons. */
5063 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5064 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5065
5066 /* Check all the data member declarations. We cannot call
5067 check_field_decls until we have called check_bases check_methods,
5068 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5069 being set appropriately. */
5070 check_field_decls (t, &access_decls,
5071 &cant_have_const_ctor,
5072 &no_const_asn_ref);
5073
5074 /* A nearly-empty class has to be vptr-containing; a nearly empty
5075 class contains just a vptr. */
5076 if (!TYPE_CONTAINS_VPTR_P (t))
5077 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5078
5079 /* Do some bookkeeping that will guide the generation of implicitly
5080 declared member functions. */
5081 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5082 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5083 /* We need to call a constructor for this class if it has a
5084 user-provided constructor, or if the default constructor is going
5085 to initialize the vptr. (This is not an if-and-only-if;
5086 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5087 themselves need constructing.) */
5088 TYPE_NEEDS_CONSTRUCTING (t)
5089 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5090 /* [dcl.init.aggr]
5091
5092 An aggregate is an array or a class with no user-provided
5093 constructors ... and no virtual functions.
5094
5095 Again, other conditions for being an aggregate are checked
5096 elsewhere. */
5097 CLASSTYPE_NON_AGGREGATE (t)
5098 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5099 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5100 retain the old definition internally for ABI reasons. */
5101 CLASSTYPE_NON_LAYOUT_POD_P (t)
5102 |= (CLASSTYPE_NON_AGGREGATE (t)
5103 || saved_nontrivial_dtor || saved_complex_asn_ref);
5104 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5105 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5106 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5107 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5108
5109 /* If the class has no user-declared constructor, but does have
5110 non-static const or reference data members that can never be
5111 initialized, issue a warning. */
5112 if (warn_uninitialized
5113 /* Classes with user-declared constructors are presumed to
5114 initialize these members. */
5115 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5116 /* Aggregates can be initialized with brace-enclosed
5117 initializers. */
5118 && CLASSTYPE_NON_AGGREGATE (t))
5119 {
5120 tree field;
5121
5122 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5123 {
5124 tree type;
5125
5126 if (TREE_CODE (field) != FIELD_DECL
5127 || DECL_INITIAL (field) != NULL_TREE)
5128 continue;
5129
5130 type = TREE_TYPE (field);
5131 if (TREE_CODE (type) == REFERENCE_TYPE)
5132 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5133 "in class without a constructor", field);
5134 else if (CP_TYPE_CONST_P (type)
5135 && (!CLASS_TYPE_P (type)
5136 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5137 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5138 "in class without a constructor", field);
5139 }
5140 }
5141
5142 /* Synthesize any needed methods. */
5143 add_implicitly_declared_members (t,
5144 cant_have_const_ctor,
5145 no_const_asn_ref);
5146
5147 /* Check defaulted declarations here so we have cant_have_const_ctor
5148 and don't need to worry about clones. */
5149 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5150 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5151 {
5152 int copy = copy_fn_p (fn);
5153 if (copy > 0)
5154 {
5155 bool imp_const_p
5156 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5157 : !no_const_asn_ref);
5158 bool fn_const_p = (copy == 2);
5159
5160 if (fn_const_p && !imp_const_p)
5161 /* If the function is defaulted outside the class, we just
5162 give the synthesis error. */
5163 error ("%q+D declared to take const reference, but implicit "
5164 "declaration would take non-const", fn);
5165 }
5166 defaulted_late_check (fn);
5167 }
5168
5169 if (LAMBDA_TYPE_P (t))
5170 {
5171 /* "The closure type associated with a lambda-expression has a deleted
5172 default constructor and a deleted copy assignment operator." */
5173 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5174 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5175 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5176 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5177
5178 /* "This class type is not an aggregate." */
5179 CLASSTYPE_NON_AGGREGATE (t) = 1;
5180 }
5181
5182 /* Compute the 'literal type' property before we
5183 do anything with non-static member functions. */
5184 finalize_literal_type_property (t);
5185
5186 /* Create the in-charge and not-in-charge variants of constructors
5187 and destructors. */
5188 clone_constructors_and_destructors (t);
5189
5190 /* Process the using-declarations. */
5191 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5192 handle_using_decl (TREE_VALUE (access_decls), t);
5193
5194 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5195 finish_struct_methods (t);
5196
5197 /* Figure out whether or not we will need a cookie when dynamically
5198 allocating an array of this type. */
5199 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5200 = type_requires_array_cookie (t);
5201 }
5202
5203 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5204 accordingly. If a new vfield was created (because T doesn't have a
5205 primary base class), then the newly created field is returned. It
5206 is not added to the TYPE_FIELDS list; it is the caller's
5207 responsibility to do that. Accumulate declared virtual functions
5208 on VIRTUALS_P. */
5209
5210 static tree
5211 create_vtable_ptr (tree t, tree* virtuals_p)
5212 {
5213 tree fn;
5214
5215 /* Collect the virtual functions declared in T. */
5216 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5217 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5218 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5219 {
5220 tree new_virtual = make_node (TREE_LIST);
5221
5222 BV_FN (new_virtual) = fn;
5223 BV_DELTA (new_virtual) = integer_zero_node;
5224 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5225
5226 TREE_CHAIN (new_virtual) = *virtuals_p;
5227 *virtuals_p = new_virtual;
5228 }
5229
5230 /* If we couldn't find an appropriate base class, create a new field
5231 here. Even if there weren't any new virtual functions, we might need a
5232 new virtual function table if we're supposed to include vptrs in
5233 all classes that need them. */
5234 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5235 {
5236 /* We build this decl with vtbl_ptr_type_node, which is a
5237 `vtable_entry_type*'. It might seem more precise to use
5238 `vtable_entry_type (*)[N]' where N is the number of virtual
5239 functions. However, that would require the vtable pointer in
5240 base classes to have a different type than the vtable pointer
5241 in derived classes. We could make that happen, but that
5242 still wouldn't solve all the problems. In particular, the
5243 type-based alias analysis code would decide that assignments
5244 to the base class vtable pointer can't alias assignments to
5245 the derived class vtable pointer, since they have different
5246 types. Thus, in a derived class destructor, where the base
5247 class constructor was inlined, we could generate bad code for
5248 setting up the vtable pointer.
5249
5250 Therefore, we use one type for all vtable pointers. We still
5251 use a type-correct type; it's just doesn't indicate the array
5252 bounds. That's better than using `void*' or some such; it's
5253 cleaner, and it let's the alias analysis code know that these
5254 stores cannot alias stores to void*! */
5255 tree field;
5256
5257 field = build_decl (input_location,
5258 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5259 DECL_VIRTUAL_P (field) = 1;
5260 DECL_ARTIFICIAL (field) = 1;
5261 DECL_FIELD_CONTEXT (field) = t;
5262 DECL_FCONTEXT (field) = t;
5263 if (TYPE_PACKED (t))
5264 DECL_PACKED (field) = 1;
5265
5266 TYPE_VFIELD (t) = field;
5267
5268 /* This class is non-empty. */
5269 CLASSTYPE_EMPTY_P (t) = 0;
5270
5271 return field;
5272 }
5273
5274 return NULL_TREE;
5275 }
5276
5277 /* Add OFFSET to all base types of BINFO which is a base in the
5278 hierarchy dominated by T.
5279
5280 OFFSET, which is a type offset, is number of bytes. */
5281
5282 static void
5283 propagate_binfo_offsets (tree binfo, tree offset)
5284 {
5285 int i;
5286 tree primary_binfo;
5287 tree base_binfo;
5288
5289 /* Update BINFO's offset. */
5290 BINFO_OFFSET (binfo)
5291 = convert (sizetype,
5292 size_binop (PLUS_EXPR,
5293 convert (ssizetype, BINFO_OFFSET (binfo)),
5294 offset));
5295
5296 /* Find the primary base class. */
5297 primary_binfo = get_primary_binfo (binfo);
5298
5299 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5300 propagate_binfo_offsets (primary_binfo, offset);
5301
5302 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5303 downwards. */
5304 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5305 {
5306 /* Don't do the primary base twice. */
5307 if (base_binfo == primary_binfo)
5308 continue;
5309
5310 if (BINFO_VIRTUAL_P (base_binfo))
5311 continue;
5312
5313 propagate_binfo_offsets (base_binfo, offset);
5314 }
5315 }
5316
5317 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5318 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5319 empty subobjects of T. */
5320
5321 static void
5322 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5323 {
5324 tree vbase;
5325 tree t = rli->t;
5326 bool first_vbase = true;
5327 tree *next_field;
5328
5329 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5330 return;
5331
5332 if (!abi_version_at_least(2))
5333 {
5334 /* In G++ 3.2, we incorrectly rounded the size before laying out
5335 the virtual bases. */
5336 finish_record_layout (rli, /*free_p=*/false);
5337 #ifdef STRUCTURE_SIZE_BOUNDARY
5338 /* Packed structures don't need to have minimum size. */
5339 if (! TYPE_PACKED (t))
5340 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5341 #endif
5342 rli->offset = TYPE_SIZE_UNIT (t);
5343 rli->bitpos = bitsize_zero_node;
5344 rli->record_align = TYPE_ALIGN (t);
5345 }
5346
5347 /* Find the last field. The artificial fields created for virtual
5348 bases will go after the last extant field to date. */
5349 next_field = &TYPE_FIELDS (t);
5350 while (*next_field)
5351 next_field = &DECL_CHAIN (*next_field);
5352
5353 /* Go through the virtual bases, allocating space for each virtual
5354 base that is not already a primary base class. These are
5355 allocated in inheritance graph order. */
5356 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5357 {
5358 if (!BINFO_VIRTUAL_P (vbase))
5359 continue;
5360
5361 if (!BINFO_PRIMARY_P (vbase))
5362 {
5363 tree basetype = TREE_TYPE (vbase);
5364
5365 /* This virtual base is not a primary base of any class in the
5366 hierarchy, so we have to add space for it. */
5367 next_field = build_base_field (rli, vbase,
5368 offsets, next_field);
5369
5370 /* If the first virtual base might have been placed at a
5371 lower address, had we started from CLASSTYPE_SIZE, rather
5372 than TYPE_SIZE, issue a warning. There can be both false
5373 positives and false negatives from this warning in rare
5374 cases; to deal with all the possibilities would probably
5375 require performing both layout algorithms and comparing
5376 the results which is not particularly tractable. */
5377 if (warn_abi
5378 && first_vbase
5379 && (tree_int_cst_lt
5380 (size_binop (CEIL_DIV_EXPR,
5381 round_up_loc (input_location,
5382 CLASSTYPE_SIZE (t),
5383 CLASSTYPE_ALIGN (basetype)),
5384 bitsize_unit_node),
5385 BINFO_OFFSET (vbase))))
5386 warning (OPT_Wabi,
5387 "offset of virtual base %qT is not ABI-compliant and "
5388 "may change in a future version of GCC",
5389 basetype);
5390
5391 first_vbase = false;
5392 }
5393 }
5394 }
5395
5396 /* Returns the offset of the byte just past the end of the base class
5397 BINFO. */
5398
5399 static tree
5400 end_of_base (tree binfo)
5401 {
5402 tree size;
5403
5404 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5405 size = TYPE_SIZE_UNIT (char_type_node);
5406 else if (is_empty_class (BINFO_TYPE (binfo)))
5407 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5408 allocate some space for it. It cannot have virtual bases, so
5409 TYPE_SIZE_UNIT is fine. */
5410 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5411 else
5412 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5413
5414 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5415 }
5416
5417 /* Returns the offset of the byte just past the end of the base class
5418 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5419 only non-virtual bases are included. */
5420
5421 static tree
5422 end_of_class (tree t, int include_virtuals_p)
5423 {
5424 tree result = size_zero_node;
5425 VEC(tree,gc) *vbases;
5426 tree binfo;
5427 tree base_binfo;
5428 tree offset;
5429 int i;
5430
5431 for (binfo = TYPE_BINFO (t), i = 0;
5432 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5433 {
5434 if (!include_virtuals_p
5435 && BINFO_VIRTUAL_P (base_binfo)
5436 && (!BINFO_PRIMARY_P (base_binfo)
5437 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5438 continue;
5439
5440 offset = end_of_base (base_binfo);
5441 if (INT_CST_LT_UNSIGNED (result, offset))
5442 result = offset;
5443 }
5444
5445 /* G++ 3.2 did not check indirect virtual bases. */
5446 if (abi_version_at_least (2) && include_virtuals_p)
5447 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5448 VEC_iterate (tree, vbases, i, base_binfo); i++)
5449 {
5450 offset = end_of_base (base_binfo);
5451 if (INT_CST_LT_UNSIGNED (result, offset))
5452 result = offset;
5453 }
5454
5455 return result;
5456 }
5457
5458 /* Warn about bases of T that are inaccessible because they are
5459 ambiguous. For example:
5460
5461 struct S {};
5462 struct T : public S {};
5463 struct U : public S, public T {};
5464
5465 Here, `(S*) new U' is not allowed because there are two `S'
5466 subobjects of U. */
5467
5468 static void
5469 warn_about_ambiguous_bases (tree t)
5470 {
5471 int i;
5472 VEC(tree,gc) *vbases;
5473 tree basetype;
5474 tree binfo;
5475 tree base_binfo;
5476
5477 /* If there are no repeated bases, nothing can be ambiguous. */
5478 if (!CLASSTYPE_REPEATED_BASE_P (t))
5479 return;
5480
5481 /* Check direct bases. */
5482 for (binfo = TYPE_BINFO (t), i = 0;
5483 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5484 {
5485 basetype = BINFO_TYPE (base_binfo);
5486
5487 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5488 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5489 basetype, t);
5490 }
5491
5492 /* Check for ambiguous virtual bases. */
5493 if (extra_warnings)
5494 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5495 VEC_iterate (tree, vbases, i, binfo); i++)
5496 {
5497 basetype = BINFO_TYPE (binfo);
5498
5499 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5500 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
5501 basetype, t);
5502 }
5503 }
5504
5505 /* Compare two INTEGER_CSTs K1 and K2. */
5506
5507 static int
5508 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5509 {
5510 return tree_int_cst_compare ((tree) k1, (tree) k2);
5511 }
5512
5513 /* Increase the size indicated in RLI to account for empty classes
5514 that are "off the end" of the class. */
5515
5516 static void
5517 include_empty_classes (record_layout_info rli)
5518 {
5519 tree eoc;
5520 tree rli_size;
5521
5522 /* It might be the case that we grew the class to allocate a
5523 zero-sized base class. That won't be reflected in RLI, yet,
5524 because we are willing to overlay multiple bases at the same
5525 offset. However, now we need to make sure that RLI is big enough
5526 to reflect the entire class. */
5527 eoc = end_of_class (rli->t,
5528 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5529 rli_size = rli_size_unit_so_far (rli);
5530 if (TREE_CODE (rli_size) == INTEGER_CST
5531 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5532 {
5533 if (!abi_version_at_least (2))
5534 /* In version 1 of the ABI, the size of a class that ends with
5535 a bitfield was not rounded up to a whole multiple of a
5536 byte. Because rli_size_unit_so_far returns only the number
5537 of fully allocated bytes, any extra bits were not included
5538 in the size. */
5539 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5540 else
5541 /* The size should have been rounded to a whole byte. */
5542 gcc_assert (tree_int_cst_equal
5543 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5544 rli->bitpos
5545 = size_binop (PLUS_EXPR,
5546 rli->bitpos,
5547 size_binop (MULT_EXPR,
5548 convert (bitsizetype,
5549 size_binop (MINUS_EXPR,
5550 eoc, rli_size)),
5551 bitsize_int (BITS_PER_UNIT)));
5552 normalize_rli (rli);
5553 }
5554 }
5555
5556 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5557 BINFO_OFFSETs for all of the base-classes. Position the vtable
5558 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5559
5560 static void
5561 layout_class_type (tree t, tree *virtuals_p)
5562 {
5563 tree non_static_data_members;
5564 tree field;
5565 tree vptr;
5566 record_layout_info rli;
5567 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5568 types that appear at that offset. */
5569 splay_tree empty_base_offsets;
5570 /* True if the last field layed out was a bit-field. */
5571 bool last_field_was_bitfield = false;
5572 /* The location at which the next field should be inserted. */
5573 tree *next_field;
5574 /* T, as a base class. */
5575 tree base_t;
5576
5577 /* Keep track of the first non-static data member. */
5578 non_static_data_members = TYPE_FIELDS (t);
5579
5580 /* Start laying out the record. */
5581 rli = start_record_layout (t);
5582
5583 /* Mark all the primary bases in the hierarchy. */
5584 determine_primary_bases (t);
5585
5586 /* Create a pointer to our virtual function table. */
5587 vptr = create_vtable_ptr (t, virtuals_p);
5588
5589 /* The vptr is always the first thing in the class. */
5590 if (vptr)
5591 {
5592 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5593 TYPE_FIELDS (t) = vptr;
5594 next_field = &DECL_CHAIN (vptr);
5595 place_field (rli, vptr);
5596 }
5597 else
5598 next_field = &TYPE_FIELDS (t);
5599
5600 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5601 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5602 NULL, NULL);
5603 build_base_fields (rli, empty_base_offsets, next_field);
5604
5605 /* Layout the non-static data members. */
5606 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5607 {
5608 tree type;
5609 tree padding;
5610
5611 /* We still pass things that aren't non-static data members to
5612 the back end, in case it wants to do something with them. */
5613 if (TREE_CODE (field) != FIELD_DECL)
5614 {
5615 place_field (rli, field);
5616 /* If the static data member has incomplete type, keep track
5617 of it so that it can be completed later. (The handling
5618 of pending statics in finish_record_layout is
5619 insufficient; consider:
5620
5621 struct S1;
5622 struct S2 { static S1 s1; };
5623
5624 At this point, finish_record_layout will be called, but
5625 S1 is still incomplete.) */
5626 if (TREE_CODE (field) == VAR_DECL)
5627 {
5628 maybe_register_incomplete_var (field);
5629 /* The visibility of static data members is determined
5630 at their point of declaration, not their point of
5631 definition. */
5632 determine_visibility (field);
5633 }
5634 continue;
5635 }
5636
5637 type = TREE_TYPE (field);
5638 if (type == error_mark_node)
5639 continue;
5640
5641 padding = NULL_TREE;
5642
5643 /* If this field is a bit-field whose width is greater than its
5644 type, then there are some special rules for allocating
5645 it. */
5646 if (DECL_C_BIT_FIELD (field)
5647 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5648 {
5649 unsigned int itk;
5650 tree integer_type;
5651 bool was_unnamed_p = false;
5652 /* We must allocate the bits as if suitably aligned for the
5653 longest integer type that fits in this many bits. type
5654 of the field. Then, we are supposed to use the left over
5655 bits as additional padding. */
5656 for (itk = itk_char; itk != itk_none; ++itk)
5657 if (integer_types[itk] != NULL_TREE
5658 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5659 TYPE_SIZE (integer_types[itk]))
5660 || INT_CST_LT (DECL_SIZE (field),
5661 TYPE_SIZE (integer_types[itk]))))
5662 break;
5663
5664 /* ITK now indicates a type that is too large for the
5665 field. We have to back up by one to find the largest
5666 type that fits. */
5667 do
5668 {
5669 --itk;
5670 integer_type = integer_types[itk];
5671 } while (itk > 0 && integer_type == NULL_TREE);
5672
5673 /* Figure out how much additional padding is required. GCC
5674 3.2 always created a padding field, even if it had zero
5675 width. */
5676 if (!abi_version_at_least (2)
5677 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5678 {
5679 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5680 /* In a union, the padding field must have the full width
5681 of the bit-field; all fields start at offset zero. */
5682 padding = DECL_SIZE (field);
5683 else
5684 {
5685 if (TREE_CODE (t) == UNION_TYPE)
5686 warning (OPT_Wabi, "size assigned to %qT may not be "
5687 "ABI-compliant and may change in a future "
5688 "version of GCC",
5689 t);
5690 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5691 TYPE_SIZE (integer_type));
5692 }
5693 }
5694 #ifdef PCC_BITFIELD_TYPE_MATTERS
5695 /* An unnamed bitfield does not normally affect the
5696 alignment of the containing class on a target where
5697 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5698 make any exceptions for unnamed bitfields when the
5699 bitfields are longer than their types. Therefore, we
5700 temporarily give the field a name. */
5701 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5702 {
5703 was_unnamed_p = true;
5704 DECL_NAME (field) = make_anon_name ();
5705 }
5706 #endif
5707 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5708 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5709 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5710 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5711 empty_base_offsets);
5712 if (was_unnamed_p)
5713 DECL_NAME (field) = NULL_TREE;
5714 /* Now that layout has been performed, set the size of the
5715 field to the size of its declared type; the rest of the
5716 field is effectively invisible. */
5717 DECL_SIZE (field) = TYPE_SIZE (type);
5718 /* We must also reset the DECL_MODE of the field. */
5719 if (abi_version_at_least (2))
5720 DECL_MODE (field) = TYPE_MODE (type);
5721 else if (warn_abi
5722 && DECL_MODE (field) != TYPE_MODE (type))
5723 /* Versions of G++ before G++ 3.4 did not reset the
5724 DECL_MODE. */
5725 warning (OPT_Wabi,
5726 "the offset of %qD may not be ABI-compliant and may "
5727 "change in a future version of GCC", field);
5728 }
5729 else
5730 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5731 empty_base_offsets);
5732
5733 /* Remember the location of any empty classes in FIELD. */
5734 if (abi_version_at_least (2))
5735 record_subobject_offsets (TREE_TYPE (field),
5736 byte_position(field),
5737 empty_base_offsets,
5738 /*is_data_member=*/true);
5739
5740 /* If a bit-field does not immediately follow another bit-field,
5741 and yet it starts in the middle of a byte, we have failed to
5742 comply with the ABI. */
5743 if (warn_abi
5744 && DECL_C_BIT_FIELD (field)
5745 /* The TREE_NO_WARNING flag gets set by Objective-C when
5746 laying out an Objective-C class. The ObjC ABI differs
5747 from the C++ ABI, and so we do not want a warning
5748 here. */
5749 && !TREE_NO_WARNING (field)
5750 && !last_field_was_bitfield
5751 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5752 DECL_FIELD_BIT_OFFSET (field),
5753 bitsize_unit_node)))
5754 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5755 "change in a future version of GCC", field);
5756
5757 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5758 offset of the field. */
5759 if (warn_abi
5760 && !abi_version_at_least (2)
5761 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5762 byte_position (field))
5763 && contains_empty_class_p (TREE_TYPE (field)))
5764 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5765 "classes to be placed at different locations in a "
5766 "future version of GCC", field);
5767
5768 /* The middle end uses the type of expressions to determine the
5769 possible range of expression values. In order to optimize
5770 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5771 must be made aware of the width of "i", via its type.
5772
5773 Because C++ does not have integer types of arbitrary width,
5774 we must (for the purposes of the front end) convert from the
5775 type assigned here to the declared type of the bitfield
5776 whenever a bitfield expression is used as an rvalue.
5777 Similarly, when assigning a value to a bitfield, the value
5778 must be converted to the type given the bitfield here. */
5779 if (DECL_C_BIT_FIELD (field))
5780 {
5781 unsigned HOST_WIDE_INT width;
5782 tree ftype = TREE_TYPE (field);
5783 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5784 if (width != TYPE_PRECISION (ftype))
5785 {
5786 TREE_TYPE (field)
5787 = c_build_bitfield_integer_type (width,
5788 TYPE_UNSIGNED (ftype));
5789 TREE_TYPE (field)
5790 = cp_build_qualified_type (TREE_TYPE (field),
5791 cp_type_quals (ftype));
5792 }
5793 }
5794
5795 /* If we needed additional padding after this field, add it
5796 now. */
5797 if (padding)
5798 {
5799 tree padding_field;
5800
5801 padding_field = build_decl (input_location,
5802 FIELD_DECL,
5803 NULL_TREE,
5804 char_type_node);
5805 DECL_BIT_FIELD (padding_field) = 1;
5806 DECL_SIZE (padding_field) = padding;
5807 DECL_CONTEXT (padding_field) = t;
5808 DECL_ARTIFICIAL (padding_field) = 1;
5809 DECL_IGNORED_P (padding_field) = 1;
5810 layout_nonempty_base_or_field (rli, padding_field,
5811 NULL_TREE,
5812 empty_base_offsets);
5813 }
5814
5815 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5816 }
5817
5818 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5819 {
5820 /* Make sure that we are on a byte boundary so that the size of
5821 the class without virtual bases will always be a round number
5822 of bytes. */
5823 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5824 normalize_rli (rli);
5825 }
5826
5827 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5828 padding. */
5829 if (!abi_version_at_least (2))
5830 include_empty_classes(rli);
5831
5832 /* Delete all zero-width bit-fields from the list of fields. Now
5833 that the type is laid out they are no longer important. */
5834 remove_zero_width_bit_fields (t);
5835
5836 /* Create the version of T used for virtual bases. We do not use
5837 make_class_type for this version; this is an artificial type. For
5838 a POD type, we just reuse T. */
5839 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5840 {
5841 base_t = make_node (TREE_CODE (t));
5842
5843 /* Set the size and alignment for the new type. In G++ 3.2, all
5844 empty classes were considered to have size zero when used as
5845 base classes. */
5846 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5847 {
5848 TYPE_SIZE (base_t) = bitsize_zero_node;
5849 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5850 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5851 warning (OPT_Wabi,
5852 "layout of classes derived from empty class %qT "
5853 "may change in a future version of GCC",
5854 t);
5855 }
5856 else
5857 {
5858 tree eoc;
5859
5860 /* If the ABI version is not at least two, and the last
5861 field was a bit-field, RLI may not be on a byte
5862 boundary. In particular, rli_size_unit_so_far might
5863 indicate the last complete byte, while rli_size_so_far
5864 indicates the total number of bits used. Therefore,
5865 rli_size_so_far, rather than rli_size_unit_so_far, is
5866 used to compute TYPE_SIZE_UNIT. */
5867 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5868 TYPE_SIZE_UNIT (base_t)
5869 = size_binop (MAX_EXPR,
5870 convert (sizetype,
5871 size_binop (CEIL_DIV_EXPR,
5872 rli_size_so_far (rli),
5873 bitsize_int (BITS_PER_UNIT))),
5874 eoc);
5875 TYPE_SIZE (base_t)
5876 = size_binop (MAX_EXPR,
5877 rli_size_so_far (rli),
5878 size_binop (MULT_EXPR,
5879 convert (bitsizetype, eoc),
5880 bitsize_int (BITS_PER_UNIT)));
5881 }
5882 TYPE_ALIGN (base_t) = rli->record_align;
5883 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5884
5885 /* Copy the fields from T. */
5886 next_field = &TYPE_FIELDS (base_t);
5887 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5888 if (TREE_CODE (field) == FIELD_DECL)
5889 {
5890 *next_field = build_decl (input_location,
5891 FIELD_DECL,
5892 DECL_NAME (field),
5893 TREE_TYPE (field));
5894 DECL_CONTEXT (*next_field) = base_t;
5895 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5896 DECL_FIELD_BIT_OFFSET (*next_field)
5897 = DECL_FIELD_BIT_OFFSET (field);
5898 DECL_SIZE (*next_field) = DECL_SIZE (field);
5899 DECL_MODE (*next_field) = DECL_MODE (field);
5900 next_field = &DECL_CHAIN (*next_field);
5901 }
5902
5903 /* Record the base version of the type. */
5904 CLASSTYPE_AS_BASE (t) = base_t;
5905 TYPE_CONTEXT (base_t) = t;
5906 }
5907 else
5908 CLASSTYPE_AS_BASE (t) = t;
5909
5910 /* Every empty class contains an empty class. */
5911 if (CLASSTYPE_EMPTY_P (t))
5912 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5913
5914 /* Set the TYPE_DECL for this type to contain the right
5915 value for DECL_OFFSET, so that we can use it as part
5916 of a COMPONENT_REF for multiple inheritance. */
5917 layout_decl (TYPE_MAIN_DECL (t), 0);
5918
5919 /* Now fix up any virtual base class types that we left lying
5920 around. We must get these done before we try to lay out the
5921 virtual function table. As a side-effect, this will remove the
5922 base subobject fields. */
5923 layout_virtual_bases (rli, empty_base_offsets);
5924
5925 /* Make sure that empty classes are reflected in RLI at this
5926 point. */
5927 include_empty_classes(rli);
5928
5929 /* Make sure not to create any structures with zero size. */
5930 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5931 place_field (rli,
5932 build_decl (input_location,
5933 FIELD_DECL, NULL_TREE, char_type_node));
5934
5935 /* If this is a non-POD, declaring it packed makes a difference to how it
5936 can be used as a field; don't let finalize_record_size undo it. */
5937 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
5938 rli->packed_maybe_necessary = true;
5939
5940 /* Let the back end lay out the type. */
5941 finish_record_layout (rli, /*free_p=*/true);
5942
5943 /* Warn about bases that can't be talked about due to ambiguity. */
5944 warn_about_ambiguous_bases (t);
5945
5946 /* Now that we're done with layout, give the base fields the real types. */
5947 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5948 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
5949 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
5950
5951 /* Clean up. */
5952 splay_tree_delete (empty_base_offsets);
5953
5954 if (CLASSTYPE_EMPTY_P (t)
5955 && tree_int_cst_lt (sizeof_biggest_empty_class,
5956 TYPE_SIZE_UNIT (t)))
5957 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
5958 }
5959
5960 /* Determine the "key method" for the class type indicated by TYPE,
5961 and set CLASSTYPE_KEY_METHOD accordingly. */
5962
5963 void
5964 determine_key_method (tree type)
5965 {
5966 tree method;
5967
5968 if (TYPE_FOR_JAVA (type)
5969 || processing_template_decl
5970 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5971 || CLASSTYPE_INTERFACE_KNOWN (type))
5972 return;
5973
5974 /* The key method is the first non-pure virtual function that is not
5975 inline at the point of class definition. On some targets the
5976 key function may not be inline; those targets should not call
5977 this function until the end of the translation unit. */
5978 for (method = TYPE_METHODS (type); method != NULL_TREE;
5979 method = DECL_CHAIN (method))
5980 if (DECL_VINDEX (method) != NULL_TREE
5981 && ! DECL_DECLARED_INLINE_P (method)
5982 && ! DECL_PURE_VIRTUAL_P (method))
5983 {
5984 CLASSTYPE_KEY_METHOD (type) = method;
5985 break;
5986 }
5987
5988 return;
5989 }
5990
5991
5992 /* Allocate and return an instance of struct sorted_fields_type with
5993 N fields. */
5994
5995 static struct sorted_fields_type *
5996 sorted_fields_type_new (int n)
5997 {
5998 struct sorted_fields_type *sft;
5999 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6000 + n * sizeof (tree));
6001 sft->len = n;
6002
6003 return sft;
6004 }
6005
6006
6007 /* Perform processing required when the definition of T (a class type)
6008 is complete. */
6009
6010 void
6011 finish_struct_1 (tree t)
6012 {
6013 tree x;
6014 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6015 tree virtuals = NULL_TREE;
6016
6017 if (COMPLETE_TYPE_P (t))
6018 {
6019 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6020 error ("redefinition of %q#T", t);
6021 popclass ();
6022 return;
6023 }
6024
6025 /* If this type was previously laid out as a forward reference,
6026 make sure we lay it out again. */
6027 TYPE_SIZE (t) = NULL_TREE;
6028 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6029
6030 /* Make assumptions about the class; we'll reset the flags if
6031 necessary. */
6032 CLASSTYPE_EMPTY_P (t) = 1;
6033 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6034 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6035 CLASSTYPE_LITERAL_P (t) = true;
6036
6037 /* Do end-of-class semantic processing: checking the validity of the
6038 bases and members and add implicitly generated methods. */
6039 check_bases_and_members (t);
6040
6041 /* Find the key method. */
6042 if (TYPE_CONTAINS_VPTR_P (t))
6043 {
6044 /* The Itanium C++ ABI permits the key method to be chosen when
6045 the class is defined -- even though the key method so
6046 selected may later turn out to be an inline function. On
6047 some systems (such as ARM Symbian OS) the key method cannot
6048 be determined until the end of the translation unit. On such
6049 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6050 will cause the class to be added to KEYED_CLASSES. Then, in
6051 finish_file we will determine the key method. */
6052 if (targetm.cxx.key_method_may_be_inline ())
6053 determine_key_method (t);
6054
6055 /* If a polymorphic class has no key method, we may emit the vtable
6056 in every translation unit where the class definition appears. */
6057 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6058 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6059 }
6060
6061 /* Layout the class itself. */
6062 layout_class_type (t, &virtuals);
6063 if (CLASSTYPE_AS_BASE (t) != t)
6064 /* We use the base type for trivial assignments, and hence it
6065 needs a mode. */
6066 compute_record_mode (CLASSTYPE_AS_BASE (t));
6067
6068 virtuals = modify_all_vtables (t, nreverse (virtuals));
6069
6070 /* If necessary, create the primary vtable for this class. */
6071 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6072 {
6073 /* We must enter these virtuals into the table. */
6074 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6075 build_primary_vtable (NULL_TREE, t);
6076 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6077 /* Here we know enough to change the type of our virtual
6078 function table, but we will wait until later this function. */
6079 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6080 }
6081
6082 if (TYPE_CONTAINS_VPTR_P (t))
6083 {
6084 int vindex;
6085 tree fn;
6086
6087 if (BINFO_VTABLE (TYPE_BINFO (t)))
6088 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6089 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6090 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6091
6092 /* Add entries for virtual functions introduced by this class. */
6093 BINFO_VIRTUALS (TYPE_BINFO (t))
6094 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6095
6096 /* Set DECL_VINDEX for all functions declared in this class. */
6097 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6098 fn;
6099 fn = TREE_CHAIN (fn),
6100 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6101 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6102 {
6103 tree fndecl = BV_FN (fn);
6104
6105 if (DECL_THUNK_P (fndecl))
6106 /* A thunk. We should never be calling this entry directly
6107 from this vtable -- we'd use the entry for the non
6108 thunk base function. */
6109 DECL_VINDEX (fndecl) = NULL_TREE;
6110 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6111 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6112 }
6113 }
6114
6115 finish_struct_bits (t);
6116 set_method_tm_attributes (t);
6117
6118 /* Complete the rtl for any static member objects of the type we're
6119 working on. */
6120 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6121 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6122 && TREE_TYPE (x) != error_mark_node
6123 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6124 DECL_MODE (x) = TYPE_MODE (t);
6125
6126 /* Done with FIELDS...now decide whether to sort these for
6127 faster lookups later.
6128
6129 We use a small number because most searches fail (succeeding
6130 ultimately as the search bores through the inheritance
6131 hierarchy), and we want this failure to occur quickly. */
6132
6133 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6134
6135 /* Complain if one of the field types requires lower visibility. */
6136 constrain_class_visibility (t);
6137
6138 /* Make the rtl for any new vtables we have created, and unmark
6139 the base types we marked. */
6140 finish_vtbls (t);
6141
6142 /* Build the VTT for T. */
6143 build_vtt (t);
6144
6145 /* This warning does not make sense for Java classes, since they
6146 cannot have destructors. */
6147 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6148 {
6149 tree dtor;
6150
6151 dtor = CLASSTYPE_DESTRUCTORS (t);
6152 if (/* An implicitly declared destructor is always public. And,
6153 if it were virtual, we would have created it by now. */
6154 !dtor
6155 || (!DECL_VINDEX (dtor)
6156 && (/* public non-virtual */
6157 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6158 || (/* non-public non-virtual with friends */
6159 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6160 && (CLASSTYPE_FRIEND_CLASSES (t)
6161 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6162 warning (OPT_Wnon_virtual_dtor,
6163 "%q#T has virtual functions and accessible"
6164 " non-virtual destructor", t);
6165 }
6166
6167 complete_vars (t);
6168
6169 if (warn_overloaded_virtual)
6170 warn_hidden (t);
6171
6172 /* Class layout, assignment of virtual table slots, etc., is now
6173 complete. Give the back end a chance to tweak the visibility of
6174 the class or perform any other required target modifications. */
6175 targetm.cxx.adjust_class_at_definition (t);
6176
6177 maybe_suppress_debug_info (t);
6178
6179 dump_class_hierarchy (t);
6180
6181 /* Finish debugging output for this type. */
6182 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6183
6184 if (TYPE_TRANSPARENT_AGGR (t))
6185 {
6186 tree field = first_field (t);
6187 if (field == NULL_TREE || error_operand_p (field))
6188 {
6189 error ("type transparent class %qT does not have any fields", t);
6190 TYPE_TRANSPARENT_AGGR (t) = 0;
6191 }
6192 else if (DECL_ARTIFICIAL (field))
6193 {
6194 if (DECL_FIELD_IS_BASE (field))
6195 error ("type transparent class %qT has base classes", t);
6196 else
6197 {
6198 gcc_checking_assert (DECL_VIRTUAL_P (field));
6199 error ("type transparent class %qT has virtual functions", t);
6200 }
6201 TYPE_TRANSPARENT_AGGR (t) = 0;
6202 }
6203 }
6204 }
6205
6206 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6207 equal to THRESHOLD or greater than THRESHOLD. */
6208
6209 static void
6210 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6211 {
6212 int n_fields = count_fields (fields);
6213 if (n_fields >= threshold)
6214 {
6215 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6216 add_fields_to_record_type (fields, field_vec, 0);
6217 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6218 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6219 }
6220 }
6221
6222 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6223
6224 void
6225 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6226 {
6227 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6228 if (sorted_fields)
6229 {
6230 int i;
6231 int n_fields
6232 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6233 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6234
6235 for (i = 0; i < sorted_fields->len; ++i)
6236 field_vec->elts[i] = sorted_fields->elts[i];
6237
6238 add_enum_fields_to_record_type (enumtype, field_vec,
6239 sorted_fields->len);
6240 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6241 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6242 }
6243 }
6244
6245 /* When T was built up, the member declarations were added in reverse
6246 order. Rearrange them to declaration order. */
6247
6248 void
6249 unreverse_member_declarations (tree t)
6250 {
6251 tree next;
6252 tree prev;
6253 tree x;
6254
6255 /* The following lists are all in reverse order. Put them in
6256 declaration order now. */
6257 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6258 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6259
6260 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6261 reverse order, so we can't just use nreverse. */
6262 prev = NULL_TREE;
6263 for (x = TYPE_FIELDS (t);
6264 x && TREE_CODE (x) != TYPE_DECL;
6265 x = next)
6266 {
6267 next = DECL_CHAIN (x);
6268 DECL_CHAIN (x) = prev;
6269 prev = x;
6270 }
6271 if (prev)
6272 {
6273 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6274 if (prev)
6275 TYPE_FIELDS (t) = prev;
6276 }
6277 }
6278
6279 tree
6280 finish_struct (tree t, tree attributes)
6281 {
6282 location_t saved_loc = input_location;
6283
6284 /* Now that we've got all the field declarations, reverse everything
6285 as necessary. */
6286 unreverse_member_declarations (t);
6287
6288 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6289
6290 /* Nadger the current location so that diagnostics point to the start of
6291 the struct, not the end. */
6292 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6293
6294 if (processing_template_decl)
6295 {
6296 tree x;
6297
6298 finish_struct_methods (t);
6299 TYPE_SIZE (t) = bitsize_zero_node;
6300 TYPE_SIZE_UNIT (t) = size_zero_node;
6301
6302 /* We need to emit an error message if this type was used as a parameter
6303 and it is an abstract type, even if it is a template. We construct
6304 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6305 account and we call complete_vars with this type, which will check
6306 the PARM_DECLS. Note that while the type is being defined,
6307 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6308 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6309 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6310 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6311 if (DECL_PURE_VIRTUAL_P (x))
6312 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
6313 complete_vars (t);
6314 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6315 an enclosing scope is a template class, so that this function be
6316 found by lookup_fnfields_1 when the using declaration is not
6317 instantiated yet. */
6318 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6319 if (TREE_CODE (x) == USING_DECL)
6320 {
6321 tree fn = strip_using_decl (x);
6322 if (is_overloaded_fn (fn))
6323 for (; fn; fn = OVL_NEXT (fn))
6324 add_method (t, OVL_CURRENT (fn), x);
6325 }
6326
6327 /* Remember current #pragma pack value. */
6328 TYPE_PRECISION (t) = maximum_field_alignment;
6329
6330 /* Fix up any variants we've already built. */
6331 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6332 {
6333 TYPE_SIZE (x) = TYPE_SIZE (t);
6334 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6335 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6336 TYPE_METHODS (x) = TYPE_METHODS (t);
6337 }
6338 }
6339 else
6340 finish_struct_1 (t);
6341
6342 input_location = saved_loc;
6343
6344 TYPE_BEING_DEFINED (t) = 0;
6345
6346 if (current_class_type)
6347 popclass ();
6348 else
6349 error ("trying to finish struct, but kicked out due to previous parse errors");
6350
6351 if (processing_template_decl && at_function_scope_p ()
6352 /* Lambdas are defined by the LAMBDA_EXPR. */
6353 && !LAMBDA_TYPE_P (t))
6354 add_stmt (build_min (TAG_DEFN, t));
6355
6356 return t;
6357 }
6358 \f
6359 /* Return the dynamic type of INSTANCE, if known.
6360 Used to determine whether the virtual function table is needed
6361 or not.
6362
6363 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6364 of our knowledge of its type. *NONNULL should be initialized
6365 before this function is called. */
6366
6367 static tree
6368 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6369 {
6370 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6371
6372 switch (TREE_CODE (instance))
6373 {
6374 case INDIRECT_REF:
6375 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6376 return NULL_TREE;
6377 else
6378 return RECUR (TREE_OPERAND (instance, 0));
6379
6380 case CALL_EXPR:
6381 /* This is a call to a constructor, hence it's never zero. */
6382 if (TREE_HAS_CONSTRUCTOR (instance))
6383 {
6384 if (nonnull)
6385 *nonnull = 1;
6386 return TREE_TYPE (instance);
6387 }
6388 return NULL_TREE;
6389
6390 case SAVE_EXPR:
6391 /* This is a call to a constructor, hence it's never zero. */
6392 if (TREE_HAS_CONSTRUCTOR (instance))
6393 {
6394 if (nonnull)
6395 *nonnull = 1;
6396 return TREE_TYPE (instance);
6397 }
6398 return RECUR (TREE_OPERAND (instance, 0));
6399
6400 case POINTER_PLUS_EXPR:
6401 case PLUS_EXPR:
6402 case MINUS_EXPR:
6403 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6404 return RECUR (TREE_OPERAND (instance, 0));
6405 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6406 /* Propagate nonnull. */
6407 return RECUR (TREE_OPERAND (instance, 0));
6408
6409 return NULL_TREE;
6410
6411 CASE_CONVERT:
6412 return RECUR (TREE_OPERAND (instance, 0));
6413
6414 case ADDR_EXPR:
6415 instance = TREE_OPERAND (instance, 0);
6416 if (nonnull)
6417 {
6418 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6419 with a real object -- given &p->f, p can still be null. */
6420 tree t = get_base_address (instance);
6421 /* ??? Probably should check DECL_WEAK here. */
6422 if (t && DECL_P (t))
6423 *nonnull = 1;
6424 }
6425 return RECUR (instance);
6426
6427 case COMPONENT_REF:
6428 /* If this component is really a base class reference, then the field
6429 itself isn't definitive. */
6430 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6431 return RECUR (TREE_OPERAND (instance, 0));
6432 return RECUR (TREE_OPERAND (instance, 1));
6433
6434 case VAR_DECL:
6435 case FIELD_DECL:
6436 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6437 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6438 {
6439 if (nonnull)
6440 *nonnull = 1;
6441 return TREE_TYPE (TREE_TYPE (instance));
6442 }
6443 /* fall through... */
6444 case TARGET_EXPR:
6445 case PARM_DECL:
6446 case RESULT_DECL:
6447 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6448 {
6449 if (nonnull)
6450 *nonnull = 1;
6451 return TREE_TYPE (instance);
6452 }
6453 else if (instance == current_class_ptr)
6454 {
6455 if (nonnull)
6456 *nonnull = 1;
6457
6458 /* if we're in a ctor or dtor, we know our type. If
6459 current_class_ptr is set but we aren't in a function, we're in
6460 an NSDMI (and therefore a constructor). */
6461 if (current_scope () != current_function_decl
6462 || (DECL_LANG_SPECIFIC (current_function_decl)
6463 && (DECL_CONSTRUCTOR_P (current_function_decl)
6464 || DECL_DESTRUCTOR_P (current_function_decl))))
6465 {
6466 if (cdtorp)
6467 *cdtorp = 1;
6468 return TREE_TYPE (TREE_TYPE (instance));
6469 }
6470 }
6471 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6472 {
6473 /* We only need one hash table because it is always left empty. */
6474 static htab_t ht;
6475 if (!ht)
6476 ht = htab_create (37,
6477 htab_hash_pointer,
6478 htab_eq_pointer,
6479 /*htab_del=*/NULL);
6480
6481 /* Reference variables should be references to objects. */
6482 if (nonnull)
6483 *nonnull = 1;
6484
6485 /* Enter the INSTANCE in a table to prevent recursion; a
6486 variable's initializer may refer to the variable
6487 itself. */
6488 if (TREE_CODE (instance) == VAR_DECL
6489 && DECL_INITIAL (instance)
6490 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6491 && !htab_find (ht, instance))
6492 {
6493 tree type;
6494 void **slot;
6495
6496 slot = htab_find_slot (ht, instance, INSERT);
6497 *slot = instance;
6498 type = RECUR (DECL_INITIAL (instance));
6499 htab_remove_elt (ht, instance);
6500
6501 return type;
6502 }
6503 }
6504 return NULL_TREE;
6505
6506 default:
6507 return NULL_TREE;
6508 }
6509 #undef RECUR
6510 }
6511
6512 /* Return nonzero if the dynamic type of INSTANCE is known, and
6513 equivalent to the static type. We also handle the case where
6514 INSTANCE is really a pointer. Return negative if this is a
6515 ctor/dtor. There the dynamic type is known, but this might not be
6516 the most derived base of the original object, and hence virtual
6517 bases may not be layed out according to this type.
6518
6519 Used to determine whether the virtual function table is needed
6520 or not.
6521
6522 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6523 of our knowledge of its type. *NONNULL should be initialized
6524 before this function is called. */
6525
6526 int
6527 resolves_to_fixed_type_p (tree instance, int* nonnull)
6528 {
6529 tree t = TREE_TYPE (instance);
6530 int cdtorp = 0;
6531 tree fixed;
6532
6533 /* processing_template_decl can be false in a template if we're in
6534 fold_non_dependent_expr, but we still want to suppress this check. */
6535 if (in_template_function ())
6536 {
6537 /* In a template we only care about the type of the result. */
6538 if (nonnull)
6539 *nonnull = true;
6540 return true;
6541 }
6542
6543 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6544 if (fixed == NULL_TREE)
6545 return 0;
6546 if (POINTER_TYPE_P (t))
6547 t = TREE_TYPE (t);
6548 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6549 return 0;
6550 return cdtorp ? -1 : 1;
6551 }
6552
6553 \f
6554 void
6555 init_class_processing (void)
6556 {
6557 current_class_depth = 0;
6558 current_class_stack_size = 10;
6559 current_class_stack
6560 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6561 local_classes = VEC_alloc (tree, gc, 8);
6562 sizeof_biggest_empty_class = size_zero_node;
6563
6564 ridpointers[(int) RID_PUBLIC] = access_public_node;
6565 ridpointers[(int) RID_PRIVATE] = access_private_node;
6566 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6567 }
6568
6569 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6570
6571 static void
6572 restore_class_cache (void)
6573 {
6574 tree type;
6575
6576 /* We are re-entering the same class we just left, so we don't
6577 have to search the whole inheritance matrix to find all the
6578 decls to bind again. Instead, we install the cached
6579 class_shadowed list and walk through it binding names. */
6580 push_binding_level (previous_class_level);
6581 class_binding_level = previous_class_level;
6582 /* Restore IDENTIFIER_TYPE_VALUE. */
6583 for (type = class_binding_level->type_shadowed;
6584 type;
6585 type = TREE_CHAIN (type))
6586 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6587 }
6588
6589 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6590 appropriate for TYPE.
6591
6592 So that we may avoid calls to lookup_name, we cache the _TYPE
6593 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6594
6595 For multiple inheritance, we perform a two-pass depth-first search
6596 of the type lattice. */
6597
6598 void
6599 pushclass (tree type)
6600 {
6601 class_stack_node_t csn;
6602
6603 type = TYPE_MAIN_VARIANT (type);
6604
6605 /* Make sure there is enough room for the new entry on the stack. */
6606 if (current_class_depth + 1 >= current_class_stack_size)
6607 {
6608 current_class_stack_size *= 2;
6609 current_class_stack
6610 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6611 current_class_stack_size);
6612 }
6613
6614 /* Insert a new entry on the class stack. */
6615 csn = current_class_stack + current_class_depth;
6616 csn->name = current_class_name;
6617 csn->type = current_class_type;
6618 csn->access = current_access_specifier;
6619 csn->names_used = 0;
6620 csn->hidden = 0;
6621 current_class_depth++;
6622
6623 /* Now set up the new type. */
6624 current_class_name = TYPE_NAME (type);
6625 if (TREE_CODE (current_class_name) == TYPE_DECL)
6626 current_class_name = DECL_NAME (current_class_name);
6627 current_class_type = type;
6628
6629 /* By default, things in classes are private, while things in
6630 structures or unions are public. */
6631 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6632 ? access_private_node
6633 : access_public_node);
6634
6635 if (previous_class_level
6636 && type != previous_class_level->this_entity
6637 && current_class_depth == 1)
6638 {
6639 /* Forcibly remove any old class remnants. */
6640 invalidate_class_lookup_cache ();
6641 }
6642
6643 if (!previous_class_level
6644 || type != previous_class_level->this_entity
6645 || current_class_depth > 1)
6646 pushlevel_class ();
6647 else
6648 restore_class_cache ();
6649 }
6650
6651 /* When we exit a toplevel class scope, we save its binding level so
6652 that we can restore it quickly. Here, we've entered some other
6653 class, so we must invalidate our cache. */
6654
6655 void
6656 invalidate_class_lookup_cache (void)
6657 {
6658 previous_class_level = NULL;
6659 }
6660
6661 /* Get out of the current class scope. If we were in a class scope
6662 previously, that is the one popped to. */
6663
6664 void
6665 popclass (void)
6666 {
6667 poplevel_class ();
6668
6669 current_class_depth--;
6670 current_class_name = current_class_stack[current_class_depth].name;
6671 current_class_type = current_class_stack[current_class_depth].type;
6672 current_access_specifier = current_class_stack[current_class_depth].access;
6673 if (current_class_stack[current_class_depth].names_used)
6674 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6675 }
6676
6677 /* Mark the top of the class stack as hidden. */
6678
6679 void
6680 push_class_stack (void)
6681 {
6682 if (current_class_depth)
6683 ++current_class_stack[current_class_depth - 1].hidden;
6684 }
6685
6686 /* Mark the top of the class stack as un-hidden. */
6687
6688 void
6689 pop_class_stack (void)
6690 {
6691 if (current_class_depth)
6692 --current_class_stack[current_class_depth - 1].hidden;
6693 }
6694
6695 /* Returns 1 if the class type currently being defined is either T or
6696 a nested type of T. */
6697
6698 bool
6699 currently_open_class (tree t)
6700 {
6701 int i;
6702
6703 if (!CLASS_TYPE_P (t))
6704 return false;
6705
6706 t = TYPE_MAIN_VARIANT (t);
6707
6708 /* We start looking from 1 because entry 0 is from global scope,
6709 and has no type. */
6710 for (i = current_class_depth; i > 0; --i)
6711 {
6712 tree c;
6713 if (i == current_class_depth)
6714 c = current_class_type;
6715 else
6716 {
6717 if (current_class_stack[i].hidden)
6718 break;
6719 c = current_class_stack[i].type;
6720 }
6721 if (!c)
6722 continue;
6723 if (same_type_p (c, t))
6724 return true;
6725 }
6726 return false;
6727 }
6728
6729 /* If either current_class_type or one of its enclosing classes are derived
6730 from T, return the appropriate type. Used to determine how we found
6731 something via unqualified lookup. */
6732
6733 tree
6734 currently_open_derived_class (tree t)
6735 {
6736 int i;
6737
6738 /* The bases of a dependent type are unknown. */
6739 if (dependent_type_p (t))
6740 return NULL_TREE;
6741
6742 if (!current_class_type)
6743 return NULL_TREE;
6744
6745 if (DERIVED_FROM_P (t, current_class_type))
6746 return current_class_type;
6747
6748 for (i = current_class_depth - 1; i > 0; --i)
6749 {
6750 if (current_class_stack[i].hidden)
6751 break;
6752 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6753 return current_class_stack[i].type;
6754 }
6755
6756 return NULL_TREE;
6757 }
6758
6759 /* Returns the innermost class type which is not a lambda closure type. */
6760
6761 tree
6762 current_nonlambda_class_type (void)
6763 {
6764 int i;
6765
6766 /* We start looking from 1 because entry 0 is from global scope,
6767 and has no type. */
6768 for (i = current_class_depth; i > 0; --i)
6769 {
6770 tree c;
6771 if (i == current_class_depth)
6772 c = current_class_type;
6773 else
6774 {
6775 if (current_class_stack[i].hidden)
6776 break;
6777 c = current_class_stack[i].type;
6778 }
6779 if (!c)
6780 continue;
6781 if (!LAMBDA_TYPE_P (c))
6782 return c;
6783 }
6784 return NULL_TREE;
6785 }
6786
6787 /* When entering a class scope, all enclosing class scopes' names with
6788 static meaning (static variables, static functions, types and
6789 enumerators) have to be visible. This recursive function calls
6790 pushclass for all enclosing class contexts until global or a local
6791 scope is reached. TYPE is the enclosed class. */
6792
6793 void
6794 push_nested_class (tree type)
6795 {
6796 /* A namespace might be passed in error cases, like A::B:C. */
6797 if (type == NULL_TREE
6798 || !CLASS_TYPE_P (type))
6799 return;
6800
6801 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6802
6803 pushclass (type);
6804 }
6805
6806 /* Undoes a push_nested_class call. */
6807
6808 void
6809 pop_nested_class (void)
6810 {
6811 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6812
6813 popclass ();
6814 if (context && CLASS_TYPE_P (context))
6815 pop_nested_class ();
6816 }
6817
6818 /* Returns the number of extern "LANG" blocks we are nested within. */
6819
6820 int
6821 current_lang_depth (void)
6822 {
6823 return VEC_length (tree, current_lang_base);
6824 }
6825
6826 /* Set global variables CURRENT_LANG_NAME to appropriate value
6827 so that behavior of name-mangling machinery is correct. */
6828
6829 void
6830 push_lang_context (tree name)
6831 {
6832 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6833
6834 if (name == lang_name_cplusplus)
6835 {
6836 current_lang_name = name;
6837 }
6838 else if (name == lang_name_java)
6839 {
6840 current_lang_name = name;
6841 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6842 (See record_builtin_java_type in decl.c.) However, that causes
6843 incorrect debug entries if these types are actually used.
6844 So we re-enable debug output after extern "Java". */
6845 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6846 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6847 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6848 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6849 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6850 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6851 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6852 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6853 }
6854 else if (name == lang_name_c)
6855 {
6856 current_lang_name = name;
6857 }
6858 else
6859 error ("language string %<\"%E\"%> not recognized", name);
6860 }
6861
6862 /* Get out of the current language scope. */
6863
6864 void
6865 pop_lang_context (void)
6866 {
6867 current_lang_name = VEC_pop (tree, current_lang_base);
6868 }
6869 \f
6870 /* Type instantiation routines. */
6871
6872 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6873 matches the TARGET_TYPE. If there is no satisfactory match, return
6874 error_mark_node, and issue an error & warning messages under
6875 control of FLAGS. Permit pointers to member function if FLAGS
6876 permits. If TEMPLATE_ONLY, the name of the overloaded function was
6877 a template-id, and EXPLICIT_TARGS are the explicitly provided
6878 template arguments.
6879
6880 If OVERLOAD is for one or more member functions, then ACCESS_PATH
6881 is the base path used to reference those member functions. If
6882 TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is
6883 resolved to a member function, access checks will be performed and
6884 errors issued if appropriate. */
6885
6886 static tree
6887 resolve_address_of_overloaded_function (tree target_type,
6888 tree overload,
6889 tsubst_flags_t flags,
6890 bool template_only,
6891 tree explicit_targs,
6892 tree access_path)
6893 {
6894 /* Here's what the standard says:
6895
6896 [over.over]
6897
6898 If the name is a function template, template argument deduction
6899 is done, and if the argument deduction succeeds, the deduced
6900 arguments are used to generate a single template function, which
6901 is added to the set of overloaded functions considered.
6902
6903 Non-member functions and static member functions match targets of
6904 type "pointer-to-function" or "reference-to-function." Nonstatic
6905 member functions match targets of type "pointer-to-member
6906 function;" the function type of the pointer to member is used to
6907 select the member function from the set of overloaded member
6908 functions. If a nonstatic member function is selected, the
6909 reference to the overloaded function name is required to have the
6910 form of a pointer to member as described in 5.3.1.
6911
6912 If more than one function is selected, any template functions in
6913 the set are eliminated if the set also contains a non-template
6914 function, and any given template function is eliminated if the
6915 set contains a second template function that is more specialized
6916 than the first according to the partial ordering rules 14.5.5.2.
6917 After such eliminations, if any, there shall remain exactly one
6918 selected function. */
6919
6920 int is_ptrmem = 0;
6921 /* We store the matches in a TREE_LIST rooted here. The functions
6922 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6923 interoperability with most_specialized_instantiation. */
6924 tree matches = NULL_TREE;
6925 tree fn;
6926 tree target_fn_type;
6927
6928 /* By the time we get here, we should be seeing only real
6929 pointer-to-member types, not the internal POINTER_TYPE to
6930 METHOD_TYPE representation. */
6931 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
6932 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
6933
6934 gcc_assert (is_overloaded_fn (overload));
6935
6936 /* Check that the TARGET_TYPE is reasonable. */
6937 if (TYPE_PTRFN_P (target_type))
6938 /* This is OK. */;
6939 else if (TYPE_PTRMEMFUNC_P (target_type))
6940 /* This is OK, too. */
6941 is_ptrmem = 1;
6942 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6943 /* This is OK, too. This comes from a conversion to reference
6944 type. */
6945 target_type = build_reference_type (target_type);
6946 else
6947 {
6948 if (flags & tf_error)
6949 error ("cannot resolve overloaded function %qD based on"
6950 " conversion to type %qT",
6951 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6952 return error_mark_node;
6953 }
6954
6955 /* Non-member functions and static member functions match targets of type
6956 "pointer-to-function" or "reference-to-function." Nonstatic member
6957 functions match targets of type "pointer-to-member-function;" the
6958 function type of the pointer to member is used to select the member
6959 function from the set of overloaded member functions.
6960
6961 So figure out the FUNCTION_TYPE that we want to match against. */
6962 target_fn_type = static_fn_type (target_type);
6963
6964 /* If we can find a non-template function that matches, we can just
6965 use it. There's no point in generating template instantiations
6966 if we're just going to throw them out anyhow. But, of course, we
6967 can only do this when we don't *need* a template function. */
6968 if (!template_only)
6969 {
6970 tree fns;
6971
6972 for (fns = overload; fns; fns = OVL_NEXT (fns))
6973 {
6974 tree fn = OVL_CURRENT (fns);
6975
6976 if (TREE_CODE (fn) == TEMPLATE_DECL)
6977 /* We're not looking for templates just yet. */
6978 continue;
6979
6980 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6981 != is_ptrmem)
6982 /* We're looking for a non-static member, and this isn't
6983 one, or vice versa. */
6984 continue;
6985
6986 /* Ignore functions which haven't been explicitly
6987 declared. */
6988 if (DECL_ANTICIPATED (fn))
6989 continue;
6990
6991 /* See if there's a match. */
6992 if (same_type_p (target_fn_type, static_fn_type (fn)))
6993 matches = tree_cons (fn, NULL_TREE, matches);
6994 }
6995 }
6996
6997 /* Now, if we've already got a match (or matches), there's no need
6998 to proceed to the template functions. But, if we don't have a
6999 match we need to look at them, too. */
7000 if (!matches)
7001 {
7002 tree target_arg_types;
7003 tree target_ret_type;
7004 tree fns;
7005 tree *args;
7006 unsigned int nargs, ia;
7007 tree arg;
7008
7009 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7010 target_ret_type = TREE_TYPE (target_fn_type);
7011
7012 nargs = list_length (target_arg_types);
7013 args = XALLOCAVEC (tree, nargs);
7014 for (arg = target_arg_types, ia = 0;
7015 arg != NULL_TREE && arg != void_list_node;
7016 arg = TREE_CHAIN (arg), ++ia)
7017 args[ia] = TREE_VALUE (arg);
7018 nargs = ia;
7019
7020 for (fns = overload; fns; fns = OVL_NEXT (fns))
7021 {
7022 tree fn = OVL_CURRENT (fns);
7023 tree instantiation;
7024 tree targs;
7025
7026 if (TREE_CODE (fn) != TEMPLATE_DECL)
7027 /* We're only looking for templates. */
7028 continue;
7029
7030 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7031 != is_ptrmem)
7032 /* We're not looking for a non-static member, and this is
7033 one, or vice versa. */
7034 continue;
7035
7036 /* Try to do argument deduction. */
7037 targs = make_tree_vec (DECL_NTPARMS (fn));
7038 if (fn_type_unification (fn, explicit_targs, targs, args, nargs,
7039 target_ret_type, DEDUCE_EXACT,
7040 LOOKUP_NORMAL, false))
7041 /* Argument deduction failed. */
7042 continue;
7043
7044 /* Instantiate the template. */
7045 instantiation = instantiate_template (fn, targs, flags);
7046 if (instantiation == error_mark_node)
7047 /* Instantiation failed. */
7048 continue;
7049
7050 /* See if there's a match. */
7051 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7052 matches = tree_cons (instantiation, fn, matches);
7053 }
7054
7055 /* Now, remove all but the most specialized of the matches. */
7056 if (matches)
7057 {
7058 tree match = most_specialized_instantiation (matches);
7059
7060 if (match != error_mark_node)
7061 matches = tree_cons (TREE_PURPOSE (match),
7062 NULL_TREE,
7063 NULL_TREE);
7064 }
7065 }
7066
7067 /* Now we should have exactly one function in MATCHES. */
7068 if (matches == NULL_TREE)
7069 {
7070 /* There were *no* matches. */
7071 if (flags & tf_error)
7072 {
7073 error ("no matches converting function %qD to type %q#T",
7074 DECL_NAME (OVL_CURRENT (overload)),
7075 target_type);
7076
7077 print_candidates (overload);
7078 }
7079 return error_mark_node;
7080 }
7081 else if (TREE_CHAIN (matches))
7082 {
7083 /* There were too many matches. First check if they're all
7084 the same function. */
7085 tree match;
7086
7087 fn = TREE_PURPOSE (matches);
7088 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7089 if (!decls_match (fn, TREE_PURPOSE (match)))
7090 break;
7091
7092 if (match)
7093 {
7094 if (flags & tf_error)
7095 {
7096 error ("converting overloaded function %qD to type %q#T is ambiguous",
7097 DECL_NAME (OVL_FUNCTION (overload)),
7098 target_type);
7099
7100 /* Since print_candidates expects the functions in the
7101 TREE_VALUE slot, we flip them here. */
7102 for (match = matches; match; match = TREE_CHAIN (match))
7103 TREE_VALUE (match) = TREE_PURPOSE (match);
7104
7105 print_candidates (matches);
7106 }
7107
7108 return error_mark_node;
7109 }
7110 }
7111
7112 /* Good, exactly one match. Now, convert it to the correct type. */
7113 fn = TREE_PURPOSE (matches);
7114
7115 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7116 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7117 {
7118 static int explained;
7119
7120 if (!(flags & tf_error))
7121 return error_mark_node;
7122
7123 permerror (input_location, "assuming pointer to member %qD", fn);
7124 if (!explained)
7125 {
7126 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7127 explained = 1;
7128 }
7129 }
7130
7131 /* If we're doing overload resolution purely for the purpose of
7132 determining conversion sequences, we should not consider the
7133 function used. If this conversion sequence is selected, the
7134 function will be marked as used at this point. */
7135 if (!(flags & tf_conv))
7136 {
7137 /* Make =delete work with SFINAE. */
7138 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7139 return error_mark_node;
7140
7141 mark_used (fn);
7142 }
7143
7144 /* We could not check access to member functions when this
7145 expression was originally created since we did not know at that
7146 time to which function the expression referred. */
7147 if (!(flags & tf_no_access_control)
7148 && DECL_FUNCTION_MEMBER_P (fn))
7149 {
7150 gcc_assert (access_path);
7151 perform_or_defer_access_check (access_path, fn, fn,
7152 tf_warning_or_error);
7153 }
7154
7155 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7156 return cp_build_addr_expr (fn, flags);
7157 else
7158 {
7159 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7160 will mark the function as addressed, but here we must do it
7161 explicitly. */
7162 cxx_mark_addressable (fn);
7163
7164 return fn;
7165 }
7166 }
7167
7168 /* This function will instantiate the type of the expression given in
7169 RHS to match the type of LHSTYPE. If errors exist, then return
7170 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7171 we complain on errors. If we are not complaining, never modify rhs,
7172 as overload resolution wants to try many possible instantiations, in
7173 the hope that at least one will work.
7174
7175 For non-recursive calls, LHSTYPE should be a function, pointer to
7176 function, or a pointer to member function. */
7177
7178 tree
7179 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7180 {
7181 tsubst_flags_t flags_in = flags;
7182 tree access_path = NULL_TREE;
7183
7184 flags &= ~tf_ptrmem_ok;
7185
7186 if (lhstype == unknown_type_node)
7187 {
7188 if (flags & tf_error)
7189 error ("not enough type information");
7190 return error_mark_node;
7191 }
7192
7193 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7194 {
7195 if (same_type_p (lhstype, TREE_TYPE (rhs)))
7196 return rhs;
7197 if (flag_ms_extensions
7198 && TYPE_PTRMEMFUNC_P (lhstype)
7199 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7200 /* Microsoft allows `A::f' to be resolved to a
7201 pointer-to-member. */
7202 ;
7203 else
7204 {
7205 if (flags & tf_error)
7206 error ("cannot convert %qE from type %qT to type %qT",
7207 rhs, TREE_TYPE (rhs), lhstype);
7208 return error_mark_node;
7209 }
7210 }
7211
7212 if (BASELINK_P (rhs))
7213 {
7214 access_path = BASELINK_ACCESS_BINFO (rhs);
7215 rhs = BASELINK_FUNCTIONS (rhs);
7216 }
7217
7218 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7219 deduce any type information. */
7220 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7221 {
7222 if (flags & tf_error)
7223 error ("not enough type information");
7224 return error_mark_node;
7225 }
7226
7227 /* There only a few kinds of expressions that may have a type
7228 dependent on overload resolution. */
7229 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7230 || TREE_CODE (rhs) == COMPONENT_REF
7231 || really_overloaded_fn (rhs)
7232 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7233
7234 /* This should really only be used when attempting to distinguish
7235 what sort of a pointer to function we have. For now, any
7236 arithmetic operation which is not supported on pointers
7237 is rejected as an error. */
7238
7239 switch (TREE_CODE (rhs))
7240 {
7241 case COMPONENT_REF:
7242 {
7243 tree member = TREE_OPERAND (rhs, 1);
7244
7245 member = instantiate_type (lhstype, member, flags);
7246 if (member != error_mark_node
7247 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7248 /* Do not lose object's side effects. */
7249 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7250 TREE_OPERAND (rhs, 0), member);
7251 return member;
7252 }
7253
7254 case OFFSET_REF:
7255 rhs = TREE_OPERAND (rhs, 1);
7256 if (BASELINK_P (rhs))
7257 return instantiate_type (lhstype, rhs, flags_in);
7258
7259 /* This can happen if we are forming a pointer-to-member for a
7260 member template. */
7261 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7262
7263 /* Fall through. */
7264
7265 case TEMPLATE_ID_EXPR:
7266 {
7267 tree fns = TREE_OPERAND (rhs, 0);
7268 tree args = TREE_OPERAND (rhs, 1);
7269
7270 return
7271 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7272 /*template_only=*/true,
7273 args, access_path);
7274 }
7275
7276 case OVERLOAD:
7277 case FUNCTION_DECL:
7278 return
7279 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7280 /*template_only=*/false,
7281 /*explicit_targs=*/NULL_TREE,
7282 access_path);
7283
7284 case ADDR_EXPR:
7285 {
7286 if (PTRMEM_OK_P (rhs))
7287 flags |= tf_ptrmem_ok;
7288
7289 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7290 }
7291
7292 case ERROR_MARK:
7293 return error_mark_node;
7294
7295 default:
7296 gcc_unreachable ();
7297 }
7298 return error_mark_node;
7299 }
7300 \f
7301 /* Return the name of the virtual function pointer field
7302 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7303 this may have to look back through base types to find the
7304 ultimate field name. (For single inheritance, these could
7305 all be the same name. Who knows for multiple inheritance). */
7306
7307 static tree
7308 get_vfield_name (tree type)
7309 {
7310 tree binfo, base_binfo;
7311 char *buf;
7312
7313 for (binfo = TYPE_BINFO (type);
7314 BINFO_N_BASE_BINFOS (binfo);
7315 binfo = base_binfo)
7316 {
7317 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7318
7319 if (BINFO_VIRTUAL_P (base_binfo)
7320 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7321 break;
7322 }
7323
7324 type = BINFO_TYPE (binfo);
7325 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7326 + TYPE_NAME_LENGTH (type) + 2);
7327 sprintf (buf, VFIELD_NAME_FORMAT,
7328 IDENTIFIER_POINTER (constructor_name (type)));
7329 return get_identifier (buf);
7330 }
7331
7332 void
7333 print_class_statistics (void)
7334 {
7335 #ifdef GATHER_STATISTICS
7336 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7337 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7338 if (n_vtables)
7339 {
7340 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7341 n_vtables, n_vtable_searches);
7342 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7343 n_vtable_entries, n_vtable_elems);
7344 }
7345 #endif
7346 }
7347
7348 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7349 according to [class]:
7350 The class-name is also inserted
7351 into the scope of the class itself. For purposes of access checking,
7352 the inserted class name is treated as if it were a public member name. */
7353
7354 void
7355 build_self_reference (void)
7356 {
7357 tree name = constructor_name (current_class_type);
7358 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7359 tree saved_cas;
7360
7361 DECL_NONLOCAL (value) = 1;
7362 DECL_CONTEXT (value) = current_class_type;
7363 DECL_ARTIFICIAL (value) = 1;
7364 SET_DECL_SELF_REFERENCE_P (value);
7365 set_underlying_type (value);
7366
7367 if (processing_template_decl)
7368 value = push_template_decl (value);
7369
7370 saved_cas = current_access_specifier;
7371 current_access_specifier = access_public_node;
7372 finish_member_declaration (value);
7373 current_access_specifier = saved_cas;
7374 }
7375
7376 /* Returns 1 if TYPE contains only padding bytes. */
7377
7378 int
7379 is_empty_class (tree type)
7380 {
7381 if (type == error_mark_node)
7382 return 0;
7383
7384 if (! CLASS_TYPE_P (type))
7385 return 0;
7386
7387 /* In G++ 3.2, whether or not a class was empty was determined by
7388 looking at its size. */
7389 if (abi_version_at_least (2))
7390 return CLASSTYPE_EMPTY_P (type);
7391 else
7392 return integer_zerop (CLASSTYPE_SIZE (type));
7393 }
7394
7395 /* Returns true if TYPE contains an empty class. */
7396
7397 static bool
7398 contains_empty_class_p (tree type)
7399 {
7400 if (is_empty_class (type))
7401 return true;
7402 if (CLASS_TYPE_P (type))
7403 {
7404 tree field;
7405 tree binfo;
7406 tree base_binfo;
7407 int i;
7408
7409 for (binfo = TYPE_BINFO (type), i = 0;
7410 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7411 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7412 return true;
7413 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7414 if (TREE_CODE (field) == FIELD_DECL
7415 && !DECL_ARTIFICIAL (field)
7416 && is_empty_class (TREE_TYPE (field)))
7417 return true;
7418 }
7419 else if (TREE_CODE (type) == ARRAY_TYPE)
7420 return contains_empty_class_p (TREE_TYPE (type));
7421 return false;
7422 }
7423
7424 /* Returns true if TYPE contains no actual data, just various
7425 possible combinations of empty classes and possibly a vptr. */
7426
7427 bool
7428 is_really_empty_class (tree type)
7429 {
7430 if (CLASS_TYPE_P (type))
7431 {
7432 tree field;
7433 tree binfo;
7434 tree base_binfo;
7435 int i;
7436
7437 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7438 out, but we'd like to be able to check this before then. */
7439 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7440 return true;
7441
7442 for (binfo = TYPE_BINFO (type), i = 0;
7443 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7444 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7445 return false;
7446 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7447 if (TREE_CODE (field) == FIELD_DECL
7448 && !DECL_ARTIFICIAL (field)
7449 && !is_really_empty_class (TREE_TYPE (field)))
7450 return false;
7451 return true;
7452 }
7453 else if (TREE_CODE (type) == ARRAY_TYPE)
7454 return is_really_empty_class (TREE_TYPE (type));
7455 return false;
7456 }
7457
7458 /* Note that NAME was looked up while the current class was being
7459 defined and that the result of that lookup was DECL. */
7460
7461 void
7462 maybe_note_name_used_in_class (tree name, tree decl)
7463 {
7464 splay_tree names_used;
7465
7466 /* If we're not defining a class, there's nothing to do. */
7467 if (!(innermost_scope_kind() == sk_class
7468 && TYPE_BEING_DEFINED (current_class_type)
7469 && !LAMBDA_TYPE_P (current_class_type)))
7470 return;
7471
7472 /* If there's already a binding for this NAME, then we don't have
7473 anything to worry about. */
7474 if (lookup_member (current_class_type, name,
7475 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7476 return;
7477
7478 if (!current_class_stack[current_class_depth - 1].names_used)
7479 current_class_stack[current_class_depth - 1].names_used
7480 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7481 names_used = current_class_stack[current_class_depth - 1].names_used;
7482
7483 splay_tree_insert (names_used,
7484 (splay_tree_key) name,
7485 (splay_tree_value) decl);
7486 }
7487
7488 /* Note that NAME was declared (as DECL) in the current class. Check
7489 to see that the declaration is valid. */
7490
7491 void
7492 note_name_declared_in_class (tree name, tree decl)
7493 {
7494 splay_tree names_used;
7495 splay_tree_node n;
7496
7497 /* Look to see if we ever used this name. */
7498 names_used
7499 = current_class_stack[current_class_depth - 1].names_used;
7500 if (!names_used)
7501 return;
7502 /* The C language allows members to be declared with a type of the same
7503 name, and the C++ standard says this diagnostic is not required. So
7504 allow it in extern "C" blocks unless predantic is specified.
7505 Allow it in all cases if -ms-extensions is specified. */
7506 if ((!pedantic && current_lang_name == lang_name_c)
7507 || flag_ms_extensions)
7508 return;
7509 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7510 if (n)
7511 {
7512 /* [basic.scope.class]
7513
7514 A name N used in a class S shall refer to the same declaration
7515 in its context and when re-evaluated in the completed scope of
7516 S. */
7517 permerror (input_location, "declaration of %q#D", decl);
7518 permerror (input_location, "changes meaning of %qD from %q+#D",
7519 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7520 }
7521 }
7522
7523 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7524 Secondary vtables are merged with primary vtables; this function
7525 will return the VAR_DECL for the primary vtable. */
7526
7527 tree
7528 get_vtbl_decl_for_binfo (tree binfo)
7529 {
7530 tree decl;
7531
7532 decl = BINFO_VTABLE (binfo);
7533 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7534 {
7535 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7536 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7537 }
7538 if (decl)
7539 gcc_assert (TREE_CODE (decl) == VAR_DECL);
7540 return decl;
7541 }
7542
7543
7544 /* Returns the binfo for the primary base of BINFO. If the resulting
7545 BINFO is a virtual base, and it is inherited elsewhere in the
7546 hierarchy, then the returned binfo might not be the primary base of
7547 BINFO in the complete object. Check BINFO_PRIMARY_P or
7548 BINFO_LOST_PRIMARY_P to be sure. */
7549
7550 static tree
7551 get_primary_binfo (tree binfo)
7552 {
7553 tree primary_base;
7554
7555 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7556 if (!primary_base)
7557 return NULL_TREE;
7558
7559 return copied_binfo (primary_base, binfo);
7560 }
7561
7562 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7563
7564 static int
7565 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7566 {
7567 if (!indented_p)
7568 fprintf (stream, "%*s", indent, "");
7569 return 1;
7570 }
7571
7572 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7573 INDENT should be zero when called from the top level; it is
7574 incremented recursively. IGO indicates the next expected BINFO in
7575 inheritance graph ordering. */
7576
7577 static tree
7578 dump_class_hierarchy_r (FILE *stream,
7579 int flags,
7580 tree binfo,
7581 tree igo,
7582 int indent)
7583 {
7584 int indented = 0;
7585 tree base_binfo;
7586 int i;
7587
7588 indented = maybe_indent_hierarchy (stream, indent, 0);
7589 fprintf (stream, "%s (0x%lx) ",
7590 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7591 (unsigned long) binfo);
7592 if (binfo != igo)
7593 {
7594 fprintf (stream, "alternative-path\n");
7595 return igo;
7596 }
7597 igo = TREE_CHAIN (binfo);
7598
7599 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7600 tree_low_cst (BINFO_OFFSET (binfo), 0));
7601 if (is_empty_class (BINFO_TYPE (binfo)))
7602 fprintf (stream, " empty");
7603 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7604 fprintf (stream, " nearly-empty");
7605 if (BINFO_VIRTUAL_P (binfo))
7606 fprintf (stream, " virtual");
7607 fprintf (stream, "\n");
7608
7609 indented = 0;
7610 if (BINFO_PRIMARY_P (binfo))
7611 {
7612 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7613 fprintf (stream, " primary-for %s (0x%lx)",
7614 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7615 TFF_PLAIN_IDENTIFIER),
7616 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7617 }
7618 if (BINFO_LOST_PRIMARY_P (binfo))
7619 {
7620 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7621 fprintf (stream, " lost-primary");
7622 }
7623 if (indented)
7624 fprintf (stream, "\n");
7625
7626 if (!(flags & TDF_SLIM))
7627 {
7628 int indented = 0;
7629
7630 if (BINFO_SUBVTT_INDEX (binfo))
7631 {
7632 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7633 fprintf (stream, " subvttidx=%s",
7634 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7635 TFF_PLAIN_IDENTIFIER));
7636 }
7637 if (BINFO_VPTR_INDEX (binfo))
7638 {
7639 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7640 fprintf (stream, " vptridx=%s",
7641 expr_as_string (BINFO_VPTR_INDEX (binfo),
7642 TFF_PLAIN_IDENTIFIER));
7643 }
7644 if (BINFO_VPTR_FIELD (binfo))
7645 {
7646 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7647 fprintf (stream, " vbaseoffset=%s",
7648 expr_as_string (BINFO_VPTR_FIELD (binfo),
7649 TFF_PLAIN_IDENTIFIER));
7650 }
7651 if (BINFO_VTABLE (binfo))
7652 {
7653 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7654 fprintf (stream, " vptr=%s",
7655 expr_as_string (BINFO_VTABLE (binfo),
7656 TFF_PLAIN_IDENTIFIER));
7657 }
7658
7659 if (indented)
7660 fprintf (stream, "\n");
7661 }
7662
7663 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7664 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7665
7666 return igo;
7667 }
7668
7669 /* Dump the BINFO hierarchy for T. */
7670
7671 static void
7672 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7673 {
7674 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7675 fprintf (stream, " size=%lu align=%lu\n",
7676 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7677 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7678 fprintf (stream, " base size=%lu base align=%lu\n",
7679 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7680 / BITS_PER_UNIT),
7681 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7682 / BITS_PER_UNIT));
7683 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7684 fprintf (stream, "\n");
7685 }
7686
7687 /* Debug interface to hierarchy dumping. */
7688
7689 void
7690 debug_class (tree t)
7691 {
7692 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7693 }
7694
7695 static void
7696 dump_class_hierarchy (tree t)
7697 {
7698 int flags;
7699 FILE *stream = dump_begin (TDI_class, &flags);
7700
7701 if (stream)
7702 {
7703 dump_class_hierarchy_1 (stream, flags, t);
7704 dump_end (TDI_class, stream);
7705 }
7706 }
7707
7708 static void
7709 dump_array (FILE * stream, tree decl)
7710 {
7711 tree value;
7712 unsigned HOST_WIDE_INT ix;
7713 HOST_WIDE_INT elt;
7714 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7715
7716 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7717 / BITS_PER_UNIT);
7718 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7719 fprintf (stream, " %s entries",
7720 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7721 TFF_PLAIN_IDENTIFIER));
7722 fprintf (stream, "\n");
7723
7724 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7725 ix, value)
7726 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7727 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7728 }
7729
7730 static void
7731 dump_vtable (tree t, tree binfo, tree vtable)
7732 {
7733 int flags;
7734 FILE *stream = dump_begin (TDI_class, &flags);
7735
7736 if (!stream)
7737 return;
7738
7739 if (!(flags & TDF_SLIM))
7740 {
7741 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7742
7743 fprintf (stream, "%s for %s",
7744 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7745 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7746 if (ctor_vtbl_p)
7747 {
7748 if (!BINFO_VIRTUAL_P (binfo))
7749 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7750 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7751 }
7752 fprintf (stream, "\n");
7753 dump_array (stream, vtable);
7754 fprintf (stream, "\n");
7755 }
7756
7757 dump_end (TDI_class, stream);
7758 }
7759
7760 static void
7761 dump_vtt (tree t, tree vtt)
7762 {
7763 int flags;
7764 FILE *stream = dump_begin (TDI_class, &flags);
7765
7766 if (!stream)
7767 return;
7768
7769 if (!(flags & TDF_SLIM))
7770 {
7771 fprintf (stream, "VTT for %s\n",
7772 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7773 dump_array (stream, vtt);
7774 fprintf (stream, "\n");
7775 }
7776
7777 dump_end (TDI_class, stream);
7778 }
7779
7780 /* Dump a function or thunk and its thunkees. */
7781
7782 static void
7783 dump_thunk (FILE *stream, int indent, tree thunk)
7784 {
7785 static const char spaces[] = " ";
7786 tree name = DECL_NAME (thunk);
7787 tree thunks;
7788
7789 fprintf (stream, "%.*s%p %s %s", indent, spaces,
7790 (void *)thunk,
7791 !DECL_THUNK_P (thunk) ? "function"
7792 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7793 name ? IDENTIFIER_POINTER (name) : "<unset>");
7794 if (DECL_THUNK_P (thunk))
7795 {
7796 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7797 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7798
7799 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7800 if (!virtual_adjust)
7801 /*NOP*/;
7802 else if (DECL_THIS_THUNK_P (thunk))
7803 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
7804 tree_low_cst (virtual_adjust, 0));
7805 else
7806 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7807 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7808 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7809 if (THUNK_ALIAS (thunk))
7810 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7811 }
7812 fprintf (stream, "\n");
7813 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7814 dump_thunk (stream, indent + 2, thunks);
7815 }
7816
7817 /* Dump the thunks for FN. */
7818
7819 void
7820 debug_thunks (tree fn)
7821 {
7822 dump_thunk (stderr, 0, fn);
7823 }
7824
7825 /* Virtual function table initialization. */
7826
7827 /* Create all the necessary vtables for T and its base classes. */
7828
7829 static void
7830 finish_vtbls (tree t)
7831 {
7832 tree vbase;
7833 VEC(constructor_elt,gc) *v = NULL;
7834 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7835
7836 /* We lay out the primary and secondary vtables in one contiguous
7837 vtable. The primary vtable is first, followed by the non-virtual
7838 secondary vtables in inheritance graph order. */
7839 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7840 vtable, t, &v);
7841
7842 /* Then come the virtual bases, also in inheritance graph order. */
7843 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7844 {
7845 if (!BINFO_VIRTUAL_P (vbase))
7846 continue;
7847 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7848 }
7849
7850 if (BINFO_VTABLE (TYPE_BINFO (t)))
7851 initialize_vtable (TYPE_BINFO (t), v);
7852 }
7853
7854 /* Initialize the vtable for BINFO with the INITS. */
7855
7856 static void
7857 initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7858 {
7859 tree decl;
7860
7861 layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7862 decl = get_vtbl_decl_for_binfo (binfo);
7863 initialize_artificial_var (decl, inits);
7864 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7865 }
7866
7867 /* Build the VTT (virtual table table) for T.
7868 A class requires a VTT if it has virtual bases.
7869
7870 This holds
7871 1 - primary virtual pointer for complete object T
7872 2 - secondary VTTs for each direct non-virtual base of T which requires a
7873 VTT
7874 3 - secondary virtual pointers for each direct or indirect base of T which
7875 has virtual bases or is reachable via a virtual path from T.
7876 4 - secondary VTTs for each direct or indirect virtual base of T.
7877
7878 Secondary VTTs look like complete object VTTs without part 4. */
7879
7880 static void
7881 build_vtt (tree t)
7882 {
7883 tree type;
7884 tree vtt;
7885 tree index;
7886 VEC(constructor_elt,gc) *inits;
7887
7888 /* Build up the initializers for the VTT. */
7889 inits = NULL;
7890 index = size_zero_node;
7891 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7892
7893 /* If we didn't need a VTT, we're done. */
7894 if (!inits)
7895 return;
7896
7897 /* Figure out the type of the VTT. */
7898 type = build_array_of_n_type (const_ptr_type_node,
7899 VEC_length (constructor_elt, inits));
7900
7901 /* Now, build the VTT object itself. */
7902 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7903 initialize_artificial_var (vtt, inits);
7904 /* Add the VTT to the vtables list. */
7905 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
7906 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7907
7908 dump_vtt (t, vtt);
7909 }
7910
7911 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7912 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7913 and CHAIN the vtable pointer for this binfo after construction is
7914 complete. VALUE can also be another BINFO, in which case we recurse. */
7915
7916 static tree
7917 binfo_ctor_vtable (tree binfo)
7918 {
7919 tree vt;
7920
7921 while (1)
7922 {
7923 vt = BINFO_VTABLE (binfo);
7924 if (TREE_CODE (vt) == TREE_LIST)
7925 vt = TREE_VALUE (vt);
7926 if (TREE_CODE (vt) == TREE_BINFO)
7927 binfo = vt;
7928 else
7929 break;
7930 }
7931
7932 return vt;
7933 }
7934
7935 /* Data for secondary VTT initialization. */
7936 typedef struct secondary_vptr_vtt_init_data_s
7937 {
7938 /* Is this the primary VTT? */
7939 bool top_level_p;
7940
7941 /* Current index into the VTT. */
7942 tree index;
7943
7944 /* Vector of initializers built up. */
7945 VEC(constructor_elt,gc) *inits;
7946
7947 /* The type being constructed by this secondary VTT. */
7948 tree type_being_constructed;
7949 } secondary_vptr_vtt_init_data;
7950
7951 /* Recursively build the VTT-initializer for BINFO (which is in the
7952 hierarchy dominated by T). INITS points to the end of the initializer
7953 list to date. INDEX is the VTT index where the next element will be
7954 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7955 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7956 for virtual bases of T. When it is not so, we build the constructor
7957 vtables for the BINFO-in-T variant. */
7958
7959 static void
7960 build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
7961 {
7962 int i;
7963 tree b;
7964 tree init;
7965 secondary_vptr_vtt_init_data data;
7966 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7967
7968 /* We only need VTTs for subobjects with virtual bases. */
7969 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7970 return;
7971
7972 /* We need to use a construction vtable if this is not the primary
7973 VTT. */
7974 if (!top_level_p)
7975 {
7976 build_ctor_vtbl_group (binfo, t);
7977
7978 /* Record the offset in the VTT where this sub-VTT can be found. */
7979 BINFO_SUBVTT_INDEX (binfo) = *index;
7980 }
7981
7982 /* Add the address of the primary vtable for the complete object. */
7983 init = binfo_ctor_vtable (binfo);
7984 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7985 if (top_level_p)
7986 {
7987 gcc_assert (!BINFO_VPTR_INDEX (binfo));
7988 BINFO_VPTR_INDEX (binfo) = *index;
7989 }
7990 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7991
7992 /* Recursively add the secondary VTTs for non-virtual bases. */
7993 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
7994 if (!BINFO_VIRTUAL_P (b))
7995 build_vtt_inits (b, t, inits, index);
7996
7997 /* Add secondary virtual pointers for all subobjects of BINFO with
7998 either virtual bases or reachable along a virtual path, except
7999 subobjects that are non-virtual primary bases. */
8000 data.top_level_p = top_level_p;
8001 data.index = *index;
8002 data.inits = *inits;
8003 data.type_being_constructed = BINFO_TYPE (binfo);
8004
8005 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8006
8007 *index = data.index;
8008
8009 /* data.inits might have grown as we added secondary virtual pointers.
8010 Make sure our caller knows about the new vector. */
8011 *inits = data.inits;
8012
8013 if (top_level_p)
8014 /* Add the secondary VTTs for virtual bases in inheritance graph
8015 order. */
8016 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8017 {
8018 if (!BINFO_VIRTUAL_P (b))
8019 continue;
8020
8021 build_vtt_inits (b, t, inits, index);
8022 }
8023 else
8024 /* Remove the ctor vtables we created. */
8025 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8026 }
8027
8028 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8029 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8030
8031 static tree
8032 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8033 {
8034 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8035
8036 /* We don't care about bases that don't have vtables. */
8037 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8038 return dfs_skip_bases;
8039
8040 /* We're only interested in proper subobjects of the type being
8041 constructed. */
8042 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8043 return NULL_TREE;
8044
8045 /* We're only interested in bases with virtual bases or reachable
8046 via a virtual path from the type being constructed. */
8047 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8048 || binfo_via_virtual (binfo, data->type_being_constructed)))
8049 return dfs_skip_bases;
8050
8051 /* We're not interested in non-virtual primary bases. */
8052 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8053 return NULL_TREE;
8054
8055 /* Record the index where this secondary vptr can be found. */
8056 if (data->top_level_p)
8057 {
8058 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8059 BINFO_VPTR_INDEX (binfo) = data->index;
8060
8061 if (BINFO_VIRTUAL_P (binfo))
8062 {
8063 /* It's a primary virtual base, and this is not a
8064 construction vtable. Find the base this is primary of in
8065 the inheritance graph, and use that base's vtable
8066 now. */
8067 while (BINFO_PRIMARY_P (binfo))
8068 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8069 }
8070 }
8071
8072 /* Add the initializer for the secondary vptr itself. */
8073 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8074
8075 /* Advance the vtt index. */
8076 data->index = size_binop (PLUS_EXPR, data->index,
8077 TYPE_SIZE_UNIT (ptr_type_node));
8078
8079 return NULL_TREE;
8080 }
8081
8082 /* Called from build_vtt_inits via dfs_walk. After building
8083 constructor vtables and generating the sub-vtt from them, we need
8084 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8085 binfo of the base whose sub vtt was generated. */
8086
8087 static tree
8088 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8089 {
8090 tree vtable = BINFO_VTABLE (binfo);
8091
8092 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8093 /* If this class has no vtable, none of its bases do. */
8094 return dfs_skip_bases;
8095
8096 if (!vtable)
8097 /* This might be a primary base, so have no vtable in this
8098 hierarchy. */
8099 return NULL_TREE;
8100
8101 /* If we scribbled the construction vtable vptr into BINFO, clear it
8102 out now. */
8103 if (TREE_CODE (vtable) == TREE_LIST
8104 && (TREE_PURPOSE (vtable) == (tree) data))
8105 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8106
8107 return NULL_TREE;
8108 }
8109
8110 /* Build the construction vtable group for BINFO which is in the
8111 hierarchy dominated by T. */
8112
8113 static void
8114 build_ctor_vtbl_group (tree binfo, tree t)
8115 {
8116 tree type;
8117 tree vtbl;
8118 tree id;
8119 tree vbase;
8120 VEC(constructor_elt,gc) *v;
8121
8122 /* See if we've already created this construction vtable group. */
8123 id = mangle_ctor_vtbl_for_type (t, binfo);
8124 if (IDENTIFIER_GLOBAL_VALUE (id))
8125 return;
8126
8127 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8128 /* Build a version of VTBL (with the wrong type) for use in
8129 constructing the addresses of secondary vtables in the
8130 construction vtable group. */
8131 vtbl = build_vtable (t, id, ptr_type_node);
8132 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8133
8134 v = NULL;
8135 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8136 binfo, vtbl, t, &v);
8137
8138 /* Add the vtables for each of our virtual bases using the vbase in T
8139 binfo. */
8140 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8141 vbase;
8142 vbase = TREE_CHAIN (vbase))
8143 {
8144 tree b;
8145
8146 if (!BINFO_VIRTUAL_P (vbase))
8147 continue;
8148 b = copied_binfo (vbase, binfo);
8149
8150 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8151 }
8152
8153 /* Figure out the type of the construction vtable. */
8154 type = build_array_of_n_type (vtable_entry_type,
8155 VEC_length (constructor_elt, v));
8156 layout_type (type);
8157 TREE_TYPE (vtbl) = type;
8158 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8159 layout_decl (vtbl, 0);
8160
8161 /* Initialize the construction vtable. */
8162 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8163 initialize_artificial_var (vtbl, v);
8164 dump_vtable (t, binfo, vtbl);
8165 }
8166
8167 /* Add the vtbl initializers for BINFO (and its bases other than
8168 non-virtual primaries) to the list of INITS. BINFO is in the
8169 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8170 the constructor the vtbl inits should be accumulated for. (If this
8171 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8172 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8173 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8174 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8175 but are not necessarily the same in terms of layout. */
8176
8177 static void
8178 accumulate_vtbl_inits (tree binfo,
8179 tree orig_binfo,
8180 tree rtti_binfo,
8181 tree vtbl,
8182 tree t,
8183 VEC(constructor_elt,gc) **inits)
8184 {
8185 int i;
8186 tree base_binfo;
8187 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8188
8189 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8190
8191 /* If it doesn't have a vptr, we don't do anything. */
8192 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8193 return;
8194
8195 /* If we're building a construction vtable, we're not interested in
8196 subobjects that don't require construction vtables. */
8197 if (ctor_vtbl_p
8198 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8199 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8200 return;
8201
8202 /* Build the initializers for the BINFO-in-T vtable. */
8203 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8204
8205 /* Walk the BINFO and its bases. We walk in preorder so that as we
8206 initialize each vtable we can figure out at what offset the
8207 secondary vtable lies from the primary vtable. We can't use
8208 dfs_walk here because we need to iterate through bases of BINFO
8209 and RTTI_BINFO simultaneously. */
8210 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8211 {
8212 /* Skip virtual bases. */
8213 if (BINFO_VIRTUAL_P (base_binfo))
8214 continue;
8215 accumulate_vtbl_inits (base_binfo,
8216 BINFO_BASE_BINFO (orig_binfo, i),
8217 rtti_binfo, vtbl, t,
8218 inits);
8219 }
8220 }
8221
8222 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8223 BINFO vtable to L. */
8224
8225 static void
8226 dfs_accumulate_vtbl_inits (tree binfo,
8227 tree orig_binfo,
8228 tree rtti_binfo,
8229 tree orig_vtbl,
8230 tree t,
8231 VEC(constructor_elt,gc) **l)
8232 {
8233 tree vtbl = NULL_TREE;
8234 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8235 int n_inits;
8236
8237 if (ctor_vtbl_p
8238 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8239 {
8240 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8241 primary virtual base. If it is not the same primary in
8242 the hierarchy of T, we'll need to generate a ctor vtable
8243 for it, to place at its location in T. If it is the same
8244 primary, we still need a VTT entry for the vtable, but it
8245 should point to the ctor vtable for the base it is a
8246 primary for within the sub-hierarchy of RTTI_BINFO.
8247
8248 There are three possible cases:
8249
8250 1) We are in the same place.
8251 2) We are a primary base within a lost primary virtual base of
8252 RTTI_BINFO.
8253 3) We are primary to something not a base of RTTI_BINFO. */
8254
8255 tree b;
8256 tree last = NULL_TREE;
8257
8258 /* First, look through the bases we are primary to for RTTI_BINFO
8259 or a virtual base. */
8260 b = binfo;
8261 while (BINFO_PRIMARY_P (b))
8262 {
8263 b = BINFO_INHERITANCE_CHAIN (b);
8264 last = b;
8265 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8266 goto found;
8267 }
8268 /* If we run out of primary links, keep looking down our
8269 inheritance chain; we might be an indirect primary. */
8270 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8271 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8272 break;
8273 found:
8274
8275 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8276 base B and it is a base of RTTI_BINFO, this is case 2. In
8277 either case, we share our vtable with LAST, i.e. the
8278 derived-most base within B of which we are a primary. */
8279 if (b == rtti_binfo
8280 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8281 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8282 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8283 binfo_ctor_vtable after everything's been set up. */
8284 vtbl = last;
8285
8286 /* Otherwise, this is case 3 and we get our own. */
8287 }
8288 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8289 return;
8290
8291 n_inits = VEC_length (constructor_elt, *l);
8292
8293 if (!vtbl)
8294 {
8295 tree index;
8296 int non_fn_entries;
8297
8298 /* Add the initializer for this vtable. */
8299 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8300 &non_fn_entries, l);
8301
8302 /* Figure out the position to which the VPTR should point. */
8303 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8304 index = size_binop (MULT_EXPR,
8305 TYPE_SIZE_UNIT (vtable_entry_type),
8306 size_int (non_fn_entries + n_inits));
8307 vtbl = fold_build_pointer_plus (vtbl, index);
8308 }
8309
8310 if (ctor_vtbl_p)
8311 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8312 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8313 straighten this out. */
8314 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8315 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8316 /* Throw away any unneeded intializers. */
8317 VEC_truncate (constructor_elt, *l, n_inits);
8318 else
8319 /* For an ordinary vtable, set BINFO_VTABLE. */
8320 BINFO_VTABLE (binfo) = vtbl;
8321 }
8322
8323 static GTY(()) tree abort_fndecl_addr;
8324
8325 /* Construct the initializer for BINFO's virtual function table. BINFO
8326 is part of the hierarchy dominated by T. If we're building a
8327 construction vtable, the ORIG_BINFO is the binfo we should use to
8328 find the actual function pointers to put in the vtable - but they
8329 can be overridden on the path to most-derived in the graph that
8330 ORIG_BINFO belongs. Otherwise,
8331 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8332 BINFO that should be indicated by the RTTI information in the
8333 vtable; it will be a base class of T, rather than T itself, if we
8334 are building a construction vtable.
8335
8336 The value returned is a TREE_LIST suitable for wrapping in a
8337 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8338 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8339 number of non-function entries in the vtable.
8340
8341 It might seem that this function should never be called with a
8342 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8343 base is always subsumed by a derived class vtable. However, when
8344 we are building construction vtables, we do build vtables for
8345 primary bases; we need these while the primary base is being
8346 constructed. */
8347
8348 static void
8349 build_vtbl_initializer (tree binfo,
8350 tree orig_binfo,
8351 tree t,
8352 tree rtti_binfo,
8353 int* non_fn_entries_p,
8354 VEC(constructor_elt,gc) **inits)
8355 {
8356 tree v;
8357 vtbl_init_data vid;
8358 unsigned ix, jx;
8359 tree vbinfo;
8360 VEC(tree,gc) *vbases;
8361 constructor_elt *e;
8362
8363 /* Initialize VID. */
8364 memset (&vid, 0, sizeof (vid));
8365 vid.binfo = binfo;
8366 vid.derived = t;
8367 vid.rtti_binfo = rtti_binfo;
8368 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8369 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8370 vid.generate_vcall_entries = true;
8371 /* The first vbase or vcall offset is at index -3 in the vtable. */
8372 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8373
8374 /* Add entries to the vtable for RTTI. */
8375 build_rtti_vtbl_entries (binfo, &vid);
8376
8377 /* Create an array for keeping track of the functions we've
8378 processed. When we see multiple functions with the same
8379 signature, we share the vcall offsets. */
8380 vid.fns = VEC_alloc (tree, gc, 32);
8381 /* Add the vcall and vbase offset entries. */
8382 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8383
8384 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8385 build_vbase_offset_vtbl_entries. */
8386 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8387 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
8388 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8389
8390 /* If the target requires padding between data entries, add that now. */
8391 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8392 {
8393 int n_entries = VEC_length (constructor_elt, vid.inits);
8394
8395 VEC_safe_grow (constructor_elt, gc, vid.inits,
8396 TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8397
8398 /* Move data entries into their new positions and add padding
8399 after the new positions. Iterate backwards so we don't
8400 overwrite entries that we would need to process later. */
8401 for (ix = n_entries - 1;
8402 VEC_iterate (constructor_elt, vid.inits, ix, e);
8403 ix--)
8404 {
8405 int j;
8406 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8407 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8408
8409 VEC_replace (constructor_elt, vid.inits, new_position, e);
8410
8411 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8412 {
8413 constructor_elt *f = VEC_index (constructor_elt, vid.inits,
8414 new_position - j);
8415 f->index = NULL_TREE;
8416 f->value = build1 (NOP_EXPR, vtable_entry_type,
8417 null_pointer_node);
8418 }
8419 }
8420 }
8421
8422 if (non_fn_entries_p)
8423 *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
8424
8425 /* The initializers for virtual functions were built up in reverse
8426 order. Straighten them out and add them to the running list in one
8427 step. */
8428 jx = VEC_length (constructor_elt, *inits);
8429 VEC_safe_grow (constructor_elt, gc, *inits,
8430 (jx + VEC_length (constructor_elt, vid.inits)));
8431
8432 for (ix = VEC_length (constructor_elt, vid.inits) - 1;
8433 VEC_iterate (constructor_elt, vid.inits, ix, e);
8434 ix--, jx++)
8435 VEC_replace (constructor_elt, *inits, jx, e);
8436
8437 /* Go through all the ordinary virtual functions, building up
8438 initializers. */
8439 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8440 {
8441 tree delta;
8442 tree vcall_index;
8443 tree fn, fn_original;
8444 tree init = NULL_TREE;
8445
8446 fn = BV_FN (v);
8447 fn_original = fn;
8448 if (DECL_THUNK_P (fn))
8449 {
8450 if (!DECL_NAME (fn))
8451 finish_thunk (fn);
8452 if (THUNK_ALIAS (fn))
8453 {
8454 fn = THUNK_ALIAS (fn);
8455 BV_FN (v) = fn;
8456 }
8457 fn_original = THUNK_TARGET (fn);
8458 }
8459
8460 /* If the only definition of this function signature along our
8461 primary base chain is from a lost primary, this vtable slot will
8462 never be used, so just zero it out. This is important to avoid
8463 requiring extra thunks which cannot be generated with the function.
8464
8465 We first check this in update_vtable_entry_for_fn, so we handle
8466 restored primary bases properly; we also need to do it here so we
8467 zero out unused slots in ctor vtables, rather than filling them
8468 with erroneous values (though harmless, apart from relocation
8469 costs). */
8470 if (BV_LOST_PRIMARY (v))
8471 init = size_zero_node;
8472
8473 if (! init)
8474 {
8475 /* Pull the offset for `this', and the function to call, out of
8476 the list. */
8477 delta = BV_DELTA (v);
8478 vcall_index = BV_VCALL_INDEX (v);
8479
8480 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8481 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8482
8483 /* You can't call an abstract virtual function; it's abstract.
8484 So, we replace these functions with __pure_virtual. */
8485 if (DECL_PURE_VIRTUAL_P (fn_original))
8486 {
8487 fn = abort_fndecl;
8488 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8489 {
8490 if (abort_fndecl_addr == NULL)
8491 abort_fndecl_addr
8492 = fold_convert (vfunc_ptr_type_node,
8493 build_fold_addr_expr (fn));
8494 init = abort_fndecl_addr;
8495 }
8496 }
8497 /* Likewise for deleted virtuals. */
8498 else if (DECL_DELETED_FN (fn_original))
8499 {
8500 fn = get_identifier ("__cxa_deleted_virtual");
8501 if (!get_global_value_if_present (fn, &fn))
8502 fn = push_library_fn (fn, (build_function_type_list
8503 (void_type_node, NULL_TREE)),
8504 NULL_TREE);
8505 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8506 init = fold_convert (vfunc_ptr_type_node,
8507 build_fold_addr_expr (fn));
8508 }
8509 else
8510 {
8511 if (!integer_zerop (delta) || vcall_index)
8512 {
8513 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8514 if (!DECL_NAME (fn))
8515 finish_thunk (fn);
8516 }
8517 /* Take the address of the function, considering it to be of an
8518 appropriate generic type. */
8519 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8520 init = fold_convert (vfunc_ptr_type_node,
8521 build_fold_addr_expr (fn));
8522 }
8523 }
8524
8525 /* And add it to the chain of initializers. */
8526 if (TARGET_VTABLE_USES_DESCRIPTORS)
8527 {
8528 int i;
8529 if (init == size_zero_node)
8530 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8531 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8532 else
8533 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8534 {
8535 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8536 fn, build_int_cst (NULL_TREE, i));
8537 TREE_CONSTANT (fdesc) = 1;
8538
8539 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8540 }
8541 }
8542 else
8543 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8544 }
8545 }
8546
8547 /* Adds to vid->inits the initializers for the vbase and vcall
8548 offsets in BINFO, which is in the hierarchy dominated by T. */
8549
8550 static void
8551 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8552 {
8553 tree b;
8554
8555 /* If this is a derived class, we must first create entries
8556 corresponding to the primary base class. */
8557 b = get_primary_binfo (binfo);
8558 if (b)
8559 build_vcall_and_vbase_vtbl_entries (b, vid);
8560
8561 /* Add the vbase entries for this base. */
8562 build_vbase_offset_vtbl_entries (binfo, vid);
8563 /* Add the vcall entries for this base. */
8564 build_vcall_offset_vtbl_entries (binfo, vid);
8565 }
8566
8567 /* Returns the initializers for the vbase offset entries in the vtable
8568 for BINFO (which is part of the class hierarchy dominated by T), in
8569 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8570 where the next vbase offset will go. */
8571
8572 static void
8573 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8574 {
8575 tree vbase;
8576 tree t;
8577 tree non_primary_binfo;
8578
8579 /* If there are no virtual baseclasses, then there is nothing to
8580 do. */
8581 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8582 return;
8583
8584 t = vid->derived;
8585
8586 /* We might be a primary base class. Go up the inheritance hierarchy
8587 until we find the most derived class of which we are a primary base:
8588 it is the offset of that which we need to use. */
8589 non_primary_binfo = binfo;
8590 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8591 {
8592 tree b;
8593
8594 /* If we have reached a virtual base, then it must be a primary
8595 base (possibly multi-level) of vid->binfo, or we wouldn't
8596 have called build_vcall_and_vbase_vtbl_entries for it. But it
8597 might be a lost primary, so just skip down to vid->binfo. */
8598 if (BINFO_VIRTUAL_P (non_primary_binfo))
8599 {
8600 non_primary_binfo = vid->binfo;
8601 break;
8602 }
8603
8604 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8605 if (get_primary_binfo (b) != non_primary_binfo)
8606 break;
8607 non_primary_binfo = b;
8608 }
8609
8610 /* Go through the virtual bases, adding the offsets. */
8611 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8612 vbase;
8613 vbase = TREE_CHAIN (vbase))
8614 {
8615 tree b;
8616 tree delta;
8617
8618 if (!BINFO_VIRTUAL_P (vbase))
8619 continue;
8620
8621 /* Find the instance of this virtual base in the complete
8622 object. */
8623 b = copied_binfo (vbase, binfo);
8624
8625 /* If we've already got an offset for this virtual base, we
8626 don't need another one. */
8627 if (BINFO_VTABLE_PATH_MARKED (b))
8628 continue;
8629 BINFO_VTABLE_PATH_MARKED (b) = 1;
8630
8631 /* Figure out where we can find this vbase offset. */
8632 delta = size_binop (MULT_EXPR,
8633 vid->index,
8634 convert (ssizetype,
8635 TYPE_SIZE_UNIT (vtable_entry_type)));
8636 if (vid->primary_vtbl_p)
8637 BINFO_VPTR_FIELD (b) = delta;
8638
8639 if (binfo != TYPE_BINFO (t))
8640 /* The vbase offset had better be the same. */
8641 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8642
8643 /* The next vbase will come at a more negative offset. */
8644 vid->index = size_binop (MINUS_EXPR, vid->index,
8645 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8646
8647 /* The initializer is the delta from BINFO to this virtual base.
8648 The vbase offsets go in reverse inheritance-graph order, and
8649 we are walking in inheritance graph order so these end up in
8650 the right order. */
8651 delta = size_diffop_loc (input_location,
8652 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8653
8654 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8655 fold_build1_loc (input_location, NOP_EXPR,
8656 vtable_entry_type, delta));
8657 }
8658 }
8659
8660 /* Adds the initializers for the vcall offset entries in the vtable
8661 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8662 to VID->INITS. */
8663
8664 static void
8665 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8666 {
8667 /* We only need these entries if this base is a virtual base. We
8668 compute the indices -- but do not add to the vtable -- when
8669 building the main vtable for a class. */
8670 if (binfo == TYPE_BINFO (vid->derived)
8671 || (BINFO_VIRTUAL_P (binfo)
8672 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8673 correspond to VID->DERIVED), we are building a primary
8674 construction virtual table. Since this is a primary
8675 virtual table, we do not need the vcall offsets for
8676 BINFO. */
8677 && binfo != vid->rtti_binfo))
8678 {
8679 /* We need a vcall offset for each of the virtual functions in this
8680 vtable. For example:
8681
8682 class A { virtual void f (); };
8683 class B1 : virtual public A { virtual void f (); };
8684 class B2 : virtual public A { virtual void f (); };
8685 class C: public B1, public B2 { virtual void f (); };
8686
8687 A C object has a primary base of B1, which has a primary base of A. A
8688 C also has a secondary base of B2, which no longer has a primary base
8689 of A. So the B2-in-C construction vtable needs a secondary vtable for
8690 A, which will adjust the A* to a B2* to call f. We have no way of
8691 knowing what (or even whether) this offset will be when we define B2,
8692 so we store this "vcall offset" in the A sub-vtable and look it up in
8693 a "virtual thunk" for B2::f.
8694
8695 We need entries for all the functions in our primary vtable and
8696 in our non-virtual bases' secondary vtables. */
8697 vid->vbase = binfo;
8698 /* If we are just computing the vcall indices -- but do not need
8699 the actual entries -- not that. */
8700 if (!BINFO_VIRTUAL_P (binfo))
8701 vid->generate_vcall_entries = false;
8702 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8703 add_vcall_offset_vtbl_entries_r (binfo, vid);
8704 }
8705 }
8706
8707 /* Build vcall offsets, starting with those for BINFO. */
8708
8709 static void
8710 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8711 {
8712 int i;
8713 tree primary_binfo;
8714 tree base_binfo;
8715
8716 /* Don't walk into virtual bases -- except, of course, for the
8717 virtual base for which we are building vcall offsets. Any
8718 primary virtual base will have already had its offsets generated
8719 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8720 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8721 return;
8722
8723 /* If BINFO has a primary base, process it first. */
8724 primary_binfo = get_primary_binfo (binfo);
8725 if (primary_binfo)
8726 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8727
8728 /* Add BINFO itself to the list. */
8729 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8730
8731 /* Scan the non-primary bases of BINFO. */
8732 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8733 if (base_binfo != primary_binfo)
8734 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8735 }
8736
8737 /* Called from build_vcall_offset_vtbl_entries_r. */
8738
8739 static void
8740 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8741 {
8742 /* Make entries for the rest of the virtuals. */
8743 if (abi_version_at_least (2))
8744 {
8745 tree orig_fn;
8746
8747 /* The ABI requires that the methods be processed in declaration
8748 order. G++ 3.2 used the order in the vtable. */
8749 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8750 orig_fn;
8751 orig_fn = DECL_CHAIN (orig_fn))
8752 if (DECL_VINDEX (orig_fn))
8753 add_vcall_offset (orig_fn, binfo, vid);
8754 }
8755 else
8756 {
8757 tree derived_virtuals;
8758 tree base_virtuals;
8759 tree orig_virtuals;
8760 /* If BINFO is a primary base, the most derived class which has
8761 BINFO as a primary base; otherwise, just BINFO. */
8762 tree non_primary_binfo;
8763
8764 /* We might be a primary base class. Go up the inheritance hierarchy
8765 until we find the most derived class of which we are a primary base:
8766 it is the BINFO_VIRTUALS there that we need to consider. */
8767 non_primary_binfo = binfo;
8768 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8769 {
8770 tree b;
8771
8772 /* If we have reached a virtual base, then it must be vid->vbase,
8773 because we ignore other virtual bases in
8774 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8775 base (possibly multi-level) of vid->binfo, or we wouldn't
8776 have called build_vcall_and_vbase_vtbl_entries for it. But it
8777 might be a lost primary, so just skip down to vid->binfo. */
8778 if (BINFO_VIRTUAL_P (non_primary_binfo))
8779 {
8780 gcc_assert (non_primary_binfo == vid->vbase);
8781 non_primary_binfo = vid->binfo;
8782 break;
8783 }
8784
8785 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8786 if (get_primary_binfo (b) != non_primary_binfo)
8787 break;
8788 non_primary_binfo = b;
8789 }
8790
8791 if (vid->ctor_vtbl_p)
8792 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8793 where rtti_binfo is the most derived type. */
8794 non_primary_binfo
8795 = original_binfo (non_primary_binfo, vid->rtti_binfo);
8796
8797 for (base_virtuals = BINFO_VIRTUALS (binfo),
8798 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8799 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8800 base_virtuals;
8801 base_virtuals = TREE_CHAIN (base_virtuals),
8802 derived_virtuals = TREE_CHAIN (derived_virtuals),
8803 orig_virtuals = TREE_CHAIN (orig_virtuals))
8804 {
8805 tree orig_fn;
8806
8807 /* Find the declaration that originally caused this function to
8808 be present in BINFO_TYPE (binfo). */
8809 orig_fn = BV_FN (orig_virtuals);
8810
8811 /* When processing BINFO, we only want to generate vcall slots for
8812 function slots introduced in BINFO. So don't try to generate
8813 one if the function isn't even defined in BINFO. */
8814 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8815 continue;
8816
8817 add_vcall_offset (orig_fn, binfo, vid);
8818 }
8819 }
8820 }
8821
8822 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8823
8824 static void
8825 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8826 {
8827 size_t i;
8828 tree vcall_offset;
8829 tree derived_entry;
8830
8831 /* If there is already an entry for a function with the same
8832 signature as FN, then we do not need a second vcall offset.
8833 Check the list of functions already present in the derived
8834 class vtable. */
8835 FOR_EACH_VEC_ELT (tree, vid->fns, i, derived_entry)
8836 {
8837 if (same_signature_p (derived_entry, orig_fn)
8838 /* We only use one vcall offset for virtual destructors,
8839 even though there are two virtual table entries. */
8840 || (DECL_DESTRUCTOR_P (derived_entry)
8841 && DECL_DESTRUCTOR_P (orig_fn)))
8842 return;
8843 }
8844
8845 /* If we are building these vcall offsets as part of building
8846 the vtable for the most derived class, remember the vcall
8847 offset. */
8848 if (vid->binfo == TYPE_BINFO (vid->derived))
8849 {
8850 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
8851 CLASSTYPE_VCALL_INDICES (vid->derived),
8852 NULL);
8853 elt->purpose = orig_fn;
8854 elt->value = vid->index;
8855 }
8856
8857 /* The next vcall offset will be found at a more negative
8858 offset. */
8859 vid->index = size_binop (MINUS_EXPR, vid->index,
8860 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8861
8862 /* Keep track of this function. */
8863 VEC_safe_push (tree, gc, vid->fns, orig_fn);
8864
8865 if (vid->generate_vcall_entries)
8866 {
8867 tree base;
8868 tree fn;
8869
8870 /* Find the overriding function. */
8871 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8872 if (fn == error_mark_node)
8873 vcall_offset = build_zero_cst (vtable_entry_type);
8874 else
8875 {
8876 base = TREE_VALUE (fn);
8877
8878 /* The vbase we're working on is a primary base of
8879 vid->binfo. But it might be a lost primary, so its
8880 BINFO_OFFSET might be wrong, so we just use the
8881 BINFO_OFFSET from vid->binfo. */
8882 vcall_offset = size_diffop_loc (input_location,
8883 BINFO_OFFSET (base),
8884 BINFO_OFFSET (vid->binfo));
8885 vcall_offset = fold_build1_loc (input_location,
8886 NOP_EXPR, vtable_entry_type,
8887 vcall_offset);
8888 }
8889 /* Add the initializer to the vtable. */
8890 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8891 }
8892 }
8893
8894 /* Return vtbl initializers for the RTTI entries corresponding to the
8895 BINFO's vtable. The RTTI entries should indicate the object given
8896 by VID->rtti_binfo. */
8897
8898 static void
8899 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8900 {
8901 tree b;
8902 tree t;
8903 tree offset;
8904 tree decl;
8905 tree init;
8906
8907 t = BINFO_TYPE (vid->rtti_binfo);
8908
8909 /* To find the complete object, we will first convert to our most
8910 primary base, and then add the offset in the vtbl to that value. */
8911 b = binfo;
8912 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8913 && !BINFO_LOST_PRIMARY_P (b))
8914 {
8915 tree primary_base;
8916
8917 primary_base = get_primary_binfo (b);
8918 gcc_assert (BINFO_PRIMARY_P (primary_base)
8919 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8920 b = primary_base;
8921 }
8922 offset = size_diffop_loc (input_location,
8923 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8924
8925 /* The second entry is the address of the typeinfo object. */
8926 if (flag_rtti)
8927 decl = build_address (get_tinfo_decl (t));
8928 else
8929 decl = integer_zero_node;
8930
8931 /* Convert the declaration to a type that can be stored in the
8932 vtable. */
8933 init = build_nop (vfunc_ptr_type_node, decl);
8934 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8935
8936 /* Add the offset-to-top entry. It comes earlier in the vtable than
8937 the typeinfo entry. Convert the offset to look like a
8938 function pointer, so that we can put it in the vtable. */
8939 init = build_nop (vfunc_ptr_type_node, offset);
8940 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8941 }
8942
8943 #include "gt-cp-class.h"