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