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