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