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