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