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