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