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