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