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