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